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