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