image-dired.el: use with-current-buffer
[emacs.git] / lisp / emacs-lisp / byte-run.el
blob8f0999b2f8016cc6bf917dff165af5d215d7e30c
1 ;;; byte-run.el --- byte-compiler support for inlining -*- lexical-binding: t -*-
3 ;; Copyright (C) 1992, 2001-2013 Free Software Foundation, Inc.
5 ;; Author: Jamie Zawinski <jwz@lucid.com>
6 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
7 ;; Maintainer: FSF
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 ;; `macro-declaration-function' are both obsolete (as marked at the end of this
34 ;; file) but used in many .elc files.
36 (defvar macro-declaration-function #'macro-declaration-function
37 "Function to process declarations in a macro definition.
38 The function will be called with two args MACRO and DECL.
39 MACRO is the name of the macro being defined.
40 DECL is a list `(declare ...)' containing the declarations.
41 The value the function returns is not used.")
43 (defalias 'macro-declaration-function
44 #'(lambda (macro decl)
45 "Process a declaration found in a macro definition.
46 This is set as the value of the variable `macro-declaration-function'.
47 MACRO is the name of the macro being defined.
48 DECL is a list `(declare ...)' containing the declarations.
49 The return value of this function is not used."
50 ;; We can't use `dolist' or `cadr' yet for bootstrapping reasons.
51 (let (d)
52 ;; Ignore the first element of `decl' (it's always `declare').
53 (while (setq decl (cdr decl))
54 (setq d (car decl))
55 (if (and (consp d)
56 (listp (cdr d))
57 (null (cdr (cdr d))))
58 (cond ((eq (car d) 'indent)
59 (put macro 'lisp-indent-function (car (cdr d))))
60 ((eq (car d) 'debug)
61 (put macro 'edebug-form-spec (car (cdr d))))
62 ((eq (car d) 'doc-string)
63 (put macro 'doc-string-elt (car (cdr d))))
65 (message "Unknown declaration %s" d)))
66 (message "Invalid declaration %s" d))))))
68 ;; We define macro-declaration-alist here because it is needed to
69 ;; handle declarations in macro definitions and this is the first file
70 ;; loaded by loadup.el that uses declarations in macros.
72 (defvar defun-declarations-alist
73 (list
74 ;; We can only use backquotes inside the lambdas and not for those
75 ;; properties that are used by functions loaded before backquote.el.
76 (list 'advertised-calling-convention
77 #'(lambda (f _args arglist when)
78 (list 'set-advertised-calling-convention
79 (list 'quote f) (list 'quote arglist) (list 'quote when))))
80 (list 'obsolete
81 #'(lambda (f _args new-name when)
82 (list 'make-obsolete
83 (list 'quote f) (list 'quote new-name) (list 'quote when))))
84 (list 'compiler-macro
85 #'(lambda (f args compiler-function)
86 ;; FIXME: Make it possible to just reuse `args'.
87 `(eval-and-compile
88 (put ',f 'compiler-macro
89 ,(if (eq (car-safe compiler-function) 'lambda)
90 `(lambda ,(append (cadr compiler-function) args)
91 ,@(cddr compiler-function))
92 `#',compiler-function)))))
93 (list 'doc-string
94 #'(lambda (f _args pos)
95 (list 'put (list 'quote f) ''doc-string-elt (list 'quote pos))))
96 (list 'indent
97 #'(lambda (f _args val)
98 (list 'put (list 'quote f)
99 ''lisp-indent-function (list 'quote val)))))
100 "List associating function properties to their macro expansion.
101 Each element of the list takes the form (PROP FUN) where FUN is
102 a function. For each (PROP . VALUES) in a function's declaration,
103 the FUN corresponding to PROP is called with the function name,
104 the function's arglist, and the VALUES and should return the code to use
105 to set this property.")
107 (defvar macro-declarations-alist
108 (cons
109 (list 'debug
110 #'(lambda (name _args spec)
111 (list 'progn :autoload-end
112 (list 'put (list 'quote name)
113 ''edebug-form-spec (list 'quote spec)))))
114 defun-declarations-alist)
115 "List associating properties of macros to their macro expansion.
116 Each element of the list takes the form (PROP FUN) where FUN is
117 a function. For each (PROP . VALUES) in a macro's declaration,
118 the FUN corresponding to PROP is called with the function name
119 and the VALUES and should return the code to use to set this property.")
121 (put 'defmacro 'doc-string-elt 3)
122 (defalias 'defmacro
123 (cons
124 'macro
125 #'(lambda (name arglist &optional docstring &rest body)
126 "Define NAME as a macro.
127 When the macro is called, as in (NAME ARGS...),
128 the function (lambda ARGLIST BODY...) is applied to
129 the list ARGS... as it appears in the expression,
130 and the result should be a form to be evaluated instead of the original.
131 DECL is a declaration, optional, of the form (declare DECLS...) where
132 DECLS is a list of elements of the form (PROP . VALUES). These are
133 interpreted according to `macro-declarations-alist'.
134 The return value is undefined.
136 \(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
137 ;; We can't just have `decl' as an &optional argument, because we need
138 ;; to distinguish
139 ;; (defmacro foo (arg) (bar) nil)
140 ;; from
141 ;; (defmacro foo (arg) (bar)).
142 (let ((decls (cond
143 ((eq (car-safe docstring) 'declare)
144 (prog1 (cdr docstring) (setq docstring nil)))
145 ((and (stringp docstring)
146 (eq (car-safe (car body)) 'declare))
147 (prog1 (cdr (car body)) (setq body (cdr body)))))))
148 (if docstring (setq body (cons docstring body))
149 (if (null body) (setq body '(nil))))
150 ;; Can't use backquote because it's not defined yet!
151 (let* ((fun (list 'function (cons 'lambda (cons arglist body))))
152 (def (list 'defalias
153 (list 'quote name)
154 (list 'cons ''macro fun)))
155 (declarations
156 (mapcar
157 #'(lambda (x)
158 (let ((f (cdr (assq (car x) macro-declarations-alist))))
159 (if f (apply (car f) name arglist (cdr x))
160 (message "Warning: Unknown macro property %S in %S"
161 (car x) name))))
162 decls)))
163 (if declarations
164 (cons 'prog1 (cons def declarations))
165 def))))))
167 ;; Now that we defined defmacro we can use it!
168 (defmacro defun (name arglist &optional docstring &rest body)
169 "Define NAME as a function.
170 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
171 See also the function `interactive'.
172 DECL is a declaration, optional, of the form (declare DECLS...) where
173 DECLS is a list of elements of the form (PROP . VALUES). These are
174 interpreted according to `defun-declarations-alist'.
175 The return value is undefined.
177 \(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
178 ;; We can't just have `decl' as an &optional argument, because we need
179 ;; to distinguish
180 ;; (defun foo (arg) (toto) nil)
181 ;; from
182 ;; (defun foo (arg) (toto)).
183 (declare (doc-string 3))
184 (let ((decls (cond
185 ((eq (car-safe docstring) 'declare)
186 (prog1 (cdr docstring) (setq docstring nil)))
187 ((and (stringp docstring)
188 (eq (car-safe (car body)) 'declare))
189 (prog1 (cdr (car body)) (setq body (cdr body)))))))
190 (if docstring (setq body (cons docstring body))
191 (if (null body) (setq body '(nil))))
192 (let ((declarations
193 (mapcar
194 #'(lambda (x)
195 (let ((f (cdr (assq (car x) defun-declarations-alist))))
196 (cond
197 (f (apply (car f) name arglist (cdr x)))
198 ;; Yuck!!
199 ((and (featurep 'cl)
200 (memq (car x) ;C.f. cl-do-proclaim.
201 '(special inline notinline optimize warn)))
202 (push (list 'declare x)
203 (if (stringp docstring)
204 (if (eq (car-safe (cadr body)) 'interactive)
205 (cddr body)
206 (cdr body))
207 (if (eq (car-safe (car body)) 'interactive)
208 (cdr body)
209 body)))
210 nil)
211 (t (message "Warning: Unknown defun property `%S' in %S"
212 (car x) name)))))
213 decls))
214 (def (list 'defalias
215 (list 'quote name)
216 (list 'function
217 (cons 'lambda
218 (cons arglist body))))))
219 (if declarations
220 (cons 'prog1 (cons def declarations))
221 def))))
223 ;; Redefined in byte-optimize.el.
224 ;; This is not documented--it's not clear that we should promote it.
225 (fset 'inline 'progn)
227 ;;; Interface to inline functions.
229 ;; (defmacro proclaim-inline (&rest fns)
230 ;; "Cause the named functions to be open-coded when called from compiled code.
231 ;; They will only be compiled open-coded when byte-compile-optimize is true."
232 ;; (cons 'eval-and-compile
233 ;; (mapcar (lambda (x)
234 ;; (or (memq (get x 'byte-optimizer)
235 ;; '(nil byte-compile-inline-expand))
236 ;; (error
237 ;; "%s already has a byte-optimizer, can't make it inline"
238 ;; x))
239 ;; (list 'put (list 'quote x)
240 ;; ''byte-optimizer ''byte-compile-inline-expand))
241 ;; fns)))
243 ;; (defmacro proclaim-notinline (&rest fns)
244 ;; "Cause the named functions to no longer be open-coded."
245 ;; (cons 'eval-and-compile
246 ;; (mapcar (lambda (x)
247 ;; (if (eq (get x 'byte-optimizer) 'byte-compile-inline-expand)
248 ;; (put x 'byte-optimizer nil))
249 ;; (list 'if (list 'eq (list 'get (list 'quote x) ''byte-optimizer)
250 ;; ''byte-compile-inline-expand)
251 ;; (list 'put x ''byte-optimizer nil)))
252 ;; fns)))
254 (defmacro defsubst (name arglist &rest body)
255 "Define an inline function. The syntax is just like that of `defun'.
256 \(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
257 (declare (debug defun) (doc-string 3))
258 (or (memq (get name 'byte-optimizer)
259 '(nil byte-compile-inline-expand))
260 (error "`%s' is a primitive" name))
261 `(prog1
262 (defun ,name ,arglist ,@body)
263 (eval-and-compile
264 (put ',name 'byte-optimizer 'byte-compile-inline-expand))))
266 (defvar advertised-signature-table (make-hash-table :test 'eq :weakness 'key))
268 (defun set-advertised-calling-convention (function signature _when)
269 "Set the advertised SIGNATURE of FUNCTION.
270 This will allow the byte-compiler to warn the programmer when she uses
271 an obsolete calling convention. WHEN specifies since when the calling
272 convention was modified."
273 (puthash (indirect-function function) signature
274 advertised-signature-table))
276 (defun make-obsolete (obsolete-name current-name &optional when)
277 "Make the byte-compiler warn that function OBSOLETE-NAME is obsolete.
278 OBSOLETE-NAME should be a function name or macro name (a symbol).
280 The warning will say that CURRENT-NAME should be used instead.
281 If CURRENT-NAME is a string, that is the `use instead' message
282 \(it should end with a period, and not start with a capital).
283 WHEN should be a string indicating when the function
284 was first made obsolete, for example a date or a release number."
285 (declare (advertised-calling-convention
286 ;; New code should always provide the `when' argument.
287 (obsolete-name current-name when) "23.1"))
288 (interactive "aMake function obsolete: \nxObsoletion replacement: ")
289 (put obsolete-name 'byte-obsolete-info
290 ;; The second entry used to hold the `byte-compile' handler, but
291 ;; is not used any more nowadays.
292 (purecopy (list current-name nil when)))
293 obsolete-name)
295 (defmacro define-obsolete-function-alias (obsolete-name current-name
296 &optional when docstring)
297 "Set OBSOLETE-NAME's function definition to CURRENT-NAME and mark it obsolete.
299 \(define-obsolete-function-alias 'old-fun 'new-fun \"22.1\" \"old-fun's doc.\")
301 is equivalent to the following two lines of code:
303 \(defalias 'old-fun 'new-fun \"old-fun's doc.\")
304 \(make-obsolete 'old-fun 'new-fun \"22.1\")
306 See the docstrings of `defalias' and `make-obsolete' for more details."
307 (declare (doc-string 4)
308 (advertised-calling-convention
309 ;; New code should always provide the `when' argument.
310 (obsolete-name current-name when &optional docstring) "23.1"))
311 `(progn
312 (defalias ,obsolete-name ,current-name ,docstring)
313 (make-obsolete ,obsolete-name ,current-name ,when)))
315 (defun make-obsolete-variable (obsolete-name current-name &optional when access-type)
316 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
317 The warning will say that CURRENT-NAME should be used instead.
318 If CURRENT-NAME is a string, that is the `use instead' message.
319 WHEN should be a string indicating when the variable
320 was first made obsolete, for example a date or a release number.
321 ACCESS-TYPE if non-nil should specify the kind of access that will trigger
322 obsolescence warnings; it can be either `get' or `set'."
323 (declare (advertised-calling-convention
324 ;; New code should always provide the `when' argument.
325 (obsolete-name current-name when &optional access-type) "23.1"))
326 (put obsolete-name 'byte-obsolete-variable
327 (purecopy (list current-name access-type when)))
328 obsolete-name)
331 (defmacro define-obsolete-variable-alias (obsolete-name current-name
332 &optional when docstring)
333 "Make OBSOLETE-NAME a variable alias for CURRENT-NAME and mark it obsolete.
334 This uses `defvaralias' and `make-obsolete-variable' (which see).
335 See the Info node `(elisp)Variable Aliases' for more details.
337 If CURRENT-NAME is a defcustom (more generally, any variable
338 where OBSOLETE-NAME may be set, e.g. in an init file, before the
339 alias is defined), then the define-obsolete-variable-alias
340 statement should be evaluated before the defcustom, if user
341 customizations are to be respected. The simplest way to achieve
342 this is to place the alias statement before the defcustom (this
343 is not necessary for aliases that are autoloaded, or in files
344 dumped with Emacs). This is so that any user customizations are
345 applied before the defcustom tries to initialize the
346 variable (this is due to the way `defvaralias' works).
348 For the benefit of `custom-set-variables', if OBSOLETE-NAME has
349 any of the following properties, they are copied to
350 CURRENT-NAME, if it does not already have them:
351 'saved-value, 'saved-variable-comment."
352 (declare (doc-string 4)
353 (advertised-calling-convention
354 ;; New code should always provide the `when' argument.
355 (obsolete-name current-name when &optional docstring) "23.1"))
356 `(progn
357 (defvaralias ,obsolete-name ,current-name ,docstring)
358 ;; See Bug#4706.
359 (dolist (prop '(saved-value saved-variable-comment))
360 (and (get ,obsolete-name prop)
361 (null (get ,current-name prop))
362 (put ,current-name prop (get ,obsolete-name prop))))
363 (make-obsolete-variable ,obsolete-name ,current-name ,when)))
365 ;; FIXME This is only defined in this file because the variable- and
366 ;; function- versions are too. Unlike those two, this one is not used
367 ;; by the byte-compiler (would be nice if it could warn about obsolete
368 ;; faces, but it doesn't really do anything special with faces).
369 ;; It only really affects M-x describe-face output.
370 (defmacro define-obsolete-face-alias (obsolete-face current-face when)
371 "Make OBSOLETE-FACE a face alias for CURRENT-FACE and mark it obsolete.
372 The string WHEN gives the Emacs version where OBSOLETE-FACE became
373 obsolete."
374 `(progn
375 (put ,obsolete-face 'face-alias ,current-face)
376 ;; Used by M-x describe-face.
377 (put ,obsolete-face 'obsolete-face (or (purecopy ,when) t))))
379 (defmacro dont-compile (&rest body)
380 "Like `progn', but the body always runs interpreted (not compiled).
381 If you think you need this, you're probably making a mistake somewhere."
382 (declare (debug t) (indent 0) (obsolete nil "24.4"))
383 (list 'eval (list 'quote (if (cdr body) (cons 'progn body) (car body)))))
386 ;; interface to evaluating things at compile time and/or load time
387 ;; these macro must come after any uses of them in this file, as their
388 ;; definition in the file overrides the magic definitions on the
389 ;; byte-compile-macro-environment.
391 (defmacro eval-when-compile (&rest body)
392 "Like `progn', but evaluates the body at compile time if you're compiling.
393 Thus, the result of the body appears to the compiler as a quoted constant.
394 In interpreted code, this is entirely equivalent to `progn'."
395 (declare (debug t) (indent 0))
396 (list 'quote (eval (cons 'progn body) lexical-binding)))
398 (defmacro eval-and-compile (&rest body)
399 "Like `progn', but evaluates the body at compile time and at load time."
400 (declare (debug t) (indent 0))
401 ;; When the byte-compiler expands code, this macro is not used, so we're
402 ;; either about to run `body' (plain interpretation) or we're doing eager
403 ;; macroexpansion.
404 (list 'quote (eval (cons 'progn body) lexical-binding)))
406 (defun with-no-warnings (&rest body)
407 "Like `progn', but prevents compiler warnings in the body."
408 (declare (indent 0))
409 ;; The implementation for the interpreter is basically trivial.
410 (car (last body)))
413 ;; I nuked this because it's not a good idea for users to think of using it.
414 ;; These options are a matter of installation preference, and have nothing to
415 ;; with particular source files; it's a mistake to suggest to users
416 ;; they should associate these with particular source files.
417 ;; There is hardly any reason to change these parameters, anyway.
418 ;; --rms.
420 ;; (put 'byte-compiler-options 'lisp-indent-function 0)
421 ;; (defmacro byte-compiler-options (&rest args)
422 ;; "Set some compilation-parameters for this file. This will affect only the
423 ;; file in which it appears; this does nothing when evaluated, and when loaded
424 ;; from a .el file.
426 ;; Each argument to this macro must be a list of a key and a value.
428 ;; Keys: Values: Corresponding variable:
430 ;; verbose t, nil byte-compile-verbose
431 ;; optimize t, nil, source, byte byte-compile-optimize
432 ;; warnings list of warnings byte-compile-warnings
433 ;; Valid elements: (callargs redefine free-vars unresolved)
434 ;; file-format emacs18, emacs19 byte-compile-compatibility
436 ;; For example, this might appear at the top of a source file:
438 ;; (byte-compiler-options
439 ;; (optimize t)
440 ;; (warnings (- free-vars)) ; Don't warn about free variables
441 ;; (file-format emacs19))"
442 ;; nil)
444 (make-obsolete-variable 'macro-declaration-function
445 'macro-declarations-alist "24.3")
446 (make-obsolete 'macro-declaration-function
447 'macro-declarations-alist "24.3")
449 ;;; byte-run.el ends here