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