]> code.delx.au - gnu-emacs/blobdiff - lisp/emacs-lisp/cconv.el
Add new function dom-remove-node
[gnu-emacs] / lisp / emacs-lisp / cconv.el
index e9d33e6c6467c34f4836571d31117d8b63da7bb9..50b1fe326618b0984db880620be3ecb7506ad055 100644 (file)
@@ -1,6 +1,6 @@
-;;; cconv.el --- Closure conversion for statically scoped Emacs lisp. -*- lexical-binding: t; coding: utf-8 -*-
+;;; cconv.el --- Closure conversion for statically scoped Emacs lisp. -*- lexical-binding: t -*-
 
-;; Copyright (C) 2011-2015 Free Software Foundation, Inc.
+;; Copyright (C) 2011-2016 Free Software Foundation, Inc.
 
 ;; Author: Igor Kuzmin <kzuminig@iro.umontreal.ca>
 ;; Maintainer: emacs-devel@gnu.org
@@ -48,7 +48,7 @@
 ;; if the function is suitable for lambda lifting (if all calls are known)
 ;;
 ;; (lambda (v0 ...) ... fv0 .. fv1 ...)  =>
-;; (internal-make-closure (v0 ...) (fv1 ...)
+;; (internal-make-closure (v0 ...) (fv0 ...) <doc>
 ;;   ... (internal-get-closed-var 0) ...  (internal-get-closed-var 1) ...)
 ;;
 ;; If the function has no free variables, we don't do anything.
 ;;
 ;;; Code:
 
+;; PROBLEM cases found during conversion to lexical binding.
+;; We should try and detect and warn about those cases, even
+;; for lexical-binding==nil to help prepare the migration.
+;; - Uses of run-hooks, and friends.
+;; - Cases where we want to apply the same code to different vars depending on
+;;   some test.  These sometimes use a (let ((foo (if bar 'a 'b)))
+;;   ... (symbol-value foo) ... (set foo ...)).
+
 ;; TODO: (not just for cconv but also for the lexbind changes in general)
 ;; - let (e)debug find the value of lexical variables from the stack.
 ;; - make eval-region do the eval-sexp-add-defvars dance.
@@ -87,9 +95,8 @@
 ;;   the bytecomp only compiles it once.
 ;; - Since we know here when a variable is not mutated, we could pass that
 ;;   info to the byte-compiler, e.g. by using a new `immutable-let'.
-;; - add tail-calls to bytecode.c and the byte compiler.
 ;; - call known non-escaping functions with `goto' rather than `call'.
-;; - optimize mapcar to a while loop.
+;; - optimize mapc to a dolist loop.
 
 ;; (defmacro dlet (binders &rest body)
 ;;   ;; Works in both lexical and non-lexical mode.
@@ -195,7 +202,7 @@ Returns a form where all lambdas don't have any free variables."
       (unless (memq (car b) s) (push b res)))
     (nreverse res)))
 
