0.8.0.54:
[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 (function (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 (function (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 (function (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 (macrolet (;; Bind *COMPILER-ERROR-BAILOUT* to a function that throws
435            ;; out of the body and converts a proxy form instead.
436            (ir1-error-bailout ((start
437                                 cont
438                                 form
439                                 &optional
440                                 (proxy ``(error 'simple-program-error
441                                           :format-control "execution of a form compiled with errors:~% ~S"
442                                           :format-arguments (list ',,form))))
443                                &body body)
444                               (with-unique-names (skip)
445                                 `(block ,skip
446                                    (catch 'ir1-error-abort
447                                      (let ((*compiler-error-bailout*
448                                             (lambda ()
449                                               (throw 'ir1-error-abort nil))))
450                                        ,@body
451                                        (return-from ,skip nil)))
452                                    (ir1-convert ,start ,cont ,proxy)))))
453
454   ;; Translate FORM into IR1. The code is inserted as the NEXT of the
455   ;; continuation START. CONT is the continuation which receives the
456   ;; value of the FORM to be translated. The translators call this
457   ;; function recursively to translate their subnodes.
458   ;;
459   ;; As a special hack to make life easier in the compiler, a LEAF
460   ;; IR1-converts into a reference to that LEAF structure. This allows
461   ;; the creation using backquote of forms that contain leaf
462   ;; references, without having to introduce dummy names into the
463   ;; namespace.
464   (declaim (ftype (function (continuation continuation t) (values)) ir1-convert))
465   (defun ir1-convert (start cont form)
466     (ir1-error-bailout (start cont form)
467       (let ((*current-path* (or (gethash form *source-paths*)
468                                 (cons form *current-path*))))
469         (if (atom form)
470             (cond ((and (symbolp form) (not (keywordp form)))
471                    (ir1-convert-var start cont form))
472                   ((leaf-p form)
473                    (reference-leaf start cont form))
474                   (t
475                    (reference-constant start cont form)))
476             (let ((opname (car form)))
477               (cond ((or (symbolp opname) (leaf-p opname))
478                      (let ((lexical-def (if (leaf-p opname)
479                                             opname
480                                             (lexenv-find opname funs))))
481                        (typecase lexical-def
482                          (null (ir1-convert-global-functoid start cont form))
483                          (functional
484                           (ir1-convert-local-combination start
485                                                          cont
486                                                          form
487                                                          lexical-def))
488                          (global-var
489                           (ir1-convert-srctran start cont lexical-def form))
490                          (t
491                           (aver (and (consp lexical-def)
492                                      (eq (car lexical-def) 'macro)))
493                           (ir1-convert start cont
494                                        (careful-expand-macro (cdr lexical-def)
495                                                              form))))))
496                     ((or (atom opname) (not (eq (car opname) 'lambda)))
497                      (compiler-error "illegal function call"))
498                     (t
499                      ;; implicitly (LAMBDA ..) because the LAMBDA
500                      ;; expression is the CAR of an executed form
501                      (ir1-convert-combination start
502                                               cont
503                                               form
504                                               (ir1-convert-lambda
505                                                opname
506                                                :debug-name (debug-namify
507                                                             "LAMBDA CAR ~S"
508                                                             opname)
509                                                :allow-debug-catch-tag t))))))))
510     (values))
511
512   ;; Generate a reference to a manifest constant, creating a new leaf
513   ;; if necessary. If we are producing a fasl file, make sure that
514   ;; MAKE-LOAD-FORM gets used on any parts of the constant that it
515   ;; needs to be.
516   (defun reference-constant (start cont value)
517     (declare (type continuation start cont)
518              (inline find-constant))
519     (ir1-error-bailout
520      (start cont value '(error "attempt to reference undumpable constant"))
521      (when (producing-fasl-file)
522        (maybe-emit-make-load-forms value))
523      (let* ((leaf (find-constant value))
524             (res (make-ref leaf)))
525        (push res (leaf-refs leaf))
526        (link-node-to-previous-continuation res start)
527        (use-continuation res cont)))
528     (values)))
529
530 ;;; Add FUNCTIONAL to the COMPONENT-REANALYZE-FUNCTIONALS, unless it's
531 ;;; some trivial type for which reanalysis is a trivial no-op, or
532 ;;; unless it doesn't belong in this component at all.
533 ;;;
534 ;;; FUNCTIONAL is returned.
535 (defun maybe-reanalyze-functional (functional)
536
537   (aver (not (eql (functional-kind functional) :deleted))) ; bug 148
538   (aver-live-component *current-component*)
539
540   ;; When FUNCTIONAL is of a type for which reanalysis isn't a trivial
541   ;; no-op
542   (when (typep functional '(or optional-dispatch clambda))
543
544     ;; When FUNCTIONAL knows its component
545     (when (lambda-p functional)
546       (aver (eql (lambda-component functional) *current-component*)))
547
548     (pushnew functional
549              (component-reanalyze-functionals *current-component*)))
550
551   functional)
552
553 ;;; Generate a REF node for LEAF, frobbing the LEAF structure as
554 ;;; needed. If LEAF represents a defined function which has already
555 ;;; been converted, and is not :NOTINLINE, then reference the
556 ;;; functional instead.
557 (defun reference-leaf (start cont leaf)
558   (declare (type continuation start cont) (type leaf leaf))
559   (let* ((type (lexenv-find leaf type-restrictions))
560          (leaf (or (and (defined-fun-p leaf)
561                         (not (eq (defined-fun-inlinep leaf)
562                                  :notinline))
563                         (let ((functional (defined-fun-functional leaf)))
564                           (when (and functional
565                                      (not (functional-kind functional)))
566                             (maybe-reanalyze-functional functional))))
567                    (when (and (lambda-p leaf)
568                               (memq (functional-kind leaf)
569                                     '(nil :optional)))
570                      (maybe-reanalyze-functional leaf))
571                    leaf))
572          (ref (make-ref leaf)))
573     (push ref (leaf-refs leaf))
574     (setf (leaf-ever-used leaf) t)
575     (link-node-to-previous-continuation ref start)
576     (cond (type (let* ((ref-cont (make-continuation))
577                        (cast (make-cast ref-cont
578                                         (make-single-value-type type)
579                                         (lexenv-policy *lexenv*))))
580                   (setf (continuation-dest ref-cont) cast)
581                   (use-continuation ref ref-cont)
582                   (link-node-to-previous-continuation cast ref-cont)
583                   (use-continuation cast cont)))
584           (t (use-continuation ref cont)))))
585
586 ;;; Convert a reference to a symbolic constant or variable. If the
587 ;;; symbol is entered in the LEXENV-VARS we use that definition,
588 ;;; otherwise we find the current global definition. This is also
589 ;;; where we pick off symbol macro and alien variable references.
590 (defun ir1-convert-var (start cont name)
591   (declare (type continuation start cont) (symbol name))
592   (let ((var (or (lexenv-find name vars) (find-free-var name))))
593     (etypecase var
594       (leaf
595        (when (lambda-var-p var)
596          (let ((home (continuation-home-lambda-or-null start)))
597            (when home
598              (pushnew var (lambda-calls-or-closes home))))
599          (when (lambda-var-ignorep var)
600            ;; (ANSI's specification for the IGNORE declaration requires
601            ;; that this be a STYLE-WARNING, not a full WARNING.)
602            (compiler-style-warn "reading an ignored variable: ~S" name)))
603        (reference-leaf start cont var))
604       (cons
605        (aver (eq (car var) 'MACRO))
606        ;; FIXME: [Free] type declarations. -- APD, 2002-01-26
607        (ir1-convert start cont (cdr var)))
608       (heap-alien-info
609        (ir1-convert start cont `(%heap-alien ',var)))))
610   (values))
611
612 ;;; Convert anything that looks like a special form, global function
613 ;;; or compiler-macro call.
614 (defun ir1-convert-global-functoid (start cont form)
615   (declare (type continuation start cont) (list form))
616   (let* ((fun-name (first form))
617          (translator (info :function :ir1-convert fun-name))
618          (cmacro-fun (sb!xc:compiler-macro-function fun-name *lexenv*)))
619     (cond (translator
620            (when cmacro-fun
621              (compiler-warn "ignoring compiler macro for special form"))
622            (funcall translator start cont form))
623           ((and cmacro-fun
624                 ;; gotcha: If you look up the DEFINE-COMPILER-MACRO
625                 ;; macro in the ANSI spec, you might think that
626                 ;; suppressing compiler-macro expansion when NOTINLINE
627                 ;; is some pre-ANSI hack. However, if you look up the
628                 ;; NOTINLINE declaration, you'll find that ANSI
629                 ;; requires this behavior after all.
630                 (not (eq (info :function :inlinep fun-name) :notinline)))
631            (let ((res (careful-expand-macro cmacro-fun form)))
632              (if (eq res form)
633                  (ir1-convert-global-functoid-no-cmacro
634                   start cont form fun-name)
635                  (ir1-convert start cont res))))
636           (t
637            (ir1-convert-global-functoid-no-cmacro start cont form fun-name)))))
638
639 ;;; Handle the case of where the call was not a compiler macro, or was
640 ;;; a compiler macro and passed.
641 (defun ir1-convert-global-functoid-no-cmacro (start cont form fun)
642   (declare (type continuation start cont) (list form))
643   ;; FIXME: Couldn't all the INFO calls here be converted into
644   ;; standard CL functions, like MACRO-FUNCTION or something?
645   ;; And what happens with lexically-defined (MACROLET) macros
646   ;; here, anyway?
647   (ecase (info :function :kind fun)
648     (:macro
649      (ir1-convert start
650                   cont
651                   (careful-expand-macro (info :function :macro-function fun)
652                                         form)))
653     ((nil :function)
654      (ir1-convert-srctran start
655                           cont
656                           (find-free-fun fun "shouldn't happen! (no-cmacro)")
657                           form))))
658
659 (defun muffle-warning-or-die ()
660   (muffle-warning)
661   (bug "no MUFFLE-WARNING restart"))
662
663 ;;; Expand FORM using the macro whose MACRO-FUNCTION is FUN, trapping
664 ;;; errors which occur during the macroexpansion.
665 (defun careful-expand-macro (fun form)
666   (let (;; a hint I (WHN) wish I'd known earlier
667         (hint "(hint: For more precise location, try *BREAK-ON-SIGNALS*.)"))
668     (flet (;; Return a string to use as a prefix in error reporting,
669            ;; telling something about which form caused the problem.
670            (wherestring ()
671              (let ((*print-pretty* nil)
672                    ;; We rely on the printer to abbreviate FORM. 
673                    (*print-length* 3)
674                    (*print-level* 1))
675                (format
676                 nil
677                 #-sb-xc-host "(in macroexpansion of ~S)"
678                 ;; longer message to avoid ambiguity "Was it the xc host
679                 ;; or the cross-compiler which encountered the problem?"
680                 #+sb-xc-host "(in cross-compiler macroexpansion of ~S)"
681                 form))))
682       (handler-bind ((style-warning (lambda (c)
683                                       (compiler-style-warn
684                                        "~@<~A~:@_~A~@:_~A~:>"
685                                        (wherestring) hint c)
686                                       (muffle-warning-or-die)))
687                      ;; KLUDGE: CMU CL in its wisdom (version 2.4.6 for
688                      ;; Debian Linux, anyway) raises a CL:WARNING
689                      ;; condition (not a CL:STYLE-WARNING) for undefined
690                      ;; symbols when converting interpreted functions,
691                      ;; causing COMPILE-FILE to think the file has a real
692                      ;; problem, causing COMPILE-FILE to return FAILURE-P
693                      ;; set (not just WARNINGS-P set). Since undefined
694                      ;; symbol warnings are often harmless forward
695                      ;; references, and since it'd be inordinately painful
696                      ;; to try to eliminate all such forward references,
697                      ;; these warnings are basically unavoidable. Thus, we
698                      ;; need to coerce the system to work through them,
699                      ;; and this code does so, by crudely suppressing all
700                      ;; warnings in cross-compilation macroexpansion. --
701                      ;; WHN 19990412
702                      #+(and cmu sb-xc-host)
703                      (warning (lambda (c)
704                                 (compiler-note
705                                  "~@<~A~:@_~
706                                   ~A~:@_~
707                                   ~@<(KLUDGE: That was a non-STYLE WARNING. ~
708                                   Ordinarily that would cause compilation to ~
709                                   fail. However, since we're running under ~
710                                   CMU CL, and since CMU CL emits non-STYLE ~
711                                   warnings for safe, hard-to-fix things (e.g. ~
712                                   references to not-yet-defined functions) ~
713                                   we're going to have to ignore it and ~
714                                   proceed anyway. Hopefully we're not ~
715                                   ignoring anything  horrible here..)~:@>~:>"
716                                  (wherestring)
717                                  c)
718                                 (muffle-warning-or-die)))
719                      #-(and cmu sb-xc-host)
720                      (warning (lambda (c)
721                                 (compiler-warn "~@<~A~:@_~A~@:_~A~:>"
722                                                (wherestring) hint c)
723                                 (muffle-warning-or-die)))
724                      (error (lambda (c)
725                               (compiler-error "~@<~A~:@_~A~@:_~A~:>"
726                                               (wherestring) hint c))))
727         (funcall sb!xc:*macroexpand-hook* fun form *lexenv*)))))
728 \f
729 ;;;; conversion utilities
730
731 ;;; Convert a bunch of forms, discarding all the values except the
732 ;;; last. If there aren't any forms, then translate a NIL.
733 (declaim (ftype (function (continuation continuation list) (values))
734                 ir1-convert-progn-body))
735 (defun ir1-convert-progn-body (start cont body)
736   (if (endp body)
737       (reference-constant start cont nil)
738       (let ((this-start start)
739             (forms body))
740         (loop
741           (let ((form (car forms)))
742             (when (endp (cdr forms))
743               (ir1-convert this-start cont form)
744               (return))
745             (let ((this-cont (make-continuation)))
746               (ir1-convert this-start this-cont form)
747               (setq this-start this-cont
748                     forms (cdr forms)))))))
749   (values))
750 \f
751 ;;;; converting combinations
752
753 ;;; Convert a function call where the function FUN is a LEAF. FORM is
754 ;;; the source for the call. We return the COMBINATION node so that
755 ;;; the caller can poke at it if it wants to.
756 (declaim (ftype (function (continuation continuation list leaf) combination)
757                 ir1-convert-combination))
758 (defun ir1-convert-combination (start cont form fun)
759   (let ((fun-cont (make-continuation)))
760     (ir1-convert start fun-cont `(the (or function symbol) ,fun))
761     (ir1-convert-combination-args fun-cont cont (cdr form))))
762
763 ;;; Convert the arguments to a call and make the COMBINATION
764 ;;; node. FUN-CONT is the continuation which yields the function to
765 ;;; call. ARGS is the list of arguments for the call, which defaults
766 ;;; to the cdr of source. We return the COMBINATION node.
767 (defun ir1-convert-combination-args (fun-cont cont args)
768   (declare (type continuation fun-cont cont) (list args))
769   (let ((node (make-combination fun-cont)))
770     (setf (continuation-dest fun-cont) node)
771     (collect ((arg-conts))
772       (let ((this-start fun-cont))
773         (dolist (arg args)
774           (let ((this-cont (make-continuation node)))
775             (ir1-convert this-start this-cont arg)
776             (setq this-start this-cont)
777             (arg-conts this-cont)))
778         (link-node-to-previous-continuation node this-start)
779         (use-continuation node cont)
780         (setf (combination-args node) (arg-conts))))
781     node))
782
783 ;;; Convert a call to a global function. If not :NOTINLINE, then we do
784 ;;; source transforms and try out any inline expansion. If there is no
785 ;;; expansion, but is :INLINE, then give an efficiency note (unless a
786 ;;; known function which will quite possibly be open-coded.) Next, we
787 ;;; go to ok-combination conversion.
788 (defun ir1-convert-srctran (start cont var form)
789   (declare (type continuation start cont) (type global-var var))
790   (let ((inlinep (when (defined-fun-p var)
791                    (defined-fun-inlinep var))))
792     (if (eq inlinep :notinline)
793         (ir1-convert-combination start cont form var)
794         (let ((transform (info :function
795                                :source-transform
796                                (leaf-source-name var))))
797           (if transform
798               (multiple-value-bind (result pass) (funcall transform form)
799                 (if pass
800                     (ir1-convert-maybe-predicate start cont form var)
801                     (ir1-convert start cont result)))
802               (ir1-convert-maybe-predicate start cont form var))))))
803
804 ;;; If the function has the PREDICATE attribute, and the CONT's DEST
805 ;;; isn't an IF, then we convert (IF <form> T NIL), ensuring that a
806 ;;; predicate always appears in a conditional context.
807 ;;;
808 ;;; If the function isn't a predicate, then we call
809 ;;; IR1-CONVERT-COMBINATION-CHECKING-TYPE.
810 (defun ir1-convert-maybe-predicate (start cont form var)
811   (declare (type continuation start cont) (list form) (type global-var var))
812   (let ((info (info :function :info (leaf-source-name var))))
813     (if (and info
814              (ir1-attributep (fun-info-attributes info) predicate)
815              (not (if-p (continuation-dest cont))))
816         (ir1-convert start cont `(if ,form t nil))
817         (ir1-convert-combination-checking-type start cont form var))))
818
819 ;;; Actually really convert a global function call that we are allowed
820 ;;; to early-bind.
821 ;;;
822 ;;; If we know the function type of the function, then we check the
823 ;;; call for syntactic legality with respect to the declared function
824 ;;; type. If it is impossible to determine whether the call is correct
825 ;;; due to non-constant keywords, then we give up, marking the call as
826 ;;; :FULL to inhibit further error messages. We return true when the
827 ;;; call is legal.
828 ;;;
829 ;;; If the call is legal, we also propagate type assertions from the
830 ;;; function type to the arg and result continuations. We do this now
831 ;;; so that IR1 optimize doesn't have to redundantly do the check
832 ;;; later so that it can do the type propagation.
833 (defun ir1-convert-combination-checking-type (start cont form var)
834   (declare (type continuation start cont) (list form) (type leaf var))
835   (let* ((node (ir1-convert-combination start cont form var))
836          (fun-cont (basic-combination-fun node))
837          (type (leaf-type var)))
838     (when (validate-call-type node type t)
839       (setf (continuation-%derived-type fun-cont)
840             (make-single-value-type type))
841       (setf (continuation-reoptimize fun-cont) nil)))
842   (values))
843
844 ;;; Convert a call to a local function, or if the function has already
845 ;;; been LET converted, then throw FUNCTIONAL to
846 ;;; LOCALL-ALREADY-LET-CONVERTED. The THROW should only happen when we
847 ;;; are converting inline expansions for local functions during
848 ;;; optimization.
849 (defun ir1-convert-local-combination (start cont form functional)
850
851   ;; The test here is for "when LET converted", as a translation of
852   ;; the old CMU CL comments into code. Unfortunately, the old CMU CL
853   ;; comments aren't specific enough to tell whether the correct
854   ;; translation is FUNCTIONAL-SOMEWHAT-LETLIKE-P or
855   ;; FUNCTIONAL-LETLIKE-P or what. The old CMU CL code assumed that
856   ;; any non-null FUNCTIONAL-KIND meant that the function "had been
857   ;; LET converted", which might even be right, but seems fragile, so
858   ;; we try to be pickier.
859   (when (or
860          ;; looks LET-converted
861          (functional-somewhat-letlike-p functional)
862          ;; It's possible for a LET-converted function to end up
863          ;; deleted later. In that case, for the purposes of this
864          ;; analysis, it is LET-converted: LET-converted functionals
865          ;; are too badly trashed to expand them inline, and deleted
866          ;; LET-converted functionals are even worse.
867          (eql (functional-kind functional) :deleted))
868     (throw 'locall-already-let-converted functional))
869   ;; Any other non-NIL KIND value is a case we haven't found a
870   ;; justification for, and at least some such values (e.g. :EXTERNAL
871   ;; and :TOPLEVEL) seem obviously wrong.
872   (aver (null (functional-kind functional)))
873
874   (ir1-convert-combination start
875                            cont
876                            form
877                            (maybe-reanalyze-functional functional)))
878 \f
879 ;;;; PROCESS-DECLS
880
881 ;;; Given a list of LAMBDA-VARs and a variable name, return the
882 ;;; LAMBDA-VAR for that name, or NIL if it isn't found. We return the
883 ;;; *last* variable with that name, since LET* bindings may be
884 ;;; duplicated, and declarations always apply to the last.
885 (declaim (ftype (function (list symbol) (or lambda-var list))
886                 find-in-bindings))
887 (defun find-in-bindings (vars name)
888   (let ((found nil))
889     (dolist (var vars)
890       (cond ((leaf-p var)
891              (when (eq (leaf-source-name var) name)
892                (setq found var))
893              (let ((info (lambda-var-arg-info var)))
894                (when info
895                  (let ((supplied-p (arg-info-supplied-p info)))
896                    (when (and supplied-p
897                               (eq (leaf-source-name supplied-p) name))
898                      (setq found supplied-p))))))
899             ((and (consp var) (eq (car var) name))
900              (setf found (cdr var)))))
901     found))
902
903 ;;; Called by PROCESS-DECLS to deal with a variable type declaration.
904 ;;; If a LAMBDA-VAR being bound, we intersect the type with the var's
905 ;;; type, otherwise we add a type restriction on the var. If a symbol
906 ;;; macro, we just wrap a THE around the expansion.
907 (defun process-type-decl (decl res vars)
908   (declare (list decl vars) (type lexenv res))
909   (let ((type (compiler-specifier-type (first decl))))
910     (collect ((restr nil cons)
911              (new-vars nil cons))
912       (dolist (var-name (rest decl))
913         (let* ((bound-var (find-in-bindings vars var-name))
914                (var (or bound-var
915                         (lexenv-find var-name vars)
916                         (find-free-var var-name))))
917           (etypecase var
918             (leaf
919              (flet ((process-var (var bound-var)
920                       (let* ((old-type (or (lexenv-find var type-restrictions)
921                                            (leaf-type var)))
922                              (int (if (or (fun-type-p type)
923                                           (fun-type-p old-type))
924                                       type
925                                       (type-approx-intersection2 old-type type))))
926                         (cond ((eq int *empty-type*)
927                                (unless (policy *lexenv* (= inhibit-warnings 3))
928                                  (compiler-warn
929                                   "The type declarations ~S and ~S for ~S conflict."
930                                   (type-specifier old-type) (type-specifier type)
931                                   var-name)))
932                               (bound-var (setf (leaf-type bound-var) int))
933                               (t
934                                (restr (cons var int)))))))
935                (process-var var bound-var)
936                (awhen (and (lambda-var-p var)
937                            (lambda-var-specvar var))
938                       (process-var it nil))))
939             (cons
940              ;; FIXME: non-ANSI weirdness
941              (aver (eq (car var) 'MACRO))
942              (new-vars `(,var-name . (MACRO . (the ,(first decl)
943                                                 ,(cdr var))))))
944             (heap-alien-info
945              (compiler-error
946               "~S is an alien variable, so its type can't be declared."
947               var-name)))))
948
949       (if (or (restr) (new-vars))
950           (make-lexenv :default res
951                        :type-restrictions (restr)
952                        :vars (new-vars))
953           res))))
954
955 ;;; This is somewhat similar to PROCESS-TYPE-DECL, but handles
956 ;;; declarations for function variables. In addition to allowing
957 ;;; declarations for functions being bound, we must also deal with
958 ;;; declarations that constrain the type of lexically apparent
959 ;;; functions.
960 (defun process-ftype-decl (spec res names fvars)
961   (declare (type list names fvars)
962            (type lexenv res))
963   (let ((type (compiler-specifier-type spec)))
964     (collect ((res nil cons))
965       (dolist (name names)
966         (let ((found (find name fvars
967                            :key #'leaf-source-name
968                            :test #'equal)))
969           (cond
970            (found
971             (setf (leaf-type found) type)
972             (assert-definition-type found type
973                                     :unwinnage-fun #'compiler-note
974                                     :where "FTYPE declaration"))
975            (t
976             (res (cons (find-lexically-apparent-fun
977                         name "in a function type declaration")
978                        type))))))
979       (if (res)
980           (make-lexenv :default res :type-restrictions (res))
981           res))))
982
983 ;;; Process a special declaration, returning a new LEXENV. A non-bound
984 ;;; special declaration is instantiated by throwing a special variable
985 ;;; into the variables.
986 (defun process-special-decl (spec res vars)
987   (declare (list spec vars) (type lexenv res))
988   (collect ((new-venv nil cons))
989     (dolist (name (cdr spec))
990       (let ((var (find-in-bindings vars name)))
991         (etypecase var
992           (cons
993            (aver (eq (car var) 'MACRO))
994            (compiler-error
995             "~S is a symbol-macro and thus can't be declared special."
996             name))
997           (lambda-var
998            (when (lambda-var-ignorep var)
999              ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1000              ;; requires that this be a STYLE-WARNING, not a full WARNING.
1001              (compiler-style-warn
1002               "The ignored variable ~S is being declared special."
1003               name))
1004            (setf (lambda-var-specvar var)
1005                  (specvar-for-binding name)))
1006           (null
1007            (unless (assoc name (new-venv) :test #'eq)
1008              (new-venv (cons name (specvar-for-binding name))))))))
1009     (if (new-venv)
1010         (make-lexenv :default res :vars (new-venv))
1011         res)))
1012
1013 ;;; Return a DEFINED-FUN which copies a GLOBAL-VAR but for its INLINEP
1014 ;;; (and TYPE if notinline).
1015 (defun make-new-inlinep (var inlinep)
1016   (declare (type global-var var) (type inlinep inlinep))
1017   (let ((res (make-defined-fun
1018               :%source-name (leaf-source-name var)
1019               :where-from (leaf-where-from var)
1020               :type (if (eq inlinep :notinline)
1021                         (specifier-type 'function)
1022                         (leaf-type var))
1023               :inlinep inlinep)))
1024     (when (defined-fun-p var)
1025       (setf (defined-fun-inline-expansion res)
1026             (defined-fun-inline-expansion var))
1027       (setf (defined-fun-functional res)
1028             (defined-fun-functional var)))
1029     res))
1030
1031 ;;; Parse an inline/notinline declaration. If it's a local function we're
1032 ;;; defining, set its INLINEP. If a global function, add a new FENV entry.
1033 (defun process-inline-decl (spec res fvars)
1034   (let ((sense (cdr (assoc (first spec) *inlinep-translations* :test #'eq)))
1035         (new-fenv ()))
1036     (dolist (name (rest spec))
1037       (let ((fvar (find name fvars
1038                         :key #'leaf-source-name
1039                         :test #'equal)))
1040         (if fvar
1041             (setf (functional-inlinep fvar) sense)
1042             (let ((found
1043                    (find-lexically-apparent-fun
1044                     name "in an inline or notinline declaration")))
1045               (etypecase found
1046                 (functional
1047                  (when (policy *lexenv* (>= speed inhibit-warnings))
1048                    (compiler-note "ignoring ~A declaration not at ~
1049                                    definition of local function:~%  ~S"
1050                                   sense name)))
1051                 (global-var
1052                  (push (cons name (make-new-inlinep found sense))
1053                        new-fenv)))))))
1054
1055     (if new-fenv
1056         (make-lexenv :default res :funs new-fenv)
1057         res)))
1058
1059 ;;; like FIND-IN-BINDINGS, but looks for #'FOO in the FVARS
1060 (defun find-in-bindings-or-fbindings (name vars fvars)
1061   (declare (list vars fvars))
1062   (if (consp name)
1063       (destructuring-bind (wot fn-name) name
1064         (unless (eq wot 'function)
1065           (compiler-error "The function or variable name ~S is unrecognizable."
1066                           name))
1067         (find fn-name fvars :key #'leaf-source-name :test #'equal))
1068       (find-in-bindings vars name)))
1069
1070 ;;; Process an ignore/ignorable declaration, checking for various losing
1071 ;;; conditions.
1072 (defun process-ignore-decl (spec vars fvars)
1073   (declare (list spec vars fvars))
1074   (dolist (name (rest spec))
1075     (let ((var (find-in-bindings-or-fbindings name vars fvars)))
1076       (cond
1077        ((not var)
1078         ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1079         ;; requires that this be a STYLE-WARNING, not a full WARNING.
1080         (compiler-style-warn "declaring unknown variable ~S to be ignored"
1081                              name))
1082        ;; FIXME: This special case looks like non-ANSI weirdness.
1083        ((and (consp var) (consp (cdr var)) (eq (cadr var) 'macro))
1084         ;; Just ignore the IGNORE decl.
1085         )
1086        ((functional-p var)
1087         (setf (leaf-ever-used var) t))
1088        ((and (lambda-var-specvar var) (eq (first spec) 'ignore))
1089         ;; ANSI's definition for "Declaration IGNORE, IGNORABLE"
1090         ;; requires that this be a STYLE-WARNING, not a full WARNING.
1091         (compiler-style-warn "declaring special variable ~S to be ignored"
1092                              name))
1093        ((eq (first spec) 'ignorable)
1094         (setf (leaf-ever-used var) t))
1095        (t
1096         (setf (lambda-var-ignorep var) t)))))
1097   (values))
1098
1099 ;;; FIXME: This is non-ANSI, so the default should be T, or it should
1100 ;;; go away, I think.
1101 (defvar *suppress-values-declaration* nil
1102   #!+sb-doc
1103   "If true, processing of the VALUES declaration is inhibited.")
1104
1105 ;;; Process a single declaration spec, augmenting the specified LEXENV
1106 ;;; RES and returning it as a result. VARS and FVARS are as described in
1107 ;;; PROCESS-DECLS.
1108 (defun process-1-decl (raw-spec res vars fvars cont)
1109   (declare (type list raw-spec vars fvars))
1110   (declare (type lexenv res))
1111   (declare (type continuation cont))
1112   (let ((spec (canonized-decl-spec raw-spec)))
1113     (case (first spec)
1114       (special (process-special-decl spec res vars))
1115       (ftype
1116        (unless (cdr spec)
1117          (compiler-error "no type specified in FTYPE declaration: ~S" spec))
1118        (process-ftype-decl (second spec) res (cddr spec) fvars))
1119       ((inline notinline maybe-inline)
1120        (process-inline-decl spec res fvars))
1121       ((ignore ignorable)
1122        (process-ignore-decl spec vars fvars)
1123        res)
1124       (optimize
1125        (make-lexenv
1126         :default res
1127         :policy (process-optimize-decl spec (lexenv-policy res))))
1128       (type
1129        (process-type-decl (cdr spec) res vars))
1130       (values ;; FIXME -- APD, 2002-01-26
1131        (if t ; *suppress-values-declaration*
1132            res
1133            (let ((types (cdr spec)))
1134              (ir1ize-the-or-values (if (eql (length types) 1)
1135                                        (car types)
1136                                        `(values ,@types))
1137                                    cont
1138                                    res
1139                                    "in VALUES declaration"))))
1140       (dynamic-extent
1141        (when (policy *lexenv* (> speed inhibit-warnings))
1142          (compiler-note
1143           "compiler limitation: ~
1144         ~%  There's no special support for DYNAMIC-EXTENT (so it's ignored)."))
1145        res)
1146       (t
1147        (unless (info :declaration :recognized (first spec))
1148          (compiler-warn "unrecognized declaration ~S" raw-spec))
1149        res))))
1150
1151 ;;; Use a list of DECLARE forms to annotate the lists of LAMBDA-VAR
1152 ;;; and FUNCTIONAL structures which are being bound. In addition to
1153 ;;; filling in slots in the leaf structures, we return a new LEXENV
1154 ;;; which reflects pervasive special and function type declarations,
1155 ;;; (NOT)INLINE declarations and OPTIMIZE declarations. CONT is the
1156 ;;; continuation affected by VALUES declarations.
1157 ;;;
1158 ;;; This is also called in main.lisp when PROCESS-FORM handles a use
1159 ;;; of LOCALLY.
1160 (defun process-decls (decls vars fvars cont &optional (env *lexenv*))
1161   (declare (list decls vars fvars) (type continuation cont))
1162   (dolist (decl decls)
1163     (dolist (spec (rest decl))
1164       (unless (consp spec)
1165         (compiler-error "malformed declaration specifier ~S in ~S" spec decl))
1166       (setq env (process-1-decl spec env vars fvars cont))))
1167   env)
1168
1169 ;;; Return the SPECVAR for NAME to use when we see a local SPECIAL
1170 ;;; declaration. If there is a global variable of that name, then
1171 ;;; check that it isn't a constant and return it. Otherwise, create an
1172 ;;; anonymous GLOBAL-VAR.
1173 (defun specvar-for-binding (name)
1174   (cond ((not (eq (info :variable :where-from name) :assumed))
1175          (let ((found (find-free-var name)))
1176            (when (heap-alien-info-p found)
1177              (compiler-error
1178               "~S is an alien variable and so can't be declared special."
1179               name))
1180            (unless (global-var-p found)
1181              (compiler-error
1182               "~S is a constant and so can't be declared special."
1183               name))
1184            found))
1185         (t
1186          (make-global-var :kind :special
1187                           :%source-name name
1188                           :where-from :declared))))