f1570193a0f5eb2a395b55801eaeca640f6a2b66
[sbcl.git] / src / compiler / ir1tran-lambda.lisp
1 ;;;; This file contains code which does the translation of lambda
2 ;;;; forms from Lisp code to the first intermediate representation
3 ;;;; (IR1).
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 (in-package "SB!C")
15
16 ;;;; LAMBDA hackery
17
18 ;;;; Note: Take a look at the compiler-overview.tex section on "Hairy
19 ;;;; function representation" before you seriously mess with this
20 ;;;; stuff.
21
22 ;;; Verify that the NAME is a legal name for a variable and return a
23 ;;; VAR structure for it, filling in info if it is globally special.
24 ;;; If it is losing, we punt with a COMPILER-ERROR. NAMES-SO-FAR is a
25 ;;; list of names which have previously been bound. If the NAME is in
26 ;;; this list, then we error out.
27 (declaim (ftype (sfunction (t list) lambda-var) varify-lambda-arg))
28 (defun varify-lambda-arg (name names-so-far)
29   (declare (inline member))
30   (unless (symbolp name)
31     (compiler-error "The lambda variable ~S is not a symbol." name))
32   (when (member name names-so-far :test #'eq)
33     (compiler-error "The variable ~S occurs more than once in the lambda list."
34                     name))
35   (let ((kind (info :variable :kind name)))
36     (when (or (keywordp name) (eq kind :constant))
37       (compiler-error "The name of the lambda variable ~S is already in use to name a constant."
38                       name))
39     (cond ((eq kind :special)
40            (let ((specvar (find-free-var name)))
41              (make-lambda-var :%source-name name
42                               :type (leaf-type specvar)
43                               :where-from (leaf-where-from specvar)
44                               :specvar specvar)))
45           (t
46            (make-lambda-var :%source-name name)))))
47
48 ;;; Make the default keyword for a &KEY arg, checking that the keyword
49 ;;; isn't already used by one of the VARS.
50 (declaim (ftype (sfunction (symbol list t) symbol) make-keyword-for-arg))
51 (defun make-keyword-for-arg (symbol vars keywordify)
52   (let ((key (if (and keywordify (not (keywordp symbol)))
53                  (keywordicate symbol)
54                  symbol)))
55     (dolist (var vars)
56       (let ((info (lambda-var-arg-info var)))
57         (when (and info
58                    (eq (arg-info-kind info) :keyword)
59                    (eq (arg-info-key info) key))
60           (compiler-error
61            "The keyword ~S appears more than once in the lambda list."
62            key))))
63     key))
64
65 ;;; Parse a lambda list into a list of VAR structures, stripping off
66 ;;; any &AUX bindings. Each arg name is checked for legality, and
67 ;;; duplicate names are checked for. If an arg is globally special,
68 ;;; the var is marked as :SPECIAL instead of :LEXICAL. &KEY,
69 ;;; &OPTIONAL and &REST args are annotated with an ARG-INFO structure
70 ;;; which contains the extra information. If we hit something losing,
71 ;;; we bug out with COMPILER-ERROR. These values are returned:
72 ;;;  1. a list of the var structures for each top level argument;
73 ;;;  2. a flag indicating whether &KEY was specified;
74 ;;;  3. a flag indicating whether other &KEY args are allowed;
75 ;;;  4. a list of the &AUX variables; and
76 ;;;  5. a list of the &AUX values.
77 (declaim (ftype (sfunction (list) (values list boolean boolean list list))
78                 make-lambda-vars))
79 (defun make-lambda-vars (list)
80   (multiple-value-bind (required optional restp rest keyp keys allowp auxp aux
81                         morep more-context more-count)
82       (parse-lambda-list list)
83     (declare (ignore auxp)) ; since we just iterate over AUX regardless
84     (collect ((vars)
85               (names-so-far)
86               (aux-vars)
87               (aux-vals))
88       (flet (;; PARSE-DEFAULT deals with defaults and supplied-p args
89              ;; for optionals and keywords args.
90              (parse-default (spec info)
91                (when (consp (cdr spec))
92                  (setf (arg-info-default info) (second spec))
93                  (when (consp (cddr spec))
94                    (let* ((supplied-p (third spec))
95                           (supplied-var (varify-lambda-arg supplied-p
96                                                            (names-so-far))))
97                      (setf (arg-info-supplied-p info) supplied-var)
98                      (names-so-far supplied-p)
99                      (when (> (length (the list spec)) 3)
100                        (compiler-error
101                         "The list ~S is too long to be an arg specifier."
102                         spec)))))))
103
104         (dolist (name required)
105           (let ((var (varify-lambda-arg name (names-so-far))))
106             (vars var)
107             (names-so-far name)))
108
109         (dolist (spec optional)
110           (if (atom spec)
111               (let ((var (varify-lambda-arg spec (names-so-far))))
112                 (setf (lambda-var-arg-info var)
113                       (make-arg-info :kind :optional))
114                 (vars var)
115                 (names-so-far spec))
116               (let* ((name (first spec))
117                      (var (varify-lambda-arg name (names-so-far)))
118                      (info (make-arg-info :kind :optional)))
119                 (setf (lambda-var-arg-info var) info)
120                 (vars var)
121                 (names-so-far name)
122                 (parse-default spec info))))
123
124         (when restp
125           (let ((var (varify-lambda-arg rest (names-so-far))))
126             (setf (lambda-var-arg-info var) (make-arg-info :kind :rest))
127             (vars var)
128             (names-so-far rest)))
129
130         (when morep
131           (let ((var (varify-lambda-arg more-context (names-so-far))))
132             (setf (lambda-var-arg-info var)
133                   (make-arg-info :kind :more-context))
134             (vars var)
135             (names-so-far more-context))
136           (let ((var (varify-lambda-arg more-count (names-so-far))))
137             (setf (lambda-var-arg-info var)
138                   (make-arg-info :kind :more-count))
139             (vars var)
140             (names-so-far more-count)))
141
142         (dolist (spec keys)
143           (cond
144            ((atom spec)
145             (let ((var (varify-lambda-arg spec (names-so-far))))
146               (setf (lambda-var-arg-info var)
147                     (make-arg-info :kind :keyword
148                                    :key (make-keyword-for-arg spec
149                                                               (vars)
150                                                               t)))
151               (vars var)
152               (names-so-far spec)))
153            ((atom (first spec))
154             (let* ((name (first spec))
155                    (var (varify-lambda-arg name (names-so-far)))
156                    (info (make-arg-info
157                           :kind :keyword
158                           :key (make-keyword-for-arg name (vars) t))))
159               (setf (lambda-var-arg-info var) info)
160               (vars var)
161               (names-so-far name)
162               (parse-default spec info)))
163            (t
164             (let ((head (first spec)))
165               (unless (proper-list-of-length-p head 2)
166                 (error "malformed &KEY argument specifier: ~S" spec))
167               (let* ((name (second head))
168                      (var (varify-lambda-arg name (names-so-far)))
169                      (info (make-arg-info
170                             :kind :keyword
171                             :key (make-keyword-for-arg (first head)
172                                                        (vars)
173                                                        nil))))
174                 (setf (lambda-var-arg-info var) info)
175                 (vars var)
176                 (names-so-far name)
177                 (parse-default spec info))))))
178
179         (dolist (spec aux)
180           (cond ((atom spec)
181                  (let ((var (varify-lambda-arg spec nil)))
182                    (aux-vars var)
183                    (aux-vals nil)
184                    (names-so-far spec)))
185                 (t
186                  (unless (proper-list-of-length-p spec 1 2)
187                    (compiler-error "malformed &AUX binding specifier: ~S"
188                                    spec))
189                  (let* ((name (first spec))
190                         (var (varify-lambda-arg name nil)))
191                    (aux-vars var)
192                    (aux-vals (second spec))
193                    (names-so-far name)))))
194
195         (values (vars) keyp allowp (aux-vars) (aux-vals))))))
196
197 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that we
198 ;;; sequentially bind each AUX-VAR to the corresponding AUX-VAL before
199 ;;; converting the body. If there are no bindings, just convert the
200 ;;; body, otherwise do one binding and recurse on the rest.
201 ;;;
202 ;;; FIXME: This could and probably should be converted to use
203 ;;; SOURCE-NAME and DEBUG-NAME. But I (WHN) don't use &AUX bindings,
204 ;;; so I'm not motivated. Patches will be accepted...
205 (defun ir1-convert-aux-bindings (start next result body aux-vars aux-vals
206                                  post-binding-lexenv)
207   (declare (type ctran start next) (type (or lvar null) result)
208            (list body aux-vars aux-vals))
209   (if (null aux-vars)
210       (let ((*lexenv* (make-lexenv :vars (copy-list post-binding-lexenv))))
211         (ir1-convert-progn-body start next result body))
212       (let ((ctran (make-ctran))
213             (fun-lvar (make-lvar))
214             (fun (ir1-convert-lambda-body body
215                                           (list (first aux-vars))
216                                           :aux-vars (rest aux-vars)
217                                           :aux-vals (rest aux-vals)
218                                           :post-binding-lexenv post-binding-lexenv
219                                           :debug-name (debug-name
220                                                        '&aux-bindings
221                                                        aux-vars))))
222         (reference-leaf start ctran fun-lvar fun)
223         (ir1-convert-combination-args fun-lvar ctran next result
224                                       (list (first aux-vals)))))
225   (values))
226
227 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that code to bind
228 ;;; the SPECVAR for each SVAR to the value of the variable is wrapped
229 ;;; around the body. If there are no special bindings, we just convert
230 ;;; the body, otherwise we do one special binding and recurse on the
231 ;;; rest.
232 ;;;
233 ;;; We make a cleanup and introduce it into the lexical
234 ;;; environment. If there are multiple special bindings, the cleanup
235 ;;; for the blocks will end up being the innermost one. We force NEXT
236 ;;; to start a block outside of this cleanup, causing cleanup code to
237 ;;; be emitted when the scope is exited.
238 (defun ir1-convert-special-bindings
239     (start next result body aux-vars aux-vals svars post-binding-lexenv)
240   (declare (type ctran start next) (type (or lvar null) result)
241            (list body aux-vars aux-vals svars))
242   (cond
243    ((null svars)
244     (ir1-convert-aux-bindings start next result body aux-vars aux-vals
245                               post-binding-lexenv))
246    (t
247     (ctran-starts-block next)
248     (let ((cleanup (make-cleanup :kind :special-bind))
249           (var (first svars))
250           (bind-ctran (make-ctran))
251           (cleanup-ctran (make-ctran)))
252       (ir1-convert start bind-ctran nil
253                    `(%special-bind ',(lambda-var-specvar var) ,var))
254       (setf (cleanup-mess-up cleanup) (ctran-use bind-ctran))
255       (let ((*lexenv* (make-lexenv :cleanup cleanup)))
256         (ir1-convert bind-ctran cleanup-ctran nil '(%cleanup-point))
257         (ir1-convert-special-bindings cleanup-ctran next result
258                                       body aux-vars aux-vals
259                                       (rest svars)
260                                       post-binding-lexenv)))))
261   (values))
262
263 ;;; Create a lambda node out of some code, returning the result. The
264 ;;; bindings are specified by the list of VAR structures VARS. We deal
265 ;;; with adding the names to the LEXENV-VARS for the conversion. The
266 ;;; result is added to the NEW-FUNCTIONALS in the *CURRENT-COMPONENT*
267 ;;; and linked to the component head and tail.
268 ;;;
269 ;;; We detect special bindings here, replacing the original VAR in the
270 ;;; lambda list with a temporary variable. We then pass a list of the
271 ;;; special vars to IR1-CONVERT-SPECIAL-BINDINGS, which actually emits
272 ;;; the special binding code.
273 ;;;
274 ;;; We ignore any ARG-INFO in the VARS, trusting that someone else is
275 ;;; dealing with &NONSENSE, except for &REST vars with DYNAMIC-EXTENT.
276 ;;;
277 ;;; AUX-VARS is a list of VAR structures for variables that are to be
278 ;;; sequentially bound. Each AUX-VAL is a form that is to be evaluated
279 ;;; to get the initial value for the corresponding AUX-VAR.
280 (defun ir1-convert-lambda-body (body
281                                 vars
282                                 &key
283                                 aux-vars
284                                 aux-vals
285                                 (source-name '.anonymous.)
286                                 debug-name
287                                 (note-lexical-bindings t)
288                                 post-binding-lexenv)
289   (declare (list body vars aux-vars aux-vals))
290
291   ;; We're about to try to put new blocks into *CURRENT-COMPONENT*.
292   (aver-live-component *current-component*)
293
294   (let* ((bind (make-bind))
295          (lambda (make-lambda :vars vars
296                   :bind bind
297                   :%source-name source-name
298                   :%debug-name debug-name))
299          (result-ctran (make-ctran))
300          (result-lvar (make-lvar)))
301
302     (awhen (lexenv-lambda *lexenv*)
303       (push lambda (lambda-children it))
304       (setf (lambda-parent lambda) it))
305
306     ;; just to check: This function should fail internal assertions if
307     ;; we didn't set up a valid debug name above.
308     ;;
309     ;; (In SBCL we try to make everything have a debug name, since we
310     ;; lack the omniscient perspective the original implementors used
311     ;; to decide which things didn't need one.)
312     (functional-debug-name lambda)
313
314     (setf (lambda-home lambda) lambda)
315     (collect ((svars)
316               (new-venv nil cons))
317
318       (dolist (var vars)
319         ;; As far as I can see, LAMBDA-VAR-HOME should never have
320         ;; been set before. Let's make sure. -- WHN 2001-09-29
321         (aver (not (lambda-var-home var)))
322         (setf (lambda-var-home var) lambda)
323         (let ((specvar (lambda-var-specvar var)))
324           (cond (specvar
325                  (svars var)
326                  (new-venv (cons (leaf-source-name specvar) specvar)))
327                 (t
328                  (when note-lexical-bindings
329                    (note-lexical-binding (leaf-source-name var)))
330                  (new-venv (cons (leaf-source-name var) var))))))
331
332       (let ((*lexenv* (make-lexenv :vars (new-venv)
333                                    :lambda lambda
334                                    :cleanup nil)))
335         (setf (bind-lambda bind) lambda)
336         (setf (node-lexenv bind) *lexenv*)
337
338         (let ((block (ctran-starts-block result-ctran)))
339           (let ((return (make-return :result result-lvar :lambda lambda))
340                 (tail-set (make-tail-set :funs (list lambda))))
341             (setf (lambda-tail-set lambda) tail-set)
342             (setf (lambda-return lambda) return)
343             (setf (lvar-dest result-lvar) return)
344             (link-node-to-previous-ctran return result-ctran)
345             (setf (block-last block) return))
346           (link-blocks block (component-tail *current-component*)))
347
348         (with-component-last-block (*current-component*
349                                     (ctran-block result-ctran))
350           (let ((prebind-ctran (make-ctran))
351                 (postbind-ctran (make-ctran)))
352             (ctran-starts-block prebind-ctran)
353             (link-node-to-previous-ctran bind prebind-ctran)
354             (use-ctran bind postbind-ctran)
355             (ir1-convert-special-bindings postbind-ctran result-ctran
356                                           result-lvar body
357                                           aux-vars aux-vals (svars)
358                                           post-binding-lexenv)))))
359
360     (link-blocks (component-head *current-component*) (node-block bind))
361     (push lambda (component-new-functionals *current-component*))
362
363     lambda))
364
365 ;;; Entry point CLAMBDAs have a special kind
366 (defun register-entry-point (entry dispatcher)
367   (declare (type clambda entry)
368            (type optional-dispatch dispatcher))
369   (setf (functional-kind entry) :optional)
370   (setf (leaf-ever-used entry) t)
371   (setf (lambda-optional-dispatch entry) dispatcher)
372   entry)
373
374 ;;; Create the actual entry-point function for an optional entry
375 ;;; point. The lambda binds copies of each of the VARS, then calls FUN
376 ;;; with the argument VALS and the DEFAULTS. Presumably the VALS refer
377 ;;; to the VARS by name. The VALS are passed in the reverse order.
378 ;;;
379 ;;; If any of the copies of the vars are referenced more than once,
380 ;;; then we mark the corresponding var as EVER-USED to inhibit
381 ;;; "defined but not read" warnings for arguments that are only used
382 ;;; by default forms.
383 (defun convert-optional-entry (fun vars vals defaults name)
384   (declare (type clambda fun) (list vars vals defaults))
385   (let* ((fvars (reverse vars))
386          (arg-vars (mapcar (lambda (var)
387                              (make-lambda-var
388                               :%source-name (leaf-source-name var)
389                               :type (leaf-type var)
390                               :where-from (leaf-where-from var)
391                               :specvar (lambda-var-specvar var)))
392                            fvars))
393          (fun (collect ((default-bindings)
394                         (default-vals))
395                 (dolist (default defaults)
396                   (if (constantp default)
397                       (default-vals default)
398                       (let ((var (gensym)))
399                         (default-bindings `(,var ,default))
400                         (default-vals var))))
401                 (ir1-convert-lambda-body `((let (,@(default-bindings))
402                                              (%funcall ,fun
403                                                        ,@(reverse vals)
404                                                        ,@(default-vals))))
405                                          arg-vars
406                                          ;; FIXME: Would be nice to
407                                          ;; share these names instead
408                                          ;; of consing up several
409                                          ;; identical ones. Oh well.
410                                          :debug-name (debug-name
411                                                       '&optional-processor
412                                                       name)
413                                          :note-lexical-bindings nil))))
414     (mapc (lambda (var arg-var)
415             (when (cdr (leaf-refs arg-var))
416               (setf (leaf-ever-used var) t)))
417           fvars arg-vars)
418     fun))
419
420 ;;; This function deals with supplied-p vars in optional arguments. If
421 ;;; the there is no supplied-p arg, then we just call
422 ;;; IR1-CONVERT-HAIRY-ARGS on the remaining arguments, and generate a
423 ;;; optional entry that calls the result. If there is a supplied-p
424 ;;; var, then we add it into the default vars and throw a T into the
425 ;;; entry values. The resulting entry point function is returned.
426 (defun generate-optional-default-entry (res default-vars default-vals
427                                         entry-vars entry-vals
428                                         vars supplied-p-p body
429                                         aux-vars aux-vals
430                                         source-name debug-name
431                                         force post-binding-lexenv)
432   (declare (type optional-dispatch res)
433            (list default-vars default-vals entry-vars entry-vals vars body
434                  aux-vars aux-vals))
435   (let* ((arg (first vars))
436          (arg-name (leaf-source-name arg))
437          (info (lambda-var-arg-info arg))
438          (default (arg-info-default info))
439          (supplied-p (arg-info-supplied-p info))
440          (force (or force
441                     (not (sb!xc:constantp (arg-info-default info)))))
442          (ep (if supplied-p
443                  (ir1-convert-hairy-args
444                   res
445                   (list* supplied-p arg default-vars)
446                   (list* (leaf-source-name supplied-p) arg-name default-vals)
447                   (cons arg entry-vars)
448                   (list* t arg-name entry-vals)
449                   (rest vars) t body aux-vars aux-vals
450                   source-name debug-name
451                   force post-binding-lexenv)
452                  (ir1-convert-hairy-args
453                   res
454                   (cons arg default-vars)
455                   (cons arg-name default-vals)
456                   (cons arg entry-vars)
457                   (cons arg-name entry-vals)
458                   (rest vars) supplied-p-p body aux-vars aux-vals
459                   source-name debug-name
460                   force post-binding-lexenv))))
461
462     ;; We want to delay converting the entry, but there exist
463     ;; problems: hidden references should not be established to
464     ;; lambdas of kind NIL should not have (otherwise the compiler
465     ;; might let-convert or delete them) and to variables.
466     (let ((name (or debug-name source-name)))
467       (if (or force
468               supplied-p-p ; this entry will be of kind NIL
469               (and (lambda-p ep) (eq (lambda-kind ep) nil)))
470           (convert-optional-entry ep
471                                   default-vars default-vals
472                                   (if supplied-p (list default nil) (list default))
473                                   name)
474           (let* ((default `',(constant-form-value default))
475                  (defaults (if supplied-p (list default nil) (list default))))
476             ;; DEFAULT can contain a reference to a
477             ;; to-be-optimized-away function/block/tag, so better to
478             ;; reduce code now (but we possibly lose syntax checking
479             ;; in an unreachable code).
480             (delay
481              (register-entry-point
482               (convert-optional-entry (force ep)
483                                       default-vars default-vals
484                                       defaults
485                                       name)
486               res)))))))
487
488 ;;; Create the MORE-ENTRY function for the OPTIONAL-DISPATCH RES.
489 ;;; ENTRY-VARS and ENTRY-VALS describe the fixed arguments. REST is
490 ;;; the var for any &REST arg. KEYS is a list of the &KEY arg vars.
491 ;;;
492 ;;; The most interesting thing that we do is parse keywords. We create
493 ;;; a bunch of temporary variables to hold the result of the parse,
494 ;;; and then loop over the supplied arguments, setting the appropriate
495 ;;; temps for the supplied keyword. Note that it is significant that
496 ;;; we iterate over the keywords in reverse order --- this implements
497 ;;; the CL requirement that (when a keyword appears more than once)
498 ;;; the first value is used.
499 ;;;
500 ;;; If there is no supplied-p var, then we initialize the temp to the
501 ;;; default and just pass the temp into the main entry. Since
502 ;;; non-constant &KEY args are forcibly given a supplied-p var, we
503 ;;; know that the default is constant, and thus safe to evaluate out
504 ;;; of order.
505 ;;;
506 ;;; If there is a supplied-p var, then we create temps for both the
507 ;;; value and the supplied-p, and pass them into the main entry,
508 ;;; letting it worry about defaulting.
509 ;;;
510 ;;; We deal with :ALLOW-OTHER-KEYS by delaying unknown keyword errors
511 ;;; until we have scanned all the keywords.
512 (defun convert-more-entry (res entry-vars entry-vals rest morep keys name)
513   (declare (type optional-dispatch res) (list entry-vars entry-vals keys))
514   (collect ((arg-vars)
515             (arg-vals (reverse entry-vals))
516             (temps)
517             (body))
518
519     (dolist (var (reverse entry-vars))
520       (arg-vars (make-lambda-var :%source-name (leaf-source-name var)
521                                  :type (leaf-type var)
522                                  :where-from (leaf-where-from var))))
523
524     (let* ((*allow-instrumenting* nil)
525            (n-context (gensym "N-CONTEXT-"))
526            (context-temp (make-lambda-var :%source-name n-context))
527            (n-count (gensym "N-COUNT-"))
528            (count-temp (make-lambda-var :%source-name n-count
529                                         :type (specifier-type 'index))))
530
531       (arg-vars context-temp count-temp)
532
533       (when rest
534         (arg-vals `(%listify-rest-args
535                     ,n-context ,n-count)))
536       (when morep
537         (arg-vals n-context)
538         (arg-vals n-count))
539
540       ;; The reason for all the noise with
541       ;; STACK-GROWS-DOWNWARD-NOT-UPWARD is to enable generation of
542       ;; slightly more efficient code on x86oid processors.  (We can
543       ;; hoist the negation of the index outside the main parsing loop
544       ;; and take advantage of the base+index+displacement addressing
545       ;; mode on x86oids.)
546       (when (optional-dispatch-keyp res)
547         (let ((n-index (gensym "N-INDEX-"))
548               (n-key (gensym "N-KEY-"))
549               (n-value-temp (gensym "N-VALUE-TEMP-"))
550               (n-allowp (gensym "N-ALLOWP-"))
551               (n-losep (gensym "N-LOSEP-"))
552               (allowp (or (optional-dispatch-allowp res)
553                           (policy *lexenv* (zerop safety))))
554               (found-allow-p nil))
555
556           (temps #!-stack-grows-downward-not-upward
557                  `(,n-index (1- ,n-count))
558                  #!+stack-grows-downward-not-upward
559                  `(,n-index (- (1- ,n-count)))
560                  #!-stack-grows-downward-not-upward n-value-temp
561                  #!-stack-grows-downward-not-upward n-key)
562           (body `(declare (fixnum ,n-index)
563                           #!-stack-grows-downward-not-upward
564                           (ignorable ,n-value-temp ,n-key)))
565
566           (collect ((tests))
567             (dolist (key keys)
568               (let* ((info (lambda-var-arg-info key))
569                      (default (arg-info-default info))
570                      (keyword (arg-info-key info))
571                      (supplied-p (arg-info-supplied-p info))
572                      (n-value (gensym "N-VALUE-"))
573                      (clause (cond (supplied-p
574                                     (let ((n-supplied (gensym "N-SUPPLIED-")))
575                                       (temps n-supplied)
576                                       (arg-vals n-value n-supplied)
577                                       `((eq ,n-key ',keyword)
578                                         (setq ,n-supplied t)
579                                         (setq ,n-value ,n-value-temp))))
580                                    (t
581                                     (arg-vals n-value)
582                                     `((eq ,n-key ',keyword)
583                                       (setq ,n-value ,n-value-temp))))))
584                 (when (and (not allowp) (eq keyword :allow-other-keys))
585                   (setq found-allow-p t)
586                   (setq clause
587                         (append clause `((setq ,n-allowp ,n-value-temp)))))
588
589                 (temps `(,n-value ,default))
590                 (tests clause)))
591
592             (unless allowp
593               (temps n-allowp n-losep)
594               (unless found-allow-p
595                 (tests `((eq ,n-key :allow-other-keys)
596                          (setq ,n-allowp ,n-value-temp))))
597               (tests `(t
598                        (setq ,n-losep (list ,n-key)))))
599
600             (body
601              `(when (oddp ,n-count)
602                 (%odd-key-args-error)))
603
604             (body
605              #!-stack-grows-downward-not-upward
606              `(locally
607                 (declare (optimize (safety 0)))
608                 (loop
609                   (when (minusp ,n-index) (return))
610                   (setf ,n-value-temp (%more-arg ,n-context ,n-index))
611                   (decf ,n-index)
612                   (setq ,n-key (%more-arg ,n-context ,n-index))
613                   (decf ,n-index)
614                   (cond ,@(tests))))
615              #!+stack-grows-downward-not-upward
616              `(locally (declare (optimize (safety 0)))
617                 (loop
618                   (when (plusp ,n-index) (return))
619                   (multiple-value-bind (,n-value-temp ,n-key)
620                       (%more-kw-arg ,n-context ,n-index)
621                     (declare (ignorable ,n-value-temp ,n-key))
622                     (incf ,n-index 2)
623                     (cond ,@(tests))))))
624
625             (unless allowp
626               (body `(when (and ,n-losep (not ,n-allowp))
627                        (%unknown-key-arg-error (car ,n-losep))))))))
628
629       (let ((ep (ir1-convert-lambda-body
630                  `((let ,(temps)
631                      ,@(body)
632                      (%funcall ,(optional-dispatch-main-entry res)
633                                ,@(arg-vals))))
634                  (arg-vars)
635                  :debug-name (debug-name '&more-processor name)
636                  :note-lexical-bindings nil)))
637         (setf (optional-dispatch-more-entry res)
638               (register-entry-point ep res)))))
639
640   (values))
641
642 ;;; This is called by IR1-CONVERT-HAIRY-ARGS when we run into a &REST
643 ;;; or &KEY arg. The arguments are similar to that function, but we
644 ;;; split off any &REST arg and pass it in separately. REST is the
645 ;;; &REST arg var, or NIL if there is no &REST arg. KEYS is a list of
646 ;;; the &KEY argument vars.
647 ;;;
648 ;;; When there are &KEY arguments, we introduce temporary gensym
649 ;;; variables to hold the values while keyword defaulting is in
650 ;;; progress to get the required sequential binding semantics.
651 ;;;
652 ;;; This gets interesting mainly when there are &KEY arguments with
653 ;;; supplied-p vars or non-constant defaults. In either case, pass in
654 ;;; a supplied-p var. If the default is non-constant, we introduce an
655 ;;; IF in the main entry that tests the supplied-p var and decides
656 ;;; whether to evaluate the default or not. In this case, the real
657 ;;; incoming value is NIL, so we must union NULL with the declared
658 ;;; type when computing the type for the main entry's argument.
659 (defun ir1-convert-more (res default-vars default-vals entry-vars entry-vals
660                              rest more-context more-count keys supplied-p-p
661                              body aux-vars aux-vals
662                              source-name debug-name post-binding-lexenv)
663   (declare (type optional-dispatch res)
664            (list default-vars default-vals entry-vars entry-vals keys body
665                  aux-vars aux-vals))
666   (collect ((main-vars (reverse default-vars))
667             (main-vals default-vals cons)
668             (bind-vars)
669             (bind-vals))
670     (when rest
671       (main-vars rest)
672       (main-vals '()))
673     (when more-context
674       (main-vars more-context)
675       (main-vals nil)
676       (main-vars more-count)
677       (main-vals 0))
678
679     (dolist (key keys)
680       (let* ((info (lambda-var-arg-info key))
681              (default (arg-info-default info))
682              (hairy-default (not (sb!xc:constantp default)))
683              (supplied-p (arg-info-supplied-p info))
684              (n-val (make-symbol (format nil
685                                          "~A-DEFAULTING-TEMP"
686                                          (leaf-source-name key))))
687              (val-temp (make-lambda-var :%source-name n-val)))
688         (main-vars val-temp)
689         (bind-vars key)
690         (cond ((or hairy-default supplied-p)
691                (let* ((n-supplied (gensym "N-SUPPLIED-"))
692                       (supplied-temp (make-lambda-var
693                                       :%source-name n-supplied)))
694                  (unless supplied-p
695                    (setf (arg-info-supplied-p info) supplied-temp))
696                  (when hairy-default
697                    (setf (arg-info-default info) nil))
698                  (main-vars supplied-temp)
699                  (cond (hairy-default
700                         (main-vals nil nil)
701                         (bind-vals `(if ,n-supplied ,n-val ,default)))
702                        (t
703                         (main-vals default nil)
704                         (bind-vals n-val)))
705                  (when supplied-p
706                    (bind-vars supplied-p)
707                    (bind-vals n-supplied))))
708               (t
709                (main-vals (arg-info-default info))
710                (bind-vals n-val)))))
711
712     (let* ((name (or debug-name source-name))
713            (main-entry (ir1-convert-lambda-body
714                         body (main-vars)
715                         :aux-vars (append (bind-vars) aux-vars)
716                         :aux-vals (append (bind-vals) aux-vals)
717                         :post-binding-lexenv post-binding-lexenv
718                         :debug-name (debug-name 'varargs-entry name)))
719            (last-entry (convert-optional-entry main-entry default-vars
720                                                (main-vals) () name)))
721       (setf (optional-dispatch-main-entry res)
722             (register-entry-point main-entry res))
723       (convert-more-entry res entry-vars entry-vals rest more-context keys
724                           name)
725
726       (push (register-entry-point
727              (if supplied-p-p
728                 (convert-optional-entry last-entry entry-vars entry-vals
729                                         () name)
730                 last-entry)
731              res)
732             (optional-dispatch-entry-points res))
733       last-entry)))
734
735 ;;; This function generates the entry point functions for the
736 ;;; OPTIONAL-DISPATCH RES. We accomplish this by recursion on the list
737 ;;; of arguments, analyzing the arglist on the way down and generating
738 ;;; entry points on the way up.
739 ;;;
740 ;;; DEFAULT-VARS is a reversed list of all the argument vars processed
741 ;;; so far, including supplied-p vars. DEFAULT-VALS is a list of the
742 ;;; names of the DEFAULT-VARS.
743 ;;;
744 ;;; ENTRY-VARS is a reversed list of processed argument vars,
745 ;;; excluding supplied-p vars. ENTRY-VALS is a list things that can be
746 ;;; evaluated to get the values for all the vars from the ENTRY-VARS.
747 ;;; It has the var name for each required or optional arg, and has T
748 ;;; for each supplied-p arg.
749 ;;;
750 ;;; VARS is a list of the LAMBDA-VAR structures for arguments that
751 ;;; haven't been processed yet. SUPPLIED-P-P is true if a supplied-p
752 ;;; argument has already been processed; only in this case are the
753 ;;; DEFAULT-XXX and ENTRY-XXX different.
754 ;;;
755 ;;; The result at each point is a lambda which should be called by the
756 ;;; above level to default the remaining arguments and evaluate the
757 ;;; body. We cause the body to be evaluated by converting it and
758 ;;; returning it as the result when the recursion bottoms out.
759 ;;;
760 ;;; Each level in the recursion also adds its entry point function to
761 ;;; the result OPTIONAL-DISPATCH. For most arguments, the defaulting
762 ;;; function and the entry point function will be the same, but when
763 ;;; SUPPLIED-P args are present they may be different.
764 ;;;
765 ;;; When we run into a &REST or &KEY arg, we punt out to
766 ;;; IR1-CONVERT-MORE, which finishes for us in this case.
767 (defun ir1-convert-hairy-args (res default-vars default-vals
768                                entry-vars entry-vals
769                                vars supplied-p-p body aux-vars
770                                aux-vals
771                                source-name debug-name
772                                force post-binding-lexenv)
773   (declare (type optional-dispatch res)
774            (list default-vars default-vals entry-vars entry-vals vars body
775                  aux-vars aux-vals))
776   (aver (or debug-name (neq '.anonymous. source-name)))
777   (cond ((not vars)
778          (if (optional-dispatch-keyp res)
779              ;; Handle &KEY with no keys...
780              (ir1-convert-more res default-vars default-vals
781                                entry-vars entry-vals
782                                nil nil nil vars supplied-p-p body aux-vars
783                                aux-vals source-name debug-name
784                                post-binding-lexenv)
785              (let* ((name (or debug-name source-name))
786                     (fun (ir1-convert-lambda-body
787                          body (reverse default-vars)
788                          :aux-vars aux-vars
789                          :aux-vals aux-vals
790                          :post-binding-lexenv post-binding-lexenv
791                          :debug-name (debug-name 'hairy-arg-processor name))))
792
793                (setf (optional-dispatch-main-entry res) fun)
794                (register-entry-point fun res)
795                (push (if supplied-p-p
796                          (register-entry-point
797                           (convert-optional-entry fun entry-vars entry-vals ()
798                                                   name)
799                           res)
800                           fun)
801                      (optional-dispatch-entry-points res))
802                fun)))
803         ((not (lambda-var-arg-info (first vars)))
804          (let* ((arg (first vars))
805                 (nvars (cons arg default-vars))
806                 (nvals (cons (leaf-source-name arg) default-vals)))
807            (ir1-convert-hairy-args res nvars nvals nvars nvals
808                                    (rest vars) nil body aux-vars aux-vals
809                                    source-name debug-name
810                                    nil post-binding-lexenv)))
811         (t
812          (let* ((arg (first vars))
813                 (info (lambda-var-arg-info arg))
814                 (kind (arg-info-kind info)))
815            (ecase kind
816              (:optional
817               (let ((ep (generate-optional-default-entry
818                          res default-vars default-vals
819                          entry-vars entry-vals vars supplied-p-p body
820                          aux-vars aux-vals
821                          source-name debug-name
822                          force post-binding-lexenv)))
823                 ;; See GENERATE-OPTIONAL-DEFAULT-ENTRY.
824                 (push (if (lambda-p ep)
825                           (register-entry-point
826                            (if supplied-p-p
827                                (convert-optional-entry
828                                 ep entry-vars entry-vals nil
829                                 (or debug-name source-name))
830                                ep)
831                            res)
832                           (progn (aver (not supplied-p-p))
833                                  ep))
834                       (optional-dispatch-entry-points res))
835                 ep))
836              (:rest
837               (ir1-convert-more res default-vars default-vals
838                                 entry-vars entry-vals
839                                 arg nil nil (rest vars) supplied-p-p body
840                                 aux-vars aux-vals
841                                 source-name debug-name
842                                 post-binding-lexenv))
843              (:more-context
844               (ir1-convert-more res default-vars default-vals
845                                 entry-vars entry-vals
846                                 nil arg (second vars) (cddr vars) supplied-p-p
847                                 body aux-vars aux-vals
848                                 source-name debug-name
849                                 post-binding-lexenv))
850              (:keyword
851               (ir1-convert-more res default-vars default-vals
852                                 entry-vars entry-vals
853                                 nil nil nil vars supplied-p-p body aux-vars
854                                 aux-vals source-name debug-name
855                                 post-binding-lexenv)))))))
856
857 ;;; This function deals with the case where we have to make an
858 ;;; OPTIONAL-DISPATCH to represent a LAMBDA. We cons up the result and
859 ;;; call IR1-CONVERT-HAIRY-ARGS to do the work. When it is done, we
860 ;;; figure out the MIN-ARGS and MAX-ARGS.
861 (defun ir1-convert-hairy-lambda (body vars keyp allowp aux-vars aux-vals
862                                  &key post-binding-lexenv
863                                  (source-name '.anonymous.)
864                                  debug-name)
865   (declare (list body vars aux-vars aux-vals))
866   (aver (or debug-name (neq '.anonymous. source-name)))
867   (let ((res (make-optional-dispatch :arglist vars
868                                      :allowp allowp
869                                      :keyp keyp
870                                      :%source-name source-name
871                                      :%debug-name debug-name
872                                      :plist `(:ir1-environment
873                                               (,*lexenv*
874                                                ,*current-path*))))
875         (min (or (position-if #'lambda-var-arg-info vars) (length vars))))
876     (aver-live-component *current-component*)
877     (push res (component-new-functionals *current-component*))
878     (ir1-convert-hairy-args res () () () () vars nil body aux-vars aux-vals
879                             source-name debug-name nil post-binding-lexenv)
880     (setf (optional-dispatch-min-args res) min)
881     (setf (optional-dispatch-max-args res)
882           (+ (1- (length (optional-dispatch-entry-points res))) min))
883
884     res))
885
886 ;;; Convert a LAMBDA form into a LAMBDA leaf or an OPTIONAL-DISPATCH leaf.
887 (defun ir1-convert-lambda (form &key (source-name '.anonymous.)
888                            debug-name maybe-add-debug-catch)
889   (unless (consp form)
890     (compiler-error "A ~S was found when expecting a lambda expression:~%  ~S"
891                     (type-of form)
892                     form))
893   (unless (eq (car form) 'lambda)
894     (compiler-error "~S was expected but ~S was found:~%  ~S"
895                     'lambda
896                     (car form)
897                     form))
898   (unless (and (consp (cdr form)) (listp (cadr form)))
899     (compiler-error
900      "The lambda expression has a missing or non-list lambda list:~%  ~S"
901      form))
902   (unless (or debug-name (neq '.anonymous. source-name))
903     (setf debug-name (name-lambdalike form)))
904   (multiple-value-bind (vars keyp allow-other-keys aux-vars aux-vals)
905       (make-lambda-vars (cadr form))
906     (multiple-value-bind (forms decls) (parse-body (cddr form))
907       (binding* (((*lexenv* result-type post-binding-lexenv)
908                   (process-decls decls (append aux-vars vars) nil
909                                  :binding-form-p t))
910                  (debug-catch-p (and maybe-add-debug-catch
911                                      *allow-instrumenting*
912                                      (policy *lexenv*
913                                              (>= insert-debug-catch 2))))
914                  (forms (if debug-catch-p
915                             (wrap-forms-in-debug-catch forms)
916                             forms))
917                  (forms (if (eq result-type *wild-type*)
918                             forms
919                             `((the ,result-type (progn ,@forms)))))
920                  (res (if (or (find-if #'lambda-var-arg-info vars) keyp)
921                           (ir1-convert-hairy-lambda forms vars keyp
922                                                     allow-other-keys
923                                                     aux-vars aux-vals
924                                                     :post-binding-lexenv post-binding-lexenv
925                                                     :source-name source-name
926                                                     :debug-name debug-name)
927                           (ir1-convert-lambda-body forms vars
928                                                    :aux-vars aux-vars
929                                                    :aux-vals aux-vals
930                                                    :post-binding-lexenv post-binding-lexenv
931                                                    :source-name source-name
932                                                    :debug-name debug-name))))
933         (setf (functional-inline-expansion res) form)
934         (setf (functional-arg-documentation res) (cadr form))
935         (when (boundp '*lambda-conversions*)
936           ;; KLUDGE: Not counting TL-XEPs is a lie, of course, but
937           ;; keeps things less confusing to users of TIME, where this
938           ;; count gets used.
939           (unless (and (consp debug-name) (eq 'tl-xep (car debug-name)))
940             (incf *lambda-conversions*)))
941         res))))
942
943 (defun wrap-forms-in-debug-catch (forms)
944   #!+unwind-to-frame-and-call-vop
945   `((multiple-value-prog1
946       (progn
947         ,@forms)
948       ;; Just ensure that there won't be any tail-calls, IR2 magic will
949       ;; handle the rest.
950       (values)))
951   #!-unwind-to-frame-and-call-vop
952   `( ;; Normally, we'll return from this block with the below RETURN-FROM.
953     (block
954         return-value-tag
955       ;; If DEBUG-CATCH-TAG is thrown (with a thunk as the value) the
956       ;; RETURN-FROM is elided and we funcall the thunk instead. That
957       ;; thunk might either return a value (for a RETURN-FROM-FRAME)
958       ;; or call this same function again (for a RESTART-FRAME).
959       ;; -- JES, 2007-01-09
960       (funcall
961        (the function
962          ;; Use a constant catch tag instead of consing a new one for every
963          ;; entry to this block. The uniquencess of the catch tags is
964          ;; ensured when the tag is throw by the debugger. It'll allocate a
965          ;; new tag, and modify the reference this tag in the proper
966          ;; catch-block structure to refer to that new tag. This
967          ;; significantly decreases the runtime cost of high debug levels.
968          ;;  -- JES, 2007-01-09
969          (catch 'debug-catch-tag
970            (return-from return-value-tag
971              (progn
972                ,@forms))))))))
973
974 ;;; helper for LAMBDA-like things, to massage them into a form
975 ;;; suitable for IR1-CONVERT-LAMBDA.
976 (defun ir1-convert-lambdalike (thing
977                                &key
978                                (source-name '.anonymous.)
979                                debug-name)
980   (when (and (not debug-name) (eq '.anonymous. source-name))
981     (setf debug-name (name-lambdalike thing)))
982   (ecase (car thing)
983     ((lambda)
984      (ir1-convert-lambda thing
985                          :maybe-add-debug-catch t
986                          :source-name source-name
987                          :debug-name debug-name))
988     ((instance-lambda)
989      (deprecation-warning 'instance-lambda 'lambda)
990      (ir1-convert-lambda `(lambda ,@(cdr thing))
991                          :source-name source-name
992                          :debug-name debug-name))
993     ((named-lambda)
994      (let ((name (cadr thing))
995            (lambda-expression `(lambda ,@(cddr thing))))
996        (if (and name (legal-fun-name-p name))
997            (let ((defined-fun-res (get-defined-fun name))
998                  (res (ir1-convert-lambda lambda-expression
999                                           :maybe-add-debug-catch t
1000                                           :source-name name)))
1001              (assert-global-function-definition-type name res)
1002              (setf (defined-fun-functional defined-fun-res) res)
1003              (unless (eq (defined-fun-inlinep defined-fun-res) :notinline)
1004                (substitute-leaf-if
1005                 (lambda (ref)
1006                   (policy ref (> recognize-self-calls 0)))
1007                 res defined-fun-res))
1008              res)
1009            (ir1-convert-lambda lambda-expression
1010                                :maybe-add-debug-catch t
1011                                :debug-name
1012                                (or name (name-lambdalike thing))))))
1013     ((lambda-with-lexenv)
1014      (ir1-convert-inline-lambda thing
1015                                 :source-name source-name
1016                                 :debug-name debug-name))))
1017 \f
1018 ;;;; defining global functions
1019
1020 ;;; Convert FUN as a lambda in the null environment, but use the
1021 ;;; current compilation policy. Note that FUN may be a
1022 ;;; LAMBDA-WITH-LEXENV, so we may have to augment the environment to
1023 ;;; reflect the state at the definition site.
1024 (defun ir1-convert-inline-lambda (fun
1025                                   &key
1026                                   (source-name '.anonymous.)
1027                                   debug-name
1028                                   system-lambda)
1029   (when (and (not debug-name) (eq '.anonymous. source-name))
1030     (setf debug-name (name-lambdalike fun)))
1031   (destructuring-bind (decls macros symbol-macros &rest body)
1032       (if (eq (car fun) 'lambda-with-lexenv)
1033           (cdr fun)
1034           `(() () () . ,(cdr fun)))
1035     (let* ((*lexenv* (make-lexenv
1036                       :default (process-decls decls nil nil
1037                                               :lexenv (make-null-lexenv))
1038                       :vars (copy-list symbol-macros)
1039                       :funs (mapcar (lambda (x)
1040                                       `(,(car x) .
1041                                          (macro . ,(coerce (cdr x) 'function))))
1042                                     macros)
1043                       ;; Inherit MUFFLE-CONDITIONS from the call-site lexenv
1044                       ;; rather than the definition-site lexenv, since it seems
1045                       ;; like a much more common case.
1046                       :handled-conditions (lexenv-handled-conditions *lexenv*)
1047                       :policy (lexenv-policy *lexenv*)))
1048            (*allow-instrumenting* (and (not system-lambda)
1049                                        *allow-instrumenting*))
1050            (clambda (ir1-convert-lambda `(lambda ,@body)
1051                                         :source-name source-name
1052                                         :debug-name debug-name)))
1053       (setf (functional-inline-expanded clambda) t)
1054       clambda)))
1055
1056 ;;; Get a DEFINED-FUN object for a function we are about to define. If
1057 ;;; the function has been forward referenced, then substitute for the
1058 ;;; previous references.
1059 (defun get-defined-fun (name)
1060   (proclaim-as-fun-name name)
1061   (let ((found (find-free-fun name "shouldn't happen! (defined-fun)")))
1062     (note-name-defined name :function)
1063     (cond ((not (defined-fun-p found))
1064            (aver (not (info :function :inlinep name)))
1065            (let* ((where-from (leaf-where-from found))
1066                   (res (make-defined-fun
1067                         :%source-name name
1068                         :where-from (if (eq where-from :declared)
1069                                         :declared :defined)
1070                         :type (leaf-type found))))
1071              (substitute-leaf res found)
1072              (setf (gethash name *free-funs*) res)))
1073           ;; If *FREE-FUNS* has a previously converted definition
1074           ;; for this name, then blow it away and try again.
1075           ((defined-fun-functional found)
1076            (remhash name *free-funs*)
1077            (get-defined-fun name))
1078           (t found))))
1079
1080 ;;; Check a new global function definition for consistency with
1081 ;;; previous declaration or definition, and assert argument/result
1082 ;;; types if appropriate. This assertion is suppressed by the
1083 ;;; EXPLICIT-CHECK attribute, which is specified on functions that
1084 ;;; check their argument types as a consequence of type dispatching.
1085 ;;; This avoids redundant checks such as NUMBERP on the args to +, etc.
1086 (defun assert-new-definition (var fun)
1087   (let ((type (leaf-type var))
1088         (for-real (eq (leaf-where-from var) :declared))
1089         (info (info :function :info (leaf-source-name var))))
1090     (assert-definition-type
1091      fun type
1092      ;; KLUDGE: Common Lisp is such a dynamic language that in general
1093      ;; all we can do here in general is issue a STYLE-WARNING. It
1094      ;; would be nice to issue a full WARNING in the special case of
1095      ;; of type mismatches within a compilation unit (as in section
1096      ;; 3.2.2.3 of the spec) but at least as of sbcl-0.6.11, we don't
1097      ;; keep track of whether the mismatched data came from the same
1098      ;; compilation unit, so we can't do that. -- WHN 2001-02-11
1099      :lossage-fun #'compiler-style-warn
1100      :unwinnage-fun (cond (info #'compiler-style-warn)
1101                           (for-real #'compiler-notify)
1102                           (t nil))
1103      :really-assert
1104      (and for-real
1105           (not (and info
1106                     (ir1-attributep (fun-info-attributes info)
1107                                     explicit-check))))
1108      :where (if for-real
1109                 "previous declaration"
1110                 "previous definition"))))
1111
1112 ;;; Used for global inline expansion. Earlier something like this was
1113 ;;; used by %DEFUN too. FIXME: And now it's probably worth rethinking
1114 ;;; whether this function is a good idea at all.
1115 (defun ir1-convert-inline-expansion (name expansion var inlinep info)
1116   ;; Unless a :INLINE function, we temporarily clobber the inline
1117   ;; expansion. This prevents recursive inline expansion of
1118   ;; opportunistic pseudo-inlines.
1119   (unless (eq inlinep :inline)
1120     (setf (defined-fun-inline-expansion var) nil))
1121   (let ((fun (ir1-convert-inline-lambda expansion
1122                                         :source-name name
1123                                         ;; prevent instrumentation of
1124                                         ;; known function expansions
1125                                         :system-lambda (and info t))))
1126     (setf (functional-inlinep fun) inlinep)
1127     (assert-new-definition var fun)
1128     (setf (defined-fun-inline-expansion var) expansion)
1129     ;; substitute for any old references
1130     (unless (or (not *block-compile*)
1131                 (and info
1132                      (or (fun-info-transforms info)
1133                          (fun-info-templates info)
1134                          (fun-info-ir2-convert info))))
1135       (substitute-leaf fun var))
1136     fun))
1137
1138 ;;; the even-at-compile-time part of DEFUN
1139 ;;;
1140 ;;; The INLINE-EXPANSION is a LAMBDA-WITH-LEXENV, or NIL if there is
1141 ;;; no inline expansion.
1142 (defun %compiler-defun (name lambda-with-lexenv compile-toplevel)
1143   (let ((defined-fun nil)) ; will be set below if we're in the compiler
1144     (when compile-toplevel
1145       ;; better be in the compiler
1146       (aver (boundp '*lexenv*))
1147       (remhash name *free-funs*)
1148       (setf defined-fun (get-defined-fun name))
1149       (aver (fasl-output-p *compile-object*))
1150       (if (member name *fun-names-in-this-file* :test #'equal)
1151           (warn 'duplicate-definition :name name)
1152           (push name *fun-names-in-this-file*)))
1153
1154     (become-defined-fun-name name)
1155
1156     (cond (lambda-with-lexenv
1157            (setf (info :function :inline-expansion-designator name)
1158                  lambda-with-lexenv)
1159            (when defined-fun
1160              (setf (defined-fun-inline-expansion defined-fun)
1161                    lambda-with-lexenv)))
1162           (t
1163            (clear-info :function :inline-expansion-designator name)))
1164
1165     ;; old CMU CL comment:
1166     ;;   If there is a type from a previous definition, blast it,
1167     ;;   since it is obsolete.
1168     (when (and defined-fun
1169                (eq (leaf-where-from defined-fun) :defined))
1170       (setf (leaf-type defined-fun)
1171             ;; FIXME: If this is a block compilation thing, shouldn't
1172             ;; we be setting the type to the full derived type for the
1173             ;; definition, instead of this most general function type?
1174             (specifier-type 'function))))
1175
1176   (values))
1177
1178 \f
1179 ;;; Entry point utilities
1180
1181 ;;; Return a function for the Nth entry point.
1182 (defun optional-dispatch-entry-point-fun (dispatcher n)
1183   (declare (type optional-dispatch dispatcher)
1184            (type unsigned-byte n))
1185   (let* ((env (getf (optional-dispatch-plist dispatcher) :ir1-environment))
1186          (*lexenv* (first env))
1187          (*current-path* (second env)))
1188     (force (nth n (optional-dispatch-entry-points dispatcher)))))