-(defun cconv--convert-function (args body env parentform)
+(defun cconv--convert-function (args body env parentform &optional docstring)
   (cl-assert (equal body (caar cconv-freevars-alist)))
   (let* ((fvs (cdr (pop cconv-freevars-alist)))
          (body-new '())
@@ -240,11 +247,11 @@ Returns a form where all lambdas don't have any free variables."
               `(,@(nreverse special-forms) (let ,letbind . ,body-new)))))
 
     (cond
-     ((null envector)                   ;if no freevars - do nothing
+     ((not (or envector docstring))     ;If no freevars - do nothing.
       `(function (lambda ,args . ,body-new)))
      (t
       `(internal-make-closure
-        ,args ,envector . ,body-new)))))
+        ,args ,envector ,docstring . ,body-new)))))
 
 (defun cconv-convert (form env extend)
   ;; This function actually rewrites the tree.
@@ -293,7 +300,8 @@ places where they originally did not directly appear."
                         (prog1 binder (setq binder (list binder)))
                        (when (cddr binder)
                          (byte-compile-log-warning
-                          (format "Malformed `%S' binding: %S" letsym binder)))
+                          (format-message "Malformed `%S' binding: %S"
+                                          letsym binder)))
                       (setq value (cadr binder))
                       (car binder)))
                (new-val
@@ -407,7 +415,9 @@ places where they originally did not directly appear."
                        cond-forms)))
 
     (`(function (lambda ,args . ,body) . ,_)
-     (cconv--convert-function args body env form))
+     (let ((docstring (if (eq :documentation (car-safe (car body)))
+                          (cconv-convert (cadr (pop body)) env extend))))
+       (cconv--convert-function args body env form docstring)))
 
     (`(internal-make-closure . ,_)
      (byte-compile-report-error
@@ -463,25 +473,28 @@ places where they originally did not directly appear."
         :fun-body ,(cconv--convert-function () body env form)))
 
     (`(setq . ,forms)                   ; setq special form
-     (let ((prognlist ()))
-       (while forms
-         (let* ((sym (pop forms))
-                (sym-new (or (cdr (assq sym env)) sym))
-                (value (cconv-convert (pop forms) env extend)))
-           (push (pcase sym-new
-                   ((pred symbolp) `(setq ,sym-new ,value))
-                   (`(car-safe ,iexp) `(setcar ,iexp ,value))
-                   ;; This "should never happen", but for variables which are
-                   ;; mutated+captured+unused, we may end up trying to `setq'
-                   ;; on a closed-over variable, so just drop the setq.
-                   (_ ;; (byte-compile-report-error
-                    ;;  (format "Internal error in cconv of (setq %s ..)"
-                    ;;          sym-new))
-                    value))
-                 prognlist)))
-       (if (cdr prognlist)
-           `(progn . ,(nreverse prognlist))
-         (car prognlist))))
+     (if (= (logand (length forms) 1) 1)
+         ;; With an odd number of args, let bytecomp.el handle the error.
+         form
+       (let ((prognlist ()))
+         (while forms
+           (let* ((sym (pop forms))
+                  (sym-new (or (cdr (assq sym env)) sym))
+                  (value (cconv-convert (pop forms) env extend)))
+             (push (pcase sym-new
+                     ((pred symbolp) `(setq ,sym-new ,value))
+                     (`(car-safe ,iexp) `(setcar ,iexp ,value))
+                     ;; This "should never happen", but for variables which are
+                     ;; mutated+captured+unused, we may end up trying to `setq'
+                     ;; on a closed-over variable, so just drop the setq.
+                     (_ ;; (byte-compile-report-error
+                      ;;  (format "Internal error in cconv of (setq %s ..)"
+                      ;;          sym-new))
+                      value))
+                   prognlist)))
+         (if (cdr prognlist)
+             `(progn . ,(nreverse prognlist))
+           (car prognlist)))))
 
     (`(,(and (or `funcall `apply) callsym) ,fun . ,args)
      ;; These are not special forms but we treat them separately for the needs
@@ -533,10 +546,10 @@ FORM is the parent form that binds this var."
   ;; use = `(,binder ,read ,mutated ,captured ,called)
   (pcase vardata
     (`(,_ nil nil nil nil) nil)
-    (`((,(and (pred (lambda (var) (eq ?_ (aref (symbol-name var) 0)))) var) . ,_)
+    (`((,(and var (guard (eq ?_ (aref (symbol-name var) 0)))) . ,_)
        ,_ ,_ ,_ ,_)
      (byte-compile-log-warning
-      (format "%s `%S' not left unused" varkind var))))
+      (format-message "%s `%S' not left unused" varkind var))))
   (pcase vardata
     (`((,var . ,_) nil ,_ ,_ nil)
      ;; FIXME: This gives warnings in the wrong order, with imprecise line
@@ -548,8 +561,8 @@ FORM is the parent form that binds this var."
               (eq ?_ (aref (symbol-name var) 0))
              ;; As a special exception, ignore "ignore".
              (eq var 'ignored))
-       (byte-compile-log-warning (format "Unused lexical %s `%S'"
-                                         varkind var))))
+       (byte-compile-log-warning (format-message "Unused lexical %s `%S'"
+                                                 varkind var))))
     ;; If it's unused, there's no point converting it into a cons-cell, even if
     ;; it's captured and mutated.
     (`(,binder ,_ t t ,_)
@@ -643,6 +656,8 @@ and updates the data stored in ENV."
          (cconv--analyze-use vardata form "variable"))))
 
     (`(function (lambda ,vrs . ,body-forms))
+     (when (eq :documentation (car-safe (car body-forms)))
+       (cconv-analyze-form (cadr (pop body-forms)) env))
      (cconv--analyze-function vrs body-forms env form))
 
     (`(setq . ,forms)
@@ -665,6 +680,10 @@ and updates the data stored in ENV."
      (dolist (forms cond-forms)
        (dolist (form forms) (cconv-analyze-form form env))))
 
+    ;; ((and `(quote ,v . ,_) (guard (assq v env)))
+    ;;  (byte-compile-log-warning
+    ;;   (format-message "Possible confusion variable/symbol for `%S'" v)))
+
     (`(quote . ,_) nil)                 ; quote form
     (`(function . ,_) nil)              ; same as quote