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