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 ;; Name of accessor, or NIL if this accessor has the same name as an
125 ;; inherited accessor (which we don't want to shadow.)
127 default ; default value expression
128 (type t) ; declared type specifier
129 ;; If this object does not describe a raw slot, this value is T.
131 ;; If this object describes a raw slot, this value is the type of the
132 ;; value that the raw slot holds. Mostly. (KLUDGE: If the raw slot has
133 ;; type (UNSIGNED-BYTE 32), the value here is UNSIGNED-BYTE, not
134 ;; (UNSIGNED-BYTE 32).)
135 (raw-type t :type (member t single-float double-float
136 #!+long-float long-float
137 complex-single-float complex-double-float
138 #!+long-float complex-long-float
140 (read-only nil :type (member t nil)))
141 (def!method print-object ((x defstruct-slot-description) stream)
142 (print-unreadable-object (x stream :type t)
143 (prin1 (dsd-name x) stream)))
145 ;;; Is DEFSTRUCT a structure with a class?
146 (defun class-structure-p (defstruct)
147 (member (dd-type defstruct) '(structure funcallable-structure)))
149 ;;; Return the name of a defstruct slot as a symbol. We store it as a
150 ;;; string to avoid creating lots of worthless symbols at load time.
151 (defun dsd-name (dsd)
152 (intern (string (dsd-%name dsd))
153 (if (dsd-accessor dsd)
154 (symbol-package (dsd-accessor dsd))
157 ;;;; typed (non-class) structures
159 ;;; Return a type specifier we can use for testing :TYPE'd structures.
160 (defun dd-lisp-type (defstruct)
161 (ecase (dd-type defstruct)
163 (vector `(simple-array ,(dd-element-type defstruct) (*)))))
165 ;;;; the legendary DEFSTRUCT macro itself (both CL:DEFSTRUCT and its
166 ;;;; close personal friend SB!XC:DEFSTRUCT)
168 ;;; Return a list of forms to install print and make-load-form funs, mentioning
169 ;;; them in the expansion so that they can be compiled.
170 (defun class-method-definitions (defstruct)
171 (let ((name (dd-name defstruct)))
173 ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant
174 ;; class names which creates fast but non-cold-loadable,
175 ;; non-compact code. In this context, we'd rather have
176 ;; compact, cold-loadable code. -- WHN 19990928
177 (declare (notinline sb!xc:find-class))
178 ,@(let ((pf (dd-print-function defstruct))
179 (po (dd-print-object defstruct))
182 ;; Giving empty :PRINT-OBJECT or :PRINT-FUNCTION options
183 ;; leaves PO or PF equal to NIL. The user-level effect is
184 ;; to generate a PRINT-OBJECT method specialized for the type,
185 ;; implementing the default #S structure-printing behavior.
186 (when (or (eq pf nil) (eq po nil))
187 (setf pf '(default-structure-print)
189 (flet (;; Given an arg from a :PRINT-OBJECT or :PRINT-FUNCTION
190 ;; option, return the value to pass as an arg to FUNCTION.
192 (destructuring-bind (function-name) oarg
194 (cond ((not (eql pf 0))
195 `((def!method print-object ((,x ,name) ,s)
196 (funcall #',(farg pf) ,x ,s *current-level*))))
198 `((def!method print-object ((,x ,name) ,s)
199 (funcall #',(farg po) ,x ,s))))
201 ,@(let ((pure (dd-pure defstruct)))
203 `((setf (layout-pure (class-layout
204 (sb!xc:find-class ',name)))
206 ((eq pure :substructure)
207 `((setf (layout-pure (class-layout
208 (sb!xc:find-class ',name)))
210 ,@(let ((def-con (dd-default-constructor defstruct)))
211 (when (and def-con (not (dd-alternate-metaclass defstruct)))
212 `((setf (structure-class-constructor (sb!xc:find-class ',name))
214 ;; FIXME: MAKE-LOAD-FORM is supposed to be handled here, too.
216 ;;; FIXME: I really would like to make structure accessors less special,
217 ;;; just ordinary inline functions. (Or perhaps inline functions with special
218 ;;; compact implementations of their expansions, to avoid bloating the system.)
220 ;;; shared logic for CL:DEFSTRUCT and SB!XC:DEFSTRUCT
222 ;;; FIXME: There should be some way to make this not be present in the
223 ;;; target executable, with EVAL-WHEN (COMPILE EXECUTE) and all that good
224 ;;; stuff, but for now I can't be bothered because of the messiness of
225 ;;; using CL:DEFMACRO in one case and SB!XC:DEFMACRO in another case.
226 ;;; Perhaps I could dodge this by defining it as an inline function instead?
227 ;;; Or perhaps just use MACROLET? I tried MACROLET and got nowhere and thought
228 ;;; I was tripping over either a compiler bug or ANSI weirdness, but this
229 ;;; test case seems to work in Debian CMU CL 2.4.9:
230 ;;; (macrolet ((emit-printer () ''(print "********")))
231 ;;; (defmacro fizz () (emit-printer)))
237 (defmacro expander-for-defstruct (name-and-options
239 expanding-into-code-for-xc-host-p)
240 `(let ((name-and-options ,name-and-options)
241 (slot-descriptions ,slot-descriptions)
242 (expanding-into-code-for-xc-host-p
243 ,expanding-into-code-for-xc-host-p))
244 (let* ((dd (parse-name-and-options-and-slot-descriptions
248 (if (class-structure-p dd)
249 (let ((inherits (inherits-for-structure dd)))
251 (/noshow0 "doing CLASS-STRUCTURE-P case for DEFSTRUCT " ,name)
252 (eval-when (:compile-toplevel :load-toplevel :execute)
253 (%compiler-only-defstruct ',dd ',inherits))
254 (%defstruct ',dd ',inherits)
255 ,@(when (eq (dd-type dd) 'structure)
256 `((%compiler-defstruct ',dd)))
257 (/noshow0 "starting not-for-the-xc-host section in DEFSTRUCT")
258 ,@(unless expanding-into-code-for-xc-host-p
259 (append (raw-accessor-definitions dd)
260 (predicate-definitions dd)
261 ;; FIXME: We've inherited from CMU CL nonparallel
262 ;; code for creating copiers for typed and untyped
263 ;; structures. This should be fixed.
264 ;(copier-definition dd)
265 (constructor-definitions dd)
266 (class-method-definitions dd)))
267 (/noshow0 "done with DEFSTRUCT " ,name)
270 (/show0 "doing NOT CLASS-STRUCTURE-P case for DEFSTRUCT " ,name)
271 (eval-when (:compile-toplevel :load-toplevel :execute)
272 (setf (info :typed-structure :info ',name) ',dd))
273 ,@(unless expanding-into-code-for-xc-host-p
274 (append (typed-accessor-definitions dd)
275 (typed-predicate-definitions dd)
276 (typed-copier-definitions dd)
277 (constructor-definitions dd)))
278 (/noshow0 "done with DEFSTRUCT " ,name)
281 (sb!xc:defmacro defstruct (name-and-options &rest slot-descriptions)
283 "DEFSTRUCT {Name | (Name Option*)} {Slot | (Slot [Default] {Key Value}*)}
284 Define the structure type Name. Instances are created by MAKE-<name>,
285 which takes &KEY arguments allowing initial slot values to the specified.
286 A SETF'able function <name>-<slot> is defined for each slot to read and
287 write slot values. <name>-p is a type predicate.
289 Popular DEFSTRUCT options (see manual for others):
293 Specify the name for the constructor or predicate.
295 (:CONSTRUCTOR Name Lambda-List)
296 Specify the name and arguments for a BOA constructor
297 (which is more efficient when keyword syntax isn't necessary.)
299 (:INCLUDE Supertype Slot-Spec*)
300 Make this type a subtype of the structure type Supertype. The optional
301 Slot-Specs override inherited slot options.
306 Asserts that the value of this slot is always of the specified type.
309 If true, no setter function is defined for this slot."
310 (expander-for-defstruct name-and-options slot-descriptions nil))
312 (defmacro sb!xc:defstruct (name-and-options &rest slot-descriptions)
314 "Cause information about a target structure to be built into the
316 (expander-for-defstruct name-and-options slot-descriptions t))
318 ;;;; functions to create various parts of DEFSTRUCT definitions
320 ;;; Catch requests to mess up definitions in COMMON-LISP.
322 (eval-when (:compile-toplevel :load-toplevel :execute)
323 (defun protect-cl (symbol)
324 (when (and *cold-init-complete-p*
325 (eq (symbol-package symbol) *cl-package*))
326 (cerror "Go ahead and patch the system."
327 "attempting to modify a symbol in the COMMON-LISP package: ~S"
330 ;;; Return forms to define readers and writers for raw slots as inline
332 (defun raw-accessor-definitions (dd)
333 (let* ((name (dd-name dd)))
335 (dolist (slot (dd-slots dd))
336 (let ((stype (dsd-type slot))
337 (accname (dsd-accessor slot))
338 (argname (gensym "ARG"))
339 (nvname (gensym "NEW-VALUE-")))
340 (multiple-value-bind (accessor offset data)
341 (slot-accessor-form dd slot argname)
342 ;; When accessor exists and is raw
343 (when (and accname (not (eq accessor '%instance-ref)))
344 (res `(declaim (inline ,accname)))
345 (res `(declaim (ftype (function (,name) ,stype) ,accname)))
346 (res `(defun ,accname (,argname)
347 (truly-the ,stype (,accessor ,data ,offset))))
348 (unless (dsd-read-only slot)
349 (res `(declaim (inline (setf ,accname))))
350 (res `(declaim (ftype (function (,stype ,name) ,stype)
352 ;; FIXME: I rewrote this somewhat from the CMU CL definition.
353 ;; Do some basic tests to make sure that reading and writing
354 ;; raw slots still works correctly.
355 (res `(defun (setf ,accname) (,nvname ,argname)
356 (setf (,accessor ,data ,offset) ,nvname)
360 ;;; Return a list of forms which create a predicate for an untyped DEFSTRUCT.
361 (defun predicate-definitions (dd)
362 (let ((pred (dd-predicate dd))
365 (if (eq (dd-type dd) 'funcallable-structure)
366 ;; FIXME: Why does this need to be special-cased for
367 ;; FUNCALLABLE-STRUCTURE? CMU CL did it, but without explanation.
368 ;; Could we do without it? What breaks if we do? Or could we
369 ;; perhaps get by with no predicates for funcallable structures?
370 `((declaim (inline ,pred))
371 (defun ,pred (,argname) (typep ,argname ',(dd-name dd))))
372 `((protect-cl ',pred)
373 (declaim (inline ,pred))
374 (defun ,pred (,argname)
375 (declare (optimize (speed 3) (safety 0)))
376 (typep-to-layout ,argname
377 (compile-time-find-layout ,(dd-name dd)))))))))
379 ;;; Return a list of forms which create a predicate function for a typed
381 (defun typed-predicate-definitions (defstruct)
382 (let ((name (dd-name defstruct))
383 (pred (dd-predicate defstruct))
385 (when (and pred (dd-named defstruct))
386 (let ((ltype (dd-lisp-type defstruct)))
387 `((defun ,pred (,argname)
388 (and (typep ,argname ',ltype)
389 (eq (elt (the ,ltype ,argname)
390 ,(cdr (car (last (find-name-indices defstruct)))))
393 ;;; FIXME: We've inherited from CMU CL code to do typed structure copiers
394 ;;; in a completely different way than untyped structure copiers. Fix this.
395 ;;; (This function was my first attempt to fix this, but I stopped before
396 ;;; figuring out how to install it completely and remove the parallel
397 ;;; code which simply SETF's the FDEFINITION of the DD-COPIER name.
399 ;;; Return the copier definition for an untyped DEFSTRUCT.
400 (defun copier-definition (dd)
401 (when (and (dd-copier dd)
402 ;; FUNCALLABLE-STRUCTUREs don't need copiers, and this
403 ;; implementation wouldn't work for them anyway, since
404 ;; COPY-STRUCTURE returns a STRUCTURE-OBJECT and they're not.
405 (not (eq (dd-type info) 'funcallable-structure)))
406 (let ((argname (gensym)))
408 (protect-cl ',(dd-copier dd))
409 (defun ,(dd-copier dd) (,argname)
410 (declare (type ,(dd-name dd) ,argname))
411 (copy-structure ,argname))))))
414 ;;; Return a list of forms to create a copier function of a typed DEFSTRUCT.
415 (defun typed-copier-definitions (defstruct)
416 (when (dd-copier defstruct)
417 `((setf (fdefinition ',(dd-copier defstruct)) #'copy-seq)
418 (declaim (ftype function ,(dd-copier defstruct))))))
420 ;;; Return a list of function definitions for accessing and setting the
421 ;;; slots of a typed DEFSTRUCT. The functions are proclaimed to be inline,
422 ;;; and the types of their arguments and results are declared as well. We
423 ;;; count on the compiler to do clever things with ELT.
424 (defun typed-accessor-definitions (defstruct)
426 (let ((ltype (dd-lisp-type defstruct)))
427 (dolist (slot (dd-slots defstruct))
428 (let ((name (dsd-accessor slot))
429 (index (dsd-index slot))
430 (slot-type `(and ,(dsd-type slot)
431 ,(dd-element-type defstruct))))
432 (stuff `(proclaim '(inline ,name (setf ,name))))
433 ;; FIXME: The arguments in the next two DEFUNs should be
434 ;; gensyms. (Otherwise e.g. if NEW-VALUE happened to be the
435 ;; name of a special variable, things could get weird.)
436 (stuff `(defun ,name (structure)
437 (declare (type ,ltype structure))
438 (the ,slot-type (elt structure ,index))))
439 (unless (dsd-read-only slot)
441 `(defun (setf ,name) (new-value structure)
442 (declare (type ,ltype structure) (type ,slot-type new-value))
443 (setf (elt structure ,index) new-value)))))))
448 (defun require-no-print-options-so-far (defstruct)
449 (unless (and (eql (dd-print-function defstruct) 0)
450 (eql (dd-print-object defstruct) 0))
451 (error "no more than one of the following options may be specified:
452 :PRINT-FUNCTION, :PRINT-OBJECT, :TYPE")))
454 ;;; Parse a single defstruct option and store the results in DEFSTRUCT.
455 (defun parse-1-option (option defstruct)
456 (let ((args (rest option))
457 (name (dd-name defstruct)))
460 (destructuring-bind (conc-name) args
461 (setf (dd-conc-name defstruct)
462 (if (symbolp conc-name)
464 (make-symbol (string conc-name))))))
466 (destructuring-bind (&optional (cname (symbolicate "MAKE-" name))
469 (push (cons cname stuff) (dd-constructors defstruct))))
471 (destructuring-bind (&optional (copier (symbolicate "COPY-" name)))
473 (setf (dd-copier defstruct) copier)))
475 (destructuring-bind (&optional (pred (symbolicate name "-P"))) args
476 (setf (dd-predicate defstruct) pred)))
478 (when (dd-include defstruct)
479 (error "more than one :INCLUDE option"))
480 (setf (dd-include defstruct) args))
481 (:alternate-metaclass
482 (setf (dd-alternate-metaclass defstruct) args))
484 (require-no-print-options-so-far defstruct)
485 (setf (dd-print-function defstruct)
486 (the (or symbol cons) args)))
488 (require-no-print-options-so-far defstruct)
489 (setf (dd-print-object defstruct)
490 (the (or symbol cons) args)))
492 (destructuring-bind (type) args
493 (cond ((eq type 'funcallable-structure)
494 (setf (dd-type defstruct) type))
495 ((member type '(list vector))
496 (setf (dd-element-type defstruct) 't)
497 (setf (dd-type defstruct) type))
498 ((and (consp type) (eq (first type) 'vector))
499 (destructuring-bind (vector vtype) type
500 (declare (ignore vector))
501 (setf (dd-element-type defstruct) vtype)
502 (setf (dd-type defstruct) 'vector)))
504 (error "~S is a bad :TYPE for Defstruct." type)))))
506 (error "The DEFSTRUCT option :NAMED takes no arguments."))
508 (destructuring-bind (offset) args
509 (setf (dd-offset defstruct) offset)))
511 (destructuring-bind (fun) args
512 (setf (dd-pure defstruct) fun)))
513 (t (error "unknown DEFSTRUCT option:~% ~S" option)))))
515 ;;; Given name and options, return a DD holding that info.
516 (eval-when (:compile-toplevel :load-toplevel :execute)
517 (defun parse-name-and-options (name-and-options)
518 (destructuring-bind (name &rest options) name-and-options
519 (aver name) ; A null name doesn't seem to make sense here.
520 (let ((defstruct (make-defstruct-description name)))
521 (dolist (option options)
522 (cond ((consp option)
523 (parse-1-option option defstruct))
525 (setf (dd-named defstruct) t))
526 ((member option '(:constructor :copier :predicate :named))
527 (parse-1-option (list option) defstruct))
529 (error "unrecognized DEFSTRUCT option: ~S" option))))
531 (case (dd-type defstruct)
533 (when (dd-offset defstruct)
534 (error ":OFFSET can't be specified unless :TYPE is specified."))
535 (unless (dd-include defstruct)
536 (incf (dd-length defstruct))))
537 (funcallable-structure)
539 (require-no-print-options-so-far defstruct)
540 (when (dd-named defstruct)
541 (incf (dd-length defstruct)))
542 (let ((offset (dd-offset defstruct)))
543 (when offset (incf (dd-length defstruct) offset)))))
545 (when (dd-include defstruct)
546 (do-inclusion-stuff defstruct))
550 ;;; Given name and options and slot descriptions (and possibly doc
551 ;;; string at the head of slot descriptions) return a DD holding that
553 (defun parse-name-and-options-and-slot-descriptions (name-and-options
555 (/noshow "PARSE-NAME-AND-OPTIONS-AND-SLOT-DESCRIPTIONS" name-and-options)
556 (let ((result (parse-name-and-options (if (atom name-and-options)
557 (list name-and-options)
559 (when (stringp (car slot-descriptions))
560 (setf (dd-doc result) (pop slot-descriptions)))
561 (dolist (slot slot-descriptions)
562 (allocate-1-slot result (parse-1-dsd result slot)))
567 ;;;; stuff to parse slot descriptions
569 ;;; Parse a slot description for DEFSTRUCT, add it to the description
570 ;;; and return it. If supplied, ISLOT is a pre-initialized DSD that we
571 ;;; modify to get the new slot. This is supplied when handling
572 ;;; included slots. If the new accessor name is already an accessor
573 ;;; for same slot in some included structure, then set the
574 ;;; DSD-ACCESSOR to NIL so that we don't clobber the more general
576 (defun parse-1-dsd (defstruct spec &optional
577 (islot (make-defstruct-slot-description :%name ""
580 (multiple-value-bind (name default default-p type type-p read-only ro-p)
585 &optional (default nil default-p)
586 &key (type nil type-p) (read-only nil ro-p))
590 (uncross type) type-p
593 (when (keywordp spec)
594 ;; FIXME: should be style warning
595 (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 islot) (string name))
605 (setf (dd-slots defstruct) (nconc (dd-slots defstruct) (list islot)))
607 (let* ((accname (symbolicate (or (dd-conc-name defstruct) "") name))
608 (existing (info :function :accessor-for accname)))
609 (if (and (structure-class-p existing)
610 (not (eq (sb!xc:class-name existing) (dd-name defstruct)))
611 (string= (dsd-%name (find accname
614 (class-layout existing)))
615 :key #'dsd-accessor))
617 (setf (dsd-accessor islot) nil)
618 (setf (dsd-accessor islot) accname)))
621 (setf (dsd-default islot) default))
623 (setf (dsd-type islot)
624 (if (eq (dsd-type islot) 't)
626 `(and ,(dsd-type islot) ,type))))
629 (setf (dsd-read-only islot) t)
630 (when (dsd-read-only islot)
631 (error "Slot ~S is :READ-ONLY in parent and must be :READ-ONLY in subtype ~S."
636 ;;; When a value of type TYPE is stored in a structure, should it be
637 ;;; stored in a raw slot? Return (VALUES RAW? RAW-TYPE WORDS), where
638 ;;; RAW? is true if TYPE should be stored in a raw slot.
639 ;;; RAW-TYPE is the raw slot type, or NIL if no raw slot.
640 ;;; WORDS is the number of words in the raw slot, or NIL if no raw slot.
641 (defun structure-raw-slot-type-and-size (type)
642 (/noshow "in STRUCTURE-RAW-SLOT-TYPE-AND-SIZE" type (sb!xc:subtypep type 'fixnum))
644 (;; FIXME: For now we suppress raw slots, since there are various
645 ;; issues about the way that the cross-compiler handles them.
646 (not (boundp '*dummy-placeholder-to-stop-compiler-warnings*))
647 (values nil nil nil))
648 ((and (sb!xc:subtypep type '(unsigned-byte 32))
649 (multiple-value-bind (fixnum? fixnum-certain?)
650 (sb!xc:subtypep type 'fixnum)
651 (/noshow fixnum? fixnum-certain?)
652 ;; (The extra test for FIXNUM-CERTAIN? here is
653 ;; intended for bootstrapping the system. In
654 ;; particular, in sbcl-0.6.2, we set up LAYOUT before
655 ;; FIXNUM is defined, and so could bogusly end up
656 ;; putting INDEX-typed values into raw slots if we
657 ;; didn't test FIXNUM-CERTAIN?.)
658 (and (not fixnum?) fixnum-certain?)))
659 (values t 'unsigned-byte 1))
660 ((sb!xc:subtypep type 'single-float)
661 (values t 'single-float 1))
662 ((sb!xc:subtypep type 'double-float)
663 (values t 'double-float 2))
665 ((sb!xc:subtypep type 'long-float)
666 (values t 'long-float #!+x86 3 #!+sparc 4))
667 ((sb!xc:subtypep type '(complex single-float))
668 (values t 'complex-single-float 2))
669 ((sb!xc:subtypep type '(complex double-float))
670 (values t 'complex-double-float 4))
672 ((sb!xc:subtypep type '(complex long-float))
673 (values t 'complex-long-float #!+x86 6 #!+sparc 8))
675 (values nil nil nil))))
677 ;;; Allocate storage for a DSD in DEFSTRUCT. This is where we decide
678 ;;; whether a slot is raw or not. If raw, and we haven't allocated a
679 ;;; raw-index yet for the raw data vector, then do it. Raw objects are
680 ;;; aligned on the unit of their size.
681 (defun allocate-1-slot (defstruct dsd)
682 (multiple-value-bind (raw? raw-type words)
683 (if (eq (dd-type defstruct) 'structure)
684 (structure-raw-slot-type-and-size (dsd-type dsd))
685 (values nil nil nil))
686 (/noshow "ALLOCATE-1-SLOT" dsd raw? raw-type words)
688 (setf (dsd-index dsd) (dd-length defstruct))
689 (incf (dd-length defstruct)))
691 (unless (dd-raw-index defstruct)
692 (setf (dd-raw-index defstruct) (dd-length defstruct))
693 (incf (dd-length defstruct)))
694 (let ((off (rem (dd-raw-length defstruct) words)))
696 (incf (dd-raw-length defstruct) (- words off))))
697 (setf (dsd-raw-type dsd) raw-type)
698 (setf (dsd-index dsd) (dd-raw-length defstruct))
699 (incf (dd-raw-length defstruct) words))))
702 (defun typed-structure-info-or-lose (name)
703 (or (info :typed-structure :info name)
704 (error ":TYPE'd DEFSTRUCT ~S not found for inclusion." name)))
706 ;;; Process any included slots pretty much like they were specified.
707 ;;; Also inherit various other attributes.
708 (defun do-inclusion-stuff (defstruct)
710 (included-name &rest modified-slots)
711 (dd-include defstruct)
712 (let* ((type (dd-type defstruct))
714 (if (class-structure-p defstruct)
715 (layout-info (compiler-layout-or-lose included-name))
716 (typed-structure-info-or-lose included-name))))
717 (unless (and (eq type (dd-type included-structure))
718 (type= (specifier-type (dd-element-type included-structure))
719 (specifier-type (dd-element-type defstruct))))
720 (error ":TYPE option mismatch between structures ~S and ~S."
721 (dd-name defstruct) included-name))
723 (incf (dd-length defstruct) (dd-length included-structure))
724 (when (class-structure-p defstruct)
725 (let ((mc (rest (dd-alternate-metaclass included-structure))))
726 (when (and mc (not (dd-alternate-metaclass defstruct)))
727 (setf (dd-alternate-metaclass defstruct)
728 (cons included-name mc))))
729 (when (eq (dd-pure defstruct) :unspecified)
730 (setf (dd-pure defstruct) (dd-pure included-structure)))
731 (setf (dd-raw-index defstruct) (dd-raw-index included-structure))
732 (setf (dd-raw-length defstruct) (dd-raw-length included-structure)))
734 (dolist (islot (dd-slots included-structure))
735 (let* ((iname (dsd-name islot))
736 (modified (or (find iname modified-slots
737 :key #'(lambda (x) (if (atom x) x (car x)))
740 (parse-1-dsd defstruct modified (copy-structure islot)))))))
742 ;;; This function is called at macroexpand time to compute the INHERITS
743 ;;; vector for a structure type definition.
744 (defun inherits-for-structure (info)
745 (declare (type defstruct-description info))
746 (let* ((include (dd-include info))
747 (superclass-opt (dd-alternate-metaclass info))
750 (compiler-layout-or-lose (first include))
751 (class-layout (sb!xc:find-class
752 (or (first superclass-opt)
753 'structure-object))))))
754 (if (eq (dd-name info) 'lisp-stream)
755 ;; a hack to added the stream class as a mixin for LISP-STREAMs
756 (concatenate 'simple-vector
757 (layout-inherits super)
759 (class-layout (sb!xc:find-class 'stream))))
760 (concatenate 'simple-vector
761 (layout-inherits super)
764 ;;; Do miscellaneous (LOAD EVAL) time actions for the structure
765 ;;; described by INFO. Create the class & layout, checking for
766 ;;; incompatible redefinition. Define setters, accessors, copier,
767 ;;; predicate, documentation, instantiate definition in load-time env.
768 ;;; This is only called for default structures.
769 (defun %defstruct (info inherits)
770 (declare (type defstruct-description info))
771 (multiple-value-bind (class layout old-layout)
772 (ensure-structure-class info inherits "current" "new")
773 (cond ((not old-layout)
774 (unless (eq (class-layout class) layout)
775 (register-layout layout)))
777 (let ((old-info (layout-info old-layout)))
778 (when (defstruct-description-p old-info)
779 (dolist (slot (dd-slots old-info))
780 (fmakunbound (dsd-accessor slot))
781 (unless (dsd-read-only slot)
782 (fmakunbound `(setf ,(dsd-accessor slot)))))))
783 (%redefine-defstruct class old-layout layout)
784 (setq layout (class-layout class))))
786 (setf (sb!xc:find-class (dd-name info)) class)
788 ;; Set FDEFINITIONs for structure accessors, setters, predicates,
791 (unless (eq (dd-type info) 'funcallable-structure)
793 (dolist (slot (dd-slots info))
795 (when (and (dsd-accessor slot)
796 (eq (dsd-raw-type slot) t))
797 (protect-cl (dsd-accessor slot))
798 (setf (symbol-function (dsd-accessor slot))
799 (structure-slot-getter layout dsd))
800 (unless (dsd-read-only slot)
801 (setf (fdefinition `(setf ,(dsd-accessor slot)))
802 (structure-slot-setter layout dsd))))))
804 ;; FIXME: See comment on corresponding code in %%COMPILER-DEFSTRUCT.
806 (when (dd-predicate info)
807 (protect-cl (dd-predicate info))
808 (setf (symbol-function (dd-predicate info))
810 (declare (optimize (speed 3) (safety 0)))
811 (typep-to-layout object layout))))
814 (when (dd-copier info)
815 (protect-cl (dd-copier info))
816 (setf (symbol-function (dd-copier info))
817 #'(lambda (structure)
818 (declare (optimize (speed 3) (safety 0)))
819 (flet ((layout-test (structure)
820 (typep-to-layout structure layout)))
821 (unless (layout-test structure)
822 (error 'simple-type-error
824 :expected-type '(satisfies layout-test)
826 "Structure for copier is not a ~S:~% ~S"
828 (list (sb!xc:class-name (layout-class layout))
830 (copy-structure structure))))))
833 (setf (fdocumentation (dd-name info) 'type) (dd-doc info)))
837 ;;; This function is called at compile-time to do the
838 ;;; compile-time-only actions for defining a structure type. It
839 ;;; installs the class in the type system in a similar way to
840 ;;; %DEFSTRUCT, but is quieter and safer in the case of redefinition.
842 ;;; The comments for the classic CMU CL version of this function said
843 ;;; that EVAL-WHEN doesn't do the right thing when nested or
844 ;;; non-top-level, and so CMU CL had the function magically called by
845 ;;; the compiler. Unfortunately, this doesn't do the right thing
846 ;;; either: compiling a function (DEFUN FOO () (DEFSTRUCT FOO X Y))
847 ;;; causes the class FOO to become defined, even though FOO is never
848 ;;; loaded or executed. Even more unfortunately, I've been unable to
849 ;;; come up with any EVAL-WHEN tricks which work -- I finally gave up
850 ;;; on this approach when trying to get the system to cross-compile
851 ;;; error.lisp. (Just because I haven't found it doesn't mean that it
852 ;;; doesn't exist, of course. Alas, I continue to have some trouble
853 ;;; understanding compile/load semantics in Common Lisp.) So we
854 ;;; continue to use the IR1 transformation approach, even though it's
855 ;;; known to be buggy. -- WHN 19990507
857 ;;; Basically, this function avoids trashing the compiler by only
858 ;;; actually defining the class if there is no current definition.
859 ;;; Instead, we just set the INFO TYPE COMPILER-LAYOUT. This behavior
860 ;;; is left over from classic CMU CL and may not be necessary in the
861 ;;; new build system. -- WHN 19990507
863 ;;; FUNCTION-%COMPILER-ONLY-DEFSTRUCT is an ordinary function, called
864 ;;; by both the IR1 transform version of %COMPILER-ONLY-DEFSTRUCT and
865 ;;; by the ordinary function version of %COMPILER-ONLY-DEFSTRUCT. (The
866 ;;; ordinary function version is there for the interpreter and for
868 (defun %compiler-only-defstruct (info inherits)
869 (function-%compiler-only-defstruct info inherits))
870 (defun function-%compiler-only-defstruct (info inherits)
871 (multiple-value-bind (class layout old-layout)
872 (multiple-value-bind (clayout clayout-p)
873 (info :type :compiler-layout (dd-name info))
874 (ensure-structure-class info
876 (if clayout-p "previously compiled" "current")
878 :compiler-layout clayout))
880 (undefine-structure (layout-class old-layout))
881 (when (and (class-subclasses class)
882 (not (eq layout old-layout)))
884 (dohash (class layout (class-subclasses class))
885 (declare (ignore layout))
886 (undefine-structure class)
887 (subs (class-proper-name class)))
889 (warn "Removing old subclasses of ~S:~% ~S"
890 (sb!xc:class-name class)
893 (unless (eq (class-layout class) layout)
894 (register-layout layout :invalidate nil))
895 (setf (sb!xc:find-class (dd-name info)) class)))
897 (setf (info :type :compiler-layout (dd-name info)) layout))
900 ;;; This function does the (COMPILE LOAD EVAL) time actions for updating the
901 ;;; compiler's global meta-information to represent the definition of the
902 ;;; structure described by Info. This primarily amounts to setting up info
903 ;;; about the accessor and other implicitly defined functions. The constructors
904 ;;; are explicitly defined by top-level code.
905 (defun %%compiler-defstruct (info)
906 (declare (type defstruct-description info))
907 (let* ((name (dd-name info))
908 (class (sb!xc:find-class name)))
909 (let ((copier (dd-copier info)))
911 (proclaim `(ftype (function (,name) ,name) ,copier))))
913 ;; FIXME: This (and corresponding code in %DEFSTRUCT) are the way
914 ;; that CMU CL defined the predicate, instead of using DEFUN.
915 ;; Perhaps it would be better to go back to to the CMU CL way, or
916 ;; something similar. I want to reduce the amount of magic in
917 ;; defstruct functions, but making the predicate be a closure
918 ;; looks like a good thing, and can even be done without magic.
919 ;; (OTOH, there are some bootstrapping issues involved, since
920 ;; GENESIS understands DEFUN but doesn't understand a
921 ;; (SETF SYMBOL-FUNCTION) call inside %DEFSTRUCT.)
923 (let ((pred (dd-predicate info)))
925 (proclaim-as-defstruct-function-name pred)
926 (setf (info :function :inlinep pred) :inline)
927 (setf (info :function :inline-expansion pred)
928 `(lambda (x) (typep x ',name)))))
931 (dolist (slot (dd-slots info))
932 (let* ((fun (dsd-accessor slot))
933 (setf-fun `(setf ,fun)))
934 (when (and fun (eq (dsd-raw-type slot) t))
935 (proclaim-as-defstruct-function-name fun)
936 (setf (info :function :accessor-for fun) class)
937 (unless (dsd-read-only slot)
938 (proclaim-as-defstruct-function-name setf-fun)
939 (setf (info :function :accessor-for setf-fun) class))))))
943 ;;; Ordinarily this is preempted by an IR1 transformation, but this
944 ;;; definition is still useful for the interpreter and code walkers.
945 (defun %compiler-defstruct (info)
946 (%%compiler-defstruct info))
948 ;;;; redefinition stuff
950 ;;; Compare the slots of OLD and NEW, returning 3 lists of slot names:
951 ;;; 1. Slots which have moved,
952 ;;; 2. Slots whose type has changed,
953 ;;; 3. Deleted slots.
954 (defun compare-slots (old new)
955 (let* ((oslots (dd-slots old))
956 (nslots (dd-slots new))
957 (onames (mapcar #'dsd-name oslots))
958 (nnames (mapcar #'dsd-name nslots)))
961 (dolist (name (intersection onames nnames))
962 (let ((os (find name oslots :key #'dsd-name))
963 (ns (find name nslots :key #'dsd-name)))
964 (unless (subtypep (dsd-type ns) (dsd-type os))
965 (/noshow "found retyped slots" ns os (dsd-type ns) (dsd-type os))
967 (unless (and (= (dsd-index os) (dsd-index ns))
968 (eq (dsd-raw-type os) (dsd-raw-type ns)))
972 (set-difference onames nnames)))))
974 ;;; If we are redefining a structure with different slots than in the
975 ;;; currently loaded version, give a warning and return true.
976 (defun redefine-structure-warning (class old new)
977 (declare (type defstruct-description old new)
978 (type sb!xc:class class)
980 (let ((name (dd-name new)))
981 (multiple-value-bind (moved retyped deleted) (compare-slots old new)
982 (when (or moved retyped deleted)
984 "incompatibly redefining slots of structure class ~S~@
985 Make sure any uses of affected accessors are recompiled:~@
986 ~@[ These slots were moved to new positions:~% ~S~%~]~
987 ~@[ These slots have new incompatible types:~% ~S~%~]~
988 ~@[ These slots were deleted:~% ~S~%~]"
989 name moved retyped deleted)
992 ;;; This function is called when we are incompatibly redefining a
993 ;;; structure Class to have the specified New-Layout. We signal an
994 ;;; error with some proceed options and return the layout that should
996 (defun %redefine-defstruct (class old-layout new-layout)
997 (declare (type sb!xc:class class) (type layout old-layout new-layout))
998 (let ((name (class-proper-name class)))
1000 (error "redefining class ~S incompatibly with the current definition"
1003 :report "Invalidate current definition."
1004 (warn "Previously loaded ~S accessors will no longer work." name)
1005 (register-layout new-layout))
1007 :report "Smash current layout, preserving old code."
1008 (warn "Any old ~S instances will be in a bad way.~@
1009 I hope you know what you're doing..."
1011 (register-layout new-layout :invalidate nil
1012 :destruct-layout old-layout))))
1015 ;;; This is called when we are about to define a structure class. It
1016 ;;; returns a (possibly new) class object and the layout which should
1017 ;;; be used for the new definition (may be the current layout, and
1018 ;;; also might be an uninstalled forward referenced layout.) The third
1019 ;;; value is true if this is an incompatible redefinition, in which
1020 ;;; case it is the old layout.
1021 (defun ensure-structure-class (info inherits old-context new-context
1022 &key compiler-layout)
1023 (multiple-value-bind (class old-layout)
1027 (class 'sb!xc:structure-class)
1028 (constructor 'make-structure-class))
1029 (dd-alternate-metaclass info)
1030 (declare (ignore name))
1031 (insured-find-class (dd-name info)
1032 (if (eq class 'sb!xc:structure-class)
1034 (typep x 'sb!xc:structure-class))
1036 (sb!xc:typep x (sb!xc:find-class class))))
1037 (fdefinition constructor)))
1038 (setf (class-direct-superclasses class)
1039 (if (eq (dd-name info) 'lisp-stream)
1040 ;; a hack to add STREAM as a superclass mixin to LISP-STREAMs
1041 (list (layout-class (svref inherits (1- (length inherits))))
1042 (layout-class (svref inherits (- (length inherits) 2))))
1043 (list (layout-class (svref inherits (1- (length inherits)))))))
1044 (let ((new-layout (make-layout :class class
1046 :depthoid (length inherits)
1047 :length (dd-length info)
1049 (old-layout (or compiler-layout old-layout)))
1052 (values class new-layout nil))
1053 (;; This clause corresponds to an assertion in REDEFINE-LAYOUT-WARNING
1054 ;; of classic CMU CL. I moved it out to here because it was only
1055 ;; exercised in this code path anyway. -- WHN 19990510
1056 (not (eq (layout-class new-layout) (layout-class old-layout)))
1057 (error "shouldn't happen: weird state of OLD-LAYOUT?"))
1058 ((not *type-system-initialized*)
1059 (setf (layout-info old-layout) info)
1060 (values class old-layout nil))
1061 ((redefine-layout-warning old-context
1064 (layout-length new-layout)
1065 (layout-inherits new-layout)
1066 (layout-depthoid new-layout))
1067 (values class new-layout old-layout))
1069 (let ((old-info (layout-info old-layout)))
1071 ((or defstruct-description)
1072 (cond ((redefine-structure-warning class old-info info)
1073 (values class new-layout old-layout))
1075 (setf (layout-info old-layout) info)
1076 (values class old-layout nil))))
1078 (setf (layout-info old-layout) info)
1079 (values class old-layout nil))
1081 (error "shouldn't happen! strange thing in LAYOUT-INFO:~% ~S"
1083 (values class new-layout old-layout)))))))))
1085 ;;; Blow away all the compiler info for the structure CLASS. Iterate
1086 ;;; over this type, clearing the compiler structure type info, and
1087 ;;; undefining all the associated functions.
1088 (defun undefine-structure (class)
1089 (let ((info (layout-info (class-layout class))))
1090 (when (defstruct-description-p info)
1091 (let ((type (dd-name info)))
1092 (setf (info :type :compiler-layout type) nil)
1093 (undefine-function-name (dd-copier info))
1094 (undefine-function-name (dd-predicate info))
1095 (dolist (slot (dd-slots info))
1096 (let ((fun (dsd-accessor slot)))
1097 (undefine-function-name fun)
1098 (unless (dsd-read-only slot)
1099 (undefine-function-name `(setf ,fun))))))
1100 ;; Clear out the SPECIFIER-TYPE cache so that subsequent
1101 ;; references are unknown types.
1102 (values-specifier-type-cache-clear)))
1105 ;;; Return a list of pairs (name . index). Used for :TYPE'd
1106 ;;; constructors to find all the names that we have to splice in &
1107 ;;; where. Note that these types don't have a layout, so we can't look
1108 ;;; at LAYOUT-INHERITS.
1109 (defun find-name-indices (defstruct)
1112 (do ((info defstruct
1113 (typed-structure-info-or-lose (first (dd-include info)))))
1114 ((not (dd-include info))
1119 (dolist (info infos)
1120 (incf i (or (dd-offset info) 0))
1121 (when (dd-named info)
1122 (res (cons (dd-name info) i)))
1123 (setq i (dd-length info)))))
1127 ;;;; slot accessors for raw slots
1129 ;;; Return info about how to read/write a slot in the value stored in
1130 ;;; OBJECT. This is also used by constructors (we can't use the
1131 ;;; accessor function, since some slots are read-only.) If supplied,
1132 ;;; DATA is a variable holding the raw-data vector.
1134 ;;; returned values:
1135 ;;; 1. accessor function name (SETFable)
1136 ;;; 2. index to pass to accessor.
1137 ;;; 3. object form to pass to accessor
1138 (defun slot-accessor-form (defstruct slot object &optional data)
1139 (let ((rtype (dsd-raw-type slot)))
1142 (single-float '%raw-ref-single)
1143 (double-float '%raw-ref-double)
1145 (long-float '%raw-ref-long)
1146 (complex-single-float '%raw-ref-complex-single)
1147 (complex-double-float '%raw-ref-complex-double)
1149 (complex-long-float '%raw-ref-complex-long)
1150 (unsigned-byte 'aref)
1152 (if (eq (dd-type defstruct) 'funcallable-structure)
1153 '%funcallable-instance-info
1158 (truncate (dsd-index slot) #!+x86 6 #!+sparc 8))
1161 (truncate (dsd-index slot) #!+x86 3 #!+sparc 4))
1163 (ash (dsd-index slot) -1))
1164 (complex-double-float
1165 (ash (dsd-index slot) -2))
1166 (complex-single-float
1167 (ash (dsd-index slot) -1))
1171 ((eq rtype 't) object)
1174 `(truly-the (simple-array (unsigned-byte 32) (*))
1175 (%instance-ref ,object ,(dd-raw-index defstruct))))))))
1177 ;;; These functions are called to actually make a constructor after we
1178 ;;; have processed the arglist. The correct variant (according to the
1179 ;;; DD-TYPE) should be called. The function is defined with the
1180 ;;; specified name and arglist. Vars and Types are used for argument
1181 ;;; type declarations. Values are the values for the slots (in order.)
1183 ;;; This is split four ways because:
1184 ;;; 1] list & vector structures need "name" symbols stuck in at
1185 ;;; various weird places, whereas STRUCTURE structures have
1187 ;;; 2] We really want to use LIST to make list structures, instead of
1188 ;;; MAKE-LIST/(SETF ELT).
1189 ;;; 3] STRUCTURE structures can have raw slots that must also be
1190 ;;; allocated and indirectly referenced. We use SLOT-ACCESSOR-FORM
1191 ;;; to compute how to set the slots, which deals with raw slots.
1192 ;;; 4] Funcallable structures are weird.
1193 (defun create-vector-constructor
1194 (defstruct cons-name arglist vars types values)
1195 (let ((temp (gensym))
1196 (etype (dd-element-type defstruct)))
1197 `(defun ,cons-name ,arglist
1198 (declare ,@(mapcar #'(lambda (var type) `(type (and ,type ,etype) ,var))
1200 (let ((,temp (make-array ,(dd-length defstruct)
1201 :element-type ',(dd-element-type defstruct))))
1202 ,@(mapcar #'(lambda (x)
1203 `(setf (aref ,temp ,(cdr x)) ',(car x)))
1204 (find-name-indices defstruct))
1205 ,@(mapcar #'(lambda (dsd value)
1206 `(setf (aref ,temp ,(dsd-index dsd)) ,value))
1207 (dd-slots defstruct) values)
1209 (defun create-list-constructor
1210 (defstruct cons-name arglist vars types values)
1211 (let ((vals (make-list (dd-length defstruct) :initial-element nil)))
1212 (dolist (x (find-name-indices defstruct))
1213 (setf (elt vals (cdr x)) `',(car x)))
1214 (loop for dsd in (dd-slots defstruct) and val in values do
1215 (setf (elt vals (dsd-index dsd)) val))
1217 `(defun ,cons-name ,arglist
1218 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1221 (defun create-structure-constructor
1222 (defstruct cons-name arglist vars types values)
1223 (let* ((temp (gensym))
1224 (raw-index (dd-raw-index defstruct))
1225 (n-raw-data (when raw-index (gensym))))
1226 `(defun ,cons-name ,arglist
1227 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1229 (let ((,temp (truly-the ,(dd-name defstruct)
1230 (%make-instance ,(dd-length defstruct))))
1233 (make-array ,(dd-raw-length defstruct)
1234 :element-type '(unsigned-byte 32))))))
1235 (setf (%instance-layout ,temp)
1236 (%delayed-get-compiler-layout ,(dd-name defstruct)))
1238 `((setf (%instance-ref ,temp ,raw-index) ,n-raw-data)))
1239 ,@(mapcar (lambda (dsd value)
1240 (multiple-value-bind (accessor index data)
1241 (slot-accessor-form defstruct dsd temp n-raw-data)
1242 `(setf (,accessor ,data ,index) ,value)))
1243 (dd-slots defstruct)
1246 (defun create-fin-constructor
1247 (defstruct cons-name arglist vars types values)
1248 (let ((temp (gensym)))
1249 `(defun ,cons-name ,arglist
1250 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1252 (let ((,temp (truly-the
1253 ,(dd-name defstruct)
1254 (%make-funcallable-instance
1255 ,(dd-length defstruct)
1256 (%delayed-get-compiler-layout ,(dd-name defstruct))))))
1257 ,@(mapcar #'(lambda (dsd value)
1258 `(setf (%funcallable-instance-info
1259 ,temp ,(dsd-index dsd))
1261 (dd-slots defstruct) values)
1264 ;;; Create a default (non-BOA) keyword constructor.
1265 (defun create-keyword-constructor (defstruct creator)
1266 (collect ((arglist (list '&key))
1269 (dolist (slot (dd-slots defstruct))
1270 (let ((dum (gensym))
1271 (name (dsd-name slot)))
1272 (arglist `((,(intern (string name) "KEYWORD") ,dum)
1273 ,(dsd-default slot)))
1274 (types (dsd-type slot))
1277 defstruct (dd-default-constructor defstruct)
1278 (arglist) (vals) (types) (vals))))
1280 ;;; Given a structure and a BOA constructor spec, call CREATOR with
1281 ;;; the appropriate args to make a constructor.
1282 (defun create-boa-constructor (defstruct boa creator)
1283 (multiple-value-bind (req opt restp rest keyp keys allowp aux)
1284 (sb!kernel:parse-lambda-list (second boa))
1288 (labels ((get-slot (name)
1289 (let ((res (find name (dd-slots defstruct)
1293 (values (dsd-type res) (dsd-default res))
1296 (multiple-value-bind (type default) (get-slot arg)
1297 (arglist `(,arg ,default))
1303 (types (get-slot arg)))
1306 (arglist '&optional)
1310 (name &optional (def (nth-value 1 (get-slot name))))
1312 (arglist `(,name ,def))
1314 (types (get-slot name))))
1316 (do-default arg)))))
1319 (arglist '&rest rest)
1327 (destructuring-bind (wot &optional (def nil def-p)) key
1328 (let ((name (if (consp wot)
1329 (destructuring-bind (key var) wot
1330 (declare (ignore key))
1333 (multiple-value-bind (type slot-def) (get-slot name)
1334 (arglist `(,wot ,(if def-p def slot-def)))
1339 (when allowp (arglist '&allow-other-keys))
1344 (let* ((arg (if (consp arg) arg (list arg)))
1348 (types (get-slot var))))))
1350 (funcall creator defstruct (first boa)
1351 (arglist) (vars) (types)
1352 (mapcar #'(lambda (slot)
1353 (or (find (dsd-name slot) (vars) :test #'string=)
1354 (dsd-default slot)))
1355 (dd-slots defstruct))))))
1357 ;;; Grovel the constructor options, and decide what constructors (if
1359 (defun constructor-definitions (defstruct)
1360 (let ((no-constructors nil)
1363 (creator (ecase (dd-type defstruct)
1364 (structure #'create-structure-constructor)
1365 (funcallable-structure #'create-fin-constructor)
1366 (vector #'create-vector-constructor)
1367 (list #'create-list-constructor))))
1368 (dolist (constructor (dd-constructors defstruct))
1369 (destructuring-bind (name &optional (boa-ll nil boa-p)) constructor
1370 (declare (ignore boa-ll))
1371 (cond ((not name) (setq no-constructors t))
1372 (boa-p (push constructor boas))
1373 (t (push name defaults)))))
1375 (when no-constructors
1376 (when (or defaults boas)
1377 (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs"))
1378 (return-from constructor-definitions ()))
1380 (unless (or defaults boas)
1381 (push (symbolicate "MAKE-" (dd-name defstruct)) defaults))
1385 (let ((cname (first defaults)))
1386 (setf (dd-default-constructor defstruct) cname)
1387 (res (create-keyword-constructor defstruct creator))
1388 (dolist (other-name (rest defaults))
1389 (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1390 (res `(declaim (ftype function ',other-name))))))
1393 (res (create-boa-constructor defstruct boa creator)))
1399 ;;; Like PROCLAIM-AS-FUNCTION-NAME, but we also set the kind to
1400 ;;; :DECLARED and blow away any ASSUMED-TYPE. Also, if the thing is a
1401 ;;; slot accessor currently, quietly unaccessorize it. And if there
1402 ;;; are any undefined warnings, we nuke them.
1403 (defun proclaim-as-defstruct-function-name (name)
1405 (when (info :function :accessor-for name)
1406 (setf (info :function :accessor-for name) nil))
1407 (proclaim-as-function-name name)
1408 (note-name-defined name :function)
1409 (setf (info :function :where-from name) :declared)
1410 (when (info :function :assumed-type name)
1411 (setf (info :function :assumed-type name) nil)))
1414 ;;;; finalizing bootstrapping
1416 ;;; early structure placeholder definitions: Set up layout and class
1417 ;;; data for structures which are needed early.
1419 '#.(sb-cold:read-from-file
1420 "src/code/early-defstruct-args.lisp-expr"))
1421 (let* ((defstruct (parse-name-and-options-and-slot-descriptions
1424 (inherits (inherits-for-structure defstruct)))
1425 (function-%compiler-only-defstruct defstruct inherits)))
1427 (/show0 "code/defstruct.lisp end of file")