0.9.1.38:
[sbcl.git] / src / compiler / ir1tran-lambda.lisp
index 1f4b91d..862f8ae 100644 (file)
 ;;; FIXME: This could and probably should be converted to use
 ;;; SOURCE-NAME and DEBUG-NAME. But I (WHN) don't use &AUX bindings,
 ;;; so I'm not motivated. Patches will be accepted...
-(defun ir1-convert-aux-bindings (start next result body aux-vars aux-vals)
+(defun ir1-convert-aux-bindings (start next result body aux-vars aux-vals
+                                post-binding-lexenv)
   (declare (type ctran start next) (type (or lvar null) result)
            (list body aux-vars aux-vals))
   (if (null aux-vars)
-      (ir1-convert-progn-body start next result body)
+      (let ((*lexenv* (make-lexenv :vars (copy-list post-binding-lexenv))))
+       (ir1-convert-progn-body start next result body))
       (let ((ctran (make-ctran))
             (fun-lvar (make-lvar))
            (fun (ir1-convert-lambda-body body
                                          (list (first aux-vars))
                                          :aux-vars (rest aux-vars)
                                          :aux-vals (rest aux-vals)
-                                         :debug-name (debug-namify
-                                                      "&AUX bindings ~S"
-                                                      aux-vars))))
+                                         :post-binding-lexenv post-binding-lexenv
+                                         :debug-name (debug-name 
+                                                       '&aux-bindings 
+                                                       aux-vars))))
        (reference-leaf start ctran fun-lvar fun)
        (ir1-convert-combination-args fun-lvar ctran next result
                                      (list (first aux-vals)))))
 ;;; to start a block outside of this cleanup, causing cleanup code to
 ;;; be emitted when the scope is exited.
 (defun ir1-convert-special-bindings
-    (start next result body aux-vars aux-vals svars)
+    (start next result body aux-vars aux-vals svars post-binding-lexenv)
   (declare (type ctran start next) (type (or lvar null) result)
           (list body aux-vars aux-vals svars))
   (cond
    ((null svars)
-    (ir1-convert-aux-bindings start next result body aux-vars aux-vals))
+    (ir1-convert-aux-bindings start next result body aux-vars aux-vals
+                             post-binding-lexenv))
    (t
     (ctran-starts-block next)
     (let ((cleanup (make-cleanup :kind :special-bind))
        (ir1-convert bind-ctran cleanup-ctran nil '(%cleanup-point))
        (ir1-convert-special-bindings cleanup-ctran next result
                                       body aux-vars aux-vals
-                                     (rest svars))))))
+                                     (rest svars)
+                                     post-binding-lexenv)))))
   (values))
 
 ;;; Create a lambda node out of some code, returning the result. The
 ;;; the special binding code.
 ;;;
 ;;; We ignore any ARG-INFO in the VARS, trusting that someone else is
-;;; dealing with &nonsense.
+;;; dealing with &NONSENSE, except for &REST vars with DYNAMIC-EXTENT.
 ;;;
 ;;; AUX-VARS is a list of VAR structures for variables that are to be
 ;;; sequentially bound. Each AUX-VAL is a form that is to be evaluated
                                aux-vals
                                (source-name '.anonymous.)
                                debug-name
-                                (note-lexical-bindings t))
+                                (note-lexical-bindings t)
+                               post-binding-lexenv)
   (declare (list body vars aux-vars aux-vals))
 
   ;; We're about to try to put new blocks into *CURRENT-COMPONENT*.
             (ctran-starts-block prebind-ctran)
             (link-node-to-previous-ctran bind prebind-ctran)
             (use-ctran bind postbind-ctran)
-            (ir1-convert-special-bindings postbind-ctran result-ctran result-lvar
-                                          body
-                                          aux-vars aux-vals (svars))))))
+           (ir1-convert-special-bindings postbind-ctran result-ctran
+                                          result-lvar body
+                                          aux-vars aux-vals (svars)
+                                         post-binding-lexenv)))))
 
     (link-blocks (component-head *current-component*) (node-block bind))
     (push lambda (component-new-functionals *current-component*))
 ;;; then we mark the corresponding var as EVER-USED to inhibit
 ;;; "defined but not read" warnings for arguments that are only used
 ;;; by default forms.
