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