inline.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. ;;; inline.el --- Define functions by their inliner -*- lexical-binding:t; -*-
  2. ;; Copyright (C) 2014-2017 Free Software Foundation, Inc.
  3. ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This package provides the macro `define-inline' which lets you define
  17. ;; functions by defining their (exhaustive) compiler macro.
  18. ;;
  19. ;; The idea is that instead of doing like defsubst and cl-defsubst (i.e. from
  20. ;; the function's definition, guess the best way to inline the function),
  21. ;; we go the other way around: the programmer provides the code that does the
  22. ;; inlining (as a compiler-macro) and from that we derive the definition of the
  23. ;; function itself. The idea originated in an attempt to clean up `cl-typep',
  24. ;; whose function definition amounted to (eval (cl--make-type-test EXP TYPE)).
  25. ;;
  26. ;; The simplest use is for plain and simple inlinable functions. Rather than:
  27. ;;
  28. ;; (defmacro myaccessor (obj)
  29. ;; (macroexp-let2 macroexp-copyable-p obj obj
  30. ;; `(if (foop ,obj) (aref (cdr ,obj) 3) (aref ,obj 2))))
  31. ;; Or
  32. ;; (defsubst myaccessor (obj)
  33. ;; (if (foop obj) (aref (cdr obj) 3) (aref obj 2)))
  34. ;; Or
  35. ;; (cl-defsubst myaccessor (obj)
  36. ;; (if (foop obj) (aref (cdr obj) 3) (aref obj 2)))
  37. ;;
  38. ;; You'd do
  39. ;;
  40. ;; (define-inline myaccessor (obj)
  41. ;; (inline-letevals (obj)
  42. ;; (inline-quote (if (foop ,obj) (aref (cdr ,obj) 3) (aref ,obj 2)))))
  43. ;;
  44. ;; Other than verbosity, you get the best of all 3 above without their
  45. ;; respective downsides:
  46. ;; - defmacro: can't be passed to `mapcar' since it's not a function.
  47. ;; - defsubst: not as efficient, and doesn't work as a `gv' place.
  48. ;; - cl-defsubst: only works by accident, since it has latent bugs in its
  49. ;; handling of variables and scopes which could bite you at any time.
  50. ;; (e.g. try (cl-defsubst my-test1 (x) (let ((y 5)) (+ x y)))
  51. ;; and then M-: (macroexpand-all '(my-test1 y)) RET)
  52. ;; There is still one downside shared with the defmacro and cl-defsubst
  53. ;; approach: when the function is inlined, the scoping rules (dynamic or
  54. ;; lexical) will be inherited from the the call site.
  55. ;; Of course, since define-inline defines a compiler macro, you can also do
  56. ;; call-site optimizations, just like you can with `defmacro', but not with
  57. ;; defsubst nor cl-defsubst.
  58. ;;; Code:
  59. (require 'macroexp)
  60. (defmacro inline-quote (_exp)
  61. "Similar to backquote, but quotes code and only accepts , and not ,@."
  62. (declare (debug t))
  63. (error "inline-quote can only be used within define-inline"))
  64. (defmacro inline-const-p (_exp)
  65. "Return non-nil if the value of EXP is already known."
  66. (declare (debug t))
  67. (error "inline-const-p can only be used within define-inline"))
  68. (defmacro inline-const-val (_exp)
  69. "Return the value of EXP."
  70. (declare (debug t))
  71. (error "inline-const-val can only be used within define-inline"))
  72. (defmacro inline-error (_format &rest _args)
  73. "Signal an error."
  74. (declare (debug t))
  75. (error "inline-error can only be used within define-inline"))
  76. (defmacro inline--leteval (_var-exp &rest _body)
  77. (declare (indent 1) (debug (sexp &rest body)))
  78. (error "inline-letevals can only be used within define-inline"))
  79. (defmacro inline--letlisteval (_list &rest _body)
  80. (declare (indent 1) (debug (sexp &rest body)))
  81. (error "inline-letevals can only be used within define-inline"))
  82. (defmacro inline-letevals (vars &rest body)
  83. "Make sure the expressions in VARS are evaluated.
  84. VARS should be a list of elements of the form (VAR EXP) or just VAR, in case
  85. EXP is equal to VAR. The result is to evaluate EXP and bind the result to VAR.
  86. The tail of VARS can be either nil or a symbol VAR which should hold a list
  87. of arguments, in which case each argument is evaluated and the resulting
  88. new list is re-bound to VAR.
  89. After VARS is handled, BODY is evaluated in the new environment."
  90. (declare (indent 1) (debug (sexp &rest form)))
  91. (cond
  92. ((consp vars)
  93. `(inline--leteval ,(pop vars) (inline-letevals ,vars ,@body)))
  94. (vars
  95. `(inline--letlisteval ,vars ,@body))
  96. (t (macroexp-progn body))))
  97. ;; (defmacro inline-if (testfun testexp then else)
  98. ;; (declare (indent 2) (debug (sexp symbolp form form)))
  99. ;; (macroexp-let2 macroexp-copyable-p testsym testexp
  100. ;; `(if (inline-const-p ,testexp)
  101. ;; (if (,testfun (inline-const-val ,testexp)) ,then ,else)
  102. ;; (inline-quote (if (,testfun ,testexp) ,(list '\, then)
  103. ;; ,(list '\, else))))))
  104. ;;;###autoload
  105. (defmacro define-inline (name args &rest body)
  106. ;; FIXME: How can this work with CL arglists?
  107. (declare (indent defun) (debug defun) (doc-string 3))
  108. (let ((doc (if (stringp (car-safe body)) (list (pop body))))
  109. (declares (if (eq (car-safe (car-safe body)) 'declare) (pop body)))
  110. (cm-name (intern (format "%s--inliner" name)))
  111. (bodyexp (macroexp-progn body)))
  112. ;; If the function is autoloaded then when we load the .el file, the
  113. ;; `compiler-macro' property is already set (from loaddefs.el) and might
  114. ;; hence be called during the macroexpand-all calls below (if the function
  115. ;; is recursive).
  116. ;; So we disable any pre-loaded compiler-macro setting to avoid this.
  117. (function-put name 'compiler-macro nil)
  118. `(progn
  119. (defun ,name ,args
  120. ,@doc
  121. (declare (compiler-macro ,cm-name) ,@(cdr declares))
  122. ,(macroexpand-all bodyexp
  123. `((inline-quote . inline--dont-quote)
  124. ;; (inline-\` . inline--dont-quote)
  125. (inline--leteval . inline--dont-leteval)
  126. (inline--letlisteval . inline--dont-letlisteval)
  127. (inline-const-p . inline--alwaysconst-p)
  128. (inline-const-val . inline--alwaysconst-val)
  129. (inline-error . inline--error)
  130. ,@macroexpand-all-environment)))
  131. :autoload-end
  132. (eval-and-compile
  133. (defun ,cm-name ,(cons 'inline--form args)
  134. (ignore inline--form) ;In case it's not used!
  135. (catch 'inline--just-use
  136. ,(macroexpand-all
  137. bodyexp
  138. `((inline-quote . inline--do-quote)
  139. ;; (inline-\` . inline--do-quote)
  140. (inline--leteval . inline--do-leteval)
  141. (inline--letlisteval
  142. . inline--do-letlisteval)
  143. (inline-const-p . inline--testconst-p)
  144. (inline-const-val . inline--getconst-val)
  145. (inline-error . inline--warning)
  146. ,@macroexpand-all-environment))))))))
  147. (defun inline--do-quote (exp)
  148. (pcase exp
  149. (`(,'\, ,e) e) ;Eval `e' now *and* later.
  150. (`'(,'\, ,e) `(list 'quote ,e)) ;Only eval `e' now, not later.
  151. (`#'(,'\, ,e) `(list 'function ,e)) ;Only eval `e' now, not later.
  152. ((pred consp)
  153. (let ((args ()))
  154. (while (and (consp exp) (not (eq '\, (car exp))))
  155. (push (inline--do-quote (pop exp)) args))
  156. (setq args (nreverse args))
  157. (if exp
  158. `(backquote-list* ,@args ,(inline--do-quote exp))
  159. `(list ,@args))))
  160. (_ (macroexp-quote exp))))
  161. (defun inline--dont-quote (exp)
  162. (pcase exp
  163. (`(,'\, ,e) e)
  164. (`'(,'\, ,e) e)
  165. (`#'(,'\, ,e) e)
  166. ((pred consp)
  167. (let ((args ()))
  168. (while (and (consp exp) (not (eq '\, (car exp))))
  169. (push (inline--dont-quote (pop exp)) args))
  170. (setq args (nreverse args))
  171. (if (null exp)
  172. args
  173. `(apply #',(car args) ,@(cdr args) ,(inline--dont-quote exp)))))
  174. (_ exp)))
  175. (defun inline--do-leteval (var-exp &rest body)
  176. `(macroexp-let2 ,(if (symbolp var-exp) #'macroexp-copyable-p #'ignore)
  177. ,(or (car-safe var-exp) var-exp)
  178. ,(or (car (cdr-safe var-exp)) var-exp)
  179. ,@body))
  180. (defun inline--dont-leteval (var-exp &rest body)
  181. (if (symbolp var-exp)
  182. (macroexp-progn body)
  183. `(let (,var-exp) ,@body)))
  184. (defun inline--do-letlisteval (listvar &rest body)
  185. ;; Here's a sample situation:
  186. ;; (define-inline foo (arg &rest keys)
  187. ;; (inline-letevals (arg . keys)
  188. ;; <check-keys>))
  189. ;; I.e. in <check-keys> we need `keys' to contain a list of
  190. ;; macroexp-copyable-p expressions.
  191. (let ((bsym (make-symbol "bindings")))
  192. `(let* ((,bsym ())
  193. (,listvar (mapcar (lambda (e)
  194. (if (macroexp-copyable-p e) e
  195. (let ((v (make-symbol "v")))
  196. (push (list v e) ,bsym)
  197. v)))
  198. ,listvar)))
  199. (macroexp-let* (nreverse ,bsym)
  200. ,(macroexp-progn body)))))
  201. (defun inline--dont-letlisteval (_listvar &rest body)
  202. (macroexp-progn body))
  203. (defun inline--testconst-p (exp)
  204. (macroexp-let2 macroexp-copyable-p exp exp
  205. `(or (macroexp-const-p ,exp)
  206. (eq (car-safe ,exp) 'function))))
  207. (defun inline--alwaysconst-p (_exp)
  208. t)
  209. (defun inline--getconst-val (exp)
  210. (macroexp-let2 macroexp-copyable-p exp exp
  211. `(cond
  212. ((not ,(inline--testconst-p exp))
  213. (throw 'inline--just-use inline--form))
  214. ((consp ,exp) (cadr ,exp))
  215. (t ,exp))))
  216. (defun inline--alwaysconst-val (exp)
  217. exp)
  218. (defun inline--error (&rest args)
  219. `(error ,@args))
  220. (defun inline--warning (&rest _args)
  221. `(throw 'inline--just-use
  222. ;; FIXME: This would inf-loop by calling us right back when
  223. ;; macroexpand-all recurses to expand inline--form.
  224. ;; (macroexp--warn-and-return (format ,@args)
  225. ;; inline--form)
  226. inline--form))
  227. (provide 'inline)
  228. ;;; inline.el ends here