1 ;;; byte-run.el --- byte-compiler support for inlining -*- lexical-binding: t -*-
3 ;; Copyright (C) 1992, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Jamie Zawinski <jwz@lucid.com>
6 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
7 ;; Maintainer: emacs-devel@gnu.org
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/>.
28 ;; interface to selectively inlining functions.
29 ;; This only happens when source-code optimization is turned on.
33 (defalias 'function-put
34 ;; We don't want people to just use `put' because we can't conveniently
35 ;; hook into `put' to remap old properties to new ones. But for now, there's
36 ;; no such remapping, so we just call `put'.
37 #'(lambda (f prop value
) (put f prop value
))
38 "Set function F's property PROP to VALUE.
39 The namespace for PROP is shared with symbols.
40 So far, F can only be a symbol, not a lambda expression.")
41 (function-put 'defmacro
'doc-string-elt
3)
42 (function-put 'defmacro
'lisp-indent-function
2)
44 ;; `macro-declaration-function' are both obsolete (as marked at the end of this
45 ;; file) but used in many .elc files.
47 (defvar macro-declaration-function
#'macro-declaration-function
48 "Function to process declarations in a macro definition.
49 The function will be called with two args MACRO and DECL.
50 MACRO is the name of the macro being defined.
51 DECL is a list `(declare ...)' containing the declarations.
52 The value the function returns is not used.")
54 (defalias 'macro-declaration-function
55 #'(lambda (macro decl
)
56 "Process a declaration found in a macro definition.
57 This is set as the value of the variable `macro-declaration-function'.
58 MACRO is the name of the macro being defined.
59 DECL is a list `(declare ...)' containing the declarations.
60 The return value of this function is not used."
61 ;; We can't use `dolist' or `cadr' yet for bootstrapping reasons.
63 ;; Ignore the first element of `decl' (it's always `declare').
64 (while (setq decl
(cdr decl
))
69 (cond ((eq (car d
) 'indent
)
70 (put macro
'lisp-indent-function
(car (cdr d
))))
72 (put macro
'edebug-form-spec
(car (cdr d
))))
73 ((eq (car d
) 'doc-string
)
74 (put macro
'doc-string-elt
(car (cdr d
))))
76 (message "Unknown declaration %s" d
)))
77 (message "Invalid declaration %s" d
))))))
79 ;; We define macro-declaration-alist here because it is needed to
80 ;; handle declarations in macro definitions and this is the first file
81 ;; loaded by loadup.el that uses declarations in macros.
83 ;; Add any new entries to info node `(elisp)Declare Form'.
85 (defvar defun-declarations-alist
87 ;; We can only use backquotes inside the lambdas and not for those
88 ;; properties that are used by functions loaded before backquote.el.
89 (list 'advertised-calling-convention
90 #'(lambda (f _args arglist when
)
91 (list 'set-advertised-calling-convention
92 (list 'quote f
) (list 'quote arglist
) (list 'quote when
))))
94 #'(lambda (f _args new-name when
)
96 (list 'quote f
) (list 'quote new-name
) (list 'quote when
))))
97 (list 'interactive-only
98 #'(lambda (f _args instead
)
99 (list 'function-put
(list 'quote f
)
100 ''interactive-only
(list 'quote instead
))))
101 ;; FIXME: Merge `pure' and `side-effect-free'.
103 #'(lambda (f _args val
)
104 (list 'function-put
(list 'quote f
)
105 ''pure
(list 'quote val
)))
106 "If non-nil, the compiler can replace calls with their return value.
107 This may shift errors from run-time to compile-time.")
108 (list 'side-effect-free
109 #'(lambda (f _args val
)
110 (list 'function-put
(list 'quote f
)
111 ''side-effect-free
(list 'quote val
)))
112 "If non-nil, calls can be ignored if their value is unused.
113 If `error-free', drop calls even if `byte-compile-delete-errors' is nil.")
114 (list 'compiler-macro
115 #'(lambda (f args compiler-function
)
117 (function-put ',f
'compiler-macro
118 ,(if (eq (car-safe compiler-function
) 'lambda
)
119 `(lambda ,(append (cadr compiler-function
) args
)
120 ,@(cddr compiler-function
))
121 `#',compiler-function
)))))
123 #'(lambda (f _args pos
)
124 (list 'function-put
(list 'quote f
)
125 ''doc-string-elt
(list 'quote pos
))))
127 #'(lambda (f _args val
)
128 (list 'function-put
(list 'quote f
)
129 ''lisp-indent-function
(list 'quote val
)))))
130 "List associating function properties to their macro expansion.
131 Each element of the list takes the form (PROP FUN) where FUN is
132 a function. For each (PROP . VALUES) in a function's declaration,
133 the FUN corresponding to PROP is called with the function name,
134 the function's arglist, and the VALUES and should return the code to use
135 to set this property.
137 This is used by `declare'.")
139 (defvar macro-declarations-alist
142 #'(lambda (name _args spec
)
143 (list 'progn
:autoload-end
144 (list 'put
(list 'quote name
)
145 ''edebug-form-spec
(list 'quote spec
)))))
146 defun-declarations-alist
)
147 "List associating properties of macros to their macro expansion.
148 Each element of the list takes the form (PROP FUN) where FUN is a function.
149 For each (PROP . VALUES) in a macro's declaration, the FUN corresponding
150 to PROP is called with the macro name, the macro's arglist, and the VALUES
151 and should return the code to use to set this property.
153 This is used by `declare'."))
158 #'(lambda (name arglist
&optional docstring
&rest body
)
159 "Define NAME as a macro.
160 When the macro is called, as in (NAME ARGS...),
161 the function (lambda ARGLIST BODY...) is applied to
162 the list ARGS... as it appears in the expression,
163 and the result should be a form to be evaluated instead of the original.
164 DECL is a declaration, optional, of the form (declare DECLS...) where
165 DECLS is a list of elements of the form (PROP . VALUES). These are
166 interpreted according to `macro-declarations-alist'.
167 The return value is undefined.
169 \(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
170 ;; We can't just have `decl' as an &optional argument, because we need
172 ;; (defmacro foo (arg) (bar) nil)
174 ;; (defmacro foo (arg) (bar)).
176 ((eq (car-safe docstring
) 'declare
)
177 (prog1 (cdr docstring
) (setq docstring nil
)))
178 ((and (stringp docstring
)
179 (eq (car-safe (car body
)) 'declare
))
180 (prog1 (cdr (car body
)) (setq body
(cdr body
)))))))
181 (if docstring
(setq body
(cons docstring body
))
182 (if (null body
) (setq body
'(nil))))
183 ;; Can't use backquote because it's not defined yet!
184 (let* ((fun (list 'function
(cons 'lambda
(cons arglist body
))))
187 (list 'cons
''macro fun
)))
191 (let ((f (cdr (assq (car x
) macro-declarations-alist
))))
192 (if f
(apply (car f
) name arglist
(cdr x
))
193 (message "Warning: Unknown macro property %S in %S"
196 (list 'eval-when
'(:compile-toplevel
:load-toplevel
:execute
)
198 (cons 'prog1
(cons def declarations
))
201 ;; Now that we defined defmacro we can use it!
202 (defmacro defun
(name arglist
&optional docstring
&rest body
)
203 "Define NAME as a function.
204 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
205 See also the function `interactive'.
206 DECL is a declaration, optional, of the form (declare DECLS...) where
207 DECLS is a list of elements of the form (PROP . VALUES). These are
208 interpreted according to `defun-declarations-alist'.
209 The return value is undefined.
211 \(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
212 ;; We can't just have `decl' as an &optional argument, because we need
214 ;; (defun foo (arg) (toto) nil)
216 ;; (defun foo (arg) (toto)).
217 (declare (doc-string 3) (indent 2))
219 ((eq (car-safe docstring
) 'declare
)
220 (prog1 (cdr docstring
) (setq docstring nil
)))
221 ((and (stringp docstring
)
222 (eq (car-safe (car body
)) 'declare
))
223 (prog1 (cdr (car body
)) (setq body
(cdr body
)))))))
224 (if docstring
(setq body
(cons docstring body
))
225 (if (null body
) (setq body
'(nil))))
229 (let ((f (cdr (assq (car x
) defun-declarations-alist
))))
231 (f (apply (car f
) name arglist
(cdr x
)))
234 (memq (car x
) ;C.f. cl-do-proclaim.
235 '(special inline notinline optimize warn
)))
236 (setq body
(cons (list 'declare x
) body
))
238 (t (message "Warning: Unknown defun property `%S' in %S"
245 (cons arglist body
))))))
248 (cons 'prog1
(cons def declarations
))
251 (list '@ '(guile) 'set-procedure-property
!)
252 (list 'symbol-function
(list 'quote name
))
254 (list 'quote name
))))))
256 ;; Redefined in byte-optimize.el.
257 ;; This is not documented--it's not clear that we should promote it.
259 (defmacro inline
(&rest body
)
262 ;;; Interface to inline functions.
264 ;; (defmacro proclaim-inline (&rest fns)
265 ;; "Cause the named functions to be open-coded when called from compiled code.
266 ;; They will only be compiled open-coded when byte-compile-optimize is true."
267 ;; (cons 'eval-and-compile
268 ;; (mapcar (lambda (x)
269 ;; (or (memq (get x 'byte-optimizer)
270 ;; '(nil byte-compile-inline-expand))
272 ;; "%s already has a byte-optimizer, can't make it inline"
274 ;; (list 'put (list 'quote x)
275 ;; ''byte-optimizer ''byte-compile-inline-expand))
278 ;; (defmacro proclaim-notinline (&rest fns)
279 ;; "Cause the named functions to no longer be open-coded."
280 ;; (cons 'eval-and-compile
281 ;; (mapcar (lambda (x)
282 ;; (if (eq (get x 'byte-optimizer) 'byte-compile-inline-expand)
283 ;; (put x 'byte-optimizer nil))
284 ;; (list 'if (list 'eq (list 'get (list 'quote x) ''byte-optimizer)
285 ;; ''byte-compile-inline-expand)
286 ;; (list 'put x ''byte-optimizer nil)))
289 (defvar advertised-signature-table
(make-hash-table :test
'eq
:weakness
'key
))
291 (defun set-advertised-calling-convention (function signature _when
)
292 "Set the advertised SIGNATURE of FUNCTION.
293 This will allow the byte-compiler to warn the programmer when she uses
294 an obsolete calling convention. WHEN specifies since when the calling
295 convention was modified."
296 (puthash (indirect-function function
) signature
297 advertised-signature-table
))
299 (defun make-obsolete (obsolete-name current-name
&optional when
)
300 "Make the byte-compiler warn that function OBSOLETE-NAME is obsolete.
301 OBSOLETE-NAME should be a function name or macro name (a symbol).
303 The warning will say that CURRENT-NAME should be used instead.
304 If CURRENT-NAME is a string, that is the `use instead' message
305 \(it should end with a period, and not start with a capital).
306 WHEN should be a string indicating when the function
307 was first made obsolete, for example a date or a release number."
308 (declare (advertised-calling-convention
309 ;; New code should always provide the `when' argument.
310 (obsolete-name current-name when
) "23.1"))
311 (put obsolete-name
'byte-obsolete-info
312 ;; The second entry used to hold the `byte-compile' handler, but
313 ;; is not used any more nowadays.
314 (purecopy (list current-name nil when
)))
317 (defmacro define-obsolete-function-alias
(obsolete-name current-name
318 &optional when docstring
)
319 "Set OBSOLETE-NAME's function definition to CURRENT-NAME and mark it obsolete.
321 \(define-obsolete-function-alias 'old-fun 'new-fun \"22.1\" \"old-fun's doc.\")
323 is equivalent to the following two lines of code:
325 \(defalias 'old-fun 'new-fun \"old-fun's doc.\")
326 \(make-obsolete 'old-fun 'new-fun \"22.1\")
328 See the docstrings of `defalias' and `make-obsolete' for more details."
329 (declare (doc-string 4)
330 (advertised-calling-convention
331 ;; New code should always provide the `when' argument.
332 (obsolete-name current-name when
&optional docstring
) "23.1"))
334 (defalias ,obsolete-name
,current-name
,docstring
)
335 (make-obsolete ,obsolete-name
,current-name
,when
)))
337 (defun make-obsolete-variable (obsolete-name current-name
&optional when access-type
)
338 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
339 The warning will say that CURRENT-NAME should be used instead.
340 If CURRENT-NAME is a string, that is the `use instead' message.
341 WHEN should be a string indicating when the variable
342 was first made obsolete, for example a date or a release number.
343 ACCESS-TYPE if non-nil should specify the kind of access that will trigger
344 obsolescence warnings; it can be either `get' or `set'."
345 (declare (advertised-calling-convention
346 ;; New code should always provide the `when' argument.
347 (obsolete-name current-name when
&optional access-type
) "23.1"))
348 (put obsolete-name
'byte-obsolete-variable
349 (purecopy (list current-name access-type when
)))
353 (defmacro define-obsolete-variable-alias
(obsolete-name current-name
354 &optional when docstring
)
355 "Make OBSOLETE-NAME a variable alias for CURRENT-NAME and mark it obsolete.
356 This uses `defvaralias' and `make-obsolete-variable' (which see).
357 See the Info node `(elisp)Variable Aliases' for more details.
359 If CURRENT-NAME is a defcustom (more generally, any variable
360 where OBSOLETE-NAME may be set, e.g. in an init file, before the
361 alias is defined), then the define-obsolete-variable-alias
362 statement should be evaluated before the defcustom, if user
363 customizations are to be respected. The simplest way to achieve
364 this is to place the alias statement before the defcustom (this
365 is not necessary for aliases that are autoloaded, or in files
366 dumped with Emacs). This is so that any user customizations are
367 applied before the defcustom tries to initialize the
368 variable (this is due to the way `defvaralias' works).
370 For the benefit of `custom-set-variables', if OBSOLETE-NAME has
371 any of the following properties, they are copied to
372 CURRENT-NAME, if it does not already have them:
373 'saved-value, 'saved-variable-comment."
374 (declare (doc-string 4)
375 (advertised-calling-convention
376 ;; New code should always provide the `when' argument.
377 (obsolete-name current-name when
&optional docstring
) "23.1"))
379 (defvaralias ,obsolete-name
,current-name
,docstring
)
381 (dolist (prop '(saved-value saved-variable-comment
))
382 (and (get ,obsolete-name prop
)
383 (null (get ,current-name prop
))
384 (put ,current-name prop
(get ,obsolete-name prop
))))
385 (make-obsolete-variable ,obsolete-name
,current-name
,when
)))
387 ;; FIXME This is only defined in this file because the variable- and
388 ;; function- versions are too. Unlike those two, this one is not used
389 ;; by the byte-compiler (would be nice if it could warn about obsolete
390 ;; faces, but it doesn't really do anything special with faces).
391 ;; It only really affects M-x describe-face output.
392 (defmacro define-obsolete-face-alias
(obsolete-face current-face when
)
393 "Make OBSOLETE-FACE a face alias for CURRENT-FACE and mark it obsolete.
394 The string WHEN gives the Emacs version where OBSOLETE-FACE became
397 (put ,obsolete-face
'face-alias
,current-face
)
398 ;; Used by M-x describe-face.
399 (put ,obsolete-face
'obsolete-face
(or (purecopy ,when
) t
))))
401 (defmacro dont-compile
(&rest body
)
402 "Like `progn', but the body always runs interpreted (not compiled).
403 If you think you need this, you're probably making a mistake somewhere."
404 (declare (debug t
) (indent 0) (obsolete nil
"24.4"))
405 (list 'eval
(list 'quote
(if (cdr body
) (cons 'progn body
) (car body
)))))
408 ;; interface to evaluating things at compile time and/or load time
409 ;; these macro must come after any uses of them in this file, as their
410 ;; definition in the file overrides the magic definitions on the
411 ;; byte-compile-macro-environment.
413 (defmacro eval-when-compile
(&rest body
)
414 "Like `progn', but evaluates the body at compile time if you're compiling.
415 Thus, the result of the body appears to the compiler as a quoted
416 constant. In interpreted code, this is entirely equivalent to
417 `progn', except that the value of the expression may be (but is
418 not necessarily) computed at load time if eager macro expansion
420 (declare (debug (&rest def-form
)) (indent 0))
421 (list 'quote
(eval (cons 'progn body
) lexical-binding
)))
423 (defmacro eval-and-compile
(&rest body
)
424 "Like `progn', but evaluates the body at compile time and at
425 load time. In interpreted code, this is entirely equivalent to
426 `progn', except that the value of the expression may be (but is
427 not necessarily) computed at load time if eager macro expansion
429 (declare (debug t
) (indent 0))
430 ;; When the byte-compiler expands code, this macro is not used, so we're
431 ;; either about to run `body' (plain interpretation) or we're doing eager
433 (list 'quote
(eval (cons 'progn body
) lexical-binding
)))
435 (defun with-no-warnings (&rest body
)
436 "Like `progn', but prevents compiler warnings in the body."
438 ;; The implementation for the interpreter is basically trivial.
442 ;; I nuked this because it's not a good idea for users to think of using it.
443 ;; These options are a matter of installation preference, and have nothing to
444 ;; with particular source files; it's a mistake to suggest to users
445 ;; they should associate these with particular source files.
446 ;; There is hardly any reason to change these parameters, anyway.
449 ;; (put 'byte-compiler-options 'lisp-indent-function 0)
450 ;; (defmacro byte-compiler-options (&rest args)
451 ;; "Set some compilation-parameters for this file. This will affect only the
452 ;; file in which it appears; this does nothing when evaluated, and when loaded
455 ;; Each argument to this macro must be a list of a key and a value.
457 ;; Keys: Values: Corresponding variable:
459 ;; verbose t, nil byte-compile-verbose
460 ;; optimize t, nil, source, byte byte-compile-optimize
461 ;; warnings list of warnings byte-compile-warnings
462 ;; Valid elements: (callargs redefine free-vars unresolved)
463 ;; file-format emacs18, emacs19 byte-compile-compatibility
465 ;; For example, this might appear at the top of a source file:
467 ;; (byte-compiler-options
469 ;; (warnings (- free-vars)) ; Don't warn about free variables
470 ;; (file-format emacs19))"
473 (make-obsolete-variable 'macro-declaration-function
474 'macro-declarations-alist
"24.3")
475 (make-obsolete 'macro-declaration-function
476 'macro-declarations-alist
"24.3")
478 ;;; byte-run.el ends here