-(defun convert-optional-entry (fun vars vals defaults)
+(defun convert-optional-entry (fun vars vals defaults name)
   (declare (type clambda fun) (list vars vals defaults))
   (let* ((fvars (reverse vars))
         (arg-vars (mapcar (lambda (var)
                                                        ,@(reverse vals)
                                                        ,@(default-vals))))
                                          arg-vars
-                                         :debug-name
-                                         (debug-namify "&OPTIONAL processor ~D"
-                                                       (random 100))
+                                         ;; FIXME: Would be nice to
+                                         ;; share these names instead
+                                         ;; of consing up several
+                                         ;; identical ones. Oh well.
+                                         :debug-name (debug-name 
+                                                      '&optional-processor 
+                                                      name)
                                          :note-lexical-bindings nil))))
     (mapc (lambda (var arg-var)
            (when (cdr (leaf-refs arg-var))
                                         vars supplied-p-p body
                                         aux-vars aux-vals
                                         source-name debug-name
-                                        force)
+                                        force post-binding-lexenv)
   (declare (type optional-dispatch res)
           (list default-vars default-vals entry-vars entry-vals vars body
                 aux-vars aux-vals))
                  (list* t arg-name entry-vals)
                  (rest vars) t body aux-vars aux-vals
                  source-name debug-name
-                  force)
+                  force post-binding-lexenv)
                 (ir1-convert-hairy-args
                  res
                  (cons arg default-vars)
                  (cons arg-name entry-vals)
                  (rest vars) supplied-p-p body aux-vars aux-vals
                  source-name debug-name
-                  force))))
+                  force post-binding-lexenv))))
 
     ;; We want to delay converting the entry, but there exist
     ;; problems: hidden references should not be established to
     ;; lambdas of kind NIL should not have (otherwise the compiler
     ;; might let-convert or delete them) and to variables.
-    (if (or force
-            supplied-p-p ; this entry will be of kind NIL
-            (and (lambda-p ep) (eq (lambda-kind ep) nil)))
-        (convert-optional-entry ep
-                                default-vars default-vals
-                                (if supplied-p
-                                    (list default nil)
-                                    (list default)))
-        (delay
-         (register-entry-point
-           (convert-optional-entry (force ep)
-                                   default-vars default-vals
-                                   (if supplied-p
-                                       (list default nil)
-                                       (list default)))
-           res)))))
+    (let ((name (or debug-name source-name))
+          (defaults (if supplied-p (list default nil) (list default))))
+      (if (or force
+              supplied-p-p ; this entry will be of kind NIL
+              (and (lambda-p ep) (eq (lambda-kind ep) nil)))
+          (convert-optional-entry ep
+                                  default-vars default-vals
+                                  defaults
+                                  name)
+          (delay
+           (register-entry-point
+            (convert-optional-entry (force ep)
+                                    default-vars default-vals
+                                    defaults
+                                    name)
+            res))))))
 
 ;;; Create the MORE-ENTRY function for the OPTIONAL-DISPATCH RES.
 ;;; ENTRY-VARS and ENTRY-VALS describe the fixed arguments. REST is
 ;;;
 ;;; We deal with :ALLOW-OTHER-KEYS by delaying unknown keyword errors
 ;;; until we have scanned all the keywords.
