Remove "-HEADER-" from SYMBOL and VALUE-CELL widetag names
[sbcl.git] / src / compiler / ir1tran.lisp
blob00a53ddfba804fec0ffae35c358afe161958e5b6
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 ;;; *CURRENT-FORM-NUMBER* is used in FIND-SOURCE-PATHS to compute the
18 ;;; form number to associate with a source path. This should be bound
19 ;;; to an initial value of 0 before the processing of each truly
20 ;;; top level form.
21 (declaim (type index *current-form-number*))
22 (defvar *current-form-number*)
24 ;;; *SOURCE-PATHS* is a hashtable from source code forms to the path
25 ;;; taken through the source to reach the form. This provides a way to
26 ;;; keep track of the location of original source forms, even when
27 ;;; macroexpansions and other arbitary permutations of the code
28 ;;; happen. This table is initialized by calling FIND-SOURCE-PATHS on
29 ;;; the original source.
30 ;;;
31 ;;; It is fairly useless to store symbols, characters, or fixnums in
32 ;;; this table, as 42 is EQ to 42 no matter where in the source it
33 ;;; appears. GET-SOURCE-PATH and NOTE-SOURCE-PATH functions should be
34 ;;; always used to access this table.
35 (declaim (hash-table *source-paths*))
36 (defvar *source-paths*)
38 (declaim (inline source-form-has-path-p))
39 (defun source-form-has-path-p (form)
40 (not (typep form '(or symbol fixnum character))))
42 (defun get-source-path (form)
43 (when (source-form-has-path-p form)
44 (gethash form *source-paths*)))
46 (defun ensure-source-path (form)
47 (or (get-source-path form)
48 (cons (simplify-source-path-form form)
49 *current-path*)))
51 (defun simplify-source-path-form (form)
52 (if (consp form)
53 (let ((op (car form)))
54 ;; In the compiler functions can be directly represented
55 ;; by leaves. Having leaves in the source path is pretty
56 ;; hard on the poor user, however, so replace with the
57 ;; source-name when possible.
58 (if (and (leaf-p op) (leaf-has-source-name-p op))
59 (cons (leaf-source-name op) (cdr form))
60 form))
61 form))
63 (defun note-source-path (form &rest arguments)
64 (when (source-form-has-path-p form)
65 (setf (gethash form *source-paths*)
66 (apply #'list* 'original-source-start *current-form-number* arguments))))
68 ;;; *CURRENT-COMPONENT* is the COMPONENT structure which we link
69 ;;; blocks into as we generate them. This just serves to glue the
70 ;;; emitted blocks together until local call analysis and flow graph
71 ;;; canonicalization figure out what is really going on. We need to
72 ;;; keep track of all the blocks generated so that we can delete them
73 ;;; if they turn out to be unreachable.
74 ;;;
75 ;;; FIXME: It's confusing having one variable named *CURRENT-COMPONENT*
76 ;;; and another named *COMPONENT-BEING-COMPILED*. (In CMU CL they
77 ;;; were called *CURRENT-COMPONENT* and *COMPILE-COMPONENT* respectively,
78 ;;; which was also confusing.)
79 (declaim (type (or component null) *current-component*))
80 (defvar *current-component*)
82 ;;; *CURRENT-PATH* is the source path of the form we are currently
83 ;;; translating. See NODE-SOURCE-PATH in the NODE structure.
84 (declaim (list *current-path*))
85 (defvar *current-path*)
87 (defun call-with-current-source-form (thunk &rest forms)
88 (let ((*current-path* (when (and (some #'identity forms)
89 (boundp '*source-paths*))
90 (or (some #'get-source-path forms)
91 (when (boundp '*current-path*)
92 *current-path*)))))
93 (funcall thunk)))
95 (defvar *derive-function-types* nil
96 "Should the compiler assume that function types will never change,
97 so that it can use type information inferred from current definitions
98 to optimize code which uses those definitions? Setting this true
99 gives non-ANSI, early-CMU-CL behavior. It can be useful for improving
100 the efficiency of stable code.")
102 (defvar *fun-names-in-this-file* nil)
104 (defvar *post-binding-variable-lexenv* nil)
106 ;;;; namespace management utilities
108 ;; As with LEXENV-FIND, we assume use of *LEXENV*, but macroexpanders
109 ;; receive an explicit environment and should pass it.
110 ;; A declaration will trump a proclamation.
111 (defun fun-lexically-notinline-p (name &optional (env *lexenv*))
112 (let ((answer
113 (typecase env
114 (null nil)
115 #!+(and sb-fasteval (host-feature sb-xc))
116 (sb!interpreter:basic-env
117 (sb!interpreter::fun-lexically-notinline-p name env))
119 (let ((fun (cdr (assoc name (lexenv-funs env) :test #'equal))))
120 ;; FIXME: this seems to omit FUNCTIONAL
121 (when (defined-fun-p fun)
122 (return-from fun-lexically-notinline-p
123 (eq (defined-fun-inlinep fun) :notinline))))))))
124 ;; If ANSWER is NIL, go for the global value
125 (eq (or answer (info :function :inlinep name))
126 :notinline)))
128 (defun maybe-defined-here (name where)
129 (if (and (eq :defined where)
130 (member name *fun-names-in-this-file* :test #'equal))
131 :defined-here
132 where))
134 ;;; Return a GLOBAL-VAR structure usable for referencing the global
135 ;;; function NAME.
136 (defun find-global-fun (name latep)
137 (unless (info :function :kind name)
138 (setf (info :function :kind name) :function)
139 (setf (info :function :where-from name) :assumed))
140 (let ((where (info :function :where-from name)))
141 (when (and (eq where :assumed)
142 ;; Slot accessors are defined just-in-time, if not already.
143 (not (typep name '(cons (eql sb!pcl::slot-accessor))))
144 ;; In the ordinary target Lisp, it's silly to report
145 ;; undefinedness when the function is defined in the
146 ;; running Lisp. But at cross-compile time, the current
147 ;; definedness of a function is irrelevant to the
148 ;; definedness at runtime, which is what matters.
149 #-sb-xc-host (not (fboundp name))
150 ;; LATEP is true when the user has indicated that
151 ;; late-late binding is desired by using eg. a quoted
152 ;; symbol -- in which case it makes little sense to
153 ;; complain about undefined functions.
154 (not latep))
155 (note-undefined-reference name :function))
156 (let ((ftype (proclaimed-ftype name))
157 (notinline (fun-lexically-notinline-p name)))
158 (make-global-var
159 :kind :global-function
160 :%source-name name
161 :type (if (or (eq where :declared)
162 (and (not latep)
163 (not notinline)
164 *derive-function-types*))
165 ftype
166 (specifier-type 'function))
167 :defined-type (if (and (not latep) (not notinline))
168 ftype
169 (specifier-type 'function))
170 :where-from (if notinline
171 where
172 (maybe-defined-here name where))))))
174 ;;; Have some DEFINED-FUN-FUNCTIONALS of a *FREE-FUNS* entry become invalid?
175 ;;; Drop 'em.
177 ;;; This was added to fix bug 138 in SBCL. It is possible for a *FREE-FUNS*
178 ;;; entry to contain a DEFINED-FUN whose DEFINED-FUN-FUNCTIONAL object
179 ;;; contained IR1 stuff (NODEs, BLOCKs...) referring to an already compiled
180 ;;; (aka "dead") component. When this IR1 stuff was reused in a new component,
181 ;;; under further obscure circumstances it could be used by
182 ;;; WITH-IR1-ENVIRONMENT-FROM-NODE to generate a binding for
183 ;;; *CURRENT-COMPONENT*. At that point things got all confused, since IR1
184 ;;; conversion was sending code to a component which had already been compiled
185 ;;; and would never be compiled again.
187 ;;; Note: as of 1.0.24.41 this seems to happen only in XC, and the original
188 ;;; BUGS entry also makes it seem like this might not be an issue at all on
189 ;;; target.
190 (defun clear-invalid-functionals (free-fun)
191 ;; There might be other reasons that *FREE-FUN* entries could
192 ;; become invalid, but the only one we've been bitten by so far
193 ;; (sbcl-0.pre7.118) is this one:
194 (when (defined-fun-p free-fun)
195 (setf (defined-fun-functionals free-fun)
196 (delete-if (lambda (functional)
197 (or (eq (functional-kind functional) :deleted)
198 (when (lambda-p functional)
200 ;; (The main reason for this first test is to bail
201 ;; out early in cases where the LAMBDA-COMPONENT
202 ;; call in the second test would fail because links
203 ;; it needs are uninitialized or invalid.)
205 ;; If the BIND node for this LAMBDA is null, then
206 ;; according to the slot comments, the LAMBDA has
207 ;; been deleted or its call has been deleted. In
208 ;; that case, it seems rather questionable to reuse
209 ;; it, and certainly it shouldn't be necessary to
210 ;; reuse it, so we cheerfully declare it invalid.
211 (not (lambda-bind functional))
212 ;; If this IR1 stuff belongs to a dead component,
213 ;; then we can't reuse it without getting into
214 ;; bizarre confusion.
215 (eq (component-info (lambda-component functional))
216 :dead)))))
217 (defined-fun-functionals free-fun)))
218 nil))
220 ;;; If NAME already has a valid entry in *FREE-FUNS*, then return
221 ;;; the value. Otherwise, make a new GLOBAL-VAR using information from
222 ;;; the global environment and enter it in *FREE-FUNS*. If NAME
223 ;;; names a macro or special form, then we error out using the
224 ;;; supplied context which indicates what we were trying to do that
225 ;;; demanded a function.
226 (declaim (ftype (sfunction (t string) global-var) find-free-fun))
227 (defun find-free-fun (name context)
228 (or (let ((old-free-fun (gethash name *free-funs*)))
229 (when old-free-fun
230 (clear-invalid-functionals old-free-fun)
231 old-free-fun))
232 (let ((kind (info :function :kind name)))
233 (ecase kind
234 ((:macro :special-form)
235 (compiler-error "The ~(~S~) name ~S was found ~A."
236 kind name context))
237 ((:function nil)
238 (check-fun-name name)
239 (let ((expansion (fun-name-inline-expansion name))
240 (inlinep (info :function :inlinep name)))
241 (setf (gethash name *free-funs*)
242 (if (or expansion inlinep)
243 (let ((where (info :function :where-from name)))
244 (make-defined-fun
245 :%source-name name
246 :inline-expansion expansion
247 :inlinep inlinep
248 :where-from (if (eq inlinep :notinline)
249 where
250 (maybe-defined-here name where))
251 :type (if (and (eq inlinep :notinline)
252 (neq where :declared))
253 (specifier-type 'function)
254 (proclaimed-ftype name))))
255 (find-global-fun name nil)))))))))
257 ;;; Return the LEAF structure for the lexically apparent function
258 ;;; definition of NAME.
259 (declaim (ftype (sfunction (t string) leaf) find-lexically-apparent-fun))
260 (defun find-lexically-apparent-fun (name context)
261 (let ((var (lexenv-find name funs :test #'equal)))
262 (cond (var
263 (unless (leaf-p var)
264 (aver (and (consp var) (eq (car var) 'macro)))
265 (compiler-error "found macro name ~S ~A" name context))
266 var)
268 (find-free-fun name context)))))
270 (defun maybe-find-free-var (name)
271 (gethash name *free-vars*))
273 ;;; Return the LEAF node for a global variable reference to NAME. If
274 ;;; NAME is already entered in *FREE-VARS*, then we just return the
275 ;;; corresponding value. Otherwise, we make a new leaf using
276 ;;; information from the global environment and enter it in
277 ;;; *FREE-VARS*. If the variable is unknown, then we emit a warning.
278 (declaim (ftype (sfunction (t) (or leaf cons heap-alien-info)) find-free-var))
279 (defun find-free-var (name)
280 (unless (symbolp name)
281 (compiler-error "Variable name is not a symbol: ~S." name))
282 (or (gethash name *free-vars*)
283 (let ((kind (info :variable :kind name))
284 (type (info :variable :type name))
285 (where-from (info :variable :where-from name))
286 (deprecation-state (deprecated-thing-p 'variable name)))
287 (when (and (eq kind :unknown) (not deprecation-state))
288 (note-undefined-reference name :variable))
289 (setf (gethash name *free-vars*)
290 (case kind
291 (:alien
292 (info :variable :alien-info name))
293 ;; FIXME: The return value in this case should really be
294 ;; of type SB!C::LEAF. I don't feel too badly about it,
295 ;; because the MACRO idiom is scattered throughout this
296 ;; file, but it should be cleaned up so we're not
297 ;; throwing random conses around. --njf 2002-03-23
298 (:macro
299 (let ((expansion (info :variable :macro-expansion name))
300 (type (type-specifier (info :variable :type name))))
301 `(macro . (the ,type ,expansion))))
302 (:constant
303 (let ((value (symbol-value name)))
304 ;; Override the values of standard symbols in XC,
305 ;; since we can't redefine them.
306 #+sb-xc-host
307 (when (eql (find-symbol (symbol-name name) :cl) name)
308 (multiple-value-bind (xc-value foundp)
309 (xc-constant-value name)
310 (cond (foundp
311 (setf value xc-value))
312 ((not (eq value name))
313 (compiler-warn
314 "Using cross-compilation host's definition of ~S: ~A~%"
315 name (symbol-value name))))))
316 (find-constant value name)))
318 (make-global-var :kind kind
319 :%source-name name
320 :type type
321 :where-from where-from)))))))
323 ;;; Grovel over CONSTANT checking for any sub-parts that need to be
324 ;;; processed with MAKE-LOAD-FORM. We have to be careful, because
325 ;;; CONSTANT might be circular. We also check that the constant (and
326 ;;; any subparts) are dumpable at all.
327 (defun maybe-emit-make-load-forms (constant &optional (name nil namep))
328 (let ((xset (alloc-xset)))
329 (labels ((trivialp (value)
330 (typep value
331 '(or
332 #-sb-xc-host
333 (or unboxed-array #!+sb-simd-pack simd-pack)
334 #+sb-xc-host
335 (and array (not (array t)))
336 symbol
337 number
338 character
339 string))) ; subsumed by UNBOXED-ARRAY
340 (grovel (value)
341 ;; Unless VALUE is an object which which obviously
342 ;; can't contain other objects
343 (unless (trivialp value)
344 (if (xset-member-p value xset)
345 (return-from grovel nil)
346 (add-to-xset value xset))
347 (typecase value
348 (cons
349 (grovel (car value))
350 (grovel (cdr value)))
351 (simple-vector
352 (dotimes (i (length value))
353 (grovel (svref value i))))
354 ((vector t)
355 (dotimes (i (length value))
356 (grovel (aref value i))))
357 ((simple-array t)
358 ;; Even though the (ARRAY T) branch does the exact
359 ;; same thing as this branch we do this separately
360 ;; so that the compiler can use faster versions of
361 ;; array-total-size and row-major-aref.
362 (dotimes (i (array-total-size value))
363 (grovel (row-major-aref value i))))
364 ((array t)
365 (dotimes (i (array-total-size value))
366 (grovel (row-major-aref value i))))
367 (#+sb-xc-host structure!object
368 #-sb-xc-host instance
369 ;; In the target SBCL, we can dump any instance, but
370 ;; in the cross-compilation host, %INSTANCE-FOO
371 ;; functions don't work on general instances, only on
372 ;; STRUCTURE!OBJECTs.
374 ;; Behold the wonderfully clear sense of this-
375 ;; WHEN (EMIT-MAKE-LOAD-FORM VALUE)
376 ;; meaning "when you're _NOT_ using a custom load-form"
378 ;; FIXME: What about funcallable instances with
379 ;; user-defined MAKE-LOAD-FORM methods?
380 (when (emit-make-load-form value)
381 #+sb-xc-host
382 (aver (eql (layout-bitmap (%instance-layout value))
383 sb!kernel::+layout-all-tagged+))
384 (do-instance-tagged-slot (i value)
385 (grovel (%instance-ref value i)))))
387 (compiler-error
388 "Objects of type ~/sb!impl:print-type-specifier/ ~
389 can't be dumped into fasl files."
390 (type-of value)))))))
391 ;; Dump all non-trivial named constants using the name.
392 (if (and namep (not (typep constant '(or symbol character
393 ;; FIXME: Cold init breaks if we
394 ;; try to reference FP constants
395 ;; thru their names.
396 #+sb-xc-host number
397 #-sb-xc-host fixnum))))
398 (emit-make-load-form constant name)
399 (grovel constant))))
400 (values))
402 ;;;; some flow-graph hacking utilities
404 ;;; This function sets up the back link between the node and the
405 ;;; ctran which continues at it.
406 (defun link-node-to-previous-ctran (node ctran)
407 (declare (type node node) (type ctran ctran))
408 (aver (not (ctran-next ctran)))
409 (setf (ctran-next ctran) node)
410 (setf (node-prev node) ctran))
412 ;;; This function is used to set the ctran for a node, and thus
413 ;;; determine what is evaluated next. If the ctran has no block, then
414 ;;; we make it be in the block that the node is in. If the ctran heads
415 ;;; its block, we end our block and link it to that block.
416 #!-sb-fluid (declaim (inline use-ctran))
417 (defun use-ctran (node ctran)
418 (declare (type node node) (type ctran ctran))
419 (if (eq (ctran-kind ctran) :unused)
420 (let ((node-block (ctran-block (node-prev node))))
421 (setf (ctran-block ctran) node-block)
422 (setf (ctran-kind ctran) :inside-block)
423 (setf (ctran-use ctran) node)
424 (setf (node-next node) ctran))
425 (%use-ctran node ctran)))
426 (defun %use-ctran (node ctran)
427 (declare (type node node) (type ctran ctran) (inline member))
428 (let ((block (ctran-block ctran))
429 (node-block (ctran-block (node-prev node))))
430 (aver (eq (ctran-kind ctran) :block-start))
431 (when (block-last node-block)
432 (error "~S has already ended." node-block))
433 (setf (block-last node-block) node)
434 (when (block-succ node-block)
435 (error "~S already has successors." node-block))
436 (setf (block-succ node-block) (list block))
437 (when (memq node-block (block-pred block))
438 (error "~S is already a predecessor of ~S." node-block block))
439 (push node-block (block-pred block))))
441 ;;; Insert NEW before OLD in the flow-graph.
442 (defun insert-node-before (old new)
443 (let ((prev (node-prev old))
444 (temp (make-ctran)))
445 (ensure-block-start prev)
446 (setf (ctran-next prev) nil)
447 (link-node-to-previous-ctran new prev)
448 (use-ctran new temp)
449 (link-node-to-previous-ctran old temp))
450 (values))
452 ;;; This function is used to set the ctran for a node, and thus
453 ;;; determine what receives the value.
454 (defun use-lvar (node lvar)
455 (declare (type valued-node node) (type (or lvar null) lvar))
456 (aver (not (node-lvar node)))
457 (when lvar
458 (setf (node-lvar node) lvar)
459 (cond ((null (lvar-uses lvar))
460 (setf (lvar-uses lvar) node))
461 ((listp (lvar-uses lvar))
462 (aver (not (memq node (lvar-uses lvar))))
463 (push node (lvar-uses lvar)))
465 (aver (neq node (lvar-uses lvar)))
466 (setf (lvar-uses lvar) (list node (lvar-uses lvar)))))
467 (reoptimize-lvar lvar)))
469 #!-sb-fluid(declaim (inline use-continuation))
470 (defun use-continuation (node ctran lvar)
471 (use-ctran node ctran)
472 (use-lvar node lvar))
474 ;;;; exported functions
476 ;;; This function takes a form and the top level form number for that
477 ;;; form, and returns a lambda representing the translation of that
478 ;;; form in the current global environment. The returned lambda is a
479 ;;; top level lambda that can be called to cause evaluation of the
480 ;;; forms. This lambda is in the initial component. If FOR-VALUE is T,
481 ;;; then the value of the form is returned from the function,
482 ;;; otherwise NIL is returned.
484 ;;; This function may have arbitrary effects on the global environment
485 ;;; due to processing of EVAL-WHENs. All syntax error checking is
486 ;;; done, with erroneous forms being replaced by a proxy which signals
487 ;;; an error if it is evaluated. Warnings about possibly inconsistent
488 ;;; or illegal changes to the global environment will also be given.
490 ;;; We make the initial component and convert the form in a PROGN (and
491 ;;; an optional NIL tacked on the end.) We then return the lambda. We
492 ;;; bind all of our state variables here, rather than relying on the
493 ;;; global value (if any) so that IR1 conversion will be reentrant.
494 ;;; This is necessary for EVAL-WHEN processing, etc.
496 ;;; The hashtables used to hold global namespace info must be
497 ;;; reallocated elsewhere. Note also that *LEXENV* is not bound, so
498 ;;; that local macro definitions can be introduced by enclosing code.
499 (defun ir1-toplevel (form path for-value &optional (allow-instrumenting t))
500 (declare (list path))
501 (let* ((*current-path* path)
502 (component (make-empty-component))
503 (*current-component* component)
504 (*allow-instrumenting* allow-instrumenting))
505 (setf (component-name component) 'initial-component)
506 (setf (component-kind component) :initial)
507 (let* ((forms (if for-value `(,form) `(,form nil)))
508 (res (ir1-convert-lambda-body
509 forms ()
510 :debug-name (debug-name 'top-level-form #+sb-xc-host nil #-sb-xc-host form))))
511 (setf (functional-entry-fun res) res
512 (functional-arg-documentation res) ()
513 (functional-kind res) :toplevel)
514 res)))
516 ;;; This function is called on freshly read forms to record the
517 ;;; initial location of each form (and subform.) Form is the form to
518 ;;; find the paths in, and TLF-NUM is the top level form number of the
519 ;;; truly top level form.
521 ;;; This gets a bit interesting when the source code is circular. This
522 ;;; can (reasonably?) happen in the case of circular list constants.
523 (defun find-source-paths (form tlf-num)
524 (declare (type index tlf-num))
525 (let ((*current-form-number* 0))
526 (sub-find-source-paths form (list tlf-num)))
527 (values))
528 (defun sub-find-source-paths (form path)
529 (unless (get-source-path form)
530 (note-source-path form path)
531 (incf *current-form-number*)
532 (let ((pos 0)
533 (subform form)
534 (trail form))
535 (declare (fixnum pos))
536 (macrolet ((frob ()
537 `(progn
538 (when (atom subform) (return))
539 (let ((fm (car subform)))
540 (when (sb!int:comma-p fm)
541 (setf fm (sb!int:comma-expr fm)))
542 (cond ((consp fm)
543 ;; If it's a cons, recurse.
544 (sub-find-source-paths fm (cons pos path)))
545 ((eq 'quote fm)
546 ;; Don't look into quoted constants.
547 ;; KLUDGE: this can't actually know about constants.
548 ;; e.g. (let ((quote (error "foo")))) or
549 ;; (list quote (error "foo")) are not
550 ;; constants and yet are ignored.
551 (return))
552 ((not (zerop pos))
553 ;; Otherwise store the containing form. It's not
554 ;; perfect, but better than nothing.
555 (note-source-path subform pos path)))
556 (incf pos))
557 (setq subform (cdr subform))
558 (when (eq subform trail) (return)))))
559 (loop
560 (frob)
561 (frob)
562 (setq trail (cdr trail)))))))
564 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
566 (declaim (ftype (sfunction (ctran ctran (or lvar null) t)
567 (values))
568 ir1-convert))
569 (macrolet (;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws
570 ;; out of the body and converts a condition signalling form
571 ;; instead. The source form is converted to a string since it
572 ;; may contain arbitrary non-externalizable objects.
573 (ir1-error-bailout ((start next result form) &body body)
574 (with-unique-names (skip condition)
575 `(block ,skip
576 (let ((,condition (catch 'ir1-error-abort
577 (let ((*compiler-error-bailout*
578 (lambda (&optional e)
579 (throw 'ir1-error-abort e))))
580 ,@body
581 (return-from ,skip nil)))))
582 (ir1-convert ,start ,next ,result
583 (make-compiler-error-form ,condition
584 ,form)))))))
586 ;; Translate FORM into IR1. The code is inserted as the NEXT of the
587 ;; CTRAN START. RESULT is the LVAR which receives the value of the
588 ;; FORM to be translated. The translators call this function
589 ;; recursively to translate their subnodes.
591 ;; As a special hack to make life easier in the compiler, a LEAF
592 ;; IR1-converts into a reference to that LEAF structure. This allows
593 ;; the creation using backquote of forms that contain leaf
594 ;; references, without having to introduce dummy names into the
595 ;; namespace.
596 (defun ir1-convert (start next result form)
597 (let* ((*current-path* (ensure-source-path form))
598 (start (instrument-coverage start nil form)))
599 (ir1-error-bailout (start next result form)
600 (cond ((atom form)
601 (cond ((and (symbolp form) (not (keywordp form)))
602 (ir1-convert-var start next result form))
603 ((leaf-p form)
604 (reference-leaf start next result form))
606 (reference-constant start next result form))))
608 (ir1-convert-functoid start next result form)))))
609 (values))
611 ;; Generate a reference to a manifest constant, creating a new leaf
612 ;; if necessary.
613 (defun reference-constant (start next result value)
614 (declare (type ctran start next)
615 (type (or lvar null) result))
616 (ir1-error-bailout (start next result value)
617 (let* ((leaf (find-constant value))
618 (res (make-ref leaf)))
619 (push res (leaf-refs leaf))
620 (link-node-to-previous-ctran res start)
621 (use-continuation res next result)))
622 (values)))
624 ;;; Add FUNCTIONAL to the COMPONENT-REANALYZE-FUNCTIONALS, unless it's
625 ;;; some trivial type for which reanalysis is a trivial no-op, or
626 ;;; unless it doesn't belong in this component at all.
628 ;;; FUNCTIONAL is returned.
629 (defun maybe-reanalyze-functional (functional)
631 (aver (not (eql (functional-kind functional) :deleted))) ; bug 148
632 (aver-live-component *current-component*)
634 ;; When FUNCTIONAL is of a type for which reanalysis isn't a trivial
635 ;; no-op
636 (when (typep functional '(or optional-dispatch clambda))
638 ;; When FUNCTIONAL knows its component
639 (when (lambda-p functional)
640 (aver (eql (lambda-component functional) *current-component*)))
642 (pushnew functional
643 (component-reanalyze-functionals *current-component*)))
645 functional)
647 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
648 ;;; needed. If LEAF represents a defined function which has already
649 ;;; been converted, and is not :NOTINLINE, then reference the
650 ;;; functional instead.
651 (defun reference-leaf (start next result leaf &optional (name '.anonymous.))
652 (declare (type ctran start next) (type (or lvar null) result) (type leaf leaf))
653 (assure-leaf-live-p leaf)
654 (let* ((type (lexenv-find leaf type-restrictions))
655 (leaf (or (and (defined-fun-p leaf)
656 (not (eq (defined-fun-inlinep leaf)
657 :notinline))
658 (let ((functional (defined-fun-functional leaf)))
659 (when (and functional (not (functional-kind functional)))
660 (maybe-reanalyze-functional functional))))
661 (when (and (lambda-p leaf)
662 (memq (functional-kind leaf)
663 '(nil :optional)))
664 (maybe-reanalyze-functional leaf))
665 leaf))
666 (ref (make-ref leaf name)))
667 (push ref (leaf-refs leaf))
668 (setf (leaf-ever-used leaf) t)
669 (link-node-to-previous-ctran ref start)
670 (cond (type (let* ((ref-ctran (make-ctran))
671 (ref-lvar (make-lvar))
672 (cast (make-cast ref-lvar
673 (make-single-value-type type)
674 (lexenv-policy *lexenv*))))
675 (setf (lvar-dest ref-lvar) cast)
676 (use-continuation ref ref-ctran ref-lvar)
677 (link-node-to-previous-ctran cast ref-ctran)
678 (use-continuation cast next result)))
679 (t (use-continuation ref next result)))))
681 (defun always-boundp (name)
682 (case (info :variable :always-bound name)
683 (:always-bound t)
684 ;; Compiling to fasl considers a symbol always-bound if its
685 ;; :always-bound info value is now T or will eventually be T.
686 (:eventually (producing-fasl-file))))
688 ;;; Convert a reference to a symbolic constant or variable. If the
689 ;;; symbol is entered in the LEXENV-VARS we use that definition,
690 ;;; otherwise we find the current global definition. This is also
691 ;;; where we pick off symbol macro and alien variable references.
692 (defun ir1-convert-var (start next result name)
693 (declare (type ctran start next) (type (or lvar null) result) (symbol name))
694 (let ((var (or (lexenv-find name vars) (find-free-var name))))
695 (if (and (global-var-p var) (not (always-boundp name)))
696 ;; KLUDGE: If the variable may be unbound, convert using SYMBOL-VALUE
697 ;; which is not flushable, so that unbound dead variables signal an
698 ;; error (bug 412, lp#722734): checking for null RESULT is not enough,
699 ;; since variables can become dead due to later optimizations.
700 (ir1-convert start next result
701 (if (eq (global-var-kind var) :global)
702 `(symbol-global-value ',name)
703 `(symbol-value ',name)))
704 (etypecase var
705 (leaf
706 (cond
707 ((lambda-var-p var)
708 (let ((home (ctran-home-lambda-or-null start)))
709 (when home
710 (sset-adjoin var (lambda-calls-or-closes home))))
711 (when (lambda-var-ignorep var)
712 ;; (ANSI's specification for the IGNORE declaration requires
713 ;; that this be a STYLE-WARNING, not a full WARNING.)
714 #-sb-xc-host
715 (compiler-style-warn "reading an ignored variable: ~S" name)
716 ;; there's no need for us to accept ANSI's lameness when
717 ;; processing our own code, though.
718 #+sb-xc-host
719 (warn "reading an ignored variable: ~S" name)))
721 ;; This case signals {EARLY,LATE}-DEPRECATION-WARNING
722 ;; for CONSTANT nodes in :EARLY and :LATE deprecation
723 ;; (constants in :FINAL deprecation are represented as
724 ;; symbol-macros).
725 (aver (memq (check-deprecated-thing 'variable name)
726 '(nil :early :late)))))
727 (reference-leaf start next result var name))
728 ((cons (eql macro)) ; symbol-macro
729 ;; This case signals {EARLY,LATE,FINAL}-DEPRECATION-WARNING
730 ;; for symbol-macros. Includes variables, constants,
731 ;; etc. in :FINAL deprecation.
732 (check-deprecated-thing 'variable name)
733 ;; FIXME: [Free] type declarations. -- APD, 2002-01-26
734 (ir1-convert start next result (cdr var)))
735 (heap-alien-info
736 (ir1-convert start next result `(%heap-alien ',var))))))
737 (values))
739 ;;; Find a compiler-macro for a form, taking FUNCALL into account.
740 (defun find-compiler-macro (opname form)
741 (flet ((legal-cm-name-p (name)
742 (and (legal-fun-name-p name)
743 (or (not (symbolp name))
744 (not (sb!xc:macro-function name *lexenv*))))))
745 (if (eq opname 'funcall)
746 (let ((fun-form (cadr form)))
747 (cond ((and (consp fun-form) (eq 'function (car fun-form))
748 (not (cddr fun-form)))
749 (let ((real-fun (cadr fun-form)))
750 (if (legal-cm-name-p real-fun)
751 (values (sb!xc:compiler-macro-function real-fun *lexenv*)
752 real-fun)
753 (values nil nil))))
754 ((sb!xc:constantp fun-form *lexenv*)
755 (let ((fun (constant-form-value fun-form *lexenv*)))
756 (if (legal-cm-name-p fun)
757 ;; CLHS tells us that local functions must shadow
758 ;; compiler-macro-functions, but since the call is
759 ;; through a name, we are obviously interested
760 ;; in the global function.
761 ;; KLUDGE: CLHS 3.2.2.1.1 also says that it can be
762 ;; "a list whose car is funcall and whose cadr is
763 ;; a list (function name)", that means that
764 ;; (funcall 'name) that gets here doesn't fit the
765 ;; definition.
766 (values (sb!xc:compiler-macro-function fun nil) fun)
767 (values nil nil))))
769 (values nil nil))))
770 (if (legal-fun-name-p opname)
771 (values (sb!xc:compiler-macro-function opname *lexenv*) opname)
772 (values nil nil)))))
774 ;;; If FORM has a usable compiler macro, use it; otherwise return FORM itself.
775 ;;; Return the name of the compiler-macro as a secondary value, if applicable.
776 (defun expand-compiler-macro (form)
777 (binding* ((name (car form))
778 ((cmacro-fun cmacro-fun-name)
779 (find-compiler-macro name form)))
780 (cond
781 ((and cmacro-fun
782 ;; CLHS 3.2.2.1.3 specifies that NOTINLINE
783 ;; suppresses compiler-macros.
784 (not (fun-lexically-notinline-p cmacro-fun-name)))
785 (check-deprecated-thing 'function name)
786 (values (handler-case (careful-expand-macro cmacro-fun form t)
787 (compiler-macro-keyword-problem (condition)
788 (print-compiler-message
789 *error-output* "note: ~A" (list condition))
790 form))
791 cmacro-fun-name))
793 (values form nil)))))
795 ;;; Picks off special forms and compiler-macro expansions, and hands
796 ;;; the rest to IR1-CONVERT-COMMON-FUNCTOID
797 (defun ir1-convert-functoid (start next result form)
798 (let* ((op (car form))
799 (translator (and (symbolp op) (info :function :ir1-convert op))))
800 (if translator
801 (funcall translator start next result form)
802 (multiple-value-bind (res cmacro-fun-name)
803 (expand-compiler-macro form)
804 (cond ((eq res form)
805 (ir1-convert-common-functoid start next result form op))
807 (unless (policy *lexenv* (zerop store-xref-data))
808 (record-call cmacro-fun-name (ctran-block start)
809 *current-path*))
810 (ir1-convert start next result res)))))))
812 ;;; Handles the "common" cases: any other forms except special forms
813 ;;; and compiler-macros.
814 (defun ir1-convert-common-functoid (start next result form op)
815 (cond ((or (symbolp op) (leaf-p op))
816 (let ((lexical-def (if (leaf-p op) op (lexenv-find op funs))))
817 (typecase lexical-def
818 (null
819 (check-deprecated-thing 'function op)
820 (ir1-convert-global-functoid start next result form op))
821 (functional
822 (ir1-convert-local-combination start next result form
823 lexical-def))
824 (global-var
825 (check-deprecated-thing 'function (leaf-source-name lexical-def))
826 (ir1-convert-srctran start next result lexical-def form))
828 (aver (and (consp lexical-def) (eq (car lexical-def) 'macro)))
829 (ir1-convert start next result
830 (careful-expand-macro (cdr lexical-def) form))))))
831 ((or (atom op) (not (eq (car op) 'lambda)))
832 (compiler-error "illegal function call"))
834 ;; implicitly (LAMBDA ..) because the LAMBDA expression is
835 ;; the CAR of an executed form.
836 (ir1-convert start next result `(%funcall ,@form)))))
838 ;;; Convert anything that looks like a global function call.
839 (defun ir1-convert-global-functoid (start next result form fun)
840 (declare (type ctran start next) (type (or lvar null) result)
841 (list form))
842 (when (eql fun 'declare)
843 (compiler-error
844 "~@<There is no function named ~S. ~
845 References to ~S in some contexts (like starts of blocks) are unevaluated ~
846 expressions, but here the expression is being evaluated, which invokes ~
847 undefined behaviour.~@:>" fun fun))
848 ;; FIXME: Couldn't all the INFO calls here be converted into
849 ;; standard CL functions, like MACRO-FUNCTION or something? And what
850 ;; happens with lexically-defined (MACROLET) macros here, anyway?
851 (ecase (info :function :kind fun)
852 (:macro
853 (ir1-convert start next result
854 (careful-expand-macro (info :function :macro-function fun)
855 form))
856 (unless (policy *lexenv* (zerop store-xref-data))
857 (record-macroexpansion fun (ctran-block start) *current-path*)))
858 ((nil :function)
859 (ir1-convert-srctran start next result
860 (find-free-fun fun "shouldn't happen! (no-cmacro)")
861 form))))
863 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
864 ;;; errors which occur during the macroexpansion.
865 (defun careful-expand-macro (fun form &optional cmacro)
866 (flet (;; Return a string to use as a prefix in error reporting,
867 ;; telling something about which form caused the problem.
868 (wherestring ()
869 (let (;; We rely on the printer to abbreviate FORM.
870 (*print-length* 3)
871 (*print-level* 3))
872 (format nil
873 "~@<~A of ~S. Use ~S to intercept.~%~:@>"
874 (cond (cmacro
875 #-sb-xc-host "Error during compiler-macroexpansion"
876 #+sb-xc-host "Error during XC compiler-macroexpansion")
878 #-sb-xc-host "during macroexpansion"
879 #+sb-xc-host "during XC macroexpansion"))
880 form
881 '*break-on-signals*))))
882 (handler-bind (;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for Debian
883 ;; Linux, anyway) raises a CL:WARNING condition (not a
884 ;; CL:STYLE-WARNING) for undefined symbols when converting
885 ;; interpreted functions, causing COMPILE-FILE to think the
886 ;; file has a real problem, causing COMPILE-FILE to return
887 ;; FAILURE-P set (not just WARNINGS-P set). Since undefined
888 ;; symbol warnings are often harmless forward references,
889 ;; and since it'd be inordinately painful to try to
890 ;; eliminate all such forward references, these warnings
891 ;; are basically unavoidable. Thus, we need to coerce the
892 ;; system to work through them, and this code does so, by
893 ;; crudely suppressing all warnings in cross-compilation
894 ;; macroexpansion. -- WHN 19990412
895 #+(and cmu sb-xc-host)
896 (warning (lambda (c)
897 (compiler-notify
898 "~@<~A~:@_~
899 ~A~:@_~
900 ~@<(KLUDGE: That was a non-STYLE WARNING. ~
901 Ordinarily that would cause compilation to ~
902 fail. However, since we're running under ~
903 CMU CL, and since CMU CL emits non-STYLE ~
904 warnings for safe, hard-to-fix things (e.g. ~
905 references to not-yet-defined functions) ~
906 we're going to have to ignore it and ~
907 proceed anyway. Hopefully we're not ~
908 ignoring anything horrible here..)~:@>~:>"
909 (wherestring)
911 (muffle-warning)
912 (bug "no MUFFLE-WARNING restart")))
913 (error
914 (lambda (c)
915 (cond
916 (cmacro
917 ;; The spec is silent on what we should do. Signaling
918 ;; a full warning but declining to expand seems like
919 ;; a conservative and sane thing to do.
920 (compiler-warn "~@<~A~@:_ ~A~:>" (wherestring) c)
921 (return-from careful-expand-macro form))
923 (compiler-error "~@<~A~@:_ ~A~:>"
924 (wherestring) c))))))
925 (funcall (valid-macroexpand-hook) fun form *lexenv*))))
927 ;;;; conversion utilities
929 ;;; Convert a bunch of forms, discarding all the values except the
930 ;;; last. If there aren't any forms, then translate a NIL.
931 (declaim (ftype (sfunction (ctran ctran (or lvar null) list) (values))
932 ir1-convert-progn-body))
933 (defun ir1-convert-progn-body (start next result body)
934 (if (endp body)
935 (reference-constant start next result nil)
936 (let ((this-start start)
937 (forms body))
938 (loop
939 (let ((form (car forms)))
940 (setf this-start
941 (maybe-instrument-progn-like this-start forms form))
942 (when (endp (cdr forms))
943 (ir1-convert this-start next result form)
944 (return))
945 (let ((this-ctran (make-ctran)))
946 (ir1-convert this-start this-ctran nil form)
947 (setq this-start this-ctran
948 forms (cdr forms)))))))
949 (values))
952 ;;;; code coverage
954 ;;; Used as the CDR of the code coverage instrumentation records
955 ;;; (instead of NIL) to ensure that any well-behaving user code will
956 ;;; not have constants EQUAL to that record. This avoids problems with
957 ;;; the records getting coalesced with non-record conses, which then
958 ;;; get mutated when the instrumentation runs. Note that it's
959 ;;; important for multiple records for the same location to be
960 ;;; coalesced. -- JES, 2008-01-02
961 ;;; Use of #. mandates :COMPILE-TOPLEVEL for several Lisps
962 ;;; even though for us it's immediately accessible to EVAL.
963 (eval-when (:compile-toplevel :load-toplevel :execute)
964 (defconstant +code-coverage-unmarked+ '%code-coverage-unmarked%))
966 ;;; Check the policy for whether we should generate code coverage
967 ;;; instrumentation. If not, just return the original START
968 ;;; ctran. Otherwise insert code coverage instrumentation after
969 ;;; START, and return the new ctran.
970 (defun instrument-coverage (start mode form)
971 ;; We don't actually use FORM for anything, it's just convenient to
972 ;; have around when debugging the instrumentation.
973 (declare (ignore form))
974 (if (and (policy *lexenv* (> store-coverage-data 0))
975 *code-coverage-records*
976 *allow-instrumenting*)
977 (let ((path (source-path-original-source *current-path*)))
978 (when mode
979 (push mode path))
980 (if (member (ctran-block start)
981 (gethash path *code-coverage-blocks*))
982 ;; If this source path has already been instrumented in
983 ;; this block, don't instrument it again.
984 start
985 (let ((store
986 ;; Get an interned record cons for the path. A cons
987 ;; with the same object identity must be used for
988 ;; each instrument for the same block.
989 (or (gethash path *code-coverage-records*)
990 (setf (gethash path *code-coverage-records*)
991 (cons path +code-coverage-unmarked+))))
992 (next (make-ctran))
993 (*allow-instrumenting* nil))
994 (push (ctran-block start)
995 (gethash path *code-coverage-blocks*))
996 (ir1-convert start next nil
997 `(locally
998 (declare (optimize speed
999 (safety 0)
1000 (debug 0)
1001 (check-constant-modification 0)))
1002 ;; We're being naughty here, and
1003 ;; modifying constant data. That's ok,
1004 ;; we know what we're doing.
1005 (%rplacd ',store t)))
1006 next)))
1007 start))
1009 ;;; In contexts where we don't have a source location for FORM
1010 ;;; e.g. due to it not being a cons, but where we have a source
1011 ;;; location for the enclosing cons, use the latter source location if
1012 ;;; available. This works pretty well in practice, since many PROGNish
1013 ;;; macroexpansions will just directly splice a block of forms into
1014 ;;; some enclosing form with `(progn ,@body), thus retaining the
1015 ;;; EQness of the conses.
1016 (defun maybe-instrument-progn-like (start forms form)
1017 (or (when (and *allow-instrumenting*
1018 (not (get-source-path form)))
1019 (let ((*current-path* (get-source-path forms)))
1020 (when *current-path*
1021 (instrument-coverage start nil form))))
1022 start))
1024 (defun record-code-coverage (info cc)
1025 (setf (gethash info *code-coverage-info*) cc))
1027 (defun clear-code-coverage ()
1028 (clrhash *code-coverage-info*))
1030 (defun reset-code-coverage ()
1031 (maphash (lambda (info cc)
1032 (declare (ignore info))
1033 (dolist (cc-entry cc)
1034 (setf (cdr cc-entry) +code-coverage-unmarked+)))
1035 *code-coverage-info*))
1037 (defun code-coverage-record-marked (record)
1038 (aver (consp record))
1039 (ecase (cdr record)
1040 ((#.+code-coverage-unmarked+) nil)
1041 ((t) t)))
1044 ;;;; converting combinations
1046 ;;; Does this form look like something that we should add single-stepping
1047 ;;; instrumentation for?
1048 (defun step-form-p (form)
1049 (flet ((step-symbol-p (symbol)
1050 (and (not (member (symbol-package symbol)
1051 (load-time-value
1052 ;; KLUDGE: packages we're not interested in
1053 ;; stepping.
1054 (mapcar #'find-package '(sb!c sb!int sb!impl
1055 sb!kernel sb!pcl)) t)))
1056 ;; Consistent treatment of *FOO* vs (SYMBOL-VALUE '*FOO*):
1057 ;; we insert calls to SYMBOL-VALUE for most non-lexical
1058 ;; variable references in order to avoid them being elided
1059 ;; if the value is unused.
1060 (or (not (member symbol '(symbol-value symbol-global-value)))
1061 (not (constantp (second form)))))))
1062 (and *allow-instrumenting*
1063 (policy *lexenv* (= insert-step-conditions 3))
1064 (listp form)
1065 (symbolp (car form))
1066 (step-symbol-p (car form)))))
1068 ;;; Convert a function call where the function FUN is a LEAF. FORM is
1069 ;;; the source for the call. We return the COMBINATION node so that
1070 ;;; the caller can poke at it if it wants to.
1071 (declaim (ftype (sfunction (ctran ctran (or lvar null) list leaf) combination)
1072 ir1-convert-combination))
1073 (defun ir1-convert-combination (start next result form fun)
1074 (let ((ctran (make-ctran))
1075 (fun-lvar (make-lvar)))
1076 (ir1-convert start ctran fun-lvar `(the (or function symbol) ,fun))
1077 (let ((combination
1078 (ir1-convert-combination-args fun-lvar ctran next result
1079 (cdr form))))
1080 (when (step-form-p form)
1081 ;; Store a string representation of the form in the
1082 ;; combination node. This will let the IR2 translator know
1083 ;; that we want stepper instrumentation for this node. The
1084 ;; string will be stored in the debug-info by DUMP-1-LOCATION.
1085 (setf (combination-step-info combination)
1086 (let ((*print-pretty* t)
1087 (*print-circle* t)
1088 (*print-readably* nil))
1089 (prin1-to-string form))))
1090 combination)))
1092 ;;; Convert the arguments to a call and make the COMBINATION
1093 ;;; node. FUN-LVAR yields the function to call. ARGS is the list of
1094 ;;; arguments for the call, which defaults to the cdr of source. We
1095 ;;; return the COMBINATION node.
1096 (defun ir1-convert-combination-args (fun-lvar start next result args)
1097 (declare (type ctran start next)
1098 (type lvar fun-lvar)
1099 (type (or lvar null) result)
1100 (list args))
1101 (let ((node (make-combination fun-lvar)))
1102 (setf (lvar-dest fun-lvar) node)
1103 (collect ((arg-lvars))
1104 (let ((this-start start)
1105 (forms args))
1106 (dolist (arg args)
1107 (setf this-start
1108 (maybe-instrument-progn-like this-start forms arg))
1109 (setf forms (cdr forms))
1110 (let ((this-ctran (make-ctran))
1111 (this-lvar (make-lvar node)))
1112 (ir1-convert this-start this-ctran this-lvar arg)
1113 (setq this-start this-ctran)
1114 (arg-lvars this-lvar)))
1115 (link-node-to-previous-ctran node this-start)
1116 (use-continuation node next result)
1117 (setf (combination-args node) (arg-lvars))))
1118 node))
1120 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
1121 ;;; source transforms and try out any inline expansion. If there is no
1122 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
1123 ;;; known function which will quite possibly be open-coded.) Next, we
1124 ;;; go to ok-combination conversion.
1125 (defun ir1-convert-srctran (start next result var form)
1126 (declare (type ctran start next) (type (or lvar null) result)
1127 (type global-var var))
1128 (let ((name (leaf-source-name var))
1129 (inlinep (when (defined-fun-p var)
1130 (defined-fun-inlinep var))))
1131 (if (eq inlinep :notinline)
1132 (ir1-convert-combination start next result form var)
1133 (let* ((transform (info :function :source-transform name)))
1134 (if transform
1135 (multiple-value-bind (transformed pass)
1136 (if (functionp transform)
1137 (funcall transform form *lexenv*)
1138 (let ((result
1139 (if (eq (cdr transform) :predicate)
1140 (and (singleton-p (cdr form))
1141 `(%instance-typep
1142 ,(cadr form)
1143 ',(dd-name (car transform))))
1144 (slot-access-transform
1145 (if (consp name) :write :read)
1146 (cdr form) transform))))
1147 (values result (null result))))
1148 (cond (pass
1149 (ir1-convert-maybe-predicate start next result form var))
1151 (unless (policy *lexenv* (zerop store-xref-data))
1152 (record-call name (ctran-block start) *current-path*))
1153 (ir1-convert start next result transformed))))
1154 (ir1-convert-maybe-predicate start next result form var))))))
1156 ;;; KLUDGE: If we insert a synthetic IF for a function with the PREDICATE
1157 ;;; attribute, don't generate any branch coverage instrumentation for it.
1158 (defvar *instrument-if-for-code-coverage* t)
1160 ;;; If the function has the PREDICATE attribute, and the RESULT's DEST
1161 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
1162 ;;; predicate always appears in a conditional context.
1164 ;;; If the function isn't a predicate, then we call
1165 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
1166 (defun ir1-convert-maybe-predicate (start next result form var)
1167 (declare (type ctran start next)
1168 (type (or lvar null) result)
1169 (list form)
1170 (type global-var var))
1171 (let ((info (info :function :info (leaf-source-name var))))
1172 (if (and info
1173 (ir1-attributep (fun-info-attributes info) predicate)
1174 (not (if-p (and result (lvar-dest result)))))
1175 (let ((*instrument-if-for-code-coverage* nil))
1176 (ir1-convert start next result `(if ,form t nil)))
1177 (ir1-convert-combination-checking-type start next result form var))))
1179 ;;; Actually really convert a global function call that we are allowed
1180 ;;; to early-bind.
1182 ;;; If we know the function type of the function, then we check the
1183 ;;; call for syntactic legality with respect to the declared function
1184 ;;; type. If it is impossible to determine whether the call is correct
1185 ;;; due to non-constant keywords, then we give up, marking the call as
1186 ;;; :FULL to inhibit further error messages. We return true when the
1187 ;;; call is legal.
1189 ;;; If the call is legal, we also propagate type assertions from the
1190 ;;; function type to the arg and result lvars. We do this now so that
1191 ;;; IR1 optimize doesn't have to redundantly do the check later so
1192 ;;; that it can do the type propagation.
1193 (defun ir1-convert-combination-checking-type (start next result form var)
1194 (declare (type ctran start next) (type (or lvar null) result)
1195 (list form)
1196 (type leaf var))
1197 (let* ((node (ir1-convert-combination start next result form var))
1198 (fun-lvar (basic-combination-fun node))
1199 (type (leaf-type var)))
1200 (when (validate-call-type node type var t)
1201 (setf (lvar-%derived-type fun-lvar)
1202 (make-single-value-type type))
1203 (setf (lvar-reoptimize fun-lvar) nil)))
1204 (values))
1206 ;;; Convert a call to a local function, or if the function has already
1207 ;;; been LET converted, then throw FUNCTIONAL to
1208 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
1209 ;;; are converting inline expansions for local functions during
1210 ;;; optimization.
1211 (defun ir1-convert-local-combination (start next result form functional)
1212 (assure-functional-live-p functional)
1213 (ir1-convert-combination start next result
1214 form
1215 (maybe-reanalyze-functional functional)))
1217 ;;;; PROCESS-DECLS
1219 ;;; Given a list of LAMBDA-VARs and a variable name, return the
1220 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
1221 ;;; *last* variable with that name, since LET* bindings may be
1222 ;;; duplicated, and declarations always apply to the last.
1223 (declaim (ftype (sfunction (list symbol) (or lambda-var list))
1224 find-in-bindings))
1225 (defun find-in-bindings (vars name)
1226 (let ((found nil))
1227 (dolist (var vars)
1228 (cond ((leaf-p var)
1229 (when (eq (leaf-source-name var) name)
1230 (setq found var))
1231 (let ((info (lambda-var-arg-info var)))
1232 (when info
1233 (let ((supplied-p (arg-info-supplied-p info)))
1234 (when (and supplied-p
1235 (eq (leaf-source-name supplied-p) name))
1236 (setq found supplied-p))))))
1237 ((and (consp var) (eq (car var) name))
1238 (setf found (cdr var)))))
1239 found))
1241 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
1242 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
1243 ;;; type, otherwise we add a type restriction on the var. If a symbol
1244 ;;; macro, we just wrap a THE around the expansion.
1245 (defun process-type-decl (decl res vars context)
1246 (declare (type list decl vars) (type lexenv res))
1247 (let* ((type-specifier (first decl))
1248 (type (progn
1249 (when (typep type-specifier 'type-specifier)
1250 (check-deprecated-type type-specifier))
1251 (compiler-specifier-type type-specifier))))
1252 (collect ((restr nil cons)
1253 (new-vars nil cons))
1254 (dolist (var-name (rest decl))
1255 (unless (symbolp var-name)
1256 (compiler-error "Variable name is not a symbol: ~S." var-name))
1257 (unless (eq (info :variable :kind var-name) :unknown)
1258 (program-assert-symbol-home-package-unlocked
1259 context var-name "declaring the type of ~A"))
1260 (let* ((bound-var (find-in-bindings vars var-name))
1261 (var (or bound-var
1262 (lexenv-find var-name vars)
1263 (find-free-var var-name))))
1264 (etypecase var
1265 (leaf
1266 (flet
1267 ((process-var (var bound-var)
1268 (let* ((old-type (or (lexenv-find var type-restrictions)
1269 (leaf-type var)))
1270 (int (if (or (fun-type-p type)
1271 (fun-type-p old-type))
1272 type
1273 (type-approx-intersection2
1274 old-type type))))
1275 (cond ((eq int *empty-type*)
1276 (unless (policy *lexenv* (= inhibit-warnings 3))
1277 (warn
1278 'type-warning
1279 :format-control
1280 "The type declarations ~
1281 ~/sb!impl:print-type/ and ~
1282 ~/sb!impl:print-type/ for ~
1283 ~S conflict."
1284 :format-arguments
1285 (list old-type type var-name))))
1286 (bound-var
1287 (setf (leaf-type bound-var) int
1288 (leaf-where-from bound-var) :declared))
1290 (restr (cons var int)))))))
1291 (process-var var bound-var)
1292 (awhen (and (lambda-var-p var)
1293 (lambda-var-specvar var))
1294 (process-var it nil))))
1295 (cons
1296 ;; FIXME: non-ANSI weirdness. [See lp#309122]
1297 (aver (eq (car var) 'macro))
1298 (new-vars `(,var-name . (macro . (the ,(first decl)
1299 ,(cdr var))))))
1300 (heap-alien-info
1301 (compiler-error
1302 "~S is an alien variable, so its type can't be declared."
1303 var-name)))))
1305 (if (or (restr) (new-vars))
1306 (make-lexenv :default res
1307 :type-restrictions (restr)
1308 :vars (new-vars))
1309 res))))
1311 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
1312 ;;; declarations for function variables. In addition to allowing
1313 ;;; declarations for functions being bound, we must also deal with
1314 ;;; declarations that constrain the type of lexically apparent
1315 ;;; functions.
1316 (defun process-ftype-decl (type-specifier res names fvars context)
1317 (declare (type list names fvars)
1318 (type lexenv res))
1319 (let ((type (compiler-specifier-type type-specifier)))
1320 (check-deprecated-type type-specifier)
1321 (unless (csubtypep type (specifier-type 'function))
1322 (compiler-style-warn "ignoring declared FTYPE: ~S (not a function type)"
1323 type-specifier)
1324 (return-from process-ftype-decl res))
1325 (collect ((res nil cons))
1326 (dolist (name names)
1327 (when (fboundp name)
1328 (program-assert-symbol-home-package-unlocked
1329 context name "declaring the ftype of ~A"))
1330 (let ((found (find name fvars :key (lambda (x)
1331 (unless (consp x) ;; macrolet
1332 (leaf-source-name x)))
1333 :test #'equal)))
1334 (cond
1335 (found
1336 (setf (leaf-type found) type)
1337 (assert-definition-type found type
1338 :unwinnage-fun #'compiler-notify
1339 :where "FTYPE declaration"))
1341 (res (cons (find-lexically-apparent-fun
1342 name "in a function type declaration")
1343 type))))))
1344 (if (res)
1345 (make-lexenv :default res :type-restrictions (res))
1346 res))))
1348 ;;; Process a special declaration, returning a new LEXENV. A non-bound
1349 ;;; special declaration is instantiated by throwing a special variable
1350 ;;; into the variables if BINDING-FORM-P is NIL, or otherwise into
1351 ;;; *POST-BINDING-VARIABLE-LEXENV*.
1352 (defun process-special-decl (spec res vars binding-form-p context)
1353 (declare (list spec vars) (type lexenv res))
1354 (collect ((new-venv nil cons))
1355 (dolist (name (cdr spec))
1356 ;; While CLHS seems to allow local SPECIAL declarations for constants,
1357 ;; whatever the semantics are supposed to be is not at all clear to me
1358 ;; -- since constants aren't allowed to be bound it should be a no-op as
1359 ;; no-one can observe the difference portably, but specials are allowed
1360 ;; to be bound... yet nowhere does it say that the special declaration
1361 ;; removes the constantness. Call it a spec bug and prohibit it. Same
1362 ;; for GLOBAL variables.
1363 (let ((kind (info :variable :kind name)))
1364 (unless (member kind '(:special :unknown))
1365 (compiler-error
1366 "Can't declare ~(~A~) variable locally special: ~S"
1367 kind name)))
1368 (program-assert-symbol-home-package-unlocked
1369 context name "declaring ~A special")
1370 (let ((var (find-in-bindings vars name)))
1371 (etypecase var
1372 (cons
1373 (aver (eq (car var) 'macro))
1374 (compiler-error
1375 "~S is a symbol-macro and thus can't be declared special."
1376 name))
1377 (lambda-var
1378 (when (lambda-var-ignorep var)
1379 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1380 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1381 (compiler-style-warn
1382 "The ignored variable ~S is being declared special."
1383 name))
1384 (setf (lambda-var-specvar var)
1385 (specvar-for-binding name)))
1386 (null
1387 (unless (or (assoc name (new-venv) :test #'eq))
1388 (new-venv (cons name (specvar-for-binding name))))))))
1389 (cond (binding-form-p
1390 (setf *post-binding-variable-lexenv*
1391 (append (new-venv) *post-binding-variable-lexenv*))
1392 res)
1393 ((new-venv)
1394 (make-lexenv :default res :vars (new-venv)))
1396 res))))
1398 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP
1399 ;;; (and TYPE if notinline), plus type-restrictions from the lexenv.
1400 (defun make-new-inlinep (var inlinep local-type)
1401 (declare (type global-var var) (type inlinep inlinep))
1402 (let* ((type (if (and (eq inlinep :notinline)
1403 (not (eq (leaf-where-from var) :declared)))
1404 (specifier-type 'function)
1405 (leaf-type var)))
1406 (res (make-defined-fun
1407 :%source-name (leaf-source-name var)
1408 :where-from (leaf-where-from var)
1409 :type (if local-type
1410 (type-intersection local-type type)
1411 type)
1412 :inlinep inlinep)))
1413 (when (defined-fun-p var)
1414 (setf (defined-fun-inline-expansion res)
1415 (defined-fun-inline-expansion var))
1416 (setf (defined-fun-functionals res)
1417 (defined-fun-functionals var)))
1418 ;; FIXME: Is this really right? Needs we not set the FUNCTIONAL
1419 ;; to the original global-var?
1420 res))
1422 ;;; Parse an inline/notinline declaration. If it's a local function we're
1423 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1424 (defun process-inline-decl (spec res fvars)
1425 (let ((sense (cdr (assoc (first spec) *inlinep-translations* :test #'eq)))
1426 (new-fenv ()))
1427 (dolist (name (rest spec))
1428 (let ((fvar (find name fvars
1429 :key (lambda (x)
1430 (unless (consp x) ;; macrolet
1431 (leaf-source-name x)))
1432 :test #'equal)))
1433 (if fvar
1434 (setf (functional-inlinep fvar) sense)
1435 (let ((found (find-lexically-apparent-fun
1436 name "in an inline or notinline declaration")))
1437 (etypecase found
1438 (functional
1439 (when (policy *lexenv* (>= speed inhibit-warnings))
1440 (compiler-notify "ignoring ~A declaration not at ~
1441 definition of local function:~% ~S"
1442 sense name)))
1443 (global-var
1444 (let ((type
1445 (cdr (assoc found (lexenv-type-restrictions res)))))
1446 (push (cons name (make-new-inlinep found sense type))
1447 new-fenv))))))))
1448 (if new-fenv
1449 (make-lexenv :default res :funs new-fenv)
1450 res)))
1452 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1453 (defun find-in-bindings-or-fbindings (name vars fvars)
1454 (declare (list vars fvars))
1455 (typecase name
1456 (atom
1457 (find-in-bindings vars name))
1458 ((cons (eql function) (cons * null))
1459 (find (cadr name) fvars :key (lambda (x)
1460 (if (consp x) ;; MACROLET
1461 (car x)
1462 (leaf-source-name x))) :test #'equal))
1464 (compiler-error "Malformed function or variable name ~S." name))))
1466 ;;; Process an ignore/ignorable declaration, checking for various losing
1467 ;;; conditions.
1468 (defun process-ignore-decl (spec vars fvars lexenv)
1469 (declare (list spec vars fvars))
1470 (dolist (name (rest spec))
1471 (let ((var (find-in-bindings-or-fbindings name vars fvars)))
1472 (cond
1473 ((not var)
1474 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1475 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1476 ;; But, other Lisp hosts signal a full warning, so when building
1477 ;; the cross-compiler, compile it as #'WARN so that in a self-hosted
1478 ;; build we can at least crash in the same way,
1479 ;; until we resolve this question about how severe the warning is.
1480 (multiple-value-call #+sb-xc-host #'warn
1481 #-sb-xc-host #'compiler-style-warn
1482 "~A declaration for ~A: ~A"
1483 (first spec)
1484 (if (symbolp name)
1485 (values
1486 (case (info :variable :kind name)
1487 (:special "a special variable")
1488 (:global "a global lexical variable")
1489 (:alien "a global alien variable")
1490 (t (if (assoc name (lexenv-vars lexenv))
1491 "a variable from outer scope"
1492 "an unknown variable")))
1493 name)
1494 (values
1495 (cond ((assoc (second name) (lexenv-funs lexenv)
1496 :test #'equal)
1497 "a function from outer scope")
1498 ((info :function :kind (second name))
1499 "a global function")
1501 "an unknown function"))
1502 (second name)))))
1503 ((and (consp var)
1504 (or (eq (car var) 'macro)
1505 (and (consp (cdr var))
1506 (eq (cadr var) 'macro))))
1507 ;; Just ignore the IGNORE decl: we don't currently signal style-warnings
1508 ;; for unused macrolet or symbol-macros, so there's no need to do anything.
1510 ((functional-p var)
1511 (setf (leaf-ever-used var) t))
1512 ((and (lambda-var-specvar var) (eq (first spec) 'ignore))
1513 ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1514 ;; requires that this be a STYLE-WARNING, not a full WARNING.
1515 (compiler-style-warn "Declaring special variable ~S to be ~A"
1516 name
1517 (first spec)))
1518 ((eq (first spec) 'ignorable)
1519 (setf (leaf-ever-used var) t))
1521 (setf (lambda-var-ignorep var) t)))))
1522 (values))
1524 (defun process-extent-decl (names vars fvars kind)
1525 (let ((extent
1526 (ecase kind
1527 (truly-dynamic-extent
1528 :always-dynamic)
1529 (dynamic-extent
1530 (when *stack-allocate-dynamic-extent*
1531 :maybe-dynamic))
1532 (indefinite-extent
1533 :indefinite))))
1534 (if extent
1535 (dolist (name names)
1536 (cond
1537 ((symbolp name)
1538 (let* ((bound-var (find-in-bindings vars name))
1539 (var (or bound-var
1540 (lexenv-find name vars)
1541 (maybe-find-free-var name))))
1542 (etypecase var
1543 (leaf
1544 (if bound-var
1545 (if (and (leaf-extent var) (neq extent (leaf-extent var)))
1546 (warn "Multiple incompatible extent declarations for ~S?" name)
1547 (setf (leaf-extent var) extent))
1548 (compiler-notify
1549 "Ignoring free ~S declaration: ~S" kind name)))
1550 (cons
1551 (compiler-error "~S on symbol-macro: ~S" kind name))
1552 (heap-alien-info
1553 (compiler-error "~S on alien-variable: ~S" kind name))
1554 (null
1555 (compiler-style-warn
1556 "Unbound variable declared ~S: ~S" kind name)))))
1557 ((and (consp name)
1558 (eq (car name) 'function)
1559 (null (cddr name))
1560 (valid-function-name-p (cadr name))
1561 (neq :indefinite extent))
1562 (let* ((fname (cadr name))
1563 (bound-fun (find fname fvars
1564 :key (lambda (x)
1565 (unless (consp x) ;; macrolet
1566 (leaf-source-name x)))
1567 :test #'equal))
1568 (fun (or bound-fun (lexenv-find fname funs))))
1569 (etypecase fun
1570 (leaf
1571 (if bound-fun
1572 #!+stack-allocatable-closures
1573 (setf (leaf-extent bound-fun) extent)
1574 #!-stack-allocatable-closures
1575 (compiler-notify
1576 "Ignoring DYNAMIC-EXTENT declaration on function ~S ~
1577 (not supported on this platform)." fname)
1578 (compiler-notify
1579 "Ignoring free DYNAMIC-EXTENT declaration: ~S" name)))
1580 (cons
1581 (compiler-error "DYNAMIC-EXTENT on macro: ~S" name))
1582 (null
1583 (compiler-style-warn
1584 "Unbound function declared DYNAMIC-EXTENT: ~S" name)))))
1586 (compiler-error "~S on a weird thing: ~S" kind name))))
1587 (when (policy *lexenv* (= speed 3))
1588 (compiler-notify "Ignoring DYNAMIC-EXTENT declarations: ~S" names)))))
1590 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1591 ;;; go away, I think.
1592 ;;; Or just rename the declaration to SB-C:RESULT-TYPE so that it's not
1593 ;;; a symbol in the CL package, and then eliminate this switch.
1594 ;;; It's permissible to have implementation-specific declarations.
1595 (defvar *suppress-values-declaration* nil
1596 "If true, processing of the VALUES declaration is inhibited.")
1598 ;;; Process a single declaration spec, augmenting the specified LEXENV
1599 ;;; RES. Return RES and result type. VARS and FVARS are as described
1600 ;;; PROCESS-DECLS.
1601 (defun process-1-decl (raw-spec res vars fvars binding-form-p context)
1602 (declare (type list raw-spec vars fvars))
1603 (declare (type lexenv res))
1604 (let ((spec (canonized-decl-spec raw-spec))
1605 (optimize-qualities))
1606 ;; FIXME: we can end up with a chain of spurious parent lexenvs,
1607 ;; when logically the processing of decls should yield at most
1608 ;; two new lexenvs: one for the bindings and one for post-binding.
1609 ;; It's possible that there's a simple fix of re-linking the resulting
1610 ;; lexenv directly to *lexenv* as its parent.
1611 (values
1612 (case (first spec)
1613 (type
1614 (process-type-decl (cdr spec) res vars context))
1615 ((ignore ignorable)
1616 (process-ignore-decl spec vars fvars res)
1617 res)
1618 (special (process-special-decl spec res vars binding-form-p context))
1619 (ftype
1620 (unless (cdr spec)
1621 (compiler-error "no type specified in FTYPE declaration: ~S" spec))
1622 (process-ftype-decl (second spec) res (cddr spec) fvars context))
1623 ((inline notinline maybe-inline)
1624 (process-inline-decl spec res fvars))
1625 (optimize
1626 (multiple-value-bind (new-policy specified-qualities)
1627 (process-optimize-decl spec (lexenv-policy res))
1628 (setq optimize-qualities specified-qualities)
1629 (make-lexenv :default res :policy new-policy)))
1630 (muffle-conditions
1631 (make-lexenv
1632 :default res
1633 :handled-conditions (process-muffle-conditions-decl
1634 spec (lexenv-handled-conditions res))))
1635 (unmuffle-conditions
1636 (make-lexenv
1637 :default res
1638 :handled-conditions (process-unmuffle-conditions-decl
1639 spec (lexenv-handled-conditions res))))
1640 ((dynamic-extent truly-dynamic-extent indefinite-extent)
1641 (process-extent-decl (cdr spec) vars fvars (first spec))
1642 res)
1643 ((disable-package-locks enable-package-locks)
1644 (make-lexenv
1645 :default res
1646 :disabled-package-locks (process-package-lock-decl
1647 spec (lexenv-disabled-package-locks res))))
1648 ;; We may want to detect LAMBDA-LIST and VALUES decls here,
1649 ;; and report them as "Misplaced" rather than "Unrecognized".
1651 (unless (info :declaration :recognized (first spec))
1652 (compiler-warn "unrecognized declaration ~S" raw-spec))
1653 (let ((fn (info :declaration :handler (first spec))))
1654 (if fn
1655 (funcall fn res spec vars fvars)
1656 res))))
1657 optimize-qualities)))
1659 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1660 ;;; and FUNCTIONAL structures which are being bound. In addition to
1661 ;;; filling in slots in the leaf structures, we return a new LEXENV,
1662 ;;; which reflects pervasive special and function type declarations,
1663 ;;; (NOT)INLINE declarations and OPTIMIZE declarations, and type of
1664 ;;; VALUES declarations. If BINDING-FORM-P is true, the third return
1665 ;;; value is a list of VARs that should not apply to the lexenv of the
1666 ;;; initialization forms for the bindings, but should apply to the body.
1668 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1669 ;;; of LOCALLY.
1670 (defun process-decls (decls vars fvars &key
1671 (lexenv *lexenv*) (binding-form-p nil) (context :compile)
1672 (allow-lambda-list nil))
1673 (declare (list decls vars fvars))
1674 (let ((result-type *wild-type*)
1675 (allow-values-decl allow-lambda-list)
1676 (explicit-check)
1677 (allow-explicit-check allow-lambda-list)
1678 (lambda-list (if allow-lambda-list :unspecified nil))
1679 (optimize-qualities)
1680 (*post-binding-variable-lexenv* nil))
1681 (flet ((process-it (spec decl)
1682 (cond ((atom spec)
1683 (compiler-error "malformed declaration specifier ~S in ~S"
1684 spec decl))
1685 ((and (eq allow-lambda-list t)
1686 (typep spec '(cons (eql lambda-list) (cons t null))))
1687 (setq lambda-list (cadr spec) allow-lambda-list nil))
1688 ((and allow-values-decl
1689 (typep spec '(cons (eql values)))
1690 (not *suppress-values-declaration*))
1691 ;; Why do we allow more than one VALUES decl? I don't know.
1692 (setq result-type
1693 (values-type-intersection
1694 result-type
1695 (compiler-values-specifier-type
1696 (let ((types (cdr spec)))
1697 (if (singleton-p types)
1698 (car types)
1699 `(values ,@types)))))))
1700 ((and allow-explicit-check
1701 (typep spec '(cons (eql explicit-check))))
1702 ;; EXPLICIT-CHECK by itself specifies that all argument and
1703 ;; result types are checked by the function body.
1704 ;; Alternatively, a subset of arguments, and/or :RESULT,
1705 ;; can be specified to indicate that only a subset are
1706 ;; checked; in that case, the compiler asserts all others.
1707 (awhen (remove-if (lambda (x)
1708 (or (member x vars
1709 :test #'eq
1710 :key #'lambda-var-%source-name)
1711 (eq x :result)))
1712 (cdr spec))
1713 (compiler-error "explicit-check list ~S must refer to lambda vars"
1714 it))
1715 (setq explicit-check (or (cdr spec) t)
1716 allow-explicit-check nil)) ; at most one of this decl
1718 (multiple-value-bind (new-env new-qualities)
1719 (process-1-decl spec lexenv vars fvars
1720 binding-form-p context)
1721 (setq lexenv new-env
1722 optimize-qualities
1723 (nconc new-qualities optimize-qualities)))))))
1724 (dolist (decl decls)
1725 (dolist (spec (rest decl))
1726 (if (eq context :compile)
1727 (with-current-source-form (spec decl) ; TODO this is a slight change to the previous code. make sure the behavior is identical
1728 (process-it spec decl))
1729 ;; Kludge: EVAL calls this function to deal with LOCALLY.
1730 (process-it spec decl)))))
1731 (warn-repeated-optimize-qualities (lexenv-policy lexenv) optimize-qualities)
1732 (values lexenv result-type *post-binding-variable-lexenv*
1733 lambda-list explicit-check)))
1735 (defun %processing-decls (decls vars fvars ctran lvar binding-form-p fun)
1736 (multiple-value-bind (*lexenv* result-type post-binding-lexenv)
1737 (process-decls decls vars fvars :binding-form-p binding-form-p)
1738 (cond ((eq result-type *wild-type*)
1739 (funcall fun ctran lvar post-binding-lexenv))
1741 (let ((value-ctran (make-ctran))
1742 (value-lvar (make-lvar)))
1743 (multiple-value-prog1
1744 (funcall fun value-ctran value-lvar post-binding-lexenv)
1745 (let ((cast (make-cast value-lvar result-type
1746 (lexenv-policy *lexenv*))))
1747 (link-node-to-previous-ctran cast value-ctran)
1748 (setf (lvar-dest value-lvar) cast)
1749 (use-continuation cast ctran lvar))))))))
1751 (defmacro processing-decls ((decls vars fvars ctran lvar
1752 &optional post-binding-lexenv)
1753 &body forms)
1754 (check-type ctran symbol)
1755 (check-type lvar symbol)
1756 (let ((post-binding-lexenv-p (not (null post-binding-lexenv)))
1757 (post-binding-lexenv (or post-binding-lexenv (sb!xc:gensym "LEXENV"))))
1758 `(%processing-decls ,decls ,vars ,fvars ,ctran ,lvar
1759 ,post-binding-lexenv-p
1760 (lambda (,ctran ,lvar ,post-binding-lexenv)
1761 (declare (ignorable ,post-binding-lexenv))
1762 ,@forms))))
1764 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1765 ;;; declaration. If there is a global variable of that name, then
1766 ;;; check that it isn't a constant and return it. Otherwise, create an
1767 ;;; anonymous GLOBAL-VAR.
1768 (defun specvar-for-binding (name)
1769 (cond ((not (eq (info :variable :where-from name) :assumed))
1770 (let ((found (find-free-var name)))
1771 (when (heap-alien-info-p found)
1772 (compiler-error
1773 "~S is an alien variable and so can't be declared special."
1774 name))
1775 (unless (global-var-p found)
1776 (compiler-error
1777 "~S is a constant and so can't be declared special."
1778 name))
1779 found))
1781 (make-global-var :kind :special
1782 :%source-name name
1783 :where-from :declared))))