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