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