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