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))
34 ;;; Normally IR2 converted, definition needed for interpreted structure
35 ;;; constructors only.
37 (defun %make-structure-instance (dd slot-specs &rest slot-values)
38 (let ((instance (%make-instance (dd-instance-length dd))))
39 (setf (%instance-layout instance) (dd-layout-or-lose dd))
40 (mapc (lambda (spec value)
41 (destructuring-bind (raw-type . index) (cdr spec)
42 (macrolet ((make-case ()
45 (setf (%instance-ref instance index) value))
48 `(,(raw-slot-data-raw-type rsd)
49 (setf (,(raw-slot-data-accessor-name rsd)
52 *raw-slot-data-list*))))
54 slot-specs slot-values)
59 (defun %raw-instance-ref/word (instance index)
60 (declare (type index index))
61 (%raw-instance-ref/word instance index))
62 (defun %raw-instance-set/word (instance index new-value)
63 (declare (type index index)
64 (type sb!vm:word new-value))
65 (%raw-instance-set/word instance index new-value))
67 (defun %raw-instance-ref/single (instance index)
68 (declare (type index index))
69 (%raw-instance-ref/single instance index))
70 (defun %raw-instance-set/single (instance index new-value)
71 (declare (type index index)
72 (type single-float new-value))
73 (%raw-instance-set/single instance index new-value))
75 (defun %raw-instance-ref/double (instance index)
76 (declare (type index index))
77 (%raw-instance-ref/double instance index))
78 (defun %raw-instance-set/double (instance index new-value)
79 (declare (type index index)
80 (type double-float new-value))
81 (%raw-instance-set/double instance index new-value))
83 (defun %raw-instance-ref/complex-single (instance index)
84 (declare (type index index))
85 (%raw-instance-ref/complex-single instance index))
86 (defun %raw-instance-set/complex-single (instance index new-value)
87 (declare (type index index)
88 (type (complex single-float) new-value))
89 (%raw-instance-set/complex-single instance index new-value))
91 (defun %raw-instance-ref/complex-double (instance index)
92 (declare (type index index))
93 (%raw-instance-ref/complex-double instance index))
94 (defun %raw-instance-set/complex-double (instance index new-value)
95 (declare (type index index)
96 (type (complex double-float) new-value))
97 (%raw-instance-set/complex-double instance index new-value))
102 (defun %raw-ref-single (vec index)
103 (declare (type index index))
104 (%raw-ref-single vec index))
106 (defun %raw-ref-double (vec index)
107 (declare (type index index))
108 (%raw-ref-double vec index))
111 (defun %raw-ref-long (vec index)
112 (declare (type index index))
113 (%raw-ref-long vec index))
115 (defun %raw-set-single (vec index val)
116 (declare (type index index))
117 (%raw-set-single vec index val))
119 (defun %raw-set-double (vec index val)
120 (declare (type index index))
121 (%raw-set-double vec index val))
124 (defun %raw-set-long (vec index val)
125 (declare (type index index))
126 (%raw-set-long vec index val))
128 (defun %raw-ref-complex-single (vec index)
129 (declare (type index index))
130 (%raw-ref-complex-single vec index))
132 (defun %raw-ref-complex-double (vec index)
133 (declare (type index index))
134 (%raw-ref-complex-double vec index))
137 (defun %raw-ref-complex-long (vec index)
138 (declare (type index index))
139 (%raw-ref-complex-long vec index))
141 (defun %raw-set-complex-single (vec index val)
142 (declare (type index index))
143 (%raw-set-complex-single vec index val))
145 (defun %raw-set-complex-double (vec index val)
146 (declare (type index index))
147 (%raw-set-complex-double vec index val))
150 (defun %raw-set-complex-long (vec index val)
151 (declare (type index index))
152 (%raw-set-complex-long vec index val))
155 (defun %instance-layout (instance)
156 (%instance-layout instance))
158 (defun %set-instance-layout (instance new-value)
159 (%set-instance-layout instance new-value))
161 (defun %make-funcallable-instance (len)
162 (%make-funcallable-instance len))
164 (defun funcallable-instance-p (x) (funcallable-instance-p x))
166 (defun %funcallable-instance-info (fin i)
167 (%funcallable-instance-info fin i))
169 (defun %set-funcallable-instance-info (fin i new-value)
170 (%set-funcallable-instance-info fin i new-value))
172 (defun funcallable-instance-fun (fin)
173 (%funcallable-instance-function fin))
175 (defun (setf funcallable-instance-fun) (new-value fin)
176 (setf (%funcallable-instance-function fin) new-value))
178 ;;;; target-only parts of the DEFSTRUCT top level code
180 ;;; A list of hooks designating functions of one argument, the
181 ;;; classoid, to be called when a defstruct is evaluated.
182 (defvar *defstruct-hooks* nil)
184 ;;; Catch attempts to mess up definitions of symbols in the CL package.
185 (defun protect-cl (symbol)
186 (/show0 "entering PROTECT-CL, SYMBOL=..")
188 (when (and *cold-init-complete-p*
189 (eq (symbol-package symbol) *cl-package*))
190 (cerror "Go ahead and patch the system."
191 "attempting to modify a symbol in the COMMON-LISP package: ~S"
193 (/show0 "leaving PROTECT-CL")
196 ;;; the part of %DEFSTRUCT which makes sense only on the target SBCL
198 ;;; (The "static" in the name is because it needs to be done not only
199 ;;; in ordinary toplevel %DEFSTRUCT, but also in cold init as early as
200 ;;; possible, to simulate static linking of structure functions as
201 ;;; nearly as possible.)
202 (defun %target-defstruct (dd layout)
203 (declare (type defstruct-description dd))
204 (declare (type layout layout))
206 (/show0 "entering %TARGET-DEFSTRUCT")
208 (remhash (dd-name dd) *typecheckfuns*)
210 ;; (Constructors aren't set up here, because constructors are
211 ;; varied enough (possibly parsing any specified argument list)
212 ;; that we can't reasonably implement them as closures, so we
213 ;; implement them with DEFUN instead.)
215 ;; Set FDEFINITIONs for slot accessors.
216 (dolist (dsd (dd-slots dd))
217 (/show0 "doing FDEFINITION for slot accessor")
218 (let ((accessor-name (dsd-accessor-name dsd)))
219 ;; We mustn't step on any inherited accessors
220 (unless (accessor-inherited-data accessor-name dd)
221 (/show0 "ACCESSOR-NAME=..")
222 (/hexstr accessor-name)
223 (protect-cl accessor-name)
224 (/hexstr "getting READER-FUN and WRITER-FUN")
225 (multiple-value-bind (reader-fun writer-fun)
226 (slot-accessor-funs dd dsd)
227 (declare (type function reader-fun writer-fun))
228 (/show0 "got READER-FUN and WRITER-FUN=..")
230 (setf (symbol-function accessor-name) reader-fun)
231 (unless (dsd-read-only dsd)
232 (/show0 "setting FDEFINITION for WRITER-FUN=..")
234 (setf (fdefinition `(setf ,accessor-name)) writer-fun))))))
236 ;; Set FDEFINITION for copier.
237 (when (dd-copier-name dd)
238 (/show0 "doing FDEFINITION for copier")
239 (protect-cl (dd-copier-name dd))
240 ;; We can't use COPY-STRUCTURE for other kinds of objects, notably
241 ;; funcallable structures, since it returns a STRUCTURE-OBJECT.
242 ;; (And funcallable instances don't need copiers anyway.)
243 (aver (eql (dd-type dd) 'structure))
244 (setf (symbol-function (dd-copier-name dd))
245 ;; FIXME: should use a closure which checks arg type before copying
248 ;; Set FDEFINITION for predicate.
249 (when (dd-predicate-name dd)
250 (/show0 "doing FDEFINITION for predicate")
251 (protect-cl (dd-predicate-name dd))
252 (setf (symbol-function (dd-predicate-name dd))
254 ;; structures with LAYOUTs
255 ((structure funcallable-structure)
256 (/show0 "with-LAYOUT case")
258 (locally ; <- to keep SAFETY 0 from affecting arg count checking
259 (declare (optimize (speed 3) (safety 0)))
260 (/noshow0 "in with-LAYOUT structure predicate closure, OBJECT,LAYOUT=..")
263 (typep-to-layout object layout))))
264 ;; structures with no LAYOUT (i.e. :TYPE VECTOR or :TYPE LIST)
266 ;; FIXME: should handle the :NAMED T case in these cases
268 (/show0 ":TYPE VECTOR case")
271 (/show0 ":TYPE LIST case")
275 (setf (fdocumentation (dd-name dd) 'structure)
278 ;; the BOUNDP test here is to get past cold-init.
279 (when (boundp '*defstruct-hooks*)
280 (dolist (fun *defstruct-hooks*)
281 (funcall fun (find-classoid (dd-name dd)))))
283 (/show0 "leaving %TARGET-DEFSTRUCT")
286 ;;;; generating out-of-line slot accessor functions
288 ;;; FIXME: Ideally, the presence of the type checks in the functions
289 ;;; here would be conditional on the optimization policy at the point
290 ;;; of expansion of DEFSTRUCT. (For now we're just doing the simpler
291 ;;; thing, putting in the type checks unconditionally.)
293 ;;; KLUDGE: Why use this closure approach at all? The macrology in
294 ;;; SLOT-ACCESSOR-FUNS seems to be half stub, half OAOOM to me. --DFL
296 ;;; Return (VALUES SLOT-READER-FUN SLOT-WRITER-FUN).
297 (defun slot-accessor-funs (dd dsd)
299 #+sb-xc (/show0 "entering SLOT-ACCESSOR-FUNS")
301 ;; various code generators
303 ;; Note: They're only minimally parameterized, and cavalierly grab
304 ;; things like INSTANCE and DSD-INDEX from the namespace they're
306 (macrolet (;; code shared between funcallable instance case and the
307 ;; ordinary STRUCTURE-OBJECT case: Handle native
308 ;; structures with LAYOUTs and (possibly) raw slots.
309 (%native-slot-accessor-funs (dd-ref-fun-name)
310 (let ((instance-type-check-form
311 '(%check-structure-type-from-layout instance layout)))
312 (/show "macroexpanding %NATIVE-SLOT-ACCESSOR-FUNS" dd-ref-fun-name instance-type-check-form)
313 `(let ((layout (dd-layout-or-lose dd))
314 (dsd-raw-type (dsd-raw-type dsd)))
315 #+sb-xc (/show0 "in %NATIVE-SLOT-ACCESSOR-FUNS macroexpanded code")
316 ;; Map over all the possible RAW-TYPEs, compiling
317 ;; a different closure function for each one, so
318 ;; that once the COND over RAW-TYPEs happens (at
319 ;; the time closure is allocated) there are no
320 ;; more decisions to be made and things execute
321 ;; reasonably efficiently.
324 ((eql dsd-raw-type t)
325 #+sb-xc (/show0 "in nonraw slot case")
326 (%slotplace-accessor-funs
327 (,dd-ref-fun-name instance dsd-index)
328 ,instance-type-check-form))
330 ,@(mapcar (lambda (rtd)
331 (let ((raw-type (raw-slot-data-raw-type rtd))
333 (raw-slot-data-accessor-name rtd)))
334 `((equal dsd-raw-type ',raw-type)
335 #+sb-xc (/show0 "in raw slot case")
336 (%slotplace-accessor-funs
337 (,accessor-name instance dsd-index)
338 ,instance-type-check-form))))
339 *raw-slot-data-list*)
342 (bug "unexpected DSD-RAW-TYPE ~S" dsd-raw-type))))))
343 ;; code shared between DEFSTRUCT :TYPE LIST and
344 ;; DEFSTRUCT :TYPE VECTOR cases: Handle the "typed
345 ;; structure" case, with no LAYOUTs and no raw slots.
346 (%colontyped-slot-accessor-funs () (error "stub"))
347 ;; the common structure of the raw-slot and not-raw-slot
348 ;; cases, defined in terms of the writable SLOTPLACE. All
349 ;; possible flavors of slot access should be able to pass
351 (%slotplace-accessor-funs (slotplace instance-type-check-form)
352 (/show "macroexpanding %SLOTPLACE-ACCESSOR-FUNS" slotplace instance-type-check-form)
353 `(let ((typecheckfun (typespec-typecheckfun dsd-type)))
354 (values (if (dsd-safe-p dsd)
356 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined reader")
357 ,instance-type-check-form
358 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
361 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined reader")
362 ,instance-type-check-form
363 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
364 (let ((value ,slotplace))
365 (funcall typecheckfun value)
367 (lambda (new-value instance)
368 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined writer")
369 ,instance-type-check-form
370 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
371 (funcall typecheckfun new-value)
372 (/noshow0 "back from TYPECHECKFUN")
373 (setf ,slotplace new-value))))))
375 (let ((dsd-index (dsd-index dsd))
376 (dsd-type (dsd-type dsd)))
378 #+sb-xc (/show0 "got DSD-TYPE=..")
379 #+sb-xc (/hexstr dsd-type)
384 #+sb-xc (/show0 "case of DSD-TYPE = STRUCTURE")
385 (%native-slot-accessor-funs %instance-ref))
387 ;; structures with the :TYPE option
389 ;; FIXME: Worry about these later..
391 ;; In :TYPE LIST and :TYPE VECTOR structures, ANSI specifies the
392 ;; layout completely, so that raw slots are impossible.
394 (dd-type-slot-accessor-funs nth-but-with-sane-arg-order
395 `(%check-structure-type-from-dd
398 (dd-type-slot-accessor-funs aref
403 ;;; Copy any old kind of structure.
404 (defun copy-structure (structure)
406 "Return a copy of STRUCTURE with the same (EQL) slot values."
407 (declare (type structure-object structure))
408 (let* ((len (%instance-length structure))
409 (res (%make-instance len))
410 (layout (%instance-layout structure))
411 (nuntagged (layout-n-untagged-slots layout)))
413 (declare (type index len))
414 (when (layout-invalid layout)
415 (error "attempt to copy an obsolete structure:~% ~S" structure))
417 ;; Copy ordinary slots.
418 (dotimes (i (- len nuntagged))
419 (declare (type index i))
420 (setf (%instance-ref res i)
421 (%instance-ref structure i)))
424 (dotimes (i nuntagged)
425 (declare (type index i))
426 (setf (%raw-instance-ref/word res i)
427 (%raw-instance-ref/word structure i)))
433 ;; Do an EQUALP comparison on the raw slots (only, not the normal slots) of a
435 (defun raw-instance-slots-equalp (layout x y)
436 ;; This implementation sucks, but hopefully EQUALP on raw structures
437 ;; won't be a major bottleneck for anyone. It'd be tempting to do
438 ;; all this with %RAW-INSTANCE-REF/WORD and bitwise comparisons, but
439 ;; that'll fail in some cases. For example -0.0 and 0.0 are EQUALP
440 ;; but have different bit patterns. -- JES, 2007-08-21
442 for dsd in (dd-slots (layout-info layout))
443 for raw-type = (dsd-raw-type dsd)
444 for rsd = (when raw-type
447 :key 'raw-slot-data-raw-type))
448 for accessor = (when rsd
449 (raw-slot-data-accessor-name rsd))
450 always (or (not accessor)
453 (equalp (funcall accessor x i)
454 (funcall accessor y i))))))
456 ;;; default PRINT-OBJECT method
458 (defun %print-structure-sans-layout-info (name stream)
459 ;; KLUDGE: during PCL build debugging, we can sometimes
460 ;; attempt to print out a PCL object (with null LAYOUT-INFO).
461 (pprint-logical-block (stream nil :prefix "#<" :suffix ">")
463 (write-char #\space stream)
464 (write-string "(no LAYOUT-INFO)" stream)))
466 (defun %print-structure-sans-slots (name stream)
467 ;; the structure type doesn't count as a component for *PRINT-LEVEL*
468 ;; processing. We can likewise elide the logical block processing,
469 ;; since all we have to print is the type name. -- CSR, 2004-10-05
470 (write-string "#S(" stream)
472 (write-char #\) stream))
474 (defun %default-structure-pretty-print (structure stream)
475 (let* ((layout (%instance-layout structure))
476 (name (classoid-name (layout-classoid layout)))
477 (dd (layout-info layout)))
479 (%print-structure-sans-layout-info name stream))
481 (%print-structure-sans-slots name stream))
483 (pprint-logical-block (stream nil :prefix "#S(" :suffix ")")
485 (let ((remaining-slots (dd-slots dd)))
486 (when remaining-slots
487 (write-char #\space stream)
488 ;; CMU CL had (PPRINT-INDENT :BLOCK 2 STREAM) here,
489 ;; but I can't see why. -- WHN 20000205
490 (pprint-newline :linear stream)
493 (let ((slot (pop remaining-slots)))
494 (write-char #\: stream)
495 (output-symbol-name (symbol-name (dsd-name slot)) stream)
496 (write-char #\space stream)
497 (pprint-newline :miser stream)
498 (output-object (funcall (fdefinition (dsd-accessor-name slot))
501 (when (null remaining-slots)
503 (write-char #\space stream)
504 (pprint-newline :linear stream))))))))))
506 (defun %default-structure-ugly-print (structure stream)
507 (let* ((layout (%instance-layout structure))
508 (name (classoid-name (layout-classoid layout)))
509 (dd (layout-info layout)))
511 (%print-structure-sans-layout-info name stream))
513 (%print-structure-sans-slots name stream))
515 (descend-into (stream)
516 (write-string "#S(" stream)
518 (do ((index 0 (1+ index))
519 (remaining-slots (dd-slots dd) (cdr remaining-slots)))
520 ((or (null remaining-slots)
521 (and (not *print-readably*)
523 (>= index *print-length*)))
524 (if (null remaining-slots)
525 (write-string ")" stream)
526 (write-string " ...)" stream)))
527 (declare (type index index))
528 (write-string " :" stream)
529 (let ((slot (first remaining-slots)))
530 (output-symbol-name (symbol-name (dsd-name slot)) stream)
531 (write-char #\space stream)
533 (funcall (fdefinition (dsd-accessor-name slot))
537 (defun default-structure-print (structure stream depth)
538 (declare (ignore depth))
539 (cond ((funcallable-instance-p structure)
540 (print-unreadable-object (structure stream :identity t :type t)))
542 (%default-structure-pretty-print structure stream))
544 (%default-structure-ugly-print structure stream))))
546 (def!method print-object ((x structure-object) stream)
547 (default-structure-print x stream *current-level-in-print*))
549 ;;;; testing structure types
551 ;;; Return true if OBJ is an object of the structure type
552 ;;; corresponding to LAYOUT. This is called by the accessor closures,
553 ;;; which have a handle on the type's LAYOUT.
555 ;;; FIXME: This is fairly big, so it should probably become
556 ;;; MAYBE-INLINE instead of INLINE, or its inlineness should become
557 ;;; conditional (probably through DEFTRANSFORM) on (> SPEED SPACE). Or
558 ;;; else we could fix things up so that the things which call it are
559 ;;; all closures, so that it's expanded only in a small number of
561 #!-sb-fluid (declaim (inline typep-to-layout))
562 (defun typep-to-layout (obj layout)
563 (declare (type layout layout) (optimize (speed 3) (safety 0)))
564 (/noshow0 "entering TYPEP-TO-LAYOUT, OBJ,LAYOUT=..")
567 (when (layout-invalid layout)
568 (error "An obsolete structure accessor function was called."))
569 (/noshow0 "back from testing LAYOUT-INVALID LAYOUT")
570 (and (%instancep obj)
571 (let ((obj-layout (%instance-layout obj)))
572 (cond ((eq obj-layout layout)
573 ;; (In this case OBJ-LAYOUT can't be invalid, because
574 ;; we determined LAYOUT is valid in the test above.)
577 ((layout-invalid obj-layout)
578 (/noshow0 "LAYOUT-INVALID case")
579 (error 'layout-invalid
580 :expected-type (layout-classoid obj-layout)
583 (let ((depthoid (layout-depthoid layout)))
584 (/noshow0 "DEPTHOID case, DEPTHOID,LAYOUT-INHERITS=..")
586 (/nohexstr layout-inherits)
587 (and (> (layout-depthoid obj-layout) depthoid)
588 (eq (svref (layout-inherits obj-layout) depthoid)
591 ;;;; checking structure types
593 ;;; Check that X is an instance of the named structure type.
594 (defmacro %check-structure-type-from-name (x name)
595 `(%check-structure-type-from-layout ,x ,(compiler-layout-or-lose name)))
597 ;;; Check that X is a structure of the type described by DD.
598 (defmacro %check-structure-type-from-dd (x dd)
599 (declare (type defstruct-description dd))
600 (let ((class-name (dd-name dd)))
602 ((structure funcallable-instance)
603 `(%check-structure-type-from-layout
605 ,(compiler-layout-or-lose class-name)))
607 (with-unique-names (xx)
609 (declare (type vector ,xx))
610 ,@(when (dd-named dd)
611 `((unless (eql (aref ,xx 0) ',class-name)
615 :expected-type `(member ,class-name)
617 "~@<missing name in instance of ~
618 VECTOR-typed structure ~S: ~2I~_S~:>"
619 :format-arguments (list ',class-name ,xx)))))
622 (with-unique-names (xx)
624 (declare (type list ,xx))
625 ,@(when (dd-named dd)
626 `((unless (eql (first ,xx) ',class-name)
630 :expected-type `(member ,class-name)
632 "~@<missing name in instance of LIST-typed structure ~S: ~
634 :format-arguments (list ',class-name ,xx)))))
637 ;;; Check that X is an instance of the structure class with layout LAYOUT.
638 (defun %check-structure-type-from-layout (x layout)
639 (unless (typep-to-layout x layout)
642 :expected-type (classoid-name (layout-classoid layout))))
646 (/show0 "target-defstruct.lisp end of file")