1.0.29.24: preserve docstrings for local and anonymous functions
[sbcl.git] / src / compiler / globaldb.lisp
1 ;;;; This file provides a functional interface to global information
2 ;;;; about named things in the system. Information is considered to be
3 ;;;; global if it must persist between invocations of the compiler. The
4 ;;;; use of a functional interface eliminates the need for the compiler
5 ;;;; to worry about the actual representation. This is important, since
6 ;;;; the information may well have several representations.
7 ;;;;
8 ;;;; The database contains arbitrary Lisp values, addressed by a
9 ;;;; combination of Name, Class and Type. The Name is a EQUAL-thing
10 ;;;; which is the name of the thing we are recording information
11 ;;;; about. Class is the kind of object involved. Typical classes are
12 ;;;; :FUNCTION, :VARIABLE, :TYPE, ... A Type names a particular piece
13 ;;;; of information within a given class. Class and Type are keywords,
14 ;;;; and are compared with EQ.
15
16 ;;;; This software is part of the SBCL system. See the README file for
17 ;;;; more information.
18 ;;;;
19 ;;;; This software is derived from the CMU CL system, which was
20 ;;;; written at Carnegie Mellon University and released into the
21 ;;;; public domain. The software is in the public domain and is
22 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
23 ;;;; files for more information.
24
25 (in-package "SB!C")
26
27 (!begin-collecting-cold-init-forms)
28 #!+sb-show (!cold-init-forms (/show0 "early in globaldb.lisp cold init"))
29
30 ;;; The DEFVAR for this appears later.
31 ;;; FIXME: centralize
32 (declaim (special *universal-type*))
33
34 ;;; This is sorta semantically equivalent to SXHASH, but optimized for
35 ;;; legal function names.
36 ;;;
37 ;;; Why optimize? We want to avoid the fully-general TYPECASE in ordinary
38 ;;; SXHASH, because
39 ;;;   1. This hash function has to run when we're initializing the globaldb,
40 ;;;      so it has to run before the type system is initialized, and it's
41 ;;;      easier to make it do this if we don't try to do a general TYPECASE.
42 ;;;   2. This function is in a potential bottleneck for the compiler,
43 ;;;      and avoiding the general TYPECASE lets us improve performance
44 ;;;      because
45 ;;;     2a. the general TYPECASE is intrinsically slow, and
46 ;;;     2b. the general TYPECASE is too big for us to easily afford
47 ;;;         to inline it, so it brings with it a full function call.
48 ;;;
49 ;;; Why not specialize instead of optimize? (I.e. why fall through to
50 ;;; general SXHASH as a last resort?) Because the INFO database is used
51 ;;; to hold all manner of things, e.g. (INFO :TYPE :BUILTIN ..)
52 ;;; which is called on values like (UNSIGNED-BYTE 29). Falling through
53 ;;; to SXHASH lets us support all manner of things (as long as they
54 ;;; aren't used too early in cold boot for SXHASH to run).
55 #!-sb-fluid (declaim (inline globaldb-sxhashoid))
56 (defun globaldb-sxhashoid (x)
57   (logand sb!xc:most-positive-fixnum
58           (cond ((symbolp x) (sxhash x))
59                 ((and (listp x)
60                       (eq (first x) 'setf)
61                       (let ((rest (rest x)))
62                         (and (symbolp (car rest))
63                              (null (cdr rest)))))
64                  ;; We need to declare the type of the value we're feeding to
65                  ;; SXHASH so that the DEFTRANSFORM on symbols kicks in.
66                  (let ((symbol (second x)))
67                    (declare (symbol symbol))
68                    (logxor (sxhash symbol) 110680597)))
69                 (t (sxhash x)))))
70
71 ;;; Given any non-negative integer, return a prime number >= to it.
72 ;;;
73 ;;; FIXME: This logic should be shared with ALMOST-PRIMIFY in
74 ;;; hash-table.lisp. Perhaps the merged logic should be
75 ;;; PRIMIFY-HASH-TABLE-SIZE, implemented as a lookup table of primes
76 ;;; after integral powers of two:
77 ;;;    #(17 37 67 131 ..)
78 ;;; (Or, if that's too coarse, after half-integral powers of two.) By
79 ;;; thus getting rid of any need for primality testing at runtime, we
80 ;;; could punt POSITIVE-PRIMEP, too.
81 (defun primify (x)
82   (declare (type unsigned-byte x))
83   (do ((n (logior x 1) (+ n 2)))
84       ((positive-primep n) n)))
85 \f
86 ;;;; info classes, info types, and type numbers, part I: what's needed
87 ;;;; not only at compile time but also at run time
88
89 ;;;; Note: This section is a blast from the past, a little trip down
90 ;;;; memory lane to revisit the weird host/target interactions of the
91 ;;;; CMU CL build process. Because of the way that the cross-compiler
92 ;;;; and target compiler share stuff here, if you change anything in
93 ;;;; here, you'd be well-advised to nuke all your fasl files and
94 ;;;; restart compilation from the very beginning of the bootstrap
95 ;;;; process.
96
97 ;;; At run time, we represent the type of info that we want by a small
98 ;;; non-negative integer.
99 (eval-when (:compile-toplevel :load-toplevel :execute)
100   (def!constant type-number-bits 6))
101 (deftype type-number () `(unsigned-byte ,type-number-bits))
102
103 ;;; Why do we suppress the :COMPILE-TOPLEVEL situation here when we're
104 ;;; running the cross-compiler? The cross-compiler (which was built
105 ;;; from these sources) has its version of these data and functions
106 ;;; defined in the same places we'd be defining into. We're happy with
107 ;;; its version, since it was compiled from the same sources, so
108 ;;; there's no point in overwriting its nice compiled version of this
109 ;;; stuff with our interpreted version. (And any time we're *not*
110 ;;; happy with its version, perhaps because we've been editing the
111 ;;; sources partway through bootstrapping, tch tch, overwriting its
112 ;;; version with our version would be unlikely to help, because that
113 ;;; would make the cross-compiler very confused.)
114 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
115
116 (defstruct (class-info
117             (:constructor make-class-info (name))
118             #-no-ansi-print-object
119             (:print-object (lambda (x s)
120                              (print-unreadable-object (x s :type t)
121                                (prin1 (class-info-name x)))))
122             (:copier nil))
123   ;; name of this class
124   (name nil :type keyword :read-only t)
125   ;; list of Type-Info structures for each type in this class
126   (types () :type list))
127
128 ;;; a map from type numbers to TYPE-INFO objects. There is one type
129 ;;; number for each defined CLASS/TYPE pair.
130 ;;;
131 ;;; We build its value at build-the-cross-compiler time (with calls to
132 ;;; DEFINE-INFO-TYPE), then generate code to recreate the compile time
133 ;;; value, and arrange for that code to be called in cold load.
134 ;;; KLUDGE: We don't try to reset its value when cross-compiling the
135 ;;; compiler, since that creates too many bootstrapping problems,
136 ;;; instead just reusing the built-in-the-cross-compiler version,
137 ;;; which is theoretically a little bit ugly but pretty safe in
138 ;;; practice because the cross-compiler is as close to the target
139 ;;; compiler as we can make it, i.e. identical in most ways, including
140 ;;; this one. -- WHN 2001-08-19
141 (defvar *info-types*)
142 (declaim (type simple-vector *info-types*))
143 #-sb-xc ; as per KLUDGE note above
144 (eval-when (:compile-toplevel :execute)
145   (setf *info-types*
146         (make-array (ash 1 type-number-bits) :initial-element nil)))
147
148 (defstruct (type-info
149             #-no-ansi-print-object
150             (:print-object (lambda (x s)
151                              (print-unreadable-object (x s)
152                                (format s
153                                        "~S ~S, Number = ~W"
154                                        (class-info-name (type-info-class x))
155                                        (type-info-name x)
156                                        (type-info-number x)))))
157             (:copier nil))
158   ;; the name of this type
159   (name (missing-arg) :type keyword)
160   ;; this type's class
161   (class (missing-arg) :type class-info)
162   ;; a number that uniquely identifies this type (and implicitly its class)
163   (number (missing-arg) :type type-number)
164   ;; a type specifier which info of this type must satisfy
165   (type nil :type t)
166   ;; a function called when there is no information of this type
167   (default (lambda () (error "type not defined yet")) :type function)
168   ;; called by (SETF INFO) before calling SET-INFO-VALUE
169   (validate-function nil :type (or function null)))
170
171 ;;; a map from class names to CLASS-INFO structures
172 ;;;
173 ;;; We build the value for this at compile time (with calls to
174 ;;; DEFINE-INFO-CLASS), then generate code to recreate the compile time
175 ;;; value, and arrange for that code to be called in cold load.
176 ;;; KLUDGE: Just as for *INFO-TYPES*, we don't try to rebuild this
177 ;;; when cross-compiling, but instead just reuse the cross-compiler's
178 ;;; version for the target compiler. -- WHN 2001-08-19
179 (defvar *info-classes*)
180 (declaim (hash-table *info-classes*))
181 #-sb-xc ; as per KLUDGE note above
182 (eval-when (:compile-toplevel :execute)
183   (setf *info-classes* (make-hash-table :test #'eq)))
184
185 ;;; If NAME is the name of a type in CLASS, then return the TYPE-INFO,
186 ;;; otherwise NIL.
187 (defun find-type-info (name class)
188   (declare (type keyword name) (type class-info class))
189   (dolist (type (class-info-types class) nil)
190     (when (eq (type-info-name type) name)
191       (return type))))
192
193 ;;; Return the info structure for an info class or type, or die trying.
194 (declaim (ftype (function (keyword) class-info) class-info-or-lose))
195 (defun class-info-or-lose (class)
196   (declare (type keyword class))
197   #+sb-xc (/noshow0 "entering CLASS-INFO-OR-LOSE, CLASS=..")
198   #+sb-xc (/nohexstr class)
199   (prog1
200       (flet ((lookup (class)
201                (or (gethash class *info-classes*)
202                    (error "~S is not a defined info class." class))))
203         (if (symbolp class)
204             (or (get class 'class-info-or-lose-cache)
205                 (setf (get class 'class-info-or-lose-cache)
206                       (lookup class)))
207             (lookup class)))
208     #+sb-xc (/noshow0 "returning from CLASS-INFO-OR-LOSE")))
209 (declaim (ftype (function (keyword keyword) type-info) type-info-or-lose))
210 (defun type-info-or-lose (class type)
211   #+sb-xc (/noshow0 "entering TYPE-INFO-OR-LOSE, CLASS,TYPE=..")
212   #+sb-xc (/nohexstr class)
213   #+sb-xc (/nohexstr type)
214   (prog1
215       (or (find-type-info type (class-info-or-lose class))
216           (error "~S is not a defined info type." type))
217     #+sb-xc (/noshow0 "returning from TYPE-INFO-OR-LOSE")))
218
219 ) ; EVAL-WHEN
220 \f
221 ;;;; info classes, info types, and type numbers, part II: what's
222 ;;;; needed only at compile time, not at run time
223
224 ;;; FIXME: Perhaps this stuff (the definition of DEFINE-INFO-CLASS
225 ;;; and the calls to it) could/should go in a separate file,
226 ;;; perhaps info-classes.lisp?
227
228 (eval-when (:compile-toplevel :execute)
229
230 ;;; Set up the data structures to support an info class.
231 ;;;
232 ;;; comment from CMU CL:
233 ;;;   We make sure that the class exists at compile time so that
234 ;;;   macros can use it, but we don't actually store the init function
235 ;;;   until load time so that we don't break the running compiler.
236 ;;; KLUDGE: I don't think that's the way it is any more, but I haven't
237 ;;; looked into it enough to write a better comment. -- WHN 2001-03-06
238 (#+sb-xc-host defmacro
239  #-sb-xc-host sb!xc:defmacro
240      define-info-class (class)
241   (declare (type keyword class))
242   `(progn
243      ;; (We don't need to evaluate this at load time, compile time is
244      ;; enough. There's special logic elsewhere which deals with cold
245      ;; load initialization by inspecting the info class data
246      ;; structures at compile time and generating code to recreate
247      ;; those data structures.)
248      (eval-when (:compile-toplevel :execute)
249        (unless (gethash ,class *info-classes*)
250          (setf (gethash ,class *info-classes*) (make-class-info ,class))))
251      ,class))
252
253 ;;; Find a type number not already in use by looking for a null entry
254 ;;; in *INFO-TYPES*.
255 (defun find-unused-type-number ()
256   (or (position nil *info-types*)
257       (error "no more INFO type numbers available")))
258
259 ;;; a list of forms for initializing the DEFAULT slots of TYPE-INFO
260 ;;; objects, accumulated during compilation and eventually converted
261 ;;; into a function to be called at cold load time after the
262 ;;; appropriate TYPE-INFO objects have been created
263 ;;;
264 ;;; Note: This is quite similar to the !COLD-INIT-FORMS machinery, but
265 ;;; we can't conveniently use the ordinary !COLD-INIT-FORMS machinery
266 ;;; here. The problem is that the natural order in which the
267 ;;; default-slot-initialization forms are generated relative to the
268 ;;; order in which the TYPE-INFO-creation forms are generated doesn't
269 ;;; match the relative order in which the forms need to be executed at
270 ;;; cold load time.
271 (defparameter *!reversed-type-info-init-forms* nil)
272
273 ;;; Define a new type of global information for CLASS. TYPE is the
274 ;;; name of the type, DEFAULT is the value for that type when it
275 ;;; hasn't been set, and TYPE-SPEC is a type specifier which values of
276 ;;; the type must satisfy. The default expression is evaluated each
277 ;;; time the information is needed, with NAME bound to the name for
278 ;;; which the information is being looked up.
279 ;;;
280 ;;; The main thing we do is determine the type's number. We need to do
281 ;;; this at macroexpansion time, since both the COMPILE and LOAD time
282 ;;; calls to %DEFINE-INFO-TYPE must use the same type number.
283 (#+sb-xc-host defmacro
284  #-sb-xc-host sb!xc:defmacro
285     define-info-type (&key (class (missing-arg))
286                            (type (missing-arg))
287                            (type-spec (missing-arg))
288                            (validate-function)
289                            default)
290   (declare (type keyword class type))
291   `(progn
292      (eval-when (:compile-toplevel :execute)
293        ;; At compile time, ensure that the type number exists. It will
294        ;; need to be forced to exist at cold load time, too, but
295        ;; that's not handled here; it's handled by later code which
296        ;; looks at the compile time state and generates code to
297        ;; replicate it at cold load time.
298        (let* ((class-info (class-info-or-lose ',class))
299               (old-type-info (find-type-info ',type class-info)))
300          (unless old-type-info
301            (let* ((new-type-number (find-unused-type-number))
302                   (new-type-info
303                    (make-type-info :name ',type
304                                    :class class-info
305                                    :number new-type-number
306                                    :type ',type-spec)))
307              (setf (aref *info-types* new-type-number) new-type-info)
308              (push new-type-info (class-info-types class-info)))))
309        ;; Arrange for TYPE-INFO-DEFAULT and
310        ;; TYPE-INFO-VALIDATE-FUNCTION to be set at cold load
311        ;; time. (They can't very well be set at cross-compile time,
312        ;; since they differ between host and target and are
313        ;; host-compiled closures.)
314        (push `(let ((type-info (type-info-or-lose ,',class ,',type)))
315                 (setf (type-info-validate-function type-info)
316                       ,',validate-function)
317                 (setf (type-info-default type-info)
318                        ;; FIXME: This code is sort of nasty. It would
319                        ;; be cleaner if DEFAULT accepted a real
320                        ;; function, instead of accepting a statement
321                        ;; which will be turned into a lambda assuming
322                        ;; that the argument name is NAME. It might
323                        ;; even be more microefficient, too, since many
324                        ;; DEFAULTs could be implemented as (CONSTANTLY
325                        ;; NIL) instead of full-blown (LAMBDA (X) NIL).
326                        (lambda (name)
327                          (declare (ignorable name))
328                          ,',default)))
329              *!reversed-type-info-init-forms*))
330      ',type))
331
332 ) ; EVAL-WHEN
333 \f
334 ;;;; generic info environments
335
336 (defstruct (info-env (:constructor nil)
337                      (:copier nil))
338   ;; some string describing what is in this environment, for
339   ;; printing/debugging purposes only
340   (name (missing-arg) :type string))
341 (def!method print-object ((x info-env) stream)
342   (print-unreadable-object (x stream :type t)
343     (prin1 (info-env-name x) stream)))
344 \f
345 ;;;; generic interfaces
346
347 ;;; FIXME: used only in this file, needn't be in runtime
348 (defmacro do-info ((env &key (name (gensym)) (class (gensym)) (type (gensym))
349                         (type-number (gensym)) (value (gensym)) known-volatile)
350                    &body body)
351   #!+sb-doc
352   "DO-INFO (Env &Key Name Class Type Value) Form*
353   Iterate over all the values stored in the Info-Env Env. Name is bound to
354   the entry's name, Class and Type are bound to the class and type
355   (represented as keywords), and Value is bound to the entry's value."
356   (once-only ((n-env env))
357     (if known-volatile
358         (do-volatile-info name class type type-number value n-env body)
359         `(if (typep ,n-env 'volatile-info-env)
360              ,(do-volatile-info name class type type-number value n-env body)
361              ,(do-compact-info name class type type-number value
362                                n-env body)))))
363
364 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
365
366 ;;; Return code to iterate over a compact info environment.
367 (defun do-compact-info (name-var class-var type-var type-number-var value-var
368                                  n-env body)
369   (let ((n-index (gensym))
370         (n-type (gensym))
371         (punt (gensym)))
372     (once-only ((n-table `(compact-info-env-table ,n-env))
373                 (n-entries-index `(compact-info-env-index ,n-env))
374                 (n-entries `(compact-info-env-entries ,n-env))
375                 (n-entries-info `(compact-info-env-entries-info ,n-env))
376                 (n-info-types '*info-types*))
377       `(dotimes (,n-index (length ,n-table))
378          (declare (type index ,n-index))
379          (block ,punt
380            (let ((,name-var (svref ,n-table ,n-index)))
381              (unless (eql ,name-var 0)
382                (do-anonymous ((,n-type (aref ,n-entries-index ,n-index)
383                                        (1+ ,n-type)))
384                              (nil)
385                  (declare (type index ,n-type))
386                  ,(once-only ((n-info `(aref ,n-entries-info ,n-type)))
387                     `(let ((,type-number-var
388                             (logand ,n-info compact-info-entry-type-mask)))
389                        ,(once-only ((n-type-info
390                                      `(svref ,n-info-types
391                                              ,type-number-var)))
392                           `(let ((,type-var (type-info-name ,n-type-info))
393                                  (,class-var (class-info-name
394                                               (type-info-class ,n-type-info)))
395                                  (,value-var (svref ,n-entries ,n-type)))
396                              (declare (ignorable ,type-var ,class-var
397                                                  ,value-var))
398                              ,@body
399                              (unless (zerop (logand ,n-info
400                                                     compact-info-entry-last))
401                                (return-from ,punt))))))))))))))
402
403 ;;; Return code to iterate over a volatile info environment.
404 (defun do-volatile-info (name-var class-var type-var type-number-var value-var
405                                   n-env body)
406   (let ((n-index (gensym)) (n-names (gensym)) (n-types (gensym)))
407     (once-only ((n-table `(volatile-info-env-table ,n-env))
408                 (n-info-types '*info-types*))
409       `(dotimes (,n-index (length ,n-table))
410          (declare (type index ,n-index))
411          (do-anonymous ((,n-names (svref ,n-table ,n-index)
412                                   (cdr ,n-names)))
413                        ((null ,n-names))
414            (let ((,name-var (caar ,n-names)))
415              (declare (ignorable ,name-var))
416              (do-anonymous ((,n-types (cdar ,n-names) (cdr ,n-types)))
417                            ((null ,n-types))
418                (let ((,type-number-var (caar ,n-types)))
419                  ,(once-only ((n-type `(svref ,n-info-types
420                                               ,type-number-var)))
421                     `(let ((,type-var (type-info-name ,n-type))
422                            (,class-var (class-info-name
423                                         (type-info-class ,n-type)))
424                            (,value-var (cdar ,n-types)))
425                        (declare (ignorable ,type-var ,class-var ,value-var))
426                        ,@body))))))))))
427
428 ) ; EVAL-WHEN
429 \f
430
431 ;;;; compact info environments
432
433 ;;; The upper limit on the size of the ENTRIES vector in a COMPACT-INFO-ENV.
434 ;;;
435 ;;; "Why (U-B 28)?", you might wonder. Originally this was (U-B 16),
436 ;;; presumably to ensure that the arrays of :ELEMENT-TYPE
437 ;;; COMPACT-INFO-ENTRIES-INDEX could use a more space-efficient representation.
438 ;;; It turns out that a environment of of only 65536 entries is insufficient in
439 ;;; the modern world (see message from Cyrus Harmon to sbcl-devel, "Subject:
440 ;;; purify failure when compact-info-env-entries-bits is too small"). Using
441 ;;; (U-B 28) instead of (U-B 29) is to avoid the need for bignum overflow
442 ;;; checks, a probably pointless micro-optimization. Hardcoding the amount of
443 ;;; bits instead of deriving it from SB!VM::N-WORD-BITS is done to allow
444 ;;; use of a more efficient array representation on 64-bit platforms.
445 ;;;   -- JES, 2005-04-06
446 (def!constant compact-info-env-entries-bits 28)
447 (deftype compact-info-entries-index () `(unsigned-byte ,compact-info-env-entries-bits))
448
449 ;;; the type of the values in COMPACT-INFO-ENTRIES-INFO
450 (deftype compact-info-entry () `(unsigned-byte ,(1+ type-number-bits)))
451
452 ;;; This is an open hashtable with rehashing. Since modification is
453 ;;; not allowed, we don't have to worry about deleted entries. We
454 ;;; indirect through a parallel vector to find the index in the
455 ;;; ENTRIES at which the entries for a given name starts.
456 (defstruct (compact-info-env (:include info-env)
457                              #-sb-xc-host (:pure :substructure)
458                              (:copier nil))
459   ;; hashtable of the names in this environment. If a bucket is
460   ;; unused, it is 0.
461   (table (missing-arg) :type simple-vector)
462   ;; an indirection vector parallel to TABLE, translating indices in
463   ;; TABLE to the start of the ENTRIES for that name. Unused entries
464   ;; are undefined.
465   (index (missing-arg) :type (simple-array compact-info-entries-index (*)))
466   ;; a vector contining in contiguous ranges the values of for all the
467   ;; types of info for each name.
468   (entries (missing-arg) :type simple-vector)
469   ;; a vector parallel to ENTRIES, indicating the type number for the
470   ;; value stored in that location and whether this location is the
471   ;; last type of info stored for this name. The type number is in the
472   ;; low TYPE-NUMBER-BITS bits, and the next bit is set if this is the
473   ;; last entry.
474   (entries-info (missing-arg) :type (simple-array compact-info-entry (*))))
475
476 (def!constant compact-info-entry-type-mask (ldb (byte type-number-bits 0) -1))
477 (def!constant compact-info-entry-last (ash 1 type-number-bits))
478
479 ;;; Return the value of the type corresponding to NUMBER for the
480 ;;; index INDEX in ENV.
481 #!-sb-fluid (declaim (inline compact-info-lookup-index))
482 (defun compact-info-lookup-index (env number index)
483   (declare (type compact-info-env env) (type type-number number))
484   (let ((entries-info (compact-info-env-entries-info env)))
485     (if index
486         (do ((index index (1+ index)))
487             (nil)
488           (declare (type index index))
489           (let ((info (aref entries-info index)))
490             (when (= (logand info compact-info-entry-type-mask) number)
491               (return (values (svref (compact-info-env-entries env) index)
492                               t)))
493             (unless (zerop (logand compact-info-entry-last info))
494               (return (values nil nil)))))
495         (values nil nil))))
496
497 ;;; Look up NAME in the compact environment ENV. HASH is the
498 ;;; GLOBALDB-SXHASHOID of NAME.
499 (defun compact-info-lookup (env name hash number)
500   (declare (type compact-info-env env)
501            (type (integer 0 #.sb!xc:most-positive-fixnum) hash))
502   (let* ((table (compact-info-env-table env))
503          (len (length table))
504          (len-2 (- len 2))
505          (hash2 (- len-2 (rem hash len-2))))
506     (declare (type index len-2 hash2))
507     (macrolet ((lookup (test)
508                  `(do ((probe (rem hash len)
509                               (let ((new (+ probe hash2)))
510                                 (declare (type index new))
511                                 ;; same as (MOD NEW LEN), but faster.
512                                 (if (>= new len)
513                                     (the index (- new len))
514                                     new))))
515                       (nil)
516                     (let ((entry (svref table probe)))
517                       (when (eql entry 0)
518                         (return nil))
519                       (when (,test entry name)
520                         (return (compact-info-lookup-index
521                                  env
522                                  number
523                                  (aref (compact-info-env-index env) probe))))))))
524       (if (symbolp name)
525           (lookup eq)
526           (lookup equal)))))
527
528 ;;; the exact density (modulo rounding) of the hashtable in a compact
529 ;;; info environment in names/bucket
530 (def!constant compact-info-environment-density 65)
531
532 ;;; Return a new compact info environment that holds the same
533 ;;; information as ENV.
534 (defun compact-info-environment (env &key (name (info-env-name env)))
535   (let ((name-count 0)
536         (prev-name 0)
537         (entry-count 0))
538     (/show0 "before COLLECT in COMPACT-INFO-ENVIRONMENT")
539
540     ;; Iterate over the environment once to find out how many names
541     ;; and entries it has, then build the result. This code assumes
542     ;; that all the entries for a name well be iterated over
543     ;; contiguously, which holds true for the implementation of
544     ;; iteration over both kinds of environments.
545     (collect ((names))
546
547       (/show0 "at head of COLLECT in COMPACT-INFO-ENVIRONMENT")
548       (let ((types ()))
549         (do-info (env :name name :type-number num :value value)
550           (/noshow0 "at head of DO-INFO in COMPACT-INFO-ENVIRONMENT")
551           (unless (eq name prev-name)
552             (/noshow0 "not (EQ NAME PREV-NAME) case")
553             (incf name-count)
554             (unless (eql prev-name 0)
555               (names (cons prev-name types)))
556             (setq prev-name name)
557             (setq types ()))
558           (incf entry-count)
559           (push (cons num value) types))
560         (unless (eql prev-name 0)
561           (/show0 "not (EQL PREV-NAME 0) case")
562           (names (cons prev-name types))))
563
564       ;; Now that we know how big the environment is, we can build
565       ;; a table to represent it.
566       ;;
567       ;; When building the table, we sort the entries by pointer
568       ;; comparison in an attempt to preserve any VM locality present
569       ;; in the original load order, rather than randomizing with the
570       ;; original hash function.
571       (/show0 "about to make/sort vectors in COMPACT-INFO-ENVIRONMENT")
572       (let* ((table-size (primify
573                           (+ (truncate (* name-count 100)
574                                        compact-info-environment-density)
575                              3)))
576              (table (make-array table-size :initial-element 0))
577              (index (make-array table-size
578                                 :element-type 'compact-info-entries-index))
579              (entries (make-array entry-count))
580              (entries-info (make-array entry-count
581                                        :element-type 'compact-info-entry))
582              (sorted (sort (names)
583                            #+sb-xc-host #'<
584                            ;; POINTER-HASH hack implements pointer
585                            ;; comparison, as explained above.
586                            #-sb-xc-host (lambda (x y)
587                                           (< (pointer-hash x)
588                                              (pointer-hash y))))))
589         (/show0 "done making/sorting vectors in COMPACT-INFO-ENVIRONMENT")
590         (let ((entries-idx 0))
591           (dolist (types sorted)
592             (let* ((name (first types))
593                    (hash (globaldb-sxhashoid name))
594                    (len-2 (- table-size 2))
595                    (hash2 (- len-2 (rem hash len-2))))
596               (do ((probe (rem hash table-size)
597                           (rem (+ probe hash2) table-size)))
598                   (nil)
599                 (let ((entry (svref table probe)))
600                   (when (eql entry 0)
601                     (setf (svref table probe) name)
602                     (setf (aref index probe) entries-idx)
603                     (return))
604                   (aver (not (equal entry name))))))
605
606             (unless (zerop entries-idx)
607               (setf (aref entries-info (1- entries-idx))
608                     (logior (aref entries-info (1- entries-idx))
609                             compact-info-entry-last)))
610
611             (loop for (num . value) in (rest types) do
612               (setf (aref entries-info entries-idx) num)
613               (setf (aref entries entries-idx) value)
614               (incf entries-idx)))
615           (/show0 "done w/ DOLIST (TYPES SORTED) in COMPACT-INFO-ENVIRONMENT")
616
617           (unless (zerop entry-count)
618             (/show0 "nonZEROP ENTRY-COUNT")
619             (setf (aref entries-info (1- entry-count))
620                   (logior (aref entries-info (1- entry-count))
621                           compact-info-entry-last)))
622
623           (/show0 "falling through to MAKE-COMPACT-INFO-ENV")
624           (make-compact-info-env :name name
625                                  :table table
626                                  :index index
627                                  :entries entries
628                                  :entries-info entries-info))))))
629 \f
630 ;;;; volatile environments
631
632 ;;; This is a closed hashtable, with the bucket being computed by
633 ;;; taking the GLOBALDB-SXHASHOID of the NAME modulo the table size.
634 (defstruct (volatile-info-env (:include info-env)
635                               (:copier nil))
636   ;; vector of alists of alists of the form:
637   ;;    ((Name . ((Type-Number . Value) ...) ...)
638   (table (missing-arg) :type simple-vector)
639   ;; the number of distinct names currently in this table. Each name
640   ;; may have multiple entries, since there can be many types of info.
641   (count 0 :type index)
642   ;; the number of names at which we should grow the table and rehash
643   (threshold 0 :type index))
644
645 ;;; Just like COMPACT-INFO-LOOKUP, only do it on a volatile environment.
646 (defun volatile-info-lookup (env name hash number)
647   (declare (type volatile-info-env env)
648            (type (integer 0 #.sb!xc:most-positive-fixnum) hash))
649   (let ((table (volatile-info-env-table env)))
650     (macrolet ((lookup (test)
651                  `(dolist (entry (svref table (mod hash (length table))) ())
652                     (when (,test (car entry) name)
653                       (dolist (type (cdr entry))
654                         (when (eql (car type) number)
655                           (return-from volatile-info-lookup
656                             (values (cdr type) t))))
657                       (return-from volatile-info-lookup
658                         (values nil nil))))))
659       (if (symbolp name)
660           (lookup eq)
661           (lookup equal)))))
662
663 ;;; Given a volatile environment ENV, bind TABLE-VAR the environment's table
664 ;;; and INDEX-VAR to the index of NAME's bucket in the table.
665 (eval-when (:compile-toplevel :execute)
666   (#+sb-xc-host cl:defmacro
667    #-sb-xc-host sb!xc:defmacro
668       with-info-bucket ((table-var index-var name env) &body body)
669     (once-only ((n-name name)
670                 (n-env env))
671       `(progn
672          (let* ((,table-var (volatile-info-env-table ,n-env))
673                 (,index-var (mod (globaldb-sxhashoid ,n-name)
674                                  (length ,table-var))))
675            ,@body)))))
676
677 ;;; Get the info environment that we use for write/modification operations.
678 ;;; This is always the first environment in the list, and must be a
679 ;;; VOLATILE-INFO-ENV.
680 #!-sb-fluid (declaim (inline get-write-info-env))
681 (defun get-write-info-env (&optional (env-list *info-environment*))
682   (let ((env (car env-list)))
683     (unless env
684       (error "no info environment?"))
685     (unless (typep env 'volatile-info-env)
686       (error "cannot modify this environment: ~S" env))
687     (the volatile-info-env env)))
688
689 ;;; If Name is already present in the table, then just create or
690 ;;; modify the specified type. Otherwise, add the new name and type,
691 ;;; checking for rehashing.
692 ;;;
693 ;;; We rehash by making a new larger environment, copying all of the
694 ;;; entries into it, then clobbering the old environment with the new
695 ;;; environment's table. We clear the old table to prevent it from
696 ;;; holding onto garbage if it is statically allocated.
697 ;;;
698 ;;; We return the new value so that this can be conveniently used in a
699 ;;; SETF function.
700 (defun set-info-value (name0 type new-value
701                              &optional (env (get-write-info-env)))
702   (declare (type type-number type) (type volatile-info-env env)
703            (inline assoc))
704   (let ((name (uncross name0)))
705     (when (eql name 0)
706       (error "0 is not a legal INFO name."))
707     (with-info-bucket (table index name env)
708       (let ((types (if (symbolp name)
709                        (assoc name (svref table index) :test #'eq)
710                        (assoc name (svref table index) :test #'equal))))
711         (cond
712          (types
713           (let ((value (assoc type (cdr types))))
714             (if value
715                 (setf (cdr value) new-value)
716                 (push (cons type new-value) (cdr types)))))
717          (t
718           (push (cons name (list (cons type new-value)))
719                 (svref table index))
720
721           (let ((count (incf (volatile-info-env-count env))))
722             (when (>= count (volatile-info-env-threshold env))
723               (let ((new (make-info-environment :size (* count 2))))
724                 (do-info (env :name entry-name :type-number entry-num
725                               :value entry-val :known-volatile t)
726                          (set-info-value entry-name entry-num entry-val new))
727                 (fill (volatile-info-env-table env) nil)
728                 (setf (volatile-info-env-table env)
729                       (volatile-info-env-table new))
730                 (setf (volatile-info-env-threshold env)
731                       (volatile-info-env-threshold new)))))))))
732     new-value))
733
734 ;;; FIXME: It should be possible to eliminate the hairy compiler macros below
735 ;;; by declaring INFO and (SETF INFO) inline and making a simple compiler macro
736 ;;; for TYPE-INFO-OR-LOSE. (If we didn't worry about efficiency of the
737 ;;; cross-compiler, we could even do it by just making TYPE-INFO-OR-LOSE
738 ;;; foldable.)
739
740 ;;; INFO is the standard way to access the database. It's settable.
741 ;;;
742 ;;; Return the information of the specified TYPE and CLASS for NAME.
743 ;;; The second value returned is true if there is any such information
744 ;;; recorded. If there is no information, the first value returned is
745 ;;; the default and the second value returned is NIL.
746 (defun info (class type name &optional (env-list nil env-list-p))
747   ;; FIXME: At some point check systematically to make sure that the
748   ;; system doesn't do any full calls to INFO or (SETF INFO), or at
749   ;; least none in any inner loops.
750   (let ((info (type-info-or-lose class type)))
751     (if env-list-p
752         (get-info-value name (type-info-number info) env-list)
753         (get-info-value name (type-info-number info)))))
754 #!-sb-fluid
755 (define-compiler-macro info
756   (&whole whole class type name &optional (env-list nil env-list-p))
757   ;; Constant CLASS and TYPE is an overwhelmingly common special case,
758   ;; and we can implement it much more efficiently than the general case.
759   (if (and (keywordp class) (keywordp type))
760       (let (#+sb-xc-host (sb!xc:*gensym-counter* sb!xc:*gensym-counter*)
761             (info (type-info-or-lose class type)))
762         (with-unique-names (value foundp)
763           `(multiple-value-bind (,value ,foundp)
764                (get-info-value ,name
765                                ,(type-info-number info)
766                                ,@(when env-list-p `(,env-list)))
767              (declare (type ,(type-info-type info) ,value))
768              (values ,value ,foundp))))
769       whole))
770
771 (defun (setf info)
772     (new-value class type name &optional (env-list nil env-list-p))
773   (let* ((info (type-info-or-lose class type))
774          (tin (type-info-number info)))
775     (when (type-info-validate-function info)
776       (funcall (type-info-validate-function info) name new-value))
777     (if env-list-p
778         (set-info-value name
779                         tin
780                         new-value
781                         (get-write-info-env env-list))
782         (set-info-value name
783                         tin
784                         new-value)))
785   new-value)
786 #!-sb-fluid
787 (progn
788   ;; Not all xc hosts are happy about SETF compiler macros: CMUCL 19
789   ;; does not accept them at all, and older SBCLs give a full warning.
790   ;; So the easy thing is to hide this optimization from all xc hosts.
791   #-sb-xc-host
792   (define-compiler-macro (setf info)
793       (&whole whole new-value class type name &optional (env-list nil env-list-p))
794     ;; Constant CLASS and TYPE is an overwhelmingly common special case,
795     ;; and we can resolve it much more efficiently than the general
796     ;; case.
797     (if (and (keywordp class) (keywordp type))
798         (let* ((info (type-info-or-lose class type))
799                (tin (type-info-number info)))
800           (if env-list-p
801               `(set-info-value ,name
802                                ,tin
803                                ,new-value
804                                (get-write-info-env ,env-list))
805               `(set-info-value ,name
806                                ,tin
807                                ,new-value))))
808     whole))
809
810 ;;; the maximum density of the hashtable in a volatile env (in
811 ;;; names/bucket)
812 ;;;
813 ;;; FIXME: actually seems to be measured in percent, should be
814 ;;; converted to be measured in names/bucket
815 (def!constant volatile-info-environment-density 50)
816
817 ;;; Make a new volatile environment of the specified size.
818 (defun make-info-environment (&key (size 42) (name "Unknown"))
819   (declare (type (integer 1) size))
820   (let ((table-size (primify (truncate (* size 100)
821                                        volatile-info-environment-density))))
822     (make-volatile-info-env :name name
823                             :table (make-array table-size :initial-element nil)
824                             :threshold size)))
825
826 ;;; Clear the information of the specified TYPE and CLASS for NAME in
827 ;;; the current environment, allowing any inherited info to become
828 ;;; visible. We return true if there was any info.
829 (defun clear-info (class type name)
830   (let ((info (type-info-or-lose class type)))
831     (clear-info-value name (type-info-number info))))
832 #!-sb-fluid
833 (define-compiler-macro clear-info (&whole whole class type name)
834   ;; Constant CLASS and TYPE is an overwhelmingly common special case, and
835   ;; we can resolve it much more efficiently than the general case.
836   (if (and (keywordp class) (keywordp type))
837     (let ((info (type-info-or-lose class type)))
838       `(clear-info-value ,name ,(type-info-number info)))
839     whole))
840 (defun clear-info-value (name type)
841   (declare (type type-number type) (inline assoc))
842   (with-info-bucket (table index name (get-write-info-env))
843     (let ((types (assoc name (svref table index) :test #'equal)))
844       (when (and types
845                  (assoc type (cdr types)))
846         (setf (cdr types)
847               (delete type (cdr types) :key #'car))
848         t))))
849 \f
850 ;;;; *INFO-ENVIRONMENT*
851
852 ;;; We do info access relative to the current *INFO-ENVIRONMENT*, a
853 ;;; list of INFO-ENVIRONMENT structures.
854 (defvar *info-environment*)
855 (declaim (type list *info-environment*))
856 (!cold-init-forms
857   (setq *info-environment*
858         (list (make-info-environment :name "initial global")))
859   (/show0 "done setting *INFO-ENVIRONMENT*"))
860 ;;; FIXME: should perhaps be *INFO-ENV-LIST*. And rename
861 ;;; all FOO-INFO-ENVIRONMENT-BAR stuff to FOO-INFO-ENV-BAR.
862 \f
863 ;;;; GET-INFO-VALUE
864
865 ;;; Return the value of NAME / TYPE from the first environment where
866 ;;; has it defined, or return the default if none does. We used to
867 ;;; do a lot of complicated caching here, but that was removed for
868 ;;; thread-safety reasons.
869 (defun get-info-value (name0 type &optional (env-list nil env-list-p))
870   (declare (type type-number type))
871   ;; sanity check: If we have screwed up initialization somehow, then
872   ;; *INFO-TYPES* could still be uninitialized at the time we try to
873   ;; get an info value, and then we'd be out of luck. (This happened,
874   ;; and was confusing to debug, when rewriting EVAL-WHEN in
875   ;; sbcl-0.pre7.x.)
876   (aver (aref *info-types* type))
877   (let ((name (uncross name0)))
878     (flet ((lookup (env-list)
879              (let ((hash nil))
880                (dolist (env env-list
881                         (multiple-value-bind (val winp)
882                             (funcall (type-info-default
883                                       (svref *info-types* type))
884                                      name)
885                           (values val winp)))
886                  (macrolet ((frob (lookup)
887                               `(progn
888                                  (setq hash (globaldb-sxhashoid name))
889                                  (multiple-value-bind (value winp)
890                                      (,lookup env name hash type)
891                                    (when winp (return (values value t)))))))
892                    (etypecase env
893                      (volatile-info-env (frob volatile-info-lookup))
894                      (compact-info-env (frob compact-info-lookup))))))))
895       (if env-list-p
896           (lookup env-list)
897           (lookup *info-environment*)))))
898 \f
899 ;;;; definitions for function information
900
901 (define-info-class :function)
902
903 ;;; the kind of functional object being described. If null, NAME isn't
904 ;;; a known functional object.
905 (define-info-type
906   :class :function
907   :type :kind
908   :type-spec (member nil :function :macro :special-form)
909   ;; I'm a little confused what the correct behavior of this default
910   ;; is. It's not clear how to generalize the FBOUNDP expression to
911   ;; the cross-compiler. As far as I can tell, NIL is a safe default
912   ;; -- it might keep the compiler from making some valid
913   ;; optimization, but it shouldn't produce incorrect code. -- WHN
914   ;; 19990330
915   :default
916   #+sb-xc-host nil
917   #-sb-xc-host (if (fboundp name) :function nil))
918
919 ;;; The type specifier for this function.
920 (define-info-type
921   :class :function
922   :type :type
923   :type-spec ctype
924   ;; Again (as in DEFINE-INFO-TYPE :CLASS :FUNCTION :TYPE :KIND) it's
925   ;; not clear how to generalize the FBOUNDP expression to the
926   ;; cross-compiler. -- WHN 19990330
927   :default
928   #+sb-xc-host (specifier-type 'function)
929   #-sb-xc-host (if (fboundp name)
930                    (specifier-type (sb!impl::%fun-type (fdefinition name)))
931                    (specifier-type 'function)))
932
933 ;;; the ASSUMED-TYPE for this function, if we have to infer the type
934 ;;; due to not having a declaration or definition
935 (define-info-type
936   :class :function
937   :type :assumed-type
938   ;; FIXME: The type-spec really should be
939   ;;   (or approximate-fun-type null)).
940   ;; It was changed to T as a hopefully-temporary hack while getting
941   ;; cold init problems untangled.
942   :type-spec t)
943
944 ;;; where this information came from:
945 ;;;    :ASSUMED  = from uses of the object
946 ;;;    :DEFINED  = from examination of the definition
947 ;;;    :DEFINED-METHOD = implicit, incremental declaration by CLOS.
948 ;;;    :DECLARED = from a declaration
949 ;;; :DEFINED trumps :ASSUMED, :DEFINED-METHOD trumps :DEFINED,
950 ;;; and :DECLARED trumps :DEFINED-METHOD.
951 ;;; :DEFINED and :ASSUMED are useful for issuing compile-time warnings,
952 ;;; :DEFINED-METHOD and :DECLARED are useful for ANSIly specializing
953 ;;; code which implements the function, or which uses the function's
954 ;;; return values.
955 (define-info-type
956   :class :function
957   :type :where-from
958   :type-spec (member :declared :defined-method :assumed :defined)
959   :default
960   ;; Again (as in DEFINE-INFO-TYPE :CLASS :FUNCTION :TYPE :KIND) it's
961   ;; not clear how to generalize the FBOUNDP expression to the
962   ;; cross-compiler. -- WHN 19990606
963   #+sb-xc-host :assumed
964   #-sb-xc-host (if (fboundp name) :defined :assumed))
965
966 ;;; something which can be decoded into the inline expansion of the
967 ;;; function, or NIL if there is none
968 ;;;
969 ;;; To inline a function, we want a lambda expression, e.g.
970 ;;; '(LAMBDA (X) (+ X 1)). That can be encoded here in one of two
971 ;;; ways.
972 ;;;   * The value in INFO can be the lambda expression itself, e.g.
973 ;;;       (SETF (INFO :FUNCTION :INLINE-EXPANSION-DESIGNATOR 'FOO)
974 ;;;             '(LAMBDA (X) (+ X 1)))
975 ;;;     This is the ordinary way, the natural way of representing e.g.
976 ;;;       (DECLAIM (INLINE FOO))
977 ;;;       (DEFUN FOO (X) (+ X 1))
978 ;;;   * The value in INFO can be a closure which returns the lambda
979 ;;;     expression, e.g.
980 ;;;       (SETF (INFO :FUNCTION :INLINE-EXPANSION-DESIGNATOR 'BAR-LEFT-CHILD)
981 ;;;             (LAMBDA ()
982 ;;;               '(LAMBDA (BAR) (BAR-REF BAR 3))))
983 ;;;     This twisty way of storing values is supported in order to
984 ;;;     allow structure slot accessors, and perhaps later other
985 ;;;     stereotyped functions, to be represented compactly.
986 (define-info-type
987   :class :function
988   :type :inline-expansion-designator
989   :type-spec (or list function)
990   :default nil)
991
992 ;;; This specifies whether this function may be expanded inline. If
993 ;;; null, we don't care.
994 (define-info-type
995   :class :function
996   :type :inlinep
997   :type-spec inlinep
998   :default nil)
999
1000 ;;; a macro-like function which transforms a call to this function
1001 ;;; into some other Lisp form. This expansion is inhibited if inline
1002 ;;; expansion is inhibited
1003 (define-info-type
1004   :class :function
1005   :type :source-transform
1006   :type-spec (or function null))
1007
1008 ;;; the macroexpansion function for this macro
1009 (define-info-type
1010   :class :function
1011   :type :macro-function
1012   :type-spec (or function null)
1013   :default nil)
1014
1015 ;;; the compiler-macroexpansion function for this macro
1016 (define-info-type
1017   :class :function
1018   :type :compiler-macro-function
1019   :type-spec (or function null)
1020   :default nil)
1021
1022 ;;; a function which converts this special form into IR1
1023 (define-info-type
1024   :class :function
1025   :type :ir1-convert
1026   :type-spec (or function null))
1027
1028 ;;; If a function is "known" to the compiler, then this is a FUN-INFO
1029 ;;; structure containing the info used to special-case compilation.
1030 (define-info-type
1031   :class :function
1032   :type :info
1033   :type-spec (or fun-info null)
1034   :default nil)
1035
1036 (define-info-type
1037   :class :function
1038   :type :definition
1039   :type-spec (or fdefn null)
1040   :default nil)
1041
1042 (define-info-type
1043   :class :function
1044   :type :structure-accessor
1045   :type-spec (or defstruct-description null)
1046   :default nil)
1047 \f
1048 ;;;; definitions for other miscellaneous information
1049
1050 (define-info-class :variable)
1051
1052 ;;; the kind of variable-like thing described
1053 (define-info-type
1054   :class :variable
1055   :type :kind
1056   :type-spec (member :special :constant :macro :global :alien :unknown)
1057   :default (if (typep name '(or boolean keyword))
1058                :constant
1059                :unknown))
1060
1061 (define-info-type
1062   :class :variable
1063   :type :always-bound
1064   :type-spec boolean
1065   :default nil)
1066
1067 ;;; the declared type for this variable
1068 (define-info-type
1069   :class :variable
1070   :type :type
1071   :type-spec ctype
1072   :default *universal-type*)
1073
1074 ;;; where this type and kind information came from
1075 (define-info-type
1076   :class :variable
1077   :type :where-from
1078   :type-spec (member :declared :assumed :defined)
1079   :default :assumed)
1080
1081 ;;; We only need a mechanism different from the
1082 ;;; usual SYMBOL-VALUE for the cross compiler.
1083 #+sb-xc-host
1084 (define-info-type
1085   :class :variable
1086   :type :xc-constant-value
1087   :type-spec t
1088   :default nil)
1089
1090 ;;; the macro-expansion for symbol-macros
1091 (define-info-type
1092   :class :variable
1093   :type :macro-expansion
1094   :type-spec t
1095   :default nil)
1096
1097 (define-info-type
1098   :class :variable
1099   :type :alien-info
1100   :type-spec (or heap-alien-info null)
1101   :default nil)
1102
1103 (define-info-type
1104   :class :variable
1105   :type :documentation
1106   :type-spec (or string null)
1107   :default nil)
1108
1109 (define-info-class :type)
1110
1111 ;;; the kind of type described. We return :INSTANCE for standard types
1112 ;;; that are implemented as structures. For PCL classes, that have
1113 ;;; only been compiled, but not loaded yet, we return
1114 ;;; :FORTHCOMING-DEFCLASS-TYPE.
1115 (define-info-type
1116   :class :type
1117   :type :kind
1118   :type-spec (member :primitive :defined :instance
1119                      :forthcoming-defclass-type nil)
1120   :default nil
1121   :validate-function (lambda (name new-value)
1122                        (declare (ignore new-value)
1123                                 (notinline info))
1124                        (when (info :declaration :recognized name)
1125                          (error 'declaration-type-conflict-error
1126                                 :format-arguments (list name)))))
1127
1128 ;;; the expander function for a defined type
1129 (define-info-type
1130   :class :type
1131   :type :expander
1132   :type-spec (or function null)
1133   :default nil)
1134
1135 (define-info-type
1136   :class :type
1137   :type :documentation
1138   :type-spec (or string null))
1139
1140 ;;; function that parses type specifiers into CTYPE structures
1141 (define-info-type
1142   :class :type
1143   :type :translator
1144   :type-spec (or function null)
1145   :default nil)
1146
1147 ;;; If true, then the type coresponding to this name. Note that if
1148 ;;; this is a built-in class with a translation, then this is the
1149 ;;; translation, not the class object. This info type keeps track of
1150 ;;; various atomic types (NIL etc.) and also serves as a cache to
1151 ;;; ensure that common standard types (atomic and otherwise) are only
1152 ;;; consed once.
1153 (define-info-type
1154   :class :type
1155   :type :builtin
1156   :type-spec (or ctype null)
1157   :default nil)
1158
1159 ;;; layout for this type being used by the compiler
1160 (define-info-type
1161   :class :type
1162   :type :compiler-layout
1163   :type-spec (or layout null)
1164   :default (let ((class (find-classoid name nil)))
1165              (when class (classoid-layout class))))
1166
1167 ;;; DEFTYPE lambda-list
1168 (define-info-type
1169    :class :type
1170    :type :lambda-list
1171    :type-spec list
1172    :default nil)
1173
1174 (define-info-type
1175    :class :type
1176    :type :source-location
1177    :type-spec t
1178    :default nil)
1179
1180 (define-info-class :typed-structure)
1181 (define-info-type
1182   :class :typed-structure
1183   :type :info
1184   :type-spec t
1185   :default nil)
1186 (define-info-type
1187   :class :typed-structure
1188   :type :documentation
1189   :type-spec (or string null)
1190   :default nil)
1191
1192 (define-info-class :declaration)
1193 (define-info-type
1194   :class :declaration
1195   :type :recognized
1196   :type-spec boolean
1197   :validate-function (lambda (name new-value)
1198                        (declare (ignore new-value)
1199                                 (notinline info))
1200                        (when (info :type :kind name)
1201                          (error 'declaration-type-conflict-error
1202                                 :format-arguments (list name)))))
1203
1204 (define-info-class :alien-type)
1205 (define-info-type
1206   :class :alien-type
1207   :type :kind
1208   :type-spec (member :primitive :defined :unknown)
1209   :default :unknown)
1210 (define-info-type
1211   :class :alien-type
1212   :type :translator
1213   :type-spec (or function null)
1214   :default nil)
1215 (define-info-type
1216   :class :alien-type
1217   :type :definition
1218   :type-spec (or alien-type null)
1219   :default nil)
1220 (define-info-type
1221   :class :alien-type
1222   :type :struct
1223   :type-spec (or alien-type null)
1224   :default nil)
1225 (define-info-type
1226   :class :alien-type
1227   :type :union
1228   :type-spec (or alien-type null)
1229   :default nil)
1230 (define-info-type
1231   :class :alien-type
1232   :type :enum
1233   :type-spec (or alien-type null)
1234   :default nil)
1235
1236 (define-info-class :setf)
1237
1238 (define-info-type
1239   :class :setf
1240   :type :inverse
1241   :type-spec (or symbol null)
1242   :default nil)
1243
1244 (define-info-type
1245   :class :setf
1246   :type :documentation
1247   :type-spec (or string null)
1248   :default nil)
1249
1250 (define-info-type
1251   :class :setf
1252   :type :expander
1253   :type-spec (or function null)
1254   :default nil)
1255
1256 ;;; This is used for storing miscellaneous documentation types. The
1257 ;;; stuff is an alist translating documentation kinds to values.
1258 (define-info-class :random-documentation)
1259 (define-info-type
1260   :class :random-documentation
1261   :type :stuff
1262   :type-spec list
1263   :default ())
1264
1265 ;;; Used to record the source location of definitions.
1266 (define-info-class :source-location)
1267
1268 (define-info-type
1269   :class :source-location
1270   :type :variable
1271   :type-spec t
1272   :default nil)
1273
1274 (define-info-type
1275   :class :source-location
1276   :type :constant
1277   :type-spec t
1278   :default nil)
1279
1280 (define-info-type
1281   :class :source-location
1282   :type :typed-structure
1283   :type-spec t
1284   :default nil)
1285
1286 (define-info-type
1287   :class :source-location
1288   :type :symbol-macro
1289   :type-spec t
1290   :default nil)
1291
1292 #!-sb-fluid (declaim (freeze-type info-env))
1293 \f
1294 ;;; Now that we have finished initializing *INFO-CLASSES* and
1295 ;;; *INFO-TYPES* (at compile time), generate code to set them at cold
1296 ;;; load time to the same state they have currently.
1297 (!cold-init-forms
1298   (/show0 "beginning *INFO-CLASSES* init, calling MAKE-HASH-TABLE")
1299   (setf *info-classes*
1300         (make-hash-table :test 'eq :size #.(* 2 (hash-table-count *info-classes*))))
1301   (/show0 "done with MAKE-HASH-TABLE in *INFO-CLASSES* init")
1302   (dolist (class-info-name '#.(let ((result nil))
1303                                 (maphash (lambda (key value)
1304                                            (declare (ignore value))
1305                                            (push key result))
1306                                          *info-classes*)
1307                                 (sort result #'string<)))
1308     (let ((class-info (make-class-info class-info-name)))
1309       (setf (gethash class-info-name *info-classes*)
1310             class-info)))
1311   (/show0 "done with *INFO-CLASSES* initialization")
1312   (/show0 "beginning *INFO-TYPES* initialization")
1313   (setf *info-types*
1314         (map 'vector
1315              (lambda (x)
1316                (/show0 "in LAMBDA (X), X=..")
1317                (/hexstr x)
1318                (when x
1319                  (let* ((class-info (class-info-or-lose (second x)))
1320                         (type-info (make-type-info :name (first x)
1321                                                    :class class-info
1322                                                    :number (third x)
1323                                                    :type (fourth x))))
1324                    (/show0 "got CLASS-INFO in LAMBDA (X)")
1325                    (push type-info (class-info-types class-info))
1326                    type-info)))
1327              '#.(map 'list
1328                      (lambda (info-type)
1329                        (when info-type
1330                          (list (type-info-name info-type)
1331                                (class-info-name (type-info-class info-type))
1332                                (type-info-number info-type)
1333                                ;; KLUDGE: for repeatable xc fasls, to
1334                                ;; avoid different cross-compiler
1335                                ;; treatment of equal constants here we
1336                                ;; COPY-TREE, which is not in general a
1337                                ;; valid identity transformation
1338                                ;; [e.g. on (EQL (FOO))] but is OK for
1339                                ;; all the types we use here.
1340                                (copy-tree (type-info-type info-type)))))
1341                      *info-types*)))
1342   (/show0 "done with *INFO-TYPES* initialization"))
1343
1344 ;;; At cold load time, after the INFO-TYPE objects have been created,
1345 ;;; we can set their DEFAULT and TYPE slots.
1346 (macrolet ((frob ()
1347              `(!cold-init-forms
1348                 ,@(reverse *!reversed-type-info-init-forms*))))
1349   (frob))
1350 \f
1351 ;;;; a hack for detecting
1352 ;;;;   (DEFUN FOO (X Y)
1353 ;;;;     ..
1354 ;;;;     (SETF (BAR A FFH) 12) ; compiles to a call to #'(SETF BAR)
1355 ;;;;     ..)
1356 ;;;;   (DEFSETF BAR SET-BAR) ; can't influence previous compilation
1357 ;;;;
1358 ;;;; KLUDGE: Arguably it should be another class/type combination in
1359 ;;;; the globaldb. However, IMHO the whole globaldb/fdefinition
1360 ;;;; treatment of SETF functions is a mess which ought to be
1361 ;;;; rewritten, and I'm not inclined to mess with it short of that. So
1362 ;;;; I just put this bag on the side of it instead..
1363
1364 ;;; true for symbols FOO which have been assumed to have '(SETF FOO)
1365 ;;; bound to a function
1366 (defvar *setf-assumed-fboundp*)
1367 (!cold-init-forms (setf *setf-assumed-fboundp* (make-hash-table)))
1368 \f
1369 (!defun-from-collected-cold-init-forms !globaldb-cold-init)