Fix a cacheing bug, which led to inordinately slow c-beginning-of-defun.
[emacs.git] / lisp / progmodes / cc-bytecomp.el
blob47ff8e437214859801ab36f5121bde5f32b68b49
1 ;;; cc-bytecomp.el --- compile time setup for proper compilation
3 ;; Copyright (C) 2000-2016 Free Software Foundation, Inc.
5 ;; Author: Martin Stjernholm
6 ;; Maintainer: bug-cc-mode@gnu.org
7 ;; Created: 15-Jul-2000
8 ;; Keywords: c languages
9 ;; Package: cc-mode
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This file is used to ensure that the CC Mode files are correctly
29 ;; compiled regardless the environment (e.g. if an older CC Mode with
30 ;; outdated macros are loaded during compilation). It also provides
31 ;; features to defeat the compiler warnings for selected symbols.
33 ;; There's really nothing CC Mode specific here; this functionality
34 ;; ought to be provided by the byte compilers or some accompanying
35 ;; library. To use it from some package "foo.el", begin by putting
36 ;; the following blurb at the top of the file:
38 ;; (eval-when-compile
39 ;; (let ((load-path
40 ;; (if (and (boundp 'byte-compile-dest-file)
41 ;; (stringp byte-compile-dest-file))
42 ;; (cons (file-name-directory byte-compile-dest-file) load-path)
43 ;; load-path)))
44 ;; (load "cc-bytecomp" nil t))
46 ;; This (unfortunately rather clumsy) form will ensure that the
47 ;; cc-bytecomp.el in the same directory as foo.el is loaded during
48 ;; byte compilation of the latter.
50 ;; At the end of foo.el there should normally be a "(provide 'foo)".
51 ;; Replace it with "(cc-provide 'foo)"; that is necessary to restore
52 ;; the environment after the byte compilation. If you don't have a
53 ;; `provide' at the end, you have to add the following as the very
54 ;; last form in the file:
56 ;; (eval-when-compile (cc-bytecomp-restore-environment))
58 ;; Now everything is set to use the various functions and macros in
59 ;; this package.
61 ;; If your package is split into several files, you should use
62 ;; `cc-require', `cc-require-when-compile' or `cc-load' to load them.
63 ;; That ensures that the files in the same directory always are
64 ;; loaded, to avoid mixup with other versions of them that might exist
65 ;; elsewhere in the load path.
67 ;; To suppress byte compiler warnings, use the macros
68 ;; `cc-bytecomp-defun' and `cc-bytecomp-defvar'.
70 ;; This file is not used at all after the package has been byte
71 ;; compiled. It is however necessary when running uncompiled.
74 ;;; Code:
76 (defvar cc-bytecomp-unbound-variables nil)
77 (defvar cc-bytecomp-original-functions nil)
78 (defvar cc-bytecomp-original-properties nil)
79 (defvar cc-bytecomp-loaded-files nil)
81 (setq cc-bytecomp-unbound-variables nil)
82 (setq cc-bytecomp-original-functions nil)
83 (setq cc-bytecomp-original-properties nil)
84 (setq cc-bytecomp-loaded-files nil)
86 (defvar cc-bytecomp-environment-set nil)
88 (defmacro cc-bytecomp-debug-msg (&rest args)
89 ;;`(message ,@args)
92 (defun cc-bytecomp-compiling-or-loading ()
93 ;; Determine whether byte-compilation or loading is currently active,
94 ;; returning 'compiling, 'loading or nil.
95 ;; If both are active, the "innermost" activity counts. Note that
96 ;; compilation can trigger loading (various `require' type forms)
97 ;; and loading can trigger compilation (the package manager does
98 ;; this). We walk the lisp stack if necessary.
99 (cond
100 ((and load-in-progress
101 (boundp 'byte-compile-dest-file)
102 (stringp byte-compile-dest-file))
103 (let ((n 0) elt)
104 (while (and
105 (setq elt (backtrace-frame n))
106 (not (and (car elt)
107 (memq (cadr elt)
108 '(load require
109 byte-compile-file byte-recompile-directory
110 batch-byte-compile)))))
111 (setq n (1+ n)))
112 (cond
113 ((memq (cadr elt) '(load require))
114 'loading)
115 ((memq (cadr elt) '(byte-compile-file
116 byte-recompile-directory
117 batch-byte-compile))
118 'compiling)
119 (t ; Can't happen.
120 (message "cc-bytecomp-compiling-or-loading: System flags spuriously set")
121 nil))))
122 (load-in-progress
123 ;; Being loaded.
124 'loading)
125 ((and (boundp 'byte-compile-dest-file)
126 (stringp byte-compile-dest-file))
127 ;; Being compiled.
128 'compiling)
130 ;; Being evaluated interactively.
131 nil)))
133 (defsubst cc-bytecomp-is-compiling ()
134 "Return non-nil if eval'ed during compilation."
135 (eq (cc-bytecomp-compiling-or-loading) 'compiling))
137 (defsubst cc-bytecomp-is-loading ()
138 "Return non-nil if eval'ed during loading.
139 Nil will be returned if we're in a compilation triggered by the loading."
140 (eq (cc-bytecomp-compiling-or-loading) 'loading))
142 (defun cc-bytecomp-setup-environment ()
143 ;; Eval'ed during compilation to setup variables, functions etc
144 ;; declared with `cc-bytecomp-defvar' et al.
145 (if (not (cc-bytecomp-is-loading))
146 (let (p)
147 (if cc-bytecomp-environment-set
148 (error "Byte compilation environment already set - \
149 perhaps a `cc-bytecomp-restore-environment' is forgotten somewhere"))
150 (setq p cc-bytecomp-unbound-variables)
151 (while p
152 (if (not (boundp (car p)))
153 (progn
154 (eval `(defvar ,(car p)))
155 (set (car p) (intern (concat "cc-bytecomp-ignore-var:"
156 (symbol-name (car p)))))
157 (cc-bytecomp-debug-msg
158 "cc-bytecomp-setup-environment: Covered variable %s"
159 (car p))))
160 (setq p (cdr p)))
161 (setq p cc-bytecomp-original-functions)
162 (while p
163 (let ((fun (car (car p)))
164 (temp-macro (car (cdr (car p)))))
165 (if (not (fboundp fun))
166 (if temp-macro
167 (progn
168 (eval `(defmacro ,fun ,@temp-macro))
169 (cc-bytecomp-debug-msg
170 "cc-bytecomp-setup-environment: Bound macro %s" fun))
171 (fset fun (intern (concat "cc-bytecomp-ignore-fun:"
172 (symbol-name fun))))
173 (cc-bytecomp-debug-msg
174 "cc-bytecomp-setup-environment: Covered function %s" fun))))
175 (setq p (cdr p)))
176 (setq p cc-bytecomp-original-properties)
177 (while p
178 (let ((sym (car (car (car p))))
179 (prop (cdr (car (car p))))
180 (tempdef (car (cdr (car p)))))
181 (put sym prop tempdef)
182 (cc-bytecomp-debug-msg
183 "cc-bytecomp-setup-environment: Bound property %s for %s to %s"
184 prop sym tempdef))
185 (setq p (cdr p)))
186 (setq cc-bytecomp-environment-set t)
187 (cc-bytecomp-debug-msg
188 "cc-bytecomp-setup-environment: Done"))))
190 (defun cc-bytecomp-restore-environment ()
191 ;; Eval'ed during compilation to restore variables, functions etc
192 ;; declared with `cc-bytecomp-defvar' et al.
193 (if (not (cc-bytecomp-is-loading))
194 (let (p)
195 (setq p cc-bytecomp-unbound-variables)
196 (while p
197 (let ((var (car p)))
198 (if (boundp var)
199 (if (eq (intern (concat "cc-bytecomp-ignore-var:"
200 (symbol-name var)))
201 (symbol-value var))
202 (progn
203 (makunbound var)
204 (cc-bytecomp-debug-msg
205 "cc-bytecomp-restore-environment: Unbound variable %s"
206 var))
207 (cc-bytecomp-debug-msg
208 "cc-bytecomp-restore-environment: Not restoring variable %s"
209 var))))
210 (setq p (cdr p)))
211 (setq p cc-bytecomp-original-functions)
212 (while p
213 (let ((fun (car (car p)))
214 (temp-macro (car (cdr (car p))))
215 (def (car (cdr (cdr (car p))))))
216 (if (fboundp fun)
217 (if (eq (or temp-macro
218 (intern (concat "cc-bytecomp-ignore-fun:"
219 (symbol-name fun))))
220 (symbol-function fun))
221 (if (eq def 'unbound)
222 (progn
223 (fmakunbound fun)
224 (cc-bytecomp-debug-msg
225 "cc-bytecomp-restore-environment: Unbound function %s"
226 fun))
227 (fset fun def)
228 (cc-bytecomp-debug-msg
229 "cc-bytecomp-restore-environment: Restored function %s"
230 fun))
231 (cc-bytecomp-debug-msg
232 "cc-bytecomp-restore-environment: Not restoring function %s"
233 fun))))
234 (setq p (cdr p)))
235 (setq p cc-bytecomp-original-properties)
236 (while p
237 (let ((sym (car (car (car p))))
238 (prop (cdr (car (car p))))
239 (tempdef (car (cdr (car p))))
240 (origdef (cdr (cdr (car p)))))
241 (if (eq (get sym prop) tempdef)
242 (progn
243 (put sym prop origdef)
244 (cc-bytecomp-debug-msg
245 "cc-bytecomp-restore-environment: Restored property %s for %s to %s"
246 prop sym origdef))
247 (cc-bytecomp-debug-msg
248 "cc-bytecomp-restore-environment: Not restoring property %s for %s"
249 prop sym)))
250 (setq p (cdr p)))
251 (setq cc-bytecomp-environment-set nil)
252 (cc-bytecomp-debug-msg
253 "cc-bytecomp-restore-environment: Done"))))
255 (defun cc-bytecomp-load (cc-part)
256 ;; A dummy function which will immediately be overwritten by the
257 ;; following at load time. This should suppress the byte compiler
258 ;; error that the function is "not known to be defined".
260 (eval
261 ;; This eval is to avoid byte compilation of the function below.
262 ;; There's some bug in XEmacs 21.4.6 that can cause it to dump core
263 ;; here otherwise. My theory is that `cc-bytecomp-load' might be
264 ;; redefined recursively during the `load' inside it, and if it in
265 ;; that case is byte compiled then the byte interpreter gets
266 ;; confused. I haven't succeeded in isolating the bug, though. /mast
268 '(defun cc-bytecomp-load (cc-part)
269 ;; Eval'ed during compilation to load a CC Mode file from the source
270 ;; directory (assuming it's the same as the compiled file
271 ;; destination dir).
272 (if (and (boundp 'byte-compile-dest-file)
273 (stringp byte-compile-dest-file))
274 (progn
275 (cc-bytecomp-restore-environment)
276 (let ((load-path
277 (cons (file-name-directory byte-compile-dest-file)
278 load-path))
279 (cc-file (concat cc-part ".el")))
280 (if (member cc-file cc-bytecomp-loaded-files)
282 (setq cc-bytecomp-loaded-files
283 (cons cc-file cc-bytecomp-loaded-files))
284 (cc-bytecomp-debug-msg
285 "cc-bytecomp-load: Loading %S" cc-file)
286 (load cc-file nil t t)
287 (cc-bytecomp-debug-msg
288 "cc-bytecomp-load: Loaded %S" cc-file)))
289 (cc-bytecomp-setup-environment)
290 t))))
292 (defmacro cc-require (cc-part)
293 "Force loading of the corresponding .el file in the current directory
294 during compilation, but compile in a `require'. Don't use within
295 `eval-when-compile'.
297 Having cyclic cc-require's will result in infinite recursion. That's
298 somewhat intentional."
299 `(progn
300 (eval-when-compile
301 (cc-bytecomp-load (symbol-name ,cc-part)))
302 (require ,cc-part)))
304 (defmacro cc-conditional-require (cc-part condition)
305 "If the CONDITION is satisfied at compile time, (i) force the
306 file CC-PART.el in the current directory to be loaded at compile
307 time, (ii) generate code to load the file at load time.
309 CC-PART will normally be a quoted name such as 'cc-fix.
310 CONDITION should not be quoted."
311 (if (eval condition)
312 (progn
313 (cc-bytecomp-load (symbol-name (eval cc-part)))
314 `(require ,cc-part))
315 '(progn)))
317 (defmacro cc-conditional-require-after-load (cc-part file condition)
318 "If the CONDITION is satisfied at compile time, (i) force the
319 file CC-PART.el in the current directory to be loaded at compile
320 time, (ii) generate an `eval-after-load' form to load CC-PART.el
321 after the loading of FILE.
323 CC-PART will normally be a quoted name such as 'cc-fix. FILE
324 should be a string. CONDITION should not be quoted."
325 (if (eval condition)
326 (progn
327 (cc-bytecomp-load (symbol-name (eval cc-part)))
328 `(eval-after-load ,file
329 '(require ,cc-part)))
330 '(progn)))
332 (defmacro cc-provide (feature)
333 "A replacement for the `provide' form that restores the environment
334 after the compilation. Don't use within `eval-when-compile'."
335 `(progn
336 (eval-when-compile (cc-bytecomp-restore-environment))
337 (provide ,feature)))
339 (defmacro cc-load (cc-part)
340 "Force loading of the corresponding .el file in the current directory
341 during compilation. Don't use outside `eval-when-compile' or
342 `eval-and-compile'.
344 Having cyclic cc-load's will result in infinite recursion. That's
345 somewhat intentional."
346 `(or (and (featurep 'cc-bytecomp)
347 (cc-bytecomp-load ,cc-part))
348 (load ,cc-part nil t nil)))
350 (defmacro cc-require-when-compile (cc-part)
351 "Force loading of the corresponding .el file in the current directory
352 during compilation, but do a compile time `require' otherwise. Don't
353 use within `eval-when-compile'."
354 `(eval-when-compile
355 (if (and (fboundp 'cc-bytecomp-is-compiling)
356 (cc-bytecomp-is-compiling))
357 (if (not (featurep ,cc-part))
358 (cc-bytecomp-load (symbol-name ,cc-part)))
359 (require ,cc-part))))
361 (defmacro cc-external-require (feature)
362 "Do a `require' of an external package.
363 This restores and sets up the compilation environment before and
364 afterwards. Don't use within `eval-when-compile'."
365 `(progn
366 (eval-when-compile (cc-bytecomp-restore-environment))
367 (require ,feature)
368 (eval-when-compile (cc-bytecomp-setup-environment))))
370 (defmacro cc-bytecomp-defvar (var)
371 "Binds the symbol as a variable during compilation of the file,
372 to silence the byte compiler. Don't use within `eval-when-compile'."
373 `(eval-when-compile
374 (if (boundp ',var)
375 (cc-bytecomp-debug-msg
376 "cc-bytecomp-defvar: %s bound already as variable" ',var)
377 (if (not (memq ',var cc-bytecomp-unbound-variables))
378 (progn
379 (cc-bytecomp-debug-msg
380 "cc-bytecomp-defvar: Saving %s (as unbound)" ',var)
381 (setq cc-bytecomp-unbound-variables
382 (cons ',var cc-bytecomp-unbound-variables))))
383 (if (cc-bytecomp-is-compiling)
384 (progn
385 (defvar ,var)
386 (set ',var (intern (concat "cc-bytecomp-ignore-var:"
387 (symbol-name ',var))))
388 (cc-bytecomp-debug-msg
389 "cc-bytecomp-defvar: Covered variable %s" ',var))))))
391 (defmacro cc-bytecomp-defun (fun)
392 "Bind the symbol as a function during compilation of the file,
393 to silence the byte compiler. Don't use within `eval-when-compile'.
395 If the symbol already is bound as a function, it will keep that
396 definition. That means that this macro will not shut up warnings
397 about incorrect number of arguments. It's dangerous to try to replace
398 existing functions since the byte compiler might need the definition
399 at compile time, e.g. for macros and inline functions."
400 `(eval-when-compile
401 (if (fboundp ',fun)
402 (cc-bytecomp-debug-msg
403 "cc-bytecomp-defun: %s bound already as function" ',fun)
404 (if (not (assq ',fun cc-bytecomp-original-functions))
405 (progn
406 (cc-bytecomp-debug-msg
407 "cc-bytecomp-defun: Saving %s (as unbound)" ',fun)
408 (setq cc-bytecomp-original-functions
409 (cons (list ',fun nil 'unbound)
410 cc-bytecomp-original-functions))))
411 (if (cc-bytecomp-is-compiling)
412 (progn
413 (fset ',fun (intern (concat "cc-bytecomp-ignore-fun:"
414 (symbol-name ',fun))))
415 (cc-bytecomp-debug-msg
416 "cc-bytecomp-defun: Covered function %s" ',fun))))))
418 (defmacro cc-bytecomp-put (symbol propname value)
419 "Set a property on a symbol during compilation (and evaluation) of
420 the file. Don't use outside `eval-when-compile'."
421 `(eval-when-compile
422 (if (not (assoc (cons ,symbol ,propname) cc-bytecomp-original-properties))
423 (progn
424 (cc-bytecomp-debug-msg
425 "cc-bytecomp-put: Saving property %s for %s with value %s"
426 ,propname ,symbol (get ,symbol ,propname))
427 (setq cc-bytecomp-original-properties
428 (cons (cons (cons ,symbol ,propname)
429 (cons ,value (get ,symbol ,propname)))
430 cc-bytecomp-original-properties))))
431 (put ,symbol ,propname ,value)
432 (cc-bytecomp-debug-msg
433 "cc-bytecomp-put: Bound property %s for %s to %s"
434 ,propname ,symbol ,value)))
436 (defmacro cc-bytecomp-boundp (symbol)
437 "Return non-nil if the given symbol is bound as a variable outside
438 the compilation. This is the same as using `boundp' but additionally
439 exclude any variables that have been bound during compilation with
440 `cc-bytecomp-defvar'."
441 (if (and (cc-bytecomp-is-compiling)
442 (memq (car (cdr symbol)) cc-bytecomp-unbound-variables))
444 `(boundp ,symbol)))
446 (defmacro cc-bytecomp-fboundp (symbol)
447 "Return non-nil if the given symbol is bound as a function outside
448 the compilation. This is the same as using `fboundp' but additionally
449 exclude any functions that have been bound during compilation with
450 `cc-bytecomp-defun'."
451 (let (fun-elem)
452 (if (and (cc-bytecomp-is-compiling)
453 (setq fun-elem (assq (car (cdr symbol))
454 cc-bytecomp-original-functions))
455 (eq (elt fun-elem 2) 'unbound))
457 `(fboundp ,symbol))))
460 (provide 'cc-bytecomp)
462 ;; Local Variables:
463 ;; indent-tabs-mode: t
464 ;; tab-width: 8
465 ;; End:
466 ;;; cc-bytecomp.el ends here