36920
|
1 ;;; cc-bytecomp.el --- Compile time setup for proper compilation
|
|
2
|
|
3 ;; Copyright (C) 2000, 01 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Martin Stjernholm
|
|
6 ;; Maintainer: bug-cc-mode@gnu.org
|
|
7 ;; Created: 15-Jul-2000
|
|
8 ;; Version: See cc-mode.el
|
|
9 ;; Keywords: c languages oop
|
|
10
|
38401
|
11 ;; This file is part of GNU Emacs.
|
|
12
|
|
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
36920
|
14 ;; it under the terms of the GNU General Public License as published by
|
38401
|
15 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;; any later version.
|
36920
|
17
|
38401
|
18 ;; GNU Emacs is distributed in the hope that it will be useful,
|
36920
|
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
|
38401
|
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
36920
|
26 ;; Boston, MA 02111-1307, USA.
|
|
27
|
|
28 ;;; Commentary:
|
|
29
|
|
30 ;; This file is used to ensure that the CC Mode files are correctly
|
|
31 ;; compiled regardless the environment (e.g. if an older CC Mode with
|
|
32 ;; outdated macros are loaded during compilation). It also provides
|
|
33 ;; features to defeat the compiler warnings for selected symbols.
|
|
34
|
|
35
|
|
36 (defvar cc-bytecomp-unbound-variables nil)
|
|
37 (defvar cc-bytecomp-original-functions nil)
|
|
38 (defvar cc-bytecomp-original-properties nil)
|
|
39 (defvar cc-bytecomp-load-depth 0)
|
|
40 (defvar cc-bytecomp-loaded-files nil)
|
|
41 (defvar cc-bytecomp-environment-set nil)
|
|
42
|
|
43 (put 'cc-eval-when-compile 'lisp-indent-hook 0)
|
|
44 (defmacro cc-eval-when-compile (&rest body)
|
|
45 "Like `progn', but evaluates the body at compile time.
|
|
46 The result of the body appears to the compiler as a quoted constant.
|
|
47
|
|
48 This variant works around what looks like a bug in
|
|
49 `eval-when-compile': During byte compilation it byte compiles its
|
|
50 contents before evaluating it. That can cause forms to be compiled in
|
|
51 situations they aren't intended to be compiled. See cc-bytecomp.el
|
|
52 for further discussion."
|
|
53 ;;
|
|
54 ;; Example: It's not possible to defsubst a primitive, e.g. the
|
|
55 ;; following will produce an error (in any emacs flavor), since
|
|
56 ;; `nthcdr' is a primitive function that's handled specially by the
|
|
57 ;; byte compiler and thus can't be redefined:
|
|
58 ;;
|
|
59 ;; (defsubst nthcdr (val) val)
|
|
60 ;;
|
|
61 ;; `defsubst', like `defmacro', needs to be evaluated at compile
|
|
62 ;; time, so this will produce an error during byte compilation.
|
|
63 ;;
|
|
64 ;; CC Mode occasionally needs to do things like this for cross-emacs
|
|
65 ;; compatibility (although we try to avoid it since it results in
|
|
66 ;; byte code that isn't compatible between emacsen). It therefore
|
|
67 ;; uses the following to conditionally do a `defsubst':
|
|
68 ;;
|
|
69 ;; (eval-when-compile
|
|
70 ;; (if (not (fboundp 'foo))
|
|
71 ;; (defsubst foo ...)))
|
|
72 ;;
|
|
73 ;; But `eval-when-compile' byte compiles its contents and _then_
|
|
74 ;; evaluates it (in all current emacs versions, up to and including
|
|
75 ;; Emacs 20.6 and XEmacs 21.1 as of this writing). So this will
|
|
76 ;; still produce an error, since the byte compiler will get to the
|
|
77 ;; defsubst anyway. That's arguably a bug because the point with
|
|
78 ;; `eval-when-compile' is that it should evaluate rather than
|
|
79 ;; compile its contents.
|
|
80 `(eval-when-compile (eval '(progn ,@body))))
|
|
81
|
|
82 (defun cc-bytecomp-setup-environment ()
|
|
83 ;; Eval'ed during compilation to setup variables, functions etc
|
|
84 ;; declared with `cc-bytecomp-defvar' et al.
|
|
85 (if (= cc-bytecomp-load-depth 0)
|
|
86 (let (p)
|
|
87 (if cc-bytecomp-environment-set
|
|
88 (error "Byte compilation environment already set - \
|
|
89 perhaps a `cc-bytecomp-restore-environment' is forgotten somewhere"))
|
|
90 (setq p cc-bytecomp-unbound-variables)
|
|
91 (while p
|
|
92 (if (not (boundp (car p)))
|
|
93 (progn
|
|
94 (eval `(defvar ,(car p)))
|
|
95 (set (car p) 'cc-bytecomp-ignore)))
|
|
96 (setq p (cdr p)))
|
|
97 (setq p cc-bytecomp-original-functions)
|
|
98 (while p
|
|
99 (let ((fun (car (car p)))
|
|
100 (temp-macro (car (cdr (car p)))))
|
|
101 (if temp-macro
|
|
102 (eval `(defmacro ,fun ,@temp-macro))
|
|
103 (fset fun 'cc-bytecomp-ignore)))
|
|
104 (setq p (cdr p)))
|
|
105 (setq p cc-bytecomp-original-properties)
|
|
106 (while p
|
|
107 (let ((sym (car (car (car p))))
|
|
108 (prop (cdr (car (car p))))
|
|
109 (tempdef (car (cdr (car p)))))
|
|
110 (put sym prop tempdef))
|
|
111 (setq p (cdr p)))
|
|
112 (setq cc-bytecomp-environment-set t))))
|
|
113
|
|
114 (defun cc-bytecomp-restore-environment ()
|
|
115 ;; Eval'ed during compilation to restore variables, functions etc
|
|
116 ;; declared with `cc-bytecomp-defvar' et al.
|
|
117 (if (= cc-bytecomp-load-depth 0)
|
|
118 (let (p)
|
|
119 (setq p cc-bytecomp-unbound-variables)
|
|
120 (while p
|
|
121 (let ((var (car p)))
|
|
122 (if (and (boundp var)
|
|
123 (eq var 'cc-bytecomp-ignore))
|
|
124 (makunbound var)))
|
|
125 (setq p (cdr p)))
|
|
126 (setq p cc-bytecomp-original-functions)
|
|
127 (while p
|
|
128 (let ((fun (car (car p)))
|
|
129 (def (car (cdr (cdr (car p))))))
|
|
130 (if (and (fboundp fun)
|
|
131 (eq (symbol-function fun) 'cc-bytecomp-ignore))
|
|
132 (if (eq def 'unbound)
|
|
133 (fmakunbound fun)
|
|
134 (fset fun def))))
|
|
135 (setq p (cdr p)))
|
|
136 (setq p cc-bytecomp-original-properties)
|
|
137 (while p
|
|
138 (let ((sym (car (car (car p))))
|
|
139 (prop (cdr (car (car p))))
|
|
140 (tempdef (car (cdr (car p))))
|
|
141 (origdef (cdr (cdr (car p)))))
|
|
142 (if (eq (get sym prop) tempdef)
|
|
143 (put sym prop origdef)))
|
|
144 (setq p (cdr p)))
|
|
145 (setq cc-bytecomp-environment-set nil))))
|
|
146
|
|
147 (defun cc-bytecomp-load (cc-part)
|
|
148 ;; Eval'ed during compilation to load a CC Mode file from the source
|
|
149 ;; directory (assuming it's the same as the compiled file
|
|
150 ;; destination dir).
|
|
151 (if (and (boundp 'byte-compile-dest-file)
|
|
152 (stringp byte-compile-dest-file))
|
|
153 (progn
|
|
154 (cc-bytecomp-restore-environment)
|
|
155 (let ((cc-bytecomp-load-depth (1+ cc-bytecomp-load-depth))
|
|
156 (load-path
|
|
157 (cons (file-name-directory byte-compile-dest-file)
|
|
158 load-path))
|
|
159 (cc-file (concat cc-part ".el")))
|
|
160 (if (member cc-file cc-bytecomp-loaded-files)
|
|
161 ()
|
|
162 (setq cc-bytecomp-loaded-files
|
|
163 (cons cc-file cc-bytecomp-loaded-files))
|
|
164 (load cc-file nil t t)))
|
|
165 (cc-bytecomp-setup-environment)
|
|
166 t)))
|
|
167
|
|
168 (defmacro cc-require (cc-part)
|
|
169 "Force loading of the corresponding .el file in the current
|
|
170 directory during compilation, but compile in a `require'. Don't use
|
|
171 within `eval-when-compile'.
|
|
172
|
|
173 Having cyclic cc-require's will result in infinite recursion. That's
|
|
174 somewhat intentional."
|
|
175 `(progn
|
|
176 (cc-eval-when-compile (cc-bytecomp-load (symbol-name ,cc-part)))
|
|
177 (require ,cc-part)))
|
|
178
|
|
179 (defmacro cc-provide (feature)
|
|
180 "A replacement for the `provide' form that restores the environment
|
|
181 after the compilation. Don't use within `eval-when-compile'."
|
|
182 `(progn
|
|
183 (eval-when-compile (cc-bytecomp-restore-environment))
|
|
184 (provide ,feature)))
|
|
185
|
|
186 (defmacro cc-load (cc-part)
|
|
187 "Force loading of the corresponding .el file in the current
|
|
188 directory during compilation. Don't use outside `eval-when-compile'
|
|
189 or `eval-and-compile'.
|
|
190
|
|
191 Having cyclic cc-load's will result in infinite recursion. That's
|
|
192 somewhat intentional."
|
|
193 `(or (and (featurep 'cc-bytecomp)
|
|
194 (cc-bytecomp-load ,cc-part))
|
|
195 (load ,cc-part nil t nil)))
|
|
196
|
|
197 (defun cc-bytecomp-is-compiling ()
|
|
198 "Return non-nil if eval'ed during compilation. Don't use outside
|
|
199 `eval-when-compile'."
|
|
200 (and (boundp 'byte-compile-dest-file)
|
|
201 (stringp byte-compile-dest-file)))
|
|
202
|
|
203 (defmacro cc-bytecomp-defvar (var)
|
|
204 "Binds the symbol as a variable during compilation of the file,
|
|
205 to silence the byte compiler. Don't use within `eval-when-compile'."
|
|
206 `(eval-when-compile
|
|
207 (if (boundp ',var)
|
|
208 nil
|
|
209 (if (not (memq ',var cc-bytecomp-unbound-variables))
|
|
210 (setq cc-bytecomp-unbound-variables
|
|
211 (cons ',var cc-bytecomp-unbound-variables)))
|
|
212 (if (and (cc-bytecomp-is-compiling)
|
|
213 (= cc-bytecomp-load-depth 0))
|
|
214 (progn
|
|
215 (defvar ,var)
|
|
216 (set ',var 'cc-bytecomp-ignore))))))
|
|
217
|
|
218 (defmacro cc-bytecomp-defun (fun)
|
|
219 "Bind the symbol as a function during compilation of the file,
|
|
220 to silence the byte compiler. Don't use within `eval-when-compile'."
|
|
221 `(eval-when-compile
|
|
222 (if (not (assq ',fun cc-bytecomp-original-functions))
|
|
223 (setq cc-bytecomp-original-functions
|
|
224 (cons (list ',fun
|
|
225 nil
|
|
226 (if (fboundp ',fun)
|
|
227 (symbol-function ',fun)
|
|
228 'unbound))
|
|
229 cc-bytecomp-original-functions)))
|
|
230 (if (and (cc-bytecomp-is-compiling)
|
|
231 (= cc-bytecomp-load-depth 0)
|
|
232 (not (fboundp ',fun)))
|
|
233 (fset ',fun 'cc-bytecomp-ignore))))
|
|
234
|
|
235 (put 'cc-bytecomp-defmacro 'lisp-indent-function 'defun)
|
|
236 (defmacro cc-bytecomp-defmacro (fun &rest temp-macro)
|
|
237 "Bind the symbol as a macro during compilation (and evaluation) of the
|
|
238 file. Don't use outside `eval-when-compile'."
|
|
239 `(progn
|
|
240 (if (not (assq ',fun cc-bytecomp-original-functions))
|
|
241 (setq cc-bytecomp-original-functions
|
|
242 (cons (list ',fun
|
|
243 ',temp-macro
|
|
244 (if (fboundp ',fun)
|
|
245 (symbol-function ',fun)
|
|
246 'unbound))
|
|
247 cc-bytecomp-original-functions)))
|
|
248 (defmacro ,fun ,@temp-macro)))
|
|
249
|
|
250 (defmacro cc-bytecomp-put (symbol propname value)
|
|
251 "Set a property on a symbol during compilation (and evaluation) of
|
|
252 the file. Don't use outside `eval-when-compile'."
|
|
253 `(cc-eval-when-compile
|
|
254 (if (not (assoc (cons ,symbol ,propname) cc-bytecomp-original-properties))
|
|
255 (setq cc-bytecomp-original-properties
|
|
256 (cons (cons (cons ,symbol ,propname)
|
|
257 (cons ,value (get ,symbol ,propname)))
|
|
258 cc-bytecomp-original-properties)))
|
|
259 (put ,symbol ,propname ,value)))
|
|
260
|
|
261 (defmacro cc-bytecomp-obsolete-var (symbol)
|
|
262 "Suppress warnings about that the given symbol is an obsolete variable.
|
|
263 Don't use within `eval-when-compile'."
|
|
264 `(eval-when-compile
|
|
265 (if (get ',symbol 'byte-obsolete-variable)
|
|
266 (cc-bytecomp-put ',symbol 'byte-obsolete-variable nil))))
|
|
267
|
|
268 (defun cc-bytecomp-ignore-obsolete (form)
|
|
269 ;; Wraps a call to `byte-compile-obsolete' that suppresses the warning.
|
|
270 (let ((byte-compile-warnings
|
|
271 (delq 'obsolete (append byte-compile-warnings nil))))
|
|
272 (byte-compile-obsolete form)))
|
|
273
|
|
274 (defmacro cc-bytecomp-obsolete-fun (symbol)
|
|
275 "Suppress warnings about that the given symbol is an obsolete function.
|
|
276 Don't use within `eval-when-compile'."
|
|
277 `(eval-when-compile
|
|
278 (if (eq (get ',symbol 'byte-compile) 'byte-compile-obsolete)
|
|
279 (cc-bytecomp-put ',symbol 'byte-compile
|
|
280 'cc-bytecomp-ignore-obsolete))))
|
|
281
|
|
282 ;; Override ourselves with a version loaded from source if we're
|
|
283 ;; compiling, like cc-require does for all the other files.
|
|
284 (if (and (cc-bytecomp-is-compiling)
|
|
285 (= cc-bytecomp-load-depth 0))
|
|
286 (let ((load-path
|
|
287 (cons (file-name-directory byte-compile-dest-file) load-path))
|
|
288 (cc-bytecomp-load-depth 1))
|
|
289 (load "cc-bytecomp.el" nil t t)))
|
|
290
|
|
291
|
|
292 (provide 'cc-bytecomp)
|