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 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 (accessor-name (dsd-accessor-name 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 accessor-name
346 (not (eq accessor-name '%instance-ref)))
347 (res `(declaim (inline ,accessor-name)))
348 (res `(declaim (ftype (function (,name) ,stype) ,accessor-name)))
349 (res `(defun ,accessor-name (,argname)
350 (truly-the ,stype (,accessor ,data ,offset))))
351 (unless (dsd-read-only slot)
352 (res `(declaim (inline (setf ,accessor-name))))
353 (res `(declaim (ftype (function (,stype ,name) ,stype)
354 (setf ,accessor-name))))
355 ;; FIXME: I rewrote this somewhat from the CMU CL definition.
356 ;; Do some basic tests to make sure that reading and writing
357 ;; raw slots still works correctly.
358 (res `(defun (setf ,accessor-name) (,nvname ,argname)
359 (setf (,accessor ,data ,offset) ,nvname)
363 ;;; Return a list of forms which create a predicate for an untyped DEFSTRUCT.
364 (defun predicate-definitions (dd)
365 (let ((pred (dd-predicate-name dd))
368 (if (eq (dd-type dd) 'funcallable-structure)
369 ;; FIXME: Why does this need to be special-cased for
370 ;; FUNCALLABLE-STRUCTURE? CMU CL did it, but without explanation.
371 ;; Could we do without it? What breaks if we do? Or could we
372 ;; perhaps get by with no predicates for funcallable structures?
373 `((declaim (inline ,pred))
374 (defun ,pred (,argname) (typep ,argname ',(dd-name dd))))
375 `((protect-cl ',pred)
376 (declaim (inline ,pred))
377 (defun ,pred (,argname)
378 (declare (optimize (speed 3) (safety 0)))
379 (typep-to-layout ,argname
380 (compile-time-find-layout ,(dd-name dd)))))))))
382 ;;; Return a list of forms which create a predicate function for a typed
384 (defun typed-predicate-definitions (defstruct)
385 (let ((name (dd-name defstruct))
386 (predicate-name (dd-predicate-name defstruct))
388 (when (and predicate-name (dd-named defstruct))
389 (let ((ltype (dd-lisp-type defstruct)))
390 `((defun ,predicate-name (,argname)
391 (and (typep ,argname ',ltype)
392 (eq (elt (the ,ltype ,argname)
393 ,(cdr (car (last (find-name-indices defstruct)))))
396 ;;; FIXME: We've inherited from CMU CL code to do typed structure copiers
397 ;;; in a completely different way than untyped structure copiers. Fix this.
398 ;;; (This function was my first attempt to fix this, but I stopped before
399 ;;; figuring out how to install it completely and remove the parallel
400 ;;; code which simply SETF's the FDEFINITION of the DD-COPIER name.
402 ;;; Return the copier definition for an untyped DEFSTRUCT.
403 (defun copier-definition (dd)
404 (when (and (dd-copier dd)
405 ;; FUNCALLABLE-STRUCTUREs don't need copiers, and this
406 ;; implementation wouldn't work for them anyway, since
407 ;; COPY-STRUCTURE returns a STRUCTURE-OBJECT and they're not.
408 (not (eq (dd-type info) 'funcallable-structure)))
409 (let ((argname (gensym)))
411 (protect-cl ',(dd-copier dd))
412 (defun ,(dd-copier dd) (,argname)
413 (declare (type ,(dd-name dd) ,argname))
414 (copy-structure ,argname))))))
417 ;;; Return a list of forms to create a copier function of a typed DEFSTRUCT.
418 (defun typed-copier-definitions (defstruct)
419 (when (dd-copier defstruct)
420 `((setf (fdefinition ',(dd-copier defstruct)) #'copy-seq)
421 (declaim (ftype function ,(dd-copier defstruct))))))
423 ;;; Return a list of function definitions for accessing and setting the
424 ;;; slots of a typed DEFSTRUCT. The functions are proclaimed to be inline,
425 ;;; and the types of their arguments and results are declared as well. We
426 ;;; count on the compiler to do clever things with ELT.
427 (defun typed-accessor-definitions (defstruct)
429 (let ((ltype (dd-lisp-type defstruct)))
430 (dolist (slot (dd-slots defstruct))
431 (let ((name (dsd-accessor slot))
432 (index (dsd-index slot))
433 (slot-type `(and ,(dsd-type slot)
434 ,(dd-element-type defstruct))))
435 (stuff `(proclaim '(inline ,name (setf ,name))))
436 ;; FIXME: The arguments in the next two DEFUNs should be
437 ;; gensyms. (Otherwise e.g. if NEW-VALUE happened to be the
438 ;; name of a special variable, things could get weird.)
439 (stuff `(defun ,name (structure)
440 (declare (type ,ltype structure))
441 (the ,slot-type (elt structure ,index))))
442 (unless (dsd-read-only slot)
444 `(defun (setf ,name) (new-value structure)
445 (declare (type ,ltype structure) (type ,slot-type new-value))
446 (setf (elt structure ,index) new-value)))))))
451 (defun require-no-print-options-so-far (defstruct)
452 (unless (and (eql (dd-print-function defstruct) 0)
453 (eql (dd-print-object defstruct) 0))
454 (error "no more than one of the following options may be specified:
455 :PRINT-FUNCTION, :PRINT-OBJECT, :TYPE")))
457 ;;; Parse a single defstruct option and store the results in DEFSTRUCT.
458 (defun parse-1-option (option defstruct)
459 (let ((args (rest option))
460 (name (dd-name defstruct)))
463 (destructuring-bind (conc-name) args
464 (setf (dd-conc-name defstruct)
465 (if (symbolp conc-name)
467 (make-symbol (string conc-name))))))
469 (destructuring-bind (&optional (cname (symbolicate "MAKE-" name))
472 (push (cons cname stuff) (dd-constructors defstruct))))
474 (destructuring-bind (&optional (copier (symbolicate "COPY-" name)))
476 (setf (dd-copier defstruct) copier)))
478 (destructuring-bind (&optional (predicate-name (symbolicate name "-P")))
480 (setf (dd-predicate-name defstruct) predicate-name)))
482 (when (dd-include defstruct)
483 (error "more than one :INCLUDE option"))
484 (setf (dd-include defstruct) args))
485 (:alternate-metaclass
486 (setf (dd-alternate-metaclass defstruct) args))
488 (require-no-print-options-so-far defstruct)
489 (setf (dd-print-function defstruct)
490 (the (or symbol cons) args)))
492 (require-no-print-options-so-far defstruct)
493 (setf (dd-print-object defstruct)
494 (the (or symbol cons) args)))
496 (destructuring-bind (type) args
497 (cond ((eq type 'funcallable-structure)
498 (setf (dd-type defstruct) type))
499 ((member type '(list vector))
500 (setf (dd-element-type defstruct) t)
501 (setf (dd-type defstruct) type))
502 ((and (consp type) (eq (first type) 'vector))
503 (destructuring-bind (vector vtype) type
504 (declare (ignore vector))
505 (setf (dd-element-type defstruct) vtype)
506 (setf (dd-type defstruct) 'vector)))
508 (error "~S is a bad :TYPE for Defstruct." type)))))
510 (error "The DEFSTRUCT option :NAMED takes no arguments."))
512 (destructuring-bind (offset) args
513 (setf (dd-offset defstruct) offset)))
515 (destructuring-bind (fun) args
516 (setf (dd-pure defstruct) fun)))
517 (t (error "unknown DEFSTRUCT option:~% ~S" option)))))
519 ;;; Given name and options, return a DD holding that info.
520 (eval-when (:compile-toplevel :load-toplevel :execute)
521 (defun parse-name-and-options (name-and-options)
522 (destructuring-bind (name &rest options) name-and-options
523 (aver name) ; A null name doesn't seem to make sense here.
524 (let ((defstruct (make-defstruct-description name)))
525 (dolist (option options)
526 (cond ((consp option)
527 (parse-1-option option defstruct))
529 (setf (dd-named defstruct) t))
530 ((member option '(:constructor :copier :predicate :named))
531 (parse-1-option (list option) defstruct))
533 (error "unrecognized DEFSTRUCT option: ~S" option))))
535 (case (dd-type defstruct)
537 (when (dd-offset defstruct)
538 (error ":OFFSET can't be specified unless :TYPE is specified."))
539 (unless (dd-include defstruct)
540 (incf (dd-length defstruct))))
541 (funcallable-structure)
543 (require-no-print-options-so-far defstruct)
544 (when (dd-named defstruct)
545 (incf (dd-length defstruct)))
546 (let ((offset (dd-offset defstruct)))
547 (when offset (incf (dd-length defstruct) offset)))))
549 (when (dd-include defstruct)
550 (do-inclusion-stuff defstruct))
554 ;;; Given name and options and slot descriptions (and possibly doc
555 ;;; string at the head of slot descriptions) return a DD holding that
557 (defun parse-name-and-options-and-slot-descriptions (name-and-options
559 (/noshow "PARSE-NAME-AND-OPTIONS-AND-SLOT-DESCRIPTIONS" name-and-options)
560 (let ((result (parse-name-and-options (if (atom name-and-options)
561 (list name-and-options)
563 (when (stringp (car slot-descriptions))
564 (setf (dd-doc result) (pop slot-descriptions)))
565 (dolist (slot-description slot-descriptions)
566 (allocate-1-slot result (parse-1-dsd result slot-description)))
571 ;;;; stuff to parse slot descriptions
573 ;;; Parse a slot description for DEFSTRUCT, add it to the description
574 ;;; and return it. If supplied, SLOT is a pre-initialized DSD
575 ;;; that we modify to get the new slot. This is supplied when handling
577 (defun parse-1-dsd (defstruct spec &optional
578 (slot (make-defstruct-slot-description :%name ""
581 (multiple-value-bind (name default default-p type type-p read-only ro-p)
586 &optional (default nil default-p)
587 &key (type nil type-p) (read-only nil ro-p))
591 (uncross type) type-p
594 (when (keywordp spec)
595 (style-warn "Keyword slot name indicates probable syntax ~
596 error in DEFSTRUCT: ~S."
600 (when (find name (dd-slots defstruct) :test #'string= :key #'dsd-%name)
601 (error 'simple-program-error
602 :format-control "duplicate slot name ~S"
603 :format-arguments (list name)))
604 (setf (dsd-%name slot) (string name))
605 (setf (dd-slots defstruct) (nconc (dd-slots defstruct) (list slot)))
607 (let ((accessor-name (symbolicate (or (dd-conc-name defstruct) "") name))
608 (predicate-name (dd-predicate-name defstruct)))
609 (setf (dsd-accessor-name slot) accessor-name)
610 (when (eql accessor-name predicate-name)
611 ;; Some adventurous soul has named a slot so that its accessor
612 ;; collides with the structure type predicate. ANSI doesn't
613 ;; specify what to do in this case. As of 2001-09-04, Martin
614 ;; Atzmueller reports that CLISP and Lispworks both give
615 ;; priority to the slot accessor, so that the predicate is
616 ;; overwritten. We might as well do the same (as well as
617 ;; signalling a warning).
619 "~@<The structure accessor name ~S is the same as the name of the ~
620 structure type predicate. ANSI doesn't specify what to do in ~
621 this case; this implementation chooses to overwrite the type ~
622 predicate with the slot accessor.~@:>"
624 (setf (dd-predicate-name defstruct) nil)))
627 (setf (dsd-default slot) default))
629 (setf (dsd-type slot)
630 (if (eq (dsd-type slot) t)
632 `(and ,(dsd-type slot) ,type))))
635 (setf (dsd-read-only slot) t)
636 (when (dsd-read-only slot)
637 (error "Slot ~S is :READ-ONLY in parent and must be :READ-ONLY in subtype ~S."
642 ;;; When a value of type TYPE is stored in a structure, should it be
643 ;;; stored in a raw slot? Return (VALUES RAW? RAW-TYPE WORDS), where
644 ;;; RAW? is true if TYPE should be stored in a raw slot.
645 ;;; RAW-TYPE is the raw slot type, or NIL if no raw slot.
646 ;;; WORDS is the number of words in the raw slot, or NIL if no raw slot.
647 (defun structure-raw-slot-type-and-size (type)
648 (/noshow "in STRUCTURE-RAW-SLOT-TYPE-AND-SIZE" type (sb!xc:subtypep type 'fixnum))
650 (;; FIXME: For now we suppress raw slots, since there are various
651 ;; issues about the way that the cross-compiler handles them.
652 (not (boundp '*dummy-placeholder-to-stop-compiler-warnings*))
653 (values nil nil nil))
654 ((and (sb!xc:subtypep type '(unsigned-byte 32))
655 (multiple-value-bind (fixnum? fixnum-certain?)
656 (sb!xc:subtypep type 'fixnum)
657 (/noshow fixnum? fixnum-certain?)
658 ;; (The extra test for FIXNUM-CERTAIN? here is
659 ;; intended for bootstrapping the system. In
660 ;; particular, in sbcl-0.6.2, we set up LAYOUT before
661 ;; FIXNUM is defined, and so could bogusly end up
662 ;; putting INDEX-typed values into raw slots if we
663 ;; didn't test FIXNUM-CERTAIN?.)
664 (and (not fixnum?) fixnum-certain?)))
665 (values t 'unsigned-byte 1))
666 ((sb!xc:subtypep type 'single-float)
667 (values t 'single-float 1))
668 ((sb!xc:subtypep type 'double-float)
669 (values t 'double-float 2))
671 ((sb!xc:subtypep type 'long-float)
672 (values t 'long-float #!+x86 3 #!+sparc 4))
673 ((sb!xc:subtypep type '(complex single-float))
674 (values t 'complex-single-float 2))
675 ((sb!xc:subtypep type '(complex double-float))
676 (values t 'complex-double-float 4))
678 ((sb!xc:subtypep type '(complex long-float))
679 (values t 'complex-long-float #!+x86 6 #!+sparc 8))
681 (values nil nil nil))))
683 ;;; Allocate storage for a DSD in DEFSTRUCT. This is where we decide
684 ;;; whether a slot is raw or not. If raw, and we haven't allocated a
685 ;;; raw-index yet for the raw data vector, then do it. Raw objects are
686 ;;; aligned on the unit of their size.
687 (defun allocate-1-slot (defstruct dsd)
688 (multiple-value-bind (raw? raw-type words)
689 (if (eq (dd-type defstruct) 'structure)
690 (structure-raw-slot-type-and-size (dsd-type dsd))
691 (values nil nil nil))
692 (/noshow "ALLOCATE-1-SLOT" dsd raw? raw-type words)
694 (setf (dsd-index dsd) (dd-length defstruct))
695 (incf (dd-length defstruct)))
697 (unless (dd-raw-index defstruct)
698 (setf (dd-raw-index defstruct) (dd-length defstruct))
699 (incf (dd-length defstruct)))
700 (let ((off (rem (dd-raw-length defstruct) words)))
702 (incf (dd-raw-length defstruct) (- words off))))
703 (setf (dsd-raw-type dsd) raw-type)
704 (setf (dsd-index dsd) (dd-raw-length defstruct))
705 (incf (dd-raw-length defstruct) words))))
708 (defun typed-structure-info-or-lose (name)
709 (or (info :typed-structure :info name)
710 (error ":TYPE'd DEFSTRUCT ~S not found for inclusion." name)))
712 ;;; Process any included slots pretty much like they were specified.
713 ;;; Also inherit various other attributes.
714 (defun do-inclusion-stuff (defstruct)
716 (included-name &rest modified-slots)
717 (dd-include defstruct)
718 (let* ((type (dd-type defstruct))
720 (if (class-structure-p defstruct)
721 (layout-info (compiler-layout-or-lose included-name))
722 (typed-structure-info-or-lose included-name))))
723 (unless (and (eq type (dd-type included-structure))
724 (type= (specifier-type (dd-element-type included-structure))
725 (specifier-type (dd-element-type defstruct))))
726 (error ":TYPE option mismatch between structures ~S and ~S."
727 (dd-name defstruct) included-name))
729 (incf (dd-length defstruct) (dd-length included-structure))
730 (when (class-structure-p defstruct)
731 (let ((mc (rest (dd-alternate-metaclass included-structure))))
732 (when (and mc (not (dd-alternate-metaclass defstruct)))
733 (setf (dd-alternate-metaclass defstruct)
734 (cons included-name mc))))
735 (when (eq (dd-pure defstruct) :unspecified)
736 (setf (dd-pure defstruct) (dd-pure included-structure)))
737 (setf (dd-raw-index defstruct) (dd-raw-index included-structure))
738 (setf (dd-raw-length defstruct) (dd-raw-length included-structure)))
740 (dolist (included-slot (dd-slots included-structure))
741 (let* ((included-name (dsd-name included-slot))
742 (modified (or (find included-name modified-slots
743 :key #'(lambda (x) (if (atom x) x (car x)))
746 (parse-1-dsd defstruct
748 (copy-structure included-slot)))))))
750 ;;; This function is called at macroexpand time to compute the INHERITS
751 ;;; vector for a structure type definition.
752 (defun inherits-for-structure (info)
753 (declare (type defstruct-description info))
754 (let* ((include (dd-include info))
755 (superclass-opt (dd-alternate-metaclass info))
758 (compiler-layout-or-lose (first include))
759 (class-layout (sb!xc:find-class
760 (or (first superclass-opt)
761 'structure-object))))))
762 (if (eq (dd-name info) 'lisp-stream)
763 ;; a hack to added the stream class as a mixin for LISP-STREAMs
764 (concatenate 'simple-vector
765 (layout-inherits super)
767 (class-layout (sb!xc:find-class 'stream))))
768 (concatenate 'simple-vector
769 (layout-inherits super)
772 ;;; Do miscellaneous (LOAD EVAL) time actions for the structure
773 ;;; described by INFO. Create the class & layout, checking for
774 ;;; incompatible redefinition. Define setters, accessors, copier,
775 ;;; predicate, documentation, instantiate definition in load-time env.
776 ;;; This is only called for default structures.
777 (defun %defstruct (info inherits)
778 (declare (type defstruct-description info))
779 (multiple-value-bind (class layout old-layout)
780 (ensure-structure-class info inherits "current" "new")
781 (cond ((not old-layout)
782 (unless (eq (class-layout class) layout)
783 (register-layout layout)))
785 (let ((old-info (layout-info old-layout)))
786 (when (defstruct-description-p old-info)
787 (dolist (slot (dd-slots old-info))
788 (fmakunbound (dsd-accessor-name slot))
789 (unless (dsd-read-only slot)
790 (fmakunbound `(setf ,(dsd-accessor-name slot)))))))
791 (%redefine-defstruct class old-layout layout)
792 (setq layout (class-layout class))))
794 (setf (sb!xc:find-class (dd-name info)) class)
796 ;; Set FDEFINITIONs for structure accessors, setters, predicates,
799 (unless (eq (dd-type info) 'funcallable-structure)
801 (dolist (slot (dd-slots info))
803 (when (and (dsd-accessor-name slot)
804 (eq (dsd-raw-type slot) t))
805 (protect-cl (dsd-accessor-name slot))
806 (setf (symbol-function (dsd-accessor-name slot))
807 (structure-slot-getter layout dsd))
808 (unless (dsd-read-only slot)
809 (setf (fdefinition `(setf ,(dsd-accessor-name slot)))
810 (structure-slot-setter layout dsd))))))
812 ;; FIXME: See comment on corresponding code in %%COMPILER-DEFSTRUCT.
814 (when (dd-predicate info)
815 (protect-cl (dd-predicate info))
816 (setf (symbol-function (dd-predicate info))
818 (declare (optimize (speed 3) (safety 0)))
819 (typep-to-layout object layout))))
822 (when (dd-copier info)
823 (protect-cl (dd-copier info))
824 (setf (symbol-function (dd-copier info))
825 #'(lambda (structure)
826 (declare (optimize (speed 3) (safety 0)))
827 (flet ((layout-test (structure)
828 (typep-to-layout structure layout)))
829 (unless (layout-test structure)
830 (error 'simple-type-error
832 :expected-type '(satisfies layout-test)
834 "Structure for copier is not a ~S:~% ~S"
836 (list (sb!xc:class-name (layout-class layout))
838 (copy-structure structure))))))
841 (setf (fdocumentation (dd-name info) 'type) (dd-doc info)))
845 ;;; This function is called at compile-time to do the
846 ;;; compile-time-only actions for defining a structure type. It
847 ;;; installs the class in the type system in a similar way to
848 ;;; %DEFSTRUCT, but is quieter and safer in the case of redefinition.
850 ;;; The comments for the classic CMU CL version of this function said
851 ;;; that EVAL-WHEN doesn't do the right thing when nested or
852 ;;; non-top-level, and so CMU CL had the function magically called by
853 ;;; the compiler. Unfortunately, this doesn't do the right thing
854 ;;; either: compiling a function (DEFUN FOO () (DEFSTRUCT FOO X Y))
855 ;;; causes the class FOO to become defined, even though FOO is never
856 ;;; loaded or executed. Even more unfortunately, I've been unable to
857 ;;; come up with any EVAL-WHEN tricks which work -- I finally gave up
858 ;;; on this approach when trying to get the system to cross-compile
859 ;;; error.lisp. (Just because I haven't found it doesn't mean that it
860 ;;; doesn't exist, of course. Alas, I continue to have some trouble
861 ;;; understanding compile/load semantics in Common Lisp.) So we
862 ;;; continue to use the IR1 transformation approach, even though it's
863 ;;; known to be buggy. -- WHN 19990507
865 ;;; Basically, this function avoids trashing the compiler by only
866 ;;; actually defining the class if there is no current definition.
867 ;;; Instead, we just set the INFO TYPE COMPILER-LAYOUT. This behavior
868 ;;; is left over from classic CMU CL and may not be necessary in the
869 ;;; new build system. -- WHN 19990507
871 ;;; FUNCTION-%COMPILER-ONLY-DEFSTRUCT is an ordinary function, called
872 ;;; by both the IR1 transform version of %COMPILER-ONLY-DEFSTRUCT and
873 ;;; by the ordinary function version of %COMPILER-ONLY-DEFSTRUCT. (The
874 ;;; ordinary function version is there for the interpreter and for
876 (defun %compiler-only-defstruct (info inherits)
877 (function-%compiler-only-defstruct info inherits))
878 (defun function-%compiler-only-defstruct (info inherits)
879 (multiple-value-bind (class layout old-layout)
880 (multiple-value-bind (clayout clayout-p)
881 (info :type :compiler-layout (dd-name info))
882 (ensure-structure-class info
884 (if clayout-p "previously compiled" "current")
886 :compiler-layout clayout))
888 (undefine-structure (layout-class old-layout))
889 (when (and (class-subclasses class)
890 (not (eq layout old-layout)))
892 (dohash (class layout (class-subclasses class))
893 (declare (ignore layout))
894 (undefine-structure class)
895 (subs (class-proper-name class)))
897 (warn "Removing old subclasses of ~S:~% ~S"
898 (sb!xc:class-name class)
901 (unless (eq (class-layout class) layout)
902 (register-layout layout :invalidate nil))
903 (setf (sb!xc:find-class (dd-name info)) class)))
905 (setf (info :type :compiler-layout (dd-name info)) layout))
908 ;;; This function does the (COMPILE LOAD EVAL) time actions for updating the
909 ;;; compiler's global meta-information to represent the definition of the
910 ;;; structure described by Info. This primarily amounts to setting up info
911 ;;; about the accessor and other implicitly defined functions. The constructors
912 ;;; are explicitly defined by top-level code.
913 (defun %%compiler-defstruct (info)
914 (declare (type defstruct-description info))
915 (let* ((name (dd-name info))
916 (class (sb!xc:find-class name)))
917 (let ((copier (dd-copier info)))
919 (proclaim `(ftype (function (,name) ,name) ,copier))))
921 ;; FIXME: This (and corresponding code in %DEFSTRUCT) are the way
922 ;; that CMU CL defined the predicate, instead of using DEFUN.
923 ;; Perhaps it would be better to go back to to the CMU CL way, or
924 ;; something similar. I want to reduce the amount of magic in
925 ;; defstruct functions, but making the predicate be a closure
926 ;; looks like a good thing, and can even be done without magic.
927 ;; (OTOH, there are some bootstrapping issues involved, since
928 ;; GENESIS understands DEFUN but doesn't understand a
929 ;; (SETF SYMBOL-FUNCTION) call inside %DEFSTRUCT.)
931 (let ((predicate-name (dd-predicate-name info)))
933 (proclaim-as-defstruct-function-name predicate-name)
934 (setf (info :function :inlinep pred) :inline)
935 (setf (info :function :inline-expansion predicate-name)
936 `(lambda (x) (typep x ',name)))))
939 (dolist (slot (dd-slots info))
940 (let* ((fun (dsd-accessor-name slot))
941 (setf-fun `(setf ,fun)))
942 (when (and fun (eq (dsd-raw-type slot) t))
943 (proclaim-as-defstruct-function-name fun)
944 (setf (info :function :accessor-for fun) class)
945 (unless (dsd-read-only slot)
946 (proclaim-as-defstruct-function-name setf-fun)
947 (setf (info :function :accessor-for setf-fun) class))))))
951 ;;; Ordinarily this is preempted by an IR1 transformation, but this
952 ;;; definition is still useful for the interpreter and code walkers.
953 (defun %compiler-defstruct (info)
954 (%%compiler-defstruct info))
956 ;;;; redefinition stuff
958 ;;; Compare the slots of OLD and NEW, returning 3 lists of slot names:
959 ;;; 1. Slots which have moved,
960 ;;; 2. Slots whose type has changed,
961 ;;; 3. Deleted slots.
962 (defun compare-slots (old new)
963 (let* ((oslots (dd-slots old))
964 (nslots (dd-slots new))
965 (onames (mapcar #'dsd-name oslots))
966 (nnames (mapcar #'dsd-name nslots)))
969 (dolist (name (intersection onames nnames))
970 (let ((os (find name oslots :key #'dsd-name))
971 (ns (find name nslots :key #'dsd-name)))
972 (unless (subtypep (dsd-type ns) (dsd-type os))
973 (/noshow "found retyped slots" ns os (dsd-type ns) (dsd-type os))
975 (unless (and (= (dsd-index os) (dsd-index ns))
976 (eq (dsd-raw-type os) (dsd-raw-type ns)))
980 (set-difference onames nnames)))))
982 ;;; If we are redefining a structure with different slots than in the
983 ;;; currently loaded version, give a warning and return true.
984 (defun redefine-structure-warning (class old new)
985 (declare (type defstruct-description old new)
986 (type sb!xc:class class)
988 (let ((name (dd-name new)))
989 (multiple-value-bind (moved retyped deleted) (compare-slots old new)
990 (when (or moved retyped deleted)
992 "incompatibly redefining slots of structure class ~S~@
993 Make sure any uses of affected accessors are recompiled:~@
994 ~@[ These slots were moved to new positions:~% ~S~%~]~
995 ~@[ These slots have new incompatible types:~% ~S~%~]~
996 ~@[ These slots were deleted:~% ~S~%~]"
997 name moved retyped deleted)
1000 ;;; This function is called when we are incompatibly redefining a
1001 ;;; structure Class to have the specified New-Layout. We signal an
1002 ;;; error with some proceed options and return the layout that should
1004 (defun %redefine-defstruct (class old-layout new-layout)
1005 (declare (type sb!xc:class class) (type layout old-layout new-layout))
1006 (let ((name (class-proper-name class)))
1008 (error "redefining class ~S incompatibly with the current definition"
1011 :report "Invalidate current definition."
1012 (warn "Previously loaded ~S accessors will no longer work." name)
1013 (register-layout new-layout))
1015 :report "Smash current layout, preserving old code."
1016 (warn "Any old ~S instances will be in a bad way.~@
1017 I hope you know what you're doing..."
1019 (register-layout new-layout :invalidate nil
1020 :destruct-layout old-layout))))
1023 ;;; This is called when we are about to define a structure class. It
1024 ;;; returns a (possibly new) class object and the layout which should
1025 ;;; be used for the new definition (may be the current layout, and
1026 ;;; also might be an uninstalled forward referenced layout.) The third
1027 ;;; value is true if this is an incompatible redefinition, in which
1028 ;;; case it is the old layout.
1029 (defun ensure-structure-class (info inherits old-context new-context
1030 &key compiler-layout)
1031 (multiple-value-bind (class old-layout)
1035 (class 'sb!xc:structure-class)
1036 (constructor 'make-structure-class))
1037 (dd-alternate-metaclass info)
1038 (declare (ignore name))
1039 (insured-find-class (dd-name info)
1040 (if (eq class 'sb!xc:structure-class)
1042 (typep x 'sb!xc:structure-class))
1044 (sb!xc:typep x (sb!xc:find-class class))))
1045 (fdefinition constructor)))
1046 (setf (class-direct-superclasses class)
1047 (if (eq (dd-name info) 'lisp-stream)
1048 ;; a hack to add STREAM as a superclass mixin to LISP-STREAMs
1049 (list (layout-class (svref inherits (1- (length inherits))))
1050 (layout-class (svref inherits (- (length inherits) 2))))
1051 (list (layout-class (svref inherits (1- (length inherits)))))))
1052 (let ((new-layout (make-layout :class class
1054 :depthoid (length inherits)
1055 :length (dd-length info)
1057 (old-layout (or compiler-layout old-layout)))
1060 (values class new-layout nil))
1061 (;; This clause corresponds to an assertion in REDEFINE-LAYOUT-WARNING
1062 ;; of classic CMU CL. I moved it out to here because it was only
1063 ;; exercised in this code path anyway. -- WHN 19990510
1064 (not (eq (layout-class new-layout) (layout-class old-layout)))
1065 (error "shouldn't happen: weird state of OLD-LAYOUT?"))
1066 ((not *type-system-initialized*)
1067 (setf (layout-info old-layout) info)
1068 (values class old-layout nil))
1069 ((redefine-layout-warning old-context
1072 (layout-length new-layout)
1073 (layout-inherits new-layout)
1074 (layout-depthoid new-layout))
1075 (values class new-layout old-layout))
1077 (let ((old-info (layout-info old-layout)))
1079 ((or defstruct-description)
1080 (cond ((redefine-structure-warning class old-info info)
1081 (values class new-layout old-layout))
1083 (setf (layout-info old-layout) info)
1084 (values class old-layout nil))))
1086 (setf (layout-info old-layout) info)
1087 (values class old-layout nil))
1089 (error "shouldn't happen! strange thing in LAYOUT-INFO:~% ~S"
1091 (values class new-layout old-layout)))))))))
1093 ;;; Blow away all the compiler info for the structure CLASS. Iterate
1094 ;;; over this type, clearing the compiler structure type info, and
1095 ;;; undefining all the associated functions.
1096 (defun undefine-structure (class)
1097 (let ((info (layout-info (class-layout class))))
1098 (when (defstruct-description-p info)
1099 (let ((type (dd-name info)))
1100 (setf (info :type :compiler-layout type) nil)
1101 (undefine-function-name (dd-copier info))
1102 (undefine-function-name (dd-predicate-name info))
1103 (dolist (slot (dd-slots info))
1104 (let ((fun (dsd-accessor-name slot)))
1105 (undefine-function-name fun)
1106 (unless (dsd-read-only slot)
1107 (undefine-function-name `(setf ,fun))))))
1108 ;; Clear out the SPECIFIER-TYPE cache so that subsequent
1109 ;; references are unknown types.
1110 (values-specifier-type-cache-clear)))
1113 ;;; Return a list of pairs (name . index). Used for :TYPE'd
1114 ;;; constructors to find all the names that we have to splice in &
1115 ;;; where. Note that these types don't have a layout, so we can't look
1116 ;;; at LAYOUT-INHERITS.
1117 (defun find-name-indices (defstruct)
1120 (do ((info defstruct
1121 (typed-structure-info-or-lose (first (dd-include info)))))
1122 ((not (dd-include info))
1127 (dolist (info infos)
1128 (incf i (or (dd-offset info) 0))
1129 (when (dd-named info)
1130 (res (cons (dd-name info) i)))
1131 (setq i (dd-length info)))))
1135 ;;;; slot accessors for raw slots
1137 ;;; Return info about how to read/write a slot in the value stored in
1138 ;;; OBJECT. This is also used by constructors (we can't use the
1139 ;;; accessor function, since some slots are read-only.) If supplied,
1140 ;;; DATA is a variable holding the raw-data vector.
1142 ;;; returned values:
1143 ;;; 1. accessor function name (SETFable)
1144 ;;; 2. index to pass to accessor.
1145 ;;; 3. object form to pass to accessor
1146 (defun slot-accessor-form (defstruct slot object &optional data)
1147 (let ((rtype (dsd-raw-type slot)))
1150 (single-float '%raw-ref-single)
1151 (double-float '%raw-ref-double)
1153 (long-float '%raw-ref-long)
1154 (complex-single-float '%raw-ref-complex-single)
1155 (complex-double-float '%raw-ref-complex-double)
1157 (complex-long-float '%raw-ref-complex-long)
1158 (unsigned-byte 'aref)
1160 (if (eq (dd-type defstruct) 'funcallable-structure)
1161 '%funcallable-instance-info
1166 (truncate (dsd-index slot) #!+x86 6 #!+sparc 8))
1169 (truncate (dsd-index slot) #!+x86 3 #!+sparc 4))
1171 (ash (dsd-index slot) -1))
1172 (complex-double-float
1173 (ash (dsd-index slot) -2))
1174 (complex-single-float
1175 (ash (dsd-index slot) -1))
1179 ((eq rtype t) object)
1182 `(truly-the (simple-array (unsigned-byte 32) (*))
1183 (%instance-ref ,object ,(dd-raw-index defstruct))))))))
1185 ;;; These functions are called to actually make a constructor after we
1186 ;;; have processed the arglist. The correct variant (according to the
1187 ;;; DD-TYPE) should be called. The function is defined with the
1188 ;;; specified name and arglist. Vars and Types are used for argument
1189 ;;; type declarations. Values are the values for the slots (in order.)
1191 ;;; This is split four ways because:
1192 ;;; 1] list & vector structures need "name" symbols stuck in at
1193 ;;; various weird places, whereas STRUCTURE structures have
1195 ;;; 2] We really want to use LIST to make list structures, instead of
1196 ;;; MAKE-LIST/(SETF ELT).
1197 ;;; 3] STRUCTURE structures can have raw slots that must also be
1198 ;;; allocated and indirectly referenced. We use SLOT-ACCESSOR-FORM
1199 ;;; to compute how to set the slots, which deals with raw slots.
1200 ;;; 4] Funcallable structures are weird.
1201 (defun create-vector-constructor
1202 (defstruct cons-name arglist vars types values)
1203 (let ((temp (gensym))
1204 (etype (dd-element-type defstruct)))
1205 `(defun ,cons-name ,arglist
1206 (declare ,@(mapcar #'(lambda (var type) `(type (and ,type ,etype) ,var))
1208 (let ((,temp (make-array ,(dd-length defstruct)
1209 :element-type ',(dd-element-type defstruct))))
1210 ,@(mapcar #'(lambda (x)
1211 `(setf (aref ,temp ,(cdr x)) ',(car x)))
1212 (find-name-indices defstruct))
1213 ,@(mapcar #'(lambda (dsd value)
1214 `(setf (aref ,temp ,(dsd-index dsd)) ,value))
1215 (dd-slots defstruct) values)
1217 (defun create-list-constructor
1218 (defstruct cons-name arglist vars types values)
1219 (let ((vals (make-list (dd-length defstruct) :initial-element nil)))
1220 (dolist (x (find-name-indices defstruct))
1221 (setf (elt vals (cdr x)) `',(car x)))
1222 (loop for dsd in (dd-slots defstruct) and val in values do
1223 (setf (elt vals (dsd-index dsd)) val))
1225 `(defun ,cons-name ,arglist
1226 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1229 (defun create-structure-constructor
1230 (defstruct cons-name arglist vars types values)
1231 (let* ((temp (gensym))
1232 (raw-index (dd-raw-index defstruct))
1233 (n-raw-data (when raw-index (gensym))))
1234 `(defun ,cons-name ,arglist
1235 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1237 (let ((,temp (truly-the ,(dd-name defstruct)
1238 (%make-instance ,(dd-length defstruct))))
1241 (make-array ,(dd-raw-length defstruct)
1242 :element-type '(unsigned-byte 32))))))
1243 (setf (%instance-layout ,temp)
1244 (%delayed-get-compiler-layout ,(dd-name defstruct)))
1246 `((setf (%instance-ref ,temp ,raw-index) ,n-raw-data)))
1247 ,@(mapcar (lambda (dsd value)
1248 (multiple-value-bind (accessor index data)
1249 (slot-accessor-form defstruct dsd temp n-raw-data)
1250 `(setf (,accessor ,data ,index) ,value)))
1251 (dd-slots defstruct)
1254 (defun create-fin-constructor
1255 (defstruct cons-name arglist vars types values)
1256 (let ((temp (gensym)))
1257 `(defun ,cons-name ,arglist
1258 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1260 (let ((,temp (truly-the
1261 ,(dd-name defstruct)
1262 (%make-funcallable-instance
1263 ,(dd-length defstruct)
1264 (%delayed-get-compiler-layout ,(dd-name defstruct))))))
1265 ,@(mapcar #'(lambda (dsd value)
1266 `(setf (%funcallable-instance-info
1267 ,temp ,(dsd-index dsd))
1269 (dd-slots defstruct) values)
1272 ;;; Create a default (non-BOA) keyword constructor.
1273 (defun create-keyword-constructor (defstruct creator)
1274 (collect ((arglist (list '&key))
1277 (dolist (slot (dd-slots defstruct))
1278 (let ((dum (gensym))
1279 (name (dsd-name slot)))
1280 (arglist `((,(keywordicate name) ,dum) ,(dsd-default slot)))
1281 (types (dsd-type slot))
1284 defstruct (dd-default-constructor defstruct)
1285 (arglist) (vals) (types) (vals))))
1287 ;;; Given a structure and a BOA constructor spec, call CREATOR with
1288 ;;; the appropriate args to make a constructor.
1289 (defun create-boa-constructor (defstruct boa creator)
1290 (multiple-value-bind (req opt restp rest keyp keys allowp aux)
1291 (sb!kernel:parse-lambda-list (second boa))
1295 (labels ((get-slot (name)
1296 (let ((res (find name (dd-slots defstruct)
1300 (values (dsd-type res) (dsd-default res))
1303 (multiple-value-bind (type default) (get-slot arg)
1304 (arglist `(,arg ,default))
1310 (types (get-slot arg)))
1313 (arglist '&optional)
1317 (name &optional (def (nth-value 1 (get-slot name))))
1319 (arglist `(,name ,def))
1321 (types (get-slot name))))
1323 (do-default arg)))))
1326 (arglist '&rest rest)
1334 (destructuring-bind (wot &optional (def nil def-p)) key
1335 (let ((name (if (consp wot)
1336 (destructuring-bind (key var) wot
1337 (declare (ignore key))
1340 (multiple-value-bind (type slot-def) (get-slot name)
1341 (arglist `(,wot ,(if def-p def slot-def)))
1346 (when allowp (arglist '&allow-other-keys))
1351 (let* ((arg (if (consp arg) arg (list arg)))
1355 (types (get-slot var))))))
1357 (funcall creator defstruct (first boa)
1358 (arglist) (vars) (types)
1359 (mapcar #'(lambda (slot)
1360 (or (find (dsd-name slot) (vars) :test #'string=)
1361 (dsd-default slot)))
1362 (dd-slots defstruct))))))
1364 ;;; Grovel the constructor options, and decide what constructors (if
1366 (defun constructor-definitions (defstruct)
1367 (let ((no-constructors nil)
1370 (creator (ecase (dd-type defstruct)
1371 (structure #'create-structure-constructor)
1372 (funcallable-structure #'create-fin-constructor)
1373 (vector #'create-vector-constructor)
1374 (list #'create-list-constructor))))
1375 (dolist (constructor (dd-constructors defstruct))
1376 (destructuring-bind (name &optional (boa-ll nil boa-p)) constructor
1377 (declare (ignore boa-ll))
1378 (cond ((not name) (setq no-constructors t))
1379 (boa-p (push constructor boas))
1380 (t (push name defaults)))))
1382 (when no-constructors
1383 (when (or defaults boas)
1384 (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs"))
1385 (return-from constructor-definitions ()))
1387 (unless (or defaults boas)
1388 (push (symbolicate "MAKE-" (dd-name defstruct)) defaults))
1392 (let ((cname (first defaults)))
1393 (setf (dd-default-constructor defstruct) cname)
1394 (res (create-keyword-constructor defstruct creator))
1395 (dolist (other-name (rest defaults))
1396 (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1397 (res `(declaim (ftype function ',other-name))))))
1400 (res (create-boa-constructor defstruct boa creator)))
1406 ;;; This is like PROCLAIM-AS-FUNCTION-NAME, but we also set the kind to
1407 ;;; :DECLARED and blow away any ASSUMED-TYPE. Also, if the thing is a
1408 ;;; slot accessor currently, quietly unaccessorize it. And if there
1409 ;;; are any undefined warnings, we nuke them.
1410 (defun proclaim-as-defstruct-function-name (name)
1412 (when (info :function :accessor-for name)
1413 (setf (info :function :accessor-for name) nil))
1414 (proclaim-as-function-name name)
1415 (note-name-defined name :function)
1416 (setf (info :function :where-from name) :declared)
1417 (when (info :function :assumed-type name)
1418 (setf (info :function :assumed-type name) nil)))
1421 ;;;; finalizing bootstrapping
1423 ;;; early structure placeholder definitions: Set up layout and class
1424 ;;; data for structures which are needed early.
1426 '#.(sb-cold:read-from-file
1427 "src/code/early-defstruct-args.lisp-expr"))
1428 (let* ((defstruct (parse-name-and-options-and-slot-descriptions
1431 (inherits (inherits-for-structure defstruct)))
1432 (function-%compiler-only-defstruct defstruct inherits)))
1434 (/show0 "code/defstruct.lisp end of file")