288bad7514b7f213216d1d81d6dc0dd5eefa8e4c
[sbcl.git] / src / compiler / ctype.lisp
1 ;;;; This file contains code which knows about both the type
2 ;;;; representation and the compiler IR1 representation. This stuff is
3 ;;;; used for doing type checking.
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 ;;;; FIXME: This is a poor name for this file, since CTYPE is the name
15 ;;;; of the type used internally to represent Lisp types. It'd
16 ;;;; probably be good to rename this file to "call-type.lisp" or
17 ;;;; "ir1-type.lisp" or something.
18
19 (in-package "SB!C")
20
21 (declaim (type (or function null) *lossage-fun* *unwinnage-fun* *ctype-test-fun*))
22
23 ;;; These are the functions that are to be called when a problem is
24 ;;; detected. They are passed format arguments. If null, we don't do
25 ;;; anything. The LOSSAGE function is called when something is
26 ;;; definitely incorrect. The UNWINNAGE function is called when it is
27 ;;; somehow impossible to tell whether the call is correct. (Thus,
28 ;;; they should correspond fairly closely to the FAILURE-P and WARNINGS-P
29 ;;; return values of CL:COMPILE and CL:COMPILE-FILE. However, see the
30 ;;; KLUDGE note below for *LOSSAGE-DETECTED*.)
31 (defvar *lossage-fun*)
32 (defvar *unwinnage-fun*)
33
34 ;;; the function that we use for type checking. The derived type is
35 ;;; its first argument and the type we are testing against is its
36 ;;; second argument. The function should return values like CSUBTYPEP.
37 (defvar *ctype-test-fun*)
38 ;;; FIXME: Why is this a variable? Explain.
39
40 ;;; *LOSSAGE-DETECTED* is set when a "definite incompatibility" is
41 ;;; detected. *UNWINNAGE-DETECTED* is set when we can't tell whether the
42 ;;; call is compatible or not. Thus, they should correspond very closely
43 ;;; to the FAILURE-P and WARNINGS-P return values of CL:COMPILE and
44 ;;; CL:COMPILE-FILE.) However...
45 ;;;
46 ;;; KLUDGE: Common Lisp is a dynamic language, even if CMU CL was not.
47 ;;; As far as I can see, none of the "definite incompatibilities"
48 ;;; detected in this file are actually definite under the ANSI spec.
49 ;;; They would be incompatibilites if the use were within the same
50 ;;; compilation unit as the contradictory definition (as per the spec
51 ;;; section "3.2.2.3 Semantic Constraints") but the old Python code
52 ;;; doesn't keep track of whether that's the case. So until/unless we
53 ;;; upgrade the code to keep track of that, we have to handle all
54 ;;; these as STYLE-WARNINGs. -- WHN 2001-02-10
55 (defvar *lossage-detected*)
56 (defvar *unwinnage-detected*)
57
58 ;;; Signal a warning if appropriate and set *FOO-DETECTED*.
59 (declaim (ftype (function (string &rest t) (values)) note-lossage note-unwinnage))
60 (defun note-lossage (format-string &rest format-args)
61   (setq *lossage-detected* t)
62   (when *lossage-fun*
63     (apply *lossage-fun* format-string format-args))
64   (values))
65 (defun note-unwinnage (format-string &rest format-args)
66   (setq *unwinnage-detected* t)
67   (when *unwinnage-fun*
68     (apply *unwinnage-fun* format-string format-args))
69   (values))
70
71 (declaim (special *compiler-error-context*))
72 \f
73 ;;;; stuff for checking a call against a function type
74 ;;;;
75 ;;;; FIXME: This is stuff to look at when I get around to fixing
76 ;;;; function type inference and declarations.
77
78 ;;; A dummy version of SUBTYPEP useful when we want a functional like
79 ;;; SUBTYPEP that always returns true.
80 (defun always-subtypep (type1 type2)
81   (declare (ignore type1 type2))
82   (values t t))
83
84 ;;; Determine whether a use of a function is consistent with its type.
85 ;;; These values are returned:
86 ;;;    T, T: the call is definitely valid.
87 ;;;    NIL, T: the call is definitely invalid.
88 ;;;    NIL, NIL: unable to determine whether the call is valid.
89 ;;;
90 ;;; The ARGUMENT-TEST function is used to determine whether an
91 ;;; argument type matches the type we are checking against. Similarly,
92 ;;; the RESULT-TEST is used to determine whether the result type
93 ;;; matches the specified result.
94 ;;;
95 ;;; Unlike the argument test, the result test may be called on values
96 ;;; or function types. If STRICT-RESULT is true and SAFETY is
97 ;;; non-zero, then the NODE-DERIVED-TYPE is always used. Otherwise, if
98 ;;; CONT's TYPE-CHECK is true, then the NODE-DERIVED-TYPE is
99 ;;; intersected with the CONT's ASSERTED-TYPE.
100 ;;;
101 ;;; The error and warning functions are functions that are called to
102 ;;; explain the result. We bind *COMPILER-ERROR-CONTEXT* to the
103 ;;; combination node so that COMPILER-WARNING and related functions
104 ;;; will do the right thing if they are supplied.
105 (defun valid-fun-use (call type &key
106                            ((:argument-test *ctype-test-fun*) #'csubtypep)
107                            (result-test #'values-subtypep)
108                            (strict-result nil)
109                            ((:lossage-fun *lossage-fun*))
110                            ((:unwinnage-fun *unwinnage-fun*)))
111   (declare (type function result-test) (type combination call)
112            ;; FIXME: Could TYPE here actually be something like
113            ;; (AND GENERIC-FUNCTION (FUNCTION (T) T))?  How
114            ;; horrible...  -- CSR, 2003-05-03
115            (type ctype type))
116   (let* ((*lossage-detected* nil)
117          (*unwinnage-detected* nil)
118          (*compiler-error-context* call)
119          (args (combination-args call))
120          (nargs (length args)))
121     (if (fun-type-p type)
122         (let* ((required (fun-type-required type))
123                (min-args (length required))
124                (optional (fun-type-optional type))
125                (max-args (+ min-args (length optional)))
126                (rest (fun-type-rest type))
127                (keyp (fun-type-keyp type)))
128           (cond
129             ((fun-type-wild-args type)
130              (do ((i 1 (1+ i))
131                   (arg args (cdr arg)))
132                  ((null arg))
133                (check-arg-type (car arg) *wild-type* i)))
134             ((not (or optional keyp rest))
135              (if (/= nargs min-args)
136                  (note-lossage
137                   "The function was called with ~R argument~:P, but wants exactly ~R."
138                   nargs min-args)
139                  (check-fixed-and-rest args required nil)))
140             ((< nargs min-args)
141              (note-lossage
142               "The function was called with ~R argument~:P, but wants at least ~R."
143               nargs min-args))
144             ((<= nargs max-args)
145              (check-fixed-and-rest args (append required optional) rest))
146             ((not (or keyp rest))
147              (note-lossage
148               "The function was called with ~R argument~:P, but wants at most ~R."
149               nargs max-args))
150             ((and keyp (oddp (- nargs max-args)))
151              (note-lossage
152               "The function has an odd number of arguments in the keyword portion."))
153             (t
154              (check-fixed-and-rest args (append required optional) rest)
155              (when keyp
156                (check-key-args args max-args type))))
157
158           (let* ((dtype (node-derived-type call))
159                  (return-type (fun-type-returns type))
160                  (cont (node-cont call))
161                  (out-type
162                   (if (or (not (continuation-type-check cont))
163                           (and strict-result (policy call (/= safety 0))))
164                       dtype
165                       (values-type-intersection (continuation-asserted-type cont)
166                                                 dtype))))
167             (multiple-value-bind (int win) (funcall result-test out-type return-type)
168               (cond ((not win)
169                      (note-unwinnage "can't tell whether the result is a ~S"
170                                      (type-specifier return-type)))
171                     ((not int)
172                      (note-lossage "The result is a ~S, not a ~S."
173                                    (type-specifier out-type)
174                                    (type-specifier return-type)))))))
175         (loop for arg in args
176               and i from 1
177               do (check-arg-type arg *wild-type* i)))
178     (cond (*lossage-detected* (values nil t))
179           (*unwinnage-detected* (values nil nil))
180           (t (values t t)))))
181
182 ;;; Check that the derived type of the continuation CONT is compatible
183 ;;; with TYPE. N is the arg number, for error message purposes. We
184 ;;; return true if arg is definitely o.k. If the type is a magic
185 ;;; CONSTANT-TYPE, then we check for the argument being a constant
186 ;;; value of the specified type. If there is a manifest type error
187 ;;; (DERIVED-TYPE = NIL), then we flame about the asserted type even
188 ;;; when our type is satisfied under the test.
189 (defun check-arg-type (cont type n)
190   (declare (type continuation cont) (type ctype type) (type index n))
191   (cond
192    ((not (constant-type-p type))
193     (let ((ctype (continuation-type cont)))
194       (multiple-value-bind (int win) (funcall *ctype-test-fun* ctype type)
195         (cond ((not win)
196                (note-unwinnage "can't tell whether the ~:R argument is a ~S"
197                                n (type-specifier type))
198                nil)
199               ((not int)
200                (note-lossage "The ~:R argument is a ~S, not a ~S."
201                              n (type-specifier ctype) (type-specifier type))
202                nil)
203               ((eq ctype *empty-type*)
204                (note-unwinnage "The ~:R argument never returns a value." n)
205                nil)
206               (t t)))))
207     ((not (constant-continuation-p cont))
208      (note-unwinnage "The ~:R argument is not a constant." n)
209      nil)
210     (t
211      (let ((val (continuation-value cont))
212            (type (constant-type-type type)))
213        (multiple-value-bind (res win) (ctypep val type)
214          (cond ((not win)
215                 (note-unwinnage "can't tell whether the ~:R argument is a ~
216                                 constant ~S:~%  ~S"
217                                 n (type-specifier type) val)
218                 nil)
219                ((not res)
220                 (note-lossage "The ~:R argument is not a constant ~S:~%  ~S"
221                               n (type-specifier type) val)
222                 nil)
223                (t t)))))))
224
225 ;;; Check that each of the type of each supplied argument intersects
226 ;;; with the type specified for that argument. If we can't tell, then
227 ;;; we can complain about the absence of manifest winnage.
228 (declaim (ftype (function (list list (or ctype null)) (values)) check-fixed-and-rest))
229 (defun check-fixed-and-rest (args types rest)
230   (do ((arg args (cdr arg))
231        (type types (cdr type))
232        (n 1 (1+ n)))
233       ((or (null type) (null arg))
234        (when rest
235          (dolist (arg arg)
236            (check-arg-type arg rest n)
237            (incf n))))
238     (declare (fixnum n))
239     (check-arg-type (car arg) (car type) n))
240   (values))
241
242 ;;; Check that the &KEY args are of the correct type. Each key should
243 ;;; be known and the corresponding argument should be of the correct
244 ;;; type. If the key isn't a constant, then we can't tell, so we can
245 ;;; complain about absence of manifest winnage.
246 (declaim (ftype (function (list fixnum fun-type) (values)) check-key-args))
247 (defun check-key-args (args pre-key type)
248   (do ((key (nthcdr pre-key args) (cddr key))
249        (n (1+ pre-key) (+ n 2)))
250       ((null key))
251     (declare (fixnum n))
252     (let ((k (car key)))
253       (cond
254        ((not (check-arg-type k (specifier-type 'symbol) n)))
255        ((not (constant-continuation-p k))
256         (note-unwinnage "The ~:R argument (in keyword position) is not a ~
257                         constant."
258                         n))
259        (t
260         (let* ((name (continuation-value k))
261                (info (find name (fun-type-keywords type)
262                            :key #'key-info-name)))
263           (cond ((not info)
264                  (unless (fun-type-allowp type)
265                    (note-lossage "~S is not a known argument keyword."
266                                  name)))
267                 (t
268                  (check-arg-type (second key) (key-info-type info)
269                                  (1+ n)))))))))
270   (values))
271
272 ;;; Construct a function type from a definition.
273 ;;;
274 ;;; Due to the lack of a (LIST X) type specifier, we can't reconstruct
275 ;;; the &REST type.
276 (declaim (ftype (function (functional) fun-type) definition-type))
277 (defun definition-type (functional)
278   (if (lambda-p functional)
279       (make-fun-type
280        :required (mapcar #'leaf-type (lambda-vars functional))
281        :returns (tail-set-type (lambda-tail-set functional)))
282       (let ((rest nil))
283         (collect ((req)
284                   (opt)
285                   (keys))
286           (dolist (arg (optional-dispatch-arglist functional))
287             (let ((info (lambda-var-arg-info arg))
288                   (type (leaf-type arg)))
289               (if info
290                   (ecase (arg-info-kind info)
291                     (:required (req type))
292                     (:optional (opt type))
293                     (:keyword
294                      (keys (make-key-info :name (arg-info-key info)
295                                           :type type)))
296                     ((:rest :more-context)
297                      (setq rest *universal-type*))
298                     (:more-count))
299                   (req type))))
300
301           (make-fun-type
302            :required (req)
303            :optional (opt)
304            :rest rest
305            :keywords (keys)
306            :keyp (optional-dispatch-keyp functional)
307            :allowp (optional-dispatch-allowp functional)
308            :returns (tail-set-type
309                      (lambda-tail-set
310                       (optional-dispatch-main-entry functional))))))))
311 \f
312 ;;;; approximate function types
313 ;;;;
314 ;;;; FIXME: This is stuff to look at when I get around to fixing function
315 ;;;; type inference and declarations.
316 ;;;;
317 ;;;; Approximate function types provide a condensed representation of all the
318 ;;;; different ways that a function has been used. If we have no declared or
319 ;;;; defined type for a function, then we build an approximate function type by
320 ;;;; examining each use of the function. When we encounter a definition or
321 ;;;; proclamation, we can check the actual type for compatibity with the
322 ;;;; previous uses.
323
324 (defstruct (approximate-fun-type (:copier nil))
325   ;; the smallest and largest numbers of arguments that this function
326   ;; has been called with.
327   (min-args sb!xc:call-arguments-limit :type fixnum)
328   (max-args 0 :type fixnum)
329   ;; a list of lists of the all the types that have been used in each
330   ;; argument position
331   (types () :type list)
332   ;; A list of APPROXIMATE-KEY-INFO structures describing all the
333   ;; things that looked like &KEY arguments. There are distinct
334   ;; structures describing each argument position in which the keyword
335   ;; appeared.
336   (keys () :type list))
337
338 (defstruct (approximate-key-info (:copier nil))
339   ;; The keyword name of this argument. Although keyword names don't
340   ;; have to be keywords, we only match on keywords when figuring an
341   ;; approximate type.
342   (name (missing-arg) :type keyword)
343   ;; The position at which this keyword appeared. 0 if it appeared as the
344   ;; first argument, etc.
345   (position (missing-arg) :type fixnum)
346   ;; a list of all the argument types that have been used with this keyword
347   (types nil :type list)
348   ;; true if this keyword has appeared only in calls with an obvious
349   ;; :ALLOW-OTHER-KEYS
350   (allowp nil :type (member t nil)))
351
352 ;;; Return an APPROXIMATE-FUN-TYPE representing the context of
353 ;;; CALL. If TYPE is supplied and not null, then we merge the
354 ;;; information into the information already accumulated in TYPE.
355 (declaim (ftype (function (combination
356                            &optional (or approximate-fun-type null))
357                           approximate-fun-type)
358                 note-fun-use))
359 (defun note-fun-use (call &optional type)
360   (let* ((type (or type (make-approximate-fun-type)))
361          (types (approximate-fun-type-types type))
362          (args (combination-args call))
363          (nargs (length args))
364          (allowp (some (lambda (x)
365                          (and (constant-continuation-p x)
366                               (eq (continuation-value x) :allow-other-keys)))
367                        args)))
368
369     (setf (approximate-fun-type-min-args type)
370           (min (approximate-fun-type-min-args type) nargs))
371     (setf (approximate-fun-type-max-args type)
372           (max (approximate-fun-type-max-args type) nargs))
373
374     (do ((old types (cdr old))
375          (arg args (cdr arg)))
376         ((null old)
377          (setf (approximate-fun-type-types type)
378                (nconc types
379                       (mapcar (lambda (x)
380                                 (list (continuation-type x)))
381                               arg))))
382       (when (null arg) (return))
383       (pushnew (continuation-type (car arg))
384                (car old)
385                :test #'type=))
386
387     (collect ((keys (approximate-fun-type-keys type) cons))
388       (do ((arg args (cdr arg))
389            (pos 0 (1+ pos)))
390           ((or (null arg) (null (cdr arg)))
391            (setf (approximate-fun-type-keys type) (keys)))
392         (let ((key (first arg))
393               (val (second arg)))
394           (when (constant-continuation-p key)
395             (let ((name (continuation-value key)))
396               (when (keywordp name)
397                 (let ((old (find-if
398                             (lambda (x)
399                               (and (eq (approximate-key-info-name x) name)
400                                    (= (approximate-key-info-position x)
401                                       pos)))
402                             (keys)))
403                       (val-type (continuation-type val)))
404                   (cond (old
405                          (pushnew val-type
406                                   (approximate-key-info-types old)
407                                   :test #'type=)
408                          (unless allowp
409                            (setf (approximate-key-info-allowp old) nil)))
410                         (t
411                          (keys (make-approximate-key-info
412                                 :name name
413                                 :position pos
414                                 :allowp allowp
415                                 :types (list val-type))))))))))))
416     type))
417
418 ;;; This is similar to VALID-FUNCTION-USE, but checks an
419 ;;; APPROXIMATE-FUN-TYPE against a real function type.
420 (declaim (ftype (function (approximate-fun-type fun-type
421                            &optional function function function)
422                           (values boolean boolean))
423                 valid-approximate-type))
424 (defun valid-approximate-type (call-type type &optional
425                                          (*ctype-test-fun*
426                                           #'types-equal-or-intersect)
427                                          (*lossage-fun*
428                                           #'compiler-style-warn)
429                                          (*unwinnage-fun* #'compiler-note))
430   (let* ((*lossage-detected* nil)
431          (*unwinnage-detected* nil)
432          (required (fun-type-required type))
433          (min-args (length required))
434          (optional (fun-type-optional type))
435          (max-args (+ min-args (length optional)))
436          (rest (fun-type-rest type))
437          (keyp (fun-type-keyp type)))
438
439     (when (fun-type-wild-args type)
440       (return-from valid-approximate-type (values t t)))
441
442     (let ((call-min (approximate-fun-type-min-args call-type)))
443       (when (< call-min min-args)
444         (note-lossage
445          "~:@<The function was previously called with ~R argument~:P, ~
446           but wants at least ~R.~:>"
447          call-min min-args)))
448
449     (let ((call-max (approximate-fun-type-max-args call-type)))
450       (cond ((<= call-max max-args))
451             ((not (or keyp rest))
452              (note-lossage
453               "~:@<The function was previously called with ~R argument~:P, ~
454                 but wants at most ~R.~:>"
455               call-max max-args))
456             ((and keyp (oddp (- call-max max-args)))
457              (note-lossage
458               "~:@<The function was previously called with an odd number of ~
459                arguments in the keyword portion.~:>")))
460
461       (when (and keyp (> call-max max-args))
462         (check-approximate-keywords call-type max-args type)))
463
464     (check-approximate-fixed-and-rest call-type (append required optional)
465                                       rest)
466
467     (cond (*lossage-detected* (values nil t))
468           (*unwinnage-detected* (values nil nil))
469           (t (values t t)))))
470
471 ;;; Check that each of the types used at each arg position is
472 ;;; compatible with the actual type.
473 (declaim (ftype (function (approximate-fun-type list (or ctype null))
474                           (values))
475                 check-approximate-fixed-and-rest))
476 (defun check-approximate-fixed-and-rest (call-type fixed rest)
477   (do ((types (approximate-fun-type-types call-type) (cdr types))
478        (n 1 (1+ n))
479        (arg fixed (cdr arg)))
480       ((null types))
481     (let ((decl-type (or (car arg) rest)))
482       (unless decl-type (return))
483       (check-approximate-arg-type (car types) decl-type "~:R" n)))
484   (values))
485
486 ;;; Check that each of the call-types is compatible with DECL-TYPE,
487 ;;; complaining if not or if we can't tell.
488 (declaim (ftype (function (list ctype string &rest t) (values))
489                 check-approximate-arg-type))
490 (defun check-approximate-arg-type (call-types decl-type context &rest args)
491   (let ((losers *empty-type*))
492     (dolist (ctype call-types)
493       (multiple-value-bind (int win) (funcall *ctype-test-fun* ctype decl-type)
494         (cond
495          ((not win)
496           (note-unwinnage "can't tell whether previous ~? ~
497                            argument type ~S is a ~S"
498                           context
499                           args
500                           (type-specifier ctype)
501                           (type-specifier decl-type)))
502          ((not int)
503           (setq losers (type-union ctype losers))))))
504
505     (unless (eq losers *empty-type*)
506       (note-lossage "~:(~?~) argument should be a ~S but was a ~S in a previous call."
507                     context args (type-specifier decl-type) (type-specifier losers))))
508   (values))
509
510 ;;; Check the types of each manifest keyword that appears in a keyword
511 ;;; argument position. Check the validity of all keys that appeared in
512 ;;; valid keyword positions.
513 ;;;
514 ;;; ### We could check the APPROXIMATE-FUN-TYPE-TYPES to make
515 ;;; sure that all arguments in keyword positions were manifest
516 ;;; keywords.
517 (defun check-approximate-keywords (call-type max-args type)
518   (let ((call-keys (approximate-fun-type-keys call-type))
519         (keys (fun-type-keywords type)))
520     (dolist (key keys)
521       (let ((name (key-info-name key)))
522         (collect ((types nil append))
523           (dolist (call-key call-keys)
524             (let ((pos (approximate-key-info-position call-key)))
525               (when (and (eq (approximate-key-info-name call-key) name)
526                          (> pos max-args) (evenp (- pos max-args)))
527                 (types (approximate-key-info-types call-key)))))
528           (check-approximate-arg-type (types) (key-info-type key) "~S" name))))
529
530     (unless (fun-type-allowp type)
531       (collect ((names () adjoin))
532         (dolist (call-key call-keys)
533           (let ((pos (approximate-key-info-position call-key)))
534             (when (and (> pos max-args) (evenp (- pos max-args))
535                        (not (approximate-key-info-allowp call-key)))
536               (names (approximate-key-info-name call-key)))))
537
538         (dolist (name (names))
539           (unless (find name keys :key #'key-info-name)
540             (note-lossage "Function previously called with unknown argument keyword ~S."
541                   name)))))))
542 \f
543 ;;;; ASSERT-DEFINITION-TYPE
544
545 ;;; Intersect LAMBDA's var types with TYPES, giving a warning if there
546 ;;; is a mismatch. If all intersections are non-null, we return lists
547 ;;; of the variables and intersections, otherwise we return NIL, NIL.
548 (defun try-type-intersections (vars types where)
549   (declare (list vars types) (string where))
550   (collect ((res))
551     (mapc (lambda (var type)
552             (let* ((vtype (leaf-type var))
553                    (int (type-approx-intersection2 vtype type)))
554               (cond
555                ((eq int *empty-type*)
556                 (note-lossage
557                  "Definition's declared type for variable ~A:~%  ~S~@
558                   conflicts with this type from ~A:~%  ~S"
559                  (leaf-debug-name var) (type-specifier vtype)
560                  where (type-specifier type))
561                 (return-from try-type-intersections (values nil nil)))
562                (t
563                 (res int)))))
564           vars types)
565     (values vars (res))))
566
567 ;;; Check that the optional-dispatch OD conforms to Type. We return
568 ;;; the values of TRY-TYPE-INTERSECTIONS if there are no syntax
569 ;;; problems, otherwise NIL, NIL.
570 ;;;
571 ;;; Note that the variables in the returned list are the actual
572 ;;; original variables (extracted from the optional dispatch arglist),
573 ;;; rather than the variables that are arguments to the main entry.
574 ;;; This difference is significant only for &KEY args with hairy
575 ;;; defaults. Returning the actual vars allows us to use the right
576 ;;; variable name in warnings.
577 ;;;
578 ;;; A slightly subtle point: with keywords and optionals, the type in
579 ;;; the function type is only an assertion on calls --- it doesn't
580 ;;; constrain the type of default values. So we have to union in the
581 ;;; type of the default. With optionals, we can't do any assertion
582 ;;; unless the default is constant.
583 ;;;
584 ;;; With keywords, we exploit our knowledge about how hairy keyword
585 ;;; defaulting is done when computing the type assertion to put on the
586 ;;; main-entry argument. In the case of hairy keywords, the default
587 ;;; has been clobbered with NIL, which is the value of the main-entry
588 ;;; arg in the unsupplied case, whatever the actual default value is.
589 ;;; So we can just assume the default is constant, effectively
590 ;;; unioning in NULL, and not totally blow off doing any type
591 ;;; assertion.
592 (defun find-optional-dispatch-types (od type where)
593   (declare (type optional-dispatch od)
594            (type fun-type type)
595            (string where))
596   (let* ((min (optional-dispatch-min-args od))
597          (req (fun-type-required type))
598          (opt (fun-type-optional type)))
599     (flet ((frob (x y what)
600              (unless (= x y)
601                (note-lossage
602                 "The definition has ~R ~A arg~P, but ~A has ~R."
603                 x what x where y))))
604       (frob min (length req) "fixed")
605       (frob (- (optional-dispatch-max-args od) min) (length opt) "optional"))
606     (flet ((frob (x y what)
607              (unless (eq x y)
608                (note-lossage
609                 "The definition ~:[doesn't have~;has~] ~A, but ~
610                 ~A ~:[doesn't~;does~]."
611                 x what where y))))
612       (frob (optional-dispatch-keyp od) (fun-type-keyp type)
613             "&KEY arguments")
614       (unless (optional-dispatch-keyp od)
615         (frob (not (null (optional-dispatch-more-entry od)))
616               (not (null (fun-type-rest type)))
617               "&REST arguments"))
618       (frob (optional-dispatch-allowp od) (fun-type-allowp type)
619             "&ALLOW-OTHER-KEYS"))
620
621     (when *lossage-detected*
622       (return-from find-optional-dispatch-types (values nil nil)))
623
624     (collect ((res)
625               (vars))
626       (let ((keys (fun-type-keywords type))
627             (arglist (optional-dispatch-arglist od)))
628         (dolist (arg arglist)
629           (cond
630            ((lambda-var-arg-info arg)
631             (let* ((info (lambda-var-arg-info arg))
632                    (default (arg-info-default info))
633                    (def-type (when (constantp default)
634                                (ctype-of (eval default)))))
635               (ecase (arg-info-kind info)
636                 (:keyword
637                  (let* ((key (arg-info-key info))
638                         (kinfo (find key keys :key #'key-info-name)))
639                    (cond
640                     (kinfo
641                      (res (type-union (key-info-type kinfo)
642                                       (or def-type (specifier-type 'null)))))
643                     (t
644                      (note-lossage
645                       "Defining a ~S keyword not present in ~A."
646                       key where)
647                      (res *universal-type*)))))
648                 (:required (res (pop req)))
649                 (:optional
650                  (res (type-union (pop opt) (or def-type *universal-type*))))
651                 (:rest
652                  (when (fun-type-rest type)
653                    (res (specifier-type 'list))))
654                 (:more-context
655                  (when (fun-type-rest type)
656                    (res *universal-type*)))
657                 (:more-count
658                  (when (fun-type-rest type)
659                    (res (specifier-type 'fixnum)))))
660               (vars arg)
661               (when (arg-info-supplied-p info)
662                 (res *universal-type*)
663                 (vars (arg-info-supplied-p info)))))
664            (t
665             (res (pop req))
666             (vars arg))))
667
668         (dolist (key keys)
669           (unless (find (key-info-name key) arglist
670                         :key (lambda (x)
671                                (let ((info (lambda-var-arg-info x)))
672                                  (when info
673                                    (arg-info-key info)))))
674             (note-lossage
675              "The definition lacks the ~S key present in ~A."
676              (key-info-name key) where))))
677
678       (try-type-intersections (vars) (res) where))))
679
680 ;;; Check that TYPE doesn't specify any funny args, and do the
681 ;;; intersection.
682 (defun find-lambda-types (lambda type where)
683   (declare (type clambda lambda) (type fun-type type) (string where))
684   (flet ((frob (x what)
685            (when x
686              (note-lossage
687               "The definition has no ~A, but the ~A did."
688               what where))))
689     (frob (fun-type-optional type) "&OPTIONAL arguments")
690     (frob (fun-type-keyp type) "&KEY arguments")
691     (frob (fun-type-rest type) "&REST argument"))
692   (let* ((vars (lambda-vars lambda))
693          (nvars (length vars))
694          (req (fun-type-required type))
695          (nreq (length req)))
696     (unless (= nvars nreq)
697       (note-lossage "The definition has ~R arg~:P, but the ~A has ~R."
698                     nvars where nreq))
699     (if *lossage-detected*
700         (values nil nil)
701         (try-type-intersections vars req where))))
702
703 ;;; Check for syntactic and type conformance between the definition
704 ;;; FUNCTIONAL and the specified FUN-TYPE. If they are compatible
705 ;;; and REALLY-ASSERT is T, then add type assertions to the definition
706 ;;; from the FUN-TYPE.
707 ;;;
708 ;;; If there is a syntactic or type problem, then we call
709 ;;; LOSSAGE-FUN with an error message using WHERE as context
710 ;;; describing where FUN-TYPE came from.
711 ;;;
712 ;;; If there is no problem, we return T (even if REALLY-ASSERT was
713 ;;; false). If there was a problem, we return NIL.
714 (defun assert-definition-type
715        (functional type &key (really-assert t)
716                    ((:lossage-fun *lossage-fun*)
717                     #'compiler-style-warn)
718                    unwinnage-fun
719                    (where "previous declaration"))
720   (declare (type functional functional)
721            (type function *lossage-fun*)
722            (string where))
723   (unless (fun-type-p type)
724     (return-from assert-definition-type t))
725   (let ((*lossage-detected* nil))
726     (multiple-value-bind (vars types)
727         (if (fun-type-wild-args type)
728             (values nil nil)
729             (etypecase functional
730               (optional-dispatch
731                (find-optional-dispatch-types functional type where))
732               (clambda
733                (find-lambda-types functional type where))))
734       (let* ((type-returns (fun-type-returns type))
735              (return (lambda-return (main-entry functional)))
736              (atype (when return
737                       (continuation-asserted-type (return-result return)))))
738         (cond
739          ((and atype (not (values-types-equal-or-intersect atype
740                                                            type-returns)))
741           (note-lossage
742            "The result type from ~A:~%  ~S~@
743            conflicts with the definition's result type assertion:~%  ~S"
744            where (type-specifier type-returns) (type-specifier atype))
745           nil)
746          (*lossage-detected* nil)
747          ((not really-assert) t)
748          (t
749           (when atype
750             (assert-continuation-type (return-result return) atype
751                                       (lexenv-policy (functional-lexenv functional))))
752           (loop for var in vars and type in types do
753             (cond ((basic-var-sets var)
754                    (when (and unwinnage-fun
755                               (not (csubtypep (leaf-type var) type)))
756                      (funcall unwinnage-fun
757                               "Assignment to argument: ~S~%  ~
758                                prevents use of assertion from function ~
759                                type ~A:~%  ~S~%"
760                               (leaf-debug-name var)
761                               where
762                               (type-specifier type))))
763                   (t
764                    (setf (leaf-type var) type)
765                    (dolist (ref (leaf-refs var))
766                      (derive-node-type ref type)))))
767           t))))))
768
769 (defun assert-global-function-definition-type (name fun)
770   (declare (type functional fun))
771   (let ((type (info :function :type name))
772         (where (info :function :where-from name)))
773     (when (eq where :declared)
774       (setf (leaf-type fun) type)
775       (assert-definition-type fun type
776                               :unwinnage-fun #'compiler-note
777                               :where "proclamation"))))
778 \f
779 ;;;; FIXME: Move to some other file.
780 (defun check-catch-tag-type (tag)
781   (declare (type continuation tag))
782   (let ((ctype (continuation-type tag)))
783     (when (csubtypep ctype (specifier-type '(or number character)))
784       (compiler-style-warn "~@<using ~S of type ~S as a catch tag (which ~
785                             tends to be unportable because THROW and CATCH ~
786                             use EQ comparison)~@:>"
787                            (continuation-source tag)
788                            (type-specifier (continuation-type tag))))))