1 ;;;; This file contains code which does the translation of lambda
2 ;;;; forms from Lisp code to the first intermediate representation
5 ;;;; This software is part of the SBCL system. See the README file for
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.
18 ;;;; Note: Take a look at the compiler-overview.tex section on "Hairy
19 ;;;; function representation" before you seriously mess with this
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 (function (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."
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."
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)
46 (make-lambda-var :%source-name name)))))
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 (function (symbol list t) keyword) make-keyword-for-arg))
51 (defun make-keyword-for-arg (symbol vars keywordify)
52 (let ((key (if (and keywordify (not (keywordp symbol)))
56 (let ((info (lambda-var-arg-info var)))
58 (eq (arg-info-kind info) :keyword)
59 (eq (arg-info-key info) key))
61 "The keyword ~S appears more than once in the lambda list."
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 (function (list) (values list boolean boolean list list))
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
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
97 (setf (arg-info-supplied-p info) supplied-var)
98 (names-so-far supplied-p)
99 (when (> (length (the list spec)) 3)
101 "The list ~S is too long to be an arg specifier."
104 (dolist (name required)
105 (let ((var (varify-lambda-arg name (names-so-far))))
107 (names-so-far name)))
109 (dolist (spec optional)
111 (let ((var (varify-lambda-arg spec (names-so-far))))
112 (setf (lambda-var-arg-info var)
113 (make-arg-info :kind :optional))
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)
122 (parse-default spec info))))
125 (let ((var (varify-lambda-arg rest (names-so-far))))
126 (setf (lambda-var-arg-info var) (make-arg-info :kind :rest))
128 (names-so-far rest)))
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))
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))
140 (names-so-far more-count)))
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
152 (names-so-far spec)))
154 (let* ((name (first spec))
155 (var (varify-lambda-arg name (names-so-far)))
158 :key (make-keyword-for-arg name (vars) t))))
159 (setf (lambda-var-arg-info var) info)
162 (parse-default spec info)))
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)))
171 :key (make-keyword-for-arg (first head)
174 (setf (lambda-var-arg-info var) info)
177 (parse-default spec info))))))
181 (let ((var (varify-lambda-arg spec nil)))
184 (names-so-far spec)))
186 (unless (proper-list-of-length-p spec 1 2)
187 (compiler-error "malformed &AUX binding specifier: ~S"
189 (let* ((name (first spec))
190 (var (varify-lambda-arg name nil)))
192 (aux-vals (second spec))
193 (names-so-far name)))))
195 (values (vars) keyp allowp (aux-vars) (aux-vals))))))
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.
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 cont body aux-vars aux-vals)
206 (declare (type continuation start cont) (list body aux-vars aux-vals))
208 (ir1-convert-progn-body start cont body)
209 (let ((fun-cont (make-continuation))
210 (fun (ir1-convert-lambda-body body
211 (list (first aux-vars))
212 :aux-vars (rest aux-vars)
213 :aux-vals (rest aux-vals)
214 :debug-name (debug-namify
217 (reference-leaf start fun-cont fun)
218 (ir1-convert-combination-args fun-cont cont
219 (list (first aux-vals)))))
222 ;;; This is similar to IR1-CONVERT-PROGN-BODY except that code to bind
223 ;;; the SPECVAR for each SVAR to the value of the variable is wrapped
224 ;;; around the body. If there are no special bindings, we just convert
225 ;;; the body, otherwise we do one special binding and recurse on the
228 ;;; We make a cleanup and introduce it into the lexical environment.
229 ;;; If there are multiple special bindings, the cleanup for the blocks
230 ;;; will end up being the innermost one. We force CONT to start a
231 ;;; block outside of this cleanup, causing cleanup code to be emitted
232 ;;; when the scope is exited.
233 (defun ir1-convert-special-bindings (start cont body aux-vars aux-vals svars)
234 (declare (type continuation start cont)
235 (list body aux-vars aux-vals svars))
238 (ir1-convert-aux-bindings start cont body aux-vars aux-vals))
240 (continuation-starts-block cont)
241 (let ((cleanup (make-cleanup :kind :special-bind))
243 (next-cont (make-continuation))
244 (nnext-cont (make-continuation)))
245 (ir1-convert start next-cont
246 `(%special-bind ',(lambda-var-specvar var) ,var))
247 (setf (cleanup-mess-up cleanup) (continuation-use next-cont))
248 (let ((*lexenv* (make-lexenv :cleanup cleanup)))
249 (ir1-convert next-cont nnext-cont '(%cleanup-point))
250 (ir1-convert-special-bindings nnext-cont cont body aux-vars aux-vals
254 ;;; Create a lambda node out of some code, returning the result. The
255 ;;; bindings are specified by the list of VAR structures VARS. We deal
256 ;;; with adding the names to the LEXENV-VARS for the conversion. The
257 ;;; result is added to the NEW-FUNCTIONALS in the *CURRENT-COMPONENT*
258 ;;; and linked to the component head and tail.
260 ;;; We detect special bindings here, replacing the original VAR in the
261 ;;; lambda list with a temporary variable. We then pass a list of the
262 ;;; special vars to IR1-CONVERT-SPECIAL-BINDINGS, which actually emits
263 ;;; the special binding code.
265 ;;; We ignore any ARG-INFO in the VARS, trusting that someone else is
266 ;;; dealing with &nonsense.
268 ;;; AUX-VARS is a list of VAR structures for variables that are to be
269 ;;; sequentially bound. Each AUX-VAL is a form that is to be evaluated
270 ;;; to get the initial value for the corresponding AUX-VAR.
271 (defun ir1-convert-lambda-body (body
277 (source-name '.anonymous.)
279 (note-lexical-bindings t))
280 (declare (list body vars aux-vars aux-vals)
281 (type (or continuation null) result))
283 ;; We're about to try to put new blocks into *CURRENT-COMPONENT*.
284 (aver-live-component *current-component*)
286 (let* ((bind (make-bind))
287 (lambda (make-lambda :vars vars
289 :%source-name source-name
290 :%debug-name debug-name))
291 (result (or result (make-continuation))))
293 (continuation-starts-block result)
295 ;; just to check: This function should fail internal assertions if
296 ;; we didn't set up a valid debug name above.
298 ;; (In SBCL we try to make everything have a debug name, since we
299 ;; lack the omniscient perspective the original implementors used
300 ;; to decide which things didn't need one.)
301 (functional-debug-name lambda)
303 (setf (lambda-home lambda) lambda)
308 ;; As far as I can see, LAMBDA-VAR-HOME should never have
309 ;; been set before. Let's make sure. -- WHN 2001-09-29
310 (aver (null (lambda-var-home var)))
311 (setf (lambda-var-home var) lambda)
312 (let ((specvar (lambda-var-specvar var)))
315 (new-venv (cons (leaf-source-name specvar) specvar)))
317 (when note-lexical-bindings
318 (note-lexical-binding (leaf-source-name var)))
319 (new-venv (cons (leaf-source-name var) var))))))
321 (let ((*lexenv* (make-lexenv :vars (new-venv)
324 (setf (bind-lambda bind) lambda)
325 (setf (node-lexenv bind) *lexenv*)
327 (let ((cont1 (make-continuation))
328 (cont2 (make-continuation)))
329 (continuation-starts-block cont1)
330 (link-node-to-previous-continuation bind cont1)
331 (use-continuation bind cont2)
332 (ir1-convert-special-bindings cont2 result body
333 aux-vars aux-vals (svars)))
335 (let ((block (continuation-block result)))
337 (let ((return (make-return :result result :lambda lambda))
338 (tail-set (make-tail-set :funs (list lambda)))
339 (dummy (make-continuation)))
340 (setf (lambda-tail-set lambda) tail-set)
341 (setf (lambda-return lambda) return)
342 (setf (continuation-dest result) return)
343 (flush-continuation-externally-checkable-type result)
344 (setf (block-last block) return)
345 (link-node-to-previous-continuation return result)
346 (use-continuation return dummy))
347 (link-blocks block (component-tail *current-component*))))))
349 (link-blocks (component-head *current-component*) (node-block bind))
350 (push lambda (component-new-functionals *current-component*))
354 ;;; Create the actual entry-point function for an optional entry
355 ;;; point. The lambda binds copies of each of the VARS, then calls FUN
356 ;;; with the argument VALS and the DEFAULTS. Presumably the VALS refer
357 ;;; to the VARS by name. The VALS are passed in in reverse order.
359 ;;; If any of the copies of the vars are referenced more than once,
360 ;;; then we mark the corresponding var as EVER-USED to inhibit
361 ;;; "defined but not read" warnings for arguments that are only used
362 ;;; by default forms.
363 (defun convert-optional-entry (fun vars vals defaults)
364 (declare (type clambda fun) (list vars vals defaults))
365 (let* ((fvars (reverse vars))
366 (arg-vars (mapcar (lambda (var)
368 :%source-name (leaf-source-name var)
369 :type (leaf-type var)
370 :where-from (leaf-where-from var)
371 :specvar (lambda-var-specvar var)))
373 (fun (collect ((default-bindings)
375 (dolist (default defaults)
376 (if (constantp default)
377 (default-vals default)
378 (let ((var (gensym)))
379 (default-bindings `(,var ,default))
380 (default-vals var))))
381 (ir1-convert-lambda-body `((let (,@(default-bindings))
386 :debug-name "&OPTIONAL processor"
387 :note-lexical-bindings nil))))
388 (mapc (lambda (var arg-var)
389 (when (cdr (leaf-refs arg-var))
390 (setf (leaf-ever-used var) t)))
394 ;;; This function deals with supplied-p vars in optional arguments. If
395 ;;; the there is no supplied-p arg, then we just call
396 ;;; IR1-CONVERT-HAIRY-ARGS on the remaining arguments, and generate a
397 ;;; optional entry that calls the result. If there is a supplied-p
398 ;;; var, then we add it into the default vars and throw a T into the
399 ;;; entry values. The resulting entry point function is returned.
400 (defun generate-optional-default-entry (res default-vars default-vals
401 entry-vars entry-vals
402 vars supplied-p-p body
403 aux-vars aux-vals cont
404 source-name debug-name)
405 (declare (type optional-dispatch res)
406 (list default-vars default-vals entry-vars entry-vals vars body
408 (type (or continuation null) cont))
409 (let* ((arg (first vars))
410 (arg-name (leaf-source-name arg))
411 (info (lambda-var-arg-info arg))
412 (supplied-p (arg-info-supplied-p info))
414 (ir1-convert-hairy-args
416 (list* supplied-p arg default-vars)
417 (list* (leaf-source-name supplied-p) arg-name default-vals)
418 (cons arg entry-vars)
419 (list* t arg-name entry-vals)
420 (rest vars) t body aux-vars aux-vals cont
421 source-name debug-name)
422 (ir1-convert-hairy-args
424 (cons arg default-vars)
425 (cons arg-name default-vals)
426 (cons arg entry-vars)
427 (cons arg-name entry-vals)
428 (rest vars) supplied-p-p body aux-vars aux-vals cont
429 source-name debug-name))))
431 (convert-optional-entry ep default-vars default-vals
433 (list (arg-info-default info) nil)
434 (list (arg-info-default info))))))
436 ;;; Create the MORE-ENTRY function for the OPTIONAL-DISPATCH RES.
437 ;;; ENTRY-VARS and ENTRY-VALS describe the fixed arguments. REST is
438 ;;; the var for any &REST arg. KEYS is a list of the &KEY arg vars.
440 ;;; The most interesting thing that we do is parse keywords. We create
441 ;;; a bunch of temporary variables to hold the result of the parse,
442 ;;; and then loop over the supplied arguments, setting the appropriate
443 ;;; temps for the supplied keyword. Note that it is significant that
444 ;;; we iterate over the keywords in reverse order --- this implements
445 ;;; the CL requirement that (when a keyword appears more than once)
446 ;;; the first value is used.
448 ;;; If there is no supplied-p var, then we initialize the temp to the
449 ;;; default and just pass the temp into the main entry. Since
450 ;;; non-constant &KEY args are forcibly given a supplied-p var, we
451 ;;; know that the default is constant, and thus safe to evaluate out
454 ;;; If there is a supplied-p var, then we create temps for both the
455 ;;; value and the supplied-p, and pass them into the main entry,
456 ;;; letting it worry about defaulting.
458 ;;; We deal with :ALLOW-OTHER-KEYS by delaying unknown keyword errors
459 ;;; until we have scanned all the keywords.
460 (defun convert-more-entry (res entry-vars entry-vals rest morep keys)
461 (declare (type optional-dispatch res) (list entry-vars entry-vals keys))
463 (arg-vals (reverse entry-vals))
467 (dolist (var (reverse entry-vars))
468 (arg-vars (make-lambda-var :%source-name (leaf-source-name var)
469 :type (leaf-type var)
470 :where-from (leaf-where-from var))))
472 (let* ((n-context (gensym "N-CONTEXT-"))
473 (context-temp (make-lambda-var :%source-name n-context))
474 (n-count (gensym "N-COUNT-"))
475 (count-temp (make-lambda-var :%source-name n-count
476 :type (specifier-type 'index))))
478 (arg-vars context-temp count-temp)
481 (arg-vals `(%listify-rest-args ,n-context ,n-count)))
486 (when (optional-dispatch-keyp res)
487 (let ((n-index (gensym "N-INDEX-"))
488 (n-key (gensym "N-KEY-"))
489 (n-value-temp (gensym "N-VALUE-TEMP-"))
490 (n-allowp (gensym "N-ALLOWP-"))
491 (n-losep (gensym "N-LOSEP-"))
492 (allowp (or (optional-dispatch-allowp res)
493 (policy *lexenv* (zerop safety))))
496 (temps `(,n-index (1- ,n-count)) n-key n-value-temp)
497 (body `(declare (fixnum ,n-index) (ignorable ,n-key ,n-value-temp)))
501 (let* ((info (lambda-var-arg-info key))
502 (default (arg-info-default info))
503 (keyword (arg-info-key info))
504 (supplied-p (arg-info-supplied-p info))
505 (n-value (gensym "N-VALUE-"))
506 (clause (cond (supplied-p
507 (let ((n-supplied (gensym "N-SUPPLIED-")))
509 (arg-vals n-value n-supplied)
510 `((eq ,n-key ',keyword)
512 (setq ,n-value ,n-value-temp))))
515 `((eq ,n-key ',keyword)
516 (setq ,n-value ,n-value-temp))))))
517 (when (and (not allowp) (eq keyword :allow-other-keys))
518 (setq found-allow-p t)
520 (append clause `((setq ,n-allowp ,n-value-temp)))))
522 (temps `(,n-value ,default))
526 (temps n-allowp n-losep)
527 (unless found-allow-p
528 (tests `((eq ,n-key :allow-other-keys)
529 (setq ,n-allowp ,n-value-temp))))
531 (setq ,n-losep ,n-key))))
534 `(when (oddp ,n-count)
535 (%odd-key-args-error)))
539 (declare (optimize (safety 0)))
541 (when (minusp ,n-index) (return))
542 (setf ,n-value-temp (%more-arg ,n-context ,n-index))
544 (setq ,n-key (%more-arg ,n-context ,n-index))
549 (body `(when (and ,n-losep (not ,n-allowp))
550 (%unknown-key-arg-error ,n-losep)))))))
552 (let ((ep (ir1-convert-lambda-body
555 (%funcall ,(optional-dispatch-main-entry res)
558 :debug-name (debug-namify "~S processing" '&more)
559 :note-lexical-bindings nil)))
560 (setf (optional-dispatch-more-entry res) ep))))
564 ;;; This is called by IR1-CONVERT-HAIRY-ARGS when we run into a &REST
565 ;;; or &KEY arg. The arguments are similar to that function, but we
566 ;;; split off any &REST arg and pass it in separately. REST is the
567 ;;; &REST arg var, or NIL if there is no &REST arg. KEYS is a list of
568 ;;; the &KEY argument vars.
570 ;;; When there are &KEY arguments, we introduce temporary gensym
571 ;;; variables to hold the values while keyword defaulting is in
572 ;;; progress to get the required sequential binding semantics.
574 ;;; This gets interesting mainly when there are &KEY arguments with
575 ;;; supplied-p vars or non-constant defaults. In either case, pass in
576 ;;; a supplied-p var. If the default is non-constant, we introduce an
577 ;;; IF in the main entry that tests the supplied-p var and decides
578 ;;; whether to evaluate the default or not. In this case, the real
579 ;;; incoming value is NIL, so we must union NULL with the declared
580 ;;; type when computing the type for the main entry's argument.
581 (defun ir1-convert-more (res default-vars default-vals entry-vars entry-vals
582 rest more-context more-count keys supplied-p-p
583 body aux-vars aux-vals cont
584 source-name debug-name)
585 (declare (type optional-dispatch res)
586 (list default-vars default-vals entry-vars entry-vals keys body
588 (type (or continuation null) cont))
589 (collect ((main-vars (reverse default-vars))
590 (main-vals default-vals cons)
597 (main-vars more-context)
599 (main-vars more-count)
603 (let* ((info (lambda-var-arg-info key))
604 (default (arg-info-default info))
605 (hairy-default (not (sb!xc:constantp default)))
606 (supplied-p (arg-info-supplied-p info))
607 (n-val (make-symbol (format nil
609 (leaf-source-name key))))
610 (key-type (leaf-type key))
611 (val-temp (make-lambda-var
613 :type (if hairy-default
614 (type-union key-type (specifier-type 'null))
618 (cond ((or hairy-default supplied-p)
619 (let* ((n-supplied (gensym "N-SUPPLIED-"))
620 (supplied-temp (make-lambda-var
621 :%source-name n-supplied)))
623 (setf (arg-info-supplied-p info) supplied-temp))
625 (setf (arg-info-default info) nil))
626 (main-vars supplied-temp)
629 (bind-vals `(if ,n-supplied ,n-val ,default)))
631 (main-vals default nil)
634 (bind-vars supplied-p)
635 (bind-vals n-supplied))))
637 (main-vals (arg-info-default info))
638 (bind-vals n-val)))))
640 (let* ((main-entry (ir1-convert-lambda-body
642 :aux-vars (append (bind-vars) aux-vars)
643 :aux-vals (append (bind-vals) aux-vals)
645 :debug-name (debug-namify "varargs entry for ~A"
646 (as-debug-name source-name
648 (last-entry (convert-optional-entry main-entry default-vars
650 (setf (optional-dispatch-main-entry res) main-entry)
651 (convert-more-entry res entry-vars entry-vals rest more-context keys)
653 (push (if supplied-p-p
654 (convert-optional-entry last-entry entry-vars entry-vals ())
656 (optional-dispatch-entry-points res))
659 ;;; This function generates the entry point functions for the
660 ;;; OPTIONAL-DISPATCH RES. We accomplish this by recursion on the list
661 ;;; of arguments, analyzing the arglist on the way down and generating
662 ;;; entry points on the way up.
664 ;;; DEFAULT-VARS is a reversed list of all the argument vars processed
665 ;;; so far, including supplied-p vars. DEFAULT-VALS is a list of the
666 ;;; names of the DEFAULT-VARS.
668 ;;; ENTRY-VARS is a reversed list of processed argument vars,
669 ;;; excluding supplied-p vars. ENTRY-VALS is a list things that can be
670 ;;; evaluated to get the values for all the vars from the ENTRY-VARS.
671 ;;; It has the var name for each required or optional arg, and has T
672 ;;; for each supplied-p arg.
674 ;;; VARS is a list of the LAMBDA-VAR structures for arguments that
675 ;;; haven't been processed yet. SUPPLIED-P-P is true if a supplied-p
676 ;;; argument has already been processed; only in this case are the
677 ;;; DEFAULT-XXX and ENTRY-XXX different.
679 ;;; The result at each point is a lambda which should be called by the
680 ;;; above level to default the remaining arguments and evaluate the
681 ;;; body. We cause the body to be evaluated by converting it and
682 ;;; returning it as the result when the recursion bottoms out.
684 ;;; Each level in the recursion also adds its entry point function to
685 ;;; the result OPTIONAL-DISPATCH. For most arguments, the defaulting
686 ;;; function and the entry point function will be the same, but when
687 ;;; SUPPLIED-P args are present they may be different.
689 ;;; When we run into a &REST or &KEY arg, we punt out to
690 ;;; IR1-CONVERT-MORE, which finishes for us in this case.
691 (defun ir1-convert-hairy-args (res default-vars default-vals
692 entry-vars entry-vals
693 vars supplied-p-p body aux-vars
695 source-name debug-name)
696 (declare (type optional-dispatch res)
697 (list default-vars default-vals entry-vars entry-vals vars body
699 (type (or continuation null) cont))
701 (if (optional-dispatch-keyp res)
702 ;; Handle &KEY with no keys...
703 (ir1-convert-more res default-vars default-vals
704 entry-vars entry-vals
705 nil nil nil vars supplied-p-p body aux-vars
706 aux-vals cont source-name debug-name)
707 (let ((fun (ir1-convert-lambda-body
708 body (reverse default-vars)
712 :debug-name (debug-namify
713 "hairy arg processor for ~A"
714 (as-debug-name source-name
716 (setf (optional-dispatch-main-entry res) fun)
717 (push (if supplied-p-p
718 (convert-optional-entry fun entry-vars entry-vals ())
720 (optional-dispatch-entry-points res))
722 ((not (lambda-var-arg-info (first vars)))
723 (let* ((arg (first vars))
724 (nvars (cons arg default-vars))
725 (nvals (cons (leaf-source-name arg) default-vals)))
726 (ir1-convert-hairy-args res nvars nvals nvars nvals
727 (rest vars) nil body aux-vars aux-vals
729 source-name debug-name)))
731 (let* ((arg (first vars))
732 (info (lambda-var-arg-info arg))
733 (kind (arg-info-kind info)))
736 (let ((ep (generate-optional-default-entry
737 res default-vars default-vals
738 entry-vars entry-vals vars supplied-p-p body
739 aux-vars aux-vals cont
740 source-name debug-name)))
741 (push (if supplied-p-p
742 (convert-optional-entry ep entry-vars entry-vals ())
744 (optional-dispatch-entry-points res))
747 (ir1-convert-more res default-vars default-vals
748 entry-vars entry-vals
749 arg nil nil (rest vars) supplied-p-p body
750 aux-vars aux-vals cont
751 source-name debug-name))
753 (ir1-convert-more res default-vars default-vals
754 entry-vars entry-vals
755 nil arg (second vars) (cddr vars) supplied-p-p
756 body aux-vars aux-vals cont
757 source-name debug-name))
759 (ir1-convert-more res default-vars default-vals
760 entry-vars entry-vals
761 nil nil nil vars supplied-p-p body aux-vars
762 aux-vals cont source-name debug-name)))))))
764 ;;; This function deals with the case where we have to make an
765 ;;; OPTIONAL-DISPATCH to represent a LAMBDA. We cons up the result and
766 ;;; call IR1-CONVERT-HAIRY-ARGS to do the work. When it is done, we
767 ;;; figure out the MIN-ARGS and MAX-ARGS.
768 (defun ir1-convert-hairy-lambda (body vars keyp allowp aux-vars aux-vals cont
770 (source-name '.anonymous.)
771 (debug-name (debug-namify
772 "OPTIONAL-DISPATCH ~S"
774 (declare (list body vars aux-vars aux-vals) (type continuation cont))
775 (let ((res (make-optional-dispatch :arglist vars
778 :%source-name source-name
779 :%debug-name debug-name))
780 (min (or (position-if #'lambda-var-arg-info vars) (length vars))))
781 (aver-live-component *current-component*)
782 (push res (component-new-functionals *current-component*))
783 (ir1-convert-hairy-args res () () () () vars nil body aux-vars aux-vals
784 cont source-name debug-name)
785 (setf (optional-dispatch-min-args res) min)
786 (setf (optional-dispatch-max-args res)
787 (+ (1- (length (optional-dispatch-entry-points res))) min))
791 (setf (functional-kind ep) :optional)
792 (setf (leaf-ever-used ep) t)
793 (setf (lambda-optional-dispatch ep) res))))
794 (dolist (ep (optional-dispatch-entry-points res)) (frob ep))
795 (frob (optional-dispatch-more-entry res))
796 (frob (optional-dispatch-main-entry res)))
800 ;;; Convert a LAMBDA form into a LAMBDA leaf or an OPTIONAL-DISPATCH leaf.
801 (defun ir1-convert-lambda (form &key (source-name '.anonymous.)
803 allow-debug-catch-tag)
806 (compiler-error "A ~S was found when expecting a lambda expression:~% ~S"
809 (unless (eq (car form) 'lambda)
810 (compiler-error "~S was expected but ~S was found:~% ~S"
814 (unless (and (consp (cdr form)) (listp (cadr form)))
816 "The lambda expression has a missing or non-list lambda list:~% ~S"
819 (let ((*allow-debug-catch-tag* (and *allow-debug-catch-tag* allow-debug-catch-tag)))
820 (multiple-value-bind (vars keyp allow-other-keys aux-vars aux-vals)
821 (make-lambda-vars (cadr form))
822 (multiple-value-bind (forms decls) (parse-body (cddr form))
823 (let* ((result-cont (make-continuation))
824 (*lexenv* (process-decls decls
825 (append aux-vars vars)
827 (forms (if (and *allow-debug-catch-tag*
828 (policy *lexenv* (> debug (max speed space))))
829 `((catch (make-symbol "SB-DEBUG-CATCH-TAG")
832 (res (if (or (find-if #'lambda-var-arg-info vars) keyp)
833 (ir1-convert-hairy-lambda forms vars keyp
835 aux-vars aux-vals result-cont
836 :source-name source-name
837 :debug-name debug-name)
838 (ir1-convert-lambda-body forms vars
842 :source-name source-name
843 :debug-name debug-name))))
844 (setf (functional-inline-expansion res) form)
845 (setf (functional-arg-documentation res) (cadr form))
848 ;;; helper for LAMBDA-like things, to massage them into a form
849 ;;; suitable for IR1-CONVERT-LAMBDA.
851 ;;; KLUDGE: We cons up a &REST list here, maybe for no particularly
852 ;;; good reason. It's probably lost in the noise of all the other
853 ;;; consing, but it's still inelegant. And we force our called
854 ;;; functions to do full runtime keyword parsing, ugh. -- CSR,
856 (defun ir1-convert-lambdalike (thing &rest args
857 &key (source-name '.anonymous.)
858 debug-name allow-debug-catch-tag)
859 (declare (ignorable source-name debug-name allow-debug-catch-tag))
861 ((lambda) (apply #'ir1-convert-lambda thing args))
863 (let ((res (apply #'ir1-convert-lambda
864 `(lambda ,@(cdr thing)) args)))
865 (setf (getf (functional-plist res) :fin-function) t)
868 (let ((name (cadr thing)))
869 (if (legal-fun-name-p name)
870 (let ((res (apply #'ir1-convert-lambda `(lambda ,@(cddr thing))
874 (assert-global-function-definition-type name res)
876 (apply #'ir1-convert-lambda `(lambda ,@(cddr thing))
877 :debug-name name args))))
878 ((lambda-with-lexenv) (apply #'ir1-convert-inline-lambda thing args))))
880 ;;;; defining global functions
882 ;;; Convert FUN as a lambda in the null environment, but use the
883 ;;; current compilation policy. Note that FUN may be a
884 ;;; LAMBDA-WITH-LEXENV, so we may have to augment the environment to
885 ;;; reflect the state at the definition site.
886 (defun ir1-convert-inline-lambda (fun &key
887 (source-name '.anonymous.)
889 allow-debug-catch-tag)
890 (destructuring-bind (decls macros symbol-macros &rest body)
891 (if (eq (car fun) 'lambda-with-lexenv)
893 `(() () () . ,(cdr fun)))
894 (let ((*lexenv* (make-lexenv
895 :default (process-decls decls nil nil
898 :vars (copy-list symbol-macros)
899 :funs (mapcar (lambda (x)
901 (macro . ,(coerce (cdr x) 'function))))
903 :policy (lexenv-policy *lexenv*))))
904 (ir1-convert-lambda `(lambda ,@body)
905 :source-name source-name
906 :debug-name debug-name
907 :allow-debug-catch-tag nil))))
909 ;;; Get a DEFINED-FUN object for a function we are about to define. If
910 ;;; the function has been forward referenced, then substitute for the
911 ;;; previous references.
912 (defun get-defined-fun (name)
913 (proclaim-as-fun-name name)
914 (let ((found (find-free-fun name "shouldn't happen! (defined-fun)")))
915 (note-name-defined name :function)
916 (cond ((not (defined-fun-p found))
917 (aver (not (info :function :inlinep name)))
918 (let* ((where-from (leaf-where-from found))
919 (res (make-defined-fun
921 :where-from (if (eq where-from :declared)
923 :type (leaf-type found))))
924 (substitute-leaf res found)
925 (setf (gethash name *free-funs*) res)))
926 ;; If *FREE-FUNS* has a previously converted definition
927 ;; for this name, then blow it away and try again.
928 ((defined-fun-functional found)
929 (remhash name *free-funs*)
930 (get-defined-fun name))
933 ;;; Check a new global function definition for consistency with
934 ;;; previous declaration or definition, and assert argument/result
935 ;;; types if appropriate. This assertion is suppressed by the
936 ;;; EXPLICIT-CHECK attribute, which is specified on functions that
937 ;;; check their argument types as a consequence of type dispatching.
938 ;;; This avoids redundant checks such as NUMBERP on the args to +, etc.
939 (defun assert-new-definition (var fun)
940 (let ((type (leaf-type var))
941 (for-real (eq (leaf-where-from var) :declared))
942 (info (info :function :info (leaf-source-name var))))
943 (assert-definition-type
945 ;; KLUDGE: Common Lisp is such a dynamic language that in general
946 ;; all we can do here in general is issue a STYLE-WARNING. It
947 ;; would be nice to issue a full WARNING in the special case of
948 ;; of type mismatches within a compilation unit (as in section
949 ;; 3.2.2.3 of the spec) but at least as of sbcl-0.6.11, we don't
950 ;; keep track of whether the mismatched data came from the same
951 ;; compilation unit, so we can't do that. -- WHN 2001-02-11
952 :lossage-fun #'compiler-style-warn
953 :unwinnage-fun (cond (info #'compiler-style-warn)
954 (for-real #'compiler-note)
959 (ir1-attributep (fun-info-attributes info)
962 "previous declaration"
963 "previous definition"))))
965 ;;; Convert a lambda doing all the basic stuff we would do if we were
966 ;;; converting a DEFUN. In the old CMU CL system, this was used both
967 ;;; by the %DEFUN translator and for global inline expansion, but
968 ;;; since sbcl-0.pre7.something %DEFUN does things differently.
969 ;;; FIXME: And now it's probably worth rethinking whether this
970 ;;; function is a good idea.
972 ;;; Unless a :INLINE function, we temporarily clobber the inline
973 ;;; expansion. This prevents recursive inline expansion of
974 ;;; opportunistic pseudo-inlines.
975 (defun ir1-convert-lambda-for-defun (lambda var expansion converter)
976 (declare (cons lambda) (function converter) (type defined-fun var))
977 (let ((var-expansion (defined-fun-inline-expansion var)))
978 (unless (eq (defined-fun-inlinep var) :inline)
979 (setf (defined-fun-inline-expansion var) nil))
980 (let* ((name (leaf-source-name var))
981 (fun (funcall converter lambda
983 (fun-info (info :function :info name)))
984 (setf (functional-inlinep fun) (defined-fun-inlinep var))
985 (assert-new-definition var fun)
986 (setf (defined-fun-inline-expansion var) var-expansion)
987 ;; If definitely not an interpreter stub, then substitute for
988 ;; any old references.
989 (unless (or (eq (defined-fun-inlinep var) :notinline)
990 (not *block-compile*)
992 (or (fun-info-transforms fun-info)
993 (fun-info-templates fun-info)
994 (fun-info-ir2-convert fun-info))))
995 (substitute-leaf fun var)
996 ;; If in a simple environment, then we can allow backward
997 ;; references to this function from following top level forms.
998 (when expansion (setf (defined-fun-functional var) fun)))
1001 ;;; the even-at-compile-time part of DEFUN
1003 ;;; The INLINE-EXPANSION is a LAMBDA-WITH-LEXENV, or NIL if there is
1004 ;;; no inline expansion.
1005 (defun %compiler-defun (name lambda-with-lexenv)
1007 (let ((defined-fun nil)) ; will be set below if we're in the compiler
1009 (when (boundp '*lexenv*) ; when in the compiler
1010 (when sb!xc:*compile-print*
1011 (compiler-mumble "~&; recognizing DEFUN ~S~%" name))
1012 (remhash name *free-funs*)
1013 (setf defined-fun (get-defined-fun name)))
1015 (become-defined-fun-name name)
1017 (cond (lambda-with-lexenv
1018 (setf (info :function :inline-expansion-designator name)
1021 (setf (defined-fun-inline-expansion defined-fun)
1022 lambda-with-lexenv)))
1024 (clear-info :function :inline-expansion-designator name)))
1026 ;; old CMU CL comment:
1027 ;; If there is a type from a previous definition, blast it,
1028 ;; since it is obsolete.
1029 (when (and defined-fun
1030 (eq (leaf-where-from defined-fun) :defined))
1031 (setf (leaf-type defined-fun)
1032 ;; FIXME: If this is a block compilation thing, shouldn't
1033 ;; we be setting the type to the full derived type for the
1034 ;; definition, instead of this most general function type?
1035 (specifier-type 'function))))