Debian Codegraph @

src/cmp/cmptype.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
6 ;;;; Copyright (c) 1990, Giuseppe Attardi.
7 ;;;;
8 ;;;; See file 'LICENSE' for the copyright details.
9
10 ;;;; CMPTYPE Type information.
11
12 (in-package "COMPILER")
13
14 ;;;
15 ;;; and-form-type
16 ;;; updates the FORM type to its intersection with TYPE
17 ;;;
18 (defun and-form-type (type form original-form &optional (mode :safe)
19 (format-string "") &rest format-args)
20 (let* ((type2 (c1form-primary-type form))
21 (type1 (type-and type type2)))
22 ;; We only change the type if it is not NIL. Is this wise?
23 (if type1
24 (setf (c1form-type form) type1)
25 (funcall (if (eq mode :safe) #'cmperr #'cmpwarn)
26 "~?, the type of the form ~s is ~s, not ~s." format-string
27 format-args original-form type2 type))
28 form))
29
30 ;;;
31 ;;; and-call-type
32 ;;; updates the FORM type to its many-values intersection with TYPE
33 ;;;
34 (defun and-call-type (type form)
35 (setf (c1form-type form) (values-type-and type (c1form-type form))))
36
37 (defun default-init (var &optional warn)
38 (declare (ignore warn))
39 (let ((new-value (cdr (assoc (var-type var)
40 `((fixnum . 0)
41 (character . #\space)
42 (long-float . 0.0L1)
43 (double-float . 0.0D1)
44 (single-float . 0.0F1)
45 ,@(when (member :complex-float *features*)
46 '((si:complex-single-float . #c(0.0f0 0.0f0))
47 (si:complex-double-float . #c(0.0d0 0.0d0))
48 (si:complex-long-float . #c(0.0l0 0.0l0)))))
49 :test #'(lambda (t1 t2)
50 (subtypep t1 t2 *cmp-env*))))))
51 (if new-value
52 (c1constant-value new-value)
53 (c1nil))))
54
55 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
56 ;;
57 ;; TYPE CHECKING
58 ;;
59
60 (defun lambda-type-check-init-forms (optionals keywords opt-types key-types)
61 (flet ((maybe-fix-type (var init type type-iterator)
62 (multiple-value-bind (constantp value)
63 (c1form-constant-p init)
64 (when (and constantp (not (typep value type *cmp-env*)))
65 (cmpwarn-style "The init-form of the argument ~A of ~:[an anonymous function~;the function ~:*~A~] is not of the declared type ~A."
66 (var-name var)
67 (fun-name *current-function*)
68 type)
69 ;; As a matter of policy, we allow init-forms whose
70 ;; type does not match the proclaimed type of the
71 ;; corresponding argument. In that case, we extend the
72 ;; type to also allow the initial value to be passed as
73 ;; an argument to the function.
74 (setf (first type-iterator) (type-or type `(eql ,value))
75 (var-type var) (type-or (var-type var) `(eql ,value)))))))
76 (loop for var in optionals by #'cdddr
77 for init in (rest optionals) by #'cdddr
78 for type-iterator on (rest opt-types) by #'cdddr
79 for type = (first type-iterator)
80 do (maybe-fix-type var init type type-iterator))
81 (loop for key-list on keywords by #'cddddr
82 for keyword = (first key-list)
83 for var = (second key-list)
84 for init = (third key-list)
85 for type-iterator = (loop for key-list on (rest key-types) by #'cddr
86 when (eq keyword (first key-list))
87 return (rest key-list)
88 finally (return '(t)))
89 for type = (first type-iterator)
90 do (maybe-fix-type var init type type-iterator))))
91
92 (defun lambda-type-check-associate (fname requireds optionals keywords global-fun-p)
93 (multiple-value-bind (arg-types found)
94 (and global-fun-p (get-arg-types fname *cmp-env* global-fun-p))
95 (if found
96 (multiple-value-bind (req-types opt-types rest-flag key-flag
97 key-types allow-other-keys)
98 (si:process-lambda-list arg-types 'ftype)
99 (declare (ignore rest-flag key-flag allow-other-keys))
100 (lambda-type-check-init-forms optionals keywords opt-types key-types)
101 (list
102 (loop for var in requireds
103 for type in (rest req-types)
104 collect (cons var type))
105 (loop for optional in optionals by #'cdddr
106 for type in (rest opt-types) by #'cdddr
107 collect (cons optional type))
108 (loop for key-list on keywords by #'cddddr
109 for keyword = (first key-list)
110 for key-var = (second key-list)
111 for type = (loop for key-list on (rest key-types) by #'cddr
112 when (eq keyword (first key-list))
113 return (second key-list)
114 finally (return t))
115 collect (cons key-var type))))
116 (list
117 (loop for var in requireds
118 collect (cons var t))
119 (loop for optional in optionals by #'cdddr
120 collect (cons optional t))
121 (loop for key-list on keywords by #'cddddr
122 for key-var = (second key-list)
123 collect (cons key-var t))))))
124
125 (defun lambda-type-check-precise (assoc-list ts)
126 (loop for record in assoc-list
127 for var = (car record)
128 for type = (assoc (var-name var) ts)
129 when type
130 do
131 ;; Instead of trusting the global proclamation, we set a check based
132 ;; on the local declaration, without type merging.
133 (rplacd record (cdr type))
134 #+(or)
135 (rplacd record (type-and (cdr record) (cdr type))))
136 assoc-list)
137
138 (defun lambda-type-check-expand (type-checks policy-check-type other-decls)
139 (loop with checks = '()
140 with new-auxs = '()
141 for (var . type) in type-checks
142 for name = (var-name var)
143 do (if (eq type t) ;; Non trivial types are the only ones we care about
144 (setf checks (list* nil checks))
145 (if (and policy-check-type
146 (loop for decl in other-decls
147 never (and (consp decl)
148 (eq (first decl)
149 'si::no-check-type)
150 (member name (rest decl)))))
151 ;; We remove assumption about types, which will be checked
152 ;; later due to this assertion...
153 (setf (var-type var) t
154 checks (list* `(type-assertion ,name ,type) checks)
155 new-auxs (list* `(ext:truly-the ,type ,name) name new-auxs))
156 ;; Or simply enforce the variable's type.
157 (setf (var-type var) (type-and (var-type var) type))))
158 finally
159 (return (values checks new-auxs))))
160
161 (defun extract-lambda-type-checks (fname requireds optionals keywords ts other-decls)
162 ;; We generate automatic type checks for function arguments that
163 ;; are declared. These checks can be deactivated by appropriate
164 ;; safety settings which are checked by ASSERT-TYPE. Note
165 ;; that not all type declarations can be checked (take for instance
166 ;; (type (function (t t) t) foo)) We let the macro do the job.
167 ;; Returns 4 values: type assertions for required, optional, keyword
168 ;; arguments and &aux type declarations. Type assertions may be nil,
169 ;; if no type check is necessary.
170 (let* ((global-fun-p (member '(si::c-global) other-decls :test #'equal))
171 (policy-check-type (policy-check-arguments-type))
172 (all-type-checks (mapcar #'(lambda (checks)
173 (lambda-type-check-precise checks ts))
174 (lambda-type-check-associate fname requireds optionals keywords global-fun-p)))
175 (checks '())
176 (new-auxs '()))
177 (setq checks
178 (loop for i in all-type-checks
179 collect (multiple-value-bind (check new-aux)
180 (lambda-type-check-expand i policy-check-type
181 other-decls)
182 (setq new-auxs (nconc new-aux new-auxs))
183 check)))
184 (when new-auxs
185 (cmpnote "In ~:[an anonymous function~;function ~:*~A~], checking types of argument~@[s~]~{ ~A~}."
186 (fun-name *current-function*)
187 (nreverse
188 (loop for i in (cdr new-auxs) by #'cddr collect i))))
189 (values (nreverse (car checks)) (nreverse (cadr checks))
190 (nreverse (caddr checks)) (nreverse new-auxs))))
191
192 (defmacro assert-type-if-known (value type &environment env)
193 "Generates a type check on an expression, ensuring that it is satisfied."
194 (multiple-value-bind (trivial valid)
195 (subtypep 't type *cmp-env*)
196 (cond ((and trivial valid)
197 value)
198 ((multiple-value-setq (valid value) (constant-value-p value env))
199 (si:maybe-quote value))
200 (t
201 (ext:with-clean-symbols (%value)
202 `(let* ((%value ,value))
203 ,(simple-type-assertion '%value (si::flatten-function-types type env))
204 (ext:truly-the ,type %value)))))))
205
206 (defmacro optional-type-check (value type)
207 (if (policy-assume-right-type)
208 value
209 `(assert-type-if-known ,value ,type)))
210
211 (defmacro with-let*-type-check (triplets &body body)
212 `(let* ,(loop for (var value type) in triplets
213 collect `(,var (assert-type-if-known ,value ,type)))
214 (declare (:read-only ,@(mapcar #'car triplets)))
215 ,@body))
216