0.9.4.27:
[sbcl.git] / src / code / target-defstruct.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
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.
9
10 (in-package "SB!KERNEL")
11
12 (/show0 "target-defstruct.lisp 12")
13 \f
14 ;;;; structure frobbing primitives
15
16 ;;; Allocate a new instance with LENGTH data slots.
17 (defun %make-instance (length)
18   (declare (type index length))
19   (%make-instance length))
20
21 ;;; Given an instance, return its length.
22 (defun %instance-length (instance)
23   (declare (type instance instance))
24   (%instance-length instance))
25
26 ;;; Return the value from the INDEXth slot of INSTANCE. This is SETFable.
27 (defun %instance-ref (instance index)
28   (%instance-ref instance index))
29
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))
33
34 #!-hppa
35 (progn
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))
43
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))
51
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))
59
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))
67
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)))
75
76 (defun %raw-ref-single (vec index)
77   (declare (type index index))
78   (%raw-ref-single vec index))
79
80 (defun %raw-ref-double (vec index)
81   (declare (type index index))
82   (%raw-ref-double vec index))
83
84 #!+long-float
85 (defun %raw-ref-long (vec index)
86   (declare (type index index))
87   (%raw-ref-long vec index))
88
89 (defun %raw-set-single (vec index val)
90   (declare (type index index))
91   (%raw-set-single vec index val))
92
93 (defun %raw-set-double (vec index val)
94   (declare (type index index))
95   (%raw-set-double vec index val))
96
97 #!+long-float
98 (defun %raw-set-long (vec index val)
99   (declare (type index index))
100   (%raw-set-long vec index val))
101
102 (defun %raw-ref-complex-single (vec index)
103   (declare (type index index))
104   (%raw-ref-complex-single vec index))
105
106 (defun %raw-ref-complex-double (vec index)
107   (declare (type index index))
108   (%raw-ref-complex-double vec index))
109
110 #!+long-float
111 (defun %raw-ref-complex-long (vec index)
112   (declare (type index index))
113   (%raw-ref-complex-long vec index))
114
115 (defun %raw-set-complex-single (vec index val)
116   (declare (type index index))
117   (%raw-set-complex-single vec index val))
118
119 (defun %raw-set-complex-double (vec index val)
120   (declare (type index index))
121   (%raw-set-complex-double vec index val))
122
123 #!+long-float
124 (defun %raw-set-complex-long (vec index val)
125   (declare (type index index))
126   (%raw-set-complex-long vec index val))
127
128 (defun %instance-layout (instance)
129   (%instance-layout instance))
130
131 (defun %set-instance-layout (instance new-value)
132   (%set-instance-layout instance new-value))
133
134 (defun %make-funcallable-instance (len)
135   (%make-funcallable-instance len))
136
137 (defun funcallable-instance-p (x) (funcallable-instance-p x))
138
139 (defun %funcallable-instance-info (fin i)
140   (%funcallable-instance-info fin i))
141
142 (defun %set-funcallable-instance-info (fin i new-value)
143   (%set-funcallable-instance-info fin i new-value))
144
145 (defun funcallable-instance-fun (fin)
146   (%funcallable-instance-lexenv fin))
147
148 ;;; The heart of the magic of funcallable instances ("FINs").  When
149 ;;; called (as with any other function), we grab the code pointer, and
150 ;;; call it, leaving the original function object in LEXENV (in case
151 ;;; it was a closure).  If it is actually a FIN, then we need to do an
152 ;;; extra indirection with funcallable-instance-lexenv to get at any
153 ;;; closure environment.  This extra indirection is set up when
154 ;;; accessing the closure environment of an INSTANCE-LAMBDA. Note that
155 ;;; the original FIN pointer is lost, so if the called function wants
156 ;;; to get at the original object to do some slot accesses, it must
157 ;;; close over the FIN object.
158 ;;;
159 ;;; If we set the FIN function to be a FIN, we directly copy across
160 ;;; both the code pointer and the lexenv, since that code pointer (for
161 ;;; an instance-lambda) is expecting that lexenv to be accessed.  This
162 ;;; effectively pre-flattens what would otherwise be a chain of
163 ;;; indirections.  (That used to happen when PCL dispatch functions
164 ;;; were byte-compiled; now that the byte compiler is gone, I can't
165 ;;; think of another example offhand. -- WHN 2001-10-06)
166 ;;;
167 ;;; The only loss is that if someone accesses the
168 ;;; FUNCALLABLE-INSTANCE-FUN, then won't get a FIN back.  This
169 ;;; probably doesn't matter, since PCL only sets the FIN function.
170 (defun (setf funcallable-instance-fun) (new-value fin)
171   (setf (%funcallable-instance-fun fin)
172         (%closure-fun new-value))
173   (setf (%funcallable-instance-lexenv fin)
174         (if (funcallable-instance-p new-value)
175             (%funcallable-instance-lexenv new-value)
176             new-value)))
177
178 ;;; service function for structure constructors
179 (defun %make-instance-with-layout (layout)
180   ;; Make sure the object ends at a two-word boundary.  Note that this does
181   ;; not affect the amount of memory used, since the allocator would add the
182   ;; same padding anyway.  However, raw slots are indexed from the length of
183   ;; the object as indicated in the header, so the pad word needs to be
184   ;; included in that length to guarantee proper alignment of raw double float
185   ;; slots, necessary for (at least) the SPARC backend.
186   (let* ((length (layout-length layout))
187          (result (%make-instance (+ length (mod (1+ length) 2)))))
188     (setf (%instance-layout result) layout)
189     result))
190 \f
191 ;;;; target-only parts of the DEFSTRUCT top level code
192
193 ;;; A list of hooks designating functions of one argument, the
194 ;;; classoid, to be called when a defstruct is evaluated.
195 (defvar *defstruct-hooks* nil)
196
197 ;;; Catch attempts to mess up definitions of symbols in the CL package.
198 (defun protect-cl (symbol)
199   (/show0 "entering PROTECT-CL, SYMBOL=..")
200   (/hexstr symbol)
201   (when (and *cold-init-complete-p*
202              (eq (symbol-package symbol) *cl-package*))
203     (cerror "Go ahead and patch the system."
204             "attempting to modify a symbol in the COMMON-LISP package: ~S"
205             symbol))
206   (/show0 "leaving PROTECT-CL")
207   (values))
208
209 ;;; the part of %DEFSTRUCT which makes sense only on the target SBCL
210 ;;;
211 ;;; (The "static" in the name is because it needs to be done not only
212 ;;; in ordinary toplevel %DEFSTRUCT, but also in cold init as early as
213 ;;; possible, to simulate static linking of structure functions as
214 ;;; nearly as possible.)
215 (defun %target-defstruct (dd layout)
216   (declare (type defstruct-description dd))
217   (declare (type layout layout))
218
219   (/show0 "entering %TARGET-DEFSTRUCT")
220
221   (remhash (dd-name dd) *typecheckfuns*)
222
223   ;; (Constructors aren't set up here, because constructors are
224   ;; varied enough (possibly parsing any specified argument list)
225   ;; that we can't reasonably implement them as closures, so we
226   ;; implement them with DEFUN instead.)
227
228   ;; Set FDEFINITIONs for slot accessors.
229   (dolist (dsd (dd-slots dd))
230     (/show0 "doing FDEFINITION for slot accessor")
231     (let ((accessor-name (dsd-accessor-name dsd)))
232       ;; We mustn't step on any inherited accessors
233       (unless (accessor-inherited-data accessor-name dd)
234         (/show0 "ACCESSOR-NAME=..")
235         (/hexstr accessor-name)
236         (protect-cl accessor-name)
237         (/hexstr "getting READER-FUN and WRITER-FUN")
238         (multiple-value-bind (reader-fun writer-fun)
239             (slot-accessor-funs dd dsd)
240           (declare (type function reader-fun writer-fun))
241           (/show0 "got READER-FUN and WRITER-FUN=..")
242           (/hexstr reader-fun)
243           (setf (symbol-function accessor-name) reader-fun)
244           (unless (dsd-read-only dsd)
245             (/show0 "setting FDEFINITION for WRITER-FUN=..")
246             (/hexstr writer-fun)
247             (setf (fdefinition `(setf ,accessor-name)) writer-fun))))))
248
249   ;; Set FDEFINITION for copier.
250   (when (dd-copier-name dd)
251     (/show0 "doing FDEFINITION for copier")
252     (protect-cl (dd-copier-name dd))
253     ;; We can't use COPY-STRUCTURE for other kinds of objects, notably
254     ;; funcallable structures, since it returns a STRUCTURE-OBJECT.
255     ;; (And funcallable instances don't need copiers anyway.)
256     (aver (eql (dd-type dd) 'structure))
257     (setf (symbol-function (dd-copier-name dd))
258           ;; FIXME: should use a closure which checks arg type before copying
259           #'copy-structure))
260
261   ;; Set FDEFINITION for predicate.
262   (when (dd-predicate-name dd)
263     (/show0 "doing FDEFINITION for predicate")
264     (protect-cl (dd-predicate-name dd))
265     (setf (symbol-function (dd-predicate-name dd))
266           (ecase (dd-type dd)
267             ;; structures with LAYOUTs
268             ((structure funcallable-structure)
269              (/show0 "with-LAYOUT case")
270              (lambda (object)
271                (locally ; <- to keep SAFETY 0 from affecting arg count checking
272                  (declare (optimize (speed 3) (safety 0)))
273                  (/noshow0 "in with-LAYOUT structure predicate closure, OBJECT,LAYOUT=..")
274                  (/nohexstr object)
275                  (/nohexstr layout)
276                  (typep-to-layout object layout))))
277             ;; structures with no LAYOUT (i.e. :TYPE VECTOR or :TYPE LIST)
278             ;;
279             ;; FIXME: should handle the :NAMED T case in these cases
280             (vector
281              (/show0 ":TYPE VECTOR case")
282              #'vectorp)
283             (list
284              (/show0 ":TYPE LIST case")
285              #'listp))))
286
287   (when (dd-doc dd)
288     (setf (fdocumentation (dd-name dd) 'structure)
289           (dd-doc dd)))
290
291   ;; the BOUNDP test here is to get past cold-init.
292   (when (boundp '*defstruct-hooks*)
293     (dolist (fun *defstruct-hooks*)
294       (funcall fun (find-classoid (dd-name dd)))))
295
296   (/show0 "leaving %TARGET-DEFSTRUCT")
297   (values))
298 \f
299 ;;;; generating out-of-line slot accessor functions
300
301 ;;; FIXME: Ideally, the presence of the type checks in the functions
302 ;;; here would be conditional on the optimization policy at the point
303 ;;; of expansion of DEFSTRUCT. (For now we're just doing the simpler
304 ;;; thing, putting in the type checks unconditionally.)
305
306 ;;; KLUDGE: Why use this closure approach at all?  The macrology in
307 ;;; SLOT-ACCESSOR-FUNS seems to be half stub, half OAOOM to me.  --DFL
308
309 ;;; Return (VALUES SLOT-READER-FUN SLOT-WRITER-FUN).
310 (defun slot-accessor-funs (dd dsd)
311
312   #+sb-xc (/show0 "entering SLOT-ACCESSOR-FUNS")
313
314   ;; various code generators
315   ;;
316   ;; Note: They're only minimally parameterized, and cavalierly grab
317   ;; things like INSTANCE and DSD-INDEX from the namespace they're
318   ;; expanded in.
319   (macrolet (;; code shared between funcallable instance case and the
320              ;; ordinary STRUCTURE-OBJECT case: Handle native
321              ;; structures with LAYOUTs and (possibly) raw slots.
322              (%native-slot-accessor-funs (dd-ref-fun-name)
323                (let ((instance-type-check-form
324                       '(%check-structure-type-from-layout instance layout)))
325                  (/show "macroexpanding %NATIVE-SLOT-ACCESSOR-FUNS" dd-ref-fun-name instance-type-check-form)
326                  `(let ((layout (dd-layout-or-lose dd))
327                         (dsd-raw-type (dsd-raw-type dsd)))
328                     #+sb-xc (/show0 "in %NATIVE-SLOT-ACCESSOR-FUNS macroexpanded code")
329                     ;; Map over all the possible RAW-TYPEs, compiling
330                     ;; a different closure function for each one, so
331                     ;; that once the COND over RAW-TYPEs happens (at
332                     ;; the time closure is allocated) there are no
333                     ;; more decisions to be made and things execute
334                     ;; reasonably efficiently.
335                     (cond
336                      ;; nonraw slot case
337                      ((eql dsd-raw-type t)
338                       #+sb-xc (/show0 "in nonraw slot case")
339                       (%slotplace-accessor-funs
340                        (,dd-ref-fun-name instance dsd-index)
341                        ,instance-type-check-form))
342                      ;; raw slot cases
343                      ,@(mapcar (lambda (rtd)
344                                  (let ((raw-type (raw-slot-data-raw-type rtd))
345                                        (accessor-name
346                                         (raw-slot-data-accessor-name rtd)))
347                                    `((equal dsd-raw-type ',raw-type)
348                                      #+sb-xc (/show0 "in raw slot case")
349                                      (%slotplace-accessor-funs
350                                       (,accessor-name instance dsd-index)
351                                       ,instance-type-check-form))))
352                                *raw-slot-data-list*)
353                      ;; oops
354                      (t
355                       (bug "unexpected DSD-RAW-TYPE ~S" dsd-raw-type))))))
356              ;; code shared between DEFSTRUCT :TYPE LIST and
357              ;; DEFSTRUCT :TYPE VECTOR cases: Handle the "typed
358              ;; structure" case, with no LAYOUTs and no raw slots.
359              (%colontyped-slot-accessor-funs () (error "stub"))
360              ;; the common structure of the raw-slot and not-raw-slot
361              ;; cases, defined in terms of the writable SLOTPLACE. All
362              ;; possible flavors of slot access should be able to pass
363              ;; through here.
364              (%slotplace-accessor-funs (slotplace instance-type-check-form)
365                (/show "macroexpanding %SLOTPLACE-ACCESSOR-FUNS" slotplace instance-type-check-form)
366                `(let ((typecheckfun (typespec-typecheckfun dsd-type)))
367                   (values (if (dsd-safe-p dsd)
368                               (lambda (instance)
369                                 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined reader")
370                                 ,instance-type-check-form
371                                 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
372                                 ,slotplace)
373                               (lambda (instance)
374                                 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined reader")
375                                 ,instance-type-check-form
376                                 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
377                                 (let ((value ,slotplace))
378                                   (funcall typecheckfun value)
379                                   value)))
380                           (lambda (new-value instance)
381                             (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined writer")
382                             ,instance-type-check-form
383                             (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
384                             (funcall typecheckfun new-value)
385                             (/noshow0 "back from TYPECHECKFUN")
386                             (setf ,slotplace new-value))))))
387
388     (let ((dsd-index (dsd-index dsd))
389           (dsd-type (dsd-type dsd)))
390
391       #+sb-xc (/show0 "got DSD-TYPE=..")
392       #+sb-xc (/hexstr dsd-type)
393       (ecase (dd-type dd)
394
395         ;; native structures
396         (structure
397          #+sb-xc (/show0 "case of DSD-TYPE = STRUCTURE")
398          (%native-slot-accessor-funs %instance-ref))
399
400         ;; structures with the :TYPE option
401
402         ;; FIXME: Worry about these later..
403         #|
404         ;; In :TYPE LIST and :TYPE VECTOR structures, ANSI specifies the
405         ;; layout completely, so that raw slots are impossible.
406         (list
407          (dd-type-slot-accessor-funs nth-but-with-sane-arg-order
408                                  `(%check-structure-type-from-dd
409                                  :maybe-raw-p nil))
410         (vector
411          (dd-type-slot-accessor-funs aref
412                                  :maybe-raw-p nil)))
413         |#
414         ))))
415 \f
416 ;;; Copy any old kind of structure.
417 (defun copy-structure (structure)
418   #!+sb-doc
419   "Return a copy of STRUCTURE with the same (EQL) slot values."
420   (declare (type structure-object structure))
421   (let* ((len (%instance-length structure))
422          (res (%make-instance len))
423          (layout (%instance-layout structure))
424          (nuntagged (layout-n-untagged-slots layout)))
425
426     (declare (type index len))
427     (when (layout-invalid layout)
428       (error "attempt to copy an obsolete structure:~%  ~S" structure))
429
430     ;; Copy ordinary slots.
431     (dotimes (i (- len nuntagged))
432       (declare (type index i))
433       (setf (%instance-ref res i)
434             (%instance-ref structure i)))
435
436     ;; Copy raw slots.
437     (dotimes (i nuntagged)
438       (declare (type index i))
439       (setf (%raw-instance-ref/word res i)
440             (%raw-instance-ref/word structure i)))
441
442     res))
443 \f
444 ;;; default PRINT-OBJECT method
445
446 (defun %default-structure-pretty-print (structure stream)
447   (let* ((layout (%instance-layout structure))
448          (name (classoid-name (layout-classoid layout)))
449          (dd (layout-info layout)))
450     ;; KLUDGE: during the build process with SB-SHOW, we can sometimes
451     ;; attempt to print out a PCL object (with null LAYOUT-INFO).
452     #!+sb-show
453     (when (null dd)
454       (pprint-logical-block (stream nil :prefix "#<" :suffix ">")
455         (prin1 name stream)
456         (write-char #\space stream)
457         (write-string "(no LAYOUT-INFO)"))
458       (return-from %default-structure-pretty-print nil))
459     ;; the structure type doesn't count as a component for
460     ;; *PRINT-LEVEL* processing.  We can likewise elide the logical
461     ;; block processing, since all we have to print is the type name.
462     ;; -- CSR, 2004-10-05
463     (when (and dd (null (dd-slots dd)))
464       (write-string "#S(" stream)
465       (prin1 name stream)
466       (write-char #\) stream)
467       (return-from %default-structure-pretty-print nil))
468     (pprint-logical-block (stream nil :prefix "#S(" :suffix ")")
469       (prin1 name stream)
470       (let ((remaining-slots (dd-slots dd)))
471         (when remaining-slots
472           (write-char #\space stream)
473           ;; CMU CL had (PPRINT-INDENT :BLOCK 2 STREAM) here,
474           ;; but I can't see why. -- WHN 20000205
475           (pprint-newline :linear stream)
476           (loop
477            (pprint-pop)
478            (let ((slot (pop remaining-slots)))
479              (write-char #\: stream)
480              (output-symbol-name (symbol-name (dsd-name slot)) stream)
481              (write-char #\space stream)
482              (pprint-newline :miser stream)
483              (output-object (funcall (fdefinition (dsd-accessor-name slot))
484                                      structure)
485                             stream)
486              (when (null remaining-slots)
487                (return))
488              (write-char #\space stream)
489              (pprint-newline :linear stream))))))))
490 (defun %default-structure-ugly-print (structure stream)
491   (let* ((layout (%instance-layout structure))
492          (name (classoid-name (layout-classoid layout)))
493          (dd (layout-info layout)))
494     (when (and dd (null (dd-slots dd)))
495       (write-string "#S(" stream)
496       (prin1 name stream)
497       (write-char #\) stream)
498       (return-from %default-structure-ugly-print nil))
499     (descend-into (stream)
500       (write-string "#S(" stream)
501       (prin1 name stream)
502       (do ((index 0 (1+ index))
503            (remaining-slots (dd-slots dd) (cdr remaining-slots)))
504           ((or (null remaining-slots)
505                (and (not *print-readably*)
506                     *print-length*
507                     (>= index *print-length*)))
508            (if (null remaining-slots)
509                (write-string ")" stream)
510                (write-string " ...)" stream)))
511         (declare (type index index))
512         (write-char #\space stream)
513         (write-char #\: stream)
514         (let ((slot (first remaining-slots)))
515           (output-symbol-name (symbol-name (dsd-name slot)) stream)
516           (write-char #\space stream)
517           (output-object
518            (funcall (fdefinition (dsd-accessor-name slot))
519                     structure)
520            stream))))))
521 (defun default-structure-print (structure stream depth)
522   (declare (ignore depth))
523   (cond ((funcallable-instance-p structure)
524          (print-unreadable-object (structure stream :identity t :type t)))
525         (*print-pretty*
526          (%default-structure-pretty-print structure stream))
527         (t
528          (%default-structure-ugly-print structure stream))))
529 (def!method print-object ((x structure-object) stream)
530   (default-structure-print x stream *current-level-in-print*))
531 \f
532 ;;;; testing structure types
533
534 ;;; Return true if OBJ is an object of the structure type
535 ;;; corresponding to LAYOUT. This is called by the accessor closures,
536 ;;; which have a handle on the type's LAYOUT.
537 ;;;
538 ;;; FIXME: This is fairly big, so it should probably become
539 ;;; MAYBE-INLINE instead of INLINE, or its inlineness should become
540 ;;; conditional (probably through DEFTRANSFORM) on (> SPEED SPACE). Or
541 ;;; else we could fix things up so that the things which call it are
542 ;;; all closures, so that it's expanded only in a small number of
543 ;;; places.
544 #!-sb-fluid (declaim (inline typep-to-layout))
545 (defun typep-to-layout (obj layout)
546   (declare (type layout layout) (optimize (speed 3) (safety 0)))
547   (/noshow0 "entering TYPEP-TO-LAYOUT, OBJ,LAYOUT=..")
548   (/nohexstr obj)
549   (/nohexstr layout)
550   (when (layout-invalid layout)
551     (error "An obsolete structure accessor function was called."))
552   (/noshow0 "back from testing LAYOUT-INVALID LAYOUT")
553   ;; FIXME: CMU CL used (%INSTANCEP OBJ) here. Check that
554   ;; (TYPEP OBJ 'INSTANCE) is optimized to equally efficient code.
555   (and (typep obj 'instance)
556        (let ((obj-layout (%instance-layout obj)))
557          (cond ((eq obj-layout layout)
558                 ;; (In this case OBJ-LAYOUT can't be invalid, because
559                 ;; we determined LAYOUT is valid in the test above.)
560                 (/noshow0 "EQ case")
561                 t)
562                ((layout-invalid obj-layout)
563                 (/noshow0 "LAYOUT-INVALID case")
564                 (error 'layout-invalid
565                        :expected-type (layout-classoid obj-layout)
566                        :datum obj))
567                (t
568                 (let ((depthoid (layout-depthoid layout)))
569                   (/noshow0 "DEPTHOID case, DEPTHOID,LAYOUT-INHERITS=..")
570                   (/nohexstr depthoid)
571                   (/nohexstr layout-inherits)
572                   (and (> (layout-depthoid obj-layout) depthoid)
573                        (eq (svref (layout-inherits obj-layout) depthoid)
574                            layout))))))))
575 \f
576 ;;;; checking structure types
577
578 ;;; Check that X is an instance of the named structure type.
579 (defmacro %check-structure-type-from-name (x name)
580   `(%check-structure-type-from-layout ,x ,(compiler-layout-or-lose name)))
581
582 ;;; Check that X is a structure of the type described by DD.
583 (defmacro %check-structure-type-from-dd (x dd)
584   (declare (type defstruct-description dd))
585   (let ((class-name (dd-name dd)))
586     (ecase (dd-type dd)
587       ((structure funcallable-instance)
588        `(%check-structure-type-from-layout
589          ,x
590          ,(compiler-layout-or-lose class-name)))
591       ((vector)
592        (with-unique-names (xx)
593          `(let ((,xx ,x))
594             (declare (type vector ,xx))
595             ,@(when (dd-named dd)
596                 `((unless (eql (aref ,xx 0) ',class-name)
597                     (error
598                      'simple-type-error
599                      :datum (aref ,xx 0)
600                      :expected-type `(member ,class-name)
601                      :format-control
602                      "~@<missing name in instance of ~
603                       VECTOR-typed structure ~S: ~2I~_S~:>"
604                      :format-arguments (list ',class-name ,xx)))))
605             (values))))
606       ((list)
607        (with-unique-names (xx)
608          `(let ((,xx ,x))
609             (declare (type list ,xx))
610             ,@(when (dd-named dd)
611                 `((unless (eql (first ,xx) ',class-name)
612                     (error
613                      'simple-type-error
614                      :datum (aref ,xx 0)
615                      :expected-type `(member ,class-name)
616                      :format-control
617                      "~@<missing name in instance of LIST-typed structure ~S: ~
618                       ~2I~_S~:>"
619                      :format-arguments (list ',class-name ,xx)))))
620             (values)))))))
621
622 ;;; Check that X is an instance of the structure class with layout LAYOUT.
623 (defun %check-structure-type-from-layout (x layout)
624   (unless (typep-to-layout x layout)
625     (error 'type-error
626            :datum x
627            :expected-type (classoid-name (layout-classoid layout))))
628   (values))
629 \f
630 (/show0 "target-defstruct.lisp end of file")