0.pre7.65:
[sbcl.git] / src / code / defstruct.lisp
1 ;;;; that part of DEFSTRUCT implementation which is needed not just 
2 ;;;; in the target Lisp but also in the cross-compilation host
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!KERNEL")
14
15 (/show0 "code/defstruct.lisp 15")
16 \f
17 ;;;; getting LAYOUTs
18
19 ;;; Return the compiler layout for NAME. (The class referred to by
20 ;;; NAME must be a structure-like class.)
21 (defun compiler-layout-or-lose (name)
22   (let ((res (info :type :compiler-layout name)))
23     (cond ((not res)
24            (error "Class is not yet defined or was undefined: ~S" name))
25           ((not (typep (layout-info res) 'defstruct-description))
26            (error "Class is not a structure class: ~S" name))
27           (t res))))
28
29 ;;; Delay looking for compiler-layout until the constructor is being
30 ;;; compiled, since it doesn't exist until after the eval-when
31 ;;; (compile) is compiled.
32 (sb!xc:defmacro %delayed-get-compiler-layout (name)
33   `',(compiler-layout-or-lose name))
34
35 ;;; Get layout right away.
36 (sb!xc:defmacro compile-time-find-layout (name)
37   (find-layout name))
38
39 ;;; re. %DELAYED-GET-COMPILER-LAYOUT and COMPILE-TIME-FIND-LAYOUT, above..
40 ;;;
41 ;;; FIXME: Perhaps both should be defined with DEFMACRO-MUNDANELY?
42 ;;; FIXME: Do we really need both? If so, their names and implementations
43 ;;; should probably be tweaked to be more parallel.
44 \f
45 ;;; The DEFSTRUCT-DESCRIPTION structure holds compile-time information about a
46 ;;; structure type.
47 (def!struct (defstruct-description
48              (:conc-name dd-)
49              (:make-load-form-fun just-dump-it-normally)
50              #-sb-xc-host (:pure t)
51              (:constructor make-defstruct-description (name)))
52   ;; name of the structure
53   (name (required-argument) :type symbol)
54   ;; documentation on the structure
55   (doc nil :type (or string null))
56   ;; prefix for slot names. If NIL, none.
57   (conc-name (symbolicate name "-") :type (or symbol null))
58   ;; the name of the primary standard keyword constructor, or NIL if none
59   (default-constructor nil :type (or symbol null))
60   ;; all the explicit :CONSTRUCTOR specs, with name defaulted
61   (constructors () :type list)
62   ;; name of copying function
63   (copier (symbolicate "COPY-" name) :type (or symbol null))
64   ;; name of type predicate
65   (predicate-name (symbolicate name "-P") :type (or symbol null))
66   ;; the arguments to the :INCLUDE option, or NIL if no included
67   ;; structure
68   (include nil :type list)
69   ;; The arguments to the :ALTERNATE-METACLASS option (an extension
70   ;; used to define structure-like objects with an arbitrary
71   ;; superclass and that may not have STRUCTURE-CLASS as the
72   ;; metaclass.) Syntax is:
73   ;;    (superclass-name metaclass-name metaclass-constructor)
74   (alternate-metaclass nil :type list)
75   ;; a list of DEFSTRUCT-SLOT-DESCRIPTION objects for all slots
76   ;; (including included ones)
77   (slots () :type list)
78   ;; number of elements we've allocated (See also RAW-LENGTH.)
79   (length 0 :type index)
80   ;; General kind of implementation.
81   (type 'structure :type (member structure vector list
82                                  funcallable-structure))
83
84   ;; The next three slots are for :TYPE'd structures (which aren't
85   ;; classes, DD-CLASS-P = NIL)
86   ;;
87   ;; vector element type
88   (element-type t)
89   ;; T if :NAMED was explicitly specified, NIL otherwise
90   (named nil :type boolean)
91   ;; any INITIAL-OFFSET option on this direct type
92   (offset nil :type (or index null))
93
94   ;; the argument to the PRINT-FUNCTION option, or NIL if a
95   ;; PRINT-FUNCTION option was given with no argument, or 0 if no
96   ;; PRINT-FUNCTION option was given
97   (print-function 0 :type (or cons symbol (member 0)))
98   ;; the argument to the PRINT-OBJECT option, or NIL if a PRINT-OBJECT
99   ;; option was given with no argument, or 0 if no PRINT-OBJECT option
100   ;; was given
101   (print-object 0 :type (or cons symbol (member 0)))
102   ;; the index of the raw data vector and the number of words in it,
103   ;; or NIL and 0 if not allocated (either because this structure
104   ;; has no raw slots, or because we're still parsing it and haven't
105   ;; run across any raw slots yet)
106   (raw-index nil :type (or index null))
107   (raw-length 0 :type index)
108   ;; the value of the :PURE option, or :UNSPECIFIED. This is only
109   ;; meaningful if DD-CLASS-P = T.
110   (pure :unspecified :type (member t nil :substructure :unspecified)))
111 (def!method print-object ((x defstruct-description) stream)
112   (print-unreadable-object (x stream :type t)
113     (prin1 (dd-name x) stream)))
114
115 ;;; A DEFSTRUCT-SLOT-DESCRIPTION holds compile-time information about
116 ;;; a structure slot.
117 (def!struct (defstruct-slot-description
118              (:make-load-form-fun just-dump-it-normally)
119              (:conc-name dsd-)
120              (:copier nil)
121              #-sb-xc-host (:pure t))
122   ;; string name of slot
123   %name 
124   ;; its position in the implementation sequence
125   (index (required-argument) :type fixnum)
126   ;; the name of the accessor function
127   ;;
128   ;; (CMU CL had extra complexity here ("..or NIL if this accessor has
129   ;; the same name as an inherited accessor (which we don't want to
130   ;; shadow)") but that behavior doesn't seem to be specified by (or
131   ;; even particularly consistent with) ANSI, so it's gone in SBCL.)
132   (accessor-name nil)
133   default                       ; default value expression
134   (type t)                      ; declared type specifier
135   ;; If this object does not describe a raw slot, this value is T.
136   ;;
137   ;; If this object describes a raw slot, this value is the type of the
138   ;; value that the raw slot holds. Mostly. (KLUDGE: If the raw slot has
139   ;; type (UNSIGNED-BYTE 32), the value here is UNSIGNED-BYTE, not
140   ;; (UNSIGNED-BYTE 32).)
141   (raw-type t :type (member t single-float double-float
142                             #!+long-float long-float
143                             complex-single-float complex-double-float
144                             #!+long-float complex-long-float
145                             unsigned-byte))
146   (read-only nil :type (member t nil)))
147 (def!method print-object ((x defstruct-slot-description) stream)
148   (print-unreadable-object (x stream :type t)
149     (prin1 (dsd-name x) stream)))
150
151 ;;; Is DEFSTRUCT a structure with a class?
152 (defun dd-class-p (defstruct)
153   (member (dd-type defstruct) '(structure funcallable-structure)))
154
155 ;;; Return the name of a defstruct slot as a symbol. We store it as a
156 ;;; string to avoid creating lots of worthless symbols at load time.
157 (defun dsd-name (dsd)
158   (intern (string (dsd-%name dsd))
159           (if (dsd-accessor-name dsd)
160               (symbol-package (dsd-accessor-name dsd))
161               (sane-package))))
162 \f
163 ;;;; typed (non-class) structures
164
165 ;;; Return a type specifier we can use for testing :TYPE'd structures.
166 (defun dd-lisp-type (defstruct)
167   (ecase (dd-type defstruct)
168     (list 'list)
169     (vector `(simple-array ,(dd-element-type defstruct) (*)))))
170 \f
171 ;;;; the legendary DEFSTRUCT macro itself (both CL:DEFSTRUCT and its
172 ;;;; close personal friend SB!XC:DEFSTRUCT)
173
174 ;;; Return a list of forms to install PRINT and MAKE-LOAD-FORM funs,
175 ;;; mentioning them in the expansion so that they can be compiled.
176 (defun class-method-definitions (defstruct)
177   (let ((name (dd-name defstruct)))
178     `((locally
179         ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant
180         ;; class names which creates fast but non-cold-loadable,
181         ;; non-compact code. In this context, we'd rather have
182         ;; compact, cold-loadable code. -- WHN 19990928
183         (declare (notinline sb!xc:find-class))
184         ,@(let ((pf (dd-print-function defstruct))
185                 (po (dd-print-object defstruct))
186                 (x (gensym))
187                 (s (gensym)))
188             ;; Giving empty :PRINT-OBJECT or :PRINT-FUNCTION options
189             ;; leaves PO or PF equal to NIL. The user-level effect is
190             ;; to generate a PRINT-OBJECT method specialized for the type,
191             ;; implementing the default #S structure-printing behavior.
192             (when (or (eq pf nil) (eq po nil))
193               (setf pf '(default-structure-print)
194                     po 0))
195             (flet (;; Given an arg from a :PRINT-OBJECT or :PRINT-FUNCTION
196                    ;; option, return the value to pass as an arg to FUNCTION.
197                    (farg (oarg)
198                      (destructuring-bind (fun-name) oarg
199                        fun-name)))
200               (cond ((not (eql pf 0))
201                      `((def!method print-object ((,x ,name) ,s)
202                          (funcall #',(farg pf) ,x ,s *current-level*))))
203                     ((not (eql po 0))
204                      `((def!method print-object ((,x ,name) ,s)
205                          (funcall #',(farg po) ,x ,s))))
206                     (t nil))))
207         ,@(let ((pure (dd-pure defstruct)))
208             (cond ((eq pure t)
209                    `((setf (layout-pure (class-layout
210                                          (sb!xc:find-class ',name)))
211                            t)))
212                   ((eq pure :substructure)
213                    `((setf (layout-pure (class-layout
214                                          (sb!xc:find-class ',name)))
215                            0)))))
216         ,@(let ((def-con (dd-default-constructor defstruct)))
217             (when (and def-con (not (dd-alternate-metaclass defstruct)))
218               `((setf (structure-class-constructor (sb!xc:find-class ',name))
219                       #',def-con))))))))
220 ;;; FIXME: I really would like to make structure accessors less
221 ;;; special, just ordinary inline functions. (Or perhaps inline
222 ;;; functions with special compact implementations of their
223 ;;; expansions, to avoid bloating the system.)
224
225 ;;; shared logic for CL:DEFSTRUCT and SB!XC:DEFSTRUCT
226 (defmacro !expander-for-defstruct (name-and-options
227                                    slot-descriptions
228                                    expanding-into-code-for-xc-host-p)
229   `(let ((name-and-options ,name-and-options)
230          (slot-descriptions ,slot-descriptions)
231          (expanding-into-code-for-xc-host-p
232           ,expanding-into-code-for-xc-host-p))
233      (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
234                  name-and-options
235                  slot-descriptions))
236             (name (dd-name dd)))
237        (if (dd-class-p dd)
238            (let ((inherits (inherits-for-structure dd)))
239              `(progn
240                 (eval-when (:compile-toplevel :load-toplevel :execute)
241                   (%compiler-defstruct ',dd ',inherits))
242                 (%defstruct ',dd ',inherits)
243                 ,@(unless expanding-into-code-for-xc-host-p
244                     (append (raw-accessor-definitions dd)
245                             (predicate-definitions dd)
246                             ;; FIXME: We've inherited from CMU CL nonparallel
247                             ;; code for creating copiers for typed and untyped
248                             ;; structures. This should be fixed.
249                                         ;(copier-definition dd)
250                             (constructor-definitions dd)
251                             (class-method-definitions dd)))
252                 ',name))
253            `(progn
254               (eval-when (:compile-toplevel :load-toplevel :execute)
255                 (setf (info :typed-structure :info ',name) ',dd))
256               ,@(unless expanding-into-code-for-xc-host-p
257                   (append (typed-accessor-definitions dd)
258                           (typed-predicate-definitions dd)
259                           (typed-copier-definitions dd)
260                           (constructor-definitions dd)))
261               ',name)))))
262
263 (sb!xc:defmacro defstruct (name-and-options &rest slot-descriptions)
264   #!+sb-doc
265   "DEFSTRUCT {Name | (Name Option*)} {Slot | (Slot [Default] {Key Value}*)}
266    Define the structure type Name. Instances are created by MAKE-<name>, 
267    which takes &KEY arguments allowing initial slot values to the specified.
268    A SETF'able function <name>-<slot> is defined for each slot to read and
269    write slot values. <name>-p is a type predicate.
270
271    Popular DEFSTRUCT options (see manual for others):
272
273    (:CONSTRUCTOR Name)
274    (:PREDICATE Name)
275        Specify the name for the constructor or predicate.
276
277    (:CONSTRUCTOR Name Lambda-List)
278        Specify the name and arguments for a BOA constructor
279        (which is more efficient when keyword syntax isn't necessary.)
280
281    (:INCLUDE Supertype Slot-Spec*)
282        Make this type a subtype of the structure type Supertype. The optional
283        Slot-Specs override inherited slot options.
284
285    Slot options:
286
287    :TYPE Type-Spec
288        Asserts that the value of this slot is always of the specified type.
289
290    :READ-ONLY {T | NIL}
291        If true, no setter function is defined for this slot."
292     (!expander-for-defstruct name-and-options slot-descriptions nil))
293 #+sb-xc-host
294 (defmacro sb!xc:defstruct (name-and-options &rest slot-descriptions)
295   #!+sb-doc
296   "Cause information about a target structure to be built into the
297   cross-compiler."
298   (!expander-for-defstruct name-and-options slot-descriptions t))
299 \f
300 ;;;; functions to generate code for various parts of DEFSTRUCT definitions
301
302 ;;; Catch requests to mess up definitions in COMMON-LISP.
303 #-sb-xc-host
304 (eval-when (:compile-toplevel :load-toplevel :execute)
305   (defun protect-cl (symbol)
306     (when (and *cold-init-complete-p*
307                (eq (symbol-package symbol) *cl-package*))
308       (cerror "Go ahead and patch the system."
309               "attempting to modify a symbol in the COMMON-LISP package: ~S"
310               symbol))))
311
312 ;;; Return forms to define readers and writers for raw slots as inline
313 ;;; functions.
314 (defun raw-accessor-definitions (dd)
315   (let* ((name (dd-name dd)))
316     (collect ((res))
317       (dolist (slot (dd-slots dd))
318         (let ((slot-type (dsd-type slot))
319               (accessor-name (dsd-accessor-name slot))
320               (argname (gensym "ARG"))
321               (nvname (gensym "NEW-VALUE-")))
322           (multiple-value-bind (accessor offset data)
323               (slot-accessor-form dd slot argname)
324             ;; When accessor exists and is raw
325             (when (and accessor-name
326                        (not (eq accessor-name '%instance-ref)))
327               (res `(declaim (inline ,accessor-name)))
328               (res `(declaim (ftype (function (,name) ,slot-type)
329                                     ,accessor-name)))
330               (res `(defun ,accessor-name (,argname)
331                       ;; Note: The DECLARE here might seem redundant
332                       ;; with the DECLAIM FTYPE above, but it's not:
333                       ;; If we're not at toplevel, the PROCLAIM inside
334                       ;; the DECLAIM doesn't get executed until after
335                       ;; this function is compiled.
336                       (declare (type ,name ,argname))
337                       (truly-the ,slot-type (,accessor ,data ,offset))))
338               (unless (dsd-read-only slot)
339                 (res `(declaim (inline (setf ,accessor-name))))
340                 (res `(declaim (ftype (function (,slot-type ,name) ,slot-type)
341                                       (setf ,accessor-name))))
342                 ;; FIXME: I rewrote this somewhat from the CMU CL definition.
343                 ;; Do some basic tests to make sure that reading and writing
344                 ;; raw slots still works correctly.
345                 (res `(defun (setf ,accessor-name) (,nvname ,argname)
346                         (declare (type ,name ,argname))
347                         (setf (,accessor ,data ,offset) ,nvname)
348                         ,nvname)))))))
349       (res))))
350
351 ;;; Return a list of forms which create a predicate for an untyped DEFSTRUCT.
352 (defun predicate-definitions (dd)
353   (let ((pred (dd-predicate-name dd))
354         (argname (gensym)))
355     (when pred
356       (if (eq (dd-type dd) 'funcallable-structure)
357           ;; FIXME: Why does this need to be special-cased for
358           ;; FUNCALLABLE-STRUCTURE? CMU CL did it, but without explanation.
359           ;; Could we do without it? What breaks if we do? Or could we
360           ;; perhaps get by with no predicates for funcallable structures?
361           `((declaim (inline ,pred))
362             (defun ,pred (,argname) (typep ,argname ',(dd-name dd))))
363           `((protect-cl ',pred)
364             (declaim (inline ,pred))
365             (defun ,pred (,argname)
366               (declare (optimize (speed 3) (safety 0)))
367               (typep-to-layout ,argname
368                                (compile-time-find-layout ,(dd-name dd)))))))))
369
370 ;;; Return a list of forms which create a predicate function for a typed
371 ;;; DEFSTRUCT.
372 (defun typed-predicate-definitions (defstruct)
373   (let ((name (dd-name defstruct))
374         (predicate-name (dd-predicate-name defstruct))
375         (argname (gensym)))
376     (when (and predicate-name (dd-named defstruct))
377       (let ((ltype (dd-lisp-type defstruct)))
378         `((defun ,predicate-name (,argname)
379             (and (typep ,argname ',ltype)
380                  (eq (elt (the ,ltype ,argname)
381                           ,(cdr (car (last (find-name-indices defstruct)))))
382                      ',name))))))))
383
384 ;;; FIXME: We've inherited from CMU CL code to do typed structure copiers
385 ;;; in a completely different way than untyped structure copiers. Fix this.
386 ;;; (This function was my first attempt to fix this, but I stopped before
387 ;;; figuring out how to install it completely and remove the parallel
388 ;;; code which simply SETF's the FDEFINITION of the DD-COPIER name.
389 #|
390 ;;; Return the copier definition for an untyped DEFSTRUCT.
391 (defun copier-definition (dd)
392   (when (and (dd-copier dd)
393              ;; FUNCALLABLE-STRUCTUREs don't need copiers, and this
394              ;; implementation wouldn't work for them anyway, since
395              ;; COPY-STRUCTURE returns a STRUCTURE-OBJECT and they're not.
396              (not (eq (dd-type info) 'funcallable-structure)))
397     (let ((argname (gensym)))
398       `(progn
399          (protect-cl ',(dd-copier dd))
400          (defun ,(dd-copier dd) (,argname)
401            (declare (type ,(dd-name dd) ,argname))
402            (copy-structure ,argname))))))
403 |#
404
405 ;;; Return a list of forms to create a copier function of a typed DEFSTRUCT.
406 (defun typed-copier-definitions (defstruct)
407   (when (dd-copier defstruct)
408     `((setf (fdefinition ',(dd-copier defstruct)) #'copy-seq)
409       (declaim (ftype function ,(dd-copier defstruct))))))
410
411 ;;; Return a list of function definitions for accessing and setting the
412 ;;; slots of a typed DEFSTRUCT. The functions are proclaimed to be inline,
413 ;;; and the types of their arguments and results are declared as well. We
414 ;;; count on the compiler to do clever things with ELT.
415 (defun typed-accessor-definitions (defstruct)
416   (collect ((stuff))
417     (let ((ltype (dd-lisp-type defstruct)))
418       (dolist (slot (dd-slots defstruct))
419         (let ((name (dsd-accessor-name slot))
420               (index (dsd-index slot))
421               (slot-type `(and ,(dsd-type slot)
422                                ,(dd-element-type defstruct))))
423           (stuff `(proclaim '(inline ,name (setf ,name))))
424           ;; FIXME: The arguments in the next two DEFUNs should be
425           ;; gensyms. (Otherwise e.g. if NEW-VALUE happened to be the
426           ;; name of a special variable, things could get weird.)
427           (stuff `(defun ,name (structure)
428                     (declare (type ,ltype structure))
429                     (the ,slot-type (elt structure ,index))))
430           (unless (dsd-read-only slot)
431             (stuff
432              `(defun (setf ,name) (new-value structure)
433                 (declare (type ,ltype structure) (type ,slot-type new-value))
434                 (setf (elt structure ,index) new-value)))))))
435     (stuff)))
436 \f
437 ;;;; parsing
438
439 (defun require-no-print-options-so-far (defstruct)
440   (unless (and (eql (dd-print-function defstruct) 0)
441                (eql (dd-print-object defstruct) 0))
442     (error "No more than one of the following options may be specified:
443   :PRINT-FUNCTION, :PRINT-OBJECT, :TYPE")))
444
445 ;;; Parse a single DEFSTRUCT option and store the results in DD.
446 (defun parse-1-dd-option (option dd)
447   (let ((args (rest option))
448         (name (dd-name dd)))
449     (case (first option)
450       (:conc-name
451        (destructuring-bind (conc-name) args
452          (setf (dd-conc-name dd)
453                (if (symbolp conc-name)
454                    conc-name
455                    (make-symbol (string conc-name))))))
456       (:constructor
457        (destructuring-bind (&optional (cname (symbolicate "MAKE-" name))
458                                       &rest stuff)
459            args
460          (push (cons cname stuff) (dd-constructors dd))))
461       (:copier
462        (destructuring-bind (&optional (copier (symbolicate "COPY-" name)))
463            args
464          (setf (dd-copier dd) copier)))
465       (:predicate
466        (destructuring-bind (&optional (predicate-name (symbolicate name "-P")))
467            args
468          (setf (dd-predicate-name dd) predicate-name)))
469       (:include
470        (when (dd-include dd)
471          (error "more than one :INCLUDE option"))
472        (setf (dd-include dd) args))
473       (:alternate-metaclass
474        (setf (dd-alternate-metaclass dd) args))
475       (:print-function
476        (require-no-print-options-so-far dd)
477        (setf (dd-print-function dd)
478              (the (or symbol cons) args)))
479       (:print-object
480        (require-no-print-options-so-far dd)
481        (setf (dd-print-object dd)
482              (the (or symbol cons) args)))
483       (:type
484        (destructuring-bind (type) args
485          (cond ((eq type 'funcallable-structure)
486                 (setf (dd-type dd) type))
487                ((member type '(list vector))
488                 (setf (dd-element-type dd) t)
489                 (setf (dd-type dd) type))
490                ((and (consp type) (eq (first type) 'vector))
491                 (destructuring-bind (vector vtype) type
492                   (declare (ignore vector))
493                   (setf (dd-element-type dd) vtype)
494                   (setf (dd-type dd) 'vector)))
495                (t
496                 (error "~S is a bad :TYPE for DEFSTRUCT." type)))))
497       (:named
498        (error "The DEFSTRUCT option :NAMED takes no arguments."))
499       (:initial-offset
500        (destructuring-bind (offset) args
501          (setf (dd-offset dd) offset)))
502       (:pure
503        (destructuring-bind (fun) args
504          (setf (dd-pure dd) fun)))
505       (t (error "unknown DEFSTRUCT option:~%  ~S" option)))))
506
507 ;;; Given name and options, return a DD holding that info.
508 (eval-when (:compile-toplevel :load-toplevel :execute)
509 (defun parse-defstruct-name-and-options (name-and-options)
510   (destructuring-bind (name &rest options) name-and-options
511     (aver name) ; A null name doesn't seem to make sense here.
512     (let ((dd (make-defstruct-description name)))
513       (dolist (option options)
514         (cond ((consp option)
515                (parse-1-dd-option option dd))
516               ((eq option :named)
517                (setf (dd-named dd) t))
518               ((member option '(:constructor :copier :predicate :named))
519                (parse-1-dd-option (list option) dd))
520               (t
521                (error "unrecognized DEFSTRUCT option: ~S" option))))
522
523       (case (dd-type dd)
524         (structure
525          (when (dd-offset dd)
526            (error ":OFFSET can't be specified unless :TYPE is specified."))
527          (unless (dd-include dd)
528            (incf (dd-length dd))))
529         (funcallable-structure)
530         (t
531          (require-no-print-options-so-far dd)
532          (when (dd-named dd)
533            (incf (dd-length dd)))
534          (let ((offset (dd-offset dd)))
535            (when offset (incf (dd-length dd) offset)))))
536
537       (when (dd-include dd)
538         (do-dd-inclusion-stuff dd))
539
540       dd)))
541
542 ;;; Given name and options and slot descriptions (and possibly doc
543 ;;; string at the head of slot descriptions) return a DD holding that
544 ;;; info.
545 (defun parse-defstruct-name-and-options-and-slot-descriptions
546     (name-and-options slot-descriptions)
547   (let ((result (parse-defstruct-name-and-options (if (atom name-and-options)
548                                                       (list name-and-options)
549                                                       name-and-options))))
550     (when (stringp (car slot-descriptions))
551       (setf (dd-doc result) (pop slot-descriptions)))
552     (dolist (slot-description slot-descriptions)
553       (allocate-1-slot result (parse-1-dsd result slot-description)))
554     result))
555
556 ) ; EVAL-WHEN
557 \f
558 ;;;; stuff to parse slot descriptions
559
560 ;;; Parse a slot description for DEFSTRUCT, add it to the description
561 ;;; and return it. If supplied, SLOT is a pre-initialized DSD
562 ;;; that we modify to get the new slot. This is supplied when handling
563 ;;; included slots.
564 (defun parse-1-dsd (defstruct spec &optional
565                     (slot (make-defstruct-slot-description :%name ""
566                                                            :index 0
567                                                            :type t)))
568   (multiple-value-bind (name default default-p type type-p read-only ro-p)
569       (cond
570        ((listp spec)
571         (destructuring-bind
572             (name
573              &optional (default nil default-p)
574              &key (type nil type-p) (read-only nil ro-p))
575             spec
576           (values name
577                   default default-p
578                   (uncross type) type-p
579                   read-only ro-p)))
580        (t
581         (when (keywordp spec)
582           (style-warn "Keyword slot name indicates probable syntax ~
583                        error in DEFSTRUCT: ~S."
584                       spec))
585         spec))
586
587     (when (find name (dd-slots defstruct) :test #'string= :key #'dsd-%name)
588       (error 'simple-program-error
589              :format-control "duplicate slot name ~S"
590              :format-arguments (list name)))
591     (setf (dsd-%name slot) (string name))
592     (setf (dd-slots defstruct) (nconc (dd-slots defstruct) (list slot)))
593
594     (let ((accessor-name (symbolicate (or (dd-conc-name defstruct) "") name))
595           (predicate-name (dd-predicate-name defstruct)))
596       (setf (dsd-accessor-name slot) accessor-name)
597       (when (eql accessor-name predicate-name)
598         ;; Some adventurous soul has named a slot so that its accessor
599         ;; collides with the structure type predicate. ANSI doesn't
600         ;; specify what to do in this case. As of 2001-09-04, Martin
601         ;; Atzmueller reports that CLISP and Lispworks both give
602         ;; priority to the slot accessor, so that the predicate is
603         ;; overwritten. We might as well do the same (as well as
604         ;; signalling a warning).
605         (style-warn
606          "~@<The structure accessor name ~S is the same as the name of the ~
607           structure type predicate. ANSI doesn't specify what to do in ~
608           this case; this implementation chooses to overwrite the type ~
609           predicate with the slot accessor.~@:>"
610          accessor-name)
611         (setf (dd-predicate-name defstruct) nil)))
612
613     (when default-p
614       (setf (dsd-default slot) default))
615     (when type-p
616       (setf (dsd-type slot)
617             (if (eq (dsd-type slot) t)
618                 type
619                 `(and ,(dsd-type slot) ,type))))
620     (when ro-p
621       (if read-only
622           (setf (dsd-read-only slot) t)
623           (when (dsd-read-only slot)
624             (error "Slot ~S is :READ-ONLY in parent and must be :READ-ONLY in subtype ~S."
625                    name
626                    (dsd-name slot)))))
627     slot))
628
629 ;;; When a value of type TYPE is stored in a structure, should it be
630 ;;; stored in a raw slot? Return (VALUES RAW? RAW-TYPE WORDS), where
631 ;;;   RAW? is true if TYPE should be stored in a raw slot.
632 ;;;   RAW-TYPE is the raw slot type, or NIL if no raw slot.
633 ;;;   WORDS is the number of words in the raw slot, or NIL if no raw slot.
634 (defun structure-raw-slot-type-and-size (type)
635   (/noshow "in STRUCTURE-RAW-SLOT-TYPE-AND-SIZE" type (sb!xc:subtypep type 'fixnum))
636   (cond #+nil
637         (;; FIXME: For now we suppress raw slots, since there are various
638          ;; issues about the way that the cross-compiler handles them.
639          (not (boundp '*dummy-placeholder-to-stop-compiler-warnings*))
640          (values nil nil nil))
641         ((and (sb!xc:subtypep type '(unsigned-byte 32))
642               (multiple-value-bind (fixnum? fixnum-certain?)
643                   (sb!xc:subtypep type 'fixnum)
644                 (/noshow fixnum? fixnum-certain?)
645                 ;; (The extra test for FIXNUM-CERTAIN? here is
646                 ;; intended for bootstrapping the system. In
647                 ;; particular, in sbcl-0.6.2, we set up LAYOUT before
648                 ;; FIXNUM is defined, and so could bogusly end up
649                 ;; putting INDEX-typed values into raw slots if we
650                 ;; didn't test FIXNUM-CERTAIN?.)
651                 (and (not fixnum?) fixnum-certain?)))
652          (values t 'unsigned-byte 1))
653         ((sb!xc:subtypep type 'single-float)
654          (values t 'single-float 1))
655         ((sb!xc:subtypep type 'double-float)
656          (values t 'double-float 2))
657         #!+long-float
658         ((sb!xc:subtypep type 'long-float)
659          (values t 'long-float #!+x86 3 #!+sparc 4))
660         ((sb!xc:subtypep type '(complex single-float))
661          (values t 'complex-single-float 2))
662         ((sb!xc:subtypep type '(complex double-float))
663          (values t 'complex-double-float 4))
664         #!+long-float
665         ((sb!xc:subtypep type '(complex long-float))
666          (values t 'complex-long-float #!+x86 6 #!+sparc 8))
667         (t
668          (values nil nil nil))))
669
670 ;;; Allocate storage for a DSD in DD. This is where we decide whether
671 ;;; a slot is raw or not. If raw, and we haven't allocated a raw-index
672 ;;; yet for the raw data vector, then do it. Raw objects are aligned
673 ;;; on the unit of their size.
674 (defun allocate-1-slot (dd dsd)
675   (multiple-value-bind (raw? raw-type words)
676       (if (eq (dd-type dd) 'structure)
677           (structure-raw-slot-type-and-size (dsd-type dsd))
678           (values nil nil nil))
679     (/noshow "ALLOCATE-1-SLOT" dsd raw? raw-type words)
680     (cond ((not raw?)
681            (setf (dsd-index dsd) (dd-length dd))
682            (incf (dd-length dd)))
683           (t
684            (unless (dd-raw-index dd)
685              (setf (dd-raw-index dd) (dd-length dd))
686              (incf (dd-length dd)))
687            (let ((off (rem (dd-raw-length dd) words)))
688              (unless (zerop off)
689                (incf (dd-raw-length dd) (- words off))))
690            (setf (dsd-raw-type dsd) raw-type)
691            (setf (dsd-index dsd) (dd-raw-length dd))
692            (incf (dd-raw-length dd) words))))
693   (values))
694
695 (defun typed-structure-info-or-lose (name)
696   (or (info :typed-structure :info name)
697       (error ":TYPE'd DEFSTRUCT ~S not found for inclusion." name)))
698
699 ;;; Process any included slots pretty much like they were specified.
700 ;;; Also inherit various other attributes.
701 (defun do-dd-inclusion-stuff (dd)
702   (destructuring-bind (included-name &rest modified-slots) (dd-include dd)
703     (let* ((type (dd-type dd))
704            (included-structure
705             (if (dd-class-p dd)
706                 (layout-info (compiler-layout-or-lose included-name))
707                 (typed-structure-info-or-lose included-name))))
708       (unless (and (eq type (dd-type included-structure))
709                    (type= (specifier-type (dd-element-type included-structure))
710                           (specifier-type (dd-element-type dd))))
711         (error ":TYPE option mismatch between structures ~S and ~S"
712                (dd-name dd) included-name))
713
714       (incf (dd-length dd) (dd-length included-structure))
715       (when (dd-class-p dd)
716         (let ((mc (rest (dd-alternate-metaclass included-structure))))
717           (when (and mc (not (dd-alternate-metaclass dd)))
718             (setf (dd-alternate-metaclass dd)
719                   (cons included-name mc))))
720         (when (eq (dd-pure dd) :unspecified)
721           (setf (dd-pure dd) (dd-pure included-structure)))
722         (setf (dd-raw-index dd) (dd-raw-index included-structure))
723         (setf (dd-raw-length dd) (dd-raw-length included-structure)))
724
725       (dolist (included-slot (dd-slots included-structure))
726         (let* ((included-name (dsd-name included-slot))
727                (modified (or (find included-name modified-slots
728                                    :key #'(lambda (x) (if (atom x) x (car x)))
729                                    :test #'string=)
730                              `(,included-name))))
731           (parse-1-dsd dd
732                        modified
733                        (copy-structure included-slot)))))))
734 \f
735 ;;;; various helper functions for setting up DEFSTRUCTs
736
737 ;;; This function is called at macroexpand time to compute the INHERITS
738 ;;; vector for a structure type definition.
739 (defun inherits-for-structure (info)
740   (declare (type defstruct-description info))
741   (let* ((include (dd-include info))
742          (superclass-opt (dd-alternate-metaclass info))
743          (super
744           (if include
745               (compiler-layout-or-lose (first include))
746               (class-layout (sb!xc:find-class
747                              (or (first superclass-opt)
748                                  'structure-object))))))
749     (if (eq (dd-name info) 'lisp-stream)
750         ;; a hack to added the stream class as a mixin for LISP-STREAMs
751         (concatenate 'simple-vector
752                      (layout-inherits super)
753                      (vector super
754                              (class-layout (sb!xc:find-class 'stream))))
755         (concatenate 'simple-vector
756                      (layout-inherits super)
757                      (vector super)))))
758
759 ;;; Do miscellaneous (LOAD EVAL) time actions for the structure
760 ;;; described by INFO. Create the class & layout, checking for
761 ;;; incompatible redefinition. Define setters, accessors, copier,
762 ;;; predicate, documentation, instantiate definition in load-time env.
763 ;;; This is only called for default structures.
764 (defun %defstruct (info inherits)
765   (declare (type defstruct-description info))
766   (multiple-value-bind (class layout old-layout)
767       (ensure-structure-class info inherits "current" "new")
768     (cond ((not old-layout)
769            (unless (eq (class-layout class) layout)
770              (register-layout layout)))
771           (t
772            (let ((old-info (layout-info old-layout)))
773              (when (defstruct-description-p old-info)
774                (dolist (slot (dd-slots old-info))
775                  (fmakunbound (dsd-accessor-name slot))
776                  (unless (dsd-read-only slot)
777                    (fmakunbound `(setf ,(dsd-accessor-name slot)))))))
778            (%redefine-defstruct class old-layout layout)
779            (setq layout (class-layout class))))
780
781     (setf (sb!xc:find-class (dd-name info)) class)
782
783     ;; Set FDEFINITIONs for structure accessors, setters, predicates,
784     ;; and copiers.
785     #-sb-xc-host
786     (unless (eq (dd-type info) 'funcallable-structure)
787
788       (dolist (slot (dd-slots info))
789         (let ((dsd slot))
790           (when (and (dsd-accessor-name slot)
791                      (eq (dsd-raw-type slot) t))
792             (protect-cl (dsd-accessor-name slot))
793             (setf (symbol-function (dsd-accessor-name slot))
794                   (structure-slot-getter layout dsd))
795             (unless (dsd-read-only slot)
796               (setf (fdefinition `(setf ,(dsd-accessor-name slot)))
797                     (structure-slot-setter layout dsd))))))
798
799       ;; FIXME: Someday it'd probably be good to go back to using
800       ;; closures for the out-of-line forms of structure accessors.
801       #|
802       (when (dd-predicate info)
803         (protect-cl (dd-predicate info))
804         (setf (symbol-function (dd-predicate info))
805               #'(lambda (object)
806                   (declare (optimize (speed 3) (safety 0)))
807                   (typep-to-layout object layout))))
808       |#
809
810       (when (dd-copier info)
811         (protect-cl (dd-copier info))
812         (setf (symbol-function (dd-copier info))
813               #'(lambda (structure)
814                   (declare (optimize (speed 3) (safety 0)))
815                   (flet ((layout-test (structure)
816                            (typep-to-layout structure layout)))
817                     (unless (layout-test structure)
818                       (error 'simple-type-error
819                              :datum structure
820                              :expected-type '(satisfies layout-test)
821                              :format-control
822                              "Structure for copier is not a ~S:~% ~S"
823                              :format-arguments
824                              (list (sb!xc:class-name (layout-class layout))
825                                    structure))))
826                   (copy-structure structure))))))
827
828   (when (dd-doc info)
829     (setf (fdocumentation (dd-name info) 'type) (dd-doc info)))
830
831   (values))
832
833 ;;; Return a form describing the writable place used for this slot
834 ;;; in the instance named INSTANCE-NAME.
835 (defun %accessor-place-form (dd dsd instance-name)
836   (let (;; the operator that we'll use to access a typed slot or, in
837         ;; the case of a raw slot, to read the vector of raw slots
838         (ref (ecase (dd-type dd)
839                (structure '%instance-ref)
840                (funcallable-structure '%funcallable-instance-info)
841                (list 'nth-but-with-sane-arg-order)
842                (vector 'aref)))
843         (raw-type (dsd-raw-type dsd)))
844     (if (eq raw-type t) ; if not raw slot
845         `(,ref ,instance-name ,(dsd-index dsd))
846         (let (;; the operator that we'll use to access one value in
847               ;; the raw data vector
848               (rawref (ecase raw-type
849                         ;; The compiler thinks that the raw data
850                         ;; vector is a vector of unsigned bytes, so if
851                         ;; the slot we want to access actually *is* an
852                         ;; unsigned byte, it'll access the slot for
853                         ;; us even if we don't lie to it at all.
854                         (unsigned-byte 'aref)
855                         ;; "A lie can travel halfway round the world while
856                         ;; the truth is putting on its shoes." -- Mark Twain
857                         (single-float '%raw-ref-single)
858                         (double-float '%raw-ref-double)
859                         #!+long-float (long-float '%raw-ref-long)
860                         (complex-single-float '%raw-ref-complex-single)
861                         (complex-double-float '%raw-ref-complex-double)
862                         #!+long-float (complex-long-float
863                                        '%raw-ref-complex-long))))
864           `(,rawref (,ref ,instance-name ,(dd-raw-index dd))
865                     ,(dsd-index dsd))))))
866
867 ;;; Return inline expansion designators (i.e. values suitable for
868 ;;; (INFO :FUNCTION :INLINE-EXPANSSION-DESIGNATOR ..)) for the reader
869 ;;; and writer functions of the slot described by DSD.
870 (defun accessor-inline-expansion-designators (dd dsd)
871   ;; ordinary tagged non-raw slot case
872   (values (lambda ()
873             `(lambda (instance)
874                (declare (type ,(dd-name dd) instance))
875                (truly-the ,(dsd-type dsd)
876                           ,(%accessor-place-form dd dsd 'instance))))
877           (lambda ()
878             `(lambda (new-value instance)
879                (declare (type ,(dsd-type dsd) new-value))
880                (declare (type ,(dd-name dd) structure-object))
881                (setf ,(%accessor-place-form dd dsd 'instance) new-value)))))
882
883 ;;; Do (COMPILE LOAD EVAL)-time actions for the defstruct described by DD.
884 (defun %compiler-defstruct (dd inherits)
885   (declare (type defstruct-description dd))
886   (multiple-value-bind (class layout old-layout)
887       (multiple-value-bind (clayout clayout-p)
888           (info :type :compiler-layout (dd-name dd))
889         (ensure-structure-class dd
890                                 inherits
891                                 (if clayout-p "previously compiled" "current")
892                                 "compiled"
893                                 :compiler-layout clayout))
894     (cond (old-layout
895            (undefine-structure (layout-class old-layout))
896            (when (and (class-subclasses class)
897                       (not (eq layout old-layout)))
898              (collect ((subs))
899                       (dohash (class layout (class-subclasses class))
900                         (declare (ignore layout))
901                         (undefine-structure class)
902                         (subs (class-proper-name class)))
903                       (when (subs)
904                         (warn "removing old subclasses of ~S:~%  ~S"
905                               (sb!xc:class-name class)
906                               (subs))))))
907           (t
908            (unless (eq (class-layout class) layout)
909              (register-layout layout :invalidate nil))
910            (setf (sb!xc:find-class (dd-name dd)) class)))
911
912     (setf (info :type :compiler-layout (dd-name dd)) layout))
913
914   (ecase (dd-type dd)
915     ((vector list funcallable-structure)
916      ;; nothing extra to do in this case
917      )
918     ((structure)
919      (let* ((name (dd-name dd))
920             (class (sb!xc:find-class name)))
921
922        (let ((copier (dd-copier dd)))
923          (when copier
924            (proclaim `(ftype (function (,name) ,name) ,copier))))
925
926        (dolist (dsd (dd-slots dd))
927          (let* ((accessor-name (dsd-accessor-name dsd)))
928            (when accessor-name
929
930              ;; new implementation sbcl-0.pre7.64
931              (multiple-value-bind (reader-designator writer-designator)
932                  (accessor-inline-expansion-designators dd dsd)
933                (setf (info :function
934                            :inline-expansion-designator
935                            accessor-name)
936                      reader-designator
937                      (info :function :inlinep accessor-name)
938                      :inline)
939                (unless (dsd-read-only dsd)
940                  (let ((setf-accessor-name `(setf ,accessor-name)))
941                    (setf (info :function
942                                :inline-expansion-designator
943                                setf-accessor-name)
944                          writer-designator
945                          (info :function :inlinep setf-accessor-name)
946                          :inline))))
947
948              ;; old code from before sbcl-0.pre7.64, will hopefully
949              ;; fade away and/or merge into new code above
950              (when (eq (dsd-raw-type dsd) t) ; when not raw slot
951                (proclaim-as-defstruct-fun-name accessor-name)
952                (setf (info :function :accessor-for accessor-name) class)
953                (unless (dsd-read-only dsd)
954                  (proclaim-as-defstruct-fun-name `(setf ,accessor-name))
955                  (setf (info :function :accessor-for `(setf ,accessor-name))
956                        class))))))
957
958        ;; FIXME: Couldn't this logic be merged into
959        ;; PROCLAIM-AS-DEFSTRUCT-FUN-NAME?
960        (when (boundp 'sb!c:*free-functions*) ; when compiling
961          (let ((free-functions sb!c:*free-functions*))
962            (dolist (slot (dd-slots dd))
963              (let ((accessor-name (dsd-accessor-name slot)))
964                (remhash accessor-name free-functions)
965                (unless (dsd-read-only slot)
966                  (remhash `(setf ,accessor-name) free-functions))))
967            (remhash (dd-predicate-name dd) free-functions)
968            (remhash (dd-copier dd) free-functions))))))
969
970   (values))
971 \f
972 ;;;; redefinition stuff
973
974 ;;; Compare the slots of OLD and NEW, returning 3 lists of slot names:
975 ;;;   1. Slots which have moved,
976 ;;;   2. Slots whose type has changed,
977 ;;;   3. Deleted slots.
978 (defun compare-slots (old new)
979   (let* ((oslots (dd-slots old))
980          (nslots (dd-slots new))
981          (onames (mapcar #'dsd-name oslots))
982          (nnames (mapcar #'dsd-name nslots)))
983     (collect ((moved)
984               (retyped))
985       (dolist (name (intersection onames nnames))
986         (let ((os (find name oslots :key #'dsd-name))
987               (ns (find name nslots :key #'dsd-name)))
988           (unless (subtypep (dsd-type ns) (dsd-type os))
989             (/noshow "found retyped slots" ns os (dsd-type ns) (dsd-type os))
990             (retyped name))
991           (unless (and (= (dsd-index os) (dsd-index ns))
992                        (eq (dsd-raw-type os) (dsd-raw-type ns)))
993             (moved name))))
994       (values (moved)
995               (retyped)
996               (set-difference onames nnames)))))
997
998 ;;; If we are redefining a structure with different slots than in the
999 ;;; currently loaded version, give a warning and return true.
1000 (defun redefine-structure-warning (class old new)
1001   (declare (type defstruct-description old new)
1002            (type sb!xc:class class)
1003            (ignore class))
1004   (let ((name (dd-name new)))
1005     (multiple-value-bind (moved retyped deleted) (compare-slots old new)
1006       (when (or moved retyped deleted)
1007         (warn
1008          "incompatibly redefining slots of structure class ~S~@
1009           Make sure any uses of affected accessors are recompiled:~@
1010           ~@[  These slots were moved to new positions:~%    ~S~%~]~
1011           ~@[  These slots have new incompatible types:~%    ~S~%~]~
1012           ~@[  These slots were deleted:~%    ~S~%~]"
1013          name moved retyped deleted)
1014         t))))
1015
1016 ;;; This function is called when we are incompatibly redefining a
1017 ;;; structure Class to have the specified New-Layout. We signal an
1018 ;;; error with some proceed options and return the layout that should
1019 ;;; be used.
1020 (defun %redefine-defstruct (class old-layout new-layout)
1021   (declare (type sb!xc:class class) (type layout old-layout new-layout))
1022   (let ((name (class-proper-name class)))
1023     (restart-case
1024         (error "redefining class ~S incompatibly with the current definition"
1025                name)
1026       (continue ()
1027         :report "Invalidate current definition."
1028         (warn "Previously loaded ~S accessors will no longer work." name)
1029         (register-layout new-layout))
1030       (clobber-it ()
1031         :report "Smash current layout, preserving old code."
1032         (warn "Any old ~S instances will be in a bad way.~@
1033                I hope you know what you're doing..."
1034               name)
1035         (register-layout new-layout :invalidate nil
1036                          :destruct-layout old-layout))))
1037   (values))
1038
1039 ;;; This is called when we are about to define a structure class. It
1040 ;;; returns a (possibly new) class object and the layout which should
1041 ;;; be used for the new definition (may be the current layout, and
1042 ;;; also might be an uninstalled forward referenced layout.) The third
1043 ;;; value is true if this is an incompatible redefinition, in which
1044 ;;; case it is the old layout.
1045 (defun ensure-structure-class (info inherits old-context new-context
1046                                     &key compiler-layout)
1047   (multiple-value-bind (class old-layout)
1048       (destructuring-bind
1049           (&optional
1050            name
1051            (class 'sb!xc:structure-class)
1052            (constructor 'make-structure-class))
1053           (dd-alternate-metaclass info)
1054         (declare (ignore name))
1055         (insured-find-class (dd-name info)
1056                             (if (eq class 'sb!xc:structure-class)
1057                               (lambda (x)
1058                                 (typep x 'sb!xc:structure-class))
1059                               (lambda (x)
1060                                 (sb!xc:typep x (sb!xc:find-class class))))
1061                             (fdefinition constructor)))
1062     (setf (class-direct-superclasses class)
1063           (if (eq (dd-name info) 'lisp-stream)
1064               ;; a hack to add STREAM as a superclass mixin to LISP-STREAMs
1065               (list (layout-class (svref inherits (1- (length inherits))))
1066                     (layout-class (svref inherits (- (length inherits) 2))))
1067               (list (layout-class (svref inherits (1- (length inherits)))))))
1068     (let ((new-layout (make-layout :class class
1069                                    :inherits inherits
1070                                    :depthoid (length inherits)
1071                                    :length (dd-length info)
1072                                    :info info))
1073           (old-layout (or compiler-layout old-layout)))
1074       (cond
1075        ((not old-layout)
1076         (values class new-layout nil))
1077        (;; This clause corresponds to an assertion in REDEFINE-LAYOUT-WARNING
1078         ;; of classic CMU CL. I moved it out to here because it was only
1079         ;; exercised in this code path anyway. -- WHN 19990510
1080         (not (eq (layout-class new-layout) (layout-class old-layout)))
1081         (error "shouldn't happen: weird state of OLD-LAYOUT?"))
1082        ((not *type-system-initialized*)
1083         (setf (layout-info old-layout) info)
1084         (values class old-layout nil))
1085        ((redefine-layout-warning old-context
1086                                  old-layout
1087                                  new-context
1088                                  (layout-length new-layout)
1089                                  (layout-inherits new-layout)
1090                                  (layout-depthoid new-layout))
1091         (values class new-layout old-layout))
1092        (t
1093         (let ((old-info (layout-info old-layout)))
1094           (typecase old-info
1095             ((or defstruct-description)
1096              (cond ((redefine-structure-warning class old-info info)
1097                     (values class new-layout old-layout))
1098                    (t
1099                     (setf (layout-info old-layout) info)
1100                     (values class old-layout nil))))
1101             (null
1102              (setf (layout-info old-layout) info)
1103              (values class old-layout nil))
1104             (t
1105              (error "shouldn't happen! strange thing in LAYOUT-INFO:~%  ~S"
1106                     old-layout)
1107              (values class new-layout old-layout)))))))))
1108
1109 ;;; Blow away all the compiler info for the structure CLASS. Iterate
1110 ;;; over this type, clearing the compiler structure type info, and
1111 ;;; undefining all the associated functions.
1112 (defun undefine-structure (class)
1113   (let ((info (layout-info (class-layout class))))
1114     (when (defstruct-description-p info)
1115       (let ((type (dd-name info)))
1116         (setf (info :type :compiler-layout type) nil)
1117         (undefine-fun-name (dd-copier info))
1118         (undefine-fun-name (dd-predicate-name info))
1119         (dolist (slot (dd-slots info))
1120           (let ((fun (dsd-accessor-name slot)))
1121             (undefine-fun-name fun)
1122             (unless (dsd-read-only slot)
1123               (undefine-fun-name `(setf ,fun))))))
1124       ;; Clear out the SPECIFIER-TYPE cache so that subsequent
1125       ;; references are unknown types.
1126       (values-specifier-type-cache-clear)))
1127   (values))
1128 \f
1129 ;;; Return a list of pairs (name . index). Used for :TYPE'd
1130 ;;; constructors to find all the names that we have to splice in &
1131 ;;; where. Note that these types don't have a layout, so we can't look
1132 ;;; at LAYOUT-INHERITS.
1133 (defun find-name-indices (defstruct)
1134   (collect ((res))
1135     (let ((infos ()))
1136       (do ((info defstruct
1137                  (typed-structure-info-or-lose (first (dd-include info)))))
1138           ((not (dd-include info))
1139            (push info infos))
1140         (push info infos))
1141
1142       (let ((i 0))
1143         (dolist (info infos)
1144           (incf i (or (dd-offset info) 0))
1145           (when (dd-named info)
1146             (res (cons (dd-name info) i)))
1147           (setq i (dd-length info)))))
1148
1149     (res)))
1150 \f
1151 ;;;; slot accessors for raw slots
1152
1153 ;;; Return info about how to read/write a slot in the value stored in
1154 ;;; OBJECT. This is also used by constructors (since we can't safely
1155 ;;; use the accessor function, since some slots are read-only). If
1156 ;;; supplied, DATA is a variable holding the raw-data vector.
1157 ;;;
1158 ;;; returned values:
1159 ;;; 1. accessor function name (SETFable)
1160 ;;; 2. index to pass to accessor.
1161 ;;; 3. object form to pass to accessor
1162 (defun slot-accessor-form (defstruct slot object &optional data)
1163   (let ((rtype (dsd-raw-type slot)))
1164     (values
1165      (ecase rtype
1166        (single-float '%raw-ref-single)
1167        (double-float '%raw-ref-double)
1168        #!+long-float
1169        (long-float '%raw-ref-long)
1170        (complex-single-float '%raw-ref-complex-single)
1171        (complex-double-float '%raw-ref-complex-double)
1172        #!+long-float
1173        (complex-long-float '%raw-ref-complex-long)
1174        (unsigned-byte 'aref)
1175        ((t)
1176         (if (eq (dd-type defstruct) 'funcallable-structure)
1177             '%funcallable-instance-info
1178             '%instance-ref)))
1179      (case rtype
1180        #!+long-float
1181        (complex-long-float
1182         (truncate (dsd-index slot) #!+x86 6 #!+sparc 8))
1183        #!+long-float
1184        (long-float
1185         (truncate (dsd-index slot) #!+x86 3 #!+sparc 4))
1186        (double-float
1187         (ash (dsd-index slot) -1))
1188        (complex-double-float
1189         (ash (dsd-index slot) -2))
1190        (complex-single-float
1191         (ash (dsd-index slot) -1))
1192        (t
1193         (dsd-index slot)))
1194      (cond
1195       ((eq rtype t) object)
1196       (data)
1197       (t
1198        `(truly-the (simple-array (unsigned-byte 32) (*))
1199                    (%instance-ref ,object ,(dd-raw-index defstruct))))))))
1200 \f
1201 ;;; These functions are called to actually make a constructor after we
1202 ;;; have processed the arglist. The correct variant (according to the
1203 ;;; DD-TYPE) should be called. The function is defined with the
1204 ;;; specified name and arglist. Vars and Types are used for argument
1205 ;;; type declarations. Values are the values for the slots (in order.)
1206 ;;;
1207 ;;; This is split four ways because:
1208 ;;; 1] list & vector structures need "name" symbols stuck in at
1209 ;;;    various weird places, whereas STRUCTURE structures have
1210 ;;;    a LAYOUT slot.
1211 ;;; 2] We really want to use LIST to make list structures, instead of
1212 ;;;    MAKE-LIST/(SETF ELT).
1213 ;;; 3] STRUCTURE structures can have raw slots that must also be
1214 ;;;    allocated and indirectly referenced. We use SLOT-ACCESSOR-FORM
1215 ;;;    to compute how to set the slots, which deals with raw slots.
1216 ;;; 4] Funcallable structures are weird.
1217 (defun create-vector-constructor
1218        (defstruct cons-name arglist vars types values)
1219   (let ((temp (gensym))
1220         (etype (dd-element-type defstruct)))
1221     `(defun ,cons-name ,arglist
1222        (declare ,@(mapcar #'(lambda (var type) `(type (and ,type ,etype) ,var))
1223                           vars types))
1224        (let ((,temp (make-array ,(dd-length defstruct)
1225                                 :element-type ',(dd-element-type defstruct))))
1226          ,@(mapcar #'(lambda (x)
1227                        `(setf (aref ,temp ,(cdr x))  ',(car x)))
1228                    (find-name-indices defstruct))
1229          ,@(mapcar #'(lambda (dsd value)
1230                        `(setf (aref ,temp ,(dsd-index dsd)) ,value))
1231                    (dd-slots defstruct) values)
1232          ,temp))))
1233 (defun create-list-constructor
1234        (defstruct cons-name arglist vars types values)
1235   (let ((vals (make-list (dd-length defstruct) :initial-element nil)))
1236     (dolist (x (find-name-indices defstruct))
1237       (setf (elt vals (cdr x)) `',(car x)))
1238     (loop for dsd in (dd-slots defstruct) and val in values do
1239       (setf (elt vals (dsd-index dsd)) val))
1240
1241     `(defun ,cons-name ,arglist
1242        (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1243                           vars types))
1244        (list ,@vals))))
1245 (defun create-structure-constructor
1246        (defstruct cons-name arglist vars types values)
1247   (let* ((temp (gensym))
1248          (raw-index (dd-raw-index defstruct))
1249          (n-raw-data (when raw-index (gensym))))
1250     `(defun ,cons-name ,arglist
1251        (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1252                           vars types))
1253        (let ((,temp (truly-the ,(dd-name defstruct)
1254                                (%make-instance ,(dd-length defstruct))))
1255              ,@(when n-raw-data
1256                  `((,n-raw-data
1257                     (make-array ,(dd-raw-length defstruct)
1258                                 :element-type '(unsigned-byte 32))))))
1259          (setf (%instance-layout ,temp)
1260                (%delayed-get-compiler-layout ,(dd-name defstruct)))
1261          ,@(when n-raw-data
1262              `((setf (%instance-ref ,temp ,raw-index) ,n-raw-data)))
1263          ,@(mapcar (lambda (dsd value)
1264                      (multiple-value-bind (accessor index data)
1265                          (slot-accessor-form defstruct dsd temp n-raw-data)
1266                        `(setf (,accessor ,data ,index) ,value)))
1267                    (dd-slots defstruct)
1268                    values)
1269          ,temp))))
1270 (defun create-fin-constructor
1271        (defstruct cons-name arglist vars types values)
1272   (let ((temp (gensym)))
1273     `(defun ,cons-name ,arglist
1274        (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
1275                           vars types))
1276        (let ((,temp (truly-the
1277                      ,(dd-name defstruct)
1278                      (%make-funcallable-instance
1279                       ,(dd-length defstruct)
1280                       (%delayed-get-compiler-layout ,(dd-name defstruct))))))
1281          ,@(mapcar #'(lambda (dsd value)
1282                        `(setf (%funcallable-instance-info
1283                                ,temp ,(dsd-index dsd))
1284                               ,value))
1285                    (dd-slots defstruct) values)
1286          ,temp))))
1287
1288 ;;; Create a default (non-BOA) keyword constructor.
1289 (defun create-keyword-constructor (defstruct creator)
1290   (collect ((arglist (list '&key))
1291             (types)
1292             (vals))
1293     (dolist (slot (dd-slots defstruct))
1294       (let ((dum (gensym))
1295             (name (dsd-name slot)))
1296         (arglist `((,(keywordicate name) ,dum) ,(dsd-default slot)))
1297         (types (dsd-type slot))
1298         (vals dum)))
1299     (funcall creator
1300              defstruct (dd-default-constructor defstruct)
1301              (arglist) (vals) (types) (vals))))
1302
1303 ;;; Given a structure and a BOA constructor spec, call CREATOR with
1304 ;;; the appropriate args to make a constructor.
1305 (defun create-boa-constructor (defstruct boa creator)
1306   (multiple-value-bind (req opt restp rest keyp keys allowp aux)
1307       (sb!kernel:parse-lambda-list (second boa))
1308     (collect ((arglist)
1309               (vars)
1310               (types))
1311       (labels ((get-slot (name)
1312                  (let ((res (find name (dd-slots defstruct)
1313                                   :test #'string=
1314                                   :key #'dsd-name)))
1315                    (if res
1316                        (values (dsd-type res) (dsd-default res))
1317                        (values t nil))))
1318                (do-default (arg)
1319                  (multiple-value-bind (type default) (get-slot arg)
1320                    (arglist `(,arg ,default))
1321                    (vars arg)
1322                    (types type))))
1323         (dolist (arg req)
1324           (arglist arg)
1325           (vars arg)
1326           (types (get-slot arg)))
1327         
1328         (when opt
1329           (arglist '&optional)
1330           (dolist (arg opt)
1331             (cond ((consp arg)
1332                    (destructuring-bind
1333                        (name &optional (def (nth-value 1 (get-slot name))))
1334                        arg
1335                      (arglist `(,name ,def))
1336                      (vars name)
1337                      (types (get-slot name))))
1338                   (t
1339                    (do-default arg)))))
1340
1341         (when restp
1342           (arglist '&rest rest)
1343           (vars rest)
1344           (types 'list))
1345
1346         (when keyp
1347           (arglist '&key)
1348           (dolist (key keys)
1349             (if (consp key)
1350                 (destructuring-bind (wot &optional (def nil def-p)) key
1351                   (let ((name (if (consp wot)
1352                                   (destructuring-bind (key var) wot
1353                                     (declare (ignore key))
1354                                     var)
1355                                   wot)))
1356                     (multiple-value-bind (type slot-def) (get-slot name)
1357                       (arglist `(,wot ,(if def-p def slot-def)))
1358                       (vars name)
1359                       (types type))))
1360                 (do-default key))))
1361
1362         (when allowp (arglist '&allow-other-keys))
1363
1364         (when aux
1365           (arglist '&aux)
1366           (dolist (arg aux)
1367             (let* ((arg (if (consp arg) arg (list arg)))
1368                    (var (first arg)))
1369               (arglist arg)
1370               (vars var)
1371               (types (get-slot var))))))
1372
1373       (funcall creator defstruct (first boa)
1374                (arglist) (vars) (types)
1375                (mapcar #'(lambda (slot)
1376                            (or (find (dsd-name slot) (vars) :test #'string=)
1377                                (dsd-default slot)))
1378                        (dd-slots defstruct))))))
1379
1380 ;;; Grovel the constructor options, and decide what constructors (if
1381 ;;; any) to create.
1382 (defun constructor-definitions (defstruct)
1383   (let ((no-constructors nil)
1384         (boas ())
1385         (defaults ())
1386         (creator (ecase (dd-type defstruct)
1387                    (structure #'create-structure-constructor)
1388                    (funcallable-structure #'create-fin-constructor)
1389                    (vector #'create-vector-constructor)
1390                    (list #'create-list-constructor))))
1391     (dolist (constructor (dd-constructors defstruct))
1392       (destructuring-bind (name &optional (boa-ll nil boa-p)) constructor
1393         (declare (ignore boa-ll))
1394         (cond ((not name) (setq no-constructors t))
1395               (boa-p (push constructor boas))
1396               (t (push name defaults)))))
1397
1398     (when no-constructors
1399       (when (or defaults boas)
1400         (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs"))
1401       (return-from constructor-definitions ()))
1402
1403     (unless (or defaults boas)
1404       (push (symbolicate "MAKE-" (dd-name defstruct)) defaults))
1405
1406     (collect ((res))
1407       (when defaults
1408         (let ((cname (first defaults)))
1409           (setf (dd-default-constructor defstruct) cname)
1410           (res (create-keyword-constructor defstruct creator))
1411           (dolist (other-name (rest defaults))
1412             (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1413             (res `(declaim (ftype function ',other-name))))))
1414
1415       (dolist (boa boas)
1416         (res (create-boa-constructor defstruct boa creator)))
1417
1418       (res))))
1419 \f
1420 ;;;; compiler stuff
1421
1422 ;;; This is like PROCLAIM-AS-FUN-NAME, but we also set the kind to
1423 ;;; :DECLARED and blow away any ASSUMED-TYPE. Also, if the thing is a
1424 ;;; slot accessor currently, quietly unaccessorize it. And if there
1425 ;;; are any undefined warnings, we nuke them.
1426 (defun proclaim-as-defstruct-fun-name (name)
1427   (when name
1428     (when (info :function :accessor-for name)
1429       (setf (info :function :accessor-for name) nil))
1430     (proclaim-as-fun-name name)
1431     (note-name-defined name :function)
1432     (setf (info :function :where-from name) :declared)
1433     (when (info :function :assumed-type name)
1434       (setf (info :function :assumed-type name) nil)))
1435   (values))
1436 \f
1437 ;;;; finalizing bootstrapping
1438
1439 ;;; early structure placeholder definitions: Set up layout and class
1440 ;;; data for structures which are needed early.
1441 (dolist (args
1442          '#.(sb-cold:read-from-file
1443              "src/code/early-defstruct-args.lisp-expr"))
1444   (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
1445               (first args)
1446               (rest args)))
1447          (inherits (inherits-for-structure dd)))
1448     (%compiler-defstruct dd inherits)))
1449
1450 (/show0 "code/defstruct.lisp end of file")