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