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