1.0.17.19: fix interpreted structure constructors (regression since 1.0.17.4)
[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 ;;; Normally IR2 converted, definition needed for interpreted structure
35 ;;; constructors only.
36 #!+sb-eval
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 ()
43                            `(ecase raw-type
44                               ((t)
45                                (setf (%instance-ref instance index) value))
46                               ,@(mapcar
47                                  (lambda (rsd)
48                                    `(,(raw-slot-data-raw-type rsd)
49                                       (setf (,(raw-slot-data-accessor-name rsd)
50                                               instance index)
51                                             value)))
52                                  *raw-slot-data-list*))))
53                 (make-case))))
54           slot-specs slot-values)
55     instance))
56
57 #!-hppa
58 (progn
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))
66
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))
74
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))
82
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))
90
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))
98 ) ; #!-HPPA
99
100 #!+hppa
101 (progn
102 (defun %raw-ref-single (vec index)
103   (declare (type index index))
104   (%raw-ref-single vec index))
105
106 (defun %raw-ref-double (vec index)
107   (declare (type index index))
108   (%raw-ref-double vec index))
109
110 #!+long-float
111 (defun %raw-ref-long (vec index)
112   (declare (type index index))
113   (%raw-ref-long vec index))
114
115 (defun %raw-set-single (vec index val)
116   (declare (type index index))
117   (%raw-set-single vec index val))
118
119 (defun %raw-set-double (vec index val)
120   (declare (type index index))
121   (%raw-set-double vec index val))
122
123 #!+long-float
124 (defun %raw-set-long (vec index val)
125   (declare (type index index))
126   (%raw-set-long vec index val))
127
128 (defun %raw-ref-complex-single (vec index)
129   (declare (type index index))
130   (%raw-ref-complex-single vec index))
131
132 (defun %raw-ref-complex-double (vec index)
133   (declare (type index index))
134   (%raw-ref-complex-double vec index))
135
136 #!+long-float
137 (defun %raw-ref-complex-long (vec index)
138   (declare (type index index))
139   (%raw-ref-complex-long vec index))
140
141 (defun %raw-set-complex-single (vec index val)
142   (declare (type index index))
143   (%raw-set-complex-single vec index val))
144
145 (defun %raw-set-complex-double (vec index val)
146   (declare (type index index))
147   (%raw-set-complex-double vec index val))
148
149 #!+long-float
150 (defun %raw-set-complex-long (vec index val)
151   (declare (type index index))
152   (%raw-set-complex-long vec index val))
153 ) ; #!+HPPA
154
155 (defun %instance-layout (instance)
156   (%instance-layout instance))
157
158 (defun %set-instance-layout (instance new-value)
159   (%set-instance-layout instance new-value))
160
161 (defun %make-funcallable-instance (len)
162   (%make-funcallable-instance len))
163
164 (defun funcallable-instance-p (x) (funcallable-instance-p x))
165
166 (defun %funcallable-instance-info (fin i)
167   (%funcallable-instance-info fin i))
168
169 (defun %set-funcallable-instance-info (fin i new-value)
170   (%set-funcallable-instance-info fin i new-value))
171
172 (defun funcallable-instance-fun (fin)
173   (%funcallable-instance-function fin))
174
175 (defun (setf funcallable-instance-fun) (new-value fin)
176   (setf (%funcallable-instance-function fin) new-value))
177 \f
178 ;;;; target-only parts of the DEFSTRUCT top level code
179
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)
183
184 ;;; Catch attempts to mess up definitions of symbols in the CL package.
185 (defun protect-cl (symbol)
186   (/show0 "entering PROTECT-CL, SYMBOL=..")
187   (/hexstr 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"
192             symbol))
193   (/show0 "leaving PROTECT-CL")
194   (values))
195
196 ;;; the part of %DEFSTRUCT which makes sense only on the target SBCL
197 ;;;
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))
205
206   (/show0 "entering %TARGET-DEFSTRUCT")
207
208   (remhash (dd-name dd) *typecheckfuns*)
209
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.)
214
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=..")
229           (/hexstr reader-fun)
230           (setf (symbol-function accessor-name) reader-fun)
231           (unless (dsd-read-only dsd)
232             (/show0 "setting FDEFINITION for WRITER-FUN=..")
233             (/hexstr writer-fun)
234             (setf (fdefinition `(setf ,accessor-name)) writer-fun))))))
235
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
246           #'copy-structure))
247
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))
253           (ecase (dd-type dd)
254             ;; structures with LAYOUTs
255             ((structure funcallable-structure)
256              (/show0 "with-LAYOUT case")
257              (lambda (object)
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=..")
261                  (/nohexstr object)
262                  (/nohexstr layout)
263                  (typep-to-layout object layout))))
264             ;; structures with no LAYOUT (i.e. :TYPE VECTOR or :TYPE LIST)
265             ;;
266             ;; FIXME: should handle the :NAMED T case in these cases
267             (vector
268              (/show0 ":TYPE VECTOR case")
269              #'vectorp)
270             (list
271              (/show0 ":TYPE LIST case")
272              #'listp))))
273
274   (when (dd-doc dd)
275     (setf (fdocumentation (dd-name dd) 'structure)
276           (dd-doc dd)))
277
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)))))
282
283   (/show0 "leaving %TARGET-DEFSTRUCT")
284   (values))
285 \f
286 ;;;; generating out-of-line slot accessor functions
287
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.)
292
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
295
296 ;;; Return (VALUES SLOT-READER-FUN SLOT-WRITER-FUN).
297 (defun slot-accessor-funs (dd dsd)
298
299   #+sb-xc (/show0 "entering SLOT-ACCESSOR-FUNS")
300
301   ;; various code generators
302   ;;
303   ;; Note: They're only minimally parameterized, and cavalierly grab
304   ;; things like INSTANCE and DSD-INDEX from the namespace they're
305   ;; expanded in.
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.
322                     (cond
323                      ;; nonraw slot case
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))
329                      ;; raw slot cases
330                      ,@(mapcar (lambda (rtd)
331                                  (let ((raw-type (raw-slot-data-raw-type rtd))
332                                        (accessor-name
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*)
340                      ;; oops
341                      (t
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
350              ;; through here.
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)
355                               (lambda (instance)
356                                 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined reader")
357                                 ,instance-type-check-form
358                                 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
359                                 ,slotplace)
360                               (lambda (instance)
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)
366                                   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))))))
374
375     (let ((dsd-index (dsd-index dsd))
376           (dsd-type (dsd-type dsd)))
377
378       #+sb-xc (/show0 "got DSD-TYPE=..")
379       #+sb-xc (/hexstr dsd-type)
380       (ecase (dd-type dd)
381
382         ;; native structures
383         (structure
384          #+sb-xc (/show0 "case of DSD-TYPE = STRUCTURE")
385          (%native-slot-accessor-funs %instance-ref))
386
387         ;; structures with the :TYPE option
388
389         ;; FIXME: Worry about these later..
390         #|
391         ;; In :TYPE LIST and :TYPE VECTOR structures, ANSI specifies the
392         ;; layout completely, so that raw slots are impossible.
393         (list
394          (dd-type-slot-accessor-funs nth-but-with-sane-arg-order
395                                  `(%check-structure-type-from-dd
396                                  :maybe-raw-p nil))
397         (vector
398          (dd-type-slot-accessor-funs aref
399                                  :maybe-raw-p nil)))
400         |#
401         ))))
402 \f
403 ;;; Copy any old kind of structure.
404 (defun copy-structure (structure)
405   #!+sb-doc
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)))
412
413     (declare (type index len))
414     (when (layout-invalid layout)
415       (error "attempt to copy an obsolete structure:~%  ~S" structure))
416
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)))
422
423     ;; Copy raw slots.
424     (dotimes (i nuntagged)
425       (declare (type index i))
426       (setf (%raw-instance-ref/word res i)
427             (%raw-instance-ref/word structure i)))
428
429     res))
430
431 \f
432
433 ;; Do an EQUALP comparison on the raw slots (only, not the normal slots) of a
434 ;; structure.
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
441   (loop with i = -1
442         for dsd in (dd-slots (layout-info layout))
443         for raw-type = (dsd-raw-type dsd)
444         for rsd = (when raw-type
445                     (find raw-type
446                           *raw-slot-data-list*
447                           :key 'raw-slot-data-raw-type))
448         for accessor = (when rsd
449                          (raw-slot-data-accessor-name rsd))
450         always (or (not accessor)
451                    (progn
452                      (incf i)
453                      (equalp (funcall accessor x i)
454                              (funcall accessor y i))))))
455 \f
456 ;;; default PRINT-OBJECT method
457
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 ">")
462     (prin1 name stream)
463     (write-char #\space stream)
464     (write-string "(no LAYOUT-INFO)" stream)))
465
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)
471   (prin1 name stream)
472   (write-char #\) stream))
473
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)))
478     (cond ((not dd)
479            (%print-structure-sans-layout-info name stream))
480           ((not (dd-slots dd))
481            (%print-structure-sans-slots name stream))
482           (t
483            (pprint-logical-block (stream nil :prefix "#S(" :suffix ")")
484              (prin1 name stream)
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)
491                  (loop
492                    (pprint-pop)
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))
499                                              structure)
500                                     stream)
501                      (when (null remaining-slots)
502                        (return))
503                      (write-char #\space stream)
504                      (pprint-newline :linear stream))))))))))
505
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)))
510     (cond ((not dd)
511            (%print-structure-sans-layout-info name stream))
512           ((not (dd-slots dd))
513            (%print-structure-sans-slots name stream))
514           (t
515            (descend-into (stream)
516              (write-string "#S(" stream)
517              (prin1 name 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*)
522                            *print-length*
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)
532                  (output-object
533                   (funcall (fdefinition (dsd-accessor-name slot))
534                            structure)
535                   stream))))))))
536
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)))
541         (*print-pretty*
542          (%default-structure-pretty-print structure stream))
543         (t
544          (%default-structure-ugly-print structure stream))))
545
546 (def!method print-object ((x structure-object) stream)
547   (default-structure-print x stream *current-level-in-print*))
548 \f
549 ;;;; testing structure types
550
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.
554 ;;;
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
560 ;;; places.
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=..")
565   (/nohexstr obj)
566   (/nohexstr 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.)
575                 (/noshow0 "EQ case")
576                 t)
577                ((layout-invalid obj-layout)
578                 (/noshow0 "LAYOUT-INVALID case")
579                 (error 'layout-invalid
580                        :expected-type (layout-classoid obj-layout)
581                        :datum obj))
582                (t
583                 (let ((depthoid (layout-depthoid layout)))
584                   (/noshow0 "DEPTHOID case, DEPTHOID,LAYOUT-INHERITS=..")
585                   (/nohexstr depthoid)
586                   (/nohexstr layout-inherits)
587                   (and (> (layout-depthoid obj-layout) depthoid)
588                        (eq (svref (layout-inherits obj-layout) depthoid)
589                            layout))))))))
590 \f
591 ;;;; checking structure types
592
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)))
596
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)))
601     (ecase (dd-type dd)
602       ((structure funcallable-instance)
603        `(%check-structure-type-from-layout
604          ,x
605          ,(compiler-layout-or-lose class-name)))
606       ((vector)
607        (with-unique-names (xx)
608          `(let ((,xx ,x))
609             (declare (type vector ,xx))
610             ,@(when (dd-named dd)
611                 `((unless (eql (aref ,xx 0) ',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 ~
618                       VECTOR-typed structure ~S: ~2I~_S~:>"
619                      :format-arguments (list ',class-name ,xx)))))
620             (values))))
621       ((list)
622        (with-unique-names (xx)
623          `(let ((,xx ,x))
624             (declare (type list ,xx))
625             ,@(when (dd-named dd)
626                 `((unless (eql (first ,xx) ',class-name)
627                     (error
628                      'simple-type-error
629                      :datum (aref ,xx 0)
630                      :expected-type `(member ,class-name)
631                      :format-control
632                      "~@<missing name in instance of LIST-typed structure ~S: ~
633                       ~2I~_S~:>"
634                      :format-arguments (list ',class-name ,xx)))))
635             (values)))))))
636
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)
640     (error 'type-error
641            :datum x
642            :expected-type (classoid-name (layout-classoid layout))))
643   (values))
644
645 \f
646 (/show0 "target-defstruct.lisp end of file")