1 ;;; byte-run.el --- byte-compiler support for inlining -*- lexical-binding: t -*-
3 ;; Copyright (C) 1992, 2001-2015 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 (function prop value
)
38 "Set FUNCTION's property PROP to VALUE.
39 The namespace for PROP is shared with symbols.
40 So far, FUNCTION can only be a symbol, not a lambda expression."
41 (put function prop value
)))
42 (function-put 'defmacro
'doc-string-elt
3)
43 (function-put 'defmacro
'lisp-indent-function
2)
45 ;; `macro-declaration-function' are both obsolete (as marked at the end of this
46 ;; file) but used in many .elc files.
48 (defvar macro-declaration-function
#'macro-declaration-function
49 "Function to process declarations in a macro definition.
50 The function will be called with two args MACRO and DECL.
51 MACRO is the name of the macro being defined.
52 DECL is a list `(declare ...)' containing the declarations.
53 The value the function returns is not used.")
55 (defalias 'macro-declaration-function
56 #'(lambda (macro decl
)
57 "Process a declaration found in a macro definition.
58 This is set as the value of the variable `macro-declaration-function'.
59 MACRO is the name of the macro being defined.
60 DECL is a list `(declare ...)' containing the declarations.
61 The return value of this function is not used."
62 ;; We can't use `dolist' or `cadr' yet for bootstrapping reasons.
64 ;; Ignore the first element of `decl' (it's always `declare').
65 (while (setq decl
(cdr decl
))
70 (cond ((eq (car d
) 'indent
)
71 (put macro
'lisp-indent-function
(car (cdr d
))))
73 (put macro
'edebug-form-spec
(car (cdr d
))))
74 ((eq (car d
) 'doc-string
)
75 (put macro
'doc-string-elt
(car (cdr d
))))
77 (message "Unknown declaration %s" d
)))
78 (message "Invalid declaration %s" d
))))))
80 ;; We define macro-declaration-alist here because it is needed to
81 ;; handle declarations in macro definitions and this is the first file
82 ;; loaded by loadup.el that uses declarations in macros.
84 ;; 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
)
116 (if (not (eq (car-safe compiler-function
) 'lambda
))
118 (function-put ',f
'compiler-macro
#',compiler-function
))
119 (let ((cfname (intern (concat (symbol-name f
) "--anon-cmacro"))))
122 (function-put ',f
'compiler-macro
#',cfname
))
123 ;; Don't autoload the compiler-macro itself, since the
124 ;; macroexpander will find this file via `f's autoload,
128 (defun ,cfname
(,@(cadr compiler-function
) ,@args
)
129 ,@(cddr compiler-function
))))))))
131 #'(lambda (f _args pos
)
132 (list 'function-put
(list 'quote f
)
133 ''doc-string-elt
(list 'quote pos
))))
135 #'(lambda (f _args val
)
136 (list 'function-put
(list 'quote f
)
137 ''lisp-indent-function
(list 'quote val
)))))
138 "List associating function properties to their macro expansion.
139 Each element of the list takes the form (PROP FUN) where FUN is
140 a function. For each (PROP . VALUES) in a function's declaration,
141 the FUN corresponding to PROP is called with the function name,
142 the function's arglist, and the VALUES and should return the code to use
143 to set this property.
145 This is used by `declare'.")
147 (defvar macro-declarations-alist
150 #'(lambda (name _args spec
)
151 (list 'progn
:autoload-end
152 (list 'put
(list 'quote name
)
153 ''edebug-form-spec
(list 'quote spec
)))))
154 defun-declarations-alist
)
155 "List associating properties of macros to their macro expansion.
156 Each element of the list takes the form (PROP FUN) where FUN is a function.
157 For each (PROP . VALUES) in a macro's declaration, the FUN corresponding
158 to PROP is called with the macro name, the macro's arglist, and the VALUES
159 and should return the code to use to set this property.
161 This is used by `declare'.")
166 #'(lambda (name arglist
&optional docstring
&rest body
)
167 "Define NAME as a macro.
168 When the macro is called, as in (NAME ARGS...),
169 the function (lambda ARGLIST BODY...) is applied to
170 the list ARGS... as it appears in the expression,
171 and the result should be a form to be evaluated instead of the original.
172 DECL is a declaration, optional, of the form (declare DECLS...) where
173 DECLS is a list of elements of the form (PROP . VALUES). These are
174 interpreted according to `macro-declarations-alist'.
175 The return value is undefined.
177 \(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
178 ;; We can't just have `decl' as an &optional argument, because we need
180 ;; (defmacro foo (arg) (bar) nil)
182 ;; (defmacro foo (arg) (bar)).
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 ;; Can't use backquote because it's not defined yet!
192 (let* ((fun (list 'function
(cons 'lambda
(cons arglist body
))))
195 (list 'cons
''macro fun
)))
199 (let ((f (cdr (assq (car x
) macro-declarations-alist
))))
200 (if f
(apply (car f
) name arglist
(cdr x
))
201 (message "Warning: Unknown macro property %S in %S"
205 (cons 'prog1
(cons def declarations
))
208 ;; Now that we defined defmacro we can use it!
209 (defmacro defun
(name arglist
&optional docstring
&rest body
)
210 "Define NAME as a function.
211 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
212 See also the function `interactive'.
213 DECL is a declaration, optional, of the form (declare DECLS...) where
214 DECLS is a list of elements of the form (PROP . VALUES). These are
215 interpreted according to `defun-declarations-alist'.
216 The return value is undefined.
218 \(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
219 ;; We can't just have `decl' as an &optional argument, because we need
221 ;; (defun foo (arg) (toto) nil)
223 ;; (defun foo (arg) (toto)).
224 (declare (doc-string 3) (indent 2))
226 ((eq (car-safe docstring
) 'declare
)
227 (prog1 (cdr docstring
) (setq docstring nil
)))
228 ((and (stringp docstring
)
229 (eq (car-safe (car body
)) 'declare
))
230 (prog1 (cdr (car body
)) (setq body
(cdr body
)))))))
231 (if docstring
(setq body
(cons docstring body
))
232 (if (null body
) (setq body
'(nil))))
236 (let ((f (cdr (assq (car x
) defun-declarations-alist
))))
238 (f (apply (car f
) name arglist
(cdr x
)))
241 (memq (car x
) ;C.f. cl-do-proclaim.
242 '(special inline notinline optimize warn
)))
243 (push (list 'declare x
)
244 (if (stringp docstring
)
245 (if (eq (car-safe (cadr body
)) 'interactive
)
248 (if (eq (car-safe (car body
)) 'interactive
)
252 (t (message "Warning: Unknown defun property `%S' in %S"
259 (cons arglist body
))))))
261 (cons 'prog1
(cons def declarations
))
265 ;; Redefined in byte-optimize.el.
266 ;; This is not documented--it's not clear that we should promote it.
267 (fset 'inline
'progn
)
269 ;;; Interface to inline functions.
271 ;; (defmacro proclaim-inline (&rest fns)
272 ;; "Cause the named functions to be open-coded when called from compiled code.
273 ;; They will only be compiled open-coded when byte-compile-optimize is true."
274 ;; (cons 'eval-and-compile
275 ;; (mapcar (lambda (x)
276 ;; (or (memq (get x 'byte-optimizer)
277 ;; '(nil byte-compile-inline-expand))
279 ;; "%s already has a byte-optimizer, can't make it inline"
281 ;; (list 'put (list 'quote x)
282 ;; ''byte-optimizer ''byte-compile-inline-expand))
285 ;; (defmacro proclaim-notinline (&rest fns)
286 ;; "Cause the named functions to no longer be open-coded."
287 ;; (cons 'eval-and-compile
288 ;; (mapcar (lambda (x)
289 ;; (if (eq (get x 'byte-optimizer) 'byte-compile-inline-expand)
290 ;; (put x 'byte-optimizer nil))
291 ;; (list 'if (list 'eq (list 'get (list 'quote x) ''byte-optimizer)
292 ;; ''byte-compile-inline-expand)
293 ;; (list 'put x ''byte-optimizer nil)))
296 (defmacro defsubst
(name arglist
&rest body
)
297 "Define an inline function. The syntax is just like that of `defun'.
298 \(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
299 (declare (debug defun
) (doc-string 3))
300 (or (memq (get name
'byte-optimizer
)
301 '(nil byte-compile-inline-expand
))
302 (error "`%s' is a primitive" name
))
304 (defun ,name
,arglist
,@body
)
306 (put ',name
'byte-optimizer
'byte-compile-inline-expand
))))
308 (defvar advertised-signature-table
(make-hash-table :test
'eq
:weakness
'key
))
310 (defun set-advertised-calling-convention (function signature _when
)
311 "Set the advertised SIGNATURE of FUNCTION.
312 This will allow the byte-compiler to warn the programmer when she uses
313 an obsolete calling convention. WHEN specifies since when the calling
314 convention was modified."
315 (puthash (indirect-function function
) signature
316 advertised-signature-table
))
318 (defun make-obsolete (obsolete-name current-name
&optional when
)
319 "Make the byte-compiler warn that function OBSOLETE-NAME is obsolete.
320 OBSOLETE-NAME should be a function name or macro name (a symbol).
322 The warning will say that CURRENT-NAME should be used instead.
323 If CURRENT-NAME is a string, that is the `use instead' message
324 \(it should end with a period, and not start with a capital).
325 WHEN should be a string indicating when the function
326 was first made obsolete, for example a date or a release number."
327 (declare (advertised-calling-convention
328 ;; New code should always provide the `when' argument.
329 (obsolete-name current-name when
) "23.1"))
330 (put obsolete-name
'byte-obsolete-info
331 ;; The second entry used to hold the `byte-compile' handler, but
332 ;; is not used any more nowadays.
333 (purecopy (list current-name nil when
)))
336 (defmacro define-obsolete-function-alias
(obsolete-name current-name
337 &optional when docstring
)
338 "Set OBSOLETE-NAME's function definition to CURRENT-NAME and mark it obsolete.
340 \(define-obsolete-function-alias 'old-fun 'new-fun \"22.1\" \"old-fun's doc.\")
342 is equivalent to the following two lines of code:
344 \(defalias 'old-fun 'new-fun \"old-fun's doc.\")
345 \(make-obsolete 'old-fun 'new-fun \"22.1\")
347 See the docstrings of `defalias' and `make-obsolete' for more details."
348 (declare (doc-string 4)
349 (advertised-calling-convention
350 ;; New code should always provide the `when' argument.
351 (obsolete-name current-name when
&optional docstring
) "23.1"))
353 (defalias ,obsolete-name
,current-name
,docstring
)
354 (make-obsolete ,obsolete-name
,current-name
,when
)))
356 (defun make-obsolete-variable (obsolete-name current-name
&optional when access-type
)
357 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
358 The warning will say that CURRENT-NAME should be used instead.
359 If CURRENT-NAME is a string, that is the `use instead' message.
360 WHEN should be a string indicating when the variable
361 was first made obsolete, for example a date or a release number.
362 ACCESS-TYPE if non-nil should specify the kind of access that will trigger
363 obsolescence warnings; it can be either `get' or `set'."
364 (declare (advertised-calling-convention
365 ;; New code should always provide the `when' argument.
366 (obsolete-name current-name when
&optional access-type
) "23.1"))
367 (put obsolete-name
'byte-obsolete-variable
368 (purecopy (list current-name access-type when
)))
372 (defmacro define-obsolete-variable-alias
(obsolete-name current-name
373 &optional when docstring
)
374 "Make OBSOLETE-NAME a variable alias for CURRENT-NAME and mark it obsolete.
375 This uses `defvaralias' and `make-obsolete-variable' (which see).
376 See the Info node `(elisp)Variable Aliases' for more details.
378 If CURRENT-NAME is a defcustom (more generally, any variable
379 where OBSOLETE-NAME may be set, e.g. in an init file, before the
380 alias is defined), then the define-obsolete-variable-alias
381 statement should be evaluated before the defcustom, if user
382 customizations are to be respected. The simplest way to achieve
383 this is to place the alias statement before the defcustom (this
384 is not necessary for aliases that are autoloaded, or in files
385 dumped with Emacs). This is so that any user customizations are
386 applied before the defcustom tries to initialize the
387 variable (this is due to the way `defvaralias' works).
389 For the benefit of `custom-set-variables', if OBSOLETE-NAME has
390 any of the following properties, they are copied to
391 CURRENT-NAME, if it does not already have them:
392 'saved-value, 'saved-variable-comment."
393 (declare (doc-string 4)
394 (advertised-calling-convention
395 ;; New code should always provide the `when' argument.
396 (obsolete-name current-name when
&optional docstring
) "23.1"))
398 (defvaralias ,obsolete-name
,current-name
,docstring
)
400 (dolist (prop '(saved-value saved-variable-comment
))
401 (and (get ,obsolete-name prop
)
402 (null (get ,current-name prop
))
403 (put ,current-name prop
(get ,obsolete-name prop
))))
404 (make-obsolete-variable ,obsolete-name
,current-name
,when
)))
406 ;; FIXME This is only defined in this file because the variable- and
407 ;; function- versions are too. Unlike those two, this one is not used
408 ;; by the byte-compiler (would be nice if it could warn about obsolete
409 ;; faces, but it doesn't really do anything special with faces).
410 ;; It only really affects M-x describe-face output.
411 (defmacro define-obsolete-face-alias
(obsolete-face current-face when
)
412 "Make OBSOLETE-FACE a face alias for CURRENT-FACE and mark it obsolete.
413 The string WHEN gives the Emacs version where OBSOLETE-FACE became
416 (put ,obsolete-face
'face-alias
,current-face
)
417 ;; Used by M-x describe-face.
418 (put ,obsolete-face
'obsolete-face
(or (purecopy ,when
) t
))))
420 (defmacro dont-compile
(&rest body
)
421 "Like `progn', but the body always runs interpreted (not compiled).
422 If you think you need this, you're probably making a mistake somewhere."
423 (declare (debug t
) (indent 0) (obsolete nil
"24.4"))
424 (list 'eval
(list 'quote
(if (cdr body
) (cons 'progn body
) (car body
)))))
427 ;; interface to evaluating things at compile time and/or load time
428 ;; these macro must come after any uses of them in this file, as their
429 ;; definition in the file overrides the magic definitions on the
430 ;; byte-compile-macro-environment.
432 (defmacro eval-when-compile
(&rest body
)
433 "Like `progn', but evaluates the body at compile time if you're compiling.
434 Thus, the result of the body appears to the compiler as a quoted
435 constant. In interpreted code, this is entirely equivalent to
436 `progn', except that the value of the expression may be (but is
437 not necessarily) computed at load time if eager macro expansion
439 (declare (debug (&rest def-form
)) (indent 0))
440 (list 'quote
(eval (cons 'progn body
) lexical-binding
)))
442 (defmacro eval-and-compile
(&rest body
)
443 "Like `progn', but evaluates the body at compile time and at
444 load time. In interpreted code, this is entirely equivalent to
445 `progn', except that the value of the expression may be (but is
446 not necessarily) computed at load time if eager macro expansion
448 (declare (debug t
) (indent 0))
449 ;; When the byte-compiler expands code, this macro is not used, so we're
450 ;; either about to run `body' (plain interpretation) or we're doing eager
452 (list 'quote
(eval (cons 'progn body
) lexical-binding
)))
454 (defun with-no-warnings (&rest body
)
455 "Like `progn', but prevents compiler warnings in the body."
457 ;; The implementation for the interpreter is basically trivial.
461 ;; I nuked this because it's not a good idea for users to think of using it.
462 ;; These options are a matter of installation preference, and have nothing to
463 ;; with particular source files; it's a mistake to suggest to users
464 ;; they should associate these with particular source files.
465 ;; There is hardly any reason to change these parameters, anyway.
468 ;; (put 'byte-compiler-options 'lisp-indent-function 0)
469 ;; (defmacro byte-compiler-options (&rest args)
470 ;; "Set some compilation-parameters for this file. This will affect only the
471 ;; file in which it appears; this does nothing when evaluated, and when loaded
474 ;; Each argument to this macro must be a list of a key and a value.
476 ;; Keys: Values: Corresponding variable:
478 ;; verbose t, nil byte-compile-verbose
479 ;; optimize t, nil, source, byte byte-compile-optimize
480 ;; warnings list of warnings byte-compile-warnings
481 ;; Valid elements: (callargs redefine free-vars unresolved)
482 ;; file-format emacs18, emacs19 byte-compile-compatibility
484 ;; For example, this might appear at the top of a source file:
486 ;; (byte-compiler-options
488 ;; (warnings (- free-vars)) ; Don't warn about free variables
489 ;; (file-format emacs19))"
492 (make-obsolete-variable 'macro-declaration-function
493 'macro-declarations-alist
"24.3")
494 (make-obsolete 'macro-declaration-function
495 'macro-declarations-alist
"24.3")
497 ;;; byte-run.el ends here