0.8.12.7: Merge package locks, AKA "what can go wrong with a 3783 line patch?"
[sbcl.git] / src / compiler / proclaim.lisp
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.
5
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.
14
15 (in-package "SB!C")
16
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*))
21
22 ;;; Look up some symbols in *FREE-VARS*, returning the var
23 ;;; structures for any which exist. If any of the names aren't
24 ;;; symbols, we complain.
25 (declaim (ftype (function (list) list) get-old-vars))
26 (defun get-old-vars (names)
27   (collect ((vars))
28     (dolist (name names (vars))
29       (unless (symbolp name)
30         (compiler-error "The name ~S is not a symbol." name))
31       (let ((old (gethash name *free-vars*)))
32         (when old (vars old))))))
33
34 ;;; Return a new POLICY containing the policy information represented
35 ;;; by the optimize declaration SPEC. Any parameters not specified are
36 ;;; defaulted from the POLICY argument.
37 (declaim (ftype (function (list policy) policy) process-optimize-decl))
38 (defun process-optimize-decl (spec policy)
39   (let ((result nil))
40     ;; Add new entries from SPEC.
41     (dolist (q-and-v-or-just-q (cdr spec))
42       (multiple-value-bind (quality raw-value)
43           (if (atom q-and-v-or-just-q)
44               (values q-and-v-or-just-q 3)
45               (destructuring-bind (quality raw-value) q-and-v-or-just-q
46                 (values quality raw-value)))
47         (cond ((not (policy-quality-name-p quality))
48                (compiler-warn "ignoring unknown optimization quality ~
49                                ~S in ~S"
50                                quality spec))
51               ((not (typep raw-value 'policy-quality))
52                (compiler-warn "ignoring bad optimization value ~S in ~S"
53                               raw-value spec))
54               (t
55                ;; we can't do this yet, because CLOS macros expand
56                ;; into code containing INHIBIT-WARNINGS.
57                #+nil
58                (when (eql quality 'sb!ext:inhibit-warnings)
59                  (compiler-style-warn "~S is deprecated: use ~S instead"
60                                       quality 'sb!ext:muffle-conditions))
61                (push (cons quality raw-value)
62                      result)))))
63     ;; Add any nonredundant entries from old POLICY.
64     (dolist (old-entry policy)
65       (unless (assq (car old-entry) result)
66         (push old-entry result)))
67     ;; Voila.
68     result))
69
70 (declaim (ftype (function (list list) list)
71                 process-handle-conditions-decl))
72 (defun process-handle-conditions-decl (spec list)
73   (let ((new (copy-alist list)))
74     (dolist (clause (cdr spec))
75       (destructuring-bind (typespec restart-name) clause
76         (let ((ospec (rassoc restart-name new :test #'eq)))
77           (if ospec
78               (setf (car ospec)
79                     (type-specifier
80                      (type-union (specifier-type (car ospec))
81                                  (specifier-type typespec))))
82               (push (cons (type-specifier (specifier-type typespec))
83                           restart-name)
84                     new)))))
85     new))
86 (declaim (ftype (function (list list) list)
87                 process-muffle-conditions-decl))
88 (defun process-muffle-conditions-decl (spec list)
89   (process-handle-conditions-decl
90    (cons 'handle-conditions
91          (mapcar (lambda (x) (list x 'muffle-warning)) (cdr spec)))
92    list))
93
94 (declaim (ftype (function (list list) list)
95                 process-unhandle-conditions-decl))
96 (defun process-unhandle-conditions-decl (spec list)
97   (let ((new (copy-alist list)))
98     (dolist (clause (cdr spec))
99       (destructuring-bind (typespec restart-name) clause
100         (let ((ospec (rassoc restart-name new :test #'eq)))
101           (if ospec
102               (let ((type-specifier
103                      (type-specifier
104                       (type-intersection
105                        (specifier-type (car ospec))
106                        (specifier-type `(not ,typespec))))))
107                 (if type-specifier
108                     (setf (car ospec) type-specifier)
109                     (setq new
110                           (delete restart-name new :test #'eq :key #'cdr))))
111               ;; do nothing?
112               nil))))
113     new))
114 (declaim (ftype (function (list list) list)
115                 process-unmuffle-conditions-decl))
116 (defun process-unmuffle-conditions-decl (spec list)
117   (process-unhandle-conditions-decl
118    (cons 'unhandle-conditions
119          (mapcar (lambda (x) (list x 'muffle-warning)) (cdr spec)))
120    list))
121
122 (declaim (ftype (function (list list) list)
123                 process-package-lock-decl))
124 (defun process-package-lock-decl (spec old)
125   (let ((decl (car spec))
126         (list (cdr spec)))
127     (ecase decl
128       (disable-package-locks
129        (union old list :test #'equal))
130       (enable-package-locks
131        (set-difference old list :test #'equal)))))
132
133 ;;; ANSI defines the declaration (FOO X Y) to be equivalent to
134 ;;; (TYPE FOO X Y) when FOO is a type specifier. This function
135 ;;; implements that by converting (FOO X Y) to (TYPE FOO X Y).
136 (defun canonized-decl-spec (decl-spec)
137   (let ((id (first decl-spec)))
138     (unless (symbolp id)
139       (error "The declaration identifier is not a symbol: ~S" id))
140     (let ((id-is-type (info :type :kind id))
141           (id-is-declared-decl (info :declaration :recognized id)))
142       (cond ((and id-is-type id-is-declared-decl)
143              (compiler-error
144               "ambiguous declaration ~S:~%  ~
145               ~S was declared as a DECLARATION, but is also a type name."
146               decl-spec id))
147             (id-is-type
148              (cons 'type decl-spec))
149             (t
150              decl-spec)))))
151
152 (defvar *queued-proclaims*) ; initialized in !COLD-INIT-FORMS
153
154 (!begin-collecting-cold-init-forms)
155 (!cold-init-forms (setf *queued-proclaims* nil))
156 (!defun-from-collected-cold-init-forms !early-proclaim-cold-init)
157
158 (defun sb!xc:proclaim (raw-form)
159   #+sb-xc (/show0 "entering PROCLAIM, RAW-FORM=..")
160   #+sb-xc (/hexstr raw-form)
161   (let* ((form (canonized-decl-spec raw-form))
162          (kind (first form))
163          (args (rest form)))
164     (case kind
165       (special
166        (dolist (name args)
167          (unless (symbolp name)
168            (error "can't declare a non-symbol as SPECIAL: ~S" name))
169          (when (constantp name)
170            (error "can't declare a constant as SPECIAL: ~S" name))
171          (with-single-package-locked-error
172              (:symbol name "globally declaraing ~A special"))
173          (clear-info :variable :constant-value name)
174          (setf (info :variable :kind name) :special)))
175       (type
176        (if *type-system-initialized*
177            (let ((type (specifier-type (first args))))
178              (dolist (name (rest args))
179                (unless (symbolp name)
180                  (error "can't declare TYPE of a non-symbol: ~S" name))
181                (with-single-package-locked-error
182                    (:symbol name "globally declaring the type of ~A"))
183                (when (eq (info :variable :where-from name) :declared)
184                  (let ((old-type (info :variable :type name)))
185                    (when (type/= type old-type)
186                      (style-warn "The new TYPE proclamation~%  ~S~@
187                                   for ~S does not match the old TYPE~@
188                                   proclamation ~S"
189                                  type name old-type))))
190                (setf (info :variable :type name) type)
191                (setf (info :variable :where-from name) :declared)))
192            (push raw-form *queued-proclaims*)))
193       (ftype
194        (if *type-system-initialized*
195            (let ((ctype (specifier-type (first args))))
196              (unless (csubtypep ctype (specifier-type 'function))
197                (error "not a function type: ~S" (first args)))
198              (dolist (name (rest args))
199                (with-single-package-locked-error
200                    (:symbol name "globally declaring the ftype of ~A"))
201                (when (eq (info :function :where-from name) :declared)
202                  (let ((old-type (info :function :type name)))
203                    (when (type/= ctype old-type)
204                      (style-warn
205                       "new FTYPE proclamation~@
206                        ~S~@
207                        for ~S does not match old FTYPE proclamation~@
208                        ~S"
209                       ctype name old-type))))
210
211                ;; Now references to this function shouldn't be warned
212                ;; about as undefined, since even if we haven't seen a
213                ;; definition yet, we know one is planned.
214                ;;
215                ;; Other consequences of we-know-you're-a-function-now
216                ;; are appropriate too, e.g. any MACRO-FUNCTION goes away.
217                (proclaim-as-fun-name name)
218                (note-name-defined name :function)
219
220                ;; the actual type declaration
221                (setf (info :function :type name) ctype
222                      (info :function :where-from name) :declared)))
223            (push raw-form *queued-proclaims*)))
224       (freeze-type
225        (dolist (type args)
226          (let ((class (specifier-type type)))
227            (when (typep class 'classoid)
228              (setf (classoid-state class) :sealed)
229              (let ((subclasses (classoid-subclasses class)))
230                (when subclasses
231                  (dohash (subclass layout subclasses)
232                    (declare (ignore layout))
233                    (setf (classoid-state subclass) :sealed))))))))
234       (optimize
235        (setq *policy* (process-optimize-decl form *policy*)))
236       (muffle-conditions
237        (setq *handled-conditions*
238              (process-muffle-conditions-decl form *handled-conditions*)))
239       (unmuffle-conditions
240        (setq *handled-conditions*
241              (process-unmuffle-conditions-decl form *handled-conditions*)))
242       ((disable-package-locks enable-package-locks)
243          (setq *disabled-package-locks*
244                (process-package-lock-decl form *disabled-package-locks*)))
245       ((inline notinline maybe-inline)
246        (dolist (name args)
247          (proclaim-as-fun-name name) ; since implicitly it is a function
248          (setf (info :function :inlinep name)
249                (ecase kind
250                  (inline :inline)
251                  (notinline :notinline)
252                  (maybe-inline :maybe-inline)))))
253       (declaration
254        (dolist (decl args)
255          (unless (symbolp decl)
256            (error "In~%  ~S~%the declaration to be recognized is not a ~
257                   symbol:~%  ~S"
258                   form decl))
259          (with-single-package-locked-error
260              (:symbol decl "globally declaring ~A as a declaration proclamation"))
261          (setf (info :declaration :recognized decl) t)))
262       (t
263        (unless (info :declaration :recognized kind)
264          (compiler-warn "unrecognized declaration ~S" raw-form)))))
265   #+sb-xc (/show0 "returning from PROCLAIM")
266   (values))