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