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