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