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) 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 ;;; The DEFSTRUCT-DESCRIPTION structure holds compile-time information about a
47 (def!struct (defstruct-description
49 (:make-load-form-fun just-dump-it-normally)
50 #-sb-xc-host (:pure t)
51 (:constructor make-defstruct-description (name)))
52 ;; name of the structure
53 (name (required-argument) :type symbol)
54 ;; documentation on the structure
55 (doc nil :type (or string null))
56 ;; prefix for slot names. If NIL, none.
57 (conc-name (symbolicate name "-") :type (or symbol null))
58 ;; the name of the primary standard keyword constructor, or NIL if none
59 (default-constructor nil :type (or symbol null))
60 ;; all the explicit :CONSTRUCTOR specs, with name defaulted
61 (constructors () :type list)
62 ;; name of copying function
63 (copier (symbolicate "COPY-" name) :type (or symbol null))
64 ;; name of type predicate
65 (predicate-name (symbolicate name "-P") :type (or symbol null))
66 ;; the arguments to the :INCLUDE option, or NIL if no included
68 (include nil :type list)
69 ;; The arguments to the :ALTERNATE-METACLASS option (an extension
70 ;; used to define structure-like objects with an arbitrary
71 ;; superclass and that may not have STRUCTURE-CLASS as the
72 ;; metaclass.) Syntax is:
73 ;; (superclass-name metaclass-name metaclass-constructor)
74 (alternate-metaclass nil :type list)
75 ;; a list of DEFSTRUCT-SLOT-DESCRIPTION objects for all slots
76 ;; (including included ones)
78 ;; number of elements we've allocated (See also RAW-LENGTH.)
79 (length 0 :type index)
80 ;; General kind of implementation.
81 (type 'structure :type (member structure vector list
82 funcallable-structure))
84 ;; The next three slots are for :TYPE'd structures (which aren't
85 ;; classes, DD-CLASS-P = NIL)
87 ;; vector element type
89 ;; T if :NAMED was explicitly specified, NIL otherwise
90 (named nil :type boolean)
91 ;; any INITIAL-OFFSET option on this direct type
92 (offset nil :type (or index null))
94 ;; the argument to the PRINT-FUNCTION option, or NIL if a
95 ;; PRINT-FUNCTION option was given with no argument, or 0 if no
96 ;; PRINT-FUNCTION option was given
97 (print-function 0 :type (or cons symbol (member 0)))
98 ;; the argument to the PRINT-OBJECT option, or NIL if a PRINT-OBJECT
99 ;; option was given with no argument, or 0 if no PRINT-OBJECT option
101 (print-object 0 :type (or cons symbol (member 0)))
102 ;; the index of the raw data vector and the number of words in it.
103 ;; NIL and 0 if not allocated yet.
104 (raw-index nil :type (or index null))
105 (raw-length 0 :type index)
106 ;; the value of the :PURE option, or :UNSPECIFIED. This is only
107 ;; meaningful if DD-CLASS-P = T.
108 (pure :unspecified :type (member t nil :substructure :unspecified)))
109 (def!method print-object ((x defstruct-description) stream)
110 (print-unreadable-object (x stream :type t)
111 (prin1 (dd-name x) stream)))
113 ;;; A DEFSTRUCT-SLOT-DESCRIPTION holds compile-time information about
114 ;;; a structure slot.
115 (def!struct (defstruct-slot-description
116 (:make-load-form-fun just-dump-it-normally)
119 #-sb-xc-host (:pure t))
120 ;; string name of slot
122 ;; its position in the implementation sequence
123 (index (required-argument) :type fixnum)
124 ;; the name of the accessor function
126 ;; (CMU CL had extra complexity here ("..or NIL if this accessor has
127 ;; the same name as an inherited accessor (which we don't want to
128 ;; shadow)") but that behavior doesn't seem to be specified by (or
129 ;; even particularly consistent with) ANSI, so it's gone in SBCL.)
131 default ; default value expression
132 (type t) ; declared type specifier
133 ;; If this object does not describe a raw slot, this value is T.
135 ;; If this object describes a raw slot, this value is the type of the
136 ;; value that the raw slot holds. Mostly. (KLUDGE: If the raw slot has
137 ;; type (UNSIGNED-BYTE 32), the value here is UNSIGNED-BYTE, not
138 ;; (UNSIGNED-BYTE 32).)
139 (raw-type t :type (member t single-float double-float
140 #!+long-float long-float
141 complex-single-float complex-double-float
142 #!+long-float complex-long-float
144 (read-only nil :type (member t nil)))
145 (def!method print-object ((x defstruct-slot-description) stream)
146 (print-unreadable-object (x stream :type t)
147 (prin1 (dsd-name x) stream)))
149 ;;; Is DEFSTRUCT a structure with a class?
150 (defun dd-class-p (defstruct)
151 (member (dd-type defstruct) '(structure funcallable-structure)))
153 ;;; Return the name of a defstruct slot as a symbol. We store it as a
154 ;;; string to avoid creating lots of worthless symbols at load time.
155 (defun dsd-name (dsd)
156 (intern (string (dsd-%name dsd))
157 (if (dsd-accessor-name dsd)
158 (symbol-package (dsd-accessor-name dsd))
161 ;;;; typed (non-class) structures
163 ;;; Return a type specifier we can use for testing :TYPE'd structures.
164 (defun dd-lisp-type (defstruct)
165 (ecase (dd-type defstruct)
167 (vector `(simple-array ,(dd-element-type defstruct) (*)))))
169 ;;;; the legendary DEFSTRUCT macro itself (both CL:DEFSTRUCT and its
170 ;;;; close personal friend SB!XC:DEFSTRUCT)
172 ;;; Return a list of forms to install PRINT and MAKE-LOAD-FORM funs,
173 ;;; mentioning them in the expansion so that they can be compiled.
174 (defun class-method-definitions (defstruct)
175 (let ((name (dd-name defstruct)))
177 ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant
178 ;; class names which creates fast but non-cold-loadable,
179 ;; non-compact code. In this context, we'd rather have
180 ;; compact, cold-loadable code. -- WHN 19990928
181 (declare (notinline sb!xc:find-class))
182 ,@(let ((pf (dd-print-function defstruct))
183 (po (dd-print-object defstruct))
186 ;; Giving empty :PRINT-OBJECT or :PRINT-FUNCTION options
187 ;; leaves PO or PF equal to NIL. The user-level effect is
188 ;; to generate a PRINT-OBJECT method specialized for the type,
189 ;; implementing the default #S structure-printing behavior.
190 (when (or (eq pf nil) (eq po nil))
191 (setf pf '(default-structure-print)
193 (flet (;; Given an arg from a :PRINT-OBJECT or :PRINT-FUNCTION
194 ;; option, return the value to pass as an arg to FUNCTION.
196 (destructuring-bind (fun-name) oarg
198 (cond ((not (eql pf 0))
199 `((def!method print-object ((,x ,name) ,s)
200 (funcall #',(farg pf) ,x ,s *current-level*))))
202 `((def!method print-object ((,x ,name) ,s)
203 (funcall #',(farg po) ,x ,s))))
205 ,@(let ((pure (dd-pure defstruct)))
207 `((setf (layout-pure (class-layout
208 (sb!xc:find-class ',name)))
210 ((eq pure :substructure)
211 `((setf (layout-pure (class-layout
212 (sb!xc:find-class ',name)))
214 ,@(let ((def-con (dd-default-constructor defstruct)))
215 (when (and def-con (not (dd-alternate-metaclass defstruct)))
216 `((setf (structure-class-constructor (sb!xc:find-class ',name))
218 ;;; FIXME: I really would like to make structure accessors less
219 ;;; special, just ordinary inline functions. (Or perhaps inline
220 ;;; functions with special compact implementations of their
221 ;;; expansions, to avoid bloating the system.)
223 ;;; shared logic for CL:DEFSTRUCT and SB!XC:DEFSTRUCT
224 (defmacro !expander-for-defstruct (name-and-options
226 expanding-into-code-for-xc-host-p)
227 `(let ((name-and-options ,name-and-options)
228 (slot-descriptions ,slot-descriptions)
229 (expanding-into-code-for-xc-host-p
230 ,expanding-into-code-for-xc-host-p))
231 (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
236 (let ((inherits (inherits-for-structure dd)))
238 (eval-when (:compile-toplevel :load-toplevel :execute)
239 (%compiler-defstruct ',dd ',inherits))
240 (%defstruct ',dd ',inherits)
241 ,@(unless expanding-into-code-for-xc-host-p
242 (append (raw-accessor-definitions dd)
243 (predicate-definitions dd)
244 ;; FIXME: We've inherited from CMU CL nonparallel
245 ;; code for creating copiers for typed and untyped
246 ;; structures. This should be fixed.
247 ;(copier-definition dd)
248 (constructor-definitions dd)
249 (class-method-definitions dd)))
252 (eval-when (:compile-toplevel :load-toplevel :execute)
253 (setf (info :typed-structure :info ',name) ',dd))
254 ,@(unless expanding-into-code-for-xc-host-p
255 (append (typed-accessor-definitions dd)
256 (typed-predicate-definitions dd)
257 (typed-copier-definitions dd)
258 (constructor-definitions dd)))
261 (sb!xc:defmacro defstruct (name-and-options &rest slot-descriptions)
263 "DEFSTRUCT {Name | (Name Option*)} {Slot | (Slot [Default] {Key Value}*)}
264 Define the structure type Name. Instances are created by MAKE-<name>,
265 which takes &KEY arguments allowing initial slot values to the specified.
266 A SETF'able function <name>-<slot> is defined for each slot to read and
267 write slot values. <name>-p is a type predicate.
269 Popular DEFSTRUCT options (see manual for others):
273 Specify the name for the constructor or predicate.
275 (:CONSTRUCTOR Name Lambda-List)
276 Specify the name and arguments for a BOA constructor
277 (which is more efficient when keyword syntax isn't necessary.)
279 (:INCLUDE Supertype Slot-Spec*)
280 Make this type a subtype of the structure type Supertype. The optional
281 Slot-Specs override inherited slot options.
286 Asserts that the value of this slot is always of the specified type.
289 If true, no setter function is defined for this slot."
290 (!expander-for-defstruct name-and-options slot-descriptions nil))
292 (defmacro sb!xc:defstruct (name-and-options &rest slot-descriptions)
294 "Cause information about a target structure to be built into the
296 (!expander-for-defstruct name-and-options slot-descriptions t))
298 ;;;; functions to generate code for various parts of DEFSTRUCT definitions
300 ;;; Catch requests to mess up definitions in COMMON-LISP.
302 (eval-when (:compile-toplevel :load-toplevel :execute)
303 (defun protect-cl (symbol)
304 (when (and *cold-init-complete-p*
305 (eq (symbol-package symbol) *cl-package*))
306 (cerror "Go ahead and patch the system."
307 "attempting to modify a symbol in the COMMON-LISP package: ~S"
310 ;;; Return forms to define readers and writers for raw slots as inline
312 (defun raw-accessor-definitions (dd)
313 (let* ((name (dd-name dd)))
315 (dolist (slot (dd-slots dd))
316 (let ((slot-type (dsd-type slot))
317 (accessor-name (dsd-accessor-name slot))
318 (argname (gensym "ARG"))
319 (nvname (gensym "NEW-VALUE-")))
320 (multiple-value-bind (accessor offset data)
321 (slot-accessor-form dd slot argname)
322 ;; When accessor exists and is raw
323 (when (and accessor-name
324 (not (eq accessor-name '%instance-ref)))
325 (res `(declaim (inline ,accessor-name)))
326 (res `(declaim (ftype (function (,name) ,slot-type)
328 (res `(defun ,accessor-name (,argname)
329 ;; Note: The DECLARE here might seem redundant
330 ;; with the DECLAIM FTYPE above, but it's not:
331 ;; If we're not at toplevel, the PROCLAIM inside
332 ;; the DECLAIM doesn't get executed until after
333 ;; this function is compiled.
334 (declare (type ,name ,argname))
335 (truly-the ,slot-type (,accessor ,data ,offset))))
336 (unless (dsd-read-only slot)
337 (res `(declaim (inline (setf ,accessor-name))))
338 (res `(declaim (ftype (function (,slot-type ,name) ,slot-type)
339 (setf ,accessor-name))))
340 ;; FIXME: I rewrote this somewhat from the CMU CL definition.
341 ;; Do some basic tests to make sure that reading and writing
342 ;; raw slots still works correctly.
343 (res `(defun (setf ,accessor-name) (,nvname ,argname)
344 (declare (type ,name ,argname))
345 (setf (,accessor ,data ,offset) ,nvname)
349 ;;; Return a list of forms which create a predicate for an untyped DEFSTRUCT.
350 (defun predicate-definitions (dd)
351 (let ((pred (dd-predicate-name dd))
354 (if (eq (dd-type dd) 'funcallable-structure)
355 ;; FIXME: Why does this need to be special-cased for
356 ;; FUNCALLABLE-STRUCTURE? CMU CL did it, but without explanation.
357 ;; Could we do without it? What breaks if we do? Or could we
358 ;; perhaps get by with no predicates for funcallable structures?
359 `((declaim (inline ,pred))
360 (defun ,pred (,argname) (typep ,argname ',(dd-name dd))))
361 `((protect-cl ',pred)
362 (declaim (inline ,pred))
363 (defun ,pred (,argname)
364 (declare (optimize (speed 3) (safety 0)))
365 (typep-to-layout ,argname
366 (compile-time-find-layout ,(dd-name dd)))))))))
368 ;;; Return a list of forms which create a predicate function for a typed
370 (defun typed-predicate-definitions (defstruct)
371 (let ((name (dd-name defstruct))
372 (predicate-name (dd-predicate-name defstruct))
374 (when (and predicate-name (dd-named defstruct))
375 (let ((ltype (dd-lisp-type defstruct)))
376 `((defun ,predicate-name (,argname)
377 (and (typep ,argname ',ltype)
378 (eq (elt (the ,ltype ,argname)
379 ,(cdr (car (last (find-name-indices defstruct)))))
382 ;;; FIXME: We've inherited from CMU CL code to do typed structure copiers
383 ;;; in a completely different way than untyped structure copiers. Fix this.
384 ;;; (This function was my first attempt to fix this, but I stopped before
385 ;;; figuring out how to install it completely and remove the parallel
386 ;;; code which simply SETF's the FDEFINITION of the DD-COPIER name.
388 ;;; Return the copier definition for an untyped DEFSTRUCT.
389 (defun copier-definition (dd)
390 (when (and (dd-copier dd)
391 ;; FUNCALLABLE-STRUCTUREs don't need copiers, and this
392 ;; implementation wouldn't work for them anyway, since
393 ;; COPY-STRUCTURE returns a STRUCTURE-OBJECT and they're not.
394 (not (eq (dd-type info) 'funcallable-structure)))
395 (let ((argname (gensym)))
397 (protect-cl ',(dd-copier dd))
398 (defun ,(dd-copier dd) (,argname)
399 (declare (type ,(dd-name dd) ,argname))
400 (copy-structure ,argname))))))
403 ;;; Return a list of forms to create a copier function of a typed DEFSTRUCT.
404 (defun typed-copier-definitions (defstruct)
405 (when (dd-copier defstruct)
406 `((setf (fdefinition ',(dd-copier defstruct)) #'copy-seq)
407 (declaim (ftype function ,(dd-copier defstruct))))))
409 ;;; Return a list of function definitions for accessing and setting the
410 ;;; slots of a typed DEFSTRUCT. The functions are proclaimed to be inline,
411 ;;; and the types of their arguments and results are declared as well. We
412 ;;; count on the compiler to do clever things with ELT.
413 (defun typed-accessor-definitions (defstruct)
415 (let ((ltype (dd-lisp-type defstruct)))
416 (dolist (slot (dd-slots defstruct))
417 (let ((name (dsd-accessor-name slot))
418 (index (dsd-index slot))
419 (slot-type `(and ,(dsd-type slot)
420 ,(dd-element-type defstruct))))
421 (stuff `(proclaim '(inline ,name (setf ,name))))
422 ;; FIXME: The arguments in the next two DEFUNs should be
423 ;; gensyms. (Otherwise e.g. if NEW-VALUE happened to be the
424 ;; name of a special variable, things could get weird.)
425 (stuff `(defun ,name (structure)
426 (declare (type ,ltype structure))
427 (the ,slot-type (elt structure ,index))))
428 (unless (dsd-read-only slot)
430 `(defun (setf ,name) (new-value structure)
431 (declare (type ,ltype structure) (type ,slot-type new-value))
432 (setf (elt structure ,index) new-value)))))))
437 (defun require-no-print-options-so-far (defstruct)
438 (unless (and (eql (dd-print-function defstruct) 0)
439 (eql (dd-print-object defstruct) 0))
440 (error "No more than one of the following options may be specified:
441 :PRINT-FUNCTION, :PRINT-OBJECT, :TYPE")))
443 ;;; Parse a single DEFSTRUCT option and store the results in DD.
444 (defun parse-1-dd-option (option dd)
445 (let ((args (rest option))
449 (destructuring-bind (conc-name) args
450 (setf (dd-conc-name dd)
451 (if (symbolp conc-name)
453 (make-symbol (string conc-name))))))
455 (destructuring-bind (&optional (cname (symbolicate "MAKE-" name))
458 (push (cons cname stuff) (dd-constructors dd))))
460 (destructuring-bind (&optional (copier (symbolicate "COPY-" name)))
462 (setf (dd-copier dd) copier)))
464 (destructuring-bind (&optional (predicate-name (symbolicate name "-P")))
466 (setf (dd-predicate-name dd) predicate-name)))
468 (when (dd-include dd)
469 (error "more than one :INCLUDE option"))
470 (setf (dd-include dd) args))
471 (:alternate-metaclass
472 (setf (dd-alternate-metaclass dd) args))
474 (require-no-print-options-so-far dd)
475 (setf (dd-print-function dd)
476 (the (or symbol cons) args)))
478 (require-no-print-options-so-far dd)
479 (setf (dd-print-object dd)
480 (the (or symbol cons) args)))
482 (destructuring-bind (type) args
483 (cond ((eq type 'funcallable-structure)
484 (setf (dd-type dd) type))
485 ((member type '(list vector))
486 (setf (dd-element-type dd) t)
487 (setf (dd-type dd) type))
488 ((and (consp type) (eq (first type) 'vector))
489 (destructuring-bind (vector vtype) type
490 (declare (ignore vector))
491 (setf (dd-element-type dd) vtype)
492 (setf (dd-type dd) 'vector)))
494 (error "~S is a bad :TYPE for DEFSTRUCT." type)))))
496 (error "The DEFSTRUCT option :NAMED takes no arguments."))
498 (destructuring-bind (offset) args
499 (setf (dd-offset dd) offset)))
501 (destructuring-bind (fun) args
502 (setf (dd-pure dd) fun)))
503 (t (error "unknown DEFSTRUCT option:~% ~S" option)))))
505 ;;; Given name and options, return a DD holding that info.
506 (eval-when (:compile-toplevel :load-toplevel :execute)
507 (defun parse-defstruct-name-and-options (name-and-options)
508 (destructuring-bind (name &rest options) name-and-options
509 (aver name) ; A null name doesn't seem to make sense here.
510 (let ((dd (make-defstruct-description name)))
511 (dolist (option options)
512 (cond ((consp option)
513 (parse-1-dd-option option dd))
515 (setf (dd-named dd) t))
516 ((member option '(:constructor :copier :predicate :named))
517 (parse-1-dd-option (list option) dd))
519 (error "unrecognized DEFSTRUCT option: ~S" option))))
524 (error ":OFFSET can't be specified unless :TYPE is specified."))
525 (unless (dd-include dd)
526 (incf (dd-length dd))))
527 (funcallable-structure)
529 (require-no-print-options-so-far dd)
531 (incf (dd-length dd)))
532 (let ((offset (dd-offset dd)))
533 (when offset (incf (dd-length dd) offset)))))
535 (when (dd-include dd)
536 (do-dd-inclusion-stuff dd))
540 ;;; Given name and options and slot descriptions (and possibly doc
541 ;;; string at the head of slot descriptions) return a DD holding that
543 (defun parse-defstruct-name-and-options-and-slot-descriptions
544 (name-and-options slot-descriptions)
545 (let ((result (parse-defstruct-name-and-options (if (atom name-and-options)
546 (list name-and-options)
548 (when (stringp (car slot-descriptions))
549 (setf (dd-doc result) (pop slot-descriptions)))
550 (dolist (slot-description slot-descriptions)
551 (allocate-1-slot result (parse-1-dsd result slot-description)))
556 ;;;; stuff to parse slot descriptions
558 ;;; Parse a slot description for DEFSTRUCT, add it to the description
559 ;;; and return it. If supplied, SLOT is a pre-initialized DSD
560 ;;; that we modify to get the new slot. This is supplied when handling
562 (defun parse-1-dsd (defstruct spec &optional
563 (slot (make-defstruct-slot-description :%name ""
566 (multiple-value-bind (name default default-p type type-p read-only ro-p)
571 &optional (default nil default-p)
572 &key (type nil type-p) (read-only nil ro-p))
576 (uncross type) type-p
579 (when (keywordp spec)
580 (style-warn "Keyword slot name indicates probable syntax ~
581 error in DEFSTRUCT: ~S."
585 (when (find name (dd-slots defstruct) :test #'string= :key #'dsd-%name)
586 (error 'simple-program-error
587 :format-control "duplicate slot name ~S"
588 :format-arguments (list name)))
589 (setf (dsd-%name slot) (string name))
590 (setf (dd-slots defstruct) (nconc (dd-slots defstruct) (list slot)))
592 (let ((accessor-name (symbolicate (or (dd-conc-name defstruct) "") name))
593 (predicate-name (dd-predicate-name defstruct)))
594 (setf (dsd-accessor-name slot) accessor-name)
595 (when (eql accessor-name predicate-name)
596 ;; Some adventurous soul has named a slot so that its accessor
597 ;; collides with the structure type predicate. ANSI doesn't
598 ;; specify what to do in this case. As of 2001-09-04, Martin
599 ;; Atzmueller reports that CLISP and Lispworks both give
600 ;; priority to the slot accessor, so that the predicate is
601 ;; overwritten. We might as well do the same (as well as
602 ;; signalling a warning).
604 "~@<The structure accessor name ~S is the same as the name of the ~
605 structure type predicate. ANSI doesn't specify what to do in ~
606 this case; this implementation chooses to overwrite the type ~
607 predicate with the slot accessor.~@:>"
609 (setf (dd-predicate-name defstruct) nil)))
612 (setf (dsd-default slot) default))
614 (setf (dsd-type slot)
615 (if (eq (dsd-type slot) t)
617 `(and ,(dsd-type slot) ,type))))
620 (setf (dsd-read-only slot) t)
621 (when (dsd-read-only slot)
622 (error "Slot ~S is :READ-ONLY in parent and must be :READ-ONLY in subtype ~S."
627 ;;; When a value of type TYPE is stored in a structure, should it be
628 ;;; stored in a raw slot? Return (VALUES RAW? RAW-TYPE WORDS), where
629 ;;; RAW? is true if TYPE should be stored in a raw slot.
630 ;;; RAW-TYPE is the raw slot type, or NIL if no raw slot.
631 ;;; WORDS is the number of words in the raw slot, or NIL if no raw slot.
632 (defun structure-raw-slot-type-and-size (type)
633 (/noshow "in STRUCTURE-RAW-SLOT-TYPE-AND-SIZE" type (sb!xc:subtypep type 'fixnum))
635 (;; FIXME: For now we suppress raw slots, since there are various
636 ;; issues about the way that the cross-compiler handles them.
637 (not (boundp '*dummy-placeholder-to-stop-compiler-warnings*))
638 (values nil nil nil))
639 ((and (sb!xc:subtypep type '(unsigned-byte 32))
640 (multiple-value-bind (fixnum? fixnum-certain?)
641 (sb!xc:subtypep type 'fixnum)
642 (/noshow fixnum? fixnum-certain?)
643 ;; (The extra test for FIXNUM-CERTAIN? here is
644 ;; intended for bootstrapping the system. In
645 ;; particular, in sbcl-0.6.2, we set up LAYOUT before
646 ;; FIXNUM is defined, and so could bogusly end up
647 ;; putting INDEX-typed values into raw slots if we
648 ;; didn't test FIXNUM-CERTAIN?.)
649 (and (not fixnum?) fixnum-certain?)))
650 (values t 'unsigned-byte 1))
651 ((sb!xc:subtypep type 'single-float)
652 (values t 'single-float 1))
653 ((sb!xc:subtypep type 'double-float)
654 (values t 'double-float 2))
656 ((sb!xc:subtypep type 'long-float)
657 (values t 'long-float #!+x86 3 #!+sparc 4))
658 ((sb!xc:subtypep type '(complex single-float))
659 (values t 'complex-single-float 2))
660 ((sb!xc:subtypep type '(complex double-float))
661 (values t 'complex-double-float 4))
663 ((sb!xc:subtypep type '(complex long-float))
664 (values t 'complex-long-float #!+x86 6 #!+sparc 8))
666 (values nil nil nil))))
668 ;;; Allocate storage for a DSD in DEFSTRUCT. This is where we decide
669 ;;; whether a slot is raw or not. If raw, and we haven't allocated a
670 ;;; raw-index yet for the raw data vector, then do it. Raw objects are
671 ;;; aligned on the unit of their size.
672 (defun allocate-1-slot (defstruct dsd)
673 (multiple-value-bind (raw? raw-type words)
674 (if (eq (dd-type defstruct) 'structure)
675 (structure-raw-slot-type-and-size (dsd-type dsd))
676 (values nil nil nil))
677 (/noshow "ALLOCATE-1-SLOT" dsd raw? raw-type words)
679 (setf (dsd-index dsd) (dd-length defstruct))
680 (incf (dd-length defstruct)))
682 (unless (dd-raw-index defstruct)
683 (setf (dd-raw-index defstruct) (dd-length defstruct))
684 (incf (dd-length defstruct)))
685 (let ((off (rem (dd-raw-length defstruct) words)))
687 (incf (dd-raw-length defstruct) (- words off))))
688 (setf (dsd-raw-type dsd) raw-type)
689 (setf (dsd-index dsd) (dd-raw-length defstruct))
690 (incf (dd-raw-length defstruct) words))))
693 (defun typed-structure-info-or-lose (name)
694 (or (info :typed-structure :info name)
695 (error ":TYPE'd DEFSTRUCT ~S not found for inclusion." name)))
697 ;;; Process any included slots pretty much like they were specified.
698 ;;; Also inherit various other attributes.
699 (defun do-dd-inclusion-stuff (dd)
700 (destructuring-bind (included-name &rest modified-slots) (dd-include dd)
701 (let* ((type (dd-type dd))
704 (layout-info (compiler-layout-or-lose included-name))
705 (typed-structure-info-or-lose included-name))))
706 (unless (and (eq type (dd-type included-structure))
707 (type= (specifier-type (dd-element-type included-structure))
708 (specifier-type (dd-element-type dd))))
709 (error ":TYPE option mismatch between structures ~S and ~S"
710 (dd-name dd) included-name))
712 (incf (dd-length dd) (dd-length included-structure))
713 (when (dd-class-p dd)
714 (let ((mc (rest (dd-alternate-metaclass included-structure))))
715 (when (and mc (not (dd-alternate-metaclass dd)))
716 (setf (dd-alternate-metaclass dd)
717 (cons included-name mc))))
718 (when (eq (dd-pure dd) :unspecified)
719 (setf (dd-pure dd) (dd-pure included-structure)))
720 (setf (dd-raw-index dd) (dd-raw-index included-structure))
721 (setf (dd-raw-length dd) (dd-raw-length included-structure)))
723 (dolist (included-slot (dd-slots included-structure))
724 (let* ((included-name (dsd-name included-slot))
725 (modified (or (find included-name modified-slots
726 :key #'(lambda (x) (if (atom x) x (car x)))
731 (copy-structure included-slot)))))))
733 ;;;; various helper functions for setting up DEFSTRUCTs
735 ;;; This function is called at macroexpand time to compute the INHERITS
736 ;;; vector for a structure type definition.
737 (defun inherits-for-structure (info)
738 (declare (type defstruct-description info))
739 (let* ((include (dd-include info))
740 (superclass-opt (dd-alternate-metaclass info))
743 (compiler-layout-or-lose (first include))
744 (class-layout (sb!xc:find-class
745 (or (first superclass-opt)
746 'structure-object))))))
747 (if (eq (dd-name info) 'lisp-stream)
748 ;; a hack to added the stream class as a mixin for LISP-STREAMs
749 (concatenate 'simple-vector
750 (layout-inherits super)
752 (class-layout (sb!xc:find-class 'stream))))
753 (concatenate 'simple-vector
754 (layout-inherits super)
757 ;;; Do miscellaneous (LOAD EVAL) time actions for the structure
758 ;;; described by INFO. Create the class & layout, checking for
759 ;;; incompatible redefinition. Define setters, accessors, copier,
760 ;;; predicate, documentation, instantiate definition in load-time env.
761 ;;; This is only called for default structures.
762 (defun %defstruct (info inherits)
763 (declare (type defstruct-description info))
764 (multiple-value-bind (class layout old-layout)
765 (ensure-structure-class info inherits "current" "new")
766 (cond ((not old-layout)
767 (unless (eq (class-layout class) layout)
768 (register-layout layout)))
770 (let ((old-info (layout-info old-layout)))
771 (when (defstruct-description-p old-info)
772 (dolist (slot (dd-slots old-info))
773 (fmakunbound (dsd-accessor-name slot))
774 (unless (dsd-read-only slot)
775 (fmakunbound `(setf ,(dsd-accessor-name slot)))))))
776 (%redefine-defstruct class old-layout layout)
777 (setq layout (class-layout class))))
779 (setf (sb!xc:find-class (dd-name info)) class)
781 ;; Set FDEFINITIONs for structure accessors, setters, predicates,
784 (unless (eq (dd-type info) 'funcallable-structure)
786 (dolist (slot (dd-slots info))
788 (when (and (dsd-accessor-name slot)
789 (eq (dsd-raw-type slot) t))
790 (protect-cl (dsd-accessor-name slot))
791 (setf (symbol-function (dsd-accessor-name slot))
792 (structure-slot-getter layout dsd))
793 (unless (dsd-read-only slot)
794 (setf (fdefinition `(setf ,(dsd-accessor-name slot)))
795 (structure-slot-setter layout dsd))))))
797 ;; FIXME: Someday it'd probably be good to go back to using
798 ;; closures for the out-of-line forms of structure accessors.
800 (when (dd-predicate info)
801 (protect-cl (dd-predicate info))
802 (setf (symbol-function (dd-predicate info))
804 (declare (optimize (speed 3) (safety 0)))
805 (typep-to-layout object layout))))
808 (when (dd-copier info)
809 (protect-cl (dd-copier info))
810 (setf (symbol-function (dd-copier info))
811 #'(lambda (structure)
812 (declare (optimize (speed 3) (safety 0)))
813 (flet ((layout-test (structure)
814 (typep-to-layout structure layout)))
815 (unless (layout-test structure)
816 (error 'simple-type-error
818 :expected-type '(satisfies layout-test)
820 "Structure for copier is not a ~S:~% ~S"
822 (list (sb!xc:class-name (layout-class layout))
824 (copy-structure structure))))))
827 (setf (fdocumentation (dd-name info) 'type) (dd-doc info)))
831 ;;; Do (COMPILE LOAD EVAL)-time actions for the defstruct described by DD.
832 (defun %compiler-defstruct (dd inherits)
833 (declare (type defstruct-description dd))
834 (multiple-value-bind (class layout old-layout)
835 (multiple-value-bind (clayout clayout-p)
836 (info :type :compiler-layout (dd-name dd))
837 (ensure-structure-class dd
839 (if clayout-p "previously compiled" "current")
841 :compiler-layout clayout))
843 (undefine-structure (layout-class old-layout))
844 (when (and (class-subclasses class)
845 (not (eq layout old-layout)))
847 (dohash (class layout (class-subclasses class))
848 (declare (ignore layout))
849 (undefine-structure class)
850 (subs (class-proper-name class)))
852 (warn "Removing old subclasses of ~S:~% ~S"
853 (sb!xc:class-name class)
856 (unless (eq (class-layout class) layout)
857 (register-layout layout :invalidate nil))
858 (setf (sb!xc:find-class (dd-name dd)) class)))
860 (setf (info :type :compiler-layout (dd-name dd)) layout))
863 ((vector list funcallable-structure)
864 ;; nothing extra to do in this case
867 (let* ((name (dd-name dd))
868 (class (sb!xc:find-class name)))
870 (let ((copier (dd-copier dd)))
872 (proclaim `(ftype (function (,name) ,name) ,copier))))
874 (dolist (slot (dd-slots dd))
875 (let* ((fun (dsd-accessor-name slot))
876 (setf-fun `(setf ,fun)))
877 (when (and fun (eq (dsd-raw-type slot) t))
878 (proclaim-as-defstruct-fun-name fun)
879 (setf (info :function :accessor-for fun) class)
880 (unless (dsd-read-only slot)
881 (proclaim-as-defstruct-fun-name setf-fun)
882 (setf (info :function :accessor-for setf-fun) class)))))
884 ;; FIXME: Couldn't this logic be merged into
885 ;; PROCLAIM-AS-DEFSTRUCT-FUNCTION?
886 (when (boundp 'sb!c:*free-functions*) ; when compiling
887 (let ((free-functions sb!c:*free-functions*))
888 (dolist (slot (dd-slots dd))
889 (let ((accessor-name (dsd-accessor-name slot)))
890 (remhash accessor-name free-functions)
891 (unless (dsd-read-only slot)
892 (remhash `(setf ,accessor-name) free-functions))))
893 (remhash (dd-predicate-name dd) free-functions)
894 (remhash (dd-copier dd) free-functions))))))
898 ;;;; redefinition stuff
900 ;;; Compare the slots of OLD and NEW, returning 3 lists of slot names:
901 ;;; 1. Slots which have moved,
902 ;;; 2. Slots whose type has changed,
903 ;;; 3. Deleted slots.
904 (defun compare-slots (old new)
905 (let* ((oslots (dd-slots old))
906 (nslots (dd-slots new))
907 (onames (mapcar #'dsd-name oslots))
908 (nnames (mapcar #'dsd-name nslots)))
911 (dolist (name (intersection onames nnames))
912 (let ((os (find name oslots :key #'dsd-name))
913 (ns (find name nslots :key #'dsd-name)))
914 (unless (subtypep (dsd-type ns) (dsd-type os))
915 (/noshow "found retyped slots" ns os (dsd-type ns) (dsd-type os))
917 (unless (and (= (dsd-index os) (dsd-index ns))
918 (eq (dsd-raw-type os) (dsd-raw-type ns)))
922 (set-difference onames nnames)))))
924 ;;; If we are redefining a structure with different slots than in the
925 ;;; currently loaded version, give a warning and return true.
926 (defun redefine-structure-warning (class old new)
927 (declare (type defstruct-description old new)
928 (type sb!xc:class class)
930 (let ((name (dd-name new)))
931 (multiple-value-bind (moved retyped deleted) (compare-slots old new)
932 (when (or moved retyped deleted)
934 "incompatibly redefining slots of structure class ~S~@
935 Make sure any uses of affected accessors are recompiled:~@
936 ~@[ These slots were moved to new positions:~% ~S~%~]~
937 ~@[ These slots have new incompatible types:~% ~S~%~]~
938 ~@[ These slots were deleted:~% ~S~%~]"
939 name moved retyped deleted)
942 ;;; This function is called when we are incompatibly redefining a
943 ;;; structure Class to have the specified New-Layout. We signal an
944 ;;; error with some proceed options and return the layout that should
946 (defun %redefine-defstruct (class old-layout new-layout)
947 (declare (type sb!xc:class class) (type layout old-layout new-layout))
948 (let ((name (class-proper-name class)))
950 (error "redefining class ~S incompatibly with the current definition"
953 :report "Invalidate current definition."
954 (warn "Previously loaded ~S accessors will no longer work." name)
955 (register-layout new-layout))
957 :report "Smash current layout, preserving old code."
958 (warn "Any old ~S instances will be in a bad way.~@
959 I hope you know what you're doing..."
961 (register-layout new-layout :invalidate nil
962 :destruct-layout old-layout))))
965 ;;; This is called when we are about to define a structure class. It
966 ;;; returns a (possibly new) class object and the layout which should
967 ;;; be used for the new definition (may be the current layout, and
968 ;;; also might be an uninstalled forward referenced layout.) The third
969 ;;; value is true if this is an incompatible redefinition, in which
970 ;;; case it is the old layout.
971 (defun ensure-structure-class (info inherits old-context new-context
972 &key compiler-layout)
973 (multiple-value-bind (class old-layout)
977 (class 'sb!xc:structure-class)
978 (constructor 'make-structure-class))
979 (dd-alternate-metaclass info)
980 (declare (ignore name))
981 (insured-find-class (dd-name info)
982 (if (eq class 'sb!xc:structure-class)
984 (typep x 'sb!xc:structure-class))
986 (sb!xc:typep x (sb!xc:find-class class))))
987 (fdefinition constructor)))
988 (setf (class-direct-superclasses class)
989 (if (eq (dd-name info) 'lisp-stream)
990 ;; a hack to add STREAM as a superclass mixin to LISP-STREAMs
991 (list (layout-class (svref inherits (1- (length inherits))))
992 (layout-class (svref inherits (- (length inherits) 2))))
993 (list (layout-class (svref inherits (1- (length inherits)))))))
994 (let ((new-layout (make-layout :class class
996 :depthoid (length inherits)
997 :length (dd-length info)
999 (old-layout (or compiler-layout old-layout)))
1002 (values class new-layout nil))
1003 (;; This clause corresponds to an assertion in REDEFINE-LAYOUT-WARNING
1004 ;; of classic CMU CL. I moved it out to here because it was only
1005 ;; exercised in this code path anyway. -- WHN 19990510
1006 (not (eq (layout-class new-layout) (layout-class old-layout)))
1007 (error "shouldn't happen: weird state of OLD-LAYOUT?"))
1008 ((not *type-system-initialized*)
1009 (setf (layout-info old-layout) info)
1010 (values class old-layout nil))
1011 ((redefine-layout-warning old-context
1014 (layout-length new-layout)
1015 (layout-inherits new-layout)
1016 (layout-depthoid new-layout))
1017 (values class new-layout old-layout))
1019 (let ((old-info (layout-info old-layout)))
1021 ((or defstruct-description)
1022 (cond ((redefine-structure-warning class old-info info)
1023 (values class new-layout old-layout))
1025 (setf (layout-info old-layout) info)
1026 (values class old-layout nil))))
1028 (setf (layout-info old-layout) info)
1029 (values class old-layout nil))
1031 (error "shouldn't happen! strange thing in LAYOUT-INFO:~% ~S"
1033 (values class new-layout old-layout)))))))))
1035 ;;; Blow away all the compiler info for the structure CLASS. Iterate
1036 ;;; over this type, clearing the compiler structure type info, and
1037 ;;; undefining all the associated functions.
1038 (defun undefine-structure (class)
1039 (let ((info (layout-info (class-layout class))))
1040 (when (defstruct-description-p info)
1041 (let ((type (dd-name info)))
1042 (setf (info :type :compiler-layout type) nil)
1043 (undefine-fun-name (dd-copier info))
1044 (undefine-fun-name (dd-predicate-name info))
1045 (dolist (slot (dd-slots info))
1046 (let ((fun (dsd-accessor-name slot)))
1047 (undefine-fun-name fun)
1048 (unless (dsd-read-only slot)
1049 (undefine-fun-name `(setf ,fun))))))
1050 ;; Clear out the SPECIFIER-TYPE cache so that subsequent
1051 ;; references are unknown types.
1052 (values-specifier-type-cache-clear)))
1055 ;;; Return a list of pairs (name . index). Used for :TYPE'd
1056 ;;; constructors to find all the names that we have to splice in &
1057 ;;; where. Note that these types don't have a layout, so we can't look
1058 ;;; at LAYOUT-INHERITS.
1059 (defun find-name-indices (defstruct)
1062 (do ((info defstruct
1063 (typed-structure-info-or-lose (first (dd-include info)))))
1064 ((not (dd-include info))
1069 (dolist (info infos)
1070 (incf i (or (dd-offset info) 0))
1071 (when (dd-named info)
1072 (res (cons (dd-name info) i)))
1073 (setq i (dd-length info)))))
1077 ;;;; slot accessors for raw slots
1079 ;;; Return info about how to read/write a slot in the value stored in
1080 ;;; OBJECT. This is also used by constructors (we can't use the
1081 ;;; accessor function, since some slots are read-only.) If supplied,
1082 ;;; DATA is a variable holding the raw-data vector.
1084 ;;; returned values:
1085 ;;; 1. accessor function name (SETFable)
1086 ;;; 2. index to pass to accessor.
1087 ;;; 3. object form to pass to accessor
1088 (defun slot-accessor-form (defstruct slot object &optional data)
1089 (let ((rtype (dsd-raw-type slot)))
1092 (single-float '%raw-ref-single)
1093 (double-float '%raw-ref-double)
1095 (long-float '%raw-ref-long)
1096 (complex-single-float '%raw-ref-complex-single)
1097 (complex-double-float '%raw-ref-complex-double)
1099 (complex-long-float '%raw-ref-complex-long)
1100 (unsigned-byte 'aref)
1102 (if (eq (dd-type defstruct) 'funcallable-structure)
1103 '%funcallable-instance-info
1108 (truncate (dsd-index slot) #!+x86 6 #!+sparc 8))
1111 (truncate (dsd-index slot) #!+x86 3 #!+sparc 4))
1113 (ash (dsd-index slot) -1))
1114 (complex-double-float
1115 (ash (dsd-index slot) -2))
1116 (complex-single-float
1117 (ash (dsd-index slot) -1))
1121 ((eq rtype t) object)
1124 `(truly-the (simple-array (unsigned-byte 32) (*))
1125 (%instance-ref ,object ,(dd-raw-index defstruct))))))))
1127 ;;; These functions are called to actually make a constructor after we
1128 ;;; have processed the arglist. The correct variant (according to the
1129 ;;; DD-TYPE) should be called. The function is defined with the
1130 ;;; specified name and arglist. Vars and Types are used for argument
1131 ;;; type declarations. Values are the values for the slots (in order.)
1133 ;;; This is split four ways because:
1134 ;;; 1] list & vector structures need "name" symbols stuck in at
1135 ;;; various weird places, whereas STRUCTURE structures have
1137 ;;; 2] We really want to use LIST to make list structures, instead of
1138 ;;; MAKE-LIST/(SETF ELT).
1139 ;;; 3] STRUCTURE structures can have raw slots that must also be
1140 ;;; allocated and indirectly referenced. We use SLOT-ACCESSOR-FORM
1141 ;;; to compute how to set the slots, which deals with raw slots.
1142 ;;; 4] Funcallable structures are weird.
1143 (defun create-vector-constructor
1144 (defstruct cons-name arglist vars types values)
1145 (let ((temp (gensym))
1146 (etype (dd-element-type defstruct)))
1147 `(defun ,cons-name ,arglist
1148 (declare ,@(mapcar #'(lambda (var type) `(type (and ,type ,etype) ,var))
1150 (let ((,temp (make-array ,(dd-length defstruct)
1151 :element-type ',(dd-element-type defstruct))))
1152 ,@(mapcar #'(lambda (x)
1153 `(setf (aref ,temp ,(cdr x)) ',(car x)))
1154 (find-name-indices defstruct))
1155 ,@(mapcar #'(lambda (dsd value)
1156 `(setf (aref ,temp ,(dsd-index dsd)) ,value))
1157 (dd-slots defstruct) values)
1159 (defun create-list-constructor
1160 (defstruct cons-name arglist vars types values)
1161 (let ((vals (make-list (dd-length defstruct) :initial-element nil)))
1162 (dolist (x (find-name-indices defstruct))
1163 (setf (elt vals (cdr x)) `',(car x)))
1164 (loop for dsd in (dd-slots defstruct) 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))
1171 (defun create-structure-constructor
1172 (defstruct cons-name arglist vars types values)
1173 (let* ((temp (gensym))
1174 (raw-index (dd-raw-index defstruct))
1175 (n-raw-data (when raw-index (gensym))))
1176 `(defun ,cons-name ,arglist
1177 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1179 (let ((,temp (truly-the ,(dd-name defstruct)
1180 (%make-instance ,(dd-length defstruct))))
1183 (make-array ,(dd-raw-length defstruct)
1184 :element-type '(unsigned-byte 32))))))
1185 (setf (%instance-layout ,temp)
1186 (%delayed-get-compiler-layout ,(dd-name defstruct)))
1188 `((setf (%instance-ref ,temp ,raw-index) ,n-raw-data)))
1189 ,@(mapcar (lambda (dsd value)
1190 (multiple-value-bind (accessor index data)
1191 (slot-accessor-form defstruct dsd temp n-raw-data)
1192 `(setf (,accessor ,data ,index) ,value)))
1193 (dd-slots defstruct)
1196 (defun create-fin-constructor
1197 (defstruct cons-name arglist vars types values)
1198 (let ((temp (gensym)))
1199 `(defun ,cons-name ,arglist
1200 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1202 (let ((,temp (truly-the
1203 ,(dd-name defstruct)
1204 (%make-funcallable-instance
1205 ,(dd-length defstruct)
1206 (%delayed-get-compiler-layout ,(dd-name defstruct))))))
1207 ,@(mapcar #'(lambda (dsd value)
1208 `(setf (%funcallable-instance-info
1209 ,temp ,(dsd-index dsd))
1211 (dd-slots defstruct) values)
1214 ;;; Create a default (non-BOA) keyword constructor.
1215 (defun create-keyword-constructor (defstruct creator)
1216 (collect ((arglist (list '&key))
1219 (dolist (slot (dd-slots defstruct))
1220 (let ((dum (gensym))
1221 (name (dsd-name slot)))
1222 (arglist `((,(keywordicate name) ,dum) ,(dsd-default slot)))
1223 (types (dsd-type slot))
1226 defstruct (dd-default-constructor defstruct)
1227 (arglist) (vals) (types) (vals))))
1229 ;;; Given a structure and a BOA constructor spec, call CREATOR with
1230 ;;; the appropriate args to make a constructor.
1231 (defun create-boa-constructor (defstruct boa creator)
1232 (multiple-value-bind (req opt restp rest keyp keys allowp aux)
1233 (sb!kernel:parse-lambda-list (second boa))
1237 (labels ((get-slot (name)
1238 (let ((res (find name (dd-slots defstruct)
1242 (values (dsd-type res) (dsd-default res))
1245 (multiple-value-bind (type default) (get-slot arg)
1246 (arglist `(,arg ,default))
1252 (types (get-slot arg)))
1255 (arglist '&optional)
1259 (name &optional (def (nth-value 1 (get-slot name))))
1261 (arglist `(,name ,def))
1263 (types (get-slot name))))
1265 (do-default arg)))))
1268 (arglist '&rest rest)
1276 (destructuring-bind (wot &optional (def nil def-p)) key
1277 (let ((name (if (consp wot)
1278 (destructuring-bind (key var) wot
1279 (declare (ignore key))
1282 (multiple-value-bind (type slot-def) (get-slot name)
1283 (arglist `(,wot ,(if def-p def slot-def)))
1288 (when allowp (arglist '&allow-other-keys))
1293 (let* ((arg (if (consp arg) arg (list arg)))
1297 (types (get-slot var))))))
1299 (funcall creator defstruct (first boa)
1300 (arglist) (vars) (types)
1301 (mapcar #'(lambda (slot)
1302 (or (find (dsd-name slot) (vars) :test #'string=)
1303 (dsd-default slot)))
1304 (dd-slots defstruct))))))
1306 ;;; Grovel the constructor options, and decide what constructors (if
1308 (defun constructor-definitions (defstruct)
1309 (let ((no-constructors nil)
1312 (creator (ecase (dd-type defstruct)
1313 (structure #'create-structure-constructor)
1314 (funcallable-structure #'create-fin-constructor)
1315 (vector #'create-vector-constructor)
1316 (list #'create-list-constructor))))
1317 (dolist (constructor (dd-constructors defstruct))
1318 (destructuring-bind (name &optional (boa-ll nil boa-p)) constructor
1319 (declare (ignore boa-ll))
1320 (cond ((not name) (setq no-constructors t))
1321 (boa-p (push constructor boas))
1322 (t (push name defaults)))))
1324 (when no-constructors
1325 (when (or defaults boas)
1326 (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs"))
1327 (return-from constructor-definitions ()))
1329 (unless (or defaults boas)
1330 (push (symbolicate "MAKE-" (dd-name defstruct)) defaults))
1334 (let ((cname (first defaults)))
1335 (setf (dd-default-constructor defstruct) cname)
1336 (res (create-keyword-constructor defstruct creator))
1337 (dolist (other-name (rest defaults))
1338 (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1339 (res `(declaim (ftype function ',other-name))))))
1342 (res (create-boa-constructor defstruct boa creator)))
1348 ;;; This is like PROCLAIM-AS-FUN-NAME, but we also set the kind to
1349 ;;; :DECLARED and blow away any ASSUMED-TYPE. Also, if the thing is a
1350 ;;; slot accessor currently, quietly unaccessorize it. And if there
1351 ;;; are any undefined warnings, we nuke them.
1352 (defun proclaim-as-defstruct-fun-name (name)
1354 (when (info :function :accessor-for name)
1355 (setf (info :function :accessor-for name) nil))
1356 (proclaim-as-fun-name name)
1357 (note-name-defined name :function)
1358 (setf (info :function :where-from name) :declared)
1359 (when (info :function :assumed-type name)
1360 (setf (info :function :assumed-type name) nil)))
1363 ;;;; finalizing bootstrapping
1365 ;;; early structure placeholder definitions: Set up layout and class
1366 ;;; data for structures which are needed early.
1368 '#.(sb-cold:read-from-file
1369 "src/code/early-defstruct-args.lisp-expr"))
1370 (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
1373 (inherits (inherits-for-structure dd)))
1374 (%compiler-defstruct dd inherits)))
1376 (/show0 "code/defstruct.lisp end of file")