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