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