tildify.el: introduce a `tildify-foreach-region-function' variable
[emacs/old-mirror.git] / lisp / emacs-lisp / byte-run.el
blob1f8b04ec8f05873c70eb0b4b6055077c2821bafc
1 ;;; byte-run.el --- byte-compiler support for inlining -*- lexical-binding: t -*-
3 ;; Copyright (C) 1992, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Jamie Zawinski <jwz@lucid.com>
6 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Keywords: internal
9 ;; Package: emacs
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; interface to selectively inlining functions.
29 ;; This only happens when source-code optimization is turned on.
31 ;;; Code:
33 (defalias 'function-put
34 ;; We don't want people to just use `put' because we can't conveniently
35 ;; hook into `put' to remap old properties to new ones. But for now, there's
36 ;; no such remapping, so we just call `put'.
37 #'(lambda (f prop value) (put f prop value))
38 "Set function F's property PROP to VALUE.
39 The namespace for PROP is shared with symbols.
40 So far, F can only be a symbol, not a lambda expression.")
41 (function-put 'defmacro 'doc-string-elt 3)
42 (function-put 'defmacro 'lisp-indent-function 2)
44 ;; `macro-declaration-function' are both obsolete (as marked at the end of this
45 ;; file) but used in many .elc files.
47 (defvar macro-declaration-function #'macro-declaration-function
48 "Function to process declarations in a macro definition.
49 The function will be called with two args MACRO and DECL.
50 MACRO is the name of the macro being defined.
51 DECL is a list `(declare ...)' containing the declarations.
52 The value the function returns is not used.")
54 (defalias 'macro-declaration-function
55 #'(lambda (macro decl)
56 "Process a declaration found in a macro definition.
57 This is set as the value of the variable `macro-declaration-function'.
58 MACRO is the name of the macro being defined.
59 DECL is a list `(declare ...)' containing the declarations.
60 The return value of this function is not used."
61 ;; We can't use `dolist' or `cadr' yet for bootstrapping reasons.
62 (let (d)
63 ;; Ignore the first element of `decl' (it's always `declare').
64 (while (setq decl (cdr decl))
65 (setq d (car decl))
66 (if (and (consp d)
67 (listp (cdr d))
68 (null (cdr (cdr d))))
69 (cond ((eq (car d) 'indent)
70 (put macro 'lisp-indent-function (car (cdr d))))
71 ((eq (car d) 'debug)
72 (put macro 'edebug-form-spec (car (cdr d))))
73 ((eq (car d) 'doc-string)
74 (put macro 'doc-string-elt (car (cdr d))))
76 (message "Unknown declaration %s" d)))
77 (message "Invalid declaration %s" d))))))
79 ;; We define macro-declaration-alist here because it is needed to
80 ;; handle declarations in macro definitions and this is the first file
81 ;; loaded by loadup.el that uses declarations in macros.
83 ;; Add any new entries to info node `(elisp)Declare Form'.
84 (defvar defun-declarations-alist
85 (list
86 ;; We can only use backquotes inside the lambdas and not for those
87 ;; properties that are used by functions loaded before backquote.el.
88 (list 'advertised-calling-convention
89 #'(lambda (f _args arglist when)
90 (list 'set-advertised-calling-convention
91 (list 'quote f) (list 'quote arglist) (list 'quote when))))
92 (list 'obsolete
93 #'(lambda (f _args new-name when)
94 (list 'make-obsolete
95 (list 'quote f) (list 'quote new-name) (list 'quote when))))
96 (list 'interactive-only
97 #'(lambda (f _args instead)
98 (list 'function-put (list 'quote f)
99 ''interactive-only (list 'quote instead))))
100 ;; FIXME: Merge `pure' and `side-effect-free'.
101 (list 'pure
102 #'(lambda (f _args val)
103 (list 'function-put (list 'quote f)
104 ''pure (list 'quote val)))
105 "If non-nil, the compiler can replace calls with their return value.
106 This may shift errors from run-time to compile-time.")
107 (list 'side-effect-free
108 #'(lambda (f _args val)
109 (list 'function-put (list 'quote f)
110 ''side-effect-free (list 'quote val)))
111 "If non-nil, calls can be ignored if their value is unused.
112 If `error-free', drop calls even if `byte-compile-delete-errors' is nil.")
113 (list 'compiler-macro
114 #'(lambda (f args compiler-function)
115 (if (not (eq (car-safe compiler-function) 'lambda))
116 `(eval-and-compile
117 (function-put ',f 'compiler-macro #',compiler-function))
118 (let ((cfname (intern (concat (symbol-name f) "--anon-cmacro"))))
119 `(progn
120 (eval-and-compile
121 (function-put ',f 'compiler-macro #',cfname))
122 ;; Don't autoload the compiler-macro itself, since the
123 ;; macroexpander will find this file via `f's autoload,
124 ;; if needed.
125 :autoload-end
126 (eval-and-compile
127 (defun ,cfname (,@(cadr compiler-function) ,@args)
128 ,@(cddr compiler-function))))))))
129 (list 'doc-string
130 #'(lambda (f _args pos)
131 (list 'function-put (list 'quote f)
132 ''doc-string-elt (list 'quote pos))))
133 (list 'indent
134 #'(lambda (f _args val)
135 (list 'function-put (list 'quote f)
136 ''lisp-indent-function (list 'quote val)))))
137 "List associating function properties to their macro expansion.
138 Each element of the list takes the form (PROP FUN) where FUN is
139 a function. For each (PROP . VALUES) in a function's declaration,
140 the FUN corresponding to PROP is called with the function name,
141 the function's arglist, and the VALUES and should return the code to use
142 to set this property.
144 This is used by `declare'.")
146 (defvar macro-declarations-alist
147 (cons
148 (list 'debug
149 #'(lambda (name _args spec)
150 (list 'progn :autoload-end
151 (list 'put (list 'quote name)
152 ''edebug-form-spec (list 'quote spec)))))
153 defun-declarations-alist)
154 "List associating properties of macros to their macro expansion.
155 Each element of the list takes the form (PROP FUN) where FUN is a function.
156 For each (PROP . VALUES) in a macro's declaration, the FUN corresponding
157 to PROP is called with the macro name, the macro's arglist, and the VALUES
158 and should return the code to use to set this property.
160 This is used by `declare'.")
162 (defalias 'defmacro
163 (cons
164 'macro
165 #'(lambda (name arglist &optional docstring &rest body)
166 "Define NAME as a macro.
167 When the macro is called, as in (NAME ARGS...),
168 the function (lambda ARGLIST BODY...) is applied to
169 the list ARGS... as it appears in the expression,
170 and the result should be a form to be evaluated instead of the original.
171 DECL is a declaration, optional, of the form (declare DECLS...) where
172 DECLS is a list of elements of the form (PROP . VALUES). These are
173 interpreted according to `macro-declarations-alist'.
174 The return value is undefined.
176 \(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
177 ;; We can't just have `decl' as an &optional argument, because we need
178 ;; to distinguish
179 ;; (defmacro foo (arg) (bar) nil)
180 ;; from
181 ;; (defmacro foo (arg) (bar)).
182 (let ((decls (cond
183 ((eq (car-safe docstring) 'declare)
184 (prog1 (cdr docstring) (setq docstring nil)))
185 ((and (stringp docstring)
186 (eq (car-safe (car body)) 'declare))
187 (prog1 (cdr (car body)) (setq body (cdr body)))))))
188 (if docstring (setq body (cons docstring body))
189 (if (null body) (setq body '(nil))))
190 ;; Can't use backquote because it's not defined yet!
191 (let* ((fun (list 'function (cons 'lambda (cons arglist body))))
192 (def (list 'defalias
193 (list 'quote name)
194 (list 'cons ''macro fun)))
195 (declarations
196 (mapcar
197 #'(lambda (x)
198 (let ((f (cdr (assq (car x) macro-declarations-alist))))
199 (if f (apply (car f) name arglist (cdr x))
200 (message "Warning: Unknown macro property %S in %S"
201 (car x) name))))
202 decls)))
203 (if declarations
204 (cons 'prog1 (cons def declarations))
205 def))))))
207 ;; Now that we defined defmacro we can use it!
208 (defmacro defun (name arglist &optional docstring &rest body)
209 "Define NAME as a function.
210 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
211 See also the function `interactive'.
212 DECL is a declaration, optional, of the form (declare DECLS...) where
213 DECLS is a list of elements of the form (PROP . VALUES). These are
214 interpreted according to `defun-declarations-alist'.
215 The return value is undefined.
217 \(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
218 ;; We can't just have `decl' as an &optional argument, because we need
219 ;; to distinguish
220 ;; (defun foo (arg) (toto) nil)
221 ;; from
222 ;; (defun foo (arg) (toto)).
223 (declare (doc-string 3) (indent 2))
224 (let ((decls (cond
225 ((eq (car-safe docstring) 'declare)
226 (prog1 (cdr docstring) (setq docstring nil)))
227 ((and (stringp docstring)
228 (eq (car-safe (car body)) 'declare))
229 (prog1 (cdr (car body)) (setq body (cdr body)))))))
230 (if docstring (setq body (cons docstring body))
231 (if (null body) (setq body '(nil))))
232 (let ((declarations
233 (mapcar
234 #'(lambda (x)
235 (let ((f (cdr (assq (car x) defun-declarations-alist))))
236 (cond
237 (f (apply (car f) name arglist (cdr x)))
238 ;; Yuck!!
239 ((and (featurep 'cl)
240 (memq (car x) ;C.f. cl-do-proclaim.
241 '(special inline notinline optimize warn)))
242 (push (list 'declare x)
243 (if (stringp docstring)
244 (if (eq (car-safe (cadr body)) 'interactive)
245 (cddr body)
246 (cdr body))
247 (if (eq (car-safe (car body)) 'interactive)
248 (cdr body)
249 body)))
250 nil)
251 (t (message "Warning: Unknown defun property `%S' in %S"
252 (car x) name)))))
253 decls))
254 (def (list 'defalias
255 (list 'quote name)
256 (list 'function
257 (cons 'lambda
258 (cons arglist body))))))
259 (if declarations
260 (cons 'prog1 (cons def declarations))
261 def))))
264 ;; Redefined in byte-optimize.el.
265 ;; This is not documented--it's not clear that we should promote it.
266 (fset 'inline 'progn)
268 ;;; Interface to inline functions.
270 ;; (defmacro proclaim-inline (&rest fns)
271 ;; "Cause the named functions to be open-coded when called from compiled code.
272 ;; They will only be compiled open-coded when byte-compile-optimize is true."
273 ;; (cons 'eval-and-compile
274 ;; (mapcar (lambda (x)
275 ;; (or (memq (get x 'byte-optimizer)
276 ;; '(nil byte-compile-inline-expand))
277 ;; (error
278 ;; "%s already has a byte-optimizer, can't make it inline"
279 ;; x))
280 ;; (list 'put (list 'quote x)
281 ;; ''byte-optimizer ''byte-compile-inline-expand))
282 ;; fns)))
284 ;; (defmacro proclaim-notinline (&rest fns)
285 ;; "Cause the named functions to no longer be open-coded."
286 ;; (cons 'eval-and-compile
287 ;; (mapcar (lambda (x)
288 ;; (if (eq (get x 'byte-optimizer) 'byte-compile-inline-expand)
289 ;; (put x 'byte-optimizer nil))
290 ;; (list 'if (list 'eq (list 'get (list 'quote x) ''byte-optimizer)
291 ;; ''byte-compile-inline-expand)
292 ;; (list 'put x ''byte-optimizer nil)))
293 ;; fns)))
295 (defmacro defsubst (name arglist &rest body)
296 "Define an inline function. The syntax is just like that of `defun'.
297 \(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
298 (declare (debug defun) (doc-string 3))
299 (or (memq (get name 'byte-optimizer)
300 '(nil byte-compile-inline-expand))
301 (error "`%s' is a primitive" name))
302 `(prog1
303 (defun ,name ,arglist ,@body)
304 (eval-and-compile
305 (put ',name 'byte-optimizer 'byte-compile-inline-expand))))
307 (defvar advertised-signature-table (make-hash-table :test 'eq :weakness 'key))
309 (defun set-advertised-calling-convention (function signature _when)
310 "Set the advertised SIGNATURE of FUNCTION.
311 This will allow the byte-compiler to warn the programmer when she uses
312 an obsolete calling convention. WHEN specifies since when the calling
313 convention was modified."
314 (puthash (indirect-function function) signature
315 advertised-signature-table))
317 (defun make-obsolete (obsolete-name current-name &optional when)
318 "Make the byte-compiler warn that function OBSOLETE-NAME is obsolete.
319 OBSOLETE-NAME should be a function name or macro name (a symbol).
321 The warning will say that CURRENT-NAME should be used instead.
322 If CURRENT-NAME is a string, that is the `use instead' message
323 \(it should end with a period, and not start with a capital).
324 WHEN should be a string indicating when the function
325 was first made obsolete, for example a date or a release number."
326 (declare (advertised-calling-convention
327 ;; New code should always provide the `when' argument.
328 (obsolete-name current-name when) "23.1"))
329 (put obsolete-name 'byte-obsolete-info
330 ;; The second entry used to hold the `byte-compile' handler, but
331 ;; is not used any more nowadays.
332 (purecopy (list current-name nil when)))
333 obsolete-name)
335 (defmacro define-obsolete-function-alias (obsolete-name current-name
336 &optional when docstring)
337 "Set OBSOLETE-NAME's function definition to CURRENT-NAME and mark it obsolete.
339 \(define-obsolete-function-alias 'old-fun 'new-fun \"22.1\" \"old-fun's doc.\")
341 is equivalent to the following two lines of code:
343 \(defalias 'old-fun 'new-fun \"old-fun's doc.\")
344 \(make-obsolete 'old-fun 'new-fun \"22.1\")
346 See the docstrings of `defalias' and `make-obsolete' for more details."
347 (declare (doc-string 4)
348 (advertised-calling-convention
349 ;; New code should always provide the `when' argument.
350 (obsolete-name current-name when &optional docstring) "23.1"))
351 `(progn
352 (defalias ,obsolete-name ,current-name ,docstring)
353 (make-obsolete ,obsolete-name ,current-name ,when)))
355 (defun make-obsolete-variable (obsolete-name current-name &optional when access-type)
356 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
357 The warning will say that CURRENT-NAME should be used instead.
358 If CURRENT-NAME is a string, that is the `use instead' message.
359 WHEN should be a string indicating when the variable
360 was first made obsolete, for example a date or a release number.
361 ACCESS-TYPE if non-nil should specify the kind of access that will trigger
362 obsolescence warnings; it can be either `get' or `set'."
363 (declare (advertised-calling-convention
364 ;; New code should always provide the `when' argument.
365 (obsolete-name current-name when &optional access-type) "23.1"))
366 (put obsolete-name 'byte-obsolete-variable
367 (purecopy (list current-name access-type when)))
368 obsolete-name)
371 (defmacro define-obsolete-variable-alias (obsolete-name current-name
372 &optional when docstring)
373 "Make OBSOLETE-NAME a variable alias for CURRENT-NAME and mark it obsolete.
374 This uses `defvaralias' and `make-obsolete-variable' (which see).
375 See the Info node `(elisp)Variable Aliases' for more details.
377 If CURRENT-NAME is a defcustom (more generally, any variable
378 where OBSOLETE-NAME may be set, e.g. in an init file, before the
379 alias is defined), then the define-obsolete-variable-alias
380 statement should be evaluated before the defcustom, if user
381 customizations are to be respected. The simplest way to achieve
382 this is to place the alias statement before the defcustom (this
383 is not necessary for aliases that are autoloaded, or in files
384 dumped with Emacs). This is so that any user customizations are
385 applied before the defcustom tries to initialize the
386 variable (this is due to the way `defvaralias' works).
388 For the benefit of `custom-set-variables', if OBSOLETE-NAME has
389 any of the following properties, they are copied to
390 CURRENT-NAME, if it does not already have them:
391 'saved-value, 'saved-variable-comment."
392 (declare (doc-string 4)
393 (advertised-calling-convention
394 ;; New code should always provide the `when' argument.
395 (obsolete-name current-name when &optional docstring) "23.1"))
396 `(progn
397 (defvaralias ,obsolete-name ,current-name ,docstring)
398 ;; See Bug#4706.
399 (dolist (prop '(saved-value saved-variable-comment))
400 (and (get ,obsolete-name prop)
401 (null (get ,current-name prop))
402 (put ,current-name prop (get ,obsolete-name prop))))
403 (make-obsolete-variable ,obsolete-name ,current-name ,when)))
405 ;; FIXME This is only defined in this file because the variable- and
406 ;; function- versions are too. Unlike those two, this one is not used
407 ;; by the byte-compiler (would be nice if it could warn about obsolete
408 ;; faces, but it doesn't really do anything special with faces).
409 ;; It only really affects M-x describe-face output.
410 (defmacro define-obsolete-face-alias (obsolete-face current-face when)
411 "Make OBSOLETE-FACE a face alias for CURRENT-FACE and mark it obsolete.
412 The string WHEN gives the Emacs version where OBSOLETE-FACE became
413 obsolete."
414 `(progn
415 (put ,obsolete-face 'face-alias ,current-face)
416 ;; Used by M-x describe-face.
417 (put ,obsolete-face 'obsolete-face (or (purecopy ,when) t))))
419 (defmacro dont-compile (&rest body)
420 "Like `progn', but the body always runs interpreted (not compiled).
421 If you think you need this, you're probably making a mistake somewhere."
422 (declare (debug t) (indent 0) (obsolete nil "24.4"))
423 (list 'eval (list 'quote (if (cdr body) (cons 'progn body) (car body)))))
426 ;; interface to evaluating things at compile time and/or load time
427 ;; these macro must come after any uses of them in this file, as their
428 ;; definition in the file overrides the magic definitions on the
429 ;; byte-compile-macro-environment.
431 (defmacro eval-when-compile (&rest body)
432 "Like `progn', but evaluates the body at compile time if you're compiling.
433 Thus, the result of the body appears to the compiler as a quoted
434 constant. In interpreted code, this is entirely equivalent to
435 `progn', except that the value of the expression may be (but is
436 not necessarily) computed at load time if eager macro expansion
437 is enabled."
438 (declare (debug (&rest def-form)) (indent 0))
439 (list 'quote (eval (cons 'progn body) lexical-binding)))
441 (defmacro eval-and-compile (&rest body)
442 "Like `progn', but evaluates the body at compile time and at
443 load time. In interpreted code, this is entirely equivalent to
444 `progn', except that the value of the expression may be (but is
445 not necessarily) computed at load time if eager macro expansion
446 is enabled."
447 (declare (debug t) (indent 0))
448 ;; When the byte-compiler expands code, this macro is not used, so we're
449 ;; either about to run `body' (plain interpretation) or we're doing eager
450 ;; macroexpansion.
451 (list 'quote (eval (cons 'progn body) lexical-binding)))
453 (defun with-no-warnings (&rest body)
454 "Like `progn', but prevents compiler warnings in the body."
455 (declare (indent 0))
456 ;; The implementation for the interpreter is basically trivial.
457 (car (last body)))
460 ;; I nuked this because it's not a good idea for users to think of using it.
461 ;; These options are a matter of installation preference, and have nothing to
462 ;; with particular source files; it's a mistake to suggest to users
463 ;; they should associate these with particular source files.
464 ;; There is hardly any reason to change these parameters, anyway.
465 ;; --rms.
467 ;; (put 'byte-compiler-options 'lisp-indent-function 0)
468 ;; (defmacro byte-compiler-options (&rest args)
469 ;; "Set some compilation-parameters for this file. This will affect only the
470 ;; file in which it appears; this does nothing when evaluated, and when loaded
471 ;; from a .el file.
473 ;; Each argument to this macro must be a list of a key and a value.
475 ;; Keys: Values: Corresponding variable:
477 ;; verbose t, nil byte-compile-verbose
478 ;; optimize t, nil, source, byte byte-compile-optimize
479 ;; warnings list of warnings byte-compile-warnings
480 ;; Valid elements: (callargs redefine free-vars unresolved)
481 ;; file-format emacs18, emacs19 byte-compile-compatibility
483 ;; For example, this might appear at the top of a source file:
485 ;; (byte-compiler-options
486 ;; (optimize t)
487 ;; (warnings (- free-vars)) ; Don't warn about free variables
488 ;; (file-format emacs19))"
489 ;; nil)
491 (make-obsolete-variable 'macro-declaration-function
492 'macro-declarations-alist "24.3")
493 (make-obsolete 'macro-declaration-function
494 'macro-declarations-alist "24.3")
496 ;;; byte-run.el ends here