Don't emit "sub rsp, 0"
[sbcl.git] / src / compiler / proclaim.lisp
blobb1f32d677b94057685cbd06fa053f6d5649d0802
1 ;;;; This file contains load-time support for declaration processing.
2 ;;;; In CMU CL it was split off from the compiler so that the compiler
3 ;;;; doesn't have to be in the cold load, but in SBCL the compiler is
4 ;;;; in the cold load again, so this might not be valuable.
6 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; more information.
8 ;;;;
9 ;;;; This software is derived from the CMU CL system, which was
10 ;;;; written at Carnegie Mellon University and released into the
11 ;;;; public domain. The software is in the public domain and is
12 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
13 ;;;; files for more information.
15 (in-package "SB!C")
17 ;;; A list of UNDEFINED-WARNING structures representing references to unknown
18 ;;; stuff which came up in a compilation unit.
19 (defvar *undefined-warnings*)
20 (declaim (list *undefined-warnings*))
22 (declaim (ftype (function (list list) list)
23 process-handle-conditions-decl))
24 (defun process-handle-conditions-decl (spec list)
25 (let ((new (copy-alist list)))
26 (dolist (clause (cdr spec) new)
27 (destructuring-bind (typespec restart-name) clause
28 (let ((type (compiler-specifier-type typespec))
29 (ospec (rassoc restart-name new :test #'eq)))
30 (if ospec
31 (setf (car ospec) (type-union (car ospec) type))
32 (push (cons type restart-name) new)))))))
34 (declaim (ftype (function (list list) list)
35 process-muffle-conditions-decl))
36 (defun process-muffle-conditions-decl (spec list)
37 (process-handle-conditions-decl
38 `(handle-conditions ((or ,@(cdr spec)) muffle-warning))
39 list))
41 (declaim (ftype (function (list list) list)
42 process-unhandle-conditions-decl))
43 (defun process-unhandle-conditions-decl (spec list)
44 (let ((new (copy-alist list)))
45 (dolist (clause (cdr spec) new)
46 (destructuring-bind (typespec restart-name) clause
47 (let ((ospec (rassoc restart-name new :test #'eq)))
48 (if ospec
49 (let ((type (type-intersection
50 (car ospec)
51 (compiler-specifier-type `(not ,typespec)))))
52 (if (type= type *empty-type*)
53 (setq new (delete restart-name new :test #'eq :key #'cdr))
54 (setf (car ospec) type)))
55 ;; do nothing?
56 nil))))))
58 (declaim (ftype (function (list list) list)
59 process-unmuffle-conditions-decl))
60 (defun process-unmuffle-conditions-decl (spec list)
61 (process-unhandle-conditions-decl
62 `(unhandle-conditions ((or ,@(cdr spec)) muffle-warning))
63 list))
65 (declaim (ftype (function (list list) list)
66 process-package-lock-decl))
67 (defun process-package-lock-decl (spec old)
68 (destructuring-bind (decl &rest names) spec
69 (ecase decl
70 (disable-package-locks
71 ;; Why are we using EQUAL here if the only way to disable the
72 ;; lock on (SETF CAR) is to list the name CAR and not (SETF CAR)?
73 (union old names :test #'equal))
74 (enable-package-locks
75 (set-difference old names :test #'equal)))))
77 (!defvar *queued-proclaims* nil) ; should this be !*QUEUED-PROCLAIMS* ?
79 (defun process-variable-declaration (name kind info-value)
80 (unless (symbolp name)
81 (error "Cannot proclaim a non-symbol as ~A: ~S" kind name))
83 (when (and (eq kind 'always-bound) (eq info-value :always-bound)
84 (not (boundp name)))
85 (error "Cannot proclaim an unbound symbol as ~A: ~S" kind name))
87 (multiple-value-bind (allowed test)
88 (ecase kind
89 (special (values '(:special :unknown) #'eq))
90 (global (values '(:global :unknown) #'eq))
91 (always-bound (values '(:constant) #'neq)))
92 (let ((old (info :variable :kind name)))
93 (unless (member old allowed :test test)
94 (error "Cannot proclaim a ~A variable ~A: ~S" old kind name))))
96 (with-single-package-locked-error
97 (:symbol name "globally declaring ~A ~A" kind)
98 (if (eq kind 'always-bound)
99 (setf (info :variable :always-bound name) info-value)
100 (setf (info :variable :kind name) info-value))))
102 (defun type-proclamation-mismatch-warn (name old new &optional description)
103 (warn 'type-proclamation-mismatch-warning
104 :name name :old old :new new :description description))
106 (defun proclaim-type (name type type-specifier where-from)
107 (unless (symbolp name)
108 (error "Cannot proclaim TYPE of a non-symbol: ~S" name))
110 (with-single-package-locked-error
111 (:symbol name "globally declaring the TYPE of ~A")
112 (when (eq (info :variable :where-from name) :declared)
113 (let ((old-type (info :variable :type name)))
114 (when (type/= type old-type)
115 (type-proclamation-mismatch-warn
116 name (type-specifier old-type) type-specifier))))
117 (setf (info :variable :type name) type
118 (info :variable :where-from name) where-from)))
120 (defun ftype-proclamation-mismatch-warn (name old new &optional description)
121 (warn 'ftype-proclamation-mismatch-warning
122 :name name :old old :new new :description description))
124 (defun proclaim-ftype (name type-oid type-specifier where-from)
125 (declare (type (or ctype defstruct-description) type-oid))
126 (unless (legal-fun-name-p name)
127 (error "Cannot declare FTYPE of illegal function name ~S" name))
128 (when (and (ctype-p type-oid)
129 (not (csubtypep type-oid (specifier-type 'function))))
130 (error "Not a function type: ~/sb!impl:print-type/" type-oid))
131 (with-single-package-locked-error
132 (:symbol name "globally declaring the FTYPE of ~A")
133 (when (eq (info :function :where-from name) :declared)
134 (let ((old-type (proclaimed-ftype name))
135 (type (if (ctype-p type-oid)
136 type-oid
137 (specifier-type type-specifier))))
138 (cond
139 ((not (type/= type old-type))) ; not changed
140 ((not (info :function :info name)) ; not a known function
141 (ftype-proclamation-mismatch-warn
142 name (type-specifier old-type) type-specifier))
143 ((csubtypep type old-type)) ; tighten known function type
145 (cerror "Continue"
146 'ftype-proclamation-mismatch-error
147 :name name
148 :old (type-specifier old-type)
149 :new type-specifier)))))
150 ;; Now references to this function shouldn't be warned about as
151 ;; undefined, since even if we haven't seen a definition yet, we
152 ;; know one is planned.
154 ;; Other consequences of we-know-you're-a-function-now are
155 ;; appropriate too, e.g. any MACRO-FUNCTION goes away.
156 (proclaim-as-fun-name name)
157 (note-name-defined name :function)
159 ;; The actual type declaration.
160 (setf (info :function :type name) type-oid
161 (info :function :where-from name) where-from)))
163 (defun seal-class (class)
164 (declare (type classoid class))
165 (setf (classoid-state class) :sealed)
166 (let ((subclasses (classoid-subclasses class)))
167 (when subclasses
168 (dohash ((subclass layout) subclasses :locked t)
169 (declare (ignore layout))
170 (setf (classoid-state subclass) :sealed)))))
172 (defun process-freeze-type-declaration (type-specifier)
173 (let ((class (specifier-type type-specifier)))
174 (when (typep class 'classoid)
175 (seal-class class))))
177 (defun process-inline-declaration (name kind)
178 ;; since implicitly it is a function, also scrubs *FREE-FUNS*
179 (proclaim-as-fun-name name)
180 ;; Check for problems before touching globaldb,
181 ;; so that the report function can see the old value.
182 (let ((newval
183 (ecase kind
184 (inline :inline)
185 (notinline :notinline)
186 (maybe-inline :maybe-inline))))
187 (warn-if-inline-failed/proclaim name newval)
188 (setf (info :function :inlinep name) newval)))
190 (defun check-deprecation-declaration (state since form)
191 (unless (typep state 'deprecation-state)
192 (error 'simple-type-error
193 :datum state
194 :expected-type 'deprecation-state
195 :format-control "~@<In declaration ~S, ~S state is not a ~
196 valid deprecation state. Expected one ~
197 of ~{~S~^, ~}.~@:>"
198 :format-arguments (list form state
199 (rest (typexpand 'deprecation-state)))))
200 (multiple-value-call #'values
201 state (sb!impl::normalize-deprecation-since since)))
203 (defun process-deprecation-declaration (thing state software version)
204 (destructuring-bind (namespace name &key replacement) thing
205 (let ((info (make-deprecation-info state software version replacement)))
206 (ecase namespace
207 (function
208 (when (eq state :final)
209 (sb!impl::setup-function-in-final-deprecation
210 software version name replacement))
211 (setf (info :function :deprecated name) info))
212 (variable
213 ;; TODO (check-variable-name name "deprecated variable declaration")
214 (when (eq state :final)
215 (sb!impl::setup-variable-in-final-deprecation
216 software version name replacement))
217 (setf (info :variable :deprecated name) info))
218 (type
219 (when (eq state :final)
220 (sb!impl::setup-type-in-final-deprecation
221 software version name replacement))
222 (setf (info :type :deprecated name) info))))))
224 (defun process-declaration-declaration (name form)
225 (unless (symbolp name)
226 (error "In~% ~S~%the declaration to be recognized is not a ~
227 symbol:~% ~S"
228 form name))
229 (with-single-package-locked-error
230 (:symbol name "globally declaring ~A as a declaration proclamation"))
231 (setf (info :declaration :recognized name) t))
233 ;;; ANSI defines the declaration (FOO X Y) to be equivalent to
234 ;;; (TYPE FOO X Y) when FOO is a type specifier. This function
235 ;;; implements that by converting (FOO X Y) to (TYPE FOO X Y).
236 (defun canonized-decl-spec (decl-spec)
237 (let ((id (first decl-spec)))
238 (if (cond ((symbolp id) (info :type :kind id))
239 ((listp id)
240 (let ((id (car id)))
241 (and (symbolp id)
242 (or (info :type :expander id)
243 (info :type :kind id)))))
245 ;; FIXME: should be (TYPEP id '(OR CLASS CLASSOID))
246 ;; but that references CLASS too soon.
247 ;; See related hack in DEF!TYPE TYPE-SPECIFIER.
248 (typep id 'instance)))
249 (cons 'type decl-spec)
250 decl-spec)))
252 ;; These return values are intended for EQ-comparison in
253 ;; STORE-LOCATION in %PROCLAIM.
254 (defun deprecation-location-key (namespace)
255 (case namespace
256 (function '(deprecated function))
257 (variable '(deprecated variable))
258 (type '(deprecated type))))
260 (defun %proclaim (raw-form location)
261 (destructuring-bind (&whole form &optional kind &rest args)
262 (canonized-decl-spec raw-form)
263 (labels ((store-location (name &key (key kind))
264 (if location
265 (setf (getf (info :source-location :declaration name) key)
266 location)
267 ;; Without this WHEN, globaldb would accumulate
268 ;; a bunch of explicitly stored empty lists because
269 ;; it does not know that there's no need to store NIL.
270 (when (info :source-location :declaration name)
271 (remf (info :source-location :declaration name) key))))
272 (map-names (names function &rest extra-args)
273 (mapc (lambda (name)
274 (store-location name)
275 (apply function name extra-args))
276 names))
277 (map-args (function &rest extra-args)
278 (apply #'map-names args function extra-args)))
279 (case kind
280 ((special global always-bound)
281 (map-args #'process-variable-declaration kind
282 (case kind
283 (special :special)
284 (global :global)
285 (always-bound :always-bound))))
286 ((type ftype)
287 (if *type-system-initialized*
288 (destructuring-bind (type &rest names) args
289 (check-deprecated-type type)
290 (let ((ctype (specifier-type type)))
291 (map-names names (ecase kind
292 (type #'proclaim-type)
293 (ftype #'proclaim-ftype))
294 ctype type :declared)))
295 (push raw-form *queued-proclaims*)))
296 (freeze-type
297 (map-args #'process-freeze-type-declaration))
298 (optimize
299 (multiple-value-bind (new-policy specified-qualities)
300 (process-optimize-decl form *policy*)
301 (setq *policy* new-policy)
302 (warn-repeated-optimize-qualities new-policy specified-qualities)))
303 (muffle-conditions
304 (setq *handled-conditions*
305 (process-muffle-conditions-decl form *handled-conditions*)))
306 (unmuffle-conditions
307 (setq *handled-conditions*
308 (process-unmuffle-conditions-decl form *handled-conditions*)))
309 ((disable-package-locks enable-package-locks)
310 (setq *disabled-package-locks*
311 (process-package-lock-decl form *disabled-package-locks*)))
312 ((inline notinline maybe-inline)
313 (map-args #'process-inline-declaration kind))
314 (deprecated
315 (destructuring-bind (state since &rest things) args
316 (multiple-value-bind (state software version)
317 (check-deprecation-declaration state since form)
318 (mapc (lambda (thing)
319 (destructuring-bind (namespace name &rest rest) thing
320 (declare (ignore rest))
321 (store-location
322 name :key (deprecation-location-key namespace)))
323 (process-deprecation-declaration thing state software version))
324 things))))
325 (declaration
326 (map-args #'process-declaration-declaration form))
328 (unless (info :declaration :recognized kind)
329 (compiler-warn "unrecognized declaration ~S" raw-form)))))))
331 (defun sb!xc:proclaim (raw-form)
332 #!+(and sb-show (host-feature sb-xc))
333 (progn (write-string "* ") (write `(declaim ,raw-form) :level nil) (terpri))
334 (%proclaim raw-form nil)
335 (values))
337 ;; Issue a style warning if there are any repeated OPTIMIZE declarations
338 ;; given the SPECIFIED-QUALITIES, unless there is no ambiguity.
339 (defun warn-repeated-optimize-qualities (new-policy specified-qualities)
340 (let (dups)
341 (dolist (quality-and-value specified-qualities)
342 (let* ((quality (car quality-and-value))
343 (current ; Read the raw quality value, not the adjusted value.
344 (%%policy-quality new-policy (policy-quality-name-p quality))))
345 (when (and (not (eql (cdr quality-and-value) current))
346 (not (assq quality dups)))
347 (push `(,quality ,current) dups))))
348 (when dups
349 ;; If a restriction is in force, this message can be misleading,
350 ;; as the "effective" value isn't always what the message claims.
351 (compiler-style-warn "Repeated OPTIMIZE qualit~@P. Using ~{~S~^ and ~}"
352 (length dups) dups))))