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