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