summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp/inline.el
blob: 058c56c3b49a1c8d09cc8e92c5a38b6a84ca4724 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
;;; inline.el --- Define functions by their inliner  -*- lexical-binding:t; -*-

;; Copyright (C) 2014-2016 Free Software Foundation, Inc.

;; Author: Stefan Monnier <monnier@iro.umontreal.ca>

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This package provides the macro `define-inline' which lets you define
;; functions by defining their (exhaustive) compiler macro.
;;
;; The idea is that instead of doing like defsubst and cl-defsubst (i.e. from
;; the function's definition, guess the best way to inline the function),
;; we go the other way around: the programmer provides the code that does the
;; inlining (as a compiler-macro) and from that we derive the definition of the
;; function itself.  The idea originated in an attempt to clean up `cl-typep',
;; whose function definition amounted to (eval (cl--make-type-test EXP TYPE)).
;;
;; The simplest use is for plain and simple inlinable functions.  Rather than:
;;
;;     (defmacro myaccessor (obj)
;;       (macroexp-let2 macroexp-copyable-p obj obj
;;         `(if (foop ,obj) (aref (cdr ,obj) 3) (aref ,obj 2))))
;; Or
;;     (defsubst myaccessor (obj)
;;       (if (foop obj) (aref (cdr obj) 3) (aref obj 2)))
;; Or
;;     (cl-defsubst myaccessor (obj)
;;       (if (foop obj) (aref (cdr obj) 3) (aref obj 2)))
;;
;; You'd do
;;
;;     (define-inline myaccessor (obj)
;;       (inline-letevals (obj)
;;         (inline-quote (if (foop ,obj) (aref (cdr ,obj) 3) (aref ,obj 2)))))
;;
;; Other than verbosity, you get the best of all 3 above without their
;; respective downsides:
;; - defmacro: can't be passed to `mapcar' since it's not a function.
;; - defsubst: not as efficient, and doesn't work as a `gv' place.
;; - cl-defsubst: only works by accident, since it has latent bugs in its
;;   handling of variables and scopes which could bite you at any time.
;;   (e.g. try (cl-defsubst my-test1 (x) (let ((y 5)) (+ x y)))
;;         and then M-: (macroexpand-all '(my-test1 y)) RET)
;; There is still one downside shared with the defmacro and cl-defsubst
;; approach: when the function is inlined, the scoping rules (dynamic or
;; lexical) will be inherited from the the call site.

;; Of course, since define-inline defines a compiler macro, you can also do
;; call-site optimizations, just like you can with `defmacro', but not with
;; defsubst nor cl-defsubst.

;;; Code:

(require 'macroexp)

(defmacro inline-quote (_exp)
  "Similar to backquote, but quotes code and only accepts , and not ,@."
  (declare (debug t))
  (error "inline-quote can only be used within define-inline"))

(defmacro inline-const-p (_exp)
  "Return non-nil if the value of EXP is already known."
  (declare (debug t))
  (error "inline-const-p can only be used within define-inline"))

(defmacro inline-const-val (_exp)
  "Return the value of EXP."
  (declare (debug t))
  (error "inline-const-val can only be used within define-inline"))

(defmacro inline-error (_format &rest _args)
  "Signal an error."
  (declare (debug t))
  (error "inline-error can only be used within define-inline"))

(defmacro inline--leteval (_var-exp &rest _body)
  (declare (indent 1) (debug (sexp &rest body)))
  (error "inline-letevals can only be used within define-inline"))
(defmacro inline--letlisteval (_list &rest _body)
  (declare (indent 1) (debug (sexp &rest body)))
  (error "inline-letevals can only be used within define-inline"))

(defmacro inline-letevals (vars &rest body)
  "Make sure the expressions in VARS are evaluated.
VARS should be a list of elements of the form (VAR EXP) or just VAR, in case
EXP is equal to VAR.  The result is to evaluate EXP and bind the result to VAR.

The tail of VARS can be either nil or a symbol VAR which should hold a list
of arguments, in which case each argument is evaluated and the resulting
new list is re-bound to VAR.

After VARS is handled, BODY is evaluated in the new environment."
  (declare (indent 1) (debug (sexp &rest form)))
  (cond
   ((consp vars)
    `(inline--leteval ,(pop vars) (inline-letevals ,vars ,@body)))
   (vars
    `(inline--letlisteval ,vars ,@body))
   (t (macroexp-progn body))))

;; (defmacro inline-if (testfun testexp then else)
;;   (declare (indent 2) (debug (sexp symbolp form form)))
;;   (macroexp-let2 macroexp-copyable-p testsym testexp
;;     `(if (inline-const-p ,testexp)
;;          (if (,testfun (inline-const-val ,testexp)) ,then ,else)
;;        (inline-quote (if (,testfun ,testexp) ,(list '\, then)
;;                         ,(list '\, else))))))

;;;###autoload
(defmacro define-inline (name args &rest body)
  ;; FIXME: How can this work with CL arglists?
  (declare (indent defun) (debug defun) (doc-string 3))
  (let ((doc (if (stringp (car-safe body)) (list (pop body))))
        (declares (if (eq (car-safe (car-safe body)) 'declare) (pop body)))
        (cm-name (intern (format "%s--inliner" name)))
        (bodyexp (macroexp-progn body)))
    ;; If the function is autoloaded then when we load the .el file, the
    ;; `compiler-macro' property is already set (from loaddefs.el) and might
    ;; hence be called during the macroexpand-all calls below (if the function
    ;; is recursive).
    ;; So we disable any pre-loaded compiler-macro setting to avoid this.
    (function-put name 'compiler-macro nil)
    `(progn
       (defun ,name ,args
         ,@doc
         (declare (compiler-macro ,cm-name) ,@(cdr declares))
         ,(macroexpand-all bodyexp
                           `((inline-quote . inline--dont-quote)
                             ;; (inline-\` . inline--dont-quote)
                             (inline--leteval . inline--dont-leteval)
                             (inline--letlisteval . inline--dont-letlisteval)
                             (inline-const-p . inline--alwaysconst-p)
                             (inline-const-val . inline--alwaysconst-val)
                             (inline-error . inline--error)
                             ,@macroexpand-all-environment)))
       :autoload-end
       (eval-and-compile
         (defun ,cm-name ,(cons 'inline--form args)
           (ignore inline--form)     ;In case it's not used!
           (catch 'inline--just-use
             ,(macroexpand-all
               bodyexp
               `((inline-quote . inline--do-quote)
                 ;; (inline-\` . inline--do-quote)
                 (inline--leteval . inline--do-leteval)
                 (inline--letlisteval
                  . inline--do-letlisteval)
                 (inline-const-p . inline--testconst-p)
                 (inline-const-val . inline--getconst-val)
                 (inline-error . inline--warning)
                 ,@macroexpand-all-environment))))))))

(defun inline--do-quote (exp)
  (pcase exp
    (`(,'\, ,e) e)                      ;Eval `e' now *and* later.
    (`'(,'\, ,e) `(list 'quote ,e))     ;Only eval `e' now, not later.
    (`#'(,'\, ,e) `(list 'function ,e)) ;Only eval `e' now, not later.
    ((pred consp)
     (let ((args ()))
       (while (and (consp exp) (not (eq '\, (car exp))))
         (push (inline--do-quote (pop exp)) args))
       (setq args (nreverse args))
       (if exp
           `(backquote-list* ,@args ,(inline--do-quote exp))
         `(list ,@args))))
    (_ (macroexp-quote exp))))

(defun inline--dont-quote (exp)
  (pcase exp
    (`(,'\, ,e) e)
    (`'(,'\, ,e) e)
    (`#'(,'\, ,e) e)
    ((pred consp)
     (let ((args ()))
       (while (and (consp exp) (not (eq '\, (car exp))))
         (push (inline--dont-quote (pop exp)) args))
       (setq args (nreverse args))
       (if exp
           `(apply ,@args ,(inline--dont-quote exp))
         args)))
    (_ exp)))

(defun inline--do-leteval (var-exp &rest body)
  `(macroexp-let2 ,(if (symbolp var-exp) #'macroexp-copyable-p #'ignore)
       ,(or (car-safe var-exp) var-exp)
       ,(or (car (cdr-safe var-exp)) var-exp)
     ,@body))

(defun inline--dont-leteval (var-exp &rest body)
  (if (symbolp var-exp)
      (macroexp-progn body)
    `(let (,var-exp) ,@body)))

(defun inline--do-letlisteval (listvar &rest body)
  ;; Here's a sample situation:
  ;; (define-inline foo (arg &rest keys)
  ;;   (inline-letevals (arg . keys)
  ;;      <check-keys>))
  ;; I.e. in <check-keys> we need `keys' to contain a list of
  ;; macroexp-copyable-p expressions.
  (let ((bsym (make-symbol "bindings")))
    `(let* ((,bsym ())
            (,listvar (mapcar (lambda (e)
                                (if (macroexp-copyable-p e) e
                                  (let ((v (make-symbol "v")))
                                    (push (list v e) ,bsym)
                                    v)))
                              ,listvar)))
       (macroexp-let* (nreverse ,bsym)
                      ,(macroexp-progn body)))))

(defun inline--dont-letlisteval (_listvar &rest body)
  (macroexp-progn body))

(defun inline--testconst-p (exp)
  (macroexp-let2 macroexp-copyable-p exp exp
    `(or (macroexp-const-p ,exp)
         (eq (car-safe ,exp) 'function))))

(defun inline--alwaysconst-p (_exp)
  t)

(defun inline--getconst-val (exp)
  (macroexp-let2 macroexp-copyable-p exp exp
    `(cond
      ((not ,(inline--testconst-p exp))
       (throw 'inline--just-use inline--form))
      ((consp ,exp) (cadr ,exp))
      (t ,exp))))

(defun inline--alwaysconst-val (exp)
  exp)

(defun inline--error (&rest args)
  `(error ,@args))

(defun inline--warning (&rest _args)
  `(throw 'inline--just-use
          ;; FIXME: This would inf-loop by calling us right back when
          ;; macroexpand-all recurses to expand inline--form.
          ;; (macroexp--warn-and-return (format ,@args)
          ;;                            inline--form)
          inline--form))

(provide 'inline)
;;; inline.el ends here