1.0.17.4: support for dynamic-extent structures
[sbcl/tcr.git] / src / code / defstruct.lisp
blobc7575501eb4c9738b069e552d44effd455bd26b5
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
5 ;;;; more information.
6 ;;;;
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")
17 ;;;; getting LAYOUTs
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)))
23 (cond ((not res)
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))
27 (t res))))
29 (defun compiler-layout-ready-p (name)
30 (let ((layout (info :type :compiler-layout name)))
31 (and layout (typep (layout-info layout) 'defstruct-description))))
33 (sb!xc:defmacro %make-structure-instance-macro (dd slot-specs &rest slot-vars)
34 `(truly-the ,(dd-name dd)
35 ,(if (compiler-layout-ready-p (dd-name dd))
36 `(%make-structure-instance ,dd ,slot-specs ,@slot-vars)
37 ;; Non-toplevel defstructs don't have a layout at compile time,
38 ;; so we need to construct the actual function at runtime -- but
39 ;; we cache it at the call site, so that we don't perform quite
40 ;; so horribly.
41 `(let* ((cell (load-time-value (list nil)))
42 (fun (car cell)))
43 (if (functionp fun)
44 (funcall fun ,@slot-vars)
45 (funcall (setf (car cell)
46 (%make-structure-instance-allocator ,dd ,slot-specs))
47 ,@slot-vars))))))
49 (declaim (ftype (sfunction (defstruct-description list) function)
50 %Make-structure-instance-allocator))
51 (defun %make-structure-instance-allocator (dd slot-specs)
52 (let ((vars (make-gensym-list (length slot-specs))))
53 (values (compile nil `(lambda (,@vars)
54 (%make-structure-instance-macro ,dd ',slot-specs ,@vars))))))
56 ;;; Delay looking for compiler-layout until the constructor is being
57 ;;; compiled, since it doesn't exist until after the EVAL-WHEN
58 ;;; (COMPILE) stuff is compiled. (Or, in the oddball case when
59 ;;; DEFSTRUCT is executing in a non-toplevel context, the
60 ;;; compiler-layout still doesn't exist at compilation time, and we
61 ;;; delay still further.)
62 (sb!xc:defmacro %delayed-get-compiler-layout (name)
63 (let ((layout (info :type :compiler-layout name)))
64 (cond (layout
65 ;; ordinary case: When the DEFSTRUCT is at top level,
66 ;; then EVAL-WHEN (COMPILE) stuff will have set up the
67 ;; layout for us to use.
68 (unless (typep (layout-info layout) 'defstruct-description)
69 (error "Class is not a structure class: ~S" name))
70 `,layout)
72 ;; KLUDGE: In the case that DEFSTRUCT is not at top-level
73 ;; the layout doesn't exist at compile time. In that case
74 ;; we laboriously look it up at run time. This code will
75 ;; run on every constructor call and will likely be quite
76 ;; slow, so if anyone cares about performance of
77 ;; non-toplevel DEFSTRUCTs, it should be rewritten to be
78 ;; cleverer. -- WHN 2002-10-23
79 (sb!c:compiler-notify
80 "implementation limitation: ~
81 Non-toplevel DEFSTRUCT constructors are slow.")
82 (with-unique-names (layout)
83 `(let ((,layout (info :type :compiler-layout ',name)))
84 (unless (typep (layout-info ,layout) 'defstruct-description)
85 (error "Class is not a structure class: ~S" ',name))
86 ,layout))))))
88 ;;; re. %DELAYED-GET-COMPILER-LAYOUT and COMPILE-TIME-FIND-LAYOUT, above..
89 ;;;
90 ;;; FIXME: Perhaps both should be defined with DEFMACRO-MUNDANELY?
91 ;;; FIXME: Do we really need both? If so, their names and implementations
92 ;;; should probably be tweaked to be more parallel.
94 ;;;; DEFSTRUCT-DESCRIPTION
96 ;;; The DEFSTRUCT-DESCRIPTION structure holds compile-time information
97 ;;; about a structure type.
98 (def!struct (defstruct-description
99 (:conc-name dd-)
100 (:make-load-form-fun just-dump-it-normally)
101 #-sb-xc-host (:pure t)
102 (:constructor make-defstruct-description
103 (name &aux
104 (conc-name (symbolicate name "-"))
105 (copier-name (symbolicate "COPY-" name))
106 (predicate-name (symbolicate name "-P")))))
107 ;; name of the structure
108 (name (missing-arg) :type symbol :read-only t)
109 ;; documentation on the structure
110 (doc nil :type (or string null))
111 ;; prefix for slot names. If NIL, none.
112 (conc-name nil :type (or symbol null))
113 ;; the name of the primary standard keyword constructor, or NIL if none
114 (default-constructor nil :type (or symbol null))
115 ;; all the explicit :CONSTRUCTOR specs, with name defaulted
116 (constructors () :type list)
117 ;; name of copying function
118 (copier-name nil :type (or symbol null))
119 ;; name of type predicate
120 (predicate-name nil :type (or symbol null))
121 ;; the arguments to the :INCLUDE option, or NIL if no included
122 ;; structure
123 (include nil :type list)
124 ;; properties used to define structure-like classes with an
125 ;; arbitrary superclass and that may not have STRUCTURE-CLASS as the
126 ;; metaclass. Syntax is:
127 ;; (superclass-name metaclass-name metaclass-constructor)
128 (alternate-metaclass nil :type list)
129 ;; a list of DEFSTRUCT-SLOT-DESCRIPTION objects for all slots
130 ;; (including included ones)
131 (slots () :type list)
132 ;; a list of (NAME . INDEX) pairs for accessors of included structures
133 (inherited-accessor-alist () :type list)
134 ;; number of elements we've allocated (See also RAW-LENGTH, which is not
135 ;; included in LENGTH.)
136 (length 0 :type index)
137 ;; General kind of implementation.
138 (type 'structure :type (member structure vector list
139 funcallable-structure))
141 ;; The next three slots are for :TYPE'd structures (which aren't
142 ;; classes, DD-CLASS-P = NIL)
144 ;; vector element type
145 (element-type t)
146 ;; T if :NAMED was explicitly specified, NIL otherwise
147 (named nil :type boolean)
148 ;; any INITIAL-OFFSET option on this direct type
149 (offset nil :type (or index null))
151 ;; the argument to the PRINT-FUNCTION option, or NIL if a
152 ;; PRINT-FUNCTION option was given with no argument, or 0 if no
153 ;; PRINT-FUNCTION option was given
154 (print-function 0 :type (or cons symbol (member 0)))
155 ;; the argument to the PRINT-OBJECT option, or NIL if a PRINT-OBJECT
156 ;; option was given with no argument, or 0 if no PRINT-OBJECT option
157 ;; was given
158 (print-object 0 :type (or cons symbol (member 0)))
159 ;; The number of untagged slots at the end.
160 (raw-length 0 :type index)
161 ;; the value of the :PURE option, or :UNSPECIFIED. This is only
162 ;; meaningful if DD-CLASS-P = T.
163 (pure :unspecified :type (member t nil :substructure :unspecified)))
164 (def!method print-object ((x defstruct-description) stream)
165 (print-unreadable-object (x stream :type t)
166 (prin1 (dd-name x) stream)))
168 ;;; Does DD describe a structure with a class?
169 (defun dd-class-p (dd)
170 (member (dd-type dd)
171 '(structure funcallable-structure)))
173 ;;; a type name which can be used when declaring things which operate
174 ;;; on structure instances
175 (defun dd-declarable-type (dd)
176 (if (dd-class-p dd)
177 ;; Native classes are known to the type system, and we can
178 ;; declare them as types.
179 (dd-name dd)
180 ;; Structures layered on :TYPE LIST or :TYPE VECTOR aren't part
181 ;; of the type system, so all we can declare is the underlying
182 ;; LIST or VECTOR type.
183 (dd-type dd)))
185 (defun dd-layout-or-lose (dd)
186 (compiler-layout-or-lose (dd-name dd)))
188 ;;;; DEFSTRUCT-SLOT-DESCRIPTION
190 ;;; A DEFSTRUCT-SLOT-DESCRIPTION holds compile-time information about
191 ;;; a structure slot.
192 (def!struct (defstruct-slot-description
193 (:make-load-form-fun just-dump-it-normally)
194 (:conc-name dsd-)
195 (:copier nil)
196 #-sb-xc-host (:pure t))
197 ;; name of slot
198 name
199 ;; its position in the implementation sequence
200 (index (missing-arg) :type fixnum)
201 ;; the name of the accessor function
203 ;; (CMU CL had extra complexity here ("..or NIL if this accessor has
204 ;; the same name as an inherited accessor (which we don't want to
205 ;; shadow)") but that behavior doesn't seem to be specified by (or
206 ;; even particularly consistent with) ANSI, so it's gone in SBCL.)
207 (accessor-name nil)
208 default ; default value expression
209 (type t) ; declared type specifier
210 (safe-p t :type boolean) ; whether the slot is known to be
211 ; always of the specified type
212 ;; If this object does not describe a raw slot, this value is T.
214 ;; If this object describes a raw slot, this value is the type of the
215 ;; value that the raw slot holds.
216 (raw-type t :type (member t single-float double-float
217 #!+long-float long-float
218 complex-single-float complex-double-float
219 #!+long-float complex-long-float
220 sb!vm:word))
221 (read-only nil :type (member t nil)))
222 (def!method print-object ((x defstruct-slot-description) stream)
223 (print-unreadable-object (x stream :type t)
224 (prin1 (dsd-name x) stream)))
226 ;;;; typed (non-class) structures
228 ;;; Return a type specifier we can use for testing :TYPE'd structures.
229 (defun dd-lisp-type (defstruct)
230 (ecase (dd-type defstruct)
231 (list 'list)
232 (vector `(simple-array ,(dd-element-type defstruct) (*)))))
234 ;;;; shared machinery for inline and out-of-line slot accessor functions
236 ;;; Classic comment preserved for entertainment value:
238 ;;; "A lie can travel halfway round the world while the truth is
239 ;;; putting on its shoes." -- Mark Twain
241 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
243 ;; information about how a slot of a given DSD-RAW-TYPE is to be accessed
244 (defstruct raw-slot-data
245 ;; the raw slot type, or T for a non-raw slot
247 ;; (Non-raw slots are in the ordinary place you'd expect, directly
248 ;; indexed off the instance pointer. Raw slots are indexed from the end
249 ;; of the instance and skipped by GC.)
250 (raw-type (missing-arg) :type (or symbol cons) :read-only t)
251 ;; What operator is used to access a slot of this type?
252 (accessor-name (missing-arg) :type symbol :read-only t)
253 (init-vop (missing-arg) :type symbol :read-only t)
254 ;; How many words are each value of this type?
255 (n-words (missing-arg) :type (and index (integer 1)) :read-only t)
256 ;; Necessary alignment in units of words. Note that instances
257 ;; themselves are aligned by exactly two words, so specifying more
258 ;; than two words here would not work.
259 (alignment 1 :type (integer 1 2) :read-only t))
261 (defvar *raw-slot-data-list*
262 #!+hppa
264 #!-hppa
265 (let ((double-float-alignment
266 ;; white list of architectures that can load unaligned doubles:
267 #!+(or x86 x86-64 ppc) 1
268 ;; at least sparc, mips and alpha can't:
269 #!-(or x86 x86-64 ppc) 2))
270 (list
271 (make-raw-slot-data :raw-type 'sb!vm:word
272 :accessor-name '%raw-instance-ref/word
273 :init-vop 'sb!vm::raw-instance-init/word
274 :n-words 1)
275 (make-raw-slot-data :raw-type 'single-float
276 :accessor-name '%raw-instance-ref/single
277 :init-vop 'sb!vm::raw-instance-init/single
278 ;; KLUDGE: On 64 bit architectures, we
279 ;; could pack two SINGLE-FLOATs into the
280 ;; same word if raw slots were indexed
281 ;; using bytes instead of words. However,
282 ;; I don't personally find optimizing
283 ;; SINGLE-FLOAT memory usage worthwile
284 ;; enough. And the other datatype that
285 ;; would really benefit is (UNSIGNED-BYTE
286 ;; 32), but that is a subtype of FIXNUM, so
287 ;; we store it unraw anyway. :-( -- DFL
288 :n-words 1)
289 (make-raw-slot-data :raw-type 'double-float
290 :accessor-name '%raw-instance-ref/double
291 :init-vop 'sb!vm::raw-instance-init/double
292 :alignment double-float-alignment
293 :n-words (/ 8 sb!vm:n-word-bytes))
294 (make-raw-slot-data :raw-type 'complex-single-float
295 :accessor-name '%raw-instance-ref/complex-single
296 :init-vop 'sb!vm::raw-instance-init/complex-single
297 :n-words (/ 8 sb!vm:n-word-bytes))
298 (make-raw-slot-data :raw-type 'complex-double-float
299 :accessor-name '%raw-instance-ref/complex-double
300 :init-vop 'sb!vm::raw-instance-init/complex-double
301 :alignment double-float-alignment
302 :n-words (/ 16 sb!vm:n-word-bytes))
303 #!+long-float
304 (make-raw-slot-data :raw-type long-float
305 :accessor-name '%raw-instance-ref/long
306 :init-vop 'sb!vm::raw-instance-init/long
307 :n-words #!+x86 3 #!+sparc 4)
308 #!+long-float
309 (make-raw-slot-data :raw-type complex-long-float
310 :accessor-name '%raw-instance-ref/complex-long
311 :init-vop 'sb!vm::raw-instance-init/complex-long
312 :n-words #!+x86 6 #!+sparc 8)))))
314 ;;;; the legendary DEFSTRUCT macro itself (both CL:DEFSTRUCT and its
315 ;;;; close personal friend SB!XC:DEFSTRUCT)
317 ;;; Return a list of forms to install PRINT and MAKE-LOAD-FORM funs,
318 ;;; mentioning them in the expansion so that they can be compiled.
319 (defun class-method-definitions (defstruct)
320 (let ((name (dd-name defstruct)))
321 `((locally
322 ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant
323 ;; class names which creates fast but non-cold-loadable,
324 ;; non-compact code. In this context, we'd rather have
325 ;; compact, cold-loadable code. -- WHN 19990928
326 (declare (notinline find-classoid))
327 ,@(let ((pf (dd-print-function defstruct))
328 (po (dd-print-object defstruct))
329 (x (gensym))
330 (s (gensym)))
331 ;; Giving empty :PRINT-OBJECT or :PRINT-FUNCTION options
332 ;; leaves PO or PF equal to NIL. The user-level effect is
333 ;; to generate a PRINT-OBJECT method specialized for the type,
334 ;; implementing the default #S structure-printing behavior.
335 (when (or (eq pf nil) (eq po nil))
336 (setf pf '(default-structure-print)
337 po 0))
338 (flet (;; Given an arg from a :PRINT-OBJECT or :PRINT-FUNCTION
339 ;; option, return the value to pass as an arg to FUNCTION.
340 (farg (oarg)
341 (destructuring-bind (fun-name) oarg
342 fun-name)))
343 (cond ((not (eql pf 0))
344 `((def!method print-object ((,x ,name) ,s)
345 (funcall #',(farg pf)
348 *current-level-in-print*))))
349 ((not (eql po 0))
350 `((def!method print-object ((,x ,name) ,s)
351 (funcall #',(farg po) ,x ,s))))
352 (t nil))))
353 ,@(let ((pure (dd-pure defstruct)))
354 (cond ((eq pure t)
355 `((setf (layout-pure (classoid-layout
356 (find-classoid ',name)))
357 t)))
358 ((eq pure :substructure)
359 `((setf (layout-pure (classoid-layout
360 (find-classoid ',name)))
361 0)))))
362 ,@(let ((def-con (dd-default-constructor defstruct)))
363 (when (and def-con (not (dd-alternate-metaclass defstruct)))
364 `((setf (structure-classoid-constructor (find-classoid ',name))
365 #',def-con))))))))
367 ;;; shared logic for host macroexpansion for SB!XC:DEFSTRUCT and
368 ;;; cross-compiler macroexpansion for CL:DEFSTRUCT
369 (defmacro !expander-for-defstruct (name-and-options
370 slot-descriptions
371 expanding-into-code-for-xc-host-p)
372 `(let ((name-and-options ,name-and-options)
373 (slot-descriptions ,slot-descriptions)
374 (expanding-into-code-for-xc-host-p
375 ,expanding-into-code-for-xc-host-p))
376 (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
377 name-and-options
378 slot-descriptions))
379 (name (dd-name dd)))
380 (if (dd-class-p dd)
381 (let ((inherits (inherits-for-structure dd)))
382 `(progn
383 ;; Note we intentionally enforce package locks and
384 ;; call %DEFSTRUCT first, and especially before
385 ;; %COMPILER-DEFSTRUCT. %DEFSTRUCT has the tests (and
386 ;; resulting CERROR) for collisions with LAYOUTs which
387 ;; already exist in the runtime. If there are any
388 ;; collisions, we want the user's response to CERROR
389 ;; to control what happens. Especially, if the user
390 ;; responds to the collision with ABORT, we don't want
391 ;; %COMPILER-DEFSTRUCT to modify the definition of the
392 ;; class.
393 (with-single-package-locked-error
394 (:symbol ',name "defining ~A as a structure"))
395 (%defstruct ',dd ',inherits (sb!c:source-location))
396 (eval-when (:compile-toplevel :load-toplevel :execute)
397 (%compiler-defstruct ',dd ',inherits))
398 ,@(unless expanding-into-code-for-xc-host-p
399 (append ;; FIXME: We've inherited from CMU CL nonparallel
400 ;; code for creating copiers for typed and untyped
401 ;; structures. This should be fixed.
402 ;(copier-definition dd)
403 (constructor-definitions dd)
404 (class-method-definitions dd)))
405 ',name))
406 `(progn
407 (with-single-package-locked-error
408 (:symbol ',name "defining ~A as a structure"))
409 (eval-when (:compile-toplevel :load-toplevel :execute)
410 (setf (info :typed-structure :info ',name) ',dd))
411 (eval-when (:load-toplevel :execute)
412 (setf (info :source-location :typed-structure ',name)
413 (sb!c:source-location)))
414 ,@(unless expanding-into-code-for-xc-host-p
415 (append (typed-accessor-definitions dd)
416 (typed-predicate-definitions dd)
417 (typed-copier-definitions dd)
418 (constructor-definitions dd)
419 (when (dd-doc dd)
420 `((setf (fdocumentation ',(dd-name dd) 'structure)
421 ',(dd-doc dd))))))
422 ',name)))))
424 (sb!xc:defmacro defstruct (name-and-options &rest slot-descriptions)
425 #!+sb-doc
426 "DEFSTRUCT {Name | (Name Option*)} {Slot | (Slot [Default] {Key Value}*)}
427 Define the structure type Name. Instances are created by MAKE-<name>,
428 which takes &KEY arguments allowing initial slot values to the specified.
429 A SETF'able function <name>-<slot> is defined for each slot to read and
430 write slot values. <name>-p is a type predicate.
432 Popular DEFSTRUCT options (see manual for others):
434 (:CONSTRUCTOR Name)
435 (:PREDICATE Name)
436 Specify the name for the constructor or predicate.
438 (:CONSTRUCTOR Name Lambda-List)
439 Specify the name and arguments for a BOA constructor
440 (which is more efficient when keyword syntax isn't necessary.)
442 (:INCLUDE Supertype Slot-Spec*)
443 Make this type a subtype of the structure type Supertype. The optional
444 Slot-Specs override inherited slot options.
446 Slot options:
448 :TYPE Type-Spec
449 Asserts that the value of this slot is always of the specified type.
451 :READ-ONLY {T | NIL}
452 If true, no setter function is defined for this slot."
453 (!expander-for-defstruct name-and-options slot-descriptions nil))
454 #+sb-xc-host
455 (defmacro sb!xc:defstruct (name-and-options &rest slot-descriptions)
456 #!+sb-doc
457 "Cause information about a target structure to be built into the
458 cross-compiler."
459 (!expander-for-defstruct name-and-options slot-descriptions t))
461 ;;;; functions to generate code for various parts of DEFSTRUCT definitions
463 ;;; First, a helper to determine whether a name names an inherited
464 ;;; accessor.
465 (defun accessor-inherited-data (name defstruct)
466 (assoc name (dd-inherited-accessor-alist defstruct) :test #'eq))
468 ;;; Return a list of forms which create a predicate function for a
469 ;;; typed DEFSTRUCT.
470 (defun typed-predicate-definitions (defstruct)
471 (let ((name (dd-name defstruct))
472 (predicate-name (dd-predicate-name defstruct))
473 (argname (gensym)))
474 (when (and predicate-name (dd-named defstruct))
475 (let ((ltype (dd-lisp-type defstruct))
476 (name-index (cdr (car (last (find-name-indices defstruct))))))
477 `((defun ,predicate-name (,argname)
478 (and (typep ,argname ',ltype)
479 ,(cond
480 ((subtypep ltype 'list)
481 `(do ((head (the ,ltype ,argname) (cdr head))
482 (i 0 (1+ i)))
483 ((or (not (consp head)) (= i ,name-index))
484 (and (consp head) (eq ',name (car head))))))
485 ((subtypep ltype 'vector)
486 `(and (= (length (the ,ltype ,argname))
487 ,(dd-length defstruct))
488 (eq ',name (aref (the ,ltype ,argname) ,name-index))))
489 (t (bug "Uncatered-for lisp type in typed DEFSTRUCT: ~S."
490 ltype))))))))))
492 ;;; Return a list of forms to create a copier function of a typed DEFSTRUCT.
493 (defun typed-copier-definitions (defstruct)
494 (when (dd-copier-name defstruct)
495 `((setf (fdefinition ',(dd-copier-name defstruct)) #'copy-seq)
496 (declaim (ftype function ,(dd-copier-name defstruct))))))
498 ;;; Return a list of function definitions for accessing and setting
499 ;;; the slots of a typed DEFSTRUCT. The functions are proclaimed to be
500 ;;; inline, and the types of their arguments and results are declared
501 ;;; as well. We count on the compiler to do clever things with ELT.
502 (defun typed-accessor-definitions (defstruct)
503 (collect ((stuff))
504 (let ((ltype (dd-lisp-type defstruct)))
505 (dolist (slot (dd-slots defstruct))
506 (let ((name (dsd-accessor-name slot))
507 (index (dsd-index slot))
508 (slot-type `(and ,(dsd-type slot)
509 ,(dd-element-type defstruct))))
510 (let ((inherited (accessor-inherited-data name defstruct)))
511 (cond
512 ((not inherited)
513 (stuff `(declaim (inline ,name ,@(unless (dsd-read-only slot)
514 `((setf ,name))))))
515 ;; FIXME: The arguments in the next two DEFUNs should
516 ;; be gensyms. (Otherwise e.g. if NEW-VALUE happened to
517 ;; be the name of a special variable, things could get
518 ;; weird.)
519 (stuff `(defun ,name (structure)
520 (declare (type ,ltype structure))
521 (the ,slot-type (elt structure ,index))))
522 (unless (dsd-read-only slot)
523 (stuff
524 `(defun (setf ,name) (new-value structure)
525 (declare (type ,ltype structure) (type ,slot-type new-value))
526 (setf (elt structure ,index) new-value)))))
527 ((not (= (cdr inherited) index))
528 (style-warn "~@<Non-overwritten accessor ~S does not access ~
529 slot with name ~S (accessing an inherited slot ~
530 instead).~:@>" name (dsd-name slot))))))))
531 (stuff)))
533 ;;;; parsing
535 (defun require-no-print-options-so-far (defstruct)
536 (unless (and (eql (dd-print-function defstruct) 0)
537 (eql (dd-print-object defstruct) 0))
538 (error "No more than one of the following options may be specified:
539 :PRINT-FUNCTION, :PRINT-OBJECT, :TYPE")))
541 ;;; Parse a single DEFSTRUCT option and store the results in DD.
542 (defun parse-1-dd-option (option dd)
543 (let ((args (rest option))
544 (name (dd-name dd)))
545 (case (first option)
546 (:conc-name
547 (destructuring-bind (&optional conc-name) args
548 (setf (dd-conc-name dd)
549 (if (symbolp conc-name)
550 conc-name
551 (make-symbol (string conc-name))))))
552 (:constructor
553 (destructuring-bind (&optional (cname (symbolicate "MAKE-" name))
554 &rest stuff)
555 args
556 (push (cons cname stuff) (dd-constructors dd))))
557 (:copier
558 (destructuring-bind (&optional (copier (symbolicate "COPY-" name)))
559 args
560 (setf (dd-copier-name dd) copier)))
561 (:predicate
562 (destructuring-bind (&optional (predicate-name (symbolicate name "-P")))
563 args
564 (setf (dd-predicate-name dd) predicate-name)))
565 (:include
566 (when (dd-include dd)
567 (error "more than one :INCLUDE option"))
568 (setf (dd-include dd) args))
569 (:print-function
570 (require-no-print-options-so-far dd)
571 (setf (dd-print-function dd)
572 (the (or symbol cons) args)))
573 (:print-object
574 (require-no-print-options-so-far dd)
575 (setf (dd-print-object dd)
576 (the (or symbol cons) args)))
577 (:type
578 (destructuring-bind (type) args
579 (cond ((member type '(list vector))
580 (setf (dd-element-type dd) t)
581 (setf (dd-type dd) type))
582 ((and (consp type) (eq (first type) 'vector))
583 (destructuring-bind (vector vtype) type
584 (declare (ignore vector))
585 (setf (dd-element-type dd) vtype)
586 (setf (dd-type dd) 'vector)))
588 (error "~S is a bad :TYPE for DEFSTRUCT." type)))))
589 (:named
590 (error "The DEFSTRUCT option :NAMED takes no arguments."))
591 (:initial-offset
592 (destructuring-bind (offset) args
593 (setf (dd-offset dd) offset)))
594 (:pure
595 (destructuring-bind (fun) args
596 (setf (dd-pure dd) fun)))
597 (t (error "unknown DEFSTRUCT option:~% ~S" option)))))
599 ;;; Given name and options, return a DD holding that info.
600 (defun parse-defstruct-name-and-options (name-and-options)
601 (destructuring-bind (name &rest options) name-and-options
602 (aver name) ; A null name doesn't seem to make sense here.
603 (let ((dd (make-defstruct-description name)))
604 (dolist (option options)
605 (cond ((eq option :named)
606 (setf (dd-named dd) t))
607 ((consp option)
608 (parse-1-dd-option option dd))
609 ((member option '(:conc-name :constructor :copier :predicate))
610 (parse-1-dd-option (list option) dd))
612 (error "unrecognized DEFSTRUCT option: ~S" option))))
614 (case (dd-type dd)
615 (structure
616 (when (dd-offset dd)
617 (error ":OFFSET can't be specified unless :TYPE is specified."))
618 (unless (dd-include dd)
619 ;; FIXME: It'd be cleaner to treat no-:INCLUDE as defaulting
620 ;; to :INCLUDE STRUCTURE-OBJECT, and then let the general-case
621 ;; (INCF (DD-LENGTH DD) (DD-LENGTH included-DD)) logic take
622 ;; care of this. (Except that the :TYPE VECTOR and :TYPE
623 ;; LIST cases, with their :NAMED and un-:NAMED flavors,
624 ;; make that messy, alas.)
625 (incf (dd-length dd))))
627 (require-no-print-options-so-far dd)
628 (when (dd-named dd)
629 (incf (dd-length dd)))
630 (let ((offset (dd-offset dd)))
631 (when offset (incf (dd-length dd) offset)))))
633 (when (dd-include dd)
634 (frob-dd-inclusion-stuff dd))
636 dd)))
638 ;;; Given name and options and slot descriptions (and possibly doc
639 ;;; string at the head of slot descriptions) return a DD holding that
640 ;;; info.
641 (defun parse-defstruct-name-and-options-and-slot-descriptions
642 (name-and-options slot-descriptions)
643 (let ((result (parse-defstruct-name-and-options (if (atom name-and-options)
644 (list name-and-options)
645 name-and-options))))
646 (when (stringp (car slot-descriptions))
647 (setf (dd-doc result) (pop slot-descriptions)))
648 (dolist (slot-description slot-descriptions)
649 (allocate-1-slot result (parse-1-dsd result slot-description)))
650 result))
652 ;;;; stuff to parse slot descriptions
654 ;;; Parse a slot description for DEFSTRUCT, add it to the description
655 ;;; and return it. If supplied, SLOT is a pre-initialized DSD
656 ;;; that we modify to get the new slot. This is supplied when handling
657 ;;; included slots.
658 (defun parse-1-dsd (defstruct spec &optional
659 (slot (make-defstruct-slot-description :name ""
660 :index 0
661 :type t)))
662 (multiple-value-bind (name default default-p type type-p read-only ro-p)
663 (typecase spec
664 (symbol
665 (when (keywordp spec)
666 (style-warn "Keyword slot name indicates probable syntax ~
667 error in DEFSTRUCT: ~S."
668 spec))
669 spec)
670 (cons
671 (destructuring-bind
672 (name
673 &optional (default nil default-p)
674 &key (type nil type-p) (read-only nil ro-p))
675 spec
676 (values name
677 default default-p
678 (uncross type) type-p
679 read-only ro-p)))
680 (t (error 'simple-program-error
681 :format-control "in DEFSTRUCT, ~S is not a legal slot ~
682 description."
683 :format-arguments (list spec))))
685 (when (find name (dd-slots defstruct)
686 :test #'string=
687 :key (lambda (x) (symbol-name (dsd-name x))))
688 (error 'simple-program-error
689 :format-control "duplicate slot name ~S"
690 :format-arguments (list name)))
691 (setf (dsd-name slot) name)
692 (setf (dd-slots defstruct) (nconc (dd-slots defstruct) (list slot)))
694 (let ((accessor-name (if (dd-conc-name defstruct)
695 (symbolicate (dd-conc-name defstruct) name)
696 name))
697 (predicate-name (dd-predicate-name defstruct)))
698 (setf (dsd-accessor-name slot) accessor-name)
699 (when (eql accessor-name predicate-name)
700 ;; Some adventurous soul has named a slot so that its accessor
701 ;; collides with the structure type predicate. ANSI doesn't
702 ;; specify what to do in this case. As of 2001-09-04, Martin
703 ;; Atzmueller reports that CLISP and Lispworks both give
704 ;; priority to the slot accessor, so that the predicate is
705 ;; overwritten. We might as well do the same (as well as
706 ;; signalling a warning).
707 (style-warn
708 "~@<The structure accessor name ~S is the same as the name of the ~
709 structure type predicate. ANSI doesn't specify what to do in ~
710 this case. We'll overwrite the type predicate with the slot ~
711 accessor, but you can't rely on this behavior, so it'd be wise to ~
712 remove the ambiguity in your code.~@:>"
713 accessor-name)
714 (setf (dd-predicate-name defstruct) nil))
715 ;; FIXME: It would be good to check for name collisions here, but
716 ;; the easy check,
717 ;;x#-sb-xc-host
718 ;;x(when (and (fboundp accessor-name)
719 ;;x (not (accessor-inherited-data accessor-name defstruct)))
720 ;;x (style-warn "redefining ~S in DEFSTRUCT" accessor-name)))
721 ;; which was done until sbcl-0.8.11.18 or so, is wrong: it causes
722 ;; a warning at MACROEXPAND time, when instead the warning should
723 ;; occur not just because the code was constructed, but because it
724 ;; is actually compiled or loaded.
727 (when default-p
728 (setf (dsd-default slot) default))
729 (when type-p
730 (setf (dsd-type slot)
731 (if (eq (dsd-type slot) t)
732 type
733 `(and ,(dsd-type slot) ,type))))
734 (when ro-p
735 (if read-only
736 (setf (dsd-read-only slot) t)
737 (when (dsd-read-only slot)
738 (error "~@<The slot ~S is :READ-ONLY in superclass, and so must ~
739 be :READ-ONLY in subclass.~:@>"
740 (dsd-name slot)))))
741 slot))
743 ;;; When a value of type TYPE is stored in a structure, should it be
744 ;;; stored in a raw slot? Return the matching RAW-SLOT-DATA structure
745 ;; if TYPE should be stored in a raw slot, or NIL if not.
746 (defun structure-raw-slot-data (type)
747 (multiple-value-bind (fixnum? fixnum-certain?)
748 (sb!xc:subtypep type 'fixnum)
749 ;; (The extra test for FIXNUM-CERTAIN? here is intended for
750 ;; bootstrapping the system. In particular, in sbcl-0.6.2, we set up
751 ;; LAYOUT before FIXNUM is defined, and so could bogusly end up
752 ;; putting INDEX-typed values into raw slots if we didn't test
753 ;; FIXNUM-CERTAIN?.)
754 (if (or fixnum? (not fixnum-certain?))
756 (dolist (data *raw-slot-data-list*)
757 (when (sb!xc:subtypep type (raw-slot-data-raw-type data))
758 (return data))))))
760 ;;; Allocate storage for a DSD in DD. This is where we decide whether
761 ;;; a slot is raw or not. Raw objects are aligned on the unit of their size.
762 (defun allocate-1-slot (dd dsd)
763 (let ((rsd
764 (if (eq (dd-type dd) 'structure)
765 (structure-raw-slot-data (dsd-type dsd))
766 nil)))
767 (cond
768 ((null rsd)
769 (setf (dsd-index dsd) (dd-length dd))
770 (incf (dd-length dd)))
772 (let* ((words (raw-slot-data-n-words rsd))
773 (alignment (raw-slot-data-alignment rsd))
774 (off (rem (dd-raw-length dd) alignment)))
775 (unless (zerop off)
776 (incf (dd-raw-length dd) (- alignment off)))
777 (setf (dsd-raw-type dsd) (raw-slot-data-raw-type rsd))
778 (setf (dsd-index dsd) (dd-raw-length dd))
779 (incf (dd-raw-length dd) words)))))
780 (values))
782 (defun typed-structure-info-or-lose (name)
783 (or (info :typed-structure :info name)
784 (error ":TYPE'd DEFSTRUCT ~S not found for inclusion." name)))
786 ;;; Process any included slots pretty much like they were specified.
787 ;;; Also inherit various other attributes.
788 (defun frob-dd-inclusion-stuff (dd)
789 (destructuring-bind (included-name &rest modified-slots) (dd-include dd)
790 (let* ((type (dd-type dd))
791 (included-structure
792 (if (dd-class-p dd)
793 (layout-info (compiler-layout-or-lose included-name))
794 (typed-structure-info-or-lose included-name))))
796 ;; checks on legality
797 (unless (and (eq type (dd-type included-structure))
798 (type= (specifier-type (dd-element-type included-structure))
799 (specifier-type (dd-element-type dd))))
800 (error ":TYPE option mismatch between structures ~S and ~S"
801 (dd-name dd) included-name))
802 (let ((included-classoid (find-classoid included-name nil)))
803 (when included-classoid
804 ;; It's not particularly well-defined to :INCLUDE any of the
805 ;; CMU CL INSTANCE weirdosities like CONDITION or
806 ;; GENERIC-FUNCTION, and it's certainly not ANSI-compliant.
807 (let* ((included-layout (classoid-layout included-classoid))
808 (included-dd (layout-info included-layout)))
809 (when (and (dd-alternate-metaclass included-dd)
810 ;; As of sbcl-0.pre7.73, anyway, STRUCTURE-OBJECT
811 ;; is represented with an ALTERNATE-METACLASS. But
812 ;; it's specifically OK to :INCLUDE (and PCL does)
813 ;; so in this one case, it's OK to include
814 ;; something with :ALTERNATE-METACLASS after all.
815 (not (eql included-name 'structure-object)))
816 (error "can't :INCLUDE class ~S (has alternate metaclass)"
817 included-name)))))
819 (incf (dd-length dd) (dd-length included-structure))
820 (when (dd-class-p dd)
821 (let ((mc (rest (dd-alternate-metaclass included-structure))))
822 (when (and mc (not (dd-alternate-metaclass dd)))
823 (setf (dd-alternate-metaclass dd)
824 (cons included-name mc))))
825 (when (eq (dd-pure dd) :unspecified)
826 (setf (dd-pure dd) (dd-pure included-structure)))
827 (setf (dd-raw-length dd) (dd-raw-length included-structure)))
829 (setf (dd-inherited-accessor-alist dd)
830 (dd-inherited-accessor-alist included-structure))
831 (dolist (included-slot (dd-slots included-structure))
832 (let* ((included-name (dsd-name included-slot))
833 (modified (or (find included-name modified-slots
834 :key (lambda (x) (if (atom x) x (car x)))
835 :test #'string=)
836 `(,included-name))))
837 ;; We stash away an alist of accessors to parents' slots
838 ;; that have already been created to avoid conflicts later
839 ;; so that structures with :INCLUDE and :CONC-NAME (and
840 ;; other edge cases) can work as specified.
841 (when (dsd-accessor-name included-slot)
842 ;; the "oldest" (i.e. highest up the tree of inheritance)
843 ;; will prevail, so don't push new ones on if they
844 ;; conflict.
845 (pushnew (cons (dsd-accessor-name included-slot)
846 (dsd-index included-slot))
847 (dd-inherited-accessor-alist dd)
848 :test #'eq :key #'car))
849 (let ((new-slot (parse-1-dsd dd
850 modified
851 (copy-structure included-slot))))
852 (when (and (neq (dsd-type new-slot) (dsd-type included-slot))
853 (not (sb!xc:subtypep (dsd-type included-slot)
854 (dsd-type new-slot)))
855 (dsd-safe-p included-slot))
856 (setf (dsd-safe-p new-slot) nil)
857 ;; XXX: notify?
858 )))))))
860 ;;;; various helper functions for setting up DEFSTRUCTs
862 ;;; This function is called at macroexpand time to compute the INHERITS
863 ;;; vector for a structure type definition.
864 (defun inherits-for-structure (info)
865 (declare (type defstruct-description info))
866 (let* ((include (dd-include info))
867 (superclass-opt (dd-alternate-metaclass info))
868 (super
869 (if include
870 (compiler-layout-or-lose (first include))
871 (classoid-layout (find-classoid
872 (or (first superclass-opt)
873 'structure-object))))))
874 (case (dd-name info)
875 ((ansi-stream)
876 (concatenate 'simple-vector
877 (layout-inherits super)
878 (vector super (classoid-layout (find-classoid 'stream)))))
879 ((fd-stream)
880 (concatenate 'simple-vector
881 (layout-inherits super)
882 (vector super
883 (classoid-layout (find-classoid 'file-stream)))))
884 ((sb!impl::string-input-stream
885 sb!impl::string-output-stream
886 sb!impl::fill-pointer-output-stream)
887 (concatenate 'simple-vector
888 (layout-inherits super)
889 (vector super
890 (classoid-layout (find-classoid 'string-stream)))))
891 (t (concatenate 'simple-vector
892 (layout-inherits super)
893 (vector super))))))
895 ;;; Do miscellaneous (LOAD EVAL) time actions for the structure
896 ;;; described by DD. Create the class and LAYOUT, checking for
897 ;;; incompatible redefinition. Define those functions which are
898 ;;; sufficiently stereotyped that we can implement them as standard
899 ;;; closures.
900 (defun %defstruct (dd inherits source-location)
901 (declare (type defstruct-description dd))
903 ;; We set up LAYOUTs even in the cross-compilation host.
904 (multiple-value-bind (classoid layout old-layout)
905 (ensure-structure-class dd inherits "current" "new")
906 (cond ((not old-layout)
907 (unless (eq (classoid-layout classoid) layout)
908 (register-layout layout)))
910 (let ((old-dd (layout-info old-layout)))
911 (when (defstruct-description-p old-dd)
912 (dolist (slot (dd-slots old-dd))
913 (fmakunbound (dsd-accessor-name slot))
914 (unless (dsd-read-only slot)
915 (fmakunbound `(setf ,(dsd-accessor-name slot)))))))
916 (%redefine-defstruct classoid old-layout layout)
917 (setq layout (classoid-layout classoid))))
918 (setf (find-classoid (dd-name dd)) classoid)
920 (sb!c:with-source-location (source-location)
921 (setf (layout-source-location layout) source-location))
923 ;; Various other operations only make sense on the target SBCL.
924 #-sb-xc-host
925 (%target-defstruct dd layout))
927 (values))
929 ;;; Return a form describing the writable place used for this slot
930 ;;; in the instance named INSTANCE-NAME.
931 (defun %accessor-place-form (dd dsd instance-name)
932 (let (;; the operator that we'll use to access a typed slot
933 (ref (ecase (dd-type dd)
934 (structure '%instance-ref)
935 (list 'nth-but-with-sane-arg-order)
936 (vector 'aref)))
937 (raw-type (dsd-raw-type dsd)))
938 (if (eq raw-type t) ; if not raw slot
939 `(,ref ,instance-name ,(dsd-index dsd))
940 (let* ((raw-slot-data (find raw-type *raw-slot-data-list*
941 :key #'raw-slot-data-raw-type
942 :test #'equal))
943 (raw-slot-accessor (raw-slot-data-accessor-name raw-slot-data)))
944 `(,raw-slot-accessor ,instance-name ,(dsd-index dsd))))))
946 ;;; Return source transforms for the reader and writer functions of
947 ;;; the slot described by DSD. They should be inline expanded, but
948 ;;; source transforms work faster.
949 (defun slot-accessor-transforms (dd dsd)
950 (let ((accessor-place-form (%accessor-place-form dd dsd
951 `(the ,(dd-name dd) instance)))
952 (dsd-type (dsd-type dsd))
953 (value-the (if (dsd-safe-p dsd) 'truly-the 'the)))
954 (values (sb!c:source-transform-lambda (instance)
955 `(,value-the ,dsd-type ,(subst instance 'instance
956 accessor-place-form)))
957 (sb!c:source-transform-lambda (new-value instance)
958 (destructuring-bind (accessor-name &rest accessor-args)
959 accessor-place-form
960 (once-only ((new-value new-value)
961 (instance instance))
962 `(,(info :setf :inverse accessor-name)
963 ,@(subst instance 'instance accessor-args)
964 (the ,dsd-type ,new-value))))))))
966 ;;; Return a LAMBDA form which can be used to set a slot.
967 (defun slot-setter-lambda-form (dd dsd)
968 ;; KLUDGE: Evaluating the results of SLOT-ACCESSOR-TRANSFORMS needs
969 ;; a lexenv.
970 (let ((sb!c:*lexenv* (if (boundp 'sb!c:*lexenv*)
971 sb!c:*lexenv*
972 (sb!c::make-null-lexenv))))
973 `(lambda (new-value instance)
974 ,(funcall (nth-value 1 (slot-accessor-transforms dd dsd))
975 '(dummy new-value instance)))))
977 ;;; core compile-time setup of any class with a LAYOUT, used even by
978 ;;; !DEFSTRUCT-WITH-ALTERNATE-METACLASS weirdosities
979 (defun %compiler-set-up-layout (dd
980 &optional
981 ;; Several special cases
982 ;; (STRUCTURE-OBJECT itself, and
983 ;; structures with alternate
984 ;; metaclasses) call this function
985 ;; directly, and they're all at the
986 ;; base of the instance class
987 ;; structure, so this is a handy
988 ;; default. (But note
989 ;; FUNCALLABLE-STRUCTUREs need
990 ;; assistance here)
991 (inherits (vector (find-layout t))))
993 (multiple-value-bind (classoid layout old-layout)
994 (multiple-value-bind (clayout clayout-p)
995 (info :type :compiler-layout (dd-name dd))
996 (ensure-structure-class dd
997 inherits
998 (if clayout-p "previously compiled" "current")
999 "compiled"
1000 :compiler-layout clayout))
1001 (cond (old-layout
1002 (undefine-structure (layout-classoid old-layout))
1003 (when (and (classoid-subclasses classoid)
1004 (not (eq layout old-layout)))
1005 (collect ((subs))
1006 (dohash ((classoid layout) (classoid-subclasses classoid)
1007 :locked t)
1008 (declare (ignore layout))
1009 (undefine-structure classoid)
1010 (subs (classoid-proper-name classoid)))
1011 (when (subs)
1012 (warn "removing old subclasses of ~S:~% ~S"
1013 (classoid-name classoid)
1014 (subs))))))
1016 (unless (eq (classoid-layout classoid) layout)
1017 (register-layout layout :invalidate nil))
1018 (setf (find-classoid (dd-name dd)) classoid)))
1020 ;; At this point the class should be set up in the INFO database.
1021 ;; But the logic that enforces this is a little tangled and
1022 ;; scattered, so it's not obvious, so let's check.
1023 (aver (find-classoid (dd-name dd) nil))
1025 (setf (info :type :compiler-layout (dd-name dd)) layout))
1027 (values))
1029 ;;; Do (COMPILE LOAD EVAL)-time actions for the normal (not
1030 ;;; ALTERNATE-LAYOUT) DEFSTRUCT described by DD.
1031 (defun %compiler-defstruct (dd inherits)
1032 (declare (type defstruct-description dd))
1034 (%compiler-set-up-layout dd inherits)
1036 (let* ((dtype (dd-declarable-type dd)))
1038 (let ((copier-name (dd-copier-name dd)))
1039 (when copier-name
1040 (sb!xc:proclaim `(ftype (sfunction (,dtype) ,dtype) ,copier-name))))
1042 (let ((predicate-name (dd-predicate-name dd)))
1043 (when predicate-name
1044 (sb!xc:proclaim `(ftype (sfunction (t) boolean) ,predicate-name))
1045 ;; Provide inline expansion (or not).
1046 (ecase (dd-type dd)
1047 ((structure funcallable-structure)
1048 ;; Let the predicate be inlined.
1049 (setf (info :function :inline-expansion-designator predicate-name)
1050 (lambda ()
1051 `(lambda (x)
1052 ;; This dead simple definition works because the
1053 ;; type system knows how to generate inline type
1054 ;; tests for instances.
1055 (typep x ',(dd-name dd))))
1056 (info :function :inlinep predicate-name)
1057 :inline))
1058 ((list vector)
1059 ;; Just punt. We could provide inline expansions for :TYPE
1060 ;; LIST and :TYPE VECTOR predicates too, but it'd be a
1061 ;; little messier and we don't bother. (Does anyway use
1062 ;; typed DEFSTRUCTs at all, let alone for high
1063 ;; performance?)
1064 ))))
1066 (dolist (dsd (dd-slots dd))
1067 (let* ((accessor-name (dsd-accessor-name dsd))
1068 (dsd-type (dsd-type dsd)))
1069 (when accessor-name
1070 (setf (info :function :structure-accessor accessor-name) dd)
1071 (let ((inherited (accessor-inherited-data accessor-name dd)))
1072 (cond
1073 ((not inherited)
1074 (multiple-value-bind (reader-designator writer-designator)
1075 (slot-accessor-transforms dd dsd)
1076 (sb!xc:proclaim `(ftype (sfunction (,dtype) ,dsd-type)
1077 ,accessor-name))
1078 (setf (info :function :source-transform accessor-name)
1079 reader-designator)
1080 (unless (dsd-read-only dsd)
1081 (let ((setf-accessor-name `(setf ,accessor-name)))
1082 (sb!xc:proclaim
1083 `(ftype (sfunction (,dsd-type ,dtype) ,dsd-type)
1084 ,setf-accessor-name))
1085 (setf (info :function :source-transform setf-accessor-name)
1086 writer-designator)))))
1087 ((not (= (cdr inherited) (dsd-index dsd)))
1088 (style-warn "~@<Non-overwritten accessor ~S does not access ~
1089 slot with name ~S (accessing an inherited slot ~
1090 instead).~:@>"
1091 accessor-name
1092 (dsd-name dsd)))))))))
1093 (values))
1095 ;;;; redefinition stuff
1097 ;;; Compare the slots of OLD and NEW, returning 3 lists of slot names:
1098 ;;; 1. Slots which have moved,
1099 ;;; 2. Slots whose type has changed,
1100 ;;; 3. Deleted slots.
1101 (defun compare-slots (old new)
1102 (let* ((oslots (dd-slots old))
1103 (nslots (dd-slots new))
1104 (onames (mapcar #'dsd-name oslots))
1105 (nnames (mapcar #'dsd-name nslots)))
1106 (collect ((moved)
1107 (retyped))
1108 (dolist (name (intersection onames nnames))
1109 (let ((os (find name oslots :key #'dsd-name :test #'string=))
1110 (ns (find name nslots :key #'dsd-name :test #'string=)))
1111 (unless (sb!xc:subtypep (dsd-type ns) (dsd-type os))
1112 (retyped name))
1113 (unless (and (= (dsd-index os) (dsd-index ns))
1114 (eq (dsd-raw-type os) (dsd-raw-type ns)))
1115 (moved name))))
1116 (values (moved)
1117 (retyped)
1118 (set-difference onames nnames :test #'string=)))))
1120 ;;; If we are redefining a structure with different slots than in the
1121 ;;; currently loaded version, give a warning and return true.
1122 (defun redefine-structure-warning (classoid old new)
1123 (declare (type defstruct-description old new)
1124 (type classoid classoid)
1125 (ignore classoid))
1126 (let ((name (dd-name new)))
1127 (multiple-value-bind (moved retyped deleted) (compare-slots old new)
1128 (when (or moved retyped deleted)
1129 (warn
1130 "incompatibly redefining slots of structure class ~S~@
1131 Make sure any uses of affected accessors are recompiled:~@
1132 ~@[ These slots were moved to new positions:~% ~S~%~]~
1133 ~@[ These slots have new incompatible types:~% ~S~%~]~
1134 ~@[ These slots were deleted:~% ~S~%~]"
1135 name moved retyped deleted)
1136 t))))
1138 ;;; This function is called when we are incompatibly redefining a
1139 ;;; structure CLASS to have the specified NEW-LAYOUT. We signal an
1140 ;;; error with some proceed options and return the layout that should
1141 ;;; be used.
1142 (defun %redefine-defstruct (classoid old-layout new-layout)
1143 (declare (type classoid classoid)
1144 (type layout old-layout new-layout))
1145 (let ((name (classoid-proper-name classoid)))
1146 (restart-case
1147 (error "~@<attempt to redefine the ~S class ~S incompatibly with the current definition~:@>"
1148 'structure-object
1149 name)
1150 (continue ()
1151 :report (lambda (s)
1152 (format s
1153 "~@<Use the new definition of ~S, invalidating ~
1154 already-loaded code and instances.~@:>"
1155 name))
1156 (register-layout new-layout))
1157 (recklessly-continue ()
1158 :report (lambda (s)
1159 (format s
1160 "~@<Use the new definition of ~S as if it were ~
1161 compatible, allowing old accessors to use new ~
1162 instances and allowing new accessors to use old ~
1163 instances.~@:>"
1164 name))
1165 ;; classic CMU CL warning: "Any old ~S instances will be in a bad way.
1166 ;; I hope you know what you're doing..."
1167 (register-layout new-layout
1168 :invalidate nil
1169 :destruct-layout old-layout))
1170 (clobber-it ()
1171 ;; FIXME: deprecated 2002-10-16, and since it's only interactive
1172 ;; hackery instead of a supported feature, can probably be deleted
1173 ;; in early 2003
1174 :report "(deprecated synonym for RECKLESSLY-CONTINUE)"
1175 (register-layout new-layout
1176 :invalidate nil
1177 :destruct-layout old-layout))))
1178 (values))
1180 (declaim (inline dd-layout-length))
1181 (defun dd-layout-length (dd)
1182 (+ (dd-length dd) (dd-raw-length dd)))
1184 (declaim (ftype (sfunction (defstruct-description) index) dd-instance-length))
1185 (defun dd-instance-length (dd)
1186 ;; Make sure the object ends at a two-word boundary. Note that this does
1187 ;; not affect the amount of memory used, since the allocator would add the
1188 ;; same padding anyway. However, raw slots are indexed from the length of
1189 ;; the object as indicated in the header, so the pad word needs to be
1190 ;; included in that length to guarantee proper alignment of raw double float
1191 ;; slots, necessary for (at least) the SPARC backend.
1192 (let ((layout-length (dd-layout-length dd)))
1193 (declare (index layout-length))
1194 (+ layout-length (mod (1+ layout-length) 2))))
1196 ;;; This is called when we are about to define a structure class. It
1197 ;;; returns a (possibly new) class object and the layout which should
1198 ;;; be used for the new definition (may be the current layout, and
1199 ;;; also might be an uninstalled forward referenced layout.) The third
1200 ;;; value is true if this is an incompatible redefinition, in which
1201 ;;; case it is the old layout.
1202 (defun ensure-structure-class (info inherits old-context new-context
1203 &key compiler-layout)
1204 (multiple-value-bind (class old-layout)
1205 (destructuring-bind
1206 (&optional
1207 name
1208 (class 'structure-classoid)
1209 (constructor 'make-structure-classoid))
1210 (dd-alternate-metaclass info)
1211 (declare (ignore name))
1212 (insured-find-classoid (dd-name info)
1213 (if (eq class 'structure-classoid)
1214 (lambda (x)
1215 (sb!xc:typep x 'structure-classoid))
1216 (lambda (x)
1217 (sb!xc:typep x (classoid-name (find-classoid class)))))
1218 (fdefinition constructor)))
1219 (setf (classoid-direct-superclasses class)
1220 (case (dd-name info)
1221 ((ansi-stream
1222 fd-stream
1223 sb!impl::string-input-stream sb!impl::string-output-stream
1224 sb!impl::fill-pointer-output-stream)
1225 (list (layout-classoid (svref inherits (1- (length inherits))))
1226 (layout-classoid (svref inherits (- (length inherits) 2)))))
1228 (list (layout-classoid
1229 (svref inherits (1- (length inherits))))))))
1230 (let ((new-layout (make-layout :classoid class
1231 :inherits inherits
1232 :depthoid (length inherits)
1233 :length (dd-layout-length info)
1234 :n-untagged-slots (dd-raw-length info)
1235 :info info))
1236 (old-layout (or compiler-layout old-layout)))
1237 (cond
1238 ((not old-layout)
1239 (values class new-layout nil))
1240 (;; This clause corresponds to an assertion in REDEFINE-LAYOUT-WARNING
1241 ;; of classic CMU CL. I moved it out to here because it was only
1242 ;; exercised in this code path anyway. -- WHN 19990510
1243 (not (eq (layout-classoid new-layout) (layout-classoid old-layout)))
1244 (error "shouldn't happen: weird state of OLD-LAYOUT?"))
1245 ((not *type-system-initialized*)
1246 (setf (layout-info old-layout) info)
1247 (values class old-layout nil))
1248 ((redefine-layout-warning old-context
1249 old-layout
1250 new-context
1251 (layout-length new-layout)
1252 (layout-inherits new-layout)
1253 (layout-depthoid new-layout)
1254 (layout-n-untagged-slots new-layout))
1255 (values class new-layout old-layout))
1257 (let ((old-info (layout-info old-layout)))
1258 (typecase old-info
1259 ((or defstruct-description)
1260 (cond ((redefine-structure-warning class old-info info)
1261 (values class new-layout old-layout))
1263 (setf (layout-info old-layout) info)
1264 (values class old-layout nil))))
1265 (null
1266 (setf (layout-info old-layout) info)
1267 (values class old-layout nil))
1269 (error "shouldn't happen! strange thing in LAYOUT-INFO:~% ~S"
1270 old-layout)
1271 (values class new-layout old-layout)))))))))
1273 ;;; Blow away all the compiler info for the structure CLASS. Iterate
1274 ;;; over this type, clearing the compiler structure type info, and
1275 ;;; undefining all the associated functions.
1276 (defun undefine-structure (class)
1277 (let ((info (layout-info (classoid-layout class))))
1278 (when (defstruct-description-p info)
1279 (let ((type (dd-name info)))
1280 (remhash type *typecheckfuns*)
1281 (setf (info :type :compiler-layout type) nil)
1282 (undefine-fun-name (dd-copier-name info))
1283 (undefine-fun-name (dd-predicate-name info))
1284 (dolist (slot (dd-slots info))
1285 (let ((fun (dsd-accessor-name slot)))
1286 (unless (accessor-inherited-data fun info)
1287 (undefine-fun-name fun)
1288 (unless (dsd-read-only slot)
1289 (undefine-fun-name `(setf ,fun)))))))
1290 ;; Clear out the SPECIFIER-TYPE cache so that subsequent
1291 ;; references are unknown types.
1292 (values-specifier-type-cache-clear)))
1293 (values))
1295 ;;; Return a list of pairs (name . index). Used for :TYPE'd
1296 ;;; constructors to find all the names that we have to splice in &
1297 ;;; where. Note that these types don't have a layout, so we can't look
1298 ;;; at LAYOUT-INHERITS.
1299 (defun find-name-indices (defstruct)
1300 (collect ((res))
1301 (let ((infos ()))
1302 (do ((info defstruct
1303 (typed-structure-info-or-lose (first (dd-include info)))))
1304 ((not (dd-include info))
1305 (push info infos))
1306 (push info infos))
1308 (let ((i 0))
1309 (dolist (info infos)
1310 (incf i (or (dd-offset info) 0))
1311 (when (dd-named info)
1312 (res (cons (dd-name info) i)))
1313 (setq i (dd-length info)))))
1315 (res)))
1317 ;;; These functions are called to actually make a constructor after we
1318 ;;; have processed the arglist. The correct variant (according to the
1319 ;;; DD-TYPE) should be called. The function is defined with the
1320 ;;; specified name and arglist. VARS and TYPES are used for argument
1321 ;;; type declarations. VALUES are the values for the slots (in order.)
1323 ;;; This is split three ways because:
1324 ;;; * LIST & VECTOR structures need "name" symbols stuck in at
1325 ;;; various weird places, whereas STRUCTURE structures have
1326 ;;; a LAYOUT slot.
1327 ;;; * We really want to use LIST to make list structures, instead of
1328 ;;; MAKE-LIST/(SETF ELT). (We can't in general use VECTOR in an
1329 ;;; analogous way, since VECTOR makes a SIMPLE-VECTOR and vector-typed
1330 ;;; structures can have arbitrary subtypes of VECTOR, not necessarily
1331 ;;; SIMPLE-VECTOR.)
1332 ;;; * STRUCTURE structures can have raw slots that must also be
1333 ;;; allocated and indirectly referenced.
1334 (defun create-vector-constructor (dd cons-name arglist vars types values)
1335 (let ((temp (gensym))
1336 (etype (dd-element-type dd)))
1337 `(defun ,cons-name ,arglist
1338 (declare ,@(mapcar (lambda (var type) `(type (and ,type ,etype) ,var))
1339 vars types))
1340 (let ((,temp (make-array ,(dd-length dd)
1341 :element-type ',(dd-element-type dd))))
1342 ,@(mapcar (lambda (x)
1343 `(setf (aref ,temp ,(cdr x)) ',(car x)))
1344 (find-name-indices dd))
1345 ,@(mapcar (lambda (dsd value)
1346 (unless (eq value '.do-not-initialize-slot.)
1347 `(setf (aref ,temp ,(dsd-index dsd)) ,value)))
1348 (dd-slots dd) values)
1349 ,temp))))
1350 (defun create-list-constructor (dd cons-name arglist vars types values)
1351 (let ((vals (make-list (dd-length dd) :initial-element nil)))
1352 (dolist (x (find-name-indices dd))
1353 (setf (elt vals (cdr x)) `',(car x)))
1354 (loop for dsd in (dd-slots dd) and val in values do
1355 (setf (elt vals (dsd-index dsd))
1356 (if (eq val '.do-not-initialize-slot.) 0 val)))
1357 `(defun ,cons-name ,arglist
1358 (declare ,@(mapcar (lambda (var type) `(type ,type ,var)) vars types))
1359 (list ,@vals))))
1360 (defun create-structure-constructor (dd cons-name arglist vars types values)
1361 ;; The difference between the two implementations here is that on all
1362 ;; platforms we don't have the appropriate RAW-INSTANCE-INIT VOPS, which
1363 ;; must be able to deal with immediate values as well -- unlike
1364 ;; RAW-INSTANCE-SET VOPs, which never end up seeing immediate values. With
1365 ;; some additional cleverness we might manage without them and just a single
1366 ;; implementation here, though -- figure out a way to ensure that on those
1367 ;; platforms we always still get a non-immediate TN in every case...
1369 ;; Until someone does that, this means that instances with raw slots can be
1370 ;; DX allocated only on platforms with those additional VOPs.
1371 #!+raw-instance-init-vops
1372 (let* ((slot-values nil)
1373 (slot-specs
1374 (mapcan (lambda (dsd value)
1375 (unless (eq value '.do-not-initialize-slot.)
1376 (push value slot-values)
1377 (list (list* :slot (dsd-raw-type dsd) (dsd-index dsd)))))
1378 (dd-slots dd)
1379 values)))
1380 `(defun ,cons-name ,arglist
1381 (declare ,@(mapcar (lambda (var type) `(type ,type ,var)) vars types))
1382 (%make-structure-instance-macro ,dd ',slot-specs ,@(reverse slot-values))))
1383 #!-raw-instance-init-vops
1384 (let ((instance (gensym "INSTANCE")) slot-values slot-specs raw-slots raw-values)
1385 (mapc (lambda (dsd value)
1386 (unless (eq value '.do-not-initialize-slot.)
1387 (let ((raw-type (dsd-raw-type dsd)))
1388 (cond ((eq t raw-type)
1389 (push value slot-values)
1390 (push (list* :slot raw-type (dsd-index dsd)) slot-specs))
1392 (push value raw-values)
1393 (push dsd raw-slots))))))
1394 (dd-slots dd)
1395 values)
1396 `(defun ,cons-name ,arglist
1397 (declare ,@(mapcar (lambda (var type) `(type ,type ,var)) vars types))
1398 ,(if raw-slots
1399 `(let ((,instance (%make-structure-instance-macro ,dd ',slot-specs ,@slot-values)))
1400 ,@(mapcar (lambda (dsd value)
1401 ;; (Note that we can't in general use the
1402 ;; ordinary named slot setter function here
1403 ;; because the slot might be :READ-ONLY, so we
1404 ;; whip up new LAMBDA representations of slot
1405 ;; setters for the occasion.)
1406 `(,(slot-setter-lambda-form dd dsd) ,value ,instance))
1407 raw-slots
1408 raw-values)
1409 ,instance)
1410 `(%make-structure-instance-macro ,dd ',slot-specs ,@slot-values)))))
1412 ;;; Create a default (non-BOA) keyword constructor.
1413 (defun create-keyword-constructor (defstruct creator)
1414 (declare (type function creator))
1415 (collect ((arglist (list '&key))
1416 (types)
1417 (vals))
1418 (dolist (slot (dd-slots defstruct))
1419 (let ((dum (gensym))
1420 (name (dsd-name slot)))
1421 (arglist `((,(keywordicate name) ,dum) ,(dsd-default slot)))
1422 (types (dsd-type slot))
1423 (vals dum)))
1424 (funcall creator
1425 defstruct (dd-default-constructor defstruct)
1426 (arglist) (vals) (types) (vals))))
1428 ;;; Given a structure and a BOA constructor spec, call CREATOR with
1429 ;;; the appropriate args to make a constructor.
1430 (defun create-boa-constructor (defstruct boa creator)
1431 (declare (type function creator))
1432 (multiple-value-bind (req opt restp rest keyp keys allowp auxp aux)
1433 (parse-lambda-list (second boa))
1434 (collect ((arglist)
1435 (vars)
1436 (types)
1437 (skipped-vars))
1438 (labels ((get-slot (name)
1439 (let ((res (find name (dd-slots defstruct)
1440 :test #'string=
1441 :key #'dsd-name)))
1442 (if res
1443 (values (dsd-type res) (dsd-default res))
1444 (values t nil))))
1445 (do-default (arg)
1446 (multiple-value-bind (type default) (get-slot arg)
1447 (arglist `(,arg ,default))
1448 (vars arg)
1449 (types type))))
1450 (dolist (arg req)
1451 (arglist arg)
1452 (vars arg)
1453 (types (get-slot arg)))
1455 (when opt
1456 (arglist '&optional)
1457 (dolist (arg opt)
1458 (cond ((consp arg)
1459 (destructuring-bind
1460 ;; FIXME: this shares some logic (though not
1461 ;; code) with the &key case below (and it
1462 ;; looks confusing) -- factor out the logic
1463 ;; if possible. - CSR, 2002-04-19
1464 (name
1465 &optional
1466 (def (nth-value 1 (get-slot name)))
1467 (supplied-test nil supplied-test-p))
1469 (arglist `(,name ,def ,@(if supplied-test-p `(,supplied-test) nil)))
1470 (vars name)
1471 (types (get-slot name))))
1473 (do-default arg)))))
1475 (when restp
1476 (arglist '&rest rest)
1477 (vars rest)
1478 (types 'list))
1480 (when keyp
1481 (arglist '&key)
1482 (dolist (key keys)
1483 (if (consp key)
1484 (destructuring-bind (wot
1485 &optional
1486 (def nil def-p)
1487 (supplied-test nil supplied-test-p))
1489 (let ((name (if (consp wot)
1490 (destructuring-bind (key var) wot
1491 (declare (ignore key))
1492 var)
1493 wot)))
1494 (multiple-value-bind (type slot-def)
1495 (get-slot name)
1496 (arglist `(,wot ,(if def-p def slot-def)
1497 ,@(if supplied-test-p `(,supplied-test) nil)))
1498 (vars name)
1499 (types type))))
1500 (do-default key))))
1502 (when allowp (arglist '&allow-other-keys))
1504 (when auxp
1505 (arglist '&aux)
1506 (dolist (arg aux)
1507 (arglist arg)
1508 (if (proper-list-of-length-p arg 2)
1509 (let ((var (first arg)))
1510 (vars var)
1511 (types (get-slot var)))
1512 (skipped-vars (if (consp arg) (first arg) arg))))))
1514 (funcall creator defstruct (first boa)
1515 (arglist) (vars) (types)
1516 (loop for slot in (dd-slots defstruct)
1517 for name = (dsd-name slot)
1518 collect (cond ((find name (skipped-vars) :test #'string=)
1519 (setf (dsd-safe-p slot) nil)
1520 '.do-not-initialize-slot.)
1521 ((or (find (dsd-name slot) (vars) :test #'string=)
1522 (dsd-default slot)))))))))
1524 ;;; Grovel the constructor options, and decide what constructors (if
1525 ;;; any) to create.
1526 (defun constructor-definitions (defstruct)
1527 (let ((no-constructors nil)
1528 (boas ())
1529 (defaults ())
1530 (creator (ecase (dd-type defstruct)
1531 (structure #'create-structure-constructor)
1532 (vector #'create-vector-constructor)
1533 (list #'create-list-constructor))))
1534 (dolist (constructor (dd-constructors defstruct))
1535 (destructuring-bind (name &optional (boa-ll nil boa-p)) constructor
1536 (declare (ignore boa-ll))
1537 (cond ((not name) (setq no-constructors t))
1538 (boa-p (push constructor boas))
1539 (t (push name defaults)))))
1541 (when no-constructors
1542 (when (or defaults boas)
1543 (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs"))
1544 (return-from constructor-definitions ()))
1546 (unless (or defaults boas)
1547 (push (symbolicate "MAKE-" (dd-name defstruct)) defaults))
1549 (collect ((res) (names))
1550 (when defaults
1551 (let ((cname (first defaults)))
1552 (setf (dd-default-constructor defstruct) cname)
1553 (res (create-keyword-constructor defstruct creator))
1554 (names cname)
1555 (dolist (other-name (rest defaults))
1556 (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1557 (names other-name))))
1559 (dolist (boa boas)
1560 (res (create-boa-constructor defstruct boa creator))
1561 (names (first boa)))
1563 (res `(declaim (ftype
1564 (sfunction *
1565 ,(if (eq (dd-type defstruct) 'structure)
1566 (dd-name defstruct)
1567 '*))
1568 ,@(names))))
1570 (res))))
1572 ;;;; instances with ALTERNATE-METACLASS
1573 ;;;;
1574 ;;;; The CMU CL support for structures with ALTERNATE-METACLASS was a
1575 ;;;; fairly general extension embedded in the main DEFSTRUCT code, and
1576 ;;;; the result was an fairly impressive mess as ALTERNATE-METACLASS
1577 ;;;; extension mixed with ANSI CL generality (e.g. :TYPE and :INCLUDE)
1578 ;;;; and CMU CL implementation hairiness (esp. raw slots). This SBCL
1579 ;;;; version is much less ambitious, noticing that ALTERNATE-METACLASS
1580 ;;;; is only used to implement CONDITION, STANDARD-INSTANCE, and
1581 ;;;; GENERIC-FUNCTION, and defining a simple specialized
1582 ;;;; separate-from-DEFSTRUCT macro to provide only enough
1583 ;;;; functionality to support those.
1584 ;;;;
1585 ;;;; KLUDGE: The defining macro here is so specialized that it's ugly
1586 ;;;; in its own way. It also violates once-and-only-once by knowing
1587 ;;;; much about structures and layouts that is already known by the
1588 ;;;; main DEFSTRUCT macro. Hopefully it will go away presently
1589 ;;;; (perhaps when CL:CLASS and SB-PCL:CLASS meet) as per FIXME below.
1590 ;;;; -- WHN 2001-10-28
1591 ;;;;
1592 ;;;; FIXME: There seems to be no good reason to shoehorn CONDITION,
1593 ;;;; STANDARD-INSTANCE, and GENERIC-FUNCTION into mutated structures
1594 ;;;; instead of just implementing them as primitive objects. (This
1595 ;;;; reduced-functionality macro seems pretty close to the
1596 ;;;; functionality of DEFINE-PRIMITIVE-OBJECT..)
1598 (defun make-dd-with-alternate-metaclass (&key (class-name (missing-arg))
1599 (superclass-name (missing-arg))
1600 (metaclass-name (missing-arg))
1601 (dd-type (missing-arg))
1602 metaclass-constructor
1603 slot-names)
1604 (let* ((dd (make-defstruct-description class-name))
1605 (conc-name (concatenate 'string (symbol-name class-name) "-"))
1606 (dd-slots (let ((reversed-result nil)
1607 ;; The index starts at 1 for ordinary named
1608 ;; slots because slot 0 is magical, used for
1609 ;; the LAYOUT in CONDITIONs and
1610 ;; FUNCALLABLE-INSTANCEs. (This is the same
1611 ;; in ordinary structures too: see (INCF
1612 ;; DD-LENGTH) in
1613 ;; PARSE-DEFSTRUCT-NAME-AND-OPTIONS).
1614 (index 1))
1615 (dolist (slot-name slot-names)
1616 (push (make-defstruct-slot-description
1617 :name slot-name
1618 :index index
1619 :accessor-name (symbolicate conc-name slot-name))
1620 reversed-result)
1621 (incf index))
1622 (nreverse reversed-result))))
1623 (case dd-type
1624 ;; We don't support inheritance of alternate metaclass stuff,
1625 ;; and it's not a general-purpose facility, so sanity check our
1626 ;; own code.
1627 (structure
1628 (aver (eq superclass-name 't)))
1629 (funcallable-structure
1630 (aver (eq superclass-name 'function)))
1631 (t (bug "Unknown DD-TYPE in ALTERNATE-METACLASS: ~S" dd-type)))
1632 (setf (dd-alternate-metaclass dd) (list superclass-name
1633 metaclass-name
1634 metaclass-constructor)
1635 (dd-slots dd) dd-slots
1636 (dd-length dd) (1+ (length slot-names))
1637 (dd-type dd) dd-type)
1638 dd))
1640 ;;; make !DEFSTRUCT-WITH-ALTERNATE-METACLASS compilable by the host
1641 ;;; lisp, installing the information we need to reason about the
1642 ;;; structures (layouts and classoids).
1644 ;;; FIXME: we should share the parsing and the DD construction between
1645 ;;; this and the cross-compiler version, but my brain was too small to
1646 ;;; get that right. -- CSR, 2006-09-14
1647 #+sb-xc-host
1648 (defmacro !defstruct-with-alternate-metaclass
1649 (class-name &key
1650 (slot-names (missing-arg))
1651 (boa-constructor (missing-arg))
1652 (superclass-name (missing-arg))
1653 (metaclass-name (missing-arg))
1654 (metaclass-constructor (missing-arg))
1655 (dd-type (missing-arg))
1656 predicate
1657 (runtime-type-checks-p t))
1659 (declare (type (and list (not null)) slot-names))
1660 (declare (type (and symbol (not null))
1661 boa-constructor
1662 superclass-name
1663 metaclass-name
1664 metaclass-constructor))
1665 (declare (type symbol predicate))
1666 (declare (type (member structure funcallable-structure) dd-type))
1667 (declare (ignore boa-constructor predicate runtime-type-checks-p))
1669 (let* ((dd (make-dd-with-alternate-metaclass
1670 :class-name class-name
1671 :slot-names slot-names
1672 :superclass-name superclass-name
1673 :metaclass-name metaclass-name
1674 :metaclass-constructor metaclass-constructor
1675 :dd-type dd-type)))
1676 `(progn
1678 (eval-when (:compile-toplevel :load-toplevel :execute)
1679 (%compiler-set-up-layout ',dd ',(inherits-for-structure dd))))))
1681 (sb!xc:defmacro !defstruct-with-alternate-metaclass
1682 (class-name &key
1683 (slot-names (missing-arg))
1684 (boa-constructor (missing-arg))
1685 (superclass-name (missing-arg))
1686 (metaclass-name (missing-arg))
1687 (metaclass-constructor (missing-arg))
1688 (dd-type (missing-arg))
1689 predicate
1690 (runtime-type-checks-p t))
1692 (declare (type (and list (not null)) slot-names))
1693 (declare (type (and symbol (not null))
1694 boa-constructor
1695 superclass-name
1696 metaclass-name
1697 metaclass-constructor))
1698 (declare (type symbol predicate))
1699 (declare (type (member structure funcallable-structure) dd-type))
1701 (let* ((dd (make-dd-with-alternate-metaclass
1702 :class-name class-name
1703 :slot-names slot-names
1704 :superclass-name superclass-name
1705 :metaclass-name metaclass-name
1706 :metaclass-constructor metaclass-constructor
1707 :dd-type dd-type))
1708 (dd-slots (dd-slots dd))
1709 (dd-length (1+ (length slot-names)))
1710 (object-gensym (gensym "OBJECT"))
1711 (new-value-gensym (gensym "NEW-VALUE-"))
1712 (delayed-layout-form `(%delayed-get-compiler-layout ,class-name)))
1713 (multiple-value-bind (raw-maker-form raw-reffer-operator)
1714 (ecase dd-type
1715 (structure
1716 (values `(%make-structure-instance-macro ,dd nil)
1717 '%instance-ref))
1718 (funcallable-structure
1719 (values `(let ((,object-gensym
1720 (%make-funcallable-instance ,dd-length)))
1721 (setf (%funcallable-instance-layout ,object-gensym)
1722 ,delayed-layout-form)
1723 ,object-gensym)
1724 '%funcallable-instance-info)))
1725 `(progn
1727 (eval-when (:compile-toplevel :load-toplevel :execute)
1728 (%compiler-set-up-layout ',dd ',(inherits-for-structure dd)))
1730 ;; slot readers and writers
1731 (declaim (inline ,@(mapcar #'dsd-accessor-name dd-slots)))
1732 ,@(mapcar (lambda (dsd)
1733 `(defun ,(dsd-accessor-name dsd) (,object-gensym)
1734 ,@(when runtime-type-checks-p
1735 `((declare (type ,class-name ,object-gensym))))
1736 (,raw-reffer-operator ,object-gensym
1737 ,(dsd-index dsd))))
1738 dd-slots)
1739 (declaim (inline ,@(mapcar (lambda (dsd)
1740 `(setf ,(dsd-accessor-name dsd)))
1741 dd-slots)))
1742 ,@(mapcar (lambda (dsd)
1743 `(defun (setf ,(dsd-accessor-name dsd)) (,new-value-gensym
1744 ,object-gensym)
1745 ,@(when runtime-type-checks-p
1746 `((declare (type ,class-name ,object-gensym))))
1747 (setf (,raw-reffer-operator ,object-gensym
1748 ,(dsd-index dsd))
1749 ,new-value-gensym)))
1750 dd-slots)
1752 ;; constructor
1753 (defun ,boa-constructor ,slot-names
1754 (let ((,object-gensym ,raw-maker-form))
1755 ,@(mapcar (lambda (slot-name)
1756 (let ((dsd (find (symbol-name slot-name) dd-slots
1757 :key (lambda (x)
1758 (symbol-name (dsd-name x)))
1759 :test #'string=)))
1760 ;; KLUDGE: bug 117 bogowarning. Neither
1761 ;; DECLAREing the type nor TRULY-THE cut
1762 ;; the mustard -- it still gives warnings.
1763 (enforce-type dsd defstruct-slot-description)
1764 `(setf (,(dsd-accessor-name dsd) ,object-gensym)
1765 ,slot-name)))
1766 slot-names)
1767 ,object-gensym))
1769 ;; predicate
1770 ,@(when predicate
1771 ;; Just delegate to the compiler's type optimization
1772 ;; code, which knows how to generate inline type tests
1773 ;; for the whole CMU CL INSTANCE menagerie.
1774 `(defun ,predicate (,object-gensym)
1775 (typep ,object-gensym ',class-name)))))))
1777 ;;;; finalizing bootstrapping
1779 ;;; Set up DD and LAYOUT for STRUCTURE-OBJECT class itself.
1781 ;;; Ordinary structure classes effectively :INCLUDE STRUCTURE-OBJECT
1782 ;;; when they have no explicit :INCLUDEs, so (1) it needs to be set up
1783 ;;; before we can define ordinary structure classes, and (2) it's
1784 ;;; special enough (and simple enough) that we just build it by hand
1785 ;;; instead of trying to generalize the ordinary DEFSTRUCT code.
1786 (defun !set-up-structure-object-class ()
1787 (let ((dd (make-defstruct-description 'structure-object)))
1788 (setf
1789 ;; Note: This has an ALTERNATE-METACLASS only because of blind
1790 ;; clueless imitation of the CMU CL code -- dunno if or why it's
1791 ;; needed. -- WHN
1792 (dd-alternate-metaclass dd) '(t)
1793 (dd-slots dd) nil
1794 (dd-length dd) 1
1795 (dd-type dd) 'structure)
1796 (%compiler-set-up-layout dd)))
1797 (!set-up-structure-object-class)
1799 ;;; early structure predeclarations: Set up DD and LAYOUT for ordinary
1800 ;;; (non-ALTERNATE-METACLASS) structures which are needed early.
1801 (dolist (args
1802 '#.(sb-cold:read-from-file
1803 "src/code/early-defstruct-args.lisp-expr"))
1804 (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
1805 (first args)
1806 (rest args)))
1807 (inherits (inherits-for-structure dd)))
1808 (%compiler-defstruct dd inherits)))
1810 ;;; finding these beasts
1811 (defun find-defstruct-description (name &optional (errorp t))
1812 (let ((info (layout-info (classoid-layout (find-classoid name errorp)))))
1813 (if (defstruct-description-p info)
1814 info
1815 (when errorp
1816 (error "No DEFSTRUCT-DESCRIPTION for ~S." name)))))
1818 (/show0 "code/defstruct.lisp end of file")