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