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,
169 ;;; mentioning 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: 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 (/noshow0 "doing CLASS-STRUCTURE-P case for DEFSTRUCT " ,name)
250 (eval-when (:compile-toplevel :load-toplevel :execute)
251 (%compiler-only-defstruct ',dd ',inherits))
252 (%defstruct ',dd ',inherits)
253 ,@(when (eq (dd-type dd) 'structure)
254 `((%compiler-defstruct ',dd)))
255 (/noshow0 "starting not-for-the-xc-host section in DEFSTRUCT")
256 ,@(unless expanding-into-code-for-xc-host-p
257 (append (raw-accessor-definitions dd)
258 (predicate-definitions dd)
259 ;; FIXME: We've inherited from CMU CL nonparallel
260 ;; code for creating copiers for typed and untyped
261 ;; structures. This should be fixed.
262 ;(copier-definition dd)
263 (constructor-definitions dd)
264 (class-method-definitions dd)))
265 (/noshow0 "done with DEFSTRUCT " ,name)
268 (/show0 "doing NOT CLASS-STRUCTURE-P case for DEFSTRUCT " ,name)
269 (eval-when (:compile-toplevel :load-toplevel :execute)
270 (setf (info :typed-structure :info ',name) ',dd))
271 ,@(unless expanding-into-code-for-xc-host-p
272 (append (typed-accessor-definitions dd)
273 (typed-predicate-definitions dd)
274 (typed-copier-definitions dd)
275 (constructor-definitions dd)))
276 (/noshow0 "done with DEFSTRUCT " ,name)
279 (sb!xc:defmacro defstruct (name-and-options &rest slot-descriptions)
281 "DEFSTRUCT {Name | (Name Option*)} {Slot | (Slot [Default] {Key Value}*)}
282 Define the structure type Name. Instances are created by MAKE-<name>,
283 which takes &KEY arguments allowing initial slot values to the specified.
284 A SETF'able function <name>-<slot> is defined for each slot to read and
285 write slot values. <name>-p is a type predicate.
287 Popular DEFSTRUCT options (see manual for others):
291 Specify the name for the constructor or predicate.
293 (:CONSTRUCTOR Name Lambda-List)
294 Specify the name and arguments for a BOA constructor
295 (which is more efficient when keyword syntax isn't necessary.)
297 (:INCLUDE Supertype Slot-Spec*)
298 Make this type a subtype of the structure type Supertype. The optional
299 Slot-Specs override inherited slot options.
304 Asserts that the value of this slot is always of the specified type.
307 If true, no setter function is defined for this slot."
308 (expander-for-defstruct name-and-options slot-descriptions nil))
310 (defmacro sb!xc:defstruct (name-and-options &rest slot-descriptions)
312 "Cause information about a target structure to be built into the
314 (expander-for-defstruct name-and-options slot-descriptions t))
316 ;;;; functions to create various parts of DEFSTRUCT definitions
318 ;;; Catch requests to mess up definitions in COMMON-LISP.
320 (eval-when (:compile-toplevel :load-toplevel :execute)
321 (defun protect-cl (symbol)
322 (when (and *cold-init-complete-p*
323 (eq (symbol-package symbol) *cl-package*))
324 (cerror "Go ahead and patch the system."
325 "attempting to modify a symbol in the COMMON-LISP package: ~S"
328 ;;; Return forms to define readers and writers for raw slots as inline
330 (defun raw-accessor-definitions (dd)
331 (let* ((name (dd-name dd)))
333 (dolist (slot (dd-slots dd))
334 (let ((stype (dsd-type slot))
335 (accname (dsd-accessor slot))
336 (argname (gensym "ARG"))
337 (nvname (gensym "NEW-VALUE-")))
338 (multiple-value-bind (accessor offset data)
339 (slot-accessor-form dd slot argname)
340 ;; When accessor exists and is raw
341 (when (and accname (not (eq accessor '%instance-ref)))
342 (res `(declaim (inline ,accname)))
343 (res `(declaim (ftype (function (,name) ,stype) ,accname)))
344 (res `(defun ,accname (,argname)
345 (truly-the ,stype (,accessor ,data ,offset))))
346 (unless (dsd-read-only slot)
347 (res `(declaim (inline (setf ,accname))))
348 (res `(declaim (ftype (function (,stype ,name) ,stype)
350 ;; FIXME: I rewrote this somewhat from the CMU CL definition.
351 ;; Do some basic tests to make sure that reading and writing
352 ;; raw slots still works correctly.
353 (res `(defun (setf ,accname) (,nvname ,argname)
354 (setf (,accessor ,data ,offset) ,nvname)
358 ;;; Return a list of forms which create a predicate for an untyped DEFSTRUCT.
359 (defun predicate-definitions (dd)
360 (let ((pred (dd-predicate dd))
363 (if (eq (dd-type dd) 'funcallable-structure)
364 ;; FIXME: Why does this need to be special-cased for
365 ;; FUNCALLABLE-STRUCTURE? CMU CL did it, but without explanation.
366 ;; Could we do without it? What breaks if we do? Or could we
367 ;; perhaps get by with no predicates for funcallable structures?
368 `((declaim (inline ,pred))
369 (defun ,pred (,argname) (typep ,argname ',(dd-name dd))))
370 `((protect-cl ',pred)
371 (declaim (inline ,pred))
372 (defun ,pred (,argname)
373 (declare (optimize (speed 3) (safety 0)))
374 (typep-to-layout ,argname
375 (compile-time-find-layout ,(dd-name dd)))))))))
377 ;;; Return a list of forms which create a predicate function for a typed
379 (defun typed-predicate-definitions (defstruct)
380 (let ((name (dd-name defstruct))
381 (pred (dd-predicate defstruct))
383 (when (and pred (dd-named defstruct))
384 (let ((ltype (dd-lisp-type defstruct)))
385 `((defun ,pred (,argname)
386 (and (typep ,argname ',ltype)
387 (eq (elt (the ,ltype ,argname)
388 ,(cdr (car (last (find-name-indices defstruct)))))
391 ;;; FIXME: We've inherited from CMU CL code to do typed structure copiers
392 ;;; in a completely different way than untyped structure copiers. Fix this.
393 ;;; (This function was my first attempt to fix this, but I stopped before
394 ;;; figuring out how to install it completely and remove the parallel
395 ;;; code which simply SETF's the FDEFINITION of the DD-COPIER name.
397 ;;; Return the copier definition for an untyped DEFSTRUCT.
398 (defun copier-definition (dd)
399 (when (and (dd-copier dd)
400 ;; FUNCALLABLE-STRUCTUREs don't need copiers, and this
401 ;; implementation wouldn't work for them anyway, since
402 ;; COPY-STRUCTURE returns a STRUCTURE-OBJECT and they're not.
403 (not (eq (dd-type info) 'funcallable-structure)))
404 (let ((argname (gensym)))
406 (protect-cl ',(dd-copier dd))
407 (defun ,(dd-copier dd) (,argname)
408 (declare (type ,(dd-name dd) ,argname))
409 (copy-structure ,argname))))))
412 ;;; Return a list of forms to create a copier function of a typed DEFSTRUCT.
413 (defun typed-copier-definitions (defstruct)
414 (when (dd-copier defstruct)
415 `((setf (fdefinition ',(dd-copier defstruct)) #'copy-seq)
416 (declaim (ftype function ,(dd-copier defstruct))))))
418 ;;; Return a list of function definitions for accessing and setting the
419 ;;; slots of a typed DEFSTRUCT. The functions are proclaimed to be inline,
420 ;;; and the types of their arguments and results are declared as well. We
421 ;;; count on the compiler to do clever things with ELT.
422 (defun typed-accessor-definitions (defstruct)
424 (let ((ltype (dd-lisp-type defstruct)))
425 (dolist (slot (dd-slots defstruct))
426 (let ((name (dsd-accessor slot))
427 (index (dsd-index slot))
428 (slot-type `(and ,(dsd-type slot)
429 ,(dd-element-type defstruct))))
430 (stuff `(proclaim '(inline ,name (setf ,name))))
431 ;; FIXME: The arguments in the next two DEFUNs should be
432 ;; gensyms. (Otherwise e.g. if NEW-VALUE happened to be the
433 ;; name of a special variable, things could get weird.)
434 (stuff `(defun ,name (structure)
435 (declare (type ,ltype structure))
436 (the ,slot-type (elt structure ,index))))
437 (unless (dsd-read-only slot)
439 `(defun (setf ,name) (new-value structure)
440 (declare (type ,ltype structure) (type ,slot-type new-value))
441 (setf (elt structure ,index) new-value)))))))
446 (defun require-no-print-options-so-far (defstruct)
447 (unless (and (eql (dd-print-function defstruct) 0)
448 (eql (dd-print-object defstruct) 0))
449 (error "no more than one of the following options may be specified:
450 :PRINT-FUNCTION, :PRINT-OBJECT, :TYPE")))
452 ;;; Parse a single defstruct option and store the results in DEFSTRUCT.
453 (defun parse-1-option (option defstruct)
454 (let ((args (rest option))
455 (name (dd-name defstruct)))
458 (destructuring-bind (conc-name) args
459 (setf (dd-conc-name defstruct)
460 (if (symbolp conc-name)
462 (make-symbol (string conc-name))))))
464 (destructuring-bind (&optional (cname (symbolicate "MAKE-" name))
467 (push (cons cname stuff) (dd-constructors defstruct))))
469 (destructuring-bind (&optional (copier (symbolicate "COPY-" name)))
471 (setf (dd-copier defstruct) copier)))
473 (destructuring-bind (&optional (pred (symbolicate name "-P"))) args
474 (setf (dd-predicate defstruct) pred)))
476 (when (dd-include defstruct)
477 (error "more than one :INCLUDE option"))
478 (setf (dd-include defstruct) args))
479 (:alternate-metaclass
480 (setf (dd-alternate-metaclass defstruct) args))
482 (require-no-print-options-so-far defstruct)
483 (setf (dd-print-function defstruct)
484 (the (or symbol cons) args)))
486 (require-no-print-options-so-far defstruct)
487 (setf (dd-print-object defstruct)
488 (the (or symbol cons) args)))
490 (destructuring-bind (type) args
491 (cond ((eq type 'funcallable-structure)
492 (setf (dd-type defstruct) type))
493 ((member type '(list vector))
494 (setf (dd-element-type defstruct) t)
495 (setf (dd-type defstruct) type))
496 ((and (consp type) (eq (first type) 'vector))
497 (destructuring-bind (vector vtype) type
498 (declare (ignore vector))
499 (setf (dd-element-type defstruct) vtype)
500 (setf (dd-type defstruct) 'vector)))
502 (error "~S is a bad :TYPE for Defstruct." type)))))
504 (error "The DEFSTRUCT option :NAMED takes no arguments."))
506 (destructuring-bind (offset) args
507 (setf (dd-offset defstruct) offset)))
509 (destructuring-bind (fun) args
510 (setf (dd-pure defstruct) fun)))
511 (t (error "unknown DEFSTRUCT option:~% ~S" option)))))
513 ;;; Given name and options, return a DD holding that info.
514 (eval-when (:compile-toplevel :load-toplevel :execute)
515 (defun parse-name-and-options (name-and-options)
516 (destructuring-bind (name &rest options) name-and-options
517 (aver name) ; A null name doesn't seem to make sense here.
518 (let ((defstruct (make-defstruct-description name)))
519 (dolist (option options)
520 (cond ((consp option)
521 (parse-1-option option defstruct))
523 (setf (dd-named defstruct) t))
524 ((member option '(:constructor :copier :predicate :named))
525 (parse-1-option (list option) defstruct))
527 (error "unrecognized DEFSTRUCT option: ~S" option))))
529 (case (dd-type defstruct)
531 (when (dd-offset defstruct)
532 (error ":OFFSET can't be specified unless :TYPE is specified."))
533 (unless (dd-include defstruct)
534 (incf (dd-length defstruct))))
535 (funcallable-structure)
537 (require-no-print-options-so-far defstruct)
538 (when (dd-named defstruct)
539 (incf (dd-length defstruct)))
540 (let ((offset (dd-offset defstruct)))
541 (when offset (incf (dd-length defstruct) offset)))))
543 (when (dd-include defstruct)
544 (do-inclusion-stuff defstruct))
548 ;;; Given name and options and slot descriptions (and possibly doc
549 ;;; string at the head of slot descriptions) return a DD holding that
551 (defun parse-name-and-options-and-slot-descriptions (name-and-options
553 (/noshow "PARSE-NAME-AND-OPTIONS-AND-SLOT-DESCRIPTIONS" name-and-options)
554 (let ((result (parse-name-and-options (if (atom name-and-options)
555 (list name-and-options)
557 (when (stringp (car slot-descriptions))
558 (setf (dd-doc result) (pop slot-descriptions)))
559 (dolist (slot slot-descriptions)
560 (allocate-1-slot result (parse-1-dsd result slot)))
565 ;;;; stuff to parse slot descriptions
567 ;;; Parse a slot description for DEFSTRUCT, add it to the description
568 ;;; and return it. If supplied, ISLOT is a pre-initialized DSD that we
569 ;;; modify to get the new slot. This is supplied when handling
570 ;;; included slots. If the new accessor name is already an accessor
571 ;;; for same slot in some included structure, then set the
572 ;;; DSD-ACCESSOR to NIL so that we don't clobber the more general
574 (defun parse-1-dsd (defstruct spec &optional
575 (islot (make-defstruct-slot-description :%name ""
578 (multiple-value-bind (name default default-p type type-p read-only ro-p)
583 &optional (default nil default-p)
584 &key (type nil type-p) (read-only nil ro-p))
588 (uncross type) type-p
591 (when (keywordp spec)
592 ;; FIXME: should be style warning
593 (warn "Keyword slot name indicates probable syntax ~
594 error in DEFSTRUCT -- ~S."
598 (when (find name (dd-slots defstruct) :test #'string= :key #'dsd-%name)
599 (error 'simple-program-error
600 :format-control "duplicate slot name ~S"
601 :format-arguments (list name)))
602 (setf (dsd-%name islot) (string name))
603 (setf (dd-slots defstruct) (nconc (dd-slots defstruct) (list islot)))
605 (let* ((accname (symbolicate (or (dd-conc-name defstruct) "") name))
606 (existing (info :function :accessor-for accname)))
607 (declare (notinline find)) ; to avoid bug 117 bogowarnings
608 (if (and (structure-class-p existing)
609 (not (eq (sb!xc:class-name existing) (dd-name defstruct)))
610 (string= (dsd-%name (find accname
613 (class-layout existing)))
614 :key #'dsd-accessor))
616 (setf (dsd-accessor islot) nil)
617 (setf (dsd-accessor islot) accname)))
620 (setf (dsd-default islot) default))
622 (setf (dsd-type islot)
623 (if (eq (dsd-type islot) t)
625 `(and ,(dsd-type islot) ,type))))
628 (setf (dsd-read-only islot) t)
629 (when (dsd-read-only islot)
630 (error "Slot ~S is :READ-ONLY in parent and must be :READ-ONLY in subtype ~S."
635 ;;; When a value of type TYPE is stored in a structure, should it be
636 ;;; stored in a raw slot? Return (VALUES RAW? RAW-TYPE WORDS), where
637 ;;; RAW? is true if TYPE should be stored in a raw slot.
638 ;;; RAW-TYPE is the raw slot type, or NIL if no raw slot.
639 ;;; WORDS is the number of words in the raw slot, or NIL if no raw slot.
640 (defun structure-raw-slot-type-and-size (type)
641 (/noshow "in STRUCTURE-RAW-SLOT-TYPE-AND-SIZE" type (sb!xc:subtypep type 'fixnum))
643 (;; FIXME: For now we suppress raw slots, since there are various
644 ;; issues about the way that the cross-compiler handles them.
645 (not (boundp '*dummy-placeholder-to-stop-compiler-warnings*))
646 (values nil nil nil))
647 ((and (sb!xc:subtypep type '(unsigned-byte 32))
648 (multiple-value-bind (fixnum? fixnum-certain?)
649 (sb!xc:subtypep type 'fixnum)
650 (/noshow fixnum? fixnum-certain?)
651 ;; (The extra test for FIXNUM-CERTAIN? here is
652 ;; intended for bootstrapping the system. In
653 ;; particular, in sbcl-0.6.2, we set up LAYOUT before
654 ;; FIXNUM is defined, and so could bogusly end up
655 ;; putting INDEX-typed values into raw slots if we
656 ;; didn't test FIXNUM-CERTAIN?.)
657 (and (not fixnum?) fixnum-certain?)))
658 (values t 'unsigned-byte 1))
659 ((sb!xc:subtypep type 'single-float)
660 (values t 'single-float 1))
661 ((sb!xc:subtypep type 'double-float)
662 (values t 'double-float 2))
664 ((sb!xc:subtypep type 'long-float)
665 (values t 'long-float #!+x86 3 #!+sparc 4))
666 ((sb!xc:subtypep type '(complex single-float))
667 (values t 'complex-single-float 2))
668 ((sb!xc:subtypep type '(complex double-float))
669 (values t 'complex-double-float 4))
671 ((sb!xc:subtypep type '(complex long-float))
672 (values t 'complex-long-float #!+x86 6 #!+sparc 8))
674 (values nil nil nil))))
676 ;;; Allocate storage for a DSD in DEFSTRUCT. This is where we decide
677 ;;; whether a slot is raw or not. If raw, and we haven't allocated a
678 ;;; raw-index yet for the raw data vector, then do it. Raw objects are
679 ;;; aligned on the unit of their size.
680 (defun allocate-1-slot (defstruct dsd)
681 (multiple-value-bind (raw? raw-type words)
682 (if (eq (dd-type defstruct) 'structure)
683 (structure-raw-slot-type-and-size (dsd-type dsd))
684 (values nil nil nil))
685 (/noshow "ALLOCATE-1-SLOT" dsd raw? raw-type words)
687 (setf (dsd-index dsd) (dd-length defstruct))
688 (incf (dd-length defstruct)))
690 (unless (dd-raw-index defstruct)
691 (setf (dd-raw-index defstruct) (dd-length defstruct))
692 (incf (dd-length defstruct)))
693 (let ((off (rem (dd-raw-length defstruct) words)))
695 (incf (dd-raw-length defstruct) (- words off))))
696 (setf (dsd-raw-type dsd) raw-type)
697 (setf (dsd-index dsd) (dd-raw-length defstruct))
698 (incf (dd-raw-length defstruct) words))))
701 (defun typed-structure-info-or-lose (name)
702 (or (info :typed-structure :info name)
703 (error ":TYPE'd DEFSTRUCT ~S not found for inclusion." name)))
705 ;;; Process any included slots pretty much like they were specified.
706 ;;; Also inherit various other attributes.
707 (defun do-inclusion-stuff (defstruct)
709 (included-name &rest modified-slots)
710 (dd-include defstruct)
711 (let* ((type (dd-type defstruct))
713 (if (class-structure-p defstruct)
714 (layout-info (compiler-layout-or-lose included-name))
715 (typed-structure-info-or-lose included-name))))
716 (unless (and (eq type (dd-type included-structure))
717 (type= (specifier-type (dd-element-type included-structure))
718 (specifier-type (dd-element-type defstruct))))
719 (error ":TYPE option mismatch between structures ~S and ~S."
720 (dd-name defstruct) included-name))
722 (incf (dd-length defstruct) (dd-length included-structure))
723 (when (class-structure-p defstruct)
724 (let ((mc (rest (dd-alternate-metaclass included-structure))))
725 (when (and mc (not (dd-alternate-metaclass defstruct)))
726 (setf (dd-alternate-metaclass defstruct)
727 (cons included-name mc))))
728 (when (eq (dd-pure defstruct) :unspecified)
729 (setf (dd-pure defstruct) (dd-pure included-structure)))
730 (setf (dd-raw-index defstruct) (dd-raw-index included-structure))
731 (setf (dd-raw-length defstruct) (dd-raw-length included-structure)))
733 (dolist (islot (dd-slots included-structure))
734 (let* ((iname (dsd-name islot))
735 (modified (or (find iname modified-slots
736 :key #'(lambda (x) (if (atom x) x (car x)))
739 (parse-1-dsd defstruct modified (copy-structure islot)))))))
741 ;;; This function is called at macroexpand time to compute the INHERITS
742 ;;; vector for a structure type definition.
743 (defun inherits-for-structure (info)
744 (declare (type defstruct-description info))
745 (let* ((include (dd-include info))
746 (superclass-opt (dd-alternate-metaclass info))
749 (compiler-layout-or-lose (first include))
750 (class-layout (sb!xc:find-class
751 (or (first superclass-opt)
752 'structure-object))))))
753 (if (eq (dd-name info) 'lisp-stream)
754 ;; a hack to added the stream class as a mixin for LISP-STREAMs
755 (concatenate 'simple-vector
756 (layout-inherits super)
758 (class-layout (sb!xc:find-class 'stream))))
759 (concatenate 'simple-vector
760 (layout-inherits super)
763 ;;; Do miscellaneous (LOAD EVAL) time actions for the structure
764 ;;; described by INFO. Create the class & layout, checking for
765 ;;; incompatible redefinition. Define setters, accessors, copier,
766 ;;; predicate, documentation, instantiate definition in load-time env.
767 ;;; This is only called for default structures.
768 (defun %defstruct (info inherits)
769 (declare (type defstruct-description info))
770 (multiple-value-bind (class layout old-layout)
771 (ensure-structure-class info inherits "current" "new")
772 (cond ((not old-layout)
773 (unless (eq (class-layout class) layout)
774 (register-layout layout)))
776 (let ((old-info (layout-info old-layout)))
777 (when (defstruct-description-p old-info)
778 (dolist (slot (dd-slots old-info))
779 (fmakunbound (dsd-accessor slot))
780 (unless (dsd-read-only slot)
781 (fmakunbound `(setf ,(dsd-accessor slot)))))))
782 (%redefine-defstruct class old-layout layout)
783 (setq layout (class-layout class))))
785 (setf (sb!xc:find-class (dd-name info)) class)
787 ;; Set FDEFINITIONs for structure accessors, setters, predicates,
790 (unless (eq (dd-type info) 'funcallable-structure)
792 (dolist (slot (dd-slots info))
794 (when (and (dsd-accessor slot)
795 (eq (dsd-raw-type slot) t))
796 (protect-cl (dsd-accessor slot))
797 (setf (symbol-function (dsd-accessor slot))
798 (structure-slot-getter layout dsd))
799 (unless (dsd-read-only slot)
800 (setf (fdefinition `(setf ,(dsd-accessor slot)))
801 (structure-slot-setter layout dsd))))))
803 ;; FIXME: See comment on corresponding code in %%COMPILER-DEFSTRUCT.
805 (when (dd-predicate info)
806 (protect-cl (dd-predicate info))
807 (setf (symbol-function (dd-predicate info))
809 (declare (optimize (speed 3) (safety 0)))
810 (typep-to-layout object layout))))
813 (when (dd-copier info)
814 (protect-cl (dd-copier info))
815 (setf (symbol-function (dd-copier info))
816 #'(lambda (structure)
817 (declare (optimize (speed 3) (safety 0)))
818 (flet ((layout-test (structure)
819 (typep-to-layout structure layout)))
820 (unless (layout-test structure)
821 (error 'simple-type-error
823 :expected-type '(satisfies layout-test)
825 "Structure for copier is not a ~S:~% ~S"
827 (list (sb!xc:class-name (layout-class layout))
829 (copy-structure structure))))))
832 (setf (fdocumentation (dd-name info) 'type) (dd-doc info)))
836 ;;; This function is called at compile-time to do the
837 ;;; compile-time-only actions for defining a structure type. It
838 ;;; installs the class in the type system in a similar way to
839 ;;; %DEFSTRUCT, but is quieter and safer in the case of redefinition.
841 ;;; The comments for the classic CMU CL version of this function said
842 ;;; that EVAL-WHEN doesn't do the right thing when nested or
843 ;;; non-top-level, and so CMU CL had the function magically called by
844 ;;; the compiler. Unfortunately, this doesn't do the right thing
845 ;;; either: compiling a function (DEFUN FOO () (DEFSTRUCT FOO X Y))
846 ;;; causes the class FOO to become defined, even though FOO is never
847 ;;; loaded or executed. Even more unfortunately, I've been unable to
848 ;;; come up with any EVAL-WHEN tricks which work -- I finally gave up
849 ;;; on this approach when trying to get the system to cross-compile
850 ;;; error.lisp. (Just because I haven't found it doesn't mean that it
851 ;;; doesn't exist, of course. Alas, I continue to have some trouble
852 ;;; understanding compile/load semantics in Common Lisp.) So we
853 ;;; continue to use the IR1 transformation approach, even though it's
854 ;;; known to be buggy. -- WHN 19990507
856 ;;; Basically, this function avoids trashing the compiler by only
857 ;;; actually defining the class if there is no current definition.
858 ;;; Instead, we just set the INFO TYPE COMPILER-LAYOUT. This behavior
859 ;;; is left over from classic CMU CL and may not be necessary in the
860 ;;; new build system. -- WHN 19990507
862 ;;; FUNCTION-%COMPILER-ONLY-DEFSTRUCT is an ordinary function, called
863 ;;; by both the IR1 transform version of %COMPILER-ONLY-DEFSTRUCT and
864 ;;; by the ordinary function version of %COMPILER-ONLY-DEFSTRUCT. (The
865 ;;; ordinary function version is there for the interpreter and for
867 (defun %compiler-only-defstruct (info inherits)
868 (function-%compiler-only-defstruct info inherits))
869 (defun function-%compiler-only-defstruct (info inherits)
870 (multiple-value-bind (class layout old-layout)
871 (multiple-value-bind (clayout clayout-p)
872 (info :type :compiler-layout (dd-name info))
873 (ensure-structure-class info
875 (if clayout-p "previously compiled" "current")
877 :compiler-layout clayout))
879 (undefine-structure (layout-class old-layout))
880 (when (and (class-subclasses class)
881 (not (eq layout old-layout)))
883 (dohash (class layout (class-subclasses class))
884 (declare (ignore layout))
885 (undefine-structure class)
886 (subs (class-proper-name class)))
888 (warn "Removing old subclasses of ~S:~% ~S"
889 (sb!xc:class-name class)
892 (unless (eq (class-layout class) layout)
893 (register-layout layout :invalidate nil))
894 (setf (sb!xc:find-class (dd-name info)) class)))
896 (setf (info :type :compiler-layout (dd-name info)) layout))
899 ;;; This function does the (COMPILE LOAD EVAL) time actions for updating the
900 ;;; compiler's global meta-information to represent the definition of the
901 ;;; structure described by Info. This primarily amounts to setting up info
902 ;;; about the accessor and other implicitly defined functions. The constructors
903 ;;; are explicitly defined by top-level code.
904 (defun %%compiler-defstruct (info)
905 (declare (type defstruct-description info))
906 (let* ((name (dd-name info))
907 (class (sb!xc:find-class name)))
908 (let ((copier (dd-copier info)))
910 (proclaim `(ftype (function (,name) ,name) ,copier))))
912 ;; FIXME: This (and corresponding code in %DEFSTRUCT) are the way
913 ;; that CMU CL defined the predicate, instead of using DEFUN.
914 ;; Perhaps it would be better to go back to to the CMU CL way, or
915 ;; something similar. I want to reduce the amount of magic in
916 ;; defstruct functions, but making the predicate be a closure
917 ;; looks like a good thing, and can even be done without magic.
918 ;; (OTOH, there are some bootstrapping issues involved, since
919 ;; GENESIS understands DEFUN but doesn't understand a
920 ;; (SETF SYMBOL-FUNCTION) call inside %DEFSTRUCT.)
922 (let ((pred (dd-predicate info)))
924 (proclaim-as-defstruct-function-name pred)
925 (setf (info :function :inlinep pred) :inline)
926 (setf (info :function :inline-expansion pred)
927 `(lambda (x) (typep x ',name)))))
930 (dolist (slot (dd-slots info))
931 (let* ((fun (dsd-accessor slot))
932 (setf-fun `(setf ,fun)))
933 (when (and fun (eq (dsd-raw-type slot) t))
934 (proclaim-as-defstruct-function-name fun)
935 (setf (info :function :accessor-for fun) class)
936 (unless (dsd-read-only slot)
937 (proclaim-as-defstruct-function-name setf-fun)
938 (setf (info :function :accessor-for setf-fun) class))))))
942 ;;; Ordinarily this is preempted by an IR1 transformation, but this
943 ;;; definition is still useful for the interpreter and code walkers.
944 (defun %compiler-defstruct (info)
945 (%%compiler-defstruct info))
947 ;;;; redefinition stuff
949 ;;; Compare the slots of OLD and NEW, returning 3 lists of slot names:
950 ;;; 1. Slots which have moved,
951 ;;; 2. Slots whose type has changed,
952 ;;; 3. Deleted slots.
953 (defun compare-slots (old new)
954 (let* ((oslots (dd-slots old))
955 (nslots (dd-slots new))
956 (onames (mapcar #'dsd-name oslots))
957 (nnames (mapcar #'dsd-name nslots)))
960 (dolist (name (intersection onames nnames))
961 (let ((os (find name oslots :key #'dsd-name))
962 (ns (find name nslots :key #'dsd-name)))
963 (unless (subtypep (dsd-type ns) (dsd-type os))
964 (/noshow "found retyped slots" ns os (dsd-type ns) (dsd-type os))
966 (unless (and (= (dsd-index os) (dsd-index ns))
967 (eq (dsd-raw-type os) (dsd-raw-type ns)))
971 (set-difference onames nnames)))))
973 ;;; If we are redefining a structure with different slots than in the
974 ;;; currently loaded version, give a warning and return true.
975 (defun redefine-structure-warning (class old new)
976 (declare (type defstruct-description old new)
977 (type sb!xc:class class)
979 (let ((name (dd-name new)))
980 (multiple-value-bind (moved retyped deleted) (compare-slots old new)
981 (when (or moved retyped deleted)
983 "incompatibly redefining slots of structure class ~S~@
984 Make sure any uses of affected accessors are recompiled:~@
985 ~@[ These slots were moved to new positions:~% ~S~%~]~
986 ~@[ These slots have new incompatible types:~% ~S~%~]~
987 ~@[ These slots were deleted:~% ~S~%~]"
988 name moved retyped deleted)
991 ;;; This function is called when we are incompatibly redefining a
992 ;;; structure Class to have the specified New-Layout. We signal an
993 ;;; error with some proceed options and return the layout that should
995 (defun %redefine-defstruct (class old-layout new-layout)
996 (declare (type sb!xc:class class) (type layout old-layout new-layout))
997 (let ((name (class-proper-name class)))
999 (error "redefining class ~S incompatibly with the current definition"
1002 :report "Invalidate current definition."
1003 (warn "Previously loaded ~S accessors will no longer work." name)
1004 (register-layout new-layout))
1006 :report "Smash current layout, preserving old code."
1007 (warn "Any old ~S instances will be in a bad way.~@
1008 I hope you know what you're doing..."
1010 (register-layout new-layout :invalidate nil
1011 :destruct-layout old-layout))))
1014 ;;; This is called when we are about to define a structure class. It
1015 ;;; returns a (possibly new) class object and the layout which should
1016 ;;; be used for the new definition (may be the current layout, and
1017 ;;; also might be an uninstalled forward referenced layout.) The third
1018 ;;; value is true if this is an incompatible redefinition, in which
1019 ;;; case it is the old layout.
1020 (defun ensure-structure-class (info inherits old-context new-context
1021 &key compiler-layout)
1022 (multiple-value-bind (class old-layout)
1026 (class 'sb!xc:structure-class)
1027 (constructor 'make-structure-class))
1028 (dd-alternate-metaclass info)
1029 (declare (ignore name))
1030 (insured-find-class (dd-name info)
1031 (if (eq class 'sb!xc:structure-class)
1033 (typep x 'sb!xc:structure-class))
1035 (sb!xc:typep x (sb!xc:find-class class))))
1036 (fdefinition constructor)))
1037 (setf (class-direct-superclasses class)
1038 (if (eq (dd-name info) 'lisp-stream)
1039 ;; a hack to add STREAM as a superclass mixin to LISP-STREAMs
1040 (list (layout-class (svref inherits (1- (length inherits))))
1041 (layout-class (svref inherits (- (length inherits) 2))))
1042 (list (layout-class (svref inherits (1- (length inherits)))))))
1043 (let ((new-layout (make-layout :class class
1045 :depthoid (length inherits)
1046 :length (dd-length info)
1048 (old-layout (or compiler-layout old-layout)))
1051 (values class new-layout nil))
1052 (;; This clause corresponds to an assertion in REDEFINE-LAYOUT-WARNING
1053 ;; of classic CMU CL. I moved it out to here because it was only
1054 ;; exercised in this code path anyway. -- WHN 19990510
1055 (not (eq (layout-class new-layout) (layout-class old-layout)))
1056 (error "shouldn't happen: weird state of OLD-LAYOUT?"))
1057 ((not *type-system-initialized*)
1058 (setf (layout-info old-layout) info)
1059 (values class old-layout nil))
1060 ((redefine-layout-warning old-context
1063 (layout-length new-layout)
1064 (layout-inherits new-layout)
1065 (layout-depthoid new-layout))
1066 (values class new-layout old-layout))
1068 (let ((old-info (layout-info old-layout)))
1070 ((or defstruct-description)
1071 (cond ((redefine-structure-warning class old-info info)
1072 (values class new-layout old-layout))
1074 (setf (layout-info old-layout) info)
1075 (values class old-layout nil))))
1077 (setf (layout-info old-layout) info)
1078 (values class old-layout nil))
1080 (error "shouldn't happen! strange thing in LAYOUT-INFO:~% ~S"
1082 (values class new-layout old-layout)))))))))
1084 ;;; Blow away all the compiler info for the structure CLASS. Iterate
1085 ;;; over this type, clearing the compiler structure type info, and
1086 ;;; undefining all the associated functions.
1087 (defun undefine-structure (class)
1088 (let ((info (layout-info (class-layout class))))
1089 (when (defstruct-description-p info)
1090 (let ((type (dd-name info)))
1091 (setf (info :type :compiler-layout type) nil)
1092 (undefine-function-name (dd-copier info))
1093 (undefine-function-name (dd-predicate info))
1094 (dolist (slot (dd-slots info))
1095 (let ((fun (dsd-accessor slot)))
1096 (undefine-function-name fun)
1097 (unless (dsd-read-only slot)
1098 (undefine-function-name `(setf ,fun))))))
1099 ;; Clear out the SPECIFIER-TYPE cache so that subsequent
1100 ;; references are unknown types.
1101 (values-specifier-type-cache-clear)))
1104 ;;; Return a list of pairs (name . index). Used for :TYPE'd
1105 ;;; constructors to find all the names that we have to splice in &
1106 ;;; where. Note that these types don't have a layout, so we can't look
1107 ;;; at LAYOUT-INHERITS.
1108 (defun find-name-indices (defstruct)
1111 (do ((info defstruct
1112 (typed-structure-info-or-lose (first (dd-include info)))))
1113 ((not (dd-include info))
1118 (dolist (info infos)
1119 (incf i (or (dd-offset info) 0))
1120 (when (dd-named info)
1121 (res (cons (dd-name info) i)))
1122 (setq i (dd-length info)))))
1126 ;;;; slot accessors for raw slots
1128 ;;; Return info about how to read/write a slot in the value stored in
1129 ;;; OBJECT. This is also used by constructors (we can't use the
1130 ;;; accessor function, since some slots are read-only.) If supplied,
1131 ;;; DATA is a variable holding the raw-data vector.
1133 ;;; returned values:
1134 ;;; 1. accessor function name (SETFable)
1135 ;;; 2. index to pass to accessor.
1136 ;;; 3. object form to pass to accessor
1137 (defun slot-accessor-form (defstruct slot object &optional data)
1138 (let ((rtype (dsd-raw-type slot)))
1141 (single-float '%raw-ref-single)
1142 (double-float '%raw-ref-double)
1144 (long-float '%raw-ref-long)
1145 (complex-single-float '%raw-ref-complex-single)
1146 (complex-double-float '%raw-ref-complex-double)
1148 (complex-long-float '%raw-ref-complex-long)
1149 (unsigned-byte 'aref)
1151 (if (eq (dd-type defstruct) 'funcallable-structure)
1152 '%funcallable-instance-info
1157 (truncate (dsd-index slot) #!+x86 6 #!+sparc 8))
1160 (truncate (dsd-index slot) #!+x86 3 #!+sparc 4))
1162 (ash (dsd-index slot) -1))
1163 (complex-double-float
1164 (ash (dsd-index slot) -2))
1165 (complex-single-float
1166 (ash (dsd-index slot) -1))
1170 ((eq rtype t) object)
1173 `(truly-the (simple-array (unsigned-byte 32) (*))
1174 (%instance-ref ,object ,(dd-raw-index defstruct))))))))
1176 ;;; These functions are called to actually make a constructor after we
1177 ;;; have processed the arglist. The correct variant (according to the
1178 ;;; DD-TYPE) should be called. The function is defined with the
1179 ;;; specified name and arglist. Vars and Types are used for argument
1180 ;;; type declarations. Values are the values for the slots (in order.)
1182 ;;; This is split four ways because:
1183 ;;; 1] list & vector structures need "name" symbols stuck in at
1184 ;;; various weird places, whereas STRUCTURE structures have
1186 ;;; 2] We really want to use LIST to make list structures, instead of
1187 ;;; MAKE-LIST/(SETF ELT).
1188 ;;; 3] STRUCTURE structures can have raw slots that must also be
1189 ;;; allocated and indirectly referenced. We use SLOT-ACCESSOR-FORM
1190 ;;; to compute how to set the slots, which deals with raw slots.
1191 ;;; 4] Funcallable structures are weird.
1192 (defun create-vector-constructor
1193 (defstruct cons-name arglist vars types values)
1194 (let ((temp (gensym))
1195 (etype (dd-element-type defstruct)))
1196 `(defun ,cons-name ,arglist
1197 (declare ,@(mapcar #'(lambda (var type) `(type (and ,type ,etype) ,var))
1199 (let ((,temp (make-array ,(dd-length defstruct)
1200 :element-type ',(dd-element-type defstruct))))
1201 ,@(mapcar #'(lambda (x)
1202 `(setf (aref ,temp ,(cdr x)) ',(car x)))
1203 (find-name-indices defstruct))
1204 ,@(mapcar #'(lambda (dsd value)
1205 `(setf (aref ,temp ,(dsd-index dsd)) ,value))
1206 (dd-slots defstruct) values)
1208 (defun create-list-constructor
1209 (defstruct cons-name arglist vars types values)
1210 (let ((vals (make-list (dd-length defstruct) :initial-element nil)))
1211 (dolist (x (find-name-indices defstruct))
1212 (setf (elt vals (cdr x)) `',(car x)))
1213 (loop for dsd in (dd-slots defstruct) and val in values do
1214 (setf (elt vals (dsd-index dsd)) val))
1216 `(defun ,cons-name ,arglist
1217 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1220 (defun create-structure-constructor
1221 (defstruct cons-name arglist vars types values)
1222 (let* ((temp (gensym))
1223 (raw-index (dd-raw-index defstruct))
1224 (n-raw-data (when raw-index (gensym))))
1225 `(defun ,cons-name ,arglist
1226 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1228 (let ((,temp (truly-the ,(dd-name defstruct)
1229 (%make-instance ,(dd-length defstruct))))
1232 (make-array ,(dd-raw-length defstruct)
1233 :element-type '(unsigned-byte 32))))))
1234 (setf (%instance-layout ,temp)
1235 (%delayed-get-compiler-layout ,(dd-name defstruct)))
1237 `((setf (%instance-ref ,temp ,raw-index) ,n-raw-data)))
1238 ,@(mapcar (lambda (dsd value)
1239 (multiple-value-bind (accessor index data)
1240 (slot-accessor-form defstruct dsd temp n-raw-data)
1241 `(setf (,accessor ,data ,index) ,value)))
1242 (dd-slots defstruct)
1245 (defun create-fin-constructor
1246 (defstruct cons-name arglist vars types values)
1247 (let ((temp (gensym)))
1248 `(defun ,cons-name ,arglist
1249 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1251 (let ((,temp (truly-the
1252 ,(dd-name defstruct)
1253 (%make-funcallable-instance
1254 ,(dd-length defstruct)
1255 (%delayed-get-compiler-layout ,(dd-name defstruct))))))
1256 ,@(mapcar #'(lambda (dsd value)
1257 `(setf (%funcallable-instance-info
1258 ,temp ,(dsd-index dsd))
1260 (dd-slots defstruct) values)
1263 ;;; Create a default (non-BOA) keyword constructor.
1264 (defun create-keyword-constructor (defstruct creator)
1265 (collect ((arglist (list '&key))
1268 (dolist (slot (dd-slots defstruct))
1269 (let ((dum (gensym))
1270 (name (dsd-name slot)))
1271 (arglist `((,(keywordicate name) ,dum) ,(dsd-default slot)))
1272 (types (dsd-type slot))
1275 defstruct (dd-default-constructor defstruct)
1276 (arglist) (vals) (types) (vals))))
1278 ;;; Given a structure and a BOA constructor spec, call CREATOR with
1279 ;;; the appropriate args to make a constructor.
1280 (defun create-boa-constructor (defstruct boa creator)
1281 (multiple-value-bind (req opt restp rest keyp keys allowp aux)
1282 (sb!kernel:parse-lambda-list (second boa))
1286 (labels ((get-slot (name)
1287 (let ((res (find name (dd-slots defstruct)
1291 (values (dsd-type res) (dsd-default res))
1294 (multiple-value-bind (type default) (get-slot arg)
1295 (arglist `(,arg ,default))
1301 (types (get-slot arg)))
1304 (arglist '&optional)
1308 (name &optional (def (nth-value 1 (get-slot name))))
1310 (arglist `(,name ,def))
1312 (types (get-slot name))))
1314 (do-default arg)))))
1317 (arglist '&rest rest)
1325 (destructuring-bind (wot &optional (def nil def-p)) key
1326 (let ((name (if (consp wot)
1327 (destructuring-bind (key var) wot
1328 (declare (ignore key))
1331 (multiple-value-bind (type slot-def) (get-slot name)
1332 (arglist `(,wot ,(if def-p def slot-def)))
1337 (when allowp (arglist '&allow-other-keys))
1342 (let* ((arg (if (consp arg) arg (list arg)))
1346 (types (get-slot var))))))
1348 (funcall creator defstruct (first boa)
1349 (arglist) (vars) (types)
1350 (mapcar #'(lambda (slot)
1351 (or (find (dsd-name slot) (vars) :test #'string=)
1352 (dsd-default slot)))
1353 (dd-slots defstruct))))))
1355 ;;; Grovel the constructor options, and decide what constructors (if
1357 (defun constructor-definitions (defstruct)
1358 (let ((no-constructors nil)
1361 (creator (ecase (dd-type defstruct)
1362 (structure #'create-structure-constructor)
1363 (funcallable-structure #'create-fin-constructor)
1364 (vector #'create-vector-constructor)
1365 (list #'create-list-constructor))))
1366 (dolist (constructor (dd-constructors defstruct))
1367 (destructuring-bind (name &optional (boa-ll nil boa-p)) constructor
1368 (declare (ignore boa-ll))
1369 (cond ((not name) (setq no-constructors t))
1370 (boa-p (push constructor boas))
1371 (t (push name defaults)))))
1373 (when no-constructors
1374 (when (or defaults boas)
1375 (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs"))
1376 (return-from constructor-definitions ()))
1378 (unless (or defaults boas)
1379 (push (symbolicate "MAKE-" (dd-name defstruct)) defaults))
1383 (let ((cname (first defaults)))
1384 (setf (dd-default-constructor defstruct) cname)
1385 (res (create-keyword-constructor defstruct creator))
1386 (dolist (other-name (rest defaults))
1387 (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1388 (res `(declaim (ftype function ',other-name))))))
1391 (res (create-boa-constructor defstruct boa creator)))
1397 ;;; Like PROCLAIM-AS-FUNCTION-NAME, but we also set the kind to
1398 ;;; :DECLARED and blow away any ASSUMED-TYPE. Also, if the thing is a
1399 ;;; slot accessor currently, quietly unaccessorize it. And if there
1400 ;;; are any undefined warnings, we nuke them.
1401 (defun proclaim-as-defstruct-function-name (name)
1403 (when (info :function :accessor-for name)
1404 (setf (info :function :accessor-for name) nil))
1405 (proclaim-as-function-name name)
1406 (note-name-defined name :function)
1407 (setf (info :function :where-from name) :declared)
1408 (when (info :function :assumed-type name)
1409 (setf (info :function :assumed-type name) nil)))
1412 ;;;; finalizing bootstrapping
1414 ;;; early structure placeholder definitions: Set up layout and class
1415 ;;; data for structures which are needed early.
1417 '#.(sb-cold:read-from-file
1418 "src/code/early-defstruct-args.lisp-expr"))
1419 (let* ((defstruct (parse-name-and-options-and-slot-descriptions
1422 (inherits (inherits-for-structure defstruct)))
1423 (function-%compiler-only-defstruct defstruct inherits)))
1425 (/show0 "code/defstruct.lisp end of file")