0.8.0.76:
[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                (push (cons quality raw-value)
56                      result)))))
57     ;; Add any nonredundant entries from old POLICY.
58     (dolist (old-entry policy)
59       (unless (assq (car old-entry) result)
60         (push old-entry result)))
61     ;; Voila.
62     result))
63
64 ;;; ANSI defines the declaration (FOO X Y) to be equivalent to
65 ;;; (TYPE FOO X Y) when FOO is a type specifier. This function
66 ;;; implements that by converting (FOO X Y) to (TYPE FOO X Y).
67 (defun canonized-decl-spec (decl-spec)
68   (let ((id (first decl-spec)))
69     (unless (symbolp id)
70       (error "The declaration identifier is not a symbol: ~S" id))
71     (let ((id-is-type (info :type :kind id))
72           (id-is-declared-decl (info :declaration :recognized id)))
73       (cond ((and id-is-type id-is-declared-decl)
74              (compiler-error
75               "ambiguous declaration ~S:~%  ~
76               ~S was declared as a DECLARATION, but is also a type name."
77               decl-spec id))
78             (id-is-type
79              (cons 'type decl-spec))
80             (t
81              decl-spec)))))
82
83 (!begin-collecting-cold-init-forms)
84 (!cold-init-forms (defvar *queued-proclaims* nil))
85 (!defun-from-collected-cold-init-forms !early-proclaim-cold-init)
86
87 (defun sb!xc:proclaim (raw-form)
88   #+sb-xc (/show0 "entering PROCLAIM, RAW-FORM=..")
89   #+sb-xc (/hexstr raw-form)
90   (let* ((form (canonized-decl-spec raw-form))
91          (kind (first form))
92          (args (rest form)))
93     (case kind
94       (special
95        (dolist (name args)
96          (unless (symbolp name)
97            (error "can't declare a non-symbol as SPECIAL: ~S" name))
98          (when (constantp name)
99            (error "can't declare a constant as SPECIAL: ~S" name))
100          (clear-info :variable :constant-value name)
101          (setf (info :variable :kind name) :special)))
102       (type
103        (if *type-system-initialized*
104            (let ((type (specifier-type (first args))))
105              (dolist (name (rest args))
106                (unless (symbolp name)
107                  (error "can't declare TYPE of a non-symbol: ~S" name))
108                (when (eq (info :variable :where-from name) :declared)
109                  (let ((old-type (info :variable :type name)))
110                    (when (type/= type old-type)
111                      (style-warn "The new TYPE proclamation~%  ~S~@
112                                   for ~S does not match the old TYPE~@
113                                   proclamation ~S"
114                                  type name old-type))))
115                (setf (info :variable :type name) type)
116                (setf (info :variable :where-from name) :declared)))
117            (push raw-form *queued-proclaims*)))
118       (ftype
119        (if *type-system-initialized*
120            (let ((ctype (specifier-type (first args))))
121              (unless (csubtypep ctype (specifier-type 'function))
122                (error "not a function type: ~S" (first args)))
123              (dolist (name (rest args))
124                (when (eq (info :function :where-from name) :declared)
125                  (let ((old-type (info :function :type name)))
126                    (when (type/= ctype old-type)
127                      (style-warn
128                       "new FTYPE proclamation~@
129                        ~S~@
130                        for ~S does not match old FTYPE proclamation~@
131                        ~S"
132                       ctype name old-type))))
133
134                ;; Now references to this function shouldn't be warned
135                ;; about as undefined, since even if we haven't seen a
136                ;; definition yet, we know one is planned.
137                ;;
138                ;; Other consequences of we-know-you're-a-function-now
139                ;; are appropriate too, e.g. any MACRO-FUNCTION goes away.
140                (proclaim-as-fun-name name)
141                (note-name-defined name :function)
142
143                ;; the actual type declaration
144                (setf (info :function :type name) ctype
145                      (info :function :where-from name) :declared)))
146            (push raw-form *queued-proclaims*)))
147       (freeze-type
148        (dolist (type args)
149          (let ((class (specifier-type type)))
150            (when (typep class 'classoid)
151              (setf (classoid-state class) :sealed)
152              (let ((subclasses (classoid-subclasses class)))
153                (when subclasses
154                  (dohash (subclass layout subclasses)
155                    (declare (ignore layout))
156                    (setf (classoid-state subclass) :sealed))))))))
157       (optimize
158        (setq *policy* (process-optimize-decl form *policy*)))
159       ((inline notinline maybe-inline)
160        (dolist (name args)
161          (proclaim-as-fun-name name) ; since implicitly it is a function
162          (setf (info :function :inlinep name)
163                (ecase kind
164                  (inline :inline)
165                  (notinline :notinline)
166                  (maybe-inline :maybe-inline)))))
167       (declaration
168        (dolist (decl args)
169          (unless (symbolp decl)
170            (error "In~%  ~S~%the declaration to be recognized is not a ~
171                   symbol:~%  ~S"
172                   form decl))
173          (setf (info :declaration :recognized decl) t)))
174       (t
175        (unless (info :declaration :recognized kind)
176          (compiler-warn "unrecognized declaration ~S" raw-form)))))
177   #+sb-xc (/show0 "returning from PROCLAIM")
178   (values))