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