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