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