ir1-convert-combination: call reference-leaf directly.
[sbcl.git] / src / compiler / ir1tran.lisp
blob1b3fe820aab01c3933248790927f95c399a60415
1 ;;;; This file contains code which does the translation from Lisp code
2 ;;;; to the first intermediate representation (IR1).
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB-C")
15 (declaim (special *compiler-error-bailout*))
17 ;;; the lexical environment we are currently converting in
18 (defvar *lexenv*)
19 (declaim (type lexenv *lexenv*))
21 ;;; *CURRENT-FORM-NUMBER* is used in FIND-SOURCE-PATHS to compute the
22 ;;; form number to associate with a source path. This should be bound
23 ;;; to an initial value of 0 before the processing of each truly
24 ;;; top level form.
25 (declaim (type index *current-form-number*))
26 (defvar *current-form-number*)
28 ;;; Report as we try each transform? (either source or IR)
29 ;;; Bind to T to see the ones that didn't abort,
30 ;;; :ALL if you want to see each attempted transform.
31 ;;; :DERIVE-TYPE for newly derived types of combinations.
32 (defvar *show-transforms-p* nil)
34 (declaim (inline source-form-has-path-p))
35 (defun source-form-has-path-p (form)
36 (not (typep form '(or symbol fixnum character))))
38 (defun get-source-path (form)
39 (when (source-form-has-path-p form)
40 (gethash form *source-paths*)))
42 (defvar *transforming* 0)
43 (defvar *inlining* 0)
44 (declaim (type fixnum *transforming* *inlining*)
45 #-sb-xc-host (always-bound *transforming* *inlining*))
47 (defun ensure-source-path (form)
48 (flet ((level> (value kind)
49 (let ((x (memq kind *current-path*)))
50 (> value
51 (if x
52 (cadr x)
53 0)))))
54 (or (get-source-path form)
55 (cond ((level> *transforming* 'transformed)
56 ;; Don't hide all the transformed paths, since we might want
57 ;; to look at the final form for error reporting.
58 (list* form 'transformed *transforming* *current-path*))
59 ;; Avoids notes about inlined code leaking out
60 ((level> *inlining* 'inlined)
61 (list* form 'inlined *inlining* *current-path*))
63 (cons form *current-path*))))))
65 (defun note-source-path (form &rest arguments)
66 (when (source-form-has-path-p form)
67 (setf (gethash form *source-paths*)
68 (apply #'list* 'original-source-start *current-form-number* arguments))))
70 (defun proper-list (form)
71 (if (proper-list-p form)
72 form
73 (compiler-error "~@<~S is not a proper list.~@:>" form)))
75 ;;; *CURRENT-COMPONENT* is the COMPONENT structure which we link
76 ;;; blocks into as we generate them. This just serves to glue the
77 ;;; emitted blocks together until local call analysis and flow graph
78 ;;; canonicalization figure out what is really going on. We need to
79 ;;; keep track of all the blocks generated so that we can delete them
80 ;;; if they turn out to be unreachable.
81 ;;;
82 ;;; FIXME: It's confusing having one variable named *CURRENT-COMPONENT*
83 ;;; and another named *COMPONENT-BEING-COMPILED*. (In CMU CL they
84 ;;; were called *CURRENT-COMPONENT* and *COMPILE-COMPONENT* respectively,
85 ;;; which was also confusing.)
86 (declaim (type (or component null) *current-component*))
87 (defvar *current-component*)
89 ;;; *CURRENT-PATH* is the source path of the form we are currently
90 ;;; translating. See NODE-SOURCE-PATH in the NODE structure.
91 (declaim (list *current-path*))
92 (defvar *current-path*)
94 (defun call-with-current-source-form (thunk &rest forms)
95 (let ((*current-path* (or (and (some #'identity forms)
96 (boundp '*source-paths*)
97 (some #'get-source-path forms))
98 (and (boundp '*current-path*)
99 *current-path*))))
100 (funcall thunk)))
102 (defvar *derive-function-types* nil
103 "Should the compiler assume that function types will never change,
104 so that it can use type information inferred from current definitions
105 to optimize code which uses those definitions? Setting this true
106 gives non-ANSI, early-CMU-CL behavior. It can be useful for improving
107 the efficiency of stable code.")
109 ;;;; namespace management utilities
111 ;;; Unpack (INFO :FUNCTION :INLINING-DATA FUN-NAME). If an explicit expansion
112 ;;; is not stored but FUN-NAME is a structure constructor, reconstitute the
113 ;;; expansion from the defstruct description. Secondary value is T if the expansion
114 ;;; was explicit. See SAVE-INLINE-EXPANSION-P for why we care.
115 ;;; (Essentially, once stored then always stored, lest inconsistency result)
116 ;;; If we have just a DXABLE-ARGS, or nothing at all, return NIL and NIL.
117 ;;; If called on a string or anything that is not a function designator,
118 ;;; return NIL and NIL.
119 (declaim (ftype (sfunction (t) (values list boolean))
120 fun-name-inline-expansion))
121 (defun fun-name-inline-expansion (fun-name)
122 (multiple-value-bind (answer winp) (info :function :inlining-data fun-name)
123 (typecase answer
124 ;; an INLINING-DATA is a DXABLE-ARGS, so test it first
125 (inlining-data (setq answer (inlining-data-expansion answer)))
126 (dxable-args (setq answer nil winp nil)))
127 (when (and (not winp) (symbolp fun-name))
128 (let ((info (info :function :source-transform fun-name)))
129 (when (typep info '(cons defstruct-description (eql :constructor)))
130 (let* ((dd (car info)) (spec (assq fun-name (dd-constructors dd))))
131 (aver spec)
132 (setq answer `(lambda ,@(structure-ctor-lambda-parts dd (cdr spec))))))))
133 (values answer winp)))
134 (defun fun-name-dx-args (fun-name)
135 (let ((answer (info :function :inlining-data fun-name)))
136 (when (typep answer 'dxable-args)
137 (dxable-args-list answer))))
139 ;; As with LEXENV-FIND, we assume use of *LEXENV*, but macroexpanders
140 ;; receive an explicit environment and should pass it.
141 ;; A declaration will trump a proclamation.
142 (defun fun-lexically-notinline-p (name &optional (env *lexenv*))
143 (let ((answer
144 (typecase env
145 (null nil)
146 #+(and sb-fasteval (not sb-xc-host))
147 (sb-interpreter:basic-env
148 (sb-interpreter::fun-lexically-notinline-p name env))
150 (let ((fun (cdr (assoc name (lexenv-funs env) :test #'equal))))
151 ;; FIXME: this seems to omit FUNCTIONAL
152 (when (defined-fun-p fun)
153 (return-from fun-lexically-notinline-p
154 (eq (defined-fun-inlinep fun) 'notinline)))
155 (loop for data in (lexenv-user-data env)
156 when (and (eq (car data) 'no-compiler-macro)
157 (eq (cdr data) name))
159 (return-from fun-lexically-notinline-p
160 t)))))))
161 ;; If ANSWER is NIL, go for the global value
162 (eq (or answer (info :function :inlinep name)) 'notinline)))
165 (declaim (start-block find-free-fun find-lexically-apparent-fun
166 ;; needed by ir1-translators
167 find-global-fun))
169 (defun maybe-defined-here (name where)
170 (if (and (eq :defined where)
171 (boundp '*compilation*)
172 (member name (fun-names-in-this-file *compilation*) :test #'equal))
173 :defined-here
174 where))
176 ;;; Return a GLOBAL-VAR structure usable for referencing the global
177 ;;; function NAME.
178 (defun find-global-fun (name latep)
179 (let ((kind (info :function :kind name)))
180 (unless kind
181 (setf (info :function :kind name) :function)
182 (setf (info :function :where-from name) :assumed))
183 (let ((where (info :function :where-from name)))
184 (when (and (eq where :assumed)
185 ;; Slot accessors are defined just-in-time, if not already.
186 (not (typep name '(cons (eql sb-pcl::slot-accessor))))
187 ;; In the ordinary target Lisp, it's silly to report
188 ;; undefinedness when the function is defined in the
189 ;; running Lisp. But at cross-compile time, the current
190 ;; definedness of a function is irrelevant to the
191 ;; definedness at runtime, which is what matters.
192 #-sb-xc-host (not (fboundp name))
193 ;; LATEP is true when the user has indicated that
194 ;; late-late binding is desired by using eg. a quoted
195 ;; symbol -- in which case it makes little sense to
196 ;; complain about undefined functions.
197 (not latep))
198 (note-undefined-reference name :function))
199 (case kind
200 ((:macro :special-form)
201 (compiler-warn "~(~a~) ~s where a function is expected" kind name)))
202 (let ((ftype (global-ftype name))
203 (notinline (fun-lexically-notinline-p name)))
204 #-sb-xc-host
205 (when (and (eq where :declared)
206 (policy *lexenv* (and (>= safety 1)
207 (= debug 3)))
208 (not (or
209 (info :function :info name)
210 (let ((name (if (consp name)
211 (second name)
212 name)))
213 (system-package-p (symbol-package name))))))
214 (setf where :declared-verify))
215 (make-global-var
216 :kind :global-function
217 :%source-name name
218 :type (if (or (eq where :declared)
219 (and (not latep)
220 (not notinline)
221 *derive-function-types*))
222 ftype
223 (specifier-type 'function))
224 :defined-type (if (and (not latep) (not notinline))
225 ftype
226 (specifier-type 'function))
227 :where-from (if notinline
228 where
229 (maybe-defined-here name where)))))))
231 ;;; If NAME already has a valid entry in (FREE-FUNS *IR1-NAMESPACE*), then return
232 ;;; the value. Otherwise, make a new GLOBAL-VAR using information from
233 ;;; the global environment and enter it in FREE-FUNS. If NAME
234 ;;; names a macro or special form, then we error out using the
235 ;;; supplied context which indicates what we were trying to do that
236 ;;; demanded a function.
237 (declaim (ftype (sfunction (t string) global-var) find-free-fun))
238 (defun find-free-fun (name context &aux (free-funs (free-funs *ir1-namespace*)))
239 (or (gethash name free-funs)
240 (let ((kind (info :function :kind name)))
241 (ecase kind
242 ((:macro :special-form)
243 (compiler-error "The ~(~S~) name ~S was found ~A."
244 kind name context))
245 ((:function nil)
246 (check-fun-name name)
247 (let ((expansion (fun-name-inline-expansion name))
248 (inlinep (info :function :inlinep name)))
249 (setf (gethash name free-funs)
250 (if (or expansion inlinep)
251 (let ((where (info :function :where-from name)))
252 (make-defined-fun
253 :%source-name name
254 :inline-expansion expansion
255 :inlinep inlinep
256 :where-from (if (eq inlinep 'notinline)
257 where
258 (maybe-defined-here name where))
259 :type (if (and (eq inlinep 'notinline)
260 (neq where :declared))
261 (specifier-type 'function)
262 (global-ftype name))))
263 (find-global-fun name nil)))))))))
265 ;;; Return the LEAF structure for the lexically apparent function
266 ;;; definition of NAME.
267 (declaim (ftype (sfunction (t string) leaf) find-lexically-apparent-fun))
268 (defun find-lexically-apparent-fun (name context)
269 (let ((var (lexenv-find name funs :test #'equal)))
270 (cond (var
271 (unless (leaf-p var)
272 (aver (and (consp var) (eq (car var) 'macro)))
273 (compiler-error "found macro name ~S ~A" name context))
274 var)
276 (find-free-fun name context)))))
278 (declaim (end-block))
280 ;;; Return the LEAF node for a global variable reference to NAME. If
281 ;;; NAME is already entered in (FREE-VARS *IR1-NAMESPACE*), then we just return the
282 ;;; corresponding value. Otherwise, we make a new leaf using
283 ;;; information from the global environment and enter it in
284 ;;; FREE-VARS.
285 (declaim (ftype (sfunction (t) (or leaf cons heap-alien-info)) find-free-var))
286 (defun find-free-var (name &aux (free-vars (free-vars *ir1-namespace*))
287 (existing (gethash name free-vars)))
288 (unless (symbolp name)
289 (compiler-error "Variable name is not a symbol: ~S." name))
290 (or (when (and existing (neq existing :deprecated))
291 existing)
292 (let ((kind (info :variable :kind name))
293 (type (info :variable :type name))
294 (where-from (info :variable :where-from name))
295 (deprecation-state (deprecated-thing-p 'variable name)))
296 ;; For deprecated vars, warn about LET and LAMBDA bindings, SETQ, and ref.
297 ;; Don't warn again if the name was already seen by the transform
298 ;; of SYMBOL[-GLOBAL]-VALUE.
299 (unless (eq existing :deprecated)
300 (case deprecation-state
301 ((:early :late)
302 (check-deprecated-thing 'variable name))))
303 (setf (gethash name free-vars)
304 (case kind
305 (:alien
306 (info :variable :alien-info name))
307 ;; FIXME: The return value in this case should really be
308 ;; of type SB-C::LEAF. I don't feel too badly about it,
309 ;; because the MACRO idiom is scattered throughout this
310 ;; file, but it should be cleaned up so we're not
311 ;; throwing random conses around. --njf 2002-03-23
312 (:macro
313 (let ((expansion (info :variable :macro-expansion name))
314 (type (type-specifier (info :variable :type name))))
315 `(macro . (the ,type ,expansion))))
316 (:constant
317 (let ((value (symbol-value name)))
318 (make-constant value (ctype-of value) name)))
320 (make-global-var :kind kind
321 :%source-name name
322 :type type
323 :where-from where-from)))))))
325 ;;; Return T if and only if OBJ's nature as an externalizable thing renders
326 ;;; it a leaf for dumping purposes. Symbols are leaflike despite havings slots
327 ;;; containing pointers; similarly (COMPLEX RATIONAL) and RATIO.
328 (defun dumpable-leaflike-p (obj)
329 (or (sb-xc:typep obj '(or symbol number character
330 ;; (ARRAY NIL) is not included in UNBOXED-ARRAY
331 (or unboxed-array (array nil))
332 system-area-pointer
333 #+sb-simd-pack simd-pack
334 #+sb-simd-pack-256 simd-pack-256))
335 ;; STANDARD-OBJECT layouts use MAKE-LOAD-FORM, but all other layouts
336 ;; have the same status as symbols - composite objects but leaflike.
337 (and (typep obj 'layout) (not (layout-for-pcl-obj-p obj)))
338 ;; PACKAGEs are also leaflike.
339 (cl:typep obj 'package)
340 ;; The cross-compiler wants to dump CTYPE instances as leaves,
341 ;; but CLASSOIDs are excluded since they have a MAKE-LOAD-FORM method.
342 #+sb-xc-host (cl:typep obj '(and ctype (not classoid)))
343 (eq obj sb-lockless:+tail+)))
345 ;;; Grovel over CONSTANT checking for any sub-parts that need to be
346 ;;; processed with MAKE-LOAD-FORM. We have to be careful, because
347 ;;; CONSTANT might be circular. We also check that the constant (and
348 ;;; any subparts) are dumpable at all.
349 (defun maybe-emit-make-load-forms (constant)
350 (declare #-sb-xc-host (inline alloc-xset))
351 (dx-let ((things-processed (alloc-xset)))
352 (named-let grovel ((value constant))
353 (unless (or (dumpable-leaflike-p value)
354 (xset-member-p value things-processed)
355 #-sb-xc-host
356 (unbound-marker-p value))
357 (add-to-xset value things-processed)
358 ;; FIXME: shouldn't this be something like SB-XC:TYPECASE ?
359 (typecase value
360 (cons
361 (grovel (car value))
362 (grovel (cdr value)))
363 (simple-vector
364 (dotimes (i (length value))
365 (grovel (svref value i))))
366 ((vector t)
367 (dotimes (i (length value))
368 (grovel (aref value i))))
369 ((simple-array t)
370 ;; Even though the (ARRAY T) branch does the exact
371 ;; same thing as this branch we do this separately
372 ;; so that the compiler can use faster versions of
373 ;; array-total-size and row-major-aref.
374 (dotimes (i (array-total-size value))
375 (grovel (row-major-aref value i))))
376 ((array t)
377 (dotimes (i (array-total-size value))
378 (grovel (row-major-aref value i))))
379 (instance
380 ;; Behold the wonderfully clear sense of this-
381 ;; WHEN (EMIT-MAKE-LOAD-FORM VALUE)
382 ;; meaning "when you're _NOT_ using a custom load-form"
384 ;; FIXME: What about funcallable instances with
385 ;; user-defined MAKE-LOAD-FORM methods?
386 (when (emit-make-load-form value)
387 #+sb-xc-host
388 (aver (eql (layout-bitmap (%instance-layout value))
389 sb-kernel:+layout-all-tagged+))
390 (do-instance-tagged-slot (i value)
391 (grovel (%instance-ref value i)))))
393 (compiler-error
394 "Objects of type ~/sb-impl:print-type-specifier/ can't be dumped into fasl files."
395 (type-of value)))))))
396 (values))
398 ;;;; some flow-graph hacking utilities
400 ;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws out of the
401 ;; body and converts a condition signalling form instead. The source
402 ;; form is converted to a string since it may contain arbitrary
403 ;; non-externalizable objects.
404 (defmacro ir1-error-bailout ((start next result form) &body body)
405 (with-unique-names (skip condition)
406 `(block ,skip
407 (let ((,condition (catch 'ir1-error-abort
408 (let ((*compiler-error-bailout*
409 (lambda (&optional e)
410 (throw 'ir1-error-abort e))))
411 ,@body
412 (return-from ,skip nil)))))
413 (ir1-convert ,start ,next ,result
414 (make-compiler-error-form ,condition
415 ,form))))))
417 ;;; This function sets up the back link between the node and the
418 ;;; ctran which continues at it.
419 (defun link-node-to-previous-ctran (node ctran)
420 (declare (type node node) (type ctran ctran))
421 (aver (not (ctran-next ctran)))
422 (setf (ctran-next ctran) node)
423 (setf (node-prev node) ctran))
425 ;;; This function is used to set the ctran for a node, and thus
426 ;;; determine what is evaluated next. If the ctran has no block, then
427 ;;; we make it be in the block that the node is in. If the ctran heads
428 ;;; its block, we end our block and link it to that block.
429 (declaim (inline use-ctran))
430 (defun use-ctran (node ctran)
431 (declare (type node node) (type ctran ctran))
432 (if (eq (ctran-kind ctran) :unused)
433 (let ((node-block (ctran-block (node-prev node))))
434 (setf (ctran-block ctran) node-block)
435 (setf (ctran-kind ctran) :inside-block)
436 (setf (ctran-use ctran) node)
437 (setf (node-next node) ctran))
438 (%use-ctran node ctran)))
439 (defun %use-ctran (node ctran)
440 (declare (type node node) (type ctran ctran) (inline member))
441 (let ((block (ctran-block ctran))
442 (node-block (ctran-block (node-prev node))))
443 (aver (eq (ctran-kind ctran) :block-start))
444 (when (block-last node-block)
445 (error "~S has already ended." node-block))
446 (setf (block-last node-block) node)
447 (when (block-succ node-block)
448 (error "~S already has successors." node-block))
449 (setf (block-succ node-block) (list block))
450 (when (memq node-block (block-pred block))
451 (error "~S is already a predecessor of ~S." node-block block))
452 (push node-block (block-pred block))))
454 ;;; Insert NEW before OLD in the flow-graph.
455 (defun insert-node-before (old new)
456 (let ((prev (node-prev old))
457 (temp (make-ctran)))
458 (setf (ctran-next prev) nil)
459 (link-node-to-previous-ctran new prev)
460 (use-ctran new temp)
461 (setf (ctran-source-path temp) (ctran-source-path prev))
462 (link-node-to-previous-ctran old temp))
463 (values))
465 (defun insert-node-after (old new)
466 (let ((next (node-next old)))
467 (cond (next
468 (insert-node-before (ctran-next next) new))
470 (let ((ctran (make-ctran)))
471 (link-node-to-previous-ctran new ctran)
472 (setf (block-last (node-block old)) new)
473 (use-ctran old ctran))))))
475 ;;; This function is used to set the ctran for a node, and thus
476 ;;; determine what receives the value.
477 (defun use-lvar (node lvar)
478 (declare (type valued-node node) (type (or lvar null) lvar))
479 (aver (not (node-lvar node)))
480 (when lvar
481 (setf (node-lvar node) lvar)
482 (cond ((null (lvar-uses lvar))
483 (setf (lvar-uses lvar) node))
484 ((listp (lvar-uses lvar))
485 (aver (not (memq node (lvar-uses lvar))))
486 (push node (lvar-uses lvar)))
488 (aver (neq node (lvar-uses lvar)))
489 (setf (lvar-uses lvar) (list node (lvar-uses lvar)))))
490 (reoptimize-lvar lvar)))
492 (declaim (inline use-continuation))
493 (defun use-continuation (node ctran lvar)
494 (use-ctran node ctran)
495 (use-lvar node lvar))
497 ;;;; exported functions
499 ;;; This function takes a form and the top level form number for that
500 ;;; form, and returns a lambda representing the translation of that
501 ;;; form in the current global environment. The returned lambda is a
502 ;;; top level lambda that can be called to cause evaluation of the
503 ;;; forms. This lambda is in the initial component. If FOR-VALUE is T,
504 ;;; then the value of the form is returned from the function,
505 ;;; otherwise NIL is returned.
507 ;;; This function may have arbitrary effects on the global environment
508 ;;; due to processing of EVAL-WHENs. All syntax error checking is
509 ;;; done, with erroneous forms being replaced by a proxy which signals
510 ;;; an error if it is evaluated. Warnings about possibly inconsistent
511 ;;; or illegal changes to the global environment will also be given.
513 ;;; We make the initial component and convert the form in a PROGN (and
514 ;;; an optional NIL tacked on the end.) We then return the lambda. We
515 ;;; bind all of our state variables here, rather than relying on the
516 ;;; global value (if any) so that IR1 conversion will be reentrant.
517 ;;; This is necessary for EVAL-WHEN processing, etc.
519 ;;; The hashtables used to hold global namespace info must be
520 ;;; reallocated elsewhere. Note also that *LEXENV* is not bound, so
521 ;;; that local macro definitions can be introduced by enclosing code.
522 (defun ir1-toplevel (form path for-value &optional (allow-instrumenting t))
523 (declare (list path))
524 (let* ((*current-path* path)
525 (component (make-empty-component))
526 (*current-component* component)
527 (*allow-instrumenting* allow-instrumenting))
528 (setf (component-name component) 'initial-component)
529 (setf (component-kind component) :initial)
530 (let* ((forms (if for-value `(,form) `(,form nil)))
531 (res (ir1-convert-lambda-body
532 forms () :debug-name "top level form")))
533 (setf (functional-entry-fun res) res
534 (functional-arg-documentation res) ()
535 (functional-kind res) (functional-kind-attributes toplevel))
536 res)))
538 ;;; This function is called on freshly read forms to record the
539 ;;; initial location of each form (and subform.) Form is the form to
540 ;;; find the paths in, and TLF-NUM is the top level form number of the
541 ;;; truly top level form.
543 ;;; This gets a bit interesting when the source code is circular. This
544 ;;; can (reasonably?) happen in the case of circular list constants.
545 (defun find-source-paths (form tlf-num)
546 (declare (type index tlf-num))
547 (let ((*current-form-number* 0))
548 (sub-find-source-paths form (list tlf-num)))
549 (values))
550 (defun sub-find-source-paths (form path)
551 (unless (get-source-path form)
552 (note-source-path form path)
553 (incf *current-form-number*)
554 (let ((pos 0)
555 (subform form)
556 (trail form))
557 (declare (fixnum pos))
558 (macrolet ((frob ()
559 `(progn
560 (let ((fm (cond ((comma-p subform)
561 (comma-expr subform))
562 ((atom subform)
563 (return))
565 (car subform)))))
566 (when (comma-p fm)
567 (setf fm (comma-expr fm)))
568 (cond ((consp fm)
569 ;; If it's a cons, recurse.
570 (sub-find-source-paths fm (cons pos path)))
571 ((eq 'quote fm)
572 ;; Don't look into quoted constants.
573 ;; KLUDGE: this can't actually know about constants.
574 ;; e.g. (let ((quote (error "foo")))) or
575 ;; (list quote (error "foo")) are not
576 ;; constants and yet are ignored.
577 (return))
578 ((not (zerop pos))
579 ;; Otherwise store the containing form. It's not
580 ;; perfect, but better than nothing.
581 (note-source-path subform pos path)))
582 (incf pos))
583 (when (comma-p subform)
584 (return))
585 (setq subform (cdr subform))
586 (when (eq subform trail) (return)))))
587 (loop
588 (frob)
589 (frob)
590 (setq trail (cdr trail)))))))
593 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
595 (declaim (start-block ir1-convert ir1-convert-progn-body
596 ir1-convert-combination-args reference-leaf
597 reference-constant
598 expand-compiler-macro
599 maybe-reanalyze-functional
600 ir1-convert-common-functoid))
602 ;;; Translate FORM into IR1. The code is inserted as the NEXT of the
603 ;;; CTRAN START. RESULT is the LVAR which receives the value of the
604 ;;; FORM to be translated. The translators call this function
605 ;;; recursively to translate their subnodes.
607 ;;; As a special hack to make life easier in the compiler, a LEAF
608 ;;; IR1-converts into a reference to that LEAF structure. This allows
609 ;;; the creation using backquote of forms that contain leaf
610 ;;; references, without having to introduce dummy names into the
611 ;;; namespace.
612 (defun ir1-convert (start next result form)
613 (declare (type ctran start next)
614 (type (or lvar null) result))
615 (let* ((*current-path* (ensure-source-path form))
616 (start (instrument-coverage start nil form)))
617 (ir1-error-bailout (start next result form)
618 (cond ((atom form)
619 (cond ((and (symbolp form) (not (keywordp form)))
620 (ir1-convert-var start next result form))
621 ((leaf-p form)
622 (reference-leaf start next result form))
624 (reference-constant start next result form))))
626 (ir1-convert-functoid start next result form)))))
627 (values))
629 ;;; Generate a reference to a manifest constant, creating a new leaf
630 ;;; if necessary. If we are producing a fasl file, make sure that
631 ;;; MAKE-LOAD-FORM gets used on any parts of the constant that it
632 ;;; needs to be.
633 (defun reference-constant (start next result value &optional (emit-load-forms (producing-fasl-file)))
634 (declare (type ctran start next)
635 (type (or lvar null) result))
636 (when emit-load-forms
637 (maybe-emit-make-load-forms value))
638 (let* ((leaf (find-constant value))
639 (res (make-ref leaf)))
640 (push res (leaf-refs leaf))
641 (link-node-to-previous-ctran res start)
642 (use-continuation res next result))
643 (values))
645 ;;; Add FUNCTIONAL to the COMPONENT-REANALYZE-FUNCTIONALS, unless it's
646 ;;; some trivial type for which reanalysis is a trivial no-op.
648 ;;; FUNCTIONAL is returned.
649 (defun maybe-reanalyze-functional (functional)
650 (aver (not (functional-kind-eq functional deleted))) ; bug 148
651 (aver-live-component *current-component*)
652 ;; When FUNCTIONAL is of a type for which reanalysis isn't a trivial
653 ;; no-op
654 (when (typep functional '(or optional-dispatch clambda))
655 (pushnew functional
656 (component-reanalyze-functionals *current-component*)))
657 functional)
659 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
660 ;;; needed. If LEAF represents a defined function which has already
661 ;;; been converted in the same compilation block, and is not
662 ;;; NOTINLINE, then reference the functional instead.
663 (defun reference-leaf (start next result leaf &optional (name '.anonymous.))
664 (declare (type ctran start next) (type (or lvar null) result) (type leaf leaf))
665 (assure-leaf-live-p leaf)
666 (let* ((type (lexenv-find leaf type-restrictions))
667 (leaf (or (and (defined-fun-p leaf)
668 (neq (defined-fun-inlinep leaf) 'notinline)
669 (defined-fun-same-block-p leaf)
670 (let ((functional (defined-fun-functional leaf)))
671 (when (and functional (functional-kind-eq functional nil))
672 (maybe-reanalyze-functional functional))))
673 (when (and (functional-p leaf)
674 (functional-kind-eq leaf nil optional))
675 (maybe-reanalyze-functional leaf))
676 leaf))
677 (ref (make-ref leaf name)))
678 (when (and result
679 (lambda-var-p leaf)
680 (lambda-var-constant leaf))
681 (push (make-lvar-lambda-var-annotation :lambda-var leaf)
682 (lvar-annotations result)))
683 (push ref (leaf-refs leaf))
684 (when (and (functional-p leaf)
685 (functional-ignore leaf))
686 (#-sb-xc-host compiler-style-warn
687 #+sb-xc-host warn
688 "Calling an ignored function: ~S" (leaf-debug-name leaf)))
689 (setf (leaf-ever-used leaf) t)
690 (link-node-to-previous-ctran ref start)
691 (cond (type (let* ((ref-ctran (make-ctran))
692 (ref-lvar (make-lvar))
693 (cast (make-cast ref-lvar
694 (make-single-value-type type)
695 (lexenv-policy *lexenv*))))
696 (setf (lvar-dest ref-lvar) cast)
697 (use-continuation ref ref-ctran ref-lvar)
698 (link-node-to-previous-ctran cast ref-ctran)
699 (use-continuation cast next result)))
700 (t (use-continuation ref next result)))))
702 ;;; Convert a reference to a symbolic constant or variable. If the
703 ;;; symbol is entered in the LEXENV-VARS we use that definition,
704 ;;; otherwise we find the current global definition. This is also
705 ;;; where we pick off symbol macro and alien variable references.
706 (defun ir1-convert-var (start next result name)
707 (declare (type ctran start next) (type (or lvar null) result) (symbol name))
708 (let ((var (or (lexenv-find name vars) (find-free-var name))))
709 (etypecase var
710 (leaf
711 (when (lambda-var-p var)
712 (let ((home (ctran-home-lambda-or-null start)))
713 (when (and home (neq (lambda-var-home var) home))
714 (sset-adjoin var (lambda-calls-or-closes home))))
715 (when (lambda-var-ignorep var)
716 ;; (ANSI's specification for the IGNORE declaration requires
717 ;; that this be a STYLE-WARNING, not a full WARNING.)
718 #-sb-xc-host
719 (compiler-style-warn "reading an ignored variable: ~S" name)
720 ;; there's no need for us to accept ANSI's lameness when
721 ;; processing our own code, though.
722 #+sb-xc-host
723 (warn "reading an ignored variable: ~S" name)))
724 (maybe-note-undefined-variable-reference var name)
725 (reference-leaf start next result var name))
726 ((cons (eql macro)) ; symbol-macro
727 ;; FIXME: the following comment is probably wrong now.
728 ;; If we warn here on :early and :late deprecation
729 ;; then we get an extra warning somehow.
730 ;; This case signals {EARLY,LATE,FINAL}-DEPRECATION-WARNING
731 ;; for symbol-macros. Includes variables, constants,
732 ;; etc. in :FINAL deprecation.
733 (when (eq (deprecated-thing-p 'variable name) :final)
734 (check-deprecated-thing 'variable name))
735 ;; FIXME: [Free] type declarations. -- APD, 2002-01-26
736 (ir1-convert start next result (cdr var)))
737 (heap-alien-info
738 (ir1-convert start next result `(%heap-alien ',var)))))
739 (values))
741 ;;; Find a compiler-macro for a form, taking FUNCALL into account.
742 (defun find-compiler-macro (opname form)
743 (flet ((legal-cm-name-p (name)
744 (and (legal-fun-name-p name)
745 (or (not (symbolp name))
746 (not (macro-function name *lexenv*))))))
747 (if (eq opname 'funcall)
748 (let ((fun-form (cadr form)))
749 (cond ((and (consp fun-form) (eq 'function (car fun-form))
750 (not (cddr fun-form)))
751 (let ((real-fun (cadr fun-form)))
752 (if (legal-cm-name-p real-fun)
753 (values (compiler-macro-function real-fun *lexenv*)
754 real-fun)
755 (values nil nil))))
756 ((constantp fun-form *lexenv*)
757 (let ((fun (constant-form-value fun-form *lexenv*)))
758 (if (legal-cm-name-p fun)
759 ;; CLHS tells us that local functions must shadow
760 ;; compiler-macro-functions, but since the call is
761 ;; through a name, we are obviously interested
762 ;; in the global function.
763 ;; KLUDGE: CLHS 3.2.2.1.1 also says that it can be
764 ;; "a list whose car is funcall and whose cadr is
765 ;; a list (function name)", that means that
766 ;; (funcall 'name) that gets here doesn't fit the
767 ;; definition.
768 (values (compiler-macro-function fun nil) fun)
769 (values nil nil))))
771 (values nil nil))))
772 (if (legal-fun-name-p opname)
773 (values (compiler-macro-function opname *lexenv*) opname)
774 (values nil nil)))))
776 ;;; If FORM has a usable compiler macro, use it; otherwise return FORM itself.
777 ;;; Return the name of the compiler-macro as a secondary value, if applicable.
778 (defun expand-compiler-macro (form)
779 (binding* ((name (car form))
780 ((cmacro-fun cmacro-fun-name)
781 (find-compiler-macro name form)))
782 (cond
783 ((and cmacro-fun
784 ;; CLHS 3.2.2.1.3 specifies that NOTINLINE
785 ;; suppresses compiler-macros.
786 (not (fun-lexically-notinline-p cmacro-fun-name)))
787 (check-deprecated-thing 'function name)
788 (values (handler-case (careful-expand-macro cmacro-fun form t)
789 (compiler-macro-keyword-problem (condition)
790 (print-compiler-message
791 *error-output* "note: ~A" (list condition))
792 form))
793 cmacro-fun-name))
795 (values form nil)))))
797 ;;; Picks off special forms and compiler-macro expansions, and hands
798 ;;; the rest to IR1-CONVERT-COMMON-FUNCTOID
799 (defun ir1-convert-functoid (start next result form)
800 (let* ((op (car form))
801 (translator (and (symbolp op) (info :function :ir1-convert op))))
802 (if translator
803 (funcall translator start next result (proper-list form))
804 (multiple-value-bind (res cmacro-fun-name)
805 (expand-compiler-macro form)
806 (cond ((eq res form)
807 (ir1-convert-common-functoid start next result form op))
809 (unless (policy *lexenv* (zerop store-xref-data))
810 (record-call cmacro-fun-name (ctran-block start)
811 *current-path*))
812 (ir1-convert start next result res)))))))
814 ;;; Handles the "common" cases: any other forms except special forms
815 ;;; and compiler-macros.
816 (defun ir1-convert-common-functoid (start next result form op)
817 (cond ((or (symbolp op) (leaf-p op))
818 (let ((lexical-def (if (leaf-p op) op (lexenv-find op funs))))
819 (typecase lexical-def
820 (null
821 (check-deprecated-thing 'function op)
822 (ir1-convert-global-functoid start next result form op))
823 (functional
824 (ir1-convert-local-combination start next result form
825 lexical-def))
826 (global-var
827 (check-deprecated-thing 'function (leaf-source-name lexical-def))
828 (ir1-convert-srctran start next result lexical-def form))
830 (aver (and (consp lexical-def) (eq (car lexical-def) 'macro)))
831 ;; Unlike inline expansion, macroexpansion is not optional,
832 ;; but we clearly can't recurse forever.
833 (let ((expansions (memq lexical-def *inline-expansions*)))
834 (if (<= (or (cadr expansions) 0) *inline-expansion-limit*)
835 (let ((*inline-expansions*
836 (if expansions
837 (progn (incf (cadr expansions))
838 *inline-expansions*)
839 (list* lexical-def 1 *inline-expansions*))))
840 (ir1-convert start next result
841 (careful-expand-macro (cdr lexical-def) form)))
842 (progn
843 (compiler-warn "Recursion limit reached while expanding local macro ~
844 ~/sb-ext:print-symbol-with-prefix/" op)
845 ;; Treat it as an global function, which is probably
846 ;; what was intended. The expansion thus far could be wrong,
847 ;; but that's not our problem.
848 (ir1-convert-global-functoid start next result form op))))))))
849 ((or (atom op) (not (eq (car op) 'lambda)))
850 (compiler-error "illegal function call"))
852 ;; implicitly (LAMBDA ..) because the LAMBDA expression is
853 ;; the CAR of an executed form.
854 (ir1-convert start next result `(%funcall ,@form)))))
856 ;;; Convert anything that looks like a global function call.
857 (defun ir1-convert-global-functoid (start next result form fun)
858 (declare (type ctran start next) (type (or lvar null) result)
859 (list form))
860 (when (eql fun 'declare)
861 (compiler-error
862 "~@<There is no function named ~S. ~
863 References to ~S in some contexts (like starts of blocks) are unevaluated ~
864 expressions, but here the expression is being evaluated, which invokes ~
865 undefined behaviour.~@:>" fun fun))
866 ;; FIXME: Couldn't all the INFO calls here be converted into
867 ;; standard CL functions, like MACRO-FUNCTION or something? And what
868 ;; happens with lexically-defined (MACROLET) macros here, anyway?
869 (ecase (info :function :kind fun)
870 (:macro
871 (ir1-convert start next result
872 (careful-expand-macro (info :function :macro-function fun)
873 form))
874 (unless (policy *lexenv* (zerop store-xref-data))
875 (record-macroexpansion fun (ctran-block start) *current-path*)))
876 ((nil :function)
877 (ir1-convert-srctran start next result
878 (find-free-fun fun "shouldn't happen! (no-cmacro)")
879 form))))
881 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
882 ;;; errors which occur during the macroexpansion.
883 (defun careful-expand-macro (fun form &optional cmacro)
884 (flet (;; Return a string to use as a prefix in error reporting,
885 ;; telling something about which form caused the problem.
886 (wherestring ()
887 (let (;; We rely on the printer to abbreviate FORM.
888 (*print-length* 3)
889 (*print-level* 3))
890 (format nil
891 "~@<~A of ~S. Use ~S to intercept.~%~:@>"
892 (cond (cmacro
893 #-sb-xc-host "Error during compiler-macroexpansion"
894 #+sb-xc-host "Error during XC compiler-macroexpansion")
896 #-sb-xc-host "during macroexpansion"
897 #+sb-xc-host "during XC macroexpansion"))
898 form
899 '*break-on-signals*))))
900 (handler-bind ((error
901 (lambda (c)
902 (cond
903 (cmacro
904 ;; The spec is silent on what we should do. Signaling
905 ;; a full warning but declining to expand seems like
906 ;; a conservative and sane thing to do.
907 (compiler-warn "~@<~A~@:_ ~A~:>" (wherestring) c)
908 (return-from careful-expand-macro form))
910 (compiler-error "~@<~A~@:_ ~A~:>"
911 (wherestring) c))))))
912 (funcall (valid-macroexpand-hook) fun form *lexenv*))))
914 ;;;; conversion utilities
916 ;;; Convert a bunch of forms, discarding all the values except the
917 ;;; last. If there aren't any forms, then translate a NIL.
918 (defun ir1-convert-progn-body (start next result body)
919 (declare (type ctran start next)
920 (type (or lvar null) result)
921 (type list body))
922 (if (endp body)
923 (reference-constant start next result nil)
924 (let ((this-start start)
925 (forms body))
926 (loop
927 (let ((form (car forms)))
928 (setf this-start
929 (maybe-instrument-progn-like this-start forms form))
930 (when (endp (cdr forms))
931 (ir1-convert this-start next result form)
932 (return))
933 (let ((this-ctran (make-ctran)))
934 (ir1-convert this-start this-ctran nil form)
935 (setq this-start this-ctran
936 forms (cdr forms)))))))
937 (values))
940 ;;;; converting combinations
942 ;;; Does this form look like something that we should add single-stepping
943 ;;; instrumentation for?
944 (defun step-form-p (form)
945 (flet ((step-symbol-p (symbol)
946 (not (member (sb-xc:symbol-package symbol)
947 (load-time-value
948 ;; KLUDGE: packages we're not interested in
949 ;; stepping.
950 (mapcar #'find-package '(sb-c sb-int sb-impl
951 sb-kernel sb-pcl))
952 t)))))
953 (and *allow-instrumenting*
954 (policy *lexenv* (= insert-step-conditions 3))
955 (listp form)
956 (symbolp (car form))
957 (step-symbol-p (car form)))))
959 ;;; Convert a function call where the function FUN is a LEAF. FORM is
960 ;;; the source for the call. We return the COMBINATION node so that
961 ;;; the caller can poke at it if it wants to.
962 (defun ir1-convert-combination (start next result form fun)
963 (declare (type ctran start next)
964 (type (or lvar null) result)
965 (type list form)
966 (type leaf fun)
967 #-sb-xc-host (values combination))
968 (let ((ctran (make-ctran))
969 (fun-lvar (make-lvar)))
970 (reference-leaf start ctran fun-lvar fun)
971 (let ((combination
972 (ir1-convert-combination-args fun-lvar ctran next result
973 (cdr (proper-list form)))))
974 (when (step-form-p form)
975 ;; Store a string representation of the form in the
976 ;; combination node. This will let the IR2 translator know
977 ;; that we want stepper instrumentation for this node. The
978 ;; string will be stored in the debug-info by DUMP-1-LOCATION.
979 (setf (combination-step-info combination)
980 (let ((*print-pretty* t)
981 (*print-circle* t)
982 (*print-readably* nil))
983 (prin1-to-string form))))
984 combination)))
986 ;;; Convert the arguments to a call and make the COMBINATION
987 ;;; node. FUN-LVAR yields the function to call. ARGS is the list of
988 ;;; arguments for the call, which defaults to the cdr of source. We
989 ;;; return the COMBINATION node.
990 (defun ir1-convert-combination-args (fun-lvar start next result args
991 &key (pass-nargs t)
992 arg-source-forms)
993 (declare (type ctran start next)
994 (type lvar fun-lvar)
995 (type (or lvar null) result)
996 (type list args))
997 (let ((node (make-combination fun-lvar)))
998 (unless pass-nargs
999 (setf (combination-pass-nargs node) nil))
1000 (setf (lvar-dest fun-lvar) node)
1001 (collect ((arg-lvars))
1002 (let ((this-start start)
1003 (forms args))
1004 (dolist (arg args)
1005 (cond ((lvar-p arg)
1006 (aver (not (lvar-dest arg)))
1007 (arg-lvars arg)
1008 (setf (lvar-dest arg) node))
1010 (setf this-start
1011 (maybe-instrument-progn-like this-start forms arg))
1012 (let ((this-ctran (make-ctran))
1013 (this-lvar (make-lvar node))
1014 (*current-path* (or (get-source-path (pop arg-source-forms))
1015 *current-path*)))
1016 (ir1-convert this-start this-ctran this-lvar arg)
1017 (setq this-start this-ctran)
1018 (arg-lvars this-lvar))))
1019 (pop forms))
1020 (link-node-to-previous-ctran node this-start)
1021 (use-continuation node next result)
1022 (setf (combination-args node) (arg-lvars))))
1023 node))
1025 ;;; Convert a call to a global function. If not NOTINLINE, then we do
1026 ;;; source transforms and try out any inline expansion. If there is no
1027 ;;; expansion, but is INLINE, then give an efficiency note (unless a
1028 ;;; known function which will quite possibly be open-coded.) Next, we
1029 ;;; go to ok-combination conversion.
1030 (defun ir1-convert-srctran (start next result var form)
1031 (declare (type ctran start next) (type (or lvar null) result)
1032 (type global-var var))
1033 (let ((name (leaf-source-name var))
1034 (inlinep (when (defined-fun-p var)
1035 (defined-fun-inlinep var))))
1036 (if (eq inlinep 'notinline)
1037 (ir1-convert-combination start next result form var)
1038 (let ((transform (info :function :source-transform name)))
1039 (if transform
1040 (multiple-value-bind (transformed pass)
1041 (if (functionp transform)
1042 (funcall transform form *lexenv*)
1043 (struct-fun-transform transform form name))
1044 ;; Note that "pass" means fail. Gotta love it.
1045 (cond (pass
1046 (ir1-convert-maybe-predicate start next result
1047 (proper-list form)
1048 var))
1050 (unless (policy *lexenv* (zerop store-xref-data))
1051 (record-call name (ctran-block start) *current-path*))
1052 (when (show-transform-p *show-transforms-p* name)
1053 (show-transform "src" name transformed))
1054 (let ((*transforming* (1+ *transforming*)))
1055 (when (eq (car *current-path*) 'original-source-start)
1056 (setf (ctran-source-path start) *current-path*))
1057 (ir1-convert start next result transformed)))))
1058 (ir1-convert-maybe-predicate start next result
1059 (proper-list form)
1060 var))))))
1062 ;;; If the function has the PREDICATE attribute, and the RESULT's DEST
1063 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
1064 ;;; predicate always appears in a conditional context.
1066 ;;; If the function isn't a predicate, then we call
1067 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
1068 (defun ir1-convert-maybe-predicate (start next result form var)
1069 (declare (type ctran start next)
1070 (type (or lvar null) result)
1071 (list form)
1072 (type global-var var))
1073 (let ((info (info :function :info (leaf-source-name var))))
1074 (if (and info
1075 (ir1-attributep (fun-info-attributes info) predicate)
1076 (not (if-p (and result (lvar-dest result)))))
1077 (wrap-predicate start next result form var)
1078 (ir1-convert-combination-checking-type start next result form var))))
1080 ;;; Like (if form t nil) but without coverage and without inserting
1081 ;;; leafs coming from %funcall into *current-path*
1082 (defun wrap-predicate (start next result form var)
1083 (let* ((pred-ctran (make-ctran))
1084 (pred-lvar (make-lvar))
1085 (then-ctran (make-ctran))
1086 (then-block (ctran-starts-block then-ctran))
1087 (else-ctran (make-ctran))
1088 (else-block (ctran-starts-block else-ctran))
1089 (node (make-if :test pred-lvar
1090 :consequent then-block
1091 :alternative else-block)))
1092 (setf (lvar-dest pred-lvar) node)
1093 (ir1-convert-combination-checking-type start pred-ctran pred-lvar form var)
1094 (link-node-to-previous-ctran node pred-ctran)
1096 (let ((start-block (ctran-block pred-ctran)))
1097 (setf (block-last start-block) node)
1098 (ctran-starts-block next)
1100 (link-blocks start-block then-block)
1101 (link-blocks start-block else-block))
1102 (let ((*current-path* (cons t *current-path*)))
1103 (reference-constant then-ctran next result t))
1104 (let ((*current-path* (cons nil *current-path*)))
1105 (reference-constant else-ctran next result nil))))
1107 ;;; Actually really convert a global function call that we are allowed
1108 ;;; to early-bind.
1110 ;;; If we know the function type of the function, then we check the
1111 ;;; call for syntactic legality with respect to the declared function
1112 ;;; type. If it is impossible to determine whether the call is correct
1113 ;;; due to non-constant keywords, then we give up, marking the call as
1114 ;;; :FULL to inhibit further error messages. We return true when the
1115 ;;; call is legal.
1117 ;;; If the call is legal, we also propagate type assertions from the
1118 ;;; function type to the arg and result lvars. We do this now so that
1119 ;;; IR1 optimize doesn't have to redundantly do the check later so
1120 ;;; that it can do the type propagation.
1121 (defun ir1-convert-combination-checking-type (start next result form var)
1122 (declare (type ctran start next) (type (or lvar null) result)
1123 (list form)
1124 (type leaf var))
1125 (let* ((node (ir1-convert-combination start next result form var))
1126 (fun-lvar (basic-combination-fun node))
1127 (type (leaf-type var)))
1128 (when (validate-call-type node type var t)
1129 (setf (lvar-%derived-type fun-lvar)
1130 (make-single-value-type type))
1131 (setf (lvar-reoptimize fun-lvar) nil)))
1132 (values))
1134 ;;; Convert a call to a local function, or if the function has already
1135 ;;; been LET converted, then throw FUNCTIONAL to
1136 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
1137 ;;; are converting inline expansions for local functions during
1138 ;;; optimization.
1139 (defun ir1-convert-local-combination (start next result form functional)
1140 (assure-functional-live-p functional)
1141 (ir1-convert-combination start next result
1142 form
1143 (maybe-reanalyze-functional functional)))
1145 ;;;; PROCESS-DECLS
1147 (declaim (start-block process-decls make-new-inlinep
1148 find-in-bindings
1149 process-muffle-decls))
1151 ;;; Given a list of LAMBDA-VARs and a variable name, return the
1152 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
1153 ;;; *last* variable with that name, since LET* bindings may be
1154 ;;; duplicated, and declarations always apply to the last.
1155 (defun find-in-bindings (vars name)
1156 (declare (list vars) (symbol name)
1157 #-sb-xc-host (values (or lambda-var list)))
1158 (let ((found nil))
1159 (dolist (var vars)
1160 (cond ((leaf-p var)
1161 (when (eq (leaf-source-name var) name)
1162 (setq found var))
1163 (let ((info (lambda-var-arg-info var)))
1164 (when info
1165 (let ((supplied-p (arg-info-supplied-p info)))
1166 (when (and supplied-p
1167 (eq (leaf-source-name supplied-p) name))
1168 (setq found supplied-p))))))
1169 ((and (consp var) (eq (car var) name))
1170 (setf found (cdr var)))))
1171 found))
1173 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
1174 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
1175 ;;; type, otherwise we add a type restriction on the var. If a symbol
1176 ;;; macro, we just wrap a THE around the expansion.
1177 (defun process-type-decl (decl res vars context)
1178 (declare (type list decl vars) (type lexenv res))
1179 (let* ((type-specifier (first decl))
1180 (type (progn
1181 (when (typep type-specifier 'type-specifier)
1182 (check-deprecated-type type-specifier))
1183 (compiler-specifier-type type-specifier))))
1184 (if type
1185 (collect ((restr nil cons)
1186 (new-vars nil cons))
1187 (dolist (var-name (rest decl))
1188 (unless (symbolp var-name)
1189 (compiler-error "Variable name is not a symbol: ~S." var-name))
1190 (unless (eq (info :variable :kind var-name) :unknown)
1191 (program-assert-symbol-home-package-unlocked
1192 context var-name "declaring the type of ~A"))
1193 (let* ((bound-var (find-in-bindings vars var-name))
1194 (var (or bound-var
1195 (lexenv-find var-name vars)
1196 (find-free-var var-name))))
1197 (maybe-note-undefined-variable-reference var var-name)
1198 (etypecase var
1199 (leaf
1200 (flet
1201 ((process-var (var bound-var)
1202 (let* ((old-type (or (lexenv-find var type-restrictions)
1203 (leaf-type var)))
1204 (int (if (or (fun-type-p type)
1205 (fun-type-p old-type))
1206 type
1207 (type-approx-intersection2
1208 old-type type))))
1209 (cond ((eq int *empty-type*)
1210 (unless (policy *lexenv* (= inhibit-warnings 3))
1211 (warn
1212 'type-warning
1213 :format-control
1214 "The type declarations ~
1215 ~/sb-impl:print-type/ and ~
1216 ~/sb-impl:print-type/ for ~
1217 ~S conflict."
1218 :format-arguments
1219 (list old-type type var-name))))
1220 (bound-var
1221 (setf (leaf-type bound-var) int
1222 (leaf-where-from bound-var) :declared))
1224 (restr (cons var int)))))))
1225 (process-var var bound-var)
1226 (awhen (and (lambda-var-p var)
1227 (lambda-var-specvar var))
1228 (process-var it nil))))
1229 (cons
1230 ;; FIXME: non-ANSI weirdness. [See lp#309122]
1231 (aver (eq (car var) 'macro))
1232 (new-vars `(,var-name . (macro . (the ,(first decl)
1233 ,(cdr var))))))
1234 (heap-alien-info
1235 (compiler-error
1236 "~S is an alien variable, so its type can't be declared."
1237 var-name)))))
1239 (if (or (restr) (new-vars))
1240 (make-lexenv :default res
1241 :type-restrictions (restr)
1242 :vars (new-vars))
1243 res))
1244 res)))
1246 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
1247 ;;; declarations for function variables. In addition to allowing
1248 ;;; declarations for functions being bound, we must also deal with
1249 ;;; declarations that constrain the type of lexically apparent
1250 ;;; functions.
1251 (defun process-ftype-decl (type-specifier res names fvars context)
1252 (declare (type list names fvars)
1253 (type lexenv res))
1254 (let ((type (or (compiler-specifier-type type-specifier)
1255 (return-from process-ftype-decl res))))
1256 (check-deprecated-type type-specifier)
1257 (unless (csubtypep type (specifier-type 'function))
1258 (compiler-style-warn "ignoring declared FTYPE: ~S (not a function type)"
1259 type-specifier)
1260 (return-from process-ftype-decl res))
1261 (collect ((res nil cons))
1262 (dolist (name names)
1263 (when (fboundp name)
1264 (program-assert-symbol-home-package-unlocked
1265 context name "declaring the ftype of ~A"))
1266 (let ((found (find name fvars :key (lambda (x)
1267 (unless (consp x) ;; macrolet
1268 (leaf-source-name x)))
1269 :test #'equal)))
1270 (cond
1271 (found
1272 (setf (leaf-type found) type)
1273 (assert-definition-type found type
1274 :unwinnage-fun #'compiler-notify
1275 :where "FTYPE declaration"))
1277 (res (cons (find-lexically-apparent-fun
1278 name "in a function type declaration")
1279 type))))))
1280 (if (res)
1281 (make-lexenv :default res :type-restrictions (res))
1282 res))))
1284 ;;; Process a special declaration, returning a new LEXENV. A non-bound
1285 ;;; special declaration is instantiated by throwing a special variable
1286 ;;; into the variables if POST-BINDING-LEXENV is NIL,
1287 ;;; or by mutating POST-BINDING-LEXENV if non-nil.
1288 ;;; Note that POST-BINDING-LEXENV is not actually a lexenv - it is only
1289 ;;; the contents of the 'vars' slot of a LEXENV, and moreover, as supplied
1290 ;;; to this function, it has a dummy cons in front which is later removed.
1291 ;;; The dummy cons serves a double purpose - as an indicator that this decl
1292 ;;; occurs in a LET/LET* form, and facilitating pass by reference of the list.
1293 (defun process-special-decl (spec res vars post-binding-lexenv context)
1294 (declare (list spec vars) (type lexenv res))
1295 (collect ((new-venv nil cons))
1296 (dolist (name (cdr spec))
1297 ;; While CLHS seems to allow local SPECIAL declarations for constants,
1298 ;; whatever the semantics are supposed to be is not at all clear to me
1299 ;; -- since constants aren't allowed to be bound it should be a no-op as
1300 ;; no-one can observe the difference portably, but specials are allowed
1301 ;; to be bound... yet nowhere does it say that the special declaration
1302 ;; removes the constantness. Call it a spec bug and prohibit it. Same
1303 ;; for GLOBAL variables.
1304 (unless (symbolp name)
1305 (compiler-error
1306 "~S is not a symbol and thus can't be declared special."
1307 name))
1308 (let ((kind (info :variable :kind name)))
1309 (unless (member kind '(:special :unknown))
1310 (compiler-error
1311 "Can't declare ~(~A~) variable locally special: ~S"
1312 kind name)))
1313 (program-assert-symbol-home-package-unlocked
1314 context name "declaring ~A special")
1315 (let ((var (find-in-bindings vars name)))
1316 (etypecase var
1317 (cons
1318 (aver (eq (car var) 'macro))
1319 (compiler-error
1320 "~S is a symbol-macro and thus can't be declared special."
1321 name))
1322 (lambda-var
1323 (when (lambda-var-ignorep var)
1324 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1325 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1326 (compiler-style-warn
1327 "The ignored variable ~S is being declared special."
1328 name))
1329 (setf (lambda-var-specvar var)
1330 (specvar-for-binding name)))
1331 (null
1332 (unless (or (assoc name (new-venv) :test #'eq))
1333 (new-venv (cons name (specvar-for-binding name))))))))
1334 (cond (post-binding-lexenv
1335 (setf (cdr post-binding-lexenv)
1336 (append (new-venv) (cdr post-binding-lexenv)))
1337 res)
1338 ((new-venv)
1339 (make-lexenv :default res :vars (new-venv)))
1341 res))))
1343 (defun process-no-constraints-decl (spec vars)
1344 (dolist (name (rest spec))
1345 (let ((var (find-in-bindings vars name)))
1346 (cond
1347 ((not var)
1348 (warn "No ~s variable" name))
1350 (setf (lambda-var-no-constraints var) t))))))
1352 (defun process-constant-decl (spec vars)
1353 (dolist (name (rest spec))
1354 (let ((var (find-in-bindings vars name)))
1355 (cond
1356 ((not var)
1357 (warn "No ~s variable" name))
1359 (setf (lambda-var-constant var) t))))))
1361 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP
1362 ;;; (and TYPE if notinline), plus type-restrictions from the lexenv.
1363 (defun make-new-inlinep (var inlinep local-type)
1364 (declare (type global-var var) (type inlinep inlinep))
1365 (let* ((type (if (and (eq inlinep 'notinline)
1366 (not (eq (leaf-where-from var) :declared)))
1367 (specifier-type 'function)
1368 (leaf-type var)))
1369 (res (make-defined-fun
1370 :%source-name (leaf-source-name var)
1371 :where-from (leaf-where-from var)
1372 :type (if local-type
1373 (type-intersection local-type type)
1374 type)
1375 :inlinep inlinep)))
1376 (when (defined-fun-p var)
1377 (setf (defined-fun-inline-expansion res)
1378 (defined-fun-inline-expansion var))
1379 (setf (defined-fun-functional res)
1380 (defined-fun-functional var))
1381 (setf (defined-fun-same-block-p res)
1382 (defined-fun-same-block-p var)))
1383 ;; FIXME: Is this really right? Needs we not set the FUNCTIONAL
1384 ;; to the original global-var?
1385 res))
1387 ;;; Parse an inline/notinline declaration. If it's a local function we're
1388 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1389 (defun process-inline-decl (spec res fvars)
1390 (let ((sense (first spec))
1391 (new-fenv ()))
1392 (dolist (name (rest spec))
1393 (let ((fvar (find name fvars
1394 :key (lambda (x)
1395 (unless (consp x) ;; macrolet
1396 (leaf-source-name x)))
1397 :test #'equal)))
1398 (if fvar
1399 (setf (functional-inlinep fvar) sense)
1400 (let ((found (find-lexically-apparent-fun
1401 name "in an inline or notinline declaration")))
1402 (etypecase found
1403 (functional
1404 (when (policy *lexenv* (>= speed inhibit-warnings))
1405 (compiler-notify "ignoring ~A declaration not at ~
1406 definition of local function:~% ~S"
1407 sense name)))
1408 (global-var
1409 (let ((type
1410 (cdr (assoc found (lexenv-type-restrictions res)))))
1411 (push (cons name (make-new-inlinep found sense type))
1412 new-fenv))))))))
1413 (if new-fenv
1414 (make-lexenv :default res :funs new-fenv)
1415 res)))
1417 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1418 (defun find-in-bindings-or-fbindings (name vars fvars)
1419 (declare (list vars fvars))
1420 (typecase name
1421 (atom
1422 (find-in-bindings vars name))
1423 ((cons (eql function) (cons * null))
1424 (find (cadr name) fvars :key (lambda (x)
1425 (if (consp x) ;; MACROLET
1426 (car x)
1427 (leaf-source-name x))) :test #'equal))
1429 (compiler-error "Malformed function or variable name ~S." name))))
1431 ;;; Process an ignore/ignorable declaration, checking for various losing
1432 ;;; conditions.
1433 (defun process-ignore-decl (spec vars fvars lexenv)
1434 (declare (list spec vars fvars))
1435 (dolist (name (rest spec))
1436 (let ((var (find-in-bindings-or-fbindings name vars fvars)))
1437 (cond
1438 ((not var)
1439 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1440 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1441 ;; But, other Lisp hosts signal a full warning, so when building
1442 ;; the cross-compiler, compile it as #'WARN so that in a self-hosted
1443 ;; build we can at least crash in the same way,
1444 ;; until we resolve this question about how severe the warning is.
1445 (multiple-value-call #+sb-xc-host #'warn
1446 #-sb-xc-host #'compiler-style-warn
1447 "~A declaration for ~A: ~A"
1448 (first spec)
1449 (if (symbolp name)
1450 (values
1451 (case (info :variable :kind name)
1452 (:special "a special variable")
1453 (:global "a global lexical variable")
1454 (:alien "a global alien variable")
1455 (t (if (assoc name (lexenv-vars lexenv))
1456 "a variable from outer scope"
1457 "an unknown variable")))
1458 name)
1459 (values
1460 (cond ((assoc (second name) (lexenv-funs lexenv)
1461 :test #'equal)
1462 "a function from outer scope")
1463 ((info :function :kind (second name))
1464 "a global function")
1466 "an unknown function"))
1467 (second name)))))
1468 ((and (consp var)
1469 (or (eq (car var) 'macro)
1470 (and (consp (cdr var))
1471 (eq (cadr var) 'macro))))
1472 ;; Just ignore the IGNORE decl: we don't currently signal style-warnings
1473 ;; for unused macrolet or symbol-macros, so there's no need to do anything.
1475 ((functional-p var)
1476 (setf (leaf-ever-used var) t)
1477 (when (eq (first spec) 'ignore)
1478 (setf (functional-ignore var) t)))
1479 ((and (lambda-var-specvar var) (eq (first spec) 'ignore))
1480 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1481 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1482 (compiler-style-warn "Declaring special variable ~S to be ~A"
1483 name
1484 (first spec)))
1485 ((eq (first spec) 'ignorable)
1486 (setf (leaf-ever-used var) t))
1488 (setf (lambda-var-ignorep var) t)))))
1489 (values))
1491 (defvar *stack-allocate-dynamic-extent* t
1492 "If true (the default), the compiler believes DYNAMIC-EXTENT declarations
1493 and stack allocates otherwise inaccessible parts of the object whenever
1494 possible.")
1496 (defun process-dynamic-extent-decl (names vars fvars)
1497 (if *stack-allocate-dynamic-extent*
1498 (dolist (name names)
1499 (cond
1500 ((symbolp name)
1501 (let* ((bound-var (find-in-bindings vars name))
1502 (var (or bound-var
1503 (lexenv-find name vars)
1504 (find-free-var name))))
1505 (maybe-note-undefined-variable-reference var name)
1506 (etypecase var
1507 (leaf
1508 (cond
1509 ((and (typep var 'global-var) (eq (global-var-kind var) :unknown)))
1510 (bound-var
1511 (setf (leaf-dynamic-extent var) t))
1512 (t (compiler-notify "Ignoring free DYNAMIC-EXTENT declaration: ~S" name))))
1513 (cons
1514 (compiler-error "DYNAMIC-EXTENT on symbol-macro: ~S" name))
1515 (heap-alien-info
1516 (compiler-error "DYNAMIC-EXTENT on alien-variable: ~S" name)))))
1517 ((and (consp name)
1518 (eq (car name) 'function)
1519 (null (cddr name))
1520 (valid-function-name-p (cadr name)))
1521 (let* ((fname (cadr name))
1522 (bound-fun (find fname fvars
1523 :key (lambda (x)
1524 (unless (consp x) ;; macrolet
1525 (leaf-source-name x)))
1526 :test #'equal))
1527 (fun (or bound-fun (lexenv-find fname funs))))
1528 (etypecase fun
1529 (leaf
1530 (if bound-fun
1531 (setf (leaf-dynamic-extent bound-fun) t)
1532 (compiler-notify
1533 "Ignoring free DYNAMIC-EXTENT declaration: ~S" name)))
1534 (cons
1535 (compiler-error "DYNAMIC-EXTENT on macro: ~S" name))
1536 (null
1537 (compiler-style-warn
1538 "Unbound function declared DYNAMIC-EXTENT: ~S" name)))))
1540 (compiler-error "DYNAMIC-EXTENT on a weird thing: ~S" name))))
1541 (when (policy *lexenv* (= speed 3))
1542 (compiler-notify "Ignoring DYNAMIC-EXTENT declarations: ~S" names))))
1544 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1545 ;;; go away, I think.
1546 ;;; Or just rename the declaration to SB-C:RESULT-TYPE so that it's not
1547 ;;; a symbol in the CL package, and then eliminate this switch.
1548 ;;; It's permissible to have implementation-specific declarations.
1549 (defvar *suppress-values-declaration* nil
1550 "If true, processing of the VALUES declaration is inhibited.")
1552 ;;; Process a single declaration spec, augmenting the specified LEXENV
1553 ;;; RES. Return RES and result type. VARS and FVARS are as described
1554 ;;; PROCESS-DECLS.
1555 (defun process-1-decl (raw-spec res vars fvars post-binding-lexenv context)
1556 (declare (type list raw-spec vars fvars))
1557 (declare (type lexenv res))
1558 (let ((spec (canonized-decl-spec raw-spec))
1559 (optimize-qualities))
1560 ;; FIXME: we can end up with a chain of spurious parent lexenvs,
1561 ;; when logically the processing of decls should yield at most
1562 ;; two new lexenvs: one for the bindings and one for post-binding.
1563 ;; It's possible that there's a simple fix of re-linking the resulting
1564 ;; lexenv directly to *lexenv* as its parent.
1565 (values
1566 (case (first spec)
1567 (type
1568 (process-type-decl (cdr spec) res vars context))
1569 ((ignore ignorable)
1570 (process-ignore-decl spec vars fvars res)
1571 res)
1572 (special
1573 (process-special-decl spec res vars post-binding-lexenv context))
1574 (ftype
1575 (unless (cdr spec)
1576 (compiler-error "no type specified in FTYPE declaration: ~S" spec))
1577 (process-ftype-decl (second spec) res (cddr spec) fvars context))
1578 ((inline notinline maybe-inline)
1579 (process-inline-decl spec res fvars))
1580 (no-compiler-macro
1581 (make-lexenv :default res
1582 :user-data (list*
1583 (cons 'no-compiler-macro (second spec))
1584 (lexenv-user-data res))))
1585 (optimize
1586 (multiple-value-bind (new-policy specified-qualities)
1587 (process-optimize-decl spec (lexenv-policy res))
1588 (setq optimize-qualities specified-qualities)
1589 (make-lexenv :default res :policy new-policy)))
1590 (muffle-conditions
1591 (make-lexenv
1592 :default res
1593 :handled-conditions (process-muffle-conditions-decl
1594 spec (lexenv-handled-conditions res))))
1595 (unmuffle-conditions
1596 (make-lexenv
1597 :default res
1598 :handled-conditions (process-unmuffle-conditions-decl
1599 spec (lexenv-handled-conditions res))))
1600 ((dynamic-extent)
1601 (process-dynamic-extent-decl (cdr spec) vars fvars)
1602 res)
1603 ((disable-package-locks enable-package-locks)
1604 (make-lexenv
1605 :default res
1606 :disabled-package-locks (process-package-lock-decl
1607 spec (lexenv-disabled-package-locks res))))
1608 (flushable
1609 (make-lexenv
1610 :default res
1611 :flushable (cdr spec)))
1612 (current-defmethod
1613 (destructuring-bind (name qualifiers specializers lambda-list)
1614 (cdr spec)
1615 (let* ((gfs (or *methods-in-compilation-unit*
1616 (setf *methods-in-compilation-unit*
1617 (make-hash-table :test #'equal))))
1618 (methods (or (gethash name gfs)
1619 (setf (gethash name gfs)
1620 (make-hash-table :test #'equal)))))
1621 (setf (gethash (cons qualifiers specializers) methods)
1622 lambda-list)))
1623 res)
1624 (no-constraints
1625 (process-no-constraints-decl spec vars)
1626 res)
1627 (constant-value
1628 (process-constant-decl spec vars)
1629 res)
1630 ;; We may want to detect LAMBDA-LIST and VALUES decls here,
1631 ;; and report them as "Misplaced" rather than "Unrecognized".
1633 (let ((info (info :declaration :known (first spec))))
1634 (unless info
1635 (compiler-warn "unrecognized declaration ~S" raw-spec))
1636 (if (functionp info)
1637 (funcall info res spec vars fvars)
1638 res))))
1639 optimize-qualities)))
1641 (defun process-muffle-decls (decls lexenv)
1642 (let (source-form)
1643 (flet ((process-it (spec)
1644 (cond ((atom spec))
1645 ((member (car spec) '(muffle-conditions unmuffle-conditions))
1646 (setq lexenv
1647 (process-1-decl spec lexenv nil nil nil nil)))
1648 ((eq (car spec) 'source-form)
1649 (setf source-form (cadr spec))))))
1650 (dolist (decl decls)
1651 (dolist (spec (rest decl))
1652 (process-it spec))))
1653 (values lexenv source-form)))
1655 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1656 ;;; and FUNCTIONAL structures which are being bound. In addition to
1657 ;;; filling in slots in the leaf structures, we return a new LEXENV,
1658 ;;; which reflects pervasive special and function type declarations,
1659 ;;; (NOT)INLINE declarations and OPTIMIZE declarations, and type of
1660 ;;; VALUES declarations. If BINDING-FORM-P is true, the third return
1661 ;;; value is a list of VARs that should not apply to the lexenv of the
1662 ;;; initialization forms for the bindings, but should apply to the body.
1664 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1665 ;;; of LOCALLY.
1666 (defun process-decls (decls vars fvars &key
1667 (lexenv *lexenv*) (binding-form-p nil) (context :compile)
1668 (allow-lambda-list nil))
1669 (declare (list decls vars fvars))
1670 (let ((result-type *wild-type*)
1671 (allow-values-decl allow-lambda-list)
1672 (explicit-check)
1673 (allow-explicit-check allow-lambda-list)
1674 (lambda-list (if allow-lambda-list :unspecified nil))
1675 (optimize-qualities)
1676 (local-optimize)
1677 source-form
1678 (post-binding-lexenv (if binding-form-p (list nil)))) ; dummy cell
1679 (flet ((process-it (spec decl)
1680 (cond ((atom spec)
1681 (compiler-warn "malformed declaration specifier ~S in ~S"
1682 spec decl))
1683 ((and (eq allow-lambda-list t)
1684 (typep spec '(cons (eql lambda-list) (cons t null))))
1685 (setq lambda-list (cadr spec) allow-lambda-list nil))
1686 ((and allow-values-decl
1687 (typep spec '(cons (eql values)))
1688 (not *suppress-values-declaration*))
1689 ;; Why do we allow more than one VALUES decl? I don't know.
1690 (setq result-type
1691 (values-type-intersection
1692 result-type
1693 (or (compiler-values-specifier-type
1694 (let ((types (cdr spec)))
1695 (if (singleton-p types)
1696 (car types)
1697 `(values ,@types))))
1698 (return-from process-it)))))
1699 ((and allow-explicit-check
1700 (typep spec '(cons (eql explicit-check))))
1701 ;; EXPLICIT-CHECK by itself specifies that all argument and
1702 ;; result types are checked by the function body.
1703 ;; Alternatively, a subset of arguments, and/or :RESULT,
1704 ;; can be specified to indicate that only a subset are
1705 ;; checked; in that case, the compiler asserts all others.
1706 (awhen (remove-if (lambda (x)
1707 (or (member x vars
1708 :test #'eq
1709 :key #'lambda-var-%source-name)
1710 (eq x :result)))
1711 (cdr spec))
1712 (compiler-error "explicit-check list ~S must refer to lambda vars"
1713 it))
1714 (setq explicit-check (or (cdr spec) t)
1715 allow-explicit-check nil)) ; at most one of this decl
1716 ((equal spec '(top-level-form))) ; ignore
1717 ((typep spec '(cons (eql source-form)))
1718 (setf source-form (cadr spec)))
1719 ;; Used only for the current function.
1720 ;; E.g. suppressing argument checking without doing
1721 ;; so in all the subforms.
1722 ((typep spec '(cons (eql local-optimize)))
1723 (setf local-optimize spec))
1725 (multiple-value-bind (new-env new-qualities)
1726 (process-1-decl spec lexenv vars fvars
1727 post-binding-lexenv context)
1728 (setq lexenv new-env
1729 optimize-qualities
1730 (nconc new-qualities optimize-qualities)))))))
1731 (dolist (decl decls)
1732 (dolist (spec (rest decl))
1733 (if (eq context :compile)
1734 (with-current-source-form (spec decl) ; TODO this is a slight change to the previous code. make sure the behavior is identical
1735 (process-it spec decl))
1736 ;; Kludge: EVAL calls this function to deal with LOCALLY.
1737 (process-it spec decl)))))
1738 (warn-repeated-optimize-qualities (lexenv-policy lexenv) optimize-qualities)
1740 (values lexenv result-type (cdr post-binding-lexenv)
1741 lambda-list explicit-check source-form
1742 (when local-optimize
1743 (process-optimize-decl local-optimize (lexenv-policy lexenv))))))
1745 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1746 ;;; declaration. If there is a global variable of that name, then
1747 ;;; check that it isn't a constant and return it. Otherwise, create an
1748 ;;; anonymous GLOBAL-VAR.
1749 (defun specvar-for-binding (name)
1750 (cond ((not (eq (info :variable :where-from name) :assumed))
1751 (let ((found (find-free-var name)))
1752 (when (heap-alien-info-p found)
1753 (compiler-error
1754 "~S is an alien variable and so can't be declared special."
1755 name))
1756 (unless (global-var-p found)
1757 (compiler-error
1758 "~S is a constant and so can't be declared special."
1759 name))
1760 found))
1762 (make-global-var :kind :special
1763 :%source-name name
1764 :where-from :declared))))
1766 (declaim (end-block))
1769 ;;;; Proclamations:
1771 ;;; PROCLAIM changes the global environment, so we must handle its
1772 ;;; compile time side effects if we are to keep the information in the
1773 ;;; (FREE-xxx *IR1-NAMESPACE*) tables up to date. When there is a var
1774 ;;; structure we disown it by replacing it with an updated copy. Uses
1775 ;;; of the variable which were translated before the PROCLAIM will get
1776 ;;; the old version, while subsequent references will get the updated
1777 ;;; information.
1779 ;;; If a special block compilation delimiter, then start or end the
1780 ;;; block as appropriate. If :BLOCK-COMPILE is T or NIL, then we
1781 ;;; ignore any start/end block declarations.
1782 (defun process-block-compile-proclamation (kind entry-points)
1783 (if (eq *block-compile-argument* :specified)
1784 (ecase kind
1785 (start-block
1786 (finish-block-compilation)
1787 (let ((compilation *compilation*))
1788 (setf (block-compile compilation) t)
1789 (setf (entry-points compilation) entry-points)))
1790 (end-block
1791 (finish-block-compilation)))
1792 (compiler-notify "ignoring ~S declaration since ~
1793 :BLOCK-COMPILE is not :SPECIFIED"
1794 kind)))
1796 ;;; Update function type info cached in (FREE-FUNS *IR1-NAMESPACE*).
1797 ;;; If:
1798 ;;; -- there is a GLOBAL-VAR, then just update the type and remove the
1799 ;;; name from the list of undefined functions. Someday we should
1800 ;;; check for incompatible redeclaration.
1801 ;;; -- there is a FUNCTIONAL, then apply the type assertion to that
1802 ;;; function. This will only happen during block compilation.
1803 (defun process-1-ftype-proclamation (name type)
1804 (proclaim-as-fun-name name)
1805 (let* ((free-funs (free-funs *ir1-namespace*))
1806 (var (gethash name free-funs)))
1807 (etypecase var
1808 (null)
1809 (global-var
1810 (setf (gethash name free-funs)
1811 (let ((kind (global-var-kind var)))
1812 (if (defined-fun-p var)
1813 (make-defined-fun
1814 :%source-name name :type type :where-from :declared :kind kind
1815 :inlinep (defined-fun-inlinep var)
1816 :inline-expansion (defined-fun-inline-expansion var)
1817 :same-block-p (defined-fun-same-block-p var)
1818 :functional (defined-fun-functional var))
1819 (make-global-var :%source-name name :type type
1820 :where-from :declared :kind kind))))
1821 (when (defined-fun-p var)
1822 (let ((fun (defined-fun-functional var)))
1823 (when fun
1824 (assert-definition-type fun type
1825 :unwinnage-fun #'compiler-notify
1826 :where "this declaration"))))))))
1828 (defun process-ftype-proclamation (spec names)
1829 (declare (list names))
1830 (let ((type (specifier-type spec)))
1831 (unless (csubtypep type (specifier-type 'function))
1832 (error "Not a function type: ~/sb-impl:print-type/" spec))
1833 (dolist (name names)
1834 (process-1-ftype-proclamation name type))))
1836 ;;; Replace each old var entry with one having the new type.
1837 (defun process-type-proclamation (spec names)
1838 (declare (list names))
1839 (let ((free-vars (free-vars *ir1-namespace*))
1840 (type (specifier-type spec)))
1841 (dolist (name names)
1842 (let ((var (gethash name free-vars)))
1843 (etypecase var
1844 (null)
1845 ;; Constants cannot be redefined, and we already give the
1846 ;; constant object the tightest type possible. We will error
1847 ;; if the type is incompatible.
1848 (constant)
1849 (global-var
1850 (setf (gethash name free-vars)
1851 (make-global-var :%source-name name
1852 :type type :where-from :declared
1853 :kind (global-var-kind var)))))))))
1855 ;;; Similar in effect to FTYPE, but change the :INLINEP. Copying the
1856 ;;; global-var ensures that when we substitute a functional for a
1857 ;;; global var (i.e. for DEFUN) that we won't clobber any uses
1858 ;;; declared :NOTINLINE.
1859 (defun process-inline-proclamation (kind funs)
1860 (declare (type (and inlinep (not null)) kind))
1861 (dolist (name funs)
1862 (proclaim-as-fun-name name)
1863 (let* ((free-funs (free-funs *ir1-namespace*))
1864 (var (gethash name free-funs)))
1865 (etypecase var
1866 (null)
1867 (global-var
1868 (setf (gethash name free-funs)
1869 ;; Use the universal type as the local type restriction,
1870 ;; since we are processing the proclamation at the top
1871 ;; level and hence in a null lexical environment.
1872 (make-new-inlinep var kind *universal-type*)))))))
1874 ;;; Handle the compile-time side effects of PROCLAIM that don't happen
1875 ;;; at load time. These mostly side-effect the global state of the
1876 ;;; compiler, rather than the global environment.
1877 (defun %compiler-proclaim (kind args)
1878 (case kind
1879 ((special global)
1880 (dolist (name args)
1881 (let* ((free-vars (free-vars *ir1-namespace*))
1882 (old (gethash name free-vars)))
1883 (when old
1884 (ecase (global-var-kind old)
1885 ((:special :global))
1886 (:unknown
1887 (setf (gethash name free-vars)
1888 (make-global-var :%source-name name :type (leaf-type old)
1889 :where-from (leaf-where-from old)
1890 :kind (ecase kind
1891 (special :special)
1892 (global :global))))))))))
1893 ((start-block end-block)
1894 #-(and sb-devel sb-xc-host)
1895 (process-block-compile-proclamation kind args))
1896 (ftype
1897 (destructuring-bind (spec &rest args) args
1898 (process-ftype-proclamation spec args)))
1899 (type
1900 (destructuring-bind (spec &rest args) args
1901 (process-type-proclamation spec args)))
1902 ((inline notinline maybe-inline)
1903 (process-inline-proclamation kind args))))