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