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