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 (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 dsd)
158 (symbol-package (dsd-accessor 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 special,
219 ;;; just ordinary inline functions. (Or perhaps inline functions with special
220 ;;; compact implementations of their expansions, to avoid bloating the system.)
222 ;;; shared logic for CL:DEFSTRUCT and SB!XC:DEFSTRUCT
224 ;;; FIXME: There should be some way to make this not be present in the
225 ;;; target executable, with EVAL-WHEN (COMPILE EXECUTE) and all that good
226 ;;; stuff, but for now I can't be bothered because of the messiness of
227 ;;; using CL:DEFMACRO in one case and SB!XC:DEFMACRO in another case.
228 ;;; Perhaps I could dodge this by defining it as an inline function instead?
229 ;;; Or perhaps just use MACROLET? I tried MACROLET and got nowhere and thought
230 ;;; I was tripping over either a compiler bug or ANSI weirdness, but this
231 ;;; test case seems to work in Debian CMU CL 2.4.9:
232 ;;; (macrolet ((emit-printer () ''(print "********")))
233 ;;; (defmacro fizz () (emit-printer)))
239 (defmacro expander-for-defstruct (name-and-options
241 expanding-into-code-for-xc-host-p)
242 `(let ((name-and-options ,name-and-options)
243 (slot-descriptions ,slot-descriptions)
244 (expanding-into-code-for-xc-host-p
245 ,expanding-into-code-for-xc-host-p))
246 (let* ((dd (parse-name-and-options-and-slot-descriptions
250 (if (class-structure-p dd)
251 (let ((inherits (inherits-for-structure dd)))
253 (/noshow0 "doing CLASS-STRUCTURE-P case for DEFSTRUCT " ,name)
254 (eval-when (:compile-toplevel :load-toplevel :execute)
255 (%compiler-only-defstruct ',dd ',inherits))
256 (%defstruct ',dd ',inherits)
257 ,@(when (eq (dd-type dd) 'structure)
258 `((%compiler-defstruct ',dd)))
259 (/noshow0 "starting not-for-the-xc-host section in DEFSTRUCT")
260 ,@(unless expanding-into-code-for-xc-host-p
261 (append (raw-accessor-definitions dd)
262 (predicate-definitions dd)
263 ;; FIXME: We've inherited from CMU CL nonparallel
264 ;; code for creating copiers for typed and untyped
265 ;; structures. This should be fixed.
266 ;(copier-definition dd)
267 (constructor-definitions dd)
268 (class-method-definitions dd)))
269 (/noshow0 "done with DEFSTRUCT " ,name)
272 (/show0 "doing NOT CLASS-STRUCTURE-P case for DEFSTRUCT " ,name)
273 (eval-when (:compile-toplevel :load-toplevel :execute)
274 (setf (info :typed-structure :info ',name) ',dd))
275 ,@(unless expanding-into-code-for-xc-host-p
276 (append (typed-accessor-definitions dd)
277 (typed-predicate-definitions dd)
278 (typed-copier-definitions dd)
279 (constructor-definitions dd)))
280 (/noshow0 "done with DEFSTRUCT " ,name)
283 (sb!xc:defmacro defstruct (name-and-options &rest slot-descriptions)
285 "DEFSTRUCT {Name | (Name Option*)} {Slot | (Slot [Default] {Key Value}*)}
286 Define the structure type Name. Instances are created by MAKE-<name>,
287 which takes &KEY arguments allowing initial slot values to the specified.
288 A SETF'able function <name>-<slot> is defined for each slot to read and
289 write slot values. <name>-p is a type predicate.
291 Popular DEFSTRUCT options (see manual for others):
295 Specify the name for the constructor or predicate.
297 (:CONSTRUCTOR Name Lambda-List)
298 Specify the name and arguments for a BOA constructor
299 (which is more efficient when keyword syntax isn't necessary.)
301 (:INCLUDE Supertype Slot-Spec*)
302 Make this type a subtype of the structure type Supertype. The optional
303 Slot-Specs override inherited slot options.
308 Asserts that the value of this slot is always of the specified type.
311 If true, no setter function is defined for this slot."
312 (expander-for-defstruct name-and-options slot-descriptions nil))
314 (defmacro sb!xc:defstruct (name-and-options &rest slot-descriptions)
316 "Cause information about a target structure to be built into the
318 (expander-for-defstruct name-and-options slot-descriptions t))
320 ;;;; functions to create various parts of DEFSTRUCT definitions
322 ;;; Catch requests to mess up definitions in COMMON-LISP.
324 (eval-when (:compile-toplevel :load-toplevel :execute)
325 (defun protect-cl (symbol)
326 (when (and *cold-init-complete-p*
327 (eq (symbol-package symbol) *cl-package*))
328 (cerror "Go ahead and patch the system."
329 "attempting to modify a symbol in the COMMON-LISP package: ~S"
332 ;;; Return forms to define readers and writers for raw slots as inline
334 (defun raw-accessor-definitions (dd)
335 (let* ((name (dd-name dd)))
337 (dolist (slot (dd-slots dd))
338 (let ((stype (dsd-type slot))
339 (accname (dsd-accessor slot))
340 (argname (gensym "ARG"))
341 (nvname (gensym "NEW-VALUE-")))
342 (multiple-value-bind (accessor offset data)
343 (slot-accessor-form dd slot argname)
344 ;; When accessor exists and is raw
345 (when (and accname (not (eq accessor '%instance-ref)))
346 (res `(declaim (inline ,accname)))
347 (res `(declaim (ftype (function (,name) ,stype) ,accname)))
348 (res `(defun ,accname (,argname)
349 (truly-the ,stype (,accessor ,data ,offset))))
350 (unless (dsd-read-only slot)
351 (res `(declaim (inline (setf ,accname))))
352 (res `(declaim (ftype (function (,stype ,name) ,stype)
354 ;; FIXME: I rewrote this somewhat from the CMU CL definition.
355 ;; Do some basic tests to make sure that reading and writing
356 ;; raw slots still works correctly.
357 (res `(defun (setf ,accname) (,nvname ,argname)
358 (setf (,accessor ,data ,offset) ,nvname)
362 ;;; Return a list of forms which create a predicate for an untyped DEFSTRUCT.
363 (defun predicate-definitions (dd)
364 (let ((pred (dd-predicate dd))
367 (if (eq (dd-type dd) 'funcallable-structure)
368 ;; FIXME: Why does this need to be special-cased for
369 ;; FUNCALLABLE-STRUCTURE? CMU CL did it, but without explanation.
370 ;; Could we do without it? What breaks if we do? Or could we
371 ;; perhaps get by with no predicates for funcallable structures?
372 `((declaim (inline ,pred))
373 (defun ,pred (,argname) (typep ,argname ',(dd-name dd))))
374 `((protect-cl ',pred)
375 (declaim (inline ,pred))
376 (defun ,pred (,argname)
377 (declare (optimize (speed 3) (safety 0)))
378 (typep-to-layout ,argname
379 (compile-time-find-layout ,(dd-name dd)))))))))
381 ;;; Return a list of forms which create a predicate function for a typed
383 (defun typed-predicate-definitions (defstruct)
384 (let ((name (dd-name defstruct))
385 (pred (dd-predicate defstruct))
387 (when (and pred (dd-named defstruct))
388 (let ((ltype (dd-lisp-type defstruct)))
389 `((defun ,pred (,argname)
390 (and (typep ,argname ',ltype)
391 (eq (elt (the ,ltype ,argname)
392 ,(cdr (car (last (find-name-indices defstruct)))))
395 ;;; FIXME: We've inherited from CMU CL code to do typed structure copiers
396 ;;; in a completely different way than untyped structure copiers. Fix this.
397 ;;; (This function was my first attempt to fix this, but I stopped before
398 ;;; figuring out how to install it completely and remove the parallel
399 ;;; code which simply SETF's the FDEFINITION of the DD-COPIER name.
401 ;;; Return the copier definition for an untyped DEFSTRUCT.
402 (defun copier-definition (dd)
403 (when (and (dd-copier dd)
404 ;; FUNCALLABLE-STRUCTUREs don't need copiers, and this
405 ;; implementation wouldn't work for them anyway, since
406 ;; COPY-STRUCTURE returns a STRUCTURE-OBJECT and they're not.
407 (not (eq (dd-type info) 'funcallable-structure)))
408 (let ((argname (gensym)))
410 (protect-cl ',(dd-copier dd))
411 (defun ,(dd-copier dd) (,argname)
412 (declare (type ,(dd-name dd) ,argname))
413 (copy-structure ,argname))))))
416 ;;; Return a list of forms to create a copier function of a typed DEFSTRUCT.
417 (defun typed-copier-definitions (defstruct)
418 (when (dd-copier defstruct)
419 `((setf (fdefinition ',(dd-copier defstruct)) #'copy-seq)
420 (declaim (ftype function ,(dd-copier defstruct))))))
422 ;;; Return a list of function definitions for accessing and setting the
423 ;;; slots of a typed DEFSTRUCT. The functions are proclaimed to be inline,
424 ;;; and the types of their arguments and results are declared as well. We
425 ;;; count on the compiler to do clever things with ELT.
426 (defun typed-accessor-definitions (defstruct)
428 (let ((ltype (dd-lisp-type defstruct)))
429 (dolist (slot (dd-slots defstruct))
430 (let ((name (dsd-accessor slot))
431 (index (dsd-index slot))
432 (slot-type `(and ,(dsd-type slot)
433 ,(dd-element-type defstruct))))
434 (stuff `(proclaim '(inline ,name (setf ,name))))
435 ;; FIXME: The arguments in the next two DEFUNs should be
436 ;; gensyms. (Otherwise e.g. if NEW-VALUE happened to be the
437 ;; name of a special variable, things could get weird.)
438 (stuff `(defun ,name (structure)
439 (declare (type ,ltype structure))
440 (the ,slot-type (elt structure ,index))))
441 (unless (dsd-read-only slot)
443 `(defun (setf ,name) (new-value structure)
444 (declare (type ,ltype structure) (type ,slot-type new-value))
445 (setf (elt structure ,index) new-value)))))))
450 (defun require-no-print-options-so-far (defstruct)
451 (unless (and (eql (dd-print-function defstruct) 0)
452 (eql (dd-print-object defstruct) 0))
453 (error "no more than one of the following options may be specified:
454 :PRINT-FUNCTION, :PRINT-OBJECT, :TYPE")))
456 ;;; Parse a single defstruct option and store the results in DEFSTRUCT.
457 (defun parse-1-option (option defstruct)
458 (let ((args (rest option))
459 (name (dd-name defstruct)))
462 (destructuring-bind (conc-name) args
463 (setf (dd-conc-name defstruct)
464 (if (symbolp conc-name)
466 (make-symbol (string conc-name))))))
468 (destructuring-bind (&optional (cname (symbolicate "MAKE-" name))
471 (push (cons cname stuff) (dd-constructors defstruct))))
473 (destructuring-bind (&optional (copier (symbolicate "COPY-" name)))
475 (setf (dd-copier defstruct) copier)))
477 (destructuring-bind (&optional (pred (symbolicate name "-P"))) args
478 (setf (dd-predicate defstruct) pred)))
480 (when (dd-include defstruct)
481 (error "more than one :INCLUDE option"))
482 (setf (dd-include defstruct) args))
483 (:alternate-metaclass
484 (setf (dd-alternate-metaclass defstruct) args))
486 (require-no-print-options-so-far defstruct)
487 (setf (dd-print-function defstruct)
488 (the (or symbol cons) args)))
490 (require-no-print-options-so-far defstruct)
491 (setf (dd-print-object defstruct)
492 (the (or symbol cons) args)))
494 (destructuring-bind (type) args
495 (cond ((eq type 'funcallable-structure)
496 (setf (dd-type defstruct) type))
497 ((member type '(list vector))
498 (setf (dd-element-type defstruct) t)
499 (setf (dd-type defstruct) type))
500 ((and (consp type) (eq (first type) 'vector))
501 (destructuring-bind (vector vtype) type
502 (declare (ignore vector))
503 (setf (dd-element-type defstruct) vtype)
504 (setf (dd-type defstruct) 'vector)))
506 (error "~S is a bad :TYPE for Defstruct." type)))))
508 (error "The DEFSTRUCT option :NAMED takes no arguments."))
510 (destructuring-bind (offset) args
511 (setf (dd-offset defstruct) offset)))
513 (destructuring-bind (fun) args
514 (setf (dd-pure defstruct) fun)))
515 (t (error "unknown DEFSTRUCT option:~% ~S" option)))))
517 ;;; Given name and options, return a DD holding that info.
518 (eval-when (:compile-toplevel :load-toplevel :execute)
519 (defun parse-name-and-options (name-and-options)
520 (destructuring-bind (name &rest options) name-and-options
521 (aver name) ; A null name doesn't seem to make sense here.
522 (let ((defstruct (make-defstruct-description name)))
523 (dolist (option options)
524 (cond ((consp option)
525 (parse-1-option option defstruct))
527 (setf (dd-named defstruct) t))
528 ((member option '(:constructor :copier :predicate :named))
529 (parse-1-option (list option) defstruct))
531 (error "unrecognized DEFSTRUCT option: ~S" option))))
533 (case (dd-type defstruct)
535 (when (dd-offset defstruct)
536 (error ":OFFSET can't be specified unless :TYPE is specified."))
537 (unless (dd-include defstruct)
538 (incf (dd-length defstruct))))
539 (funcallable-structure)
541 (require-no-print-options-so-far defstruct)
542 (when (dd-named defstruct)
543 (incf (dd-length defstruct)))
544 (let ((offset (dd-offset defstruct)))
545 (when offset (incf (dd-length defstruct) offset)))))
547 (when (dd-include defstruct)
548 (do-inclusion-stuff defstruct))
552 ;;; Given name and options and slot descriptions (and possibly doc
553 ;;; string at the head of slot descriptions) return a DD holding that
555 (defun parse-name-and-options-and-slot-descriptions (name-and-options
557 (/noshow "PARSE-NAME-AND-OPTIONS-AND-SLOT-DESCRIPTIONS" name-and-options)
558 (let ((result (parse-name-and-options (if (atom name-and-options)
559 (list name-and-options)
561 (when (stringp (car slot-descriptions))
562 (setf (dd-doc result) (pop slot-descriptions)))
563 (dolist (slot slot-descriptions)
564 (allocate-1-slot result (parse-1-dsd result slot)))
569 ;;;; stuff to parse slot descriptions
571 ;;; Parse a slot description for DEFSTRUCT, add it to the description
572 ;;; and return it. If supplied, ISLOT is a pre-initialized DSD that we
573 ;;; modify to get the new slot. This is supplied when handling
575 (defun parse-1-dsd (defstruct spec &optional
576 (islot (make-defstruct-slot-description :%name ""
579 (multiple-value-bind (name default default-p type type-p read-only ro-p)
584 &optional (default nil default-p)
585 &key (type nil type-p) (read-only nil ro-p))
589 (uncross type) type-p
592 (when (keywordp spec)
593 (style-warn "Keyword slot name indicates probable syntax ~
594 error in DEFSTRUCT: ~S."
598 (when (find name (dd-slots defstruct) :test #'string= :key #'dsd-%name)
599 (error 'simple-program-error
600 :format-control "duplicate slot name ~S"
601 :format-arguments (list name)))
602 (setf (dsd-%name islot) (string name))
603 (setf (dd-slots defstruct) (nconc (dd-slots defstruct) (list islot)))
605 (let ((accessor-name (symbolicate (or (dd-conc-name defstruct) "") name))
606 (predicate-name (dd-predicate defstruct)))
607 (setf (dsd-accessor islot) accessor-name)
608 (when (eql accessor-name predicate-name)
609 ;; Some adventurous soul has named a slot so that its accessor
610 ;; collides with the structure type predicate. ANSI doesn't
611 ;; specify what to do in this case. As of 2001-09-04, Martin
612 ;; Atzmueller reports that CLISP and Lispworks both give
613 ;; priority to the slot accessor, so that the predicate is
614 ;; overwritten. We might as well do the same (as well as
615 ;; signalling a warning).
617 "~@<The structure accessor name ~S is the same as the name of the ~
618 structure type predicate. ANSI doesn't specify what to do in ~
619 this case; this implementation chooses to overwrite the type ~
620 predicate with the slot accessor.~@:>"
622 (setf (dd-predicate defstruct) nil)))
625 (setf (dsd-default islot) default))
627 (setf (dsd-type islot)
628 (if (eq (dsd-type islot) t)
630 `(and ,(dsd-type islot) ,type))))
633 (setf (dsd-read-only islot) t)
634 (when (dsd-read-only islot)
635 (error "Slot ~S is :READ-ONLY in parent and must be :READ-ONLY in subtype ~S."
640 ;;; When a value of type TYPE is stored in a structure, should it be
641 ;;; stored in a raw slot? Return (VALUES RAW? RAW-TYPE WORDS), where
642 ;;; RAW? is true if TYPE should be stored in a raw slot.
643 ;;; RAW-TYPE is the raw slot type, or NIL if no raw slot.
644 ;;; WORDS is the number of words in the raw slot, or NIL if no raw slot.
645 (defun structure-raw-slot-type-and-size (type)
646 (/noshow "in STRUCTURE-RAW-SLOT-TYPE-AND-SIZE" type (sb!xc:subtypep type 'fixnum))
648 (;; FIXME: For now we suppress raw slots, since there are various
649 ;; issues about the way that the cross-compiler handles them.
650 (not (boundp '*dummy-placeholder-to-stop-compiler-warnings*))
651 (values nil nil nil))
652 ((and (sb!xc:subtypep type '(unsigned-byte 32))
653 (multiple-value-bind (fixnum? fixnum-certain?)
654 (sb!xc:subtypep type 'fixnum)
655 (/noshow fixnum? fixnum-certain?)
656 ;; (The extra test for FIXNUM-CERTAIN? here is
657 ;; intended for bootstrapping the system. In
658 ;; particular, in sbcl-0.6.2, we set up LAYOUT before
659 ;; FIXNUM is defined, and so could bogusly end up
660 ;; putting INDEX-typed values into raw slots if we
661 ;; didn't test FIXNUM-CERTAIN?.)
662 (and (not fixnum?) fixnum-certain?)))
663 (values t 'unsigned-byte 1))
664 ((sb!xc:subtypep type 'single-float)
665 (values t 'single-float 1))
666 ((sb!xc:subtypep type 'double-float)
667 (values t 'double-float 2))
669 ((sb!xc:subtypep type 'long-float)
670 (values t 'long-float #!+x86 3 #!+sparc 4))
671 ((sb!xc:subtypep type '(complex single-float))
672 (values t 'complex-single-float 2))
673 ((sb!xc:subtypep type '(complex double-float))
674 (values t 'complex-double-float 4))
676 ((sb!xc:subtypep type '(complex long-float))
677 (values t 'complex-long-float #!+x86 6 #!+sparc 8))
679 (values nil nil nil))))
681 ;;; Allocate storage for a DSD in DEFSTRUCT. This is where we decide
682 ;;; whether a slot is raw or not. If raw, and we haven't allocated a
683 ;;; raw-index yet for the raw data vector, then do it. Raw objects are
684 ;;; aligned on the unit of their size.
685 (defun allocate-1-slot (defstruct dsd)
686 (multiple-value-bind (raw? raw-type words)
687 (if (eq (dd-type defstruct) 'structure)
688 (structure-raw-slot-type-and-size (dsd-type dsd))
689 (values nil nil nil))
690 (/noshow "ALLOCATE-1-SLOT" dsd raw? raw-type words)
692 (setf (dsd-index dsd) (dd-length defstruct))
693 (incf (dd-length defstruct)))
695 (unless (dd-raw-index defstruct)
696 (setf (dd-raw-index defstruct) (dd-length defstruct))
697 (incf (dd-length defstruct)))
698 (let ((off (rem (dd-raw-length defstruct) words)))
700 (incf (dd-raw-length defstruct) (- words off))))
701 (setf (dsd-raw-type dsd) raw-type)
702 (setf (dsd-index dsd) (dd-raw-length defstruct))
703 (incf (dd-raw-length defstruct) words))))
706 (defun typed-structure-info-or-lose (name)
707 (or (info :typed-structure :info name)
708 (error ":TYPE'd DEFSTRUCT ~S not found for inclusion." name)))
710 ;;; Process any included slots pretty much like they were specified.
711 ;;; Also inherit various other attributes.
712 (defun do-inclusion-stuff (defstruct)
714 (included-name &rest modified-slots)
715 (dd-include defstruct)
716 (let* ((type (dd-type defstruct))
718 (if (class-structure-p defstruct)
719 (layout-info (compiler-layout-or-lose included-name))
720 (typed-structure-info-or-lose included-name))))
721 (unless (and (eq type (dd-type included-structure))
722 (type= (specifier-type (dd-element-type included-structure))
723 (specifier-type (dd-element-type defstruct))))
724 (error ":TYPE option mismatch between structures ~S and ~S."
725 (dd-name defstruct) included-name))
727 (incf (dd-length defstruct) (dd-length included-structure))
728 (when (class-structure-p defstruct)
729 (let ((mc (rest (dd-alternate-metaclass included-structure))))
730 (when (and mc (not (dd-alternate-metaclass defstruct)))
731 (setf (dd-alternate-metaclass defstruct)
732 (cons included-name mc))))
733 (when (eq (dd-pure defstruct) :unspecified)
734 (setf (dd-pure defstruct) (dd-pure included-structure)))
735 (setf (dd-raw-index defstruct) (dd-raw-index included-structure))
736 (setf (dd-raw-length defstruct) (dd-raw-length included-structure)))
738 (dolist (islot (dd-slots included-structure))
739 (let* ((iname (dsd-name islot))
740 (modified (or (find iname modified-slots
741 :key #'(lambda (x) (if (atom x) x (car x)))
744 (parse-1-dsd defstruct modified (copy-structure islot)))))))
746 ;;; This function is called at macroexpand time to compute the INHERITS
747 ;;; vector for a structure type definition.
748 (defun inherits-for-structure (info)
749 (declare (type defstruct-description info))
750 (let* ((include (dd-include info))
751 (superclass-opt (dd-alternate-metaclass info))
754 (compiler-layout-or-lose (first include))
755 (class-layout (sb!xc:find-class
756 (or (first superclass-opt)
757 'structure-object))))))
758 (if (eq (dd-name info) 'lisp-stream)
759 ;; a hack to added the stream class as a mixin for LISP-STREAMs
760 (concatenate 'simple-vector
761 (layout-inherits super)
763 (class-layout (sb!xc:find-class 'stream))))
764 (concatenate 'simple-vector
765 (layout-inherits super)
768 ;;; Do miscellaneous (LOAD EVAL) time actions for the structure
769 ;;; described by INFO. Create the class & layout, checking for
770 ;;; incompatible redefinition. Define setters, accessors, copier,
771 ;;; predicate, documentation, instantiate definition in load-time env.
772 ;;; This is only called for default structures.
773 (defun %defstruct (info inherits)
774 (declare (type defstruct-description info))
775 (multiple-value-bind (class layout old-layout)
776 (ensure-structure-class info inherits "current" "new")
777 (cond ((not old-layout)
778 (unless (eq (class-layout class) layout)
779 (register-layout layout)))
781 (let ((old-info (layout-info old-layout)))
782 (when (defstruct-description-p old-info)
783 (dolist (slot (dd-slots old-info))
784 (fmakunbound (dsd-accessor slot))
785 (unless (dsd-read-only slot)
786 (fmakunbound `(setf ,(dsd-accessor slot)))))))
787 (%redefine-defstruct class old-layout layout)
788 (setq layout (class-layout class))))
790 (setf (sb!xc:find-class (dd-name info)) class)
792 ;; Set FDEFINITIONs for structure accessors, setters, predicates,
795 (unless (eq (dd-type info) 'funcallable-structure)
797 (dolist (slot (dd-slots info))
799 (when (and (dsd-accessor slot)
800 (eq (dsd-raw-type slot) t))
801 (protect-cl (dsd-accessor slot))
802 (setf (symbol-function (dsd-accessor slot))
803 (structure-slot-getter layout dsd))
804 (unless (dsd-read-only slot)
805 (setf (fdefinition `(setf ,(dsd-accessor slot)))
806 (structure-slot-setter layout dsd))))))
808 ;; FIXME: See comment on corresponding code in %%COMPILER-DEFSTRUCT.
810 (when (dd-predicate info)
811 (protect-cl (dd-predicate info))
812 (setf (symbol-function (dd-predicate info))
814 (declare (optimize (speed 3) (safety 0)))
815 (typep-to-layout object layout))))
818 (when (dd-copier info)
819 (protect-cl (dd-copier info))
820 (setf (symbol-function (dd-copier info))
821 #'(lambda (structure)
822 (declare (optimize (speed 3) (safety 0)))
823 (flet ((layout-test (structure)
824 (typep-to-layout structure layout)))
825 (unless (layout-test structure)
826 (error 'simple-type-error
828 :expected-type '(satisfies layout-test)
830 "Structure for copier is not a ~S:~% ~S"
832 (list (sb!xc:class-name (layout-class layout))
834 (copy-structure structure))))))
837 (setf (fdocumentation (dd-name info) 'type) (dd-doc info)))
841 ;;; This function is called at compile-time to do the
842 ;;; compile-time-only actions for defining a structure type. It
843 ;;; installs the class in the type system in a similar way to
844 ;;; %DEFSTRUCT, but is quieter and safer in the case of redefinition.
846 ;;; The comments for the classic CMU CL version of this function said
847 ;;; that EVAL-WHEN doesn't do the right thing when nested or
848 ;;; non-top-level, and so CMU CL had the function magically called by
849 ;;; the compiler. Unfortunately, this doesn't do the right thing
850 ;;; either: compiling a function (DEFUN FOO () (DEFSTRUCT FOO X Y))
851 ;;; causes the class FOO to become defined, even though FOO is never
852 ;;; loaded or executed. Even more unfortunately, I've been unable to
853 ;;; come up with any EVAL-WHEN tricks which work -- I finally gave up
854 ;;; on this approach when trying to get the system to cross-compile
855 ;;; error.lisp. (Just because I haven't found it doesn't mean that it
856 ;;; doesn't exist, of course. Alas, I continue to have some trouble
857 ;;; understanding compile/load semantics in Common Lisp.) So we
858 ;;; continue to use the IR1 transformation approach, even though it's
859 ;;; known to be buggy. -- WHN 19990507
861 ;;; Basically, this function avoids trashing the compiler by only
862 ;;; actually defining the class if there is no current definition.
863 ;;; Instead, we just set the INFO TYPE COMPILER-LAYOUT. This behavior
864 ;;; is left over from classic CMU CL and may not be necessary in the
865 ;;; new build system. -- WHN 19990507
867 ;;; FUNCTION-%COMPILER-ONLY-DEFSTRUCT is an ordinary function, called
868 ;;; by both the IR1 transform version of %COMPILER-ONLY-DEFSTRUCT and
869 ;;; by the ordinary function version of %COMPILER-ONLY-DEFSTRUCT. (The
870 ;;; ordinary function version is there for the interpreter and for
872 (defun %compiler-only-defstruct (info inherits)
873 (function-%compiler-only-defstruct info inherits))
874 (defun function-%compiler-only-defstruct (info inherits)
875 (multiple-value-bind (class layout old-layout)
876 (multiple-value-bind (clayout clayout-p)
877 (info :type :compiler-layout (dd-name info))
878 (ensure-structure-class info
880 (if clayout-p "previously compiled" "current")
882 :compiler-layout clayout))
884 (undefine-structure (layout-class old-layout))
885 (when (and (class-subclasses class)
886 (not (eq layout old-layout)))
888 (dohash (class layout (class-subclasses class))
889 (declare (ignore layout))
890 (undefine-structure class)
891 (subs (class-proper-name class)))
893 (warn "Removing old subclasses of ~S:~% ~S"
894 (sb!xc:class-name class)
897 (unless (eq (class-layout class) layout)
898 (register-layout layout :invalidate nil))
899 (setf (sb!xc:find-class (dd-name info)) class)))
901 (setf (info :type :compiler-layout (dd-name info)) layout))
904 ;;; This function does the (COMPILE LOAD EVAL) time actions for updating the
905 ;;; compiler's global meta-information to represent the definition of the
906 ;;; structure described by Info. This primarily amounts to setting up info
907 ;;; about the accessor and other implicitly defined functions. The constructors
908 ;;; are explicitly defined by top-level code.
909 (defun %%compiler-defstruct (info)
910 (declare (type defstruct-description info))
911 (let* ((name (dd-name info))
912 (class (sb!xc:find-class name)))
913 (let ((copier (dd-copier info)))
915 (proclaim `(ftype (function (,name) ,name) ,copier))))
917 ;; FIXME: This (and corresponding code in %DEFSTRUCT) are the way
918 ;; that CMU CL defined the predicate, instead of using DEFUN.
919 ;; Perhaps it would be better to go back to to the CMU CL way, or
920 ;; something similar. I want to reduce the amount of magic in
921 ;; defstruct functions, but making the predicate be a closure
922 ;; looks like a good thing, and can even be done without magic.
923 ;; (OTOH, there are some bootstrapping issues involved, since
924 ;; GENESIS understands DEFUN but doesn't understand a
925 ;; (SETF SYMBOL-FUNCTION) call inside %DEFSTRUCT.)
927 (let ((pred (dd-predicate info)))
929 (proclaim-as-defstruct-function-name pred)
930 (setf (info :function :inlinep pred) :inline)
931 (setf (info :function :inline-expansion pred)
932 `(lambda (x) (typep x ',name)))))
935 (dolist (slot (dd-slots info))
936 (let* ((fun (dsd-accessor slot))
937 (setf-fun `(setf ,fun)))
938 (when (and fun (eq (dsd-raw-type slot) t))
939 (proclaim-as-defstruct-function-name fun)
940 (setf (info :function :accessor-for fun) class)
941 (unless (dsd-read-only slot)
942 (proclaim-as-defstruct-function-name setf-fun)
943 (setf (info :function :accessor-for setf-fun) class))))))
947 ;;; Ordinarily this is preempted by an IR1 transformation, but this
948 ;;; definition is still useful for the interpreter and code walkers.
949 (defun %compiler-defstruct (info)
950 (%%compiler-defstruct info))
952 ;;;; redefinition stuff
954 ;;; Compare the slots of OLD and NEW, returning 3 lists of slot names:
955 ;;; 1. Slots which have moved,
956 ;;; 2. Slots whose type has changed,
957 ;;; 3. Deleted slots.
958 (defun compare-slots (old new)
959 (let* ((oslots (dd-slots old))
960 (nslots (dd-slots new))
961 (onames (mapcar #'dsd-name oslots))
962 (nnames (mapcar #'dsd-name nslots)))
965 (dolist (name (intersection onames nnames))
966 (let ((os (find name oslots :key #'dsd-name))
967 (ns (find name nslots :key #'dsd-name)))
968 (unless (subtypep (dsd-type ns) (dsd-type os))
969 (/noshow "found retyped slots" ns os (dsd-type ns) (dsd-type os))
971 (unless (and (= (dsd-index os) (dsd-index ns))
972 (eq (dsd-raw-type os) (dsd-raw-type ns)))
976 (set-difference onames nnames)))))
978 ;;; If we are redefining a structure with different slots than in the
979 ;;; currently loaded version, give a warning and return true.
980 (defun redefine-structure-warning (class old new)
981 (declare (type defstruct-description old new)
982 (type sb!xc:class class)
984 (let ((name (dd-name new)))
985 (multiple-value-bind (moved retyped deleted) (compare-slots old new)
986 (when (or moved retyped deleted)
988 "incompatibly redefining slots of structure class ~S~@
989 Make sure any uses of affected accessors are recompiled:~@
990 ~@[ These slots were moved to new positions:~% ~S~%~]~
991 ~@[ These slots have new incompatible types:~% ~S~%~]~
992 ~@[ These slots were deleted:~% ~S~%~]"
993 name moved retyped deleted)
996 ;;; This function is called when we are incompatibly redefining a
997 ;;; structure Class to have the specified New-Layout. We signal an
998 ;;; error with some proceed options and return the layout that should
1000 (defun %redefine-defstruct (class old-layout new-layout)
1001 (declare (type sb!xc:class class) (type layout old-layout new-layout))
1002 (let ((name (class-proper-name class)))
1004 (error "redefining class ~S incompatibly with the current definition"
1007 :report "Invalidate current definition."
1008 (warn "Previously loaded ~S accessors will no longer work." name)
1009 (register-layout new-layout))
1011 :report "Smash current layout, preserving old code."
1012 (warn "Any old ~S instances will be in a bad way.~@
1013 I hope you know what you're doing..."
1015 (register-layout new-layout :invalidate nil
1016 :destruct-layout old-layout))))
1019 ;;; This is called when we are about to define a structure class. It
1020 ;;; returns a (possibly new) class object and the layout which should
1021 ;;; be used for the new definition (may be the current layout, and
1022 ;;; also might be an uninstalled forward referenced layout.) The third
1023 ;;; value is true if this is an incompatible redefinition, in which
1024 ;;; case it is the old layout.
1025 (defun ensure-structure-class (info inherits old-context new-context
1026 &key compiler-layout)
1027 (multiple-value-bind (class old-layout)
1031 (class 'sb!xc:structure-class)
1032 (constructor 'make-structure-class))
1033 (dd-alternate-metaclass info)
1034 (declare (ignore name))
1035 (insured-find-class (dd-name info)
1036 (if (eq class 'sb!xc:structure-class)
1038 (typep x 'sb!xc:structure-class))
1040 (sb!xc:typep x (sb!xc:find-class class))))
1041 (fdefinition constructor)))
1042 (setf (class-direct-superclasses class)
1043 (if (eq (dd-name info) 'lisp-stream)
1044 ;; a hack to add STREAM as a superclass mixin to LISP-STREAMs
1045 (list (layout-class (svref inherits (1- (length inherits))))
1046 (layout-class (svref inherits (- (length inherits) 2))))
1047 (list (layout-class (svref inherits (1- (length inherits)))))))
1048 (let ((new-layout (make-layout :class class
1050 :depthoid (length inherits)
1051 :length (dd-length info)
1053 (old-layout (or compiler-layout old-layout)))
1056 (values class new-layout nil))
1057 (;; This clause corresponds to an assertion in REDEFINE-LAYOUT-WARNING
1058 ;; of classic CMU CL. I moved it out to here because it was only
1059 ;; exercised in this code path anyway. -- WHN 19990510
1060 (not (eq (layout-class new-layout) (layout-class old-layout)))
1061 (error "shouldn't happen: weird state of OLD-LAYOUT?"))
1062 ((not *type-system-initialized*)
1063 (setf (layout-info old-layout) info)
1064 (values class old-layout nil))
1065 ((redefine-layout-warning old-context
1068 (layout-length new-layout)
1069 (layout-inherits new-layout)
1070 (layout-depthoid new-layout))
1071 (values class new-layout old-layout))
1073 (let ((old-info (layout-info old-layout)))
1075 ((or defstruct-description)
1076 (cond ((redefine-structure-warning class old-info info)
1077 (values class new-layout old-layout))
1079 (setf (layout-info old-layout) info)
1080 (values class old-layout nil))))
1082 (setf (layout-info old-layout) info)
1083 (values class old-layout nil))
1085 (error "shouldn't happen! strange thing in LAYOUT-INFO:~% ~S"
1087 (values class new-layout old-layout)))))))))
1089 ;;; Blow away all the compiler info for the structure CLASS. Iterate
1090 ;;; over this type, clearing the compiler structure type info, and
1091 ;;; undefining all the associated functions.
1092 (defun undefine-structure (class)
1093 (let ((info (layout-info (class-layout class))))
1094 (when (defstruct-description-p info)
1095 (let ((type (dd-name info)))
1096 (setf (info :type :compiler-layout type) nil)
1097 (undefine-function-name (dd-copier info))
1098 (undefine-function-name (dd-predicate info))
1099 (dolist (slot (dd-slots info))
1100 (let ((fun (dsd-accessor slot)))
1101 (undefine-function-name fun)
1102 (unless (dsd-read-only slot)
1103 (undefine-function-name `(setf ,fun))))))
1104 ;; Clear out the SPECIFIER-TYPE cache so that subsequent
1105 ;; references are unknown types.
1106 (values-specifier-type-cache-clear)))
1109 ;;; Return a list of pairs (name . index). Used for :TYPE'd
1110 ;;; constructors to find all the names that we have to splice in &
1111 ;;; where. Note that these types don't have a layout, so we can't look
1112 ;;; at LAYOUT-INHERITS.
1113 (defun find-name-indices (defstruct)
1116 (do ((info defstruct
1117 (typed-structure-info-or-lose (first (dd-include info)))))
1118 ((not (dd-include info))
1123 (dolist (info infos)
1124 (incf i (or (dd-offset info) 0))
1125 (when (dd-named info)
1126 (res (cons (dd-name info) i)))
1127 (setq i (dd-length info)))))
1131 ;;;; slot accessors for raw slots
1133 ;;; Return info about how to read/write a slot in the value stored in
1134 ;;; OBJECT. This is also used by constructors (we can't use the
1135 ;;; accessor function, since some slots are read-only.) If supplied,
1136 ;;; DATA is a variable holding the raw-data vector.
1138 ;;; returned values:
1139 ;;; 1. accessor function name (SETFable)
1140 ;;; 2. index to pass to accessor.
1141 ;;; 3. object form to pass to accessor
1142 (defun slot-accessor-form (defstruct slot object &optional data)
1143 (let ((rtype (dsd-raw-type slot)))
1146 (single-float '%raw-ref-single)
1147 (double-float '%raw-ref-double)
1149 (long-float '%raw-ref-long)
1150 (complex-single-float '%raw-ref-complex-single)
1151 (complex-double-float '%raw-ref-complex-double)
1153 (complex-long-float '%raw-ref-complex-long)
1154 (unsigned-byte 'aref)
1156 (if (eq (dd-type defstruct) 'funcallable-structure)
1157 '%funcallable-instance-info
1162 (truncate (dsd-index slot) #!+x86 6 #!+sparc 8))
1165 (truncate (dsd-index slot) #!+x86 3 #!+sparc 4))
1167 (ash (dsd-index slot) -1))
1168 (complex-double-float
1169 (ash (dsd-index slot) -2))
1170 (complex-single-float
1171 (ash (dsd-index slot) -1))
1175 ((eq rtype t) object)
1178 `(truly-the (simple-array (unsigned-byte 32) (*))
1179 (%instance-ref ,object ,(dd-raw-index defstruct))))))))
1181 ;;; These functions are called to actually make a constructor after we
1182 ;;; have processed the arglist. The correct variant (according to the
1183 ;;; DD-TYPE) should be called. The function is defined with the
1184 ;;; specified name and arglist. Vars and Types are used for argument
1185 ;;; type declarations. Values are the values for the slots (in order.)
1187 ;;; This is split four ways because:
1188 ;;; 1] list & vector structures need "name" symbols stuck in at
1189 ;;; various weird places, whereas STRUCTURE structures have
1191 ;;; 2] We really want to use LIST to make list structures, instead of
1192 ;;; MAKE-LIST/(SETF ELT).
1193 ;;; 3] STRUCTURE structures can have raw slots that must also be
1194 ;;; allocated and indirectly referenced. We use SLOT-ACCESSOR-FORM
1195 ;;; to compute how to set the slots, which deals with raw slots.
1196 ;;; 4] Funcallable structures are weird.
1197 (defun create-vector-constructor
1198 (defstruct cons-name arglist vars types values)
1199 (let ((temp (gensym))
1200 (etype (dd-element-type defstruct)))
1201 `(defun ,cons-name ,arglist
1202 (declare ,@(mapcar #'(lambda (var type) `(type (and ,type ,etype) ,var))
1204 (let ((,temp (make-array ,(dd-length defstruct)
1205 :element-type ',(dd-element-type defstruct))))
1206 ,@(mapcar #'(lambda (x)
1207 `(setf (aref ,temp ,(cdr x)) ',(car x)))
1208 (find-name-indices defstruct))
1209 ,@(mapcar #'(lambda (dsd value)
1210 `(setf (aref ,temp ,(dsd-index dsd)) ,value))
1211 (dd-slots defstruct) values)
1213 (defun create-list-constructor
1214 (defstruct cons-name arglist vars types values)
1215 (let ((vals (make-list (dd-length defstruct) :initial-element nil)))
1216 (dolist (x (find-name-indices defstruct))
1217 (setf (elt vals (cdr x)) `',(car x)))
1218 (loop for dsd in (dd-slots defstruct) and val in values do
1219 (setf (elt vals (dsd-index dsd)) val))
1221 `(defun ,cons-name ,arglist
1222 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1225 (defun create-structure-constructor
1226 (defstruct cons-name arglist vars types values)
1227 (let* ((temp (gensym))
1228 (raw-index (dd-raw-index defstruct))
1229 (n-raw-data (when raw-index (gensym))))
1230 `(defun ,cons-name ,arglist
1231 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1233 (let ((,temp (truly-the ,(dd-name defstruct)
1234 (%make-instance ,(dd-length defstruct))))
1237 (make-array ,(dd-raw-length defstruct)
1238 :element-type '(unsigned-byte 32))))))
1239 (setf (%instance-layout ,temp)
1240 (%delayed-get-compiler-layout ,(dd-name defstruct)))
1242 `((setf (%instance-ref ,temp ,raw-index) ,n-raw-data)))
1243 ,@(mapcar (lambda (dsd value)
1244 (multiple-value-bind (accessor index data)
1245 (slot-accessor-form defstruct dsd temp n-raw-data)
1246 `(setf (,accessor ,data ,index) ,value)))
1247 (dd-slots defstruct)
1250 (defun create-fin-constructor
1251 (defstruct cons-name arglist vars types values)
1252 (let ((temp (gensym)))
1253 `(defun ,cons-name ,arglist
1254 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1256 (let ((,temp (truly-the
1257 ,(dd-name defstruct)
1258 (%make-funcallable-instance
1259 ,(dd-length defstruct)
1260 (%delayed-get-compiler-layout ,(dd-name defstruct))))))
1261 ,@(mapcar #'(lambda (dsd value)
1262 `(setf (%funcallable-instance-info
1263 ,temp ,(dsd-index dsd))
1265 (dd-slots defstruct) values)
1268 ;;; Create a default (non-BOA) keyword constructor.
1269 (defun create-keyword-constructor (defstruct creator)
1270 (collect ((arglist (list '&key))
1273 (dolist (slot (dd-slots defstruct))
1274 (let ((dum (gensym))
1275 (name (dsd-name slot)))
1276 (arglist `((,(keywordicate name) ,dum) ,(dsd-default slot)))
1277 (types (dsd-type slot))
1280 defstruct (dd-default-constructor defstruct)
1281 (arglist) (vals) (types) (vals))))
1283 ;;; Given a structure and a BOA constructor spec, call CREATOR with
1284 ;;; the appropriate args to make a constructor.
1285 (defun create-boa-constructor (defstruct boa creator)
1286 (multiple-value-bind (req opt restp rest keyp keys allowp aux)
1287 (sb!kernel:parse-lambda-list (second boa))
1291 (labels ((get-slot (name)
1292 (let ((res (find name (dd-slots defstruct)
1296 (values (dsd-type res) (dsd-default res))
1299 (multiple-value-bind (type default) (get-slot arg)
1300 (arglist `(,arg ,default))
1306 (types (get-slot arg)))
1309 (arglist '&optional)
1313 (name &optional (def (nth-value 1 (get-slot name))))
1315 (arglist `(,name ,def))
1317 (types (get-slot name))))
1319 (do-default arg)))))
1322 (arglist '&rest rest)
1330 (destructuring-bind (wot &optional (def nil def-p)) key
1331 (let ((name (if (consp wot)
1332 (destructuring-bind (key var) wot
1333 (declare (ignore key))
1336 (multiple-value-bind (type slot-def) (get-slot name)
1337 (arglist `(,wot ,(if def-p def slot-def)))
1342 (when allowp (arglist '&allow-other-keys))
1347 (let* ((arg (if (consp arg) arg (list arg)))
1351 (types (get-slot var))))))
1353 (funcall creator defstruct (first boa)
1354 (arglist) (vars) (types)
1355 (mapcar #'(lambda (slot)
1356 (or (find (dsd-name slot) (vars) :test #'string=)
1357 (dsd-default slot)))
1358 (dd-slots defstruct))))))
1360 ;;; Grovel the constructor options, and decide what constructors (if
1362 (defun constructor-definitions (defstruct)
1363 (let ((no-constructors nil)
1366 (creator (ecase (dd-type defstruct)
1367 (structure #'create-structure-constructor)
1368 (funcallable-structure #'create-fin-constructor)
1369 (vector #'create-vector-constructor)
1370 (list #'create-list-constructor))))
1371 (dolist (constructor (dd-constructors defstruct))
1372 (destructuring-bind (name &optional (boa-ll nil boa-p)) constructor
1373 (declare (ignore boa-ll))
1374 (cond ((not name) (setq no-constructors t))
1375 (boa-p (push constructor boas))
1376 (t (push name defaults)))))
1378 (when no-constructors
1379 (when (or defaults boas)
1380 (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs"))
1381 (return-from constructor-definitions ()))
1383 (unless (or defaults boas)
1384 (push (symbolicate "MAKE-" (dd-name defstruct)) defaults))
1388 (let ((cname (first defaults)))
1389 (setf (dd-default-constructor defstruct) cname)
1390 (res (create-keyword-constructor defstruct creator))
1391 (dolist (other-name (rest defaults))
1392 (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1393 (res `(declaim (ftype function ',other-name))))))
1396 (res (create-boa-constructor defstruct boa creator)))
1402 ;;; Like PROCLAIM-AS-FUNCTION-NAME, but we also set the kind to
1403 ;;; :DECLARED and blow away any ASSUMED-TYPE. Also, if the thing is a
1404 ;;; slot accessor currently, quietly unaccessorize it. And if there
1405 ;;; are any undefined warnings, we nuke them.
1406 (defun proclaim-as-defstruct-function-name (name)
1408 (when (info :function :accessor-for name)
1409 (setf (info :function :accessor-for name) nil))
1410 (proclaim-as-function-name name)
1411 (note-name-defined name :function)
1412 (setf (info :function :where-from name) :declared)
1413 (when (info :function :assumed-type name)
1414 (setf (info :function :assumed-type name) nil)))
1417 ;;;; finalizing bootstrapping
1419 ;;; early structure placeholder definitions: Set up layout and class
1420 ;;; data for structures which are needed early.
1422 '#.(sb-cold:read-from-file
1423 "src/code/early-defstruct-args.lisp-expr"))
1424 (let* ((defstruct (parse-name-and-options-and-slot-descriptions
1427 (inherits (inherits-for-structure defstruct)))
1428 (function-%compiler-only-defstruct defstruct inherits)))
1430 (/show0 "code/defstruct.lisp end of file")