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, CLASS-STRUCTURE-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 CLASS-STRUCTURE-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 class-structure-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 (function-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
235 (if (class-structure-p dd)
236 (let ((inherits (inherits-for-structure dd)))
238 (eval-when (:compile-toplevel :load-toplevel :execute)
239 (%compiler-defstruct ',dd ',inherits)
240 ,@(when (eq (dd-type dd) 'structure)
241 `((%compiler-truly-defstruct ',dd))))
242 (%defstruct ',dd ',inherits)
243 ,@(unless expanding-into-code-for-xc-host-p
244 (append (raw-accessor-definitions dd)
245 (predicate-definitions dd)
246 ;; FIXME: We've inherited from CMU CL nonparallel
247 ;; code for creating copiers for typed and untyped
248 ;; structures. This should be fixed.
249 ;(copier-definition dd)
250 (constructor-definitions dd)
251 (class-method-definitions dd)))
254 (eval-when (:compile-toplevel :load-toplevel :execute)
255 (setf (info :typed-structure :info ',name) ',dd))
256 ,@(unless expanding-into-code-for-xc-host-p
257 (append (typed-accessor-definitions dd)
258 (typed-predicate-definitions dd)
259 (typed-copier-definitions dd)
260 (constructor-definitions dd)))
263 (sb!xc:defmacro defstruct (name-and-options &rest slot-descriptions)
265 "DEFSTRUCT {Name | (Name Option*)} {Slot | (Slot [Default] {Key Value}*)}
266 Define the structure type Name. Instances are created by MAKE-<name>,
267 which takes &KEY arguments allowing initial slot values to the specified.
268 A SETF'able function <name>-<slot> is defined for each slot to read and
269 write slot values. <name>-p is a type predicate.
271 Popular DEFSTRUCT options (see manual for others):
275 Specify the name for the constructor or predicate.
277 (:CONSTRUCTOR Name Lambda-List)
278 Specify the name and arguments for a BOA constructor
279 (which is more efficient when keyword syntax isn't necessary.)
281 (:INCLUDE Supertype Slot-Spec*)
282 Make this type a subtype of the structure type Supertype. The optional
283 Slot-Specs override inherited slot options.
288 Asserts that the value of this slot is always of the specified type.
291 If true, no setter function is defined for this slot."
292 (!expander-for-defstruct name-and-options slot-descriptions nil))
294 (defmacro sb!xc:defstruct (name-and-options &rest slot-descriptions)
296 "Cause information about a target structure to be built into the
298 (!expander-for-defstruct name-and-options slot-descriptions t))
300 ;;;; functions to generate code for various parts of DEFSTRUCT definitions
302 ;;; Catch requests to mess up definitions in COMMON-LISP.
304 (eval-when (:compile-toplevel :load-toplevel :execute)
305 (defun protect-cl (symbol)
306 (when (and *cold-init-complete-p*
307 (eq (symbol-package symbol) *cl-package*))
308 (cerror "Go ahead and patch the system."
309 "attempting to modify a symbol in the COMMON-LISP package: ~S"
312 ;;; Return forms to define readers and writers for raw slots as inline
314 (defun raw-accessor-definitions (dd)
315 (let* ((name (dd-name dd)))
317 (dolist (slot (dd-slots dd))
318 (let ((stype (dsd-type slot))
319 (accessor-name (dsd-accessor-name slot))
320 (argname (gensym "ARG"))
321 (nvname (gensym "NEW-VALUE-")))
322 (multiple-value-bind (accessor offset data)
323 (slot-accessor-form dd slot argname)
324 ;; When accessor exists and is raw
325 (when (and accessor-name
326 (not (eq accessor-name '%instance-ref)))
327 (res `(declaim (inline ,accessor-name)))
328 (res `(declaim (ftype (function (,name) ,stype) ,accessor-name)))
329 (res `(defun ,accessor-name (,argname)
330 (truly-the ,stype (,accessor ,data ,offset))))
331 (unless (dsd-read-only slot)
332 (res `(declaim (inline (setf ,accessor-name))))
333 (res `(declaim (ftype (function (,stype ,name) ,stype)
334 (setf ,accessor-name))))
335 ;; FIXME: I rewrote this somewhat from the CMU CL definition.
336 ;; Do some basic tests to make sure that reading and writing
337 ;; raw slots still works correctly.
338 (res `(defun (setf ,accessor-name) (,nvname ,argname)
339 (setf (,accessor ,data ,offset) ,nvname)
343 ;;; Return a list of forms which create a predicate for an untyped DEFSTRUCT.
344 (defun predicate-definitions (dd)
345 (let ((pred (dd-predicate-name dd))
348 (if (eq (dd-type dd) 'funcallable-structure)
349 ;; FIXME: Why does this need to be special-cased for
350 ;; FUNCALLABLE-STRUCTURE? CMU CL did it, but without explanation.
351 ;; Could we do without it? What breaks if we do? Or could we
352 ;; perhaps get by with no predicates for funcallable structures?
353 `((declaim (inline ,pred))
354 (defun ,pred (,argname) (typep ,argname ',(dd-name dd))))
355 `((protect-cl ',pred)
356 (declaim (inline ,pred))
357 (defun ,pred (,argname)
358 (declare (optimize (speed 3) (safety 0)))
359 (typep-to-layout ,argname
360 (compile-time-find-layout ,(dd-name dd)))))))))
362 ;;; Return a list of forms which create a predicate function for a typed
364 (defun typed-predicate-definitions (defstruct)
365 (let ((name (dd-name defstruct))
366 (predicate-name (dd-predicate-name defstruct))
368 (when (and predicate-name (dd-named defstruct))
369 (let ((ltype (dd-lisp-type defstruct)))
370 `((defun ,predicate-name (,argname)
371 (and (typep ,argname ',ltype)
372 (eq (elt (the ,ltype ,argname)
373 ,(cdr (car (last (find-name-indices defstruct)))))
376 ;;; FIXME: We've inherited from CMU CL code to do typed structure copiers
377 ;;; in a completely different way than untyped structure copiers. Fix this.
378 ;;; (This function was my first attempt to fix this, but I stopped before
379 ;;; figuring out how to install it completely and remove the parallel
380 ;;; code which simply SETF's the FDEFINITION of the DD-COPIER name.
382 ;;; Return the copier definition for an untyped DEFSTRUCT.
383 (defun copier-definition (dd)
384 (when (and (dd-copier dd)
385 ;; FUNCALLABLE-STRUCTUREs don't need copiers, and this
386 ;; implementation wouldn't work for them anyway, since
387 ;; COPY-STRUCTURE returns a STRUCTURE-OBJECT and they're not.
388 (not (eq (dd-type info) 'funcallable-structure)))
389 (let ((argname (gensym)))
391 (protect-cl ',(dd-copier dd))
392 (defun ,(dd-copier dd) (,argname)
393 (declare (type ,(dd-name dd) ,argname))
394 (copy-structure ,argname))))))
397 ;;; Return a list of forms to create a copier function of a typed DEFSTRUCT.
398 (defun typed-copier-definitions (defstruct)
399 (when (dd-copier defstruct)
400 `((setf (fdefinition ',(dd-copier defstruct)) #'copy-seq)
401 (declaim (ftype function ,(dd-copier defstruct))))))
403 ;;; Return a list of function definitions for accessing and setting the
404 ;;; slots of a typed DEFSTRUCT. The functions are proclaimed to be inline,
405 ;;; and the types of their arguments and results are declared as well. We
406 ;;; count on the compiler to do clever things with ELT.
407 (defun typed-accessor-definitions (defstruct)
409 (let ((ltype (dd-lisp-type defstruct)))
410 (dolist (slot (dd-slots defstruct))
411 (let ((name (dsd-accessor slot))
412 (index (dsd-index slot))
413 (slot-type `(and ,(dsd-type slot)
414 ,(dd-element-type defstruct))))
415 (stuff `(proclaim '(inline ,name (setf ,name))))
416 ;; FIXME: The arguments in the next two DEFUNs should be
417 ;; gensyms. (Otherwise e.g. if NEW-VALUE happened to be the
418 ;; name of a special variable, things could get weird.)
419 (stuff `(defun ,name (structure)
420 (declare (type ,ltype structure))
421 (the ,slot-type (elt structure ,index))))
422 (unless (dsd-read-only slot)
424 `(defun (setf ,name) (new-value structure)
425 (declare (type ,ltype structure) (type ,slot-type new-value))
426 (setf (elt structure ,index) new-value)))))))
431 (defun require-no-print-options-so-far (defstruct)
432 (unless (and (eql (dd-print-function defstruct) 0)
433 (eql (dd-print-object defstruct) 0))
434 (error "no more than one of the following options may be specified:
435 :PRINT-FUNCTION, :PRINT-OBJECT, :TYPE")))
437 ;;; Parse a single DEFSTRUCT option and store the results in DD.
438 (defun parse-1-dd-option (option dd)
439 (let ((args (rest option))
443 (destructuring-bind (conc-name) args
444 (setf (dd-conc-name dd)
445 (if (symbolp conc-name)
447 (make-symbol (string conc-name))))))
449 (destructuring-bind (&optional (cname (symbolicate "MAKE-" name))
452 (push (cons cname stuff) (dd-constructors dd))))
454 (destructuring-bind (&optional (copier (symbolicate "COPY-" name)))
456 (setf (dd-copier dd) copier)))
458 (destructuring-bind (&optional (predicate-name (symbolicate name "-P")))
460 (setf (dd-predicate-name dd) predicate-name)))
462 (when (dd-include dd)
463 (error "more than one :INCLUDE option"))
464 (setf (dd-include dd) args))
465 (:alternate-metaclass
466 (setf (dd-alternate-metaclass dd) args))
468 (require-no-print-options-so-far dd)
469 (setf (dd-print-function dd)
470 (the (or symbol cons) args)))
472 (require-no-print-options-so-far dd)
473 (setf (dd-print-object dd)
474 (the (or symbol cons) args)))
476 (destructuring-bind (type) args
477 (cond ((eq type 'funcallable-structure)
478 (setf (dd-type dd) type))
479 ((member type '(list vector))
480 (setf (dd-element-type dd) t)
481 (setf (dd-type dd) type))
482 ((and (consp type) (eq (first type) 'vector))
483 (destructuring-bind (vector vtype) type
484 (declare (ignore vector))
485 (setf (dd-element-type dd) vtype)
486 (setf (dd-type dd) 'vector)))
488 (error "~S is a bad :TYPE for DEFSTRUCT." type)))))
490 (error "The DEFSTRUCT option :NAMED takes no arguments."))
492 (destructuring-bind (offset) args
493 (setf (dd-offset dd) offset)))
495 (destructuring-bind (fun) args
496 (setf (dd-pure dd) fun)))
497 (t (error "unknown DEFSTRUCT option:~% ~S" option)))))
499 ;;; Given name and options, return a DD holding that info.
500 (eval-when (:compile-toplevel :load-toplevel :execute)
501 (defun parse-defstruct-name-and-options (name-and-options)
502 (destructuring-bind (name &rest options) name-and-options
503 (aver name) ; A null name doesn't seem to make sense here.
504 (let ((dd (make-defstruct-description name)))
505 (dolist (option options)
506 (cond ((consp option)
507 (parse-1-dd-option option dd))
509 (setf (dd-named dd) t))
510 ((member option '(:constructor :copier :predicate :named))
511 (parse-1-dd-option (list option) dd))
513 (error "unrecognized DEFSTRUCT option: ~S" option))))
518 (error ":OFFSET can't be specified unless :TYPE is specified."))
519 (unless (dd-include dd)
520 (incf (dd-length dd))))
521 (funcallable-structure)
523 (require-no-print-options-so-far dd)
525 (incf (dd-length dd)))
526 (let ((offset (dd-offset dd)))
527 (when offset (incf (dd-length dd) offset)))))
529 (when (dd-include dd)
530 (do-dd-inclusion-stuff dd))
534 ;;; Given name and options and slot descriptions (and possibly doc
535 ;;; string at the head of slot descriptions) return a DD holding that
537 (defun parse-defstruct-name-and-options-and-slot-descriptions
538 (name-and-options slot-descriptions)
539 (let ((result (parse-defstruct-name-and-options (if (atom name-and-options)
540 (list name-and-options)
542 (when (stringp (car slot-descriptions))
543 (setf (dd-doc result) (pop slot-descriptions)))
544 (dolist (slot-description slot-descriptions)
545 (allocate-1-slot result (parse-1-dsd result slot-description)))
550 ;;;; stuff to parse slot descriptions
552 ;;; Parse a slot description for DEFSTRUCT, add it to the description
553 ;;; and return it. If supplied, SLOT is a pre-initialized DSD
554 ;;; that we modify to get the new slot. This is supplied when handling
556 (defun parse-1-dsd (defstruct spec &optional
557 (slot (make-defstruct-slot-description :%name ""
560 (multiple-value-bind (name default default-p type type-p read-only ro-p)
565 &optional (default nil default-p)
566 &key (type nil type-p) (read-only nil ro-p))
570 (uncross type) type-p
573 (when (keywordp spec)
574 (style-warn "Keyword slot name indicates probable syntax ~
575 error in DEFSTRUCT: ~S."
579 (when (find name (dd-slots defstruct) :test #'string= :key #'dsd-%name)
580 (error 'simple-program-error
581 :format-control "duplicate slot name ~S"
582 :format-arguments (list name)))
583 (setf (dsd-%name slot) (string name))
584 (setf (dd-slots defstruct) (nconc (dd-slots defstruct) (list slot)))
586 (let ((accessor-name (symbolicate (or (dd-conc-name defstruct) "") name))
587 (predicate-name (dd-predicate-name defstruct)))
588 (setf (dsd-accessor-name slot) accessor-name)
589 (when (eql accessor-name predicate-name)
590 ;; Some adventurous soul has named a slot so that its accessor
591 ;; collides with the structure type predicate. ANSI doesn't
592 ;; specify what to do in this case. As of 2001-09-04, Martin
593 ;; Atzmueller reports that CLISP and Lispworks both give
594 ;; priority to the slot accessor, so that the predicate is
595 ;; overwritten. We might as well do the same (as well as
596 ;; signalling a warning).
598 "~@<The structure accessor name ~S is the same as the name of the ~
599 structure type predicate. ANSI doesn't specify what to do in ~
600 this case; this implementation chooses to overwrite the type ~
601 predicate with the slot accessor.~@:>"
603 (setf (dd-predicate-name defstruct) nil)))
606 (setf (dsd-default slot) default))
608 (setf (dsd-type slot)
609 (if (eq (dsd-type slot) t)
611 `(and ,(dsd-type slot) ,type))))
614 (setf (dsd-read-only slot) t)
615 (when (dsd-read-only slot)
616 (error "Slot ~S is :READ-ONLY in parent and must be :READ-ONLY in subtype ~S."
621 ;;; When a value of type TYPE is stored in a structure, should it be
622 ;;; stored in a raw slot? Return (VALUES RAW? RAW-TYPE WORDS), where
623 ;;; RAW? is true if TYPE should be stored in a raw slot.
624 ;;; RAW-TYPE is the raw slot type, or NIL if no raw slot.
625 ;;; WORDS is the number of words in the raw slot, or NIL if no raw slot.
626 (defun structure-raw-slot-type-and-size (type)
627 (/noshow "in STRUCTURE-RAW-SLOT-TYPE-AND-SIZE" type (sb!xc:subtypep type 'fixnum))
629 (;; FIXME: For now we suppress raw slots, since there are various
630 ;; issues about the way that the cross-compiler handles them.
631 (not (boundp '*dummy-placeholder-to-stop-compiler-warnings*))
632 (values nil nil nil))
633 ((and (sb!xc:subtypep type '(unsigned-byte 32))
634 (multiple-value-bind (fixnum? fixnum-certain?)
635 (sb!xc:subtypep type 'fixnum)
636 (/noshow fixnum? fixnum-certain?)
637 ;; (The extra test for FIXNUM-CERTAIN? here is
638 ;; intended for bootstrapping the system. In
639 ;; particular, in sbcl-0.6.2, we set up LAYOUT before
640 ;; FIXNUM is defined, and so could bogusly end up
641 ;; putting INDEX-typed values into raw slots if we
642 ;; didn't test FIXNUM-CERTAIN?.)
643 (and (not fixnum?) fixnum-certain?)))
644 (values t 'unsigned-byte 1))
645 ((sb!xc:subtypep type 'single-float)
646 (values t 'single-float 1))
647 ((sb!xc:subtypep type 'double-float)
648 (values t 'double-float 2))
650 ((sb!xc:subtypep type 'long-float)
651 (values t 'long-float #!+x86 3 #!+sparc 4))
652 ((sb!xc:subtypep type '(complex single-float))
653 (values t 'complex-single-float 2))
654 ((sb!xc:subtypep type '(complex double-float))
655 (values t 'complex-double-float 4))
657 ((sb!xc:subtypep type '(complex long-float))
658 (values t 'complex-long-float #!+x86 6 #!+sparc 8))
660 (values nil nil nil))))
662 ;;; Allocate storage for a DSD in DEFSTRUCT. This is where we decide
663 ;;; whether a slot is raw or not. If raw, and we haven't allocated a
664 ;;; raw-index yet for the raw data vector, then do it. Raw objects are
665 ;;; aligned on the unit of their size.
666 (defun allocate-1-slot (defstruct dsd)
667 (multiple-value-bind (raw? raw-type words)
668 (if (eq (dd-type defstruct) 'structure)
669 (structure-raw-slot-type-and-size (dsd-type dsd))
670 (values nil nil nil))
671 (/noshow "ALLOCATE-1-SLOT" dsd raw? raw-type words)
673 (setf (dsd-index dsd) (dd-length defstruct))
674 (incf (dd-length defstruct)))
676 (unless (dd-raw-index defstruct)
677 (setf (dd-raw-index defstruct) (dd-length defstruct))
678 (incf (dd-length defstruct)))
679 (let ((off (rem (dd-raw-length defstruct) words)))
681 (incf (dd-raw-length defstruct) (- words off))))
682 (setf (dsd-raw-type dsd) raw-type)
683 (setf (dsd-index dsd) (dd-raw-length defstruct))
684 (incf (dd-raw-length defstruct) words))))
687 (defun typed-structure-info-or-lose (name)
688 (or (info :typed-structure :info name)
689 (error ":TYPE'd DEFSTRUCT ~S not found for inclusion." name)))
691 ;;; Process any included slots pretty much like they were specified.
692 ;;; Also inherit various other attributes.
693 (defun do-dd-inclusion-stuff (dd)
694 (destructuring-bind (included-name &rest modified-slots) (dd-include dd)
695 (let* ((type (dd-type dd))
697 (if (class-structure-p dd)
698 (layout-info (compiler-layout-or-lose included-name))
699 (typed-structure-info-or-lose included-name))))
700 (unless (and (eq type (dd-type included-structure))
701 (type= (specifier-type (dd-element-type included-structure))
702 (specifier-type (dd-element-type dd))))
703 (error ":TYPE option mismatch between structures ~S and ~S."
704 (dd-name dd) included-name))
706 (incf (dd-length dd) (dd-length included-structure))
707 (when (class-structure-p dd)
708 (let ((mc (rest (dd-alternate-metaclass included-structure))))
709 (when (and mc (not (dd-alternate-metaclass dd)))
710 (setf (dd-alternate-metaclass dd)
711 (cons included-name mc))))
712 (when (eq (dd-pure dd) :unspecified)
713 (setf (dd-pure dd) (dd-pure included-structure)))
714 (setf (dd-raw-index dd) (dd-raw-index included-structure))
715 (setf (dd-raw-length dd) (dd-raw-length included-structure)))
717 (dolist (included-slot (dd-slots included-structure))
718 (let* ((included-name (dsd-name included-slot))
719 (modified (or (find included-name modified-slots
720 :key #'(lambda (x) (if (atom x) x (car x)))
725 (copy-structure included-slot)))))))
727 ;;;; various helper functions for setting up DEFSTRUCTs
729 ;;; This function is called at macroexpand time to compute the INHERITS
730 ;;; vector for a structure type definition.
731 (defun inherits-for-structure (info)
732 (declare (type defstruct-description info))
733 (let* ((include (dd-include info))
734 (superclass-opt (dd-alternate-metaclass info))
737 (compiler-layout-or-lose (first include))
738 (class-layout (sb!xc:find-class
739 (or (first superclass-opt)
740 'structure-object))))))
741 (if (eq (dd-name info) 'lisp-stream)
742 ;; a hack to added the stream class as a mixin for LISP-STREAMs
743 (concatenate 'simple-vector
744 (layout-inherits super)
746 (class-layout (sb!xc:find-class 'stream))))
747 (concatenate 'simple-vector
748 (layout-inherits super)
751 ;;; Do miscellaneous (LOAD EVAL) time actions for the structure
752 ;;; described by INFO. Create the class & layout, checking for
753 ;;; incompatible redefinition. Define setters, accessors, copier,
754 ;;; predicate, documentation, instantiate definition in load-time env.
755 ;;; This is only called for default structures.
756 (defun %defstruct (info inherits)
757 (declare (type defstruct-description info))
758 (multiple-value-bind (class layout old-layout)
759 (ensure-structure-class info inherits "current" "new")
760 (cond ((not old-layout)
761 (unless (eq (class-layout class) layout)
762 (register-layout layout)))
764 (let ((old-info (layout-info old-layout)))
765 (when (defstruct-description-p old-info)
766 (dolist (slot (dd-slots old-info))
767 (fmakunbound (dsd-accessor-name slot))
768 (unless (dsd-read-only slot)
769 (fmakunbound `(setf ,(dsd-accessor-name slot)))))))
770 (%redefine-defstruct class old-layout layout)
771 (setq layout (class-layout class))))
773 (setf (sb!xc:find-class (dd-name info)) class)
775 ;; Set FDEFINITIONs for structure accessors, setters, predicates,
778 (unless (eq (dd-type info) 'funcallable-structure)
780 (dolist (slot (dd-slots info))
782 (when (and (dsd-accessor-name slot)
783 (eq (dsd-raw-type slot) t))
784 (protect-cl (dsd-accessor-name slot))
785 (setf (symbol-function (dsd-accessor-name slot))
786 (structure-slot-getter layout dsd))
787 (unless (dsd-read-only slot)
788 (setf (fdefinition `(setf ,(dsd-accessor-name slot)))
789 (structure-slot-setter layout dsd))))))
791 ;; FIXME: Someday it'd probably be good to go back to using
792 ;; closures for the out-of-line forms of structure accessors.
793 ;; See comment on corresponding code in %%COMPILER-TRULY-DEFSTRUCT.
795 (when (dd-predicate info)
796 (protect-cl (dd-predicate info))
797 (setf (symbol-function (dd-predicate info))
799 (declare (optimize (speed 3) (safety 0)))
800 (typep-to-layout object layout))))
803 (when (dd-copier info)
804 (protect-cl (dd-copier info))
805 (setf (symbol-function (dd-copier info))
806 #'(lambda (structure)
807 (declare (optimize (speed 3) (safety 0)))
808 (flet ((layout-test (structure)
809 (typep-to-layout structure layout)))
810 (unless (layout-test structure)
811 (error 'simple-type-error
813 :expected-type '(satisfies layout-test)
815 "Structure for copier is not a ~S:~% ~S"
817 (list (sb!xc:class-name (layout-class layout))
819 (copy-structure structure))))))
822 (setf (fdocumentation (dd-name info) 'type) (dd-doc info)))
826 ;;; Do compile-time actions for DEFSTRUCT.
827 (defun %compiler-defstruct (info inherits)
828 (multiple-value-bind (class layout old-layout)
829 (multiple-value-bind (clayout clayout-p)
830 (info :type :compiler-layout (dd-name info))
831 (ensure-structure-class info
833 (if clayout-p "previously compiled" "current")
835 :compiler-layout clayout))
837 (undefine-structure (layout-class old-layout))
838 (when (and (class-subclasses class)
839 (not (eq layout old-layout)))
841 (dohash (class layout (class-subclasses class))
842 (declare (ignore layout))
843 (undefine-structure class)
844 (subs (class-proper-name class)))
846 (warn "Removing old subclasses of ~S:~% ~S"
847 (sb!xc:class-name class)
850 (unless (eq (class-layout class) layout)
851 (register-layout layout :invalidate nil))
852 (setf (sb!xc:find-class (dd-name info)) class)))
854 (setf (info :type :compiler-layout (dd-name info)) layout))
857 ;;; Do (COMPILE LOAD EVAL) time actions for updating the compiler's
858 ;;; global meta-information to represent the definition of a structure
859 ;;; (truly a structure, not just DEFSTRUCT :TYPE VECTOR or DEFSTRUCT
860 ;;; :TYPE LIST) described by INFO.
861 (defun %compiler-truly-defstruct (info)
862 (declare (type defstruct-description info))
863 (let* ((name (dd-name info))
864 (class (sb!xc:find-class name)))
866 (let ((copier (dd-copier info)))
868 (proclaim `(ftype (function (,name) ,name) ,copier))))
870 ;; FIXME: This (and corresponding code in %DEFSTRUCT) are the way
871 ;; that CMU CL defined the predicate, instead of using DEFUN.
872 ;; Perhaps it would be better to go back to to the CMU CL way, or
873 ;; something similar. I want to reduce the amount of magic in
874 ;; DEFSTRUCT functions, but making the predicate be a closure
875 ;; looks like a good thing, and can even be done without magic.
876 ;; (OTOH, there are some bootstrapping issues involved, since
877 ;; GENESIS understands DEFUN but doesn't understand a
878 ;; (SETF SYMBOL-FUNCTION) call inside %DEFSTRUCT.)
880 (let ((predicate-name (dd-predicate-name info)))
882 (proclaim-as-defstruct-function-name predicate-name)
883 (setf (info :function :inlinep pred) :inline)
884 (setf (info :function :inline-expansion predicate-name)
885 `(lambda (x) (typep x ',name)))))
888 (dolist (slot (dd-slots info))
889 (let* ((fun (dsd-accessor-name slot))
890 (setf-fun `(setf ,fun)))
891 (when (and fun (eq (dsd-raw-type slot) t))
892 (proclaim-as-defstruct-function-name fun)
893 (setf (info :function :accessor-for fun) class)
894 (unless (dsd-read-only slot)
895 (proclaim-as-defstruct-function-name setf-fun)
896 (setf (info :function :accessor-for setf-fun) class)))))
898 ;; FIXME: Couldn't this logic be merged into
899 ;; PROCLAIM-AS-DEFSTRUCT-FUNCTION?
900 (when (boundp 'sb!c:*free-functions*) ; when compiling
901 (let ((free-functions sb!c:*free-functions*))
902 (dolist (slot (dd-slots info))
903 (let ((accessor-name (dsd-accessor-name slot)))
904 (remhash accessor-name free-functions)
905 (unless (dsd-read-only slot)
906 (remhash `(setf ,accessor-name) free-functions))))
907 (remhash (dd-predicate-name info) free-functions)
908 (remhash (dd-copier info) free-functions))))
912 ;;;; redefinition stuff
914 ;;; Compare the slots of OLD and NEW, returning 3 lists of slot names:
915 ;;; 1. Slots which have moved,
916 ;;; 2. Slots whose type has changed,
917 ;;; 3. Deleted slots.
918 (defun compare-slots (old new)
919 (let* ((oslots (dd-slots old))
920 (nslots (dd-slots new))
921 (onames (mapcar #'dsd-name oslots))
922 (nnames (mapcar #'dsd-name nslots)))
925 (dolist (name (intersection onames nnames))
926 (let ((os (find name oslots :key #'dsd-name))
927 (ns (find name nslots :key #'dsd-name)))
928 (unless (subtypep (dsd-type ns) (dsd-type os))
929 (/noshow "found retyped slots" ns os (dsd-type ns) (dsd-type os))
931 (unless (and (= (dsd-index os) (dsd-index ns))
932 (eq (dsd-raw-type os) (dsd-raw-type ns)))
936 (set-difference onames nnames)))))
938 ;;; If we are redefining a structure with different slots than in the
939 ;;; currently loaded version, give a warning and return true.
940 (defun redefine-structure-warning (class old new)
941 (declare (type defstruct-description old new)
942 (type sb!xc:class class)
944 (let ((name (dd-name new)))
945 (multiple-value-bind (moved retyped deleted) (compare-slots old new)
946 (when (or moved retyped deleted)
948 "incompatibly redefining slots of structure class ~S~@
949 Make sure any uses of affected accessors are recompiled:~@
950 ~@[ These slots were moved to new positions:~% ~S~%~]~
951 ~@[ These slots have new incompatible types:~% ~S~%~]~
952 ~@[ These slots were deleted:~% ~S~%~]"
953 name moved retyped deleted)
956 ;;; This function is called when we are incompatibly redefining a
957 ;;; structure Class to have the specified New-Layout. We signal an
958 ;;; error with some proceed options and return the layout that should
960 (defun %redefine-defstruct (class old-layout new-layout)
961 (declare (type sb!xc:class class) (type layout old-layout new-layout))
962 (let ((name (class-proper-name class)))
964 (error "redefining class ~S incompatibly with the current definition"
967 :report "Invalidate current definition."
968 (warn "Previously loaded ~S accessors will no longer work." name)
969 (register-layout new-layout))
971 :report "Smash current layout, preserving old code."
972 (warn "Any old ~S instances will be in a bad way.~@
973 I hope you know what you're doing..."
975 (register-layout new-layout :invalidate nil
976 :destruct-layout old-layout))))
979 ;;; This is called when we are about to define a structure class. It
980 ;;; returns a (possibly new) class object and the layout which should
981 ;;; be used for the new definition (may be the current layout, and
982 ;;; also might be an uninstalled forward referenced layout.) The third
983 ;;; value is true if this is an incompatible redefinition, in which
984 ;;; case it is the old layout.
985 (defun ensure-structure-class (info inherits old-context new-context
986 &key compiler-layout)
987 (multiple-value-bind (class old-layout)
991 (class 'sb!xc:structure-class)
992 (constructor 'make-structure-class))
993 (dd-alternate-metaclass info)
994 (declare (ignore name))
995 (insured-find-class (dd-name info)
996 (if (eq class 'sb!xc:structure-class)
998 (typep x 'sb!xc:structure-class))
1000 (sb!xc:typep x (sb!xc:find-class class))))
1001 (fdefinition constructor)))
1002 (setf (class-direct-superclasses class)
1003 (if (eq (dd-name info) 'lisp-stream)
1004 ;; a hack to add STREAM as a superclass mixin to LISP-STREAMs
1005 (list (layout-class (svref inherits (1- (length inherits))))
1006 (layout-class (svref inherits (- (length inherits) 2))))
1007 (list (layout-class (svref inherits (1- (length inherits)))))))
1008 (let ((new-layout (make-layout :class class
1010 :depthoid (length inherits)
1011 :length (dd-length info)
1013 (old-layout (or compiler-layout old-layout)))
1016 (values class new-layout nil))
1017 (;; This clause corresponds to an assertion in REDEFINE-LAYOUT-WARNING
1018 ;; of classic CMU CL. I moved it out to here because it was only
1019 ;; exercised in this code path anyway. -- WHN 19990510
1020 (not (eq (layout-class new-layout) (layout-class old-layout)))
1021 (error "shouldn't happen: weird state of OLD-LAYOUT?"))
1022 ((not *type-system-initialized*)
1023 (setf (layout-info old-layout) info)
1024 (values class old-layout nil))
1025 ((redefine-layout-warning old-context
1028 (layout-length new-layout)
1029 (layout-inherits new-layout)
1030 (layout-depthoid new-layout))
1031 (values class new-layout old-layout))
1033 (let ((old-info (layout-info old-layout)))
1035 ((or defstruct-description)
1036 (cond ((redefine-structure-warning class old-info info)
1037 (values class new-layout old-layout))
1039 (setf (layout-info old-layout) info)
1040 (values class old-layout nil))))
1042 (setf (layout-info old-layout) info)
1043 (values class old-layout nil))
1045 (error "shouldn't happen! strange thing in LAYOUT-INFO:~% ~S"
1047 (values class new-layout old-layout)))))))))
1049 ;;; Blow away all the compiler info for the structure CLASS. Iterate
1050 ;;; over this type, clearing the compiler structure type info, and
1051 ;;; undefining all the associated functions.
1052 (defun undefine-structure (class)
1053 (let ((info (layout-info (class-layout class))))
1054 (when (defstruct-description-p info)
1055 (let ((type (dd-name info)))
1056 (setf (info :type :compiler-layout type) nil)
1057 (undefine-function-name (dd-copier info))
1058 (undefine-function-name (dd-predicate-name info))
1059 (dolist (slot (dd-slots info))
1060 (let ((fun (dsd-accessor-name slot)))
1061 (undefine-function-name fun)
1062 (unless (dsd-read-only slot)
1063 (undefine-function-name `(setf ,fun))))))
1064 ;; Clear out the SPECIFIER-TYPE cache so that subsequent
1065 ;; references are unknown types.
1066 (values-specifier-type-cache-clear)))
1069 ;;; Return a list of pairs (name . index). Used for :TYPE'd
1070 ;;; constructors to find all the names that we have to splice in &
1071 ;;; where. Note that these types don't have a layout, so we can't look
1072 ;;; at LAYOUT-INHERITS.
1073 (defun find-name-indices (defstruct)
1076 (do ((info defstruct
1077 (typed-structure-info-or-lose (first (dd-include info)))))
1078 ((not (dd-include info))
1083 (dolist (info infos)
1084 (incf i (or (dd-offset info) 0))
1085 (when (dd-named info)
1086 (res (cons (dd-name info) i)))
1087 (setq i (dd-length info)))))
1091 ;;;; slot accessors for raw slots
1093 ;;; Return info about how to read/write a slot in the value stored in
1094 ;;; OBJECT. This is also used by constructors (we can't use the
1095 ;;; accessor function, since some slots are read-only.) If supplied,
1096 ;;; DATA is a variable holding the raw-data vector.
1098 ;;; returned values:
1099 ;;; 1. accessor function name (SETFable)
1100 ;;; 2. index to pass to accessor.
1101 ;;; 3. object form to pass to accessor
1102 (defun slot-accessor-form (defstruct slot object &optional data)
1103 (let ((rtype (dsd-raw-type slot)))
1106 (single-float '%raw-ref-single)
1107 (double-float '%raw-ref-double)
1109 (long-float '%raw-ref-long)
1110 (complex-single-float '%raw-ref-complex-single)
1111 (complex-double-float '%raw-ref-complex-double)
1113 (complex-long-float '%raw-ref-complex-long)
1114 (unsigned-byte 'aref)
1116 (if (eq (dd-type defstruct) 'funcallable-structure)
1117 '%funcallable-instance-info
1122 (truncate (dsd-index slot) #!+x86 6 #!+sparc 8))
1125 (truncate (dsd-index slot) #!+x86 3 #!+sparc 4))
1127 (ash (dsd-index slot) -1))
1128 (complex-double-float
1129 (ash (dsd-index slot) -2))
1130 (complex-single-float
1131 (ash (dsd-index slot) -1))
1135 ((eq rtype t) object)
1138 `(truly-the (simple-array (unsigned-byte 32) (*))
1139 (%instance-ref ,object ,(dd-raw-index defstruct))))))))
1141 ;;; These functions are called to actually make a constructor after we
1142 ;;; have processed the arglist. The correct variant (according to the
1143 ;;; DD-TYPE) should be called. The function is defined with the
1144 ;;; specified name and arglist. Vars and Types are used for argument
1145 ;;; type declarations. Values are the values for the slots (in order.)
1147 ;;; This is split four ways because:
1148 ;;; 1] list & vector structures need "name" symbols stuck in at
1149 ;;; various weird places, whereas STRUCTURE structures have
1151 ;;; 2] We really want to use LIST to make list structures, instead of
1152 ;;; MAKE-LIST/(SETF ELT).
1153 ;;; 3] STRUCTURE structures can have raw slots that must also be
1154 ;;; allocated and indirectly referenced. We use SLOT-ACCESSOR-FORM
1155 ;;; to compute how to set the slots, which deals with raw slots.
1156 ;;; 4] Funcallable structures are weird.
1157 (defun create-vector-constructor
1158 (defstruct cons-name arglist vars types values)
1159 (let ((temp (gensym))
1160 (etype (dd-element-type defstruct)))
1161 `(defun ,cons-name ,arglist
1162 (declare ,@(mapcar #'(lambda (var type) `(type (and ,type ,etype) ,var))
1164 (let ((,temp (make-array ,(dd-length defstruct)
1165 :element-type ',(dd-element-type defstruct))))
1166 ,@(mapcar #'(lambda (x)
1167 `(setf (aref ,temp ,(cdr x)) ',(car x)))
1168 (find-name-indices defstruct))
1169 ,@(mapcar #'(lambda (dsd value)
1170 `(setf (aref ,temp ,(dsd-index dsd)) ,value))
1171 (dd-slots defstruct) values)
1173 (defun create-list-constructor
1174 (defstruct cons-name arglist vars types values)
1175 (let ((vals (make-list (dd-length defstruct) :initial-element nil)))
1176 (dolist (x (find-name-indices defstruct))
1177 (setf (elt vals (cdr x)) `',(car x)))
1178 (loop for dsd in (dd-slots defstruct) and val in values do
1179 (setf (elt vals (dsd-index dsd)) val))
1181 `(defun ,cons-name ,arglist
1182 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1185 (defun create-structure-constructor
1186 (defstruct cons-name arglist vars types values)
1187 (let* ((temp (gensym))
1188 (raw-index (dd-raw-index defstruct))
1189 (n-raw-data (when raw-index (gensym))))
1190 `(defun ,cons-name ,arglist
1191 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1193 (let ((,temp (truly-the ,(dd-name defstruct)
1194 (%make-instance ,(dd-length defstruct))))
1197 (make-array ,(dd-raw-length defstruct)
1198 :element-type '(unsigned-byte 32))))))
1199 (setf (%instance-layout ,temp)
1200 (%delayed-get-compiler-layout ,(dd-name defstruct)))
1202 `((setf (%instance-ref ,temp ,raw-index) ,n-raw-data)))
1203 ,@(mapcar (lambda (dsd value)
1204 (multiple-value-bind (accessor index data)
1205 (slot-accessor-form defstruct dsd temp n-raw-data)
1206 `(setf (,accessor ,data ,index) ,value)))
1207 (dd-slots defstruct)
1210 (defun create-fin-constructor
1211 (defstruct cons-name arglist vars types values)
1212 (let ((temp (gensym)))
1213 `(defun ,cons-name ,arglist
1214 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1216 (let ((,temp (truly-the
1217 ,(dd-name defstruct)
1218 (%make-funcallable-instance
1219 ,(dd-length defstruct)
1220 (%delayed-get-compiler-layout ,(dd-name defstruct))))))
1221 ,@(mapcar #'(lambda (dsd value)
1222 `(setf (%funcallable-instance-info
1223 ,temp ,(dsd-index dsd))
1225 (dd-slots defstruct) values)
1228 ;;; Create a default (non-BOA) keyword constructor.
1229 (defun create-keyword-constructor (defstruct creator)
1230 (collect ((arglist (list '&key))
1233 (dolist (slot (dd-slots defstruct))
1234 (let ((dum (gensym))
1235 (name (dsd-name slot)))
1236 (arglist `((,(keywordicate name) ,dum) ,(dsd-default slot)))
1237 (types (dsd-type slot))
1240 defstruct (dd-default-constructor defstruct)
1241 (arglist) (vals) (types) (vals))))
1243 ;;; Given a structure and a BOA constructor spec, call CREATOR with
1244 ;;; the appropriate args to make a constructor.
1245 (defun create-boa-constructor (defstruct boa creator)
1246 (multiple-value-bind (req opt restp rest keyp keys allowp aux)
1247 (sb!kernel:parse-lambda-list (second boa))
1251 (labels ((get-slot (name)
1252 (let ((res (find name (dd-slots defstruct)
1256 (values (dsd-type res) (dsd-default res))
1259 (multiple-value-bind (type default) (get-slot arg)
1260 (arglist `(,arg ,default))
1266 (types (get-slot arg)))
1269 (arglist '&optional)
1273 (name &optional (def (nth-value 1 (get-slot name))))
1275 (arglist `(,name ,def))
1277 (types (get-slot name))))
1279 (do-default arg)))))
1282 (arglist '&rest rest)
1290 (destructuring-bind (wot &optional (def nil def-p)) key
1291 (let ((name (if (consp wot)
1292 (destructuring-bind (key var) wot
1293 (declare (ignore key))
1296 (multiple-value-bind (type slot-def) (get-slot name)
1297 (arglist `(,wot ,(if def-p def slot-def)))
1302 (when allowp (arglist '&allow-other-keys))
1307 (let* ((arg (if (consp arg) arg (list arg)))
1311 (types (get-slot var))))))
1313 (funcall creator defstruct (first boa)
1314 (arglist) (vars) (types)
1315 (mapcar #'(lambda (slot)
1316 (or (find (dsd-name slot) (vars) :test #'string=)
1317 (dsd-default slot)))
1318 (dd-slots defstruct))))))
1320 ;;; Grovel the constructor options, and decide what constructors (if
1322 (defun constructor-definitions (defstruct)
1323 (let ((no-constructors nil)
1326 (creator (ecase (dd-type defstruct)
1327 (structure #'create-structure-constructor)
1328 (funcallable-structure #'create-fin-constructor)
1329 (vector #'create-vector-constructor)
1330 (list #'create-list-constructor))))
1331 (dolist (constructor (dd-constructors defstruct))
1332 (destructuring-bind (name &optional (boa-ll nil boa-p)) constructor
1333 (declare (ignore boa-ll))
1334 (cond ((not name) (setq no-constructors t))
1335 (boa-p (push constructor boas))
1336 (t (push name defaults)))))
1338 (when no-constructors
1339 (when (or defaults boas)
1340 (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs"))
1341 (return-from constructor-definitions ()))
1343 (unless (or defaults boas)
1344 (push (symbolicate "MAKE-" (dd-name defstruct)) defaults))
1348 (let ((cname (first defaults)))
1349 (setf (dd-default-constructor defstruct) cname)
1350 (res (create-keyword-constructor defstruct creator))
1351 (dolist (other-name (rest defaults))
1352 (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1353 (res `(declaim (ftype function ',other-name))))))
1356 (res (create-boa-constructor defstruct boa creator)))
1362 ;;; This is like PROCLAIM-AS-FUNCTION-NAME, but we also set the kind to
1363 ;;; :DECLARED and blow away any ASSUMED-TYPE. Also, if the thing is a
1364 ;;; slot accessor currently, quietly unaccessorize it. And if there
1365 ;;; are any undefined warnings, we nuke them.
1366 (defun proclaim-as-defstruct-function-name (name)
1368 (when (info :function :accessor-for name)
1369 (setf (info :function :accessor-for name) nil))
1370 (proclaim-as-function-name name)
1371 (note-name-defined name :function)
1372 (setf (info :function :where-from name) :declared)
1373 (when (info :function :assumed-type name)
1374 (setf (info :function :assumed-type name) nil)))
1377 ;;;; finalizing bootstrapping
1379 ;;; early structure placeholder definitions: Set up layout and class
1380 ;;; data for structures which are needed early.
1382 '#.(sb-cold:read-from-file
1383 "src/code/early-defstruct-args.lisp-expr"))
1384 (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
1387 (inherits (inherits-for-structure dd)))
1388 (%compiler-defstruct dd inherits)))
1390 (/show0 "code/defstruct.lisp end of file")