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