]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/byte-run.el
Don’t create unnecessary marker in ‘delete-trailing-whitespace’
[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-2016 Free Software Foundation, Inc.
4
5 ;; Author: Jamie Zawinski <jwz@lucid.com>
6 ;; Hallvard Furuseth <hbf@ulrik.uio.no>
7 ;; Maintainer: emacs-devel@gnu.org
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 (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)
44
45 ;; `macro-declaration-function' are both obsolete (as marked at the end of this
46 ;; file) but used in many .elc files.
47
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.")
54
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.
63 (let (d)
64 ;; Ignore the first element of `decl' (it's always `declare').
65 (while (setq decl (cdr decl))
66 (setq d (car decl))
67 (if (and (consp d)
68 (listp (cdr d))
69 (null (cdr (cdr d))))
70 (cond ((eq (car d) 'indent)
71 (put macro 'lisp-indent-function (car (cdr d))))
72 ((eq (car d) 'debug)
73 (put macro 'edebug-form-spec (car (cdr d))))
74 ((eq (car d) 'doc-string)
75 (put macro 'doc-string-elt (car (cdr d))))
76 (t
77 (message "Unknown declaration %s" d)))
78 (message "Invalid declaration %s" d))))))
79
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.
83
84 ;; Add any new entries to info node `(elisp)Declare Form'.
85 (defvar defun-declarations-alist
86 (list
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))))
93 (list 'obsolete
94 #'(lambda (f _args new-name when)
95 (list 'make-obsolete
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'.
102 (list 'pure
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))
117 `(eval-and-compile
118 (function-put ',f 'compiler-macro #',compiler-function))
119 (let ((cfname (intern (concat (symbol-name f) "--anon-cmacro"))))
120 `(progn
121 (eval-and-compile
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,
125 ;; if needed.
126 :autoload-end
127 (eval-and-compile
128 (defun ,cfname (,@(cadr compiler-function) ,@args)
129 ,@(cddr compiler-function))))))))
130 (list 'doc-string
131 #'(lambda (f _args pos)
132 (list 'function-put (list 'quote f)
133 ''doc-string-elt (list 'quote pos))))
134 (list 'indent
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.
144
145 This is used by `declare'.")
146
147 (defvar macro-declarations-alist
148 (cons
149 (list 'debug
150 #'(lambda (name _args spec)
151 (list 'progn :autoload-end
152 (list 'put (list 'quote name)
153 ''edebug-form-spec (list 'quote spec)))))
154 (cons
155 (list 'no-font-lock-keyword
156 #'(lambda (name _args val)
157 (list 'function-put (list 'quote name)
158 ''no-font-lock-keyword (list 'quote val))))
159 defun-declarations-alist))
160 "List associating properties of macros to their macro expansion.
161 Each element of the list takes the form (PROP FUN) where FUN is a function.
162 For each (PROP . VALUES) in a macro's declaration, the FUN corresponding
163 to PROP is called with the macro name, the macro's arglist, and the VALUES
164 and should return the code to use to set this property.
165
166 This is used by `declare'.")
167
168 (defalias 'defmacro
169 (cons
170 'macro
171 #'(lambda (name arglist &optional docstring &rest body)
172 "Define NAME as a macro.
173 When the macro is called, as in (NAME ARGS...),
174 the function (lambda ARGLIST BODY...) is applied to
175 the list ARGS... as it appears in the expression,
176 and the result should be a form to be evaluated instead of the original.
177 DECL is a declaration, optional, of the form (declare DECLS...) where
178 DECLS is a list of elements of the form (PROP . VALUES). These are
179 interpreted according to `macro-declarations-alist'.
180 The return value is undefined.
181
182 \(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
183 ;; We can't just have `decl' as an &optional argument, because we need
184 ;; to distinguish
185 ;; (defmacro foo (arg) (bar) nil)
186 ;; from
187 ;; (defmacro foo (arg) (bar)).
188 (let ((decls (cond
189 ((eq (car-safe docstring) 'declare)
190 (prog1 (cdr docstring) (setq docstring nil)))
191 ((and (stringp docstring)
192 (eq (car-safe (car body)) 'declare))
193 (prog1 (cdr (car body)) (setq body (cdr body)))))))
194 (if docstring (setq body (cons docstring body))
195 (if (null body) (setq body '(nil))))
196 ;; Can't use backquote because it's not defined yet!
197 (let* ((fun (list 'function (cons 'lambda (cons arglist body))))
198 (def (list 'defalias
199 (list 'quote name)
200 (list 'cons ''macro fun)))
201 (declarations
202 (mapcar
203 #'(lambda (x)
204 (let ((f (cdr (assq (car x) macro-declarations-alist))))
205 (if f (apply (car f) name arglist (cdr x))
206 (message "Warning: Unknown macro property %S in %S"
207 (car x) name))))
208 decls)))
209 ;; Refresh font-lock if this is a new macro, or it is an
210 ;; existing macro whose 'no-font-lock-keyword declaration
211 ;; has changed.
212 (if (and
213 ;; If lisp-mode hasn't been loaded, there's no reason
214 ;; to flush.
215 (fboundp 'lisp--el-font-lock-flush-elisp-buffers)
216 (or (not (fboundp name)) ;; new macro
217 (and (fboundp name) ;; existing macro
218 (member `(function-put ',name 'no-font-lock-keyword
219 ',(get name 'no-font-lock-keyword))
220 declarations))))
221 (lisp--el-font-lock-flush-elisp-buffers))
222 (if declarations
223 (cons 'prog1 (cons def declarations))
224 def))))))
225
226 ;; Now that we defined defmacro we can use it!
227 (defmacro defun (name arglist &optional docstring &rest body)
228 "Define NAME as a function.
229 The definition is (lambda ARGLIST [DOCSTRING] BODY...).
230 See also the function `interactive'.
231 DECL is a declaration, optional, of the form (declare DECLS...) where
232 DECLS is a list of elements of the form (PROP . VALUES). These are
233 interpreted according to `defun-declarations-alist'.
234 The return value is undefined.
235
236 \(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
237 ;; We can't just have `decl' as an &optional argument, because we need
238 ;; to distinguish
239 ;; (defun foo (arg) (toto) nil)
240 ;; from
241 ;; (defun foo (arg) (toto)).
242 (declare (doc-string 3) (indent 2))
243 (let ((decls (cond
244 ((eq (car-safe docstring) 'declare)
245 (prog1 (cdr docstring) (setq docstring nil)))
246 ((and (stringp docstring)
247 (eq (car-safe (car body)) 'declare))
248 (prog1 (cdr (car body)) (setq body (cdr body)))))))
249 (if docstring (setq body (cons docstring body))
250 (if (null body) (setq body '(nil))))
251 (let ((declarations
252 (mapcar
253 #'(lambda (x)
254 (let ((f (cdr (assq (car x) defun-declarations-alist))))
255 (cond
256 (f (apply (car f) name arglist (cdr x)))
257 ;; Yuck!!
258 ((and (featurep 'cl)
259 (memq (car x) ;C.f. cl-do-proclaim.
260 '(special inline notinline optimize warn)))
261 (push (list 'declare x)
262 (if (stringp docstring)
263 (if (eq (car-safe (cadr body)) 'interactive)
264 (cddr body)
265 (cdr body))
266 (if (eq (car-safe (car body)) 'interactive)
267 (cdr body)
268 body)))
269 nil)
270 (t (message "Warning: Unknown defun property `%S' in %S"
271 (car x) name)))))
272 decls))
273 (def (list 'defalias
274 (list 'quote name)
275 (list 'function
276 (cons 'lambda
277 (cons arglist body))))))
278 (if declarations
279 (cons 'prog1 (cons def declarations))
280 def))))
281
282 \f
283 ;; Redefined in byte-optimize.el.
284 ;; This is not documented--it's not clear that we should promote it.
285 (fset 'inline 'progn)
286
287 ;;; Interface to inline functions.
288
289 ;; (defmacro proclaim-inline (&rest fns)
290 ;; "Cause the named functions to be open-coded when called from compiled code.
291 ;; They will only be compiled open-coded when byte-compile-optimize is true."
292 ;; (cons 'eval-and-compile
293 ;; (mapcar (lambda (x)
294 ;; (or (memq (get x 'byte-optimizer)
295 ;; '(nil byte-compile-inline-expand))
296 ;; (error
297 ;; "%s already has a byte-optimizer, can't make it inline"
298 ;; x))
299 ;; (list 'put (list 'quote x)
300 ;; ''byte-optimizer ''byte-compile-inline-expand))
301 ;; fns)))
302
303 ;; (defmacro proclaim-notinline (&rest fns)
304 ;; "Cause the named functions to no longer be open-coded."
305 ;; (cons 'eval-and-compile
306 ;; (mapcar (lambda (x)
307 ;; (if (eq (get x 'byte-optimizer) 'byte-compile-inline-expand)
308 ;; (put x 'byte-optimizer nil))
309 ;; (list 'if (list 'eq (list 'get (list 'quote x) ''byte-optimizer)
310 ;; ''byte-compile-inline-expand)
311 ;; (list 'put x ''byte-optimizer nil)))
312 ;; fns)))
313
314 (defmacro defsubst (name arglist &rest body)
315 "Define an inline function. The syntax is just like that of `defun'.
316 \(fn NAME ARGLIST &optional DOCSTRING DECL &rest BODY)"
317 (declare (debug defun) (doc-string 3))
318 (or (memq (get name 'byte-optimizer)
319 '(nil byte-compile-inline-expand))
320 (error "`%s' is a primitive" name))
321 `(prog1
322 (defun ,name ,arglist ,@body)
323 (eval-and-compile
324 (put ',name 'byte-optimizer 'byte-compile-inline-expand))))
325
326 (defvar advertised-signature-table (make-hash-table :test 'eq :weakness 'key))
327
328 (defun set-advertised-calling-convention (function signature _when)
329 "Set the advertised SIGNATURE of FUNCTION.
330 This will allow the byte-compiler to warn the programmer when she uses
331 an obsolete calling convention. WHEN specifies since when the calling
332 convention was modified."
333 (puthash (indirect-function function) signature
334 advertised-signature-table))
335
336 (defun make-obsolete (obsolete-name current-name &optional when)
337 "Make the byte-compiler warn that function OBSOLETE-NAME is obsolete.
338 OBSOLETE-NAME should be a function name or macro name (a symbol).
339
340 The warning will say that CURRENT-NAME should be used instead.
341 If CURRENT-NAME is a string, that is the `use instead' message
342 \(it should end with a period, and not start with a capital).
343 WHEN should be a string indicating when the function
344 was first made obsolete, for example a date or a release number."
345 (declare (advertised-calling-convention
346 ;; New code should always provide the `when' argument.
347 (obsolete-name current-name when) "23.1"))
348 (put obsolete-name 'byte-obsolete-info
349 ;; The second entry used to hold the `byte-compile' handler, but
350 ;; is not used any more nowadays.
351 (purecopy (list current-name nil when)))
352 obsolete-name)
353
354 (defmacro define-obsolete-function-alias (obsolete-name current-name
355 &optional when docstring)
356 "Set OBSOLETE-NAME's function definition to CURRENT-NAME and mark it obsolete.
357
358 \(define-obsolete-function-alias \\='old-fun \\='new-fun \"22.1\" \"old-fun's doc.\")
359
360 is equivalent to the following two lines of code:
361
362 \(defalias \\='old-fun \\='new-fun \"old-fun's doc.\")
363 \(make-obsolete \\='old-fun \\='new-fun \"22.1\")
364
365 If provided, WHEN should be a string indicating when the function
366 was first made obsolete, for example a date or a release number.
367
368 See the docstrings of `defalias' and `make-obsolete' for more details."
369 (declare (doc-string 4)
370 (advertised-calling-convention
371 ;; New code should always provide the `when' argument.
372 (obsolete-name current-name when &optional docstring) "23.1"))
373 `(progn
374 (defalias ,obsolete-name ,current-name ,docstring)
375 (make-obsolete ,obsolete-name ,current-name ,when)))
376
377 (defun make-obsolete-variable (obsolete-name current-name &optional when access-type)
378 "Make the byte-compiler warn that OBSOLETE-NAME is obsolete.
379 The warning will say that CURRENT-NAME should be used instead.
380 If CURRENT-NAME is a string, that is the `use instead' message.
381 WHEN should be a string indicating when the variable
382 was first made obsolete, for example a date or a release number.
383 ACCESS-TYPE if non-nil should specify the kind of access that will trigger
384 obsolescence warnings; it can be either `get' or `set'."
385 (declare (advertised-calling-convention
386 ;; New code should always provide the `when' argument.
387 (obsolete-name current-name when &optional access-type) "23.1"))
388 (put obsolete-name 'byte-obsolete-variable
389 (purecopy (list current-name access-type when)))
390 obsolete-name)
391
392
393 (defmacro define-obsolete-variable-alias (obsolete-name current-name
394 &optional when docstring)
395 "Make OBSOLETE-NAME a variable alias for CURRENT-NAME and mark it obsolete.
396 This uses `defvaralias' and `make-obsolete-variable' (which see).
397 See the Info node `(elisp)Variable Aliases' for more details.
398
399 If CURRENT-NAME is a defcustom or a defvar (more generally, any variable
400 where OBSOLETE-NAME may be set, e.g. in an init file, before the
401 alias is defined), then the define-obsolete-variable-alias
402 statement should be evaluated before the defcustom, if user
403 customizations are to be respected. The simplest way to achieve
404 this is to place the alias statement before the defcustom (this
405 is not necessary for aliases that are autoloaded, or in files
406 dumped with Emacs). This is so that any user customizations are
407 applied before the defcustom tries to initialize the
408 variable (this is due to the way `defvaralias' works).
409
410 If provided, WHEN should be a string indicating when the variable
411 was first made obsolete, for example a date or a release number.
412
413 For the benefit of `custom-set-variables', if OBSOLETE-NAME has
414 any of the following properties, they are copied to
415 CURRENT-NAME, if it does not already have them:
416 `saved-value', `saved-variable-comment'."
417 (declare (doc-string 4)
418 (advertised-calling-convention
419 ;; New code should always provide the `when' argument.
420 (obsolete-name current-name when &optional docstring) "23.1"))
421 `(progn
422 (defvaralias ,obsolete-name ,current-name ,docstring)
423 ;; See Bug#4706.
424 (dolist (prop '(saved-value saved-variable-comment))
425 (and (get ,obsolete-name prop)
426 (null (get ,current-name prop))
427 (put ,current-name prop (get ,obsolete-name prop))))
428 (make-obsolete-variable ,obsolete-name ,current-name ,when)))
429
430 ;; FIXME This is only defined in this file because the variable- and
431 ;; function- versions are too. Unlike those two, this one is not used
432 ;; by the byte-compiler (would be nice if it could warn about obsolete
433 ;; faces, but it doesn't really do anything special with faces).
434 ;; It only really affects M-x describe-face output.
435 (defmacro define-obsolete-face-alias (obsolete-face current-face when)
436 "Make OBSOLETE-FACE a face alias for CURRENT-FACE and mark it obsolete.
437 If provided, WHEN should be a string indicating when the face
438 was first made obsolete, for example a date or a release number."
439 `(progn
440 (put ,obsolete-face 'face-alias ,current-face)
441 ;; Used by M-x describe-face.
442 (put ,obsolete-face 'obsolete-face (or (purecopy ,when) t))))
443
444 (defmacro dont-compile (&rest body)
445 "Like `progn', but the body always runs interpreted (not compiled).
446 If you think you need this, you're probably making a mistake somewhere."
447 (declare (debug t) (indent 0) (obsolete nil "24.4"))
448 (list 'eval (list 'quote (if (cdr body) (cons 'progn body) (car body)))))
449
450 \f
451 ;; interface to evaluating things at compile time and/or load time
452 ;; these macro must come after any uses of them in this file, as their
453 ;; definition in the file overrides the magic definitions on the
454 ;; byte-compile-macro-environment.
455
456 (defmacro eval-when-compile (&rest body)
457 "Like `progn', but evaluates the body at compile time if you're compiling.
458 Thus, the result of the body appears to the compiler as a quoted
459 constant. In interpreted code, this is entirely equivalent to
460 `progn', except that the value of the expression may be (but is
461 not necessarily) computed at load time if eager macro expansion
462 is enabled."
463 (declare (debug (&rest def-form)) (indent 0))
464 (list 'quote (eval (cons 'progn body) lexical-binding)))
465
466 (defmacro eval-and-compile (&rest body)
467 "Like `progn', but evaluates the body at compile time and at
468 load time. In interpreted code, this is entirely equivalent to
469 `progn', except that the value of the expression may be (but is
470 not necessarily) computed at load time if eager macro expansion
471 is enabled."
472 (declare (debug t) (indent 0))
473 ;; When the byte-compiler expands code, this macro is not used, so we're
474 ;; either about to run `body' (plain interpretation) or we're doing eager
475 ;; macroexpansion.
476 (list 'quote (eval (cons 'progn body) lexical-binding)))
477
478 (defun with-no-warnings (&rest body)
479 "Like `progn', but prevents compiler warnings in the body."
480 (declare (indent 0))
481 ;; The implementation for the interpreter is basically trivial.
482 (car (last body)))
483
484 \f
485 ;; I nuked this because it's not a good idea for users to think of using it.
486 ;; These options are a matter of installation preference, and have nothing to
487 ;; with particular source files; it's a mistake to suggest to users
488 ;; they should associate these with particular source files.
489 ;; There is hardly any reason to change these parameters, anyway.
490 ;; --rms.
491
492 ;; (put 'byte-compiler-options 'lisp-indent-function 0)
493 ;; (defmacro byte-compiler-options (&rest args)
494 ;; "Set some compilation-parameters for this file. This will affect only the
495 ;; file in which it appears; this does nothing when evaluated, and when loaded
496 ;; from a .el file.
497 ;;
498 ;; Each argument to this macro must be a list of a key and a value.
499 ;;
500 ;; Keys: Values: Corresponding variable:
501 ;;
502 ;; verbose t, nil byte-compile-verbose
503 ;; optimize t, nil, source, byte byte-compile-optimize
504 ;; warnings list of warnings byte-compile-warnings
505 ;; Valid elements: (callargs redefine free-vars unresolved)
506 ;; file-format emacs18, emacs19 byte-compile-compatibility
507 ;;
508 ;; For example, this might appear at the top of a source file:
509 ;;
510 ;; (byte-compiler-options
511 ;; (optimize t)
512 ;; (warnings (- free-vars)) ; Don't warn about free variables
513 ;; (file-format emacs19))"
514 ;; nil)
515
516 (make-obsolete-variable 'macro-declaration-function
517 'macro-declarations-alist "24.3")
518 (make-obsolete 'macro-declaration-function
519 'macro-declarations-alist "24.3")
520
521 ;;; byte-run.el ends here