0.8.0.31:
[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                    (when (and (lambda-p leaf)
554                               (memq (functional-kind leaf)
555                                     '(nil :optional)))
556                      (maybe-reanalyze-functional leaf))
557                    leaf))
558          (ref (make-ref leaf)))
559     (push ref (leaf-refs leaf))
560     (setf (leaf-ever-used leaf) t)
561     (link-node-to-previous-continuation ref start)
562     (cond (type (let* ((ref-cont (make-continuation))
563                        (cast (make-cast ref-cont
564                                         (make-single-value-type type)
565                                         (lexenv-policy *lexenv*))))
566                   (setf (continuation-dest ref-cont) cast)
567                   (use-continuation ref ref-cont)
568                   (link-node-to-previous-continuation cast ref-cont)
569                   (use-continuation cast cont)))
570           (t (use-continuation ref cont)))))
571
572 ;;; Convert a reference to a symbolic constant or variable. If the
573 ;;; symbol is entered in the LEXENV-VARS we use that definition,
574 ;;; otherwise we find the current global definition. This is also
575 ;;; where we pick off symbol macro and alien variable references.
576 (defun ir1-convert-var (start cont name)
577   (declare (type continuation start cont) (symbol name))
578   (let ((var (or (lexenv-find name vars) (find-free-var name))))
579     (etypecase var
580       (leaf
581        (when (lambda-var-p var)
582          (let ((home (continuation-home-lambda-or-null start)))
583            (when home
584              (pushnew var (lambda-calls-or-closes home))))
585          (when (lambda-var-ignorep var)
586            ;; (ANSI's specification for the IGNORE declaration requires
587            ;; that this be a STYLE-WARNING, not a full WARNING.)
588            (compiler-style-warn "reading an ignored variable: ~S" name)))
589        (reference-leaf start cont var))
590       (cons
591        (aver (eq (car var) 'MACRO))
592        ;; FIXME: [Free] type declarations. -- APD, 2002-01-26
593        (ir1-convert start cont (cdr var)))
594       (heap-alien-info
595        (ir1-convert start cont `(%heap-alien ',var)))))
596   (values))
597
598 ;;; Convert anything that looks like a special form, global function
599 ;;; or compiler-macro call.
600 (defun ir1-convert-global-functoid (start cont form)
601   (declare (type continuation start cont) (list form))
602   (let* ((fun-name (first form))
603          (translator (info :function :ir1-convert fun-name))
604          (cmacro-fun (sb!xc:compiler-macro-function fun-name *lexenv*)))
605     (cond (translator
606            (when cmacro-fun
607              (compiler-warn "ignoring compiler macro for special form"))
608            (funcall translator start cont form))
609           ((and cmacro-fun
610                 ;; gotcha: If you look up the DEFINE-COMPILER-MACRO
611                 ;; macro in the ANSI spec, you might think that
612                 ;; suppressing compiler-macro expansion when NOTINLINE
613                 ;; is some pre-ANSI hack. However, if you look up the
614                 ;; NOTINLINE declaration, you'll find that ANSI
615                 ;; requires this behavior after all.
616                 (not (eq (info :function :inlinep fun-name) :notinline)))
617            (let ((res (careful-expand-macro cmacro-fun form)))
618              (if (eq res form)
619                  (ir1-convert-global-functoid-no-cmacro
620                   start cont form fun-name)
621                  (ir1-convert start cont res))))
622           (t
623            (ir1-convert-global-functoid-no-cmacro start cont form fun-name)))))
624
625 ;;; Handle the case of where the call was not a compiler macro, or was
626 ;;; a compiler macro and passed.
627 (defun ir1-convert-global-functoid-no-cmacro (start cont form fun)
628   (declare (type continuation start cont) (list form))
629   ;; FIXME: Couldn't all the INFO calls here be converted into
630   ;; standard CL functions, like MACRO-FUNCTION or something?
631   ;; And what happens with lexically-defined (MACROLET) macros
632   ;; here, anyway?
633   (ecase (info :function :kind fun)
634     (:macro
635      (ir1-convert start
636                   cont
637                   (careful-expand-macro (info :function :macro-function fun)
638                                         form)))
639     ((nil :function)
640      (ir1-convert-srctran start
641                           cont
642                           (find-free-fun fun "shouldn't happen! (no-cmacro)")
643                           form))))
644
645 (defun muffle-warning-or-die ()
646   (muffle-warning)
647   (bug "no MUFFLE-WARNING restart"))
648
649 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
650 ;;; errors which occur during the macroexpansion.
651 (defun careful-expand-macro (fun form)
652   (let (;; a hint I (WHN) wish I'd known earlier
653         (hint "(hint: For more precise location, try *BREAK-ON-SIGNALS*.)"))
654     (flet (;; Return a string to use as a prefix in error reporting,
655            ;; telling something about which form caused the problem.
656            (wherestring ()
657              (let ((*print-pretty* nil)
658                    ;; We rely on the printer to abbreviate FORM. 
659                    (*print-length* 3)
660                    (*print-level* 1))
661                (format
662                 nil
663                 #-sb-xc-host "(in macroexpansion of ~S)"
664                 ;; longer message to avoid ambiguity "Was it the xc host
665                 ;; or the cross-compiler which encountered the problem?"
666                 #+sb-xc-host "(in cross-compiler macroexpansion of ~S)"
667                 form))))
668       (handler-bind ((style-warning (lambda (c)
669                                       (compiler-style-warn
670                                        "~@<~A~:@_~A~@:_~A~:>"
671                                        (wherestring) hint c)
672                                       (muffle-warning-or-die)))
673                      ;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for
674                      ;; Debian Linux, anyway) raises a CL:WARNING
675                      ;; condition (not a CL:STYLE-WARNING) for undefined
676                      ;; symbols when converting interpreted functions,
677                      ;; causing COMPILE-FILE to think the file has a real
678                      ;; problem, causing COMPILE-FILE to return FAILURE-P
679                      ;; set (not just WARNINGS-P set). Since undefined
680                      ;; symbol warnings are often harmless forward
681                      ;; references, and since it'd be inordinately painful
682                      ;; to try to eliminate all such forward references,
683                      ;; these warnings are basically unavoidable. Thus, we
684                      ;; need to coerce the system to work through them,
685                      ;; and this code does so, by crudely suppressing all
686                      ;; warnings in cross-compilation macroexpansion. --
687                      ;; WHN 19990412
688                      #+(and cmu sb-xc-host)
689                      (warning (lambda (c)
690                                 (compiler-note
691                                  "~@<~A~:@_~
692                                   ~A~:@_~
693                                   ~@<(KLUDGE: That was a non-STYLE WARNING. ~
694                                   Ordinarily that would cause compilation to ~
695                                   fail. However, since we're running under ~
696                                   CMU CL, and since CMU CL emits non-STYLE ~
697                                   warnings for safe, hard-to-fix things (e.g. ~
698                                   references to not-yet-defined functions) ~
699                                   we're going to have to ignore it and ~
700                                   proceed anyway. Hopefully we're not ~
701                                   ignoring anything  horrible here..)~:@>~:>"
702                                  (wherestring)
703                                  c)
704                                 (muffle-warning-or-die)))
705                      #-(and cmu sb-xc-host)
706                      (warning (lambda (c)
707                                 (compiler-warn "~@<~A~:@_~A~@:_~A~:>"
708                                                (wherestring) hint c)
709                                 (muffle-warning-or-die)))
710                      (error (lambda (c)
711                               (compiler-error "~@<~A~:@_~A~@:_~A~:>"
712                                               (wherestring) hint c))))
713         (funcall sb!xc:*macroexpand-hook* fun form *lexenv*)))))
714 \f
715 ;;;; conversion utilities
716
717 ;;; Convert a bunch of forms, discarding all the values except the
718 ;;; last. If there aren't any forms, then translate a NIL.
719 (declaim (ftype (function (continuation continuation list) (values))
720                 ir1-convert-progn-body))
721 (defun ir1-convert-progn-body (start cont body)
722   (if (endp body)
723       (reference-constant start cont nil)
724       (let ((this-start start)
725             (forms body))
726         (loop
727           (let ((form (car forms)))
728             (when (endp (cdr forms))
729               (ir1-convert this-start cont form)
730               (return))
731             (let ((this-cont (make-continuation)))
732               (ir1-convert this-start this-cont form)
733               (setq this-start this-cont
734                     forms (cdr forms)))))))
735   (values))
736 \f
737 ;;;; converting combinations
738
739 ;;; Convert a function call where the function FUN is a LEAF. FORM is
740 ;;; the source for the call. We return the COMBINATION node so that
741 ;;; the caller can poke at it if it wants to.
742 (declaim (ftype (function (continuation continuation list leaf) combination)
743                 ir1-convert-combination))
744 (defun ir1-convert-combination (start cont form fun)
745   (let ((fun-cont (make-continuation)))
746     (ir1-convert start fun-cont `(the (or function symbol) ,fun))
747     (ir1-convert-combination-args fun-cont cont (cdr form))))
748
749 ;;; Convert the arguments to a call and make the COMBINATION
750 ;;; node. FUN-CONT is the continuation which yields the function to
751 ;;; call. ARGS is the list of arguments for the call, which defaults
752 ;;; to the cdr of source. We return the COMBINATION node.
753 (defun ir1-convert-combination-args (fun-cont cont args)
754   (declare (type continuation fun-cont cont) (list args))
755   (let ((node (make-combination fun-cont)))
756     (setf (continuation-dest fun-cont) node)
757     (collect ((arg-conts))
758       (let ((this-start fun-cont))
759         (dolist (arg args)
760           (let ((this-cont (make-continuation node)))
761             (ir1-convert this-start this-cont arg)
762             (setq this-start this-cont)
763             (arg-conts this-cont)))
764         (link-node-to-previous-continuation node this-start)
765         (use-continuation node cont)
766         (setf (combination-args node) (arg-conts))))
767     node))
768
769 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
770 ;;; source transforms and try out any inline expansion. If there is no
771 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
772 ;;; known function which will quite possibly be open-coded.) Next, we
773 ;;; go to ok-combination conversion.
774 (defun ir1-convert-srctran (start cont var form)
775   (declare (type continuation start cont) (type global-var var))
776   (let ((inlinep (when (defined-fun-p var)
777                    (defined-fun-inlinep var))))
778     (if (eq inlinep :notinline)
779         (ir1-convert-combination start cont form var)
780         (let ((transform (info :function
781                                :source-transform
782                                (leaf-source-name var))))
783           (if transform
784               (multiple-value-bind (result pass) (funcall transform form)
785                 (if pass
786                     (ir1-convert-maybe-predicate start cont form var)
787                     (ir1-convert start cont result)))
788               (ir1-convert-maybe-predicate start cont form var))))))
789
790 ;;; If the function has the PREDICATE attribute, and the CONT's DEST
791 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
792 ;;; predicate always appears in a conditional context.
793 ;;;
794 ;;; If the function isn't a predicate, then we call
795 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
796 (defun ir1-convert-maybe-predicate (start cont form var)
797   (declare (type continuation start cont) (list form) (type global-var var))
798   (let ((info (info :function :info (leaf-source-name var))))
799     (if (and info
800              (ir1-attributep (fun-info-attributes info) predicate)
801              (not (if-p (continuation-dest cont))))
802         (ir1-convert start cont `(if ,form t nil))
803         (ir1-convert-combination-checking-type start cont form var))))
804
805 ;;; Actually really convert a global function call that we are allowed
806 ;;; to early-bind.
807 ;;;
808 ;;; If we know the function type of the function, then we check the
809 ;;; call for syntactic legality with respect to the declared function
810 ;;; type. If it is impossible to determine whether the call is correct
811 ;;; due to non-constant keywords, then we give up, marking the call as
812 ;;; :FULL to inhibit further error messages. We return true when the
813 ;;; call is legal.
814 ;;;
815 ;;; If the call is legal, we also propagate type assertions from the
816 ;;; function type to the arg and result continuations. We do this now
817 ;;; so that IR1 optimize doesn't have to redundantly do the check
818 ;;; later so that it can do the type propagation.
819 (defun ir1-convert-combination-checking-type (start cont form var)
820   (declare (type continuation start cont) (list form) (type leaf var))
821   (let* ((node (ir1-convert-combination start cont form var))
822          (fun-cont (basic-combination-fun node))
823          (type (leaf-type var)))
824     (when (validate-call-type node type t)
825       (setf (continuation-%derived-type fun-cont)
826             (make-single-value-type type))
827       (setf (continuation-reoptimize fun-cont) nil)))
828   (values))
829
830 ;;; Convert a call to a local function, or if the function has already
831 ;;; been LET converted, then throw FUNCTIONAL to
832 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
833 ;;; are converting inline expansions for local functions during
834 ;;; optimization.
835 (defun ir1-convert-local-combination (start cont form functional)
836
837   ;; The test here is for "when LET converted", as a translation of
838   ;; the old CMU CL comments into code. Unfortunately, the old CMU CL
839   ;; comments aren't specific enough to tell whether the correct
840   ;; translation is FUNCTIONAL-SOMEWHAT-LETLIKE-P or
841   ;; FUNCTIONAL-LETLIKE-P or what. The old CMU CL code assumed that
842   ;; any non-null FUNCTIONAL-KIND meant that the function "had been
843   ;; LET converted", which might even be right, but seems fragile, so
844   ;; we try to be pickier.
845   (when (or
846          ;; looks LET-converted
847          (functional-somewhat-letlike-p functional)
848          ;; It's possible for a LET-converted function to end up
849          ;; deleted later. In that case, for the purposes of this
850          ;; analysis, it is LET-converted: LET-converted functionals
851          ;; are too badly trashed to expand them inline, and deleted
852          ;; LET-converted functionals are even worse.
853          (eql (functional-kind functional) :deleted))
854     (throw 'locall-already-let-converted functional))
855   ;; Any other non-NIL KIND value is a case we haven't found a
856   ;; justification for, and at least some such values (e.g. :EXTERNAL
857   ;; and :TOPLEVEL) seem obviously wrong.
858   (aver (null (functional-kind functional)))
859
860   (ir1-convert-combination start
861                            cont
862                            form
863                            (maybe-reanalyze-functional functional)))
864 \f
865 ;;;; PROCESS-DECLS
866
867 ;;; Given a list of LAMBDA-VARs and a variable name, return the
868 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
869 ;;; *last* variable with that name, since LET* bindings may be
870 ;;; duplicated, and declarations always apply to the last.
871 (declaim (ftype (function (list symbol) (or lambda-var list))
872                 find-in-bindings))
873 (defun find-in-bindings (vars name)
874   (let ((found nil))
875     (dolist (var vars)
876       (cond ((leaf-p var)
877              (when (eq (leaf-source-name var) name)
878                (setq found var))
879              (let ((info (lambda-var-arg-info var)))
880                (when info
881                  (let ((supplied-p (arg-info-supplied-p info)))
882                    (when (and supplied-p
883                               (eq (leaf-source-name supplied-p) name))
884                      (setq found supplied-p))))))
885             ((and (consp var) (eq (car var) name))
886              (setf found (cdr var)))))
887     found))
888
889 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
890 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
891 ;;; type, otherwise we add a type restriction on the var. If a symbol
892 ;;; macro, we just wrap a THE around the expansion.
893 (defun process-type-decl (decl res vars)
894   (declare (list decl vars) (type lexenv res))
895   (let ((type (compiler-specifier-type (first decl))))
896     (collect ((restr nil cons)
897              (new-vars nil cons))
898       (dolist (var-name (rest decl))
899         (let* ((bound-var (find-in-bindings vars var-name))
900                (var (or bound-var
901                         (lexenv-find var-name vars)
902                         (find-free-var var-name))))
903           (etypecase var
904             (leaf
905              (flet ((process-var (var bound-var)
906                       (let* ((old-type (or (lexenv-find var type-restrictions)
907                                            (leaf-type var)))
908                              (int (if (or (fun-type-p type)
909                                           (fun-type-p old-type))
910                                       type
911                                       (type-approx-intersection2 old-type type))))
912                         (cond ((eq int *empty-type*)
913                                (unless (policy *lexenv* (= inhibit-warnings 3))
914                                  (compiler-warn
915                                   "The type declarations ~S and ~S for ~S conflict."
916                                   (type-specifier old-type) (type-specifier type)
917                                   var-name)))
918                               (bound-var (setf (leaf-type bound-var) int))
919                               (t
920                                (restr (cons var int)))))))
921                (process-var var bound-var)
922                (awhen (and (lambda-var-p var)
923                            (lambda-var-specvar var))
924                       (process-var it nil))))
925             (cons
926              ;; FIXME: non-ANSI weirdness
927              (aver (eq (car var) 'MACRO))
928              (new-vars `(,var-name . (MACRO . (the ,(first decl)
929                                                 ,(cdr var))))))
930             (heap-alien-info
931              (compiler-error
932               "~S is an alien variable, so its type can't be declared."
933               var-name)))))
934
935       (if (or (restr) (new-vars))
936           (make-lexenv :default res
937                        :type-restrictions (restr)
938                        :vars (new-vars))
939           res))))
940
941 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
942 ;;; declarations for function variables. In addition to allowing
943 ;;; declarations for functions being bound, we must also deal with
944 ;;; declarations that constrain the type of lexically apparent
945 ;;; functions.
946 (defun process-ftype-decl (spec res names fvars)
947   (declare (type list names fvars)
948            (type lexenv res))
949   (let ((type (compiler-specifier-type spec)))
950     (collect ((res nil cons))
951       (dolist (name names)
952         (let ((found (find name fvars
953                            :key #'leaf-source-name
954                            :test #'equal)))
955           (cond
956            (found
957             (setf (leaf-type found) type)
958             (assert-definition-type found type
959                                     :unwinnage-fun #'compiler-note
960                                     :where "FTYPE declaration"))
961            (t
962             (res (cons (find-lexically-apparent-fun
963                         name "in a function type declaration")
964                        type))))))
965       (if (res)
966           (make-lexenv :default res :type-restrictions (res))
967           res))))
968
969 ;;; Process a special declaration, returning a new LEXENV. A non-bound
970 ;;; special declaration is instantiated by throwing a special variable
971 ;;; into the variables.
972 (defun process-special-decl (spec res vars)
973   (declare (list spec vars) (type lexenv res))
974   (collect ((new-venv nil cons))
975     (dolist (name (cdr spec))
976       (let ((var (find-in-bindings vars name)))
977         (etypecase var
978           (cons
979            (aver (eq (car var) 'MACRO))
980            (compiler-error
981             "~S is a symbol-macro and thus can't be declared special."
982             name))
983           (lambda-var
984            (when (lambda-var-ignorep var)
985              ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
986              ;; requires that this be a STYLE-WARNING, not a full WARNING.
987              (compiler-style-warn
988               "The ignored variable ~S is being declared special."
989               name))
990            (setf (lambda-var-specvar var)
991                  (specvar-for-binding name)))
992           (null
993            (unless (assoc name (new-venv) :test #'eq)
994              (new-venv (cons name (specvar-for-binding name))))))))
995     (if (new-venv)
996         (make-lexenv :default res :vars (new-venv))
997         res)))
998
999 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP.
1000 (defun make-new-inlinep (var inlinep)
1001   (declare (type global-var var) (type inlinep inlinep))
1002   (let ((res (make-defined-fun
1003               :%source-name (leaf-source-name var)
1004               :where-from (leaf-where-from var)
1005               :type (leaf-type var)
1006               :inlinep inlinep)))
1007     (when (defined-fun-p var)
1008       (setf (defined-fun-inline-expansion res)
1009             (defined-fun-inline-expansion var))
1010       (setf (defined-fun-functional res)
1011             (defined-fun-functional var)))
1012     res))
1013
1014 ;;; Parse an inline/notinline declaration. If it's a local function we're
1015 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1016 (defun process-inline-decl (spec res fvars)
1017   (let ((sense (cdr (assoc (first spec) *inlinep-translations* :test #'eq)))
1018         (new-fenv ()))
1019     (dolist (name (rest spec))
1020       (let ((fvar (find name fvars
1021                         :key #'leaf-source-name
1022                         :test #'equal)))
1023         (if fvar
1024             (setf (functional-inlinep fvar) sense)
1025             (let ((found
1026                    (find-lexically-apparent-fun
1027                     name "in an inline or notinline declaration")))
1028               (etypecase found
1029                 (functional
1030                  (when (policy *lexenv* (>= speed inhibit-warnings))
1031                    (compiler-note "ignoring ~A declaration not at ~
1032                                    definition of local function:~%  ~S"
1033                                   sense name)))
1034                 (global-var
1035                  (push (cons name (make-new-inlinep found sense))
1036                        new-fenv)))))))
1037
1038     (if new-fenv
1039         (make-lexenv :default res :funs new-fenv)
1040         res)))
1041
1042 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1043 (defun find-in-bindings-or-fbindings (name vars fvars)
1044   (declare (list vars fvars))
1045   (if (consp name)
1046       (destructuring-bind (wot fn-name) name
1047         (unless (eq wot 'function)
1048           (compiler-error "The function or variable name ~S is unrecognizable."
1049                           name))
1050         (find fn-name fvars :key #'leaf-source-name :test #'equal))
1051       (find-in-bindings vars name)))
1052
1053 ;;; Process an ignore/ignorable declaration, checking for various losing
1054 ;;; conditions.
1055 (defun process-ignore-decl (spec vars fvars)
1056   (declare (list spec vars fvars))
1057   (dolist (name (rest spec))
1058     (let ((var (find-in-bindings-or-fbindings name vars fvars)))
1059       (cond
1060        ((not var)
1061         ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1062         ;; requires that this be a STYLE-WARNING, not a full WARNING.
1063         (compiler-style-warn "declaring unknown variable ~S to be ignored"
1064                              name))
1065        ;; FIXME: This special case looks like non-ANSI weirdness.
1066        ((and (consp var) (consp (cdr var)) (eq (cadr var) 'macro))
1067         ;; Just ignore the IGNORE decl.
1068         )
1069        ((functional-p var)
1070         (setf (leaf-ever-used var) t))
1071        ((and (lambda-var-specvar var) (eq (first spec) 'ignore))
1072         ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1073         ;; requires that this be a STYLE-WARNING, not a full WARNING.
1074         (compiler-style-warn "declaring special variable ~S to be ignored"
1075                              name))
1076        ((eq (first spec) 'ignorable)
1077         (setf (leaf-ever-used var) t))
1078        (t
1079         (setf (lambda-var-ignorep var) t)))))
1080   (values))
1081
1082 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1083 ;;; go away, I think.
1084 (defvar *suppress-values-declaration* nil
1085   #!+sb-doc
1086   "If true, processing of the VALUES declaration is inhibited.")
1087
1088 ;;; Process a single declaration spec, augmenting the specified LEXENV
1089 ;;; RES and returning it as a result. VARS and FVARS are as described in
1090 ;;; PROCESS-DECLS.
1091 (defun process-1-decl (raw-spec res vars fvars cont)
1092   (declare (type list raw-spec vars fvars))
1093   (declare (type lexenv res))
1094   (declare (type continuation cont))
1095   (let ((spec (canonized-decl-spec raw-spec)))
1096     (case (first spec)
1097       (special (process-special-decl spec res vars))
1098       (ftype
1099        (unless (cdr spec)
1100          (compiler-error "no type specified in FTYPE declaration: ~S" spec))
1101        (process-ftype-decl (second spec) res (cddr spec) fvars))
1102       ((inline notinline maybe-inline)
1103        (process-inline-decl spec res fvars))
1104       ((ignore ignorable)
1105        (process-ignore-decl spec vars fvars)
1106        res)
1107       (optimize
1108        (make-lexenv
1109         :default res
1110         :policy (process-optimize-decl spec (lexenv-policy res))))
1111       (type
1112        (process-type-decl (cdr spec) res vars))
1113       (values ;; FIXME -- APD, 2002-01-26
1114        (if t ; *suppress-values-declaration*
1115            res
1116            (let ((types (cdr spec)))
1117              (ir1ize-the-or-values (if (eql (length types) 1)
1118                                        (car types)
1119                                        `(values ,@types))
1120                                    cont
1121                                    res
1122                                    "in VALUES declaration"))))
1123       (dynamic-extent
1124        (when (policy *lexenv* (> speed inhibit-warnings))
1125          (compiler-note
1126           "compiler limitation: ~
1127         ~%  There's no special support for DYNAMIC-EXTENT (so it's ignored)."))
1128        res)
1129       (t
1130        (unless (info :declaration :recognized (first spec))
1131          (compiler-warn "unrecognized declaration ~S" raw-spec))
1132        res))))
1133
1134 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1135 ;;; and FUNCTIONAL structures which are being bound. In addition to
1136 ;;; filling in slots in the leaf structures, we return a new LEXENV
1137 ;;; which reflects pervasive special and function type declarations,
1138 ;;; (NOT)INLINE declarations and OPTIMIZE declarations. CONT is the
1139 ;;; continuation affected by VALUES declarations.
1140 ;;;
1141 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1142 ;;; of LOCALLY.
1143 (defun process-decls (decls vars fvars cont &optional (env *lexenv*))
1144   (declare (list decls vars fvars) (type continuation cont))
1145   (dolist (decl decls)
1146     (dolist (spec (rest decl))
1147       (unless (consp spec)
1148         (compiler-error "malformed declaration specifier ~S in ~S" spec decl))
1149       (setq env (process-1-decl spec env vars fvars cont))))
1150   env)
1151
1152 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1153 ;;; declaration. If there is a global variable of that name, then
1154 ;;; check that it isn't a constant and return it. Otherwise, create an
1155 ;;; anonymous GLOBAL-VAR.
1156 (defun specvar-for-binding (name)
1157   (cond ((not (eq (info :variable :where-from name) :assumed))
1158          (let ((found (find-free-var name)))
1159            (when (heap-alien-info-p found)
1160              (compiler-error
1161               "~S is an alien variable and so can't be declared special."
1162               name))
1163            (unless (global-var-p found)
1164              (compiler-error
1165               "~S is a constant and so can't be declared special."
1166               name))
1167            found))
1168         (t
1169          (make-global-var :kind :special
1170                           :%source-name name
1171                           :where-from :declared))))