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 #+sb-xc (/show0 "entering COMPILER-LAYOUT-OR-LOSE")
23 (let ((res (info :type :compiler-layout name)))
25 (error "Class is not yet defined or was undefined: ~S" name))
26 ((not (typep (layout-info res) 'defstruct-description))
27 (error "Class is not a structure class: ~S" name))
30 ;;; Delay looking for compiler-layout until the constructor is being
31 ;;; compiled, since it doesn't exist until after the EVAL-WHEN (COMPILE)
32 ;;; stuff is compiled.
33 (sb!xc:defmacro %delayed-get-compiler-layout (name)
34 `',(compiler-layout-or-lose name))
36 ;;; Get layout right away.
37 (sb!xc:defmacro compile-time-find-layout (name)
40 ;;; re. %DELAYED-GET-COMPILER-LAYOUT and COMPILE-TIME-FIND-LAYOUT, above..
42 ;;; FIXME: Perhaps both should be defined with DEFMACRO-MUNDANELY?
43 ;;; FIXME: Do we really need both? If so, their names and implementations
44 ;;; should probably be tweaked to be more parallel.
46 ;;;; DEFSTRUCT-DESCRIPTION
48 ;;; The DEFSTRUCT-DESCRIPTION structure holds compile-time information
49 ;;; about a structure type.
50 (def!struct (defstruct-description
52 (:make-load-form-fun just-dump-it-normally)
53 #-sb-xc-host (:pure t)
54 (:constructor make-defstruct-description (name)))
55 ;; name of the structure
56 (name (missing-arg) :type symbol)
57 ;; documentation on the structure
58 (doc nil :type (or string null))
59 ;; prefix for slot names. If NIL, none.
60 (conc-name (symbolicate name "-") :type (or symbol null))
61 ;; the name of the primary standard keyword constructor, or NIL if none
62 (default-constructor nil :type (or symbol null))
63 ;; all the explicit :CONSTRUCTOR specs, with name defaulted
64 (constructors () :type list)
65 ;; name of copying function
66 (copier-name (symbolicate "COPY-" name) :type (or symbol null))
67 ;; name of type predicate
68 (predicate-name (symbolicate name "-P") :type (or symbol null))
69 ;; the arguments to the :INCLUDE option, or NIL if no included
71 (include nil :type list)
72 ;; properties used to define structure-like classes with an
73 ;; arbitrary superclass and that may not have STRUCTURE-CLASS as the
74 ;; metaclass. Syntax is:
75 ;; (superclass-name metaclass-name metaclass-constructor)
76 (alternate-metaclass nil :type list)
77 ;; a list of DEFSTRUCT-SLOT-DESCRIPTION objects for all slots
78 ;; (including included ones)
80 ;; number of elements we've allocated (See also RAW-LENGTH.)
81 (length 0 :type index)
82 ;; General kind of implementation.
83 (type 'structure :type (member structure vector list
84 funcallable-structure))
86 ;; The next three slots are for :TYPE'd structures (which aren't
87 ;; classes, DD-CLASS-P = NIL)
89 ;; vector element type
91 ;; T if :NAMED was explicitly specified, NIL otherwise
92 (named nil :type boolean)
93 ;; any INITIAL-OFFSET option on this direct type
94 (offset nil :type (or index null))
96 ;; the argument to the PRINT-FUNCTION option, or NIL if a
97 ;; PRINT-FUNCTION option was given with no argument, or 0 if no
98 ;; PRINT-FUNCTION option was given
99 (print-function 0 :type (or cons symbol (member 0)))
100 ;; the argument to the PRINT-OBJECT option, or NIL if a PRINT-OBJECT
101 ;; option was given with no argument, or 0 if no PRINT-OBJECT option
103 (print-object 0 :type (or cons symbol (member 0)))
104 ;; the index of the raw data vector and the number of words in it,
105 ;; or NIL and 0 if not allocated (either because this structure
106 ;; has no raw slots, or because we're still parsing it and haven't
107 ;; run across any raw slots yet)
108 (raw-index nil :type (or index null))
109 (raw-length 0 :type index)
110 ;; the value of the :PURE option, or :UNSPECIFIED. This is only
111 ;; meaningful if DD-CLASS-P = T.
112 (pure :unspecified :type (member t nil :substructure :unspecified)))
113 (def!method print-object ((x defstruct-description) stream)
114 (print-unreadable-object (x stream :type t)
115 (prin1 (dd-name x) stream)))
117 ;;; Does DD describe a structure with a class?
118 (defun dd-class-p (dd)
120 '(structure funcallable-structure)))
122 ;;; a type name which can be used when declaring things which operate
123 ;;; on structure instances
124 (defun dd-declarable-type (dd)
126 ;; Native classes are known to the type system, and we can
127 ;; declare them as types.
129 ;; Structures layered on :TYPE LIST or :TYPE VECTOR aren't part
130 ;; of the type system, so all we can declare is the underlying
131 ;; LIST or VECTOR type.
134 (defun dd-layout-or-lose (dd)
135 (compiler-layout-or-lose (dd-name dd)))
137 ;;;; DEFSTRUCT-SLOT-DESCRIPTION
139 ;;; A DEFSTRUCT-SLOT-DESCRIPTION holds compile-time information about
140 ;;; a structure slot.
141 (def!struct (defstruct-slot-description
142 (:make-load-form-fun just-dump-it-normally)
145 #-sb-xc-host (:pure t))
146 ;; string name of slot
148 ;; its position in the implementation sequence
149 (index (missing-arg) :type fixnum)
150 ;; the name of the accessor function
152 ;; (CMU CL had extra complexity here ("..or NIL if this accessor has
153 ;; the same name as an inherited accessor (which we don't want to
154 ;; shadow)") but that behavior doesn't seem to be specified by (or
155 ;; even particularly consistent with) ANSI, so it's gone in SBCL.)
157 default ; default value expression
158 (type t) ; declared type specifier
159 ;; If this object does not describe a raw slot, this value is T.
161 ;; If this object describes a raw slot, this value is the type of the
162 ;; value that the raw slot holds. Mostly. (KLUDGE: If the raw slot has
163 ;; type (UNSIGNED-BYTE 32), the value here is UNSIGNED-BYTE, not
164 ;; (UNSIGNED-BYTE 32).)
165 (raw-type t :type (member t single-float double-float
166 #!+long-float long-float
167 complex-single-float complex-double-float
168 #!+long-float complex-long-float
170 (read-only nil :type (member t nil)))
171 (def!method print-object ((x defstruct-slot-description) stream)
172 (print-unreadable-object (x stream :type t)
173 (prin1 (dsd-name x) stream)))
175 ;;; Return the name of a defstruct slot as a symbol. We store it as a
176 ;;; string to avoid creating lots of worthless symbols at load time.
177 (defun dsd-name (dsd)
178 (intern (string (dsd-%name dsd))
179 (if (dsd-accessor-name dsd)
180 (symbol-package (dsd-accessor-name dsd))
183 ;;;; typed (non-class) structures
185 ;;; Return a type specifier we can use for testing :TYPE'd structures.
186 (defun dd-lisp-type (defstruct)
187 (ecase (dd-type defstruct)
189 (vector `(simple-array ,(dd-element-type defstruct) (*)))))
191 ;;;; shared machinery for inline and out-of-line slot accessor functions
193 ;;; an alist mapping from raw slot type to the operator used to access
196 ;;; FIXME: should be shared
197 (eval-when (:compile-toplevel :load-toplevel :execute)
198 (defvar *raw-type->rawref-fun-name*
199 '(;; The compiler thinks that the raw data vector is a vector of
200 ;; unsigned bytes, so if the slot we want to access actually *is*
201 ;; an unsigned byte, it'll access the slot for us even if we don't
203 (unsigned-byte . aref)
204 ;; "A lie can travel halfway round the world while the truth is
205 ;; putting on its shoes." -- Mark Twain
206 (single-float . %raw-ref-single)
207 (double-float . %raw-ref-double)
208 #!+long-float (long-float . %raw-ref-long)
209 (complex-single-float . %raw-ref-complex-single)
210 (complex-double-float . %raw-ref-complex-double)
211 #!+long-float (complex-long-float . %raw-ref-complex-long))))
213 ;;;; generating out-of-line slot accessor functions
215 ;;; FIXME: Ideally, the presence of the type checks in the functions
216 ;;; here would be conditional on the optimization policy at the point
217 ;;; of expansion of DEFSTRUCT. (For now we're just doing the simpler
218 ;;; thing, putting in the type checks unconditionally.)
220 ;;; Return (VALUES SLOT-READER-FUN SLOT-WRITER-FUN).
221 (defun slot-accessor-funs (dd dsd)
223 #+sb-xc (/show0 "entering SLOT-ACCESSOR-FUNS")
225 ;; various code generators
227 ;; Note: They're only minimally parameterized, and cavalierly grab
228 ;; things like INSTANCE and DSD-INDEX from the namespace they're
230 (macrolet (;; code shared between funcallable instance case and the
231 ;; ordinary STRUCTURE-OBJECT case: Handle native
232 ;; structures with LAYOUTs and (possibly) raw slots.
233 (%native-slot-accessor-funs (dd-ref-fun-name)
234 (let ((instance-type-check-form
235 '(%check-structure-type-from-layout instance layout)))
236 (/show "macroexpanding %NATIVE-SLOT-ACCESSOR-FUNS" dd-ref-fun-name instance-type-check-form)
237 `(let ((layout (dd-layout-or-lose dd))
238 (dsd-raw-type (dsd-raw-type dsd)))
239 #+sb-xc (/show0 "in %NATIVE-SLOT-ACCESSOR-FUNS macroexpanded code")
240 ;; Map over all the possible RAW-TYPEs, compiling
241 ;; a different closure-function for each one, so
242 ;; that once the COND over RAW-TYPEs happens (at
243 ;; the time closure is allocated) there are no
244 ;; more decisions to be made and things execute
245 ;; reasonably efficiently.
248 ((eql dsd-raw-type t)
249 #+sb-xc (/show0 "in nonraw slot case")
250 (%slotplace-accessor-funs
251 (,dd-ref-fun-name instance dsd-index)
252 ,instance-type-check-form))
254 ,@(mapcar (lambda (raw-type-and-rawref-fun-name)
255 (destructuring-bind (raw-type
257 raw-type-and-rawref-fun-name
258 `((equal dsd-raw-type ',raw-type)
259 #+sb-xc (/show0 "in raw slot case")
260 (let ((raw-index (dd-raw-index dd)))
261 (%slotplace-accessor-funs
262 (,rawref-fun-name (,dd-ref-fun-name
266 ,instance-type-check-form)))))
267 *raw-type->rawref-fun-name*)
270 (error "internal error: unexpected DSD-RAW-TYPE ~S"
272 ;; code shared between DEFSTRUCT :TYPE LIST and
273 ;; DEFSTRUCT :TYPE VECTOR cases: Handle the "typed
274 ;; structure" case, with no LAYOUTs and no raw slots.
275 (%colontyped-slot-accessor-funs () (error "stub"))
276 ;; the common structure of the raw-slot and not-raw-slot
277 ;; cases, defined in terms of the writable SLOTPLACE. All
278 ;; possible flavors of slot access should be able to pass
280 (%slotplace-accessor-funs (slotplace instance-type-check-form)
281 (/show "macroexpanding %SLOTPLACE-ACCESSOR-FUNS" slotplace instance-type-check-form)
282 `(values (lambda (instance)
283 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined reader")
284 ,instance-type-check-form
285 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
287 (let ((typecheckfun (typespec-typecheckfun dsd-type)))
288 (lambda (new-value instance)
289 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined writer")
290 ,instance-type-check-form
291 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
292 (funcall typecheckfun new-value)
293 (/noshow0 "back from TYPECHECKFUN")
294 (setf ,slotplace new-value))))))
296 (let ((dsd-index (dsd-index dsd))
297 (dsd-type (dsd-type dsd)))
299 #+sb-xc (/show0 "got DSD-TYPE=..")
300 #+sb-xc (/hexstr dsd-type)
305 #+sb-xc (/show0 "case of DSD-TYPE = STRUCTURE")
306 (%native-slot-accessor-funs %instance-ref))
308 ;; structures with the :TYPE option
310 ;; FIXME: Worry about these later..
312 ;; In :TYPE LIST and :TYPE VECTOR structures, ANSI specifies the
313 ;; layout completely, so that raw slots are impossible.
315 (dd-type-slot-accessor-funs nth-but-with-sane-arg-order
316 `(%check-structure-type-from-dd
319 (dd-type-slot-accessor-funs aref
324 ;;;; baby steps for the new out-of-line slot accessor functions
326 ;;;; REMOVEME after new structure code works
329 (in-package :sb-kernel)
334 (b 5 :type package :read-only t)
336 (x 5 :type (unsigned-byte 32))
337 (y 5.0 :type single-float :read-only t))
339 (load "/usr/stuff/sbcl/src/cold/chill")
340 (cl-user:fasl "/usr/stuff/sbcl/src/code/typecheckfuns")
341 (cl-user:fasl "/usr/stuff/outsacc")
343 (let* ((foo-layout (compiler-layout-or-lose 'foo))
344 (foo-dd (layout-info foo-layout))
345 (foo-dsds (dd-slots foo-dd))
346 (foo-a-dsd (find "A" foo-dsds :test #'string= :key #'dsd-%name))
347 (foo-b-dsd (find "B" foo-dsds :test #'string= :key #'dsd-%name))
348 (foo-x-dsd (find "X" foo-dsds :test #'string= :key #'dsd-%name))
349 (foo-y-dsd (find "X" foo-dsds :test #'string= :key #'dsd-%name))
350 (foo (make-foo :a 'avalue
351 :b (find-package :cl)
353 (declare (type layout foo-layout))
354 (declare (type defstruct-description foo-dd))
355 (declare (type defstruct-slot-description foo-a-dsd))
359 (multiple-value-bind (foo-a-reader foo-a-writer)
360 (slot-accessor-funs foo-dd foo-a-dsd)
362 ;; basic functionality
363 (cl-user:/show foo-a-reader)
364 (cl-user:/show (funcall foo-a-reader foo))
365 (aver (eql (funcall foo-a-reader foo) 'avalue))
366 (cl-user:/show foo-a-writer)
367 (cl-user:/show (funcall foo-a-writer 'replacedavalue foo))
368 (cl-user:/show "new" (funcall foo-a-reader foo))
369 (aver (eql (funcall foo-a-reader foo) 'replacedavalue))
371 ;; type checks on FOO-ness of instance argument
372 (cl-user:/show (nth-value 1 (ignore-errors (funcall foo-a-reader 3))))
373 (aver (typep (nth-value 1 (ignore-errors (funcall foo-a-reader 3)))
375 (aver (typep (nth-value 1 (ignore-errors (funcall foo-a-writer 3 4)))
378 ;; type checks on written slot value
379 (multiple-value-bind (foo-b-reader foo-b-writer)
380 (slot-accessor-funs foo-dd foo-b-dsd)
381 (cl-user:/show "old" (funcall foo-b-reader foo))
382 (aver (not (eql (funcall foo-b-reader foo) (find-package :cl-user))))
383 (funcall foo-b-writer (find-package :cl-user) foo)
384 (cl-user:/show "new" (funcall foo-b-reader foo))
385 (aver (eql (funcall foo-b-reader foo) (find-package :cl-user)))
386 (aver (typep (nth-value 1 (ignore-errors (funcall foo-b-writer 5 foo)))
388 (aver (eql (funcall foo-b-reader foo) (find-package :cl-user))))
391 (cl-user:/describe foo-x-dsd)
392 (cl-user:/describe foo-y-dsd)
393 (multiple-value-bind (foo-x-reader foo-x-writer)
394 (slot-accessor-funs foo-dd foo-x-dsd)
395 (multiple-value-bind (foo-y-reader foo-y-writer)
396 (slot-accessor-funs foo-dd foo-y-dsd)
398 ;; basic functionality for (UNSIGNED-BYTE 32) slot
399 (cl-user:/show foo-x-reader)
400 (cl-user:/show (funcall foo-x-reader foo))
401 (aver (eql (funcall foo-x-reader foo) 50))
402 (cl-user:/show foo-x-writer)
403 (cl-user:/show (funcall foo-x-writer 14 foo))
404 (cl-user:/show "new" (funcall foo-x-reader foo))
405 (aver (eql (funcall foo-x-reader foo) 14)))
407 ;; type check for (UNSIGNED-BYTE 32) slot
408 (/show "to do: type check X")
414 ;;;; the legendary DEFSTRUCT macro itself (both CL:DEFSTRUCT and its
415 ;;;; close personal friend SB!XC:DEFSTRUCT)
417 ;;; Return a list of forms to install PRINT and MAKE-LOAD-FORM funs,
418 ;;; mentioning them in the expansion so that they can be compiled.
419 (defun class-method-definitions (defstruct)
420 (let ((name (dd-name defstruct)))
422 ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant
423 ;; class names which creates fast but non-cold-loadable,
424 ;; non-compact code. In this context, we'd rather have
425 ;; compact, cold-loadable code. -- WHN 19990928
426 (declare (notinline sb!xc:find-class))
427 #+sb-xc (/show0 "beginning CLASS-METHOD-DEFINITIONS forms")
428 ,@(let ((pf (dd-print-function defstruct))
429 (po (dd-print-object defstruct))
432 ;; Giving empty :PRINT-OBJECT or :PRINT-FUNCTION options
433 ;; leaves PO or PF equal to NIL. The user-level effect is
434 ;; to generate a PRINT-OBJECT method specialized for the type,
435 ;; implementing the default #S structure-printing behavior.
436 (when (or (eq pf nil) (eq po nil))
437 (setf pf '(default-structure-print)
439 (flet (;; Given an arg from a :PRINT-OBJECT or :PRINT-FUNCTION
440 ;; option, return the value to pass as an arg to FUNCTION.
442 (destructuring-bind (fun-name) oarg
444 (cond ((not (eql pf 0))
445 `((def!method print-object ((,x ,name) ,s)
446 (funcall #',(farg pf) ,x ,s *current-level*))))
448 `((def!method print-object ((,x ,name) ,s)
449 (funcall #',(farg po) ,x ,s))))
451 ,@(let ((pure (dd-pure defstruct)))
453 `((setf (layout-pure (class-layout
454 (sb!xc:find-class ',name)))
456 ((eq pure :substructure)
457 `((setf (layout-pure (class-layout
458 (sb!xc:find-class ',name)))
460 ,@(let ((def-con (dd-default-constructor defstruct)))
461 (when (and def-con (not (dd-alternate-metaclass defstruct)))
462 `((setf (structure-class-constructor (sb!xc:find-class ',name))
464 #+sb-xc (/show0 "done with CLASS-METHOD-DEFINITIONS forms")))))
465 ;;; FIXME: I really would like to make structure accessors less
466 ;;; special, just ordinary inline functions. (Or perhaps inline
467 ;;; functions with special compact implementations of their
468 ;;; expansions, to avoid bloating the system.)
470 ;;; shared logic for CL:DEFSTRUCT and SB!XC:DEFSTRUCT
471 (defmacro !expander-for-defstruct (name-and-options
473 expanding-into-code-for-xc-host-p)
474 `(let ((name-and-options ,name-and-options)
475 (slot-descriptions ,slot-descriptions)
476 (expanding-into-code-for-xc-host-p
477 ,expanding-into-code-for-xc-host-p))
478 (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
483 (let ((inherits (inherits-for-structure dd)))
485 (/show0 "beginning macroexpanded DEFSTRUCT code")
486 (eval-when (:compile-toplevel :load-toplevel :execute)
487 (%compiler-defstruct ',dd ',inherits))
488 (/show0 "back from %COMPILER-DEFSTRUCT")
489 (%defstruct ',dd ',inherits)
490 (/show0 "back from %DEFSTRUCT")
491 ,@(unless expanding-into-code-for-xc-host-p
492 (append (raw-accessor-definitions dd)
493 (predicate-definitions dd)
494 ;; FIXME: We've inherited from CMU CL nonparallel
495 ;; code for creating copiers for typed and untyped
496 ;; structures. This should be fixed.
497 ;(copier-definition dd)
498 (constructor-definitions dd)
499 (class-method-definitions dd)))
500 (/show0 "done with macroexpanded DEFSTRUCT code")
503 (/show0 "beginning macroexpanded typed DEFSTRUCT code")
504 (eval-when (:compile-toplevel :load-toplevel :execute)
505 (setf (info :typed-structure :info ',name) ',dd))
506 ,@(unless expanding-into-code-for-xc-host-p
507 (append (typed-accessor-definitions dd)
508 (typed-predicate-definitions dd)
509 (typed-copier-definitions dd)
510 (constructor-definitions dd)))
511 (/show0 "done with macroexpanded typed DEFSTRUCT code")
514 (sb!xc:defmacro defstruct (name-and-options &rest slot-descriptions)
516 "DEFSTRUCT {Name | (Name Option*)} {Slot | (Slot [Default] {Key Value}*)}
517 Define the structure type Name. Instances are created by MAKE-<name>,
518 which takes &KEY arguments allowing initial slot values to the specified.
519 A SETF'able function <name>-<slot> is defined for each slot to read and
520 write slot values. <name>-p is a type predicate.
522 Popular DEFSTRUCT options (see manual for others):
526 Specify the name for the constructor or predicate.
528 (:CONSTRUCTOR Name Lambda-List)
529 Specify the name and arguments for a BOA constructor
530 (which is more efficient when keyword syntax isn't necessary.)
532 (:INCLUDE Supertype Slot-Spec*)
533 Make this type a subtype of the structure type Supertype. The optional
534 Slot-Specs override inherited slot options.
539 Asserts that the value of this slot is always of the specified type.
542 If true, no setter function is defined for this slot."
543 (!expander-for-defstruct name-and-options slot-descriptions nil))
545 (defmacro sb!xc:defstruct (name-and-options &rest slot-descriptions)
547 "Cause information about a target structure to be built into the
549 (!expander-for-defstruct name-and-options slot-descriptions t))
551 ;;;; functions to generate code for various parts of DEFSTRUCT definitions
553 ;;; Return forms to define readers and writers for raw slots as inline
555 (defun raw-accessor-definitions (dd)
556 (let* ((name (dd-name dd))
557 (dtype (dd-declarable-type dd)))
559 (dolist (slot (dd-slots dd))
560 (let ((slot-type (dsd-type slot))
561 (accessor-name (dsd-accessor-name slot))
562 (argname (gensym "ARG"))
563 (nvname (gensym "NEW-VALUE-")))
564 (multiple-value-bind (accessor offset data)
565 (slot-accessor-form dd slot argname)
566 ;; When accessor exists and is raw
567 (when (and accessor-name
568 (not (eq accessor-name '%instance-ref)))
569 (res `(/show0 "doing one slot, ACCESSOR-NAME=.."))
570 (res `(/hexstr ',accessor-name))
571 (res `(declaim (inline ,accessor-name)))
572 (res `(/show0 "done with reader DECLAIM INLINE"))
573 (res `(declaim (ftype (function (,dtype) ,slot-type)
575 (res `(/show0 "done with reader DECLAIM FTYPE, doing DEFUN"))
576 (res `(defun ,accessor-name (,argname)
577 ;; Note: The DECLARE here might seem redundant
578 ;; with the DECLAIM FTYPE above, but it's not:
579 ;; If we're not at toplevel, the PROCLAIM inside
580 ;; the DECLAIM doesn't get executed until after
581 ;; this function is compiled.
582 (declare (type ,dtype ,argname))
583 (truly-the ,slot-type (,accessor ,data ,offset))))
584 (unless (dsd-read-only slot)
585 (res `(/show0 "doing writer DECLAIM INLINE"))
586 (res `(declaim (inline (setf ,accessor-name))))
587 (res `(/show0 "doing writer DECLAIM FTYPE"))
588 (res `(declaim (ftype (function (,slot-type ,dtype) ,slot-type)
589 (setf ,accessor-name))))
590 ;; FIXME: I rewrote this somewhat from the CMU CL definition.
591 ;; Do some basic tests to make sure that reading and writing
592 ;; raw slots still works correctly.
593 (res `(/show0 "doing writer DEFUN"))
594 (res `(defun (setf ,accessor-name) (,nvname ,argname)
595 (declare (type ,dtype ,argname))
596 (setf (,accessor ,data ,offset) ,nvname)
598 (res `(/show0 "done with one slot"))))))
599 `((/show0 "beginning RAW-ACCESSOR-DEFINITIONS forms")
601 (/show0 "done with RAW-ACCESSOR-DEFINITIONS forms")))))
603 ;;; Return a list of forms which create a predicate for an untyped DEFSTRUCT.
604 (defun predicate-definitions (dd)
605 (let ((pred (dd-predicate-name dd))
606 (argname (gensym "ARG")))
608 `((/show0 "beginning PREDICATE-DEFINITIONS forms")
610 (declaim (inline ,pred))
611 (defun ,pred (,argname)
612 (declare (optimize (speed 3) (safety 0)))
613 (typep-to-layout ,argname
614 (compile-time-find-layout ,(dd-name dd))))
615 (/show0 "done with PREDICATE-DEFINITIONS forms")))))
617 ;;; Return a list of forms which create a predicate function for a typed
619 (defun typed-predicate-definitions (defstruct)
620 (let ((name (dd-name defstruct))
621 (predicate-name (dd-predicate-name defstruct))
623 (when (and predicate-name (dd-named defstruct))
624 (let ((ltype (dd-lisp-type defstruct)))
625 `((defun ,predicate-name (,argname)
626 (and (typep ,argname ',ltype)
627 (eq (elt (the ,ltype ,argname)
628 ,(cdr (car (last (find-name-indices defstruct)))))
631 ;;; FIXME: We've inherited from CMU CL code to do typed structure copiers
632 ;;; in a completely different way than untyped structure copiers. Fix this.
633 ;;; (This function was my first attempt to fix this, but I stopped before
634 ;;; figuring out how to install it completely and remove the parallel
635 ;;; code which simply SETF's the FDEFINITION of the DD-COPIER name.
637 ;;; Return the copier definition for an untyped DEFSTRUCT.
638 (defun copier-definition (dd)
640 (let ((argname (gensym)))
642 (protect-cl ',(dd-copier dd))
643 (defun ,(dd-copier dd) (,argname)
644 (declare (type ,(dd-name dd) ,argname))
645 (copy-structure ,argname))))))
648 ;;; Return a list of forms to create a copier function of a typed DEFSTRUCT.
649 (defun typed-copier-definitions (defstruct)
650 (when (dd-copier-name defstruct)
651 `((setf (fdefinition ',(dd-copier-name defstruct)) #'copy-seq)
652 (declaim (ftype function ,(dd-copier-name defstruct))))))
654 ;;; Return a list of function definitions for accessing and setting
655 ;;; the slots of a typed DEFSTRUCT. The functions are proclaimed to be
656 ;;; inline, and the types of their arguments and results are declared
657 ;;; as well. We count on the compiler to do clever things with ELT.
658 (defun typed-accessor-definitions (defstruct)
660 (let ((ltype (dd-lisp-type defstruct)))
661 (dolist (slot (dd-slots defstruct))
662 (let ((name (dsd-accessor-name slot))
663 (index (dsd-index slot))
664 (slot-type `(and ,(dsd-type slot)
665 ,(dd-element-type defstruct))))
666 (stuff `(proclaim '(inline ,name (setf ,name))))
667 ;; FIXME: The arguments in the next two DEFUNs should be
668 ;; gensyms. (Otherwise e.g. if NEW-VALUE happened to be the
669 ;; name of a special variable, things could get weird.)
670 (stuff `(defun ,name (structure)
671 (declare (type ,ltype structure))
672 (the ,slot-type (elt structure ,index))))
673 (unless (dsd-read-only slot)
675 `(defun (setf ,name) (new-value structure)
676 (declare (type ,ltype structure) (type ,slot-type new-value))
677 (setf (elt structure ,index) new-value)))))))
682 (defun require-no-print-options-so-far (defstruct)
683 (unless (and (eql (dd-print-function defstruct) 0)
684 (eql (dd-print-object defstruct) 0))
685 (error "No more than one of the following options may be specified:
686 :PRINT-FUNCTION, :PRINT-OBJECT, :TYPE")))
688 ;;; Parse a single DEFSTRUCT option and store the results in DD.
689 (defun parse-1-dd-option (option dd)
690 (let ((args (rest option))
694 (destructuring-bind (conc-name) args
695 (setf (dd-conc-name dd)
696 (if (symbolp conc-name)
698 (make-symbol (string conc-name))))))
700 (destructuring-bind (&optional (cname (symbolicate "MAKE-" name))
703 (push (cons cname stuff) (dd-constructors dd))))
705 (destructuring-bind (&optional (copier (symbolicate "COPY-" name)))
707 (setf (dd-copier-name dd) copier)))
709 (destructuring-bind (&optional (predicate-name (symbolicate name "-P")))
711 (setf (dd-predicate-name dd) predicate-name)))
713 (when (dd-include dd)
714 (error "more than one :INCLUDE option"))
715 (setf (dd-include dd) args))
717 (require-no-print-options-so-far dd)
718 (setf (dd-print-function dd)
719 (the (or symbol cons) args)))
721 (require-no-print-options-so-far dd)
722 (setf (dd-print-object dd)
723 (the (or symbol cons) args)))
725 (destructuring-bind (type) args
726 (cond ((member type '(list vector))
727 (setf (dd-element-type dd) t)
728 (setf (dd-type dd) type))
729 ((and (consp type) (eq (first type) 'vector))
730 (destructuring-bind (vector vtype) type
731 (declare (ignore vector))
732 (setf (dd-element-type dd) vtype)
733 (setf (dd-type dd) 'vector)))
735 (error "~S is a bad :TYPE for DEFSTRUCT." type)))))
737 (error "The DEFSTRUCT option :NAMED takes no arguments."))
739 (destructuring-bind (offset) args
740 (setf (dd-offset dd) offset)))
742 (destructuring-bind (fun) args
743 (setf (dd-pure dd) fun)))
744 (t (error "unknown DEFSTRUCT option:~% ~S" option)))))
746 ;;; Given name and options, return a DD holding that info.
747 (eval-when (:compile-toplevel :load-toplevel :execute)
748 (defun parse-defstruct-name-and-options (name-and-options)
749 (destructuring-bind (name &rest options) name-and-options
750 (aver name) ; A null name doesn't seem to make sense here.
751 (let ((dd (make-defstruct-description name)))
752 (dolist (option options)
753 (cond ((eq option :named)
754 (setf (dd-named dd) t))
756 (parse-1-dd-option option dd))
757 ((member option '(:conc-name :constructor :copier :predicate))
758 (parse-1-dd-option (list option) dd))
760 (error "unrecognized DEFSTRUCT option: ~S" option))))
765 (error ":OFFSET can't be specified unless :TYPE is specified."))
766 (unless (dd-include dd)
767 ;; FIXME: It'd be cleaner to treat no-:INCLUDE as defaulting
768 ;; to :INCLUDE STRUCTURE-OBJECT, and then let the general-case
769 ;; (INCF (DD-LENGTH DD) (DD-LENGTH included-DD)) logic take
770 ;; care of this. (Except that the :TYPE VECTOR and :TYPE
771 ;; LIST cases, with their :NAMED and un-:NAMED flavors,
772 ;; make that messy, alas.)
773 (incf (dd-length dd))))
775 (require-no-print-options-so-far dd)
777 (incf (dd-length dd)))
778 (let ((offset (dd-offset dd)))
779 (when offset (incf (dd-length dd) offset)))))
781 (when (dd-include dd)
782 (do-dd-inclusion-stuff dd))
786 ;;; Given name and options and slot descriptions (and possibly doc
787 ;;; string at the head of slot descriptions) return a DD holding that
789 (defun parse-defstruct-name-and-options-and-slot-descriptions
790 (name-and-options slot-descriptions)
791 (let ((result (parse-defstruct-name-and-options (if (atom name-and-options)
792 (list name-and-options)
794 (when (stringp (car slot-descriptions))
795 (setf (dd-doc result) (pop slot-descriptions)))
796 (dolist (slot-description slot-descriptions)
797 (allocate-1-slot result (parse-1-dsd result slot-description)))
802 ;;;; stuff to parse slot descriptions
804 ;;; Parse a slot description for DEFSTRUCT, add it to the description
805 ;;; and return it. If supplied, SLOT is a pre-initialized DSD
806 ;;; that we modify to get the new slot. This is supplied when handling
808 (defun parse-1-dsd (defstruct spec &optional
809 (slot (make-defstruct-slot-description :%name ""
812 (multiple-value-bind (name default default-p type type-p read-only ro-p)
817 &optional (default nil default-p)
818 &key (type nil type-p) (read-only nil ro-p))
822 (uncross type) type-p
825 (when (keywordp spec)
826 (style-warn "Keyword slot name indicates probable syntax ~
827 error in DEFSTRUCT: ~S."
831 (when (find name (dd-slots defstruct) :test #'string= :key #'dsd-%name)
832 (error 'simple-program-error
833 :format-control "duplicate slot name ~S"
834 :format-arguments (list name)))
835 (setf (dsd-%name slot) (string name))
836 (setf (dd-slots defstruct) (nconc (dd-slots defstruct) (list slot)))
838 (let ((accessor-name (symbolicate (or (dd-conc-name defstruct) "") name))
839 (predicate-name (dd-predicate-name defstruct)))
840 (setf (dsd-accessor-name slot) accessor-name)
841 (when (eql accessor-name predicate-name)
842 ;; Some adventurous soul has named a slot so that its accessor
843 ;; collides with the structure type predicate. ANSI doesn't
844 ;; specify what to do in this case. As of 2001-09-04, Martin
845 ;; Atzmueller reports that CLISP and Lispworks both give
846 ;; priority to the slot accessor, so that the predicate is
847 ;; overwritten. We might as well do the same (as well as
848 ;; signalling a warning).
850 "~@<The structure accessor name ~S is the same as the name of the ~
851 structure type predicate. ANSI doesn't specify what to do in ~
852 this case. We'll overwrite the type predicate with the slot ~
853 accessor, but you can't rely on this behavior, so it'd be wise to ~
854 remove the ambiguity in your code.~@:>"
856 (setf (dd-predicate-name defstruct) nil)))
859 (setf (dsd-default slot) default))
861 (setf (dsd-type slot)
862 (if (eq (dsd-type slot) t)
864 `(and ,(dsd-type slot) ,type))))
867 (setf (dsd-read-only slot) t)
868 (when (dsd-read-only slot)
869 (error "Slot ~S is :READ-ONLY in parent and must be :READ-ONLY in subtype ~S."
874 ;;; When a value of type TYPE is stored in a structure, should it be
875 ;;; stored in a raw slot? Return (VALUES RAW? RAW-TYPE WORDS), where
876 ;;; RAW? is true if TYPE should be stored in a raw slot.
877 ;;; RAW-TYPE is the raw slot type, or NIL if no raw slot.
878 ;;; WORDS is the number of words in the raw slot, or NIL if no raw slot.
879 (defun structure-raw-slot-type-and-size (type)
880 (/noshow "in STRUCTURE-RAW-SLOT-TYPE-AND-SIZE" type (sb!xc:subtypep type 'fixnum))
882 (;; FIXME: For now we suppress raw slots, since there are various
883 ;; issues about the way that the cross-compiler handles them.
884 (not (boundp '*dummy-placeholder-to-stop-compiler-warnings*))
885 (values nil nil nil))
886 ((and (sb!xc:subtypep type '(unsigned-byte 32))
887 (multiple-value-bind (fixnum? fixnum-certain?)
888 (sb!xc:subtypep type 'fixnum)
889 (/noshow fixnum? fixnum-certain?)
890 ;; (The extra test for FIXNUM-CERTAIN? here is
891 ;; intended for bootstrapping the system. In
892 ;; particular, in sbcl-0.6.2, we set up LAYOUT before
893 ;; FIXNUM is defined, and so could bogusly end up
894 ;; putting INDEX-typed values into raw slots if we
895 ;; didn't test FIXNUM-CERTAIN?.)
896 (and (not fixnum?) fixnum-certain?)))
897 (values t 'unsigned-byte 1))
898 ((sb!xc:subtypep type 'single-float)
899 (values t 'single-float 1))
900 ((sb!xc:subtypep type 'double-float)
901 (values t 'double-float 2))
903 ((sb!xc:subtypep type 'long-float)
904 (values t 'long-float #!+x86 3 #!+sparc 4))
905 ((sb!xc:subtypep type '(complex single-float))
906 (values t 'complex-single-float 2))
907 ((sb!xc:subtypep type '(complex double-float))
908 (values t 'complex-double-float 4))
910 ((sb!xc:subtypep type '(complex long-float))
911 (values t 'complex-long-float #!+x86 6 #!+sparc 8))
913 (values nil nil nil))))
915 ;;; Allocate storage for a DSD in DD. This is where we decide whether
916 ;;; a slot is raw or not. If raw, and we haven't allocated a raw-index
917 ;;; yet for the raw data vector, then do it. Raw objects are aligned
918 ;;; on the unit of their size.
919 (defun allocate-1-slot (dd dsd)
920 #+sb-xc (/show0 "entering ALLOCATE-1-SLOT")
921 (multiple-value-bind (raw? raw-type words)
922 (if (eq (dd-type dd) 'structure)
923 (structure-raw-slot-type-and-size (dsd-type dsd))
924 (values nil nil nil))
925 (/noshow "ALLOCATE-1-SLOT" dsd raw? raw-type words)
927 (setf (dsd-index dsd) (dd-length dd))
928 (incf (dd-length dd)))
930 (unless (dd-raw-index dd)
931 (setf (dd-raw-index dd) (dd-length dd))
932 (incf (dd-length dd)))
933 (let ((off (rem (dd-raw-length dd) words)))
935 (incf (dd-raw-length dd) (- words off))))
936 (setf (dsd-raw-type dsd) raw-type)
937 (setf (dsd-index dsd) (dd-raw-length dd))
938 (incf (dd-raw-length dd) words))))
939 #+sb-xc (/show0 "leaving ALLOCATE-1-SLOT")
942 (defun typed-structure-info-or-lose (name)
943 (or (info :typed-structure :info name)
944 (error ":TYPE'd DEFSTRUCT ~S not found for inclusion." name)))
946 ;;; Process any included slots pretty much like they were specified.
947 ;;; Also inherit various other attributes.
948 (defun do-dd-inclusion-stuff (dd)
949 (destructuring-bind (included-name &rest modified-slots) (dd-include dd)
950 (let* ((type (dd-type dd))
953 (layout-info (compiler-layout-or-lose included-name))
954 (typed-structure-info-or-lose included-name))))
956 ;; checks on legality
957 (unless (and (eq type (dd-type included-structure))
958 (type= (specifier-type (dd-element-type included-structure))
959 (specifier-type (dd-element-type dd))))
960 (error ":TYPE option mismatch between structures ~S and ~S"
961 (dd-name dd) included-name))
962 (let ((included-class (sb!xc:find-class included-name nil)))
964 ;; It's not particularly well-defined to :INCLUDE any of the
965 ;; CMU CL INSTANCE weirdosities like CONDITION or
966 ;; GENERIC-FUNCTION, and it's certainly not ANSI-compliant.
967 (let* ((included-layout (class-layout included-class))
968 (included-dd (layout-info included-layout)))
969 (when (and (dd-alternate-metaclass included-dd)
970 ;; As of sbcl-0.pre7.73, anyway, STRUCTURE-OBJECT
971 ;; is represented with an ALTERNATE-METACLASS. But
972 ;; it's specifically OK to :INCLUDE (and PCL does)
973 ;; so in this one case, it's OK to include
974 ;; something with :ALTERNATE-METACLASS after all.
975 (not (eql included-name 'structure-object)))
976 (error "can't :INCLUDE class ~S (has alternate metaclass)"
979 (incf (dd-length dd) (dd-length included-structure))
980 (when (dd-class-p dd)
981 (let ((mc (rest (dd-alternate-metaclass included-structure))))
982 (when (and mc (not (dd-alternate-metaclass dd)))
983 (setf (dd-alternate-metaclass dd)
984 (cons included-name mc))))
985 (when (eq (dd-pure dd) :unspecified)
986 (setf (dd-pure dd) (dd-pure included-structure)))
987 (setf (dd-raw-index dd) (dd-raw-index included-structure))
988 (setf (dd-raw-length dd) (dd-raw-length included-structure)))
990 (dolist (included-slot (dd-slots included-structure))
991 (let* ((included-name (dsd-name included-slot))
992 (modified (or (find included-name modified-slots
993 :key #'(lambda (x) (if (atom x) x (car x)))
998 (copy-structure included-slot)))))))
1000 ;;;; various helper functions for setting up DEFSTRUCTs
1002 ;;; This function is called at macroexpand time to compute the INHERITS
1003 ;;; vector for a structure type definition.
1004 (defun inherits-for-structure (info)
1005 (declare (type defstruct-description info))
1006 (let* ((include (dd-include info))
1007 (superclass-opt (dd-alternate-metaclass info))
1010 (compiler-layout-or-lose (first include))
1011 (class-layout (sb!xc:find-class
1012 (or (first superclass-opt)
1013 'structure-object))))))
1014 (if (eq (dd-name info) 'lisp-stream)
1015 ;; a hack to added the stream class as a mixin for LISP-STREAMs
1016 (concatenate 'simple-vector
1017 (layout-inherits super)
1019 (class-layout (sb!xc:find-class 'stream))))
1020 (concatenate 'simple-vector
1021 (layout-inherits super)
1024 ;;; Do miscellaneous (LOAD EVAL) time actions for the structure
1025 ;;; described by DD. Create the class & LAYOUT, checking for
1026 ;;; incompatible redefinition. Define those functions which are
1027 ;;; sufficiently stereotyped that we can implement them as standard
1029 (defun %defstruct (dd inherits)
1030 (declare (type defstruct-description dd))
1032 #+sb-xc (/show0 "entering %DEFSTRUCT")
1034 ;; We set up LAYOUTs even in the cross-compilation host.
1035 (multiple-value-bind (class layout old-layout)
1036 (ensure-structure-class dd inherits "current" "new")
1037 (cond ((not old-layout)
1038 (unless (eq (class-layout class) layout)
1039 (register-layout layout)))
1041 (let ((old-dd (layout-info old-layout)))
1042 (when (defstruct-description-p old-dd)
1043 (dolist (slot (dd-slots old-dd))
1044 (fmakunbound (dsd-accessor-name slot))
1045 (unless (dsd-read-only slot)
1046 (fmakunbound `(setf ,(dsd-accessor-name slot)))))))
1047 (%redefine-defstruct class old-layout layout)
1048 (setq layout (class-layout class))))
1049 (setf (sb!xc:find-class (dd-name dd)) class)
1051 ;; It doesn't make sense to do these in the cross-compilation host.
1054 #+sb-xc (/show0 "doing #+SB-XC stuff in %DEFSTRUCT")
1055 (remhash (dd-name dd) *typecheckfuns*)
1056 (%target-defstruct dd layout)
1058 (setf (fdocumentation (dd-name dd) 'type)
1060 #+sb-xc (/show0 "done with #+SB-XC stuff in %DEFSTRUCT")
1063 #+sb-xc (/show0 "leaving %DEFSTRUCT")
1066 ;;; Return a form describing the writable place used for this slot
1067 ;;; in the instance named INSTANCE-NAME.
1068 (defun %accessor-place-form (dd dsd instance-name)
1069 (let (;; the operator that we'll use to access a typed slot or, in
1070 ;; the case of a raw slot, to read the vector of raw slots
1071 (ref (ecase (dd-type dd)
1072 (structure '%instance-ref)
1073 (list 'nth-but-with-sane-arg-order)
1075 (raw-type (dsd-raw-type dsd)))
1076 (if (eq raw-type t) ; if not raw slot
1077 `(,ref ,instance-name ,(dsd-index dsd))
1078 (let (;; the operator that we'll use to access one value in
1079 ;; the raw data vector
1080 (rawref (ecase raw-type
1081 ;; The compiler thinks that the raw data
1082 ;; vector is a vector of unsigned bytes, so if
1083 ;; the slot we want to access actually *is* an
1084 ;; unsigned byte, it'll access the slot for
1085 ;; us even if we don't lie to it at all.
1086 (unsigned-byte 'aref)
1087 ;; "A lie can travel halfway round the world while
1088 ;; the truth is putting on its shoes." -- Mark Twain
1089 (single-float '%raw-ref-single)
1090 (double-float '%raw-ref-double)
1091 #!+long-float (long-float '%raw-ref-long)
1092 (complex-single-float '%raw-ref-complex-single)
1093 (complex-double-float '%raw-ref-complex-double)
1094 #!+long-float (complex-long-float
1095 '%raw-ref-complex-long))))
1096 `(,rawref (,ref ,instance-name ,(dd-raw-index dd))
1097 ,(dsd-index dsd))))))
1099 ;;; Return inline expansion designators (i.e. values suitable for
1100 ;;; (INFO :FUNCTION :INLINE-EXPANSSION-DESIGNATOR ..)) for the reader
1101 ;;; and writer functions of the slot described by DSD.
1102 (defun accessor-inline-expansion-designators (dd dsd)
1105 (declare (type ,(dd-name dd) instance))
1106 (truly-the ,(dsd-type dsd)
1107 ,(%accessor-place-form dd dsd 'instance))))
1109 `(lambda (new-value instance)
1110 (declare (type ,(dsd-type dsd) new-value))
1111 (declare (type ,(dd-name dd) structure-object))
1112 (setf ,(%accessor-place-form dd dsd 'instance) new-value)))))
1114 ;;; core compile-time setup of any class with a LAYOUT, used even by
1115 ;;; !DEFSTRUCT-WITH-ALTERNATE-METACLASS weirdosities
1116 (defun %compiler-set-up-layout (dd
1118 ;; Several special cases (STRUCTURE-OBJECT
1119 ;; itself, and structures with alternate
1120 ;; metaclasses) call this function directly,
1121 ;; and they're all at the base of the
1122 ;; instance class structure, so this is
1124 (inherits (vector (find-layout t)
1125 (find-layout 'instance))))
1127 (/show "entering %COMPILER-SET-UP-LAYOUT for" (dd-name dd))
1129 (multiple-value-bind (class layout old-layout)
1130 (multiple-value-bind (clayout clayout-p)
1131 (info :type :compiler-layout (dd-name dd))
1132 (ensure-structure-class dd
1134 (if clayout-p "previously compiled" "current")
1136 :compiler-layout clayout))
1138 (/show "non-NIL" old-layout)
1139 (undefine-structure (layout-class old-layout))
1140 (when (and (class-subclasses class)
1141 (not (eq layout old-layout)))
1143 (dohash (class layout (class-subclasses class))
1144 (declare (ignore layout))
1145 (undefine-structure class)
1146 (subs (class-proper-name class)))
1148 (warn "removing old subclasses of ~S:~% ~S"
1149 (sb!xc:class-name class)
1152 (unless (eq (class-layout class) layout)
1153 (register-layout layout :invalidate nil))
1154 (setf (sb!xc:find-class (dd-name dd)) class)))
1156 ;; At this point the class should be set up in the INFO database.
1157 ;; But the logic that enforces this is a little tangled and
1158 ;; scattered, so it's not obvious, so let's check.
1159 (aver (sb!xc:find-class (dd-name dd) nil))
1161 (setf (info :type :compiler-layout (dd-name dd)) layout))
1163 (/show0 "leaving %COMPILER-SET-UP-LAYOUT")
1167 ;;; Do (COMPILE LOAD EVAL)-time actions for the normal (not
1168 ;;; ALTERNATE-LAYOUT) DEFSTRUCT described by DD.
1169 (defun %compiler-defstruct (dd inherits)
1170 (declare (type defstruct-description dd))
1171 #+sb-xc (/show0 "entering %COMPILER-DEFSTRUCT")
1173 (%compiler-set-up-layout dd inherits)
1175 (let* ((dd-name (dd-name dd))
1176 (dtype (dd-declarable-type dd))
1177 (class (sb!xc:find-class dd-name)))
1179 (let ((copier-name (dd-copier-name dd)))
1181 (sb!xc:proclaim `(ftype (function (,dtype) ,dtype) ,copier-name))))
1183 (let ((predicate-name (dd-predicate-name dd)))
1184 (when predicate-name
1185 (sb!xc:proclaim `(ftype (function (t) t) ,predicate-name))))
1187 (dolist (dsd (dd-slots dd))
1188 (let* ((accessor-name (dsd-accessor-name dsd))
1189 (dsd-type (dsd-type dsd)))
1191 (multiple-value-bind (reader-designator writer-designator)
1192 (accessor-inline-expansion-designators dd dsd)
1193 (sb!xc:proclaim `(ftype (function (,dtype) ,dsd-type)
1195 (setf (info :function
1196 :inline-expansion-designator
1199 (info :function :inlinep accessor-name)
1201 (unless (dsd-read-only dsd)
1202 (let ((setf-accessor-name `(setf ,accessor-name)))
1204 `(ftype (function (,dsd-type ,dtype) ,dsd-type)
1205 ,setf-accessor-name))
1206 (setf (info :function
1207 :inline-expansion-designator
1210 (info :function :inlinep setf-accessor-name)
1213 #+sb-xc (/show0 "leaving %COMPILER-DEFSTRUCT")
1216 ;;;; redefinition stuff
1218 ;;; Compare the slots of OLD and NEW, returning 3 lists of slot names:
1219 ;;; 1. Slots which have moved,
1220 ;;; 2. Slots whose type has changed,
1221 ;;; 3. Deleted slots.
1222 (defun compare-slots (old new)
1223 (let* ((oslots (dd-slots old))
1224 (nslots (dd-slots new))
1225 (onames (mapcar #'dsd-name oslots))
1226 (nnames (mapcar #'dsd-name nslots)))
1229 (dolist (name (intersection onames nnames))
1230 (let ((os (find name oslots :key #'dsd-name))
1231 (ns (find name nslots :key #'dsd-name)))
1232 (unless (subtypep (dsd-type ns) (dsd-type os))
1233 (/noshow "found retyped slots" ns os (dsd-type ns) (dsd-type os))
1235 (unless (and (= (dsd-index os) (dsd-index ns))
1236 (eq (dsd-raw-type os) (dsd-raw-type ns)))
1240 (set-difference onames nnames)))))
1242 ;;; If we are redefining a structure with different slots than in the
1243 ;;; currently loaded version, give a warning and return true.
1244 (defun redefine-structure-warning (class old new)
1245 (declare (type defstruct-description old new)
1246 (type sb!xc:class class)
1248 (let ((name (dd-name new)))
1249 (multiple-value-bind (moved retyped deleted) (compare-slots old new)
1250 (when (or moved retyped deleted)
1252 "incompatibly redefining slots of structure class ~S~@
1253 Make sure any uses of affected accessors are recompiled:~@
1254 ~@[ These slots were moved to new positions:~% ~S~%~]~
1255 ~@[ These slots have new incompatible types:~% ~S~%~]~
1256 ~@[ These slots were deleted:~% ~S~%~]"
1257 name moved retyped deleted)
1260 ;;; This function is called when we are incompatibly redefining a
1261 ;;; structure CLASS to have the specified NEW-LAYOUT. We signal an
1262 ;;; error with some proceed options and return the layout that should
1264 (defun %redefine-defstruct (class old-layout new-layout)
1265 (declare (type sb!xc:class class) (type layout old-layout new-layout))
1266 #+sb-xc (/show0 "entering %REDEFINE-DEFSTRUCT")
1267 (let ((name (class-proper-name class)))
1269 (error "redefining class ~S incompatibly with the current definition"
1272 :report "Invalidate current definition."
1273 (warn "Previously loaded ~S accessors will no longer work." name)
1274 (register-layout new-layout))
1276 :report "Smash current layout, preserving old code."
1277 (warn "Any old ~S instances will be in a bad way.~@
1278 I hope you know what you're doing..."
1280 (register-layout new-layout :invalidate nil
1281 :destruct-layout old-layout))))
1282 #+sb-xc (/show0 "leaving %REDEFINE-DEFSTRUCT")
1285 ;;; This is called when we are about to define a structure class. It
1286 ;;; returns a (possibly new) class object and the layout which should
1287 ;;; be used for the new definition (may be the current layout, and
1288 ;;; also might be an uninstalled forward referenced layout.) The third
1289 ;;; value is true if this is an incompatible redefinition, in which
1290 ;;; case it is the old layout.
1291 (defun ensure-structure-class (info inherits old-context new-context
1292 &key compiler-layout)
1293 (multiple-value-bind (class old-layout)
1297 (class 'sb!xc:structure-class)
1298 (constructor 'make-structure-class))
1299 (dd-alternate-metaclass info)
1300 (declare (ignore name))
1301 (insured-find-class (dd-name info)
1302 (if (eq class 'sb!xc:structure-class)
1304 (typep x 'sb!xc:structure-class))
1306 (sb!xc:typep x (sb!xc:find-class class))))
1307 (fdefinition constructor)))
1308 (setf (class-direct-superclasses class)
1309 (if (eq (dd-name info) 'lisp-stream)
1310 ;; a hack to add STREAM as a superclass mixin to LISP-STREAMs
1311 (list (layout-class (svref inherits (1- (length inherits))))
1312 (layout-class (svref inherits (- (length inherits) 2))))
1313 (list (layout-class (svref inherits (1- (length inherits)))))))
1314 (let ((new-layout (make-layout :class class
1316 :depthoid (length inherits)
1317 :length (dd-length info)
1319 (old-layout (or compiler-layout old-layout)))
1322 (values class new-layout nil))
1323 (;; This clause corresponds to an assertion in REDEFINE-LAYOUT-WARNING
1324 ;; of classic CMU CL. I moved it out to here because it was only
1325 ;; exercised in this code path anyway. -- WHN 19990510
1326 (not (eq (layout-class new-layout) (layout-class old-layout)))
1327 (error "shouldn't happen: weird state of OLD-LAYOUT?"))
1328 ((not *type-system-initialized*)
1329 (setf (layout-info old-layout) info)
1330 (values class old-layout nil))
1331 ((redefine-layout-warning old-context
1334 (layout-length new-layout)
1335 (layout-inherits new-layout)
1336 (layout-depthoid new-layout))
1337 (values class new-layout old-layout))
1339 (let ((old-info (layout-info old-layout)))
1341 ((or defstruct-description)
1342 (cond ((redefine-structure-warning class old-info info)
1343 (values class new-layout old-layout))
1345 (setf (layout-info old-layout) info)
1346 (values class old-layout nil))))
1348 (setf (layout-info old-layout) info)
1349 (values class old-layout nil))
1351 (error "shouldn't happen! strange thing in LAYOUT-INFO:~% ~S"
1353 (values class new-layout old-layout)))))))))
1355 ;;; Blow away all the compiler info for the structure CLASS. Iterate
1356 ;;; over this type, clearing the compiler structure type info, and
1357 ;;; undefining all the associated functions.
1358 (defun undefine-structure (class)
1359 #+sb-xc (/show0 "entering UNDEFINE-STRUCTURE")
1360 (let ((info (layout-info (class-layout class))))
1361 (when (defstruct-description-p info)
1362 (let ((type (dd-name info)))
1363 (remhash type *typecheckfuns*)
1364 (setf (info :type :compiler-layout type) nil)
1365 (undefine-fun-name (dd-copier-name info))
1366 (undefine-fun-name (dd-predicate-name info))
1367 (dolist (slot (dd-slots info))
1368 (let ((fun (dsd-accessor-name slot)))
1369 (undefine-fun-name fun)
1370 (unless (dsd-read-only slot)
1371 (undefine-fun-name `(setf ,fun))))))
1372 ;; Clear out the SPECIFIER-TYPE cache so that subsequent
1373 ;; references are unknown types.
1374 (values-specifier-type-cache-clear)))
1375 #+sb-xc (/show0 "leaving UNDEFINE-STRUCTURE")
1378 ;;; Return a list of pairs (name . index). Used for :TYPE'd
1379 ;;; constructors to find all the names that we have to splice in &
1380 ;;; where. Note that these types don't have a layout, so we can't look
1381 ;;; at LAYOUT-INHERITS.
1382 (defun find-name-indices (defstruct)
1385 (do ((info defstruct
1386 (typed-structure-info-or-lose (first (dd-include info)))))
1387 ((not (dd-include info))
1392 (dolist (info infos)
1393 (incf i (or (dd-offset info) 0))
1394 (when (dd-named info)
1395 (res (cons (dd-name info) i)))
1396 (setq i (dd-length info)))))
1400 ;;;; slot accessors for raw slots
1402 ;;; Return info about how to read/write a slot in the value stored in
1403 ;;; OBJECT. This is also used by constructors (since we can't safely
1404 ;;; use the accessor function, since some slots are read-only). If
1405 ;;; supplied, DATA is a variable holding the raw-data vector.
1407 ;;; returned values:
1408 ;;; 1. accessor function name (SETFable)
1409 ;;; 2. index to pass to accessor.
1410 ;;; 3. object form to pass to accessor
1411 (defun slot-accessor-form (defstruct slot object &optional data)
1412 (let ((rtype (dsd-raw-type slot)))
1415 (single-float '%raw-ref-single)
1416 (double-float '%raw-ref-double)
1418 (long-float '%raw-ref-long)
1419 (complex-single-float '%raw-ref-complex-single)
1420 (complex-double-float '%raw-ref-complex-double)
1422 (complex-long-float '%raw-ref-complex-long)
1423 (unsigned-byte 'aref)
1424 ((t) '%instance-ref))
1428 (truncate (dsd-index slot) #!+x86 6 #!+sparc 8))
1431 (truncate (dsd-index slot) #!+x86 3 #!+sparc 4))
1433 (ash (dsd-index slot) -1))
1434 (complex-double-float
1435 (ash (dsd-index slot) -2))
1436 (complex-single-float
1437 (ash (dsd-index slot) -1))
1441 ((eq rtype t) object)
1444 `(truly-the (simple-array (unsigned-byte 32) (*))
1445 (%instance-ref ,object ,(dd-raw-index defstruct))))))))
1447 ;;; These functions are called to actually make a constructor after we
1448 ;;; have processed the arglist. The correct variant (according to the
1449 ;;; DD-TYPE) should be called. The function is defined with the
1450 ;;; specified name and arglist. VARS and TYPES are used for argument
1451 ;;; type declarations. VALUES are the values for the slots (in order.)
1453 ;;; This is split three ways because:
1454 ;;; * LIST & VECTOR structures need "name" symbols stuck in at
1455 ;;; various weird places, whereas STRUCTURE structures have
1457 ;;; * We really want to use LIST to make list structures, instead of
1458 ;;; MAKE-LIST/(SETF ELT).
1459 ;;; * STRUCTURE structures can have raw slots that must also be
1460 ;;; allocated and indirectly referenced. We use SLOT-ACCESSOR-FORM
1461 ;;; to compute how to set the slots, which deals with raw slots.
1462 (defun create-vector-constructor (dd cons-name arglist vars types values)
1463 (let ((temp (gensym))
1464 (etype (dd-element-type dd)))
1465 `(defun ,cons-name ,arglist
1466 (declare ,@(mapcar #'(lambda (var type) `(type (and ,type ,etype) ,var))
1468 (let ((,temp (make-array ,(dd-length dd)
1469 :element-type ',(dd-element-type dd))))
1470 ,@(mapcar #'(lambda (x)
1471 `(setf (aref ,temp ,(cdr x)) ',(car x)))
1472 (find-name-indices dd))
1473 ,@(mapcar #'(lambda (dsd value)
1474 `(setf (aref ,temp ,(dsd-index dsd)) ,value))
1475 (dd-slots dd) values)
1477 (defun create-list-constructor (dd cons-name arglist vars types values)
1478 (let ((vals (make-list (dd-length dd) :initial-element nil)))
1479 (dolist (x (find-name-indices dd))
1480 (setf (elt vals (cdr x)) `',(car x)))
1481 (loop for dsd in (dd-slots dd) and val in values do
1482 (setf (elt vals (dsd-index dsd)) val))
1484 `(defun ,cons-name ,arglist
1485 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1488 (defun create-structure-constructor (dd cons-name arglist vars types values)
1489 (let* ((temp (gensym))
1490 (raw-index (dd-raw-index dd))
1491 (n-raw-data (when raw-index (gensym))))
1492 `(defun ,cons-name ,arglist
1493 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1495 (let ((,temp (truly-the ,(dd-name dd)
1496 (%make-instance ,(dd-length dd))))
1499 (make-array ,(dd-raw-length dd)
1500 :element-type '(unsigned-byte 32))))))
1501 (setf (%instance-layout ,temp)
1502 (%delayed-get-compiler-layout ,(dd-name dd)))
1504 `((setf (%instance-ref ,temp ,raw-index) ,n-raw-data)))
1505 ,@(mapcar (lambda (dsd value)
1506 (multiple-value-bind (accessor index data)
1507 (slot-accessor-form dd dsd temp n-raw-data)
1508 `(setf (,accessor ,data ,index) ,value)))
1513 ;;; Create a default (non-BOA) keyword constructor.
1514 (defun create-keyword-constructor (defstruct creator)
1515 (collect ((arglist (list '&key))
1518 (dolist (slot (dd-slots defstruct))
1519 (let ((dum (gensym))
1520 (name (dsd-name slot)))
1521 (arglist `((,(keywordicate name) ,dum) ,(dsd-default slot)))
1522 (types (dsd-type slot))
1525 defstruct (dd-default-constructor defstruct)
1526 (arglist) (vals) (types) (vals))))
1528 ;;; Given a structure and a BOA constructor spec, call CREATOR with
1529 ;;; the appropriate args to make a constructor.
1530 (defun create-boa-constructor (defstruct boa creator)
1531 (multiple-value-bind (req opt restp rest keyp keys allowp aux)
1532 (sb!kernel:parse-lambda-list (second boa))
1536 (labels ((get-slot (name)
1537 (let ((res (find name (dd-slots defstruct)
1541 (values (dsd-type res) (dsd-default res))
1544 (multiple-value-bind (type default) (get-slot arg)
1545 (arglist `(,arg ,default))
1551 (types (get-slot arg)))
1554 (arglist '&optional)
1558 (name &optional (def (nth-value 1 (get-slot name))))
1560 (arglist `(,name ,def))
1562 (types (get-slot name))))
1564 (do-default arg)))))
1567 (arglist '&rest rest)
1575 (destructuring-bind (wot &optional (def nil def-p)) key
1576 (let ((name (if (consp wot)
1577 (destructuring-bind (key var) wot
1578 (declare (ignore key))
1581 (multiple-value-bind (type slot-def) (get-slot name)
1582 (arglist `(,wot ,(if def-p def slot-def)))
1587 (when allowp (arglist '&allow-other-keys))
1592 (let* ((arg (if (consp arg) arg (list arg)))
1596 (types (get-slot var))))))
1598 (funcall creator defstruct (first boa)
1599 (arglist) (vars) (types)
1600 (mapcar #'(lambda (slot)
1601 (or (find (dsd-name slot) (vars) :test #'string=)
1602 (dsd-default slot)))
1603 (dd-slots defstruct))))))
1605 ;;; Grovel the constructor options, and decide what constructors (if
1607 (defun constructor-definitions (defstruct)
1608 (let ((no-constructors nil)
1611 (creator (ecase (dd-type defstruct)
1612 (structure #'create-structure-constructor)
1613 (vector #'create-vector-constructor)
1614 (list #'create-list-constructor))))
1615 (dolist (constructor (dd-constructors defstruct))
1616 (destructuring-bind (name &optional (boa-ll nil boa-p)) constructor
1617 (declare (ignore boa-ll))
1618 (cond ((not name) (setq no-constructors t))
1619 (boa-p (push constructor boas))
1620 (t (push name defaults)))))
1622 (when no-constructors
1623 (when (or defaults boas)
1624 (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs"))
1625 (return-from constructor-definitions ()))
1627 (unless (or defaults boas)
1628 (push (symbolicate "MAKE-" (dd-name defstruct)) defaults))
1632 (let ((cname (first defaults)))
1633 (setf (dd-default-constructor defstruct) cname)
1634 (res (create-keyword-constructor defstruct creator))
1635 (dolist (other-name (rest defaults))
1636 (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1637 (res `(declaim (ftype function ',other-name))))))
1640 (res (create-boa-constructor defstruct boa creator)))
1642 `((/show0 "beginning CONSTRUCTOR-DEFINITIONS forms")
1644 (/show0 "done with CONSTRUCTOR-DEFINITIONS forms")))))
1646 ;;;; instances with ALTERNATE-METACLASS
1648 ;;;; The CMU CL support for structures with ALTERNATE-METACLASS was a
1649 ;;;; fairly general extension embedded in the main DEFSTRUCT code, and
1650 ;;;; the result was an fairly impressive mess as ALTERNATE-METACLASS
1651 ;;;; extension mixed with ANSI CL generality (e.g. :TYPE and :INCLUDE)
1652 ;;;; and CMU CL implementation hairiness (esp. raw slots). This SBCL
1653 ;;;; version is much less ambitious, noticing that ALTERNATE-METACLASS
1654 ;;;; is only used to implement CONDITION, STANDARD-INSTANCE, and
1655 ;;;; GENERIC-FUNCTION, and defining a simple specialized
1656 ;;;; separate-from-DEFSTRUCT macro to provide only enough
1657 ;;;; functionality to support those.
1659 ;;;; KLUDGE: The defining macro here is so specialized that it's ugly
1660 ;;;; in its own way. It also violates once-and-only-once by knowing
1661 ;;;; much about structures and layouts that is already known by the
1662 ;;;; main DEFSTRUCT macro. Hopefully it will go away presently
1663 ;;;; (perhaps when CL:CLASS and SB-PCL:CLASS meet) as per FIXME below.
1664 ;;;; -- WHN 2001-10-28
1666 ;;;; FIXME: There seems to be no good reason to shoehorn CONDITION,
1667 ;;;; STANDARD-INSTANCE, and GENERIC-FUNCTION into mutated structures
1668 ;;;; instead of just implementing them as primitive objects. (This
1669 ;;;; reduced-functionality macro seems pretty close to the
1670 ;;;; functionality of DEFINE-PRIMITIVE-OBJECT..)
1672 (defun make-dd-with-alternate-metaclass (&key (class-name (missing-arg))
1673 (superclass-name (missing-arg))
1674 (metaclass-name (missing-arg))
1675 (dd-type (missing-arg))
1676 metaclass-constructor
1678 (let* ((dd (make-defstruct-description class-name))
1679 (conc-name (concatenate 'string (symbol-name class-name) "-"))
1680 (dd-slots (let ((reversed-result nil)
1681 ;; The index starts at 1 for ordinary
1682 ;; named slots because slot 0 is
1683 ;; magical, used for LAYOUT in
1684 ;; CONDITIONs or for something (?) in
1685 ;; funcallable instances.
1687 (dolist (slot-name slot-names)
1688 (push (make-defstruct-slot-description
1689 :%name (symbol-name slot-name)
1691 :accessor-name (symbolicate conc-name slot-name))
1694 (nreverse reversed-result))))
1695 (setf (dd-alternate-metaclass dd) (list superclass-name
1697 metaclass-constructor)
1698 (dd-slots dd) dd-slots
1699 (dd-length dd) (1+ (length slot-names))
1700 (dd-type dd) dd-type)
1703 (sb!xc:defmacro !defstruct-with-alternate-metaclass
1705 (slot-names (missing-arg))
1706 (boa-constructor (missing-arg))
1707 (superclass-name (missing-arg))
1708 (metaclass-name (missing-arg))
1709 (metaclass-constructor (missing-arg))
1710 (dd-type (missing-arg))
1712 (runtime-type-checks-p t))
1714 (declare (type (and list (not null)) slot-names))
1715 (declare (type (and symbol (not null))
1719 metaclass-constructor))
1720 (declare (type symbol predicate))
1721 (declare (type (member structure funcallable-structure) dd-type))
1723 (/show "entering !DEFSTRUCT-WITH-ALTERNATE-METACLASS expander" class-name)
1724 (let* ((dd (make-dd-with-alternate-metaclass
1725 :class-name class-name
1726 :slot-names slot-names
1727 :superclass-name superclass-name
1728 :metaclass-name metaclass-name
1729 :metaclass-constructor metaclass-constructor
1731 (conc-name (concatenate 'string (symbol-name class-name) "-"))
1732 (dd-slots (dd-slots dd))
1733 (dd-length (1+ (length slot-names)))
1734 (object-gensym (gensym "OBJECT"))
1735 (new-value-gensym (gensym "NEW-VALUE-"))
1736 (delayed-layout-form `(%delayed-get-compiler-layout ,class-name)))
1737 (multiple-value-bind (raw-maker-form raw-reffer-operator)
1740 (values `(let ((,object-gensym (%make-instance ,dd-length)))
1741 (setf (%instance-layout ,object-gensym)
1742 ,delayed-layout-form)
1745 (funcallable-structure
1746 (values `(%make-funcallable-instance ,dd-length
1747 ,delayed-layout-form)
1748 '%funcallable-instance-info)))
1749 (/show dd raw-maker-form raw-reffer-operator)
1752 (eval-when (:compile-toplevel :load-toplevel :execute)
1753 (%compiler-set-up-layout ',dd))
1755 ;; slot readers and writers
1756 (declaim (inline ,@(mapcar #'dsd-accessor-name dd-slots)))
1757 ,@(mapcar (lambda (dsd)
1758 `(defun ,(dsd-accessor-name dsd) (,object-gensym)
1759 ,@(when runtime-type-checks-p
1760 `((declare (type ,class-name ,object-gensym))))
1761 (,raw-reffer-operator ,object-gensym
1764 (declaim (inline ,@(mapcar (lambda (dsd)
1765 `(setf ,(dsd-accessor-name dsd)))
1767 ,@(mapcar (lambda (dsd)
1768 `(defun (setf ,(dsd-accessor-name dsd)) (,new-value-gensym
1770 ,@(when runtime-type-checks-p
1771 `((declare (type ,class-name ,object-gensym))))
1772 (setf (,raw-reffer-operator ,object-gensym
1774 ,new-value-gensym)))
1778 (defun ,boa-constructor ,slot-names
1779 (let ((,object-gensym ,raw-maker-form))
1780 ,@(mapcar (lambda (slot-name)
1781 (let ((dsd (find (symbol-name slot-name) dd-slots
1784 `(setf (,(dsd-accessor-name dsd) ,object-gensym)
1791 ;; Just delegate to the compiler's type optimization
1792 ;; code, which knows how to generate inline type tests
1793 ;; for the whole CMU CL INSTANCE menagerie.
1794 `(defun ,predicate (,object-gensym)
1795 (typep ,object-gensym ',class-name)))))))
1797 ;;;; finalizing bootstrapping
1799 ;;; Set up DD and LAYOUT for STRUCTURE-OBJECT class itself.
1801 ;;; Ordinary structure classes effectively :INCLUDE STRUCTURE-OBJECT
1802 ;;; when they have no explicit :INCLUDEs, so (1) it needs to be set up
1803 ;;; before we can define ordinary structure classes, and (2) it's
1804 ;;; special enough (and simple enough) that we just build it by hand
1805 ;;; instead of trying to generalize the ordinary DEFSTRUCT code.
1806 (defun !set-up-structure-object-class ()
1807 (/show0 "entering !SET-UP-STRUCTURE-OBJECT-CLASS")
1808 (let ((dd (make-defstruct-description 'structure-object)))
1810 ;; Note: This has an ALTERNATE-METACLASS only because of blind
1811 ;; clueless imitation of the CMU CL code -- dunno if or why it's
1813 (dd-alternate-metaclass dd) '(instance)
1816 (dd-type dd) 'structure)
1817 (/show0 "about to %COMPILER-SET-UP-LAYOUT")
1818 (%compiler-set-up-layout dd))
1819 (/show0 "leaving !SET-UP-STRUCTURE-OBJECT-CLASS"))
1820 (!set-up-structure-object-class)
1822 ;;; early structure predeclarations: Set up DD and LAYOUT for ordinary
1823 ;;; (non-ALTERNATE-METACLASS) structures which are needed early.
1825 '#.(sb-cold:read-from-file
1826 "src/code/early-defstruct-args.lisp-expr"))
1827 (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
1830 (inherits (inherits-for-structure dd)))
1831 (%compiler-defstruct dd inherits)))
1833 (/show0 "code/defstruct.lisp end of file")