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