new function: is_lisp_immediate()
[sbcl/pkhuong.git] / src / code / defstruct.lisp
blob07dd5b4711bb2838f575a4e431b0b56eacef7abe
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 ;; CLHS 3.4.6 Boa Lambda Lists
1520 (setf (dsd-safe-p slot) nil)
1521 '.do-not-initialize-slot.)
1522 ((or (find (dsd-name slot) (vars) :test #'string=)
1523 (let ((type (dsd-type slot)))
1524 (if (eq t type)
1525 (dsd-default slot)
1526 `(the ,type ,(dsd-default slot))))))))))))
1528 ;;; Grovel the constructor options, and decide what constructors (if
1529 ;;; any) to create.
1530 (defun constructor-definitions (defstruct)
1531 (let ((no-constructors nil)
1532 (boas ())
1533 (defaults ())
1534 (creator (ecase (dd-type defstruct)
1535 (structure #'create-structure-constructor)
1536 (vector #'create-vector-constructor)
1537 (list #'create-list-constructor))))
1538 (dolist (constructor (dd-constructors defstruct))
1539 (destructuring-bind (name &optional (boa-ll nil boa-p)) constructor
1540 (declare (ignore boa-ll))
1541 (cond ((not name) (setq no-constructors t))
1542 (boa-p (push constructor boas))
1543 (t (push name defaults)))))
1545 (when no-constructors
1546 (when (or defaults boas)
1547 (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs"))
1548 (return-from constructor-definitions ()))
1550 (unless (or defaults boas)
1551 (push (symbolicate "MAKE-" (dd-name defstruct)) defaults))
1553 (collect ((res) (names))
1554 (when defaults
1555 (let ((cname (first defaults)))
1556 (setf (dd-default-constructor defstruct) cname)
1557 (res (create-keyword-constructor defstruct creator))
1558 (names cname)
1559 (dolist (other-name (rest defaults))
1560 (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1561 (names other-name))))
1563 (dolist (boa boas)
1564 (res (create-boa-constructor defstruct boa creator))
1565 (names (first boa)))
1567 (res `(declaim (ftype
1568 (sfunction *
1569 ,(if (eq (dd-type defstruct) 'structure)
1570 (dd-name defstruct)
1571 '*))
1572 ,@(names))))
1574 (res))))
1576 ;;;; instances with ALTERNATE-METACLASS
1577 ;;;;
1578 ;;;; The CMU CL support for structures with ALTERNATE-METACLASS was a
1579 ;;;; fairly general extension embedded in the main DEFSTRUCT code, and
1580 ;;;; the result was an fairly impressive mess as ALTERNATE-METACLASS
1581 ;;;; extension mixed with ANSI CL generality (e.g. :TYPE and :INCLUDE)
1582 ;;;; and CMU CL implementation hairiness (esp. raw slots). This SBCL
1583 ;;;; version is much less ambitious, noticing that ALTERNATE-METACLASS
1584 ;;;; is only used to implement CONDITION, STANDARD-INSTANCE, and
1585 ;;;; GENERIC-FUNCTION, and defining a simple specialized
1586 ;;;; separate-from-DEFSTRUCT macro to provide only enough
1587 ;;;; functionality to support those.
1588 ;;;;
1589 ;;;; KLUDGE: The defining macro here is so specialized that it's ugly
1590 ;;;; in its own way. It also violates once-and-only-once by knowing
1591 ;;;; much about structures and layouts that is already known by the
1592 ;;;; main DEFSTRUCT macro. Hopefully it will go away presently
1593 ;;;; (perhaps when CL:CLASS and SB-PCL:CLASS meet) as per FIXME below.
1594 ;;;; -- WHN 2001-10-28
1595 ;;;;
1596 ;;;; FIXME: There seems to be no good reason to shoehorn CONDITION,
1597 ;;;; STANDARD-INSTANCE, and GENERIC-FUNCTION into mutated structures
1598 ;;;; instead of just implementing them as primitive objects. (This
1599 ;;;; reduced-functionality macro seems pretty close to the
1600 ;;;; functionality of DEFINE-PRIMITIVE-OBJECT..)
1602 (defun make-dd-with-alternate-metaclass (&key (class-name (missing-arg))
1603 (superclass-name (missing-arg))
1604 (metaclass-name (missing-arg))
1605 (dd-type (missing-arg))
1606 metaclass-constructor
1607 slot-names)
1608 (let* ((dd (make-defstruct-description class-name))
1609 (conc-name (concatenate 'string (symbol-name class-name) "-"))
1610 (dd-slots (let ((reversed-result nil)
1611 ;; The index starts at 1 for ordinary named
1612 ;; slots because slot 0 is magical, used for
1613 ;; the LAYOUT in CONDITIONs and
1614 ;; FUNCALLABLE-INSTANCEs. (This is the same
1615 ;; in ordinary structures too: see (INCF
1616 ;; DD-LENGTH) in
1617 ;; PARSE-DEFSTRUCT-NAME-AND-OPTIONS).
1618 (index 1))
1619 (dolist (slot-name slot-names)
1620 (push (make-defstruct-slot-description
1621 :name slot-name
1622 :index index
1623 :accessor-name (symbolicate conc-name slot-name))
1624 reversed-result)
1625 (incf index))
1626 (nreverse reversed-result))))
1627 (case dd-type
1628 ;; We don't support inheritance of alternate metaclass stuff,
1629 ;; and it's not a general-purpose facility, so sanity check our
1630 ;; own code.
1631 (structure
1632 (aver (eq superclass-name 't)))
1633 (funcallable-structure
1634 (aver (eq superclass-name 'function)))
1635 (t (bug "Unknown DD-TYPE in ALTERNATE-METACLASS: ~S" dd-type)))
1636 (setf (dd-alternate-metaclass dd) (list superclass-name
1637 metaclass-name
1638 metaclass-constructor)
1639 (dd-slots dd) dd-slots
1640 (dd-length dd) (1+ (length slot-names))
1641 (dd-type dd) dd-type)
1642 dd))
1644 ;;; make !DEFSTRUCT-WITH-ALTERNATE-METACLASS compilable by the host
1645 ;;; lisp, installing the information we need to reason about the
1646 ;;; structures (layouts and classoids).
1648 ;;; FIXME: we should share the parsing and the DD construction between
1649 ;;; this and the cross-compiler version, but my brain was too small to
1650 ;;; get that right. -- CSR, 2006-09-14
1651 #+sb-xc-host
1652 (defmacro !defstruct-with-alternate-metaclass
1653 (class-name &key
1654 (slot-names (missing-arg))
1655 (boa-constructor (missing-arg))
1656 (superclass-name (missing-arg))
1657 (metaclass-name (missing-arg))
1658 (metaclass-constructor (missing-arg))
1659 (dd-type (missing-arg))
1660 predicate
1661 (runtime-type-checks-p t))
1663 (declare (type (and list (not null)) slot-names))
1664 (declare (type (and symbol (not null))
1665 boa-constructor
1666 superclass-name
1667 metaclass-name
1668 metaclass-constructor))
1669 (declare (type symbol predicate))
1670 (declare (type (member structure funcallable-structure) dd-type))
1671 (declare (ignore boa-constructor predicate runtime-type-checks-p))
1673 (let* ((dd (make-dd-with-alternate-metaclass
1674 :class-name class-name
1675 :slot-names slot-names
1676 :superclass-name superclass-name
1677 :metaclass-name metaclass-name
1678 :metaclass-constructor metaclass-constructor
1679 :dd-type dd-type)))
1680 `(progn
1682 (eval-when (:compile-toplevel :load-toplevel :execute)
1683 (%compiler-set-up-layout ',dd ',(inherits-for-structure dd))))))
1685 (sb!xc:defmacro !defstruct-with-alternate-metaclass
1686 (class-name &key
1687 (slot-names (missing-arg))
1688 (boa-constructor (missing-arg))
1689 (superclass-name (missing-arg))
1690 (metaclass-name (missing-arg))
1691 (metaclass-constructor (missing-arg))
1692 (dd-type (missing-arg))
1693 predicate
1694 (runtime-type-checks-p t))
1696 (declare (type (and list (not null)) slot-names))
1697 (declare (type (and symbol (not null))
1698 boa-constructor
1699 superclass-name
1700 metaclass-name
1701 metaclass-constructor))
1702 (declare (type symbol predicate))
1703 (declare (type (member structure funcallable-structure) dd-type))
1705 (let* ((dd (make-dd-with-alternate-metaclass
1706 :class-name class-name
1707 :slot-names slot-names
1708 :superclass-name superclass-name
1709 :metaclass-name metaclass-name
1710 :metaclass-constructor metaclass-constructor
1711 :dd-type dd-type))
1712 (dd-slots (dd-slots dd))
1713 (dd-length (1+ (length slot-names)))
1714 (object-gensym (gensym "OBJECT"))
1715 (new-value-gensym (gensym "NEW-VALUE-"))
1716 (delayed-layout-form `(%delayed-get-compiler-layout ,class-name)))
1717 (multiple-value-bind (raw-maker-form raw-reffer-operator)
1718 (ecase dd-type
1719 (structure
1720 (values `(%make-structure-instance-macro ,dd nil)
1721 '%instance-ref))
1722 (funcallable-structure
1723 (values `(let ((,object-gensym
1724 (%make-funcallable-instance ,dd-length)))
1725 (setf (%funcallable-instance-layout ,object-gensym)
1726 ,delayed-layout-form)
1727 ,object-gensym)
1728 '%funcallable-instance-info)))
1729 `(progn
1731 (eval-when (:compile-toplevel :load-toplevel :execute)
1732 (%compiler-set-up-layout ',dd ',(inherits-for-structure dd)))
1734 ;; slot readers and writers
1735 (declaim (inline ,@(mapcar #'dsd-accessor-name dd-slots)))
1736 ,@(mapcar (lambda (dsd)
1737 `(defun ,(dsd-accessor-name dsd) (,object-gensym)
1738 ,@(when runtime-type-checks-p
1739 `((declare (type ,class-name ,object-gensym))))
1740 (,raw-reffer-operator ,object-gensym
1741 ,(dsd-index dsd))))
1742 dd-slots)
1743 (declaim (inline ,@(mapcar (lambda (dsd)
1744 `(setf ,(dsd-accessor-name dsd)))
1745 dd-slots)))
1746 ,@(mapcar (lambda (dsd)
1747 `(defun (setf ,(dsd-accessor-name dsd)) (,new-value-gensym
1748 ,object-gensym)
1749 ,@(when runtime-type-checks-p
1750 `((declare (type ,class-name ,object-gensym))))
1751 (setf (,raw-reffer-operator ,object-gensym
1752 ,(dsd-index dsd))
1753 ,new-value-gensym)))
1754 dd-slots)
1756 ;; constructor
1757 (defun ,boa-constructor ,slot-names
1758 (let ((,object-gensym ,raw-maker-form))
1759 ,@(mapcar (lambda (slot-name)
1760 (let ((dsd (find (symbol-name slot-name) dd-slots
1761 :key (lambda (x)
1762 (symbol-name (dsd-name x)))
1763 :test #'string=)))
1764 ;; KLUDGE: bug 117 bogowarning. Neither
1765 ;; DECLAREing the type nor TRULY-THE cut
1766 ;; the mustard -- it still gives warnings.
1767 (enforce-type dsd defstruct-slot-description)
1768 `(setf (,(dsd-accessor-name dsd) ,object-gensym)
1769 ,slot-name)))
1770 slot-names)
1771 ,object-gensym))
1773 ;; predicate
1774 ,@(when predicate
1775 ;; Just delegate to the compiler's type optimization
1776 ;; code, which knows how to generate inline type tests
1777 ;; for the whole CMU CL INSTANCE menagerie.
1778 `(defun ,predicate (,object-gensym)
1779 (typep ,object-gensym ',class-name)))))))
1781 ;;;; finalizing bootstrapping
1783 ;;; Set up DD and LAYOUT for STRUCTURE-OBJECT class itself.
1785 ;;; Ordinary structure classes effectively :INCLUDE STRUCTURE-OBJECT
1786 ;;; when they have no explicit :INCLUDEs, so (1) it needs to be set up
1787 ;;; before we can define ordinary structure classes, and (2) it's
1788 ;;; special enough (and simple enough) that we just build it by hand
1789 ;;; instead of trying to generalize the ordinary DEFSTRUCT code.
1790 (defun !set-up-structure-object-class ()
1791 (let ((dd (make-defstruct-description 'structure-object)))
1792 (setf
1793 ;; Note: This has an ALTERNATE-METACLASS only because of blind
1794 ;; clueless imitation of the CMU CL code -- dunno if or why it's
1795 ;; needed. -- WHN
1796 (dd-alternate-metaclass dd) '(t)
1797 (dd-slots dd) nil
1798 (dd-length dd) 1
1799 (dd-type dd) 'structure)
1800 (%compiler-set-up-layout dd)))
1801 (!set-up-structure-object-class)
1803 ;;; early structure predeclarations: Set up DD and LAYOUT for ordinary
1804 ;;; (non-ALTERNATE-METACLASS) structures which are needed early.
1805 (dolist (args
1806 '#.(sb-cold:read-from-file
1807 "src/code/early-defstruct-args.lisp-expr"))
1808 (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
1809 (first args)
1810 (rest args)))
1811 (inherits (inherits-for-structure dd)))
1812 (%compiler-defstruct dd inherits)))
1814 ;;; finding these beasts
1815 (defun find-defstruct-description (name &optional (errorp t))
1816 (let ((info (layout-info (classoid-layout (find-classoid name errorp)))))
1817 (if (defstruct-description-p info)
1818 info
1819 (when errorp
1820 (error "No DEFSTRUCT-DESCRIPTION for ~S." name)))))
1822 (/show0 "code/defstruct.lisp end of file")