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