-(defun convert-more-entry (res entry-vars entry-vals rest morep keys)
+(defun convert-more-entry (res entry-vars entry-vals rest morep keys name)
   (declare (type optional-dispatch res) (list entry-vars entry-vals keys))
   (collect ((arg-vars)
            (arg-vals (reverse entry-vals))
                                 :type (leaf-type var)
                                 :where-from (leaf-where-from var))))
 
-    (let* ((n-context (gensym "N-CONTEXT-"))
+    (let* ((*allow-instrumenting* nil)
+           (n-context (gensym "N-CONTEXT-"))
           (context-temp (make-lambda-var :%source-name n-context))
           (n-count (gensym "N-COUNT-"))
           (count-temp (make-lambda-var :%source-name n-count
       (arg-vars context-temp count-temp)
 
       (when rest
-       (arg-vals `(%listify-rest-args ,n-context ,n-count)))
+       (arg-vals `(%listify-rest-args
+                   ,n-context ,n-count)))
       (when morep
        (arg-vals n-context)
        (arg-vals n-count))
                 (tests `((eq ,n-key :allow-other-keys)
                          (setq ,n-allowp ,n-value-temp))))
              (tests `(t
-                      (setq ,n-losep ,n-key))))
+                      (setq ,n-losep (list ,n-key)))))
 
            (body
             `(when (oddp ,n-count)
 
            (unless allowp
              (body `(when (and ,n-losep (not ,n-allowp))
-                      (%unknown-key-arg-error ,n-losep)))))))
+                      (%unknown-key-arg-error (car ,n-losep))))))))
 
       (let ((ep (ir1-convert-lambda-body
                 `((let ,(temps)
                     (%funcall ,(optional-dispatch-main-entry res)
                               ,@(arg-vals))))
                 (arg-vars)
-                :debug-name (debug-namify "~S processing" '&more)
+                :debug-name (debug-name '&more-processor name)
                  :note-lexical-bindings nil)))
        (setf (optional-dispatch-more-entry res)
               (register-entry-point ep res)))))
 (defun ir1-convert-more (res default-vars default-vals entry-vars entry-vals
                             rest more-context more-count keys supplied-p-p
                             body aux-vars aux-vals
-                            source-name debug-name)
+                            source-name debug-name post-binding-lexenv)
   (declare (type optional-dispatch res)
           (list default-vars default-vals entry-vars entry-vals keys body
                 aux-vars aux-vals))
               (main-vals (arg-info-default info))
               (bind-vals n-val)))))
 
-    (let* ((main-entry (ir1-convert-lambda-body
+    (let* ((name (or debug-name source-name))
+           (main-entry (ir1-convert-lambda-body
                        body (main-vars)
                        :aux-vars (append (bind-vars) aux-vars)
                        :aux-vals (append (bind-vals) aux-vals)
-                       :debug-name (debug-namify "varargs entry for ~A"
-                                                 (as-debug-name source-name
-                                                                debug-name))))
+                       :post-binding-lexenv post-binding-lexenv
+                       :debug-name (debug-name 'varargs-entry name)))
           (last-entry (convert-optional-entry main-entry default-vars
-                                              (main-vals) ())))
+                                              (main-vals) () name)))
       (setf (optional-dispatch-main-entry res)
             (register-entry-point main-entry res))
-      (convert-more-entry res entry-vars entry-vals rest more-context keys)
+      (convert-more-entry res entry-vars entry-vals rest more-context keys
+                          name)
 
       (push (register-entry-point
              (if supplied-p-p
-               (convert-optional-entry last-entry entry-vars entry-vals ())
+               (convert-optional-entry last-entry entry-vars entry-vals 
+                                        () name)
                last-entry)
              res)
            (optional-dispatch-entry-points res))
                                vars supplied-p-p body aux-vars
                                aux-vals
                                source-name debug-name
-                               force)
+                               force post-binding-lexenv)
   (declare (type optional-dispatch res)
            (list default-vars default-vals entry-vars entry-vals vars body
                  aux-vars aux-vals))
              (ir1-convert-more res default-vars default-vals
                                entry-vars entry-vals
                                nil nil nil vars supplied-p-p body aux-vars
-                               aux-vals source-name debug-name)
-             (let ((fun (ir1-convert-lambda-body
+                               aux-vals source-name debug-name
+                              post-binding-lexenv)
+             (let* ((name (or debug-name source-name))
+                    (fun (ir1-convert-lambda-body
                         body (reverse default-vars)
                         :aux-vars aux-vars
                         :aux-vals aux-vals
-                        :debug-name (debug-namify
-                                     "hairy arg processor for ~A"
-                                     (as-debug-name source-name
-                                                    debug-name)))))
+                        :post-binding-lexenv post-binding-lexenv
+                        :debug-name (debug-name 'hairy-arg-processor name))))
+
                (setf (optional-dispatch-main-entry res) fun)
                (register-entry-point fun res)
                (push (if supplied-p-p
                          (register-entry-point
-                          (convert-optional-entry fun entry-vars entry-vals ())
+                          (convert-optional-entry fun entry-vars entry-vals ()
+                                                  name)
                           res)
                           fun)
                      (optional-dispatch-entry-points res))
            (ir1-convert-hairy-args res nvars nvals nvars nvals
                                    (rest vars) nil body aux-vars aux-vals
                                   source-name debug-name
-                                   nil)))
+                                   nil post-binding-lexenv)))
         (t
          (let* ((arg (first vars))
                 (info (lambda-var-arg-info arg))
                          entry-vars entry-vals vars supplied-p-p body
                          aux-vars aux-vals
                         source-name debug-name
-                         force)))
+                         force post-binding-lexenv)))
                 ;; See GENERATE-OPTIONAL-DEFAULT-ENTRY.
                 (push (if (lambda-p ep)
                           (register-entry-point
                            (if supplied-p-p
-                               (convert-optional-entry ep entry-vars entry-vals ())
+                               (convert-optional-entry 
+                                ep entry-vars entry-vals nil
+                                (or debug-name source-name))
                                ep)
                            res)
                           (progn (aver (not supplied-p-p))
                                 entry-vars entry-vals
                                 arg nil nil (rest vars) supplied-p-p body
                                 aux-vars aux-vals
-                               source-name debug-name))
+                               source-name debug-name
+                               post-binding-lexenv))
              (:more-context
               (ir1-convert-more res default-vars default-vals
                                 entry-vars entry-vals
                                 nil arg (second vars) (cddr vars) supplied-p-p
                                 body aux-vars aux-vals
-                               source-name debug-name))
+                               source-name debug-name
+                               post-binding-lexenv))
              (:keyword
               (ir1-convert-more res default-vars default-vals
                                 entry-vars entry-vals
                                 nil nil nil vars supplied-p-p body aux-vars
-                                aux-vals source-name debug-name)))))))
+                                aux-vals source-name debug-name
+                               post-binding-lexenv)))))))
 
 ;;; This function deals with the case where we have to make an
 ;;; OPTIONAL-DISPATCH to represent a LAMBDA. We cons up the result and
 ;;; figure out the MIN-ARGS and MAX-ARGS.
 (defun ir1-convert-hairy-lambda (body vars keyp allowp aux-vars aux-vals
                                      &key
+                                     post-binding-lexenv
                                      (source-name '.anonymous.)
-                                     (debug-name (debug-namify
-                                                  "OPTIONAL-DISPATCH ~S"
-                                                  vars)))
+                                     (debug-name 
+                                       (debug-name '&optional-dispatch vars)))
   (declare (list body vars aux-vars aux-vals))
   (let ((res (make-optional-dispatch :arglist vars
                                     :allowp allowp
     (aver-live-component *current-component*)
     (push res (component-new-functionals *current-component*))
     (ir1-convert-hairy-args res () () () () vars nil body aux-vars aux-vals
-                           source-name debug-name nil)
+                           source-name debug-name nil post-binding-lexenv)
     (setf (optional-dispatch-min-args res) min)
     (setf (optional-dispatch-max-args res)
          (+ (1- (length (optional-dispatch-entry-points res))) min))
 
 ;;; Convert a LAMBDA form into a LAMBDA leaf or an OPTIONAL-DISPATCH leaf.
 (defun ir1-convert-lambda (form &key (source-name '.anonymous.)
-                           debug-name
-                           allow-debug-catch-tag)
-
+                           debug-name)
   (unless (consp form)
     (compiler-error "A ~S was found when expecting a lambda expression:~%  ~S"
                    (type-of form)
      "The lambda expression has a missing or non-list lambda list:~%  ~S"
      form))
 
-  (let ((*allow-debug-catch-tag* (and *allow-debug-catch-tag* allow-debug-catch-tag)))
-    (multiple-value-bind (vars keyp allow-other-keys aux-vars aux-vals)
-       (make-lambda-vars (cadr form))
-      (multiple-value-bind (forms decls) (parse-body (cddr form))
-       (binding* (((*lexenv* result-type)
-                    (process-decls decls (append aux-vars vars) nil))
-                   (forms (if (and *allow-debug-catch-tag*
-                                   (policy *lexenv* (>= insert-debug-catch 2)))
-                              `((catch (make-symbol "SB-DEBUG-CATCH-TAG")
-                                  ,@forms))
-                              forms))
-                   (forms (if (eq result-type *wild-type*)
-                              forms
-                              `((the ,result-type (progn ,@forms)))))
-                   (res (if (or (find-if #'lambda-var-arg-info vars) keyp)
-                            (ir1-convert-hairy-lambda forms vars keyp
-                                                      allow-other-keys
-                                                      aux-vars aux-vals
-                                                      :source-name source-name
-                                                      :debug-name debug-name)
-                            (ir1-convert-lambda-body forms vars
-                                                     :aux-vars aux-vars
-                                                     :aux-vals aux-vals
-                                                     :source-name source-name
-                                                     :debug-name debug-name))))
-         (setf (functional-inline-expansion res) form)
-         (setf (functional-arg-documentation res) (cadr form))
-         res)))))
+  (multiple-value-bind (vars keyp allow-other-keys aux-vars aux-vals)
+      (make-lambda-vars (cadr form))
+    (multiple-value-bind (forms decls) (parse-body (cddr form))
+      (binding* (((*lexenv* result-type post-binding-lexenv)
+                  (process-decls decls (append aux-vars vars) nil
+                                :binding-form-p t))
+                 (forms (if (and *allow-instrumenting*
+                                 (policy *lexenv* (>= insert-debug-catch 2)))
+                            `((catch (locally 
+                                         (declare (optimize (insert-step-conditions 0)))
+                                    (make-symbol "SB-DEBUG-CATCH-TAG"))
+                                ,@forms))
+                            forms))
+                 (forms (if (eq result-type *wild-type*)
+                            forms
+                            `((the ,result-type (progn ,@forms)))))
+                 (res (if (or (find-if #'lambda-var-arg-info vars) keyp)
+                          (ir1-convert-hairy-lambda forms vars keyp
+                                                    allow-other-keys
+                                                    aux-vars aux-vals
+                                                   :post-binding-lexenv post-binding-lexenv                                                
+                                                    :source-name source-name
+                                                    :debug-name debug-name)
+                          (ir1-convert-lambda-body forms vars
+                                                   :aux-vars aux-vars
+                                                   :aux-vals aux-vals
+                                                  :post-binding-lexenv post-binding-lexenv
+                                                   :source-name source-name
+                                                   :debug-name debug-name))))
+        (setf (functional-inline-expansion res) form)
+        (setf (functional-arg-documentation res) (cadr form))
+        res))))
 
 ;;; helper for LAMBDA-like things, to massage them into a form
 ;;; suitable for IR1-CONVERT-LAMBDA.
-;;;
-;;; KLUDGE: We cons up a &REST list here, maybe for no particularly
-;;; good reason.  It's probably lost in the noise of all the other
-;;; consing, but it's still inelegant.  And we force our called
-;;; functions to do full runtime keyword parsing, ugh.  -- CSR,
-;;; 2003-01-25
-(defun ir1-convert-lambdalike (thing &rest args
-                              &key (source-name '.anonymous.)
-                              debug-name allow-debug-catch-tag)
-  (declare (ignorable source-name debug-name allow-debug-catch-tag))
+(defun ir1-convert-lambdalike (thing
+                              &key 
+                               (source-name '.anonymous.)
+                              debug-name)
   (ecase (car thing)
-    ((lambda) (apply #'ir1-convert-lambda thing args))
+    ((lambda) 
+     (ir1-convert-lambda thing 
+                         :source-name source-name 
+                         :debug-name debug-name))
     ((instance-lambda)
-     (let ((res (apply #'ir1-convert-lambda
-                      `(lambda ,@(cdr thing)) args)))
+     (let ((res (ir1-convert-lambda `(lambda ,@(cdr thing))
+                                    :source-name source-name
+                                    :debug-name debug-name)))
        (setf (getf (functional-plist res) :fin-function) t)
        res))
     ((named-lambda)
-     (let ((name (cadr thing)))
+     (let ((name (cadr thing))
+           (lambda-expression `(lambda ,@(cddr thing))))
        (if (legal-fun-name-p name)
           (let ((defined-fun-res (get-defined-fun name))
-                 (res (apply #'ir1-convert-lambda `(lambda ,@(cddr thing))
-                            :source-name name
-                            :debug-name nil
-                            args)))
+                 (res (ir1-convert-lambda lambda-expression 
+                                          :source-name name)))
             (assert-global-function-definition-type name res)
-             (setf (defined-fun-functional defined-fun-res)
-                   res)
+             (setf (defined-fun-functional defined-fun-res) res)
              (unless (eq (defined-fun-inlinep defined-fun-res) :notinline)
                (substitute-leaf-if
                 (lambda (ref)
                   (policy ref (> recognize-self-calls 0)))
                 res defined-fun-res))
             res)
-          (apply #'ir1-convert-lambda `(lambda ,@(cddr thing))
-                 :debug-name name args))))
-    ((lambda-with-lexenv) (apply #'ir1-convert-inline-lambda thing args))))
+          (ir1-convert-lambda lambda-expression :debug-name name))))
+    ((lambda-with-lexenv) 
+     (ir1-convert-inline-lambda thing 
+                                :source-name source-name 
+                                :debug-name debug-name))))
 \f
 ;;;; defining global functions
 
 ;;; reflect the state at the definition site.
 (defun ir1-convert-inline-lambda (fun &key
                                      (source-name '.anonymous.)
-                                     debug-name
-                                     allow-debug-catch-tag)
+                                     debug-name)
   (destructuring-bind (decls macros symbol-macros &rest body)
                      (if (eq (car fun) 'lambda-with-lexenv)
                          (cdr fun)
                          `(() () () . ,(cdr fun)))
     (let ((*lexenv* (make-lexenv
                     :default (process-decls decls nil nil
-                                            (make-null-lexenv))
+                                            :lexenv (make-null-lexenv))
                     :vars (copy-list symbol-macros)
                     :funs (mapcar (lambda (x)
                                     `(,(car x) .
                     :policy (lexenv-policy *lexenv*))))
       (ir1-convert-lambda `(lambda ,@body)
                          :source-name source-name
-                         :debug-name debug-name
-                         :allow-debug-catch-tag nil))))
+                         :debug-name debug-name))))
 
 ;;; Get a DEFINED-FUN object for a function we are about to define. If
 ;;; the function has been forward referenced, then substitute for the
 ;;;
 ;;; The INLINE-EXPANSION is a LAMBDA-WITH-LEXENV, or NIL if there is
 ;;; no inline expansion.
-(defun %compiler-defun (name lambda-with-lexenv)
-
+(defun %compiler-defun (name lambda-with-lexenv compile-toplevel)
   (let ((defined-fun nil)) ; will be set below if we're in the compiler
-
-    (when (boundp '*lexenv*) ; when in the compiler
-      (when sb!xc:*compile-print*
-       (compiler-mumble "~&; recognizing DEFUN ~S~%" name))
+    (when compile-toplevel
+      ;; better be in the compiler
+      (aver (boundp '*lexenv*)) 
       (remhash name *free-funs*)
-      (setf defined-fun (get-defined-fun name)))
+      (setf defined-fun (get-defined-fun name))
+      (aver (fasl-output-p *compile-object*))
+      (if (member name *fun-names-in-this-file* :test #'equal)
+         (warn 'duplicate-definition :name name)
+         (push name *fun-names-in-this-file*)))
 
     (become-defined-fun-name name)
-
+    
     (cond (lambda-with-lexenv
           (setf (info :function :inline-expansion-designator name)
                 lambda-with-lexenv)