0.8.7.43:
[sbcl.git] / src / compiler / fndb.lisp
1 ;;;; This file defines all the standard functions to be known
2 ;;;; functions. Each function has type and side-effect information,
3 ;;;; and may also have IR1 optimizers.
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 (in-package "SB!C")
15 \f
16 ;;;; information for known functions:
17
18 (defknown coerce (t type-specifier) t
19   ;; Note:
20   ;; (1) This is not FLUSHABLE because it's defined to signal errors.
21   ;; (2) It's not worth trying to make this FOLDABLE in the
22   ;;     cross-compiler,because
23   ;;       (a) it would probably be really hard to make all the 
24   ;;           tricky issues (e.g. which specialized array types are
25   ;;           supported) match between cross-compiler and target
26   ;;           compiler, and besides
27   ;;       (b) leaving it not FOLDABLE lets us use the idiom
28   ;;               (COERCE FOO 'SOME-SPECIALIZED-ARRAY-TYPE-OR-ANOTHER)
29   ;;           as a way of delaying the generation of specialized
30   ;;           array types until runtime, which helps us keep the
31   ;;           cross-compiler's dumper relatively simple and which
32   ;;           lets us preserve distinctions which might not even exist
33   ;;           on the cross-compilation host (because ANSI doesn't
34   ;;           guarantee that specialized array types exist there).
35   ;; FIXME: It's actually not clear that COERCE on non-NUMBER types
36   ;; is FOLDABLE at all. Check this.
37   (movable #-sb-xc-host foldable)
38   ;; :DERIVE-TYPE RESULT-TYPE-SPEC-NTH-ARG 2 ? Nope... (COERCE 1 'COMPLEX)
39   ;; returns REAL/INTEGER, not COMPLEX.
40   )
41 (defknown list-to-vector* (list type-specifier) vector)
42 (defknown vector-to-vector* (vector type-specifier) vector)
43
44 (defknown type-of (t) t (foldable flushable))
45
46 ;;; These can be affected by type definitions, so they're not FOLDABLE.
47 (defknown (upgraded-complex-part-type sb!xc:upgraded-array-element-type)
48           (type-specifier &optional lexenv-designator) type-specifier
49   (unsafely-flushable))
50 \f
51 ;;;; from the "Predicates" chapter:
52
53 ;;; FIXME: Is it right to have TYPEP (and TYPE-OF, elsewhere; and
54 ;;; perhaps SPECIAL-OPERATOR-P and others) be FOLDABLE in the
55 ;;; cross-compilation host? After all, some type relationships (e.g.
56 ;;; FIXNUMness) might be different between host and target. Perhaps
57 ;;; this property should be protected by #-SB-XC-HOST? Perhaps we need
58 ;;; 3-stage bootstrapping after all? (Ugh! It's *so* slow already!)
59 (defknown typep (t type-specifier &optional lexenv-designator) t
60    ;; Unlike SUBTYPEP or UPGRADED-ARRAY-ELEMENT-TYPE and friends, this
61    ;; seems to be FOLDABLE. Like SUBTYPEP, it's affected by type
62    ;; definitions, but unlike SUBTYPEP, there should be no way to make
63    ;; a TYPEP expression with constant arguments which doesn't return
64    ;; an error before the type declaration (because of undefined
65    ;; type). E.g. you can do
66    ;;   (SUBTYPEP 'INTEGER 'FOO) => NIL, NIL
67    ;;   (DEFTYPE FOO () T)
68    ;;   (SUBTYPEP 'INTEGER 'FOO) => T, T
69    ;; but the analogous
70    ;;   (TYPEP 12 'FOO)
71    ;;   (DEFTYPE FOO () T)
72    ;;   (TYPEP 12 'FOO)
73    ;; doesn't work because the first call is an error.
74    ;;
75    ;; (UPGRADED-ARRAY-ELEMENT-TYPE and UPGRADED-COMPLEX-PART-TYPE have
76    ;; behavior like SUBTYPEP in this respect, not like TYPEP.)
77    (foldable))
78 (defknown subtypep (type-specifier type-specifier &optional lexenv-designator)
79   (values boolean boolean)
80   ;; This is not FOLDABLE because its value is affected by type
81   ;; definitions.
82   ;;
83   ;; FIXME: Is it OK to fold this when the types have already been
84   ;; defined? Does the code inherited from CMU CL already do this?
85   (unsafely-flushable))
86
87 (defknown (null symbolp atom consp listp numberp integerp rationalp floatp
88                 complexp characterp stringp bit-vector-p vectorp
89                 simple-vector-p simple-string-p simple-bit-vector-p arrayp
90                 sb!xc:packagep functionp compiled-function-p not)
91   (t) boolean (movable foldable flushable))
92
93 (defknown (eq eql) (t t) boolean (movable foldable flushable))
94 (defknown (equal equalp) (t t) boolean (foldable flushable recursive))
95 \f
96 ;;;; classes
97
98 (sb!xc:deftype name-for-class () t)
99 (defknown classoid-name (classoid) name-for-class (flushable))
100 (defknown find-classoid (name-for-class &optional t lexenv-designator)
101   (or classoid null) ())
102 (defknown classoid-of (t) classoid (flushable))
103 (defknown layout-of (t) layout (flushable))
104 (defknown copy-structure (structure-object) structure-object
105   (flushable unsafe))
106 \f
107 ;;;; from the "Control Structure" chapter:
108
109 ;;; This is not FLUSHABLE, since it's required to signal an error if
110 ;;; unbound.
111 (defknown (symbol-value) (symbol) t ())
112 ;;; From CLHS, "If the symbol is globally defined as a macro or a
113 ;;; special operator, an object of implementation-dependent nature and
114 ;;; identity is returned. If the symbol is not globally defined as
115 ;;; either a macro or a special operator, and if the symbol is fbound,
116 ;;; a function object is returned".  Our objects of
117 ;;; implementation-dependent nature happen to be functions.
118 (defknown (symbol-function) (symbol) function ())
119
120 (defknown boundp (symbol) boolean (flushable))
121 (defknown fboundp ((or symbol cons)) boolean (unsafely-flushable explicit-check))
122 (defknown special-operator-p (symbol) t
123   ;; The set of special operators never changes.
124   (movable foldable flushable))
125 (defknown set (symbol t) t (unsafe)
126   :derive-type #'result-type-last-arg)
127 (defknown fdefinition ((or symbol cons)) function (unsafe explicit-check))
128 (defknown %set-fdefinition ((or symbol cons) function) function
129   (unsafe explicit-check))
130 (defknown makunbound (symbol) symbol)
131 (defknown fmakunbound ((or symbol cons)) (or symbol cons)
132   (unsafe explicit-check))
133 (defknown (get-setf-method get-setf-method-multiple-value)
134   ((or list symbol) &optional lexenv-designator)
135   (values list list list form form)
136   (flushable))
137 (defknown apply (callable t &rest t) *) ; ### Last arg must be List...
138 (defknown funcall (callable &rest t) *)
139
140 (defknown (mapcar maplist) (callable list &rest list) list
141   (call))
142
143 ;;; According to CLHS the result must be a LIST, but we do not check
144 ;;; it.
145 (defknown (mapcan mapcon) (callable list &rest list) t
146   (call))
147
148 (defknown (mapc mapl) (callable list &rest list) list (foldable call))
149
150 ;;; We let VALUES-LIST be foldable, since constant-folding will turn
151 ;;; it into VALUES. VALUES is not foldable, since MV constants are
152 ;;; represented by a call to VALUES.
153 (defknown values (&rest t) * (movable flushable unsafe))
154 (defknown values-list (list) * (movable foldable unsafely-flushable))
155 \f
156 ;;;; from the "Macros" chapter:
157
158 (defknown macro-function (symbol &optional lexenv-designator)
159   (or function null)
160   (flushable))
161 (defknown (macroexpand macroexpand-1) (t &optional lexenv-designator)
162   (values form &optional boolean))
163
164 (defknown compiler-macro-function (t &optional lexenv-designator)
165   (or function null)
166   (flushable))
167 \f
168 ;;;; from the "Declarations" chapter:
169
170 (defknown proclaim (list) (values) (recursive))
171
172 ;;;; from the "Symbols" chapter:
173
174 (defknown get (symbol t &optional t) t (flushable))
175 (defknown remprop (symbol t) t)
176 (defknown symbol-plist (symbol) list (flushable))
177 (defknown getf (list t &optional t) t (foldable flushable))
178 (defknown get-properties (list list) (values t t list) (foldable flushable))
179 (defknown symbol-name (symbol) simple-string (movable foldable flushable))
180 (defknown make-symbol (string) symbol (flushable))
181 (defknown copy-symbol (symbol &optional t) symbol (flushable))
182 (defknown gensym (&optional (or string unsigned-byte)) symbol ())
183 (defknown symbol-package (symbol) (or sb!xc:package null) (flushable))
184 (defknown keywordp (t) boolean (flushable))       ; If someone uninterns it...
185 \f
186 ;;;; from the "Packages" chapter:
187
188 (sb!xc:deftype package-designator () '(or stringable sb!xc:package))
189 (sb!xc:deftype symbols () '(or list symbol))
190
191 (defknown gentemp (&optional string package-designator) symbol)
192
193 (defknown make-package (stringable &key
194                                    (:use list)
195                                    (:nicknames list)
196                                    ;; ### extensions...
197                                    (:internal-symbols index)
198                                    (:external-symbols index))
199   sb!xc:package)
200 (defknown find-package (package-designator) (or sb!xc:package null)
201   (flushable))
202 (defknown package-name (package-designator) (or simple-string null)
203   (unsafely-flushable))
204 (defknown package-nicknames (package-designator) list (unsafely-flushable))
205 (defknown rename-package (package-designator package-designator &optional list)
206   sb!xc:package)
207 (defknown package-use-list (package-designator) list (unsafely-flushable))
208 (defknown package-used-by-list (package-designator) list (unsafely-flushable))
209 (defknown package-shadowing-symbols (package-designator) list (unsafely-flushable))
210 (defknown list-all-packages () list (flushable))
211 (defknown intern (string &optional package-designator)
212   (values symbol (member :internal :external :inherited nil))
213   ())
214 (defknown find-symbol (string &optional package-designator)
215   (values symbol (member :internal :external :inherited nil))
216   (flushable))
217 (defknown (export import) (symbols &optional package-designator) (eql t))
218 (defknown unintern (symbol &optional package-designator) boolean)
219 (defknown unexport (symbols &optional package-designator) (eql t))
220 (defknown shadowing-import (symbols &optional package-designator) (eql t))
221 (defknown shadow ((or symbol string list) &optional package-designator)
222   (eql t))
223 (defknown (use-package unuse-package)
224   ((or list package-designator) &optional package-designator) (eql t))
225 (defknown find-all-symbols (stringable) list (flushable))
226 \f
227 ;;;; from the "Numbers" chapter:
228
229 (defknown zerop (number) boolean (movable foldable flushable explicit-check))
230 (defknown (plusp minusp) (real) boolean
231   (movable foldable flushable explicit-check))
232 (defknown (oddp evenp) (integer) boolean
233   (movable foldable flushable explicit-check))
234 (defknown (= /=) (number &rest number) boolean
235   (movable foldable flushable explicit-check))
236 (defknown (< > <= >=) (real &rest real) boolean
237   (movable foldable flushable explicit-check))
238 (defknown (max min) (real &rest real) real
239   (movable foldable flushable explicit-check))
240
241 (defknown + (&rest number) number
242   (movable foldable flushable explicit-check))
243 (defknown - (number &rest number) number
244   (movable foldable flushable explicit-check))
245 (defknown * (&rest number) number
246   (movable foldable flushable explicit-check))
247 (defknown / (number &rest number) number
248   (movable foldable flushable explicit-check))
249 (defknown (1+ 1-) (number) number
250   (movable foldable flushable explicit-check))
251
252 (defknown conjugate (number) number
253   (movable foldable flushable explicit-check))
254
255 (defknown gcd (&rest integer) unsigned-byte
256   (movable foldable flushable explicit-check)
257   #|:derive-type 'boolean-result-type|#)
258 (defknown lcm (&rest integer) unsigned-byte
259   (movable foldable flushable explicit-check))
260
261 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
262 (defknown exp (number) irrational
263   (movable foldable flushable explicit-check recursive)
264   :derive-type #'result-type-float-contagion)
265
266 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
267 (defknown exp (number) irrational
268   (movable foldable flushable explicit-check recursive))
269
270 (defknown expt (number number) number
271   (movable foldable flushable explicit-check recursive))
272 (defknown log (number &optional real) irrational
273   (movable foldable flushable explicit-check))
274 (defknown sqrt (number) irrational
275   (movable foldable flushable explicit-check))
276 (defknown isqrt (unsigned-byte) unsigned-byte
277   (movable foldable flushable explicit-check recursive))
278
279 (defknown (abs phase signum) (number) number
280   (movable foldable flushable explicit-check))
281 (defknown cis (real) (complex float)
282   (movable foldable flushable explicit-check))
283
284 #+sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
285 (progn
286 (defknown (sin cos) (number)
287   (or (float -1.0 1.0) (complex float))
288   (movable foldable flushable explicit-check recursive)
289   :derive-type #'result-type-float-contagion)
290
291 (defknown atan
292   (number &optional real) irrational
293   (movable foldable unsafely-flushable explicit-check recursive)
294   :derive-type #'result-type-float-contagion)
295
296 (defknown (tan sinh cosh tanh asinh)
297   (number) irrational (movable foldable flushable explicit-check recursive)
298   :derive-type #'result-type-float-contagion)
299 ) ; PROGN
300
301 #-sb-xc-host ; (See CROSS-FLOAT-INFINITY-KLUDGE.)
302 (progn
303 (defknown (sin cos) (number)
304   (or (float -1.0 1.0) (complex float))
305   (movable foldable flushable explicit-check recursive))
306
307 (defknown atan
308   (number &optional real) irrational
309   (movable foldable unsafely-flushable explicit-check recursive))
310
311 (defknown (tan sinh cosh tanh asinh)
312   (number) irrational (movable foldable flushable explicit-check recursive))
313 ) ; PROGN
314
315 (defknown (asin acos acosh atanh)
316   (number) irrational
317   (movable foldable flushable explicit-check recursive))
318
319 (defknown float (real &optional float) float
320   (movable foldable flushable explicit-check))
321
322 (defknown (rational) (real) rational
323   (movable foldable flushable explicit-check))
324
325 (defknown (rationalize) (real) rational
326   (movable foldable flushable explicit-check recursive))
327
328 (defknown (numerator denominator) (rational) integer
329   (movable foldable flushable))
330
331 (defknown (floor ceiling truncate round)
332   (real &optional real) (values integer real)
333   (movable foldable flushable explicit-check))
334
335 (defknown (mod rem) (real real) real
336   (movable foldable flushable explicit-check))
337
338 (defknown (ffloor fceiling fround ftruncate)
339   (real &optional real) (values float real)
340   (movable foldable flushable explicit-check))
341
342 (defknown decode-float (float) (values float float-exponent float)
343   (movable foldable flushable explicit-check))
344 (defknown scale-float (float float-exponent) float
345   (movable foldable unsafely-flushable explicit-check))
346 (defknown float-radix (float) float-radix
347   (movable foldable flushable))
348 (defknown float-sign (float &optional float) float
349   (movable foldable flushable explicit-check))
350 (defknown (float-digits float-precision) (float) float-digits
351   (movable foldable flushable explicit-check))
352 (defknown integer-decode-float (float)
353           (values integer float-int-exponent (member -1 1))
354           (movable foldable flushable explicit-check))
355
356 (defknown complex (real &optional real) number
357   (movable foldable flushable explicit-check))
358
359 (defknown (realpart imagpart) (number) real (movable foldable flushable))
360
361 (defknown (logior logxor logand logeqv) (&rest integer) integer
362   (movable foldable flushable explicit-check))
363
364 (defknown (lognand lognor logandc1 logandc2 logorc1 logorc2)
365           (integer integer) integer
366   (movable foldable flushable explicit-check))
367
368 (defknown boole (boole-code integer integer) integer
369   (movable foldable flushable))
370
371 (defknown lognot (integer) integer (movable foldable flushable explicit-check))
372 (defknown logtest (integer integer) boolean (movable foldable flushable))
373 (defknown logbitp (unsigned-byte integer) boolean (movable foldable flushable))
374 (defknown ash (integer integer) integer
375   (movable foldable flushable explicit-check))
376 (defknown (logcount integer-length) (integer) bit-index
377   (movable foldable flushable explicit-check))
378 ;;; FIXME: According to the ANSI spec, it's legal to use any
379 ;;; nonnegative indices for BYTE arguments, not just BIT-INDEX. It's
380 ;;; hard to come up with useful ways to do this, but it is possible to
381 ;;; come up with *legal* ways to do this, so it would be nice
382 ;;; to fix this so we comply with the spec.
383 (defknown byte (bit-index bit-index) byte-specifier
384   (movable foldable flushable))
385 (defknown (byte-size byte-position) (byte-specifier) bit-index
386   (movable foldable flushable))
387 (defknown ldb (byte-specifier integer) integer (movable foldable flushable))
388 (defknown ldb-test (byte-specifier integer) boolean
389   (movable foldable flushable))
390 (defknown mask-field (byte-specifier integer) integer
391   (movable foldable flushable))
392 (defknown dpb (integer byte-specifier integer) integer
393   (movable foldable flushable))
394 (defknown deposit-field (integer byte-specifier integer) integer
395   (movable foldable flushable))
396 (defknown random ((or (float (0.0)) (integer 1)) &optional random-state)
397   (or (float 0.0) (integer 0)) ())
398 (defknown make-random-state (&optional (or (member nil t) random-state))
399   random-state (flushable))
400 (defknown random-state-p (t) boolean (movable foldable flushable))
401 \f
402 ;;;; from the "Characters" chapter:
403 (defknown (standard-char-p graphic-char-p alpha-char-p
404                            upper-case-p lower-case-p both-case-p alphanumericp)
405   (character) boolean (movable foldable flushable))
406
407 (defknown digit-char-p (character &optional (integer 2 36))
408   (or (integer 0 35) null) (movable foldable flushable))
409
410 (defknown (char= char/= char< char> char<= char>= char-equal char-not-equal
411                  char-lessp char-greaterp char-not-greaterp char-not-lessp)
412   (character &rest character) boolean (movable foldable flushable))
413
414 (defknown character (t) character (movable foldable unsafely-flushable))
415 (defknown char-code (character) char-code (movable foldable flushable))
416 (defknown (char-upcase char-downcase) (character) character
417   (movable foldable flushable))
418 (defknown digit-char (unsigned-byte &optional (integer 2 36))
419   (or character null) (movable foldable flushable))
420 (defknown char-int (character) char-code (movable foldable flushable))
421 (defknown char-name (character) (or simple-string null)
422   (movable foldable flushable))
423 (defknown name-char (stringable) (or character null)
424   (movable foldable flushable))
425 (defknown code-char (char-code) base-char
426   ;; By suppressing constant folding on CODE-CHAR when the
427   ;; cross-compiler is running in the cross-compilation host vanilla
428   ;; ANSI Common Lisp, we can use CODE-CHAR expressions to delay until
429   ;; target Lisp run time the generation of CHARACTERs which aren't
430   ;; STANDARD-CHARACTERs. That way, we don't need to rely on the host
431   ;; Common Lisp being able to handle any characters other than those
432   ;; guaranteed by the ANSI spec.
433   (movable #-sb-xc-host foldable flushable))
434 \f
435 ;;;; from the "Sequences" chapter:
436
437 (defknown elt (sequence index) t (foldable unsafely-flushable))
438
439 (defknown subseq (sequence index &optional sequence-end) consed-sequence
440   (flushable)
441   :derive-type (sequence-result-nth-arg 1))
442
443 (defknown copy-seq (sequence) consed-sequence (flushable)
444   :derive-type (sequence-result-nth-arg 1))
445
446 (defknown length (sequence) index (foldable flushable))
447
448 (defknown reverse (sequence) consed-sequence (flushable)
449   :derive-type (sequence-result-nth-arg 1))
450
451 (defknown nreverse (sequence) sequence ()
452   :derive-type #'result-type-first-arg)
453
454 (defknown make-sequence (type-specifier index
455                                         &key
456                                         (:initial-element t))
457   consed-sequence
458   (movable unsafe)
459   :derive-type (creation-result-type-specifier-nth-arg 1))
460
461 (defknown concatenate (type-specifier &rest sequence) consed-sequence
462   ()
463   :derive-type (creation-result-type-specifier-nth-arg 1))
464
465 (defknown (map %map) (type-specifier callable sequence &rest sequence)
466   consed-sequence
467   (call)
468 ; :DERIVE-TYPE 'TYPE-SPEC-ARG1 ? Nope... (MAP NIL ...) returns NULL, not NIL.
469   )
470 (defknown %map-to-list-arity-1 (callable sequence) list (flushable call))
471 (defknown %map-to-simple-vector-arity-1 (callable sequence) simple-vector
472   (flushable call))
473 (defknown %map-to-nil-on-simple-vector (callable simple-vector) null
474   (flushable call))
475 (defknown %map-to-nil-on-vector (callable vector) null (flushable call))
476 (defknown %map-to-nil-on-sequence (callable sequence) null (flushable call))
477
478 (defknown map-into (sequence callable &rest sequence)
479   sequence
480   (call)
481   :derive-type #'result-type-first-arg)
482
483 ;;; returns the result from the predicate...
484 (defknown some (callable sequence &rest sequence) t
485   (foldable unsafely-flushable call))
486
487 (defknown (every notany notevery) (callable sequence &rest sequence) boolean
488   (foldable unsafely-flushable call))
489
490 ;;; unsafe for :INITIAL-VALUE...
491 (defknown reduce (callable
492                   sequence
493                   &key
494                   (:from-end t)
495                   (:start index)
496                   (:end sequence-end)
497                   (:initial-value t)
498                   (:key callable))
499   t
500   (foldable flushable call unsafe))
501
502 (defknown fill (sequence t &key (:start index) (:end sequence-end)) sequence
503   (unsafe)
504   :derive-type #'result-type-first-arg)
505
506 (defknown replace (sequence
507                    sequence
508                    &key
509                    (:start1 index)
510                    (:end1 sequence-end)
511                    (:start2 index)
512                    (:end2 sequence-end))
513   sequence ()
514   :derive-type #'result-type-first-arg)
515
516 (defknown remove
517   (t sequence &key (:from-end t) (:test callable)
518      (:test-not callable) (:start index) (:end sequence-end)
519      (:count sequence-count) (:key callable))
520   consed-sequence
521   (flushable call)
522   :derive-type (sequence-result-nth-arg 2))
523
524 (defknown substitute
525   (t t sequence &key (:from-end t) (:test callable)
526      (:test-not callable) (:start index) (:end sequence-end)
527      (:count sequence-count) (:key callable))
528   consed-sequence
529   (flushable call)
530   :derive-type (sequence-result-nth-arg 3))
531
532 (defknown (remove-if remove-if-not)
533   (callable sequence &key (:from-end t) (:start index) (:end sequence-end)
534             (:count sequence-count) (:key callable))
535   consed-sequence
536   (flushable call)
537   :derive-type (sequence-result-nth-arg 2))
538
539 (defknown (substitute-if substitute-if-not)
540   (t callable sequence &key (:from-end t) (:start index) (:end sequence-end)
541      (:count sequence-count) (:key callable))
542   consed-sequence
543   (flushable call)
544   :derive-type (sequence-result-nth-arg 3))
545
546 (defknown delete
547   (t sequence &key (:from-end t) (:test callable)
548      (:test-not callable) (:start index) (:end sequence-end)
549      (:count sequence-count) (:key callable))
550   sequence
551   (flushable call)
552   :derive-type (sequence-result-nth-arg 2))
553
554 (defknown nsubstitute
555   (t t sequence &key (:from-end t) (:test callable)
556      (:test-not callable) (:start index) (:end sequence-end)
557      (:count sequence-count) (:key callable))
558   sequence
559   (flushable call)
560   :derive-type (sequence-result-nth-arg 3))
561
562 (defknown (delete-if delete-if-not)
563   (callable sequence &key (:from-end t) (:start index) (:end sequence-end)
564             (:count sequence-count) (:key callable))
565   sequence
566   (flushable call)
567   :derive-type (sequence-result-nth-arg 2))
568
569 (defknown (nsubstitute-if nsubstitute-if-not)
570   (t callable sequence &key (:from-end t) (:start index) (:end sequence-end)
571      (:count sequence-count) (:key callable))
572   sequence
573   (flushable call)
574   :derive-type (sequence-result-nth-arg 3))
575
576 (defknown remove-duplicates
577   (sequence &key (:test callable) (:test-not callable) (:start index)
578             (:from-end t) (:end sequence-end) (:key callable))
579   consed-sequence
580   (unsafely-flushable call)
581   :derive-type (sequence-result-nth-arg 1))
582
583 (defknown delete-duplicates
584   (sequence &key (:test callable) (:test-not callable) (:start index)
585             (:from-end t) (:end sequence-end) (:key callable))
586   sequence
587   (unsafely-flushable call)
588   :derive-type (sequence-result-nth-arg 1))
589
590 (defknown find (t sequence &key (:test callable) (:test-not callable)
591                   (:start index) (:from-end t) (:end sequence-end)
592                   (:key callable))
593   t
594   (foldable flushable call))
595
596 (defknown (find-if find-if-not)
597   (callable sequence &key (:from-end t) (:start index) (:end sequence-end)
598             (:key callable))
599   t
600   (foldable flushable call))
601
602 (defknown position (t sequence &key (:test callable) (:test-not callable)
603                       (:start index) (:from-end t) (:end sequence-end)
604                       (:key callable))
605   (or index null)
606   (foldable flushable call))
607
608 (defknown (position-if position-if-not)
609   (callable sequence &key (:from-end t) (:start index) (:end sequence-end)
610             (:key callable))
611   (or index null)
612   (foldable flushable call))
613
614 (defknown count (t sequence &key (:test callable) (:test-not callable)
615                       (:start index) (:from-end t) (:end sequence-end)
616                       (:key callable))
617   index
618   (foldable flushable call))
619
620 (defknown (count-if count-if-not)
621   (callable sequence &key (:from-end t) (:start index) (:end sequence-end)
622             (:key callable))
623   index
624   (foldable flushable call))
625
626 (defknown (mismatch search)
627   (sequence sequence &key (:from-end t) (:test callable) (:test-not callable)
628             (:start1 index) (:end1 sequence-end)
629             (:start2 index) (:end2 sequence-end)
630             (:key callable))
631   (or index null)
632   (foldable flushable call))
633
634 ;;; not FLUSHABLE, since vector sort guaranteed in-place...
635 (defknown (stable-sort sort) (sequence callable &key (:key callable)) sequence
636   (call)
637   :derive-type (sequence-result-nth-arg 1))
638 (defknown sb!impl::sort-vector (vector index index function (or function null))
639   * ; SORT-VECTOR works through side-effect
640   (call))
641
642 (defknown merge (type-specifier sequence sequence callable
643                                 &key (:key callable))
644   sequence
645   (call)
646   :derive-type (creation-result-type-specifier-nth-arg 1))
647
648 ;;; not FLUSHABLE, despite what CMU CL's DEFKNOWN said..
649 (defknown read-sequence (sequence stream
650                                   &key
651                                   (:start index)
652                                   (:end sequence-end))
653   (index)
654   ())
655
656 (defknown write-sequence (sequence stream
657                                    &key
658                                    (:start index)
659                                    (:end sequence-end))
660   sequence
661   ()
662   :derive-type (sequence-result-nth-arg 1))
663 \f
664 ;;;; from the "Manipulating List Structure" chapter:
665 (defknown (car cdr first rest)
666   (list)
667   t
668   (foldable flushable))
669
670 ;; Correct argument type restrictions for these functions are
671 ;; complicated, so we just declare them to accept LISTs and suppress
672 ;; flushing is safe code.
673 (defknown (caar cadr cdar cddr
674                 caaar caadr cadar caddr cdaar cdadr cddar cdddr
675                 caaaar caaadr caadar caaddr cadaar cadadr caddar cadddr
676                 cdaaar cdaadr cdadar cdaddr cddaar cddadr cdddar cddddr
677                 second third fourth fifth sixth seventh eighth ninth tenth)
678   (list)
679   t
680   (foldable unsafely-flushable))
681
682 (defknown cons (t t) cons (movable flushable unsafe))
683
684 (defknown tree-equal (t t &key (:test callable) (:test-not callable)) boolean
685   (foldable flushable call))
686 (defknown endp (list) boolean (foldable flushable movable))
687 (defknown list-length (list) (or index null) (foldable unsafely-flushable))
688 (defknown nth (unsigned-byte list) t (foldable flushable))
689 (defknown nthcdr (unsigned-byte list) t (foldable unsafely-flushable))
690 (defknown last (list &optional unsigned-byte) t (foldable flushable))
691 (defknown list (&rest t) list (movable flushable unsafe))
692 (defknown list* (t &rest t) t (movable flushable unsafe))
693 (defknown make-list (index &key (:initial-element t)) list
694   (movable flushable unsafe))
695
696 ;;; All but last must be of type LIST, but there seems to be no way to
697 ;;; express that in this syntax.
698 (defknown append (&rest t) t (flushable))
699
700 (defknown copy-list (list) list (flushable))
701 (defknown copy-alist (list) list (flushable))
702 (defknown copy-tree (t) t (flushable recursive))
703 (defknown revappend (list t) t (flushable))
704
705 ;;; All but last must be of type LIST, but there seems to be no way to
706 ;;; express that in this syntax. The result must be LIST, but we do
707 ;;; not check it now :-).
708 (defknown nconc (&rest t) t ())
709
710 (defknown nreconc (list t) t ())
711 (defknown butlast (list &optional unsigned-byte) list (flushable))
712 (defknown nbutlast (list &optional unsigned-byte) list ())
713 (defknown ldiff (list t) list (flushable))
714 (defknown (rplaca rplacd) (cons t) list (unsafe))
715
716 (defknown (nsubst subst) (t t t &key (:key callable) (:test callable)
717                             (:test-not callable))
718   t (flushable unsafe call))
719
720 (defknown (subst-if subst-if-not nsubst-if nsubst-if-not)
721           (t callable t &key (:key callable))
722   t (flushable unsafe call))
723
724 (defknown (sublis nsublis) (list t &key (:key callable) (:test callable)
725                                  (:test-not callable))
726   t (flushable unsafe call))
727
728 (defknown member (t list &key (:key callable) (:test callable)
729                     (:test-not callable))
730   list (foldable flushable call))
731 (defknown (member-if member-if-not) (callable list &key (:key callable))
732   list (foldable flushable call))
733
734 (defknown tailp (t list) boolean (foldable flushable))
735
736 (defknown adjoin (t list &key (:key callable) (:test callable)
737                     (:test-not callable))
738   list (foldable flushable unsafe call))
739
740 (defknown (union intersection set-difference set-exclusive-or)
741   (list list &key (:key callable) (:test callable) (:test-not callable))
742   list
743   (foldable flushable call))
744
745 (defknown (nunion nintersection nset-difference nset-exclusive-or)
746   (list list &key (:key callable) (:test callable) (:test-not callable))
747   list
748   (foldable flushable call))
749
750 (defknown subsetp
751   (list list &key (:key callable) (:test callable) (:test-not callable))
752   boolean
753   (foldable flushable call))
754
755 (defknown acons (t t t) list (movable flushable unsafe))
756 (defknown pairlis (t t &optional t) list (flushable unsafe))
757
758 (defknown (rassoc assoc)
759           (t list &key (:key callable) (:test callable) (:test-not callable))
760   list (foldable flushable call))
761 (defknown (assoc-if-not assoc-if rassoc-if rassoc-if-not)
762           (callable list &key (:key callable)) list (foldable flushable call))
763
764 (defknown (memq assq) (t list) list (foldable flushable unsafe))
765 (defknown delq (t list) list (flushable unsafe))
766 \f
767 ;;;; from the "Hash Tables" chapter:
768
769 (defknown make-hash-table
770   (&key (:test callable) (:size unsigned-byte)
771         (:rehash-size (or (integer 1) (float (1.0))))
772         (:rehash-threshold (real 0 1))
773         (:weak-p t))
774   hash-table
775   (flushable unsafe))
776 (defknown hash-table-p (t) boolean (movable foldable flushable))
777 (defknown gethash (t hash-table &optional t) (values t boolean)
778   (flushable unsafe)) ; not FOLDABLE, since hash table contents can change
779 (defknown %puthash (t hash-table t) t (unsafe))
780 (defknown remhash (t hash-table) boolean ())
781 (defknown maphash (callable hash-table) null (flushable call))
782 (defknown clrhash (hash-table) hash-table ())
783 (defknown hash-table-count (hash-table) index (flushable))
784 (defknown hash-table-rehash-size (hash-table) (or (integer 1) (float (1.0)))
785   (foldable flushable))
786 (defknown hash-table-rehash-threshold (hash-table) (real 0 1)
787   (foldable flushable))
788 (defknown hash-table-size (hash-table) index (flushable))
789 (defknown hash-table-test (hash-table) symbol (foldable flushable))
790 (defknown sxhash (t) (integer 0 #.sb!xc:most-positive-fixnum)
791   (foldable flushable))
792 \f
793 ;;;; from the "Arrays" chapter
794
795 (defknown make-array ((or index list)
796                       &key
797                       (:element-type type-specifier)
798                       (:initial-element t)
799                       (:initial-contents t)
800                       (:adjustable t)
801                       (:fill-pointer t)
802                       (:displaced-to (or array null))
803                       (:displaced-index-offset index))
804   array (flushable unsafe))
805
806 (defknown vector (&rest t) simple-vector (flushable unsafe))
807
808 (defknown aref (array &rest index) t (foldable))
809 (defknown row-major-aref (array index) t (foldable))
810
811 (defknown array-element-type (array)
812   type-specifier
813   (foldable flushable recursive))
814 (defknown array-rank (array) array-rank (foldable flushable))
815 (defknown array-dimension (array array-rank) index (foldable flushable))
816 (defknown array-dimensions (array) list (foldable flushable))
817 (defknown array-in-bounds-p (array &rest integer) boolean (foldable flushable))
818 (defknown array-row-major-index (array &rest index) array-total-size
819   (foldable flushable))
820 (defknown array-total-size (array) array-total-size (foldable flushable))
821 (defknown adjustable-array-p (array) boolean (movable foldable flushable))
822
823 (defknown svref (simple-vector index) t (foldable flushable))
824 (defknown bit ((array bit) &rest index) bit (foldable flushable))
825 (defknown sbit ((simple-array bit) &rest index) bit (foldable flushable))
826
827 (defknown (bit-and bit-ior bit-xor bit-eqv bit-nand bit-nor bit-andc1 bit-andc2
828                    bit-orc1 bit-orc2)
829   ((array bit) (array bit) &optional (or (array bit) (member t nil)))
830   (array bit)
831   (foldable)
832   #|:derive-type #'result-type-last-arg|#)
833
834 (defknown bit-not ((array bit) &optional (or (array bit) (member t nil)))
835   (array bit)
836   (foldable)
837   #|:derive-type #'result-type-last-arg|#)
838
839 (defknown bit-vector-= (bit-vector bit-vector) boolean
840   (movable foldable flushable))
841
842 (defknown array-has-fill-pointer-p (array) boolean
843   (movable foldable flushable))
844 (defknown fill-pointer (vector) index (foldable unsafely-flushable))
845 (defknown vector-push (t vector) (or index null) ())
846 (defknown vector-push-extend (t vector &optional index) index ())
847 (defknown vector-pop (vector) t ())
848
849 (defknown adjust-array
850   (array (or index list) &key (:element-type type-specifier)
851          (:initial-element t) (:initial-contents t)
852          (:fill-pointer t) (:displaced-to (or array null))
853          (:displaced-index-offset index))
854   array (unsafe))
855 ;  :derive-type 'result-type-arg1) Not even close...
856 \f
857 ;;;; from the "Strings" chapter:
858
859 (defknown char (string index) character (foldable flushable))
860 (defknown schar (simple-string index) character (foldable flushable))
861
862 (sb!xc:deftype stringable () '(or character string symbol))
863
864 (defknown (string= string-equal)
865   (stringable stringable &key (:start1 index) (:end1 sequence-end)
866               (:start2 index) (:end2 sequence-end))
867   boolean
868   (foldable flushable))
869
870 (defknown (string< string> string<= string>= string/= string-lessp
871                    string-greaterp string-not-lessp string-not-greaterp
872                    string-not-equal)
873   (stringable stringable &key (:start1 index) (:end1 sequence-end)
874               (:start2 index) (:end2 sequence-end))
875   (or index null)
876   (foldable flushable))
877
878 (defknown make-string (index &key (:element-type type-specifier)
879                        (:initial-element character))
880   simple-string (flushable))
881
882 (defknown (string-trim string-left-trim string-right-trim)
883   (sequence stringable) simple-string (flushable))
884
885 (defknown (string-upcase string-downcase string-capitalize)
886   (stringable &key (:start index) (:end sequence-end))
887   simple-string (flushable))
888
889 (defknown (nstring-upcase nstring-downcase nstring-capitalize)
890   (string &key (:start index) (:end sequence-end))
891   string ())
892
893 (defknown string (stringable) string
894   (flushable explicit-check))
895 \f
896 ;;;; internal non-keyword versions of string predicates:
897
898 (defknown (string<* string>* string<=* string>=* string/=*)
899   (stringable stringable index sequence-end index sequence-end)
900   (or index null)
901   (foldable flushable))
902
903 (defknown string=*
904   (stringable stringable index sequence-end index sequence-end)
905   boolean
906   (foldable flushable))
907 \f
908 ;;;; from the "Eval" chapter:
909
910 (defknown eval (t) * (recursive))
911 (defknown constantp (t &optional lexenv-designator) boolean
912   (foldable flushable))
913 \f
914 ;;;; from the "Streams" chapter:
915
916 (defknown make-synonym-stream (symbol) stream (flushable))
917 (defknown make-broadcast-stream (&rest stream) stream (unsafely-flushable))
918 (defknown make-concatenated-stream (&rest stream) stream (unsafely-flushable))
919 (defknown make-two-way-stream (stream stream) stream (unsafely-flushable))
920 (defknown make-echo-stream (stream stream) stream (flushable))
921 (defknown make-string-input-stream (string &optional index index) stream
922   (flushable unsafe))
923 (defknown make-string-output-stream 
924     (&key (:element-type type-specifier)) 
925     stream 
926   (flushable))
927 (defknown get-output-stream-string (stream) simple-string ())
928 (defknown streamp (t) boolean (movable foldable flushable))
929 (defknown stream-element-type (stream) type-specifier
930   (movable foldable flushable))
931 (defknown (output-stream-p input-stream-p) (stream) boolean
932   (movable foldable flushable))
933 (defknown close (stream &key (:abort t)) (eql t) ())
934 \f
935 ;;;; from the "Input/Output" chapter:
936
937 ;;; (The I/O functions are given effects ANY under the theory that
938 ;;; code motion over I/O operations is particularly confusing and not
939 ;;; very important for efficiency.)
940
941 (defknown copy-readtable (&optional (or readtable null) (or readtable null))
942   readtable
943   ())
944 (defknown readtablep (t) boolean (movable foldable flushable))
945
946 (defknown set-syntax-from-char
947   (character character &optional (or readtable null) readtable) (eql t)
948   ())
949
950 (defknown set-macro-character (character callable &optional t readtable)
951   (eql t)
952   (unsafe))
953 (defknown get-macro-character (character &optional (or readtable null))
954   (values callable boolean) (flushable))
955
956 (defknown make-dispatch-macro-character (character &optional t readtable)
957   (eql t) ())
958 (defknown set-dispatch-macro-character
959   (character character callable &optional readtable) function
960   (unsafe))
961 (defknown get-dispatch-macro-character
962   (character character &optional (or readtable null)) (or callable null)
963   ())
964
965 (defknown copy-pprint-dispatch
966   (&optional (or sb!pretty:pprint-dispatch-table null))
967   sb!pretty:pprint-dispatch-table
968   ())
969 (defknown pprint-dispatch
970   (t &optional (or sb!pretty:pprint-dispatch-table null))
971   (values callable boolean)
972   ())
973 (defknown (pprint-fill pprint-linear)
974   (streamlike t &optional t t)
975   null
976   ())
977 (defknown pprint-tabular
978   (streamlike t &optional t t unsigned-byte)
979   null
980   ())
981 (defknown pprint-indent
982   ((member :block :current) real &optional streamlike)
983   null
984   ())
985 (defknown pprint-newline
986   ((member :linear :fill :miser :mandatory) &optional streamlike)
987   null
988   ())
989 (defknown pprint-tab
990   ((member :line :section :line-relative :section-relative)
991    unsigned-byte unsigned-byte &optional streamlike)
992   null
993   ())
994 (defknown set-pprint-dispatch
995   (type-specifier (or null callable)
996    &optional real sb!pretty:pprint-dispatch-table)
997   null
998   ())
999
1000 ;;; may return any type due to eof-value...
1001 (defknown (read read-preserving-whitespace read-char-no-hang read-char)
1002   (&optional streamlike t t t) t (explicit-check))
1003
1004 (defknown read-delimited-list (character &optional streamlike t) list
1005   (explicit-check))
1006 (defknown read-line (&optional streamlike t t t) (values t boolean)
1007   (explicit-check))
1008 (defknown unread-char (character &optional streamlike) t
1009   (explicit-check))
1010 (defknown peek-char (&optional (or character (member nil t)) streamlike t t t)
1011   t
1012   (explicit-check))
1013 (defknown listen (&optional streamlike) boolean (flushable explicit-check))
1014
1015 (defknown clear-input (&optional stream) null (explicit-check))
1016
1017 (defknown read-from-string
1018   (string &optional t t
1019           &key
1020           (:start index)
1021           (:end sequence-end)
1022           (:preserve-whitespace t))
1023   (values t index))
1024 (defknown parse-integer
1025   (string &key
1026           (:start index)
1027           (:end sequence-end)
1028           (:radix (integer 2 36))
1029           (:junk-allowed t))
1030   (values (or integer null ()) index))
1031
1032 (defknown read-byte (stream &optional t t) t (explicit-check))
1033
1034 (defknown write
1035   (t &key
1036      (:stream streamlike)
1037      (:escape t)
1038      (:radix t)
1039      (:base (integer 2 36))
1040      (:circle t)
1041      (:pretty t)
1042      (:level (or unsigned-byte null))
1043      (:readably t)
1044      (:length (or unsigned-byte null))
1045      (:case t)
1046      (:array t)
1047      (:gensym t)
1048      (:lines (or unsigned-byte null))
1049      (:right-margin (or unsigned-byte null))
1050      (:miser-width (or unsigned-byte null))
1051      (:pprint-dispatch t))
1052   t
1053   (any explicit-check)
1054   :derive-type #'result-type-first-arg)
1055
1056 (defknown (prin1 print princ) (t &optional streamlike) t (any explicit-check)
1057   :derive-type #'result-type-first-arg)
1058
1059 ;;; xxx-TO-STRING functions are not foldable because they depend on
1060 ;;; the dynamic environment.
1061 (defknown write-to-string
1062   (t &key (:escape t) (:radix t) (:base (integer 2 36)) (:readably t)
1063      (:circle t) (:pretty t) (:level (or unsigned-byte null))
1064      (:length (or unsigned-byte null)) (:case t) (:array t) (:gensym t)
1065      (:lines (or unsigned-byte null)) (:right-margin (or unsigned-byte null))
1066      (:miser-width (or unsigned-byte null)) (:pprint-dispatch t))
1067   simple-string
1068   (foldable flushable explicit-check))
1069
1070 (defknown (prin1-to-string princ-to-string) (t) simple-string (flushable))
1071
1072 (defknown write-char (character &optional streamlike) character
1073   (explicit-check))
1074 (defknown (write-string write-line)
1075   (string &optional streamlike &key (:start index) (:end sequence-end))
1076   string
1077   (explicit-check))
1078
1079 (defknown (terpri finish-output force-output clear-output)
1080   (&optional streamlike) null
1081   (explicit-check))
1082
1083 (defknown fresh-line (&optional streamlike) boolean
1084   (explicit-check))
1085
1086 (defknown write-byte (integer stream) integer
1087   (explicit-check))
1088
1089 (defknown format ((or streamlike string) (or string function) &rest t)
1090   (or string null)
1091   (explicit-check))
1092
1093 (defknown (y-or-n-p yes-or-no-p) (&optional string &rest t) boolean
1094   (explicit-check))
1095 \f
1096 ;;;; from the "File System Interface" chapter:
1097
1098 ;;; (No pathname functions are FOLDABLE because they all potentially
1099 ;;; depend on *DEFAULT-PATHNAME-DEFAULTS*, e.g. to provide a default
1100 ;;; host when parsing a namestring. They are not FLUSHABLE because
1101 ;;; parsing of a PATHNAME-DESIGNATOR might signal an error.)
1102
1103 (defknown wild-pathname-p (pathname-designator
1104                            &optional
1105                            (member nil :host :device
1106                                    :directory :name
1107                                    :type :version))
1108   generalized-boolean
1109   ())
1110 (defknown pathname-match-p (pathname-designator pathname-designator)
1111   generalized-boolean
1112   ())
1113 (defknown translate-pathname (pathname-designator
1114                               pathname-designator
1115                               pathname-designator &key)
1116   pathname
1117   ())
1118
1119 (defknown logical-pathname (pathname-designator) logical-pathname ())
1120 (defknown translate-logical-pathname (pathname-designator &key) pathname
1121   (recursive))
1122 (defknown load-logical-pathname-translations (string) t ())
1123 (defknown logical-pathname-translations (logical-host-designator) list ())
1124
1125 (defknown pathname (pathname-designator) pathname (unsafely-flushable))
1126 (defknown truename (pathname-designator) pathname ())
1127
1128 (defknown parse-namestring
1129   (pathname-designator &optional
1130                        (or list host string (member :unspecific))
1131                        pathname-designator
1132                        &key
1133                        (:start index)
1134                        (:end sequence-end)
1135                        (:junk-allowed t))
1136   (values (or pathname null) sequence-end)
1137   ())
1138
1139 (defknown merge-pathnames
1140   (pathname-designator &optional pathname-designator pathname-version)
1141   pathname
1142   (unsafely-flushable))
1143
1144 (defknown make-pathname
1145  (&key (:defaults pathname-designator)
1146        (:host (or string pathname-host))
1147        (:device (or string pathname-device))
1148        (:directory (or pathname-directory string (member :wild)))
1149        (:name (or pathname-name string (member :wild)))
1150        (:type (or pathname-type string (member :wild)))
1151        (:version pathname-version) (:case (member :local :common)))
1152   pathname (unsafely-flushable))
1153
1154 (defknown pathnamep (t) boolean (movable flushable))
1155
1156 (defknown pathname-host (pathname-designator
1157                          &key (:case (member :local :common)))
1158   pathname-host (flushable))
1159 (defknown pathname-device (pathname-designator
1160                            &key (:case (member :local :common)))
1161   pathname-device (flushable))
1162 (defknown pathname-directory (pathname-designator
1163                               &key (:case (member :local :common)))
1164   pathname-directory (flushable))
1165 (defknown pathname-name (pathname-designator
1166                          &key (:case (member :local :common)))
1167   pathname-name (flushable))
1168 (defknown pathname-type (pathname-designator
1169                          &key (:case (member :local :common)))
1170   pathname-type (flushable))
1171 (defknown pathname-version (pathname-designator)
1172   pathname-version (flushable))
1173
1174 (defknown (namestring file-namestring directory-namestring host-namestring)
1175   (pathname-designator) (or simple-string null)
1176   (unsafely-flushable))
1177
1178 (defknown enough-namestring (pathname-designator &optional pathname-designator)
1179   simple-string
1180   (unsafely-flushable))
1181
1182 (defknown user-homedir-pathname (&optional t) pathname (flushable))
1183
1184 (defknown open
1185   (pathname-designator &key
1186                        (:direction (member :input :output :io :probe))
1187                        (:element-type type-specifier)
1188                        (:if-exists (member :error :new-version :rename
1189                                            :rename-and-delete :overwrite
1190                                            :append :supersede nil))
1191                        (:if-does-not-exist (member :error :create nil))
1192                        (:external-format (member :default)))
1193   (or stream null))
1194
1195 (defknown rename-file (pathname-designator filename)
1196   (values pathname pathname pathname))
1197 (defknown delete-file (pathname-designator) t)
1198 (defknown probe-file (pathname-designator) (or pathname null) ())
1199 (defknown file-write-date (pathname-designator) (or unsigned-byte null)
1200   ())
1201 (defknown file-author (pathname-designator) (or simple-string null)
1202   ())
1203
1204 (defknown file-position (stream &optional
1205                                 (or unsigned-byte (member :start :end)))
1206   (or unsigned-byte (member t nil)))
1207 (defknown file-length (stream) (or unsigned-byte null) (unsafely-flushable))
1208
1209 (defknown load
1210   ((or filename stream)
1211    &key
1212    (:verbose t)
1213    (:print t)
1214    (:if-does-not-exist (member :error :create nil))
1215    (:external-format (member :default)))
1216   t)
1217
1218 (defknown directory (pathname-designator &key)
1219   list ())
1220 \f
1221 ;;;; from the "Conditions" chapter:
1222
1223 (defknown cell-error-name (cell-error) t)
1224 (defknown error (t &rest t) nil)
1225 (defknown cerror (format-control t &rest t) null)
1226 (defknown invalid-method-error (t format-control &rest t) *) ; FIXME: first arg is METHOD
1227 (defknown method-combination-error (format-control &rest t) *)
1228 (defknown signal (t &rest t) null)
1229 (defknown simple-condition-format-control (condition)
1230   format-control)
1231 (defknown simple-condition-format-arguments (condition)
1232   list)
1233 (defknown warn (t &rest t) null)
1234 (defknown invoke-debugger (condition) nil)
1235 (defknown break (&optional format-control &rest t) null)
1236 (defknown make-condition (type-specifier &rest t) condition)
1237 (defknown compute-restarts (&optional (or condition null)) list)
1238 (defknown find-restart (restart-designator &optional (or condition null))
1239   (or restart null))
1240 (defknown invoke-restart (restart-designator &rest t) *)
1241 (defknown invoke-restart-interactively (restart-designator) *)
1242 (defknown restart-name (restart) symbol)
1243 (defknown (abort muffle-warning) (&optional (or condition null)) nil)
1244 (defknown continue (&optional (or condition null)) null)
1245 (defknown (store-value use-value) (t &optional (or condition null))
1246   null)
1247
1248 ;;; and analogous SBCL extension:
1249 (defknown bug (t &rest t) nil) ; never returns
1250 \f
1251 ;;;; from the "Miscellaneous" Chapter:
1252
1253 (defknown compile ((or symbol cons) &optional (or list function null))
1254   (values (or function symbol cons) boolean boolean))
1255
1256 (defknown compile-file
1257   (filename
1258    &key
1259
1260    ;; ANSI options
1261    (:output-file (or filename
1262                      null
1263                      ;; FIXME: This last case is a non-ANSI hack.
1264                      (member t)))
1265    (:verbose t)
1266    (:print t)
1267    (:external-format t)
1268
1269    ;; extensions
1270    (:trace-file t)
1271    (:block-compile t))
1272   (values (or pathname null) boolean boolean))
1273
1274 ;; FIXME: consider making (OR CALLABLE CONS) something like
1275 ;; EXTENDED-FUNCTION-DESIGNATOR
1276 (defknown disassemble ((or callable cons) &key
1277                        (:stream stream) (:use-labels t))
1278   null)
1279
1280 (defknown fdocumentation (t symbol)
1281   (or string null)
1282   (flushable))
1283
1284 (defknown describe (t &optional (or stream (member t nil))) (values))
1285 (defknown inspect (t) (values))
1286 (defknown room (&optional (member t nil :default)) (values))
1287 (defknown ed (&optional (or symbol cons filename) &key (:init t) (:display t))
1288   t)
1289 (defknown dribble (&optional filename &key (:if-exists t)) (values))
1290
1291 (defknown apropos      (stringable &optional package-designator t) (values))
1292 (defknown apropos-list (stringable &optional package-designator t) list
1293   (flushable))
1294
1295 (defknown get-decoded-time ()
1296   (values (integer 0 59) (integer 0 59) (integer 0 23) (integer 1 31)
1297           (integer 1 12) unsigned-byte (integer 0 6) boolean (rational -24 24))
1298   (flushable))
1299
1300 (defknown get-universal-time () unsigned-byte (flushable))
1301
1302 (defknown decode-universal-time
1303           (unsigned-byte &optional (or null (rational -24 24)))
1304   (values (integer 0 59) (integer 0 59) (integer 0 23) (integer 1 31)
1305           (integer 1 12) unsigned-byte (integer 0 6) boolean (rational -24 24))
1306   (flushable))
1307
1308 (defknown encode-universal-time
1309   ((integer 0 59) (integer 0 59) (integer 0 23) (integer 1 31)
1310    (integer 1 12) unsigned-byte &optional (or null (rational -24 24)))
1311   unsigned-byte
1312   (flushable))
1313
1314 (defknown (get-internal-run-time get-internal-real-time)
1315   () internal-time (flushable))
1316
1317 (defknown sleep ((or (rational 0) (float 0.0))) null)
1318
1319 ;;; Even though ANSI defines LISP-IMPLEMENTATION-TYPE and
1320 ;;; LISP-IMPLEMENTATION-VERSION to possibly punt and return NIL, we
1321 ;;; know that there's no valid reason for our implementations to ever
1322 ;;; do so, so we can safely guarantee that they'll return strings.
1323 (defknown (lisp-implementation-type lisp-implementation-version)
1324   () simple-string (flushable))
1325
1326 ;;; For any of these functions, meaningful information might not be
1327 ;;; available, so -- unlike the related LISP-IMPLEMENTATION-FOO
1328 ;;; functions -- these really can return NIL.
1329 (defknown (machine-type machine-version machine-instance
1330            software-type software-version
1331            short-site-name long-site-name)
1332   () (or simple-string null) (flushable))
1333
1334 (defknown identity (t) t (movable foldable flushable unsafe)
1335   :derive-type #'result-type-first-arg)
1336
1337 (defknown constantly (t) function (movable flushable))
1338 (defknown complement (function) function (movable flushable))
1339 \f
1340 ;;;; miscellaneous extensions
1341
1342 (defknown get-bytes-consed () unsigned-byte (flushable))
1343
1344 ;;; PCOUNTERs
1345 (defknown incf-pcounter (pcounter unsigned-byte) pcounter)
1346 (defknown pcounter->integer (pcounter) unsigned-byte)
1347 (defknown %incf-pcounter-or-fixnum ((or pcounter fixnum) unsigned-byte)
1348   (or pcounter fixnum))
1349 (defknown pcounter-or-fixnum->integer ((or pcounter fixnum)) unsigned-byte)
1350 \f
1351 ;;;; magical compiler frobs
1352
1353 ;;; We can't fold this in general because of SATISFIES. There is a
1354 ;;; special optimizer anyway.
1355 (defknown %typep (t (or type-specifier ctype)) boolean
1356   (movable flushable explicit-check))
1357 (defknown %instance-typep (t (or type-specifier ctype)) boolean
1358   (movable flushable explicit-check))
1359
1360 (defknown %cleanup-point () t)
1361 (defknown %special-bind (t t) t)
1362 (defknown %special-unbind (t) t)
1363 (defknown %listify-rest-args (t index) list (flushable))
1364 (defknown %more-arg-context (t t) (values t index) (flushable))
1365 (defknown %more-arg (t index) t)
1366 (defknown %more-arg-values (t index index) * (flushable))
1367 (defknown %verify-arg-count (index index) (values))
1368 (defknown %arg-count-error (t) nil)
1369 (defknown %unknown-values () *)
1370 (defknown %catch (t t) t)
1371 (defknown %unwind-protect (t t) t)
1372 (defknown (%catch-breakup %unwind-protect-breakup) () t)
1373 (defknown %lexical-exit-breakup (t) t)
1374 (defknown %continue-unwind (t t t) nil)
1375 (defknown %throw (t &rest t) nil) ; This is MV-called.
1376 (defknown %nlx-entry (t) *)
1377 (defknown %%primitive (t t &rest t) *)
1378 (defknown %pop-values (t) t)
1379 (defknown %type-check-error (t t) nil)
1380
1381 ;; FIXME: This function does not return, but due to the implementation
1382 ;; of FILTER-LVAR we cannot write it here.
1383 (defknown %compile-time-type-error (t t t) *)
1384
1385 (defknown %odd-key-args-error () nil)
1386 (defknown %unknown-key-arg-error (t) nil)
1387 (defknown (%ldb %mask-field) (bit-index bit-index integer) unsigned-byte
1388   (movable foldable flushable explicit-check))
1389 (defknown (%dpb %deposit-field) (integer bit-index bit-index integer) integer
1390   (movable foldable flushable explicit-check))
1391 (defknown %negate (number) number (movable foldable flushable explicit-check))
1392 (defknown %check-bound (array index fixnum) index (movable foldable flushable))
1393 (defknown data-vector-ref (simple-array index) t
1394   (foldable explicit-check))
1395 (defknown data-vector-set (array index t) t (unsafe explicit-check))
1396 (defknown hairy-data-vector-ref (array index) t
1397   (foldable explicit-check))
1398 (defknown hairy-data-vector-set (array index t) t (unsafe explicit-check))
1399 (defknown %caller-frame-and-pc () (values t t) (flushable))
1400 (defknown %with-array-data (array index (or index null))
1401   (values (simple-array * (*)) index index index)
1402   (foldable flushable))
1403 (defknown %set-symbol-package (symbol t) t (unsafe))
1404 (defknown %coerce-name-to-fun ((or symbol cons)) function (flushable))
1405 (defknown %coerce-callable-to-fun (callable) function (flushable))
1406 (defknown failed-%with-array-data (t t t) nil)
1407 (defknown %find-position
1408   (t sequence t index sequence-end function function)
1409   (values t (or index null))
1410   (flushable call))
1411 (defknown (%find-position-if %find-position-if-not)
1412   (function sequence t index sequence-end function)
1413   (values t (or index null))
1414   (call))
1415 (defknown effective-find-position-test (callable callable)
1416   function
1417   (flushable foldable))
1418 (defknown effective-find-position-key (callable)
1419   function
1420   (flushable foldable))
1421
1422 (defknown %check-vector-sequence-bounds (vector index sequence-end)
1423   index
1424   (unwind))
1425 ;;; FIXME: including this information here is probably necessary to
1426 ;;; get efficient compilation of the inline expansion of
1427 ;;; %FIND-POSITION-IF, so it should maybe be in a more
1428 ;;; compiler-friendly package (SB-INT?)
1429 (defknown sb!impl::signal-bounding-indices-bad-error
1430     (sequence index sequence-end)
1431   nil) ; never returns
1432   
1433
1434 (defknown arg-count-error (t t t t t t) nil (unsafe))
1435 \f
1436 ;;;; SETF inverses
1437
1438 (defknown %aset (array &rest t) t (unsafe))
1439 (defknown %set-row-major-aref (array index t) t (unsafe))
1440 (defknown %rplaca (cons t) t (unsafe))
1441 (defknown %rplacd (cons t) t (unsafe))
1442 (defknown %put (symbol t t) t (unsafe))
1443 (defknown %setelt (sequence index t) t (unsafe))
1444 (defknown %svset (simple-vector index t) t (unsafe))
1445 (defknown %bitset ((array bit) &rest index) bit (unsafe))
1446 (defknown %sbitset ((simple-array bit) &rest index) bit (unsafe))
1447 (defknown %charset (string index character) character (unsafe))
1448 (defknown %scharset (simple-string index character) character (unsafe))
1449 (defknown %set-symbol-value (symbol t) t (unsafe))
1450 (defknown (setf symbol-function) (function symbol) function (unsafe))
1451 (defknown %set-symbol-plist (symbol t) t (unsafe))
1452 (defknown (setf fdocumentation) ((or string null) t symbol)
1453   (or string null)
1454   ())
1455 (defknown %setnth (unsigned-byte list t) t (unsafe))
1456 (defknown %set-fill-pointer (vector index) index (unsafe))
1457 \f
1458 ;;;; ALIEN and call-out-to-C stuff
1459
1460 ;;; 'unsafe' attribute because we store the arg on the stack, which is in
1461 ;;; some sense 'passing it upwards'
1462 (defknown sb!vm::push-word-on-c-stack (system-area-pointer) (values) (unsafe))
1463 (defknown sb!vm::pop-words-from-c-stack (index) (values) ())
1464
1465 ;;;; miscellaneous internal utilities
1466
1467 (defknown %fun-name (function) t (flushable))
1468 (defknown (setf %fun-name) (t function) t (unsafe))
1469
1470 (defknown policy-quality (policy symbol) policy-quality
1471           (flushable))
1472
1473 (defknown (compiler-abort compiler-error) (string &rest t) nil ())
1474 (defknown (compiler-warn compiler-style-warn) (string &rest t) (values) ())
1475 (defknown (compiler-notify maybe-compiler-notify) ((or string symbol) &rest t)
1476   (values)
1477   ())
1478 (defknown style-warn (string &rest t) null ())