1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
10 (in-package "SB!KERNEL")
12 (/show0
"target-defstruct.lisp 12")
14 ;;;; structure frobbing primitives
16 ;;; Allocate a new instance with LENGTH data slots.
17 (defun %make-instance
(length)
18 (declare (type index length
))
19 (%make-instance length
))
21 ;;; Given an instance, return its length.
22 (defun %instance-length
(instance)
23 (declare (type instance instance
))
24 (%instance-length instance
))
26 ;;; Return the value from the INDEXth slot of INSTANCE. This is SETFable.
27 (defun %instance-ref
(instance index
)
28 (%instance-ref instance index
))
30 ;;; Set the INDEXth slot of INSTANCE to NEW-VALUE.
31 (defun %instance-set
(instance index new-value
)
32 (setf (%instance-ref instance index
) new-value
))
36 (defun %raw-instance-ref
/word
(instance index
)
37 (declare (type index index
))
38 (%raw-instance-ref
/word instance index
))
39 (defun %raw-instance-set
/word
(instance index new-value
)
40 (declare (type index index
)
41 (type sb
!vm
:word new-value
))
42 (%raw-instance-set
/word instance index new-value
))
44 (defun %raw-instance-ref
/single
(instance index
)
45 (declare (type index index
))
46 (%raw-instance-ref
/single instance index
))
47 (defun %raw-instance-set
/single
(instance index new-value
)
48 (declare (type index index
)
49 (type single-float new-value
))
50 (%raw-instance-set
/single instance index new-value
))
52 (defun %raw-instance-ref
/double
(instance index
)
53 (declare (type index index
))
54 (%raw-instance-ref
/double instance index
))
55 (defun %raw-instance-set
/double
(instance index new-value
)
56 (declare (type index index
)
57 (type double-float new-value
))
58 (%raw-instance-set
/double instance index new-value
))
60 (defun %raw-instance-ref
/complex-single
(instance index
)
61 (declare (type index index
))
62 (%raw-instance-ref
/complex-single instance index
))
63 (defun %raw-instance-set
/complex-single
(instance index new-value
)
64 (declare (type index index
)
65 (type (complex single-float
) new-value
))
66 (%raw-instance-set
/complex-single instance index new-value
))
68 (defun %raw-instance-ref
/complex-double
(instance index
)
69 (declare (type index index
))
70 (%raw-instance-ref
/complex-double instance index
))
71 (defun %raw-instance-set
/complex-double
(instance index new-value
)
72 (declare (type index index
)
73 (type (complex double-float
) new-value
))
74 (%raw-instance-set
/complex-double instance index new-value
))
79 (defun %raw-ref-single
(vec index
)
80 (declare (type index index
))
81 (%raw-ref-single vec index
))
83 (defun %raw-ref-double
(vec index
)
84 (declare (type index index
))
85 (%raw-ref-double vec index
))
88 (defun %raw-ref-long
(vec index
)
89 (declare (type index index
))
90 (%raw-ref-long vec index
))
92 (defun %raw-set-single
(vec index val
)
93 (declare (type index index
))
94 (%raw-set-single vec index val
))
96 (defun %raw-set-double
(vec index val
)
97 (declare (type index index
))
98 (%raw-set-double vec index val
))
101 (defun %raw-set-long
(vec index val
)
102 (declare (type index index
))
103 (%raw-set-long vec index val
))
105 (defun %raw-ref-complex-single
(vec index
)
106 (declare (type index index
))
107 (%raw-ref-complex-single vec index
))
109 (defun %raw-ref-complex-double
(vec index
)
110 (declare (type index index
))
111 (%raw-ref-complex-double vec index
))
114 (defun %raw-ref-complex-long
(vec index
)
115 (declare (type index index
))
116 (%raw-ref-complex-long vec index
))
118 (defun %raw-set-complex-single
(vec index val
)
119 (declare (type index index
))
120 (%raw-set-complex-single vec index val
))
122 (defun %raw-set-complex-double
(vec index val
)
123 (declare (type index index
))
124 (%raw-set-complex-double vec index val
))
127 (defun %raw-set-complex-long
(vec index val
)
128 (declare (type index index
))
129 (%raw-set-complex-long vec index val
))
132 (defun %instance-layout
(instance)
133 (%instance-layout instance
))
135 (defun %set-instance-layout
(instance new-value
)
136 (%set-instance-layout instance new-value
))
138 (defun %make-funcallable-instance
(len)
139 (%make-funcallable-instance len
))
141 (defun funcallable-instance-p (x) (funcallable-instance-p x
))
143 (defun %funcallable-instance-info
(fin i
)
144 (%funcallable-instance-info fin i
))
146 (defun %set-funcallable-instance-info
(fin i new-value
)
147 (%set-funcallable-instance-info fin i new-value
))
149 (defun funcallable-instance-fun (fin)
150 (%funcallable-instance-function fin
))
152 (defun (setf funcallable-instance-fun
) (new-value fin
)
153 (setf (%funcallable-instance-function fin
) new-value
))
155 ;;;; target-only parts of the DEFSTRUCT top level code
157 ;;; A list of hooks designating functions of one argument, the
158 ;;; classoid, to be called when a defstruct is evaluated.
159 (defvar *defstruct-hooks
* nil
)
161 ;;; Catch attempts to mess up definitions of symbols in the CL package.
162 (defun protect-cl (symbol)
163 (/show0
"entering PROTECT-CL, SYMBOL=..")
165 (when (and *cold-init-complete-p
*
166 (eq (symbol-package symbol
) *cl-package
*))
167 (cerror "Go ahead and patch the system."
168 "attempting to modify a symbol in the COMMON-LISP package: ~S"
170 (/show0
"leaving PROTECT-CL")
173 ;;; the part of %DEFSTRUCT which makes sense only on the target SBCL
175 ;;; (The "static" in the name is because it needs to be done not only
176 ;;; in ordinary toplevel %DEFSTRUCT, but also in cold init as early as
177 ;;; possible, to simulate static linking of structure functions as
178 ;;; nearly as possible.)
179 (defun %target-defstruct
(dd layout
)
180 (declare (type defstruct-description dd
))
181 (declare (type layout layout
))
183 (/show0
"entering %TARGET-DEFSTRUCT")
185 (remhash (dd-name dd
) *typecheckfuns
*)
187 ;; (Constructors aren't set up here, because constructors are
188 ;; varied enough (possibly parsing any specified argument list)
189 ;; that we can't reasonably implement them as closures, so we
190 ;; implement them with DEFUN instead.)
192 ;; Set FDEFINITIONs for slot accessors.
193 (dolist (dsd (dd-slots dd
))
194 (/show0
"doing FDEFINITION for slot accessor")
195 (let ((accessor-name (dsd-accessor-name dsd
)))
196 ;; We mustn't step on any inherited accessors
197 (unless (accessor-inherited-data accessor-name dd
)
198 (/show0
"ACCESSOR-NAME=..")
199 (/hexstr accessor-name
)
200 (protect-cl accessor-name
)
201 (/hexstr
"getting READER-FUN and WRITER-FUN")
202 (multiple-value-bind (reader-fun writer-fun
)
203 (slot-accessor-funs dd dsd
)
204 (declare (type function reader-fun writer-fun
))
205 (/show0
"got READER-FUN and WRITER-FUN=..")
207 (setf (symbol-function accessor-name
) reader-fun
)
208 (unless (dsd-read-only dsd
)
209 (/show0
"setting FDEFINITION for WRITER-FUN=..")
211 (setf (fdefinition `(setf ,accessor-name
)) writer-fun
))))))
213 ;; Set FDEFINITION for copier.
214 (when (dd-copier-name dd
)
215 (/show0
"doing FDEFINITION for copier")
216 (protect-cl (dd-copier-name dd
))
217 ;; We can't use COPY-STRUCTURE for other kinds of objects, notably
218 ;; funcallable structures, since it returns a STRUCTURE-OBJECT.
219 ;; (And funcallable instances don't need copiers anyway.)
220 (aver (eql (dd-type dd
) 'structure
))
221 (setf (symbol-function (dd-copier-name dd
))
222 ;; FIXME: should use a closure which checks arg type before copying
225 ;; Set FDEFINITION for predicate.
226 (when (dd-predicate-name dd
)
227 (/show0
"doing FDEFINITION for predicate")
228 (protect-cl (dd-predicate-name dd
))
229 (setf (symbol-function (dd-predicate-name dd
))
231 ;; structures with LAYOUTs
232 ((structure funcallable-structure
)
233 (/show0
"with-LAYOUT case")
235 (locally ; <- to keep SAFETY 0 from affecting arg count checking
236 (declare (optimize (speed 3) (safety 0)))
237 (/noshow0
"in with-LAYOUT structure predicate closure, OBJECT,LAYOUT=..")
240 (typep-to-layout object layout
))))
241 ;; structures with no LAYOUT (i.e. :TYPE VECTOR or :TYPE LIST)
243 ;; FIXME: should handle the :NAMED T case in these cases
245 (/show0
":TYPE VECTOR case")
248 (/show0
":TYPE LIST case")
252 (setf (fdocumentation (dd-name dd
) 'structure
)
255 ;; the BOUNDP test here is to get past cold-init.
256 (when (boundp '*defstruct-hooks
*)
257 (dolist (fun *defstruct-hooks
*)
258 (funcall fun
(find-classoid (dd-name dd
)))))
260 (/show0
"leaving %TARGET-DEFSTRUCT")
263 ;;;; generating out-of-line slot accessor functions
265 ;;; FIXME: Ideally, the presence of the type checks in the functions
266 ;;; here would be conditional on the optimization policy at the point
267 ;;; of expansion of DEFSTRUCT. (For now we're just doing the simpler
268 ;;; thing, putting in the type checks unconditionally.)
270 ;;; KLUDGE: Why use this closure approach at all? The macrology in
271 ;;; SLOT-ACCESSOR-FUNS seems to be half stub, half OAOOM to me. --DFL
273 ;;; Return (VALUES SLOT-READER-FUN SLOT-WRITER-FUN).
274 (defun slot-accessor-funs (dd dsd
)
276 #+sb-xc
(/show0
"entering SLOT-ACCESSOR-FUNS")
278 ;; various code generators
280 ;; Note: They're only minimally parameterized, and cavalierly grab
281 ;; things like INSTANCE and DSD-INDEX from the namespace they're
283 (macrolet (;; code shared between funcallable instance case and the
284 ;; ordinary STRUCTURE-OBJECT case: Handle native
285 ;; structures with LAYOUTs and (possibly) raw slots.
286 (%native-slot-accessor-funs
(dd-ref-fun-name)
287 (let ((instance-type-check-form
288 '(%check-structure-type-from-layout instance layout
)))
289 (/show
"macroexpanding %NATIVE-SLOT-ACCESSOR-FUNS" dd-ref-fun-name instance-type-check-form
)
290 `(let ((layout (dd-layout-or-lose dd
))
291 (dsd-raw-type (dsd-raw-type dsd
)))
292 #+sb-xc
(/show0
"in %NATIVE-SLOT-ACCESSOR-FUNS macroexpanded code")
293 ;; Map over all the possible RAW-TYPEs, compiling
294 ;; a different closure function for each one, so
295 ;; that once the COND over RAW-TYPEs happens (at
296 ;; the time closure is allocated) there are no
297 ;; more decisions to be made and things execute
298 ;; reasonably efficiently.
301 ((eql dsd-raw-type t
)
302 #+sb-xc
(/show0
"in nonraw slot case")
303 (%slotplace-accessor-funs
304 (,dd-ref-fun-name instance dsd-index
)
305 ,instance-type-check-form
))
307 ,@(mapcar (lambda (rtd)
308 (let ((raw-type (raw-slot-data-raw-type rtd
))
310 (raw-slot-data-accessor-name rtd
)))
311 `((equal dsd-raw-type
',raw-type
)
312 #+sb-xc
(/show0
"in raw slot case")
313 (%slotplace-accessor-funs
314 (,accessor-name instance dsd-index
)
315 ,instance-type-check-form
))))
316 *raw-slot-data-list
*)
319 (bug "unexpected DSD-RAW-TYPE ~S" dsd-raw-type
))))))
320 ;; code shared between DEFSTRUCT :TYPE LIST and
321 ;; DEFSTRUCT :TYPE VECTOR cases: Handle the "typed
322 ;; structure" case, with no LAYOUTs and no raw slots.
323 (%colontyped-slot-accessor-funs
() (error "stub"))
324 ;; the common structure of the raw-slot and not-raw-slot
325 ;; cases, defined in terms of the writable SLOTPLACE. All
326 ;; possible flavors of slot access should be able to pass
328 (%slotplace-accessor-funs
(slotplace instance-type-check-form
)
329 (/show
"macroexpanding %SLOTPLACE-ACCESSOR-FUNS" slotplace instance-type-check-form
)
330 `(let ((typecheckfun (typespec-typecheckfun dsd-type
)))
331 (values (if (dsd-safe-p dsd
)
333 (/noshow0
"in %SLOTPLACE-ACCESSOR-FUNS-defined reader")
334 ,instance-type-check-form
335 (/noshow0
"back from INSTANCE-TYPE-CHECK-FORM")
338 (/noshow0
"in %SLOTPLACE-ACCESSOR-FUNS-defined reader")
339 ,instance-type-check-form
340 (/noshow0
"back from INSTANCE-TYPE-CHECK-FORM")
341 (let ((value ,slotplace
))
342 (funcall typecheckfun value
)
344 (lambda (new-value instance
)
345 (/noshow0
"in %SLOTPLACE-ACCESSOR-FUNS-defined writer")
346 ,instance-type-check-form
347 (/noshow0
"back from INSTANCE-TYPE-CHECK-FORM")
348 (funcall typecheckfun new-value
)
349 (/noshow0
"back from TYPECHECKFUN")
350 (setf ,slotplace new-value
))))))
352 (let ((dsd-index (dsd-index dsd
))
353 (dsd-type (dsd-type dsd
)))
355 #+sb-xc
(/show0
"got DSD-TYPE=..")
356 #+sb-xc
(/hexstr dsd-type
)
361 #+sb-xc
(/show0
"case of DSD-TYPE = STRUCTURE")
362 (%native-slot-accessor-funs %instance-ref
))
364 ;; structures with the :TYPE option
366 ;; FIXME: Worry about these later..
368 ;; In :TYPE LIST and :TYPE VECTOR structures, ANSI specifies the
369 ;; layout completely, so that raw slots are impossible.
371 (dd-type-slot-accessor-funs nth-but-with-sane-arg-order
372 `(%check-structure-type-from-dd
375 (dd-type-slot-accessor-funs aref
380 ;;; Copy any old kind of structure.
381 (defun copy-structure (structure)
383 "Return a copy of STRUCTURE with the same (EQL) slot values."
384 (declare (type structure-object structure
))
385 (let* ((len (%instance-length structure
))
386 (res (%make-instance len
))
387 (layout (%instance-layout structure
))
388 (nuntagged (layout-n-untagged-slots layout
)))
390 (declare (type index len
))
391 (when (layout-invalid layout
)
392 (error "attempt to copy an obsolete structure:~% ~S" structure
))
394 ;; Copy ordinary slots.
395 (dotimes (i (- len nuntagged
))
396 (declare (type index i
))
397 (setf (%instance-ref res i
)
398 (%instance-ref structure i
)))
401 (dotimes (i nuntagged
)
402 (declare (type index i
))
403 (setf (%raw-instance-ref
/word res i
)
404 (%raw-instance-ref
/word structure i
)))
410 ;; Do an EQUALP comparison on the raw slots (only, not the normal slots) of a
412 (defun raw-instance-slots-equalp (layout x y
)
413 ;; This implementation sucks, but hopefully EQUALP on raw structures
414 ;; won't be a major bottleneck for anyone. It'd be tempting to do
415 ;; all this with %RAW-INSTANCE-REF/WORD and bitwise comparisons, but
416 ;; that'll fail in some cases. For example -0.0 and 0.0 are EQUALP
417 ;; but have different bit patterns. -- JES, 2007-08-21
419 for dsd in
(dd-slots (layout-info layout
))
420 for raw-type
= (dsd-raw-type dsd
)
421 for rsd
= (when raw-type
424 :key
'raw-slot-data-raw-type
))
425 for accessor
= (when rsd
426 (raw-slot-data-accessor-name rsd
))
427 always
(or (not accessor
)
430 (equalp (funcall accessor x i
)
431 (funcall accessor y i
))))))
433 ;;; default PRINT-OBJECT method
435 (defun %print-structure-sans-layout-info
(name stream
)
436 ;; KLUDGE: during PCL build debugging, we can sometimes
437 ;; attempt to print out a PCL object (with null LAYOUT-INFO).
438 (pprint-logical-block (stream nil
:prefix
"#<" :suffix
">")
440 (write-char #\space stream
)
441 (write-string "(no LAYOUT-INFO)" stream
)))
443 (defun %print-structure-sans-slots
(name stream
)
444 ;; the structure type doesn't count as a component for *PRINT-LEVEL*
445 ;; processing. We can likewise elide the logical block processing,
446 ;; since all we have to print is the type name. -- CSR, 2004-10-05
447 (write-string "#S(" stream
)
449 (write-char #\
) stream
))
451 (defun %default-structure-pretty-print
(structure stream
)
452 (let* ((layout (%instance-layout structure
))
453 (name (classoid-name (layout-classoid layout
)))
454 (dd (layout-info layout
)))
456 (%print-structure-sans-layout-info name stream
))
458 (%print-structure-sans-slots name stream
))
460 (pprint-logical-block (stream nil
:prefix
"#S(" :suffix
")")
462 (let ((remaining-slots (dd-slots dd
)))
463 (when remaining-slots
464 (write-char #\space stream
)
465 ;; CMU CL had (PPRINT-INDENT :BLOCK 2 STREAM) here,
466 ;; but I can't see why. -- WHN 20000205
467 (pprint-newline :linear stream
)
470 (let ((slot (pop remaining-slots
)))
471 (write-char #\
: stream
)
472 (output-symbol-name (symbol-name (dsd-name slot
)) stream
)
473 (write-char #\space stream
)
474 (pprint-newline :miser stream
)
475 (output-object (funcall (fdefinition (dsd-accessor-name slot
))
478 (when (null remaining-slots
)
480 (write-char #\space stream
)
481 (pprint-newline :linear stream
))))))))))
483 (defun %default-structure-ugly-print
(structure stream
)
484 (let* ((layout (%instance-layout structure
))
485 (name (classoid-name (layout-classoid layout
)))
486 (dd (layout-info layout
)))
488 (%print-structure-sans-layout-info name stream
))
490 (%print-structure-sans-slots name stream
))
492 (descend-into (stream)
493 (write-string "#S(" stream
)
495 (do ((index 0 (1+ index
))
496 (remaining-slots (dd-slots dd
) (cdr remaining-slots
)))
497 ((or (null remaining-slots
)
498 (and (not *print-readably
*)
500 (>= index
*print-length
*)))
501 (if (null remaining-slots
)
502 (write-string ")" stream
)
503 (write-string " ...)" stream
)))
504 (declare (type index index
))
505 (write-string " :" stream
)
506 (let ((slot (first remaining-slots
)))
507 (output-symbol-name (symbol-name (dsd-name slot
)) stream
)
508 (write-char #\space stream
)
510 (funcall (fdefinition (dsd-accessor-name slot
))
514 (defun default-structure-print (structure stream depth
)
515 (declare (ignore depth
))
516 (cond ((funcallable-instance-p structure
)
517 (print-unreadable-object (structure stream
:identity t
:type t
)))
519 (%default-structure-pretty-print structure stream
))
521 (%default-structure-ugly-print structure stream
))))
523 (def!method print-object
((x structure-object
) stream
)
524 (default-structure-print x stream
*current-level-in-print
*))
526 ;;;; testing structure types
528 ;;; Return true if OBJ is an object of the structure type
529 ;;; corresponding to LAYOUT. This is called by the accessor closures,
530 ;;; which have a handle on the type's LAYOUT.
532 ;;; FIXME: This is fairly big, so it should probably become
533 ;;; MAYBE-INLINE instead of INLINE, or its inlineness should become
534 ;;; conditional (probably through DEFTRANSFORM) on (> SPEED SPACE). Or
535 ;;; else we could fix things up so that the things which call it are
536 ;;; all closures, so that it's expanded only in a small number of
538 #!-sb-fluid
(declaim (inline typep-to-layout
))
539 (defun typep-to-layout (obj layout
)
540 (declare (type layout layout
) (optimize (speed 3) (safety 0)))
541 (/noshow0
"entering TYPEP-TO-LAYOUT, OBJ,LAYOUT=..")
544 (when (layout-invalid layout
)
545 (error "An obsolete structure accessor function was called."))
546 (/noshow0
"back from testing LAYOUT-INVALID LAYOUT")
547 (and (%instancep obj
)
548 (let ((obj-layout (%instance-layout obj
)))
549 (cond ((eq obj-layout layout
)
550 ;; (In this case OBJ-LAYOUT can't be invalid, because
551 ;; we determined LAYOUT is valid in the test above.)
554 ((layout-invalid obj-layout
)
555 (/noshow0
"LAYOUT-INVALID case")
556 (error 'layout-invalid
557 :expected-type
(layout-classoid obj-layout
)
560 (let ((depthoid (layout-depthoid layout
)))
561 (/noshow0
"DEPTHOID case, DEPTHOID,LAYOUT-INHERITS=..")
563 (/nohexstr layout-inherits
)
564 (and (> (layout-depthoid obj-layout
) depthoid
)
565 (eq (svref (layout-inherits obj-layout
) depthoid
)
568 ;;;; checking structure types
570 ;;; Check that X is an instance of the named structure type.
571 (defmacro %check-structure-type-from-name
(x name
)
572 `(%check-structure-type-from-layout
,x
,(compiler-layout-or-lose name
)))
574 ;;; Check that X is a structure of the type described by DD.
575 (defmacro %check-structure-type-from-dd
(x dd
)
576 (declare (type defstruct-description dd
))
577 (let ((class-name (dd-name dd
)))
579 ((structure funcallable-instance
)
580 `(%check-structure-type-from-layout
582 ,(compiler-layout-or-lose class-name
)))
584 (with-unique-names (xx)
586 (declare (type vector
,xx
))
587 ,@(when (dd-named dd
)
588 `((unless (eql (aref ,xx
0) ',class-name
)
592 :expected-type
`(member ,class-name
)
594 "~@<missing name in instance of ~
595 VECTOR-typed structure ~S: ~2I~_S~:>"
596 :format-arguments
(list ',class-name
,xx
)))))
599 (with-unique-names (xx)
601 (declare (type list
,xx
))
602 ,@(when (dd-named dd
)
603 `((unless (eql (first ,xx
) ',class-name
)
607 :expected-type
`(member ,class-name
)
609 "~@<missing name in instance of LIST-typed structure ~S: ~
611 :format-arguments
(list ',class-name
,xx
)))))
614 ;;; Check that X is an instance of the structure class with layout LAYOUT.
615 (defun %check-structure-type-from-layout
(x layout
)
616 (unless (typep-to-layout x layout
)
619 :expected-type
(classoid-name (layout-classoid layout
))))
623 (/show0
"target-defstruct.lisp end of file")