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