summaryrefslogtreecommitdiff
path: root/lisp/progmodes/cc-defs.el
diff options
context:
space:
mode:
authorStefan Monnier <monnier@iro.umontreal.ca>2014-08-28 16:37:13 -0400
committerStefan Monnier <monnier@iro.umontreal.ca>2014-08-28 16:37:13 -0400
commit16f1e25b9c3ef1c73b198c67a41b38ca21df069b (patch)
treeb1790b4e2bcacfcf4bea7e8215a166c27a291d88 /lisp/progmodes/cc-defs.el
parent0f8fb0b98a85cf0dcf266b3963c20363fb3e1955 (diff)
downloademacs-16f1e25b9c3ef1c73b198c67a41b38ca21df069b.tar.gz
* lisp/progmodes/cc-defs.el: Expose c-lanf-defconst's expressions to the
byte-compiler. (lookup-syntax-properties): Silence byte-compiler. (c-lang-defconst): Quote the code with `lambda' rather than with `quote'. (c-lang-const): Avoid unneeded setq. (c-lang-constants-under-evaluation): Add docstring. (c-lang--novalue): New constant. (c-find-assignment-for-mode): Use it instead of c-lang-constants. (c-get-lang-constant): Same here. Get the mode's value using `funcall' now that the code is quoted with `lambda'.
Diffstat (limited to 'lisp/progmodes/cc-defs.el')
-rw-r--r--lisp/progmodes/cc-defs.el166
1 files changed, 86 insertions, 80 deletions
diff --git a/lisp/progmodes/cc-defs.el b/lisp/progmodes/cc-defs.el
index d6134a510b2..5d528caabb2 100644
--- a/lisp/progmodes/cc-defs.el
+++ b/lisp/progmodes/cc-defs.el
@@ -1556,6 +1556,8 @@ non-nil, a caret is prepended to invert the set."
(cc-bytecomp-defvar open-paren-in-column-0-is-defun-start)
+(defvar lookup-syntax-properties) ;XEmacs.
+
(defconst c-emacs-features
(let (list)
@@ -1801,18 +1803,18 @@ system."
(error "Unknown base mode `%s'" base-mode))
(put mode 'c-fallback-mode base-mode))
-(defvar c-lang-constants (make-vector 151 0))
-;; This obarray is a cache to keep track of the language constants
-;; defined by `c-lang-defconst' and the evaluated values returned by
-;; `c-lang-const'. It's mostly used at compile time but it's not
-;; stored in compiled files.
-;;
-;; The obarray contains all the language constants as symbols. The
-;; value cells hold the evaluated values as alists where each car is
-;; the mode name symbol and the corresponding cdr is the evaluated
-;; value in that mode. The property lists hold the source definitions
-;; and other miscellaneous data. The obarray might also contain
-;; various other symbols, but those don't have any variable bindings.
+(defvar c-lang-constants (make-vector 151 0)
+ "Obarray used as a cache to keep track of the language constants.
+The constants stored are those defined by `c-lang-defconst' and the values
+computed by `c-lang-const'. It's mostly used at compile time but it's not
+stored in compiled files.
+
+The obarray contains all the language constants as symbols. The
+value cells hold the evaluated values as alists where each car is
+the mode name symbol and the corresponding cdr is the evaluated
+value in that mode. The property lists hold the source definitions
+and other miscellaneous data. The obarray might also contain
+various other symbols, but those don't have any variable bindings.")
(defvar c-lang-const-expansion nil)
@@ -1897,7 +1899,7 @@ constant. A file is identified by its base name."
pre-files)
(or (symbolp name)
- (error "Not a symbol: %s" name))
+ (error "Not a symbol: %S" name))
(when (stringp (car-safe args))
;; The docstring is hardly used anywhere since there's no normal
@@ -1907,7 +1909,7 @@ constant. A file is identified by its base name."
(setq args (cdr args)))
(or args
- (error "No assignments in `c-lang-defconst' for %s" name))
+ (error "No assignments in `c-lang-defconst' for %S" name))
;; Rework ARGS to an association list to make it easier to handle.
;; It's reversed at the same time to make it easier to implement
@@ -1921,17 +1923,17 @@ constant. A file is identified by its base name."
((listp (car args))
(mapcar (lambda (lang)
(or (symbolp lang)
- (error "Not a list of symbols: %s"
+ (error "Not a list of symbols: %S"
(car args)))
(intern (concat (symbol-name lang)
"-mode")))
(car args)))
- (t (error "Not a symbol or a list of symbols: %s"
+ (t (error "Not a symbol or a list of symbols: %S"
(car args)))))
val)
(or (cdr args)
- (error "No value for %s" (car args)))
+ (error "No value for %S" (car args)))
(setq args (cdr args)
val (car args))
@@ -1945,7 +1947,7 @@ constant. A file is identified by its base name."
;; dependencies on the `c-lang-const's in VAL.)
(setq val (macroexpand-all val))
- (setq bindings (cons (cons assigned-mode val) bindings)
+ (setq bindings `(cons (cons ',assigned-mode (lambda () ,val)) ,bindings)
args (cdr args))))
;; Compile in the other files that have provided source
@@ -1957,7 +1959,7 @@ constant. A file is identified by its base name."
(mapcar 'car (get sym 'source))))
`(eval-and-compile
- (c-define-lang-constant ',name ',bindings
+ (c-define-lang-constant ',name ,bindings
,@(and pre-files `(',pre-files))))))
(put 'c-lang-defconst 'lisp-indent-function 1)
@@ -2022,19 +2024,16 @@ language. NAME and LANG are not evaluated so they should not be
quoted."
(or (symbolp name)
- (error "Not a symbol: %s" name))
+ (error "Not a symbol: %S" name))
(or (symbolp lang)
- (error "Not a symbol: %s" lang))
+ (error "Not a symbol: %S" lang))
(let ((sym (intern (symbol-name name) c-lang-constants))
- mode source-files args)
+ (mode (when lang (intern (concat (symbol-name lang) "-mode")))))
- (when lang
- (setq mode (intern (concat (symbol-name lang) "-mode")))
- (unless (get mode 'c-mode-prefix)
- (error
- "Unknown language %S since it got no `c-mode-prefix' property"
- (symbol-name lang))))
+ (or (get mode 'c-mode-prefix) (null mode)
+ (error "Unknown language %S: no `c-mode-prefix' property"
+ lang))
(if (eq c-lang-const-expansion 'immediate)
;; No need to find out the source file(s) when we evaluate
@@ -2042,49 +2041,56 @@ quoted."
;; `source' property.
`',(c-get-lang-constant name nil mode)
- (let ((file (c-get-current-file)))
- (if file (setq file (intern file)))
- ;; Get the source file(s) that must be loaded to get the value
- ;; of the constant. If the symbol isn't defined yet we assume
- ;; that its definition will come later in this file, and thus
- ;; are no file dependencies needed.
- (setq source-files (nreverse
- ;; Reverse to get the right load order.
- (apply 'nconc
- (mapcar (lambda (elem)
- (if (eq file (car elem))
- nil ; Exclude our own file.
- (list (car elem))))
- (get sym 'source))))))
-
- ;; Make some effort to do a compact call to
- ;; `c-get-lang-constant' since it will be compiled in.
- (setq args (and mode `(',mode)))
- (if (or source-files args)
- (setq args (cons (and source-files `',source-files)
- args)))
-
- (if (or (eq c-lang-const-expansion 'call)
- (and (not c-lang-const-expansion)
- (not mode))
- load-in-progress
- (not (boundp 'byte-compile-dest-file))
- (not (stringp byte-compile-dest-file)))
- ;; Either a straight call is requested in the context, or
- ;; we're in an "uncontrolled" context and got no language,
- ;; or we're not being byte compiled so the compile time
- ;; stuff below is unnecessary.
- `(c-get-lang-constant ',name ,@args)
-
- ;; Being compiled. If the loading and compiling version is
- ;; the same we use a value that is evaluated at compile time,
- ;; otherwise it's evaluated at runtime.
- `(if (eq c-version-sym ',c-version-sym)
- (cc-eval-when-compile
- (c-get-lang-constant ',name ,@args))
- (c-get-lang-constant ',name ,@args))))))
-
-(defvar c-lang-constants-under-evaluation nil)
+ (let ((source-files
+ (let ((file (c-get-current-file)))
+ (if file (setq file (intern file)))
+ ;; Get the source file(s) that must be loaded to get the value
+ ;; of the constant. If the symbol isn't defined yet we assume
+ ;; that its definition will come later in this file, and thus
+ ;; are no file dependencies needed.
+ (nreverse
+ ;; Reverse to get the right load order.
+ (apply 'nconc
+ (mapcar (lambda (elem)
+ (if (eq file (car elem))
+ nil ; Exclude our own file.
+ (list (car elem))))
+ (get sym 'source))))))
+ ;; Make some effort to do a compact call to
+ ;; `c-get-lang-constant' since it will be compiled in.
+ (args (and mode `(',mode))))
+
+ (if (or source-files args)
+ (push (and source-files `',source-files) args))
+
+ (if (or (eq c-lang-const-expansion 'call)
+ (and (not c-lang-const-expansion)
+ (not mode))
+ load-in-progress
+ (not (boundp 'byte-compile-dest-file))
+ (not (stringp byte-compile-dest-file)))
+ ;; Either a straight call is requested in the context, or
+ ;; we're in an "uncontrolled" context and got no language,
+ ;; or we're not being byte compiled so the compile time
+ ;; stuff below is unnecessary.
+ `(c-get-lang-constant ',name ,@args)
+
+ ;; Being compiled. If the loading and compiling version is
+ ;; the same we use a value that is evaluated at compile time,
+ ;; otherwise it's evaluated at runtime.
+ `(if (eq c-version-sym ',c-version-sym)
+ (cc-eval-when-compile
+ (c-get-lang-constant ',name ,@args))
+ (c-get-lang-constant ',name ,@args)))))))
+
+(defvar c-lang-constants-under-evaluation nil
+ "Alist of constants in the process of being evaluated.
+The `cdr' of each entry indicates how far we've looked in the list
+of definitions, so that the def for var FOO in c-mode can be defined in
+terms of the def for that same var FOO (which will then rely on the
+fallback definition for all modes, to break the cycle).")
+
+(defconst c-lang--novalue "novalue")
(defun c-get-lang-constant (name &optional source-files mode)
;; Used by `c-lang-const'.
@@ -2150,7 +2156,7 @@ quoted."
;; mode might have an explicit entry before that.
(eq (setq value (c-find-assignment-for-mode
(cdr source-pos) mode nil name))
- c-lang-constants)
+ c-lang--novalue)
;; Try again with the fallback mode from the
;; original position. Note that
;; `c-buffer-is-cc-mode' still is the real mode if
@@ -2158,22 +2164,22 @@ quoted."
(eq (setq value (c-find-assignment-for-mode
(setcdr source-pos backup-source-pos)
fallback t name))
- c-lang-constants)))
+ c-lang--novalue)))
;; A simple lookup with no fallback mode.
(eq (setq value (c-find-assignment-for-mode
(cdr source-pos) mode t name))
- c-lang-constants))
+ c-lang--novalue))
(error
- "`%s' got no (prior) value in %s (might be a cyclic reference)"
+ "`%s' got no (prior) value in %S (might be a cyclic reference)"
name mode))
(condition-case err
- (setq value (eval value))
+ (setq value (funcall value))
(error
;; Print a message to aid in locating the error. We don't
;; print the error itself since that will be done later by
;; some caller higher up.
- (message "Eval error in the `c-lang-defconst' for `%s' in %s:"
+ (message "Eval error in the `c-lang-defconst' for `%S' in %s:"
sym mode)
(makunbound sym)
(signal (car err) (cdr err))))
@@ -2181,13 +2187,13 @@ quoted."
(set sym (cons (cons mode value) (symbol-value sym)))
value))))
-(defun c-find-assignment-for-mode (source-pos mode match-any-lang name)
+(defun c-find-assignment-for-mode (source-pos mode match-any-lang _name)
;; Find the first assignment entry that applies to MODE at or after
;; SOURCE-POS. If MATCH-ANY-LANG is non-nil, entries with `t' as
;; the language list are considered to match, otherwise they don't.
;; On return SOURCE-POS is updated to point to the next assignment
;; after the returned one. If no assignment is found,
- ;; `c-lang-constants' is returned as a magic value.
+ ;; `c-lang--novalue' is returned as a magic value.
;;
;; SOURCE-POS is a vector that points out a specific assignment in
;; the double alist that's used in the `source' property. The first
@@ -2243,7 +2249,7 @@ quoted."
match-any-lang)
(throw 'found (cdr assignment))))
- c-lang-constants)))
+ c-lang--novalue)))
(defun c-lang-major-mode-is (mode)
;; `c-major-mode-is' expands to a call to this function inside