Declare EXPLICIT-CHECK on CONCATENATE, MAKE-STRING, SET-PPRINT-DISPATCH.
[sbcl.git] / src / code / defbangstruct.lisp
blob9304d16f102bf23f5bb27b8b5284d135b9c1f690
1 ;;;; DEF!STRUCT = bootstrap DEFSTRUCT, a wrapper around DEFSTRUCT which
2 ;;;; provides special features to help at bootstrap time:
3 ;;;; 1. Layout information, inheritance information, and so forth is
4 ;;;; retained in such a way that we can get to it even on vanilla
5 ;;;; ANSI Common Lisp at cross-compiler build time.
6 ;;;; 2. MAKE-LOAD-FORM information is stored in such a way that we can
7 ;;;; get to it at bootstrap time before CLOS is built. This is
8 ;;;; important because at least as of sbcl-0.6.11.26, CLOS is built
9 ;;;; (compiled) after cold init, so we need to have the compiler
10 ;;;; even before CLOS runs.
12 ;;;; This software is part of the SBCL system. See the README file for
13 ;;;; more information.
14 ;;;;
15 ;;;; This software is derived from the CMU CL system, which was
16 ;;;; written at Carnegie Mellon University and released into the
17 ;;;; public domain. The software is in the public domain and is
18 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
19 ;;;; files for more information.
21 (in-package "SB!KERNEL")
23 ;;; A bootstrap MAKE-LOAD-FORM method can be a function or the name
24 ;;; of a function.
25 (deftype def!struct-type-make-load-form-fun () '(or function symbol))
27 ;;; a little single-inheritance system to keep track of MAKE-LOAD-FORM
28 ;;; information for DEF!STRUCT-defined types
29 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
31 ;; (DEF!STRUCT-SUPERTYPE TYPE) is the DEF!STRUCT-defined type that
32 ;; TYPE inherits from, or NIL if none.
33 (defvar *def!struct-supertype* (make-hash-table))
34 (defun def!struct-supertype (type)
35 (multiple-value-bind (value value-p) (gethash type *def!struct-supertype*)
36 (unless value-p
37 (error "~S is not a DEF!STRUCT-defined type." type))
38 value))
39 (defun (setf def!struct-supertype) (value type)
40 (when (and value #-sb-xc-host *type-system-initialized*)
41 (aver (subtypep value 'structure!object))
42 (aver (subtypep type value)))
43 (setf (gethash type *def!struct-supertype*) value))
45 ;; (DEF!STRUCT-TYPE-MAKE-LOAD-FORM-FUN TYPE) is the load form
46 ;; generator associated with the DEF!STRUCT-defined structure named
47 ;; TYPE, stored in a way which works independently of CLOS. The
48 ;; *DEF!STRUCT-TYPE-MAKE-LOAD-FORM-FUN* table is used to store the
49 ;; values. All types defined by DEF!STRUCT have an entry in the
50 ;; table; those with no MAKE-LOAD-FORM function have an explicit NIL
51 ;; entry.
52 (defvar *def!struct-type-make-load-form-fun* (make-hash-table))
53 (defun def!struct-type-make-load-form-fun (type)
54 (do ((supertype type))
55 (nil)
56 (multiple-value-bind (value value-p)
57 (gethash supertype *def!struct-type-make-load-form-fun*)
58 (unless value-p
59 (error "~S (supertype of ~S) is not a DEF!STRUCT-defined type."
60 supertype
61 type))
62 (when value
63 (return value))
64 (setf supertype (def!struct-supertype supertype))
65 (unless supertype
66 (error "There is no MAKE-LOAD-FORM function for bootstrap type ~S."
67 type)))))
68 (defun (setf def!struct-type-make-load-form-fun) (new-value type)
69 (when #+sb-xc-host t #-sb-xc-host *type-system-initialized*
70 (aver (subtypep type 'structure!object))
71 (aver (typep new-value 'def!struct-type-make-load-form-fun)))
72 (setf (gethash type *def!struct-type-make-load-form-fun*) new-value)))
74 ;;; the simplest, most vanilla MAKE-LOAD-FORM function for DEF!STRUCT
75 ;;; objects
76 ;;; If, in general, we could reverse-engineer that when a user-written
77 ;;; MAKE-LOAD-FORM was (MAKE-LOAD-FORM-SAVING-SLOTS ...) in an expected way,
78 ;;; we could obtain fasl-op-based dumping of user-defined structures.
79 ;;; At present we've no way to infer when the semantics of the Lisp code
80 ;;; produced by MAKE-LOAD-FORM-SAVING-SLOTS is effectively the same as
81 ;;; "dump the layout, dump the slots" (a/k/a :just-dump-it-normally).
82 ;;; Barring that, there's no choice but to compile code to recreate users'
83 ;;; constant structures. Otherwise we could nearly eliminate DEF!STRUCT too.
84 ;;; Consider its objectives:
85 ;;; - enable efficient structure dumping
86 ;;; - inform the cross-compiler of SBCL-style metadata using only ANSI Lisp
87 ;;; - doing it *before* the defining form has been seen during cross-compile
88 ;;; - (maybe) even during building of the XC
89 ;;; Of those goals, all but the last can be achieved by writing an ordinary
90 ;;; DEFSTRUCT, putting it early enough, and attaching a magic property to
91 ;;; the symbol naming your DEFSTRUCT. Witness that "backq.lisp" contains a
92 ;;; manipulatable dumpable defstruct, modulo a problem that would be fixed by
93 ;;; just delaying until after defsetfs is processed. backq is unique in that
94 ;;; no other defstruct has the distinction of preceding defsetfs.
95 (defun just-dump-it-normally (object &optional (env nil env-p))
96 (declare (type structure!object object))
97 (declare (ignorable env env-p object))
98 ;; KLUDGE: we require essentially three different behaviours of
99 ;; JUST-DUMP-IT-NORMALLY, two of which (host compiler's
100 ;; MAKE-LOAD-FORM, cross-compiler's MAKE-LOAD-FORM) are handled by
101 ;; the #+SB-XC-HOST clause. The #-SB-XC-HOST clause is the
102 ;; behaviour required by the target, before the CLOS-based
103 ;; MAKE-LOAD-FORM-SAVING-SLOTS is implemented.
104 #+sb-xc-host
105 (if env-p
106 (sb!xc:make-load-form-saving-slots object :environment env)
107 (sb!xc:make-load-form-saving-slots object))
108 #-sb-xc-host
109 :sb-just-dump-it-normally)
111 ;;; a MAKE-LOAD-FORM function for objects which don't use the load
112 ;;; form system. This is used for LAYOUT objects because the special
113 ;;; dumping requirements of LAYOUT objects are met by using special
114 ;;; VOPs which bypass the load form system. It's also used for various
115 ;;; compiler internal structures like nodes and VOP-INFO (FIXME:
116 ;;; Why?).
117 (defun ignore-it (object &optional env)
118 (declare (type structure!object object))
119 (declare (ignore object env))
120 ;; This magic tag is handled specially by the compiler downstream.
121 :ignore-it)
123 ;;; machinery used in the implementation of DEF!STRUCT
124 #+sb-xc-host
125 (eval-when (:compile-toplevel :load-toplevel :execute)
126 ;; a description of a DEF!STRUCT call to be stored until we get
127 ;; enough of the system running to finish processing it
128 (defstruct delayed-def!struct
129 (args (missing-arg) :type cons)
130 (package (sane-package) :type package))
131 ;; a list of DELAYED-DEF!STRUCTs stored until we get DEF!STRUCT
132 ;; working fully so that we can apply it to them then. After
133 ;; DEF!STRUCT is made to work fully, this list is processed, then
134 ;; made unbound, and should no longer be used.
135 (defvar *delayed-def!structs* nil))
136 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
137 ;; Parse the arguments for a DEF!STRUCT call, and return
138 ;; (VALUES NAME DEFSTRUCT-ARGS MAKE-LOAD-FORM-FUN DEF!STRUCT-SUPERTYPE),
139 ;; where NAME is the name of the new type, DEFSTRUCT-ARGS is the
140 ;; munged result suitable for passing on to DEFSTRUCT,
141 ;; MAKE-LOAD-FORM-FUN is the make load form function, or NIL if
142 ;; there's none, and DEF!STRUCT-SUPERTYPE is the direct supertype of
143 ;; the type if it is another DEF!STRUCT-defined type, or NIL
144 ;; otherwise.
145 (defun parse-def!struct-args (nameoid &rest rest)
146 (multiple-value-bind (name options) ; Note: OPTIONS can change below.
147 (if (consp nameoid)
148 (values (first nameoid) (rest nameoid))
149 (values nameoid nil))
150 (declare (type list options))
151 (let* ((include-clause (find :include options :key #'first))
152 (def!struct-supertype nil) ; may change below
153 (mlff-clause (find :make-load-form-fun options :key #'first))
154 (mlff (and mlff-clause (second mlff-clause))))
155 (when (find :type options :key #'first)
156 (error "can't use :TYPE option in DEF!STRUCT"))
157 (when mlff-clause
158 (setf options (remove mlff-clause options)))
159 (when include-clause
160 (setf def!struct-supertype (second include-clause)))
161 (if (eq name 'structure!object) ; if root of hierarchy
162 (aver (not include-clause))
163 (unless include-clause
164 (setf def!struct-supertype 'structure!object)
165 (push `(:include ,def!struct-supertype) options)))
166 (values name `((,name ,@options) ,@rest) mlff def!struct-supertype)))))
168 ;;; a helper function for DEF!STRUCT in the #+SB-XC-HOST case: Return
169 ;;; DEFSTRUCT-style arguments with any class names in the SB!XC
170 ;;; package (i.e. the name of the class being defined, and/or the
171 ;;; names of classes in :INCLUDE clauses) converted from SB!XC::FOO to
172 ;;; CL::FOO.
173 #+sb-xc-host
174 (eval-when (:compile-toplevel :load-toplevel :execute)
175 (defun uncross-defstruct-args (defstruct-args)
176 (destructuring-bind (name-and-options &rest slots-and-doc) defstruct-args
177 (multiple-value-bind (name options)
178 (if (symbolp name-and-options)
179 (values name-and-options nil)
180 (values (first name-and-options)
181 (rest name-and-options)))
182 (flet ((uncross-option (option)
183 (if (eq (first option) :include)
184 (destructuring-bind
185 (include-keyword included-name &rest rest)
186 option
187 `(,include-keyword
188 ,(uncross included-name)
189 ,@rest))
190 option)))
191 `((,(uncross name)
192 ,@(mapcar #'uncross-option options))
193 ,@slots-and-doc))))))
195 ;;; DEF!STRUCT's arguments are like DEFSTRUCT's arguments, except that
196 ;;; DEF!STRUCT accepts an extra optional :MAKE-LOAD-FORM-FUN clause.
197 ;;; DEF!STRUCT also does some magic to ensure that anything it defines
198 ;;; includes STRUCTURE!OBJECT, so that when CLOS is/becomes available,
199 ;;; we can hook the DEF!STRUCT system into
200 ;;; (DEFMETHOD MAKE-LOAD-FORM ((X STRUCTURE!OBJECT) &OPTIONAL ENV) ..)
201 ;;; and everything will continue to work.
202 (defmacro def!struct (&rest args)
203 (multiple-value-bind (name defstruct-args mlff def!struct-supertype)
204 (apply #'parse-def!struct-args args)
205 `(progn
206 ;; There are two valid cases here: creating the
207 ;; STRUCTURE!OBJECT root of the inheritance hierarchy, or
208 ;; inheriting from STRUCTURE!OBJECT somehow.
210 ;; The invalid case that we want to exclude is when an :INCLUDE
211 ;; clause was used, and the included class didn't inherit frmo
212 ;; STRUCTURE!OBJECT. We want to catch that error ASAP because
213 ;; otherwise the bug might lurk until someone tried to do
214 ;; MAKE-LOAD-FORM on an instance of the class.
215 ,@(if (eq name 'structure!object)
216 (aver (null def!struct-supertype))
217 `((aver (subtypep ',def!struct-supertype 'structure!object))))
218 (defstruct ,@defstruct-args)
219 (setf (def!struct-type-make-load-form-fun ',name)
220 ,(if (symbolp mlff)
221 `',mlff
222 mlff)
223 (def!struct-supertype ',name)
224 ',def!struct-supertype)
225 #+sb-xc-host ,(let ((u (uncross-defstruct-args defstruct-args)))
226 (if (boundp '*delayed-def!structs*)
227 `(push (make-delayed-def!struct :args ',u)
228 *delayed-def!structs*)
229 `(sb!xc:defstruct ,@u)))
230 ',name)))
232 ;;; When building the cross-compiler, this function has to be called
233 ;;; some time after SB!XC:DEFSTRUCT is set up, in order to take care
234 ;;; of any processing which had to be delayed until then.
235 #+sb-xc-host
236 (defun force-delayed-def!structs ()
237 (if (boundp '*delayed-def!structs*)
238 (progn
239 (mapcar (lambda (x)
240 (let ((*package* (delayed-def!struct-package x)))
241 ;; KLUDGE(?): EVAL is almost always the wrong thing.
242 ;; However, since we have to map DEFSTRUCT over the
243 ;; list, and since ANSI declined to specify any
244 ;; functional primitives corresponding to the
245 ;; DEFSTRUCT macro, it seems to me that EVAL is
246 ;; required in there somewhere..
247 (eval `(sb!xc:defstruct ,@(delayed-def!struct-args x)))))
248 (reverse *delayed-def!structs*))
249 ;; We shouldn't need this list any more. Making it unbound
250 ;; serves as a signal to DEF!STRUCT that it needn't delay
251 ;; DEF!STRUCTs any more. It is also generally a good thing for
252 ;; other reasons: it frees garbage, and it discourages anyone
253 ;; else from pushing anything else onto the list later.
254 (makunbound '*delayed-def!structs*))
255 ;; This condition is probably harmless if it comes up when
256 ;; interactively experimenting with the system by loading a source
257 ;; file into it more than once. But it's worth warning about it
258 ;; because it definitely shouldn't come up in an ordinary build
259 ;; process.
260 (warn "*DELAYED-DEF!STRUCTS* is already unbound.")))
262 ;;; The STRUCTURE!OBJECT abstract class is the base of the type
263 ;;; hierarchy for objects which have/use DEF!STRUCT functionality.
264 ;;; (The extra hackery in DEF!STRUCT-defined things isn't needed for
265 ;;; STRUCTURE-OBJECTs defined by ordinary, post-warm-init programs, so
266 ;;; it's only put into STRUCTURE-OBJECTs which inherit from
267 ;;; STRUCTURE!OBJECT.)
268 (def!struct (structure!object (:constructor nil) (:copier nil) (:predicate nil)))
270 ;;;; hooking this all into the standard MAKE-LOAD-FORM system
272 ;;; MAKE-LOAD-FORM for DEF!STRUCT-defined types
273 (defun structure!object-make-load-form (object &optional env)
274 (declare (ignore env))
275 (funcall (def!struct-type-make-load-form-fun (type-of object))
276 object))
278 ;;; Do the right thing at cold load time.
280 ;;; (Eventually this MAKE-LOAD-FORM function be overwritten by CLOS's
281 ;;; generic MAKE-LOAD-FORM, at which time a STRUCTURE!OBJECT method
282 ;;; should be added to call STRUCTURE!OBJECT-MAKE-LOAD-FORM.)
283 (setf (symbol-function 'sb!xc:make-load-form)
284 #'structure!object-make-load-form)
286 ;;; Do the right thing in the vanilla ANSI CLOS of the
287 ;;; cross-compilation host. (Something similar will have to be done in
288 ;;; our CLOS, too, but later, some time long after the toplevel forms
289 ;;; of this file have run.)
290 #+sb-xc-host
291 (defmethod make-load-form ((obj structure!object) &optional (env nil env-p))
292 (if env-p
293 (structure!object-make-load-form obj env)
294 (structure!object-make-load-form obj)))