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