Merge from emacs-24; up to 2014-04-25T10:35:01Z!michael.albinus@gmx.de
[bpt/emacs.git] / lisp / emacs-lisp / byte-run.el
1 ;;; byte-run.el --- byte-compiler support for inlining -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1992, 2001-2014 Free Software Foundation, Inc.
4
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
10
11 ;; This file is part of GNU Emacs.
12
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.
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
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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
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)
43
44 ;; `macro-declaration-function' are both obsolete (as marked at the end of this
45 ;; file) but used in many .elc files.
46
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.")
53
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))))
75 (t
76 (message "Unknown declaration %s" d)))
77 (message "Invalid declaration %s" d))))))
78
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
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 `(eval-and-compile
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)))))
121 (list 'doc-string
122 #'(lambda (f _args pos)
123 (list 'function-put (list 'quote f)
124 ''doc-string-elt (list 'quote pos))))
125 (list 'indent
126 #'(lambda (f _args val)
127 (list 'function-put (list 'quote f)
128 ''lisp-indent-function (list 'quote val)))))
129 "List associating function properties to their macro expansion.
130 Each element of the list takes the form (PROP FUN) where FUN is
131 a function. For each (PROP . VALUES) in a function's declaration,
132 the FUN corresponding to PROP is called with the function name,
133 the function's arglist, and the VALUES and should return the code to use
134 to set this property.
135
136 This is used by `declare'.")
137
138 (defvar macro-declarations-alist
139 (cons
140 (list 'debug
141 #'(lambda (name _args spec)
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.
147 Each element of the list takes the form (PROP FUN) where FUN is a function.
148 For each (PROP . VALUES) in a macro's declaration, the FUN corresponding
149 to PROP is called with the macro name, the macro's arglist, and the VALUES
150 and should return the code to use to set this property.
151
152 This is used by `declare'.")
153
154 (defalias 'defmacro
155 (cons
156 'macro
157 #'(lambda (name arglist &optional docstring &rest body)
158 "Define NAME as a macro.
159 When the macro is called, as in (NAME ARGS...),
160 the function (lambda ARGLIST BODY...) is applied to
161 the list ARGS... as it appears in the expression,
162 and the result should be a form to be evaluated instead of the original.
163 DECL is a declaration, optional, of the form (declare DECLS...) where
164 DECLS is a list of elements of the form (PROP . VALUES). These are
165 interpreted according to `macro-declarations-alist'.
166 The 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))))))
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.
202 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
203 See also the function `interactive'.
204 DECL is a declaration, optional, of the form (declare DECLS...) where
205 DECLS is a list of elements of the form (PROP . VALUES). These are
206 interpreted according to `defun-declarations-alist'.
207 The return value is undefined.
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)).
215 (declare (doc-string 3) (indent 2))
216 (let ((decls (cond
217 ((eq (car-safe docstring) 'declare)
218 (prog1 (cdr docstring) (setq docstring nil)))
219 ((and (stringp docstring)
220 (eq (car-safe (car body)) 'declare))
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
229 (f (apply (car f) name arglist (cdr x)))
230 ;; Yuck!!
231 ((and (featurep 'cl)
232 (memq (car x) ;C.f. cl-do-proclaim.
233 '(special inline notinline optimize warn)))
234 (push (list 'declare x)
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)))
242 nil)
243 (t (message "Warning: Unknown defun property `%S' in %S"
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))
253 def))))
254
255 \f
256 ;; Redefined in byte-optimize.el.
257 ;; This is not documented--it's not clear that we should promote it.
258 (fset 'inline 'progn)
259
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
266 ;; (mapcar (lambda (x)
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
279 ;; (mapcar (lambda (x)
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
287 (defmacro defsubst (name arglist &rest body)
288 "Define an inline function. The syntax is just like that of `defun'.
289 \(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
290 (declare (debug defun) (doc-string 3))
291 (or (memq (get name 'byte-optimizer)
292 '(nil byte-compile-inline-expand))
293 (error "`%s' is a primitive" name))
294 `(prog1
295 (defun ,name ,arglist ,@body)
296 (eval-and-compile
297 (put ',name 'byte-optimizer 'byte-compile-inline-expand))))
298
299 (defvar advertised-signature-table (make-hash-table :test 'eq :weakness 'key))
300
301 (defun set-advertised-calling-convention (function signature _when)
302 "Set the advertised SIGNATURE of FUNCTION.
303 This will allow the byte-compiler to warn the programmer when she uses
304 an obsolete calling convention. WHEN specifies since when the calling
305 convention was modified."
306 (puthash (indirect-function function) signature
307 advertised-signature-table))
308
309 (defun make-obsolete (obsolete-name current-name &optional when)
310 "Make the byte-compiler warn that function OBSOLETE-NAME is obsolete.
311 OBSOLETE-NAME should be a function name or macro name (a symbol).
312
313 The warning will say that CURRENT-NAME should be used instead.
314 If CURRENT-NAME is a string, that is the `use instead' message
315 \(it should end with a period, and not start with a capital).
316 WHEN should be a string indicating when the function
317 was first made obsolete, for example a date or a release number."
318 (declare (advertised-calling-convention
319 ;; New code should always provide the `when' argument.
320 (obsolete-name current-name when) "23.1"))
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.
324 (purecopy (list current-name nil when)))
325 obsolete-name)
326
327 (defmacro define-obsolete-function-alias (obsolete-name current-name
328 &optional when docstring)
329 "Set OBSOLETE-NAME's function definition to CURRENT-NAME and mark it obsolete.
330
331 \(define-obsolete-function-alias 'old-fun 'new-fun \"22.1\" \"old-fun's doc.\")
332
333 is 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
338 See the docstrings of `defalias' and `make-obsolete' for more details."
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"))
343 `(progn
344 (defalias ,obsolete-name ,current-name ,docstring)
345 (make-obsolete ,obsolete-name ,current-name ,when)))
346
347 (defun make-obsolete-variable (obsolete-name current-name &optional when access-type)
348 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
349 The warning will say that CURRENT-NAME should be used instead.
350 If CURRENT-NAME is a string, that is the `use instead' message.
351 WHEN should be a string indicating when the variable
352 was first made obsolete, for example a date or a release number.
353 ACCESS-TYPE if non-nil should specify the kind of access that will trigger
354 obsolescence warnings; it can be either `get' or `set'."
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"))
358 (put obsolete-name 'byte-obsolete-variable
359 (purecopy (list current-name access-type when)))
360 obsolete-name)
361
362
363 (defmacro define-obsolete-variable-alias (obsolete-name current-name
364 &optional when docstring)
365 "Make OBSOLETE-NAME a variable alias for CURRENT-NAME and mark it obsolete.
366 This uses `defvaralias' and `make-obsolete-variable' (which see).
367 See the Info node `(elisp)Variable Aliases' for more details.
368
369 If CURRENT-NAME is a defcustom (more generally, any variable
370 where OBSOLETE-NAME may be set, e.g. in an init file, before the
371 alias is defined), then the define-obsolete-variable-alias
372 statement should be evaluated before the defcustom, if user
373 customizations are to be respected. The simplest way to achieve
374 this is to place the alias statement before the defcustom (this
375 is not necessary for aliases that are autoloaded, or in files
376 dumped with Emacs). This is so that any user customizations are
377 applied before the defcustom tries to initialize the
378 variable (this is due to the way `defvaralias' works).
379
380 For the benefit of `custom-set-variables', if OBSOLETE-NAME has
381 any of the following properties, they are copied to
382 CURRENT-NAME, if it does not already have them:
383 'saved-value, 'saved-variable-comment."
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"))
388 `(progn
389 (defvaralias ,obsolete-name ,current-name ,docstring)
390 ;; See Bug#4706.
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))))
395 (make-obsolete-variable ,obsolete-name ,current-name ,when)))
396
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.
402 (defmacro define-obsolete-face-alias (obsolete-face current-face when)
403 "Make OBSOLETE-FACE a face alias for CURRENT-FACE and mark it obsolete.
404 The string WHEN gives the Emacs version where OBSOLETE-FACE became
405 obsolete."
406 `(progn
407 (put ,obsolete-face 'face-alias ,current-face)
408 ;; Used by M-x describe-face.
409 (put ,obsolete-face 'obsolete-face (or (purecopy ,when) t))))
410
411 (defmacro dont-compile (&rest body)
412 "Like `progn', but the body always runs interpreted (not compiled).
413 If you think you need this, you're probably making a mistake somewhere."
414 (declare (debug t) (indent 0) (obsolete nil "24.4"))
415 (list 'eval (list 'quote (if (cdr body) (cons 'progn body) (car body)))))
416
417 \f
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.
422
423 (defmacro eval-when-compile (&rest body)
424 "Like `progn', but evaluates the body at compile time if you're compiling.
425 Thus, the result of the body appears to the compiler as a quoted
426 constant. In interpreted code, this is entirely equivalent to
427 `progn', except that the value of the expression may be (but is
428 not necessarily) computed at load time if eager macro expansion
429 is enabled."
430 (declare (debug (&rest def-form)) (indent 0))
431 (list 'quote (eval (cons 'progn body) lexical-binding)))
432
433 (defmacro eval-and-compile (&rest body)
434 "Like `progn', but evaluates the body at compile time and at
435 load time. In interpreted code, this is entirely equivalent to
436 `progn', except that the value of the expression may be (but is
437 not necessarily) computed at load time if eager macro expansion
438 is enabled."
439 (declare (debug t) (indent 0))
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)))
444
445 (defun with-no-warnings (&rest body)
446 "Like `progn', but prevents compiler warnings in the body."
447 (declare (indent 0))
448 ;; The implementation for the interpreter is basically trivial.
449 (car (last body)))
450
451 \f
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.
458
459 ;; (put 'byte-compiler-options 'lisp-indent-function 0)
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
472 ;; Valid elements: (callargs redefine free-vars unresolved)
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
483 (make-obsolete-variable 'macro-declaration-function
484 'macro-declarations-alist "24.3")
485 (make-obsolete 'macro-declaration-function
486 'macro-declarations-alist "24.3")
487
488 ;;; byte-run.el ends here