Use `declare' in defmacros.
[bpt/emacs.git] / lisp / emacs-lisp / byte-run.el
CommitLineData
5e046f6d
JB
1;;; byte-run.el --- byte-compiler support for inlining
2
95ed0f11 3;; Copyright (C) 1992, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
114f9c96 4;; 2009, 2010 Free Software Foundation, Inc.
5e046f6d
JB
5
6;; Author: Jamie Zawinski <jwz@lucid.com>
7;; Hallvard Furuseth <hbf@ulrik.uio.no>
8;; Maintainer: FSF
9;; Keywords: internal
bd78fa1d 10;; Package: emacs
5e046f6d
JB
11
12;; This file is part of GNU Emacs.
13
d6cba7ae 14;; GNU Emacs is free software: you can redistribute it and/or modify
5e046f6d 15;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
16;; the Free Software Foundation, either version 3 of the License, or
17;; (at your option) any later version.
5e046f6d
JB
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
d6cba7ae 25;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
5e046f6d
JB
26
27;;; Commentary:
28
29;; interface to selectively inlining functions.
30;; This only happens when source-code optimization is turned on.
31
32;;; Code:
33
623374a5
LK
34;; We define macro-declaration-function here because it is needed to
35;; handle declarations in macro definitions and this is the first file
36;; loaded by loadup.el that uses declarations in macros.
37
38(defun macro-declaration-function (macro decl)
39 "Process a declaration found in a macro definition.
40This is set as the value of the variable `macro-declaration-function'.
41MACRO is the name of the macro being defined.
42DECL is a list `(declare ...)' containing the declarations.
43The return value of this function is not used."
44 ;; We can't use `dolist' or `cadr' yet for bootstrapping reasons.
45 (let (d)
46 ;; Ignore the first element of `decl' (it's always `declare').
47 (while (setq decl (cdr decl))
48 (setq d (car decl))
15183da6
CY
49 (if (and (consp d)
50 (listp (cdr d))
51 (null (cdr (cdr d))))
52 (cond ((eq (car d) 'indent)
53 (put macro 'lisp-indent-function (car (cdr d))))
54 ((eq (car d) 'debug)
55 (put macro 'edebug-form-spec (car (cdr d))))
56 ((eq (car d) 'doc-string)
57 (put macro 'doc-string-elt (car (cdr d))))
58 (t
59 (message "Unknown declaration %s" d)))
60 (message "Invalid declaration %s" d)))))
61
623374a5
LK
62
63(setq macro-declaration-function 'macro-declaration-function)
64
65\f
5e046f6d
JB
66;; Redefined in byte-optimize.el.
67;; This is not documented--it's not clear that we should promote it.
68(fset 'inline 'progn)
5e046f6d 69
5e046f6d
JB
70;;; Interface to inline functions.
71
72;; (defmacro proclaim-inline (&rest fns)
73;; "Cause the named functions to be open-coded when called from compiled code.
74;; They will only be compiled open-coded when byte-compile-optimize is true."
75;; (cons 'eval-and-compile
76;; (mapcar '(lambda (x)
77;; (or (memq (get x 'byte-optimizer)
78;; '(nil byte-compile-inline-expand))
79;; (error
80;; "%s already has a byte-optimizer, can't make it inline"
81;; x))
82;; (list 'put (list 'quote x)
83;; ''byte-optimizer ''byte-compile-inline-expand))
84;; fns)))
85
86;; (defmacro proclaim-notinline (&rest fns)
87;; "Cause the named functions to no longer be open-coded."
88;; (cons 'eval-and-compile
89;; (mapcar '(lambda (x)
90;; (if (eq (get x 'byte-optimizer) 'byte-compile-inline-expand)
91;; (put x 'byte-optimizer nil))
92;; (list 'if (list 'eq (list 'get (list 'quote x) ''byte-optimizer)
93;; ''byte-compile-inline-expand)
94;; (list 'put x ''byte-optimizer nil)))
95;; fns)))
96
97;; This has a special byte-hunk-handler in bytecomp.el.
98(defmacro defsubst (name arglist &rest body)
99 "Define an inline function. The syntax is just like that of `defun'."
66599b54 100 (declare (debug defun))
5e046f6d
JB
101 (or (memq (get name 'byte-optimizer)
102 '(nil byte-compile-inline-expand))
103 (error "`%s' is a primitive" name))
66599b54
SM
104 `(prog1
105 (defun ,name ,arglist ,@body)
106 (eval-and-compile
107 (put ',name 'byte-optimizer 'byte-compile-inline-expand))))
5e046f6d 108
ced10a4c
SM
109(defvar advertised-signature-table (make-hash-table :test 'eq :weakness 'key))
110
111(defun set-advertised-calling-convention (function signature)
112 "Set the advertised SIGNATURE of FUNCTION.
113This will allow the byte-compiler to warn the programmer when she uses
114an obsolete calling convention."
115 (puthash (indirect-function function) signature
116 advertised-signature-table))
117
fbb70c53
JB
118(defun make-obsolete (obsolete-name current-name &optional when)
119 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
120The warning will say that CURRENT-NAME should be used instead.
584dcd8f
GM
121If CURRENT-NAME is a string, that is the `use instead' message
122\(it should end with a period, and not start with a capital).
5e046f6d
JB
123If provided, WHEN should be a string indicating when the function
124was first made obsolete, for example a date or a release number."
125 (interactive "aMake function obsolete: \nxObsoletion replacement: ")
fbb70c53 126 (let ((handler (get obsolete-name 'byte-compile)))
5e046f6d 127 (if (eq 'byte-compile-obsolete handler)
fbb70c53
JB
128 (setq handler (nth 1 (get obsolete-name 'byte-obsolete-info)))
129 (put obsolete-name 'byte-compile 'byte-compile-obsolete))
a7610c52
DN
130 (put obsolete-name 'byte-obsolete-info
131 (list (purecopy current-name) handler (purecopy when))))
fbb70c53 132 obsolete-name)
ced10a4c
SM
133(set-advertised-calling-convention
134 ;; New code should always provide the `when' argument.
135 'make-obsolete '(obsolete-name current-name when))
5e046f6d 136
fbb70c53 137(defmacro define-obsolete-function-alias (obsolete-name current-name
0448b476 138 &optional when docstring)
fbb70c53 139 "Set OBSOLETE-NAME's function definition to CURRENT-NAME and mark it obsolete.
342ef03d
LT
140
141\(define-obsolete-function-alias 'old-fun 'new-fun \"22.1\" \"old-fun's doc.\")
142
143is equivalent to the following two lines of code:
144
145\(defalias 'old-fun 'new-fun \"old-fun's doc.\")
146\(make-obsolete 'old-fun 'new-fun \"22.1\")
147
148See the docstrings of `defalias' and `make-obsolete' for more details."
79e74246 149 (declare (doc-string 4))
0448b476 150 `(progn
fbb70c53
JB
151 (defalias ,obsolete-name ,current-name ,docstring)
152 (make-obsolete ,obsolete-name ,current-name ,when)))
ced10a4c
SM
153(set-advertised-calling-convention
154 ;; New code should always provide the `when' argument.
155 'define-obsolete-function-alias
156 '(obsolete-name current-name when &optional docstring))
0448b476 157
fbb70c53
JB
158(defun make-obsolete-variable (obsolete-name current-name &optional when)
159 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
160The warning will say that CURRENT-NAME should be used instead.
161If CURRENT-NAME is a string, that is the `use instead' message.
5e046f6d
JB
162If provided, WHEN should be a string indicating when the variable
163was first made obsolete, for example a date or a release number."
164 (interactive
165 (list
166 (let ((str (completing-read "Make variable obsolete: " obarray 'boundp t)))
167 (if (equal str "") (error ""))
168 (intern str))
169 (car (read-from-string (read-string "Obsoletion replacement: ")))))
905a9ed3
DN
170 (put obsolete-name 'byte-obsolete-variable
171 (cons
172 (if (stringp current-name)
173 (purecopy current-name)
174 current-name) (purecopy when)))
fbb70c53 175 obsolete-name)
ced10a4c
SM
176(set-advertised-calling-convention
177 ;; New code should always provide the `when' argument.
178 'make-obsolete-variable '(obsolete-name current-name when))
5e046f6d 179
fbb70c53 180(defmacro define-obsolete-variable-alias (obsolete-name current-name
f7f8f37a 181 &optional when docstring)
fbb70c53 182 "Make OBSOLETE-NAME a variable alias for CURRENT-NAME and mark it obsolete.
850bfd04
GM
183This uses `defvaralias' and `make-obsolete-variable' (which see).
184See the Info node `(elisp)Variable Aliases' for more details.
342ef03d 185
f8754ca2
GM
186If CURRENT-NAME is a defcustom (more generally, any variable
187where OBSOLETE-NAME may be set, e.g. in a .emacs file, before the
188alias is defined), then the define-obsolete-variable-alias
850bfd04
GM
189statement should be evaluated before the defcustom, if user
190customizations are to be respected. The simplest way to achieve
191this is to place the alias statement before the defcustom (this
192is not necessary for aliases that are autoloaded, or in files
193dumped with Emacs). This is so that any user customizations are
194applied before the defcustom tries to initialize the
195variable (this is due to the way `defvaralias' works).
196
197For the benefit of `custom-set-variables', if OBSOLETE-NAME has
198any of the following properties, they are copied to
199CURRENT-NAME, if it does not already have them:
200'saved-value, 'saved-variable-comment."
79e74246 201 (declare (doc-string 4))
f7f8f37a 202 `(progn
fbb70c53 203 (defvaralias ,obsolete-name ,current-name ,docstring)
850bfd04 204 ;; See Bug#4706.
6e39d3b2
SM
205 (dolist (prop '(saved-value saved-variable-comment))
206 (and (get ,obsolete-name prop)
207 (null (get ,current-name prop))
208 (put ,current-name prop (get ,obsolete-name prop))))
79e74246 209 (make-obsolete-variable ,obsolete-name ,current-name ,when)))
ced10a4c
SM
210(set-advertised-calling-convention
211 ;; New code should always provide the `when' argument.
212 'define-obsolete-variable-alias
213 '(obsolete-name current-name when &optional docstring))
f7f8f37a 214
95ed0f11
GM
215;; FIXME This is only defined in this file because the variable- and
216;; function- versions are too. Unlike those two, this one is not used
217;; by the byte-compiler (would be nice if it could warn about obsolete
218;; faces, but it doesn't really do anything special with faces).
219;; It only really affects M-x describe-face output.
ced10a4c 220(defmacro define-obsolete-face-alias (obsolete-face current-face when)
95ed0f11 221 "Make OBSOLETE-FACE a face alias for CURRENT-FACE and mark it obsolete.
73fe714a
GM
222The string WHEN gives the Emacs version where OBSOLETE-FACE became
223obsolete."
95ed0f11
GM
224 `(progn
225 (put ,obsolete-face 'face-alias ,current-face)
226 ;; Used by M-x describe-face.
1e8780b1 227 (put ,obsolete-face 'obsolete-face (or (purecopy ,when) t))))
95ed0f11 228
5e046f6d
JB
229(defmacro dont-compile (&rest body)
230 "Like `progn', but the body always runs interpreted (not compiled).
231If you think you need this, you're probably making a mistake somewhere."
623374a5 232 (declare (debug t) (indent 0))
5e046f6d
JB
233 (list 'eval (list 'quote (if (cdr body) (cons 'progn body) (car body)))))
234
235\f
79e74246
SM
236;; interface to evaluating things at compile time and/or load time
237;; these macro must come after any uses of them in this file, as their
238;; definition in the file overrides the magic definitions on the
239;; byte-compile-macro-environment.
5e046f6d 240
5e046f6d 241(defmacro eval-when-compile (&rest body)
8cd567b8
RS
242 "Like `progn', but evaluates the body at compile time if you're compiling.
243Thus, the result of the body appears to the compiler as a quoted constant.
244In interpreted code, this is entirely equivalent to `progn'."
623374a5 245 (declare (debug t) (indent 0))
5e046f6d
JB
246 ;; Not necessary because we have it in b-c-initial-macro-environment
247 ;; (list 'quote (eval (cons 'progn body)))
248 (cons 'progn body))
249
5e046f6d
JB
250(defmacro eval-and-compile (&rest body)
251 "Like `progn', but evaluates the body at compile time and at load time."
623374a5 252 (declare (debug t) (indent 0))
5e046f6d
JB
253 ;; Remember, it's magic.
254 (cons 'progn body))
255
3fdfb09c 256(put 'with-no-warnings 'lisp-indent-function 0)
ae122ad2 257(defun with-no-warnings (&rest body)
5e046f6d
JB
258 "Like `progn', but prevents compiler warnings in the body."
259 ;; The implementation for the interpreter is basically trivial.
ae122ad2 260 (car (last body)))
5e046f6d
JB
261
262\f
79e74246
SM
263;; I nuked this because it's not a good idea for users to think of using it.
264;; These options are a matter of installation preference, and have nothing to
265;; with particular source files; it's a mistake to suggest to users
266;; they should associate these with particular source files.
267;; There is hardly any reason to change these parameters, anyway.
268;; --rms.
5e046f6d 269
3fdfb09c 270;; (put 'byte-compiler-options 'lisp-indent-function 0)
5e046f6d
JB
271;; (defmacro byte-compiler-options (&rest args)
272;; "Set some compilation-parameters for this file. This will affect only the
273;; file in which it appears; this does nothing when evaluated, and when loaded
274;; from a .el file.
275;;
276;; Each argument to this macro must be a list of a key and a value.
277;;
278;; Keys: Values: Corresponding variable:
279;;
280;; verbose t, nil byte-compile-verbose
281;; optimize t, nil, source, byte byte-compile-optimize
282;; warnings list of warnings byte-compile-warnings
9b9a4122 283;; Valid elements: (callargs redefine free-vars unresolved)
5e046f6d
JB
284;; file-format emacs18, emacs19 byte-compile-compatibility
285;;
286;; For example, this might appear at the top of a source file:
287;;
288;; (byte-compiler-options
289;; (optimize t)
290;; (warnings (- free-vars)) ; Don't warn about free variables
291;; (file-format emacs19))"
292;; nil)
293
79e74246 294;; arch-tag: 76f8328a-1f66-4df2-9b6d-5c3666dc05e9
5e046f6d 295;;; byte-run.el ends here