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