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")
17 ;;; Return the compiler layout for Name. (The class referred to by
18 ;;; NAME must be a structure-like class.)
19 (defun compiler-layout-or-lose (name)
20 (let ((res (info :type :compiler-layout name)))
22 (error "Class is not yet defined or was undefined: ~S" name))
23 ((not (typep (layout-info res) 'defstruct-description))
24 (error "Class is not a structure class: ~S" name))
27 ;;; Delay looking for compiler-layout until the constructor is being
28 ;;; compiled, since it doesn't exist until after the eval-when
29 ;;; (compile) is compiled.
30 (sb!xc:defmacro %delayed-get-compiler-layout (name)
31 `',(compiler-layout-or-lose name))
33 ;;; Get layout right away.
34 (sb!xc:defmacro compile-time-find-layout (name)
37 ;;; re. %DELAYED-GET-COMPILER-LAYOUT and COMPILE-TIME-FIND-LAYOUT, above..
39 ;;; FIXME: Perhaps both should be defined with DEFMACRO-MUNDANELY?
40 ;;; FIXME: Do we really need both? If so, their names and implementations
41 ;;; should probably be tweaked to be more parallel.
43 ;;; The DEFSTRUCT-DESCRIPTION structure holds compile-time information about a
45 (def!struct (defstruct-description
47 (:make-load-form-fun just-dump-it-normally)
48 #-sb-xc-host (:pure t)
49 (:constructor make-defstruct-description (name)))
50 ;; name of the structure
51 (name (required-argument) :type symbol)
52 ;; documentation on the structure
53 (doc nil :type (or string null))
54 ;; prefix for slot names. If NIL, none.
55 (conc-name (concat-pnames name '-) :type (or symbol null))
56 ;; the name of the primary standard keyword constructor, or NIL if none
57 (default-constructor nil :type (or symbol null))
58 ;; all the explicit :CONSTRUCTOR specs, with name defaulted
59 (constructors () :type list)
60 ;; name of copying function
61 (copier (concat-pnames 'copy- name) :type (or symbol null))
62 ;; name of type predicate
63 (predicate (concat-pnames name '-p) :type (or symbol null))
64 ;; the arguments to the :INCLUDE option, or NIL if no included
66 (include nil :type list)
67 ;; The arguments to the :ALTERNATE-METACLASS option (an extension
68 ;; used to define structure-like objects with an arbitrary
69 ;; superclass and that may not have STRUCTURE-CLASS as the
70 ;; metaclass.) Syntax is:
71 ;; (superclass-name metaclass-name metaclass-constructor)
72 (alternate-metaclass nil :type list)
73 ;; a list of DEFSTRUCT-SLOT-DESCRIPTION objects for all slots
74 ;; (including included ones)
76 ;; number of elements we've allocated (See also RAW-LENGTH.)
77 (length 0 :type index)
78 ;; General kind of implementation.
79 (type 'structure :type (member structure vector list
80 funcallable-structure))
82 ;; The next three slots are for :TYPE'd structures (which aren't
83 ;; classes, CLASS-STRUCTURE-P = NIL)
85 ;; vector element type
87 ;; T if :NAMED was explicitly specified, NIL otherwise
88 (named nil :type boolean)
89 ;; any INITIAL-OFFSET option on this direct type
90 (offset nil :type (or index null))
92 ;; the argument to the PRINT-FUNCTION option, or NIL if a
93 ;; PRINT-FUNCTION option was given with no argument, or 0 if no
94 ;; PRINT-FUNCTION option was given
95 (print-function 0 :type (or cons symbol (member 0)))
96 ;; the argument to the PRINT-OBJECT option, or NIL if a PRINT-OBJECT
97 ;; option was given with no argument, or 0 if no PRINT-OBJECT option
99 (print-object 0 :type (or cons symbol (member 0)))
100 ;; the index of the raw data vector and the number of words in it.
101 ;; NIL and 0 if not allocated yet.
102 (raw-index nil :type (or index null))
103 (raw-length 0 :type index)
104 ;; the value of the :PURE option, or :UNSPECIFIED. This is only
105 ;; meaningful if CLASS-STRUCTURE-P = T.
106 (pure :unspecified :type (member t nil :substructure :unspecified)))
107 (def!method print-object ((x defstruct-description) stream)
108 (print-unreadable-object (x stream :type t)
109 (prin1 (dd-name x) stream)))
111 ;;; A DEFSTRUCT-SLOT-DESCRIPTION holds compile-time information about
112 ;;; a structure slot.
113 (def!struct (defstruct-slot-description
114 (:make-load-form-fun just-dump-it-normally)
117 #-sb-xc-host (:pure t))
118 ;; string name of slot
120 ;; its position in the implementation sequence
121 (index (required-argument) :type fixnum)
122 ;; Name of accessor, or NIL if this accessor has the same name as an
123 ;; inherited accessor (which we don't want to shadow.)
125 default ; default value expression
126 (type t) ; declared type specifier
127 ;; If this object does not describe a raw slot, this value is T.
129 ;; If this object describes a raw slot, this value is the type of the
130 ;; value that the raw slot holds. Mostly. (KLUDGE: If the raw slot has
131 ;; type (UNSIGNED-BYTE 32), the value here is UNSIGNED-BYTE, not
132 ;; (UNSIGNED-BYTE 32).)
133 (raw-type t :type (member t single-float double-float
134 #!+long-float long-float
135 complex-single-float complex-double-float
136 #!+long-float complex-long-float
138 (read-only nil :type (member t nil)))
139 (def!method print-object ((x defstruct-slot-description) stream)
140 (print-unreadable-object (x stream :type t)
141 (prin1 (dsd-name x) stream)))
143 ;;; Is DEFSTRUCT a structure with a class?
144 (defun class-structure-p (defstruct)
145 (member (dd-type defstruct) '(structure funcallable-structure)))
147 ;;; Return the name of a defstruct slot as a symbol. We store it as a
148 ;;; string to avoid creating lots of worthless symbols at load time.
149 (defun dsd-name (dsd)
150 (intern (string (dsd-%name dsd))
151 (if (dsd-accessor dsd)
152 (symbol-package (dsd-accessor dsd))
155 ;;;; typed (non-class) structures
157 ;;; Return a type specifier we can use for testing :TYPE'd structures.
158 (defun dd-lisp-type (defstruct)
159 (ecase (dd-type defstruct)
161 (vector `(simple-array ,(dd-element-type defstruct) (*)))))
163 ;;;; the legendary DEFSTRUCT macro itself (both CL:DEFSTRUCT and its
164 ;;;; close personal friend SB!XC:DEFSTRUCT)
166 ;;; Return a list of forms to install print and make-load-form funs, mentioning
167 ;;; them in the expansion so that they can be compiled.
168 (defun class-method-definitions (defstruct)
169 (let ((name (dd-name defstruct)))
171 ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant
172 ;; class names which creates fast but non-cold-loadable,
173 ;; non-compact code. In this context, we'd rather have
174 ;; compact, cold-loadable code. -- WHN 19990928
175 (declare (notinline sb!xc:find-class))
176 ,@(let ((pf (dd-print-function defstruct))
177 (po (dd-print-object defstruct))
180 ;; Giving empty :PRINT-OBJECT or :PRINT-FUNCTION options
181 ;; leaves PO or PF equal to NIL. The user-level effect is
182 ;; to generate a PRINT-OBJECT method specialized for the type,
183 ;; implementing the default #S structure-printing behavior.
184 (when (or (eq pf nil) (eq po nil))
185 (setf pf '(default-structure-print)
187 (flet (;; Given an arg from a :PRINT-OBJECT or :PRINT-FUNCTION
188 ;; option, return the value to pass as an arg to FUNCTION.
190 (destructuring-bind (function-name) oarg
192 (cond ((not (eql pf 0))
193 `((def!method print-object ((,x ,name) ,s)
194 (funcall #',(farg pf) ,x ,s *current-level*))))
196 `((def!method print-object ((,x ,name) ,s)
197 (funcall #',(farg po) ,x ,s))))
199 ,@(let ((pure (dd-pure defstruct)))
201 `((setf (layout-pure (class-layout
202 (sb!xc:find-class ',name)))
204 ((eq pure :substructure)
205 `((setf (layout-pure (class-layout
206 (sb!xc:find-class ',name)))
208 ,@(let ((def-con (dd-default-constructor defstruct)))
209 (when (and def-con (not (dd-alternate-metaclass defstruct)))
210 `((setf (structure-class-constructor (sb!xc:find-class ',name))
212 ;; FIXME: MAKE-LOAD-FORM is supposed to be handled here, too.
214 ;;; FIXME: I really would like to make structure accessors less special,
215 ;;; just ordinary inline functions. (Or perhaps inline functions with special
216 ;;; compact implementations of their expansions, to avoid bloating the system.)
218 ;;; shared logic for CL:DEFSTRUCT and SB!XC:DEFSTRUCT
220 ;;; FIXME: There should be some way to make this not be present in the
221 ;;; target executable, with EVAL-WHEN (COMPILE EXECUTE) and all that good
222 ;;; stuff, but for now I can't be bothered because of the messiness of
223 ;;; using CL:DEFMACRO in one case and SB!XC:DEFMACRO in another case.
224 ;;; Perhaps I could dodge this by defining it as an inline function instead?
225 ;;; Or perhaps just use MACROLET? I tried MACROLET and got nowhere and thought
226 ;;; I was tripping over either a compiler bug or ANSI weirdness, but this
227 ;;; test case seems to work in Debian CMU CL 2.4.9:
228 ;;; (macrolet ((emit-printer () ''(print "********")))
229 ;;; (defmacro fizz () (emit-printer)))
235 (defmacro expander-for-defstruct (name-and-options
237 expanding-into-code-for-xc-host-p)
238 `(let ((name-and-options ,name-and-options)
239 (slot-descriptions ,slot-descriptions)
240 (expanding-into-code-for-xc-host-p
241 ,expanding-into-code-for-xc-host-p))
242 (let* ((dd (parse-name-and-options-and-slot-descriptions
246 (if (class-structure-p dd)
247 (let ((inherits (inherits-for-structure dd)))
249 (eval-when (:compile-toplevel :load-toplevel :execute)
250 (%compiler-only-defstruct ',dd ',inherits))
251 (%defstruct ',dd ',inherits)
252 ,@(when (eq (dd-type dd) 'structure)
253 `((%compiler-defstruct ',dd)))
254 ,@(unless expanding-into-code-for-xc-host-p
255 (append (raw-accessor-definitions dd)
256 (predicate-definitions dd)
257 ;; FIXME: We've inherited from CMU CL nonparallel
258 ;; code for creating copiers for typed and untyped
259 ;; structures. This should be fixed.
260 ;(copier-definition dd)
261 (constructor-definitions dd)
262 (class-method-definitions dd)))
265 (eval-when (:compile-toplevel :load-toplevel :execute)
266 (setf (info :typed-structure :info ',name) ',dd))
267 ,@(unless expanding-into-code-for-xc-host-p
268 (append (typed-accessor-definitions dd)
269 (typed-predicate-definitions dd)
270 (typed-copier-definitions dd)
271 (constructor-definitions dd)))
274 (sb!xc:defmacro defstruct (name-and-options &rest slot-descriptions)
276 "DEFSTRUCT {Name | (Name Option*)} {Slot | (Slot [Default] {Key Value}*)}
277 Define the structure type Name. Instances are created by MAKE-<name>, which
278 takes keyword arguments allowing initial slot values to the specified.
279 A SETF'able function <name>-<slot> is defined for each slot to read and
280 write slot values. <name>-p is a type predicate.
282 Popular DEFSTRUCT options (see manual for others):
286 Specify the name for the constructor or predicate.
288 (:CONSTRUCTOR Name Lambda-List)
289 Specify the name and arguments for a BOA constructor
290 (which is more efficient when keyword syntax isn't necessary.)
292 (:INCLUDE Supertype Slot-Spec*)
293 Make this type a subtype of the structure type Supertype. The optional
294 Slot-Specs override inherited slot options.
299 Asserts that the value of this slot is always of the specified type.
302 If true, no setter function is defined for this slot."
303 (expander-for-defstruct name-and-options slot-descriptions nil))
305 (defmacro sb!xc:defstruct (name-and-options &rest slot-descriptions)
307 "Cause information about a target structure to be built into the
309 (expander-for-defstruct name-and-options slot-descriptions t))
311 ;;;; functions to create various parts of DEFSTRUCT definitions
313 ;;; Catch requests to mess up definitions in COMMON-LISP.
315 (eval-when (:compile-toplevel :load-toplevel :execute)
316 (defun protect-cl (symbol)
317 (when (and *cold-init-complete-p*
318 (eq (symbol-package symbol) *cl-package*))
319 (cerror "Go ahead and patch the system."
320 "attempting to modify a symbol in the COMMON-LISP package: ~S"
323 ;;; Return forms to define readers and writers for raw slots as inline
325 (defun raw-accessor-definitions (dd)
326 (let* ((name (dd-name dd)))
328 (dolist (slot (dd-slots dd))
329 (let ((stype (dsd-type slot))
330 (accname (dsd-accessor slot))
331 (argname (gensym "ARG"))
332 (nvname (gensym "NEW-VALUE-")))
333 (multiple-value-bind (accessor offset data)
334 (slot-accessor-form dd slot argname)
335 ;; When accessor exists and is raw
336 (when (and accname (not (eq accessor '%instance-ref)))
337 (res `(declaim (inline ,accname)))
338 (res `(declaim (ftype (function (,name) ,stype) ,accname)))
339 (res `(defun ,accname (,argname)
340 (truly-the ,stype (,accessor ,data ,offset))))
341 (unless (dsd-read-only slot)
342 (res `(declaim (inline (setf ,accname))))
343 (res `(declaim (ftype (function (,stype ,name) ,stype)
345 ;; FIXME: I rewrote this somewhat from the CMU CL definition.
346 ;; Do some basic tests to make sure that reading and writing
347 ;; raw slots still works correctly.
348 (res `(defun (setf ,accname) (,nvname ,argname)
349 (setf (,accessor ,data ,offset) ,nvname)
353 ;;; Return a list of forms which create a predicate for an untyped DEFSTRUCT.
354 (defun predicate-definitions (dd)
355 (let ((pred (dd-predicate dd))
358 (if (eq (dd-type dd) 'funcallable-structure)
359 ;; FIXME: Why does this need to be special-cased for
360 ;; FUNCALLABLE-STRUCTURE? CMU CL did it, but without explanation.
361 ;; Could we do without it? What breaks if we do? Or could we
362 ;; perhaps get by with no predicates for funcallable structures?
363 `((declaim (inline ,pred))
364 (defun ,pred (,argname) (typep ,argname ',(dd-name dd))))
365 `((protect-cl ',pred)
366 (declaim (inline ,pred))
367 (defun ,pred (,argname)
368 (declare (optimize (speed 3) (safety 0)))
369 (typep-to-layout ,argname
370 (compile-time-find-layout ,(dd-name dd)))))))))
372 ;;; Return a list of forms which create a predicate function for a typed
374 (defun typed-predicate-definitions (defstruct)
375 (let ((name (dd-name defstruct))
376 (pred (dd-predicate defstruct))
378 (when (and pred (dd-named defstruct))
379 (let ((ltype (dd-lisp-type defstruct)))
380 `((defun ,pred (,argname)
381 (and (typep ,argname ',ltype)
382 (eq (elt (the ,ltype ,argname)
383 ,(cdr (car (last (find-name-indices defstruct)))))
386 ;;; FIXME: We've inherited from CMU CL code to do typed structure copiers
387 ;;; in a completely different way than untyped structure copiers. Fix this.
388 ;;; (This function was my first attempt to fix this, but I stopped before
389 ;;; figuring out how to install it completely and remove the parallel
390 ;;; code which simply SETF's the FDEFINITION of the DD-COPIER name.
392 ;;; Return the copier definition for an untyped DEFSTRUCT.
393 (defun copier-definition (dd)
394 (when (and (dd-copier dd)
395 ;; FUNCALLABLE-STRUCTUREs don't need copiers, and this
396 ;; implementation wouldn't work for them anyway, since
397 ;; COPY-STRUCTURE returns a STRUCTURE-OBJECT and they're not.
398 (not (eq (dd-type info) 'funcallable-structure)))
399 (let ((argname (gensym)))
401 (protect-cl ',(dd-copier dd))
402 (defun ,(dd-copier dd) (,argname)
403 (declare (type ,(dd-name dd) ,argname))
404 (copy-structure ,argname))))))
407 ;;; Return a list of forms to create a copier function of a typed DEFSTRUCT.
408 (defun typed-copier-definitions (defstruct)
409 (when (dd-copier defstruct)
410 `((setf (fdefinition ',(dd-copier defstruct)) #'copy-seq)
411 (declaim (ftype function ,(dd-copier defstruct))))))
413 ;;; Return a list of function definitions for accessing and setting the
414 ;;; slots of a typed DEFSTRUCT. The functions are proclaimed to be inline,
415 ;;; and the types of their arguments and results are declared as well. We
416 ;;; count on the compiler to do clever things with ELT.
417 (defun typed-accessor-definitions (defstruct)
419 (let ((ltype (dd-lisp-type defstruct)))
420 (dolist (slot (dd-slots defstruct))
421 (let ((name (dsd-accessor slot))
422 (index (dsd-index slot))
423 (slot-type `(and ,(dsd-type slot)
424 ,(dd-element-type defstruct))))
425 (stuff `(proclaim '(inline ,name (setf ,name))))
426 ;; FIXME: The arguments in the next two DEFUNs should be
427 ;; gensyms. (Otherwise e.g. if NEW-VALUE happened to be the
428 ;; name of a special variable, things could get weird.)
429 (stuff `(defun ,name (structure)
430 (declare (type ,ltype structure))
431 (the ,slot-type (elt structure ,index))))
432 (unless (dsd-read-only slot)
434 `(defun (setf ,name) (new-value structure)
435 (declare (type ,ltype structure) (type ,slot-type new-value))
436 (setf (elt structure ,index) new-value)))))))
441 (defun require-no-print-options-so-far (defstruct)
442 (unless (and (eql (dd-print-function defstruct) 0)
443 (eql (dd-print-object defstruct) 0))
444 (error "no more than one of the following options may be specified:
445 :PRINT-FUNCTION, :PRINT-OBJECT, :TYPE")))
447 ;;; Parse a single defstruct option and store the results in DEFSTRUCT.
448 (defun parse-1-option (option defstruct)
449 (let ((args (rest option))
450 (name (dd-name defstruct)))
453 (destructuring-bind (conc-name) args
454 (setf (dd-conc-name defstruct)
455 (if (symbolp conc-name)
457 (make-symbol (string conc-name))))))
459 (destructuring-bind (&optional (cname (concat-pnames 'make- name))
462 (push (cons cname stuff) (dd-constructors defstruct))))
464 (destructuring-bind (&optional (copier (concat-pnames 'copy- name)))
466 (setf (dd-copier defstruct) copier)))
468 (destructuring-bind (&optional (pred (concat-pnames name '-p))) args
469 (setf (dd-predicate defstruct) pred)))
471 (when (dd-include defstruct)
472 (error "more than one :INCLUDE option"))
473 (setf (dd-include defstruct) args))
474 (:alternate-metaclass
475 (setf (dd-alternate-metaclass defstruct) args))
477 (require-no-print-options-so-far defstruct)
478 (setf (dd-print-function defstruct)
479 (the (or symbol cons) args)))
481 (require-no-print-options-so-far defstruct)
482 (setf (dd-print-object defstruct)
483 (the (or symbol cons) args)))
485 (destructuring-bind (type) args
486 (cond ((eq type 'funcallable-structure)
487 (setf (dd-type defstruct) type))
488 ((member type '(list vector))
489 (setf (dd-element-type defstruct) 't)
490 (setf (dd-type defstruct) type))
491 ((and (consp type) (eq (first type) 'vector))
492 (destructuring-bind (vector vtype) type
493 (declare (ignore vector))
494 (setf (dd-element-type defstruct) vtype)
495 (setf (dd-type defstruct) 'vector)))
497 (error "~S is a bad :TYPE for Defstruct." type)))))
499 (error "The DEFSTRUCT option :NAMED takes no arguments."))
501 (destructuring-bind (offset) args
502 (setf (dd-offset defstruct) offset)))
504 (destructuring-bind (fun) args
505 (setf (dd-pure defstruct) fun)))
506 (t (error "unknown DEFSTRUCT option:~% ~S" option)))))
508 ;;; Given name and options, return a DD holding that info.
509 (eval-when (:compile-toplevel :load-toplevel :execute)
510 (defun parse-name-and-options (name-and-options)
511 (destructuring-bind (name &rest options) name-and-options
512 (let ((defstruct (make-defstruct-description name)))
513 (dolist (option options)
514 (cond ((consp option)
515 (parse-1-option option defstruct))
517 (setf (dd-named defstruct) t))
518 ((member option '(:constructor :copier :predicate :named))
519 (parse-1-option (list option) defstruct))
521 (error "unrecognized DEFSTRUCT option: ~S" option))))
523 (case (dd-type defstruct)
525 (when (dd-offset defstruct)
526 (error ":OFFSET can't be specified unless :TYPE is specified."))
527 (unless (dd-include defstruct)
528 (incf (dd-length defstruct))))
529 (funcallable-structure)
531 (require-no-print-options-so-far defstruct)
532 (when (dd-named defstruct)
533 (incf (dd-length defstruct)))
534 (let ((offset (dd-offset defstruct)))
535 (when offset (incf (dd-length defstruct) offset)))))
537 (when (dd-include defstruct)
538 (do-inclusion-stuff defstruct))
542 ;;; Given name and options and slot descriptions (and possibly doc
543 ;;; string at the head of slot descriptions) return a DD holding that
545 (defun parse-name-and-options-and-slot-descriptions (name-and-options
547 (/noshow "PARSE-NAME-AND-OPTIONS-AND-SLOT-DESCRIPTIONS" name-and-options)
548 (let ((result (parse-name-and-options (if (atom name-and-options)
549 (list name-and-options)
551 (when (stringp (car slot-descriptions))
552 (setf (dd-doc result) (pop slot-descriptions)))
553 (dolist (slot slot-descriptions)
554 (allocate-1-slot result (parse-1-dsd result slot)))
559 ;;;; stuff to parse slot descriptions
561 ;;; Parse a slot description for DEFSTRUCT, add it to the description
562 ;;; and return it. If supplied, ISLOT is a pre-initialized DSD that we
563 ;;; modify to get the new slot. This is supplied when handling
564 ;;; included slots. If the new accessor name is already an accessor
565 ;;; for same slot in some included structure, then set the
566 ;;; DSD-ACCESSOR to NIL so that we don't clobber the more general
568 (defun parse-1-dsd (defstruct spec &optional
569 (islot (make-defstruct-slot-description :%name ""
572 (multiple-value-bind (name default default-p type type-p read-only ro-p)
577 &optional (default nil default-p)
578 &key (type nil type-p) (read-only nil ro-p))
582 (uncross type) type-p
585 (when (keywordp spec)
586 ;; FIXME: should be style warning
587 (warn "Keyword slot name indicates probable syntax ~
588 error in DEFSTRUCT -- ~S."
592 (when (find name (dd-slots defstruct) :test #'string= :key #'dsd-%name)
593 (error 'program-error
594 :format-control "duplicate slot name ~S"
595 :format-arguments (list name)))
596 (setf (dsd-%name islot) (string name))
597 (setf (dd-slots defstruct) (nconc (dd-slots defstruct) (list islot)))
599 (let* ((accname (concat-pnames (dd-conc-name defstruct) name))
600 (existing (info :function :accessor-for accname)))
601 (if (and (structure-class-p existing)
602 (not (eq (sb!xc:class-name existing) (dd-name defstruct)))
603 (string= (dsd-%name (find accname
606 (class-layout existing)))
607 :key #'dsd-accessor))
609 (setf (dsd-accessor islot) nil)
610 (setf (dsd-accessor islot) accname)))
613 (setf (dsd-default islot) default))
615 (setf (dsd-type islot)
616 (if (eq (dsd-type islot) 't)
618 `(and ,(dsd-type islot) ,type))))
621 (setf (dsd-read-only islot) t)
622 (when (dsd-read-only islot)
623 (error "Slot ~S is :READ-ONLY in parent and must be :READ-ONLY in subtype ~S."
628 ;;; When a value of type TYPE is stored in a structure, should it be
629 ;;; stored in a raw slot? Return (VALUES RAW? RAW-TYPE WORDS), where
630 ;;; RAW? is true if TYPE should be stored in a raw slot.
631 ;;; RAW-TYPE is the raw slot type, or NIL if no raw slot.
632 ;;; WORDS is the number of words in the raw slot, or NIL if no raw slot.
633 (defun structure-raw-slot-type-and-size (type)
634 (/noshow "in STRUCTURE-RAW-SLOT-TYPE-AND-SIZE" type (sb!xc:subtypep type 'fixnum))
636 (;; FIXME: For now we suppress raw slots, since there are various
637 ;; issues about the way that the cross-compiler handles them.
638 (not (boundp '*dummy-placeholder-to-stop-compiler-warnings*))
639 (values nil nil nil))
640 ((and (sb!xc:subtypep type '(unsigned-byte 32))
641 (multiple-value-bind (fixnum? fixnum-certain?)
642 (sb!xc:subtypep type 'fixnum)
643 (/noshow fixnum? fixnum-certain?)
644 ;; (The extra test for FIXNUM-CERTAIN? here is
645 ;; intended for bootstrapping the system. In
646 ;; particular, in sbcl-0.6.2, we set up LAYOUT before
647 ;; FIXNUM is defined, and so could bogusly end up
648 ;; putting INDEX-typed values into raw slots if we
649 ;; didn't test FIXNUM-CERTAIN?.)
650 (and (not fixnum?) fixnum-certain?)))
651 (values t 'unsigned-byte 1))
652 ((sb!xc:subtypep type 'single-float)
653 (values t 'single-float 1))
654 ((sb!xc:subtypep type 'double-float)
655 (values t 'double-float 2))
657 ((sb!xc:subtypep type 'long-float)
658 (values t 'long-float #!+x86 3 #!+sparc 4))
659 ((sb!xc:subtypep type '(complex single-float))
660 (values t 'complex-single-float 2))
661 ((sb!xc:subtypep type '(complex double-float))
662 (values t 'complex-double-float 4))
664 ((sb!xc:subtypep type '(complex long-float))
665 (values t 'complex-long-float #!+x86 6 #!+sparc 8))
667 (values nil nil nil))))
669 ;;; Allocate storage for a DSD in DEFSTRUCT. This is where we decide
670 ;;; whether a slot is raw or not. If raw, and we haven't allocated a
671 ;;; raw-index yet for the raw data vector, then do it. Raw objects are
672 ;;; aligned on the unit of their size.
673 (defun allocate-1-slot (defstruct dsd)
674 (multiple-value-bind (raw? raw-type words)
675 (if (eq (dd-type defstruct) 'structure)
676 (structure-raw-slot-type-and-size (dsd-type dsd))
677 (values nil nil nil))
678 (/noshow "ALLOCATE-1-SLOT" dsd raw? raw-type words)
680 (setf (dsd-index dsd) (dd-length defstruct))
681 (incf (dd-length defstruct)))
683 (unless (dd-raw-index defstruct)
684 (setf (dd-raw-index defstruct) (dd-length defstruct))
685 (incf (dd-length defstruct)))
686 (let ((off (rem (dd-raw-length defstruct) words)))
688 (incf (dd-raw-length defstruct) (- words off))))
689 (setf (dsd-raw-type dsd) raw-type)
690 (setf (dsd-index dsd) (dd-raw-length defstruct))
691 (incf (dd-raw-length defstruct) words))))
694 (defun typed-structure-info-or-lose (name)
695 (or (info :typed-structure :info name)
696 (error ":TYPE'd DEFSTRUCT ~S not found for inclusion." name)))
698 ;;; Process any included slots pretty much like they were specified.
699 ;;; Also inherit various other attributes.
700 (defun do-inclusion-stuff (defstruct)
702 (included-name &rest modified-slots)
703 (dd-include defstruct)
704 (let* ((type (dd-type defstruct))
706 (if (class-structure-p defstruct)
707 (layout-info (compiler-layout-or-lose included-name))
708 (typed-structure-info-or-lose included-name))))
709 (unless (and (eq type (dd-type included-structure))
710 (type= (specifier-type (dd-element-type included-structure))
711 (specifier-type (dd-element-type defstruct))))
712 (error ":TYPE option mismatch between structures ~S and ~S."
713 (dd-name defstruct) included-name))
715 (incf (dd-length defstruct) (dd-length included-structure))
716 (when (class-structure-p defstruct)
717 (let ((mc (rest (dd-alternate-metaclass included-structure))))
718 (when (and mc (not (dd-alternate-metaclass defstruct)))
719 (setf (dd-alternate-metaclass defstruct)
720 (cons included-name mc))))
721 (when (eq (dd-pure defstruct) :unspecified)
722 (setf (dd-pure defstruct) (dd-pure included-structure)))
723 (setf (dd-raw-index defstruct) (dd-raw-index included-structure))
724 (setf (dd-raw-length defstruct) (dd-raw-length included-structure)))
726 (dolist (islot (dd-slots included-structure))
727 (let* ((iname (dsd-name islot))
728 (modified (or (find iname modified-slots
729 :key #'(lambda (x) (if (atom x) x (car x)))
732 (parse-1-dsd defstruct modified (copy-structure islot)))))))
734 ;;; This function is called at macroexpand time to compute the INHERITS
735 ;;; vector for a structure type definition.
736 (defun inherits-for-structure (info)
737 (declare (type defstruct-description info))
738 (let* ((include (dd-include info))
739 (superclass-opt (dd-alternate-metaclass info))
742 (compiler-layout-or-lose (first include))
743 (class-layout (sb!xc:find-class
744 (or (first superclass-opt)
745 'structure-object))))))
746 (if (eq (dd-name info) 'lisp-stream)
747 ;; a hack to added the stream class as a mixin for LISP-STREAMs
748 (concatenate 'simple-vector
749 (layout-inherits super)
751 (class-layout (sb!xc:find-class 'stream))))
752 (concatenate 'simple-vector
753 (layout-inherits super)
756 ;;; Do miscellaneous (LOAD EVAL) time actions for the structure
757 ;;; described by INFO. Create the class & layout, checking for
758 ;;; incompatible redefinition. Define setters, accessors, copier,
759 ;;; predicate, documentation, instantiate definition in load-time env.
760 ;;; This is only called for default structures.
761 (defun %defstruct (info inherits)
762 (declare (type defstruct-description info))
763 (multiple-value-bind (class layout old-layout)
764 (ensure-structure-class info inherits "current" "new")
765 (cond ((not old-layout)
766 (unless (eq (class-layout class) layout)
767 (register-layout layout)))
769 (let ((old-info (layout-info old-layout)))
770 (when (defstruct-description-p old-info)
771 (dolist (slot (dd-slots old-info))
772 (fmakunbound (dsd-accessor slot))
773 (unless (dsd-read-only slot)
774 (fmakunbound `(setf ,(dsd-accessor slot)))))))
775 (%redefine-defstruct class old-layout layout)
776 (setq layout (class-layout class))))
778 (setf (sb!xc:find-class (dd-name info)) class)
780 ;; Set FDEFINITIONs for structure accessors, setters, predicates,
783 (unless (eq (dd-type info) 'funcallable-structure)
785 (dolist (slot (dd-slots info))
787 (when (and (dsd-accessor slot)
788 (eq (dsd-raw-type slot) t))
789 (protect-cl (dsd-accessor slot))
790 (setf (symbol-function (dsd-accessor slot))
791 (structure-slot-getter layout dsd))
792 (unless (dsd-read-only slot)
793 (setf (fdefinition `(setf ,(dsd-accessor slot)))
794 (structure-slot-setter layout dsd))))))
796 ;; FIXME: See comment on corresponding code in %%COMPILER-DEFSTRUCT.
798 (when (dd-predicate info)
799 (protect-cl (dd-predicate info))
800 (setf (symbol-function (dd-predicate info))
802 (declare (optimize (speed 3) (safety 0)))
803 (typep-to-layout object layout))))
806 (when (dd-copier info)
807 (protect-cl (dd-copier info))
808 (setf (symbol-function (dd-copier info))
809 #'(lambda (structure)
810 (declare (optimize (speed 3) (safety 0)))
811 (flet ((layout-test (structure)
812 (typep-to-layout structure layout)))
813 (unless (layout-test structure)
814 (error 'simple-type-error
816 :expected-type '(satisfies layout-test)
818 "Structure for copier is not a ~S:~% ~S"
820 (list (sb!xc:class-name (layout-class layout))
822 (copy-structure structure))))))
825 (setf (fdocumentation (dd-name info) 'type) (dd-doc info)))
829 ;;; This function is called at compile-time to do the
830 ;;; compile-time-only actions for defining a structure type. It
831 ;;; installs the class in the type system in a similar way to
832 ;;; %DEFSTRUCT, but is quieter and safer in the case of redefinition.
834 ;;; The comments for the classic CMU CL version of this function said
835 ;;; that EVAL-WHEN doesn't do the right thing when nested or
836 ;;; non-top-level, and so CMU CL had the function magically called by
837 ;;; the compiler. Unfortunately, this doesn't do the right thing
838 ;;; either: compiling a function (DEFUN FOO () (DEFSTRUCT FOO X Y))
839 ;;; causes the class FOO to become defined, even though FOO is never
840 ;;; loaded or executed. Even more unfortunately, I've been unable to
841 ;;; come up with any EVAL-WHEN tricks which work -- I finally gave up
842 ;;; on this approach when trying to get the system to cross-compile
843 ;;; error.lisp. (Just because I haven't found it doesn't mean that it
844 ;;; doesn't exist, of course. Alas, I continue to have some trouble
845 ;;; understanding compile/load semantics in Common Lisp.) So we
846 ;;; continue to use the IR1 transformation approach, even though it's
847 ;;; known to be buggy. -- WHN 19990507
849 ;;; Basically, this function avoids trashing the compiler by only
850 ;;; actually defining the class if there is no current definition.
851 ;;; Instead, we just set the INFO TYPE COMPILER-LAYOUT. This behavior
852 ;;; is left over from classic CMU CL and may not be necessary in the
853 ;;; new build system. -- WHN 19990507
855 ;;; FUNCTION-%COMPILER-ONLY-DEFSTRUCT is an ordinary function, called
856 ;;; by both the IR1 transform version of %COMPILER-ONLY-DEFSTRUCT and
857 ;;; by the ordinary function version of %COMPILER-ONLY-DEFSTRUCT. (The
858 ;;; ordinary function version is there for the interpreter and for
860 (defun %compiler-only-defstruct (info inherits)
861 (function-%compiler-only-defstruct info inherits))
862 (defun function-%compiler-only-defstruct (info inherits)
863 (multiple-value-bind (class layout old-layout)
864 (multiple-value-bind (clayout clayout-p)
865 (info :type :compiler-layout (dd-name info))
866 (ensure-structure-class info
868 (if clayout-p "previously compiled" "current")
870 :compiler-layout clayout))
872 (undefine-structure (layout-class old-layout))
873 (when (and (class-subclasses class)
874 (not (eq layout old-layout)))
876 (dohash (class layout (class-subclasses class))
877 (declare (ignore layout))
878 (undefine-structure class)
879 (subs (class-proper-name class)))
881 (warn "Removing old subclasses of ~S:~% ~S"
882 (sb!xc:class-name class)
885 (unless (eq (class-layout class) layout)
886 (register-layout layout :invalidate nil))
887 (setf (sb!xc:find-class (dd-name info)) class)))
889 (setf (info :type :compiler-layout (dd-name info)) layout))
892 ;;; This function does the (COMPILE LOAD EVAL) time actions for updating the
893 ;;; compiler's global meta-information to represent the definition of the
894 ;;; structure described by Info. This primarily amounts to setting up info
895 ;;; about the accessor and other implicitly defined functions. The constructors
896 ;;; are explicitly defined by top-level code.
897 (defun %%compiler-defstruct (info)
898 (declare (type defstruct-description info))
899 (let* ((name (dd-name info))
900 (class (sb!xc:find-class name)))
901 (let ((copier (dd-copier info)))
903 (proclaim `(ftype (function (,name) ,name) ,copier))))
905 ;; FIXME: This (and corresponding code in %DEFSTRUCT) are the way
906 ;; that CMU CL defined the predicate, instead of using DEFUN.
907 ;; Perhaps it would be better to go back to to the CMU CL way, or
908 ;; something similar. I want to reduce the amount of magic in
909 ;; defstruct functions, but making the predicate be a closure
910 ;; looks like a good thing, and can even be done without magic.
911 ;; (OTOH, there are some bootstrapping issues involved, since
912 ;; GENESIS understands DEFUN but doesn't understand a
913 ;; (SETF SYMBOL-FUNCTION) call inside %DEFSTRUCT.)
915 (let ((pred (dd-predicate info)))
917 (proclaim-as-defstruct-function-name pred)
918 (setf (info :function :inlinep pred) :inline)
919 (setf (info :function :inline-expansion pred)
920 `(lambda (x) (typep x ',name)))))
923 (dolist (slot (dd-slots info))
924 (let* ((fun (dsd-accessor slot))
925 (setf-fun `(setf ,fun)))
926 (when (and fun (eq (dsd-raw-type slot) t))
927 (proclaim-as-defstruct-function-name fun)
928 (setf (info :function :accessor-for fun) class)
929 (unless (dsd-read-only slot)
930 (proclaim-as-defstruct-function-name setf-fun)
931 (setf (info :function :accessor-for setf-fun) class))))))
935 ;;; Ordinarily this is preempted by an IR1 transformation, but this
936 ;;; definition is still useful for the interpreter and code walkers.
937 (defun %compiler-defstruct (info)
938 (%%compiler-defstruct info))
940 ;;;; redefinition stuff
942 ;;; Compare the slots of OLD and NEW, returning 3 lists of slot names:
943 ;;; 1. Slots which have moved,
944 ;;; 2. Slots whose type has changed,
945 ;;; 3. Deleted slots.
946 (defun compare-slots (old new)
947 (let* ((oslots (dd-slots old))
948 (nslots (dd-slots new))
949 (onames (mapcar #'dsd-name oslots))
950 (nnames (mapcar #'dsd-name nslots)))
953 (dolist (name (intersection onames nnames))
954 (let ((os (find name oslots :key #'dsd-name))
955 (ns (find name nslots :key #'dsd-name)))
956 (unless (subtypep (dsd-type ns) (dsd-type os))
957 (/noshow "found retyped slots" ns os (dsd-type ns) (dsd-type os))
959 (unless (and (= (dsd-index os) (dsd-index ns))
960 (eq (dsd-raw-type os) (dsd-raw-type ns)))
964 (set-difference onames nnames)))))
966 ;;; If we are redefining a structure with different slots than in the
967 ;;; currently loaded version, give a warning and return true.
968 (defun redefine-structure-warning (class old new)
969 (declare (type defstruct-description old new)
970 (type sb!xc:class class)
972 (let ((name (dd-name new)))
973 (multiple-value-bind (moved retyped deleted) (compare-slots old new)
974 (when (or moved retyped deleted)
976 "incompatibly redefining slots of structure class ~S~@
977 Make sure any uses of affected accessors are recompiled:~@
978 ~@[ These slots were moved to new positions:~% ~S~%~]~
979 ~@[ These slots have new incompatible types:~% ~S~%~]~
980 ~@[ These slots were deleted:~% ~S~%~]"
981 name moved retyped deleted)
984 ;;; This function is called when we are incompatibly redefining a
985 ;;; structure Class to have the specified New-Layout. We signal an
986 ;;; error with some proceed options and return the layout that should
988 (defun %redefine-defstruct (class old-layout new-layout)
989 (declare (type sb!xc:class class) (type layout old-layout new-layout))
990 (let ((name (class-proper-name class)))
992 (error "redefining class ~S incompatibly with the current definition"
995 :report "Invalidate current definition."
996 (warn "Previously loaded ~S accessors will no longer work." name)
997 (register-layout new-layout))
999 :report "Smash current layout, preserving old code."
1000 (warn "Any old ~S instances will be in a bad way.~@
1001 I hope you know what you're doing..."
1003 (register-layout new-layout :invalidate nil
1004 :destruct-layout old-layout))))
1007 ;;; This is called when we are about to define a structure class. It
1008 ;;; returns a (possibly new) class object and the layout which should
1009 ;;; be used for the new definition (may be the current layout, and
1010 ;;; also might be an uninstalled forward referenced layout.) The third
1011 ;;; value is true if this is an incompatible redefinition, in which
1012 ;;; case it is the old layout.
1013 (defun ensure-structure-class (info inherits old-context new-context
1014 &key compiler-layout)
1015 (multiple-value-bind (class old-layout)
1019 (class 'sb!xc:structure-class)
1020 (constructor 'make-structure-class))
1021 (dd-alternate-metaclass info)
1022 (declare (ignore name))
1023 (insured-find-class (dd-name info)
1024 (if (eq class 'sb!xc:structure-class)
1026 (typep x 'sb!xc:structure-class))
1028 (sb!xc:typep x (sb!xc:find-class class))))
1029 (fdefinition constructor)))
1030 (setf (class-direct-superclasses class)
1031 (if (eq (dd-name info) 'lisp-stream)
1032 ;; a hack to add STREAM as a superclass mixin to LISP-STREAMs
1033 (list (layout-class (svref inherits (1- (length inherits))))
1034 (layout-class (svref inherits (- (length inherits) 2))))
1035 (list (layout-class (svref inherits (1- (length inherits)))))))
1036 (let ((new-layout (make-layout :class class
1038 :depthoid (length inherits)
1039 :length (dd-length info)
1041 (old-layout (or compiler-layout old-layout)))
1044 (values class new-layout nil))
1045 (;; This clause corresponds to an assertion in REDEFINE-LAYOUT-WARNING
1046 ;; of classic CMU CL. I moved it out to here because it was only
1047 ;; exercised in this code path anyway. -- WHN 19990510
1048 (not (eq (layout-class new-layout) (layout-class old-layout)))
1049 (error "shouldn't happen: weird state of OLD-LAYOUT?"))
1050 ((not *type-system-initialized*)
1051 (setf (layout-info old-layout) info)
1052 (values class old-layout nil))
1053 ((redefine-layout-warning old-context
1056 (layout-length new-layout)
1057 (layout-inherits new-layout)
1058 (layout-depthoid new-layout))
1059 (values class new-layout old-layout))
1061 (let ((old-info (layout-info old-layout)))
1063 ((or defstruct-description)
1064 (cond ((redefine-structure-warning class old-info info)
1065 (values class new-layout old-layout))
1067 (setf (layout-info old-layout) info)
1068 (values class old-layout nil))))
1070 (setf (layout-info old-layout) info)
1071 (values class old-layout nil))
1073 (error "shouldn't happen! strange thing in LAYOUT-INFO:~% ~S"
1075 (values class new-layout old-layout)))))))))
1077 ;;; Blow away all the compiler info for the structure CLASS. Iterate
1078 ;;; over this type, clearing the compiler structure type info, and
1079 ;;; undefining all the associated functions.
1080 (defun undefine-structure (class)
1081 (let ((info (layout-info (class-layout class))))
1082 (when (defstruct-description-p info)
1083 (let ((type (dd-name info)))
1084 (setf (info :type :compiler-layout type) nil)
1085 (undefine-function-name (dd-copier info))
1086 (undefine-function-name (dd-predicate info))
1087 (dolist (slot (dd-slots info))
1088 (let ((fun (dsd-accessor slot)))
1089 (undefine-function-name fun)
1090 (unless (dsd-read-only slot)
1091 (undefine-function-name `(setf ,fun))))))
1092 ;; Clear out the SPECIFIER-TYPE cache so that subsequent
1093 ;; references are unknown types.
1094 (values-specifier-type-cache-clear)))
1097 ;;; Return a list of pairs (name . index). Used for :TYPE'd
1098 ;;; constructors to find all the names that we have to splice in &
1099 ;;; where. Note that these types don't have a layout, so we can't look
1100 ;;; at LAYOUT-INHERITS.
1101 (defun find-name-indices (defstruct)
1104 (do ((info defstruct
1105 (typed-structure-info-or-lose (first (dd-include info)))))
1106 ((not (dd-include info))
1111 (dolist (info infos)
1112 (incf i (or (dd-offset info) 0))
1113 (when (dd-named info)
1114 (res (cons (dd-name info) i)))
1115 (setq i (dd-length info)))))
1119 ;;;; slot accessors for raw slots
1121 ;;; Return info about how to read/write a slot in the value stored in
1122 ;;; OBJECT. This is also used by constructors (we can't use the
1123 ;;; accessor function, since some slots are read-only.) If supplied,
1124 ;;; DATA is a variable holding the raw-data vector.
1126 ;;; returned values:
1127 ;;; 1. accessor function name (SETFable)
1128 ;;; 2. index to pass to accessor.
1129 ;;; 3. object form to pass to accessor
1130 (defun slot-accessor-form (defstruct slot object &optional data)
1131 (let ((rtype (dsd-raw-type slot)))
1134 (single-float '%raw-ref-single)
1135 (double-float '%raw-ref-double)
1137 (long-float '%raw-ref-long)
1138 (complex-single-float '%raw-ref-complex-single)
1139 (complex-double-float '%raw-ref-complex-double)
1141 (complex-long-float '%raw-ref-complex-long)
1142 (unsigned-byte 'aref)
1144 (if (eq (dd-type defstruct) 'funcallable-structure)
1145 '%funcallable-instance-info
1150 (truncate (dsd-index slot) #!+x86 6 #!+sparc 8))
1153 (truncate (dsd-index slot) #!+x86 3 #!+sparc 4))
1155 (ash (dsd-index slot) -1))
1156 (complex-double-float
1157 (ash (dsd-index slot) -2))
1158 (complex-single-float
1159 (ash (dsd-index slot) -1))
1163 ((eq rtype 't) object)
1166 `(truly-the (simple-array (unsigned-byte 32) (*))
1167 (%instance-ref ,object ,(dd-raw-index defstruct))))))))
1169 ;;; These functions are called to actually make a constructor after we
1170 ;;; have processed the arglist. The correct variant (according to the
1171 ;;; DD-TYPE) should be called. The function is defined with the
1172 ;;; specified name and arglist. Vars and Types are used for argument
1173 ;;; type declarations. Values are the values for the slots (in order.)
1175 ;;; This is split four ways because:
1176 ;;; 1] list & vector structures need "name" symbols stuck in at
1177 ;;; various weird places, whereas STRUCTURE structures have
1179 ;;; 2] We really want to use LIST to make list structures, instead of
1180 ;;; MAKE-LIST/(SETF ELT).
1181 ;;; 3] STRUCTURE structures can have raw slots that must also be
1182 ;;; allocated and indirectly referenced. We use SLOT-ACCESSOR-FORM
1183 ;;; to compute how to set the slots, which deals with raw slots.
1184 ;;; 4] Funcallable structures are weird.
1185 (defun create-vector-constructor
1186 (defstruct cons-name arglist vars types values)
1187 (let ((temp (gensym))
1188 (etype (dd-element-type defstruct)))
1189 `(defun ,cons-name ,arglist
1190 (declare ,@(mapcar #'(lambda (var type) `(type (and ,type ,etype) ,var))
1192 (let ((,temp (make-array ,(dd-length defstruct)
1193 :element-type ',(dd-element-type defstruct))))
1194 ,@(mapcar #'(lambda (x)
1195 `(setf (aref ,temp ,(cdr x)) ',(car x)))
1196 (find-name-indices defstruct))
1197 ,@(mapcar #'(lambda (dsd value)
1198 `(setf (aref ,temp ,(dsd-index dsd)) ,value))
1199 (dd-slots defstruct) values)
1201 (defun create-list-constructor
1202 (defstruct cons-name arglist vars types values)
1203 (let ((vals (make-list (dd-length defstruct) :initial-element nil)))
1204 (dolist (x (find-name-indices defstruct))
1205 (setf (elt vals (cdr x)) `',(car x)))
1206 (loop for dsd in (dd-slots defstruct) and val in values do
1207 (setf (elt vals (dsd-index dsd)) val))
1209 `(defun ,cons-name ,arglist
1210 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1213 (defun create-structure-constructor
1214 (defstruct cons-name arglist vars types values)
1215 (let* ((temp (gensym))
1216 (raw-index (dd-raw-index defstruct))
1217 (n-raw-data (when raw-index (gensym))))
1218 `(defun ,cons-name ,arglist
1219 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1221 (let ((,temp (truly-the ,(dd-name defstruct)
1222 (%make-instance ,(dd-length defstruct))))
1225 (make-array ,(dd-raw-length defstruct)
1226 :element-type '(unsigned-byte 32))))))
1227 (setf (%instance-layout ,temp)
1228 (%delayed-get-compiler-layout ,(dd-name defstruct)))
1230 `((setf (%instance-ref ,temp ,raw-index) ,n-raw-data)))
1231 ,@(mapcar #'(lambda (dsd value)
1232 (multiple-value-bind (accessor index data)
1233 (slot-accessor-form defstruct dsd temp n-raw-data)
1234 `(setf (,accessor ,data ,index) ,value)))
1235 (dd-slots defstruct)
1238 (defun create-fin-constructor
1239 (defstruct cons-name arglist vars types values)
1240 (let ((temp (gensym)))
1241 `(defun ,cons-name ,arglist
1242 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1244 (let ((,temp (truly-the
1245 ,(dd-name defstruct)
1246 (%make-funcallable-instance
1247 ,(dd-length defstruct)
1248 (%delayed-get-compiler-layout ,(dd-name defstruct))))))
1249 ,@(mapcar #'(lambda (dsd value)
1250 `(setf (%funcallable-instance-info
1251 ,temp ,(dsd-index dsd))
1253 (dd-slots defstruct) values)
1256 ;;; Create a default (non-BOA) keyword constructor.
1257 (defun create-keyword-constructor (defstruct creator)
1258 (collect ((arglist (list '&key))
1261 (dolist (slot (dd-slots defstruct))
1262 (let ((dum (gensym))
1263 (name (dsd-name slot)))
1264 (arglist `((,(intern (string name) "KEYWORD") ,dum)
1265 ,(dsd-default slot)))
1266 (types (dsd-type slot))
1269 defstruct (dd-default-constructor defstruct)
1270 (arglist) (vals) (types) (vals))))
1272 ;;; Given a structure and a BOA constructor spec, call Creator with
1273 ;;; the appropriate args to make a constructor.
1274 (defun create-boa-constructor (defstruct boa creator)
1275 (multiple-value-bind (req opt restp rest keyp keys allowp aux)
1276 (sb!kernel:parse-lambda-list (second boa))
1280 (labels ((get-slot (name)
1281 (let ((res (find name (dd-slots defstruct)
1285 (values (dsd-type res) (dsd-default res))
1288 (multiple-value-bind (type default) (get-slot arg)
1289 (arglist `(,arg ,default))
1295 (types (get-slot arg)))
1298 (arglist '&optional)
1302 (name &optional (def (nth-value 1 (get-slot name))))
1304 (arglist `(,name ,def))
1306 (types (get-slot name))))
1308 (do-default arg)))))
1311 (arglist '&rest rest)
1319 (destructuring-bind (wot &optional (def nil def-p)) key
1320 (let ((name (if (consp wot)
1321 (destructuring-bind (key var) wot
1322 (declare (ignore key))
1325 (multiple-value-bind (type slot-def) (get-slot name)
1326 (arglist `(,wot ,(if def-p def slot-def)))
1331 (when allowp (arglist '&allow-other-keys))
1336 (let* ((arg (if (consp arg) arg (list arg)))
1340 (types (get-slot var))))))
1342 (funcall creator defstruct (first boa)
1343 (arglist) (vars) (types)
1344 (mapcar #'(lambda (slot)
1345 (or (find (dsd-name slot) (vars) :test #'string=)
1346 (dsd-default slot)))
1347 (dd-slots defstruct))))))
1349 ;;; Grovel the constructor options, and decide what constructors (if
1351 (defun constructor-definitions (defstruct)
1352 (let ((no-constructors nil)
1355 (creator (ecase (dd-type defstruct)
1356 (structure #'create-structure-constructor)
1357 (funcallable-structure #'create-fin-constructor)
1358 (vector #'create-vector-constructor)
1359 (list #'create-list-constructor))))
1360 (dolist (constructor (dd-constructors defstruct))
1361 (destructuring-bind (name &optional (boa-ll nil boa-p)) constructor
1362 (declare (ignore boa-ll))
1363 (cond ((not name) (setq no-constructors t))
1364 (boa-p (push constructor boas))
1365 (t (push name defaults)))))
1367 (when no-constructors
1368 (when (or defaults boas)
1369 (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs"))
1370 (return-from constructor-definitions ()))
1372 (unless (or defaults boas)
1373 (push (concat-pnames 'make- (dd-name defstruct)) defaults))
1377 (let ((cname (first defaults)))
1378 (setf (dd-default-constructor defstruct) cname)
1379 (res (create-keyword-constructor defstruct creator))
1380 (dolist (other-name (rest defaults))
1381 (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1382 (res `(declaim (ftype function ',other-name))))))
1385 (res (create-boa-constructor defstruct boa creator)))
1391 ;;; Like PROCLAIM-AS-FUNCTION-NAME, but we also set the kind to
1392 ;;; :DECLARED and blow away any ASSUMED-TYPE. Also, if the thing is a
1393 ;;; slot accessor currently, quietly unaccessorize it. And if there
1394 ;;; are any undefined warnings, we nuke them.
1395 (defun proclaim-as-defstruct-function-name (name)
1397 (when (info :function :accessor-for name)
1398 (setf (info :function :accessor-for name) nil))
1399 (proclaim-as-function-name name)
1400 (note-name-defined name :function)
1401 (setf (info :function :where-from name) :declared)
1402 (when (info :function :assumed-type name)
1403 (setf (info :function :assumed-type name) nil)))
1406 ;;;; finalizing bootstrapping
1408 ;;; early structure placeholder definitions: Set up layout and class
1409 ;;; data for structures which are needed early.
1411 '#.(sb-cold:read-from-file
1412 "src/code/early-defstruct-args.lisp-expr"))
1413 (let* ((defstruct (parse-name-and-options-and-slot-descriptions
1416 (inherits (inherits-for-structure defstruct)))
1417 (function-%compiler-only-defstruct defstruct inherits)))