0.8.21.42:
[sbcl.git] / src / compiler / ir1tran.lisp
1 ;;;; This file contains code which does the translation from Lisp code
2 ;;;; to the first intermediate representation (IR1).
3
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.
12
13 (in-package "SB!C")
14
15 (declaim (special *compiler-error-bailout*))
16
17 ;;; *SOURCE-PATHS* is a hashtable from source code forms to the path
18 ;;; taken through the source to reach the form. This provides a way to
19 ;;; keep track of the location of original source forms, even when
20 ;;; macroexpansions and other arbitary permutations of the code
21 ;;; happen. This table is initialized by calling FIND-SOURCE-PATHS on
22 ;;; the original source.
23 (declaim (hash-table *source-paths*))
24 (defvar *source-paths*)
25
26 ;;; *CURRENT-COMPONENT* is the COMPONENT structure which we link
27 ;;; blocks into as we generate them. This just serves to glue the
28 ;;; emitted blocks together until local call analysis and flow graph
29 ;;; canonicalization figure out what is really going on. We need to
30 ;;; keep track of all the blocks generated so that we can delete them
31 ;;; if they turn out to be unreachable.
32 ;;;
33 ;;; FIXME: It's confusing having one variable named *CURRENT-COMPONENT*
34 ;;; and another named *COMPONENT-BEING-COMPILED*. (In CMU CL they
35 ;;; were called *CURRENT-COMPONENT* and *COMPILE-COMPONENT* respectively,
36 ;;; which was also confusing.)
37 (declaim (type (or component null) *current-component*))
38 (defvar *current-component*)
39
40 ;;; *CURRENT-PATH* is the source path of the form we are currently
41 ;;; translating. See NODE-SOURCE-PATH in the NODE structure.
42 (declaim (list *current-path*))
43 (defvar *current-path*)
44
45 (defvar *derive-function-types* nil
46   "Should the compiler assume that function types will never change,
47   so that it can use type information inferred from current definitions
48   to optimize code which uses those definitions? Setting this true
49   gives non-ANSI, early-CMU-CL behavior. It can be useful for improving
50   the efficiency of stable code.")
51
52 (defvar *fun-names-in-this-file* nil)
53
54 (defvar *post-binding-variable-lexenv* nil)
55 \f
56 ;;;; namespace management utilities
57
58 (defun fun-lexically-notinline-p (name)
59   (let ((fun (lexenv-find name funs :test #'equal)))
60     ;; a declaration will trump a proclamation
61     (if (and fun (defined-fun-p fun))
62         (eq (defined-fun-inlinep fun) :notinline)
63         (eq (info :function :inlinep name) :notinline))))
64
65 ;;; Return a GLOBAL-VAR structure usable for referencing the global
66 ;;; function NAME.
67 (defun find-free-really-fun (name)
68   (unless (info :function :kind name)
69     (setf (info :function :kind name) :function)
70     (setf (info :function :where-from name) :assumed))
71   (let ((where (info :function :where-from name)))
72     (when (and (eq where :assumed)
73                ;; In the ordinary target Lisp, it's silly to report
74                ;; undefinedness when the function is defined in the
75                ;; running Lisp. But at cross-compile time, the current
76                ;; definedness of a function is irrelevant to the
77                ;; definedness at runtime, which is what matters.
78                #-sb-xc-host (not (fboundp name)))
79       (note-undefined-reference name :function))
80     (make-global-var
81      :kind :global-function
82      :%source-name name
83      :type (if (or *derive-function-types*
84                    (eq where :declared)
85                    (and (member name *fun-names-in-this-file* :test #'equal)
86                         (not (fun-lexically-notinline-p name))))
87                (info :function :type name)
88                (specifier-type 'function))
89      :where-from where)))
90
91 ;;; Has the *FREE-FUNS* entry FREE-FUN become invalid?
92 ;;;
93 ;;; In CMU CL, the answer was implicitly always true, so this 
94 ;;; predicate didn't exist.
95 ;;;
96 ;;; This predicate was added to fix bug 138 in SBCL. In some obscure
97 ;;; circumstances, it was possible for a *FREE-FUNS* entry to contain a
98 ;;; DEFINED-FUN whose DEFINED-FUN-FUNCTIONAL object contained IR1
99 ;;; stuff (NODEs, BLOCKs...) referring to an already compiled (aka
100 ;;; "dead") component. When this IR1 stuff was reused in a new
101 ;;; component, under further obscure circumstances it could be used by
102 ;;; WITH-IR1-ENVIRONMENT-FROM-NODE to generate a binding for
103 ;;; *CURRENT-COMPONENT*. At that point things got all confused, since
104 ;;; IR1 conversion was sending code to a component which had already
105 ;;; been compiled and would never be compiled again.
106 (defun invalid-free-fun-p (free-fun)
107   ;; There might be other reasons that *FREE-FUN* entries could
108   ;; become invalid, but the only one we've been bitten by so far
109   ;; (sbcl-0.pre7.118) is this one:
110   (and (defined-fun-p free-fun)
111        (let ((functional (defined-fun-functional free-fun)))
112          (or (and functional
113                   (eql (functional-kind functional) :deleted))
114              (and (lambda-p functional)
115                   (or
116                    ;; (The main reason for this first test is to bail
117                    ;; out early in cases where the LAMBDA-COMPONENT
118                    ;; call in the second test would fail because links
119                    ;; it needs are uninitialized or invalid.)
120                    ;;
121                    ;; If the BIND node for this LAMBDA is null, then
122                    ;; according to the slot comments, the LAMBDA has
123                    ;; been deleted or its call has been deleted. In
124                    ;; that case, it seems rather questionable to reuse
125                    ;; it, and certainly it shouldn't be necessary to
126                    ;; reuse it, so we cheerfully declare it invalid.
127                    (null (lambda-bind functional))
128                    ;; If this IR1 stuff belongs to a dead component,
129                    ;; then we can't reuse it without getting into
130                    ;; bizarre confusion.
131                    (eql (component-info (lambda-component functional))
132                         :dead)))))))
133
134 ;;; If NAME already has a valid entry in *FREE-FUNS*, then return
135 ;;; the value. Otherwise, make a new GLOBAL-VAR using information from
136 ;;; the global environment and enter it in *FREE-FUNS*. If NAME
137 ;;; names a macro or special form, then we error out using the
138 ;;; supplied context which indicates what we were trying to do that
139 ;;; demanded a function.
140 (declaim (ftype (sfunction (t string) global-var) find-free-fun))
141 (defun find-free-fun (name context)
142   (or (let ((old-free-fun (gethash name *free-funs*)))
143         (and (not (invalid-free-fun-p old-free-fun))
144              old-free-fun))
145       (ecase (info :function :kind name)
146         ;; FIXME: The :MACRO and :SPECIAL-FORM cases could be merged.
147         (:macro
148          (compiler-error "The macro name ~S was found ~A." name context))
149         (:special-form
150          (compiler-error "The special form name ~S was found ~A."
151                          name
152                          context))
153         ((:function nil)
154          (check-fun-name name)
155          (note-if-setf-fun-and-macro name)
156          (let ((expansion (fun-name-inline-expansion name))
157                (inlinep (info :function :inlinep name)))
158            (setf (gethash name *free-funs*)
159                  (if (or expansion inlinep)
160                      (make-defined-fun
161                       :%source-name name
162                       :inline-expansion expansion
163                       :inlinep inlinep
164                       :where-from (info :function :where-from name)
165                       :type (if (eq inlinep :notinline)
166                                 (specifier-type 'function)
167                                 (info :function :type name)))
168                      (find-free-really-fun name))))))))
169
170 ;;; Return the LEAF structure for the lexically apparent function
171 ;;; definition of NAME.
172 (declaim (ftype (sfunction (t string) leaf) find-lexically-apparent-fun))
173 (defun find-lexically-apparent-fun (name context)
174   (let ((var (lexenv-find name funs :test #'equal)))
175     (cond (var
176            (unless (leaf-p var)
177              (aver (and (consp var) (eq (car var) 'macro)))
178              (compiler-error "found macro name ~S ~A" name context))
179            var)
180           (t
181            (find-free-fun name context)))))
182
183 ;;; Return the LEAF node for a global variable reference to NAME. If
184 ;;; NAME is already entered in *FREE-VARS*, then we just return the
185 ;;; corresponding value. Otherwise, we make a new leaf using
186 ;;; information from the global environment and enter it in
187 ;;; *FREE-VARS*. If the variable is unknown, then we emit a warning.
188 (declaim (ftype (sfunction (t) (or leaf cons heap-alien-info)) find-free-var))
189 (defun find-free-var (name)
190   (unless (symbolp name)
191     (compiler-error "Variable name is not a symbol: ~S." name))
192   (or (gethash name *free-vars*)
193       (let ((kind (info :variable :kind name))
194             (type (info :variable :type name))
195             (where-from (info :variable :where-from name)))
196         (when (and (eq where-from :assumed) (eq kind :global))
197           (note-undefined-reference name :variable))
198         (setf (gethash name *free-vars*)
199               (case kind
200                 (:alien
201                  (info :variable :alien-info name))
202                 ;; FIXME: The return value in this case should really be
203                 ;; of type SB!C::LEAF.  I don't feel too badly about it,
204                 ;; because the MACRO idiom is scattered throughout this
205                 ;; file, but it should be cleaned up so we're not
206                 ;; throwing random conses around.  --njf 2002-03-23
207                 (:macro
208                  (let ((expansion (info :variable :macro-expansion name))
209                        (type (type-specifier (info :variable :type name))))
210                    `(MACRO . (the ,type ,expansion))))
211                 (:constant
212                  (let ((value (info :variable :constant-value name)))
213                    (make-constant :value value
214                                   :%source-name name
215                                   :type (ctype-of value)
216                                   :where-from where-from)))
217                 (t
218                  (make-global-var :kind kind
219                                   :%source-name name
220                                   :type type
221                                   :where-from where-from)))))))
222 \f
223 ;;; Grovel over CONSTANT checking for any sub-parts that need to be
224 ;;; processed with MAKE-LOAD-FORM. We have to be careful, because
225 ;;; CONSTANT might be circular. We also check that the constant (and
226 ;;; any subparts) are dumpable at all.
227 (eval-when (:compile-toplevel :load-toplevel :execute)
228   ;; The EVAL-WHEN is necessary for #.(1+ LIST-TO-HASH-TABLE-THRESHOLD)
229   ;; below. -- AL 20010227
230   (def!constant list-to-hash-table-threshold 32))
231 (defun maybe-emit-make-load-forms (constant)
232   (let ((things-processed nil)
233         (count 0))
234     ;; FIXME: Does this LIST-or-HASH-TABLE messiness give much benefit?
235     (declare (type (or list hash-table) things-processed)
236              (type (integer 0 #.(1+ list-to-hash-table-threshold)) count)
237              (inline member))
238     (labels ((grovel (value)
239                ;; Unless VALUE is an object which which obviously
240                ;; can't contain other objects
241                (unless (typep value
242                               '(or #-sb-xc-host unboxed-array
243                                    #+sb-xc-host (simple-array (unsigned-byte 8) (*))
244                                    symbol
245                                    number
246                                    character
247                                    string))
248                  (etypecase things-processed
249                    (list
250                     (when (member value things-processed :test #'eq)
251                       (return-from grovel nil))
252                     (push value things-processed)
253                     (incf count)
254                     (when (> count list-to-hash-table-threshold)
255                       (let ((things things-processed))
256                         (setf things-processed
257                               (make-hash-table :test 'eq))
258                         (dolist (thing things)
259                           (setf (gethash thing things-processed) t)))))
260                    (hash-table
261                     (when (gethash value things-processed)
262                       (return-from grovel nil))
263                     (setf (gethash value things-processed) t)))
264                  (typecase value
265                    (cons
266                     (grovel (car value))
267                     (grovel (cdr value)))
268                    (simple-vector
269                     (dotimes (i (length value))
270                       (grovel (svref value i))))
271                    ((vector t)
272                     (dotimes (i (length value))
273                       (grovel (aref value i))))
274                    ((simple-array t)
275                     ;; Even though the (ARRAY T) branch does the exact
276                     ;; same thing as this branch we do this separately
277                     ;; so that the compiler can use faster versions of
278                     ;; array-total-size and row-major-aref.
279                     (dotimes (i (array-total-size value))
280                       (grovel (row-major-aref value i))))
281                    ((array t)
282                     (dotimes (i (array-total-size value))
283                       (grovel (row-major-aref value i))))
284                    (;; In the target SBCL, we can dump any instance,
285                     ;; but in the cross-compilation host,
286                     ;; %INSTANCE-FOO functions don't work on general
287                     ;; instances, only on STRUCTURE!OBJECTs.
288                     #+sb-xc-host structure!object
289                     #-sb-xc-host instance
290                     (when (emit-make-load-form value)
291                       (dotimes (i (%instance-length value))
292                         (grovel (%instance-ref value i)))))
293                    (t
294                     (compiler-error
295                      "Objects of type ~S can't be dumped into fasl files."
296                      (type-of value)))))))
297       (grovel constant)))
298   (values))
299 \f
300 ;;;; some flow-graph hacking utilities
301
302 ;;; This function sets up the back link between the node and the
303 ;;; ctran which continues at it.
304 (defun link-node-to-previous-ctran (node ctran)
305   (declare (type node node) (type ctran ctran))
306   (aver (not (ctran-next ctran)))
307   (setf (ctran-next ctran) node)
308   (setf (node-prev node) ctran))
309
310 ;;; This function is used to set the ctran for a node, and thus
311 ;;; determine what is evaluated next. If the ctran has no block, then
312 ;;; we make it be in the block that the node is in. If the ctran heads
313 ;;; its block, we end our block and link it to that block.
314 #!-sb-fluid (declaim (inline use-ctran))
315 (defun use-ctran (node ctran)
316   (declare (type node node) (type ctran ctran))
317   (if (eq (ctran-kind ctran) :unused)
318       (let ((node-block (ctran-block (node-prev node))))
319         (setf (ctran-block ctran) node-block)
320         (setf (ctran-kind ctran) :inside-block)
321         (setf (ctran-use ctran) node)
322         (setf (node-next node) ctran))
323       (%use-ctran node ctran)))
324 (defun %use-ctran (node ctran)
325   (declare (type node node) (type ctran ctran) (inline member))
326   (let ((block (ctran-block ctran))
327         (node-block (ctran-block (node-prev node))))
328     (aver (eq (ctran-kind ctran) :block-start))
329     (when (block-last node-block)
330       (error "~S has already ended." node-block))
331     (setf (block-last node-block) node)
332     (when (block-succ node-block)
333       (error "~S already has successors." node-block))
334     (setf (block-succ node-block) (list block))
335     (when (memq node-block (block-pred block))
336       (error "~S is already a predecessor of ~S." node-block block))
337     (push node-block (block-pred block))))
338
339 ;;; This function is used to set the ctran for a node, and thus
340 ;;; determine what receives the value.
341 (defun use-lvar (node lvar)
342   (declare (type valued-node node) (type (or lvar null) lvar))
343   (aver (not (node-lvar node)))
344   (when lvar
345     (setf (node-lvar node) lvar)
346     (cond ((null (lvar-uses lvar))
347            (setf (lvar-uses lvar) node))
348           ((listp (lvar-uses lvar))
349            (aver (not (memq node (lvar-uses lvar))))
350            (push node (lvar-uses lvar)))
351           (t
352            (aver (neq node (lvar-uses lvar)))
353            (setf (lvar-uses lvar) (list node (lvar-uses lvar)))))
354     (reoptimize-lvar lvar)))
355
356 #!-sb-fluid(declaim (inline use-continuation))
357 (defun use-continuation (node ctran lvar)
358   (use-ctran node ctran)
359   (use-lvar node lvar))
360 \f
361 ;;;; exported functions
362
363 ;;; This function takes a form and the top level form number for that
364 ;;; form, and returns a lambda representing the translation of that
365 ;;; form in the current global environment. The returned lambda is a
366 ;;; top level lambda that can be called to cause evaluation of the
367 ;;; forms. This lambda is in the initial component. If FOR-VALUE is T,
368 ;;; then the value of the form is returned from the function,
369 ;;; otherwise NIL is returned.
370 ;;;
371 ;;; This function may have arbitrary effects on the global environment
372 ;;; due to processing of EVAL-WHENs. All syntax error checking is
373 ;;; done, with erroneous forms being replaced by a proxy which signals
374 ;;; an error if it is evaluated. Warnings about possibly inconsistent
375 ;;; or illegal changes to the global environment will also be given.
376 ;;;
377 ;;; We make the initial component and convert the form in a PROGN (and
378 ;;; an optional NIL tacked on the end.) We then return the lambda. We
379 ;;; bind all of our state variables here, rather than relying on the
380 ;;; global value (if any) so that IR1 conversion will be reentrant.
381 ;;; This is necessary for EVAL-WHEN processing, etc.
382 ;;;
383 ;;; The hashtables used to hold global namespace info must be
384 ;;; reallocated elsewhere. Note also that *LEXENV* is not bound, so
385 ;;; that local macro definitions can be introduced by enclosing code.
386 (defun ir1-toplevel (form path for-value)
387   (declare (list path))
388   (let* ((*current-path* path)
389          (component (make-empty-component))
390          (*current-component* component)
391          (*allow-instrumenting* t))
392     (setf (component-name component) 'initial-component)
393     (setf (component-kind component) :initial)
394     (let* ((forms (if for-value `(,form) `(,form nil)))
395            (res (ir1-convert-lambda-body
396                  forms ()
397                  :debug-name (debug-name 'top-level-form form))))
398       (setf (functional-entry-fun res) res
399             (functional-arg-documentation res) ()
400             (functional-kind res) :toplevel)
401       res)))
402
403 ;;; *CURRENT-FORM-NUMBER* is used in FIND-SOURCE-PATHS to compute the
404 ;;; form number to associate with a source path. This should be bound
405 ;;; to an initial value of 0 before the processing of each truly
406 ;;; top level form.
407 (declaim (type index *current-form-number*))
408 (defvar *current-form-number*)
409
410 ;;; This function is called on freshly read forms to record the
411 ;;; initial location of each form (and subform.) Form is the form to
412 ;;; find the paths in, and TLF-NUM is the top level form number of the
413 ;;; truly top level form.
414 ;;;
415 ;;; This gets a bit interesting when the source code is circular. This
416 ;;; can (reasonably?) happen in the case of circular list constants.
417 (defun find-source-paths (form tlf-num)
418   (declare (type index tlf-num))
419   (let ((*current-form-number* 0))
420     (sub-find-source-paths form (list tlf-num)))
421   (values))
422 (defun sub-find-source-paths (form path)
423   (unless (gethash form *source-paths*)
424     (setf (gethash form *source-paths*)
425           (list* 'original-source-start *current-form-number* path))
426     (incf *current-form-number*)
427     (let ((pos 0)
428           (subform form)
429           (trail form))
430       (declare (fixnum pos))
431       (macrolet ((frob ()
432                    '(progn
433                       (when (atom subform) (return))
434                       (let ((fm (car subform)))
435                         (when (consp fm)
436                           (sub-find-source-paths fm (cons pos path)))
437                         (incf pos))
438                       (setq subform (cdr subform))
439                       (when (eq subform trail) (return)))))
440         (loop
441           (frob)
442           (frob)
443           (setq trail (cdr trail)))))))
444 \f
445 ;;;; IR1-CONVERT, macroexpansion and special form dispatching
446
447 (declaim (ftype (sfunction (ctran ctran (or lvar null) t) (values))
448                 ir1-convert))
449 (macrolet (;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws
450            ;; out of the body and converts a condition signalling form
451            ;; instead. The source form is converted to a string since it
452            ;; may contain arbitrary non-externalizable objects.
453            (ir1-error-bailout ((start next result form) &body body)
454              (with-unique-names (skip condition)
455                `(block ,skip
456                  (let ((,condition (catch 'ir1-error-abort
457                                      (let ((*compiler-error-bailout*
458                                             (lambda (&optional e)
459                                               (throw 'ir1-error-abort e))))
460                                        ,@body
461                                        (return-from ,skip nil)))))
462                    (ir1-convert ,start ,next ,result
463                                 (make-compiler-error-form ,condition ,form)))))))
464
465   ;; Translate FORM into IR1. The code is inserted as the NEXT of the
466   ;; CTRAN START. RESULT is the LVAR which receives the value of the
467   ;; FORM to be translated. The translators call this function
468   ;; recursively to translate their subnodes.
469   ;;
470   ;; As a special hack to make life easier in the compiler, a LEAF
471   ;; IR1-converts into a reference to that LEAF structure. This allows
472   ;; the creation using backquote of forms that contain leaf
473   ;; references, without having to introduce dummy names into the
474   ;; namespace.
475   (defun ir1-convert (start next result form)
476     (ir1-error-bailout (start next result form)
477       (let ((*current-path* (or (gethash form *source-paths*)
478                                 (cons form *current-path*))))
479         (cond ((step-form-p form)
480                (ir1-convert-step start next result form))
481               ((atom form)
482                (cond ((and (symbolp form) (not (keywordp form)))
483                       (ir1-convert-var start next result form))
484                      ((leaf-p form)
485                       (reference-leaf start next result form))
486                      (t
487                       (reference-constant start next result form))))
488               (t
489                (let ((opname (car form)))
490                  (cond ((or (symbolp opname) (leaf-p opname))
491                         (let ((lexical-def (if (leaf-p opname)
492                                                opname
493                                                (lexenv-find opname funs))))
494                           (typecase lexical-def
495                             (null
496                              (ir1-convert-global-functoid start next result
497                                                           form))
498                             (functional
499                              (ir1-convert-local-combination start next result
500                                                             form
501                                                             lexical-def))
502                             (global-var
503                              (ir1-convert-srctran start next result
504                                                   lexical-def form))
505                             (t
506                              (aver (and (consp lexical-def)
507                                         (eq (car lexical-def) 'macro)))
508                              (ir1-convert start next result
509                                           (careful-expand-macro (cdr lexical-def)
510                                                                 form))))))
511                        ((or (atom opname) (not (eq (car opname) 'lambda)))
512                         (compiler-error "illegal function call"))
513                        (t
514                         ;; implicitly (LAMBDA ..) because the LAMBDA
515                         ;; expression is the CAR of an executed form
516                         (ir1-convert-combination start next result
517                                                  form
518                                                  (ir1-convert-lambda
519                                                   opname
520                                                   :debug-name (debug-name
521                                                                'lambda-car 
522                                                                opname))))))))))
523     (values))
524   
525   ;; Generate a reference to a manifest constant, creating a new leaf
526   ;; if necessary. If we are producing a fasl file, make sure that
527   ;; MAKE-LOAD-FORM gets used on any parts of the constant that it
528   ;; needs to be.
529   (defun reference-constant (start next result value)
530     (declare (type ctran start next)
531              (type (or lvar null) result)
532              (inline find-constant))
533     (ir1-error-bailout (start next result value)
534      (when (producing-fasl-file)
535        (maybe-emit-make-load-forms value))
536      (let* ((leaf (find-constant value))
537             (res (make-ref leaf)))
538        (push res (leaf-refs leaf))
539        (link-node-to-previous-ctran res start)
540        (use-continuation res next result)))
541     (values)))
542
543 ;;; Add FUNCTIONAL to the COMPONENT-REANALYZE-FUNCTIONALS, unless it's
544 ;;; some trivial type for which reanalysis is a trivial no-op, or
545 ;;; unless it doesn't belong in this component at all.
546 ;;;
547 ;;; FUNCTIONAL is returned.
548 (defun maybe-reanalyze-functional (functional)
549
550   (aver (not (eql (functional-kind functional) :deleted))) ; bug 148
551   (aver-live-component *current-component*)
552
553   ;; When FUNCTIONAL is of a type for which reanalysis isn't a trivial
554   ;; no-op
555   (when (typep functional '(or optional-dispatch clambda))
556
557     ;; When FUNCTIONAL knows its component
558     (when (lambda-p functional)
559       (aver (eql (lambda-component functional) *current-component*)))
560
561     (pushnew functional
562              (component-reanalyze-functionals *current-component*)))
563
564   functional)
565
566 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
567 ;;; needed. If LEAF represents a defined function which has already
568 ;;; been converted, and is not :NOTINLINE, then reference the
569 ;;; functional instead.
570 (defun reference-leaf (start next result leaf)
571   (declare (type ctran start next) (type (or lvar null) result) (type leaf leaf))
572   (when (functional-p leaf)
573     (assure-functional-live-p leaf))
574   (let* ((type (lexenv-find leaf type-restrictions))
575          (leaf (or (and (defined-fun-p leaf)
576                         (not (eq (defined-fun-inlinep leaf)
577                                  :notinline))
578                         (let ((functional (defined-fun-functional leaf)))
579                           (when (and functional
580                                      (not (functional-kind functional))
581                                      ;; Bug MISC.320: ir1-transform
582                                      ;; can create a reference to a
583                                      ;; inline-expanded function,
584                                      ;; defined in another component.
585                                      (not (and (lambda-p functional)
586                                                (neq (lambda-component functional)
587                                                     *current-component*))))
588                             (maybe-reanalyze-functional functional))))
589                    (when (and (lambda-p leaf)
590                               (memq (functional-kind leaf)
591                                     '(nil :optional)))
592                      (maybe-reanalyze-functional leaf))
593                    leaf))
594          (ref (make-ref leaf)))
595     (push ref (leaf-refs leaf))
596     (setf (leaf-ever-used leaf) t)
597     (link-node-to-previous-ctran ref start)
598     (cond (type (let* ((ref-ctran (make-ctran))
599                        (ref-lvar (make-lvar))
600                        (cast (make-cast ref-lvar
601                                         (make-single-value-type type)
602                                         (lexenv-policy *lexenv*))))
603                   (setf (lvar-dest ref-lvar) cast)
604                   (use-continuation ref ref-ctran ref-lvar)
605                   (link-node-to-previous-ctran cast ref-ctran)
606                   (use-continuation cast next result)))
607           (t (use-continuation ref next result)))))
608
609 ;;; Convert a reference to a symbolic constant or variable. If the
610 ;;; symbol is entered in the LEXENV-VARS we use that definition,
611 ;;; otherwise we find the current global definition. This is also
612 ;;; where we pick off symbol macro and alien variable references.
613 (defun ir1-convert-var (start next result name)
614   (declare (type ctran start next) (type (or lvar null) result) (symbol name))
615   (let ((var (or (lexenv-find name vars) (find-free-var name))))
616     (etypecase var
617       (leaf
618        (when (lambda-var-p var)
619          (let ((home (ctran-home-lambda-or-null start)))
620            (when home
621              (pushnew var (lambda-calls-or-closes home))))
622          (when (lambda-var-ignorep var)
623            ;; (ANSI's specification for the IGNORE declaration requires
624            ;; that this be a STYLE-WARNING, not a full WARNING.)
625            #-sb-xc-host
626            (compiler-style-warn "reading an ignored variable: ~S" name)
627            ;; there's no need for us to accept ANSI's lameness when
628            ;; processing our own code, though.
629            #+sb-xc-host
630            (warn "reading an ignored variable: ~S" name)))
631        (reference-leaf start next result var))
632       (cons
633        (aver (eq (car var) 'MACRO))
634        ;; FIXME: [Free] type declarations. -- APD, 2002-01-26
635        (ir1-convert start next result (cdr var)))
636       (heap-alien-info
637        (ir1-convert start next result `(%heap-alien ',var)))))
638   (values))
639
640 ;;; Convert anything that looks like a special form, global function
641 ;;; or compiler-macro call.
642 (defun ir1-convert-global-functoid (start next result form)
643   (declare (type ctran start next) (type (or lvar null) result) (list form))
644   (let* ((fun-name (first form))
645          (translator (info :function :ir1-convert fun-name))
646          (cmacro-fun (sb!xc:compiler-macro-function fun-name *lexenv*)))
647     (cond (translator
648            (when cmacro-fun
649              (compiler-warn "ignoring compiler macro for special form"))
650            (funcall translator start next result form))
651           ((and cmacro-fun
652                 ;; gotcha: If you look up the DEFINE-COMPILER-MACRO
653                 ;; macro in the ANSI spec, you might think that
654                 ;; suppressing compiler-macro expansion when NOTINLINE
655                 ;; is some pre-ANSI hack. However, if you look up the
656                 ;; NOTINLINE declaration, you'll find that ANSI
657                 ;; requires this behavior after all.
658                 (not (eq (info :function :inlinep fun-name) :notinline)))
659            (let ((res (careful-expand-macro cmacro-fun form)))
660              (if (eq res form)
661                  (ir1-convert-global-functoid-no-cmacro
662                   start next result form fun-name)
663                  (ir1-convert start next result res))))
664           (t
665            (ir1-convert-global-functoid-no-cmacro start next result
666                                                   form fun-name)))))
667
668 ;;; Handle the case of where the call was not a compiler macro, or was
669 ;;; a compiler macro and passed.
670 (defun ir1-convert-global-functoid-no-cmacro (start next result form fun)
671   (declare (type ctran start next) (type (or lvar null) result)
672            (list form))
673   ;; FIXME: Couldn't all the INFO calls here be converted into
674   ;; standard CL functions, like MACRO-FUNCTION or something?
675   ;; And what happens with lexically-defined (MACROLET) macros
676   ;; here, anyway?
677   (ecase (info :function :kind fun)
678     (:macro
679      (ir1-convert start next result
680                   (careful-expand-macro (info :function :macro-function fun)
681                                         form)))
682     ((nil :function)
683      (ir1-convert-srctran start next result
684                           (find-free-fun fun "shouldn't happen! (no-cmacro)")
685                           form))))
686
687 (defun muffle-warning-or-die ()
688   (muffle-warning)
689   (bug "no MUFFLE-WARNING restart"))
690
691 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
692 ;;; errors which occur during the macroexpansion.
693 (defun careful-expand-macro (fun form)
694   (let (;; a hint I (WHN) wish I'd known earlier
695         (hint "(hint: For more precise location, try *BREAK-ON-SIGNALS*.)"))
696     (flet (;; Return a string to use as a prefix in error reporting,
697            ;; telling something about which form caused the problem.
698            (wherestring ()
699              (let ((*print-pretty* nil)
700                    ;; We rely on the printer to abbreviate FORM. 
701                    (*print-length* 3)
702                    (*print-level* 1))
703                (format
704                 nil
705                 #-sb-xc-host "(in macroexpansion of ~S)"
706                 ;; longer message to avoid ambiguity "Was it the xc host
707                 ;; or the cross-compiler which encountered the problem?"
708                 #+sb-xc-host "(in cross-compiler macroexpansion of ~S)"
709                 form))))
710       (handler-bind ((style-warning (lambda (c)
711                                       (compiler-style-warn
712                                        "~@<~A~:@_~A~@:_~A~:>"
713                                        (wherestring) hint c)
714                                       (muffle-warning-or-die)))
715                      ;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for
716                      ;; Debian Linux, anyway) raises a CL:WARNING
717                      ;; condition (not a CL:STYLE-WARNING) for undefined
718                      ;; symbols when converting interpreted functions,
719                      ;; causing COMPILE-FILE to think the file has a real
720                      ;; problem, causing COMPILE-FILE to return FAILURE-P
721                      ;; set (not just WARNINGS-P set). Since undefined
722                      ;; symbol warnings are often harmless forward
723                      ;; references, and since it'd be inordinately painful
724                      ;; to try to eliminate all such forward references,
725                      ;; these warnings are basically unavoidable. Thus, we
726                      ;; need to coerce the system to work through them,
727                      ;; and this code does so, by crudely suppressing all
728                      ;; warnings in cross-compilation macroexpansion. --
729                      ;; WHN 19990412
730                      #+(and cmu sb-xc-host)
731                      (warning (lambda (c)
732                                 (compiler-notify
733                                  "~@<~A~:@_~
734                                   ~A~:@_~
735                                   ~@<(KLUDGE: That was a non-STYLE WARNING. ~
736                                   Ordinarily that would cause compilation to ~
737                                   fail. However, since we're running under ~
738                                   CMU CL, and since CMU CL emits non-STYLE ~
739                                   warnings for safe, hard-to-fix things (e.g. ~
740                                   references to not-yet-defined functions) ~
741                                   we're going to have to ignore it and ~
742                                   proceed anyway. Hopefully we're not ~
743                                   ignoring anything  horrible here..)~:@>~:>"
744                                  (wherestring)
745                                  c)
746                                 (muffle-warning-or-die)))
747                      #-(and cmu sb-xc-host)
748                      (warning (lambda (c)
749                                 (warn "~@<~A~:@_~A~@:_~A~:>"
750                                       (wherestring) hint c)
751                                 (muffle-warning-or-die)))
752                      (error (lambda (c)
753                               (compiler-error "~@<~A~:@_~A~@:_~A~:>"
754                                               (wherestring) hint c))))
755         (funcall sb!xc:*macroexpand-hook* fun form *lexenv*)))))
756 \f
757 ;;;; conversion utilities
758
759 ;;; Convert a bunch of forms, discarding all the values except the
760 ;;; last. If there aren't any forms, then translate a NIL.
761 (declaim (ftype (sfunction (ctran ctran (or lvar null) list) (values))
762                 ir1-convert-progn-body))
763 (defun ir1-convert-progn-body (start next result body)
764   (if (endp body)
765       (reference-constant start next result nil)
766       (let ((this-start start)
767             (forms body))
768         (loop
769           (let ((form (car forms)))
770             (when (endp (cdr forms))
771               (ir1-convert this-start next result form)
772               (return))
773             (let ((this-ctran (make-ctran)))
774               (ir1-convert this-start this-ctran nil form)
775               (setq this-start this-ctran
776                     forms (cdr forms)))))))
777   (values))
778 \f
779 ;;;; converting combinations
780
781 ;;; Convert a function call where the function FUN is a LEAF. FORM is
782 ;;; the source for the call. We return the COMBINATION node so that
783 ;;; the caller can poke at it if it wants to.
784 (declaim (ftype (sfunction (ctran ctran (or lvar null) list leaf) combination)
785                 ir1-convert-combination))
786 (defun ir1-convert-combination (start next result form fun)
787   (let ((ctran (make-ctran))
788         (fun-lvar (make-lvar)))
789     (ir1-convert start ctran fun-lvar `(the (or function symbol) ,fun))
790     (ir1-convert-combination-args fun-lvar ctran next result (cdr form))))
791
792 ;;; Convert the arguments to a call and make the COMBINATION
793 ;;; node. FUN-LVAR yields the function to call. ARGS is the list of
794 ;;; arguments for the call, which defaults to the cdr of source. We
795 ;;; return the COMBINATION node.
796 (defun ir1-convert-combination-args (fun-lvar start next result args)
797   (declare (type ctran start next)
798            (type lvar fun-lvar)
799            (type (or lvar null) result)
800            (list args))
801   (let ((node (make-combination fun-lvar)))
802     (setf (lvar-dest fun-lvar) node)
803     (collect ((arg-lvars))
804       (let ((this-start start))
805         (dolist (arg args)
806           (let ((this-ctran (make-ctran))
807                 (this-lvar (make-lvar node)))
808             (ir1-convert this-start this-ctran this-lvar arg)
809             (setq this-start this-ctran)
810             (arg-lvars this-lvar)))
811         (link-node-to-previous-ctran node this-start)
812         (use-continuation node next result)
813         (setf (combination-args node) (arg-lvars))))
814     node))
815
816 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
817 ;;; source transforms and try out any inline expansion. If there is no
818 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
819 ;;; known function which will quite possibly be open-coded.) Next, we
820 ;;; go to ok-combination conversion.
821 (defun ir1-convert-srctran (start next result var form)
822   (declare (type ctran start next) (type (or lvar null) result)
823            (type global-var var))
824   (let ((inlinep (when (defined-fun-p var)
825                    (defined-fun-inlinep var))))
826     (if (eq inlinep :notinline)
827         (ir1-convert-combination start next result form var)
828         (let ((transform (info :function
829                                :source-transform
830                                (leaf-source-name var))))
831           (if transform
832               (multiple-value-bind (transformed pass) (funcall transform form)
833                 (if pass
834                     (ir1-convert-maybe-predicate start next result form var)
835                     (ir1-convert start next result transformed)))
836               (ir1-convert-maybe-predicate start next result form var))))))
837
838 ;;; If the function has the PREDICATE attribute, and the RESULT's DEST
839 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
840 ;;; predicate always appears in a conditional context.
841 ;;;
842 ;;; If the function isn't a predicate, then we call
843 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
844 (defun ir1-convert-maybe-predicate (start next result form var)
845   (declare (type ctran start next)
846            (type (or lvar null) result)
847            (list form)
848            (type global-var var))
849   (let ((info (info :function :info (leaf-source-name var))))
850     (if (and info
851              (ir1-attributep (fun-info-attributes info) predicate)
852              (not (if-p (and result (lvar-dest result)))))
853         (ir1-convert start next result `(if ,form t nil))
854         (ir1-convert-combination-checking-type start next result form var))))
855
856 ;;; Actually really convert a global function call that we are allowed
857 ;;; to early-bind.
858 ;;;
859 ;;; If we know the function type of the function, then we check the
860 ;;; call for syntactic legality with respect to the declared function
861 ;;; type. If it is impossible to determine whether the call is correct
862 ;;; due to non-constant keywords, then we give up, marking the call as
863 ;;; :FULL to inhibit further error messages. We return true when the
864 ;;; call is legal.
865 ;;;
866 ;;; If the call is legal, we also propagate type assertions from the
867 ;;; function type to the arg and result lvars. We do this now so that
868 ;;; IR1 optimize doesn't have to redundantly do the check later so
869 ;;; that it can do the type propagation.
870 (defun ir1-convert-combination-checking-type (start next result form var)
871   (declare (type ctran start next) (type (or lvar null) result)
872            (list form)
873            (type leaf var))
874   (let* ((node (ir1-convert-combination start next result form var))
875          (fun-lvar (basic-combination-fun node))
876          (type (leaf-type var)))
877     (when (validate-call-type node type t)
878       (setf (lvar-%derived-type fun-lvar)
879             (make-single-value-type type))
880       (setf (lvar-reoptimize fun-lvar) nil)))
881   (values))
882
883 ;;; Convert a call to a local function, or if the function has already
884 ;;; been LET converted, then throw FUNCTIONAL to
885 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
886 ;;; are converting inline expansions for local functions during
887 ;;; optimization.
888 (defun ir1-convert-local-combination (start next result form functional)
889   (assure-functional-live-p functional)
890   (ir1-convert-combination start next result
891                            form
892                            (maybe-reanalyze-functional functional)))
893 \f
894 ;;;; PROCESS-DECLS
895
896 ;;; Given a list of LAMBDA-VARs and a variable name, return the
897 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
898 ;;; *last* variable with that name, since LET* bindings may be
899 ;;; duplicated, and declarations always apply to the last.
900 (declaim (ftype (sfunction (list symbol) (or lambda-var list))
901                 find-in-bindings))
902 (defun find-in-bindings (vars name)
903   (let ((found nil))
904     (dolist (var vars)
905       (cond ((leaf-p var)
906              (when (eq (leaf-source-name var) name)
907                (setq found var))
908              (let ((info (lambda-var-arg-info var)))
909                (when info
910                  (let ((supplied-p (arg-info-supplied-p info)))
911                    (when (and supplied-p
912                               (eq (leaf-source-name supplied-p) name))
913                      (setq found supplied-p))))))
914             ((and (consp var) (eq (car var) name))
915              (setf found (cdr var)))))
916     found))
917
918 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
919 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
920 ;;; type, otherwise we add a type restriction on the var. If a symbol
921 ;;; macro, we just wrap a THE around the expansion.
922 (defun process-type-decl (decl res vars)
923   (declare (list decl vars) (type lexenv res))
924   (let ((type (compiler-specifier-type (first decl))))
925     (collect ((restr nil cons)
926              (new-vars nil cons))
927       (dolist (var-name (rest decl))
928         (when (boundp var-name)
929           (compiler-assert-symbol-home-package-unlocked
930            var-name "declaring the type of ~A"))
931         (let* ((bound-var (find-in-bindings vars var-name))
932                (var (or bound-var
933                         (lexenv-find var-name vars)
934                         (find-free-var var-name))))
935           (etypecase var
936             (leaf
937              (flet 
938                  ((process-var (var bound-var)
939                     (let* ((old-type (or (lexenv-find var type-restrictions)
940                                          (leaf-type var)))
941                            (int (if (or (fun-type-p type)
942                                         (fun-type-p old-type))
943                                     type
944                                     (type-approx-intersection2 
945                                      old-type type))))
946                       (cond ((eq int *empty-type*)
947                              (unless (policy *lexenv* (= inhibit-warnings 3))
948                                (warn
949                                 'type-warning
950                                 :format-control
951                                 "The type declarations ~S and ~S for ~S conflict."
952                                 :format-arguments
953                                 (list
954                                  (type-specifier old-type) 
955                                  (type-specifier type)
956                                  var-name))))
957                             (bound-var (setf (leaf-type bound-var) int))
958                             (t
959                              (restr (cons var int)))))))
960                (process-var var bound-var)
961                (awhen (and (lambda-var-p var)
962                            (lambda-var-specvar var))
963                       (process-var it nil))))
964             (cons
965              ;; FIXME: non-ANSI weirdness
966              (aver (eq (car var) 'MACRO))
967              (new-vars `(,var-name . (MACRO . (the ,(first decl)
968                                                 ,(cdr var))))))
969             (heap-alien-info
970              (compiler-error
971               "~S is an alien variable, so its type can't be declared."
972               var-name)))))
973
974       (if (or (restr) (new-vars))
975           (make-lexenv :default res
976                        :type-restrictions (restr)
977                        :vars (new-vars))
978           res))))
979
980 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
981 ;;; declarations for function variables. In addition to allowing
982 ;;; declarations for functions being bound, we must also deal with
983 ;;; declarations that constrain the type of lexically apparent
984 ;;; functions.
985 (defun process-ftype-decl (spec res names fvars)
986   (declare (type list names fvars)
987            (type lexenv res))
988   (let ((type (compiler-specifier-type spec)))
989     (collect ((res nil cons))
990       (dolist (name names)
991         (when (fboundp name)
992           (compiler-assert-symbol-home-package-unlocked 
993            name "declaring the ftype of ~A"))
994         (let ((found (find name fvars :key #'leaf-source-name :test #'equal)))
995           (cond
996            (found
997             (setf (leaf-type found) type)
998             (assert-definition-type found type
999                                     :unwinnage-fun #'compiler-notify
1000                                     :where "FTYPE declaration"))
1001            (t
1002             (res (cons (find-lexically-apparent-fun
1003                         name "in a function type declaration")
1004                        type))))))
1005       (if (res)
1006           (make-lexenv :default res :type-restrictions (res))
1007           res))))
1008
1009 ;;; Process a special declaration, returning a new LEXENV. A non-bound
1010 ;;; special declaration is instantiated by throwing a special variable
1011 ;;; into the variables if BINDING-FORM-P is NIL, or otherwise into
1012 ;;; *POST-BINDING-VARIABLE-LEXENV*. 
1013 (defun process-special-decl (spec res vars binding-form-p)
1014   (declare (list spec vars) (type lexenv res))
1015   (collect ((new-venv nil cons))
1016     (dolist (name (cdr spec))
1017       (compiler-assert-symbol-home-package-unlocked name "declaring ~A special")
1018       (let ((var (find-in-bindings vars name)))
1019         (etypecase var
1020           (cons
1021            (aver (eq (car var) 'MACRO))
1022            (compiler-error
1023             "~S is a symbol-macro and thus can't be declared special."
1024             name))
1025           (lambda-var
1026            (when (lambda-var-ignorep var)
1027              ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1028              ;; requires that this be a STYLE-WARNING, not a full WARNING.
1029              (compiler-style-warn
1030               "The ignored variable ~S is being declared special."
1031               name))
1032            (setf (lambda-var-specvar var)
1033                  (specvar-for-binding name)))
1034           (null
1035            (unless (or (assoc name (new-venv) :test #'eq))
1036              (new-venv (cons name (specvar-for-binding name))))))))
1037     (cond (binding-form-p
1038            (setf *post-binding-variable-lexenv*
1039                  (append (new-venv) *post-binding-variable-lexenv*))
1040            res)
1041           ((new-venv)
1042            (make-lexenv :default res :vars (new-venv)))
1043           (t
1044            res))))
1045
1046 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP
1047 ;;; (and TYPE if notinline), plus type-restrictions from the lexenv.
1048 (defun make-new-inlinep (var inlinep local-type)
1049   (declare (type global-var var) (type inlinep inlinep))
1050   (let* ((type (if (and (eq inlinep :notinline)
1051                         (not (eq (leaf-where-from var) :declared)))
1052                    (specifier-type 'function)
1053                    (leaf-type var)))
1054          (res (make-defined-fun
1055                :%source-name (leaf-source-name var)
1056                :where-from (leaf-where-from var)
1057                :type (if local-type 
1058                          (type-intersection local-type type)
1059                          type)
1060                :inlinep inlinep)))
1061     (when (defined-fun-p var)
1062       (setf (defined-fun-inline-expansion res)
1063             (defined-fun-inline-expansion var))
1064       (setf (defined-fun-functional res)
1065             (defined-fun-functional var)))
1066     res))
1067
1068 ;;; Parse an inline/notinline declaration. If it's a local function we're
1069 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1070 (defun process-inline-decl (spec res fvars)
1071   (let ((sense (cdr (assoc (first spec) *inlinep-translations* :test #'eq)))
1072         (new-fenv ()))
1073     (dolist (name (rest spec))
1074       (let ((fvar (find name fvars :key #'leaf-source-name :test #'equal)))
1075         (if fvar
1076             (setf (functional-inlinep fvar) sense)
1077             (let ((found (find-lexically-apparent-fun
1078                           name "in an inline or notinline declaration")))
1079               (etypecase found
1080                 (functional
1081                  (when (policy *lexenv* (>= speed inhibit-warnings))
1082                    (compiler-notify "ignoring ~A declaration not at ~
1083                                      definition of local function:~%  ~S"
1084                                     sense name)))
1085                 (global-var
1086                  (let ((type 
1087                         (cdr (assoc found (lexenv-type-restrictions res)))))
1088                    (push (cons name (make-new-inlinep found sense type))
1089                          new-fenv))))))))
1090     (if new-fenv
1091         (make-lexenv :default res :funs new-fenv)
1092         res)))
1093
1094 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1095 (defun find-in-bindings-or-fbindings (name vars fvars)
1096   (declare (list vars fvars))
1097   (if (consp name)
1098       (destructuring-bind (wot fn-name) name
1099         (unless (eq wot 'function)
1100           (compiler-error "The function or variable name ~S is unrecognizable."
1101                           name))
1102         (find fn-name fvars :key #'leaf-source-name :test #'equal))
1103       (find-in-bindings vars name)))
1104
1105 ;;; Process an ignore/ignorable declaration, checking for various losing
1106 ;;; conditions.
1107 (defun process-ignore-decl (spec vars fvars)
1108   (declare (list spec vars fvars))
1109   (dolist (name (rest spec))
1110     (let ((var (find-in-bindings-or-fbindings name vars fvars)))
1111       (cond
1112        ((not var)
1113         ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1114         ;; requires that this be a STYLE-WARNING, not a full WARNING.
1115         (compiler-style-warn "declaring unknown variable ~S to be ignored"
1116                              name))
1117        ;; FIXME: This special case looks like non-ANSI weirdness.
1118        ((and (consp var) (eq (car var) 'macro))
1119         ;; Just ignore the IGNORE decl.
1120         )
1121        ((functional-p var)
1122         (setf (leaf-ever-used var) t))
1123        ((and (lambda-var-specvar var) (eq (first spec) 'ignore))
1124         ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1125         ;; requires that this be a STYLE-WARNING, not a full WARNING.
1126         (compiler-style-warn "declaring special variable ~S to be ignored"
1127                              name))
1128        ((eq (first spec) 'ignorable)
1129         (setf (leaf-ever-used var) t))
1130        (t
1131         (setf (lambda-var-ignorep var) t)))))
1132   (values))
1133
1134 (defun process-dx-decl (names vars fvars)
1135   (flet ((maybe-notify (control &rest args)
1136            (when (policy *lexenv* (> speed inhibit-warnings))
1137              (apply #'compiler-notify control args))))
1138     (if (policy *lexenv* (= stack-allocate-dynamic-extent 3))
1139         (dolist (name names)
1140           (cond
1141             ((symbolp name)
1142              (let* ((bound-var (find-in-bindings vars name))
1143                     (var (or bound-var
1144                              (lexenv-find name vars)
1145                              (find-free-var name))))
1146                (etypecase var
1147                  (leaf
1148                   (if bound-var
1149                       (setf (leaf-dynamic-extent var) t)
1150                       (maybe-notify
1151                        "ignoring DYNAMIC-EXTENT declaration for free ~S"
1152                        name)))
1153                  (cons
1154                   (compiler-error "DYNAMIC-EXTENT on symbol-macro: ~S" name))
1155                  (heap-alien-info
1156                   (compiler-error "DYNAMIC-EXTENT on heap-alien-info: ~S"
1157                                   name)))))
1158             ((and (consp name)
1159                   (eq (car name) 'function)
1160                   (null (cddr name))
1161                   (valid-function-name-p (cadr name)))
1162              (let* ((fname (cadr name))
1163                     (bound-fun (find fname fvars
1164                                      :key #'leaf-source-name
1165                                      :test #'equal)))
1166                (etypecase bound-fun
1167                  (leaf
1168                   #!+stack-allocatable-closures
1169                   (setf (leaf-dynamic-extent bound-fun) t)
1170                   #!-stack-allocatable-closures
1171                   (maybe-notify
1172                    "ignoring DYNAMIC-EXTENT declaration on a function ~S ~
1173                     (not supported on this platform)." fname))
1174                  (cons
1175                   (compiler-error "DYNAMIC-EXTENT on macro: ~S" fname))
1176                  (null
1177                   (maybe-notify
1178                    "ignoring DYNAMIC-EXTENT declaration for free ~S"
1179                    fname)))))
1180             (t (compiler-error "DYNAMIC-EXTENT on a weird thing: ~S" name))))
1181       (maybe-notify "ignoring DYNAMIC-EXTENT declarations for ~S" names))))
1182
1183 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1184 ;;; go away, I think.
1185 (defvar *suppress-values-declaration* nil
1186   #!+sb-doc
1187   "If true, processing of the VALUES declaration is inhibited.")
1188
1189 ;;; Process a single declaration spec, augmenting the specified LEXENV
1190 ;;; RES. Return RES and result type. VARS and FVARS are as described
1191 ;;; PROCESS-DECLS.
1192 (defun process-1-decl (raw-spec res vars fvars binding-form-p)
1193   (declare (type list raw-spec vars fvars))
1194   (declare (type lexenv res))
1195   (let ((spec (canonized-decl-spec raw-spec))
1196         (result-type *wild-type*))
1197     (values
1198      (case (first spec)
1199        (special (process-special-decl spec res vars binding-form-p))
1200        (ftype
1201         (unless (cdr spec)
1202           (compiler-error "no type specified in FTYPE declaration: ~S" spec))
1203         (process-ftype-decl (second spec) res (cddr spec) fvars))
1204        ((inline notinline maybe-inline)
1205         (process-inline-decl spec res fvars))
1206        ((ignore ignorable)
1207         (process-ignore-decl spec vars fvars)
1208         res)
1209        (optimize
1210         (make-lexenv
1211          :default res
1212          :policy (process-optimize-decl spec (lexenv-policy res))))
1213        (muffle-conditions
1214         (make-lexenv
1215          :default res
1216          :handled-conditions (process-muffle-conditions-decl
1217                               spec (lexenv-handled-conditions res))))
1218        (unmuffle-conditions
1219         (make-lexenv
1220          :default res
1221          :handled-conditions (process-unmuffle-conditions-decl
1222                               spec (lexenv-handled-conditions res))))
1223        (type
1224         (process-type-decl (cdr spec) res vars))
1225        (values
1226         (unless *suppress-values-declaration*
1227           (let ((types (cdr spec)))
1228             (setq result-type
1229                   (compiler-values-specifier-type
1230                    (if (singleton-p types)
1231                        (car types)
1232                        `(values ,@types)))))
1233           res))
1234        (dynamic-extent
1235         (process-dx-decl (cdr spec) vars fvars)
1236         res)
1237        ((disable-package-locks enable-package-locks)
1238         (make-lexenv
1239          :default res
1240          :disabled-package-locks (process-package-lock-decl
1241                                   spec (lexenv-disabled-package-locks res))))
1242        (t
1243         (unless (info :declaration :recognized (first spec))
1244           (compiler-warn "unrecognized declaration ~S" raw-spec))
1245         res))
1246      result-type)))
1247
1248 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1249 ;;; and FUNCTIONAL structures which are being bound. In addition to
1250 ;;; filling in slots in the leaf structures, we return a new LEXENV,
1251 ;;; which reflects pervasive special and function type declarations,
1252 ;;; (NOT)INLINE declarations and OPTIMIZE declarations, and type of
1253 ;;; VALUES declarations. If BINDING-FORM-P is true, the third return
1254 ;;; value is a list of VARs that should not apply to the lexenv of the
1255 ;;; initialization forms for the bindings, but should apply to the body.
1256 ;;;
1257 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1258 ;;; of LOCALLY.
1259 (defun process-decls (decls vars fvars &key (lexenv *lexenv*)
1260                                             (binding-form-p nil))
1261   (declare (list decls vars fvars))
1262   (let ((result-type *wild-type*)
1263         (*post-binding-variable-lexenv* nil))
1264     (dolist (decl decls)
1265       (dolist (spec (rest decl))
1266         (unless (consp spec)
1267           (compiler-error "malformed declaration specifier ~S in ~S" spec decl))
1268         (multiple-value-bind (new-env new-result-type)
1269             (process-1-decl spec lexenv vars fvars binding-form-p)
1270           (setq lexenv new-env)
1271           (unless (eq new-result-type *wild-type*)
1272             (setq result-type
1273                   (values-type-intersection result-type new-result-type))))))
1274     (values lexenv result-type *post-binding-variable-lexenv*)))
1275
1276 (defun %processing-decls (decls vars fvars ctran lvar binding-form-p fun)
1277   (multiple-value-bind (*lexenv* result-type post-binding-lexenv)
1278       (process-decls decls vars fvars :binding-form-p binding-form-p)
1279     (cond ((eq result-type *wild-type*)
1280            (funcall fun ctran lvar post-binding-lexenv))
1281           (t
1282            (let ((value-ctran (make-ctran))
1283                  (value-lvar (make-lvar)))
1284              (multiple-value-prog1
1285                  (funcall fun value-ctran value-lvar post-binding-lexenv)
1286                (let ((cast (make-cast value-lvar result-type
1287                                       (lexenv-policy *lexenv*))))
1288                  (link-node-to-previous-ctran cast value-ctran)
1289                  (setf (lvar-dest value-lvar) cast)
1290                  (use-continuation cast ctran lvar))))))))
1291 (defmacro processing-decls ((decls vars fvars ctran lvar
1292                                    &optional post-binding-lexenv)
1293                             &body forms)
1294   (check-type ctran symbol)
1295   (check-type lvar symbol)
1296   (let ((post-binding-lexenv-p (not (null post-binding-lexenv)))
1297         (post-binding-lexenv (or post-binding-lexenv (gensym))))
1298     `(%processing-decls ,decls ,vars ,fvars ,ctran ,lvar
1299                         ,post-binding-lexenv-p
1300                         (lambda (,ctran ,lvar ,post-binding-lexenv)
1301                           (declare (ignorable ,post-binding-lexenv))
1302                           ,@forms))))
1303
1304 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1305 ;;; declaration. If there is a global variable of that name, then
1306 ;;; check that it isn't a constant and return it. Otherwise, create an
1307 ;;; anonymous GLOBAL-VAR.
1308 (defun specvar-for-binding (name)
1309   (cond ((not (eq (info :variable :where-from name) :assumed))
1310          (let ((found (find-free-var name)))
1311            (when (heap-alien-info-p found)
1312              (compiler-error
1313               "~S is an alien variable and so can't be declared special."
1314               name))
1315            (unless (global-var-p found)
1316              (compiler-error
1317               "~S is a constant and so can't be declared special."
1318               name))
1319            found))
1320         (t
1321          (make-global-var :kind :special
1322                           :%source-name name
1323                           :where-from :declared))))