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