unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob 59d4f997b079968536936ce231914da5f4296290 19479 bytes (raw)
name: lisp/emacs-lisp/inline.el 	 # note: path name is non-authoritative(*)

  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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
 
;;; inline.el --- Define functions by their inliner  -*- lexical-binding:t; -*-

;; Copyright (C) 2014-2023 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 <https://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 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 (backquote-form)))
  (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 body)))
  ;; BEWARE: if we're here it's presumably via macro-expansion of
  ;; inline-letevals, so signal the error in terms of the user's code.
  (error "inline-letevals can only be used within define-inline"))
(defmacro inline--letlisteval (_list &rest _body)
  (declare (indent 1) (debug (sexp body)))
  ;; BEWARE: if we're here it's presumably via macro-expansion of
  ;; inline-letevals, so signal the error in terms of the user's code.
  (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 body)))
  (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)
  "Define an inline function NAME with arguments ARGS and body in BODY.

This is like `defmacro', but has several advantages.
See Info node `(elisp)Defining Functions' for more details."
  ;; 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 (null exp)
           args
         `(apply #',(car args) ,@(cdr args) ,(inline--dont-quote exp)))))
    (_ 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 (gensym "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))

(defun inline-extract-arglist (fxn-name)
  "Construct arglist based on FXN docstring if provided in help format."
  (let* ((s (or (documentation fxn-name t) ""))
	 (found (string-match "\n(fn \\([^\)]*\\))$" s))
	 (n (length "\n\(fn ")))
    (if (not found)
	;; punt
	'(&rest args)
      (let ((arglist-string
	     (format "\(%s"
		     (downcase (substring s (+ found n))))))
	(with-temp-buffer
	  (insert arglist-string)
	  (goto-char (point-min))
	  (read (current-buffer)))))))


(defun inline-application-form (fxn args &optional rands)
  "Construct an application form for function FXN with argument list ARGS and operands RANDS."
  (let ((ls args)
	(required 0)
	params opt restp)
    (while ls
      (pcase ls
	(`(&rest ,param)
	 (push param params)
	 (setq restp t)
	 (setq ls nil))
	(`(&rest . ,_ignored)
	 (error "argument list: %s: malformed &rest parameter %S" fxn args))
	(`(&optional . ,_ignored)
	 (when opt
	   (error "argument list: %s: multiple &optional markers %S"
		  fxn args))
	 (pop ls)
	 (setq opt 0))
	(`(,param . ,_ignored)
	 (push param params)
	 (pop ls)
	 (if opt
	     (setq opt (1+ opt))
	   (setq required (1+ required))))
	(_
	 (error "malformed argument list: %s: %S" fxn args))))
    (setq params (nreverse params))
    (unless (or (subrp fxn)
                (byte-code-function-p fxn)
                (compiled-function-p fxn)
                (symbolp fxn)
                (and (consp fxn)
                     (eq (car fxn) 'function)))
      (setq fxn (list 'function fxn)))
    (unless opt
      (setq opt 0))
    (if rands
        (progn
          ;; (display-warning '(inline)
          ;;                  (format "Source operands: %S %S" fxn rands))
          `(,fxn ,@rands))
      (if restp
	  `(apply ,(if (symbolp fxn) `#',fxn fxn) ,@params)
        `(,fxn ,@params)))))

;; Derived from inline.el
(defun inline--testconst-exp-p (exp)
  (or (macroexp-const-p exp)
      (eq (car-safe exp) 'function)))

;;;###autoload
(defmacro define-inline-pure-subr (name args &optional new-name)
  "Define NEW-NAME to inline the subr currently bound to NAME.
The function must have the signature specified by ARGS.
This inlining enables compile-time evaluation during macroexpansion
rather than during the byte-compiler's optimization phase.
NEW-NAME defaults to NAME."
  (declare (indent defun) (debug defun) (doc-string 3))
  ;; (message "Redefining as inline %s %S %s" name args new-name)
  (when (and new-name (not (eq new-name name)))
    (setplist new-name (seq-copy (symbol-plist name))))
  (unless new-name
    (setq new-name name))
  (let ((doc (documentation name t))
	(fxn (symbol-function name))
        (fxn-sym (intern (format "inline-%s" new-name)))
        (cm-name (intern (format "%s--inliner" new-name)))
	app-form)
    (while (symbolp fxn)
      (setq fxn (symbol-function fxn)))
    (unless (or (subrp fxn)
                (byte-code-function-p fxn)
                (compiled-function-p fxn)
                (and (consp fxn)
                     (or (eq (car fxn) 'function)
                         (eq (car fxn) 'lambda))))
      (setq fxn (list 'function fxn)))
    (function-put new-name 'compiler-macro nil) ; see define-inline
    (setq app-form (inline-application-form fxn-sym args))
    (fset fxn-sym fxn)
    `(progn
       (fset ',fxn-sym ,fxn)
       (defun ,new-name ,args
         ,doc
         (fset ',fxn-sym ,fxn)
         ,(if (eq new-name name)
              `(progn
                 (fset ',new-name ,fxn)
                 ;; see define-inline
                 (function-put ',new-name 'compiler-macro ',cm-name))
            ;; first pass ensure definition of underlying function
            ;; then redefine to bypass this trampoline
            `(,(if (memq (get name 'byte-optimizer)
		         '(nil byte-compile-inline-expand))
		   'defsubst
	         'defun)
	      ,new-name ,args ,doc
              (declare (compiler-macro ,cm-name))
	      ,app-form))
         ,app-form)
       (eval-and-compile
         (defun ,cm-name ,(cons 'inline--form args)
           (fset ',fxn-sym ,fxn)
           (defun ,cm-name ,(cons 'inline--form args)
	     (let* ((rands (mapcar #'macroexpand-all (cdr inline--form)))
		    (expander-app-form
                     `(,',fxn-sym ,@rands)))
	       (if (seq-every-p #'inline--testconst-exp-p rands)
                   (progn
		     (let ((r (eval expander-app-form)))
		       (unless (macroexp-const-p r)
		         (setq r `(quote ,r)))
		       r))
	         inline--form)))
           ,(inline-application-form cm-name (cons 'inline--form args))))
       (function-put ',new-name 'compiler-macro ',cm-name))))

;;;###autoload
(defvar inline--inlined-primitives nil
  "Association list of pure functions and their argument lists for inlining.")

(defvar inline--defining-inlines nil)
(defun inline-all-pure-functions ()
  "Create macro-expansion evaluating versions of known pure functions.
These inline versions reduce constant arguments at macro-expansion time without
involvement of the byte-compiler."
  (let ((inline--defining-inlines t))
    (setq inline--inlined-primitives
          (let (purefuncs)
            (mapatoms (lambda (x)
		        (and (fboundp x) (get x 'pure)
		             (push `(,x . ,x) purefuncs))))
            ;; these are not truly pure
            ;; make inline-* variants available for explicit use
            (push '(format . inline-format) purefuncs)
            (push '(intern . inline-intern) purefuncs)
            (setq purefuncs (nreverse purefuncs))
            (mapcar (lambda (x)
	              `(,(car x) ,(cdr x) . ,(inline-extract-arglist (car x))))
	            purefuncs))))

    (mapc (lambda (pr)
	    (eval `(define-inline-pure-subr ,(car pr) ,(cddr pr) ,(cadr pr))))
          inline--inlined-primitives))

(when (and (or (featurep 'bytecomp) (featurep 'byte-opt))
           (fboundp 'function-documentation)
           (not inline--defining-inlines))
  (inline-all-pure-functions))

;; (defmacro define-inline-pure (name args &rest body)
;;   "Define NAME as inlined pure function with signature ARGS.
;; BODY will be evaluated during macroexpansion if given constant arguments."
;;   (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))
;; 	expanded-ct-body ct-fxn app-form)
;;     (function-put name 'compiler-macro nil) ; see define-inline
;;     (setq app-form (inline-application-form fxn args))
;;     (setq expanded-ct-body
;; 	  `(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))))
;;     ;; construct a function that should not have
;;     ;; circular dependency on the function symbol
;;     ;; being inlined
;;     (setq ct-fxn
;; 	  (let ((x (cl-gensym "x-"))
;; 		(expanded-body
;; 		 `(catch 'inline--just-use
;; 		    ,expanded-ct-body)))
;; 	    (byte-compile
;; 	     `(lambda (,args)
;; 		(cl-labels ((,name ,args ,@expanded-ct-body))
;; 		  ,app-form)))))
;;     `(progn
;;        (defun ,name ,args
;; 	 ,@doc
;;          (declare (compiler-macro ,cm-name) ,@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)))
;;        (eval-and-compile
;;          (defun ,cm-name ,(cons 'inline--form args)
;; 	   (let* ((rands (mapcar #'macroexpand-all (cdr inline--form)))
;; 		  (expander-app-form `(,,fxn ,@rands)))
;; 	     (if (seq-every-p #'inline--testconst-exp-p rands)
;; 		 (let ((r
;; 			;; (eval expander-app-form)))
;; 			(apply ct-fxn rands)))
;; 		   (unless (macroexp-const-p r)
;; 		     (setq r `(quote ,r)))
;; 		   r)
;; 	       ,@expanded-ct-body)))))))

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

debug log:

solving f31850dafad ...
found f31850dafad in https://yhetil.org/emacs-devel/CAM=F=bAEpL9-9Yvx6Eh8JzG9YdZj4OvoenAm76Or3+jtJjfjag@mail.gmail.com/
found f761fda29a8 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 f761fda29a87c1b58fa62dfb58d055a006812777	lisp/emacs-lisp/inline.el

applying [1/1] https://yhetil.org/emacs-devel/CAM=F=bAEpL9-9Yvx6Eh8JzG9YdZj4OvoenAm76Or3+jtJjfjag@mail.gmail.com/
diff --git a/lisp/emacs-lisp/inline.el b/lisp/emacs-lisp/inline.el
index f761fda29a8..f31850dafad 100644

Checking patch lisp/emacs-lisp/inline.el...
Applied patch lisp/emacs-lisp/inline.el cleanly.

index at:
100644 59d4f997b079968536936ce231914da5f4296290	lisp/emacs-lisp/inline.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).