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) stuff is compiled. (Or, in the oddball case when
32 ;;; DEFSTRUCT is executing in a non-toplevel context, the
33 ;;; compiler-layout still doesn't exist at compilation time, and we
34 ;;; delay still further.)
35 (sb!xc:defmacro %delayed-get-compiler-layout (name)
36 (let ((layout (info :type :compiler-layout name)))
38 ;; ordinary case: When the DEFSTRUCT is at top level,
39 ;; then EVAL-WHEN (COMPILE) stuff will have set up the
40 ;; layout for us to use.
41 (unless (typep (layout-info layout) 'defstruct-description)
42 (error "Class is not a structure class: ~S" name))
45 ;; KLUDGE: In the case that DEFSTRUCT is not at top-level
46 ;; the layout doesn't exist at compile time. In that case
47 ;; we laboriously look it up at run time. This code will
48 ;; run on every constructor call and will likely be quite
49 ;; slow, so if anyone cares about performance of
50 ;; non-toplevel DEFSTRUCTs, it should be rewritten to be
51 ;; cleverer. -- WHN 2002-10-23
53 "implementation limitation: ~
54 Non-toplevel DEFSTRUCT constructors are slow.")
55 (with-unique-names (layout)
56 `(let ((,layout (info :type :compiler-layout ',name)))
57 (unless (typep (layout-info ,layout) 'defstruct-description)
58 (error "Class is not a structure class: ~S" ',name))
61 ;;; Get layout right away.
62 (sb!xc:defmacro compile-time-find-layout (name)
65 ;;; re. %DELAYED-GET-COMPILER-LAYOUT and COMPILE-TIME-FIND-LAYOUT, above..
67 ;;; FIXME: Perhaps both should be defined with DEFMACRO-MUNDANELY?
68 ;;; FIXME: Do we really need both? If so, their names and implementations
69 ;;; should probably be tweaked to be more parallel.
71 ;;;; DEFSTRUCT-DESCRIPTION
73 ;;; The DEFSTRUCT-DESCRIPTION structure holds compile-time information
74 ;;; about a structure type.
75 (def!struct (defstruct-description
77 (:make-load-form-fun just-dump-it-normally)
78 #-sb-xc-host (:pure t)
79 (:constructor make-defstruct-description
81 (conc-name (symbolicate name "-"))
82 (copier-name (symbolicate "COPY-" name))
83 (predicate-name (symbolicate name "-P")))))
84 ;; name of the structure
85 (name (missing-arg) :type symbol :read-only t)
86 ;; documentation on the structure
87 (doc nil :type (or string null))
88 ;; prefix for slot names. If NIL, none.
89 (conc-name nil :type (or symbol null))
90 ;; the name of the primary standard keyword constructor, or NIL if none
91 (default-constructor nil :type (or symbol null))
92 ;; all the explicit :CONSTRUCTOR specs, with name defaulted
93 (constructors () :type list)
94 ;; name of copying function
95 (copier-name nil :type (or symbol null))
96 ;; name of type predicate
97 (predicate-name nil :type (or symbol null))
98 ;; the arguments to the :INCLUDE option, or NIL if no included
100 (include nil :type list)
101 ;; properties used to define structure-like classes with an
102 ;; arbitrary superclass and that may not have STRUCTURE-CLASS as the
103 ;; metaclass. Syntax is:
104 ;; (superclass-name metaclass-name metaclass-constructor)
105 (alternate-metaclass nil :type list)
106 ;; a list of DEFSTRUCT-SLOT-DESCRIPTION objects for all slots
107 ;; (including included ones)
108 (slots () :type list)
109 ;; a list of (NAME . INDEX) pairs for accessors of included structures
110 (inherited-accessor-alist () :type list)
111 ;; number of elements we've allocated (See also RAW-LENGTH.)
112 (length 0 :type index)
113 ;; General kind of implementation.
114 (type 'structure :type (member structure vector list
115 funcallable-structure))
117 ;; The next three slots are for :TYPE'd structures (which aren't
118 ;; classes, DD-CLASS-P = NIL)
120 ;; vector element type
122 ;; T if :NAMED was explicitly specified, NIL otherwise
123 (named nil :type boolean)
124 ;; any INITIAL-OFFSET option on this direct type
125 (offset nil :type (or index null))
127 ;; the argument to the PRINT-FUNCTION option, or NIL if a
128 ;; PRINT-FUNCTION option was given with no argument, or 0 if no
129 ;; PRINT-FUNCTION option was given
130 (print-function 0 :type (or cons symbol (member 0)))
131 ;; the argument to the PRINT-OBJECT option, or NIL if a PRINT-OBJECT
132 ;; option was given with no argument, or 0 if no PRINT-OBJECT option
134 (print-object 0 :type (or cons symbol (member 0)))
135 ;; the index of the raw data vector and the number of words in it,
136 ;; or NIL and 0 if not allocated (either because this structure
137 ;; has no raw slots, or because we're still parsing it and haven't
138 ;; run across any raw slots yet)
139 (raw-index nil :type (or index null))
140 (raw-length 0 :type index)
141 ;; the value of the :PURE option, or :UNSPECIFIED. This is only
142 ;; meaningful if DD-CLASS-P = T.
143 (pure :unspecified :type (member t nil :substructure :unspecified)))
144 (def!method print-object ((x defstruct-description) stream)
145 (print-unreadable-object (x stream :type t)
146 (prin1 (dd-name x) stream)))
148 ;;; Does DD describe a structure with a class?
149 (defun dd-class-p (dd)
151 '(structure funcallable-structure)))
153 ;;; a type name which can be used when declaring things which operate
154 ;;; on structure instances
155 (defun dd-declarable-type (dd)
157 ;; Native classes are known to the type system, and we can
158 ;; declare them as types.
160 ;; Structures layered on :TYPE LIST or :TYPE VECTOR aren't part
161 ;; of the type system, so all we can declare is the underlying
162 ;; LIST or VECTOR type.
165 (defun dd-layout-or-lose (dd)
166 (compiler-layout-or-lose (dd-name dd)))
168 ;;;; DEFSTRUCT-SLOT-DESCRIPTION
170 ;;; A DEFSTRUCT-SLOT-DESCRIPTION holds compile-time information about
171 ;;; a structure slot.
172 (def!struct (defstruct-slot-description
173 (:make-load-form-fun just-dump-it-normally)
176 #-sb-xc-host (:pure t))
179 ;; its position in the implementation sequence
180 (index (missing-arg) :type fixnum)
181 ;; the name of the accessor function
183 ;; (CMU CL had extra complexity here ("..or NIL if this accessor has
184 ;; the same name as an inherited accessor (which we don't want to
185 ;; shadow)") but that behavior doesn't seem to be specified by (or
186 ;; even particularly consistent with) ANSI, so it's gone in SBCL.)
188 default ; default value expression
189 (type t) ; declared type specifier
190 (safe-p t :type boolean) ; whether the slot is known to be
191 ; always of the specified type
192 ;; If this object does not describe a raw slot, this value is T.
194 ;; If this object describes a raw slot, this value is the type of the
195 ;; value that the raw slot holds. Mostly. (KLUDGE: If the raw slot has
196 ;; type (UNSIGNED-BYTE 32), the value here is UNSIGNED-BYTE, not
197 ;; (UNSIGNED-BYTE 32).)
198 (raw-type t :type (member t single-float double-float
199 #!+long-float long-float
200 complex-single-float complex-double-float
201 #!+long-float complex-long-float
203 (read-only nil :type (member t nil)))
204 (def!method print-object ((x defstruct-slot-description) stream)
205 (print-unreadable-object (x stream :type t)
206 (prin1 (dsd-name x) stream)))
208 ;;;; typed (non-class) structures
210 ;;; Return a type specifier we can use for testing :TYPE'd structures.
211 (defun dd-lisp-type (defstruct)
212 (ecase (dd-type defstruct)
214 (vector `(simple-array ,(dd-element-type defstruct) (*)))))
216 ;;;; shared machinery for inline and out-of-line slot accessor functions
218 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
220 ;; information about how a slot of a given DSD-RAW-TYPE is to be accessed
221 (defstruct raw-slot-data
222 ;; the raw slot type, or T for a non-raw slot
224 ;; (Raw slots are allocated in the raw slots array in a vector which
225 ;; the GC doesn't need to scavenge. Non-raw slots are in the
226 ;; ordinary place you'd expect, directly indexed off the instance
228 (raw-type (missing-arg) :type (or symbol cons) :read-only t)
229 ;; What operator is used (on the raw data vector) to access a slot
231 (accessor-name (missing-arg) :type symbol :read-only t)
232 ;; How many words are each value of this type? (This is used to
233 ;; rescale the offset into the raw data vector.)
234 (n-words (missing-arg) :type (and index (integer 1)) :read-only t))
236 (defvar *raw-slot-data-list*
238 ;; The compiler thinks that the raw data vector is a vector of
239 ;; word-sized unsigned bytes, so if the slot we want to access
240 ;; actually *is* an unsigned byte, it'll access the slot for us
241 ;; even if we don't lie to it at all, just let it use normal AREF.
242 (make-raw-slot-data :raw-type 'unsigned-byte
245 ;; In the other cases, we lie to the compiler, making it use
246 ;; some low-level AREFish access in order to pun the hapless
247 ;; bits into some other-than-unsigned-byte meaning.
249 ;; "A lie can travel halfway round the world while the truth is
250 ;; putting on its shoes." -- Mark Twain
251 (make-raw-slot-data :raw-type 'single-float
252 :accessor-name '%raw-ref-single
254 (make-raw-slot-data :raw-type 'double-float
255 :accessor-name '%raw-ref-double
257 (make-raw-slot-data :raw-type 'complex-single-float
258 :accessor-name '%raw-ref-complex-single
260 (make-raw-slot-data :raw-type 'complex-double-float
261 :accessor-name '%raw-ref-complex-double
264 (make-raw-slot-data :raw-type long-float
265 :accessor-name '%raw-ref-long
266 :n-words #!+x86 3 #!+sparc 4)
268 (make-raw-slot-data :raw-type complex-long-float
269 :accessor-name '%raw-ref-complex-long
270 :n-words #!+x86 6 #!+sparc 8))))
272 ;;;; the legendary DEFSTRUCT macro itself (both CL:DEFSTRUCT and its
273 ;;;; close personal friend SB!XC:DEFSTRUCT)
275 ;;; Return a list of forms to install PRINT and MAKE-LOAD-FORM funs,
276 ;;; mentioning them in the expansion so that they can be compiled.
277 (defun class-method-definitions (defstruct)
278 (let ((name (dd-name defstruct)))
280 ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant
281 ;; class names which creates fast but non-cold-loadable,
282 ;; non-compact code. In this context, we'd rather have
283 ;; compact, cold-loadable code. -- WHN 19990928
284 (declare (notinline find-classoid))
285 ,@(let ((pf (dd-print-function defstruct))
286 (po (dd-print-object defstruct))
289 ;; Giving empty :PRINT-OBJECT or :PRINT-FUNCTION options
290 ;; leaves PO or PF equal to NIL. The user-level effect is
291 ;; to generate a PRINT-OBJECT method specialized for the type,
292 ;; implementing the default #S structure-printing behavior.
293 (when (or (eq pf nil) (eq po nil))
294 (setf pf '(default-structure-print)
296 (flet (;; Given an arg from a :PRINT-OBJECT or :PRINT-FUNCTION
297 ;; option, return the value to pass as an arg to FUNCTION.
299 (destructuring-bind (fun-name) oarg
301 (cond ((not (eql pf 0))
302 `((def!method print-object ((,x ,name) ,s)
303 (funcall #',(farg pf)
306 *current-level-in-print*))))
308 `((def!method print-object ((,x ,name) ,s)
309 (funcall #',(farg po) ,x ,s))))
311 ,@(let ((pure (dd-pure defstruct)))
313 `((setf (layout-pure (classoid-layout
314 (find-classoid ',name)))
316 ((eq pure :substructure)
317 `((setf (layout-pure (classoid-layout
318 (find-classoid ',name)))
320 ,@(let ((def-con (dd-default-constructor defstruct)))
321 (when (and def-con (not (dd-alternate-metaclass defstruct)))
322 `((setf (structure-classoid-constructor (find-classoid ',name))
325 ;;; shared logic for CL:DEFSTRUCT and SB!XC:DEFSTRUCT
326 (defmacro !expander-for-defstruct (name-and-options
328 expanding-into-code-for-xc-host-p)
329 `(let ((name-and-options ,name-and-options)
330 (slot-descriptions ,slot-descriptions)
331 (expanding-into-code-for-xc-host-p
332 ,expanding-into-code-for-xc-host-p))
333 (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
338 (let ((inherits (inherits-for-structure dd)))
340 ;; Note we intentionally enforce package locks and
341 ;; call %DEFSTRUCT first, and especially before
342 ;; %COMPILER-DEFSTRUCT. %DEFSTRUCT has the tests (and
343 ;; resulting CERROR) for collisions with LAYOUTs which
344 ;; already exist in the runtime. If there are any
345 ;; collisions, we want the user's response to CERROR
346 ;; to control what happens. Especially, if the user
347 ;; responds to the collision with ABORT, we don't want
348 ;; %COMPILER-DEFSTRUCT to modify the definition of the
350 (with-single-package-locked-error
351 (:symbol ',name "defining ~A as a structure"))
352 (%defstruct ',dd ',inherits)
353 (eval-when (:compile-toplevel :load-toplevel :execute)
354 (%compiler-defstruct ',dd ',inherits))
355 ,@(unless expanding-into-code-for-xc-host-p
356 (append ;; FIXME: We've inherited from CMU CL nonparallel
357 ;; code for creating copiers for typed and untyped
358 ;; structures. This should be fixed.
359 ;(copier-definition dd)
360 (constructor-definitions dd)
361 (class-method-definitions dd)))
364 (with-single-package-locked-error
365 (:symbol ',name "defining ~A as a structure"))
366 (eval-when (:compile-toplevel :load-toplevel :execute)
367 (setf (info :typed-structure :info ',name) ',dd))
368 ,@(unless expanding-into-code-for-xc-host-p
369 (append (typed-accessor-definitions dd)
370 (typed-predicate-definitions dd)
371 (typed-copier-definitions dd)
372 (constructor-definitions dd)))
375 (sb!xc:defmacro defstruct (name-and-options &rest slot-descriptions)
377 "DEFSTRUCT {Name | (Name Option*)} {Slot | (Slot [Default] {Key Value}*)}
378 Define the structure type Name. Instances are created by MAKE-<name>,
379 which takes &KEY arguments allowing initial slot values to the specified.
380 A SETF'able function <name>-<slot> is defined for each slot to read and
381 write slot values. <name>-p is a type predicate.
383 Popular DEFSTRUCT options (see manual for others):
387 Specify the name for the constructor or predicate.
389 (:CONSTRUCTOR Name Lambda-List)
390 Specify the name and arguments for a BOA constructor
391 (which is more efficient when keyword syntax isn't necessary.)
393 (:INCLUDE Supertype Slot-Spec*)
394 Make this type a subtype of the structure type Supertype. The optional
395 Slot-Specs override inherited slot options.
400 Asserts that the value of this slot is always of the specified type.
403 If true, no setter function is defined for this slot."
404 (!expander-for-defstruct name-and-options slot-descriptions nil))
406 (defmacro sb!xc:defstruct (name-and-options &rest slot-descriptions)
408 "Cause information about a target structure to be built into the
410 (!expander-for-defstruct name-and-options slot-descriptions t))
412 ;;;; functions to generate code for various parts of DEFSTRUCT definitions
414 ;;; First, a helper to determine whether a name names an inherited
416 (defun accessor-inherited-data (name defstruct)
417 (assoc name (dd-inherited-accessor-alist defstruct) :test #'eq))
419 ;;; Return a list of forms which create a predicate function for a
421 (defun typed-predicate-definitions (defstruct)
422 (let ((name (dd-name defstruct))
423 (predicate-name (dd-predicate-name defstruct))
425 (when (and predicate-name (dd-named defstruct))
426 (let ((ltype (dd-lisp-type defstruct))
427 (name-index (cdr (car (last (find-name-indices defstruct))))))
428 `((defun ,predicate-name (,argname)
429 (and (typep ,argname ',ltype)
431 ((subtypep ltype 'list)
432 `(do ((head (the ,ltype ,argname) (cdr head))
434 ((or (not (consp head)) (= i ,name-index))
435 (and (consp head) (eq ',name (car head))))))
436 ((subtypep ltype 'vector)
437 `(and (= (length (the ,ltype ,argname))
438 ,(dd-length defstruct))
439 (eq ',name (aref (the ,ltype ,argname) ,name-index))))
440 (t (bug "Uncatered-for lisp type in typed DEFSTRUCT: ~S."
443 ;;; Return a list of forms to create a copier function of a typed DEFSTRUCT.
444 (defun typed-copier-definitions (defstruct)
445 (when (dd-copier-name defstruct)
446 `((setf (fdefinition ',(dd-copier-name defstruct)) #'copy-seq)
447 (declaim (ftype function ,(dd-copier-name defstruct))))))
449 ;;; Return a list of function definitions for accessing and setting
450 ;;; the slots of a typed DEFSTRUCT. The functions are proclaimed to be
451 ;;; inline, and the types of their arguments and results are declared
452 ;;; as well. We count on the compiler to do clever things with ELT.
453 (defun typed-accessor-definitions (defstruct)
455 (let ((ltype (dd-lisp-type defstruct)))
456 (dolist (slot (dd-slots defstruct))
457 (let ((name (dsd-accessor-name slot))
458 (index (dsd-index slot))
459 (slot-type `(and ,(dsd-type slot)
460 ,(dd-element-type defstruct))))
461 (let ((inherited (accessor-inherited-data name defstruct)))
464 (stuff `(declaim (inline ,name (setf ,name))))
465 ;; FIXME: The arguments in the next two DEFUNs should
466 ;; be gensyms. (Otherwise e.g. if NEW-VALUE happened to
467 ;; be the name of a special variable, things could get
469 (stuff `(defun ,name (structure)
470 (declare (type ,ltype structure))
471 (the ,slot-type (elt structure ,index))))
472 (unless (dsd-read-only slot)
474 `(defun (setf ,name) (new-value structure)
475 (declare (type ,ltype structure) (type ,slot-type new-value))
476 (setf (elt structure ,index) new-value)))))
477 ((not (= (cdr inherited) index))
478 (style-warn "~@<Non-overwritten accessor ~S does not access ~
479 slot with name ~S (accessing an inherited slot ~
480 instead).~:@>" name (dsd-name slot))))))))
485 (defun require-no-print-options-so-far (defstruct)
486 (unless (and (eql (dd-print-function defstruct) 0)
487 (eql (dd-print-object defstruct) 0))
488 (error "No more than one of the following options may be specified:
489 :PRINT-FUNCTION, :PRINT-OBJECT, :TYPE")))
491 ;;; Parse a single DEFSTRUCT option and store the results in DD.
492 (defun parse-1-dd-option (option dd)
493 (let ((args (rest option))
497 (destructuring-bind (&optional conc-name) args
498 (setf (dd-conc-name dd)
499 (if (symbolp conc-name)
501 (make-symbol (string conc-name))))))
503 (destructuring-bind (&optional (cname (symbolicate "MAKE-" name))
506 (push (cons cname stuff) (dd-constructors dd))))
508 (destructuring-bind (&optional (copier (symbolicate "COPY-" name)))
510 (setf (dd-copier-name dd) copier)))
512 (destructuring-bind (&optional (predicate-name (symbolicate name "-P")))
514 (setf (dd-predicate-name dd) predicate-name)))
516 (when (dd-include dd)
517 (error "more than one :INCLUDE option"))
518 (setf (dd-include dd) args))
520 (require-no-print-options-so-far dd)
521 (setf (dd-print-function dd)
522 (the (or symbol cons) args)))
524 (require-no-print-options-so-far dd)
525 (setf (dd-print-object dd)
526 (the (or symbol cons) args)))
528 (destructuring-bind (type) args
529 (cond ((member type '(list vector))
530 (setf (dd-element-type dd) t)
531 (setf (dd-type dd) type))
532 ((and (consp type) (eq (first type) 'vector))
533 (destructuring-bind (vector vtype) type
534 (declare (ignore vector))
535 (setf (dd-element-type dd) vtype)
536 (setf (dd-type dd) 'vector)))
538 (error "~S is a bad :TYPE for DEFSTRUCT." type)))))
540 (error "The DEFSTRUCT option :NAMED takes no arguments."))
542 (destructuring-bind (offset) args
543 (setf (dd-offset dd) offset)))
545 (destructuring-bind (fun) args
546 (setf (dd-pure dd) fun)))
547 (t (error "unknown DEFSTRUCT option:~% ~S" option)))))
549 ;;; Given name and options, return a DD holding that info.
550 (defun parse-defstruct-name-and-options (name-and-options)
551 (destructuring-bind (name &rest options) name-and-options
552 (aver name) ; A null name doesn't seem to make sense here.
553 (let ((dd (make-defstruct-description name)))
554 (dolist (option options)
555 (cond ((eq option :named)
556 (setf (dd-named dd) t))
558 (parse-1-dd-option option dd))
559 ((member option '(:conc-name :constructor :copier :predicate))
560 (parse-1-dd-option (list option) dd))
562 (error "unrecognized DEFSTRUCT option: ~S" option))))
567 (error ":OFFSET can't be specified unless :TYPE is specified."))
568 (unless (dd-include dd)
569 ;; FIXME: It'd be cleaner to treat no-:INCLUDE as defaulting
570 ;; to :INCLUDE STRUCTURE-OBJECT, and then let the general-case
571 ;; (INCF (DD-LENGTH DD) (DD-LENGTH included-DD)) logic take
572 ;; care of this. (Except that the :TYPE VECTOR and :TYPE
573 ;; LIST cases, with their :NAMED and un-:NAMED flavors,
574 ;; make that messy, alas.)
575 (incf (dd-length dd))))
577 (require-no-print-options-so-far dd)
579 (incf (dd-length dd)))
580 (let ((offset (dd-offset dd)))
581 (when offset (incf (dd-length dd) offset)))))
583 (when (dd-include dd)
584 (frob-dd-inclusion-stuff dd))
588 ;;; Given name and options and slot descriptions (and possibly doc
589 ;;; string at the head of slot descriptions) return a DD holding that
591 (defun parse-defstruct-name-and-options-and-slot-descriptions
592 (name-and-options slot-descriptions)
593 (let ((result (parse-defstruct-name-and-options (if (atom name-and-options)
594 (list name-and-options)
596 (when (stringp (car slot-descriptions))
597 (setf (dd-doc result) (pop slot-descriptions)))
598 (dolist (slot-description slot-descriptions)
599 (allocate-1-slot result (parse-1-dsd result slot-description)))
602 ;;;; stuff to parse slot descriptions
604 ;;; Parse a slot description for DEFSTRUCT, add it to the description
605 ;;; and return it. If supplied, SLOT is a pre-initialized DSD
606 ;;; that we modify to get the new slot. This is supplied when handling
608 (defun parse-1-dsd (defstruct spec &optional
609 (slot (make-defstruct-slot-description :name ""
612 (multiple-value-bind (name default default-p type type-p read-only ro-p)
615 (when (keywordp spec)
616 (style-warn "Keyword slot name indicates probable syntax ~
617 error in DEFSTRUCT: ~S."
623 &optional (default nil default-p)
624 &key (type nil type-p) (read-only nil ro-p))
628 (uncross type) type-p
630 (t (error 'simple-program-error
631 :format-control "in DEFSTRUCT, ~S is not a legal slot ~
633 :format-arguments (list spec))))
635 (when (find name (dd-slots defstruct)
637 :key (lambda (x) (symbol-name (dsd-name x))))
638 (error 'simple-program-error
639 :format-control "duplicate slot name ~S"
640 :format-arguments (list name)))
641 (setf (dsd-name slot) name)
642 (setf (dd-slots defstruct) (nconc (dd-slots defstruct) (list slot)))
644 (let ((accessor-name (if (dd-conc-name defstruct)
645 (symbolicate (dd-conc-name defstruct) name)
647 (predicate-name (dd-predicate-name defstruct)))
648 (setf (dsd-accessor-name slot) accessor-name)
649 (when (eql accessor-name predicate-name)
650 ;; Some adventurous soul has named a slot so that its accessor
651 ;; collides with the structure type predicate. ANSI doesn't
652 ;; specify what to do in this case. As of 2001-09-04, Martin
653 ;; Atzmueller reports that CLISP and Lispworks both give
654 ;; priority to the slot accessor, so that the predicate is
655 ;; overwritten. We might as well do the same (as well as
656 ;; signalling a warning).
658 "~@<The structure accessor name ~S is the same as the name of the ~
659 structure type predicate. ANSI doesn't specify what to do in ~
660 this case. We'll overwrite the type predicate with the slot ~
661 accessor, but you can't rely on this behavior, so it'd be wise to ~
662 remove the ambiguity in your code.~@:>"
664 (setf (dd-predicate-name defstruct) nil))
665 ;; FIXME: It would be good to check for name collisions here, but
668 ;;x(when (and (fboundp accessor-name)
669 ;;x (not (accessor-inherited-data accessor-name defstruct)))
670 ;;x (style-warn "redefining ~S in DEFSTRUCT" accessor-name)))
671 ;; which was done until sbcl-0.8.11.18 or so, is wrong: it causes
672 ;; a warning at MACROEXPAND time, when instead the warning should
673 ;; occur not just because the code was constructed, but because it
674 ;; is actually compiled or loaded.
678 (setf (dsd-default slot) default))
680 (setf (dsd-type slot)
681 (if (eq (dsd-type slot) t)
683 `(and ,(dsd-type slot) ,type))))
686 (setf (dsd-read-only slot) t)
687 (when (dsd-read-only slot)
688 (error "~@<The slot ~S is :READ-ONLY in superclass, and so must ~
689 be :READ-ONLY in subclass.~:@>"
693 ;;; When a value of type TYPE is stored in a structure, should it be
694 ;;; stored in a raw slot? Return (VALUES RAW? RAW-TYPE WORDS), where
695 ;;; RAW? is true if TYPE should be stored in a raw slot.
696 ;;; RAW-TYPE is the raw slot type, or NIL if no raw slot.
697 ;;; WORDS is the number of words in the raw slot, or NIL if no raw slot.
699 ;;; FIXME: This should use the data in *RAW-SLOT-DATA-LIST*.
700 (defun structure-raw-slot-type-and-size (type)
701 (cond ((and (sb!xc:subtypep type '(unsigned-byte 32))
702 (multiple-value-bind (fixnum? fixnum-certain?)
703 (sb!xc:subtypep type 'fixnum)
704 ;; (The extra test for FIXNUM-CERTAIN? here is
705 ;; intended for bootstrapping the system. In
706 ;; particular, in sbcl-0.6.2, we set up LAYOUT before
707 ;; FIXNUM is defined, and so could bogusly end up
708 ;; putting INDEX-typed values into raw slots if we
709 ;; didn't test FIXNUM-CERTAIN?.)
710 (and (not fixnum?) fixnum-certain?)))
711 (values t 'unsigned-byte 1))
712 ((sb!xc:subtypep type 'single-float)
713 (values t 'single-float 1))
714 ((sb!xc:subtypep type 'double-float)
715 (values t 'double-float 2))
717 ((sb!xc:subtypep type 'long-float)
718 (values t 'long-float #!+x86 3 #!+sparc 4))
719 ((sb!xc:subtypep type '(complex single-float))
720 (values t 'complex-single-float 2))
721 ((sb!xc:subtypep type '(complex double-float))
722 (values t 'complex-double-float 4))
724 ((sb!xc:subtypep type '(complex long-float))
725 (values t 'complex-long-float #!+x86 6 #!+sparc 8))
727 (values nil nil nil))))
729 ;;; Allocate storage for a DSD in DD. This is where we decide whether
730 ;;; a slot is raw or not. If raw, and we haven't allocated a raw-index
731 ;;; yet for the raw data vector, then do it. Raw objects are aligned
732 ;;; on the unit of their size.
733 (defun allocate-1-slot (dd dsd)
734 (multiple-value-bind (raw? raw-type words)
735 (if (eq (dd-type dd) 'structure)
736 (structure-raw-slot-type-and-size (dsd-type dsd))
737 (values nil nil nil))
739 (setf (dsd-index dsd) (dd-length dd))
740 (incf (dd-length dd)))
742 (unless (dd-raw-index dd)
743 (setf (dd-raw-index dd) (dd-length dd))
744 (incf (dd-length dd)))
745 (let ((off (rem (dd-raw-length dd) words)))
747 (incf (dd-raw-length dd) (- words off))))
748 (setf (dsd-raw-type dsd) raw-type)
749 (setf (dsd-index dsd) (dd-raw-length dd))
750 (incf (dd-raw-length dd) words))))
753 (defun typed-structure-info-or-lose (name)
754 (or (info :typed-structure :info name)
755 (error ":TYPE'd DEFSTRUCT ~S not found for inclusion." name)))
757 ;;; Process any included slots pretty much like they were specified.
758 ;;; Also inherit various other attributes.
759 (defun frob-dd-inclusion-stuff (dd)
760 (destructuring-bind (included-name &rest modified-slots) (dd-include dd)
761 (let* ((type (dd-type dd))
764 (layout-info (compiler-layout-or-lose included-name))
765 (typed-structure-info-or-lose included-name))))
767 ;; checks on legality
768 (unless (and (eq type (dd-type included-structure))
769 (type= (specifier-type (dd-element-type included-structure))
770 (specifier-type (dd-element-type dd))))
771 (error ":TYPE option mismatch between structures ~S and ~S"
772 (dd-name dd) included-name))
773 (let ((included-classoid (find-classoid included-name nil)))
774 (when included-classoid
775 ;; It's not particularly well-defined to :INCLUDE any of the
776 ;; CMU CL INSTANCE weirdosities like CONDITION or
777 ;; GENERIC-FUNCTION, and it's certainly not ANSI-compliant.
778 (let* ((included-layout (classoid-layout included-classoid))
779 (included-dd (layout-info included-layout)))
780 (when (and (dd-alternate-metaclass included-dd)
781 ;; As of sbcl-0.pre7.73, anyway, STRUCTURE-OBJECT
782 ;; is represented with an ALTERNATE-METACLASS. But
783 ;; it's specifically OK to :INCLUDE (and PCL does)
784 ;; so in this one case, it's OK to include
785 ;; something with :ALTERNATE-METACLASS after all.
786 (not (eql included-name 'structure-object)))
787 (error "can't :INCLUDE class ~S (has alternate metaclass)"
790 (incf (dd-length dd) (dd-length included-structure))
791 (when (dd-class-p dd)
792 (let ((mc (rest (dd-alternate-metaclass included-structure))))
793 (when (and mc (not (dd-alternate-metaclass dd)))
794 (setf (dd-alternate-metaclass dd)
795 (cons included-name mc))))
796 (when (eq (dd-pure dd) :unspecified)
797 (setf (dd-pure dd) (dd-pure included-structure)))
798 (setf (dd-raw-index dd) (dd-raw-index included-structure))
799 (setf (dd-raw-length dd) (dd-raw-length included-structure)))
801 (setf (dd-inherited-accessor-alist dd)
802 (dd-inherited-accessor-alist included-structure))
803 (dolist (included-slot (dd-slots included-structure))
804 (let* ((included-name (dsd-name included-slot))
805 (modified (or (find included-name modified-slots
806 :key (lambda (x) (if (atom x) x (car x)))
809 ;; We stash away an alist of accessors to parents' slots
810 ;; that have already been created to avoid conflicts later
811 ;; so that structures with :INCLUDE and :CONC-NAME (and
812 ;; other edge cases) can work as specified.
813 (when (dsd-accessor-name included-slot)
814 ;; the "oldest" (i.e. highest up the tree of inheritance)
815 ;; will prevail, so don't push new ones on if they
817 (pushnew (cons (dsd-accessor-name included-slot)
818 (dsd-index included-slot))
819 (dd-inherited-accessor-alist dd)
820 :test #'eq :key #'car))
821 (let ((new-slot (parse-1-dsd dd
823 (copy-structure included-slot))))
824 (when (and (neq (dsd-type new-slot) (dsd-type included-slot))
825 (not (subtypep (dsd-type included-slot)
826 (dsd-type new-slot)))
827 (dsd-safe-p included-slot))
828 (setf (dsd-safe-p new-slot) nil)
832 ;;;; various helper functions for setting up DEFSTRUCTs
834 ;;; This function is called at macroexpand time to compute the INHERITS
835 ;;; vector for a structure type definition.
836 (defun inherits-for-structure (info)
837 (declare (type defstruct-description info))
838 (let* ((include (dd-include info))
839 (superclass-opt (dd-alternate-metaclass info))
842 (compiler-layout-or-lose (first include))
843 (classoid-layout (find-classoid
844 (or (first superclass-opt)
845 'structure-object))))))
846 (if (eq (dd-name info) 'ansi-stream)
847 ;; a hack to add the CL:STREAM class as a mixin for ANSI-STREAMs
848 (concatenate 'simple-vector
849 (layout-inherits super)
851 (classoid-layout (find-classoid 'stream))))
852 (concatenate 'simple-vector
853 (layout-inherits super)
856 ;;; Do miscellaneous (LOAD EVAL) time actions for the structure
857 ;;; described by DD. Create the class and LAYOUT, checking for
858 ;;; incompatible redefinition. Define those functions which are
859 ;;; sufficiently stereotyped that we can implement them as standard
861 (defun %defstruct (dd inherits)
862 (declare (type defstruct-description dd))
864 ;; We set up LAYOUTs even in the cross-compilation host.
865 (multiple-value-bind (classoid layout old-layout)
866 (ensure-structure-class dd inherits "current" "new")
867 (cond ((not old-layout)
868 (unless (eq (classoid-layout classoid) layout)
869 (register-layout layout)))
871 (let ((old-dd (layout-info old-layout)))
872 (when (defstruct-description-p old-dd)
873 (dolist (slot (dd-slots old-dd))
874 (fmakunbound (dsd-accessor-name slot))
875 (unless (dsd-read-only slot)
876 (fmakunbound `(setf ,(dsd-accessor-name slot)))))))
877 (%redefine-defstruct classoid old-layout layout)
878 (setq layout (classoid-layout classoid))))
879 (setf (find-classoid (dd-name dd)) classoid)
881 ;; Various other operations only make sense on the target SBCL.
883 (%target-defstruct dd layout))
887 ;;; Return a form describing the writable place used for this slot
888 ;;; in the instance named INSTANCE-NAME.
889 (defun %accessor-place-form (dd dsd instance-name)
890 (let (;; the operator that we'll use to access a typed slot or, in
891 ;; the case of a raw slot, to read the vector of raw slots
892 (ref (ecase (dd-type dd)
893 (structure '%instance-ref)
894 (list 'nth-but-with-sane-arg-order)
896 (raw-type (dsd-raw-type dsd)))
897 (if (eq raw-type t) ; if not raw slot
898 `(,ref ,instance-name ,(dsd-index dsd))
899 (let* ((raw-slot-data (find raw-type *raw-slot-data-list*
900 :key #'raw-slot-data-raw-type
902 (raw-slot-accessor (raw-slot-data-accessor-name raw-slot-data))
903 (raw-n-words (raw-slot-data-n-words raw-slot-data)))
904 (multiple-value-bind (scaled-dsd-index misalignment)
905 (floor (dsd-index dsd) raw-n-words)
906 (aver (zerop misalignment))
907 (let* ((raw-vector-bare-form
908 `(,ref ,instance-name ,(dd-raw-index dd)))
910 (if (eq raw-type 'unsigned-byte)
912 (aver (= raw-n-words 1))
913 (aver (eq raw-slot-accessor 'aref))
914 ;; FIXME: when the 64-bit world rolls
915 ;; around, this will need to be reviewed,
916 ;; along with the whole RAW-SLOT thing.
917 `(truly-the (simple-array (unsigned-byte 32) (*))
918 ,raw-vector-bare-form))
919 raw-vector-bare-form)))
920 `(,raw-slot-accessor ,raw-vector-form ,scaled-dsd-index)))))))
922 ;;; Return source transforms for the reader and writer functions of
923 ;;; the slot described by DSD. They should be inline expanded, but
924 ;;; source transforms work faster.
925 (defun slot-accessor-transforms (dd dsd)
926 (let ((accessor-place-form (%accessor-place-form dd dsd
927 `(the ,(dd-name dd) instance)))
928 (dsd-type (dsd-type dsd))
929 (value-the (if (dsd-safe-p dsd) 'truly-the 'the)))
930 (values (sb!c:source-transform-lambda (instance)
931 `(,value-the ,dsd-type ,(subst instance 'instance
932 accessor-place-form)))
933 (sb!c:source-transform-lambda (new-value instance)
934 (destructuring-bind (accessor-name &rest accessor-args)
936 `(,(info :setf :inverse accessor-name)
937 ,@(subst instance 'instance accessor-args)
938 (the ,dsd-type ,new-value)))))))
940 ;;; Return a LAMBDA form which can be used to set a slot.
941 (defun slot-setter-lambda-form (dd dsd)
942 `(lambda (new-value instance)
943 ,(funcall (nth-value 1 (slot-accessor-transforms dd dsd))
944 '(dummy new-value instance))))
946 ;;; core compile-time setup of any class with a LAYOUT, used even by
947 ;;; !DEFSTRUCT-WITH-ALTERNATE-METACLASS weirdosities
948 (defun %compiler-set-up-layout (dd
950 ;; Several special cases (STRUCTURE-OBJECT
951 ;; itself, and structures with alternate
952 ;; metaclasses) call this function directly,
953 ;; and they're all at the base of the
954 ;; instance class structure, so this is
956 (inherits (vector (find-layout t)
957 (find-layout 'instance))))
959 (multiple-value-bind (classoid layout old-layout)
960 (multiple-value-bind (clayout clayout-p)
961 (info :type :compiler-layout (dd-name dd))
962 (ensure-structure-class dd
964 (if clayout-p "previously compiled" "current")
966 :compiler-layout clayout))
968 (undefine-structure (layout-classoid old-layout))
969 (when (and (classoid-subclasses classoid)
970 (not (eq layout old-layout)))
972 (dohash (classoid layout (classoid-subclasses classoid))
973 (declare (ignore layout))
974 (undefine-structure classoid)
975 (subs (classoid-proper-name classoid)))
977 (warn "removing old subclasses of ~S:~% ~S"
978 (classoid-name classoid)
981 (unless (eq (classoid-layout classoid) layout)
982 (register-layout layout :invalidate nil))
983 (setf (find-classoid (dd-name dd)) classoid)))
985 ;; At this point the class should be set up in the INFO database.
986 ;; But the logic that enforces this is a little tangled and
987 ;; scattered, so it's not obvious, so let's check.
988 (aver (find-classoid (dd-name dd) nil))
990 (setf (info :type :compiler-layout (dd-name dd)) layout))
994 ;;; Do (COMPILE LOAD EVAL)-time actions for the normal (not
995 ;;; ALTERNATE-LAYOUT) DEFSTRUCT described by DD.
996 (defun %compiler-defstruct (dd inherits)
997 (declare (type defstruct-description dd))
999 (%compiler-set-up-layout dd inherits)
1001 (let* ((dtype (dd-declarable-type dd)))
1003 (let ((copier-name (dd-copier-name dd)))
1005 (sb!xc:proclaim `(ftype (sfunction (,dtype) ,dtype) ,copier-name))))
1007 (let ((predicate-name (dd-predicate-name dd)))
1008 (when predicate-name
1009 (sb!xc:proclaim `(ftype (sfunction (t) boolean) ,predicate-name))
1010 ;; Provide inline expansion (or not).
1012 ((structure funcallable-structure)
1013 ;; Let the predicate be inlined.
1014 (setf (info :function :inline-expansion-designator predicate-name)
1017 ;; This dead simple definition works because the
1018 ;; type system knows how to generate inline type
1019 ;; tests for instances.
1020 (typep x ',(dd-name dd))))
1021 (info :function :inlinep predicate-name)
1024 ;; Just punt. We could provide inline expansions for :TYPE
1025 ;; LIST and :TYPE VECTOR predicates too, but it'd be a
1026 ;; little messier and we don't bother. (Does anyway use
1027 ;; typed DEFSTRUCTs at all, let alone for high
1031 (dolist (dsd (dd-slots dd))
1032 (let* ((accessor-name (dsd-accessor-name dsd))
1033 (dsd-type (dsd-type dsd)))
1035 (let ((inherited (accessor-inherited-data accessor-name dd)))
1038 (multiple-value-bind (reader-designator writer-designator)
1039 (slot-accessor-transforms dd dsd)
1040 (sb!xc:proclaim `(ftype (sfunction (,dtype) ,dsd-type)
1042 (setf (info :function :source-transform accessor-name)
1044 (unless (dsd-read-only dsd)
1045 (let ((setf-accessor-name `(setf ,accessor-name)))
1047 `(ftype (sfunction (,dsd-type ,dtype) ,dsd-type)
1048 ,setf-accessor-name))
1049 (setf (info :function :source-transform setf-accessor-name)
1050 writer-designator)))))
1051 ((not (= (cdr inherited) (dsd-index dsd)))
1052 (style-warn "~@<Non-overwritten accessor ~S does not access ~
1053 slot with name ~S (accessing an inherited slot ~
1056 (dsd-name dsd)))))))))
1059 ;;;; redefinition stuff
1061 ;;; Compare the slots of OLD and NEW, returning 3 lists of slot names:
1062 ;;; 1. Slots which have moved,
1063 ;;; 2. Slots whose type has changed,
1064 ;;; 3. Deleted slots.
1065 (defun compare-slots (old new)
1066 (let* ((oslots (dd-slots old))
1067 (nslots (dd-slots new))
1068 (onames (mapcar #'dsd-name oslots))
1069 (nnames (mapcar #'dsd-name nslots)))
1072 (dolist (name (intersection onames nnames))
1073 (let ((os (find name oslots :key #'dsd-name :test #'string=))
1074 (ns (find name nslots :key #'dsd-name :test #'string=)))
1075 (unless (sb!xc:subtypep (dsd-type ns) (dsd-type os))
1077 (unless (and (= (dsd-index os) (dsd-index ns))
1078 (eq (dsd-raw-type os) (dsd-raw-type ns)))
1082 (set-difference onames nnames :test #'string=)))))
1084 ;;; If we are redefining a structure with different slots than in the
1085 ;;; currently loaded version, give a warning and return true.
1086 (defun redefine-structure-warning (classoid old new)
1087 (declare (type defstruct-description old new)
1088 (type classoid classoid)
1090 (let ((name (dd-name new)))
1091 (multiple-value-bind (moved retyped deleted) (compare-slots old new)
1092 (when (or moved retyped deleted)
1094 "incompatibly redefining slots of structure class ~S~@
1095 Make sure any uses of affected accessors are recompiled:~@
1096 ~@[ These slots were moved to new positions:~% ~S~%~]~
1097 ~@[ These slots have new incompatible types:~% ~S~%~]~
1098 ~@[ These slots were deleted:~% ~S~%~]"
1099 name moved retyped deleted)
1102 ;;; This function is called when we are incompatibly redefining a
1103 ;;; structure CLASS to have the specified NEW-LAYOUT. We signal an
1104 ;;; error with some proceed options and return the layout that should
1106 (defun %redefine-defstruct (classoid old-layout new-layout)
1107 (declare (type classoid classoid)
1108 (type layout old-layout new-layout))
1109 (let ((name (classoid-proper-name classoid)))
1111 (error "~@<attempt to redefine the ~S class ~S incompatibly with the current definition~:@>"
1117 "~@<Use the new definition of ~S, invalidating ~
1118 already-loaded code and instances.~@:>"
1120 (register-layout new-layout))
1121 (recklessly-continue ()
1124 "~@<Use the new definition of ~S as if it were ~
1125 compatible, allowing old accessors to use new ~
1126 instances and allowing new accessors to use old ~
1129 ;; classic CMU CL warning: "Any old ~S instances will be in a bad way.
1130 ;; I hope you know what you're doing..."
1131 (register-layout new-layout
1133 :destruct-layout old-layout))
1135 ;; FIXME: deprecated 2002-10-16, and since it's only interactive
1136 ;; hackery instead of a supported feature, can probably be deleted
1138 :report "(deprecated synonym for RECKLESSLY-CONTINUE)"
1139 (register-layout new-layout
1141 :destruct-layout old-layout))))
1144 ;;; This is called when we are about to define a structure class. It
1145 ;;; returns a (possibly new) class object and the layout which should
1146 ;;; be used for the new definition (may be the current layout, and
1147 ;;; also might be an uninstalled forward referenced layout.) The third
1148 ;;; value is true if this is an incompatible redefinition, in which
1149 ;;; case it is the old layout.
1150 (defun ensure-structure-class (info inherits old-context new-context
1151 &key compiler-layout)
1152 (multiple-value-bind (class old-layout)
1156 (class 'structure-classoid)
1157 (constructor 'make-structure-classoid))
1158 (dd-alternate-metaclass info)
1159 (declare (ignore name))
1160 (insured-find-classoid (dd-name info)
1161 (if (eq class 'structure-classoid)
1163 (sb!xc:typep x 'structure-classoid))
1165 (sb!xc:typep x (find-classoid class))))
1166 (fdefinition constructor)))
1167 (setf (classoid-direct-superclasses class)
1168 (if (eq (dd-name info) 'ansi-stream)
1169 ;; a hack to add CL:STREAM as a superclass mixin to ANSI-STREAMs
1170 (list (layout-classoid (svref inherits (1- (length inherits))))
1171 (layout-classoid (svref inherits (- (length inherits) 2))))
1172 (list (layout-classoid
1173 (svref inherits (1- (length inherits)))))))
1174 (let ((new-layout (make-layout :classoid class
1176 :depthoid (length inherits)
1177 :length (dd-length info)
1179 (old-layout (or compiler-layout old-layout)))
1182 (values class new-layout nil))
1183 (;; This clause corresponds to an assertion in REDEFINE-LAYOUT-WARNING
1184 ;; of classic CMU CL. I moved it out to here because it was only
1185 ;; exercised in this code path anyway. -- WHN 19990510
1186 (not (eq (layout-classoid new-layout) (layout-classoid old-layout)))
1187 (error "shouldn't happen: weird state of OLD-LAYOUT?"))
1188 ((not *type-system-initialized*)
1189 (setf (layout-info old-layout) info)
1190 (values class old-layout nil))
1191 ((redefine-layout-warning old-context
1194 (layout-length new-layout)
1195 (layout-inherits new-layout)
1196 (layout-depthoid new-layout))
1197 (values class new-layout old-layout))
1199 (let ((old-info (layout-info old-layout)))
1201 ((or defstruct-description)
1202 (cond ((redefine-structure-warning class old-info info)
1203 (values class new-layout old-layout))
1205 (setf (layout-info old-layout) info)
1206 (values class old-layout nil))))
1208 (setf (layout-info old-layout) info)
1209 (values class old-layout nil))
1211 (error "shouldn't happen! strange thing in LAYOUT-INFO:~% ~S"
1213 (values class new-layout old-layout)))))))))
1215 ;;; Blow away all the compiler info for the structure CLASS. Iterate
1216 ;;; over this type, clearing the compiler structure type info, and
1217 ;;; undefining all the associated functions.
1218 (defun undefine-structure (class)
1219 (let ((info (layout-info (classoid-layout class))))
1220 (when (defstruct-description-p info)
1221 (let ((type (dd-name info)))
1222 (remhash type *typecheckfuns*)
1223 (setf (info :type :compiler-layout type) nil)
1224 (undefine-fun-name (dd-copier-name info))
1225 (undefine-fun-name (dd-predicate-name info))
1226 (dolist (slot (dd-slots info))
1227 (let ((fun (dsd-accessor-name slot)))
1228 (unless (accessor-inherited-data fun info)
1229 (undefine-fun-name fun)
1230 (unless (dsd-read-only slot)
1231 (undefine-fun-name `(setf ,fun)))))))
1232 ;; Clear out the SPECIFIER-TYPE cache so that subsequent
1233 ;; references are unknown types.
1234 (values-specifier-type-cache-clear)))
1237 ;;; Return a list of pairs (name . index). Used for :TYPE'd
1238 ;;; constructors to find all the names that we have to splice in &
1239 ;;; where. Note that these types don't have a layout, so we can't look
1240 ;;; at LAYOUT-INHERITS.
1241 (defun find-name-indices (defstruct)
1244 (do ((info defstruct
1245 (typed-structure-info-or-lose (first (dd-include info)))))
1246 ((not (dd-include info))
1251 (dolist (info infos)
1252 (incf i (or (dd-offset info) 0))
1253 (when (dd-named info)
1254 (res (cons (dd-name info) i)))
1255 (setq i (dd-length info)))))
1259 ;;; These functions are called to actually make a constructor after we
1260 ;;; have processed the arglist. The correct variant (according to the
1261 ;;; DD-TYPE) should be called. The function is defined with the
1262 ;;; specified name and arglist. VARS and TYPES are used for argument
1263 ;;; type declarations. VALUES are the values for the slots (in order.)
1265 ;;; This is split three ways because:
1266 ;;; * LIST & VECTOR structures need "name" symbols stuck in at
1267 ;;; various weird places, whereas STRUCTURE structures have
1269 ;;; * We really want to use LIST to make list structures, instead of
1270 ;;; MAKE-LIST/(SETF ELT). (We can't in general use VECTOR in an
1271 ;;; analogous way, since VECTOR makes a SIMPLE-VECTOR and vector-typed
1272 ;;; structures can have arbitrary subtypes of VECTOR, not necessarily
1274 ;;; * STRUCTURE structures can have raw slots that must also be
1275 ;;; allocated and indirectly referenced.
1276 (defun create-vector-constructor (dd cons-name arglist vars types values)
1277 (let ((temp (gensym))
1278 (etype (dd-element-type dd)))
1279 `(defun ,cons-name ,arglist
1280 (declare ,@(mapcar (lambda (var type) `(type (and ,type ,etype) ,var))
1282 (let ((,temp (make-array ,(dd-length dd)
1283 :element-type ',(dd-element-type dd))))
1284 ,@(mapcar (lambda (x)
1285 `(setf (aref ,temp ,(cdr x)) ',(car x)))
1286 (find-name-indices dd))
1287 ,@(mapcar (lambda (dsd value)
1288 (unless (eq value '.do-not-initialize-slot.)
1289 `(setf (aref ,temp ,(dsd-index dsd)) ,value)))
1290 (dd-slots dd) values)
1292 (defun create-list-constructor (dd cons-name arglist vars types values)
1293 (let ((vals (make-list (dd-length dd) :initial-element nil)))
1294 (dolist (x (find-name-indices dd))
1295 (setf (elt vals (cdr x)) `',(car x)))
1296 (loop for dsd in (dd-slots dd) and val in values do
1297 (setf (elt vals (dsd-index dsd))
1298 (if (eq val '.do-not-initialize-slot.) 0 val)))
1300 `(defun ,cons-name ,arglist
1301 (declare ,@(mapcar (lambda (var type) `(type ,type ,var)) vars types))
1303 (defun create-structure-constructor (dd cons-name arglist vars types values)
1304 (let* ((instance (gensym "INSTANCE"))
1305 (raw-index (dd-raw-index dd)))
1306 `(defun ,cons-name ,arglist
1307 (declare ,@(mapcar (lambda (var type) `(type ,type ,var))
1309 (let ((,instance (truly-the ,(dd-name dd)
1310 (%make-instance-with-layout
1311 (%delayed-get-compiler-layout ,(dd-name dd))))))
1313 `((setf (%instance-ref ,instance ,raw-index)
1314 (make-array ,(dd-raw-length dd)
1315 :element-type '(unsigned-byte 32)))))
1316 ,@(mapcar (lambda (dsd value)
1317 ;; (Note that we can't in general use the
1318 ;; ordinary named slot setter function here
1319 ;; because the slot might be :READ-ONLY, so we
1320 ;; whip up new LAMBDA representations of slot
1321 ;; setters for the occasion.)
1322 (unless (eq value '.do-not-initialize-slot.)
1323 `(,(slot-setter-lambda-form dd dsd) ,value ,instance)))
1328 ;;; Create a default (non-BOA) keyword constructor.
1329 (defun create-keyword-constructor (defstruct creator)
1330 (declare (type function creator))
1331 (collect ((arglist (list '&key))
1334 (dolist (slot (dd-slots defstruct))
1335 (let ((dum (gensym))
1336 (name (dsd-name slot)))
1337 (arglist `((,(keywordicate name) ,dum) ,(dsd-default slot)))
1338 (types (dsd-type slot))
1341 defstruct (dd-default-constructor defstruct)
1342 (arglist) (vals) (types) (vals))))
1344 ;;; Given a structure and a BOA constructor spec, call CREATOR with
1345 ;;; the appropriate args to make a constructor.
1346 (defun create-boa-constructor (defstruct boa creator)
1347 (declare (type function creator))
1348 (multiple-value-bind (req opt restp rest keyp keys allowp auxp aux)
1349 (parse-lambda-list (second boa))
1354 (labels ((get-slot (name)
1355 (let ((res (find name (dd-slots defstruct)
1359 (values (dsd-type res) (dsd-default res))
1362 (multiple-value-bind (type default) (get-slot arg)
1363 (arglist `(,arg ,default))
1369 (types (get-slot arg)))
1372 (arglist '&optional)
1376 ;; FIXME: this shares some logic (though not
1377 ;; code) with the &key case below (and it
1378 ;; looks confusing) -- factor out the logic
1379 ;; if possible. - CSR, 2002-04-19
1382 (def (nth-value 1 (get-slot name)))
1383 (supplied-test nil supplied-test-p))
1385 (arglist `(,name ,def ,@(if supplied-test-p `(,supplied-test) nil)))
1387 (types (get-slot name))))
1389 (do-default arg)))))
1392 (arglist '&rest rest)
1400 (destructuring-bind (wot
1403 (supplied-test nil supplied-test-p))
1405 (let ((name (if (consp wot)
1406 (destructuring-bind (key var) wot
1407 (declare (ignore key))
1410 (multiple-value-bind (type slot-def)
1412 (arglist `(,wot ,(if def-p def slot-def)
1413 ,@(if supplied-test-p `(,supplied-test) nil)))
1418 (when allowp (arglist '&allow-other-keys))
1424 (if (proper-list-of-length-p arg 2)
1425 (let ((var (first arg)))
1427 (types (get-slot var)))
1428 (skipped-vars (if (consp arg) (first arg) arg))))))
1430 (funcall creator defstruct (first boa)
1431 (arglist) (vars) (types)
1432 (loop for slot in (dd-slots defstruct)
1433 for name = (dsd-name slot)
1434 collect (cond ((find name (skipped-vars) :test #'string=)
1435 (setf (dsd-safe-p slot) nil)
1436 '.do-not-initialize-slot.)
1437 ((or (find (dsd-name slot) (vars) :test #'string=)
1438 (dsd-default slot)))))))))
1440 ;;; Grovel the constructor options, and decide what constructors (if
1442 (defun constructor-definitions (defstruct)
1443 (let ((no-constructors nil)
1446 (creator (ecase (dd-type defstruct)
1447 (structure #'create-structure-constructor)
1448 (vector #'create-vector-constructor)
1449 (list #'create-list-constructor))))
1450 (dolist (constructor (dd-constructors defstruct))
1451 (destructuring-bind (name &optional (boa-ll nil boa-p)) constructor
1452 (declare (ignore boa-ll))
1453 (cond ((not name) (setq no-constructors t))
1454 (boa-p (push constructor boas))
1455 (t (push name defaults)))))
1457 (when no-constructors
1458 (when (or defaults boas)
1459 (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs"))
1460 (return-from constructor-definitions ()))
1462 (unless (or defaults boas)
1463 (push (symbolicate "MAKE-" (dd-name defstruct)) defaults))
1465 (collect ((res) (names))
1467 (let ((cname (first defaults)))
1468 (setf (dd-default-constructor defstruct) cname)
1469 (res (create-keyword-constructor defstruct creator))
1471 (dolist (other-name (rest defaults))
1472 (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1473 (names other-name))))
1476 (res (create-boa-constructor defstruct boa creator))
1477 (names (first boa)))
1479 (res `(declaim (ftype
1481 ,(if (eq (dd-type defstruct) 'structure)
1488 ;;;; instances with ALTERNATE-METACLASS
1490 ;;;; The CMU CL support for structures with ALTERNATE-METACLASS was a
1491 ;;;; fairly general extension embedded in the main DEFSTRUCT code, and
1492 ;;;; the result was an fairly impressive mess as ALTERNATE-METACLASS
1493 ;;;; extension mixed with ANSI CL generality (e.g. :TYPE and :INCLUDE)
1494 ;;;; and CMU CL implementation hairiness (esp. raw slots). This SBCL
1495 ;;;; version is much less ambitious, noticing that ALTERNATE-METACLASS
1496 ;;;; is only used to implement CONDITION, STANDARD-INSTANCE, and
1497 ;;;; GENERIC-FUNCTION, and defining a simple specialized
1498 ;;;; separate-from-DEFSTRUCT macro to provide only enough
1499 ;;;; functionality to support those.
1501 ;;;; KLUDGE: The defining macro here is so specialized that it's ugly
1502 ;;;; in its own way. It also violates once-and-only-once by knowing
1503 ;;;; much about structures and layouts that is already known by the
1504 ;;;; main DEFSTRUCT macro. Hopefully it will go away presently
1505 ;;;; (perhaps when CL:CLASS and SB-PCL:CLASS meet) as per FIXME below.
1506 ;;;; -- WHN 2001-10-28
1508 ;;;; FIXME: There seems to be no good reason to shoehorn CONDITION,
1509 ;;;; STANDARD-INSTANCE, and GENERIC-FUNCTION into mutated structures
1510 ;;;; instead of just implementing them as primitive objects. (This
1511 ;;;; reduced-functionality macro seems pretty close to the
1512 ;;;; functionality of DEFINE-PRIMITIVE-OBJECT..)
1514 (defun make-dd-with-alternate-metaclass (&key (class-name (missing-arg))
1515 (superclass-name (missing-arg))
1516 (metaclass-name (missing-arg))
1517 (dd-type (missing-arg))
1518 metaclass-constructor
1520 (let* ((dd (make-defstruct-description class-name))
1521 (conc-name (concatenate 'string (symbol-name class-name) "-"))
1522 (dd-slots (let ((reversed-result nil)
1523 ;; The index starts at 1 for ordinary
1524 ;; named slots because slot 0 is
1525 ;; magical, used for LAYOUT in
1526 ;; CONDITIONs or for something (?) in
1527 ;; funcallable instances.
1529 (dolist (slot-name slot-names)
1530 (push (make-defstruct-slot-description
1533 :accessor-name (symbolicate conc-name slot-name))
1536 (nreverse reversed-result))))
1537 (setf (dd-alternate-metaclass dd) (list superclass-name
1539 metaclass-constructor)
1540 (dd-slots dd) dd-slots
1541 (dd-length dd) (1+ (length slot-names))
1542 (dd-type dd) dd-type)
1545 (sb!xc:defmacro !defstruct-with-alternate-metaclass
1547 (slot-names (missing-arg))
1548 (boa-constructor (missing-arg))
1549 (superclass-name (missing-arg))
1550 (metaclass-name (missing-arg))
1551 (metaclass-constructor (missing-arg))
1552 (dd-type (missing-arg))
1554 (runtime-type-checks-p t))
1556 (declare (type (and list (not null)) slot-names))
1557 (declare (type (and symbol (not null))
1561 metaclass-constructor))
1562 (declare (type symbol predicate))
1563 (declare (type (member structure funcallable-structure) dd-type))
1565 (let* ((dd (make-dd-with-alternate-metaclass
1566 :class-name class-name
1567 :slot-names slot-names
1568 :superclass-name superclass-name
1569 :metaclass-name metaclass-name
1570 :metaclass-constructor metaclass-constructor
1572 (dd-slots (dd-slots dd))
1573 (dd-length (1+ (length slot-names)))
1574 (object-gensym (gensym "OBJECT"))
1575 (new-value-gensym (gensym "NEW-VALUE-"))
1576 (delayed-layout-form `(%delayed-get-compiler-layout ,class-name)))
1577 (multiple-value-bind (raw-maker-form raw-reffer-operator)
1580 (values `(let ((,object-gensym (%make-instance ,dd-length)))
1581 (setf (%instance-layout ,object-gensym)
1582 ,delayed-layout-form)
1585 (funcallable-structure
1586 (values `(%make-funcallable-instance ,dd-length
1587 ,delayed-layout-form)
1588 '%funcallable-instance-info)))
1591 (eval-when (:compile-toplevel :load-toplevel :execute)
1592 (%compiler-set-up-layout ',dd))
1594 ;; slot readers and writers
1595 (declaim (inline ,@(mapcar #'dsd-accessor-name dd-slots)))
1596 ,@(mapcar (lambda (dsd)
1597 `(defun ,(dsd-accessor-name dsd) (,object-gensym)
1598 ,@(when runtime-type-checks-p
1599 `((declare (type ,class-name ,object-gensym))))
1600 (,raw-reffer-operator ,object-gensym
1603 (declaim (inline ,@(mapcar (lambda (dsd)
1604 `(setf ,(dsd-accessor-name dsd)))
1606 ,@(mapcar (lambda (dsd)
1607 `(defun (setf ,(dsd-accessor-name dsd)) (,new-value-gensym
1609 ,@(when runtime-type-checks-p
1610 `((declare (type ,class-name ,object-gensym))))
1611 (setf (,raw-reffer-operator ,object-gensym
1613 ,new-value-gensym)))
1617 (defun ,boa-constructor ,slot-names
1618 (let ((,object-gensym ,raw-maker-form))
1619 ,@(mapcar (lambda (slot-name)
1620 (let ((dsd (find (symbol-name slot-name) dd-slots
1622 (symbol-name (dsd-name x)))
1624 ;; KLUDGE: bug 117 bogowarning. Neither
1625 ;; DECLAREing the type nor TRULY-THE cut
1626 ;; the mustard -- it still gives warnings.
1627 (enforce-type dsd defstruct-slot-description)
1628 `(setf (,(dsd-accessor-name dsd) ,object-gensym)
1635 ;; Just delegate to the compiler's type optimization
1636 ;; code, which knows how to generate inline type tests
1637 ;; for the whole CMU CL INSTANCE menagerie.
1638 `(defun ,predicate (,object-gensym)
1639 (typep ,object-gensym ',class-name)))))))
1641 ;;;; finalizing bootstrapping
1643 ;;; Set up DD and LAYOUT for STRUCTURE-OBJECT class itself.
1645 ;;; Ordinary structure classes effectively :INCLUDE STRUCTURE-OBJECT
1646 ;;; when they have no explicit :INCLUDEs, so (1) it needs to be set up
1647 ;;; before we can define ordinary structure classes, and (2) it's
1648 ;;; special enough (and simple enough) that we just build it by hand
1649 ;;; instead of trying to generalize the ordinary DEFSTRUCT code.
1650 (defun !set-up-structure-object-class ()
1651 (let ((dd (make-defstruct-description 'structure-object)))
1653 ;; Note: This has an ALTERNATE-METACLASS only because of blind
1654 ;; clueless imitation of the CMU CL code -- dunno if or why it's
1656 (dd-alternate-metaclass dd) '(instance)
1659 (dd-type dd) 'structure)
1660 (%compiler-set-up-layout dd)))
1661 (!set-up-structure-object-class)
1663 ;;; early structure predeclarations: Set up DD and LAYOUT for ordinary
1664 ;;; (non-ALTERNATE-METACLASS) structures which are needed early.
1666 '#.(sb-cold:read-from-file
1667 "src/code/early-defstruct-args.lisp-expr"))
1668 (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
1671 (inherits (inherits-for-structure dd)))
1672 (%compiler-defstruct dd inherits)))
1674 (/show0 "code/defstruct.lisp end of file")