Document reserved keys
[emacs.git] / lisp / progmodes / cc-bytecomp.el
blob754edf9f3ddda7a09e6e124e2b9d77ab426a8585
1 ;;; cc-bytecomp.el --- compile time setup for proper compilation
3 ;; Copyright (C) 2000-2018 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 <https://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 (ignore args)
90 ;;`(message ,@args)
93 (defun cc-bytecomp-compiling-or-loading ()
94 ;; Determine whether byte-compilation or loading is currently active,
95 ;; returning 'compiling, 'loading or nil.
96 ;; If both are active, the "innermost" activity counts. Note that
97 ;; compilation can trigger loading (various `require' type forms)
98 ;; and loading can trigger compilation (the package manager does
99 ;; this). We walk the lisp stack if necessary.
100 (cond
101 ((and load-in-progress
102 (boundp 'byte-compile-dest-file)
103 (stringp byte-compile-dest-file))
104 (let ((n 0) elt)
105 (while (and
106 (setq elt (backtrace-frame n))
107 (not (and (car elt)
108 (memq (cadr elt)
109 '(load require
110 byte-compile-file byte-recompile-directory
111 batch-byte-compile)))))
112 (setq n (1+ n)))
113 (cond
114 ((memq (cadr elt) '(load require))
115 'loading)
116 ((memq (cadr elt) '(byte-compile-file
117 byte-recompile-directory
118 batch-byte-compile))
119 'compiling)
120 (t ; Can't happen.
121 (message "cc-bytecomp-compiling-or-loading: System flags spuriously set")
122 nil))))
123 (load-in-progress
124 ;; Being loaded.
125 'loading)
126 ((and (boundp 'byte-compile-dest-file)
127 (stringp byte-compile-dest-file))
128 ;; Being compiled.
129 'compiling)
131 ;; Being evaluated interactively.
132 nil)))
134 (defsubst cc-bytecomp-is-compiling ()
135 "Return non-nil if eval'ed during compilation."
136 (eq (cc-bytecomp-compiling-or-loading) 'compiling))
138 (defsubst cc-bytecomp-is-loading ()
139 "Return non-nil if eval'ed during loading.
140 Nil will be returned if we're in a compilation triggered by the loading."
141 (eq (cc-bytecomp-compiling-or-loading) 'loading))
143 (defun cc-bytecomp-setup-environment ()
144 ;; Eval'ed during compilation to setup variables, functions etc
145 ;; declared with `cc-bytecomp-defvar' et al.
146 (if (not (cc-bytecomp-is-loading))
147 (let (p)
148 (if cc-bytecomp-environment-set
149 (error "Byte compilation environment already set - \
150 perhaps a `cc-bytecomp-restore-environment' is forgotten somewhere"))
151 (setq p cc-bytecomp-unbound-variables)
152 (while p
153 (if (not (boundp (car p)))
154 (progn
155 (eval `(defvar ,(car p)))
156 (set (car p) (intern (concat "cc-bytecomp-ignore-var:"
157 (symbol-name (car p)))))
158 (cc-bytecomp-debug-msg
159 "cc-bytecomp-setup-environment: Covered variable %s"
160 (car p))))
161 (setq p (cdr p)))
162 (setq p cc-bytecomp-original-functions)
163 (while p
164 (let ((fun (car (car p)))
165 (temp-macro (car (cdr (car p)))))
166 (if (not (fboundp fun))
167 (if temp-macro
168 (progn
169 (eval `(defmacro ,fun ,@temp-macro))
170 (cc-bytecomp-debug-msg
171 "cc-bytecomp-setup-environment: Bound macro %s" fun))
172 (fset fun (intern (concat "cc-bytecomp-ignore-fun:"
173 (symbol-name fun))))
174 (cc-bytecomp-debug-msg
175 "cc-bytecomp-setup-environment: Covered function %s" fun))))
176 (setq p (cdr p)))
177 (setq p cc-bytecomp-original-properties)
178 (while p
179 (let ((sym (car (car (car p))))
180 (prop (cdr (car (car p))))
181 (tempdef (car (cdr (car p)))))
182 (put sym prop tempdef)
183 (cc-bytecomp-debug-msg
184 "cc-bytecomp-setup-environment: Bound property %s for %s to %s"
185 prop sym tempdef))
186 (setq p (cdr p)))
187 (setq cc-bytecomp-environment-set t)
188 (cc-bytecomp-debug-msg
189 "cc-bytecomp-setup-environment: Done"))))
191 (defun cc-bytecomp-restore-environment ()
192 ;; Eval'ed during compilation to restore variables, functions etc
193 ;; declared with `cc-bytecomp-defvar' et al.
194 (if (not (cc-bytecomp-is-loading))
195 (let (p)
196 (setq p cc-bytecomp-unbound-variables)
197 (while p
198 (let ((var (car p)))
199 (if (boundp var)
200 (if (eq (intern (concat "cc-bytecomp-ignore-var:"
201 (symbol-name var)))
202 (symbol-value var))
203 (progn
204 (makunbound var)
205 (cc-bytecomp-debug-msg
206 "cc-bytecomp-restore-environment: Unbound variable %s"
207 var))
208 (cc-bytecomp-debug-msg
209 "cc-bytecomp-restore-environment: Not restoring variable %s"
210 var))))
211 (setq p (cdr p)))
212 (setq p cc-bytecomp-original-functions)
213 (while p
214 (let ((fun (car (car p)))
215 (temp-macro (car (cdr (car p))))
216 (def (car (cdr (cdr (car p))))))
217 (if (fboundp fun)
218 (if (eq (or temp-macro
219 (intern (concat "cc-bytecomp-ignore-fun:"
220 (symbol-name fun))))
221 (symbol-function fun))
222 (if (eq def 'unbound)
223 (progn
224 (fmakunbound fun)
225 (cc-bytecomp-debug-msg
226 "cc-bytecomp-restore-environment: Unbound function %s"
227 fun))
228 (fset fun def)
229 (cc-bytecomp-debug-msg
230 "cc-bytecomp-restore-environment: Restored function %s"
231 fun))
232 (cc-bytecomp-debug-msg
233 "cc-bytecomp-restore-environment: Not restoring function %s"
234 fun))))
235 (setq p (cdr p)))
236 (setq p cc-bytecomp-original-properties)
237 (while p
238 (let ((sym (car (car (car p))))
239 (prop (cdr (car (car p))))
240 (tempdef (car (cdr (car p))))
241 (origdef (cdr (cdr (car p)))))
242 (if (eq (get sym prop) tempdef)
243 (progn
244 (put sym prop origdef)
245 (cc-bytecomp-debug-msg
246 "cc-bytecomp-restore-environment: Restored property %s for %s to %s"
247 prop sym origdef))
248 (cc-bytecomp-debug-msg
249 "cc-bytecomp-restore-environment: Not restoring property %s for %s"
250 prop sym)))
251 (setq p (cdr p)))
252 (setq cc-bytecomp-environment-set nil)
253 (cc-bytecomp-debug-msg
254 "cc-bytecomp-restore-environment: Done"))))
256 (defun cc-bytecomp-load (_cc-part)
257 ;; A dummy function which will immediately be overwritten by the
258 ;; following at load time. This should suppress the byte compiler
259 ;; error that the function is "not known to be defined".
261 (eval
262 ;; This eval is to avoid byte compilation of the function below.
263 ;; There's some bug in XEmacs 21.4.6 that can cause it to dump core
264 ;; here otherwise. My theory is that `cc-bytecomp-load' might be
265 ;; redefined recursively during the `load' inside it, and if it in
266 ;; that case is byte compiled then the byte interpreter gets
267 ;; confused. I haven't succeeded in isolating the bug, though. /mast
269 '(defun cc-bytecomp-load (cc-part)
270 ;; Eval'ed during compilation to load a CC Mode file from the source
271 ;; directory (assuming it's the same as the compiled file
272 ;; destination dir).
273 (if (and (boundp 'byte-compile-dest-file)
274 (stringp byte-compile-dest-file))
275 (progn
276 (cc-bytecomp-restore-environment)
277 (let ((load-path
278 (cons (file-name-directory byte-compile-dest-file)
279 load-path))
280 (cc-file (concat cc-part ".el")))
281 (if (member cc-file cc-bytecomp-loaded-files)
283 (setq cc-bytecomp-loaded-files
284 (cons cc-file cc-bytecomp-loaded-files))
285 (cc-bytecomp-debug-msg
286 "cc-bytecomp-load: Loading %S" cc-file)
287 (load cc-file nil t t)
288 (cc-bytecomp-debug-msg
289 "cc-bytecomp-load: Loaded %S" cc-file)))
290 (cc-bytecomp-setup-environment)
291 t))))
293 (defmacro cc-require (cc-part)
294 "Force loading of the corresponding .el file in the current directory
295 during compilation, but compile in a `require'. Don't use within
296 `eval-when-compile'.
298 Having cyclic cc-require's will result in infinite recursion. That's
299 somewhat intentional."
300 `(progn
301 (eval-when-compile
302 (cc-bytecomp-load (symbol-name ,cc-part)))
303 (require ,cc-part)))
305 (defmacro cc-conditional-require (cc-part condition)
306 "If the CONDITION is satisfied at compile time, (i) force the
307 file CC-PART.el in the current directory to be loaded at compile
308 time, (ii) generate code to load the file at load time.
310 CC-PART will normally be a quoted name such as 'cc-fix.
311 CONDITION should not be quoted."
312 (if (eval condition)
313 (progn
314 (cc-bytecomp-load (symbol-name (eval cc-part)))
315 `(require ,cc-part))
316 '(progn)))
318 (defmacro cc-conditional-require-after-load (cc-part file condition)
319 "If the CONDITION is satisfied at compile time, (i) force the
320 file CC-PART.el in the current directory to be loaded at compile
321 time, (ii) generate an `eval-after-load' form to load CC-PART.el
322 after the loading of FILE.
324 CC-PART will normally be a quoted name such as 'cc-fix. FILE
325 should be a string. CONDITION should not be quoted."
326 (if (eval condition)
327 (progn
328 (cc-bytecomp-load (symbol-name (eval cc-part)))
329 `(eval-after-load ,file
330 '(require ,cc-part)))
331 '(progn)))
333 (defmacro cc-provide (feature)
334 "A replacement for the `provide' form that restores the environment
335 after the compilation. Don't use within `eval-when-compile'."
336 `(progn
337 (eval-when-compile (cc-bytecomp-restore-environment))
338 (provide ,feature)))
340 (defmacro cc-load (cc-part)
341 "Force loading of the corresponding .el file in the current directory
342 during compilation. Don't use outside `eval-when-compile' or
343 `eval-and-compile'.
345 Having cyclic cc-load's will result in infinite recursion. That's
346 somewhat intentional."
347 `(or (and (featurep 'cc-bytecomp)
348 (cc-bytecomp-load ,cc-part))
349 (load ,cc-part nil t nil)))
351 (defmacro cc-require-when-compile (cc-part)
352 "Force loading of the corresponding .el file in the current directory
353 during compilation, but do a compile time `require' otherwise. Don't
354 use within `eval-when-compile'."
355 `(eval-when-compile
356 (if (and (fboundp 'cc-bytecomp-is-compiling)
357 (cc-bytecomp-is-compiling))
358 (if (not (featurep ,cc-part))
359 (cc-bytecomp-load (symbol-name ,cc-part)))
360 (require ,cc-part))))
362 (defmacro cc-external-require (feature)
363 "Do a `require' of an external package.
364 This restores and sets up the compilation environment before and
365 afterwards. Don't use within `eval-when-compile'."
366 `(progn
367 (eval-when-compile (cc-bytecomp-restore-environment))
368 (require ,feature)
369 (eval-when-compile (cc-bytecomp-setup-environment))))
371 (defmacro cc-bytecomp-defvar (var)
372 "Binds the symbol as a variable during compilation of the file,
373 to silence the byte compiler. Don't use within `eval-when-compile'."
374 `(eval-when-compile
375 (if (boundp ',var)
376 (cc-bytecomp-debug-msg
377 "cc-bytecomp-defvar: %s bound already as variable" ',var)
378 (if (not (memq ',var cc-bytecomp-unbound-variables))
379 (progn
380 (cc-bytecomp-debug-msg
381 "cc-bytecomp-defvar: Saving %s (as unbound)" ',var)
382 (setq cc-bytecomp-unbound-variables
383 (cons ',var cc-bytecomp-unbound-variables))))
384 (if (cc-bytecomp-is-compiling)
385 (progn
386 (defvar ,var)
387 (set ',var (intern (concat "cc-bytecomp-ignore-var:"
388 (symbol-name ',var))))
389 (cc-bytecomp-debug-msg
390 "cc-bytecomp-defvar: Covered variable %s" ',var))))))
392 (defmacro cc-bytecomp-defun (fun)
393 "Bind the symbol as a function during compilation of the file,
394 to silence the byte compiler. Don't use within `eval-when-compile'.
396 If the symbol already is bound as a function, it will keep that
397 definition. That means that this macro will not shut up warnings
398 about incorrect number of arguments. It's dangerous to try to replace
399 existing functions since the byte compiler might need the definition
400 at compile time, e.g. for macros and inline functions."
401 `(eval-when-compile
402 (if (fboundp ',fun)
403 (cc-bytecomp-debug-msg
404 "cc-bytecomp-defun: %s bound already as function" ',fun)
405 (if (not (assq ',fun cc-bytecomp-original-functions))
406 (progn
407 (cc-bytecomp-debug-msg
408 "cc-bytecomp-defun: Saving %s (as unbound)" ',fun)
409 (setq cc-bytecomp-original-functions
410 (cons (list ',fun nil 'unbound)
411 cc-bytecomp-original-functions))))
412 (if (cc-bytecomp-is-compiling)
413 (progn
414 (fset ',fun (intern (concat "cc-bytecomp-ignore-fun:"
415 (symbol-name ',fun))))
416 (cc-bytecomp-debug-msg
417 "cc-bytecomp-defun: Covered function %s" ',fun))))))
419 (defmacro cc-bytecomp-put (symbol propname value)
420 "Set a property on a symbol during compilation (and evaluation) of
421 the file. Don't use outside `eval-when-compile'."
422 `(eval-when-compile
423 (if (not (assoc (cons ,symbol ,propname) cc-bytecomp-original-properties))
424 (progn
425 (cc-bytecomp-debug-msg
426 "cc-bytecomp-put: Saving property %s for %s with value %s"
427 ,propname ,symbol (get ,symbol ,propname))
428 (setq cc-bytecomp-original-properties
429 (cons (cons (cons ,symbol ,propname)
430 (cons ,value (get ,symbol ,propname)))
431 cc-bytecomp-original-properties))))
432 (put ,symbol ,propname ,value)
433 (cc-bytecomp-debug-msg
434 "cc-bytecomp-put: Bound property %s for %s to %s"
435 ,propname ,symbol ,value)))
437 (defmacro cc-bytecomp-boundp (symbol)
438 "Return non-nil if the given symbol is bound as a variable outside
439 the compilation. This is the same as using `boundp' but additionally
440 exclude any variables that have been bound during compilation with
441 `cc-bytecomp-defvar'."
442 (if (and (cc-bytecomp-is-compiling)
443 (memq (car (cdr symbol)) cc-bytecomp-unbound-variables))
445 `(boundp ,symbol)))
447 (defmacro cc-bytecomp-fboundp (symbol)
448 "Return non-nil if the given symbol is bound as a function outside
449 the compilation. This is the same as using `fboundp' but additionally
450 exclude any functions that have been bound during compilation with
451 `cc-bytecomp-defun'."
452 (let (fun-elem)
453 (if (and (cc-bytecomp-is-compiling)
454 (setq fun-elem (assq (car (cdr symbol))
455 cc-bytecomp-original-functions))
456 (eq (elt fun-elem 2) 'unbound))
458 `(fboundp ,symbol))))
461 (provide 'cc-bytecomp)
463 ;; Local Variables:
464 ;; indent-tabs-mode: t
465 ;; tab-width: 8
466 ;; End:
467 ;;; cc-bytecomp.el ends here