1.0.20.9: fix DEFINE-STRUCTURE-SLOT-ADDRESSOR to work with raw slots as well
[sbcl/pkhuong.git] / src / code / defstruct.lisp
blobaac8f2dc22761554f0d9b3b930c98983fc8f3677
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)))))
313 (defun raw-slot-words (type)
314 (let ((rsd (find type *raw-slot-data-list* :key #'raw-slot-data-raw-type)))
315 (if rsd
316 (raw-slot-data-n-words rsd)
317 (error "Invalid raw slot type: ~S" type))))
319 ;;;; the legendary DEFSTRUCT macro itself (both CL:DEFSTRUCT and its
320 ;;;; close personal friend SB!XC:DEFSTRUCT)
322 ;;; Return a list of forms to install PRINT and MAKE-LOAD-FORM funs,
323 ;;; mentioning them in the expansion so that they can be compiled.
324 (defun class-method-definitions (defstruct)
325 (let ((name (dd-name defstruct)))
326 `((locally
327 ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant
328 ;; class names which creates fast but non-cold-loadable,
329 ;; non-compact code. In this context, we'd rather have
330 ;; compact, cold-loadable code. -- WHN 19990928
331 (declare (notinline find-classoid))
332 ,@(let ((pf (dd-print-function defstruct))
333 (po (dd-print-object defstruct))
334 (x (gensym))
335 (s (gensym)))
336 ;; Giving empty :PRINT-OBJECT or :PRINT-FUNCTION options
337 ;; leaves PO or PF equal to NIL. The user-level effect is
338 ;; to generate a PRINT-OBJECT method specialized for the type,
339 ;; implementing the default #S structure-printing behavior.
340 (when (or (eq pf nil) (eq po nil))
341 (setf pf '(default-structure-print)
342 po 0))
343 (flet (;; Given an arg from a :PRINT-OBJECT or :PRINT-FUNCTION
344 ;; option, return the value to pass as an arg to FUNCTION.
345 (farg (oarg)
346 (destructuring-bind (fun-name) oarg
347 fun-name)))
348 (cond ((not (eql pf 0))
349 `((def!method print-object ((,x ,name) ,s)
350 (funcall #',(farg pf)
353 *current-level-in-print*))))
354 ((not (eql po 0))
355 `((def!method print-object ((,x ,name) ,s)
356 (funcall #',(farg po) ,x ,s))))
357 (t nil))))
358 ,@(let ((pure (dd-pure defstruct)))
359 (cond ((eq pure t)
360 `((setf (layout-pure (classoid-layout
361 (find-classoid ',name)))
362 t)))
363 ((eq pure :substructure)
364 `((setf (layout-pure (classoid-layout
365 (find-classoid ',name)))
366 0)))))
367 ,@(let ((def-con (dd-default-constructor defstruct)))
368 (when (and def-con (not (dd-alternate-metaclass defstruct)))
369 `((setf (structure-classoid-constructor (find-classoid ',name))
370 #',def-con))))))))
372 ;;; shared logic for host macroexpansion for SB!XC:DEFSTRUCT and
373 ;;; cross-compiler macroexpansion for CL:DEFSTRUCT
374 (defmacro !expander-for-defstruct (name-and-options
375 slot-descriptions
376 expanding-into-code-for-xc-host-p)
377 `(let ((name-and-options ,name-and-options)
378 (slot-descriptions ,slot-descriptions)
379 (expanding-into-code-for-xc-host-p
380 ,expanding-into-code-for-xc-host-p))
381 (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
382 name-and-options
383 slot-descriptions))
384 (name (dd-name dd)))
385 (if (dd-class-p dd)
386 (let ((inherits (inherits-for-structure dd)))
387 `(progn
388 ;; Note we intentionally enforce package locks and
389 ;; call %DEFSTRUCT first, and especially before
390 ;; %COMPILER-DEFSTRUCT. %DEFSTRUCT has the tests (and
391 ;; resulting CERROR) for collisions with LAYOUTs which
392 ;; already exist in the runtime. If there are any
393 ;; collisions, we want the user's response to CERROR
394 ;; to control what happens. Especially, if the user
395 ;; responds to the collision with ABORT, we don't want
396 ;; %COMPILER-DEFSTRUCT to modify the definition of the
397 ;; class.
398 (with-single-package-locked-error
399 (:symbol ',name "defining ~A as a structure"))
400 (%defstruct ',dd ',inherits (sb!c:source-location))
401 (eval-when (:compile-toplevel :load-toplevel :execute)
402 (%compiler-defstruct ',dd ',inherits))
403 ,@(unless expanding-into-code-for-xc-host-p
404 (append ;; FIXME: We've inherited from CMU CL nonparallel
405 ;; code for creating copiers for typed and untyped
406 ;; structures. This should be fixed.
407 ;(copier-definition dd)
408 (constructor-definitions dd)
409 (class-method-definitions dd)))
410 ',name))
411 `(progn
412 (with-single-package-locked-error
413 (:symbol ',name "defining ~A as a structure"))
414 (eval-when (:compile-toplevel :load-toplevel :execute)
415 (setf (info :typed-structure :info ',name) ',dd))
416 (eval-when (:load-toplevel :execute)
417 (setf (info :source-location :typed-structure ',name)
418 (sb!c:source-location)))
419 ,@(unless expanding-into-code-for-xc-host-p
420 (append (typed-accessor-definitions dd)
421 (typed-predicate-definitions dd)
422 (typed-copier-definitions dd)
423 (constructor-definitions dd)
424 (when (dd-doc dd)
425 `((setf (fdocumentation ',(dd-name dd) 'structure)
426 ',(dd-doc dd))))))
427 ',name)))))
429 (sb!xc:defmacro defstruct (name-and-options &rest slot-descriptions)
430 #!+sb-doc
431 "DEFSTRUCT {Name | (Name Option*)} {Slot | (Slot [Default] {Key Value}*)}
432 Define the structure type Name. Instances are created by MAKE-<name>,
433 which takes &KEY arguments allowing initial slot values to the specified.
434 A SETF'able function <name>-<slot> is defined for each slot to read and
435 write slot values. <name>-p is a type predicate.
437 Popular DEFSTRUCT options (see manual for others):
439 (:CONSTRUCTOR Name)
440 (:PREDICATE Name)
441 Specify the name for the constructor or predicate.
443 (:CONSTRUCTOR Name Lambda-List)
444 Specify the name and arguments for a BOA constructor
445 (which is more efficient when keyword syntax isn't necessary.)
447 (:INCLUDE Supertype Slot-Spec*)
448 Make this type a subtype of the structure type Supertype. The optional
449 Slot-Specs override inherited slot options.
451 Slot options:
453 :TYPE Type-Spec
454 Asserts that the value of this slot is always of the specified type.
456 :READ-ONLY {T | NIL}
457 If true, no setter function is defined for this slot."
458 (!expander-for-defstruct name-and-options slot-descriptions nil))
459 #+sb-xc-host
460 (defmacro sb!xc:defstruct (name-and-options &rest slot-descriptions)
461 #!+sb-doc
462 "Cause information about a target structure to be built into the
463 cross-compiler."
464 (!expander-for-defstruct name-and-options slot-descriptions t))
466 ;;;; functions to generate code for various parts of DEFSTRUCT definitions
468 ;;; First, a helper to determine whether a name names an inherited
469 ;;; accessor.
470 (defun accessor-inherited-data (name defstruct)
471 (assoc name (dd-inherited-accessor-alist defstruct) :test #'eq))
473 ;;; Return a list of forms which create a predicate function for a
474 ;;; typed DEFSTRUCT.
475 (defun typed-predicate-definitions (defstruct)
476 (let ((name (dd-name defstruct))
477 (predicate-name (dd-predicate-name defstruct))
478 (argname (gensym)))
479 (when (and predicate-name (dd-named defstruct))
480 (let ((ltype (dd-lisp-type defstruct))
481 (name-index (cdr (car (last (find-name-indices defstruct))))))
482 `((defun ,predicate-name (,argname)
483 (and (typep ,argname ',ltype)
484 ,(cond
485 ((subtypep ltype 'list)
486 `(do ((head (the ,ltype ,argname) (cdr head))
487 (i 0 (1+ i)))
488 ((or (not (consp head)) (= i ,name-index))
489 (and (consp head) (eq ',name (car head))))))
490 ((subtypep ltype 'vector)
491 `(and (= (length (the ,ltype ,argname))
492 ,(dd-length defstruct))
493 (eq ',name (aref (the ,ltype ,argname) ,name-index))))
494 (t (bug "Uncatered-for lisp type in typed DEFSTRUCT: ~S."
495 ltype))))))))))
497 ;;; Return a list of forms to create a copier function of a typed DEFSTRUCT.
498 (defun typed-copier-definitions (defstruct)
499 (when (dd-copier-name defstruct)
500 `((setf (fdefinition ',(dd-copier-name defstruct)) #'copy-seq)
501 (declaim (ftype function ,(dd-copier-name defstruct))))))
503 ;;; Return a list of function definitions for accessing and setting
504 ;;; the slots of a typed DEFSTRUCT. The functions are proclaimed to be
505 ;;; inline, and the types of their arguments and results are declared
506 ;;; as well. We count on the compiler to do clever things with ELT.
507 (defun typed-accessor-definitions (defstruct)
508 (collect ((stuff))
509 (let ((ltype (dd-lisp-type defstruct)))
510 (dolist (slot (dd-slots defstruct))
511 (let ((name (dsd-accessor-name slot))
512 (index (dsd-index slot))
513 (slot-type `(and ,(dsd-type slot)
514 ,(dd-element-type defstruct))))
515 (let ((inherited (accessor-inherited-data name defstruct)))
516 (cond
517 ((not inherited)
518 (stuff `(declaim (inline ,name ,@(unless (dsd-read-only slot)
519 `((setf ,name))))))
520 ;; FIXME: The arguments in the next two DEFUNs should
521 ;; be gensyms. (Otherwise e.g. if NEW-VALUE happened to
522 ;; be the name of a special variable, things could get
523 ;; weird.)
524 (stuff `(defun ,name (structure)
525 (declare (type ,ltype structure))
526 (the ,slot-type (elt structure ,index))))
527 (unless (dsd-read-only slot)
528 (stuff
529 `(defun (setf ,name) (new-value structure)
530 (declare (type ,ltype structure) (type ,slot-type new-value))
531 (setf (elt structure ,index) new-value)))))
532 ((not (= (cdr inherited) index))
533 (style-warn "~@<Non-overwritten accessor ~S does not access ~
534 slot with name ~S (accessing an inherited slot ~
535 instead).~:@>" name (dsd-name slot))))))))
536 (stuff)))
538 ;;;; parsing
540 (defun require-no-print-options-so-far (defstruct)
541 (unless (and (eql (dd-print-function defstruct) 0)
542 (eql (dd-print-object defstruct) 0))
543 (error "No more than one of the following options may be specified:
544 :PRINT-FUNCTION, :PRINT-OBJECT, :TYPE")))
546 ;;; Parse a single DEFSTRUCT option and store the results in DD.
547 (defun parse-1-dd-option (option dd)
548 (let ((args (rest option))
549 (name (dd-name dd)))
550 (case (first option)
551 (:conc-name
552 (destructuring-bind (&optional conc-name) args
553 (setf (dd-conc-name dd)
554 (if (symbolp conc-name)
555 conc-name
556 (make-symbol (string conc-name))))))
557 (:constructor
558 (destructuring-bind (&optional (cname (symbolicate "MAKE-" name))
559 &rest stuff)
560 args
561 (push (cons cname stuff) (dd-constructors dd))))
562 (:copier
563 (destructuring-bind (&optional (copier (symbolicate "COPY-" name)))
564 args
565 (setf (dd-copier-name dd) copier)))
566 (:predicate
567 (destructuring-bind (&optional (predicate-name (symbolicate name "-P")))
568 args
569 (setf (dd-predicate-name dd) predicate-name)))
570 (:include
571 (when (dd-include dd)
572 (error "more than one :INCLUDE option"))
573 (setf (dd-include dd) args))
574 (:print-function
575 (require-no-print-options-so-far dd)
576 (setf (dd-print-function dd)
577 (the (or symbol cons) args)))
578 (:print-object
579 (require-no-print-options-so-far dd)
580 (setf (dd-print-object dd)
581 (the (or symbol cons) args)))
582 (:type
583 (destructuring-bind (type) args
584 (cond ((member type '(list vector))
585 (setf (dd-element-type dd) t)
586 (setf (dd-type dd) type))
587 ((and (consp type) (eq (first type) 'vector))
588 (destructuring-bind (vector vtype) type
589 (declare (ignore vector))
590 (setf (dd-element-type dd) vtype)
591 (setf (dd-type dd) 'vector)))
593 (error "~S is a bad :TYPE for DEFSTRUCT." type)))))
594 (:named
595 (error "The DEFSTRUCT option :NAMED takes no arguments."))
596 (:initial-offset
597 (destructuring-bind (offset) args
598 (setf (dd-offset dd) offset)))
599 (:pure
600 (destructuring-bind (fun) args
601 (setf (dd-pure dd) fun)))
602 (t (error "unknown DEFSTRUCT option:~% ~S" option)))))
604 ;;; Given name and options, return a DD holding that info.
605 (defun parse-defstruct-name-and-options (name-and-options)
606 (destructuring-bind (name &rest options) name-and-options
607 (aver name) ; A null name doesn't seem to make sense here.
608 (let ((dd (make-defstruct-description name)))
609 (dolist (option options)
610 (cond ((eq option :named)
611 (setf (dd-named dd) t))
612 ((consp option)
613 (parse-1-dd-option option dd))
614 ((member option '(:conc-name :constructor :copier :predicate))
615 (parse-1-dd-option (list option) dd))
617 (error "unrecognized DEFSTRUCT option: ~S" option))))
619 (case (dd-type dd)
620 (structure
621 (when (dd-offset dd)
622 (error ":OFFSET can't be specified unless :TYPE is specified."))
623 (unless (dd-include dd)
624 ;; FIXME: It'd be cleaner to treat no-:INCLUDE as defaulting
625 ;; to :INCLUDE STRUCTURE-OBJECT, and then let the general-case
626 ;; (INCF (DD-LENGTH DD) (DD-LENGTH included-DD)) logic take
627 ;; care of this. (Except that the :TYPE VECTOR and :TYPE
628 ;; LIST cases, with their :NAMED and un-:NAMED flavors,
629 ;; make that messy, alas.)
630 (incf (dd-length dd))))
632 (require-no-print-options-so-far dd)
633 (when (dd-named dd)
634 (incf (dd-length dd)))
635 (let ((offset (dd-offset dd)))
636 (when offset (incf (dd-length dd) offset)))))
638 (when (dd-include dd)
639 (frob-dd-inclusion-stuff dd))
641 dd)))
643 ;;; Given name and options and slot descriptions (and possibly doc
644 ;;; string at the head of slot descriptions) return a DD holding that
645 ;;; info.
646 (defun parse-defstruct-name-and-options-and-slot-descriptions
647 (name-and-options slot-descriptions)
648 (let ((result (parse-defstruct-name-and-options (if (atom name-and-options)
649 (list name-and-options)
650 name-and-options))))
651 (when (stringp (car slot-descriptions))
652 (setf (dd-doc result) (pop slot-descriptions)))
653 (dolist (slot-description slot-descriptions)
654 (allocate-1-slot result (parse-1-dsd result slot-description)))
655 result))
657 ;;;; stuff to parse slot descriptions
659 ;;; Parse a slot description for DEFSTRUCT, add it to the description
660 ;;; and return it. If supplied, SLOT is a pre-initialized DSD
661 ;;; that we modify to get the new slot. This is supplied when handling
662 ;;; included slots.
663 (defun parse-1-dsd (defstruct spec &optional
664 (slot (make-defstruct-slot-description :name ""
665 :index 0
666 :type t)))
667 (multiple-value-bind (name default default-p type type-p read-only ro-p)
668 (typecase spec
669 (symbol
670 (when (keywordp spec)
671 (style-warn "Keyword slot name indicates probable syntax ~
672 error in DEFSTRUCT: ~S."
673 spec))
674 spec)
675 (cons
676 (destructuring-bind
677 (name
678 &optional (default nil default-p)
679 &key (type nil type-p) (read-only nil ro-p))
680 spec
681 (values name
682 default default-p
683 (uncross type) type-p
684 read-only ro-p)))
685 (t (error 'simple-program-error
686 :format-control "in DEFSTRUCT, ~S is not a legal slot ~
687 description."
688 :format-arguments (list spec))))
690 (when (find name (dd-slots defstruct)
691 :test #'string=
692 :key (lambda (x) (symbol-name (dsd-name x))))
693 (error 'simple-program-error
694 :format-control "duplicate slot name ~S"
695 :format-arguments (list name)))
696 (setf (dsd-name slot) name)
697 (setf (dd-slots defstruct) (nconc (dd-slots defstruct) (list slot)))
699 (let ((accessor-name (if (dd-conc-name defstruct)
700 (symbolicate (dd-conc-name defstruct) name)
701 name))
702 (predicate-name (dd-predicate-name defstruct)))
703 (setf (dsd-accessor-name slot) accessor-name)
704 (when (eql accessor-name predicate-name)
705 ;; Some adventurous soul has named a slot so that its accessor
706 ;; collides with the structure type predicate. ANSI doesn't
707 ;; specify what to do in this case. As of 2001-09-04, Martin
708 ;; Atzmueller reports that CLISP and Lispworks both give
709 ;; priority to the slot accessor, so that the predicate is
710 ;; overwritten. We might as well do the same (as well as
711 ;; signalling a warning).
712 (style-warn
713 "~@<The structure accessor name ~S is the same as the name of the ~
714 structure type predicate. ANSI doesn't specify what to do in ~
715 this case. We'll overwrite the type predicate with the slot ~
716 accessor, but you can't rely on this behavior, so it'd be wise to ~
717 remove the ambiguity in your code.~@:>"
718 accessor-name)
719 (setf (dd-predicate-name defstruct) nil))
720 ;; FIXME: It would be good to check for name collisions here, but
721 ;; the easy check,
722 ;;x#-sb-xc-host
723 ;;x(when (and (fboundp accessor-name)
724 ;;x (not (accessor-inherited-data accessor-name defstruct)))
725 ;;x (style-warn "redefining ~S in DEFSTRUCT" accessor-name)))
726 ;; which was done until sbcl-0.8.11.18 or so, is wrong: it causes
727 ;; a warning at MACROEXPAND time, when instead the warning should
728 ;; occur not just because the code was constructed, but because it
729 ;; is actually compiled or loaded.
732 (when default-p
733 (setf (dsd-default slot) default))
734 (when type-p
735 (setf (dsd-type slot)
736 (if (eq (dsd-type slot) t)
737 type
738 `(and ,(dsd-type slot) ,type))))
739 (when ro-p
740 (if read-only
741 (setf (dsd-read-only slot) t)
742 (when (dsd-read-only slot)
743 (error "~@<The slot ~S is :READ-ONLY in superclass, and so must ~
744 be :READ-ONLY in subclass.~:@>"
745 (dsd-name slot)))))
746 slot))
748 ;;; When a value of type TYPE is stored in a structure, should it be
749 ;;; stored in a raw slot? Return the matching RAW-SLOT-DATA structure
750 ;; if TYPE should be stored in a raw slot, or NIL if not.
751 (defun structure-raw-slot-data (type)
752 (multiple-value-bind (fixnum? fixnum-certain?)
753 (sb!xc:subtypep type 'fixnum)
754 ;; (The extra test for FIXNUM-CERTAIN? here is intended for
755 ;; bootstrapping the system. In particular, in sbcl-0.6.2, we set up
756 ;; LAYOUT before FIXNUM is defined, and so could bogusly end up
757 ;; putting INDEX-typed values into raw slots if we didn't test
758 ;; FIXNUM-CERTAIN?.)
759 (if (or fixnum? (not fixnum-certain?))
761 (dolist (data *raw-slot-data-list*)
762 (when (sb!xc:subtypep type (raw-slot-data-raw-type data))
763 (return data))))))
765 ;;; Allocate storage for a DSD in DD. This is where we decide whether
766 ;;; a slot is raw or not. Raw objects are aligned on the unit of their size.
767 (defun allocate-1-slot (dd dsd)
768 (let ((rsd
769 (if (eq (dd-type dd) 'structure)
770 (structure-raw-slot-data (dsd-type dsd))
771 nil)))
772 (cond
773 ((null rsd)
774 (setf (dsd-index dsd) (dd-length dd))
775 (incf (dd-length dd)))
777 (let* ((words (raw-slot-data-n-words rsd))
778 (alignment (raw-slot-data-alignment rsd))
779 (off (rem (dd-raw-length dd) alignment)))
780 (unless (zerop off)
781 (incf (dd-raw-length dd) (- alignment off)))
782 (setf (dsd-raw-type dsd) (raw-slot-data-raw-type rsd))
783 (setf (dsd-index dsd) (dd-raw-length dd))
784 (incf (dd-raw-length dd) words)))))
785 (values))
787 (defun typed-structure-info-or-lose (name)
788 (or (info :typed-structure :info name)
789 (error ":TYPE'd DEFSTRUCT ~S not found for inclusion." name)))
791 ;;; Process any included slots pretty much like they were specified.
792 ;;; Also inherit various other attributes.
793 (defun frob-dd-inclusion-stuff (dd)
794 (destructuring-bind (included-name &rest modified-slots) (dd-include dd)
795 (let* ((type (dd-type dd))
796 (included-structure
797 (if (dd-class-p dd)
798 (layout-info (compiler-layout-or-lose included-name))
799 (typed-structure-info-or-lose included-name))))
801 ;; checks on legality
802 (unless (and (eq type (dd-type included-structure))
803 (type= (specifier-type (dd-element-type included-structure))
804 (specifier-type (dd-element-type dd))))
805 (error ":TYPE option mismatch between structures ~S and ~S"
806 (dd-name dd) included-name))
807 (let ((included-classoid (find-classoid included-name nil)))
808 (when included-classoid
809 ;; It's not particularly well-defined to :INCLUDE any of the
810 ;; CMU CL INSTANCE weirdosities like CONDITION or
811 ;; GENERIC-FUNCTION, and it's certainly not ANSI-compliant.
812 (let* ((included-layout (classoid-layout included-classoid))
813 (included-dd (layout-info included-layout)))
814 (when (and (dd-alternate-metaclass included-dd)
815 ;; As of sbcl-0.pre7.73, anyway, STRUCTURE-OBJECT
816 ;; is represented with an ALTERNATE-METACLASS. But
817 ;; it's specifically OK to :INCLUDE (and PCL does)
818 ;; so in this one case, it's OK to include
819 ;; something with :ALTERNATE-METACLASS after all.
820 (not (eql included-name 'structure-object)))
821 (error "can't :INCLUDE class ~S (has alternate metaclass)"
822 included-name)))))
824 (incf (dd-length dd) (dd-length included-structure))
825 (when (dd-class-p dd)
826 (let ((mc (rest (dd-alternate-metaclass included-structure))))
827 (when (and mc (not (dd-alternate-metaclass dd)))
828 (setf (dd-alternate-metaclass dd)
829 (cons included-name mc))))
830 (when (eq (dd-pure dd) :unspecified)
831 (setf (dd-pure dd) (dd-pure included-structure)))
832 (setf (dd-raw-length dd) (dd-raw-length included-structure)))
834 (setf (dd-inherited-accessor-alist dd)
835 (dd-inherited-accessor-alist included-structure))
836 (dolist (included-slot (dd-slots included-structure))
837 (let* ((included-name (dsd-name included-slot))
838 (modified (or (find included-name modified-slots
839 :key (lambda (x) (if (atom x) x (car x)))
840 :test #'string=)
841 `(,included-name))))
842 ;; We stash away an alist of accessors to parents' slots
843 ;; that have already been created to avoid conflicts later
844 ;; so that structures with :INCLUDE and :CONC-NAME (and
845 ;; other edge cases) can work as specified.
846 (when (dsd-accessor-name included-slot)
847 ;; the "oldest" (i.e. highest up the tree of inheritance)
848 ;; will prevail, so don't push new ones on if they
849 ;; conflict.
850 (pushnew (cons (dsd-accessor-name included-slot)
851 (dsd-index included-slot))
852 (dd-inherited-accessor-alist dd)
853 :test #'eq :key #'car))
854 (let ((new-slot (parse-1-dsd dd
855 modified
856 (copy-structure included-slot))))
857 (when (and (neq (dsd-type new-slot) (dsd-type included-slot))
858 (not (sb!xc:subtypep (dsd-type included-slot)
859 (dsd-type new-slot)))
860 (dsd-safe-p included-slot))
861 (setf (dsd-safe-p new-slot) nil)
862 ;; XXX: notify?
863 )))))))
865 ;;;; various helper functions for setting up DEFSTRUCTs
867 ;;; This function is called at macroexpand time to compute the INHERITS
868 ;;; vector for a structure type definition.
869 (defun inherits-for-structure (info)
870 (declare (type defstruct-description info))
871 (let* ((include (dd-include info))
872 (superclass-opt (dd-alternate-metaclass info))
873 (super
874 (if include
875 (compiler-layout-or-lose (first include))
876 (classoid-layout (find-classoid
877 (or (first superclass-opt)
878 'structure-object))))))
879 (case (dd-name info)
880 ((ansi-stream)
881 (concatenate 'simple-vector
882 (layout-inherits super)
883 (vector super (classoid-layout (find-classoid 'stream)))))
884 ((fd-stream)
885 (concatenate 'simple-vector
886 (layout-inherits super)
887 (vector super
888 (classoid-layout (find-classoid 'file-stream)))))
889 ((sb!impl::string-input-stream
890 sb!impl::string-output-stream
891 sb!impl::fill-pointer-output-stream)
892 (concatenate 'simple-vector
893 (layout-inherits super)
894 (vector super
895 (classoid-layout (find-classoid 'string-stream)))))
896 (t (concatenate 'simple-vector
897 (layout-inherits super)
898 (vector super))))))
900 ;;; Do miscellaneous (LOAD EVAL) time actions for the structure
901 ;;; described by DD. Create the class and LAYOUT, checking for
902 ;;; incompatible redefinition. Define those functions which are
903 ;;; sufficiently stereotyped that we can implement them as standard
904 ;;; closures.
905 (defun %defstruct (dd inherits source-location)
906 (declare (type defstruct-description dd))
908 ;; We set up LAYOUTs even in the cross-compilation host.
909 (multiple-value-bind (classoid layout old-layout)
910 (ensure-structure-class dd inherits "current" "new")
911 (cond ((not old-layout)
912 (unless (eq (classoid-layout classoid) layout)
913 (register-layout layout)))
915 (let ((old-dd (layout-info old-layout)))
916 (when (defstruct-description-p old-dd)
917 (dolist (slot (dd-slots old-dd))
918 (fmakunbound (dsd-accessor-name slot))
919 (unless (dsd-read-only slot)
920 (fmakunbound `(setf ,(dsd-accessor-name slot)))))))
921 (%redefine-defstruct classoid old-layout layout)
922 (setq layout (classoid-layout classoid))))
923 (setf (find-classoid (dd-name dd)) classoid)
925 (sb!c:with-source-location (source-location)
926 (setf (layout-source-location layout) source-location))
928 ;; Various other operations only make sense on the target SBCL.
929 #-sb-xc-host
930 (%target-defstruct dd layout))
932 (values))
934 ;;; Return a form describing the writable place used for this slot
935 ;;; in the instance named INSTANCE-NAME.
936 (defun %accessor-place-form (dd dsd instance-name)
937 (let (;; the operator that we'll use to access a typed slot
938 (ref (ecase (dd-type dd)
939 (structure '%instance-ref)
940 (list 'nth-but-with-sane-arg-order)
941 (vector 'aref)))
942 (raw-type (dsd-raw-type dsd)))
943 (if (eq raw-type t) ; if not raw slot
944 `(,ref ,instance-name ,(dsd-index dsd))
945 (let* ((raw-slot-data (find raw-type *raw-slot-data-list*
946 :key #'raw-slot-data-raw-type
947 :test #'equal))
948 (raw-slot-accessor (raw-slot-data-accessor-name raw-slot-data)))
949 `(,raw-slot-accessor ,instance-name ,(dsd-index dsd))))))
951 ;;; Return source transforms for the reader and writer functions of
952 ;;; the slot described by DSD. They should be inline expanded, but
953 ;;; source transforms work faster.
954 (defun slot-accessor-transforms (dd dsd)
955 (let ((accessor-place-form (%accessor-place-form dd dsd
956 `(the ,(dd-name dd) instance)))
957 (dsd-type (dsd-type dsd))
958 (value-the (if (dsd-safe-p dsd) 'truly-the 'the)))
959 (values (sb!c:source-transform-lambda (instance)
960 `(,value-the ,dsd-type ,(subst instance 'instance
961 accessor-place-form)))
962 (sb!c:source-transform-lambda (new-value instance)
963 (destructuring-bind (accessor-name &rest accessor-args)
964 accessor-place-form
965 (once-only ((new-value new-value)
966 (instance instance))
967 `(,(info :setf :inverse accessor-name)
968 ,@(subst instance 'instance accessor-args)
969 (the ,dsd-type ,new-value))))))))
971 ;;; Return a LAMBDA form which can be used to set a slot.
972 (defun slot-setter-lambda-form (dd dsd)
973 ;; KLUDGE: Evaluating the results of SLOT-ACCESSOR-TRANSFORMS needs
974 ;; a lexenv.
975 (let ((sb!c:*lexenv* (if (boundp 'sb!c:*lexenv*)
976 sb!c:*lexenv*
977 (sb!c::make-null-lexenv))))
978 `(lambda (new-value instance)
979 ,(funcall (nth-value 1 (slot-accessor-transforms dd dsd))
980 '(dummy new-value instance)))))
982 ;;; core compile-time setup of any class with a LAYOUT, used even by
983 ;;; !DEFSTRUCT-WITH-ALTERNATE-METACLASS weirdosities
984 (defun %compiler-set-up-layout (dd
985 &optional
986 ;; Several special cases
987 ;; (STRUCTURE-OBJECT itself, and
988 ;; structures with alternate
989 ;; metaclasses) call this function
990 ;; directly, and they're all at the
991 ;; base of the instance class
992 ;; structure, so this is a handy
993 ;; default. (But note
994 ;; FUNCALLABLE-STRUCTUREs need
995 ;; assistance here)
996 (inherits (vector (find-layout t))))
998 (multiple-value-bind (classoid layout old-layout)
999 (multiple-value-bind (clayout clayout-p)
1000 (info :type :compiler-layout (dd-name dd))
1001 (ensure-structure-class dd
1002 inherits
1003 (if clayout-p "previously compiled" "current")
1004 "compiled"
1005 :compiler-layout clayout))
1006 (cond (old-layout
1007 (undefine-structure (layout-classoid old-layout))
1008 (when (and (classoid-subclasses classoid)
1009 (not (eq layout old-layout)))
1010 (collect ((subs))
1011 (dohash ((classoid layout) (classoid-subclasses classoid)
1012 :locked t)
1013 (declare (ignore layout))
1014 (undefine-structure classoid)
1015 (subs (classoid-proper-name classoid)))
1016 (when (subs)
1017 (warn "removing old subclasses of ~S:~% ~S"
1018 (classoid-name classoid)
1019 (subs))))))
1021 (unless (eq (classoid-layout classoid) layout)
1022 (register-layout layout :invalidate nil))
1023 (setf (find-classoid (dd-name dd)) classoid)))
1025 ;; At this point the class should be set up in the INFO database.
1026 ;; But the logic that enforces this is a little tangled and
1027 ;; scattered, so it's not obvious, so let's check.
1028 (aver (find-classoid (dd-name dd) nil))
1030 (setf (info :type :compiler-layout (dd-name dd)) layout))
1032 (values))
1034 ;;; Do (COMPILE LOAD EVAL)-time actions for the normal (not
1035 ;;; ALTERNATE-LAYOUT) DEFSTRUCT described by DD.
1036 (defun %compiler-defstruct (dd inherits)
1037 (declare (type defstruct-description dd))
1039 (%compiler-set-up-layout dd inherits)
1041 (let* ((dtype (dd-declarable-type dd)))
1043 (let ((copier-name (dd-copier-name dd)))
1044 (when copier-name
1045 (sb!xc:proclaim `(ftype (sfunction (,dtype) ,dtype) ,copier-name))))
1047 (let ((predicate-name (dd-predicate-name dd)))
1048 (when predicate-name
1049 (sb!xc:proclaim `(ftype (sfunction (t) boolean) ,predicate-name))
1050 ;; Provide inline expansion (or not).
1051 (ecase (dd-type dd)
1052 ((structure funcallable-structure)
1053 ;; Let the predicate be inlined.
1054 (setf (info :function :inline-expansion-designator predicate-name)
1055 (lambda ()
1056 `(lambda (x)
1057 ;; This dead simple definition works because the
1058 ;; type system knows how to generate inline type
1059 ;; tests for instances.
1060 (typep x ',(dd-name dd))))
1061 (info :function :inlinep predicate-name)
1062 :inline))
1063 ((list vector)
1064 ;; Just punt. We could provide inline expansions for :TYPE
1065 ;; LIST and :TYPE VECTOR predicates too, but it'd be a
1066 ;; little messier and we don't bother. (Does anyway use
1067 ;; typed DEFSTRUCTs at all, let alone for high
1068 ;; performance?)
1069 ))))
1071 (dolist (dsd (dd-slots dd))
1072 (let* ((accessor-name (dsd-accessor-name dsd))
1073 (dsd-type (dsd-type dsd)))
1074 (when accessor-name
1075 (setf (info :function :structure-accessor accessor-name) dd)
1076 (let ((inherited (accessor-inherited-data accessor-name dd)))
1077 (cond
1078 ((not inherited)
1079 (multiple-value-bind (reader-designator writer-designator)
1080 (slot-accessor-transforms dd dsd)
1081 (sb!xc:proclaim `(ftype (sfunction (,dtype) ,dsd-type)
1082 ,accessor-name))
1083 (setf (info :function :source-transform accessor-name)
1084 reader-designator)
1085 (unless (dsd-read-only dsd)
1086 (let ((setf-accessor-name `(setf ,accessor-name)))
1087 (sb!xc:proclaim
1088 `(ftype (sfunction (,dsd-type ,dtype) ,dsd-type)
1089 ,setf-accessor-name))
1090 (setf (info :function :source-transform setf-accessor-name)
1091 writer-designator)))))
1092 ((not (= (cdr inherited) (dsd-index dsd)))
1093 (style-warn "~@<Non-overwritten accessor ~S does not access ~
1094 slot with name ~S (accessing an inherited slot ~
1095 instead).~:@>"
1096 accessor-name
1097 (dsd-name dsd)))))))))
1098 (values))
1100 ;;;; redefinition stuff
1102 ;;; Compare the slots of OLD and NEW, returning 3 lists of slot names:
1103 ;;; 1. Slots which have moved,
1104 ;;; 2. Slots whose type has changed,
1105 ;;; 3. Deleted slots.
1106 (defun compare-slots (old new)
1107 (let* ((oslots (dd-slots old))
1108 (nslots (dd-slots new))
1109 (onames (mapcar #'dsd-name oslots))
1110 (nnames (mapcar #'dsd-name nslots)))
1111 (collect ((moved)
1112 (retyped))
1113 (dolist (name (intersection onames nnames))
1114 (let ((os (find name oslots :key #'dsd-name :test #'string=))
1115 (ns (find name nslots :key #'dsd-name :test #'string=)))
1116 (unless (sb!xc:subtypep (dsd-type ns) (dsd-type os))
1117 (retyped name))
1118 (unless (and (= (dsd-index os) (dsd-index ns))
1119 (eq (dsd-raw-type os) (dsd-raw-type ns)))
1120 (moved name))))
1121 (values (moved)
1122 (retyped)
1123 (set-difference onames nnames :test #'string=)))))
1125 ;;; If we are redefining a structure with different slots than in the
1126 ;;; currently loaded version, give a warning and return true.
1127 (defun redefine-structure-warning (classoid old new)
1128 (declare (type defstruct-description old new)
1129 (type classoid classoid)
1130 (ignore classoid))
1131 (let ((name (dd-name new)))
1132 (multiple-value-bind (moved retyped deleted) (compare-slots old new)
1133 (when (or moved retyped deleted)
1134 (warn
1135 "incompatibly redefining slots of structure class ~S~@
1136 Make sure any uses of affected accessors are recompiled:~@
1137 ~@[ These slots were moved to new positions:~% ~S~%~]~
1138 ~@[ These slots have new incompatible types:~% ~S~%~]~
1139 ~@[ These slots were deleted:~% ~S~%~]"
1140 name moved retyped deleted)
1141 t))))
1143 ;;; This function is called when we are incompatibly redefining a
1144 ;;; structure CLASS to have the specified NEW-LAYOUT. We signal an
1145 ;;; error with some proceed options and return the layout that should
1146 ;;; be used.
1147 (defun %redefine-defstruct (classoid old-layout new-layout)
1148 (declare (type classoid classoid)
1149 (type layout old-layout new-layout))
1150 (let ((name (classoid-proper-name classoid)))
1151 (restart-case
1152 (error "~@<attempt to redefine the ~S class ~S incompatibly with the current definition~:@>"
1153 'structure-object
1154 name)
1155 (continue ()
1156 :report (lambda (s)
1157 (format s
1158 "~@<Use the new definition of ~S, invalidating ~
1159 already-loaded code and instances.~@:>"
1160 name))
1161 (register-layout new-layout))
1162 (recklessly-continue ()
1163 :report (lambda (s)
1164 (format s
1165 "~@<Use the new definition of ~S as if it were ~
1166 compatible, allowing old accessors to use new ~
1167 instances and allowing new accessors to use old ~
1168 instances.~@:>"
1169 name))
1170 ;; classic CMU CL warning: "Any old ~S instances will be in a bad way.
1171 ;; I hope you know what you're doing..."
1172 (register-layout new-layout
1173 :invalidate nil
1174 :destruct-layout old-layout))
1175 (clobber-it ()
1176 ;; FIXME: deprecated 2002-10-16, and since it's only interactive
1177 ;; hackery instead of a supported feature, can probably be deleted
1178 ;; in early 2003
1179 :report "(deprecated synonym for RECKLESSLY-CONTINUE)"
1180 (register-layout new-layout
1181 :invalidate nil
1182 :destruct-layout old-layout))))
1183 (values))
1185 (declaim (inline dd-layout-length))
1186 (defun dd-layout-length (dd)
1187 (+ (dd-length dd) (dd-raw-length dd)))
1189 (declaim (ftype (sfunction (defstruct-description) index) dd-instance-length))
1190 (defun dd-instance-length (dd)
1191 ;; Make sure the object ends at a two-word boundary. Note that this does
1192 ;; not affect the amount of memory used, since the allocator would add the
1193 ;; same padding anyway. However, raw slots are indexed from the length of
1194 ;; the object as indicated in the header, so the pad word needs to be
1195 ;; included in that length to guarantee proper alignment of raw double float
1196 ;; slots, necessary for (at least) the SPARC backend.
1197 (let ((layout-length (dd-layout-length dd)))
1198 (declare (index layout-length))
1199 (+ layout-length (mod (1+ layout-length) 2))))
1201 ;;; This is called when we are about to define a structure class. It
1202 ;;; returns a (possibly new) class object and the layout which should
1203 ;;; be used for the new definition (may be the current layout, and
1204 ;;; also might be an uninstalled forward referenced layout.) The third
1205 ;;; value is true if this is an incompatible redefinition, in which
1206 ;;; case it is the old layout.
1207 (defun ensure-structure-class (info inherits old-context new-context
1208 &key compiler-layout)
1209 (multiple-value-bind (class old-layout)
1210 (destructuring-bind
1211 (&optional
1212 name
1213 (class 'structure-classoid)
1214 (constructor 'make-structure-classoid))
1215 (dd-alternate-metaclass info)
1216 (declare (ignore name))
1217 (insured-find-classoid (dd-name info)
1218 (if (eq class 'structure-classoid)
1219 (lambda (x)
1220 (sb!xc:typep x 'structure-classoid))
1221 (lambda (x)
1222 (sb!xc:typep x (classoid-name (find-classoid class)))))
1223 (fdefinition constructor)))
1224 (setf (classoid-direct-superclasses class)
1225 (case (dd-name info)
1226 ((ansi-stream
1227 fd-stream
1228 sb!impl::string-input-stream sb!impl::string-output-stream
1229 sb!impl::fill-pointer-output-stream)
1230 (list (layout-classoid (svref inherits (1- (length inherits))))
1231 (layout-classoid (svref inherits (- (length inherits) 2)))))
1233 (list (layout-classoid
1234 (svref inherits (1- (length inherits))))))))
1235 (let ((new-layout (make-layout :classoid class
1236 :inherits inherits
1237 :depthoid (length inherits)
1238 :length (dd-layout-length info)
1239 :n-untagged-slots (dd-raw-length info)
1240 :info info))
1241 (old-layout (or compiler-layout old-layout)))
1242 (cond
1243 ((not old-layout)
1244 (values class new-layout nil))
1245 (;; This clause corresponds to an assertion in REDEFINE-LAYOUT-WARNING
1246 ;; of classic CMU CL. I moved it out to here because it was only
1247 ;; exercised in this code path anyway. -- WHN 19990510
1248 (not (eq (layout-classoid new-layout) (layout-classoid old-layout)))
1249 (error "shouldn't happen: weird state of OLD-LAYOUT?"))
1250 ((not *type-system-initialized*)
1251 (setf (layout-info old-layout) info)
1252 (values class old-layout nil))
1253 ((redefine-layout-warning old-context
1254 old-layout
1255 new-context
1256 (layout-length new-layout)
1257 (layout-inherits new-layout)
1258 (layout-depthoid new-layout)
1259 (layout-n-untagged-slots new-layout))
1260 (values class new-layout old-layout))
1262 (let ((old-info (layout-info old-layout)))
1263 (typecase old-info
1264 ((or defstruct-description)
1265 (cond ((redefine-structure-warning class old-info info)
1266 (values class new-layout old-layout))
1268 (setf (layout-info old-layout) info)
1269 (values class old-layout nil))))
1270 (null
1271 (setf (layout-info old-layout) info)
1272 (values class old-layout nil))
1274 (error "shouldn't happen! strange thing in LAYOUT-INFO:~% ~S"
1275 old-layout)
1276 (values class new-layout old-layout)))))))))
1278 ;;; Blow away all the compiler info for the structure CLASS. Iterate
1279 ;;; over this type, clearing the compiler structure type info, and
1280 ;;; undefining all the associated functions.
1281 (defun undefine-structure (class)
1282 (let ((info (layout-info (classoid-layout class))))
1283 (when (defstruct-description-p info)
1284 (let ((type (dd-name info)))
1285 (remhash type *typecheckfuns*)
1286 (setf (info :type :compiler-layout type) nil)
1287 (undefine-fun-name (dd-copier-name info))
1288 (undefine-fun-name (dd-predicate-name info))
1289 (dolist (slot (dd-slots info))
1290 (let ((fun (dsd-accessor-name slot)))
1291 (unless (accessor-inherited-data fun info)
1292 (undefine-fun-name fun)
1293 (unless (dsd-read-only slot)
1294 (undefine-fun-name `(setf ,fun)))))))
1295 ;; Clear out the SPECIFIER-TYPE cache so that subsequent
1296 ;; references are unknown types.
1297 (values-specifier-type-cache-clear)))
1298 (values))
1300 ;;; Return a list of pairs (name . index). Used for :TYPE'd
1301 ;;; constructors to find all the names that we have to splice in &
1302 ;;; where. Note that these types don't have a layout, so we can't look
1303 ;;; at LAYOUT-INHERITS.
1304 (defun find-name-indices (defstruct)
1305 (collect ((res))
1306 (let ((infos ()))
1307 (do ((info defstruct
1308 (typed-structure-info-or-lose (first (dd-include info)))))
1309 ((not (dd-include info))
1310 (push info infos))
1311 (push info infos))
1313 (let ((i 0))
1314 (dolist (info infos)
1315 (incf i (or (dd-offset info) 0))
1316 (when (dd-named info)
1317 (res (cons (dd-name info) i)))
1318 (setq i (dd-length info)))))
1320 (res)))
1322 ;;; These functions are called to actually make a constructor after we
1323 ;;; have processed the arglist. The correct variant (according to the
1324 ;;; DD-TYPE) should be called. The function is defined with the
1325 ;;; specified name and arglist. VARS and TYPES are used for argument
1326 ;;; type declarations. VALUES are the values for the slots (in order.)
1328 ;;; This is split three ways because:
1329 ;;; * LIST & VECTOR structures need "name" symbols stuck in at
1330 ;;; various weird places, whereas STRUCTURE structures have
1331 ;;; a LAYOUT slot.
1332 ;;; * We really want to use LIST to make list structures, instead of
1333 ;;; MAKE-LIST/(SETF ELT). (We can't in general use VECTOR in an
1334 ;;; analogous way, since VECTOR makes a SIMPLE-VECTOR and vector-typed
1335 ;;; structures can have arbitrary subtypes of VECTOR, not necessarily
1336 ;;; SIMPLE-VECTOR.)
1337 ;;; * STRUCTURE structures can have raw slots that must also be
1338 ;;; allocated and indirectly referenced.
1339 (defun create-vector-constructor (dd cons-name arglist vars types values)
1340 (let ((temp (gensym))
1341 (etype (dd-element-type dd)))
1342 `(defun ,cons-name ,arglist
1343 (declare ,@(mapcar (lambda (var type) `(type (and ,type ,etype) ,var))
1344 vars types))
1345 (let ((,temp (make-array ,(dd-length dd)
1346 :element-type ',(dd-element-type dd))))
1347 ,@(mapcar (lambda (x)
1348 `(setf (aref ,temp ,(cdr x)) ',(car x)))
1349 (find-name-indices dd))
1350 ,@(mapcar (lambda (dsd value)
1351 (unless (eq value '.do-not-initialize-slot.)
1352 `(setf (aref ,temp ,(dsd-index dsd)) ,value)))
1353 (dd-slots dd) values)
1354 ,temp))))
1355 (defun create-list-constructor (dd cons-name arglist vars types values)
1356 (let ((vals (make-list (dd-length dd) :initial-element nil)))
1357 (dolist (x (find-name-indices dd))
1358 (setf (elt vals (cdr x)) `',(car x)))
1359 (loop for dsd in (dd-slots dd) and val in values do
1360 (setf (elt vals (dsd-index dsd))
1361 (if (eq val '.do-not-initialize-slot.) 0 val)))
1362 `(defun ,cons-name ,arglist
1363 (declare ,@(mapcar (lambda (var type) `(type ,type ,var)) vars types))
1364 (list ,@vals))))
1365 (defun create-structure-constructor (dd cons-name arglist vars types values)
1366 ;; The difference between the two implementations here is that on all
1367 ;; platforms we don't have the appropriate RAW-INSTANCE-INIT VOPS, which
1368 ;; must be able to deal with immediate values as well -- unlike
1369 ;; RAW-INSTANCE-SET VOPs, which never end up seeing immediate values. With
1370 ;; some additional cleverness we might manage without them and just a single
1371 ;; implementation here, though -- figure out a way to ensure that on those
1372 ;; platforms we always still get a non-immediate TN in every case...
1374 ;; Until someone does that, this means that instances with raw slots can be
1375 ;; DX allocated only on platforms with those additional VOPs.
1376 #!+raw-instance-init-vops
1377 (let* ((slot-values nil)
1378 (slot-specs
1379 (mapcan (lambda (dsd value)
1380 (unless (eq value '.do-not-initialize-slot.)
1381 (push value slot-values)
1382 (list (list* :slot (dsd-raw-type dsd) (dsd-index dsd)))))
1383 (dd-slots dd)
1384 values)))
1385 `(defun ,cons-name ,arglist
1386 (declare ,@(mapcar (lambda (var type) `(type ,type ,var)) vars types))
1387 (%make-structure-instance-macro ,dd ',slot-specs ,@(reverse slot-values))))
1388 #!-raw-instance-init-vops
1389 (let ((instance (gensym "INSTANCE")) slot-values slot-specs raw-slots raw-values)
1390 (mapc (lambda (dsd value)
1391 (unless (eq value '.do-not-initialize-slot.)
1392 (let ((raw-type (dsd-raw-type dsd)))
1393 (cond ((eq t raw-type)
1394 (push value slot-values)
1395 (push (list* :slot raw-type (dsd-index dsd)) slot-specs))
1397 (push value raw-values)
1398 (push dsd raw-slots))))))
1399 (dd-slots dd)
1400 values)
1401 `(defun ,cons-name ,arglist
1402 (declare ,@(mapcar (lambda (var type) `(type ,type ,var)) vars types))
1403 ,(if raw-slots
1404 `(let ((,instance (%make-structure-instance-macro ,dd ',slot-specs ,@slot-values)))
1405 ,@(mapcar (lambda (dsd value)
1406 ;; (Note that we can't in general use the
1407 ;; ordinary named slot setter function here
1408 ;; because the slot might be :READ-ONLY, so we
1409 ;; whip up new LAMBDA representations of slot
1410 ;; setters for the occasion.)
1411 `(,(slot-setter-lambda-form dd dsd) ,value ,instance))
1412 raw-slots
1413 raw-values)
1414 ,instance)
1415 `(%make-structure-instance-macro ,dd ',slot-specs ,@slot-values)))))
1417 ;;; Create a default (non-BOA) keyword constructor.
1418 (defun create-keyword-constructor (defstruct creator)
1419 (declare (type function creator))
1420 (collect ((arglist (list '&key))
1421 (types)
1422 (vals))
1423 (dolist (slot (dd-slots defstruct))
1424 (let ((dum (gensym))
1425 (name (dsd-name slot)))
1426 (arglist `((,(keywordicate name) ,dum) ,(dsd-default slot)))
1427 (types (dsd-type slot))
1428 (vals dum)))
1429 (funcall creator
1430 defstruct (dd-default-constructor defstruct)
1431 (arglist) (vals) (types) (vals))))
1433 ;;; Given a structure and a BOA constructor spec, call CREATOR with
1434 ;;; the appropriate args to make a constructor.
1435 (defun create-boa-constructor (defstruct boa creator)
1436 (declare (type function creator))
1437 (multiple-value-bind (req opt restp rest keyp keys allowp auxp aux)
1438 (parse-lambda-list (second boa))
1439 (collect ((arglist)
1440 (vars)
1441 (types)
1442 (skipped-vars))
1443 (labels ((get-slot (name)
1444 (let ((res (find name (dd-slots defstruct)
1445 :test #'string=
1446 :key #'dsd-name)))
1447 (if res
1448 (values (dsd-type res) (dsd-default res))
1449 (values t nil))))
1450 (do-default (arg)
1451 (multiple-value-bind (type default) (get-slot arg)
1452 (arglist `(,arg ,default))
1453 (vars arg)
1454 (types type))))
1455 (dolist (arg req)
1456 (arglist arg)
1457 (vars arg)
1458 (types (get-slot arg)))
1460 (when opt
1461 (arglist '&optional)
1462 (dolist (arg opt)
1463 (cond ((consp arg)
1464 (destructuring-bind
1465 ;; FIXME: this shares some logic (though not
1466 ;; code) with the &key case below (and it
1467 ;; looks confusing) -- factor out the logic
1468 ;; if possible. - CSR, 2002-04-19
1469 (name
1470 &optional
1471 (def (nth-value 1 (get-slot name)))
1472 (supplied-test nil supplied-test-p))
1474 (arglist `(,name ,def ,@(if supplied-test-p `(,supplied-test) nil)))
1475 (vars name)
1476 (types (get-slot name))))
1478 (do-default arg)))))
1480 (when restp
1481 (arglist '&rest rest)
1482 (vars rest)
1483 (types 'list))
1485 (when keyp
1486 (arglist '&key)
1487 (dolist (key keys)
1488 (if (consp key)
1489 (destructuring-bind (wot
1490 &optional
1491 (def nil def-p)
1492 (supplied-test nil supplied-test-p))
1494 (let ((name (if (consp wot)
1495 (destructuring-bind (key var) wot
1496 (declare (ignore key))
1497 var)
1498 wot)))
1499 (multiple-value-bind (type slot-def)
1500 (get-slot name)
1501 (arglist `(,wot ,(if def-p def slot-def)
1502 ,@(if supplied-test-p `(,supplied-test) nil)))
1503 (vars name)
1504 (types type))))
1505 (do-default key))))
1507 (when allowp (arglist '&allow-other-keys))
1509 (when auxp
1510 (arglist '&aux)
1511 (dolist (arg aux)
1512 (arglist arg)
1513 (if (proper-list-of-length-p arg 2)
1514 (let ((var (first arg)))
1515 (vars var)
1516 (types (get-slot var)))
1517 (skipped-vars (if (consp arg) (first arg) arg))))))
1519 (funcall creator defstruct (first boa)
1520 (arglist) (vars) (types)
1521 (loop for slot in (dd-slots defstruct)
1522 for name = (dsd-name slot)
1523 collect (cond ((find name (skipped-vars) :test #'string=)
1524 ;; CLHS 3.4.6 Boa Lambda Lists
1525 (setf (dsd-safe-p slot) nil)
1526 '.do-not-initialize-slot.)
1527 ((or (find (dsd-name slot) (vars) :test #'string=)
1528 (let ((type (dsd-type slot)))
1529 (if (eq t type)
1530 (dsd-default slot)
1531 `(the ,type ,(dsd-default slot))))))))))))
1533 ;;; Grovel the constructor options, and decide what constructors (if
1534 ;;; any) to create.
1535 (defun constructor-definitions (defstruct)
1536 (let ((no-constructors nil)
1537 (boas ())
1538 (defaults ())
1539 (creator (ecase (dd-type defstruct)
1540 (structure #'create-structure-constructor)
1541 (vector #'create-vector-constructor)
1542 (list #'create-list-constructor))))
1543 (dolist (constructor (dd-constructors defstruct))
1544 (destructuring-bind (name &optional (boa-ll nil boa-p)) constructor
1545 (declare (ignore boa-ll))
1546 (cond ((not name) (setq no-constructors t))
1547 (boa-p (push constructor boas))
1548 (t (push name defaults)))))
1550 (when no-constructors
1551 (when (or defaults boas)
1552 (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs"))
1553 (return-from constructor-definitions ()))
1555 (unless (or defaults boas)
1556 (push (symbolicate "MAKE-" (dd-name defstruct)) defaults))
1558 (collect ((res) (names))
1559 (when defaults
1560 (let ((cname (first defaults)))
1561 (setf (dd-default-constructor defstruct) cname)
1562 (res (create-keyword-constructor defstruct creator))
1563 (names cname)
1564 (dolist (other-name (rest defaults))
1565 (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1566 (names other-name))))
1568 (dolist (boa boas)
1569 (res (create-boa-constructor defstruct boa creator))
1570 (names (first boa)))
1572 (res `(declaim (ftype
1573 (sfunction *
1574 ,(if (eq (dd-type defstruct) 'structure)
1575 (dd-name defstruct)
1576 '*))
1577 ,@(names))))
1579 (res))))
1581 ;;;; instances with ALTERNATE-METACLASS
1582 ;;;;
1583 ;;;; The CMU CL support for structures with ALTERNATE-METACLASS was a
1584 ;;;; fairly general extension embedded in the main DEFSTRUCT code, and
1585 ;;;; the result was an fairly impressive mess as ALTERNATE-METACLASS
1586 ;;;; extension mixed with ANSI CL generality (e.g. :TYPE and :INCLUDE)
1587 ;;;; and CMU CL implementation hairiness (esp. raw slots). This SBCL
1588 ;;;; version is much less ambitious, noticing that ALTERNATE-METACLASS
1589 ;;;; is only used to implement CONDITION, STANDARD-INSTANCE, and
1590 ;;;; GENERIC-FUNCTION, and defining a simple specialized
1591 ;;;; separate-from-DEFSTRUCT macro to provide only enough
1592 ;;;; functionality to support those.
1593 ;;;;
1594 ;;;; KLUDGE: The defining macro here is so specialized that it's ugly
1595 ;;;; in its own way. It also violates once-and-only-once by knowing
1596 ;;;; much about structures and layouts that is already known by the
1597 ;;;; main DEFSTRUCT macro. Hopefully it will go away presently
1598 ;;;; (perhaps when CL:CLASS and SB-PCL:CLASS meet) as per FIXME below.
1599 ;;;; -- WHN 2001-10-28
1600 ;;;;
1601 ;;;; FIXME: There seems to be no good reason to shoehorn CONDITION,
1602 ;;;; STANDARD-INSTANCE, and GENERIC-FUNCTION into mutated structures
1603 ;;;; instead of just implementing them as primitive objects. (This
1604 ;;;; reduced-functionality macro seems pretty close to the
1605 ;;;; functionality of DEFINE-PRIMITIVE-OBJECT..)
1607 (defun make-dd-with-alternate-metaclass (&key (class-name (missing-arg))
1608 (superclass-name (missing-arg))
1609 (metaclass-name (missing-arg))
1610 (dd-type (missing-arg))
1611 metaclass-constructor
1612 slot-names)
1613 (let* ((dd (make-defstruct-description class-name))
1614 (conc-name (concatenate 'string (symbol-name class-name) "-"))
1615 (dd-slots (let ((reversed-result nil)
1616 ;; The index starts at 1 for ordinary named
1617 ;; slots because slot 0 is magical, used for
1618 ;; the LAYOUT in CONDITIONs and
1619 ;; FUNCALLABLE-INSTANCEs. (This is the same
1620 ;; in ordinary structures too: see (INCF
1621 ;; DD-LENGTH) in
1622 ;; PARSE-DEFSTRUCT-NAME-AND-OPTIONS).
1623 (index 1))
1624 (dolist (slot-name slot-names)
1625 (push (make-defstruct-slot-description
1626 :name slot-name
1627 :index index
1628 :accessor-name (symbolicate conc-name slot-name))
1629 reversed-result)
1630 (incf index))
1631 (nreverse reversed-result))))
1632 (case dd-type
1633 ;; We don't support inheritance of alternate metaclass stuff,
1634 ;; and it's not a general-purpose facility, so sanity check our
1635 ;; own code.
1636 (structure
1637 (aver (eq superclass-name 't)))
1638 (funcallable-structure
1639 (aver (eq superclass-name 'function)))
1640 (t (bug "Unknown DD-TYPE in ALTERNATE-METACLASS: ~S" dd-type)))
1641 (setf (dd-alternate-metaclass dd) (list superclass-name
1642 metaclass-name
1643 metaclass-constructor)
1644 (dd-slots dd) dd-slots
1645 (dd-length dd) (1+ (length slot-names))
1646 (dd-type dd) dd-type)
1647 dd))
1649 ;;; make !DEFSTRUCT-WITH-ALTERNATE-METACLASS compilable by the host
1650 ;;; lisp, installing the information we need to reason about the
1651 ;;; structures (layouts and classoids).
1653 ;;; FIXME: we should share the parsing and the DD construction between
1654 ;;; this and the cross-compiler version, but my brain was too small to
1655 ;;; get that right. -- CSR, 2006-09-14
1656 #+sb-xc-host
1657 (defmacro !defstruct-with-alternate-metaclass
1658 (class-name &key
1659 (slot-names (missing-arg))
1660 (boa-constructor (missing-arg))
1661 (superclass-name (missing-arg))
1662 (metaclass-name (missing-arg))
1663 (metaclass-constructor (missing-arg))
1664 (dd-type (missing-arg))
1665 predicate
1666 (runtime-type-checks-p t))
1668 (declare (type (and list (not null)) slot-names))
1669 (declare (type (and symbol (not null))
1670 boa-constructor
1671 superclass-name
1672 metaclass-name
1673 metaclass-constructor))
1674 (declare (type symbol predicate))
1675 (declare (type (member structure funcallable-structure) dd-type))
1676 (declare (ignore boa-constructor predicate runtime-type-checks-p))
1678 (let* ((dd (make-dd-with-alternate-metaclass
1679 :class-name class-name
1680 :slot-names slot-names
1681 :superclass-name superclass-name
1682 :metaclass-name metaclass-name
1683 :metaclass-constructor metaclass-constructor
1684 :dd-type dd-type)))
1685 `(progn
1687 (eval-when (:compile-toplevel :load-toplevel :execute)
1688 (%compiler-set-up-layout ',dd ',(inherits-for-structure dd))))))
1690 (sb!xc:defmacro !defstruct-with-alternate-metaclass
1691 (class-name &key
1692 (slot-names (missing-arg))
1693 (boa-constructor (missing-arg))
1694 (superclass-name (missing-arg))
1695 (metaclass-name (missing-arg))
1696 (metaclass-constructor (missing-arg))
1697 (dd-type (missing-arg))
1698 predicate
1699 (runtime-type-checks-p t))
1701 (declare (type (and list (not null)) slot-names))
1702 (declare (type (and symbol (not null))
1703 boa-constructor
1704 superclass-name
1705 metaclass-name
1706 metaclass-constructor))
1707 (declare (type symbol predicate))
1708 (declare (type (member structure funcallable-structure) dd-type))
1710 (let* ((dd (make-dd-with-alternate-metaclass
1711 :class-name class-name
1712 :slot-names slot-names
1713 :superclass-name superclass-name
1714 :metaclass-name metaclass-name
1715 :metaclass-constructor metaclass-constructor
1716 :dd-type dd-type))
1717 (dd-slots (dd-slots dd))
1718 (dd-length (1+ (length slot-names)))
1719 (object-gensym (gensym "OBJECT"))
1720 (new-value-gensym (gensym "NEW-VALUE-"))
1721 (delayed-layout-form `(%delayed-get-compiler-layout ,class-name)))
1722 (multiple-value-bind (raw-maker-form raw-reffer-operator)
1723 (ecase dd-type
1724 (structure
1725 (values `(%make-structure-instance-macro ,dd nil)
1726 '%instance-ref))
1727 (funcallable-structure
1728 (values `(let ((,object-gensym
1729 (%make-funcallable-instance ,dd-length)))
1730 (setf (%funcallable-instance-layout ,object-gensym)
1731 ,delayed-layout-form)
1732 ,object-gensym)
1733 '%funcallable-instance-info)))
1734 `(progn
1736 (eval-when (:compile-toplevel :load-toplevel :execute)
1737 (%compiler-set-up-layout ',dd ',(inherits-for-structure dd)))
1739 ;; slot readers and writers
1740 (declaim (inline ,@(mapcar #'dsd-accessor-name dd-slots)))
1741 ,@(mapcar (lambda (dsd)
1742 `(defun ,(dsd-accessor-name dsd) (,object-gensym)
1743 ,@(when runtime-type-checks-p
1744 `((declare (type ,class-name ,object-gensym))))
1745 (,raw-reffer-operator ,object-gensym
1746 ,(dsd-index dsd))))
1747 dd-slots)
1748 (declaim (inline ,@(mapcar (lambda (dsd)
1749 `(setf ,(dsd-accessor-name dsd)))
1750 dd-slots)))
1751 ,@(mapcar (lambda (dsd)
1752 `(defun (setf ,(dsd-accessor-name dsd)) (,new-value-gensym
1753 ,object-gensym)
1754 ,@(when runtime-type-checks-p
1755 `((declare (type ,class-name ,object-gensym))))
1756 (setf (,raw-reffer-operator ,object-gensym
1757 ,(dsd-index dsd))
1758 ,new-value-gensym)))
1759 dd-slots)
1761 ;; constructor
1762 (defun ,boa-constructor ,slot-names
1763 (let ((,object-gensym ,raw-maker-form))
1764 ,@(mapcar (lambda (slot-name)
1765 (let ((dsd (find (symbol-name slot-name) dd-slots
1766 :key (lambda (x)
1767 (symbol-name (dsd-name x)))
1768 :test #'string=)))
1769 ;; KLUDGE: bug 117 bogowarning. Neither
1770 ;; DECLAREing the type nor TRULY-THE cut
1771 ;; the mustard -- it still gives warnings.
1772 (enforce-type dsd defstruct-slot-description)
1773 `(setf (,(dsd-accessor-name dsd) ,object-gensym)
1774 ,slot-name)))
1775 slot-names)
1776 ,object-gensym))
1778 ;; predicate
1779 ,@(when predicate
1780 ;; Just delegate to the compiler's type optimization
1781 ;; code, which knows how to generate inline type tests
1782 ;; for the whole CMU CL INSTANCE menagerie.
1783 `(defun ,predicate (,object-gensym)
1784 (typep ,object-gensym ',class-name)))))))
1786 ;;;; finalizing bootstrapping
1788 ;;; Set up DD and LAYOUT for STRUCTURE-OBJECT class itself.
1790 ;;; Ordinary structure classes effectively :INCLUDE STRUCTURE-OBJECT
1791 ;;; when they have no explicit :INCLUDEs, so (1) it needs to be set up
1792 ;;; before we can define ordinary structure classes, and (2) it's
1793 ;;; special enough (and simple enough) that we just build it by hand
1794 ;;; instead of trying to generalize the ordinary DEFSTRUCT code.
1795 (defun !set-up-structure-object-class ()
1796 (let ((dd (make-defstruct-description 'structure-object)))
1797 (setf
1798 ;; Note: This has an ALTERNATE-METACLASS only because of blind
1799 ;; clueless imitation of the CMU CL code -- dunno if or why it's
1800 ;; needed. -- WHN
1801 (dd-alternate-metaclass dd) '(t)
1802 (dd-slots dd) nil
1803 (dd-length dd) 1
1804 (dd-type dd) 'structure)
1805 (%compiler-set-up-layout dd)))
1806 (!set-up-structure-object-class)
1808 ;;; early structure predeclarations: Set up DD and LAYOUT for ordinary
1809 ;;; (non-ALTERNATE-METACLASS) structures which are needed early.
1810 (dolist (args
1811 '#.(sb-cold:read-from-file
1812 "src/code/early-defstruct-args.lisp-expr"))
1813 (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
1814 (first args)
1815 (rest args)))
1816 (inherits (inherits-for-structure dd)))
1817 (%compiler-defstruct dd inherits)))
1819 ;;; finding these beasts
1820 (defun find-defstruct-description (name &optional (errorp t))
1821 (let ((info (layout-info (classoid-layout (find-classoid name errorp)))))
1822 (if (defstruct-description-p info)
1823 info
1824 (when errorp
1825 (error "No DEFSTRUCT-DESCRIPTION for ~S." name)))))
1827 (/show0 "code/defstruct.lisp end of file")