2014-04-22 Daniel Colascione <dancol@dancol.org>
[bpt/emacs.git] / lisp / emacs-lisp / byte-run.el
CommitLineData
500fcedc 1;;; byte-run.el --- byte-compiler support for inlining -*- lexical-binding: t -*-
5e046f6d 2
ba318903 3;; Copyright (C) 1992, 2001-2014 Free Software Foundation, Inc.
5e046f6d
JB
4
5;; Author: Jamie Zawinski <jwz@lucid.com>
6;; Hallvard Furuseth <hbf@ulrik.uio.no>
34dc21db 7;; Maintainer: emacs-devel@gnu.org
5e046f6d 8;; Keywords: internal
bd78fa1d 9;; Package: emacs
5e046f6d
JB
10
11;; This file is part of GNU Emacs.
12
d6cba7ae 13;; GNU Emacs is free software: you can redistribute it and/or modify
5e046f6d 14;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
5e046f6d
JB
17
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.
22
23;; You should have received a copy of the GNU General Public License
d6cba7ae 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
5e046f6d
JB
25
26;;; Commentary:
27
28;; interface to selectively inlining functions.
29;; This only happens when source-code optimization is turned on.
30
31;;; Code:
32
1b0f10d2
DC
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.
39The namespace for PROP is shared with symbols.
40So 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)
43
500fcedc
SM
44;; `macro-declaration-function' are both obsolete (as marked at the end of this
45;; file) but used in many .elc files.
623374a5 46
61b108cc
SM
47(defvar macro-declaration-function #'macro-declaration-function
48 "Function to process declarations in a macro definition.
49The function will be called with two args MACRO and DECL.
50MACRO is the name of the macro being defined.
51DECL is a list `(declare ...)' containing the declarations.
52The value the function returns is not used.")
53
54(defalias 'macro-declaration-function
55 #'(lambda (macro decl)
56 "Process a declaration found in a macro definition.
623374a5
LK
57This is set as the value of the variable `macro-declaration-function'.
58MACRO is the name of the macro being defined.
59DECL is a list `(declare ...)' containing the declarations.
60The return value of this function is not used."
61b108cc
SM
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))))
75 (t
76 (message "Unknown declaration %s" d)))
77 (message "Invalid declaration %s" d))))))
78
500fcedc
SM
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.
82
5076d275 83;; Add any new entries to info node `(elisp)Declare Form'.
500fcedc 84(defvar defun-declarations-alist
500fcedc 85 (list
d9857e53
SM
86 ;; We can only use backquotes inside the lambdas and not for those
87 ;; properties that are used by functions loaded before backquote.el.
500fcedc 88 (list 'advertised-calling-convention
d9857e53 89 #'(lambda (f _args arglist when)
500fcedc
SM
90 (list 'set-advertised-calling-convention
91 (list 'quote f) (list 'quote arglist) (list 'quote when))))
d9857e53
SM
92 (list 'obsolete
93 #'(lambda (f _args new-name when)
e7f7cb1a
SM
94 (list 'make-obsolete
95 (list 'quote f) (list 'quote new-name) (list 'quote when))))
5076d275
GM
96 (list 'interactive-only
97 #'(lambda (f _args instead)
67c477ae
SM
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.
106This 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.
112If `error-free', drop calls even if `byte-compile-delete-errors' is nil.")
d9857e53 113 (list 'compiler-macro
14146222 114 #'(lambda (f args compiler-function)
14146222 115 `(eval-and-compile
67c477ae
SM
116 (function-put ',f 'compiler-macro
117 ,(if (eq (car-safe compiler-function) 'lambda)
118 `(lambda ,(append (cadr compiler-function) args)
119 ,@(cddr compiler-function))
120 `#',compiler-function)))))
500fcedc 121 (list 'doc-string
d9857e53 122 #'(lambda (f _args pos)
67c477ae
SM
123 (list 'function-put (list 'quote f)
124 ''doc-string-elt (list 'quote pos))))
500fcedc 125 (list 'indent
d9857e53 126 #'(lambda (f _args val)
67c477ae 127 (list 'function-put (list 'quote f)
500fcedc
SM
128 ''lisp-indent-function (list 'quote val)))))
129 "List associating function properties to their macro expansion.
130Each element of the list takes the form (PROP FUN) where FUN is
131a function. For each (PROP . VALUES) in a function's declaration,
d9857e53
SM
132the FUN corresponding to PROP is called with the function name,
133the function's arglist, and the VALUES and should return the code to use
5076d275
GM
134to set this property.
135
136This is used by `declare'.")
500fcedc
SM
137
138(defvar macro-declarations-alist
139 (cons
140 (list 'debug
d9857e53 141 #'(lambda (name _args spec)
500fcedc
SM
142 (list 'progn :autoload-end
143 (list 'put (list 'quote name)
144 ''edebug-form-spec (list 'quote spec)))))
145 defun-declarations-alist)
146 "List associating properties of macros to their macro expansion.
d1a6bccc
SM
147Each element of the list takes the form (PROP FUN) where FUN is a function.
148For each (PROP . VALUES) in a macro's declaration, the FUN corresponding
149to PROP is called with the macro name, the macro's arglist, and the VALUES
5076d275
GM
150and should return the code to use to set this property.
151
152This is used by `declare'.")
500fcedc 153
61b108cc
SM
154(defalias 'defmacro
155 (cons
156 'macro
5cebef2d 157 #'(lambda (name arglist &optional docstring &rest body)
61b108cc
SM
158 "Define NAME as a macro.
159When the macro is called, as in (NAME ARGS...),
160the function (lambda ARGLIST BODY...) is applied to
161the list ARGS... as it appears in the expression,
162and the result should be a form to be evaluated instead of the original.
500fcedc
SM
163DECL is a declaration, optional, of the form (declare DECLS...) where
164DECLS is a list of elements of the form (PROP . VALUES). These are
1053a871 165interpreted according to `macro-declarations-alist'.
5cebef2d
AS
166The return value is undefined.
167
168\(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
169 ;; We can't just have `decl' as an &optional argument, because we need
170 ;; to distinguish
171 ;; (defmacro foo (arg) (bar) nil)
172 ;; from
173 ;; (defmacro foo (arg) (bar)).
174 (let ((decls (cond
175 ((eq (car-safe docstring) 'declare)
176 (prog1 (cdr docstring) (setq docstring nil)))
177 ((and (stringp docstring)
178 (eq (car-safe (car body)) 'declare))
179 (prog1 (cdr (car body)) (setq body (cdr body)))))))
180 (if docstring (setq body (cons docstring body))
181 (if (null body) (setq body '(nil))))
182 ;; Can't use backquote because it's not defined yet!
183 (let* ((fun (list 'function (cons 'lambda (cons arglist body))))
184 (def (list 'defalias
185 (list 'quote name)
186 (list 'cons ''macro fun)))
187 (declarations
188 (mapcar
189 #'(lambda (x)
190 (let ((f (cdr (assq (car x) macro-declarations-alist))))
191 (if f (apply (car f) name arglist (cdr x))
192 (message "Warning: Unknown macro property %S in %S"
193 (car x) name))))
194 decls)))
195 (if declarations
196 (cons 'prog1 (cons def declarations))
197 def))))))
61b108cc
SM
198
199;; Now that we defined defmacro we can use it!
200(defmacro defun (name arglist &optional docstring &rest body)
201 "Define NAME as a function.
202The definition is (lambda ARGLIST [DOCSTRING] BODY...).
500fcedc
SM
203See also the function `interactive'.
204DECL is a declaration, optional, of the form (declare DECLS...) where
205DECLS is a list of elements of the form (PROP . VALUES). These are
206interpreted according to `defun-declarations-alist'.
1053a871 207The return value is undefined.
500fcedc
SM
208
209\(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
210 ;; We can't just have `decl' as an &optional argument, because we need
211 ;; to distinguish
212 ;; (defun foo (arg) (toto) nil)
213 ;; from
214 ;; (defun foo (arg) (toto)).
c93f3f5c 215 (declare (doc-string 3) (indent 2))
500fcedc
SM
216 (let ((decls (cond
217 ((eq (car-safe docstring) 'declare)
218 (prog1 (cdr docstring) (setq docstring nil)))
5cebef2d
AS
219 ((and (stringp docstring)
220 (eq (car-safe (car body)) 'declare))
500fcedc
SM
221 (prog1 (cdr (car body)) (setq body (cdr body)))))))
222 (if docstring (setq body (cons docstring body))
223 (if (null body) (setq body '(nil))))
224 (let ((declarations
225 (mapcar
226 #'(lambda (x)
227 (let ((f (cdr (assq (car x) defun-declarations-alist))))
228 (cond
d9857e53 229 (f (apply (car f) name arglist (cdr x)))
500fcedc
SM
230 ;; Yuck!!
231 ((and (featurep 'cl)
232 (memq (car x) ;C.f. cl-do-proclaim.
233 '(special inline notinline optimize warn)))
daac280a 234 (push (list 'declare x)
ad235a8b
GM
235 (if (stringp docstring)
236 (if (eq (car-safe (cadr body)) 'interactive)
237 (cddr body)
238 (cdr body))
239 (if (eq (car-safe (car body)) 'interactive)
240 (cdr body)
241 body)))
500fcedc 242 nil)
daac280a 243 (t (message "Warning: Unknown defun property `%S' in %S"
500fcedc
SM
244 (car x) name)))))
245 decls))
246 (def (list 'defalias
247 (list 'quote name)
248 (list 'function
249 (cons 'lambda
250 (cons arglist body))))))
251 (if declarations
252 (cons 'prog1 (cons def declarations))
1b0f10d2
DC
253 def))))
254
623374a5 255\f
5e046f6d
JB
256;; Redefined in byte-optimize.el.
257;; This is not documented--it's not clear that we should promote it.
258(fset 'inline 'progn)
5e046f6d 259
5e046f6d
JB
260;;; Interface to inline functions.
261
262;; (defmacro proclaim-inline (&rest fns)
263;; "Cause the named functions to be open-coded when called from compiled code.
264;; They will only be compiled open-coded when byte-compile-optimize is true."
265;; (cons 'eval-and-compile
4f91a816 266;; (mapcar (lambda (x)
5e046f6d
JB
267;; (or (memq (get x 'byte-optimizer)
268;; '(nil byte-compile-inline-expand))
269;; (error
270;; "%s already has a byte-optimizer, can't make it inline"
271;; x))
272;; (list 'put (list 'quote x)
273;; ''byte-optimizer ''byte-compile-inline-expand))
274;; fns)))
275
276;; (defmacro proclaim-notinline (&rest fns)
277;; "Cause the named functions to no longer be open-coded."
278;; (cons 'eval-and-compile
4f91a816 279;; (mapcar (lambda (x)
5e046f6d
JB
280;; (if (eq (get x 'byte-optimizer) 'byte-compile-inline-expand)
281;; (put x 'byte-optimizer nil))
282;; (list 'if (list 'eq (list 'get (list 'quote x) ''byte-optimizer)
283;; ''byte-compile-inline-expand)
284;; (list 'put x ''byte-optimizer nil)))
285;; fns)))
286
5e046f6d 287(defmacro defsubst (name arglist &rest body)
d18a0d24
CY
288 "Define an inline function. The syntax is just like that of `defun'.
289\(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
b581bb5c 290 (declare (debug defun) (doc-string 3))
5e046f6d
JB
291 (or (memq (get name 'byte-optimizer)
292 '(nil byte-compile-inline-expand))
293 (error "`%s' is a primitive" name))
66599b54
SM
294 `(prog1
295 (defun ,name ,arglist ,@body)
296 (eval-and-compile
297 (put ',name 'byte-optimizer 'byte-compile-inline-expand))))
5e046f6d 298
ced10a4c
SM
299(defvar advertised-signature-table (make-hash-table :test 'eq :weakness 'key))
300
500fcedc 301(defun set-advertised-calling-convention (function signature _when)
ced10a4c
SM
302 "Set the advertised SIGNATURE of FUNCTION.
303This will allow the byte-compiler to warn the programmer when she uses
f3a30a50
SM
304an obsolete calling convention. WHEN specifies since when the calling
305convention was modified."
ced10a4c
SM
306 (puthash (indirect-function function) signature
307 advertised-signature-table))
308
fbb70c53 309(defun make-obsolete (obsolete-name current-name &optional when)
59f7af81
CY
310 "Make the byte-compiler warn that function OBSOLETE-NAME is obsolete.
311OBSOLETE-NAME should be a function name or macro name (a symbol).
312
fbb70c53 313The warning will say that CURRENT-NAME should be used instead.
584dcd8f
GM
314If CURRENT-NAME is a string, that is the `use instead' message
315\(it should end with a period, and not start with a capital).
2462470b 316WHEN should be a string indicating when the function
5e046f6d 317was first made obsolete, for example a date or a release number."
500fcedc
SM
318 (declare (advertised-calling-convention
319 ;; New code should always provide the `when' argument.
320 (obsolete-name current-name when) "23.1"))
a9de04fa
SM
321 (put obsolete-name 'byte-obsolete-info
322 ;; The second entry used to hold the `byte-compile' handler, but
323 ;; is not used any more nowadays.
2462470b 324 (purecopy (list current-name nil when)))
fbb70c53 325 obsolete-name)
5e046f6d 326
fbb70c53 327(defmacro define-obsolete-function-alias (obsolete-name current-name
0448b476 328 &optional when docstring)
fbb70c53 329 "Set OBSOLETE-NAME's function definition to CURRENT-NAME and mark it obsolete.
342ef03d
LT
330
331\(define-obsolete-function-alias 'old-fun 'new-fun \"22.1\" \"old-fun's doc.\")
332
333is equivalent to the following two lines of code:
334
335\(defalias 'old-fun 'new-fun \"old-fun's doc.\")
336\(make-obsolete 'old-fun 'new-fun \"22.1\")
337
338See the docstrings of `defalias' and `make-obsolete' for more details."
500fcedc
SM
339 (declare (doc-string 4)
340 (advertised-calling-convention
341 ;; New code should always provide the `when' argument.
342 (obsolete-name current-name when &optional docstring) "23.1"))
0448b476 343 `(progn
fbb70c53
JB
344 (defalias ,obsolete-name ,current-name ,docstring)
345 (make-obsolete ,obsolete-name ,current-name ,when)))
0448b476 346
2403c841 347(defun make-obsolete-variable (obsolete-name current-name &optional when access-type)
fbb70c53
JB
348 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
349The warning will say that CURRENT-NAME should be used instead.
350If CURRENT-NAME is a string, that is the `use instead' message.
2403c841
SM
351WHEN should be a string indicating when the variable
352was first made obsolete, for example a date or a release number.
353ACCESS-TYPE if non-nil should specify the kind of access that will trigger
354 obsolescence warnings; it can be either `get' or `set'."
500fcedc
SM
355 (declare (advertised-calling-convention
356 ;; New code should always provide the `when' argument.
357 (obsolete-name current-name when &optional access-type) "23.1"))
905a9ed3 358 (put obsolete-name 'byte-obsolete-variable
2403c841 359 (purecopy (list current-name access-type when)))
fbb70c53 360 obsolete-name)
500fcedc 361
5e046f6d 362
fbb70c53 363(defmacro define-obsolete-variable-alias (obsolete-name current-name
f7f8f37a 364 &optional when docstring)
fbb70c53 365 "Make OBSOLETE-NAME a variable alias for CURRENT-NAME and mark it obsolete.
850bfd04
GM
366This uses `defvaralias' and `make-obsolete-variable' (which see).
367See the Info node `(elisp)Variable Aliases' for more details.
342ef03d 368
f8754ca2 369If CURRENT-NAME is a defcustom (more generally, any variable
865fe16f 370where OBSOLETE-NAME may be set, e.g. in an init file, before the
f8754ca2 371alias is defined), then the define-obsolete-variable-alias
850bfd04
GM
372statement should be evaluated before the defcustom, if user
373customizations are to be respected. The simplest way to achieve
374this is to place the alias statement before the defcustom (this
375is not necessary for aliases that are autoloaded, or in files
376dumped with Emacs). This is so that any user customizations are
377applied before the defcustom tries to initialize the
378variable (this is due to the way `defvaralias' works).
379
380For the benefit of `custom-set-variables', if OBSOLETE-NAME has
381any of the following properties, they are copied to
382CURRENT-NAME, if it does not already have them:
383'saved-value, 'saved-variable-comment."
500fcedc
SM
384 (declare (doc-string 4)
385 (advertised-calling-convention
386 ;; New code should always provide the `when' argument.
387 (obsolete-name current-name when &optional docstring) "23.1"))
f7f8f37a 388 `(progn
fbb70c53 389 (defvaralias ,obsolete-name ,current-name ,docstring)
850bfd04 390 ;; See Bug#4706.
6e39d3b2
SM
391 (dolist (prop '(saved-value saved-variable-comment))
392 (and (get ,obsolete-name prop)
393 (null (get ,current-name prop))
394 (put ,current-name prop (get ,obsolete-name prop))))
79e74246 395 (make-obsolete-variable ,obsolete-name ,current-name ,when)))
f7f8f37a 396
95ed0f11
GM
397;; FIXME This is only defined in this file because the variable- and
398;; function- versions are too. Unlike those two, this one is not used
399;; by the byte-compiler (would be nice if it could warn about obsolete
400;; faces, but it doesn't really do anything special with faces).
401;; It only really affects M-x describe-face output.
ced10a4c 402(defmacro define-obsolete-face-alias (obsolete-face current-face when)
95ed0f11 403 "Make OBSOLETE-FACE a face alias for CURRENT-FACE and mark it obsolete.
73fe714a
GM
404The string WHEN gives the Emacs version where OBSOLETE-FACE became
405obsolete."
95ed0f11
GM
406 `(progn
407 (put ,obsolete-face 'face-alias ,current-face)
408 ;; Used by M-x describe-face.
1e8780b1 409 (put ,obsolete-face 'obsolete-face (or (purecopy ,when) t))))
95ed0f11 410
5e046f6d
JB
411(defmacro dont-compile (&rest body)
412 "Like `progn', but the body always runs interpreted (not compiled).
413If you think you need this, you're probably making a mistake somewhere."
17f32327 414 (declare (debug t) (indent 0) (obsolete nil "24.4"))
5e046f6d
JB
415 (list 'eval (list 'quote (if (cdr body) (cons 'progn body) (car body)))))
416
417\f
79e74246
SM
418;; interface to evaluating things at compile time and/or load time
419;; these macro must come after any uses of them in this file, as their
420;; definition in the file overrides the magic definitions on the
421;; byte-compile-macro-environment.
5e046f6d 422
5e046f6d 423(defmacro eval-when-compile (&rest body)
8cd567b8 424 "Like `progn', but evaluates the body at compile time if you're compiling.
12b1389c
DC
425Thus, the result of the body appears to the compiler as a quoted
426constant. In interpreted code, this is entirely equivalent to
427`progn', except that the value of the expression may be (but is
428not necessarily) computed at load time if eager macro expansion
429is enabled."
38b2c142 430 (declare (debug (&rest def-form)) (indent 0))
7f526211 431 (list 'quote (eval (cons 'progn body) lexical-binding)))
5e046f6d 432
5e046f6d 433(defmacro eval-and-compile (&rest body)
12b1389c
DC
434 "Like `progn', but evaluates the body at compile time and at
435load time. In interpreted code, this is entirely equivalent to
436`progn', except that the value of the expression may be (but is
437not necessarily) computed at load time if eager macro expansion
438is enabled."
623374a5 439 (declare (debug t) (indent 0))
7f526211
SM
440 ;; When the byte-compiler expands code, this macro is not used, so we're
441 ;; either about to run `body' (plain interpretation) or we're doing eager
442 ;; macroexpansion.
443 (list 'quote (eval (cons 'progn body) lexical-binding)))
5e046f6d 444
ae122ad2 445(defun with-no-warnings (&rest body)
5e046f6d 446 "Like `progn', but prevents compiler warnings in the body."
17f32327 447 (declare (indent 0))
5e046f6d 448 ;; The implementation for the interpreter is basically trivial.
ae122ad2 449 (car (last body)))
5e046f6d
JB
450
451\f
79e74246
SM
452;; I nuked this because it's not a good idea for users to think of using it.
453;; These options are a matter of installation preference, and have nothing to
454;; with particular source files; it's a mistake to suggest to users
455;; they should associate these with particular source files.
456;; There is hardly any reason to change these parameters, anyway.
457;; --rms.
5e046f6d 458
3fdfb09c 459;; (put 'byte-compiler-options 'lisp-indent-function 0)
5e046f6d
JB
460;; (defmacro byte-compiler-options (&rest args)
461;; "Set some compilation-parameters for this file. This will affect only the
462;; file in which it appears; this does nothing when evaluated, and when loaded
463;; from a .el file.
464;;
465;; Each argument to this macro must be a list of a key and a value.
466;;
467;; Keys: Values: Corresponding variable:
468;;
469;; verbose t, nil byte-compile-verbose
470;; optimize t, nil, source, byte byte-compile-optimize
471;; warnings list of warnings byte-compile-warnings
9b9a4122 472;; Valid elements: (callargs redefine free-vars unresolved)
5e046f6d
JB
473;; file-format emacs18, emacs19 byte-compile-compatibility
474;;
475;; For example, this might appear at the top of a source file:
476;;
477;; (byte-compiler-options
478;; (optimize t)
479;; (warnings (- free-vars)) ; Don't warn about free variables
480;; (file-format emacs19))"
481;; nil)
482
500fcedc 483(make-obsolete-variable 'macro-declaration-function
2a1e2476 484 'macro-declarations-alist "24.3")
500fcedc 485(make-obsolete 'macro-declaration-function
2a1e2476 486 'macro-declarations-alist "24.3")
500fcedc 487
5e046f6d 488;;; byte-run.el ends here