8a616d003d1d2c509c9277d3c76390f2edc526b4
[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-VARIABLES*, 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-variables*)))
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-warning "ignoring unknown optimization quality ~
49                                  ~S in ~S"
50                                  quality spec))
51               ((not (and (typep raw-value 'real) (<= 0 raw-value 3)))
52                (compiler-warning "ignoring bad optimization value ~S in ~S"
53                                  raw-value spec))
54               (t
55                (push (cons quality (rational 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 (defun sb!xc:proclaim (raw-form)
84   #+sb-xc (/show0 "entering PROCLAIM, RAW-FORM=..")
85   #+sb-xc (/hexstr raw-form)
86   (let* ((form (canonized-decl-spec raw-form))
87          (kind (first form))
88          (args (rest form)))
89     (case kind
90       (special
91        (dolist (name args)
92          (unless (symbolp name)
93            (error "can't declare a non-symbol as SPECIAL: ~S" name))
94          (when (constantp name)
95            (error "can't declare a constant as SPECIAL: ~S" name))
96          (clear-info :variable :constant-value name)
97          (setf (info :variable :kind name) :special)))
98       (type
99        (when *type-system-initialized*
100          (let ((type (specifier-type (first args))))
101            (dolist (name (rest args))
102              (unless (symbolp name)
103                (error "can't declare TYPE of a non-symbol: ~S" name))
104              (when (eq (info :variable :where-from name) :declared)
105                (let ((old-type (info :variable :type name)))
106                  (when (type/= type old-type)
107                    (style-warn "The new TYPE proclamation~%  ~S~@
108                                for ~S does not match the old TYPE~@
109                                proclamation ~S"
110                                type name old-type))))
111              (setf (info :variable :type name) type)
112              (setf (info :variable :where-from name) :declared)))))
113       (ftype
114        ;; FIXME: Since currently *TYPE-SYSTEM-INITIALIZED* is not set
115        ;; until many toplevel forms have run, this condition on
116        ;; PROCLAIM (FTYPE ..) (and on PROCLAIM (TYPE ..), above) means
117        ;; that valid PROCLAIMs in cold code could get lost. Probably
118        ;; the cleanest way to deal with this would be to initialize
119        ;; the type system completely in special cold init forms,
120        ;; before any ordinary toplevel forms run. Failing that, we
121        ;; could queue up PROCLAIMs to be done after the type system is
122        ;; initialized. Failing that, we could at least issue a warning
123        ;; when we have to ignore a PROCLAIM because the type system is
124        ;; uninitialized.
125        (when *type-system-initialized*
126          (let ((type (specifier-type (first args))))
127            (unless (csubtypep type (specifier-type 'function))
128              (error "not a function type: ~S" (first args)))
129            (dolist (name (rest args))
130
131              ;; KLUDGE: Something like the commented-out TYPE/=
132              ;; check here would be nice, but it has been
133              ;; commented out because TYPE/= doesn't support
134              ;; function types. It could probably be made to do
135              ;; so, but it might take some time, since function
136              ;; types involve values types, which aren't
137              ;; supported, and since the SUBTYPEP operator for
138              ;; FUNCTION types is rather broken, e.g.
139              ;;   (SUBTYPEP '(FUNCTION (T BOOLEAN) NIL)
140              ;;             '(FUNCTION (FIXNUM FIXNUM) NIL)) => T, T
141              ;; -- WHN 20000229
142              #|
143              (when (eq (info :function :where-from name) :declared)
144                (let ((old-type (info :function :type name)))
145                  (when (type/= type old-type)
146                    (style-warn
147                     "new FTYPE proclamation~@
148                      ~S~@
149                      for ~S does not match old FTYPE proclamation~@
150                      ~S"
151                     (list type name old-type)))))
152              |#
153
154              ;; Now references to this function shouldn't be warned
155              ;; about as undefined, since even if we haven't seen a
156              ;; definition yet, we know one is planned. 
157              ;;
158              ;; Other consequences of we-know-you're-a-function-now
159              ;; are appropriate too, e.g. any MACRO-FUNCTION goes away.
160              (proclaim-as-fun-name name)
161              (note-name-defined name :function)
162
163              ;; the actual type declaration
164              (setf (info :function :type name) type
165                    (info :function :where-from name) :declared)))))
166       (freeze-type
167        (dolist (type args)
168          (let ((class (specifier-type type)))
169            (when (typep class 'sb!xc:class)
170              (setf (class-state class) :sealed)
171              (let ((subclasses (class-subclasses class)))
172                (when subclasses
173                  (dohash (subclass layout subclasses)
174                    (declare (ignore layout))
175                    (setf (class-state subclass) :sealed))))))))
176       (optimize
177        (setq *policy* (process-optimize-decl form *policy*)))
178       ((inline notinline maybe-inline)
179        (dolist (name args)
180          (proclaim-as-fun-name name) ; since implicitly it is a function
181          (setf (info :function :inlinep name)
182                (ecase kind
183                  (inline :inline)
184                  (notinline :notinline)
185                  (maybe-inline :maybe-inline)))))
186       (declaration
187        (dolist (decl args)
188          (unless (symbolp decl)
189            (error "In~%  ~S~%the declaration to be recognized is not a ~
190                   symbol:~%  ~S"
191                   form decl))
192          (setf (info :declaration :recognized decl) t)))
193       (t
194        (unless (info :declaration :recognized kind)
195          (compiler-warning "unrecognized declaration ~S" raw-form)))))
196   #+sb-xc (/show0 "returning from PROCLAIM")
197   (values))