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