Add "Incompatible Lisp Changes in Emacs 23.2".
[bpt/emacs.git] / lisp / emacs-lisp / bytecomp.el
CommitLineData
55535639 1;;; bytecomp.el --- compilation of Lisp code into byte code
fd5285f3 2
977b50fb 3;; Copyright (C) 1985, 1986, 1987, 1992, 1994, 1998, 2000, 2001, 2002,
ae940284 4;; 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
3a801d0c 5
fd5285f3
RS
6;; Author: Jamie Zawinski <jwz@lucid.com>
7;; Hallvard Furuseth <hbf@ulrik.uio.no>
74dfd056 8;; Maintainer: FSF
713ea1de 9;; Keywords: lisp
1c393159 10
1c393159
JB
11;; This file is part of GNU Emacs.
12
d6cba7ae 13;; GNU Emacs is free software: you can redistribute it and/or modify
1c393159 14;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
1c393159
JB
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
d6cba7ae 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
1c393159 25
e41b2db1
ER
26;;; Commentary:
27
28;; The Emacs Lisp byte compiler. This crunches lisp source into a sort
a586093f
SM
29;; of p-code (`lapcode') which takes up less space and can be interpreted
30;; faster. [`LAP' == `Lisp Assembly Program'.]
e41b2db1
ER
31;; The user entry points are byte-compile-file and byte-recompile-directory.
32
fd5285f3
RS
33;;; Code:
34
b578f267
EN
35;; ========================================================================
36;; Entry points:
37;; byte-recompile-directory, byte-compile-file,
38;; batch-byte-compile, batch-byte-recompile-directory,
39;; byte-compile, compile-defun,
40;; display-call-tree
41;; (byte-compile-buffer and byte-compile-and-load-file were turned off
42;; because they are not terribly useful and get in the way of completion.)
43
44;; This version of the byte compiler has the following improvements:
45;; + optimization of compiled code:
46;; - removal of unreachable code;
47;; - removal of calls to side-effectless functions whose return-value
48;; is unused;
49;; - compile-time evaluation of safe constant forms, such as (consp nil)
50;; and (ash 1 6);
51;; - open-coding of literal lambdas;
52;; - peephole optimization of emitted code;
53;; - trivial functions are left uncompiled for speed.
54;; + support for inline functions;
55;; + compile-time evaluation of arbitrary expressions;
56;; + compile-time warning messages for:
57;; - functions being redefined with incompatible arglists;
58;; - functions being redefined as macros, or vice-versa;
59;; - functions or macros defined multiple times in the same file;
60;; - functions being called with the incorrect number of arguments;
c5091f25 61;; - functions being called which are not defined globally, in the
b578f267
EN
62;; file, or as autoloads;
63;; - assignment and reference of undeclared free variables;
64;; - various syntax errors;
65;; + correct compilation of nested defuns, defmacros, defvars and defsubsts;
66;; + correct compilation of top-level uses of macros;
67;; + the ability to generate a histogram of functions called.
68
416d3588 69;; User customization variables: M-x customize-group bytecomp
b578f267
EN
70
71;; New Features:
72;;
73;; o The form `defsubst' is just like `defun', except that the function
74;; generated will be open-coded in compiled code which uses it. This
75;; means that no function call will be generated, it will simply be
76;; spliced in. Lisp functions calls are very slow, so this can be a
77;; big win.
78;;
79;; You can generally accomplish the same thing with `defmacro', but in
80;; that case, the defined procedure can't be used as an argument to
81;; mapcar, etc.
82;;
83;; o You can also open-code one particular call to a function without
84;; open-coding all calls. Use the 'inline' form to do this, like so:
85;;
86;; (inline (foo 1 2 3)) ;; `foo' will be open-coded
87;; or...
c5091f25 88;; (inline ;; `foo' and `baz' will be
b578f267
EN
89;; (foo 1 2 3 (bar 5)) ;; open-coded, but `bar' will not.
90;; (baz 0))
91;;
92;; o It is possible to open-code a function in the same file it is defined
6b61353c 93;; in without having to load that file before compiling it. The
b578f267
EN
94;; byte-compiler has been modified to remember function definitions in
95;; the compilation environment in the same way that it remembers macro
96;; definitions.
97;;
98;; o Forms like ((lambda ...) ...) are open-coded.
99;;
100;; o The form `eval-when-compile' is like progn, except that the body
101;; is evaluated at compile-time. When it appears at top-level, this
102;; is analogous to the Common Lisp idiom (eval-when (compile) ...).
103;; When it does not appear at top-level, it is similar to the
104;; Common Lisp #. reader macro (but not in interpreted code).
105;;
106;; o The form `eval-and-compile' is similar to eval-when-compile, but
107;; the whole form is evalled both at compile-time and at run-time.
108;;
109;; o The command compile-defun is analogous to eval-defun.
110;;
c5091f25 111;; o If you run byte-compile-file on a filename which is visited in a
b578f267
EN
112;; buffer, and that buffer is modified, you are asked whether you want
113;; to save the buffer before compiling.
114;;
115;; o byte-compiled files now start with the string `;ELC'.
116;; Some versions of `file' can be customized to recognize that.
1c393159 117
79d52eea 118(require 'backquote)
14acf2f5 119(eval-when-compile (require 'cl))
79d52eea 120
1c393159
JB
121(or (fboundp 'defsubst)
122 ;; This really ought to be loaded already!
6c2161c4 123 (load "byte-run"))
1c393159 124
3614fc84
GM
125;; The feature of compiling in a specific target Emacs version
126;; has been turned off because compile time options are a bad idea.
52799cb8 127(defmacro byte-compile-single-version () nil)
1c393159 128
3614fc84
GM
129;; The crud you see scattered through this file of the form
130;; (or (and (boundp 'epoch::version) epoch::version)
131;; (string-lessp emacs-version "19"))
132;; is because the Epoch folks couldn't be bothered to follow the
133;; normal emacs version numbering convention.
1c393159 134
52799cb8
RS
135;; (if (byte-compile-version-cond
136;; (or (and (boundp 'epoch::version) epoch::version)
137;; (string-lessp emacs-version "19")))
138;; (progn
139;; ;; emacs-18 compatibility.
140;; (defvar baud-rate (baud-rate)) ;Define baud-rate if it's undefined
1f006824 141;;
52799cb8 142;; (if (byte-compile-single-version)
ed015bdd
JB
143;; (defmacro byte-code-function-p (x) "Emacs 18 doesn't have these." nil)
144;; (defun byte-code-function-p (x) "Emacs 18 doesn't have these." nil))
1f006824 145;;
52799cb8
RS
146;; (or (and (fboundp 'member)
147;; ;; avoid using someone else's possibly bogus definition of this.
148;; (subrp (symbol-function 'member)))
149;; (defun member (elt list)
150;; "like memq, but uses equal instead of eq. In v19, this is a subr."
151;; (while (and list (not (equal elt (car list))))
152;; (setq list (cdr list)))
153;; list))))
154
155
713ea1de 156(defgroup bytecomp nil
25d1fc94 157 "Emacs Lisp byte-compiler."
713ea1de
RS
158 :group 'lisp)
159
5692cc8c 160(defcustom emacs-lisp-file-regexp "\\.el\\'"
2b9c3b12 161 "Regexp which matches Emacs Lisp source files.
3f12e5bd 162If you change this, you might want to set `byte-compile-dest-file-function'."
713ea1de
RS
163 :group 'bytecomp
164 :type 'regexp)
1c393159 165
3f12e5bd
GM
166(defcustom byte-compile-dest-file-function nil
167 "Function for the function `byte-compile-dest-file' to call.
168It should take one argument, the name of an Emacs Lisp source
169file name, and return the name of the compiled file."
170 :group 'bytecomp
171 :type '(choice (const nil) function)
172 :version "23.2")
173
2140206e
RS
174;; This enables file name handlers such as jka-compr
175;; to remove parts of the file name that should not be copied
176;; through to the output file name.
177(defun byte-compiler-base-file-name (filename)
178 (let ((handler (find-file-name-handler filename
179 'byte-compiler-base-file-name)))
180 (if handler
181 (funcall handler 'byte-compiler-base-file-name filename)
182 filename)))
183
1c393159 184(or (fboundp 'byte-compile-dest-file)
e27c3564 185 ;; The user may want to redefine this along with emacs-lisp-file-regexp,
1c393159 186 ;; so only define it if it is undefined.
3f12e5bd
GM
187 ;; Note - redefining this function is obsolete as of 23.2.
188 ;; Customize byte-compile-dest-file-function instead.
1c393159 189 (defun byte-compile-dest-file (filename)
f9b4b5d8 190 "Convert an Emacs Lisp source file name to a compiled file name.
3f12e5bd
GM
191If `byte-compile-dest-file-function' is non-nil, uses that
192function to do the work. Otherwise, if FILENAME matches
193`emacs-lisp-file-regexp' (by default, files with the extension `.el'),
194adds `c' to it; otherwise adds `.elc'."
195 (if byte-compile-dest-file-function
196 (funcall byte-compile-dest-file-function filename)
197 (setq filename (file-name-sans-versions
198 (byte-compiler-base-file-name filename)))
199 (cond ((string-match emacs-lisp-file-regexp filename)
200 (concat (substring filename 0 (match-beginning 0)) ".elc"))
201 (t (concat filename ".elc"))))))
1c393159
JB
202
203;; This can be the 'byte-compile property of any symbol.
52799cb8 204(autoload 'byte-compile-inline-expand "byte-opt")
1c393159
JB
205
206;; This is the entrypoint to the lapcode optimizer pass1.
52799cb8 207(autoload 'byte-optimize-form "byte-opt")
1c393159 208;; This is the entrypoint to the lapcode optimizer pass2.
52799cb8
RS
209(autoload 'byte-optimize-lapcode "byte-opt")
210(autoload 'byte-compile-unfold-lambda "byte-opt")
1c393159 211
ed015bdd
JB
212;; This is the entry point to the decompiler, which is used by the
213;; disassembler. The disassembler just requires 'byte-compile, but
214;; that doesn't define this function, so this seems to be a reasonable
215;; thing to do.
216(autoload 'byte-decompile-bytecode "byte-opt")
217
713ea1de 218(defcustom byte-compile-verbose
1c393159 219 (and (not noninteractive) (> baud-rate search-slow-speed))
2b9c3b12 220 "Non-nil means print messages describing progress of byte-compiler."
713ea1de
RS
221 :group 'bytecomp
222 :type 'boolean)
1c393159 223
52799cb8
RS
224;; (defvar byte-compile-generate-emacs19-bytecodes
225;; (not (or (and (boundp 'epoch::version) epoch::version)
226;; (string-lessp emacs-version "19")))
c5091f25 227;; "*If this is true, then the byte-compiler will generate bytecode which
52799cb8
RS
228;; makes use of byte-ops which are present only in Emacs 19. Code generated
229;; this way can never be run in Emacs 18, and may even cause it to crash.")
1c393159 230
713ea1de 231(defcustom byte-optimize t
2b9c3b12 232 "Enable optimization in the byte compiler.
9bb2e9f8
JB
233Possible values are:
234 nil - no optimization
235 t - all optimizations
236 `source' - source-level optimizations only
237 `byte' - code-level optimizations only"
713ea1de
RS
238 :group 'bytecomp
239 :type '(choice (const :tag "none" nil)
240 (const :tag "all" t)
241 (const :tag "source-level" source)
242 (const :tag "byte-level" byte)))
243
cd91e34c 244(defcustom byte-compile-delete-errors nil
2b9c3b12 245 "If non-nil, the optimizer may delete forms that may signal an error.
713ea1de
RS
246This includes variable references and calls to functions such as `car'."
247 :group 'bytecomp
248 :type 'boolean)
1c393159 249
d82e848c 250(defvar byte-compile-dynamic nil
713ea1de 251 "If non-nil, compile function bodies so they load lazily.
458f70dc
RS
252They are hidden in comments in the compiled file,
253and each one is brought into core when the
d82e848c
RS
254function is called.
255
256To enable this option, make it a file-local variable
257in the source file you want it to apply to.
258For example, add -*-byte-compile-dynamic: t;-*- on the first line.
259
260When this option is true, if you load the compiled file and then move it,
261the functions you loaded will not be able to run.")
631c8020 262;;;###autoload(put 'byte-compile-dynamic 'safe-local-variable 'booleanp)
d82e848c 263
0e66b003
KH
264(defvar byte-compile-disable-print-circle nil
265 "If non-nil, disable `print-circle' on printing a byte-compiled code.")
266;;;###autoload(put 'byte-compile-disable-print-circle 'safe-local-variable 'booleanp)
267
713ea1de 268(defcustom byte-compile-dynamic-docstrings t
2b9c3b12
JB
269 "If non-nil, compile doc strings for lazy access.
270We bury the doc strings of functions and variables inside comments in
271the file, and bring them into core only when they are actually needed.
d82e848c
RS
272
273When this option is true, if you load the compiled file and then move it,
274you won't be able to find the documentation of anything in that file.
275
1c660f5a
KH
276To disable this option for a certain file, make it a file-local variable
277in the source file. For example, add this to the first line:
278 -*-byte-compile-dynamic-docstrings:nil;-*-
279You can also set the variable globally.
280
713ea1de
RS
281This option is enabled by default because it reduces Emacs memory usage."
282 :group 'bytecomp
283 :type 'boolean)
631c8020 284;;;###autoload(put 'byte-compile-dynamic-docstrings 'safe-local-variable 'booleanp)
d82e848c 285
713ea1de 286(defcustom byte-optimize-log nil
2b9c3b12 287 "If true, the byte-compiler will log its optimizations into *Compile-Log*.
1c393159 288If this is 'source, then only source-level optimizations will be logged.
713ea1de
RS
289If it is 'byte, then only byte-level optimizations will be logged."
290 :group 'bytecomp
291 :type '(choice (const :tag "none" nil)
292 (const :tag "all" t)
293 (const :tag "source-level" source)
294 (const :tag "byte-level" byte)))
295
296(defcustom byte-compile-error-on-warn nil
2b9c3b12 297 "If true, the byte-compiler reports warnings with `error'."
713ea1de
RS
298 :group 'bytecomp
299 :type 'boolean)
1c393159 300
9290191f 301(defconst byte-compile-warning-types
086af77c 302 '(redefine callargs free-vars unresolved
8accceac 303 obsolete noruntime cl-functions interactive-only
416d3588 304 make-local mapcar constants)
4795d1c7 305 "The list of warning types used when `byte-compile-warnings' is t.")
713ea1de 306(defcustom byte-compile-warnings t
2b9c3b12 307 "List of warnings that the byte-compiler should issue (t for all).
4795d1c7 308
9bb2e9f8 309Elements of the list may be:
9e2b097b
JB
310
311 free-vars references to variables not in the current lexical scope.
312 unresolved calls to unknown functions.
11efeb9b
RS
313 callargs function calls with args that don't match the definition.
314 redefine function name redefined from a macro to ordinary function or vice
9e2b097b 315 versa, or redefined to take a different number of arguments.
4795d1c7
RS
316 obsolete obsolete variables and functions.
317 noruntime functions that may not be defined at runtime (typically
318 defined only under `eval-when-compile').
6b8c2efc 319 cl-functions calls to runtime functions from the CL package (as
086af77c
RS
320 distinguished from macros and aliases).
321 interactive-only
15ce9dcf 322 commands that normally shouldn't be called from Lisp code.
86da2828 323 make-local calls to make-variable-buffer-local that may be incorrect.
cf637a34 324 mapcar mapcar called for effect.
416d3588 325 constants let-binding of, or assignment to, constants/nonvariables.
cf637a34
GM
326
327If the list begins with `not', then the remaining elements specify warnings to
328suppress. For example, (not mapcar) will suppress warnings about mapcar."
713ea1de 329 :group 'bytecomp
4795d1c7 330 :type `(choice (const :tag "All" t)
aa635691
AS
331 (set :menu-tag "Some"
332 (const free-vars) (const unresolved)
4795d1c7 333 (const callargs) (const redefine)
086af77c 334 (const obsolete) (const noruntime)
15ce9dcf 335 (const cl-functions) (const interactive-only)
416d3588 336 (const make-local) (const mapcar) (const constants))))
6a831405
GM
337;;;###autoload(put 'byte-compile-warnings 'safe-local-variable 'byte-compile-warnings-safe-p)
338
0027258d
RS
339;;;###autoload
340(defun byte-compile-warnings-safe-p (x)
daedb196 341 "Return non-nil if X is valid as a value of `byte-compile-warnings'."
0027258d
RS
342 (or (booleanp x)
343 (and (listp x)
cf637a34
GM
344 (if (eq (car x) 'not) (setq x (cdr x))
345 t)
0027258d
RS
346 (equal (mapcar
347 (lambda (e)
daedb196 348 (when (memq e byte-compile-warning-types)
0027258d
RS
349 e))
350 x)
351 x))))
086af77c 352
cf637a34
GM
353(defun byte-compile-warning-enabled-p (warning)
354 "Return non-nil if WARNING is enabled, according to `byte-compile-warnings'."
355 (or (eq byte-compile-warnings t)
356 (if (eq (car byte-compile-warnings) 'not)
357 (not (memq warning byte-compile-warnings))
358 (memq warning byte-compile-warnings))))
359
360;;;###autoload
361(defun byte-compile-disable-warning (warning)
362 "Change `byte-compile-warnings' to disable WARNING.
363If `byte-compile-warnings' is t, set it to `(not WARNING)'.
798bd437
GM
364Otherwise, if the first element is `not', add WARNING, else remove it.
365Normally you should let-bind `byte-compile-warnings' before calling this,
366else the global value will be modified."
cf637a34
GM
367 (setq byte-compile-warnings
368 (cond ((eq byte-compile-warnings t)
369 (list 'not warning))
370 ((eq (car byte-compile-warnings) 'not)
371 (if (memq warning byte-compile-warnings)
372 byte-compile-warnings
373 (append byte-compile-warnings (list warning))))
374 (t
375 (delq warning byte-compile-warnings)))))
376
377;;;###autoload
378(defun byte-compile-enable-warning (warning)
379 "Change `byte-compile-warnings' to enable WARNING.
380If `byte-compile-warnings' is `t', do nothing. Otherwise, if the
798bd437
GM
381first element is `not', remove WARNING, else add it.
382Normally you should let-bind `byte-compile-warnings' before calling this,
383else the global value will be modified."
cf637a34
GM
384 (or (eq byte-compile-warnings t)
385 (setq byte-compile-warnings
386 (cond ((eq (car byte-compile-warnings) 'not)
387 (delq warning byte-compile-warnings))
388 ((memq warning byte-compile-warnings)
389 byte-compile-warnings)
390 (t
391 (append byte-compile-warnings (list warning)))))))
392
086af77c
RS
393(defvar byte-compile-interactive-only-functions
394 '(beginning-of-buffer end-of-buffer replace-string replace-regexp
dfd4e693 395 insert-file insert-buffer insert-file-literally previous-line next-line
6444d64a 396 goto-line comint-run)
086af77c 397 "List of commands that are not meant to be called from Lisp.")
1c393159 398
8480fc7c
GM
399(defvar byte-compile-not-obsolete-vars nil
400 "If non-nil, a list of variables that shouldn't be reported as obsolete.")
401
402(defvar byte-compile-not-obsolete-funcs nil
403 "If non-nil, a list of functions that shouldn't be reported as obsolete.")
6b61353c 404
713ea1de 405(defcustom byte-compile-generate-call-tree nil
2b9c3b12 406 "Non-nil means collect call-graph information when compiling.
78bba1c8 407This records which functions were called and from where.
52799cb8
RS
408If the value is t, compilation displays the call graph when it finishes.
409If the value is neither t nor nil, compilation asks you whether to display
410the graph.
1c393159
JB
411
412The call tree only lists functions called, not macros used. Those functions
413which the byte-code interpreter knows about directly (eq, cons, etc.) are
414not reported.
415
416The call tree also lists those functions which are not known to be called
5023d9a0 417\(that is, to which no calls have been compiled). Functions which can be
713ea1de
RS
418invoked interactively are excluded from this list."
419 :group 'bytecomp
420 :type '(choice (const :tag "Yes" t) (const :tag "No" nil)
778c7576 421 (other :tag "Ask" lambda)))
1c393159 422
2b9c3b12
JB
423(defvar byte-compile-call-tree nil
424 "Alist of functions and their call tree.
1c393159
JB
425Each element looks like
426
427 \(FUNCTION CALLERS CALLS\)
428
429where CALLERS is a list of functions that call FUNCTION, and CALLS
430is a list of functions for which calls were generated while compiling
431FUNCTION.")
432
713ea1de 433(defcustom byte-compile-call-tree-sort 'name
2b9c3b12 434 "If non-nil, sort the call tree.
52799cb8 435The values `name', `callers', `calls', `calls+callers'
713ea1de
RS
436specify different fields to sort on."
437 :group 'bytecomp
438 :type '(choice (const name) (const callers) (const calls)
439 (const calls+callers) (const nil)))
52799cb8 440
ccb3c8de
CW
441(defvar byte-compile-debug nil)
442
52799cb8
RS
443;; (defvar byte-compile-overwrite-file t
444;; "If nil, old .elc files are deleted before the new is saved, and .elc
445;; files will have the same modes as the corresponding .el file. Otherwise,
446;; existing .elc files will simply be overwritten, and the existing modes
c5091f25 447;; will not be changed. If this variable is nil, then an .elc file which
52799cb8
RS
448;; is a symbolic link will be turned into a normal file, instead of the file
449;; which the link points to being overwritten.")
1c393159
JB
450
451(defvar byte-compile-constants nil
a586093f 452 "List of all constants encountered during compilation of this form.")
1c393159 453(defvar byte-compile-variables nil
a586093f 454 "List of all variables encountered during compilation of this form.")
1c393159 455(defvar byte-compile-bound-variables nil
b92dd692
DL
456 "List of variables bound in the context of the current form.
457This list lives partly on the stack.")
6c2161c4
SM
458(defvar byte-compile-const-variables nil
459 "List of variables declared as constants during compilation of this file.")
1c393159
JB
460(defvar byte-compile-free-references)
461(defvar byte-compile-free-assignments)
462
ab94e6e7
RS
463(defvar byte-compiler-error-flag)
464
1c393159 465(defconst byte-compile-initial-macro-environment
52799cb8
RS
466 '(
467;; (byte-compiler-options . (lambda (&rest forms)
468;; (apply 'byte-compiler-options-handler forms)))
1c393159 469 (eval-when-compile . (lambda (&rest body)
a586093f
SM
470 (list 'quote
471 (byte-compile-eval (byte-compile-top-level
472 (cons 'progn body))))))
1c393159 473 (eval-and-compile . (lambda (&rest body)
4795d1c7 474 (byte-compile-eval-before-compile (cons 'progn body))
1c393159
JB
475 (cons 'progn body))))
476 "The default macro-environment passed to macroexpand by the compiler.
477Placing a macro here will cause a macro to have different semantics when
478expanded by the compiler as when expanded by the interpreter.")
479
480(defvar byte-compile-macro-environment byte-compile-initial-macro-environment
52799cb8
RS
481 "Alist of macros defined in the file being compiled.
482Each element looks like (MACRONAME . DEFINITION). It is
e27c3564 483\(MACRONAME . nil) when a macro is redefined as a function.")
1c393159
JB
484
485(defvar byte-compile-function-environment nil
52799cb8
RS
486 "Alist of functions defined in the file being compiled.
487This is so we can inline them when necessary.
488Each element looks like (FUNCTIONNAME . DEFINITION). It is
a7a7ddf1
RS
489\(FUNCTIONNAME . nil) when a function is redefined as a macro.
490It is \(FUNCTIONNAME . t) when all we know is that it was defined,
cb4fb1d0
GM
491and we don't know the definition. For an autoloaded function, DEFINITION
492has the form (autoload . FILENAME).")
1c393159
JB
493
494(defvar byte-compile-unresolved-functions nil
a586093f 495 "Alist of undefined functions to which calls have been compiled.
2cb63a7c
AM
496This variable is only significant whilst compiling an entire buffer.
497Used for warnings when a function is not known to be defined or is later
a586093f 498defined with incorrect args.")
1c393159 499
6b61353c
KH
500(defvar byte-compile-noruntime-functions nil
501 "Alist of functions called that may not be defined when the compiled code is run.
502Used for warnings about calling a function that is defined during compilation
503but won't necessarily be defined when the compiled file is loaded.")
504
1c393159
JB
505(defvar byte-compile-tag-number 0)
506(defvar byte-compile-output nil
507 "Alist describing contents to put in byte code string.
508Each element is (INDEX . VALUE)")
509(defvar byte-compile-depth 0 "Current depth of execution stack.")
510(defvar byte-compile-maxdepth 0 "Maximum depth of execution stack.")
511
512\f
513;;; The byte codes; this information is duplicated in bytecomp.c
514
fef3407e 515(defvar byte-code-vector nil
1c393159
JB
516 "An array containing byte-code names indexed by byte-code values.")
517
fef3407e 518(defvar byte-stack+-info nil
1c393159
JB
519 "An array with the stack adjustment for each byte-code.")
520
521(defmacro byte-defop (opcode stack-adjust opname &optional docstring)
522 ;; This is a speed-hack for building the byte-code-vector at compile-time.
523 ;; We fill in the vector at macroexpand-time, and then after the last call
524 ;; to byte-defop, we write the vector out as a constant instead of writing
525 ;; out a bunch of calls to aset.
526 ;; Actually, we don't fill in the vector itself, because that could make
527 ;; it problematic to compile big changes to this compiler; we store the
528 ;; values on its plist, and remove them later in -extrude.
529 (let ((v1 (or (get 'byte-code-vector 'tmp-compile-time-value)
530 (put 'byte-code-vector 'tmp-compile-time-value
531 (make-vector 256 nil))))
532 (v2 (or (get 'byte-stack+-info 'tmp-compile-time-value)
533 (put 'byte-stack+-info 'tmp-compile-time-value
534 (make-vector 256 nil)))))
535 (aset v1 opcode opname)
536 (aset v2 opcode stack-adjust))
537 (if docstring
538 (list 'defconst opname opcode (concat "Byte code opcode " docstring "."))
539 (list 'defconst opname opcode)))
540
541(defmacro byte-extrude-byte-code-vectors ()
542 (prog1 (list 'setq 'byte-code-vector
543 (get 'byte-code-vector 'tmp-compile-time-value)
544 'byte-stack+-info
545 (get 'byte-stack+-info 'tmp-compile-time-value))
fd9b0a6b
DL
546 (put 'byte-code-vector 'tmp-compile-time-value nil)
547 (put 'byte-stack+-info 'tmp-compile-time-value nil)))
1c393159
JB
548
549
550;; unused: 0-7
551
552;; These opcodes are special in that they pack their argument into the
553;; opcode word.
554;;
555(byte-defop 8 1 byte-varref "for variable reference")
556(byte-defop 16 -1 byte-varset "for setting a variable")
557(byte-defop 24 -1 byte-varbind "for binding a variable")
558(byte-defop 32 0 byte-call "for calling a function")
559(byte-defop 40 0 byte-unbind "for unbinding special bindings")
eb8c3be9 560;; codes 8-47 are consumed by the preceding opcodes
1c393159
JB
561
562;; unused: 48-55
563
564(byte-defop 56 -1 byte-nth)
565(byte-defop 57 0 byte-symbolp)
566(byte-defop 58 0 byte-consp)
567(byte-defop 59 0 byte-stringp)
568(byte-defop 60 0 byte-listp)
569(byte-defop 61 -1 byte-eq)
570(byte-defop 62 -1 byte-memq)
571(byte-defop 63 0 byte-not)
572(byte-defop 64 0 byte-car)
573(byte-defop 65 0 byte-cdr)
574(byte-defop 66 -1 byte-cons)
575(byte-defop 67 0 byte-list1)
576(byte-defop 68 -1 byte-list2)
577(byte-defop 69 -2 byte-list3)
578(byte-defop 70 -3 byte-list4)
579(byte-defop 71 0 byte-length)
580(byte-defop 72 -1 byte-aref)
581(byte-defop 73 -2 byte-aset)
582(byte-defop 74 0 byte-symbol-value)
583(byte-defop 75 0 byte-symbol-function) ; this was commented out
584(byte-defop 76 -1 byte-set)
585(byte-defop 77 -1 byte-fset) ; this was commented out
586(byte-defop 78 -1 byte-get)
587(byte-defop 79 -2 byte-substring)
588(byte-defop 80 -1 byte-concat2)
589(byte-defop 81 -2 byte-concat3)
590(byte-defop 82 -3 byte-concat4)
591(byte-defop 83 0 byte-sub1)
592(byte-defop 84 0 byte-add1)
593(byte-defop 85 -1 byte-eqlsign)
594(byte-defop 86 -1 byte-gtr)
595(byte-defop 87 -1 byte-lss)
596(byte-defop 88 -1 byte-leq)
597(byte-defop 89 -1 byte-geq)
598(byte-defop 90 -1 byte-diff)
599(byte-defop 91 0 byte-negate)
600(byte-defop 92 -1 byte-plus)
601(byte-defop 93 -1 byte-max)
602(byte-defop 94 -1 byte-min)
603(byte-defop 95 -1 byte-mult) ; v19 only
604(byte-defop 96 1 byte-point)
1c393159
JB
605(byte-defop 98 0 byte-goto-char)
606(byte-defop 99 0 byte-insert)
607(byte-defop 100 1 byte-point-max)
608(byte-defop 101 1 byte-point-min)
609(byte-defop 102 0 byte-char-after)
610(byte-defop 103 1 byte-following-char)
611(byte-defop 104 1 byte-preceding-char)
612(byte-defop 105 1 byte-current-column)
613(byte-defop 106 0 byte-indent-to)
614(byte-defop 107 0 byte-scan-buffer-OBSOLETE) ; no longer generated as of v18
615(byte-defop 108 1 byte-eolp)
616(byte-defop 109 1 byte-eobp)
617(byte-defop 110 1 byte-bolp)
618(byte-defop 111 1 byte-bobp)
619(byte-defop 112 1 byte-current-buffer)
620(byte-defop 113 0 byte-set-buffer)
78943c8a
RS
621(byte-defop 114 0 byte-save-current-buffer
622 "To make a binding to record the current buffer")
1c393159
JB
623(byte-defop 115 0 byte-set-mark-OBSOLETE)
624(byte-defop 116 1 byte-interactive-p)
625
626;; These ops are new to v19
627(byte-defop 117 0 byte-forward-char)
628(byte-defop 118 0 byte-forward-word)
629(byte-defop 119 -1 byte-skip-chars-forward)
630(byte-defop 120 -1 byte-skip-chars-backward)
631(byte-defop 121 0 byte-forward-line)
632(byte-defop 122 0 byte-char-syntax)
633(byte-defop 123 -1 byte-buffer-substring)
634(byte-defop 124 -1 byte-delete-region)
635(byte-defop 125 -1 byte-narrow-to-region)
636(byte-defop 126 1 byte-widen)
637(byte-defop 127 0 byte-end-of-line)
638
639;; unused: 128
640
641;; These store their argument in the next two bytes
642(byte-defop 129 1 byte-constant2
643 "for reference to a constant with vector index >= byte-constant-limit")
644(byte-defop 130 0 byte-goto "for unconditional jump")
645(byte-defop 131 -1 byte-goto-if-nil "to pop value and jump if it's nil")
646(byte-defop 132 -1 byte-goto-if-not-nil "to pop value and jump if it's not nil")
647(byte-defop 133 -1 byte-goto-if-nil-else-pop
c5091f25 648 "to examine top-of-stack, jump and don't pop it if it's nil,
1c393159
JB
649otherwise pop it")
650(byte-defop 134 -1 byte-goto-if-not-nil-else-pop
c5091f25 651 "to examine top-of-stack, jump and don't pop it if it's non nil,
1c393159
JB
652otherwise pop it")
653
654(byte-defop 135 -1 byte-return "to pop a value and return it from `byte-code'")
655(byte-defop 136 -1 byte-discard "to discard one value from stack")
656(byte-defop 137 1 byte-dup "to duplicate the top of the stack")
657
658(byte-defop 138 0 byte-save-excursion
659 "to make a binding to record the buffer, point and mark")
660(byte-defop 139 0 byte-save-window-excursion
661 "to make a binding to record entire window configuration")
662(byte-defop 140 0 byte-save-restriction
663 "to make a binding to record the current buffer clipping restrictions")
664(byte-defop 141 -1 byte-catch
665 "for catch. Takes, on stack, the tag and an expression for the body")
666(byte-defop 142 -1 byte-unwind-protect
667 "for unwind-protect. Takes, on stack, an expression for the unwind-action")
668
c5091f25 669;; For condition-case. Takes, on stack, the variable to bind,
52799cb8
RS
670;; an expression for the body, and a list of clauses.
671(byte-defop 143 -2 byte-condition-case)
1c393159 672
52799cb8
RS
673;; For entry to with-output-to-temp-buffer.
674;; Takes, on stack, the buffer name.
675;; Binds standard-output and does some other things.
676;; Returns with temp buffer on the stack in place of buffer name.
677(byte-defop 144 0 byte-temp-output-buffer-setup)
1c393159 678
52799cb8
RS
679;; For exit from with-output-to-temp-buffer.
680;; Expects the temp buffer on the stack underneath value to return.
681;; Pops them both, then pushes the value back on.
682;; Unbinds standard-output and makes the temp buffer visible.
683(byte-defop 145 -1 byte-temp-output-buffer-show)
1c393159
JB
684
685;; these ops are new to v19
52799cb8
RS
686
687;; To unbind back to the beginning of this frame.
69dc83fd 688;; Not used yet, but will be needed for tail-recursion elimination.
52799cb8 689(byte-defop 146 0 byte-unbind-all)
1c393159
JB
690
691;; these ops are new to v19
692(byte-defop 147 -2 byte-set-marker)
693(byte-defop 148 0 byte-match-beginning)
694(byte-defop 149 0 byte-match-end)
695(byte-defop 150 0 byte-upcase)
696(byte-defop 151 0 byte-downcase)
697(byte-defop 152 -1 byte-string=)
698(byte-defop 153 -1 byte-string<)
699(byte-defop 154 -1 byte-equal)
700(byte-defop 155 -1 byte-nthcdr)
701(byte-defop 156 -1 byte-elt)
702(byte-defop 157 -1 byte-member)
703(byte-defop 158 -1 byte-assq)
704(byte-defop 159 0 byte-nreverse)
705(byte-defop 160 -1 byte-setcar)
706(byte-defop 161 -1 byte-setcdr)
707(byte-defop 162 0 byte-car-safe)
708(byte-defop 163 0 byte-cdr-safe)
709(byte-defop 164 -1 byte-nconc)
710(byte-defop 165 -1 byte-quo)
711(byte-defop 166 -1 byte-rem)
712(byte-defop 167 0 byte-numberp)
713(byte-defop 168 0 byte-integerp)
714
3eac9910 715;; unused: 169-174
1c393159
JB
716(byte-defop 175 nil byte-listN)
717(byte-defop 176 nil byte-concatN)
718(byte-defop 177 nil byte-insertN)
719
720;; unused: 178-191
721
722(byte-defop 192 1 byte-constant "for reference to a constant")
723;; codes 193-255 are consumed by byte-constant.
724(defconst byte-constant-limit 64
725 "Exclusive maximum index usable in the `byte-constant' opcode.")
726
727(defconst byte-goto-ops '(byte-goto byte-goto-if-nil byte-goto-if-not-nil
728 byte-goto-if-nil-else-pop
729 byte-goto-if-not-nil-else-pop)
52799cb8 730 "List of byte-codes whose offset is a pc.")
1c393159
JB
731
732(defconst byte-goto-always-pop-ops '(byte-goto-if-nil byte-goto-if-not-nil))
733
1c393159
JB
734(byte-extrude-byte-code-vectors)
735\f
736;;; lapcode generator
3614fc84
GM
737;;
738;; the byte-compiler now does source -> lapcode -> bytecode instead of
739;; source -> bytecode, because it's a lot easier to make optimizations
740;; on lapcode than on bytecode.
741;;
742;; Elements of the lapcode list are of the form (<instruction> . <parameter>)
743;; where instruction is a symbol naming a byte-code instruction,
744;; and parameter is an argument to that instruction, if any.
745;;
746;; The instruction can be the pseudo-op TAG, which means that this position
747;; in the instruction stream is a target of a goto. (car PARAMETER) will be
748;; the PC for this location, and the whole instruction "(TAG pc)" will be the
749;; parameter for some goto op.
750;;
751;; If the operation is varbind, varref, varset or push-constant, then the
752;; parameter is (variable/constant . index_in_constant_vector).
753;;
754;; First, the source code is macroexpanded and optimized in various ways.
755;; Then the resultant code is compiled into lapcode. Another set of
756;; optimizations are then run over the lapcode. Then the variables and
757;; constants referenced by the lapcode are collected and placed in the
758;; constants-vector. (This happens now so that variables referenced by dead
759;; code don't consume space.) And finally, the lapcode is transformed into
760;; compacted byte-code.
761;;
762;; A distinction is made between variables and constants because the variable-
763;; referencing instructions are more sensitive to the variables being near the
764;; front of the constants-vector than the constant-referencing instructions.
765;; Also, this lets us notice references to free variables.
1c393159
JB
766
767(defun byte-compile-lapcode (lap)
768 "Turns lapcode into bytecode. The lapcode is destroyed."
769 ;; Lapcode modifications: changes the ID of a tag to be the tag's PC.
770 (let ((pc 0) ; Program counter
771 op off ; Operation & offset
772 (bytes '()) ; Put the output bytes here
6c2161c4 773 (patchlist nil)) ; List of tags and goto's to patch
1c393159
JB
774 (while lap
775 (setq op (car (car lap))
776 off (cdr (car lap)))
777 (cond ((not (symbolp op))
52799cb8 778 (error "Non-symbolic opcode `%s'" op))
1c393159
JB
779 ((eq op 'TAG)
780 (setcar off pc)
781 (setq patchlist (cons off patchlist)))
782 ((memq op byte-goto-ops)
783 (setq pc (+ pc 3))
784 (setq bytes (cons (cons pc (cdr off))
785 (cons nil
786 (cons (symbol-value op) bytes))))
787 (setq patchlist (cons bytes patchlist)))
788 (t
789 (setq bytes
790 (cond ((cond ((consp off)
791 ;; Variable or constant reference
792 (setq off (cdr off))
793 (eq op 'byte-constant)))
794 (cond ((< off byte-constant-limit)
795 (setq pc (1+ pc))
796 (cons (+ byte-constant off) bytes))
797 (t
798 (setq pc (+ 3 pc))
799 (cons (lsh off -8)
800 (cons (logand off 255)
801 (cons byte-constant2 bytes))))))
802 ((<= byte-listN (symbol-value op))
803 (setq pc (+ 2 pc))
804 (cons off (cons (symbol-value op) bytes)))
805 ((< off 6)
806 (setq pc (1+ pc))
807 (cons (+ (symbol-value op) off) bytes))
808 ((< off 256)
809 (setq pc (+ 2 pc))
810 (cons off (cons (+ (symbol-value op) 6) bytes)))
811 (t
812 (setq pc (+ 3 pc))
813 (cons (lsh off -8)
814 (cons (logand off 255)
815 (cons (+ (symbol-value op) 7)
816 bytes))))))))
817 (setq lap (cdr lap)))
818 ;;(if (not (= pc (length bytes)))
52799cb8 819 ;; (error "Compiler error: pc mismatch - %s %s" pc (length bytes)))
1c393159
JB
820 ;; Patch PC into jumps
821 (let (bytes)
822 (while patchlist
823 (setq bytes (car patchlist))
824 (cond ((atom (car bytes))) ; Tag
1c393159
JB
825 (t ; Absolute jump
826 (setq pc (car (cdr (car bytes)))) ; Pick PC from tag
827 (setcar (cdr bytes) (logand pc 255))
8476cfaf
SM
828 (setcar bytes (lsh pc -8))
829 ;; FIXME: Replace this by some workaround.
636a36a0 830 (if (> (car bytes) 255) (error "Bytecode overflow"))))
1c393159 831 (setq patchlist (cdr patchlist))))
da9e269f 832 (apply 'unibyte-string (nreverse bytes))))
1c393159
JB
833
834\f
a586093f
SM
835;;; compile-time evaluation
836
3f12e5bd
GM
837(defun byte-compile-cl-file-p (file)
838 "Return non-nil if FILE is one of the CL files."
839 (and (stringp file)
840 (string-match "^cl\\>" (file-name-nondirectory file))))
841
ea4b0ca3
SM
842(defun byte-compile-eval (form)
843 "Eval FORM and mark the functions defined therein.
6b61353c 844Each function's symbol gets added to `byte-compile-noruntime-functions'."
a586093f
SM
845 (let ((hist-orig load-history)
846 (hist-nil-orig current-load-list))
ea4b0ca3 847 (prog1 (eval form)
cf637a34 848 (when (byte-compile-warning-enabled-p 'noruntime)
a586093f
SM
849 (let ((hist-new load-history)
850 (hist-nil-new current-load-list))
ea4b0ca3
SM
851 ;; Go through load-history, look for newly loaded files
852 ;; and mark all the functions defined therein.
853 (while (and hist-new (not (eq hist-new hist-orig)))
d1a57439
RS
854 (let ((xs (pop hist-new))
855 old-autoloads)
ea4b0ca3 856 ;; Make sure the file was not already loaded before.
997011eb 857 (unless (or (assoc (car xs) hist-orig)
3f12e5bd
GM
858 ;; Don't give both the "noruntime" and
859 ;; "cl-functions" warning for the same function.
860 ;; FIXME This seems incorrect - these are two
861 ;; independent warnings. For example, you may be
862 ;; choosing to see the cl warnings but ignore them.
863 ;; You probably don't want to ignore noruntime in the
864 ;; same way.
865 (and (byte-compile-warning-enabled-p 'cl-functions)
866 (byte-compile-cl-file-p (car xs))))
ea4b0ca3
SM
867 (dolist (s xs)
868 (cond
d1a57439
RS
869 ((symbolp s)
870 (unless (memq s old-autoloads)
6b61353c 871 (push s byte-compile-noruntime-functions)))
d1a57439 872 ((and (consp s) (eq t (car s)))
6c2161c4 873 (push (cdr s) old-autoloads))
ea4b0ca3 874 ((and (consp s) (eq 'autoload (car s)))
6b61353c 875 (push (cdr s) byte-compile-noruntime-functions)))))))
ea4b0ca3 876 ;; Go through current-load-list for the locally defined funs.
d1a57439
RS
877 (let (old-autoloads)
878 (while (and hist-nil-new (not (eq hist-nil-new hist-nil-orig)))
879 (let ((s (pop hist-nil-new)))
880 (when (and (symbolp s) (not (memq s old-autoloads)))
6b61353c 881 (push s byte-compile-noruntime-functions))
d1a57439 882 (when (and (consp s) (eq t (car s)))
997011eb 883 (push (cdr s) old-autoloads)))))))
cf637a34 884 (when (byte-compile-warning-enabled-p 'cl-functions)
b8104a2b 885 (let ((hist-new load-history))
3f12e5bd
GM
886 ;; Go through load-history, looking for the cl files.
887 ;; Since new files are added at the start of load-history,
888 ;; we scan the new history until the tail matches the old.
889 (while (and (not byte-compile-cl-functions)
890 hist-new (not (eq hist-new hist-orig)))
891 ;; We used to check if the file had already been loaded,
892 ;; but it is better to check non-nil byte-compile-cl-functions.
893 (and (byte-compile-cl-file-p (car (pop hist-new)))
894 (byte-compile-find-cl-functions))))))))
a586093f 895
4795d1c7
RS
896(defun byte-compile-eval-before-compile (form)
897 "Evaluate FORM for `eval-and-compile'."
898 (let ((hist-nil-orig current-load-list))
899 (prog1 (eval form)
900 ;; (eval-and-compile (require 'cl) turns off warnings for cl functions.
3f12e5bd
GM
901 ;; FIXME Why does it do that - just as a hack?
902 ;; There are other ways to do this nowadays.
4795d1c7
RS
903 (let ((tem current-load-list))
904 (while (not (eq tem hist-nil-orig))
905 (when (equal (car tem) '(require . cl))
cf637a34 906 (byte-compile-disable-warning 'cl-functions))
4795d1c7 907 (setq tem (cdr tem)))))))
a586093f 908\f
1c393159
JB
909;;; byte compiler messages
910
d82e848c 911(defvar byte-compile-current-form nil)
d82e848c 912(defvar byte-compile-dest-file nil)
9985d391 913(defvar byte-compile-current-file nil)
ab5111e3 914(defvar byte-compile-current-group nil)
ccb3c8de 915(defvar byte-compile-current-buffer nil)
6a619620 916
22788fb8 917;; Log something that isn't a warning.
1c393159 918(defmacro byte-compile-log (format-string &rest args)
b88a41d0
SM
919 `(and
920 byte-optimize
921 (memq byte-optimize-log '(t source))
922 (let ((print-escape-newlines t)
923 (print-level 4)
924 (print-length 4))
925 (byte-compile-log-1
926 (format
927 ,format-string
928 ,@(mapcar
929 (lambda (x) (if (symbolp x) (list 'prin1-to-string x) x))
930 args))))))
1c393159 931
22788fb8
RS
932;; Log something that isn't a warning.
933(defun byte-compile-log-1 (string)
b88a41d0 934 (with-current-buffer "*Compile-Log*"
997011eb
RS
935 (let ((inhibit-read-only t))
936 (goto-char (point-max))
937 (byte-compile-warning-prefix nil nil)
938 (cond (noninteractive
939 (message " %s" string))
940 (t
941 (insert (format "%s\n" string)))))))
1c393159 942
ccb3c8de
CW
943(defvar byte-compile-read-position nil
944 "Character position we began the last `read' from.")
945(defvar byte-compile-last-position nil
946 "Last known character position in the input.")
947
948;; copied from gnus-util.el
1fd592a0 949(defsubst byte-compile-delete-first (elt list)
ccb3c8de
CW
950 (if (eq (car list) elt)
951 (cdr list)
952 (let ((total list))
953 (while (and (cdr list)
954 (not (eq (cadr list) elt)))
955 (setq list (cdr list)))
956 (when (cdr list)
957 (setcdr list (cddr list)))
958 total)))
959
960;; The purpose of this function is to iterate through the
961;; `read-symbol-positions-list'. Each time we process, say, a
962;; function definition (`defun') we remove `defun' from
963;; `read-symbol-positions-list', and set `byte-compile-last-position'
964;; to that symbol's character position. Similarly, if we encounter a
965;; variable reference, like in (1+ foo), we remove `foo' from the
966;; list. If our current position is after the symbol's position, we
967;; assume we've already passed that point, and look for the next
6b8c2efc 968;; occurrence of the symbol.
4ec5239c
LH
969;;
970;; This function should not be called twice for the same occurrence of
971;; a symbol, and it should not be called for symbols generated by the
972;; byte compiler itself; because rather than just fail looking up the
973;; symbol, we may find an occurrence of the symbol further ahead, and
974;; then `byte-compile-last-position' as advanced too far.
975;;
6b8c2efc 976;; So your're probably asking yourself: Isn't this function a
ccb3c8de
CW
977;; gross hack? And the answer, of course, would be yes.
978(defun byte-compile-set-symbol-position (sym &optional allow-previous)
979 (when byte-compile-read-position
1fd592a0 980 (let (last entry)
ccb3c8de 981 (while (progn
0b46acbf
RS
982 (setq last byte-compile-last-position
983 entry (assq sym read-symbol-positions-list))
984 (when entry
985 (setq byte-compile-last-position
986 (+ byte-compile-read-position (cdr entry))
987 read-symbol-positions-list
988 (byte-compile-delete-first
989 entry read-symbol-positions-list)))
ccb3c8de
CW
990 (or (and allow-previous (not (= last byte-compile-last-position)))
991 (> last byte-compile-last-position)))))))
b8175fe6 992
22788fb8
RS
993(defvar byte-compile-last-warned-form nil)
994(defvar byte-compile-last-logged-file nil)
995
22788fb8 996;; This is used as warning-prefix for the compiler.
4390021b 997;; It is always called with the warnings buffer current.
22788fb8 998(defun byte-compile-warning-prefix (level entry)
997011eb
RS
999 (let* ((inhibit-read-only t)
1000 (dir default-directory)
4390021b
RS
1001 (file (cond ((stringp byte-compile-current-file)
1002 (format "%s:" (file-relative-name byte-compile-current-file dir)))
b8175fe6 1003 ((bufferp byte-compile-current-file)
1f006824 1004 (format "Buffer %s:"
b8175fe6
GM
1005 (buffer-name byte-compile-current-file)))
1006 (t "")))
1f006824 1007 (pos (if (and byte-compile-current-file
ccb3c8de
CW
1008 (integerp byte-compile-read-position))
1009 (with-current-buffer byte-compile-current-buffer
ea1cb2bd 1010 (format "%d:%d:"
b0aa2c65
RS
1011 (save-excursion
1012 (goto-char byte-compile-last-position)
1013 (1+ (count-lines (point-min) (point-at-bol))))
ccb3c8de
CW
1014 (save-excursion
1015 (goto-char byte-compile-last-position)
1016 (1+ (current-column)))))
b8175fe6 1017 ""))
4390021b
RS
1018 (form (if (eq byte-compile-current-form :end) "end of data"
1019 (or byte-compile-current-form "toplevel form"))))
1020 (when (or (and byte-compile-current-file
1021 (not (equal byte-compile-current-file
1022 byte-compile-last-logged-file)))
6b61353c 1023 (and byte-compile-current-form
4390021b
RS
1024 (not (eq byte-compile-current-form
1025 byte-compile-last-warned-form))))
22788fb8 1026 (insert (format "\nIn %s:\n" form)))
4390021b
RS
1027 (when level
1028 (insert (format "%s%s" file pos))))
cb3069bb 1029 (setq byte-compile-last-logged-file byte-compile-current-file
22788fb8
RS
1030 byte-compile-last-warned-form byte-compile-current-form)
1031 entry)
1c393159 1032
4390021b
RS
1033;; This no-op function is used as the value of warning-series
1034;; to tell inner calls to displaying-byte-compile-warnings
1035;; not to bind warning-series.
1036(defun byte-compile-warning-series (&rest ignore)
1037 nil)
1038
db283402 1039;; (compile-mode) will cause this to be loaded.
2c52d7a3 1040(declare-function compilation-forget-errors "compile" ())
db283402 1041
144b2637 1042;; Log the start of a file in *Compile-Log*, and mark it as done.
22788fb8 1043;; Return the position of the start of the page in the log buffer.
144b2637
RS
1044;; But do nothing in batch mode.
1045(defun byte-compile-log-file ()
4390021b 1046 (and (not (equal byte-compile-current-file byte-compile-last-logged-file))
cb3069bb 1047 (not noninteractive)
008e2c2a 1048 (with-current-buffer (get-buffer-create "*Compile-Log*")
ca96ae0b 1049 (goto-char (point-max))
997011eb
RS
1050 (let* ((inhibit-read-only t)
1051 (dir (and byte-compile-current-file
4390021b
RS
1052 (file-name-directory byte-compile-current-file)))
1053 (was-same (equal default-directory dir))
1054 pt)
1055 (when dir
1056 (unless was-same
1057 (insert (format "Leaving directory `%s'\n" default-directory))))
1058 (unless (bolp)
1059 (insert "\n"))
1060 (setq pt (point-marker))
1061 (if byte-compile-current-file
1062 (insert "\f\nCompiling "
1063 (if (stringp byte-compile-current-file)
1064 (concat "file " byte-compile-current-file)
1065 (concat "buffer " (buffer-name byte-compile-current-file)))
1066 " at " (current-time-string) "\n")
1067 (insert "\f\nCompiling no file at " (current-time-string) "\n"))
1068 (when dir
1069 (setq default-directory dir)
1070 (unless was-same
1071 (insert (format "Entering directory `%s'\n" default-directory))))
6b61353c
KH
1072 (setq byte-compile-last-logged-file byte-compile-current-file
1073 byte-compile-last-warned-form nil)
977f31f8 1074 ;; Do this after setting default-directory.
5c7ffa04 1075 (unless (derived-mode-p 'compilation-mode) (compilation-mode))
b88a41d0 1076 (compilation-forget-errors)
22788fb8
RS
1077 pt))))
1078
1079;; Log a message STRING in *Compile-Log*.
1080;; Also log the current function and file if not already done.
1081(defun byte-compile-log-warning (string &optional fill level)
1082 (let ((warning-prefix-function 'byte-compile-warning-prefix)
6b61353c 1083 (warning-type-format "")
997011eb
RS
1084 (warning-fill-prefix (if fill " "))
1085 (inhibit-read-only t))
22788fb8 1086 (display-warning 'bytecomp string level "*Compile-Log*")))
144b2637 1087
1c393159 1088(defun byte-compile-warn (format &rest args)
22788fb8 1089 "Issue a byte compiler warning; use (format FORMAT ARGS...) for message."
1c393159
JB
1090 (setq format (apply 'format format args))
1091 (if byte-compile-error-on-warn
1092 (error "%s" format) ; byte-compile-file catches and logs it
22788fb8
RS
1093 (byte-compile-log-warning format t :warning)))
1094
5791bedf
GM
1095(defun byte-compile-warn-obsolete (symbol)
1096 "Warn that SYMBOL (a variable or function) is obsolete."
1097 (when (byte-compile-warning-enabled-p 'obsolete)
1098 (let* ((funcp (get symbol 'byte-obsolete-info))
1099 (obsolete (or funcp (get symbol 'byte-obsolete-variable)))
1100 (instead (car obsolete))
1101 (asof (if funcp (nth 2 obsolete) (cdr obsolete))))
8480fc7c
GM
1102 (unless (and funcp (memq symbol byte-compile-not-obsolete-funcs))
1103 (byte-compile-warn "`%s' is an obsolete %s%s%s" symbol
1104 (if funcp "function" "variable")
1105 (if asof (concat " (as of Emacs " asof ")") "")
1106 (cond ((stringp instead)
1107 (concat "; " instead))
1108 (instead
1109 (format "; use `%s' instead." instead))
1110 (t ".")))))))
5791bedf 1111
0b030df7 1112(defun byte-compile-report-error (error-info)
22788fb8 1113 "Report Lisp error in compilation. ERROR-INFO is the error data."
ab94e6e7 1114 (setq byte-compiler-error-flag t)
22788fb8
RS
1115 (byte-compile-log-warning
1116 (error-message-string error-info)
1117 nil :error))
0b030df7 1118
1c393159
JB
1119;;; Used by make-obsolete.
1120(defun byte-compile-obsolete (form)
5791bedf
GM
1121 (byte-compile-set-symbol-position (car form))
1122 (byte-compile-warn-obsolete (car form))
1123 (funcall (or (cadr (get (car form) 'byte-obsolete-info)) ; handler
1124 'byte-compile-normal-call) form))
1c393159
JB
1125\f
1126;; Compiler options
1127
52799cb8
RS
1128;; (defvar byte-compiler-valid-options
1129;; '((optimize byte-optimize (t nil source byte) val)
1130;; (file-format byte-compile-compatibility (emacs18 emacs19)
1131;; (eq val 'emacs18))
1132;; ;; (new-bytecodes byte-compile-generate-emacs19-bytecodes (t nil) val)
1133;; (delete-errors byte-compile-delete-errors (t nil) val)
1134;; (verbose byte-compile-verbose (t nil) val)
1135;; (warnings byte-compile-warnings ((callargs redefine free-vars unresolved))
1136;; val)))
1c393159
JB
1137
1138;; Inhibit v18/v19 selectors if the version is hardcoded.
c5091f25 1139;; #### This should print a warning if the user tries to change something
1c393159 1140;; than can't be changed because the running compiler doesn't support it.
52799cb8
RS
1141;; (cond
1142;; ((byte-compile-single-version)
1143;; (setcar (cdr (cdr (assq 'new-bytecodes byte-compiler-valid-options)))
1144;; (list (byte-compile-version-cond
1145;; byte-compile-generate-emacs19-bytecodes)))
1146;; (setcar (cdr (cdr (assq 'file-format byte-compiler-valid-options)))
1147;; (if (byte-compile-version-cond byte-compile-compatibility)
1148;; '(emacs18) '(emacs19)))))
1149
1150;; (defun byte-compiler-options-handler (&rest args)
1151;; (let (key val desc choices)
1152;; (while args
1153;; (if (or (atom (car args)) (nthcdr 2 (car args)) (null (cdr (car args))))
1154;; (error "Malformed byte-compiler option `%s'" (car args)))
1155;; (setq key (car (car args))
1156;; val (car (cdr (car args)))
1157;; desc (assq key byte-compiler-valid-options))
1158;; (or desc
1159;; (error "Unknown byte-compiler option `%s'" key))
1160;; (setq choices (nth 2 desc))
1161;; (if (consp (car choices))
1162;; (let (this
1163;; (handler 'cons)
1164;; (ret (and (memq (car val) '(+ -))
1165;; (copy-sequence (if (eq t (symbol-value (nth 1 desc)))
1166;; choices
1167;; (symbol-value (nth 1 desc)))))))
1168;; (setq choices (car choices))
1169;; (while val
1170;; (setq this (car val))
1171;; (cond ((memq this choices)
1172;; (setq ret (funcall handler this ret)))
1173;; ((eq this '+) (setq handler 'cons))
1174;; ((eq this '-) (setq handler 'delq))
1175;; ((error "`%s' only accepts %s" key choices)))
1176;; (setq val (cdr val)))
1177;; (set (nth 1 desc) ret))
1178;; (or (memq val choices)
1179;; (error "`%s' must be one of `%s'" key choices))
1180;; (set (nth 1 desc) (eval (nth 3 desc))))
1181;; (setq args (cdr args)))
1182;; nil))
1c393159
JB
1183\f
1184;;; sanity-checking arglists
1185
1186(defun byte-compile-fdefinition (name macro-p)
ced10a4c
SM
1187 ;; If a function has an entry saying (FUNCTION . t).
1188 ;; that means we know it is defined but we don't know how.
1189 ;; If a function has an entry saying (FUNCTION . nil),
1190 ;; that means treat it as not defined.
1c393159
JB
1191 (let* ((list (if macro-p
1192 byte-compile-macro-environment
5286a842 1193 byte-compile-function-environment))
1c393159
JB
1194 (env (cdr (assq name list))))
1195 (or env
1196 (let ((fn name))
1197 (while (and (symbolp fn)
1198 (fboundp fn)
1199 (or (symbolp (symbol-function fn))
1200 (consp (symbol-function fn))
1201 (and (not macro-p)
ed015bdd 1202 (byte-code-function-p (symbol-function fn)))))
1c393159 1203 (setq fn (symbol-function fn)))
9d28c33e
SM
1204 (let ((advertised (gethash (if (and (symbolp fn) (fboundp fn))
1205 ;; Could be a subr.
1206 (symbol-function fn)
1207 fn)
1208 advertised-signature-table t)))
ced10a4c
SM
1209 (cond
1210 ((listp advertised)
1211 (if macro-p
1212 `(macro lambda ,advertised)
1213 `(lambda ,advertised)))
1214 ((and (not macro-p) (byte-code-function-p fn)) fn)
1215 ((not (consp fn)) nil)
1216 ((eq 'macro (car fn)) (cdr fn))
1217 (macro-p nil)
1218 ((eq 'autoload (car fn)) nil)
1219 (t fn)))))))
1c393159
JB
1220
1221(defun byte-compile-arglist-signature (arglist)
1222 (let ((args 0)
1223 opts
1224 restp)
1225 (while arglist
1226 (cond ((eq (car arglist) '&optional)
1227 (or opts (setq opts 0)))
1228 ((eq (car arglist) '&rest)
1229 (if (cdr arglist)
1230 (setq restp t
1231 arglist nil)))
1232 (t
1233 (if opts
1234 (setq opts (1+ opts))
1235 (setq args (1+ args)))))
1236 (setq arglist (cdr arglist)))
1237 (cons args (if restp nil (if opts (+ args opts) args)))))
1238
1239
1240(defun byte-compile-arglist-signatures-congruent-p (old new)
1241 (not (or
1242 (> (car new) (car old)) ; requires more args now
a7acbbe4 1243 (and (null (cdr old)) ; took rest-args, doesn't any more
1c393159
JB
1244 (cdr new))
1245 (and (cdr new) (cdr old) ; can't take as many args now
1246 (< (cdr new) (cdr old)))
1247 )))
1248
1249(defun byte-compile-arglist-signature-string (signature)
1250 (cond ((null (cdr signature))
1251 (format "%d+" (car signature)))
1252 ((= (car signature) (cdr signature))
1253 (format "%d" (car signature)))
1254 (t (format "%d-%d" (car signature) (cdr signature)))))
1255
1256
52799cb8 1257;; Warn if the form is calling a function with the wrong number of arguments.
1c393159 1258(defun byte-compile-callargs-warn (form)
1c393159
JB
1259 (let* ((def (or (byte-compile-fdefinition (car form) nil)
1260 (byte-compile-fdefinition (car form) t)))
a7a7ddf1 1261 (sig (if (and def (not (eq def t)))
416d3588
GM
1262 (progn
1263 (and (eq (car-safe def) 'macro)
1264 (eq (car-safe (cdr-safe def)) 'lambda)
1265 (setq def (cdr def)))
1266 (byte-compile-arglist-signature
1267 (if (memq (car-safe def) '(declared lambda))
1268 (nth 1 def)
1269 (if (byte-code-function-p def)
1270 (aref def 0)
1271 '(&rest def)))))
ed62683d
DL
1272 (if (and (fboundp (car form))
1273 (subrp (symbol-function (car form))))
1274 (subr-arity (symbol-function (car form))))))
1c393159 1275 (ncall (length (cdr form))))
ed62683d
DL
1276 ;; Check many or unevalled from subr-arity.
1277 (if (and (cdr-safe sig)
1278 (not (numberp (cdr sig))))
1279 (setcdr sig nil))
1c393159 1280 (if sig
ccb3c8de 1281 (when (or (< ncall (car sig))
1c393159 1282 (and (cdr sig) (> ncall (cdr sig))))
ccb3c8de
CW
1283 (byte-compile-set-symbol-position (car form))
1284 (byte-compile-warn
1285 "%s called with %d argument%s, but %s %s"
1286 (car form) ncall
1287 (if (= 1 ncall) "" "s")
1288 (if (< ncall (car sig))
1289 "requires"
1290 "accepts only")
ba76e7fa 1291 (byte-compile-arglist-signature-string sig))))
6b61353c 1292 (byte-compile-format-warn form)
ba76e7fa
SM
1293 ;; Check to see if the function will be available at runtime
1294 ;; and/or remember its arity if it's unknown.
a7a7ddf1 1295 (or (and (or def (fboundp (car form))) ; might be a subr or autoload.
6b61353c 1296 (not (memq (car form) byte-compile-noruntime-functions)))
ba76e7fa
SM
1297 (eq (car form) byte-compile-current-form) ; ## this doesn't work
1298 ; with recursion.
1299 ;; It's a currently-undefined function.
1300 ;; Remember number of args in call.
1301 (let ((cons (assq (car form) byte-compile-unresolved-functions))
1302 (n (length (cdr form))))
1303 (if cons
1304 (or (memq n (cdr cons))
1305 (setcdr cons (cons n (cdr cons))))
977b50fb
SM
1306 (push (list (car form) n)
1307 byte-compile-unresolved-functions))))))
1c393159 1308
6b61353c
KH
1309(defun byte-compile-format-warn (form)
1310 "Warn if FORM is `format'-like with inconsistent args.
1311Applies if head of FORM is a symbol with non-nil property
1312`byte-compile-format-like' and first arg is a constant string.
1313Then check the number of format fields matches the number of
1314extra args."
1315 (when (and (symbolp (car form))
1316 (stringp (nth 1 form))
1317 (get (car form) 'byte-compile-format-like))
1318 (let ((nfields (with-temp-buffer
1319 (insert (nth 1 form))
b8104a2b 1320 (goto-char (point-min))
6b61353c
KH
1321 (let ((n 0))
1322 (while (re-search-forward "%." nil t)
1323 (unless (eq ?% (char-after (1+ (match-beginning 0))))
1324 (setq n (1+ n))))
1325 n)))
1326 (nargs (- (length form) 2)))
1327 (unless (= nargs nfields)
1328 (byte-compile-warn
1329 "`%s' called with %d args to fill %d format field(s)" (car form)
1330 nargs nfields)))))
1331
1332(dolist (elt '(format message error))
1333 (put elt 'byte-compile-format-like t))
1334
11efeb9b
RS
1335;; Warn if a custom definition fails to specify :group.
1336(defun byte-compile-nogroup-warn (form)
ab5111e3
SM
1337 (if (and (memq (car form) '(custom-declare-face custom-declare-variable))
1338 byte-compile-current-group)
1339 ;; The group will be provided implicitly.
1340 nil
1341 (let ((keyword-args (cdr (cdr (cdr (cdr form)))))
1342 (name (cadr form)))
1343 (or (not (eq (car-safe name) 'quote))
3ab6a7ae
SM
1344 (and (eq (car form) 'custom-declare-group)
1345 (equal name ''emacs))
1346 (plist-get keyword-args :group)
1347 (not (and (consp name) (eq (car name) 'quote)))
1348 (byte-compile-warn
1349 "%s for `%s' fails to specify containing group"
1350 (cdr (assq (car form)
ab5111e3
SM
1351 '((custom-declare-group . defgroup)
1352 (custom-declare-face . defface)
1353 (custom-declare-variable . defcustom))))
1354 (cadr name)))
1355 ;; Update the current group, if needed.
1356 (if (and byte-compile-current-file ;Only when byte-compiling a whole file.
1357 (eq (car form) 'custom-declare-group)
1358 (eq (car-safe name) 'quote))
1359 (setq byte-compile-current-group (cadr name))))))
11efeb9b 1360
52799cb8
RS
1361;; Warn if the function or macro is being redefined with a different
1362;; number of arguments.
1c393159 1363(defun byte-compile-arglist-warn (form macrop)
1c393159 1364 (let ((old (byte-compile-fdefinition (nth 1 form) macrop)))
a7a7ddf1 1365 (if (and old (not (eq old t)))
416d3588
GM
1366 (progn
1367 (and (eq 'macro (car-safe old))
1368 (eq 'lambda (car-safe (cdr-safe old)))
1369 (setq old (cdr old)))
1370 (let ((sig1 (byte-compile-arglist-signature
1371 (if (eq 'lambda (car-safe old))
1372 (nth 1 old)
1373 (if (byte-code-function-p old)
1374 (aref old 0)
1375 '(&rest def)))))
1376 (sig2 (byte-compile-arglist-signature (nth 2 form))))
1377 (unless (byte-compile-arglist-signatures-congruent-p sig1 sig2)
1378 (byte-compile-set-symbol-position (nth 1 form))
1379 (byte-compile-warn
1380 "%s %s used to take %s %s, now takes %s"
1381 (if (eq (car form) 'defun) "function" "macro")
1382 (nth 1 form)
1383 (byte-compile-arglist-signature-string sig1)
1384 (if (equal sig1 '(1 . 1)) "argument" "arguments")
1385 (byte-compile-arglist-signature-string sig2)))))
1c393159
JB
1386 ;; This is the first definition. See if previous calls are compatible.
1387 (let ((calls (assq (nth 1 form) byte-compile-unresolved-functions))
1388 nums sig min max)
1389 (if calls
1390 (progn
1391 (setq sig (byte-compile-arglist-signature (nth 2 form))
1392 nums (sort (copy-sequence (cdr calls)) (function <))
1393 min (car nums)
1394 max (car (nreverse nums)))
ccb3c8de 1395 (when (or (< min (car sig))
1c393159 1396 (and (cdr sig) (> max (cdr sig))))
ccb3c8de
CW
1397 (byte-compile-set-symbol-position (nth 1 form))
1398 (byte-compile-warn
1399 "%s being defined to take %s%s, but was previously called with %s"
1400 (nth 1 form)
1401 (byte-compile-arglist-signature-string sig)
1402 (if (equal sig '(1 . 1)) " arg" " args")
1403 (byte-compile-arglist-signature-string (cons min max))))
1f006824 1404
1c393159
JB
1405 (setq byte-compile-unresolved-functions
1406 (delq calls byte-compile-unresolved-functions)))))
1407 )))
1408
95c997fa
RS
1409(defvar byte-compile-cl-functions nil
1410 "List of functions defined in CL.")
1411
3f12e5bd
GM
1412;; Can't just add this to cl-load-hook, because that runs just before
1413;; the forms from cl.el get added to load-history.
95c997fa
RS
1414(defun byte-compile-find-cl-functions ()
1415 (unless byte-compile-cl-functions
1416 (dolist (elt load-history)
3f12e5bd
GM
1417 (and (byte-compile-cl-file-p (car elt))
1418 (dolist (e (cdr elt))
1419 ;; Includes the cl-foo functions that cl autoloads.
1420 (when (memq (car-safe e) '(autoload defun))
1421 (push (cdr e) byte-compile-cl-functions)))))))
95c997fa 1422
4795d1c7
RS
1423(defun byte-compile-cl-warn (form)
1424 "Warn if FORM is a call of a function from the CL package."
95c997fa
RS
1425 (let ((func (car-safe form)))
1426 (if (and byte-compile-cl-functions
1427 (memq func byte-compile-cl-functions)
6b61353c 1428 ;; Aliases which won't have been expanded at this point.
4795d1c7
RS
1429 ;; These aren't all aliases of subrs, so not trivial to
1430 ;; avoid hardwiring the list.
1431 (not (memq func
9cb9a7bc
RS
1432 '(cl-block-wrapper cl-block-throw
1433 multiple-value-call nth-value
95c997fa 1434 copy-seq first second rest endp cl-member
d1a57439
RS
1435 ;; These are included in generated code
1436 ;; that can't be called except at compile time
1437 ;; or unless cl is loaded anyway.
1438 cl-defsubst-expand cl-struct-setf-expander
8f876842
RS
1439 ;; These would sometimes be warned about
1440 ;; but such warnings are never useful,
1441 ;; so don't warn about them.
118861df
DL
1442 macroexpand cl-macroexpand-all
1443 cl-compiling-file)))
1444 ;; Avoid warnings for things which are safe because they
1445 ;; have suitable compiler macros, but those aren't
1446 ;; expanded at this stage. There should probably be more
1447 ;; here than caaar and friends.
1448 (not (and (eq (get func 'byte-compile)
1449 'cl-byte-compile-compiler-macro)
6640c250 1450 (string-match "\\`c[ad]+r\\'" (symbol-name func)))))
4795d1c7
RS
1451 (byte-compile-warn "Function `%s' from cl package called at runtime"
1452 func)))
1453 form)
1454
a586093f 1455(defun byte-compile-print-syms (str1 strn syms)
ccb3c8de
CW
1456 (when syms
1457 (byte-compile-set-symbol-position (car syms) t))
b8175fe6
GM
1458 (cond ((and (cdr syms) (not noninteractive))
1459 (let* ((str strn)
1460 (L (length str))
1461 s)
1462 (while syms
1463 (setq s (symbol-name (pop syms))
1464 L (+ L (length s) 2))
1465 (if (< L (1- fill-column))
1466 (setq str (concat str " " s (and syms ",")))
1467 (setq str (concat str "\n " s (and syms ","))
1468 L (+ (length s) 4))))
1469 (byte-compile-warn "%s" str)))
1470 ((cdr syms)
1f006824 1471 (byte-compile-warn "%s %s"
b8175fe6
GM
1472 strn
1473 (mapconcat #'symbol-name syms ", ")))
1474
1475 (syms
1476 (byte-compile-warn str1 (car syms)))))
a586093f 1477
1f006824 1478;; If we have compiled any calls to functions which are not known to be
52799cb8
RS
1479;; defined, issue a warning enumerating them.
1480;; `unresolved' in the list `byte-compile-warnings' disables this.
1c393159 1481(defun byte-compile-warn-about-unresolved-functions ()
cf637a34 1482 (when (byte-compile-warning-enabled-p 'unresolved)
b8175fe6 1483 (let ((byte-compile-current-form :end)
a586093f
SM
1484 (noruntime nil)
1485 (unresolved nil))
1486 ;; Separate the functions that will not be available at runtime
1487 ;; from the truly unresolved ones.
1488 (dolist (f byte-compile-unresolved-functions)
1489 (setq f (car f))
1490 (if (fboundp f) (push f noruntime) (push f unresolved)))
1491 ;; Complain about the no-run-time functions
1492 (byte-compile-print-syms
b8175fe6
GM
1493 "the function `%s' might not be defined at runtime."
1494 "the following functions might not be defined at runtime:"
a586093f
SM
1495 noruntime)
1496 ;; Complain about the unresolved functions
1497 (byte-compile-print-syms
b8175fe6
GM
1498 "the function `%s' is not known to be defined."
1499 "the following functions are not known to be defined:"
a586093f 1500 unresolved)))
1c393159
JB
1501 nil)
1502
1503\f
582a857c 1504(defsubst byte-compile-const-symbol-p (symbol &optional any-value)
6c2161c4 1505 "Non-nil if SYMBOL is constant.
582a857c 1506If ANY-VALUE is nil, only return non-nil if the value of the symbol is the
6c2161c4 1507symbol itself."
1639b803 1508 (or (memq symbol '(nil t))
6c2161c4 1509 (keywordp symbol)
d988dbf6
SM
1510 (if any-value
1511 (or (memq symbol byte-compile-const-variables)
1512 ;; FIXME: We should provide a less intrusive way to find out
1513 ;; is a variable is "constant".
1514 (and (boundp symbol)
1515 (condition-case nil
1516 (progn (set symbol (symbol-value symbol)) nil)
1517 (setting-constant t)))))))
1639b803 1518
1c393159 1519(defmacro byte-compile-constp (form)
c5091f25 1520 "Return non-nil if FORM is a constant."
1639b803
DL
1521 `(cond ((consp ,form) (eq (car ,form) 'quote))
1522 ((not (symbolp ,form)))
1523 ((byte-compile-const-symbol-p ,form))))
1c393159
JB
1524
1525(defmacro byte-compile-close-variables (&rest body)
1526 (cons 'let
1527 (cons '(;;
1528 ;; Close over these variables to encapsulate the
1529 ;; compilation state
1530 ;;
1531 (byte-compile-macro-environment
1532 ;; Copy it because the compiler may patch into the
1533 ;; macroenvironment.
1534 (copy-alist byte-compile-initial-macro-environment))
1535 (byte-compile-function-environment nil)
1536 (byte-compile-bound-variables nil)
6c2161c4 1537 (byte-compile-const-variables nil)
1c393159
JB
1538 (byte-compile-free-references nil)
1539 (byte-compile-free-assignments nil)
1540 ;;
1541 ;; Close over these variables so that `byte-compiler-options'
1542 ;; can change them on a per-file basis.
1543 ;;
1544 (byte-compile-verbose byte-compile-verbose)
1545 (byte-optimize byte-optimize)
d82e848c
RS
1546 (byte-compile-dynamic byte-compile-dynamic)
1547 (byte-compile-dynamic-docstrings
1548 byte-compile-dynamic-docstrings)
52799cb8
RS
1549;; (byte-compile-generate-emacs19-bytecodes
1550;; byte-compile-generate-emacs19-bytecodes)
cf637a34 1551 (byte-compile-warnings byte-compile-warnings)
1c393159
JB
1552 )
1553 body)))
1554
1c393159 1555(defmacro displaying-byte-compile-warnings (&rest body)
4390021b
RS
1556 `(let* ((--displaying-byte-compile-warnings-fn (lambda () ,@body))
1557 (warning-series-started
1558 (and (markerp warning-series)
1559 (eq (marker-buffer warning-series)
1560 (get-buffer "*Compile-Log*")))))
95c997fa 1561 (byte-compile-find-cl-functions)
4390021b
RS
1562 (if (or (eq warning-series 'byte-compile-warning-series)
1563 warning-series-started)
1564 ;; warning-series does come from compilation,
1565 ;; so don't bind it, but maybe do set it.
1566 (let (tem)
1567 ;; Log the file name. Record position of that text.
1568 (setq tem (byte-compile-log-file))
1569 (unless warning-series-started
1570 (setq warning-series (or tem 'byte-compile-warning-series)))
1571 (if byte-compile-debug
1572 (funcall --displaying-byte-compile-warnings-fn)
1573 (condition-case error-info
1574 (funcall --displaying-byte-compile-warnings-fn)
1575 (error (byte-compile-report-error error-info)))))
1576 ;; warning-series does not come from compilation, so bind it.
1577 (let ((warning-series
1578 ;; Log the file name. Record position of that text.
1579 (or (byte-compile-log-file) 'byte-compile-warning-series)))
1580 (if byte-compile-debug
22788fb8 1581 (funcall --displaying-byte-compile-warnings-fn)
4390021b
RS
1582 (condition-case error-info
1583 (funcall --displaying-byte-compile-warnings-fn)
1584 (error (byte-compile-report-error error-info))))))))
1c393159 1585\f
fd5285f3 1586;;;###autoload
9742dbc0
RS
1587(defun byte-force-recompile (directory)
1588 "Recompile every `.el' file in DIRECTORY that already has a `.elc' file.
1589Files in subdirectories of DIRECTORY are processed also."
c0f43df5 1590 (interactive "DByte force recompile (directory): ")
9742dbc0
RS
1591 (byte-recompile-directory directory nil t))
1592
1c3b663f
GM
1593;; The `bytecomp-' prefix is applied to all local variables with
1594;; otherwise common names in this and similar functions for the sake
1595;; of the boundp test in byte-compile-variable-ref.
1596;; http://lists.gnu.org/archive/html/emacs-devel/2008-01/msg00237.html
1597;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2008-02/msg00134.html
8480fc7c 1598;; Note that similar considerations apply to command-line-1 in startup.el.
9742dbc0 1599;;;###autoload
1c3b663f
GM
1600(defun byte-recompile-directory (bytecomp-directory &optional bytecomp-arg
1601 bytecomp-force)
1602 "Recompile every `.el' file in BYTECOMP-DIRECTORY that needs recompilation.
2b9c3b12 1603This happens when a `.elc' file exists but is older than the `.el' file.
1c3b663f 1604Files in subdirectories of BYTECOMP-DIRECTORY are processed also.
1c393159 1605
c4f2cabd 1606If the `.elc' file does not exist, normally this function *does not*
1c3b663f
GM
1607compile the corresponding `.el' file. However, if the prefix argument
1608BYTECOMP-ARG is 0, that means do compile all those files. A nonzero
1609BYTECOMP-ARG means ask the user, for each such `.el' file, whether to
1610compile it. A nonzero BYTECOMP-ARG also means ask about each subdirectory
1611before scanning it.
1612
1613If the third argument BYTECOMP-FORCE is non-nil, recompile every `.el' file
1614that already has a `.elc' file."
1c393159 1615 (interactive "DByte recompile directory: \nP")
1c3b663f
GM
1616 (if bytecomp-arg
1617 (setq bytecomp-arg (prefix-numeric-value bytecomp-arg)))
e27c3564
JB
1618 (if noninteractive
1619 nil
1620 (save-some-buffers)
ba901388 1621 (force-mode-line-update))
008e2c2a 1622 (with-current-buffer (get-buffer-create "*Compile-Log*")
1c3b663f 1623 (setq default-directory (expand-file-name bytecomp-directory))
977f31f8
RS
1624 ;; compilation-mode copies value of default-directory.
1625 (unless (eq major-mode 'compilation-mode)
1626 (compilation-mode))
1c3b663f 1627 (let ((bytecomp-directories (list default-directory))
4eb4926c
RS
1628 (default-directory default-directory)
1629 (skip-count 0)
1630 (fail-count 0)
1631 (file-count 0)
1632 (dir-count 0)
1633 last-dir)
1634 (displaying-byte-compile-warnings
1c3b663f
GM
1635 (while bytecomp-directories
1636 (setq bytecomp-directory (car bytecomp-directories))
1637 (message "Checking %s..." bytecomp-directory)
1638 (let ((bytecomp-files (directory-files bytecomp-directory))
1639 bytecomp-source bytecomp-dest)
1640 (dolist (bytecomp-file bytecomp-files)
1641 (setq bytecomp-source
1642 (expand-file-name bytecomp-file bytecomp-directory))
1643 (if (and (not (member bytecomp-file '("RCS" "CVS")))
1644 (not (eq ?\. (aref bytecomp-file 0)))
1645 (file-directory-p bytecomp-source)
1646 (not (file-symlink-p bytecomp-source)))
4eb4926c 1647 ;; This file is a subdirectory. Handle them differently.
1c3b663f
GM
1648 (when (or (null bytecomp-arg)
1649 (eq 0 bytecomp-arg)
1650 (y-or-n-p (concat "Check " bytecomp-source "? ")))
1651 (setq bytecomp-directories
1652 (nconc bytecomp-directories (list bytecomp-source))))
4eb4926c 1653 ;; It is an ordinary file. Decide whether to compile it.
1c3b663f
GM
1654 (if (and (string-match emacs-lisp-file-regexp bytecomp-source)
1655 (file-readable-p bytecomp-source)
1656 (not (auto-save-file-name-p bytecomp-source))
1657 (setq bytecomp-dest
1658 (byte-compile-dest-file bytecomp-source))
1659 (if (file-exists-p bytecomp-dest)
4eb4926c 1660 ;; File was already compiled.
1c3b663f
GM
1661 (or bytecomp-force
1662 (file-newer-than-file-p bytecomp-source
1663 bytecomp-dest))
4eb4926c 1664 ;; No compiled file exists yet.
1c3b663f
GM
1665 (and bytecomp-arg
1666 (or (eq 0 bytecomp-arg)
1667 (y-or-n-p (concat "Compile "
1668 bytecomp-source "? "))))))
4eb4926c 1669 (progn (if (and noninteractive (not byte-compile-verbose))
1c3b663f
GM
1670 (message "Compiling %s..." bytecomp-source))
1671 (let ((bytecomp-res (byte-compile-file
1672 bytecomp-source)))
1673 (cond ((eq bytecomp-res 'no-byte-compile)
4eb4926c 1674 (setq skip-count (1+ skip-count)))
1c3b663f 1675 ((eq bytecomp-res t)
4eb4926c 1676 (setq file-count (1+ file-count)))
1c3b663f 1677 ((eq bytecomp-res nil)
4eb4926c
RS
1678 (setq fail-count (1+ fail-count)))))
1679 (or noninteractive
1c3b663f
GM
1680 (message "Checking %s..." bytecomp-directory))
1681 (if (not (eq last-dir bytecomp-directory))
1682 (setq last-dir bytecomp-directory
4eb4926c
RS
1683 dir-count (1+ dir-count)))
1684 )))))
1c3b663f 1685 (setq bytecomp-directories (cdr bytecomp-directories))))
4eb4926c
RS
1686 (message "Done (Total of %d file%s compiled%s%s%s)"
1687 file-count (if (= file-count 1) "" "s")
1688 (if (> fail-count 0) (format ", %d failed" fail-count) "")
1689 (if (> skip-count 0) (format ", %d skipped" skip-count) "")
1c3b663f
GM
1690 (if (> dir-count 1)
1691 (format " in %d directories" dir-count) "")))))
1c393159 1692
fef3407e 1693(defvar no-byte-compile nil
2b9c3b12 1694 "Non-nil to prevent byte-compiling of Emacs Lisp code.
fef3407e
SM
1695This is normally set in local file variables at the end of the elisp file:
1696
1697;; Local Variables:\n;; no-byte-compile: t\n;; End: ")
631c8020 1698;;;###autoload(put 'no-byte-compile 'safe-local-variable 'booleanp)
fef3407e 1699
fd5285f3 1700;;;###autoload
1c3b663f
GM
1701(defun byte-compile-file (bytecomp-filename &optional load)
1702 "Compile a file of Lisp code named BYTECOMP-FILENAME into a file of byte code.
1703The output file's name is generated by passing BYTECOMP-FILENAME to the
f279aaab 1704function `byte-compile-dest-file' (which see).
3614fc84 1705With prefix arg (noninteractively: 2nd arg), LOAD the file after compiling.
d90a41e8 1706The value is non-nil if there were no errors, nil if errors."
1c393159
JB
1707;; (interactive "fByte compile file: \nP")
1708 (interactive
1c3b663f
GM
1709 (let ((bytecomp-file buffer-file-name)
1710 (bytecomp-file-name nil)
1711 (bytecomp-file-dir nil))
1712 (and bytecomp-file
1c393159
JB
1713 (eq (cdr (assq 'major-mode (buffer-local-variables)))
1714 'emacs-lisp-mode)
1c3b663f
GM
1715 (setq bytecomp-file-name (file-name-nondirectory bytecomp-file)
1716 bytecomp-file-dir (file-name-directory bytecomp-file)))
52799cb8
RS
1717 (list (read-file-name (if current-prefix-arg
1718 "Byte compile and load file: "
1719 "Byte compile file: ")
1c3b663f 1720 bytecomp-file-dir bytecomp-file-name nil)
fd5285f3 1721 current-prefix-arg)))
1c393159 1722 ;; Expand now so we get the current buffer's defaults
1c3b663f 1723 (setq bytecomp-filename (expand-file-name bytecomp-filename))
1c393159
JB
1724
1725 ;; If we're compiling a file that's in a buffer and is modified, offer
1726 ;; to save it first.
1727 (or noninteractive
1c3b663f 1728 (let ((b (get-file-buffer (expand-file-name bytecomp-filename))))
1c393159 1729 (if (and b (buffer-modified-p b)
a586093f 1730 (y-or-n-p (format "Save buffer %s first? " (buffer-name b))))
008e2c2a 1731 (with-current-buffer b (save-buffer)))))
1c393159 1732
4390021b
RS
1733 ;; Force logging of the file name for each file compiled.
1734 (setq byte-compile-last-logged-file nil)
1c3b663f 1735 (let ((byte-compile-current-file bytecomp-filename)
ab5111e3 1736 (byte-compile-current-group nil)
dc14ae36 1737 (set-auto-coding-for-load t)
d82e848c
RS
1738 target-file input-buffer output-buffer
1739 byte-compile-dest-file)
1c3b663f 1740 (setq target-file (byte-compile-dest-file bytecomp-filename))
d82e848c 1741 (setq byte-compile-dest-file target-file)
ea1cb2bd 1742 (with-current-buffer
008e2c2a 1743 (setq input-buffer (get-buffer-create " *Compiler Input*"))
1c393159 1744 (erase-buffer)
7a28e3b1 1745 (setq buffer-file-coding-system nil)
746dd298 1746 ;; Always compile an Emacs Lisp file as multibyte
b92dd692 1747 ;; unless the file itself forces unibyte with -*-coding: raw-text;-*-
746dd298 1748 (set-buffer-multibyte t)
1c3b663f 1749 (insert-file-contents bytecomp-filename)
844da0ff
KH
1750 ;; Mimic the way after-insert-file-set-coding can make the
1751 ;; buffer unibyte when visiting this file.
7a28e3b1
RS
1752 (when (or (eq last-coding-system-used 'no-conversion)
1753 (eq (coding-system-type last-coding-system-used) 5))
1754 ;; For coding systems no-conversion and raw-text...,
1755 ;; edit the buffer as unibyte.
1756 (set-buffer-multibyte nil))
1c393159
JB
1757 ;; Run hooks including the uncompression hook.
1758 ;; If they change the file name, then change it for the output also.
14acf2f5
SM
1759 (letf ((buffer-file-name bytecomp-filename)
1760 ((default-value 'major-mode) 'emacs-lisp-mode)
1761 ;; Ignore unsafe local variables.
1762 ;; We only care about a few of them for our purposes.
1763 (enable-local-variables :safe)
1764 (enable-local-eval nil))
aa9addfa
RS
1765 ;; Arg of t means don't alter enable-local-variables.
1766 (normal-mode t)
1c3b663f 1767 (setq bytecomp-filename buffer-file-name))
cd891e68 1768 ;; Set the default directory, in case an eval-when-compile uses it.
1c3b663f 1769 (setq default-directory (file-name-directory bytecomp-filename)))
3614fc84
GM
1770 ;; Check if the file's local variables explicitly specify not to
1771 ;; compile this file.
fef3407e 1772 (if (with-current-buffer input-buffer no-byte-compile)
3614fc84 1773 (progn
6b61353c 1774 ;; (message "%s not compiled because of `no-byte-compile: %s'"
1c3b663f 1775 ;; (file-relative-name bytecomp-filename)
6b61353c
KH
1776 ;; (with-current-buffer input-buffer no-byte-compile))
1777 (when (file-exists-p target-file)
1778 (message "%s deleted because of `no-byte-compile: %s'"
1779 (file-relative-name target-file)
1780 (buffer-local-value 'no-byte-compile input-buffer))
1781 (condition-case nil (delete-file target-file) (error nil)))
82345a9a 1782 ;; We successfully didn't compile this file.
d90a41e8 1783 'no-byte-compile)
ccb3c8de 1784 (when byte-compile-verbose
1c3b663f 1785 (message "Compiling %s..." bytecomp-filename))
82345a9a
SM
1786 (setq byte-compiler-error-flag nil)
1787 ;; It is important that input-buffer not be current at this call,
1788 ;; so that the value of point set in input-buffer
1789 ;; within byte-compile-from-buffer lingers in that buffer.
4f6d5bf0
SM
1790 (setq output-buffer
1791 (save-current-buffer
1c3b663f 1792 (byte-compile-from-buffer input-buffer bytecomp-filename)))
82345a9a
SM
1793 (if byte-compiler-error-flag
1794 nil
ccb3c8de 1795 (when byte-compile-verbose
1c3b663f 1796 (message "Compiling %s...done" bytecomp-filename))
82345a9a
SM
1797 (kill-buffer input-buffer)
1798 (with-current-buffer output-buffer
1799 (goto-char (point-max))
1800 (insert "\n") ; aaah, unix.
82345a9a
SM
1801 (if (file-writable-p target-file)
1802 ;; We must disable any code conversion here.
1803 (let ((coding-system-for-write 'no-conversion))
1804 (if (memq system-type '(ms-dos 'windows-nt))
1805 (setq buffer-file-type t))
1806 (when (file-exists-p target-file)
1807 ;; Remove the target before writing it, so that any
1808 ;; hard-links continue to point to the old file (this makes
1809 ;; it possible for installed files to share disk space with
1810 ;; the build tree, without causing problems when emacs-lisp
1811 ;; files in the build tree are recompiled).
1812 (delete-file target-file))
ba76e7fa 1813 (write-region (point-min) (point-max) target-file))
82345a9a
SM
1814 ;; This is just to give a better error message than write-region
1815 (signal 'file-error
1816 (list "Opening output file"
1817 (if (file-exists-p target-file)
1818 "cannot overwrite file"
1819 "directory not writable or nonexistent")
7c2fb837 1820 target-file)))
82345a9a
SM
1821 (kill-buffer (current-buffer)))
1822 (if (and byte-compile-generate-call-tree
1823 (or (eq t byte-compile-generate-call-tree)
1c3b663f
GM
1824 (y-or-n-p (format "Report call tree for %s? "
1825 bytecomp-filename))))
82345a9a 1826 (save-excursion
1c3b663f 1827 (display-call-tree bytecomp-filename)))
82345a9a
SM
1828 (if load
1829 (load target-file))
1830 t))))
1c393159 1831
52799cb8
RS
1832;;(defun byte-compile-and-load-file (&optional filename)
1833;; "Compile a file of Lisp code named FILENAME into a file of byte code,
c5091f25 1834;;and then load it. The output file's name is made by appending \"c\" to
52799cb8
RS
1835;;the end of FILENAME."
1836;; (interactive)
1837;; (if filename ; I don't get it, (interactive-p) doesn't always work
1838;; (byte-compile-file filename t)
1839;; (let ((current-prefix-arg '(4)))
1840;; (call-interactively 'byte-compile-file))))
1841
1842;;(defun byte-compile-buffer (&optional buffer)
1843;; "Byte-compile and evaluate contents of BUFFER (default: the current buffer)."
1844;; (interactive "bByte compile buffer: ")
1845;; (setq buffer (if buffer (get-buffer buffer) (current-buffer)))
1846;; (message "Compiling %s..." (buffer-name buffer))
1847;; (let* ((filename (or (buffer-file-name buffer)
1848;; (concat "#<buffer " (buffer-name buffer) ">")))
1849;; (byte-compile-current-file buffer))
8a5dd086 1850;; (byte-compile-from-buffer buffer nil))
52799cb8
RS
1851;; (message "Compiling %s...done" (buffer-name buffer))
1852;; t)
1c393159
JB
1853
1854;;; compiling a single function
fd5285f3 1855;;;###autoload
52799cb8 1856(defun compile-defun (&optional arg)
1c393159 1857 "Compile and evaluate the current top-level form.
6b61353c 1858Print the result in the echo area.
2b9c3b12 1859With argument ARG, insert value in current buffer after the form."
1c393159
JB
1860 (interactive "P")
1861 (save-excursion
1862 (end-of-defun)
1863 (beginning-of-defun)
1864 (let* ((byte-compile-current-file nil)
ccb3c8de
CW
1865 (byte-compile-current-buffer (current-buffer))
1866 (byte-compile-read-position (point))
1867 (byte-compile-last-position byte-compile-read-position)
1c393159 1868 (byte-compile-last-warned-form 'nothing)
ccb3c8de 1869 (value (eval
9cb9a7bc 1870 (let ((read-with-symbol-positions (current-buffer))
ccb3c8de
CW
1871 (read-symbol-positions-list nil))
1872 (displaying-byte-compile-warnings
1873 (byte-compile-sexp (read (current-buffer))))))))
1c393159
JB
1874 (cond (arg
1875 (message "Compiling from buffer... done.")
1876 (prin1 value (current-buffer))
1877 (insert "\n"))
1878 ((message "%s" (prin1-to-string value)))))))
1879
1880
a2b3fdbf 1881(defun byte-compile-from-buffer (bytecomp-inbuffer &optional bytecomp-filename)
8a5dd086 1882 ;; Filename is used for the loading-into-Emacs-18 error message.
a2b3fdbf
GM
1883 (let (bytecomp-outbuffer
1884 (byte-compile-current-buffer bytecomp-inbuffer)
ccb3c8de
CW
1885 (byte-compile-read-position nil)
1886 (byte-compile-last-position nil)
d82e848c
RS
1887 ;; Prevent truncation of flonums and lists as we read and print them
1888 (float-output-format nil)
1889 (case-fold-search nil)
1890 (print-length nil)
95e7d933 1891 (print-level nil)
74dfd056
RS
1892 ;; Prevent edebug from interfering when we compile
1893 ;; and put the output into a file.
ccb3c8de
CW
1894;; (edebug-all-defs nil)
1895;; (edebug-all-forms nil)
d82e848c
RS
1896 ;; Simulate entry to byte-compile-top-level
1897 (byte-compile-constants nil)
1898 (byte-compile-variables nil)
1899 (byte-compile-tag-number 0)
1900 (byte-compile-depth 0)
1901 (byte-compile-maxdepth 0)
1902 (byte-compile-output nil)
ccb3c8de 1903 ;; This allows us to get the positions of symbols read; it's
bf247b6e 1904 ;; new in Emacs 22.1.
a2b3fdbf 1905 (read-with-symbol-positions bytecomp-inbuffer)
ccb3c8de 1906 (read-symbol-positions-list nil)
d82e848c 1907 ;; #### This is bound in b-c-close-variables.
cf637a34 1908 ;; (byte-compile-warnings byte-compile-warnings)
d82e848c
RS
1909 )
1910 (byte-compile-close-variables
b8104a2b 1911 (with-current-buffer
a2b3fdbf 1912 (setq bytecomp-outbuffer (get-buffer-create " *Compiler Output*"))
08b59cd3 1913 (set-buffer-multibyte t)
d82e848c
RS
1914 (erase-buffer)
1915 ;; (emacs-lisp-mode)
1916 (setq case-fold-search nil)
d82e848c
RS
1917 ;; This is a kludge. Some operating systems (OS/2, DOS) need to
1918 ;; write files containing binary information specially.
1919 ;; Under most circumstances, such files will be in binary
1920 ;; overwrite mode, so those OS's use that flag to guess how
1921 ;; they should write their data. Advise them that .elc files
1922 ;; need to be written carefully.
1923 (setq overwrite-mode 'overwrite-mode-binary))
1924 (displaying-byte-compile-warnings
a2b3fdbf
GM
1925 (and bytecomp-filename
1926 (byte-compile-insert-header bytecomp-filename bytecomp-inbuffer
1927 bytecomp-outbuffer))
1928 (with-current-buffer bytecomp-inbuffer
b8104a2b 1929 (goto-char (point-min))
2cb63a7c
AM
1930 ;; Should we always do this? When calling multiple files, it
1931 ;; would be useful to delay this warning until all have been
1932 ;; compiled. A: Yes! b-c-u-f might contain dross from a
1933 ;; previous byte-compile.
1934 (setq byte-compile-unresolved-functions nil)
d82e848c
RS
1935
1936 ;; Compile the forms from the input buffer.
1937 (while (progn
1938 (while (progn (skip-chars-forward " \t\n\^l")
1939 (looking-at ";"))
1940 (forward-line 1))
1941 (not (eobp)))
ccb3c8de
CW
1942 (setq byte-compile-read-position (point)
1943 byte-compile-last-position byte-compile-read-position)
36e65f70 1944 (let* ((old-style-backquotes nil)
a2b3fdbf 1945 (form (read bytecomp-inbuffer)))
36e65f70
SM
1946 ;; Warn about the use of old-style backquotes.
1947 (when old-style-backquotes
1948 (byte-compile-warn "!! The file uses old-style backquotes !!
1949This functionality has been obsolete for more than 10 years already
1950and will be removed soon. See (elisp)Backquote in the manual."))
ccb3c8de 1951 (byte-compile-file-form form)))
d82e848c
RS
1952 ;; Compile pending forms at end of file.
1953 (byte-compile-flush-pending)
977f31f8
RS
1954 ;; Make warnings about unresolved functions
1955 ;; give the end of the file as their position.
1956 (setq byte-compile-last-position (point-max))
2cb63a7c 1957 (byte-compile-warn-about-unresolved-functions))
fb639443
RS
1958 ;; Fix up the header at the front of the output
1959 ;; if the buffer contains multibyte characters.
a2b3fdbf
GM
1960 (and bytecomp-filename
1961 (byte-compile-fix-header bytecomp-filename bytecomp-inbuffer
1962 bytecomp-outbuffer))))
1963 bytecomp-outbuffer))
8a5dd086 1964
fb639443 1965(defun byte-compile-fix-header (filename inbuffer outbuffer)
6c2161c4 1966 (with-current-buffer outbuffer
fb639443 1967 ;; See if the buffer has any multibyte characters.
447a052b 1968 (when (< (point-max) (position-bytes (point-max)))
fb639443
RS
1969 (goto-char (point-min))
1970 ;; Find the comment that describes the version test.
1971 (search-forward "\n;;; This file")
1972 (beginning-of-line)
1973 (narrow-to-region (point) (point-max))
1974 ;; Find the line of ballast semicolons.
1975 (search-forward ";;;;;;;;;;")
1976 (beginning-of-line)
1977
1978 (narrow-to-region (point-min) (point))
1979 (let ((old-header-end (point))
1980 delta)
1981 (goto-char (point-min))
1982 (delete-region (point) (progn (re-search-forward "^(")
1983 (beginning-of-line)
1984 (point)))
351697be 1985 (insert ";;; This file contains utf-8 non-ASCII characters\n"
8c914fdb
SZ
1986 ";;; and therefore cannot be loaded into Emacs 22 or earlier.\n")
1987 ;; Replace "19" or "19.29" with "23", twice.
fb639443 1988 (re-search-forward "19\\(\\.[0-9]+\\)")
8589dc17 1989 (replace-match "23")
fb639443 1990 (re-search-forward "19\\(\\.[0-9]+\\)")
8589dc17 1991 (replace-match "23")
fb639443
RS
1992 ;; Now compensate for the change in size,
1993 ;; to make sure all positions in the file remain valid.
1994 (setq delta (- (point-max) old-header-end))
1995 (goto-char (point-max))
1996 (widen)
1997 (delete-char delta)))))
1998
f1f32df9 1999(defun byte-compile-insert-header (filename inbuffer outbuffer)
a5832373
RS
2000 (with-current-buffer inbuffer
2001 (let ((dynamic-docstrings byte-compile-dynamic-docstrings)
2002 (dynamic byte-compile-dynamic))
2003 (set-buffer outbuffer)
2004 (goto-char (point-min))
2005 ;; The magic number of .elc files is ";ELC", or 0x3B454C43. After
430d2ee2 2006 ;; that is the file-format version number (18, 19, 20, or 23) as a
a5832373
RS
2007 ;; byte, followed by some nulls. The primary motivation for doing
2008 ;; this is to get some binary characters up in the first line of
2009 ;; the file so that `diff' will simply say "Binary files differ"
2010 ;; instead of actually doing a diff of two .elc files. An extra
2011 ;; benefit is that you can add this to /etc/magic:
2012
2013 ;; 0 string ;ELC GNU Emacs Lisp compiled file,
2014 ;; >4 byte x version %d
2015
e5c89ce9 2016 (insert ";ELC" 23 "\000\000\000\n")
a5832373
RS
2017 (insert ";;; Compiled by "
2018 (or (and (boundp 'user-mail-address) user-mail-address)
2019 (concat (user-login-name) "@" (system-name)))
2020 " on "
2021 (current-time-string) "\n;;; from file " filename "\n")
2022 (insert ";;; in Emacs version " emacs-version "\n")
2023 (insert ";;; "
2024 (cond
2025 ((eq byte-optimize 'source) "with source-level optimization only")
2026 ((eq byte-optimize 'byte) "with byte-level optimization only")
2027 (byte-optimize "with all optimizations")
2028 (t "without optimization"))
e5c89ce9 2029 ".\n")
a5832373
RS
2030 (if dynamic
2031 (insert ";;; Function definitions are lazy-loaded.\n"))
e5c89ce9
GM
2032 (let (intro-string minimum-version)
2033 ;; Figure out which Emacs version to require,
2034 ;; and what comment to use to explain why.
2035 ;; Note that this fails to take account of whether
2036 ;; the buffer contains multibyte characters. We may have to
2037 ;; compensate at the end in byte-compile-fix-header.
2038 (if dynamic-docstrings
fb639443 2039 (setq intro-string
e5c89ce9
GM
2040 ";;; This file uses dynamic docstrings, first added in Emacs 19.29.\n"
2041 minimum-version "19.29")
2042 (setq intro-string
2043 ";;; This file uses opcodes which do not exist in Emacs 18.\n"
2044 minimum-version "19"))
2045 ;; Now insert the comment and the error check.
2046 (insert
2047 "\n"
2048 intro-string
2049 ;; Have to check if emacs-version is bound so that this works
2050 ;; in files loaded early in loadup.el.
2051 "(if (and (boundp 'emacs-version)\n"
2052 ;; If there is a name at the end of emacs-version,
2053 ;; don't try to check the version number.
2054 "\t (< (aref emacs-version (1- (length emacs-version))) ?A)\n"
2055 "\t (or (and (boundp 'epoch::version) epoch::version)\n"
2056 (format "\t (string-lessp emacs-version \"%s\")))\n"
2057 minimum-version)
2058 " (error \"`"
2059 ;; prin1-to-string is used to quote backslashes.
2060 (substring (prin1-to-string (file-name-nondirectory filename))
2061 1 -1)
2062 (format "' was compiled for Emacs %s or later\"))\n\n"
2063 minimum-version)
2064 ;; Insert semicolons as ballast, so that byte-compile-fix-header
2065 ;; can delete them so as to keep the buffer positions
2066 ;; constant for the actual compiled code.
2067 ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n\n")))))
1c393159 2068
a2b3fdbf
GM
2069;; Dynamically bound in byte-compile-from-buffer.
2070;; NB also used in cl.el and cl-macs.el.
2071(defvar bytecomp-outbuffer)
2072
1c393159
JB
2073(defun byte-compile-output-file-form (form)
2074 ;; writes the given form to the output buffer, being careful of docstrings
36b7e523
RS
2075 ;; in defun, defmacro, defvar, defconst, autoload and
2076 ;; custom-declare-variable because make-docfile is so amazingly stupid.
c36881cf
ER
2077 ;; defalias calls are output directly by byte-compile-file-form-defmumble;
2078 ;; it does not pay to first build the defalias in defmumble and then parse
2079 ;; it here.
36b7e523
RS
2080 (if (and (memq (car-safe form) '(defun defmacro defvar defconst autoload
2081 custom-declare-variable))
1c393159 2082 (stringp (nth 3 form)))
d82e848c 2083 (byte-compile-output-docform nil nil '("\n(" 3 ")") form nil
36b7e523
RS
2084 (memq (car form)
2085 '(autoload custom-declare-variable)))
1c393159 2086 (let ((print-escape-newlines t)
37c29340
KH
2087 (print-length nil)
2088 (print-level nil)
77308fd7 2089 (print-quoted t)
5e51de79 2090 (print-gensym t)
0e66b003
KH
2091 (print-circle ; handle circular data structures
2092 (not byte-compile-disable-print-circle)))
a2b3fdbf
GM
2093 (princ "\n" bytecomp-outbuffer)
2094 (prin1 form bytecomp-outbuffer)
1c393159
JB
2095 nil)))
2096
6c2161c4
SM
2097(defvar print-gensym-alist) ;Used before print-circle existed.
2098
d82e848c 2099(defun byte-compile-output-docform (preface name info form specindex quoted)
dac6f673
RS
2100 "Print a form with a doc string. INFO is (prefix doc-index postfix).
2101If PREFACE and NAME are non-nil, print them too,
2102before INFO and the FORM but after the doc string itself.
2103If SPECINDEX is non-nil, it is the index in FORM
2104of the function bytecode string. In that case,
2b9c3b12
JB
2105we output that argument and the following argument
2106\(the constants vector) together, for lazy loading.
dac6f673
RS
2107QUOTED says that we have to put a quote before the
2108list that represents a doc string reference.
36b7e523 2109`autoload' and `custom-declare-variable' need that."
dac6f673
RS
2110 ;; We need to examine byte-compile-dynamic-docstrings
2111 ;; in the input buffer (now current), not in the output buffer.
2112 (let ((dynamic-docstrings byte-compile-dynamic-docstrings))
a2b3fdbf 2113 (with-current-buffer bytecomp-outbuffer
9ec5dfe6
SM
2114 (let (position)
2115
2116 ;; Insert the doc string, and make it a comment with #@LENGTH.
2117 (and (>= (nth 1 info) 0)
2118 dynamic-docstrings
9ec5dfe6
SM
2119 (progn
2120 ;; Make the doc string start at beginning of line
2121 ;; for make-docfile's sake.
2122 (insert "\n")
2123 (setq position
2124 (byte-compile-output-as-comment
2125 (nth (nth 1 info) form) nil))
2126 (setq position (- (position-bytes position) (point-min) -1))
2127 ;; If the doc string starts with * (a user variable),
2128 ;; negate POSITION.
2129 (if (and (stringp (nth (nth 1 info) form))
2130 (> (length (nth (nth 1 info) form)) 0)
2131 (eq (aref (nth (nth 1 info) form) 0) ?*))
2132 (setq position (- position)))))
2133
2134 (if preface
2135 (progn
2136 (insert preface)
a2b3fdbf 2137 (prin1 name bytecomp-outbuffer)))
9ec5dfe6
SM
2138 (insert (car info))
2139 (let ((print-escape-newlines t)
2140 (print-quoted t)
2141 ;; For compatibility with code before print-circle,
2142 ;; use a cons cell to say that we want
2143 ;; print-gensym-alist not to be cleared
2144 ;; between calls to print functions.
2145 (print-gensym '(t))
2146 (print-circle ; handle circular data structures
2147 (not byte-compile-disable-print-circle))
2148 print-gensym-alist ; was used before print-circle existed.
2149 (print-continuous-numbering t)
2150 print-number-table
2151 (index 0))
a2b3fdbf 2152 (prin1 (car form) bytecomp-outbuffer)
9ec5dfe6
SM
2153 (while (setq form (cdr form))
2154 (setq index (1+ index))
2155 (insert " ")
2156 (cond ((and (numberp specindex) (= index specindex)
2157 ;; Don't handle the definition dynamically
2158 ;; if it refers (or might refer)
2159 ;; to objects already output
2160 ;; (for instance, gensyms in the arg list).
2161 (let (non-nil)
2162 (dotimes (i (length print-number-table))
2163 (if (aref print-number-table i)
2164 (setq non-nil t)))
2165 (not non-nil)))
2166 ;; Output the byte code and constants specially
2167 ;; for lazy dynamic loading.
2168 (let ((position
2169 (byte-compile-output-as-comment
2170 (cons (car form) (nth 1 form))
2171 t)))
2172 (setq position (- (position-bytes position) (point-min) -1))
a2b3fdbf 2173 (princ (format "(#$ . %d) nil" position) bytecomp-outbuffer)
9ec5dfe6
SM
2174 (setq form (cdr form))
2175 (setq index (1+ index))))
2176 ((= index (nth 1 info))
2177 (if position
2178 (princ (format (if quoted "'(#$ . %d)" "(#$ . %d)")
2179 position)
a2b3fdbf 2180 bytecomp-outbuffer)
9ec5dfe6
SM
2181 (let ((print-escape-newlines nil))
2182 (goto-char (prog1 (1+ (point))
a2b3fdbf 2183 (prin1 (car form) bytecomp-outbuffer)))
9ec5dfe6
SM
2184 (insert "\\\n")
2185 (goto-char (point-max)))))
2186 (t
a2b3fdbf 2187 (prin1 (car form) bytecomp-outbuffer)))))
9ec5dfe6 2188 (insert (nth 2 info)))))
1c393159
JB
2189 nil)
2190
c2768569 2191(defun byte-compile-keep-pending (form &optional bytecomp-handler)
1c393159
JB
2192 (if (memq byte-optimize '(t source))
2193 (setq form (byte-optimize-form form t)))
c2768569 2194 (if bytecomp-handler
1c393159
JB
2195 (let ((for-effect t))
2196 ;; To avoid consing up monstrously large forms at load time, we split
2197 ;; the output regularly.
b4ff4a23
RS
2198 (and (memq (car-safe form) '(fset defalias))
2199 (nthcdr 300 byte-compile-output)
1c393159 2200 (byte-compile-flush-pending))
c2768569 2201 (funcall bytecomp-handler form)
1c393159
JB
2202 (if for-effect
2203 (byte-compile-discard)))
2204 (byte-compile-form form t))
2205 nil)
2206
2207(defun byte-compile-flush-pending ()
2208 (if byte-compile-output
2209 (let ((form (byte-compile-out-toplevel t 'file)))
2210 (cond ((eq (car-safe form) 'progn)
ed62683d 2211 (mapc 'byte-compile-output-file-form (cdr form)))
1c393159
JB
2212 (form
2213 (byte-compile-output-file-form form)))
2214 (setq byte-compile-constants nil
2215 byte-compile-variables nil
2216 byte-compile-depth 0
2217 byte-compile-maxdepth 0
2218 byte-compile-output nil))))
2219
2220(defun byte-compile-file-form (form)
2221 (let ((byte-compile-current-form nil) ; close over this for warnings.
c2768569 2222 bytecomp-handler)
1c393159
JB
2223 (cond
2224 ((not (consp form))
2225 (byte-compile-keep-pending form))
2226 ((and (symbolp (car form))
c2768569
GM
2227 (setq bytecomp-handler (get (car form) 'byte-hunk-handler)))
2228 (cond ((setq form (funcall bytecomp-handler form))
1c393159
JB
2229 (byte-compile-flush-pending)
2230 (byte-compile-output-file-form form))))
2231 ((eq form (setq form (macroexpand form byte-compile-macro-environment)))
2232 (byte-compile-keep-pending form))
2233 (t
2234 (byte-compile-file-form form)))))
2235
2236;; Functions and variables with doc strings must be output separately,
2237;; so make-docfile can recognise them. Most other things can be output
2238;; as byte-code.
2239
2240(put 'defsubst 'byte-hunk-handler 'byte-compile-file-form-defsubst)
2241(defun byte-compile-file-form-defsubst (form)
6c2161c4
SM
2242 (when (assq (nth 1 form) byte-compile-unresolved-functions)
2243 (setq byte-compile-current-form (nth 1 form))
1d5c17c0 2244 (byte-compile-warn "defsubst `%s' was used before it was defined"
6c2161c4 2245 (nth 1 form)))
1c393159
JB
2246 (byte-compile-file-form
2247 (macroexpand form byte-compile-macro-environment))
2248 ;; Return nil so the form is not output twice.
2249 nil)
2250
2251(put 'autoload 'byte-hunk-handler 'byte-compile-file-form-autoload)
2252(defun byte-compile-file-form-autoload (form)
2253 (and (let ((form form))
2254 (while (if (setq form (cdr form)) (byte-compile-constp (car form))))
2255 (null form)) ;Constants only
2256 (eval (nth 5 form)) ;Macro
2257 (eval form)) ;Define the autoload.
c5091f25 2258 ;; Avoid undefined function warnings for the autoload.
cb4fb1d0 2259 (when (and (consp (nth 1 form))
c5091f25
DL
2260 (eq (car (nth 1 form)) 'quote)
2261 (consp (cdr (nth 1 form)))
2262 (symbolp (nth 1 (nth 1 form))))
cb4fb1d0
GM
2263 (push (cons (nth 1 (nth 1 form))
2264 (cons 'autoload (cdr (cdr form))))
2265 byte-compile-function-environment)
2266 ;; If an autoload occurs _before_ the first call to a function,
2267 ;; byte-compile-callargs-warn does not add an entry to
2268 ;; byte-compile-unresolved-functions. Here we mimic the logic
2269 ;; of byte-compile-callargs-warn so as not to warn if the
2270 ;; autoload comes _after_ the function call.
2271 ;; Alternatively, similar logic could go in
2272 ;; byte-compile-warn-about-unresolved-functions.
2273 (or (memq (nth 1 (nth 1 form)) byte-compile-noruntime-functions)
2274 (setq byte-compile-unresolved-functions
2275 (delq (assq (nth 1 (nth 1 form))
2276 byte-compile-unresolved-functions)
2277 byte-compile-unresolved-functions))))
1c393159
JB
2278 (if (stringp (nth 3 form))
2279 form
2280 ;; No doc string, so we can compile this as a normal form.
2281 (byte-compile-keep-pending form 'byte-compile-normal-call)))
2282
2283(put 'defvar 'byte-hunk-handler 'byte-compile-file-form-defvar)
2284(put 'defconst 'byte-hunk-handler 'byte-compile-file-form-defvar)
2285(defun byte-compile-file-form-defvar (form)
2286 (if (null (nth 3 form))
2287 ;; Since there is no doc string, we can compile this as a normal form,
2288 ;; and not do a file-boundary.
2289 (byte-compile-keep-pending form)
2aea6521
GM
2290 (push (nth 1 form) byte-compile-bound-variables)
2291 (if (eq (car form) 'defconst)
2292 (push (nth 1 form) byte-compile-const-variables))
1c393159
JB
2293 (cond ((consp (nth 2 form))
2294 (setq form (copy-sequence form))
2295 (setcar (cdr (cdr form))
2296 (byte-compile-top-level (nth 2 form) nil 'file))))
2297 form))
2298
b7c76a30
SM
2299(put 'define-abbrev-table 'byte-hunk-handler 'byte-compile-file-form-define-abbrev-table)
2300(defun byte-compile-file-form-define-abbrev-table (form)
2aea6521
GM
2301 (if (eq 'quote (car-safe (car-safe (cdr form))))
2302 (push (car-safe (cdr (cadr form))) byte-compile-bound-variables))
b7c76a30
SM
2303 (byte-compile-keep-pending form))
2304
8c731d3d
RS
2305(put 'custom-declare-variable 'byte-hunk-handler
2306 'byte-compile-file-form-custom-declare-variable)
2307(defun byte-compile-file-form-custom-declare-variable (form)
cf637a34 2308 (when (byte-compile-warning-enabled-p 'callargs)
fe33e7c8 2309 (byte-compile-nogroup-warn form))
2aea6521 2310 (push (nth 1 (nth 1 form)) byte-compile-bound-variables)
2546bcdd
SM
2311 ;; Don't compile the expression because it may be displayed to the user.
2312 ;; (when (eq (car-safe (nth 2 form)) 'quote)
2313 ;; ;; (nth 2 form) is meant to evaluate to an expression, so if we have the
2314 ;; ;; final value already, we can byte-compile it.
2315 ;; (setcar (cdr (nth 2 form))
2316 ;; (byte-compile-top-level (cadr (nth 2 form)) nil 'file)))
347a36bc
RS
2317 (let ((tail (nthcdr 4 form)))
2318 (while tail
2546bcdd
SM
2319 (unless (keywordp (car tail)) ;No point optimizing keywords.
2320 ;; Compile the keyword arguments.
2321 (setcar tail (byte-compile-top-level (car tail) nil 'file)))
347a36bc 2322 (setq tail (cdr tail))))
8c731d3d
RS
2323 form)
2324
997011eb
RS
2325(put 'require 'byte-hunk-handler 'byte-compile-file-form-require)
2326(defun byte-compile-file-form-require (form)
3f12e5bd
GM
2327 (let ((args (mapcar 'eval (cdr form)))
2328 (hist-orig load-history)
2329 hist-new)
997011eb 2330 (apply 'require args)
3f12e5bd
GM
2331 (when (byte-compile-warning-enabled-p 'cl-functions)
2332 ;; Detect (require 'cl) in a way that works even if cl is already loaded.
2333 (if (member (car args) '("cl" cl))
2334 (progn
2335 (byte-compile-warn "cl package required at runtime")
2336 (byte-compile-disable-warning 'cl-functions))
2337 ;; We may have required something that causes cl to be loaded, eg
2338 ;; the uncompiled version of a file that requires cl when compiling.
2339 (setq hist-new load-history)
2340 (while (and (not byte-compile-cl-functions)
2341 hist-new (not (eq hist-new hist-orig)))
2342 (and (byte-compile-cl-file-p (car (pop hist-new)))
2343 (byte-compile-find-cl-functions))))))
1c393159
JB
2344 (byte-compile-keep-pending form 'byte-compile-normal-call))
2345
2346(put 'progn 'byte-hunk-handler 'byte-compile-file-form-progn)
2347(put 'prog1 'byte-hunk-handler 'byte-compile-file-form-progn)
2348(put 'prog2 'byte-hunk-handler 'byte-compile-file-form-progn)
2349(defun byte-compile-file-form-progn (form)
ed62683d 2350 (mapc 'byte-compile-file-form (cdr form))
1c393159
JB
2351 ;; Return nil so the forms are not output twice.
2352 nil)
2353
cb4fb1d0
GM
2354(put 'with-no-warnings 'byte-hunk-handler
2355 'byte-compile-file-form-with-no-warnings)
2356(defun byte-compile-file-form-with-no-warnings (form)
2357 ;; cf byte-compile-file-form-progn.
2358 (let (byte-compile-warnings)
2359 (mapc 'byte-compile-file-form (cdr form))
2360 nil))
2361
1c393159
JB
2362;; This handler is not necessary, but it makes the output from dont-compile
2363;; and similar macros cleaner.
2364(put 'eval 'byte-hunk-handler 'byte-compile-file-form-eval)
2365(defun byte-compile-file-form-eval (form)
2366 (if (eq (car-safe (nth 1 form)) 'quote)
2367 (nth 1 (nth 1 form))
2368 (byte-compile-keep-pending form)))
2369
2370(put 'defun 'byte-hunk-handler 'byte-compile-file-form-defun)
2371(defun byte-compile-file-form-defun (form)
2372 (byte-compile-file-form-defmumble form nil))
2373
2374(put 'defmacro 'byte-hunk-handler 'byte-compile-file-form-defmacro)
2375(defun byte-compile-file-form-defmacro (form)
2376 (byte-compile-file-form-defmumble form t))
2377
e3a6b82f
SM
2378(defun byte-compile-defmacro-declaration (form)
2379 "Generate code for declarations in macro definitions.
2380Remove declarations from the body of the macro definition
2381by side-effects."
2382 (let ((tail (nthcdr 2 form))
2383 (res '()))
2384 (when (stringp (car (cdr tail)))
2385 (setq tail (cdr tail)))
2386 (while (and (consp (car (cdr tail)))
2387 (eq (car (car (cdr tail))) 'declare))
2388 (let ((declaration (car (cdr tail))))
2389 (setcdr tail (cdr (cdr tail)))
2390 (push `(if macro-declaration-function
2391 (funcall macro-declaration-function
2392 ',(car (cdr form)) ',declaration))
2393 res)))
2394 res))
2395
1c393159 2396(defun byte-compile-file-form-defmumble (form macrop)
a2b3fdbf
GM
2397 (let* ((bytecomp-name (car (cdr form)))
2398 (bytecomp-this-kind (if macrop 'byte-compile-macro-environment
1c393159 2399 'byte-compile-function-environment))
a2b3fdbf 2400 (bytecomp-that-kind (if macrop 'byte-compile-function-environment
1c393159 2401 'byte-compile-macro-environment))
a2b3fdbf
GM
2402 (bytecomp-this-one (assq bytecomp-name
2403 (symbol-value bytecomp-this-kind)))
2404 (bytecomp-that-one (assq bytecomp-name
2405 (symbol-value bytecomp-that-kind)))
1c393159
JB
2406 (byte-compile-free-references nil)
2407 (byte-compile-free-assignments nil))
a2b3fdbf 2408 (byte-compile-set-symbol-position bytecomp-name)
1c393159
JB
2409 ;; When a function or macro is defined, add it to the call tree so that
2410 ;; we can tell when functions are not used.
2411 (if byte-compile-generate-call-tree
a2b3fdbf 2412 (or (assq bytecomp-name byte-compile-call-tree)
1c393159 2413 (setq byte-compile-call-tree
a2b3fdbf 2414 (cons (list bytecomp-name nil nil) byte-compile-call-tree))))
1c393159 2415
a2b3fdbf 2416 (setq byte-compile-current-form bytecomp-name) ; for warnings
cf637a34 2417 (if (byte-compile-warning-enabled-p 'redefine)
1c393159
JB
2418 (byte-compile-arglist-warn form macrop))
2419 (if byte-compile-verbose
a2b3fdbf
GM
2420 ;; bytecomp-filename is from byte-compile-from-buffer.
2421 (message "Compiling %s... (%s)" (or bytecomp-filename "") (nth 1 form)))
2422 (cond (bytecomp-that-one
cf637a34 2423 (if (and (byte-compile-warning-enabled-p 'redefine)
52799cb8 2424 ;; don't warn when compiling the stubs in byte-run...
1c393159
JB
2425 (not (assq (nth 1 form)
2426 byte-compile-initial-macro-environment)))
2427 (byte-compile-warn
1d5c17c0 2428 "`%s' defined multiple times, as both function and macro"
1c393159 2429 (nth 1 form)))
a2b3fdbf
GM
2430 (setcdr bytecomp-that-one nil))
2431 (bytecomp-this-one
cf637a34 2432 (when (and (byte-compile-warning-enabled-p 'redefine)
1c393159 2433 ;; hack: don't warn when compiling the magic internal
52799cb8 2434 ;; byte-compiler macros in byte-run.el...
1c393159
JB
2435 (not (assq (nth 1 form)
2436 byte-compile-initial-macro-environment)))
1d5c17c0 2437 (byte-compile-warn "%s `%s' defined multiple times in this file"
ccb3c8de
CW
2438 (if macrop "macro" "function")
2439 (nth 1 form))))
a2b3fdbf
GM
2440 ((and (fboundp bytecomp-name)
2441 (eq (car-safe (symbol-function bytecomp-name))
1c393159 2442 (if macrop 'lambda 'macro)))
cf637a34 2443 (when (byte-compile-warning-enabled-p 'redefine)
1d5c17c0 2444 (byte-compile-warn "%s `%s' being redefined as a %s"
ccb3c8de
CW
2445 (if macrop "function" "macro")
2446 (nth 1 form)
2447 (if macrop "macro" "function")))
1c393159 2448 ;; shadow existing definition
a2b3fdbf
GM
2449 (set bytecomp-this-kind
2450 (cons (cons bytecomp-name nil)
2451 (symbol-value bytecomp-this-kind))))
1c393159
JB
2452 )
2453 (let ((body (nthcdr 3 form)))
ccb3c8de
CW
2454 (when (and (stringp (car body))
2455 (symbolp (car-safe (cdr-safe body)))
2456 (car-safe (cdr-safe body))
2457 (stringp (car-safe (cdr-safe (cdr-safe body)))))
2458 (byte-compile-set-symbol-position (nth 1 form))
2459 (byte-compile-warn "probable `\"' without `\\' in doc string of %s"
2460 (nth 1 form))))
6b8c2efc 2461
985b4686
GM
2462 ;; Generate code for declarations in macro definitions.
2463 ;; Remove declarations from the body of the macro definition.
2464 (when macrop
e3a6b82f
SM
2465 (dolist (decl (byte-compile-defmacro-declaration form))
2466 (prin1 decl bytecomp-outbuffer)))
6b8c2efc 2467
4ec5239c 2468 (let* ((new-one (byte-compile-lambda (nthcdr 2 form) t))
1c393159 2469 (code (byte-compile-byte-code-maker new-one)))
a2b3fdbf
GM
2470 (if bytecomp-this-one
2471 (setcdr bytecomp-this-one new-one)
2472 (set bytecomp-this-kind
2473 (cons (cons bytecomp-name new-one)
2474 (symbol-value bytecomp-this-kind))))
1c393159
JB
2475 (if (and (stringp (nth 3 form))
2476 (eq 'quote (car-safe code))
2477 (eq 'lambda (car-safe (nth 1 code))))
2478 (cons (car form)
a2b3fdbf 2479 (cons bytecomp-name (cdr (nth 1 code))))
d82e848c 2480 (byte-compile-flush-pending)
1c393159 2481 (if (not (stringp (nth 3 form)))
d82e848c
RS
2482 ;; No doc string. Provide -1 as the "doc string index"
2483 ;; so that no element will be treated as a doc string.
2484 (byte-compile-output-docform
e5c89ce9 2485 "\n(defalias '"
a2b3fdbf 2486 bytecomp-name
d82e848c
RS
2487 (cond ((atom code)
2488 (if macrop '(" '(macro . #[" -1 "])") '(" #[" -1 "]")))
2489 ((eq (car code) 'quote)
2490 (setq code new-one)
2491 (if macrop '(" '(macro " -1 ")") '(" '(" -1 ")")))
2492 ((if macrop '(" (cons 'macro (" -1 "))") '(" (" -1 ")"))))
2493 (append code nil)
2494 (and (atom code) byte-compile-dynamic
2495 1)
2496 nil)
1c393159 2497 ;; Output the form by hand, that's much simpler than having
c36881cf 2498 ;; b-c-output-file-form analyze the defalias.
1c393159 2499 (byte-compile-output-docform
e5c89ce9 2500 "\n(defalias '"
a2b3fdbf 2501 bytecomp-name
1c393159
JB
2502 (cond ((atom code)
2503 (if macrop '(" '(macro . #[" 4 "])") '(" #[" 4 "]")))
2504 ((eq (car code) 'quote)
2505 (setq code new-one)
2506 (if macrop '(" '(macro " 2 ")") '(" '(" 2 ")")))
2507 ((if macrop '(" (cons 'macro (" 5 "))") '(" (" 5 ")"))))
d82e848c
RS
2508 (append code nil)
2509 (and (atom code) byte-compile-dynamic
2510 1)
2511 nil))
a2b3fdbf 2512 (princ ")" bytecomp-outbuffer)
d82e848c
RS
2513 nil))))
2514
2515;; Print Lisp object EXP in the output file, inside a comment,
2516;; and return the file position it will have.
2517;; If QUOTED is non-nil, print with quoting; otherwise, print without quoting.
2518(defun byte-compile-output-as-comment (exp quoted)
2d5975fa 2519 (let ((position (point)))
a2b3fdbf 2520 (with-current-buffer bytecomp-outbuffer
9ec5dfe6
SM
2521
2522 ;; Insert EXP, and make it a comment with #@LENGTH.
2523 (insert " ")
2524 (if quoted
a2b3fdbf
GM
2525 (prin1 exp bytecomp-outbuffer)
2526 (princ exp bytecomp-outbuffer))
9ec5dfe6
SM
2527 (goto-char position)
2528 ;; Quote certain special characters as needed.
2529 ;; get_doc_string in doc.c does the unquoting.
2530 (while (search-forward "\^A" nil t)
2531 (replace-match "\^A\^A" t t))
2532 (goto-char position)
2533 (while (search-forward "\000" nil t)
2534 (replace-match "\^A0" t t))
2535 (goto-char position)
2536 (while (search-forward "\037" nil t)
2537 (replace-match "\^A_" t t))
2538 (goto-char (point-max))
2539 (insert "\037")
2540 (goto-char position)
2541 (insert "#@" (format "%d" (- (position-bytes (point-max))
2542 (position-bytes position))))
2543
2544 ;; Save the file position of the object.
2545 ;; Note we should add 1 to skip the space
2546 ;; that we inserted before the actual doc string,
2547 ;; and subtract 1 to convert from an 1-origin Emacs position
2548 ;; to a file position; they cancel.
2549 (setq position (point))
2550 (goto-char (point-max)))
d82e848c
RS
2551 position))
2552
1c393159
JB
2553
2554\f
fd5285f3 2555;;;###autoload
1c393159
JB
2556(defun byte-compile (form)
2557 "If FORM is a symbol, byte-compile its function definition.
2558If FORM is a lambda or a macro, byte-compile it as a function."
2559 (displaying-byte-compile-warnings
2560 (byte-compile-close-variables
2561 (let* ((fun (if (symbolp form)
2562 (and (fboundp form) (symbol-function form))
2563 form))
2564 (macro (eq (car-safe fun) 'macro)))
2565 (if macro
2566 (setq fun (cdr fun)))
2567 (cond ((eq (car-safe fun) 'lambda)
2568 (setq fun (if macro
2569 (cons 'macro (byte-compile-lambda fun))
2570 (byte-compile-lambda fun)))
2571 (if (symbolp form)
c36881cf 2572 (defalias form fun)
1c393159
JB
2573 fun)))))))
2574
2575(defun byte-compile-sexp (sexp)
2576 "Compile and return SEXP."
2577 (displaying-byte-compile-warnings
2578 (byte-compile-close-variables
2579 (byte-compile-top-level sexp))))
2580
2581;; Given a function made by byte-compile-lambda, make a form which produces it.
2582(defun byte-compile-byte-code-maker (fun)
2583 (cond
1c393159
JB
2584 ;; ## atom is faster than compiled-func-p.
2585 ((atom fun) ; compiled function.
2586 ;; generate-emacs19-bytecodes must be on, otherwise byte-compile-lambda
2587 ;; would have produced a lambda.
469414a0 2588 fun)
1c393159 2589 ;; b-c-lambda didn't produce a compiled-function, so it's either a trivial
52799cb8 2590 ;; function, or this is Emacs 18, or generate-emacs19-bytecodes is off.
1c393159
JB
2591 ((let (tmp)
2592 (if (and (setq tmp (assq 'byte-code (cdr-safe (cdr fun))))
2593 (null (cdr (memq tmp fun))))
2594 ;; Generate a make-byte-code call.
2595 (let* ((interactive (assq 'interactive (cdr (cdr fun)))))
2596 (nconc (list 'make-byte-code
2597 (list 'quote (nth 1 fun)) ;arglist
2598 (nth 1 tmp) ;bytes
2599 (nth 2 tmp) ;consts
2600 (nth 3 tmp)) ;depth
2601 (cond ((stringp (nth 2 fun))
2602 (list (nth 2 fun))) ;doc
2603 (interactive
2604 (list nil)))
2605 (cond (interactive
2606 (list (if (or (null (nth 1 interactive))
2607 (stringp (nth 1 interactive)))
2608 (nth 1 interactive)
2609 ;; Interactive spec is a list or a variable
2610 ;; (if it is correct).
2611 (list 'quote (nth 1 interactive))))))))
2612 ;; a non-compiled function (probably trivial)
2613 (list 'quote fun))))))
2614
2615;; Turn a function into an ordinary lambda. Needed for v18 files.
2616(defun byte-compile-byte-code-unmake (function)
2617 (if (consp function)
2618 function;;It already is a lambda.
2619 (setq function (append function nil)) ; turn it into a list
2620 (nconc (list 'lambda (nth 0 function))
2621 (and (nth 4 function) (list (nth 4 function)))
2622 (if (nthcdr 5 function)
2623 (list (cons 'interactive (if (nth 5 function)
2624 (nthcdr 5 function)))))
2625 (list (list 'byte-code
2626 (nth 1 function) (nth 2 function)
2627 (nth 3 function))))))
2628
2629
eadd6444
GM
2630(defun byte-compile-check-lambda-list (list)
2631 "Check lambda-list LIST for errors."
2632 (let (vars)
2633 (while list
2634 (let ((arg (car list)))
ccb3c8de
CW
2635 (when (symbolp arg)
2636 (byte-compile-set-symbol-position arg))
1f006824 2637 (cond ((or (not (symbolp arg))
6c2161c4 2638 (byte-compile-const-symbol-p arg t))
eadd6444
GM
2639 (error "Invalid lambda variable %s" arg))
2640 ((eq arg '&rest)
2641 (unless (cdr list)
2642 (error "&rest without variable name"))
2643 (when (cddr list)
2644 (error "Garbage following &rest VAR in lambda-list")))
2645 ((eq arg '&optional)
2646 (unless (cdr list)
2647 (error "Variable name missing after &optional")))
2648 ((memq arg vars)
e34fd2f2 2649 (byte-compile-warn "repeated variable %s in lambda-list" arg))
1f006824 2650 (t
eadd6444
GM
2651 (push arg vars))))
2652 (setq list (cdr list)))))
2653
2654
1c393159
JB
2655;; Byte-compile a lambda-expression and return a valid function.
2656;; The value is usually a compiled function but may be the original
2657;; lambda-expression.
4ec5239c
LH
2658;; When ADD-LAMBDA is non-nil, the symbol `lambda' is added as head
2659;; of the list FUN and `byte-compile-set-symbol-position' is not called.
2660;; Use this feature to avoid calling `byte-compile-set-symbol-position'
2661;; for symbols generated by the byte compiler itself.
c2768569 2662(defun byte-compile-lambda (bytecomp-fun &optional add-lambda)
4ec5239c 2663 (if add-lambda
c2768569
GM
2664 (setq bytecomp-fun (cons 'lambda bytecomp-fun))
2665 (unless (eq 'lambda (car-safe bytecomp-fun))
2666 (error "Not a lambda list: %S" bytecomp-fun))
4ec5239c 2667 (byte-compile-set-symbol-position 'lambda))
c2768569
GM
2668 (byte-compile-check-lambda-list (nth 1 bytecomp-fun))
2669 (let* ((bytecomp-arglist (nth 1 bytecomp-fun))
1c393159 2670 (byte-compile-bound-variables
cf637a34 2671 (nconc (and (byte-compile-warning-enabled-p 'free-vars)
c2768569
GM
2672 (delq '&rest
2673 (delq '&optional (copy-sequence bytecomp-arglist))))
1c393159 2674 byte-compile-bound-variables))
c2768569
GM
2675 (bytecomp-body (cdr (cdr bytecomp-fun)))
2676 (bytecomp-doc (if (stringp (car bytecomp-body))
2677 (prog1 (car bytecomp-body)
d8f59f56
RS
2678 ;; Discard the doc string
2679 ;; unless it is the last element of the body.
c2768569
GM
2680 (if (cdr bytecomp-body)
2681 (setq bytecomp-body (cdr bytecomp-body))))))
2682 (bytecomp-int (assq 'interactive bytecomp-body)))
6c2161c4 2683 ;; Process the interactive spec.
c2768569 2684 (when bytecomp-int
6c2161c4
SM
2685 (byte-compile-set-symbol-position 'interactive)
2686 ;; Skip (interactive) if it is in front (the most usual location).
c2768569
GM
2687 (if (eq bytecomp-int (car bytecomp-body))
2688 (setq bytecomp-body (cdr bytecomp-body)))
2689 (cond ((consp (cdr bytecomp-int))
2690 (if (cdr (cdr bytecomp-int))
6c2161c4 2691 (byte-compile-warn "malformed interactive spec: %s"
c2768569 2692 (prin1-to-string bytecomp-int)))
6b61353c
KH
2693 ;; If the interactive spec is a call to `list', don't
2694 ;; compile it, because `call-interactively' looks at the
2695 ;; args of `list'. Actually, compile it to get warnings,
2696 ;; but don't use the result.
c2768569 2697 (let ((form (nth 1 bytecomp-int)))
6c2161c4
SM
2698 (while (memq (car-safe form) '(let let* progn save-excursion))
2699 (while (consp (cdr form))
2700 (setq form (cdr form)))
2701 (setq form (car form)))
6b61353c 2702 (if (eq (car-safe form) 'list)
c2768569
GM
2703 (byte-compile-top-level (nth 1 bytecomp-int))
2704 (setq bytecomp-int (list 'interactive
2705 (byte-compile-top-level
2706 (nth 1 bytecomp-int)))))))
2707 ((cdr bytecomp-int)
6c2161c4 2708 (byte-compile-warn "malformed interactive spec: %s"
c2768569 2709 (prin1-to-string bytecomp-int)))))
6c2161c4 2710 ;; Process the body.
c2768569
GM
2711 (let ((compiled (byte-compile-top-level
2712 (cons 'progn bytecomp-body) nil 'lambda)))
6c2161c4 2713 ;; Build the actual byte-coded function.
e5c89ce9 2714 (if (eq 'byte-code (car-safe compiled))
1c393159 2715 (apply 'make-byte-code
c2768569 2716 (append (list bytecomp-arglist)
1c393159
JB
2717 ;; byte-string, constants-vector, stack depth
2718 (cdr compiled)
2719 ;; optionally, the doc string.
c2768569
GM
2720 (if (or bytecomp-doc bytecomp-int)
2721 (list bytecomp-doc))
1c393159 2722 ;; optionally, the interactive spec.
c2768569
GM
2723 (if bytecomp-int
2724 (list (nth 1 bytecomp-int)))))
1c393159 2725 (setq compiled
c2768569 2726 (nconc (if bytecomp-int (list bytecomp-int))
1c393159
JB
2727 (cond ((eq (car-safe compiled) 'progn) (cdr compiled))
2728 (compiled (list compiled)))))
c2768569
GM
2729 (nconc (list 'lambda bytecomp-arglist)
2730 (if (or bytecomp-doc (stringp (car compiled)))
2731 (cons bytecomp-doc (cond (compiled)
2732 (bytecomp-body (list nil))))
1c393159
JB
2733 compiled))))))
2734
2735(defun byte-compile-constants-vector ()
2736 ;; Builds the constants-vector from the current variables and constants.
2737 ;; This modifies the constants from (const . nil) to (const . offset).
2738 ;; To keep the byte-codes to look up the vector as short as possible:
2739 ;; First 6 elements are vars, as there are one-byte varref codes for those.
2740 ;; Next up to byte-constant-limit are constants, still with one-byte codes.
2741 ;; Next variables again, to get 2-byte codes for variable lookup.
2742 ;; The rest of the constants and variables need 3-byte byte-codes.
2743 (let* ((i -1)
2744 (rest (nreverse byte-compile-variables)) ; nreverse because the first
2745 (other (nreverse byte-compile-constants)) ; vars often are used most.
2746 ret tmp
2747 (limits '(5 ; Use the 1-byte varref codes,
2748 63 ; 1-constlim ; 1-byte byte-constant codes,
2749 255 ; 2-byte varref codes,
2750 65535)) ; 3-byte codes for the rest.
2751 limit)
2752 (while (or rest other)
2753 (setq limit (car limits))
2754 (while (and rest (not (eq i limit)))
2755 (if (setq tmp (assq (car (car rest)) ret))
2756 (setcdr (car rest) (cdr tmp))
2757 (setcdr (car rest) (setq i (1+ i)))
2758 (setq ret (cons (car rest) ret)))
2759 (setq rest (cdr rest)))
2760 (setq limits (cdr limits)
2761 rest (prog1 other
2762 (setq other rest))))
2763 (apply 'vector (nreverse (mapcar 'car ret)))))
2764
2765;; Given an expression FORM, compile it and return an equivalent byte-code
2766;; expression (a call to the function byte-code).
2767(defun byte-compile-top-level (form &optional for-effect output-type)
2768 ;; OUTPUT-TYPE advises about how form is expected to be used:
2769 ;; 'eval or nil -> a single form,
2770 ;; 'progn or t -> a list of forms,
2771 ;; 'lambda -> body of a lambda,
2772 ;; 'file -> used at file-level.
285cdf4e
RS
2773 (let ((byte-compile-constants nil)
2774 (byte-compile-variables nil)
2775 (byte-compile-tag-number 0)
2776 (byte-compile-depth 0)
2777 (byte-compile-maxdepth 0)
2778 (byte-compile-output nil))
d9e42bcf
RS
2779 (if (memq byte-optimize '(t source))
2780 (setq form (byte-optimize-form form for-effect)))
2781 (while (and (eq (car-safe form) 'progn) (null (cdr (cdr form))))
2782 (setq form (nth 1 form)))
2783 (if (and (eq 'byte-code (car-safe form))
2784 (not (memq byte-optimize '(t byte)))
2785 (stringp (nth 1 form)) (vectorp (nth 2 form))
2786 (natnump (nth 3 form)))
2787 form
2788 (byte-compile-form form for-effect)
285cdf4e 2789 (byte-compile-out-toplevel for-effect output-type))))
1c393159
JB
2790
2791(defun byte-compile-out-toplevel (&optional for-effect output-type)
2792 (if for-effect
2793 ;; The stack is empty. Push a value to be returned from (byte-code ..).
2794 (if (eq (car (car byte-compile-output)) 'byte-discard)
2795 (setq byte-compile-output (cdr byte-compile-output))
2796 (byte-compile-push-constant
2797 ;; Push any constant - preferably one which already is used, and
2798 ;; a number or symbol - ie not some big sequence. The return value
2799 ;; isn't returned, but it would be a shame if some textually large
2800 ;; constant was not optimized away because we chose to return it.
2801 (and (not (assq nil byte-compile-constants)) ; Nil is often there.
2802 (let ((tmp (reverse byte-compile-constants)))
ba76e7fa
SM
2803 (while (and tmp (not (or (symbolp (caar tmp))
2804 (numberp (caar tmp)))))
1c393159 2805 (setq tmp (cdr tmp)))
ba76e7fa 2806 (caar tmp))))))
1c393159
JB
2807 (byte-compile-out 'byte-return 0)
2808 (setq byte-compile-output (nreverse byte-compile-output))
2809 (if (memq byte-optimize '(t byte))
2810 (setq byte-compile-output
2811 (byte-optimize-lapcode byte-compile-output for-effect)))
1f006824 2812
1c393159
JB
2813 ;; Decompile trivial functions:
2814 ;; only constants and variables, or a single funcall except in lambdas.
2815 ;; Except for Lisp_Compiled objects, forms like (foo "hi")
2816 ;; are still quicker than (byte-code "..." [foo "hi"] 2).
2817 ;; Note that even (quote foo) must be parsed just as any subr by the
2818 ;; interpreter, so quote should be compiled into byte-code in some contexts.
2819 ;; What to leave uncompiled:
69dc83fd
KH
2820 ;; lambda -> never. we used to leave it uncompiled if the body was
2821 ;; a single atom, but that causes confusion if the docstring
2822 ;; uses the (file . pos) syntax. Besides, now that we have
2823 ;; the Lisp_Compiled type, the compiled form is faster.
1c393159
JB
2824 ;; eval -> atom, quote or (function atom atom atom)
2825 ;; progn -> as <<same-as-eval>> or (progn <<same-as-eval>> atom)
2826 ;; file -> as progn, but takes both quotes and atoms, and longer forms.
2827 (let (rest
2828 (maycall (not (eq output-type 'lambda))) ; t if we may make a funcall.
2829 tmp body)
2830 (cond
2831 ;; #### This should be split out into byte-compile-nontrivial-function-p.
69dc83fd
KH
2832 ((or (eq output-type 'lambda)
2833 (nthcdr (if (eq output-type 'file) 50 8) byte-compile-output)
1c393159
JB
2834 (assq 'TAG byte-compile-output) ; Not necessary, but speeds up a bit.
2835 (not (setq tmp (assq 'byte-return byte-compile-output)))
2836 (progn
2837 (setq rest (nreverse
2838 (cdr (memq tmp (reverse byte-compile-output)))))
2839 (while (cond
2840 ((memq (car (car rest)) '(byte-varref byte-constant))
2841 (setq tmp (car (cdr (car rest))))
469414a0
RS
2842 (if (if (eq (car (car rest)) 'byte-constant)
2843 (or (consp tmp)
2844 (and (symbolp tmp)
1639b803 2845 (not (byte-compile-const-symbol-p tmp)))))
469414a0
RS
2846 (if maycall
2847 (setq body (cons (list 'quote tmp) body)))
2848 (setq body (cons tmp body))))
1c393159
JB
2849 ((and maycall
2850 ;; Allow a funcall if at most one atom follows it.
2851 (null (nthcdr 3 rest))
2852 (setq tmp (get (car (car rest)) 'byte-opcode-invert))
2853 (or (null (cdr rest))
2854 (and (memq output-type '(file progn t))
2855 (cdr (cdr rest))
2856 (eq (car (nth 1 rest)) 'byte-discard)
2857 (progn (setq rest (cdr rest)) t))))
2858 (setq maycall nil) ; Only allow one real function call.
2859 (setq body (nreverse body))
2860 (setq body (list
2861 (if (and (eq tmp 'funcall)
2862 (eq (car-safe (car body)) 'quote))
2863 (cons (nth 1 (car body)) (cdr body))
2864 (cons tmp body))))
2865 (or (eq output-type 'file)
2866 (not (delq nil (mapcar 'consp (cdr (car body))))))))
2867 (setq rest (cdr rest)))
69dc83fd 2868 rest))
1c393159
JB
2869 (let ((byte-compile-vector (byte-compile-constants-vector)))
2870 (list 'byte-code (byte-compile-lapcode byte-compile-output)
2871 byte-compile-vector byte-compile-maxdepth)))
2872 ;; it's a trivial function
2873 ((cdr body) (cons 'progn (nreverse body)))
2874 ((car body)))))
2875
c2768569
GM
2876;; Given BYTECOMP-BODY, compile it and return a new body.
2877(defun byte-compile-top-level-body (bytecomp-body &optional for-effect)
2878 (setq bytecomp-body
2879 (byte-compile-top-level (cons 'progn bytecomp-body) for-effect t))
2880 (cond ((eq (car-safe bytecomp-body) 'progn)
2881 (cdr bytecomp-body))
2882 (bytecomp-body
2883 (list bytecomp-body))))
d97362d7
GM
2884
2885(put 'declare-function 'byte-hunk-handler 'byte-compile-declare-function)
2886(defun byte-compile-declare-function (form)
2887 (push (cons (nth 1 form)
7628b337
GM
2888 (if (and (> (length form) 3)
2889 (listp (nth 3 form)))
2890 (list 'declared (nth 3 form))
2891 t)) ; arglist not specified
d97362d7 2892 byte-compile-function-environment)
a342aca4
GM
2893 ;; We are stating that it _will_ be defined at runtime.
2894 (setq byte-compile-noruntime-functions
2895 (delq (nth 1 form) byte-compile-noruntime-functions))
d97362d7
GM
2896 nil)
2897
1c393159 2898\f
c5091f25 2899;; This is the recursive entry point for compiling each subform of an
1c393159
JB
2900;; expression.
2901;; If for-effect is non-nil, byte-compile-form will output a byte-discard
2902;; before terminating (ie no value will be left on the stack).
2903;; A byte-compile handler may, when for-effect is non-nil, choose output code
2904;; which does not leave a value on the stack, and then set for-effect to nil
2905;; (to prevent byte-compile-form from outputting the byte-discard).
2906;; If a handler wants to call another handler, it should do so via
2907;; byte-compile-form, or take extreme care to handle for-effect correctly.
2908;; (Use byte-compile-form-do-effect to reset the for-effect flag too.)
2909;;
2910(defun byte-compile-form (form &optional for-effect)
2911 (setq form (macroexpand form byte-compile-macro-environment))
2912 (cond ((not (consp form))
1639b803 2913 (cond ((or (not (symbolp form)) (byte-compile-const-symbol-p form))
0b46acbf
RS
2914 (when (symbolp form)
2915 (byte-compile-set-symbol-position form))
1c393159
JB
2916 (byte-compile-constant form))
2917 ((and for-effect byte-compile-delete-errors)
0b46acbf
RS
2918 (when (symbolp form)
2919 (byte-compile-set-symbol-position form))
1c393159
JB
2920 (setq for-effect nil))
2921 (t (byte-compile-variable-ref 'byte-varref form))))
2922 ((symbolp (car form))
c2768569
GM
2923 (let* ((bytecomp-fn (car form))
2924 (bytecomp-handler (get bytecomp-fn 'byte-compile)))
2925 (when (byte-compile-const-symbol-p bytecomp-fn)
2926 (byte-compile-warn "`%s' called as a function" bytecomp-fn))
cf637a34 2927 (and (byte-compile-warning-enabled-p 'interactive-only)
c2768569 2928 (memq bytecomp-fn byte-compile-interactive-only-functions)
086af77c 2929 (byte-compile-warn "`%s' used from Lisp code\n\
c2768569 2930That command is designed for interactive use only" bytecomp-fn))
88d5190c
GM
2931 (when (byte-compile-warning-enabled-p 'callargs)
2932 (if (memq bytecomp-fn
2933 '(custom-declare-group custom-declare-variable
2934 custom-declare-face))
2935 (byte-compile-nogroup-warn form))
2936 (byte-compile-callargs-warn form))
c2768569 2937 (if (and bytecomp-handler
67438f77
SM
2938 ;; Make sure that function exists. This is important
2939 ;; for CL compiler macros since the symbol may be
2940 ;; `cl-byte-compile-compiler-macro' but if CL isn't
2941 ;; loaded, this function doesn't exist.
c2768569
GM
2942 (or (not (memq bytecomp-handler
2943 '(cl-byte-compile-compiler-macro)))
e5c89ce9 2944 (functionp bytecomp-handler)))
c2768569 2945 (funcall bytecomp-handler form)
4795d1c7 2946 (byte-compile-normal-call form))
cf637a34 2947 (if (byte-compile-warning-enabled-p 'cl-functions)
4795d1c7 2948 (byte-compile-cl-warn form))))
ed015bdd 2949 ((and (or (byte-code-function-p (car form))
1c393159
JB
2950 (eq (car-safe (car form)) 'lambda))
2951 ;; if the form comes out the same way it went in, that's
2952 ;; because it was malformed, and we couldn't unfold it.
2953 (not (eq form (setq form (byte-compile-unfold-lambda form)))))
2954 (byte-compile-form form for-effect)
2955 (setq for-effect nil))
2956 ((byte-compile-normal-call form)))
2957 (if for-effect
2958 (byte-compile-discard)))
2959
2960(defun byte-compile-normal-call (form)
2961 (if byte-compile-generate-call-tree
2962 (byte-compile-annotate-call-tree form))
86da2828 2963 (when (and for-effect (eq (car form) 'mapcar)
cf637a34 2964 (byte-compile-warning-enabled-p 'mapcar))
89c91fdb
GM
2965 (byte-compile-set-symbol-position 'mapcar)
2966 (byte-compile-warn
2967 "`mapcar' called for effect; use `mapc' or `dolist' instead"))
1c393159 2968 (byte-compile-push-constant (car form))
ed62683d 2969 (mapc 'byte-compile-form (cdr form)) ; wasteful, but faster.
1c393159
JB
2970 (byte-compile-out 'byte-call (length (cdr form))))
2971
c2768569
GM
2972(defun byte-compile-variable-ref (base-op bytecomp-var)
2973 (when (symbolp bytecomp-var)
2974 (byte-compile-set-symbol-position bytecomp-var))
2975 (if (or (not (symbolp bytecomp-var))
2976 (byte-compile-const-symbol-p bytecomp-var
2977 (not (eq base-op 'byte-varref))))
416d3588
GM
2978 (if (byte-compile-warning-enabled-p 'constants)
2979 (byte-compile-warn
2980 (cond ((eq base-op 'byte-varbind) "attempt to let-bind %s `%s'")
2981 ((eq base-op 'byte-varset) "variable assignment to %s `%s'")
2982 (t "variable reference to %s `%s'"))
2983 (if (symbolp bytecomp-var) "constant" "nonvariable")
2984 (prin1-to-string bytecomp-var)))
c2768569 2985 (and (get bytecomp-var 'byte-obsolete-variable)
8480fc7c 2986 (not (memq bytecomp-var byte-compile-not-obsolete-vars))
c2768569 2987 (byte-compile-warn-obsolete bytecomp-var))
2aea6521
GM
2988 (if (eq base-op 'byte-varbind)
2989 (push bytecomp-var byte-compile-bound-variables)
2990 (or (not (byte-compile-warning-enabled-p 'free-vars))
2991 (boundp bytecomp-var)
2992 (memq bytecomp-var byte-compile-bound-variables)
2993 (if (eq base-op 'byte-varset)
2994 (or (memq bytecomp-var byte-compile-free-assignments)
2995 (progn
2996 (byte-compile-warn "assignment to free variable `%s'"
2997 bytecomp-var)
2998 (push bytecomp-var byte-compile-free-assignments)))
2999 (or (memq bytecomp-var byte-compile-free-references)
3000 (progn
3001 (byte-compile-warn "reference to free variable `%s'"
3002 bytecomp-var)
3003 (push bytecomp-var byte-compile-free-references)))))))
c2768569 3004 (let ((tmp (assq bytecomp-var byte-compile-variables)))
6c2161c4 3005 (unless tmp
c2768569 3006 (setq tmp (list bytecomp-var))
6c2161c4 3007 (push tmp byte-compile-variables))
1c393159
JB
3008 (byte-compile-out base-op tmp)))
3009
3010(defmacro byte-compile-get-constant (const)
1639b803 3011 `(or (if (stringp ,const)
7fb4fa10
RS
3012 ;; In a string constant, treat properties as significant.
3013 (let (result)
3014 (dolist (elt byte-compile-constants)
3015 (if (equal-including-properties (car elt) ,const)
3016 (setq result elt)))
3017 result)
1639b803
DL
3018 (assq ,const byte-compile-constants))
3019 (car (setq byte-compile-constants
3020 (cons (list ,const) byte-compile-constants)))))
1c393159
JB
3021
3022;; Use this when the value of a form is a constant. This obeys for-effect.
3023(defun byte-compile-constant (const)
3024 (if for-effect
3025 (setq for-effect nil)
ccb3c8de
CW
3026 (when (symbolp const)
3027 (byte-compile-set-symbol-position const))
1c393159
JB
3028 (byte-compile-out 'byte-constant (byte-compile-get-constant const))))
3029
3030;; Use this for a constant that is not the value of its containing form.
3031;; This ignores for-effect.
3032(defun byte-compile-push-constant (const)
3033 (let ((for-effect nil))
3034 (inline (byte-compile-constant const))))
3035
3036\f
3037;; Compile those primitive ordinary functions
3038;; which have special byte codes just for speed.
3039
3040(defmacro byte-defop-compiler (function &optional compile-handler)
9d28c33e
SM
3041 "Add a compiler-form for FUNCTION.
3042If function is a symbol, then the variable \"byte-SYMBOL\" must name
3043the opcode to be used. If function is a list, the first element
3044is the function and the second element is the bytecode-symbol.
3045The second element may be nil, meaning there is no opcode.
3046COMPILE-HANDLER is the function to use to compile this byte-op, or
3047may be the abbreviations 0, 1, 2, 3, 0-1, or 1-2.
3048If it is nil, then the handler is \"byte-compile-SYMBOL.\""
1c393159
JB
3049 (let (opcode)
3050 (if (symbolp function)
3051 (setq opcode (intern (concat "byte-" (symbol-name function))))
3052 (setq opcode (car (cdr function))
3053 function (car function)))
3054 (let ((fnform
3055 (list 'put (list 'quote function) ''byte-compile
3056 (list 'quote
3057 (or (cdr (assq compile-handler
3058 '((0 . byte-compile-no-args)
3059 (1 . byte-compile-one-arg)
3060 (2 . byte-compile-two-args)
3061 (3 . byte-compile-three-args)
3062 (0-1 . byte-compile-zero-or-one-arg)
3063 (1-2 . byte-compile-one-or-two-args)
3064 (2-3 . byte-compile-two-or-three-args)
3065 )))
3066 compile-handler
3067 (intern (concat "byte-compile-"
3068 (symbol-name function))))))))
3069 (if opcode
3070 (list 'progn fnform
3071 (list 'put (list 'quote function)
3072 ''byte-opcode (list 'quote opcode))
3073 (list 'put (list 'quote opcode)
3074 ''byte-opcode-invert (list 'quote function)))
3075 fnform))))
3076
3077(defmacro byte-defop-compiler19 (function &optional compile-handler)
e5c89ce9
GM
3078 ;; Just like byte-defop-compiler, but used to define an opcode to only
3079 ;; be used when byte-compile-compatibility was false.
3080 (list 'progn
3081 (list 'put
3082 (list 'quote
3083 (or (car (cdr-safe function))
3084 (intern (concat "byte-"
3085 (symbol-name (or (car-safe function)
3086 function))))))
3087 ''emacs19-opcode t)
3088 (list 'byte-defop-compiler function compile-handler)))
1c393159
JB
3089
3090(defmacro byte-defop-compiler-1 (function &optional compile-handler)
3091 (list 'byte-defop-compiler (list function nil) compile-handler))
3092
3093\f
3094(put 'byte-call 'byte-opcode-invert 'funcall)
3095(put 'byte-list1 'byte-opcode-invert 'list)
3096(put 'byte-list2 'byte-opcode-invert 'list)
3097(put 'byte-list3 'byte-opcode-invert 'list)
3098(put 'byte-list4 'byte-opcode-invert 'list)
3099(put 'byte-listN 'byte-opcode-invert 'list)
3100(put 'byte-concat2 'byte-opcode-invert 'concat)
3101(put 'byte-concat3 'byte-opcode-invert 'concat)
3102(put 'byte-concat4 'byte-opcode-invert 'concat)
3103(put 'byte-concatN 'byte-opcode-invert 'concat)
3104(put 'byte-insertN 'byte-opcode-invert 'insert)
3105
1c393159
JB
3106(byte-defop-compiler point 0)
3107;;(byte-defop-compiler mark 0) ;; obsolete
3108(byte-defop-compiler point-max 0)
3109(byte-defop-compiler point-min 0)
3110(byte-defop-compiler following-char 0)
3111(byte-defop-compiler preceding-char 0)
3112(byte-defop-compiler current-column 0)
3113(byte-defop-compiler eolp 0)
3114(byte-defop-compiler eobp 0)
3115(byte-defop-compiler bolp 0)
3116(byte-defop-compiler bobp 0)
3117(byte-defop-compiler current-buffer 0)
3118;;(byte-defop-compiler read-char 0) ;; obsolete
3119(byte-defop-compiler interactive-p 0)
3120(byte-defop-compiler19 widen 0)
3121(byte-defop-compiler19 end-of-line 0-1)
3122(byte-defop-compiler19 forward-char 0-1)
3123(byte-defop-compiler19 forward-line 0-1)
3124(byte-defop-compiler symbolp 1)
3125(byte-defop-compiler consp 1)
3126(byte-defop-compiler stringp 1)
3127(byte-defop-compiler listp 1)
3128(byte-defop-compiler not 1)
3129(byte-defop-compiler (null byte-not) 1)
3130(byte-defop-compiler car 1)
3131(byte-defop-compiler cdr 1)
3132(byte-defop-compiler length 1)
3133(byte-defop-compiler symbol-value 1)
3134(byte-defop-compiler symbol-function 1)
3135(byte-defop-compiler (1+ byte-add1) 1)
3136(byte-defop-compiler (1- byte-sub1) 1)
3137(byte-defop-compiler goto-char 1)
b8ae93ad 3138(byte-defop-compiler char-after 0-1)
1c393159
JB
3139(byte-defop-compiler set-buffer 1)
3140;;(byte-defop-compiler set-mark 1) ;; obsolete
78642e03 3141(byte-defop-compiler19 forward-word 0-1)
1c393159
JB
3142(byte-defop-compiler19 char-syntax 1)
3143(byte-defop-compiler19 nreverse 1)
3144(byte-defop-compiler19 car-safe 1)
3145(byte-defop-compiler19 cdr-safe 1)
3146(byte-defop-compiler19 numberp 1)
3147(byte-defop-compiler19 integerp 1)
3148(byte-defop-compiler19 skip-chars-forward 1-2)
3149(byte-defop-compiler19 skip-chars-backward 1-2)
1c393159
JB
3150(byte-defop-compiler eq 2)
3151(byte-defop-compiler memq 2)
3152(byte-defop-compiler cons 2)
3153(byte-defop-compiler aref 2)
3154(byte-defop-compiler set 2)
3155(byte-defop-compiler (= byte-eqlsign) 2)
3156(byte-defop-compiler (< byte-lss) 2)
3157(byte-defop-compiler (> byte-gtr) 2)
3158(byte-defop-compiler (<= byte-leq) 2)
3159(byte-defop-compiler (>= byte-geq) 2)
3160(byte-defop-compiler get 2)
3161(byte-defop-compiler nth 2)
3162(byte-defop-compiler substring 2-3)
9e2b097b 3163(byte-defop-compiler19 (move-marker byte-set-marker) 2-3)
1c393159
JB
3164(byte-defop-compiler19 set-marker 2-3)
3165(byte-defop-compiler19 match-beginning 1)
3166(byte-defop-compiler19 match-end 1)
3167(byte-defop-compiler19 upcase 1)
3168(byte-defop-compiler19 downcase 1)
3169(byte-defop-compiler19 string= 2)
3170(byte-defop-compiler19 string< 2)
9e2b097b
JB
3171(byte-defop-compiler19 (string-equal byte-string=) 2)
3172(byte-defop-compiler19 (string-lessp byte-string<) 2)
1c393159
JB
3173(byte-defop-compiler19 equal 2)
3174(byte-defop-compiler19 nthcdr 2)
3175(byte-defop-compiler19 elt 2)
3176(byte-defop-compiler19 member 2)
3177(byte-defop-compiler19 assq 2)
9e2b097b
JB
3178(byte-defop-compiler19 (rplaca byte-setcar) 2)
3179(byte-defop-compiler19 (rplacd byte-setcdr) 2)
1c393159
JB
3180(byte-defop-compiler19 setcar 2)
3181(byte-defop-compiler19 setcdr 2)
3182(byte-defop-compiler19 buffer-substring 2)
3183(byte-defop-compiler19 delete-region 2)
3184(byte-defop-compiler19 narrow-to-region 2)
1c393159
JB
3185(byte-defop-compiler19 (% byte-rem) 2)
3186(byte-defop-compiler aset 3)
3187
3188(byte-defop-compiler max byte-compile-associative)
3189(byte-defop-compiler min byte-compile-associative)
3190(byte-defop-compiler (+ byte-plus) byte-compile-associative)
3191(byte-defop-compiler19 (* byte-mult) byte-compile-associative)
3192
3193;;####(byte-defop-compiler19 move-to-column 1)
3194(byte-defop-compiler-1 interactive byte-compile-noop)
3195
3196\f
3197(defun byte-compile-subr-wrong-args (form n)
ccb3c8de 3198 (byte-compile-set-symbol-position (car form))
1d5c17c0 3199 (byte-compile-warn "`%s' called with %d arg%s, but requires %s"
1c393159
JB
3200 (car form) (length (cdr form))
3201 (if (= 1 (length (cdr form))) "" "s") n)
3202 ;; get run-time wrong-number-of-args error.
3203 (byte-compile-normal-call form))
3204
3205(defun byte-compile-no-args (form)
3206 (if (not (= (length form) 1))
3207 (byte-compile-subr-wrong-args form "none")
3208 (byte-compile-out (get (car form) 'byte-opcode) 0)))
3209
3210(defun byte-compile-one-arg (form)
3211 (if (not (= (length form) 2))
3212 (byte-compile-subr-wrong-args form 1)
3213 (byte-compile-form (car (cdr form))) ;; Push the argument
3214 (byte-compile-out (get (car form) 'byte-opcode) 0)))
3215
3216(defun byte-compile-two-args (form)
3217 (if (not (= (length form) 3))
3218 (byte-compile-subr-wrong-args form 2)
3219 (byte-compile-form (car (cdr form))) ;; Push the arguments
3220 (byte-compile-form (nth 2 form))
3221 (byte-compile-out (get (car form) 'byte-opcode) 0)))
3222
3223(defun byte-compile-three-args (form)
3224 (if (not (= (length form) 4))
3225 (byte-compile-subr-wrong-args form 3)
3226 (byte-compile-form (car (cdr form))) ;; Push the arguments
3227 (byte-compile-form (nth 2 form))
3228 (byte-compile-form (nth 3 form))
3229 (byte-compile-out (get (car form) 'byte-opcode) 0)))
3230
3231(defun byte-compile-zero-or-one-arg (form)
3232 (let ((len (length form)))
3233 (cond ((= len 1) (byte-compile-one-arg (append form '(nil))))
3234 ((= len 2) (byte-compile-one-arg form))
3235 (t (byte-compile-subr-wrong-args form "0-1")))))
3236
3237(defun byte-compile-one-or-two-args (form)
3238 (let ((len (length form)))
3239 (cond ((= len 2) (byte-compile-two-args (append form '(nil))))
3240 ((= len 3) (byte-compile-two-args form))
3241 (t (byte-compile-subr-wrong-args form "1-2")))))
3242
3243(defun byte-compile-two-or-three-args (form)
3244 (let ((len (length form)))
3245 (cond ((= len 3) (byte-compile-three-args (append form '(nil))))
3246 ((= len 4) (byte-compile-three-args form))
3247 (t (byte-compile-subr-wrong-args form "2-3")))))
3248
3249(defun byte-compile-noop (form)
3250 (byte-compile-constant nil))
3251
3252(defun byte-compile-discard ()
3253 (byte-compile-out 'byte-discard 0))
3254
3255
3256;; Compile a function that accepts one or more args and is right-associative.
c0f43df5
RS
3257;; We do it by left-associativity so that the operations
3258;; are done in the same order as in interpreted code.
10809e0f
RS
3259;; We treat the one-arg case, as in (+ x), like (+ x 0).
3260;; in order to convert markers to numbers, and trigger expected errors.
1c393159
JB
3261(defun byte-compile-associative (form)
3262 (if (cdr form)
c0f43df5 3263 (let ((opcode (get (car form) 'byte-opcode))
24ae8da4
CY
3264 args)
3265 (if (and (< 3 (length form))
3266 (memq opcode (list (get '+ 'byte-opcode)
3267 (get '* 'byte-opcode))))
3268 ;; Don't use binary operations for > 2 operands, as that
3269 ;; may cause overflow/truncation in float operations.
3270 (byte-compile-normal-call form)
3271 (setq args (copy-sequence (cdr form)))
3272 (byte-compile-form (car args))
3273 (setq args (cdr args))
3274 (or args (setq args '(0)
3275 opcode (get '+ 'byte-opcode)))
3276 (dolist (arg args)
3277 (byte-compile-form arg)
3278 (byte-compile-out opcode 0))))
1c393159
JB
3279 (byte-compile-constant (eval form))))
3280
3281\f
3282;; more complicated compiler macros
3283
ec448ae2 3284(byte-defop-compiler char-before)
a746fb65
GM
3285(byte-defop-compiler backward-char)
3286(byte-defop-compiler backward-word)
1c393159
JB
3287(byte-defop-compiler list)
3288(byte-defop-compiler concat)
3289(byte-defop-compiler fset)
3290(byte-defop-compiler (indent-to-column byte-indent-to) byte-compile-indent-to)
3291(byte-defop-compiler indent-to)
3292(byte-defop-compiler insert)
3293(byte-defop-compiler-1 function byte-compile-function-form)
3294(byte-defop-compiler-1 - byte-compile-minus)
3295(byte-defop-compiler19 (/ byte-quo) byte-compile-quo)
3296(byte-defop-compiler19 nconc)
1c393159 3297
ec448ae2
GM
3298(defun byte-compile-char-before (form)
3299 (cond ((= 2 (length form))
a746fb65
GM
3300 (byte-compile-form (list 'char-after (if (numberp (nth 1 form))
3301 (1- (nth 1 form))
3302 `(1- ,(nth 1 form))))))
3303 ((= 1 (length form))
3304 (byte-compile-form '(char-after (1- (point)))))
3305 (t (byte-compile-subr-wrong-args form "0-1"))))
3306
3307;; backward-... ==> forward-... with negated argument.
3308(defun byte-compile-backward-char (form)
3309 (cond ((= 2 (length form))
3310 (byte-compile-form (list 'forward-char (if (numberp (nth 1 form))
3311 (- (nth 1 form))
3312 `(- ,(nth 1 form))))))
3313 ((= 1 (length form))
3314 (byte-compile-form '(forward-char -1)))
3315 (t (byte-compile-subr-wrong-args form "0-1"))))
3316
3317(defun byte-compile-backward-word (form)
3318 (cond ((= 2 (length form))
3319 (byte-compile-form (list 'forward-word (if (numberp (nth 1 form))
3320 (- (nth 1 form))
3321 `(- ,(nth 1 form))))))
3322 ((= 1 (length form))
3323 (byte-compile-form '(forward-word -1)))
3324 (t (byte-compile-subr-wrong-args form "0-1"))))
ec448ae2 3325
1c393159
JB
3326(defun byte-compile-list (form)
3327 (let ((count (length (cdr form))))
3328 (cond ((= count 0)
3329 (byte-compile-constant nil))
3330 ((< count 5)
ed62683d 3331 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3332 (byte-compile-out
3333 (aref [byte-list1 byte-list2 byte-list3 byte-list4] (1- count)) 0))
e5c89ce9 3334 ((< count 256)
ed62683d 3335 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3336 (byte-compile-out 'byte-listN count))
3337 (t (byte-compile-normal-call form)))))
3338
3339(defun byte-compile-concat (form)
3340 (let ((count (length (cdr form))))
3341 (cond ((and (< 1 count) (< count 5))
ed62683d 3342 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3343 (byte-compile-out
3344 (aref [byte-concat2 byte-concat3 byte-concat4] (- count 2))
3345 0))
3346 ;; Concat of one arg is not a no-op if arg is not a string.
3347 ((= count 0)
3348 (byte-compile-form ""))
e5c89ce9 3349 ((< count 256)
ed62683d 3350 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3351 (byte-compile-out 'byte-concatN count))
3352 ((byte-compile-normal-call form)))))
3353
3354(defun byte-compile-minus (form)
24ae8da4
CY
3355 (let ((len (length form)))
3356 (cond
3357 ((= 1 len) (byte-compile-constant 0))
3358 ((= 2 len)
3359 (byte-compile-form (cadr form))
3360 (byte-compile-out 'byte-negate 0))
2b9c3b12 3361 ((= 3 len)
24ae8da4
CY
3362 (byte-compile-form (nth 1 form))
3363 (byte-compile-form (nth 2 form))
3364 (byte-compile-out 'byte-diff 0))
3365 ;; Don't use binary operations for > 2 operands, as that may
3366 ;; cause overflow/truncation in float operations.
3367 (t (byte-compile-normal-call form)))))
1c393159
JB
3368
3369(defun byte-compile-quo (form)
3370 (let ((len (length form)))
3371 (cond ((<= len 2)
3372 (byte-compile-subr-wrong-args form "2 or more"))
24ae8da4
CY
3373 ((= len 3)
3374 (byte-compile-two-args form))
1c393159 3375 (t
24ae8da4
CY
3376 ;; Don't use binary operations for > 2 operands, as that
3377 ;; may cause overflow/truncation in float operations.
3378 (byte-compile-normal-call form)))))
1c393159
JB
3379
3380(defun byte-compile-nconc (form)
3381 (let ((len (length form)))
3382 (cond ((= len 1)
3383 (byte-compile-constant nil))
3384 ((= len 2)
3385 ;; nconc of one arg is a noop, even if that arg isn't a list.
3386 (byte-compile-form (nth 1 form)))
3387 (t
3388 (byte-compile-form (car (setq form (cdr form))))
3389 (while (setq form (cdr form))
3390 (byte-compile-form (car form))
3391 (byte-compile-out 'byte-nconc 0))))))
3392
3393(defun byte-compile-fset (form)
3394 ;; warn about forms like (fset 'foo '(lambda () ...))
3395 ;; (where the lambda expression is non-trivial...)
3396 (let ((fn (nth 2 form))
3397 body)
3398 (if (and (eq (car-safe fn) 'quote)
3399 (eq (car-safe (setq fn (nth 1 fn))) 'lambda))
3400 (progn
3401 (setq body (cdr (cdr fn)))
3402 (if (stringp (car body)) (setq body (cdr body)))
3403 (if (eq 'interactive (car-safe (car body))) (setq body (cdr body)))
3404 (if (and (consp (car body))
3405 (not (eq 'byte-code (car (car body)))))
3406 (byte-compile-warn
1d5c17c0 3407 "A quoted lambda form is the second argument of `fset'. This is probably
1c393159
JB
3408 not what you want, as that lambda cannot be compiled. Consider using
3409 the syntax (function (lambda (...) ...)) instead.")))))
3410 (byte-compile-two-args form))
3411
3412(defun byte-compile-funarg (form)
3413 ;; (mapcar '(lambda (x) ..) ..) ==> (mapcar (function (lambda (x) ..)) ..)
eb8c3be9 3414 ;; for cases where it's guaranteed that first arg will be used as a lambda.
1c393159
JB
3415 (byte-compile-normal-call
3416 (let ((fn (nth 1 form)))
3417 (if (and (eq (car-safe fn) 'quote)
3418 (eq (car-safe (nth 1 fn)) 'lambda))
3419 (cons (car form)
3420 (cons (cons 'function (cdr fn))
3421 (cdr (cdr form))))
3422 form))))
3423
5a6037bb
RS
3424(defun byte-compile-funarg-2 (form)
3425 ;; (sort ... '(lambda (x) ..)) ==> (sort ... (function (lambda (x) ..)))
3426 ;; for cases where it's guaranteed that second arg will be used as a lambda.
3427 (byte-compile-normal-call
3428 (let ((fn (nth 2 form)))
3429 (if (and (eq (car-safe fn) 'quote)
3430 (eq (car-safe (nth 1 fn)) 'lambda))
3431 (cons (car form)
3432 (cons (nth 1 form)
3433 (cons (cons 'function (cdr fn))
3434 (cdr (cdr (cdr form))))))
3435 form))))
3436
1c393159
JB
3437;; (function foo) must compile like 'foo, not like (symbol-function 'foo).
3438;; Otherwise it will be incompatible with the interpreter,
3439;; and (funcall (function foo)) will lose with autoloads.
3440
3441(defun byte-compile-function-form (form)
3442 (byte-compile-constant
3443 (cond ((symbolp (nth 1 form))
3444 (nth 1 form))
1c393159
JB
3445 ((byte-compile-lambda (nth 1 form))))))
3446
3447(defun byte-compile-indent-to (form)
3448 (let ((len (length form)))
3449 (cond ((= len 2)
3450 (byte-compile-form (car (cdr form)))
3451 (byte-compile-out 'byte-indent-to 0))
3452 ((= len 3)
3453 ;; no opcode for 2-arg case.
3454 (byte-compile-normal-call form))
3455 (t
3456 (byte-compile-subr-wrong-args form "1-2")))))
3457
3458(defun byte-compile-insert (form)
3459 (cond ((null (cdr form))
3460 (byte-compile-constant nil))
e5c89ce9 3461 ((<= (length form) 256)
ed62683d 3462 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3463 (if (cdr (cdr form))
3464 (byte-compile-out 'byte-insertN (length (cdr form)))
3465 (byte-compile-out 'byte-insert 0)))
3466 ((memq t (mapcar 'consp (cdr (cdr form))))
3467 (byte-compile-normal-call form))
3468 ;; We can split it; there is no function call after inserting 1st arg.
3469 (t
3470 (while (setq form (cdr form))
3471 (byte-compile-form (car form))
3472 (byte-compile-out 'byte-insert 0)
3473 (if (cdr form)
3474 (byte-compile-discard))))))
3475
1c393159
JB
3476\f
3477(byte-defop-compiler-1 setq)
3478(byte-defop-compiler-1 setq-default)
3479(byte-defop-compiler-1 quote)
3480(byte-defop-compiler-1 quote-form)
3481
3482(defun byte-compile-setq (form)
c2768569
GM
3483 (let ((bytecomp-args (cdr form)))
3484 (if bytecomp-args
3485 (while bytecomp-args
3486 (byte-compile-form (car (cdr bytecomp-args)))
3487 (or for-effect (cdr (cdr bytecomp-args))
1c393159 3488 (byte-compile-out 'byte-dup 0))
c2768569
GM
3489 (byte-compile-variable-ref 'byte-varset (car bytecomp-args))
3490 (setq bytecomp-args (cdr (cdr bytecomp-args))))
1c393159
JB
3491 ;; (setq), with no arguments.
3492 (byte-compile-form nil for-effect))
3493 (setq for-effect nil)))
3494
3495(defun byte-compile-setq-default (form)
c2768569 3496 (let ((bytecomp-args (cdr form))
ca38179a 3497 setters)
c2768569
GM
3498 (while bytecomp-args
3499 (let ((var (car bytecomp-args)))
416d3588
GM
3500 (and (or (not (symbolp var))
3501 (byte-compile-const-symbol-p var t))
3502 (byte-compile-warning-enabled-p 'constants)
3503 (byte-compile-warn
3504 "variable assignment to %s `%s'"
3505 (if (symbolp var) "constant" "nonvariable")
3506 (prin1-to-string var)))
c2768569 3507 (push (list 'set-default (list 'quote var) (car (cdr bytecomp-args)))
d988dbf6 3508 setters))
c2768569 3509 (setq bytecomp-args (cdr (cdr bytecomp-args))))
ca38179a 3510 (byte-compile-form (cons 'progn (nreverse setters)))))
1c393159
JB
3511
3512(defun byte-compile-quote (form)
3513 (byte-compile-constant (car (cdr form))))
3514
3515(defun byte-compile-quote-form (form)
3516 (byte-compile-constant (byte-compile-top-level (nth 1 form))))
3517
3518\f
3519;;; control structures
3520
c2768569
GM
3521(defun byte-compile-body (bytecomp-body &optional for-effect)
3522 (while (cdr bytecomp-body)
3523 (byte-compile-form (car bytecomp-body) t)
3524 (setq bytecomp-body (cdr bytecomp-body)))
3525 (byte-compile-form (car bytecomp-body) for-effect))
1c393159 3526
c2768569
GM
3527(defsubst byte-compile-body-do-effect (bytecomp-body)
3528 (byte-compile-body bytecomp-body for-effect)
1c393159
JB
3529 (setq for-effect nil))
3530
52799cb8 3531(defsubst byte-compile-form-do-effect (form)
1c393159
JB
3532 (byte-compile-form form for-effect)
3533 (setq for-effect nil))
3534
3535(byte-defop-compiler-1 inline byte-compile-progn)
3536(byte-defop-compiler-1 progn)
3537(byte-defop-compiler-1 prog1)
3538(byte-defop-compiler-1 prog2)
3539(byte-defop-compiler-1 if)
3540(byte-defop-compiler-1 cond)
3541(byte-defop-compiler-1 and)
3542(byte-defop-compiler-1 or)
3543(byte-defop-compiler-1 while)
3544(byte-defop-compiler-1 funcall)
3545(byte-defop-compiler-1 apply byte-compile-funarg)
3546(byte-defop-compiler-1 mapcar byte-compile-funarg)
3547(byte-defop-compiler-1 mapatoms byte-compile-funarg)
3548(byte-defop-compiler-1 mapconcat byte-compile-funarg)
28bb2cef 3549(byte-defop-compiler-1 mapc byte-compile-funarg)
def9389a
DL
3550(byte-defop-compiler-1 maphash byte-compile-funarg)
3551(byte-defop-compiler-1 map-char-table byte-compile-funarg)
351697be
DL
3552(byte-defop-compiler-1 map-char-table byte-compile-funarg-2)
3553;; map-charset-chars should be funarg but has optional third arg
5a6037bb 3554(byte-defop-compiler-1 sort byte-compile-funarg-2)
1c393159
JB
3555(byte-defop-compiler-1 let)
3556(byte-defop-compiler-1 let*)
3557
3558(defun byte-compile-progn (form)
3559 (byte-compile-body-do-effect (cdr form)))
3560
3561(defun byte-compile-prog1 (form)
3562 (byte-compile-form-do-effect (car (cdr form)))
3563 (byte-compile-body (cdr (cdr form)) t))
3564
3565(defun byte-compile-prog2 (form)
3566 (byte-compile-form (nth 1 form) t)
3567 (byte-compile-form-do-effect (nth 2 form))
3568 (byte-compile-body (cdr (cdr (cdr form))) t))
3569
3570(defmacro byte-compile-goto-if (cond discard tag)
1639b803
DL
3571 `(byte-compile-goto
3572 (if ,cond
3573 (if ,discard 'byte-goto-if-not-nil 'byte-goto-if-not-nil-else-pop)
3574 (if ,discard 'byte-goto-if-nil 'byte-goto-if-nil-else-pop))
3575 ,tag))
1c393159 3576
70f41945
DN
3577;; Return the list of items in CONDITION-PARAM that match PRED-LIST.
3578;; Only return items that are not in ONLY-IF-NOT-PRESENT.
516b3653
JB
3579(defun byte-compile-find-bound-condition (condition-param
3580 pred-list
70f41945
DN
3581 &optional only-if-not-present)
3582 (let ((result nil)
3583 (nth-one nil)
516b3653 3584 (cond-list
70f41945
DN
3585 (if (memq (car-safe condition-param) pred-list)
3586 ;; The condition appears by itself.
3587 (list condition-param)
3588 ;; If the condition is an `and', look for matches among the
3589 ;; `and' arguments.
3590 (when (eq 'and (car-safe condition-param))
3591 (cdr condition-param)))))
516b3653 3592
70f41945
DN
3593 (dolist (crt cond-list)
3594 (when (and (memq (car-safe crt) pred-list)
3595 (eq 'quote (car-safe (setq nth-one (nth 1 crt))))
3596 ;; Ignore if the symbol is already on the unresolved
3597 ;; list.
3598 (not (assq (nth 1 nth-one) ; the relevant symbol
3599 only-if-not-present)))
3600 (push (nth 1 (nth 1 crt)) result)))
3601 result))
3602
6b61353c
KH
3603(defmacro byte-compile-maybe-guarded (condition &rest body)
3604 "Execute forms in BODY, potentially guarded by CONDITION.
82a726b4 3605CONDITION is a variable whose value is a test in an `if' or `cond'.
d6dc41d5
GM
3606BODY is the code to compile in the first arm of the if or the body of
3607the cond clause. If CONDITION's value is of the form (fboundp 'foo)
ad50a502 3608or (boundp 'foo), the relevant warnings from BODY about foo's
8480fc7c 3609being undefined (or obsolete) will be suppressed.
82a726b4 3610
b2e948ee 3611If CONDITION's value is (not (featurep 'emacs)) or (featurep 'xemacs),
ad50a502 3612that suppresses all warnings during execution of BODY."
6b61353c 3613 (declare (indent 1) (debug t))
516b3653
JB
3614 `(let* ((fbound-list (byte-compile-find-bound-condition
3615 ,condition (list 'fboundp)
70f41945 3616 byte-compile-unresolved-functions))
516b3653 3617 (bound-list (byte-compile-find-bound-condition
70f41945 3618 ,condition (list 'boundp 'default-boundp)))
6b61353c
KH
3619 ;; Maybe add to the bound list.
3620 (byte-compile-bound-variables
70f41945
DN
3621 (if bound-list
3622 (append bound-list byte-compile-bound-variables)
86408b24 3623 byte-compile-bound-variables)))
82a726b4 3624 (unwind-protect
8480fc7c
GM
3625 ;; If things not being bound at all is ok, so must them being obsolete.
3626 ;; Note that we add to the existing lists since Tramp (ab)uses
3627 ;; this feature.
3628 (let ((byte-compile-not-obsolete-vars
3629 (append byte-compile-not-obsolete-vars bound-list))
3630 (byte-compile-not-obsolete-funcs
3631 (append byte-compile-not-obsolete-funcs fbound-list)))
3632 ,@body)
82a726b4 3633 ;; Maybe remove the function symbol from the unresolved list.
70f41945
DN
3634 (dolist (fbound fbound-list)
3635 (when fbound
82a726b4
RS
3636 (setq byte-compile-unresolved-functions
3637 (delq (assq fbound byte-compile-unresolved-functions)
70f41945 3638 byte-compile-unresolved-functions)))))))
6b61353c 3639
1c393159
JB
3640(defun byte-compile-if (form)
3641 (byte-compile-form (car (cdr form)))
b8234c84
DL
3642 ;; Check whether we have `(if (fboundp ...' or `(if (boundp ...'
3643 ;; and avoid warnings about the relevent symbols in the consequent.
6b61353c
KH
3644 (let ((clause (nth 1 form))
3645 (donetag (byte-compile-make-tag)))
b8234c84
DL
3646 (if (null (nthcdr 3 form))
3647 ;; No else-forms
3648 (progn
3649 (byte-compile-goto-if nil for-effect donetag)
6b61353c 3650 (byte-compile-maybe-guarded clause
b8234c84 3651 (byte-compile-form (nth 2 form) for-effect))
b8234c84
DL
3652 (byte-compile-out-tag donetag))
3653 (let ((elsetag (byte-compile-make-tag)))
3654 (byte-compile-goto 'byte-goto-if-nil elsetag)
6b61353c
KH
3655 (byte-compile-maybe-guarded clause
3656 (byte-compile-form (nth 2 form) for-effect))
b8234c84
DL
3657 (byte-compile-goto 'byte-goto donetag)
3658 (byte-compile-out-tag elsetag)
300f994a
RS
3659 (byte-compile-maybe-guarded (list 'not clause)
3660 (byte-compile-body (cdr (cdr (cdr form))) for-effect))
b8234c84 3661 (byte-compile-out-tag donetag))))
1c393159
JB
3662 (setq for-effect nil))
3663
3664(defun byte-compile-cond (clauses)
3665 (let ((donetag (byte-compile-make-tag))
3666 nexttag clause)
3667 (while (setq clauses (cdr clauses))
3668 (setq clause (car clauses))
3669 (cond ((or (eq (car clause) t)
3670 (and (eq (car-safe (car clause)) 'quote)
3671 (car-safe (cdr-safe (car clause)))))
3672 ;; Unconditional clause
3673 (setq clause (cons t clause)
3674 clauses nil))
3675 ((cdr clauses)
3676 (byte-compile-form (car clause))
3677 (if (null (cdr clause))
3678 ;; First clause is a singleton.
3679 (byte-compile-goto-if t for-effect donetag)
82a726b4
RS
3680 (setq nexttag (byte-compile-make-tag))
3681 (byte-compile-goto 'byte-goto-if-nil nexttag)
3682 (byte-compile-maybe-guarded (car clause)
3683 (byte-compile-body (cdr clause) for-effect))
3684 (byte-compile-goto 'byte-goto donetag)
3685 (byte-compile-out-tag nexttag)))))
1c393159 3686 ;; Last clause
6b61353c
KH
3687 (let ((guard (car clause)))
3688 (and (cdr clause) (not (eq guard t))
3689 (progn (byte-compile-form guard)
3690 (byte-compile-goto-if nil for-effect donetag)
3691 (setq clause (cdr clause))))
3692 (byte-compile-maybe-guarded guard
3693 (byte-compile-body-do-effect clause)))
1c393159
JB
3694 (byte-compile-out-tag donetag)))
3695
3696(defun byte-compile-and (form)
3697 (let ((failtag (byte-compile-make-tag))
c2768569
GM
3698 (bytecomp-args (cdr form)))
3699 (if (null bytecomp-args)
1c393159 3700 (byte-compile-form-do-effect t)
c2768569 3701 (byte-compile-and-recursion bytecomp-args failtag))))
8877fa6f 3702
83b0af6e 3703;; Handle compilation of a nontrivial `and' call.
8877fa6f
RS
3704;; We use tail recursion so we can use byte-compile-maybe-guarded.
3705(defun byte-compile-and-recursion (rest failtag)
3706 (if (cdr rest)
3707 (progn
3708 (byte-compile-form (car rest))
1c393159 3709 (byte-compile-goto-if nil for-effect failtag)
8877fa6f
RS
3710 (byte-compile-maybe-guarded (car rest)
3711 (byte-compile-and-recursion (cdr rest) failtag)))
3712 (byte-compile-form-do-effect (car rest))
3713 (byte-compile-out-tag failtag)))
1c393159
JB
3714
3715(defun byte-compile-or (form)
3716 (let ((wintag (byte-compile-make-tag))
c2768569
GM
3717 (bytecomp-args (cdr form)))
3718 (if (null bytecomp-args)
1c393159 3719 (byte-compile-form-do-effect nil)
c2768569 3720 (byte-compile-or-recursion bytecomp-args wintag))))
83b0af6e
RS
3721
3722;; Handle compilation of a nontrivial `or' call.
3723;; We use tail recursion so we can use byte-compile-maybe-guarded.
3724(defun byte-compile-or-recursion (rest wintag)
3725 (if (cdr rest)
3726 (progn
3727 (byte-compile-form (car rest))
1c393159 3728 (byte-compile-goto-if t for-effect wintag)
83b0af6e
RS
3729 (byte-compile-maybe-guarded (list 'not (car rest))
3730 (byte-compile-or-recursion (cdr rest) wintag)))
3731 (byte-compile-form-do-effect (car rest))
3732 (byte-compile-out-tag wintag)))
1c393159
JB
3733
3734(defun byte-compile-while (form)
3735 (let ((endtag (byte-compile-make-tag))
3736 (looptag (byte-compile-make-tag)))
3737 (byte-compile-out-tag looptag)
3738 (byte-compile-form (car (cdr form)))
3739 (byte-compile-goto-if nil for-effect endtag)
3740 (byte-compile-body (cdr (cdr form)) t)
3741 (byte-compile-goto 'byte-goto looptag)
3742 (byte-compile-out-tag endtag)
3743 (setq for-effect nil)))
3744
3745(defun byte-compile-funcall (form)
ed62683d 3746 (mapc 'byte-compile-form (cdr form))
1c393159
JB
3747 (byte-compile-out 'byte-call (length (cdr (cdr form)))))
3748
3749
3750(defun byte-compile-let (form)
3751 ;; First compute the binding values in the old scope.
3752 (let ((varlist (car (cdr form))))
6c2161c4
SM
3753 (dolist (var varlist)
3754 (if (consp var)
3755 (byte-compile-form (car (cdr var)))
3756 (byte-compile-push-constant nil))))
1c393159
JB
3757 (let ((byte-compile-bound-variables byte-compile-bound-variables) ;new scope
3758 (varlist (reverse (car (cdr form)))))
6c2161c4 3759 (dolist (var varlist)
8480fc7c
GM
3760 (byte-compile-variable-ref 'byte-varbind
3761 (if (consp var) (car var) var)))
1c393159
JB
3762 (byte-compile-body-do-effect (cdr (cdr form)))
3763 (byte-compile-out 'byte-unbind (length (car (cdr form))))))
3764
3765(defun byte-compile-let* (form)
3766 (let ((byte-compile-bound-variables byte-compile-bound-variables) ;new scope
3767 (varlist (copy-sequence (car (cdr form)))))
6c2161c4
SM
3768 (dolist (var varlist)
3769 (if (atom var)
1c393159 3770 (byte-compile-push-constant nil)
6c2161c4
SM
3771 (byte-compile-form (car (cdr var)))
3772 (setq var (car var)))
3773 (byte-compile-variable-ref 'byte-varbind var))
1c393159
JB
3774 (byte-compile-body-do-effect (cdr (cdr form)))
3775 (byte-compile-out 'byte-unbind (length (car (cdr form))))))
3776
3777
3778(byte-defop-compiler-1 /= byte-compile-negated)
3779(byte-defop-compiler-1 atom byte-compile-negated)
3780(byte-defop-compiler-1 nlistp byte-compile-negated)
3781
3782(put '/= 'byte-compile-negated-op '=)
3783(put 'atom 'byte-compile-negated-op 'consp)
3784(put 'nlistp 'byte-compile-negated-op 'listp)
3785
3786(defun byte-compile-negated (form)
3787 (byte-compile-form-do-effect (byte-compile-negation-optimizer form)))
3788
3789;; Even when optimization is off, /= is optimized to (not (= ...)).
3790(defun byte-compile-negation-optimizer (form)
3791 ;; an optimizer for forms where <form1> is less efficient than (not <form2>)
ccb3c8de 3792 (byte-compile-set-symbol-position (car form))
1c393159
JB
3793 (list 'not
3794 (cons (or (get (car form) 'byte-compile-negated-op)
3795 (error
52799cb8 3796 "Compiler error: `%s' has no `byte-compile-negated-op' property"
1c393159
JB
3797 (car form)))
3798 (cdr form))))
3799\f
3800;;; other tricky macro-like special-forms
3801
3802(byte-defop-compiler-1 catch)
3803(byte-defop-compiler-1 unwind-protect)
3804(byte-defop-compiler-1 condition-case)
3805(byte-defop-compiler-1 save-excursion)
f3e472b0 3806(byte-defop-compiler-1 save-current-buffer)
1c393159
JB
3807(byte-defop-compiler-1 save-restriction)
3808(byte-defop-compiler-1 save-window-excursion)
3809(byte-defop-compiler-1 with-output-to-temp-buffer)
6e8d0db7 3810(byte-defop-compiler-1 track-mouse)
1c393159
JB
3811
3812(defun byte-compile-catch (form)
3813 (byte-compile-form (car (cdr form)))
3814 (byte-compile-push-constant
3815 (byte-compile-top-level (cons 'progn (cdr (cdr form))) for-effect))
3816 (byte-compile-out 'byte-catch 0))
3817
3818(defun byte-compile-unwind-protect (form)
3819 (byte-compile-push-constant
3820 (byte-compile-top-level-body (cdr (cdr form)) t))
3821 (byte-compile-out 'byte-unwind-protect 0)
3822 (byte-compile-form-do-effect (car (cdr form)))
3823 (byte-compile-out 'byte-unbind 1))
3824
6e8d0db7 3825(defun byte-compile-track-mouse (form)
d7846e08 3826 (byte-compile-form
6c2161c4
SM
3827 `(funcall '(lambda nil
3828 (track-mouse ,@(byte-compile-top-level-body (cdr form)))))))
6e8d0db7 3829
1c393159
JB
3830(defun byte-compile-condition-case (form)
3831 (let* ((var (nth 1 form))
3832 (byte-compile-bound-variables
3833 (if var (cons var byte-compile-bound-variables)
3834 byte-compile-bound-variables)))
ccb3c8de
CW
3835 (byte-compile-set-symbol-position 'condition-case)
3836 (unless (symbolp var)
3837 (byte-compile-warn
1d5c17c0 3838 "`%s' is not a variable-name or nil (in condition-case)" var))
1c393159
JB
3839 (byte-compile-push-constant var)
3840 (byte-compile-push-constant (byte-compile-top-level
3841 (nth 2 form) for-effect))
3842 (let ((clauses (cdr (cdr (cdr form))))
3843 compiled-clauses)
3844 (while clauses
e27c3564
JB
3845 (let* ((clause (car clauses))
3846 (condition (car clause)))
2abcddce
RS
3847 (cond ((not (or (symbolp condition)
3848 (and (listp condition)
3849 (let ((syms condition) (ok t))
3850 (while syms
3851 (if (not (symbolp (car syms)))
3852 (setq ok nil))
3853 (setq syms (cdr syms)))
3854 ok))))
e27c3564 3855 (byte-compile-warn
1d5c17c0 3856 "`%s' is not a condition name or list of such (in condition-case)"
e27c3564 3857 (prin1-to-string condition)))
2abcddce
RS
3858;; ((not (or (eq condition 't)
3859;; (and (stringp (get condition 'error-message))
3860;; (consp (get condition 'error-conditions)))))
3861;; (byte-compile-warn
1d5c17c0 3862;; "`%s' is not a known condition name (in condition-case)"
2abcddce
RS
3863;; condition))
3864 )
1c393159 3865 (setq compiled-clauses
e27c3564 3866 (cons (cons condition
1c393159
JB
3867 (byte-compile-top-level-body
3868 (cdr clause) for-effect))
3869 compiled-clauses)))
3870 (setq clauses (cdr clauses)))
3871 (byte-compile-push-constant (nreverse compiled-clauses)))
3872 (byte-compile-out 'byte-condition-case 0)))
3873
3874
3875(defun byte-compile-save-excursion (form)
3876 (byte-compile-out 'byte-save-excursion 0)
3877 (byte-compile-body-do-effect (cdr form))
3878 (byte-compile-out 'byte-unbind 1))
3879
3880(defun byte-compile-save-restriction (form)
3881 (byte-compile-out 'byte-save-restriction 0)
3882 (byte-compile-body-do-effect (cdr form))
3883 (byte-compile-out 'byte-unbind 1))
3884
f3e472b0
RS
3885(defun byte-compile-save-current-buffer (form)
3886 (byte-compile-out 'byte-save-current-buffer 0)
3887 (byte-compile-body-do-effect (cdr form))
3888 (byte-compile-out 'byte-unbind 1))
3889
1c393159
JB
3890(defun byte-compile-save-window-excursion (form)
3891 (byte-compile-push-constant
3892 (byte-compile-top-level-body (cdr form) for-effect))
3893 (byte-compile-out 'byte-save-window-excursion 0))
3894
3895(defun byte-compile-with-output-to-temp-buffer (form)
3896 (byte-compile-form (car (cdr form)))
3897 (byte-compile-out 'byte-temp-output-buffer-setup 0)
3898 (byte-compile-body (cdr (cdr form)))
3899 (byte-compile-out 'byte-temp-output-buffer-show 0))
1c393159
JB
3900\f
3901;;; top-level forms elsewhere
3902
3903(byte-defop-compiler-1 defun)
3904(byte-defop-compiler-1 defmacro)
3905(byte-defop-compiler-1 defvar)
3906(byte-defop-compiler-1 defconst byte-compile-defvar)
3907(byte-defop-compiler-1 autoload)
3908(byte-defop-compiler-1 lambda byte-compile-lambda-form)
3909
3910(defun byte-compile-defun (form)
3911 ;; This is not used for file-level defuns with doc strings.
ccb3c8de
CW
3912 (if (symbolp (car form))
3913 (byte-compile-set-symbol-position (car form))
3914 (byte-compile-set-symbol-position 'defun)
eadd6444 3915 (error "defun name must be a symbol, not %s" (car form)))
e5c89ce9
GM
3916 ;; We prefer to generate a defalias form so it will record the function
3917 ;; definition just like interpreting a defun.
3918 (byte-compile-form
3919 (list 'defalias
3920 (list 'quote (nth 1 form))
3921 (byte-compile-byte-code-maker
3922 (byte-compile-lambda (cdr (cdr form)) t)))
3923 t)
1c393159
JB
3924 (byte-compile-constant (nth 1 form)))
3925
3926(defun byte-compile-defmacro (form)
3927 ;; This is not used for file-level defmacros with doc strings.
3928 (byte-compile-body-do-effect
e3a6b82f
SM
3929 (let ((decls (byte-compile-defmacro-declaration form))
3930 (code (byte-compile-byte-code-maker
3931 (byte-compile-lambda (cdr (cdr form)) t))))
3932 `((defalias ',(nth 1 form)
3933 ,(if (eq (car-safe code) 'make-byte-code)
3934 `(cons 'macro ,code)
3935 `'(macro . ,(eval code))))
3936 ,@decls
3937 ',(nth 1 form)))))
1c393159
JB
3938
3939(defun byte-compile-defvar (form)
3940 ;; This is not used for file-level defvar/consts with doc strings.
1bc20d83
GM
3941 (let ((fun (nth 0 form))
3942 (var (nth 1 form))
1c393159
JB
3943 (value (nth 2 form))
3944 (string (nth 3 form)))
ccb3c8de 3945 (byte-compile-set-symbol-position fun)
6c2161c4
SM
3946 (when (or (> (length form) 4)
3947 (and (eq fun 'defconst) (null (cddr form))))
d0e07261
SM
3948 (let ((ncall (length (cdr form))))
3949 (byte-compile-warn
1d5c17c0 3950 "`%s' called with %d argument%s, but %s %s"
d0e07261
SM
3951 fun ncall
3952 (if (= 1 ncall) "" "s")
3953 (if (< ncall 2) "requires" "accepts only")
3954 "2-3")))
2aea6521
GM
3955 (push var byte-compile-bound-variables)
3956 (if (eq fun 'defconst)
3957 (push var byte-compile-const-variables))
1c393159 3958 (byte-compile-body-do-effect
1bc20d83
GM
3959 (list
3960 ;; Put the defined variable in this library's load-history entry
3961 ;; just as a real defvar would, but only in top-level forms.
3614fc84 3962 (when (and (cddr form) (null byte-compile-current-form))
1bc20d83
GM
3963 `(push ',var current-load-list))
3964 (when (> (length form) 3)
3965 (when (and string (not (stringp string)))
1d5c17c0 3966 (byte-compile-warn "third arg to `%s %s' is not a string: %s"
1bc20d83
GM
3967 fun var string))
3968 `(put ',var 'variable-documentation ,string))
fef3407e 3969 (if (cddr form) ; `value' provided
8480fc7c 3970 (let ((byte-compile-not-obsolete-vars (list var)))
6b61353c
KH
3971 (if (eq fun 'defconst)
3972 ;; `defconst' sets `var' unconditionally.
3973 (let ((tmp (make-symbol "defconst-tmp-var")))
3974 `(funcall '(lambda (,tmp) (defconst ,var ,tmp))
3975 ,value))
3976 ;; `defvar' sets `var' only when unbound.
3977 `(if (not (default-boundp ',var)) (setq-default ,var ,value))))
6c2161c4
SM
3978 (when (eq fun 'defconst)
3979 ;; This will signal an appropriate error at runtime.
3980 `(eval ',form)))
1bc20d83 3981 `',var))))
1c393159
JB
3982
3983(defun byte-compile-autoload (form)
ccb3c8de 3984 (byte-compile-set-symbol-position 'autoload)
1c393159
JB
3985 (and (byte-compile-constp (nth 1 form))
3986 (byte-compile-constp (nth 5 form))
3987 (eval (nth 5 form)) ; macro-p
3988 (not (fboundp (eval (nth 1 form))))
3989 (byte-compile-warn
c5091f25 3990 "The compiler ignores `autoload' except at top level. You should
1c393159
JB
3991 probably put the autoload of the macro `%s' at top-level."
3992 (eval (nth 1 form))))
3993 (byte-compile-normal-call form))
3994
c5091f25 3995;; Lambdas in valid places are handled as special cases by various code.
1c393159
JB
3996;; The ones that remain are errors.
3997(defun byte-compile-lambda-form (form)
ccb3c8de 3998 (byte-compile-set-symbol-position 'lambda)
1c393159
JB
3999 (error "`lambda' used as function name is invalid"))
4000
5286a842 4001;; Compile normally, but deal with warnings for the function being defined.
977b50fb
SM
4002(put 'defalias 'byte-hunk-handler 'byte-compile-file-form-defalias)
4003(defun byte-compile-file-form-defalias (form)
5286a842
RS
4004 (if (and (consp (cdr form)) (consp (nth 1 form))
4005 (eq (car (nth 1 form)) 'quote)
4006 (consp (cdr (nth 1 form)))
a7a7ddf1
RS
4007 (symbolp (nth 1 (nth 1 form))))
4008 (let ((constant
4009 (and (consp (nthcdr 2 form))
4010 (consp (nth 2 form))
4011 (eq (car (nth 2 form)) 'quote)
4012 (consp (cdr (nth 2 form)))
4013 (symbolp (nth 1 (nth 2 form))))))
6c2161c4 4014 (byte-compile-defalias-warn (nth 1 (nth 1 form)))
977b50fb
SM
4015 (push (cons (nth 1 (nth 1 form))
4016 (if constant (nth 1 (nth 2 form)) t))
4017 byte-compile-function-environment)))
a2b3fdbf 4018 ;; We used to just do: (byte-compile-normal-call form)
b7a5a208
SM
4019 ;; But it turns out that this fails to optimize the code.
4020 ;; So instead we now do the same as what other byte-hunk-handlers do,
4021 ;; which is to call back byte-compile-file-form and then return nil.
4022 ;; Except that we can't just call byte-compile-file-form since it would
4023 ;; call us right back.
4024 (byte-compile-keep-pending form)
4025 ;; Return nil so the form is not output twice.
4026 nil)
5286a842
RS
4027
4028;; Turn off warnings about prior calls to the function being defalias'd.
4029;; This could be smarter and compare those calls with
4030;; the function it is being aliased to.
6c2161c4 4031(defun byte-compile-defalias-warn (new)
5286a842
RS
4032 (let ((calls (assq new byte-compile-unresolved-functions)))
4033 (if calls
4034 (setq byte-compile-unresolved-functions
4035 (delq calls byte-compile-unresolved-functions)))))
3c9dc1cf
RS
4036
4037(byte-defop-compiler-1 with-no-warnings byte-compile-no-warnings)
4038(defun byte-compile-no-warnings (form)
4039 (let (byte-compile-warnings)
a4f66531 4040 (byte-compile-form (cons 'progn (cdr form)))))
01e4a4fa
SM
4041
4042;; Warn about misuses of make-variable-buffer-local.
49fec531
SM
4043(byte-defop-compiler-1 make-variable-buffer-local
4044 byte-compile-make-variable-buffer-local)
01e4a4fa 4045(defun byte-compile-make-variable-buffer-local (form)
15ce9dcf 4046 (if (and (eq (car-safe (car-safe (cdr-safe form))) 'quote)
cf637a34 4047 (byte-compile-warning-enabled-p 'make-local))
01e4a4fa
SM
4048 (byte-compile-warn
4049 "`make-variable-buffer-local' should be called at toplevel"))
4050 (byte-compile-normal-call form))
4051(put 'make-variable-buffer-local
4052 'byte-hunk-handler 'byte-compile-form-make-variable-buffer-local)
4053(defun byte-compile-form-make-variable-buffer-local (form)
4054 (byte-compile-keep-pending form 'byte-compile-normal-call))
4055
1c393159
JB
4056\f
4057;;; tags
4058
4059;; Note: Most operations will strip off the 'TAG, but it speeds up
4060;; optimization to have the 'TAG as a part of the tag.
4061;; Tags will be (TAG . (tag-number . stack-depth)).
4062(defun byte-compile-make-tag ()
4063 (list 'TAG (setq byte-compile-tag-number (1+ byte-compile-tag-number))))
4064
4065
4066(defun byte-compile-out-tag (tag)
4067 (setq byte-compile-output (cons tag byte-compile-output))
4068 (if (cdr (cdr tag))
4069 (progn
4070 ;; ## remove this someday
4071 (and byte-compile-depth
4072 (not (= (cdr (cdr tag)) byte-compile-depth))
52799cb8 4073 (error "Compiler bug: depth conflict at tag %d" (car (cdr tag))))
1c393159
JB
4074 (setq byte-compile-depth (cdr (cdr tag))))
4075 (setcdr (cdr tag) byte-compile-depth)))
4076
4077(defun byte-compile-goto (opcode tag)
6c2161c4 4078 (push (cons opcode tag) byte-compile-output)
1c393159
JB
4079 (setcdr (cdr tag) (if (memq opcode byte-goto-always-pop-ops)
4080 (1- byte-compile-depth)
4081 byte-compile-depth))
4082 (setq byte-compile-depth (and (not (eq opcode 'byte-goto))
4083 (1- byte-compile-depth))))
4084
4085(defun byte-compile-out (opcode offset)
6c2161c4 4086 (push (cons opcode offset) byte-compile-output)
1c393159
JB
4087 (cond ((eq opcode 'byte-call)
4088 (setq byte-compile-depth (- byte-compile-depth offset)))
4089 ((eq opcode 'byte-return)
4090 ;; This is actually an unnecessary case, because there should be
4091 ;; no more opcodes behind byte-return.
4092 (setq byte-compile-depth nil))
4093 (t
4094 (setq byte-compile-depth (+ byte-compile-depth
4095 (or (aref byte-stack+-info
4096 (symbol-value opcode))
4097 (- (1- offset))))
4098 byte-compile-maxdepth (max byte-compile-depth
4099 byte-compile-maxdepth))))
52799cb8 4100 ;;(if (< byte-compile-depth 0) (error "Compiler error: stack underflow"))
1c393159
JB
4101 )
4102
4103\f
4104;;; call tree stuff
4105
4106(defun byte-compile-annotate-call-tree (form)
4107 (let (entry)
4108 ;; annotate the current call
4109 (if (setq entry (assq (car form) byte-compile-call-tree))
4110 (or (memq byte-compile-current-form (nth 1 entry)) ;callers
4111 (setcar (cdr entry)
4112 (cons byte-compile-current-form (nth 1 entry))))
4113 (setq byte-compile-call-tree
4114 (cons (list (car form) (list byte-compile-current-form) nil)
4115 byte-compile-call-tree)))
4116 ;; annotate the current function
4117 (if (setq entry (assq byte-compile-current-form byte-compile-call-tree))
4118 (or (memq (car form) (nth 2 entry)) ;called
4119 (setcar (cdr (cdr entry))
4120 (cons (car form) (nth 2 entry))))
4121 (setq byte-compile-call-tree
4122 (cons (list byte-compile-current-form nil (list (car form)))
4123 byte-compile-call-tree)))
4124 ))
4125
52799cb8
RS
4126;; Renamed from byte-compile-report-call-tree
4127;; to avoid interfering with completion of byte-compile-file.
fd5285f3 4128;;;###autoload
52799cb8
RS
4129(defun display-call-tree (&optional filename)
4130 "Display a call graph of a specified file.
4131This lists which functions have been called, what functions called
4132them, and what functions they call. The list includes all functions
4133whose definitions have been compiled in this Emacs session, as well as
4134all functions called by those functions.
1c393159 4135
52799cb8
RS
4136The call graph does not include macros, inline functions, or
4137primitives that the byte-code interpreter knows about directly \(eq,
4138cons, etc.\).
1c393159
JB
4139
4140The call tree also lists those functions which are not known to be called
52799cb8
RS
4141\(that is, to which no calls have been compiled\), and which cannot be
4142invoked interactively."
1c393159
JB
4143 (interactive)
4144 (message "Generating call tree...")
4145 (with-output-to-temp-buffer "*Call-Tree*"
4146 (set-buffer "*Call-Tree*")
4147 (erase-buffer)
47cf9d3a 4148 (message "Generating call tree... (sorting on %s)"
1c393159
JB
4149 byte-compile-call-tree-sort)
4150 (insert "Call tree for "
4151 (cond ((null byte-compile-current-file) (or filename "???"))
4152 ((stringp byte-compile-current-file)
4153 byte-compile-current-file)
4154 (t (buffer-name byte-compile-current-file)))
4155 " sorted on "
4156 (prin1-to-string byte-compile-call-tree-sort)
4157 ":\n\n")
4158 (if byte-compile-call-tree-sort
4159 (setq byte-compile-call-tree
4160 (sort byte-compile-call-tree
4161 (cond ((eq byte-compile-call-tree-sort 'callers)
4162 (function (lambda (x y) (< (length (nth 1 x))
4163 (length (nth 1 y))))))
4164 ((eq byte-compile-call-tree-sort 'calls)
4165 (function (lambda (x y) (< (length (nth 2 x))
4166 (length (nth 2 y))))))
4167 ((eq byte-compile-call-tree-sort 'calls+callers)
4168 (function (lambda (x y) (< (+ (length (nth 1 x))
4169 (length (nth 2 x)))
4170 (+ (length (nth 1 y))
4171 (length (nth 2 y)))))))
4172 ((eq byte-compile-call-tree-sort 'name)
4173 (function (lambda (x y) (string< (car x)
4174 (car y)))))
52799cb8 4175 (t (error "`byte-compile-call-tree-sort': `%s' - unknown sort mode"
1c393159
JB
4176 byte-compile-call-tree-sort))))))
4177 (message "Generating call tree...")
4178 (let ((rest byte-compile-call-tree)
4179 (b (current-buffer))
4180 f p
4181 callers calls)
4182 (while rest
4183 (prin1 (car (car rest)) b)
4184 (setq callers (nth 1 (car rest))
4185 calls (nth 2 (car rest)))
4186 (insert "\t"
4187 (cond ((not (fboundp (setq f (car (car rest)))))
4188 (if (null f)
4189 " <top level>";; shouldn't insert nil then, actually -sk
4190 " <not defined>"))
4191 ((subrp (setq f (symbol-function f)))
4192 " <subr>")
4193 ((symbolp f)
4194 (format " ==> %s" f))
ed015bdd 4195 ((byte-code-function-p f)
1c393159
JB
4196 "<compiled function>")
4197 ((not (consp f))
4198 "<malformed function>")
4199 ((eq 'macro (car f))
ed015bdd 4200 (if (or (byte-code-function-p (cdr f))
1c393159
JB
4201 (assq 'byte-code (cdr (cdr (cdr f)))))
4202 " <compiled macro>"
4203 " <macro>"))
4204 ((assq 'byte-code (cdr (cdr f)))
4205 "<compiled lambda>")
4206 ((eq 'lambda (car f))
4207 "<function>")
4208 (t "???"))
4209 (format " (%d callers + %d calls = %d)"
4210 ;; Does the optimizer eliminate common subexpressions?-sk
4211 (length callers)
4212 (length calls)
4213 (+ (length callers) (length calls)))
4214 "\n")
4215 (if callers
4216 (progn
4217 (insert " called by:\n")
4218 (setq p (point))
4219 (insert " " (if (car callers)
4220 (mapconcat 'symbol-name callers ", ")
4221 "<top level>"))
4222 (let ((fill-prefix " "))
78bba1c8
TTN
4223 (fill-region-as-paragraph p (point)))
4224 (unless (= 0 (current-column))
4225 (insert "\n"))))
1c393159
JB
4226 (if calls
4227 (progn
4228 (insert " calls:\n")
4229 (setq p (point))
4230 (insert " " (mapconcat 'symbol-name calls ", "))
4231 (let ((fill-prefix " "))
78bba1c8
TTN
4232 (fill-region-as-paragraph p (point)))
4233 (unless (= 0 (current-column))
4234 (insert "\n"))))
1c393159
JB
4235 (setq rest (cdr rest)))
4236
4237 (message "Generating call tree...(finding uncalled functions...)")
4238 (setq rest byte-compile-call-tree)
416d3588 4239 (let (uncalled def)
1c393159
JB
4240 (while rest
4241 (or (nth 1 (car rest))
416d3588
GM
4242 (null (setq f (caar rest)))
4243 (progn
4244 (setq def (byte-compile-fdefinition f t))
4245 (and (eq (car-safe def) 'macro)
4246 (eq (car-safe (cdr-safe def)) 'lambda)
4247 (setq def (cdr def)))
4248 (functionp def))
4249 (progn
4250 (setq def (byte-compile-fdefinition f nil))
4251 (and (eq (car-safe def) 'macro)
4252 (eq (car-safe (cdr-safe def)) 'lambda)
4253 (setq def (cdr def)))
4254 (commandp def))
1c393159
JB
4255 (setq uncalled (cons f uncalled)))
4256 (setq rest (cdr rest)))
4257 (if uncalled
4258 (let ((fill-prefix " "))
4259 (insert "Noninteractive functions not known to be called:\n ")
4260 (setq p (point))
4261 (insert (mapconcat 'symbol-name (nreverse uncalled) ", "))
416d3588
GM
4262 (fill-region-as-paragraph p (point))))))
4263 (message "Generating call tree...done.")))
1c393159
JB
4264
4265\f
814c447f 4266;;;###autoload
7e7d0f8b
RS
4267(defun batch-byte-compile-if-not-done ()
4268 "Like `byte-compile-file' but doesn't recompile if already up to date.
4269Use this from the command line, with `-batch';
4270it won't work in an interactive Emacs."
4271 (batch-byte-compile t))
4272
1c393159
JB
4273;;; by crl@newton.purdue.edu
4274;;; Only works noninteractively.
fd5285f3 4275;;;###autoload
7e7d0f8b 4276(defun batch-byte-compile (&optional noforce)
52799cb8
RS
4277 "Run `byte-compile-file' on the files remaining on the command line.
4278Use this from the command line, with `-batch';
4279it won't work in an interactive Emacs.
4280Each file is processed even if an error occurred previously.
7e7d0f8b
RS
4281For example, invoke \"emacs -batch -f batch-byte-compile $emacs/ ~/*.el\".
4282If NOFORCE is non-nil, don't recompile a file that seems to be
4283already up-to-date."
1c393159
JB
4284 ;; command-line-args-left is what is left of the command line (from startup.el)
4285 (defvar command-line-args-left) ;Avoid 'free variable' warning
4286 (if (not noninteractive)
52799cb8 4287 (error "`batch-byte-compile' is to be used only with -batch"))
c2768569 4288 (let ((bytecomp-error nil))
1c393159
JB
4289 (while command-line-args-left
4290 (if (file-directory-p (expand-file-name (car command-line-args-left)))
7e7d0f8b 4291 ;; Directory as argument.
1c3b663f
GM
4292 (let ((bytecomp-files (directory-files (car command-line-args-left)))
4293 bytecomp-source bytecomp-dest)
4294 (dolist (bytecomp-file bytecomp-files)
4295 (if (and (string-match emacs-lisp-file-regexp bytecomp-file)
4296 (not (auto-save-file-name-p bytecomp-file))
4297 (setq bytecomp-source
4298 (expand-file-name bytecomp-file
4299 (car command-line-args-left)))
4300 (setq bytecomp-dest (byte-compile-dest-file
4301 bytecomp-source))
4302 (file-exists-p bytecomp-dest)
4303 (file-newer-than-file-p bytecomp-source bytecomp-dest))
4304 (if (null (batch-byte-compile-file bytecomp-source))
c2768569 4305 (setq bytecomp-error t)))))
7e7d0f8b
RS
4306 ;; Specific file argument
4307 (if (or (not noforce)
1c3b663f
GM
4308 (let* ((bytecomp-source (car command-line-args-left))
4309 (bytecomp-dest (byte-compile-dest-file bytecomp-source)))
4310 (or (not (file-exists-p bytecomp-dest))
4311 (file-newer-than-file-p bytecomp-source bytecomp-dest))))
7e7d0f8b 4312 (if (null (batch-byte-compile-file (car command-line-args-left)))
c2768569 4313 (setq bytecomp-error t))))
1c393159 4314 (setq command-line-args-left (cdr command-line-args-left)))
c2768569 4315 (kill-emacs (if bytecomp-error 1 0))))
1c393159 4316
1c3b663f 4317(defun batch-byte-compile-file (bytecomp-file)
6b61353c 4318 (if debug-on-error
1c3b663f 4319 (byte-compile-file bytecomp-file)
6b61353c 4320 (condition-case err
1c3b663f 4321 (byte-compile-file bytecomp-file)
6b61353c
KH
4322 (file-error
4323 (message (if (cdr err)
4324 ">>Error occurred processing %s: %s (%s)"
d09b1c02 4325 ">>Error occurred processing %s: %s")
1c3b663f 4326 bytecomp-file
6b61353c
KH
4327 (get (car err) 'error-message)
4328 (prin1-to-string (cdr err)))
1c3b663f
GM
4329 (let ((bytecomp-destfile (byte-compile-dest-file bytecomp-file)))
4330 (if (file-exists-p bytecomp-destfile)
4331 (delete-file bytecomp-destfile)))
6b61353c
KH
4332 nil)
4333 (error
4334 (message (if (cdr err)
4335 ">>Error occurred processing %s: %s (%s)"
1c393159 4336 ">>Error occurred processing %s: %s")
1c3b663f 4337 bytecomp-file
6b61353c
KH
4338 (get (car err) 'error-message)
4339 (prin1-to-string (cdr err)))
4340 nil))))
1c393159 4341
49fec531
SM
4342(defun byte-compile-refresh-preloaded ()
4343 "Reload any Lisp file that was changed since Emacs was dumped.
4344Use with caution."
4345 (let* ((argv0 (car command-line-args))
4346 (emacs-file (executable-find argv0)))
4347 (if (not (and emacs-file (file-executable-p emacs-file)))
4348 (message "Can't find %s to refresh preloaded Lisp files" argv0)
4349 (dolist (f (reverse load-history))
4350 (setq f (car f))
4351 (if (string-match "elc\\'" f) (setq f (substring f 0 -1)))
4352 (when (and (file-readable-p f)
4353 (file-newer-than-file-p f emacs-file))
4354 (message "Reloading stale %s" (file-name-nondirectory f))
4355 (condition-case nil
4356 (load f 'noerror nil 'nosuffix)
4357 ;; Probably shouldn't happen, but in case of an error, it seems
4358 ;; at least as useful to ignore it as it is to stop compilation.
4359 (error nil)))))))
4360
e9681c45 4361;;;###autoload
6f8e3590 4362(defun batch-byte-recompile-directory (&optional arg)
4f6d5bf0 4363 "Run `byte-recompile-directory' on the dirs remaining on the command line.
79c6071d 4364Must be used only with `-batch', and kills Emacs on completion.
defe3b41
EZ
4365For example, invoke `emacs -batch -f batch-byte-recompile-directory .'.
4366
4367Optional argument ARG is passed as second argument ARG to
516b3653 4368`byte-recompile-directory'; see there for its possible values
defe3b41 4369and corresponding effects."
e27c3564
JB
4370 ;; command-line-args-left is what is left of the command line (startup.el)
4371 (defvar command-line-args-left) ;Avoid 'free variable' warning
4372 (if (not noninteractive)
4373 (error "batch-byte-recompile-directory is to be used only with -batch"))
4374 (or command-line-args-left
4375 (setq command-line-args-left '(".")))
4376 (while command-line-args-left
6f8e3590 4377 (byte-recompile-directory (car command-line-args-left) arg)
e27c3564
JB
4378 (setq command-line-args-left (cdr command-line-args-left)))
4379 (kill-emacs 0))
4380
1c393159 4381(provide 'byte-compile)
200503bb 4382(provide 'bytecomp)
1c393159
JB
4383
4384\f
4385;;; report metering (see the hacks in bytecode.c)
4386
08d21785 4387(defvar byte-code-meter)
52799cb8 4388(defun byte-compile-report-ops ()
52799cb8
RS
4389 (with-output-to-temp-buffer "*Meter*"
4390 (set-buffer "*Meter*")
4391 (let ((i 0) n op off)
4392 (while (< i 256)
4393 (setq n (aref (aref byte-code-meter 0) i)
4394 off nil)
4395 (if t ;(not (zerop n))
4396 (progn
4397 (setq op i)
4398 (setq off nil)
4399 (cond ((< op byte-nth)
4400 (setq off (logand op 7))
4401 (setq op (logand op 248)))
4402 ((>= op byte-constant)
4403 (setq off (- op byte-constant)
4404 op byte-constant)))
4405 (setq op (aref byte-code-vector op))
4406 (insert (format "%-4d" i))
4407 (insert (symbol-name op))
4408 (if off (insert " [" (int-to-string off) "]"))
4409 (indent-to 40)
4410 (insert (int-to-string n) "\n")))
4411 (setq i (1+ i))))))
1c393159
JB
4412\f
4413;; To avoid "lisp nesting exceeds max-lisp-eval-depth" when bytecomp compiles
4414;; itself, compile some of its most used recursive functions (at load time).
4415;;
4416(eval-when-compile
591655c7
SM
4417 (or (byte-code-function-p (symbol-function 'byte-compile-form))
4418 (assq 'byte-code (symbol-function 'byte-compile-form))
4419 (let ((byte-optimize nil) ; do it fast
4420 (byte-compile-warnings nil))
86da2828
GM
4421 (mapc (lambda (x)
4422 (or noninteractive (message "compiling %s..." x))
4423 (byte-compile x)
4424 (or noninteractive (message "compiling %s...done" x)))
4425 '(byte-compile-normal-call
4426 byte-compile-form
4427 byte-compile-body
4428 ;; Inserted some more than necessary, to speed it up.
4429 byte-compile-top-level
4430 byte-compile-out-toplevel
4431 byte-compile-constant
4432 byte-compile-variable-ref))))
591655c7 4433 nil)
fd5285f3 4434
3433c43f
DL
4435(run-hooks 'bytecomp-load-hook)
4436
977b50fb 4437;; arch-tag: 9c97b0f0-8745-4571-bfc3-8dceb677292a
fd5285f3 4438;;; bytecomp.el ends here