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