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