Make vc-toggle-read-only an alias for toggle-read-only.
[bpt/emacs.git] / lisp / progmodes / cc-engine.el
Content-type: text/html HCoop Git - bpt/emacs.git/blame - lisp/progmodes/cc-engine.el


500 - Internal Server Error

Malformed UTF-8 character (fatal) at (eval 8) line 1, <$fd> line 17134.
CommitLineData
785eecbb
RS
1;;; cc-engine.el --- core syntax guessing engine for CC mode
2
73b0cd50 3;; Copyright (C) 1985, 1987, 1992-2011 Free Software Foundation, Inc.
785eecbb 4
e309f66c
AM
5;; Authors: 2001- Alan Mackenzie
6;; 1998- Martin Stjernholm
d9e94c22 7;; 1992-1999 Barry A. Warsaw
5858f68c
GM
8;; 1987 Dave Detlefs
9;; 1987 Stewart Clamen
785eecbb 10;; 1985 Richard M. Stallman
0ec8351b 11;; Maintainer: bug-cc-mode@gnu.org
785eecbb 12;; Created: 22-Apr-1997 (split from cc-mode.el)
bd78fa1d
CY
13;; Keywords: c languages
14;; Package: cc-mode
785eecbb
RS
15
16;; This file is part of GNU Emacs.
17
b1fc2b50 18;; GNU Emacs is free software: you can redistribute it and/or modify
785eecbb 19;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
20;; the Free Software Foundation, either version 3 of the License, or
21;; (at your option) any later version.
785eecbb
RS
22
23;; GNU Emacs is distributed in the hope that it will be useful,
24;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26;; GNU General Public License for more details.
27
28;; You should have received a copy of the GNU General Public License
b1fc2b50 29;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
785eecbb 30
3afbc435
PJ
31;;; Commentary:
32
a66cd3ee
MS
33;; The functions which have docstring documentation can be considered
34;; part of an API which other packages can use in CC Mode buffers.
35;; Otoh, undocumented functions and functions with the documentation
36;; in comments are considered purely internal and can change semantics
37;; or even disappear in the future.
38;;
39;; (This policy applies to CC Mode as a whole, not just this file. It
40;; probably also applies to many other Emacs packages, but here it's
41;; clearly spelled out.)
42
d9e94c22
MS
43;; Hidden buffer changes
44;;
45;; Various functions in CC Mode use text properties for caching and
46;; syntactic markup purposes, and those of them that might modify such
0386b551
AM
47;; properties but still don't modify the buffer in a visible way are
48;; said to do "hidden buffer changes". They should be used within
49;; `c-save-buffer-state' or a similar function that saves and restores
50;; buffer modifiedness, disables buffer change hooks, etc.
d9e94c22 51;;
0386b551
AM
52;; Interactive functions are assumed to not do hidden buffer changes,
53;; except in the specific parts of them that do real changes.
d9e94c22 54;;
0386b551
AM
55;; Lineup functions are assumed to do hidden buffer changes. They
56;; must not do real changes, though.
d9e94c22 57;;
0386b551
AM
58;; All other functions that do hidden buffer changes have that noted
59;; in their doc string or comment.
60;;
61;; The intention with this system is to avoid wrapping every leaf
62;; function that do hidden buffer changes inside
63;; `c-save-buffer-state'. It should be used as near the top of the
64;; interactive functions as possible.
65;;
66;; Functions called during font locking are allowed to do hidden
67;; buffer changes since the font-lock package run them in a context
68;; similar to `c-save-buffer-state' (in fact, that function is heavily
69;; inspired by `save-buffer-state' in the font-lock package).
d9e94c22
MS
70
71;; Use of text properties
72;;
73;; CC Mode uses several text properties internally to mark up various
74;; positions, e.g. to improve speed and to eliminate glitches in
75;; interactive refontification.
76;;
7bfc3fdb
MS
77;; Note: This doc is for internal use only. Other packages should not
78;; assume that these text properties are used as described here.
79;;
0ec1d2c5
AM
80;; 'category
81;; Used for "indirection". With its help, some other property can
82;; be cheaply and easily switched on or off everywhere it occurs.
83;;
d9e94c22 84;; 'syntax-table
c8018ede
AM
85;; Used to modify the syntax of some characters. It is used to
86;; mark the "<" and ">" of angle bracket parens with paren syntax, and
87;; to "hide" obtrusive characters in preprocessor lines.
d9e94c22
MS
88;;
89;; This property is used on single characters and is therefore
90;; always treated as front and rear nonsticky (or start and end open
91;; in XEmacs vocabulary). It's therefore installed on
92;; `text-property-default-nonsticky' if that variable exists (Emacs
93;; >= 21).
94;;
95;; 'c-is-sws and 'c-in-sws
96;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
97;; speed them up. See the comment blurb before `c-put-is-sws'
98;; below for further details.
99;;
100;; 'c-type
101;; This property is used on single characters to mark positions with
0386b551
AM
102;; special syntactic relevance of various sorts. Its primary use is
103;; to avoid glitches when multiline constructs are refontified
d9e94c22
MS
104;; interactively (on font lock decoration level 3). It's cleared in
105;; a region before it's fontified and is then put on relevant chars
106;; in that region as they are encountered during the fontification.
107;; The value specifies the kind of position:
108;;
109;; 'c-decl-arg-start
110;; Put on the last char of the token preceding each declaration
111;; inside a declaration style arglist (typically in a function
112;; prototype).
113;;
114;; 'c-decl-end
115;; Put on the last char of the token preceding a declaration.
116;; This is used in cases where declaration boundaries can't be
117;; recognized simply by looking for a token like ";" or "}".
118;; `c-type-decl-end-used' must be set if this is used (see also
119;; `c-find-decl-spots').
120;;
121;; 'c-<>-arg-sep
122;; Put on the commas that separate arguments in angle bracket
123;; arglists like C++ template arglists.
124;;
125;; 'c-decl-id-start and 'c-decl-type-start
126;; Put on the last char of the token preceding each declarator
127;; in the declarator list of a declaration. They are also used
128;; between the identifiers cases like enum declarations.
129;; 'c-decl-type-start is used when the declarators are types,
130;; 'c-decl-id-start otherwise.
131;;
132;; 'c-awk-NL-prop
133;; Used in AWK mode to mark the various kinds of newlines. See
134;; cc-awk.el.
135
3afbc435
PJ
136;;; Code:
137
0ec8351b 138(eval-when-compile
51f606de 139 (let ((load-path
130c507e
GM
140 (if (and (boundp 'byte-compile-dest-file)
141 (stringp byte-compile-dest-file))
142 (cons (file-name-directory byte-compile-dest-file) load-path)
51f606de 143 load-path)))
d9e94c22 144 (load "cc-bytecomp" nil t)))
130c507e
GM
145
146(cc-require 'cc-defs)
d9e94c22 147(cc-require-when-compile 'cc-langs)
130c507e 148(cc-require 'cc-vars)
d9e94c22 149
130c507e
GM
150;; Silence the compiler.
151(cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
0ec8351b 152
51f606de 153\f
d9e94c22
MS
154;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
155
156(defmacro c-declare-lang-variables ()
157 `(progn
485fe977
RS
158 ,@(apply 'nconc
159 (mapcar (lambda (init)
160 `(,(if (elt init 2)
161 `(defvar ,(car init) nil ,(elt init 2))
162 `(defvar ,(car init) nil))
163 (make-variable-buffer-local ',(car init))))
164 (cdr c-lang-variable-inits)))))
d9e94c22
MS
165(c-declare-lang-variables)
166
167\f
168;;; Internal state variables.
169
170;; Internal state of hungry delete key feature
171(defvar c-hungry-delete-key nil)
172(make-variable-buffer-local 'c-hungry-delete-key)
173
0386b551
AM
174;; The electric flag (toggled by `c-toggle-electric-state').
175;; If t, electric actions (like automatic reindentation, and (if
176;; c-auto-newline is also set) auto newlining) will happen when an electric
177;; key like `{' is pressed (or an electric keyword like `else').
178(defvar c-electric-flag t)
179(make-variable-buffer-local 'c-electric-flag)
180
d9e94c22
MS
181;; Internal state of auto newline feature.
182(defvar c-auto-newline nil)
183(make-variable-buffer-local 'c-auto-newline)
184
0386b551 185;; Included in the mode line to indicate the active submodes.
cb694ab7
AM
186;; (defvar c-submode-indicators nil)
187;; (make-variable-buffer-local 'c-submode-indicators)
d9e94c22 188
a66cd3ee
MS
189(defun c-calculate-state (arg prevstate)
190 ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
191 ;; arg is nil or zero, toggle the state. If arg is negative, turn
192 ;; the state off, and if arg is positive, turn the state on
193 (if (or (not arg)
194 (zerop (setq arg (prefix-numeric-value arg))))
195 (not prevstate)
196 (> arg 0)))
197
d9e94c22 198;; Dynamically bound cache for `c-in-literal'.
130c507e 199(defvar c-in-literal-cache t)
d9e94c22 200
d9e94c22 201\f
037558bf
MS
202;; Basic handling of preprocessor directives.
203
204;; This is a dynamically bound cache used together with
205;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
206;; works as long as point doesn't cross a macro boundary.
207(defvar c-macro-start 'unknown)
208
209(defsubst c-query-and-set-macro-start ()
037558bf
MS
210 (if (symbolp c-macro-start)
211 (setq c-macro-start (save-excursion
0386b551
AM
212 (c-save-buffer-state ()
213 (and (c-beginning-of-macro)
214 (point)))))
037558bf
MS
215 c-macro-start))
216
217(defsubst c-query-macro-start ()
037558bf
MS
218 (if (symbolp c-macro-start)
219 (save-excursion
0386b551
AM
220 (c-save-buffer-state ()
221 (and (c-beginning-of-macro)
222 (point))))
037558bf
MS
223 c-macro-start))
224
225(defun c-beginning-of-macro (&optional lim)
226 "Go to the beginning of a preprocessor directive.
227Leave point at the beginning of the directive and return t if in one,
228otherwise return nil and leave point unchanged.
229
0386b551
AM
230Note that this function might do hidden buffer changes. See the
231comment at the start of cc-engine.el for more info."
037558bf
MS
232 (when c-opt-cpp-prefix
233 (let ((here (point)))
234 (save-restriction
235 (if lim (narrow-to-region lim (point-max)))
236 (beginning-of-line)
237 (while (eq (char-before (1- (point))) ?\\)
238 (forward-line -1))
239 (back-to-indentation)
240 (if (and (<= (point) here)
241 (looking-at c-opt-cpp-start))
242 t
243 (goto-char here)
244 nil)))))
245
246(defun c-end-of-macro ()
247 "Go to the end of a preprocessor directive.
0386b551
AM
248More accurately, move the point to the end of the closest following
249line that doesn't end with a line continuation backslash - no check is
250done that the point is inside a cpp directive to begin with.
037558bf 251
0386b551
AM
252Note that this function might do hidden buffer changes. See the
253comment at the start of cc-engine.el for more info."
037558bf
MS
254 (while (progn
255 (end-of-line)
256 (when (and (eq (char-before) ?\\)
257 (not (eobp)))
258 (forward-char)
259 t))))
260
0ec1d2c5
AM
261(defun c-syntactic-end-of-macro ()
262 ;; Go to the end of a CPP directive, or a "safe" pos just before.
263 ;;
264 ;; This is normally the end of the next non-escaped line. A "safe"
265 ;; position is one not within a string or comment. (The EOL on a line
266 ;; comment is NOT "safe").
267 ;;
268 ;; This function must only be called from the beginning of a CPP construct.
269 ;;
270 ;; Note that this function might do hidden buffer changes. See the comment
271 ;; at the start of cc-engine.el for more info.
272 (let* ((here (point))
273 (there (progn (c-end-of-macro) (point)))
274 (s (parse-partial-sexp here there)))
275 (while (and (or (nth 3 s) ; in a string
276 (nth 4 s)) ; in a comment (maybe at end of line comment)
277 (> there here)) ; No infinite loops, please.
278 (setq there (1- (nth 8 s)))
279 (setq s (parse-partial-sexp here there)))
280 (point)))
281
51c9af45
AM
282(defun c-forward-over-cpp-define-id ()
283 ;; Assuming point is at the "#" that introduces a preprocessor
284 ;; directive, it's moved forward to the end of the identifier which is
285 ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
286 ;; is returned in this case, in all other cases nil is returned and
287 ;; point isn't moved.
288 ;;
289 ;; This function might do hidden buffer changes.
290 (when (and c-opt-cpp-macro-define-id
291 (looking-at c-opt-cpp-macro-define-id))
292 (goto-char (match-end 0))))
293
037558bf
MS
294(defun c-forward-to-cpp-define-body ()
295 ;; Assuming point is at the "#" that introduces a preprocessor
296 ;; directive, it's moved forward to the start of the definition body
0386b551
AM
297 ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
298 ;; specifies). Non-nil is returned in this case, in all other cases
299 ;; nil is returned and point isn't moved.
300 ;;
301 ;; This function might do hidden buffer changes.
302 (when (and c-opt-cpp-macro-define-start
303 (looking-at c-opt-cpp-macro-define-start)
037558bf
MS
304 (not (= (match-end 0) (c-point 'eol))))
305 (goto-char (match-end 0))))
306
307\f
d9e94c22
MS
308;;; Basic utility functions.
309
0386b551 310(defun c-syntactic-content (from to paren-level)
d9e94c22
MS
311 ;; Return the given region as a string where all syntactic
312 ;; whitespace is removed or, where necessary, replaced with a single
0386b551
AM
313 ;; space. If PAREN-LEVEL is given then all parens in the region are
314 ;; collapsed to "()", "[]" etc.
315 ;;
316 ;; This function might do hidden buffer changes.
317
d9e94c22 318 (save-excursion
0386b551
AM
319 (save-restriction
320 (narrow-to-region from to)
321 (goto-char from)
322 (let* ((parts (list nil)) (tail parts) pos in-paren)
323
324 (while (re-search-forward c-syntactic-ws-start to t)
325 (goto-char (setq pos (match-beginning 0)))
326 (c-forward-syntactic-ws)
327 (if (= (point) pos)
328 (forward-char)
329
330 (when paren-level
331 (save-excursion
332 (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
333 pos (point))))
334
335 (if (and (> pos from)
336 (< (point) to)
337 (looking-at "\\w\\|\\s_")
338 (save-excursion
339 (goto-char (1- pos))
340 (looking-at "\\w\\|\\s_")))
341 (progn
342 (setcdr tail (list (buffer-substring-no-properties from pos)
343 " "))
344 (setq tail (cddr tail)))
345 (setcdr tail (list (buffer-substring-no-properties from pos)))
346 (setq tail (cdr tail)))
347
348 (when in-paren
349 (when (= (car (parse-partial-sexp pos to -1)) -1)
350 (setcdr tail (list (buffer-substring-no-properties
351 (1- (point)) (point))))
352 (setq tail (cdr tail))))
353
354 (setq from (point))))
355
356 (setcdr tail (list (buffer-substring-no-properties from to)))
357 (apply 'concat (cdr parts))))))
358
359(defun c-shift-line-indentation (shift-amt)
360 ;; Shift the indentation of the current line with the specified
361 ;; amount (positive inwards). The buffer is modified only if
362 ;; SHIFT-AMT isn't equal to zero.
363 (let ((pos (- (point-max) (point)))
364 (c-macro-start c-macro-start)
365 tmp-char-inserted)
366 (if (zerop shift-amt)
367 nil
368 ;; If we're on an empty line inside a macro, we take the point
369 ;; to be at the current indentation and shift it to the
370 ;; appropriate column. This way we don't treat the extra
371 ;; whitespace out to the line continuation as indentation.
372 (when (and (c-query-and-set-macro-start)
373 (looking-at "[ \t]*\\\\$")
374 (save-excursion
375 (skip-chars-backward " \t")
376 (bolp)))
377 (insert ?x)
378 (backward-char)
379 (setq tmp-char-inserted t))
380 (unwind-protect
381 (let ((col (current-indentation)))
382 (delete-region (c-point 'bol) (c-point 'boi))
383 (beginning-of-line)
384 (indent-to (+ col shift-amt)))
385 (when tmp-char-inserted
386 (delete-char 1))))
387 ;; If initial point was within line's indentation and we're not on
388 ;; a line with a line continuation in a macro, position after the
389 ;; indentation. Else stay at same point in text.
390 (if (and (< (point) (c-point 'boi))
391 (not tmp-char-inserted))
392 (back-to-indentation)
393 (if (> (- (point-max) pos) (point))
394 (goto-char (- (point-max) pos))))))
d9e94c22
MS
395
396(defsubst c-keyword-sym (keyword)
397 ;; Return non-nil if the string KEYWORD is a known keyword. More
398 ;; precisely, the value is the symbol for the keyword in
399 ;; `c-keywords-obarray'.
400 (intern-soft keyword c-keywords-obarray))
401
402(defsubst c-keyword-member (keyword-sym lang-constant)
403 ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
404 ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
405 ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
406 ;; nil then the result is nil.
407 (get keyword-sym lang-constant))
408
409;; String syntax chars, suitable for skip-syntax-(forward|backward).
410(defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
411 "\"|"
412 "\""))
413
0386b551 414;; Regexp matching string limit syntax.
d9e94c22
MS
415(defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
416 "\\s\"\\|\\s|"
417 "\\s\""))
418
0386b551
AM
419;; Regexp matching WS followed by string limit syntax.
420(defconst c-ws*-string-limit-regexp
421 (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
422
d9e94c22
MS
423;; Holds formatted error strings for the few cases where parse errors
424;; are reported.
a66cd3ee 425(defvar c-parsing-error nil)
d9e94c22
MS
426(make-variable-buffer-local 'c-parsing-error)
427
428(defun c-echo-parsing-error (&optional quiet)
d9e94c22
MS
429 (when (and c-report-syntactic-errors c-parsing-error (not quiet))
430 (c-benign-error "%s" c-parsing-error))
431 c-parsing-error)
432
433;; Faces given to comments and string literals. This is used in some
434;; situations to speed up recognition; it isn't mandatory that font
435;; locking is in use. This variable is extended with the face in
436;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
1e330469 437(defvar c-literal-faces
0386b551
AM
438 (append '(font-lock-comment-face font-lock-string-face)
439 (when (facep 'font-lock-comment-delimiter-face)
440 ;; New in Emacs 22.
441 '(font-lock-comment-delimiter-face))))
442
443(defsubst c-put-c-type-property (pos value)
444 ;; Put a c-type property with the given value at POS.
445 (c-put-char-property pos 'c-type value))
446
447(defun c-clear-c-type-property (from to value)
5a89f0a7 448 ;; Remove all occurrences of the c-type property that has the given
0386b551
AM
449 ;; value in the region between FROM and TO. VALUE is assumed to not
450 ;; be nil.
451 ;;
452 ;; Note: This assumes that c-type is put on single chars only; it's
453 ;; very inefficient if matching properties cover large regions.
454 (save-excursion
455 (goto-char from)
456 (while (progn
457 (when (eq (get-text-property (point) 'c-type) value)
458 (c-clear-char-property (point) 'c-type))
459 (goto-char (next-single-property-change (point) 'c-type nil to))
460 (< (point) to)))))
037558bf 461
d9e94c22
MS
462\f
463;; Some debug tools to visualize various special positions. This
464;; debug code isn't as portable as the rest of CC Mode.
465
466(cc-bytecomp-defun overlays-in)
467(cc-bytecomp-defun overlay-get)
468(cc-bytecomp-defun overlay-start)
469(cc-bytecomp-defun overlay-end)
470(cc-bytecomp-defun delete-overlay)
471(cc-bytecomp-defun overlay-put)
472(cc-bytecomp-defun make-overlay)
473
474(defun c-debug-add-face (beg end face)
475 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
476 (while overlays
477 (setq overlay (car overlays)
478 overlays (cdr overlays))
479 (when (eq (overlay-get overlay 'face) face)
480 (setq beg (min beg (overlay-start overlay))
481 end (max end (overlay-end overlay)))
482 (delete-overlay overlay)))
483 (overlay-put (make-overlay beg end) 'face face)))
484
485(defun c-debug-remove-face (beg end face)
486 (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
487 (ol-beg beg) (ol-end end))
488 (while overlays
489 (setq overlay (car overlays)
490 overlays (cdr overlays))
491 (when (eq (overlay-get overlay 'face) face)
492 (setq ol-beg (min ol-beg (overlay-start overlay))
493 ol-end (max ol-end (overlay-end overlay)))
494 (delete-overlay overlay)))
495 (when (< ol-beg beg)
496 (overlay-put (make-overlay ol-beg beg) 'face face))
497 (when (> ol-end end)
498 (overlay-put (make-overlay end ol-end) 'face face))))
499
500\f
501;; `c-beginning-of-statement-1' and accompanying stuff.
130c507e 502
64001211
RS
503;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
504;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
505;; better way should be implemented, but this will at least shut up
506;; the byte compiler.
0386b551 507(defvar c-maybe-labelp)
64001211 508
d9e94c22
MS
509;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
510
a66cd3ee
MS
511;; Macros used internally in c-beginning-of-statement-1 for the
512;; automaton actions.
513(defmacro c-bos-push-state ()
514 '(setq stack (cons (cons state saved-pos)
515 stack)))
516(defmacro c-bos-pop-state (&optional do-if-done)
517 `(if (setq state (car (car stack))
518 saved-pos (cdr (car stack))
519 stack (cdr stack))
520 t
521 ,do-if-done
522 (throw 'loop nil)))
523(defmacro c-bos-pop-state-and-retry ()
524 '(throw 'loop (setq state (car (car stack))
525 saved-pos (cdr (car stack))
526 ;; Throw nil if stack is empty, else throw non-nil.
527 stack (cdr stack))))
528(defmacro c-bos-save-pos ()
529 '(setq saved-pos (vector pos tok ptok pptok)))
530(defmacro c-bos-restore-pos ()
531 '(unless (eq (elt saved-pos 0) start)
532 (setq pos (elt saved-pos 0)
533 tok (elt saved-pos 1)
534 ptok (elt saved-pos 2)
535 pptok (elt saved-pos 3))
536 (goto-char pos)
537 (setq sym nil)))
538(defmacro c-bos-save-error-info (missing got)
539 `(setq saved-pos (vector pos ,missing ,got)))
540(defmacro c-bos-report-error ()
541 '(unless noerror
542 (setq c-parsing-error
543 (format "No matching `%s' found for `%s' on line %d"
544 (elt saved-pos 1)
545 (elt saved-pos 2)
546 (1+ (count-lines (point-min)
547 (c-point 'bol (elt saved-pos 0))))))))
548
549(defun c-beginning-of-statement-1 (&optional lim ignore-labels
550 noerror comma-delim)
551 "Move to the start of the current statement or declaration, or to
552the previous one if already at the beginning of one. Only
553statements/declarations on the same level are considered, i.e. don't
554move into or out of sexps (not even normal expression parentheses).
555
5a89f0a7 556If point is already at the earliest statement within braces or parens,
a85fd6da
AM
557this function doesn't move back into any whitespace preceding it; it
558returns 'same in this case.
559
d9e94c22
MS
560Stop at statement continuation tokens like \"else\", \"catch\",
561\"finally\" and the \"while\" in \"do ... while\" if the start point
562is within the continuation. If starting at such a token, move to the
563corresponding statement start. If at the beginning of a statement,
564move to the closest containing statement if there is any. This might
565also stop at a continuation clause.
a66cd3ee 566
0386b551
AM
567Labels are treated as part of the following statements if
568IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
cb694ab7
AM
569statement start keyword.) Otherwise, each label is treated as a
570separate statement.
a66cd3ee 571
cb694ab7
AM
572Macros are ignored \(i.e. skipped over) unless point is within one, in
573which case the content of the macro is treated as normal code. Aside
574from any normal statement starts found in it, stop at the first token
575of the content in the macro, i.e. the expression of an \"#if\" or the
576start of the definition in a \"#define\". Also stop at start of
577macros before leaving them.
a66cd3ee 578
a85fd6da 579Return:
d28e7f28 580'label if stopped at a label or \"case...:\" or \"default:\";
a85fd6da
AM
581'same if stopped at the beginning of the current statement;
582'up if stepped to a containing statement;
583'previous if stepped to a preceding statement;
584'beginning if stepped from a statement continuation clause to
585 its start clause; or
586'macro if stepped to a macro start.
587Note that 'same and not 'label is returned if stopped at the same
588label without crossing the colon character.
a66cd3ee
MS
589
590LIM may be given to limit the search. If the search hits the limit,
591point will be left at the closest following token, or at the start
592position if that is less ('same is returned in this case).
593
594NOERROR turns off error logging to `c-parsing-error'.
595
cb694ab7
AM
596Normally only ';' and virtual semicolons are considered to delimit
597statements, but if COMMA-DELIM is non-nil then ',' is treated
598as a delimiter too.
0386b551
AM
599
600Note that this function might do hidden buffer changes. See the
601comment at the start of cc-engine.el for more info."
a66cd3ee 602
d9e94c22
MS
603 ;; The bulk of this function is a pushdown automaton that looks at statement
604 ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
605 ;; purpose is to keep track of nested statements, ensuring that such
5a89f0a7 606 ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
d9e94c22 607 ;; does with nested braces/brackets/parentheses).
a66cd3ee
MS
608 ;;
609 ;; Note: The position of a boundary is the following token.
610 ;;
d9e94c22
MS
611 ;; Beginning with the current token (the one following point), move back one
612 ;; sexp at a time (where a sexp is, more or less, either a token or the
613 ;; entire contents of a brace/bracket/paren pair). Each time a statement
614 ;; boundary is crossed or a "while"-like token is found, update the state of
615 ;; the PDA. Stop at the beginning of a statement when the stack (holding
616 ;; nested statement info) is empty and the position has been moved.
617 ;;
618 ;; The following variables constitute the PDA:
619 ;;
620 ;; sym: This is either the "while"-like token (e.g. 'for) we've just
621 ;; scanned back over, 'boundary if we've just gone back over a
622 ;; statement boundary, or nil otherwise.
623 ;; state: takes one of the values (nil else else-boundary while
624 ;; while-boundary catch catch-boundary).
625 ;; nil means "no "while"-like token yet scanned".
626 ;; 'else, for example, means "just gone back over an else".
627 ;; 'else-boundary means "just gone back over a statement boundary
628 ;; immediately after having gone back over an else".
629 ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
630 ;; of error reporting information.
631 ;; stack: The stack onto which the PDA pushes its state. Each entry
632 ;; consists of a saved value of state and saved-pos. An entry is
633 ;; pushed when we move back over a "continuation" token (e.g. else)
634 ;; and popped when we encounter the corresponding opening token
635 ;; (e.g. if).
636 ;;
637 ;;
b414f371 638 ;; The following diagram briefly outlines the PDA.
a66cd3ee
MS
639 ;;
640 ;; Common state:
d9e94c22
MS
641 ;; "else": Push state, goto state `else'.
642 ;; "while": Push state, goto state `while'.
643 ;; "catch" or "finally": Push state, goto state `catch'.
644 ;; boundary: Pop state.
a66cd3ee
MS
645 ;; other: Do nothing special.
646 ;;
d9e94c22
MS
647 ;; State `else':
648 ;; boundary: Goto state `else-boundary'.
649 ;; other: Error, pop state, retry token.
650 ;;
651 ;; State `else-boundary':
652 ;; "if": Pop state.
653 ;; boundary: Error, pop state.
654 ;; other: See common state.
655 ;;
656 ;; State `while':
657 ;; boundary: Save position, goto state `while-boundary'.
658 ;; other: Pop state, retry token.
659 ;;
660 ;; State `while-boundary':
661 ;; "do": Pop state.
662 ;; boundary: Restore position if it's not at start, pop state. [*see below]
663 ;; other: See common state.
664 ;;
665 ;; State `catch':
666 ;; boundary: Goto state `catch-boundary'.
667 ;; other: Error, pop state, retry token.
668 ;;
669 ;; State `catch-boundary':
670 ;; "try": Pop state.
671 ;; "catch": Goto state `catch'.
672 ;; boundary: Error, pop state.
673 ;; other: See common state.
674 ;;
675 ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
676 ;; searching for a "do" which would have opened a do-while. If we didn't
677 ;; find it, we discard the analysis done since the "while", go back to this
678 ;; token in the buffer and restart the scanning there, this time WITHOUT
679 ;; pushing the 'while state onto the stack.
680 ;;
a66cd3ee
MS
681 ;; In addition to the above there is some special handling of labels
682 ;; and macros.
683
684 (let ((case-fold-search nil)
685 (start (point))
686 macro-start
687 (delims (if comma-delim '(?\; ?,) '(?\;)))
688 (c-stmt-delim-chars (if comma-delim
689 c-stmt-delim-chars-with-comma
690 c-stmt-delim-chars))
d28e7f28 691 c-in-literal-cache c-maybe-labelp after-case:-pos saved
0386b551
AM
692 ;; Current position.
693 pos
694 ;; Position of last stmt boundary character (e.g. ;).
695 boundary-pos
696 ;; The position of the last sexp or bound that follows the
697 ;; first found colon, i.e. the start of the nonlabel part of
698 ;; the statement. It's `start' if a colon is found just after
699 ;; the start.
700 after-labels-pos
701 ;; Like `after-labels-pos', but the first such position inside
702 ;; a label, i.e. the start of the last label before the start
703 ;; of the nonlabel part of the statement.
704 last-label-pos
705 ;; The last position where a label is possible provided the
706 ;; statement started there. It's nil as long as no invalid
707 ;; label content has been found (according to
708 ;; `c-nonlabel-token-key'. It's `start' if no valid label
709 ;; content was found in the label. Note that we might still
710 ;; regard it a label if it starts with `c-label-kwds'.
711 label-good-pos
b248a85d
AM
712 ;; Putative positions of the components of a bitfield declaration,
713 ;; e.g. "int foo : NUM_FOO_BITS ;"
714 bitfield-type-pos bitfield-id-pos bitfield-size-pos
0386b551
AM
715 ;; Symbol just scanned back over (e.g. 'while or 'boundary).
716 ;; See above.
717 sym
718 ;; Current state in the automaton. See above.
719 state
720 ;; Current saved positions. See above.
721 saved-pos
722 ;; Stack of conses (state . saved-pos).
723 stack
724 ;; Regexp which matches "for", "if", etc.
725 (cond-key (or c-opt-block-stmt-key
a66cd3ee 726 "\\<\\>")) ; Matches nothing.
0386b551
AM
727 ;; Return value.
728 (ret 'same)
729 ;; Positions of the last three sexps or bounds we've stopped at.
730 tok ptok pptok)
a66cd3ee
MS
731
732 (save-restriction
733 (if lim (narrow-to-region lim (point-max)))
734
735 (if (save-excursion
736 (and (c-beginning-of-macro)
737 (/= (point) start)))
738 (setq macro-start (point)))
739
d9e94c22 740 ;; Try to skip back over unary operator characters, to register
a66cd3ee
MS
741 ;; that we've moved.
742 (while (progn
743 (setq pos (point))
0386b551
AM
744 (c-backward-syntactic-ws)
745 ;; Protect post-++/-- operators just before a virtual semicolon.
746 (and (not (c-at-vsemi-p))
747 (/= (skip-chars-backward "-+!*&~@`#") 0))))
d9e94c22
MS
748
749 ;; Skip back over any semicolon here. If it was a bare semicolon, we're
0386b551 750 ;; done. Later on we ignore the boundaries for statements that don't
d9e94c22
MS
751 ;; contain any sexp. The only thing that is affected is that the error
752 ;; checking is a little less strict, and we really don't bother.
a66cd3ee
MS
753 (if (and (memq (char-before) delims)
754 (progn (forward-char -1)
755 (setq saved (point))
0386b551 756 (c-backward-syntactic-ws)
a66cd3ee
MS
757 (or (memq (char-before) delims)
758 (memq (char-before) '(?: nil))
d9e94c22 759 (eq (char-syntax (char-before)) ?\()
0386b551 760 (c-at-vsemi-p))))
a66cd3ee
MS
761 (setq ret 'previous
762 pos saved)
763
764 ;; Begin at start and not pos to detect macros if we stand
765 ;; directly after the #.
766 (goto-char start)
767 (if (looking-at "\\<\\|\\W")
768 ;; Record this as the first token if not starting inside it.
769 (setq tok start))
770
b248a85d
AM
771
772 ;; The following while loop goes back one sexp (balanced parens,
773 ;; etc. with contents, or symbol or suchlike) each iteration. This
774 ;; movement is accomplished with a call to c-backward-sexp approx 170
775 ;; lines below.
776 ;;
777 ;; The loop is exited only by throwing nil to the (catch 'loop ...):
778 ;; 1. On reaching the start of a macro;
779 ;; 2. On having passed a stmt boundary with the PDA stack empty;
780 ;; 3. On reaching the start of an Objective C method def;
781 ;; 4. From macro `c-bos-pop-state'; when the stack is empty;
782 ;; 5. From macro `c-bos-pop-state-and-retry' when the stack is empty.
a66cd3ee
MS
783 (while
784 (catch 'loop ;; Throw nil to break, non-nil to continue.
785 (cond
b248a85d 786 ;; Are we in a macro, just after the opening #?
a66cd3ee 787 ((save-excursion
0386b551 788 (and macro-start ; Always NIL for AWK.
a66cd3ee
MS
789 (progn (skip-chars-backward " \t")
790 (eq (char-before) ?#))
791 (progn (setq saved (1- (point)))
792 (beginning-of-line)
793 (not (eq (char-before (1- (point))) ?\\)))
d9e94c22 794 (looking-at c-opt-cpp-start)
a66cd3ee
MS
795 (progn (skip-chars-forward " \t")
796 (eq (point) saved))))
797 (goto-char saved)
798 (if (and (c-forward-to-cpp-define-body)
799 (progn (c-forward-syntactic-ws start)
800 (< (point) start)))
801 ;; Stop at the first token in the content of the macro.
802 (setq pos (point)
803 ignore-labels t) ; Avoid the label check on exit.
804 (setq pos saved
805 ret 'macro
806 ignore-labels t))
b248a85d 807 (throw 'loop nil)) ; 1. Start of macro.
a66cd3ee 808
d9e94c22
MS
809 ;; Do a round through the automaton if we've just passed a
810 ;; statement boundary or passed a "while"-like token.
a66cd3ee
MS
811 ((or sym
812 (and (looking-at cond-key)
813 (setq sym (intern (match-string 1)))))
814
815 (when (and (< pos start) (null stack))
b248a85d 816 (throw 'loop nil)) ; 2. Statement boundary.
a66cd3ee 817
d9e94c22
MS
818 ;; The PDA state handling.
819 ;;
037558bf 820 ;; Refer to the description of the PDA in the opening
d9e94c22
MS
821 ;; comments. In the following OR form, the first leaf
822 ;; attempts to handles one of the specific actions detailed
823 ;; (e.g., finding token "if" whilst in state `else-boundary').
824 ;; We drop through to the second leaf (which handles common
825 ;; state) if no specific handler is found in the first cond.
826 ;; If a parsing error is detected (e.g. an "else" with no
827 ;; preceding "if"), we throw to the enclosing catch.
828 ;;
829 ;; Note that the (eq state 'else) means
830 ;; "we've just passed an else", NOT "we're looking for an
831 ;; else".
a66cd3ee
MS
832 (or (cond
833 ((eq state 'else)
834 (if (eq sym 'boundary)
835 (setq state 'else-boundary)
836 (c-bos-report-error)
837 (c-bos-pop-state-and-retry)))
838
839 ((eq state 'else-boundary)
840 (cond ((eq sym 'if)
841 (c-bos-pop-state (setq ret 'beginning)))
842 ((eq sym 'boundary)
843 (c-bos-report-error)
844 (c-bos-pop-state))))
845
846 ((eq state 'while)
847 (if (and (eq sym 'boundary)
848 ;; Since this can cause backtracking we do a
849 ;; little more careful analysis to avoid it:
850 ;; If there's a label in front of the while
851 ;; it can't be part of a do-while.
852 (not after-labels-pos))
853 (progn (c-bos-save-pos)
854 (setq state 'while-boundary))
d9e94c22 855 (c-bos-pop-state-and-retry))) ; Can't be a do-while
a66cd3ee
MS
856
857 ((eq state 'while-boundary)
858 (cond ((eq sym 'do)
859 (c-bos-pop-state (setq ret 'beginning)))
d9e94c22
MS
860 ((eq sym 'boundary) ; isn't a do-while
861 (c-bos-restore-pos) ; the position of the while
862 (c-bos-pop-state)))) ; no longer searching for do.
a66cd3ee
MS
863
864 ((eq state 'catch)
865 (if (eq sym 'boundary)
866 (setq state 'catch-boundary)
867 (c-bos-report-error)
868 (c-bos-pop-state-and-retry)))
869
870 ((eq state 'catch-boundary)
871 (cond
872 ((eq sym 'try)
873 (c-bos-pop-state (setq ret 'beginning)))
874 ((eq sym 'catch)
875 (setq state 'catch))
876 ((eq sym 'boundary)
877 (c-bos-report-error)
878 (c-bos-pop-state)))))
879
d9e94c22
MS
880 ;; This is state common. We get here when the previous
881 ;; cond statement found no particular state handler.
a66cd3ee 882 (cond ((eq sym 'boundary)
d9e94c22
MS
883 ;; If we have a boundary at the start
884 ;; position we push a frame to go to the
885 ;; previous statement.
886 (if (>= pos start)
887 (c-bos-push-state)
888 (c-bos-pop-state)))
a66cd3ee
MS
889 ((eq sym 'else)
890 (c-bos-push-state)
891 (c-bos-save-error-info 'if 'else)
892 (setq state 'else))
893 ((eq sym 'while)
0386b551
AM
894 ;; Is this a real while, or a do-while?
895 ;; The next `when' triggers unless we are SURE that
896 ;; the `while' is not the tailend of a `do-while'.
a66cd3ee 897 (when (or (not pptok)
d9e94c22 898 (memq (char-after pptok) delims)
0386b551
AM
899 ;; The following kludge is to prevent
900 ;; infinite recursion when called from
901 ;; c-awk-after-if-for-while-condition-p,
902 ;; or the like.
903 (and (eq (point) start)
904 (c-vsemi-status-unknown-p))
905 (c-at-vsemi-p pptok))
a66cd3ee
MS
906 ;; Since this can cause backtracking we do a
907 ;; little more careful analysis to avoid it: If
0386b551
AM
908 ;; the while isn't followed by a (possibly
909 ;; virtual) semicolon it can't be a do-while.
a66cd3ee
MS
910 (c-bos-push-state)
911 (setq state 'while)))
912 ((memq sym '(catch finally))
913 (c-bos-push-state)
914 (c-bos-save-error-info 'try sym)
915 (setq state 'catch))))
916
917 (when c-maybe-labelp
918 ;; We're either past a statement boundary or at the
919 ;; start of a statement, so throw away any label data
920 ;; for the previous one.
921 (setq after-labels-pos nil
922 last-label-pos nil
923 c-maybe-labelp nil))))
924
d9e94c22
MS
925 ;; Step to the previous sexp, but not if we crossed a
926 ;; boundary, since that doesn't consume an sexp.
a66cd3ee
MS
927 (if (eq sym 'boundary)
928 (setq ret 'previous)
d9e94c22
MS
929
930 ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
0386b551
AM
931 ;; BACKWARDS THROUGH THE SOURCE.
932
0386b551 933 (c-backward-syntactic-ws)
0386b551 934 (let ((before-sws-pos (point))
0386b551
AM
935 ;; The end position of the area to search for statement
936 ;; barriers in this round.
b248a85d 937 (maybe-after-boundary-pos pos))
0386b551 938
b248a85d
AM
939 ;; Go back over exactly one logical sexp, taking proper
940 ;; account of macros and escaped EOLs.
0386b551
AM
941 (while
942 (progn
943 (unless (c-safe (c-backward-sexp) t)
944 ;; Give up if we hit an unbalanced block. Since the
945 ;; stack won't be empty the code below will report a
946 ;; suitable error.
a66cd3ee 947 (throw 'loop nil))
b248a85d
AM
948 (cond
949 ;; Have we moved into a macro?
950 ((and (not macro-start)
951 (c-beginning-of-macro))
952 ;; Have we crossed a statement boundary? If not,
953 ;; keep going back until we find one or a "real" sexp.
954 (and
955 (save-excursion
956 (c-end-of-macro)
957 (not (c-crosses-statement-barrier-p
958 (point) maybe-after-boundary-pos)))
959 (setq maybe-after-boundary-pos (point))))
960 ;; Have we just gone back over an escaped NL? This
961 ;; doesn't count as a sexp.
962 ((looking-at "\\\\$")))))
963
964 ;; Have we crossed a statement boundary?
965 (setq boundary-pos
966 (cond
967 ;; Are we at a macro beginning?
968 ((and (not macro-start)
969 c-opt-cpp-prefix
970 (looking-at c-opt-cpp-prefix))
971 (save-excursion
972 (c-end-of-macro)
973 (c-crosses-statement-barrier-p
974 (point) maybe-after-boundary-pos)))
975 ;; Just gone back over a brace block?
976 ((and
977 (eq (char-after) ?{)
978 (not (c-looking-at-inexpr-block lim nil t)))
979 (save-excursion
980 (c-forward-sexp) (point)))
981 ;; Just gone back over some paren block?
982 ((looking-at "\\s\(")
983 (save-excursion
984 (goto-char (1+ (c-down-list-backward
985 before-sws-pos)))
986 (c-crosses-statement-barrier-p
987 (point) maybe-after-boundary-pos)))
988 ;; Just gone back over an ordinary symbol of some sort?
989 (t (c-crosses-statement-barrier-p
990 (point) maybe-after-boundary-pos))))
991
992 (when boundary-pos
993 (setq pptok ptok
994 ptok tok
995 tok boundary-pos
996 sym 'boundary)
997 ;; Like a C "continue". Analyze the next sexp.
998 (throw 'loop t))))
a66cd3ee
MS
999
1000 ;; ObjC method def?
1001 (when (and c-opt-method-key
1002 (setq saved (c-in-method-def-p)))
1003 (setq pos saved
1004 ignore-labels t) ; Avoid the label check on exit.
b248a85d
AM
1005 (throw 'loop nil)) ; 3. ObjC method def.
1006
1007 ;; Might we have a bitfield declaration, "<type> <id> : <size>"?
1008 (if c-has-bitfields
1009 (cond
1010 ;; The : <size> and <id> fields?
1011 ((and (numberp c-maybe-labelp)
1012 (not bitfield-size-pos)
1013 (save-excursion
1014 (goto-char (or tok start))
1015 (not (looking-at c-keywords-regexp)))
1016 (not (looking-at c-keywords-regexp))
1017 (not (c-punctuation-in (point) c-maybe-labelp)))
1018 (setq bitfield-size-pos (or tok start)
1019 bitfield-id-pos (point)))
1020 ;; The <type> field?
1021 ((and bitfield-id-pos
1022 (not bitfield-type-pos))
1023 (if (and (looking-at c-symbol-key) ; Can only be an integer type. :-)
1024 (not (looking-at c-not-primitive-type-keywords-regexp))
1025 (not (c-punctuation-in (point) tok)))
1026 (setq bitfield-type-pos (point))
1027 (setq bitfield-size-pos nil
1028 bitfield-id-pos nil)))))
a66cd3ee 1029
0386b551
AM
1030 ;; Handle labels.
1031 (unless (eq ignore-labels t)
1032 (when (numberp c-maybe-labelp)
cb694ab7
AM
1033 ;; `c-crosses-statement-barrier-p' has found a colon, so we
1034 ;; might be in a label now. Have we got a real label
1035 ;; (including a case label) or something like C++'s "public:"?
d28e7f28
AM
1036 ;; A case label might use an expression rather than a token.
1037 (setq after-case:-pos (or tok start))
1038 (if (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
1039 (setq c-maybe-labelp nil)
1040 (if after-labels-pos ; Have we already encountered a label?
1041 (if (not last-label-pos)
1042 (setq last-label-pos (or tok start)))
1043 (setq after-labels-pos (or tok start)))
1044 (setq c-maybe-labelp t
1045 label-good-pos nil))) ; bogus "label"
cb694ab7
AM
1046
1047 (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
1048 ; been found.
1049 (looking-at c-nonlabel-token-key)) ; e.g. "while :"
0386b551
AM
1050 ;; We're in a potential label and it's the first
1051 ;; time we've found something that isn't allowed in
1052 ;; one.
1053 (setq label-good-pos (or tok start))))
1054
1055 ;; We've moved back by a sexp, so update the token positions.
a66cd3ee
MS
1056 (setq sym nil
1057 pptok ptok
1058 ptok tok
1059 tok (point)
b248a85d
AM
1060 pos tok) ; always non-nil
1061 ) ; end of (catch loop ....)
1062 ) ; end of sexp-at-a-time (while ....)
1063
a66cd3ee
MS
1064 ;; If the stack isn't empty there might be errors to report.
1065 (while stack
1066 (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
1067 (c-bos-report-error))
1068 (setq saved-pos (cdr (car stack))
1069 stack (cdr stack)))
1070
1071 (when (and (eq ret 'same)
1072 (not (memq sym '(boundary ignore nil))))
1073 ;; Need to investigate closer whether we've crossed
1074 ;; between a substatement and its containing statement.
1075 (if (setq saved (if (looking-at c-block-stmt-1-key)
1076 ptok
1077 pptok))
1078 (cond ((> start saved) (setq pos saved))
1079 ((= start saved) (setq ret 'up)))))
1080
0386b551
AM
1081 (when (and (not ignore-labels)
1082 (eq c-maybe-labelp t)
d9e94c22 1083 (not (eq ret 'beginning))
0386b551 1084 after-labels-pos
b248a85d 1085 (not bitfield-type-pos) ; Bitfields take precedence over labels.
0386b551
AM
1086 (or (not label-good-pos)
1087 (<= label-good-pos pos)
1088 (progn
1089 (goto-char (if (and last-label-pos
1090 (< last-label-pos start))
1091 last-label-pos
1092 pos))
1093 (looking-at c-label-kwds-regexp))))
a66cd3ee
MS
1094 ;; We're in a label. Maybe we should step to the statement
1095 ;; after it.
1096 (if (< after-labels-pos start)
1097 (setq pos after-labels-pos)
1098 (setq ret 'label)
0386b551
AM
1099 (if (and last-label-pos (< last-label-pos start))
1100 ;; Might have jumped over several labels. Go to the last one.
a66cd3ee
MS
1101 (setq pos last-label-pos)))))
1102
d28e7f28 1103 ;; Have we got "case <expression>:"?
a66cd3ee 1104 (goto-char pos)
d28e7f28
AM
1105 (when (and after-case:-pos
1106 (not (eq ret 'beginning))
1107 (looking-at c-case-kwds-regexp))
1108 (if (< after-case:-pos start)
0000ee90
AM
1109 (setq pos after-case:-pos))
1110 (if (eq ret 'same)
1111 (setq ret 'label)))
d28e7f28
AM
1112
1113 ;; Skip over the unary operators that can start the statement.
a66cd3ee 1114 (while (progn
0386b551
AM
1115 (c-backward-syntactic-ws)
1116 ;; protect AWK post-inc/decrement operators, etc.
1117 (and (not (c-at-vsemi-p (point)))
1118 (/= (skip-chars-backward "-+!*&~@`#") 0)))
a66cd3ee
MS
1119 (setq pos (point)))
1120 (goto-char pos)
1121 ret)))
785eecbb 1122
b248a85d
AM
1123(defun c-punctuation-in (from to)
1124 "Return non-nil if there is a non-comment non-macro punctuation character
1125between FROM and TO. FROM must not be in a string or comment. The returned
1126value is the position of the first such character."
1127 (save-excursion
1128 (goto-char from)
1129 (let ((pos (point)))
1130 (while (progn (skip-chars-forward c-symbol-chars to)
1131 (c-forward-syntactic-ws to)
1132 (> (point) pos))
1133 (setq pos (point))))
1134 (and (< (point) to) (point))))
1135
785eecbb 1136(defun c-crosses-statement-barrier-p (from to)
a66cd3ee
MS
1137 "Return non-nil if buffer positions FROM to TO cross one or more
1138statement or declaration boundaries. The returned value is actually
d9e94c22
MS
1139the position of the earliest boundary char. FROM must not be within
1140a string or comment.
a66cd3ee
MS
1141
1142The variable `c-maybe-labelp' is set to the position of the first `:' that
1143might start a label (i.e. not part of `::' and not preceded by `?'). If a
0386b551
AM
1144single `?' is found, then `c-maybe-labelp' is cleared.
1145
1146For AWK, a statement which is terminated by an EOL (not a \; or a }) is
1147regarded as having a \"virtual semicolon\" immediately after the last token on
b414f371 1148the line. If this virtual semicolon is _at_ from, the function recognizes it.
0386b551
AM
1149
1150Note that this function might do hidden buffer changes. See the
1151comment at the start of cc-engine.el for more info."
a66cd3ee
MS
1152 (let ((skip-chars c-stmt-delim-chars)
1153 lit-range)
1154 (save-excursion
1155 (catch 'done
1156 (goto-char from)
1157 (while (progn (skip-chars-forward skip-chars to)
785eecbb 1158 (< (point) to))
0386b551
AM
1159 (cond
1160 ((setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
1161 (goto-char (cdr lit-range)))
1162 ((eq (char-after) ?:)
1163 (forward-char)
1164 (if (and (eq (char-after) ?:)
1165 (< (point) to))
1166 ;; Ignore scope operators.
1167 (forward-char)
1168 (setq c-maybe-labelp (1- (point)))))
1169 ((eq (char-after) ??)
1170 ;; A question mark. Can't be a label, so stop
1171 ;; looking for more : and ?.
1172 (setq c-maybe-labelp nil
1173 skip-chars (substring c-stmt-delim-chars 0 -2)))
1174 ((memq (char-after) '(?# ?\n ?\r)) ; A virtual semicolon?
1175 (if (and (eq (char-before) ?\\) (memq (char-after) '(?\n ?\r)))
1176 (backward-char))
1177 (skip-chars-backward " \t" from)
1178 (if (c-at-vsemi-p)
1179 (throw 'done (point))
1180 (forward-line)))
1181 (t (throw 'done (point)))))
1182 ;; In trailing space after an as yet undetected virtual semicolon?
1183 (c-backward-syntactic-ws from)
1184 (if (and (< (point) to)
1185 (c-at-vsemi-p))
1186 (point)
1187 nil)))))
1188
1189(defun c-at-statement-start-p ()
1190 "Return non-nil if the point is at the first token in a statement
1191or somewhere in the syntactic whitespace before it.
1192
1193A \"statement\" here is not restricted to those inside code blocks.
1194Any kind of declaration-like construct that occur outside function
1195bodies is also considered a \"statement\".
1196
1197Note that this function might do hidden buffer changes. See the
1198comment at the start of cc-engine.el for more info."
1199
1200 (save-excursion
1201 (let ((end (point))
1202 c-maybe-labelp)
1203 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1204 (or (bobp)
1205 (eq (char-before) ?})
1206 (and (eq (char-before) ?{)
1207 (not (and c-special-brace-lists
1208 (progn (backward-char)
1209 (c-looking-at-special-brace-list)))))
1210 (c-crosses-statement-barrier-p (point) end)))))
1211
1212(defun c-at-expression-start-p ()
1213 "Return non-nil if the point is at the first token in an expression or
1214statement, or somewhere in the syntactic whitespace before it.
1215
1216An \"expression\" here is a bit different from the normal language
1217grammar sense: It's any sequence of expression tokens except commas,
1218unless they are enclosed inside parentheses of some kind. Also, an
1219expression never continues past an enclosing parenthesis, but it might
1220contain parenthesis pairs of any sort except braces.
1221
1222Since expressions never cross statement boundaries, this function also
1223recognizes statement beginnings, just like `c-at-statement-start-p'.
1224
1225Note that this function might do hidden buffer changes. See the
1226comment at the start of cc-engine.el for more info."
1227
1228 (save-excursion
1229 (let ((end (point))
1230 (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
1231 c-maybe-labelp)
1232 (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
1233 (or (bobp)
1234 (memq (char-before) '(?{ ?}))
1235 (save-excursion (backward-char)
1236 (looking-at "\\s("))
1237 (c-crosses-statement-barrier-p (point) end)))))
785eecbb
RS
1238
1239\f
d9e94c22
MS
1240;; A set of functions that covers various idiosyncrasies in
1241;; implementations of `forward-comment'.
1242
1243;; Note: Some emacsen considers incorrectly that any line comment
1244;; ending with a backslash continues to the next line. I can't think
1245;; of any way to work around that in a reliable way without changing
1246;; the buffer, though. Suggestions welcome. ;) (No, temporarily
1247;; changing the syntax for backslash doesn't work since we must treat
1248;; escapes in string literals correctly.)
1249
1250(defun c-forward-single-comment ()
1251 "Move forward past whitespace and the closest following comment, if any.
1252Return t if a comment was found, nil otherwise. In either case, the
1253point is moved past the following whitespace. Line continuations,
1254i.e. a backslashes followed by line breaks, are treated as whitespace.
1255The line breaks that end line comments are considered to be the
1256comment enders, so the point will be put on the beginning of the next
1257line if it moved past a line comment.
1258
1259This function does not do any hidden buffer changes."
1260
1261 (let ((start (point)))
1262 (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
1263 (goto-char (match-end 0)))
1264
1265 (when (forward-comment 1)
1266 (if (eobp)
1267 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1268 ;; forwards at eob.
1269 nil
1270
1271 ;; Emacs includes the ending newline in a b-style (c++)
1272 ;; comment, but XEmacs doesn't. We depend on the Emacs
1273 ;; behavior (which also is symmetric).
1274 (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
1275 (condition-case nil (forward-char 1)))
1276
1277 t))))
1278
1279(defsubst c-forward-comments ()
1280 "Move forward past all following whitespace and comments.
1281Line continuations, i.e. a backslashes followed by line breaks, are
1282treated as whitespace.
1283
0386b551
AM
1284Note that this function might do hidden buffer changes. See the
1285comment at the start of cc-engine.el for more info."
d9e94c22
MS
1286
1287 (while (or
1288 ;; If forward-comment in at least XEmacs 21 is given a large
1289 ;; positive value, it'll loop all the way through if it hits
1290 ;; eob.
1291 (and (forward-comment 5)
1292 ;; Some emacsen (e.g. XEmacs 21) return t when moving
1293 ;; forwards at eob.
1294 (not (eobp)))
1295
1296 (when (looking-at "\\\\[\n\r]")
1297 (forward-char 2)
1298 t))))
1299
1300(defun c-backward-single-comment ()
1301 "Move backward past whitespace and the closest preceding comment, if any.
1302Return t if a comment was found, nil otherwise. In either case, the
1303point is moved past the preceding whitespace. Line continuations,
1304i.e. a backslashes followed by line breaks, are treated as whitespace.
1305The line breaks that end line comments are considered to be the
1306comment enders, so the point cannot be at the end of the same line to
1307move over a line comment.
1308
1309This function does not do any hidden buffer changes."
1310
1311 (let ((start (point)))
1312 ;; When we got newline terminated comments, forward-comment in all
1313 ;; supported emacsen so far will stop at eol of each line not
1314 ;; ending with a comment when moving backwards. This corrects for
1315 ;; that, and at the same time handles line continuations.
1316 (while (progn
1317 (skip-chars-backward " \t\n\r\f\v")
1318 (and (looking-at "[\n\r]")
0386b551 1319 (eq (char-before) ?\\)))
d9e94c22
MS
1320 (backward-char))
1321
1322 (if (bobp)
1323 ;; Some emacsen (e.g. Emacs 19.34) return t when moving
1324 ;; backwards at bob.
1325 nil
1326
1327 ;; Leave point after the closest following newline if we've
1328 ;; backed up over any above, since forward-comment won't move
1329 ;; backward over a line comment if point is at the end of the
1330 ;; same line.
1331 (re-search-forward "\\=\\s *[\n\r]" start t)
1332
bf2c1571 1333 (if (if (let (open-paren-in-column-0-is-defun-start) (forward-comment -1))
d9e94c22
MS
1334 (if (eolp)
1335 ;; If forward-comment above succeeded and we're at eol
1336 ;; then the newline we moved over above didn't end a
1337 ;; line comment, so we give it another go.
bf2c1571
AM
1338 (let (open-paren-in-column-0-is-defun-start)
1339 (forward-comment -1))
d9e94c22
MS
1340 t))
1341
1342 ;; Emacs <= 20 and XEmacs move back over the closer of a
1343 ;; block comment that lacks an opener.
1344 (if (looking-at "\\*/")
1345 (progn (forward-char 2) nil)
1346 t)))))
1347
1348(defsubst c-backward-comments ()
1349 "Move backward past all preceding whitespace and comments.
1350Line continuations, i.e. a backslashes followed by line breaks, are
1351treated as whitespace. The line breaks that end line comments are
1352considered to be the comment enders, so the point cannot be at the end
0386b551
AM
1353of the same line to move over a line comment. Unlike
1354c-backward-syntactic-ws, this function doesn't move back over
1355preprocessor directives.
d9e94c22 1356
0386b551
AM
1357Note that this function might do hidden buffer changes. See the
1358comment at the start of cc-engine.el for more info."
d9e94c22
MS
1359
1360 (let ((start (point)))
1361 (while (and
0386b551 1362 ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
d9e94c22
MS
1363 ;; return t when moving backwards at bob.
1364 (not (bobp))
1365
bf2c1571
AM
1366 (if (let (open-paren-in-column-0-is-defun-start)
1367 (forward-comment -1))
d9e94c22
MS
1368 (if (looking-at "\\*/")
1369 ;; Emacs <= 20 and XEmacs move back over the
1370 ;; closer of a block comment that lacks an opener.
1371 (progn (forward-char 2) nil)
1372 t)
1373
1374 ;; XEmacs treats line continuations as whitespace but
1375 ;; only in the backward direction, which seems a bit
1376 ;; odd. Anyway, this is necessary for Emacs.
1377 (when (and (looking-at "[\n\r]")
1378 (eq (char-before) ?\\)
1379 (< (point) start))
1380 (backward-char)
1381 t))))))
1382
1383\f
d9e94c22 1384;; Tools for skipping over syntactic whitespace.
a66cd3ee 1385
d9e94c22
MS
1386;; The following functions use text properties to cache searches over
1387;; large regions of syntactic whitespace. It works as follows:
1388;;
1389;; o If a syntactic whitespace region contains anything but simple
1390;; whitespace (i.e. space, tab and line breaks), the text property
1391;; `c-in-sws' is put over it. At places where we have stopped
1392;; within that region there's also a `c-is-sws' text property.
1393;; That since there typically are nested whitespace inside that
1394;; must be handled separately, e.g. whitespace inside a comment or
1395;; cpp directive. Thus, from one point with `c-is-sws' it's safe
1396;; to jump to another point with that property within the same
1397;; `c-in-sws' region. It can be likened to a ladder where
1398;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
1399;;
1400;; o The `c-is-sws' property is put on the simple whitespace chars at
1401;; a "rung position" and also maybe on the first following char.
1402;; As many characters as can be conveniently found in this range
1403;; are marked, but no assumption can be made that the whole range
1404;; is marked (it could be clobbered by later changes, for
1405;; instance).
1406;;
1407;; Note that some part of the beginning of a sequence of simple
1408;; whitespace might be part of the end of a preceding line comment
1409;; or cpp directive and must not be considered part of the "rung".
1410;; Such whitespace is some amount of horizontal whitespace followed
1411;; by a newline. In the case of cpp directives it could also be
1412;; two newlines with horizontal whitespace between them.
1413;;
1414;; The reason to include the first following char is to cope with
1415;; "rung positions" that doesn't have any ordinary whitespace. If
1416;; `c-is-sws' is put on a token character it does not have
1417;; `c-in-sws' set simultaneously. That's the only case when that
1418;; can occur, and the reason for not extending the `c-in-sws'
1419;; region to cover it is that the `c-in-sws' region could then be
1420;; accidentally merged with a following one if the token is only
1421;; one character long.
1422;;
1423;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
1424;; removed in the changed region. If the change was inside
1425;; syntactic whitespace that means that the "ladder" is broken, but
1426;; a later call to `c-forward-sws' or `c-backward-sws' will use the
1427;; parts on either side and use an ordinary search only to "repair"
1428;; the gap.
1429;;
1430;; Special care needs to be taken if a region is removed: If there
1431;; are `c-in-sws' on both sides of it which do not connect inside
1432;; the region then they can't be joined. If e.g. a marked macro is
1433;; broken, syntactic whitespace inside the new text might be
1434;; marked. If those marks would become connected with the old
1435;; `c-in-sws' range around the macro then we could get a ladder
1436;; with one end outside the macro and the other at some whitespace
1437;; within it.
1438;;
1439;; The main motivation for this system is to increase the speed in
1440;; skipping over the large whitespace regions that can occur at the
1441;; top level in e.g. header files that contain a lot of comments and
1442;; cpp directives. For small comments inside code it's probably
1443;; slower than using `forward-comment' straightforwardly, but speed is
1444;; not a significant factor there anyway.
1445
1446; (defface c-debug-is-sws-face
1447; '((t (:background "GreenYellow")))
1448; "Debug face to mark the `c-is-sws' property.")
1449; (defface c-debug-in-sws-face
1450; '((t (:underline t)))
1451; "Debug face to mark the `c-in-sws' property.")
1452
1453; (defun c-debug-put-sws-faces ()
1454; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
1455; ;; properties in the buffer.
1456; (interactive)
1457; (save-excursion
0386b551 1458; (c-save-buffer-state (in-face)
d9e94c22
MS
1459; (goto-char (point-min))
1460; (setq in-face (if (get-text-property (point) 'c-is-sws)
1461; (point)))
1462; (while (progn
1463; (goto-char (next-single-property-change
1464; (point) 'c-is-sws nil (point-max)))
1465; (if in-face
1466; (progn
1467; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
1468; (setq in-face nil))
1469; (setq in-face (point)))
1470; (not (eobp))))
1471; (goto-char (point-min))
1472; (setq in-face (if (get-text-property (point) 'c-in-sws)
1473; (point)))
1474; (while (progn
1475; (goto-char (next-single-property-change
1476; (point) 'c-in-sws nil (point-max)))
1477; (if in-face
1478; (progn
1479; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
1480; (setq in-face nil))
1481; (setq in-face (point)))
1482; (not (eobp)))))))
1483
1484(defmacro c-debug-sws-msg (&rest args)
1485 ;;`(message ,@args)
1486 )
1487
1488(defmacro c-put-is-sws (beg end)
0386b551 1489 ;; This macro does a hidden buffer change.
d9e94c22
MS
1490 `(let ((beg ,beg) (end ,end))
1491 (put-text-property beg end 'c-is-sws t)
1492 ,@(when (facep 'c-debug-is-sws-face)
1493 `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
1494
1495(defmacro c-put-in-sws (beg end)
0386b551 1496 ;; This macro does a hidden buffer change.
d9e94c22
MS
1497 `(let ((beg ,beg) (end ,end))
1498 (put-text-property beg end 'c-in-sws t)
1499 ,@(when (facep 'c-debug-is-sws-face)
1500 `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
1501
1502(defmacro c-remove-is-sws (beg end)
0386b551 1503 ;; This macro does a hidden buffer change.
d9e94c22
MS
1504 `(let ((beg ,beg) (end ,end))
1505 (remove-text-properties beg end '(c-is-sws nil))
1506 ,@(when (facep 'c-debug-is-sws-face)
1507 `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
1508
1509(defmacro c-remove-in-sws (beg end)
0386b551 1510 ;; This macro does a hidden buffer change.
d9e94c22
MS
1511 `(let ((beg ,beg) (end ,end))
1512 (remove-text-properties beg end '(c-in-sws nil))
1513 ,@(when (facep 'c-debug-is-sws-face)
1514 `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1515
1516(defmacro c-remove-is-and-in-sws (beg end)
0386b551 1517 ;; This macro does a hidden buffer change.
d9e94c22
MS
1518 `(let ((beg ,beg) (end ,end))
1519 (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
1520 ,@(when (facep 'c-debug-is-sws-face)
1521 `((c-debug-remove-face beg end 'c-debug-is-sws-face)
1522 (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
1523
1524(defsubst c-invalidate-sws-region-after (beg end)
1525 ;; Called from `after-change-functions'. Note that if
1526 ;; `c-forward-sws' or `c-backward-sws' are used outside
1527 ;; `c-save-buffer-state' or similar then this will remove the cache
1528 ;; properties right after they're added.
0386b551
AM
1529 ;;
1530 ;; This function does hidden buffer changes.
d9e94c22
MS
1531
1532 (save-excursion
1533 ;; Adjust the end to remove the properties in any following simple
1534 ;; ws up to and including the next line break, if there is any
1535 ;; after the changed region. This is necessary e.g. when a rung
1536 ;; marked empty line is converted to a line comment by inserting
1537 ;; "//" before the line break. In that case the line break would
1538 ;; keep the rung mark which could make a later `c-backward-sws'
1539 ;; move into the line comment instead of over it.
1540 (goto-char end)
1541 (skip-chars-forward " \t\f\v")
1542 (when (and (eolp) (not (eobp)))
1543 (setq end (1+ (point)))))
1544
1545 (when (and (= beg end)
1546 (get-text-property beg 'c-in-sws)
2a15eb73 1547 (> beg (point-min))
d9e94c22
MS
1548 (get-text-property (1- beg) 'c-in-sws))
1549 ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
1550 ;; safe to keep a range that was continuous before the change. E.g:
1551 ;;
1552 ;; #define foo
1553 ;; \
1554 ;; bar
1555 ;;
1556 ;; There can be a "ladder" between "#" and "b". Now, if the newline
1557 ;; after "foo" is removed then "bar" will become part of the cpp
1558 ;; directive instead of a syntactically relevant token. In that
1559 ;; case there's no longer syntactic ws from "#" to "b".
1560 (setq beg (1- beg)))
1561
1562 (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
1563 (c-remove-is-and-in-sws beg end))
1564
1565(defun c-forward-sws ()
1566 ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
0386b551
AM
1567 ;;
1568 ;; This function might do hidden buffer changes.
d9e94c22
MS
1569
1570 (let (;; `rung-pos' is set to a position as early as possible in the
1571 ;; unmarked part of the simple ws region.
1572 (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
1573 rung-is-marked next-rung-is-marked simple-ws-end
1574 ;; `safe-start' is set when it's safe to cache the start position.
1575 ;; It's not set if we've initially skipped over comments and line
1576 ;; continuations since we might have gone out through the end of a
1577 ;; macro then. This provision makes `c-forward-sws' not populate the
1578 ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
1579 ;; more common.
1580 safe-start)
1581
1582 ;; Skip simple ws and do a quick check on the following character to see
1583 ;; if it's anything that can't start syntactic ws, so we can bail out
1584 ;; early in the majority of cases when there just are a few ws chars.
1585 (skip-chars-forward " \t\n\r\f\v")
1586 (when (looking-at c-syntactic-ws-start)
1587
1588 (setq rung-end-pos (min (1+ (point)) (point-max)))
1589 (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
1590 'c-is-sws t))
1591 ;; Find the last rung position to avoid setting properties in all
1592 ;; the cases when the marked rung is complete.
1593 ;; (`next-single-property-change' is certain to move at least one
1594 ;; step forward.)
1595 (setq rung-pos (1- (next-single-property-change
1596 rung-is-marked 'c-is-sws nil rung-end-pos)))
1597 ;; Got no marked rung here. Since the simple ws might have started
1598 ;; inside a line comment or cpp directive we must set `rung-pos' as
1599 ;; high as possible.
1600 (setq rung-pos (point)))
1601
1602 (while
1603 (progn
1604 (while
1605 (when (and rung-is-marked
1606 (get-text-property (point) 'c-in-sws))
1607
1608 ;; The following search is the main reason that `c-in-sws'
1609 ;; and `c-is-sws' aren't combined to one property.
1610 (goto-char (next-single-property-change
1611 (point) 'c-in-sws nil (point-max)))
1612 (unless (get-text-property (point) 'c-is-sws)
1613 ;; If the `c-in-sws' region extended past the last
1614 ;; `c-is-sws' char we have to go back a bit.
1615 (or (get-text-property (1- (point)) 'c-is-sws)
1616 (goto-char (previous-single-property-change
1617 (point) 'c-is-sws)))
1618 (backward-char))
1619
1620 (c-debug-sws-msg
1621 "c-forward-sws cached move %s -> %s (max %s)"
1622 rung-pos (point) (point-max))
1623
1624 (setq rung-pos (point))
1625 (and (> (skip-chars-forward " \t\n\r\f\v") 0)
1626 (not (eobp))))
1627
1628 ;; We'll loop here if there is simple ws after the last rung.
1629 ;; That means that there's been some change in it and it's
1630 ;; possible that we've stepped into another ladder, so extend
1631 ;; the previous one to join with it if there is one, and try to
1632 ;; use the cache again.
1633 (c-debug-sws-msg
1634 "c-forward-sws extending rung with [%s..%s] (max %s)"
1635 (1+ rung-pos) (1+ (point)) (point-max))
1636 (unless (get-text-property (point) 'c-is-sws)
1637 ;; Remove any `c-in-sws' property from the last char of
1638 ;; the rung before we mark it with `c-is-sws', so that we
1639 ;; won't connect with the remains of a broken "ladder".
1640 (c-remove-in-sws (point) (1+ (point))))
1641 (c-put-is-sws (1+ rung-pos)
1642 (1+ (point)))
1643 (c-put-in-sws rung-pos
1644 (setq rung-pos (point)
1645 last-put-in-sws-pos rung-pos)))
1646
1647 (setq simple-ws-end (point))
1648 (c-forward-comments)
1649
1650 (cond
1651 ((/= (point) simple-ws-end)
1652 ;; Skipped over comments. Don't cache at eob in case the buffer
1653 ;; is narrowed.
1654 (not (eobp)))
1655
1656 ((save-excursion
1657 (and c-opt-cpp-prefix
1658 (looking-at c-opt-cpp-start)
1659 (progn (skip-chars-backward " \t")
1660 (bolp))
1661 (or (bobp)
1662 (progn (backward-char)
1663 (not (eq (char-before) ?\\))))))
1664 ;; Skip a preprocessor directive.
1665 (end-of-line)
1666 (while (and (eq (char-before) ?\\)
1667 (= (forward-line 1) 0))
1668 (end-of-line))
1669 (forward-line 1)
1670 (setq safe-start t)
1671 ;; Don't cache at eob in case the buffer is narrowed.
1672 (not (eobp)))))
1673
1674 ;; We've searched over a piece of non-white syntactic ws. See if this
1675 ;; can be cached.
1676 (setq next-rung-pos (point))
1677 (skip-chars-forward " \t\n\r\f\v")
1678 (setq rung-end-pos (min (1+ (point)) (point-max)))
1679
1680 (if (or
1681 ;; Cache if we haven't skipped comments only, and if we started
1682 ;; either from a marked rung or from a completely uncached
1683 ;; position.
1684 (and safe-start
1685 (or rung-is-marked
1686 (not (get-text-property simple-ws-end 'c-in-sws))))
1687
1688 ;; See if there's a marked rung in the encountered simple ws. If
1689 ;; so then we can cache, unless `safe-start' is nil. Even then
1690 ;; we need to do this to check if the cache can be used for the
1691 ;; next step.
1692 (and (setq next-rung-is-marked
1693 (text-property-any next-rung-pos rung-end-pos
1694 'c-is-sws t))
1695 safe-start))
b2acd789 1696
0ec8351b 1697 (progn
d9e94c22
MS
1698 (c-debug-sws-msg
1699 "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
1700 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1701 (point-max))
1702
1703 ;; Remove the properties for any nested ws that might be cached.
1704 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1705 ;; anyway.
1706 (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
1707 (unless (and rung-is-marked (= rung-pos simple-ws-end))
1708 (c-put-is-sws rung-pos
1709 (1+ simple-ws-end))
1710 (setq rung-is-marked t))
1711 (c-put-in-sws rung-pos
1712 (setq rung-pos (point)
1713 last-put-in-sws-pos rung-pos))
1714 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1715 ;; Remove any `c-in-sws' property from the last char of
1716 ;; the rung before we mark it with `c-is-sws', so that we
1717 ;; won't connect with the remains of a broken "ladder".
1718 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1719 (c-put-is-sws next-rung-pos
1720 rung-end-pos))
1721
1722 (c-debug-sws-msg
1723 "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
1724 rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
1725 (point-max))
1726
1727 ;; Set `rung-pos' for the next rung. It's the same thing here as
1728 ;; initially, except that the rung position is set as early as
1729 ;; possible since we can't be in the ending ws of a line comment or
1730 ;; cpp directive now.
1731 (if (setq rung-is-marked next-rung-is-marked)
1732 (setq rung-pos (1- (next-single-property-change
1733 rung-is-marked 'c-is-sws nil rung-end-pos)))
1734 (setq rung-pos next-rung-pos))
1735 (setq safe-start t)))
1736
1737 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1738 ;; another one after the point (which might occur when editing inside a
1739 ;; comment or macro).
1740 (when (eq last-put-in-sws-pos (point))
1741 (cond ((< last-put-in-sws-pos (point-max))
1742 (c-debug-sws-msg
1743 "c-forward-sws clearing at %s for cache separation"
1744 last-put-in-sws-pos)
1745 (c-remove-in-sws last-put-in-sws-pos
1746 (1+ last-put-in-sws-pos)))
1747 (t
1748 ;; If at eob we have to clear the last character before the end
1749 ;; instead since the buffer might be narrowed and there might
1750 ;; be a `c-in-sws' after (point-max). In this case it's
1751 ;; necessary to clear both properties.
1752 (c-debug-sws-msg
1753 "c-forward-sws clearing thoroughly at %s for cache separation"
1754 (1- last-put-in-sws-pos))
1755 (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
1756 last-put-in-sws-pos))))
1757 )))
b2acd789 1758
d9e94c22
MS
1759(defun c-backward-sws ()
1760 ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
0386b551
AM
1761 ;;
1762 ;; This function might do hidden buffer changes.
d9e94c22
MS
1763
1764 (let (;; `rung-pos' is set to a position as late as possible in the unmarked
1765 ;; part of the simple ws region.
1766 (rung-pos (point)) next-rung-pos last-put-in-sws-pos
1767 rung-is-marked simple-ws-beg cmt-skip-pos)
1768
1769 ;; Skip simple horizontal ws and do a quick check on the preceding
1770 ;; character to see if it's anying that can't end syntactic ws, so we can
1771 ;; bail out early in the majority of cases when there just are a few ws
1772 ;; chars. Newlines are complicated in the backward direction, so we can't
1773 ;; skip over them.
1774 (skip-chars-backward " \t\f")
1775 (when (and (not (bobp))
1776 (save-excursion
1777 (backward-char)
1778 (looking-at c-syntactic-ws-end)))
1779
1780 ;; Try to find a rung position in the simple ws preceding point, so that
1781 ;; we can get a cache hit even if the last bit of the simple ws has
1782 ;; changed recently.
1783 (setq simple-ws-beg (point))
1784 (skip-chars-backward " \t\n\r\f\v")
1785 (if (setq rung-is-marked (text-property-any
1786 (point) (min (1+ rung-pos) (point-max))
1787 'c-is-sws t))
1788 ;; `rung-pos' will be the earliest marked position, which means that
1789 ;; there might be later unmarked parts in the simple ws region.
1790 ;; It's not worth the effort to fix that; the last part of the
1791 ;; simple ws is also typically edited often, so it could be wasted.
1792 (goto-char (setq rung-pos rung-is-marked))
1793 (goto-char simple-ws-beg))
1794
1795 (while
1796 (progn
1797 (while
1798 (when (and rung-is-marked
1799 (not (bobp))
1800 (get-text-property (1- (point)) 'c-in-sws))
1801
1802 ;; The following search is the main reason that `c-in-sws'
1803 ;; and `c-is-sws' aren't combined to one property.
1804 (goto-char (previous-single-property-change
1805 (point) 'c-in-sws nil (point-min)))
1806 (unless (get-text-property (point) 'c-is-sws)
1807 ;; If the `c-in-sws' region extended past the first
1808 ;; `c-is-sws' char we have to go forward a bit.
1809 (goto-char (next-single-property-change
1810 (point) 'c-is-sws)))
1811
1812 (c-debug-sws-msg
1813 "c-backward-sws cached move %s <- %s (min %s)"
1814 (point) rung-pos (point-min))
1815
1816 (setq rung-pos (point))
1817 (if (and (< (min (skip-chars-backward " \t\f\v")
1818 (progn
1819 (setq simple-ws-beg (point))
1820 (skip-chars-backward " \t\n\r\f\v")))
1821 0)
1822 (setq rung-is-marked
1823 (text-property-any (point) rung-pos
1824 'c-is-sws t)))
1825 t
1826 (goto-char simple-ws-beg)
1827 nil))
1828
1829 ;; We'll loop here if there is simple ws before the first rung.
1830 ;; That means that there's been some change in it and it's
1831 ;; possible that we've stepped into another ladder, so extend
1832 ;; the previous one to join with it if there is one, and try to
1833 ;; use the cache again.
1834 (c-debug-sws-msg
1835 "c-backward-sws extending rung with [%s..%s] (min %s)"
1836 rung-is-marked rung-pos (point-min))
1837 (unless (get-text-property (1- rung-pos) 'c-is-sws)
1838 ;; Remove any `c-in-sws' property from the last char of
1839 ;; the rung before we mark it with `c-is-sws', so that we
1840 ;; won't connect with the remains of a broken "ladder".
1841 (c-remove-in-sws (1- rung-pos) rung-pos))
1842 (c-put-is-sws rung-is-marked
1843 rung-pos)
1844 (c-put-in-sws rung-is-marked
1845 (1- rung-pos))
1846 (setq rung-pos rung-is-marked
1847 last-put-in-sws-pos rung-pos))
1848
1849 (c-backward-comments)
1850 (setq cmt-skip-pos (point))
a66cd3ee 1851
d9e94c22
MS
1852 (cond
1853 ((and c-opt-cpp-prefix
1854 (/= cmt-skip-pos simple-ws-beg)
1855 (c-beginning-of-macro))
1856 ;; Inside a cpp directive. See if it should be skipped over.
1857 (let ((cpp-beg (point)))
1858
1859 ;; Move back over all line continuations in the region skipped
1860 ;; over by `c-backward-comments'. If we go past it then we
1861 ;; started inside the cpp directive.
1862 (goto-char simple-ws-beg)
1863 (beginning-of-line)
1864 (while (and (> (point) cmt-skip-pos)
1865 (progn (backward-char)
1866 (eq (char-before) ?\\)))
1867 (beginning-of-line))
1868
1869 (if (< (point) cmt-skip-pos)
1870 ;; Don't move past the cpp directive if we began inside
1871 ;; it. Note that the position at the end of the last line
1872 ;; of the macro is also considered to be within it.
1873 (progn (goto-char cmt-skip-pos)
1874 nil)
1875
1876 ;; It's worthwhile to spend a little bit of effort on finding
1877 ;; the end of the macro, to get a good `simple-ws-beg'
1878 ;; position for the cache. Note that `c-backward-comments'
1879 ;; could have stepped over some comments before going into
1880 ;; the macro, and then `simple-ws-beg' must be kept on the
1881 ;; same side of those comments.
1882 (goto-char simple-ws-beg)
1883 (skip-chars-backward " \t\n\r\f\v")
1884 (if (eq (char-before) ?\\)
1885 (forward-char))
1886 (forward-line 1)
1887 (if (< (point) simple-ws-beg)
1888 ;; Might happen if comments after the macro were skipped
1889 ;; over.
1890 (setq simple-ws-beg (point)))
1891
1892 (goto-char cpp-beg)
1893 t)))
1894
1895 ((/= (save-excursion
1896 (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
1897 (setq next-rung-pos (point)))
1898 simple-ws-beg)
1899 ;; Skipped over comments. Must put point at the end of
1900 ;; the simple ws at point since we might be after a line
1901 ;; comment or cpp directive that's been partially
1902 ;; narrowed out, and we can't risk marking the simple ws
1903 ;; at the end of it.
1904 (goto-char next-rung-pos)
1905 t)))
1906
1907 ;; We've searched over a piece of non-white syntactic ws. See if this
1908 ;; can be cached.
1909 (setq next-rung-pos (point))
1910 (skip-chars-backward " \t\f\v")
1911
1912 (if (or
1913 ;; Cache if we started either from a marked rung or from a
1914 ;; completely uncached position.
1915 rung-is-marked
1916 (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
1917
1918 ;; Cache if there's a marked rung in the encountered simple ws.
1919 (save-excursion
1920 (skip-chars-backward " \t\n\r\f\v")
1921 (text-property-any (point) (min (1+ next-rung-pos) (point-max))
1922 'c-is-sws t)))
a66cd3ee 1923
d9e94c22
MS
1924 (progn
1925 (c-debug-sws-msg
1926 "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
1927 (point) (1+ next-rung-pos)
1928 simple-ws-beg (min (1+ rung-pos) (point-max))
1929 (point-min))
1930
1931 ;; Remove the properties for any nested ws that might be cached.
1932 ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
1933 ;; anyway.
1934 (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
1935 (unless (and rung-is-marked (= simple-ws-beg rung-pos))
1936 (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
1937 (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
1938 ;; Remove any `c-in-sws' property from the last char of
1939 ;; the rung before we mark it with `c-is-sws', so that we
1940 ;; won't connect with the remains of a broken "ladder".
1941 (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
1942 (c-put-is-sws simple-ws-beg
1943 rung-end-pos)
1944 (setq rung-is-marked t)))
1945 (c-put-in-sws (setq simple-ws-beg (point)
1946 last-put-in-sws-pos simple-ws-beg)
1947 rung-pos)
1948 (c-put-is-sws (setq rung-pos simple-ws-beg)
1949 (1+ next-rung-pos)))
1950
1951 (c-debug-sws-msg
1952 "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
1953 (point) (1+ next-rung-pos)
1954 simple-ws-beg (min (1+ rung-pos) (point-max))
1955 (point-min))
1956 (setq rung-pos next-rung-pos
1957 simple-ws-beg (point))
1958 ))
1959
1960 ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
1961 ;; another one before the point (which might occur when editing inside a
1962 ;; comment or macro).
1963 (when (eq last-put-in-sws-pos (point))
1964 (cond ((< (point-min) last-put-in-sws-pos)
1965 (c-debug-sws-msg
1966 "c-backward-sws clearing at %s for cache separation"
1967 (1- last-put-in-sws-pos))
1968 (c-remove-in-sws (1- last-put-in-sws-pos)
1969 last-put-in-sws-pos))
1970 ((> (point-min) 1)
1971 ;; If at bob and the buffer is narrowed, we have to clear the
1972 ;; character we're standing on instead since there might be a
1973 ;; `c-in-sws' before (point-min). In this case it's necessary
1974 ;; to clear both properties.
1975 (c-debug-sws-msg
1976 "c-backward-sws clearing thoroughly at %s for cache separation"
1977 last-put-in-sws-pos)
1978 (c-remove-is-and-in-sws last-put-in-sws-pos
1979 (1+ last-put-in-sws-pos)))))
1980 )))
785eecbb 1981
d9e94c22 1982\f
580fba94
AM
1983;; Other whitespace tools
1984(defun c-partial-ws-p (beg end)
1985 ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
1986 ;; region? This is a "heuristic" function. .....
b414f371 1987 ;;
c421028a
AM
1988 ;; The motivation for the second bit is to check whether removing this
1989 ;; region would coalesce two symbols.
580fba94
AM
1990 ;;
1991 ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
1992 ;; careful about using this function for, e.g. AWK. (2007/3/7)
1993 (save-excursion
1994 (let ((end+1 (min (1+ end) (point-max))))
1995 (or (progn (goto-char (max (point-min) (1- beg)))
1996 (c-skip-ws-forward end)
1997 (eq (point) end))
1998 (progn (goto-char beg)
1999 (c-skip-ws-forward end+1)
2000 (eq (point) end+1))))))
2001\f
0386b551 2002;; A system for finding noteworthy parens before the point.
e1c458ae 2003
0ec1d2c5
AM
2004(defconst c-state-cache-too-far 5000)
2005;; A maximum comfortable scanning distance, e.g. between
2006;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
2007;; this distance is exceeded, we take "emergency meausures", e.g. by clearing
2008;; the cache and starting again from point-min or a beginning of defun. This
2009;; value can be tuned for efficiency or set to a lower value for testing.
2010
d9e94c22
MS
2011(defvar c-state-cache nil)
2012(make-variable-buffer-local 'c-state-cache)
2013;; The state cache used by `c-parse-state' to cut down the amount of
0ec1d2c5
AM
2014;; searching. It's the result from some earlier `c-parse-state' call. See
2015;; `c-parse-state''s doc string for details of its structure.
0386b551 2016;;
d9e94c22
MS
2017;; The use of the cached info is more effective if the next
2018;; `c-parse-state' call is on a line close by the one the cached state
2019;; was made at; the cache can actually slow down a little if the
2020;; cached state was made very far back in the buffer. The cache is
2021;; most effective if `c-parse-state' is used on each line while moving
2022;; forward.
e1c458ae 2023
0386b551
AM
2024(defvar c-state-cache-good-pos 1)
2025(make-variable-buffer-local 'c-state-cache-good-pos)
0ec1d2c5
AM
2026;; This is a position where `c-state-cache' is known to be correct, or
2027;; nil (see below). It's a position inside one of the recorded unclosed
2028;; parens or the top level, but not further nested inside any literal or
2029;; subparen that is closed before the last recorded position.
0386b551
AM
2030;;
2031;; The exact position is chosen to try to be close to yet earlier than
2032;; the position where `c-state-cache' will be called next. Right now
2033;; the heuristic is to set it to the position after the last found
2034;; closing paren (of any type) before the line on which
2035;; `c-parse-state' was called. That is chosen primarily to work well
2036;; with refontification of the current line.
0ec1d2c5
AM
2037;;
2038;; 2009-07-28: When `c-state-point-min' and the last position where
2039;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
2040;; both in the same literal, there is no such "good position", and
2041;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
2042;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
2043;;
2044;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
2045;; the middle of the desert, as long as it is not within a brace pair
2046;; recorded in `c-state-cache' or a paren/bracket pair.
2047
2048
2049;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2050;; We maintain a simple cache of positions which aren't in a literal, so as to
2051;; speed up testing for non-literality.
2052(defconst c-state-nonlit-pos-interval 10000)
2053;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
2054
2055(defvar c-state-nonlit-pos-cache nil)
2056(make-variable-buffer-local 'c-state-nonlit-pos-cache)
e84efb70
AM
2057;; A list of buffer positions which are known not to be in a literal or a cpp
2058;; construct. This is ordered with higher positions at the front of the list.
2059;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
0ec1d2c5
AM
2060
2061(defvar c-state-nonlit-pos-cache-limit 1)
2062(make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
2063;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
2064;; reduced by buffer changes, and increased by invocations of
2065;; `c-state-literal-at'.
2066
2067(defsubst c-state-pp-to-literal (from to)
2068 ;; Do a parse-partial-sexp from FROM to TO, returning the bounds of any
2069 ;; literal at TO as a cons, otherwise NIL.
2070 ;; FROM must not be in a literal, and the buffer should already be wide
2071 ;; enough.
2072 (save-excursion
2073 (let ((s (parse-partial-sexp from to)))
2074 (when (or (nth 3 s) (nth 4 s)) ; in a string or comment
2075 (parse-partial-sexp (point) (point-max)
2076 nil ; TARGETDEPTH
2077 nil ; STOPBEFORE
2078 s ; OLDSTATE
2079 'syntax-table) ; stop at end of literal
2080 (cons (nth 8 s) (point))))))
2081
2082(defun c-state-literal-at (here)
2083 ;; If position HERE is inside a literal, return (START . END), the
2084 ;; boundaries of the literal (which may be outside the accessible bit of the
2085 ;; buffer). Otherwise, return nil.
2086 ;;
2087 ;; This function is almost the same as `c-literal-limits'. It differs in
2088 ;; that it is a lower level function, and that it rigourously follows the
2089 ;; syntax from BOB, whereas `c-literal-limits' uses a "local" safe position.
e84efb70
AM
2090 ;;
2091 ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
2092 ;; MAY NOT contain any positions within macros, since macros are frequently
2093 ;; turned into comments by use of the `c-cpp-delimiter' category properties.
2094 ;; We cannot rely on this mechanism whilst determining a cache pos since
2095 ;; this function is also called from outwith `c-parse-state'.
0ec1d2c5
AM
2096 (save-restriction
2097 (widen)
2098 (save-excursion
2099 (let ((c c-state-nonlit-pos-cache)
2100 pos npos lit)
2101 ;; Trim the cache to take account of buffer changes.
2102 (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
2103 (setq c (cdr c)))
2104 (setq c-state-nonlit-pos-cache c)
2105
2106 (while (and c (> (car c) here))
2107 (setq c (cdr c)))
2108 (setq pos (or (car c) (point-min)))
2109
2110 (while (<= (setq npos (+ pos c-state-nonlit-pos-interval))
2111 here)
2112 (setq lit (c-state-pp-to-literal pos npos))
2113 (setq pos (or (cdr lit) npos)) ; end of literal containing npos.
e84efb70
AM
2114 (goto-char pos)
2115 (when (and (c-beginning-of-macro) (/= (point) pos))
2116 (c-syntactic-end-of-macro)
2117 (or (eobp) (forward-char))
2118 (setq pos (point)))
0ec1d2c5
AM
2119 (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
2120
2121 (if (> pos c-state-nonlit-pos-cache-limit)
2122 (setq c-state-nonlit-pos-cache-limit pos))
2123 (if (< pos here)
2124 (setq lit (c-state-pp-to-literal pos here)))
2125 lit))))
2126
2127(defsubst c-state-lit-beg (pos)
2128 ;; Return the start of the literal containing POS, or POS itself.
2129 (or (car (c-state-literal-at pos))
2130 pos))
2131
2132(defsubst c-state-cache-non-literal-place (pos state)
9c4aeabf 2133 ;; Return a position outside of a string/comment/macro at or before POS.
0ec1d2c5 2134 ;; STATE is the parse-partial-sexp state at POS.
9c4aeabf
AM
2135 (let ((res (if (or (nth 3 state) ; in a string?
2136 (nth 4 state)) ; in a comment?
2137 (nth 8 state)
2138 pos)))
2139 (save-excursion
2140 (goto-char res)
2141 (if (c-beginning-of-macro)
2142 (point)
2143 res))))
0ec1d2c5
AM
2144
2145;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2146;; Stuff to do with point-min, and coping with any literal there.
2147(defvar c-state-point-min 1)
2148(make-variable-buffer-local 'c-state-point-min)
2149;; This is (point-min) when `c-state-cache' was last calculated. A change of
2150;; narrowing is likely to affect the parens that are visible before the point.
2151
2152(defvar c-state-point-min-lit-type nil)
2153(make-variable-buffer-local 'c-state-point-min-lit-type)
2154(defvar c-state-point-min-lit-start nil)
2155(make-variable-buffer-local 'c-state-point-min-lit-start)
2156;; These two variables define the literal, if any, containing point-min.
2157;; Their values are, respectively, 'string, c, or c++, and the start of the
2158;; literal. If there's no literal there, they're both nil.
2159
2160(defvar c-state-min-scan-pos 1)
2161(make-variable-buffer-local 'c-state-min-scan-pos)
2162;; This is the earliest buffer-pos from which scanning can be done. It is
2163;; either the end of the literal containing point-min, or point-min itself.
2164;; It becomes nil if the buffer is changed earlier than this point.
2165(defun c-state-get-min-scan-pos ()
2166 ;; Return the lowest valid scanning pos. This will be the end of the
2167 ;; literal enclosing point-min, or point-min itself.
2168 (or c-state-min-scan-pos
2169 (save-restriction
2170 (save-excursion
2171 (widen)
2172 (goto-char c-state-point-min-lit-start)
2173 (if (eq c-state-point-min-lit-type 'string)
2174 (forward-sexp)
2175 (forward-comment 1))
2176 (setq c-state-min-scan-pos (point))))))
2177
2178(defun c-state-mark-point-min-literal ()
2179 ;; Determine the properties of any literal containing POINT-MIN, setting the
2180 ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
2181 ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
2182 (let ((p-min (point-min))
2183 lit)
2184 (save-restriction
2185 (widen)
2186 (setq lit (c-state-literal-at p-min))
2187 (if lit
2188 (setq c-state-point-min-lit-type
2189 (save-excursion
2190 (goto-char (car lit))
2191 (cond
2192 ((looking-at c-block-comment-start-regexp) 'c)
2193 ((looking-at c-line-comment-starter) 'c++)
2194 (t 'string)))
2195 c-state-point-min-lit-start (car lit)
2196 c-state-min-scan-pos (cdr lit))
2197 (setq c-state-point-min-lit-type nil
2198 c-state-point-min-lit-start nil
2199 c-state-min-scan-pos p-min)))))
2200
2201
2202;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2203;; A variable which signals a brace dessert - helpful for reducing the number
2204;; of fruitless backward scans.
2205(defvar c-state-brace-pair-desert nil)
2206(make-variable-buffer-local 'c-state-brace-pair-desert)
e84efb70 2207;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
0ec1d2c5
AM
2208;; that defun has searched backwards for a brace pair and not found one. Its
2209;; value is either nil or a cons (PA . FROM), where PA is the position of the
2210;; enclosing opening paren/brace/bracket which bounds the backwards search (or
2211;; nil when at top level) and FROM is where the backward search started. It
2212;; is reset to nil in `c-invalidate-state-cache'.
2213
2214
2215;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2216;; Lowish level functions/macros which work directly on `c-state-cache', or a
2217;; list of like structure.
2218(defmacro c-state-cache-top-lparen (&optional cache)
2219 ;; Return the address of the top left brace/bracket/paren recorded in CACHE
2220 ;; (default `c-state-cache') (or nil).
2221 (let ((cash (or cache 'c-state-cache)))
2222 `(if (consp (car ,cash))
2223 (caar ,cash)
2224 (car ,cash))))
2225
2226(defmacro c-state-cache-top-paren (&optional cache)
2227 ;; Return the address of the latest brace/bracket/paren (whether left or
2228 ;; right) recorded in CACHE (default `c-state-cache') or nil.
2229 (let ((cash (or cache 'c-state-cache)))
2230 `(if (consp (car ,cash))
2231 (cdar ,cash)
2232 (car ,cash))))
2233
2234(defmacro c-state-cache-after-top-paren (&optional cache)
2235 ;; Return the position just after the latest brace/bracket/paren (whether
2236 ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
2237 (let ((cash (or cache 'c-state-cache)))
2238 `(if (consp (car ,cash))
2239 (cdar ,cash)
2240 (and (car ,cash)
2241 (1+ (car ,cash))))))
2242
2243(defun c-get-cache-scan-pos (here)
2244 ;; From the state-cache, determine the buffer position from which we might
2245 ;; scan forward to HERE to update this cache. This position will be just
2246 ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
2247 ;; return the earliest position in the accessible region which isn't within
2248 ;; a literal. If the visible portion of the buffer is entirely within a
2249 ;; literal, return NIL.
2250 (let ((c c-state-cache) elt)
2251 ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
2252 (while (and c
2253 (>= (c-state-cache-top-lparen c) here))
2254 (setq c (cdr c)))
2255
2256 (setq elt (car c))
2257 (cond
2258 ((consp elt)
2259 (if (> (cdr elt) here)
2260 (1+ (car elt))
2261 (cdr elt)))
2262 (elt (1+ elt))
2263 ((<= (c-state-get-min-scan-pos) here)
2264 (c-state-get-min-scan-pos))
2265 (t nil))))
2266
2267;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2268;; Variables which keep track of preprocessor constructs.
2269(defvar c-state-old-cpp-beg nil)
2270(make-variable-buffer-local 'c-state-old-cpp-beg)
2271(defvar c-state-old-cpp-end nil)
2272(make-variable-buffer-local 'c-state-old-cpp-end)
2273;; These are the limits of the macro containing point at the previous call of
2274;; `c-parse-state', or nil.
2275
2276;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2277;; Defuns which analyse the buffer, yet don't change `c-state-cache'.
2278(defun c-get-fallback-scan-pos (here)
2279 ;; Return a start position for building `c-state-cache' from
2280 ;; scratch. This will be at the top level, 2 defuns back.
0386b551
AM
2281 (save-excursion
2282 ;; Go back 2 bods, but ignore any bogus positions returned by
2283 ;; beginning-of-defun (i.e. open paren in column zero).
2284 (goto-char here)
2285 (let ((cnt 2))
2286 (while (not (or (bobp) (zerop cnt)))
0ec1d2c5 2287 (c-beginning-of-defun-1) ; Pure elisp BOD.
0386b551
AM
2288 (if (eq (char-after) ?\{)
2289 (setq cnt (1- cnt)))))
2290 (point)))
2291
c0209c2c
AM
2292(defun c-state-balance-parens-backwards (here- here+ top)
2293 ;; Return the position of the opening paren/brace/bracket before HERE- which
2294 ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
2295 ;; there's a macro, HERE- and HERE+ are the same. Like this:
0ec1d2c5 2296 ;;
c0209c2c
AM
2297 ;; ............................................
2298 ;; | |
2299 ;; ( [ ( .........#macro.. ) ( ) ] )
2300 ;; ^ ^ ^ ^
2301 ;; | | | |
2302 ;; return HERE- HERE+ TOP
0ec1d2c5
AM
2303 ;;
2304 ;; If there aren't enough opening paren/brace/brackets, return the position
c0209c2c
AM
2305 ;; of the outermost one found, or HERE- if there are none. If there are no
2306 ;; closeing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
2307 ;; must not be inside literals. Only the accessible portion of the buffer
2308 ;; will be scanned.
2309
2310 ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
2311 ;; `here'. Go round the next loop each time we pass over such a ")". These
2312 ;; probably match "("s before `here-'.
0ec1d2c5
AM
2313 (let (pos pa ren+1 lonely-rens)
2314 (save-excursion
2315 (save-restriction
2316 (narrow-to-region (point-min) top) ; This can move point, sometimes.
c0209c2c 2317 (setq pos here+)
0ec1d2c5
AM
2318 (c-safe
2319 (while
2320 (setq ren+1 (scan-lists pos 1 1)) ; might signal
2321 (setq lonely-rens (cons ren+1 lonely-rens)
2322 pos ren+1)))))
2323
c0209c2c 2324 ;; PART 2: Scan back before `here-' searching for the "("s
0ec1d2c5
AM
2325 ;; matching/mismatching the ")"s found above. We only need to direct the
2326 ;; caller to scan when we've encountered unmatched right parens.
c0209c2c
AM
2327 (setq pos here-)
2328 (when lonely-rens
2329 (c-safe
2330 (while
2331 (and lonely-rens ; actual values aren't used.
2332 (setq pa (scan-lists pos -1 1)))
2333 (setq pos pa)
2334 (setq lonely-rens (cdr lonely-rens)))))
2335 pos))
0ec1d2c5
AM
2336
2337(defun c-parse-state-get-strategy (here good-pos)
2338 ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
2339 ;; to minimise the amount of scanning. HERE is the pertinent position in
2340 ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
2341 ;; its head trimmed) is known to be good, or nil if there is no such
2342 ;; position.
2343 ;;
2344 ;; The return value is a list, one of the following:
2345 ;;
2346 ;; o - ('forward CACHE-POS START-POINT) - scan forward from START-POINT,
2347 ;; which is not less than CACHE-POS.
2348 ;; o - ('backward CACHE-POS nil) - scan backwards (from HERE).
2349 ;; o - ('BOD nil START-POINT) - scan forwards from START-POINT, which is at the
2350 ;; top level.
2351 ;; o - ('IN-LIT nil nil) - point is inside the literal containing point-min.
2352 ;; , where CACHE-POS is the highest position recorded in `c-state-cache' at
2353 ;; or below HERE.
2354 (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
2355 BOD-pos ; position of 2nd BOD before HERE.
2356 strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
2357 start-point
2358 how-far) ; putative scanning distance.
2359 (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
2360 (cond
2361 ((< here (c-state-get-min-scan-pos))
2362 (setq strategy 'IN-LIT
2363 start-point nil
2364 cache-pos nil
2365 how-far 0))
2366 ((<= good-pos here)
2367 (setq strategy 'forward
2368 start-point (max good-pos cache-pos)
2369 how-far (- here start-point)))
2370 ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
2371 (setq strategy 'backward
2372 how-far (- good-pos here)))
2373 (t
2374 (setq strategy 'forward
2375 how-far (- here cache-pos)
2376 start-point cache-pos)))
2377
2378 ;; Might we be better off starting from the top level, two defuns back,
2379 ;; instead?
2380 (when (> how-far c-state-cache-too-far)
2381 (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
2382 (if (< (- here BOD-pos) how-far)
2383 (setq strategy 'BOD
2384 start-point BOD-pos)))
2385
2386 (list
2387 strategy
2388 (and (memq strategy '(forward backward)) cache-pos)
2389 (and (memq strategy '(forward BOD)) start-point))))
2390
2391
2392;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2393;; Routines which change `c-state-cache' and associated values.
2394(defun c-renarrow-state-cache ()
2395 ;; The region (more precisely, point-min) has changed since we
2396 ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
2397 (if (< (point-min) c-state-point-min)
2398 ;; If point-min has MOVED BACKWARDS then we drop the state completely.
2399 ;; It would be possible to do a better job here and recalculate the top
2400 ;; only.
2401 (progn
2402 (c-state-mark-point-min-literal)
2403 (setq c-state-cache nil
2404 c-state-cache-good-pos c-state-min-scan-pos
2405 c-state-brace-pair-desert nil))
2406
2407 ;; point-min has MOVED FORWARD.
2408
2409 ;; Is the new point-min inside a (different) literal?
2410 (unless (and c-state-point-min-lit-start ; at prev. point-min
2411 (< (point-min) (c-state-get-min-scan-pos)))
2412 (c-state-mark-point-min-literal))
2413
2414 ;; Cut off a bit of the tail from `c-state-cache'.
2415 (let ((ptr (cons nil c-state-cache))
2416 pa)
2417 (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
2418 (>= pa (point-min)))
2419 (setq ptr (cdr ptr)))
2420
2421 (when (consp ptr)
2422 (if (eq (cdr ptr) c-state-cache)
2423 (setq c-state-cache nil
2424 c-state-cache-good-pos c-state-min-scan-pos)
2425 (setcdr ptr nil)
2426 (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
2427 )))
2428
2429 (setq c-state-point-min (point-min)))
2430
2431(defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim)
2432 ;; If there is a brace pair preceding FROM in the buffer (not necessarily
2433 ;; immediately preceding), push a cons onto `c-state-cache' to represent it.
2434 ;; FROM must not be inside a literal. If UPPER-LIM is non-nil, we append
2435 ;; the highest brace pair whose "}" is below UPPER-LIM.
2436 ;;
2437 ;; Return non-nil when this has been done.
2438 ;;
2439 ;; This routine should be fast. Since it can get called a LOT, we maintain
2440 ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
2441 ;; reduce the time wasted in repeated fruitless searches in brace deserts.
2442 (save-excursion
2443 (save-restriction
2444 (let ((bra from) ce ; Positions of "{" and "}".
2445 new-cons
2446 (cache-pos (c-state-cache-top-lparen)) ; might be nil.
2447 (macro-start-or-from
2448 (progn (goto-char from)
2449 (c-beginning-of-macro)
2450 (point))))
2451 (or upper-lim (setq upper-lim from))
2452
2453 ;; If we're essentially repeating a fruitless search, just give up.
2454 (unless (and c-state-brace-pair-desert
2455 (eq cache-pos (car c-state-brace-pair-desert))
2456 (<= from (cdr c-state-brace-pair-desert)))
2457 ;; Only search what we absolutely need to:
2458 (if (and c-state-brace-pair-desert
cd5495ff 2459 (eq cache-pos (car c-state-brace-pair-desert)))
0ec1d2c5
AM
2460 (narrow-to-region (cdr c-state-brace-pair-desert) (point-max)))
2461
2462 ;; In the next pair of nested loops, the inner one moves back past a
2463 ;; pair of (mis-)matching parens or brackets; the outer one moves
2464 ;; back over a sequence of unmatched close brace/paren/bracket each
2465 ;; time round.
2466 (while
2467 (progn
2468 (c-safe
2469 (while
2470 (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal
2471 (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal
2472 (or (> ce upper-lim)
2473 (not (eq (char-after bra) ?\{))
2474 (and (goto-char bra)
2475 (c-beginning-of-macro)
2476 (< (point) macro-start-or-from))))))
2477 (and ce (< ce bra)))
2478 (setq bra ce)) ; If we just backed over an unbalanced closing
2479 ; brace, ignore it.
2480
2481 (if (and ce (< bra ce) (eq (char-after bra) ?\{))
2482 ;; We've found the desired brace-pair.
2483 (progn
2484 (setq new-cons (cons bra (1+ ce)))
2485 (cond
2486 ((consp (car c-state-cache))
2487 (setcar c-state-cache new-cons))
2488 ((and (numberp (car c-state-cache)) ; probably never happens
2489 (< ce (car c-state-cache)))
2490 (setcdr c-state-cache
2491 (cons new-cons (cdr c-state-cache))))
2492 (t (setq c-state-cache (cons new-cons c-state-cache)))))
2493
2494 ;; We haven't found a brace pair. Record this.
2495 (setq c-state-brace-pair-desert (cons cache-pos from))))))))
2496
2497(defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
2498 ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
2499 ;; following a {, and that brace has a (mis-)matching } (or ]), and we
2500 ;; "push" "a" brace pair onto `c-state-cache'.
2501 ;;
2502 ;; Here "push" means overwrite the top element if it's itself a brace-pair,
2503 ;; otherwise push it normally.
2504 ;;
2505 ;; The brace pair we push is normally the one surrounding BRA+1, but if the
2506 ;; latter is inside a macro, not being a macro containing
2507 ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
2508 ;; base pair. This latter case is assumed to be rare.
2509 ;;
2510 ;; Note: POINT is not preserved in this routine.
2511 (if bra+1
2512 (if (or (> bra+1 macro-start-or-here)
2513 (progn (goto-char bra+1)
2514 (not (c-beginning-of-macro))))
2515 (setq c-state-cache
2516 (cons (cons (1- bra+1)
2517 (scan-lists bra+1 1 1))
2518 (if (consp (car c-state-cache))
2519 (cdr c-state-cache)
2520 c-state-cache)))
2521 ;; N.B. This defsubst codes one method for the simple, normal case,
2522 ;; and a more sophisticated, slower way for the general case. Don't
2523 ;; eliminate this defsubst - it's a speed optimisation.
2524 (c-append-lower-brace-pair-to-state-cache (1- bra+1)))))
2525
2526(defun c-append-to-state-cache (from)
2527 ;; Scan the buffer from FROM to (point-max), adding elements into
2528 ;; `c-state-cache' for braces etc. Return a candidate for
2529 ;; `c-state-cache-good-pos'.
2530 ;;
2531 ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
2532 ;; any. Typically, it is immediately after it. It must not be inside a
2533 ;; literal.
2534 (let ((here-bol (c-point 'bol (point-max)))
2535 (macro-start-or-here
2536 (save-excursion (goto-char (point-max))
2537 (if (c-beginning-of-macro)
2538 (point)
2539 (point-max))))
2540 pa+1 ; pos just after an opening PAren (or brace).
2541 (ren+1 from) ; usually a pos just after an closing paREN etc.
2542 ; Is actually the pos. to scan for a (/{/[ from,
2543 ; which sometimes is after a silly )/}/].
2544 paren+1 ; Pos after some opening or closing paren.
2545 paren+1s ; A list of `paren+1's; used to determine a
2546 ; good-pos.
2547 bra+1 ce+1 ; just after L/R bra-ces.
2548 bra+1s ; list of OLD values of bra+1.
2549 mstart) ; start of a macro.
2550
2551 (save-excursion
2552 ;; Each time round the following loop, we enter a succesively deeper
2553 ;; level of brace/paren nesting. (Except sometimes we "continue at
2554 ;; the existing level".) `pa+1' is a pos inside an opening
2555 ;; brace/paren/bracket, usually just after it.
2556 (while
2557 (progn
2558 ;; Each time round the next loop moves forward over an opening then
2559 ;; a closing brace/bracket/paren. This loop is white hot, so it
2560 ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
2561 ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
2562 ;; call of `scan-lists' signals an error, which happens when there
2563 ;; are no more b/b/p's to scan.
2564 (c-safe
2565 (while t
2566 (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal
2567 paren+1s (cons pa+1 paren+1s))
2568 (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
2569 (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
2570 (setq bra+1 pa+1))
2571 (setcar paren+1s ren+1)))
2572
2573 (if (and pa+1 (> pa+1 ren+1))
2574 ;; We've just entered a deeper nesting level.
2575 (progn
2576 ;; Insert the brace pair (if present) and the single open
2577 ;; paren/brace/bracket into `c-state-cache' It cannot be
2578 ;; inside a macro, except one around point, because of what
2579 ;; `c-neutralize-syntax-in-CPP' has done.
2580 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2581 ;; Insert the opening brace/bracket/paren position.
2582 (setq c-state-cache (cons (1- pa+1) c-state-cache))
2583 ;; Clear admin stuff for the next more nested part of the scan.
2584 (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
2585 t) ; Carry on the loop
2586
2587 ;; All open p/b/b's at this nesting level, if any, have probably
2588 ;; been closed by matching/mismatching ones. We're probably
2589 ;; finished - we just need to check for having found an
2590 ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
2591 ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
2592 (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control.
2593
2594 ;; Record the final, innermost, brace-pair if there is one.
2595 (c-state-push-any-brace-pair bra+1 macro-start-or-here)
2596
2597 ;; Determine a good pos
2598 (while (and (setq paren+1 (car paren+1s))
2599 (> (if (> paren+1 macro-start-or-here)
2600 paren+1
2601 (goto-char paren+1)
2602 (setq mstart (and (c-beginning-of-macro)
2603 (point)))
2604 (or mstart paren+1))
2605 here-bol))
2606 (setq paren+1s (cdr paren+1s)))
2607 (cond
2608 ((and paren+1 mstart)
2609 (min paren+1 mstart))
2610 (paren+1)
2611 (t from)))))
2612
2613(defun c-remove-stale-state-cache (good-pos pps-point)
2614 ;; Remove stale entries from the `c-cache-state', i.e. those which will
2615 ;; not be in it when it is amended for position (point-max).
2616 ;; Additionally, the "outermost" open-brace entry before (point-max)
2617 ;; will be converted to a cons if the matching close-brace is scanned.
2618 ;;
2619 ;; GOOD-POS is a "maximal" "safe position" - there must be no open
2620 ;; parens/braces/brackets between GOOD-POS and (point-max).
2621 ;;
2622 ;; As a second thing, calculate the result of parse-partial-sexp at
2623 ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that
2624 ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
2625 ;; adjust it to get outside a string/comment. (Sorry about this! The code
2626 ;; needs to be FAST).
2627 ;;
2628 ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
2629 ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
2630 ;; to be good (we aim for this to be as high as possible);
2631 ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
2632 ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
2633 ;; position to scan backwards from.
2634 ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
2635 (save-restriction
2636 (narrow-to-region 1 (point-max))
2637 (save-excursion
eb2f6eeb 2638 (let* ((in-macro-start ; start of macro containing (point-max) or nil.
0ec1d2c5
AM
2639 (save-excursion
2640 (goto-char (point-max))
2641 (and (c-beginning-of-macro)
2642 (point))))
2643 (good-pos-actual-macro-start ; Start of macro containing good-pos
2644 ; or nil
2645 (and (< good-pos (point-max))
2646 (save-excursion
2647 (goto-char good-pos)
2648 (and (c-beginning-of-macro)
2649 (point)))))
2650 (good-pos-actual-macro-end ; End of this macro, (maybe
2651 ; (point-max)), or nil.
2652 (and good-pos-actual-macro-start
2653 (save-excursion
2654 (goto-char good-pos-actual-macro-start)
2655 (c-end-of-macro)
2656 (point))))
2657 pps-state ; Will be 9 or 10 elements long.
2658 pos
2659 upper-lim ; ,beyond which `c-state-cache' entries are removed
2660 scan-back-pos
2661 pair-beg pps-point-state target-depth)
2662
2663 ;; Remove entries beyond (point-max). Also remove any entries inside
2664 ;; a macro, unless (point-max) is in the same macro.
2665 (setq upper-lim
2666 (if (or (null c-state-old-cpp-beg)
2667 (and (> (point-max) c-state-old-cpp-beg)
2668 (< (point-max) c-state-old-cpp-end)))
2669 (point-max)
2670 (min (point-max) c-state-old-cpp-beg)))
657071fc 2671 (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
0ec1d2c5
AM
2672 (setq c-state-cache (cdr c-state-cache)))
2673 ;; If `upper-lim' is inside the last recorded brace pair, remove its
2674 ;; RBrace and indicate we'll need to search backwards for a previous
2675 ;; brace pair.
2676 (when (and c-state-cache
2677 (consp (car c-state-cache))
2678 (> (cdar c-state-cache) upper-lim))
2679 (setcar c-state-cache (caar c-state-cache))
2680 (setq scan-back-pos (car c-state-cache)))
2681
2682 ;; The next loop jumps forward out of a nested level of parens each
2683 ;; time round; the corresponding elements in `c-state-cache' are
2684 ;; removed. `pos' is just after the brace-pair or the open paren at
2685 ;; (car c-state-cache). There can be no open parens/braces/brackets
2686 ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max),
2687 ;; due to the interface spec to this function.
dd21b621 2688 (setq pos (if (and good-pos-actual-macro-end
eb2f6eeb
AM
2689 (not (eq good-pos-actual-macro-start
2690 in-macro-start)))
0ec1d2c5
AM
2691 (1+ good-pos-actual-macro-end) ; get outside the macro as
2692 ; marked by a `category' text property.
2693 good-pos))
2694 (goto-char pos)
2695 (while (and c-state-cache
2696 (< (point) (point-max)))
2697 (cond
2698 ((null pps-state) ; first time through
2699 (setq target-depth -1))
2700 ((eq (car pps-state) target-depth) ; found closing ),},]
2701 (setq target-depth (1- (car pps-state))))
2702 ;; Do nothing when we've merely reached pps-point.
2703 )
2704
2705 ;; Scan!
2706 (setq pps-state
2707 (parse-partial-sexp
2708 (point) (if (< (point) pps-point) pps-point (point-max))
2709 target-depth
2710 nil pps-state))
2711
2712 (if (= (point) pps-point)
2713 (setq pps-point-state pps-state))
2714
2715 (when (eq (car pps-state) target-depth)
2716 (setq pos (point)) ; POS is now just after an R-paren/brace.
2717 (cond
2718 ((and (consp (car c-state-cache))
2719 (eq (point) (cdar c-state-cache)))
2720 ;; We've just moved out of the paren pair containing the brace-pair
2721 ;; at (car c-state-cache). `pair-beg' is where the open paren is,
2722 ;; and is potentially where the open brace of a cons in
2723 ;; c-state-cache will be.
2724 (setq pair-beg (car-safe (cdr c-state-cache))
2725 c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
2726 ((numberp (car c-state-cache))
2727 (setq pair-beg (car c-state-cache)
2728 c-state-cache (cdr c-state-cache))) ; remove this
2729 ; containing Lparen
2730 ((numberp (cadr c-state-cache))
2731 (setq pair-beg (cadr c-state-cache)
2732 c-state-cache (cddr c-state-cache))) ; Remove a paren pair
2733 ; together with enclosed brace pair.
2734 ;; (t nil) ; Ignore an unmated Rparen.
2735 )))
2736
2737 (if (< (point) pps-point)
2738 (setq pps-state (parse-partial-sexp (point) pps-point
2739 nil nil ; TARGETDEPTH, STOPBEFORE
2740 pps-state)))
2741
2742 ;; If the last paren pair we moved out of was actually a brace pair,
2743 ;; insert it into `c-state-cache'.
2744 (when (and pair-beg (eq (char-after pair-beg) ?{))
2745 (if (consp (car-safe c-state-cache))
2746 (setq c-state-cache (cdr c-state-cache)))
2747 (setq c-state-cache (cons (cons pair-beg pos)
2748 c-state-cache)))
2749
2750 (list pos scan-back-pos pps-state)))))
2751
2752(defun c-remove-stale-state-cache-backwards (here cache-pos)
2753 ;; Strip stale elements of `c-state-cache' by moving backwards through the
2754 ;; buffer, and inform the caller of the scenario detected.
2755 ;;
2756 ;; HERE is the position we're setting `c-state-cache' for.
2757 ;; CACHE-POS is just after the latest recorded position in `c-state-cache'
2758 ;; before HERE, or a position at or near point-min which isn't in a
2759 ;; literal.
2760 ;;
2761 ;; This function must only be called only when (> `c-state-cache-good-pos'
2762 ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
2763 ;; optimised to eliminate (or minimise) scanning between these two
2764 ;; positions.
2765 ;;
2766 ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
2767 ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
2768 ;; could become so after missing elements are inserted into
2769 ;; `c-state-cache'. This is JUST AFTER an opening or closing
2770 ;; brace/paren/bracket which is already in `c-state-cache' or just before
2771 ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
2772 ;; before `here''s line, or the start of the literal containing it.
2773 ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
2774 ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
2775 ;; to scan backwards from.
2776 ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
2777 ;; POS and HERE which aren't recorded in `c-state-cache'.
2778 ;;
2779 ;; The comments in this defun use "paren" to mean parenthesis or square
2780 ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
2781 ;;
2782 ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
2783 ;; | | | | | |
2784 ;; CP E here D C good
2785 (let ((pos c-state-cache-good-pos)
2786 pa ren ; positions of "(" and ")"
2787 dropped-cons ; whether the last element dropped from `c-state-cache'
2788 ; was a cons (representing a brace-pair)
2789 good-pos ; see above.
2790 lit ; (START . END) of a literal containing some point.
2791 here-lit-start here-lit-end ; bounds of literal containing `here'
2792 ; or `here' itself.
c0209c2c 2793 here- here+ ; start/end of macro around HERE, or HERE
0ec1d2c5
AM
2794 (here-bol (c-point 'bol here))
2795 (too-far-back (max (- here c-state-cache-too-far) 1)))
2796
2797 ;; Remove completely irrelevant entries from `c-state-cache'.
2798 (while (and c-state-cache
2799 (>= (setq pa (c-state-cache-top-lparen)) here))
2800 (setq dropped-cons (consp (car c-state-cache)))
2801 (setq c-state-cache (cdr c-state-cache))
2802 (setq pos pa))
2803 ;; At this stage, (> pos here);
2804 ;; (< (c-state-cache-top-lparen) here) (or is nil).
2805
c0209c2c
AM
2806 (cond
2807 ((and (consp (car c-state-cache))
2808 (> (cdar c-state-cache) here))
2809 ;; CASE 1: The top of the cache is a brace pair which now encloses
2810 ;; `here'. As good-pos, return the address. of the "{". Since we've no
2811 ;; knowledge of what's inside these braces, we have no alternative but
2812 ;; to direct the caller to scan the buffer from the opening brace.
2813 (setq pos (caar c-state-cache))
2814 (setcar c-state-cache pos)
2815 (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
2816 ; entry into a { entry, so the caller needs to
2817 ; search for a brace pair before the {.
2818
2819 ;; `here' might be inside a literal. Check for this.
2820 ((progn
2821 (setq lit (c-state-literal-at here)
2822 here-lit-start (or (car lit) here)
2823 here-lit-end (or (cdr lit) here))
2824 ;; Has `here' just "newly entered" a macro?
2825 (save-excursion
2826 (goto-char here-lit-start)
2827 (if (and (c-beginning-of-macro)
2828 (or (null c-state-old-cpp-beg)
2829 (not (= (point) c-state-old-cpp-beg))))
2830 (progn
2831 (setq here- (point))
2832 (c-end-of-macro)
2833 (setq here+ (point)))
2834 (setq here- here-lit-start
2835 here+ here-lit-end)))
2836
2837 ;; `here' might be nested inside any depth of parens (or brackets but
2838 ;; not braces). Scan backwards to find the outermost such opening
2839 ;; paren, if there is one. This will be the scan position to return.
2840 (save-restriction
2841 (narrow-to-region cache-pos (point-max))
2842 (setq pos (c-state-balance-parens-backwards here- here+ pos)))
2843 nil)) ; for the cond
2844
2845 ((< pos here-lit-start)
2846 ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
2847 ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
2848 ;; a brace pair preceding this, it will already be in `c-state-cache',
2849 ;; unless there was a brace pair after it, i.e. there'll only be one to
2850 ;; scan for if we've just deleted one.
2851 (list pos (and dropped-cons pos) t)) ; Return value.
2852
2853 ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
2854 ;; Further forward scanning isn't needed, but we still need to find a
2855 ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
2856 ((progn
0ec1d2c5
AM
2857 (save-restriction
2858 (narrow-to-region here-bol (point-max))
2859 (setq pos here-lit-start)
2860 (c-safe (while (setq pa (scan-lists pos -1 1))
2861 (setq pos pa)))) ; might signal
c0209c2c
AM
2862 nil)) ; for the cond
2863
2864 ((setq ren (c-safe-scan-lists pos -1 -1 too-far-back))
2865 ;; CASE 3: After a }/)/] before `here''s BOL.
2866 (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
2867
2868 (t
2869 ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
2870 ;; literal containing it.
2871 (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
2872 (list good-pos (and dropped-cons good-pos) nil)))))
0ec1d2c5
AM
2873
2874
2875;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2876;; Externally visible routines.
2877
2878(defun c-state-cache-init ()
2879 (setq c-state-cache nil
2880 c-state-cache-good-pos 1
2881 c-state-nonlit-pos-cache nil
2882 c-state-nonlit-pos-cache-limit 1
2883 c-state-brace-pair-desert nil
2884 c-state-point-min 1
2885 c-state-point-min-lit-type nil
2886 c-state-point-min-lit-start nil
2887 c-state-min-scan-pos 1
2888 c-state-old-cpp-beg nil
2889 c-state-old-cpp-end nil)
2890 (c-state-mark-point-min-literal))
2891
e84efb70
AM
2892;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2893;; Debugging routines to dump `c-state-cache' in a "replayable" form.
2894;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
2895;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
2896;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
2897;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
2898;; (defun c-state-dump ()
2899;; ;; For debugging.
2900;; ;(message
2901;; (concat
2902;; (c-sc-qde c-state-cache)
2903;; (c-sc-de c-state-cache-good-pos)
2904;; (c-sc-qde c-state-nonlit-pos-cache)
2905;; (c-sc-de c-state-nonlit-pos-cache-limit)
2906;; (c-sc-qde c-state-brace-pair-desert)
2907;; (c-sc-de c-state-point-min)
2908;; (c-sc-de c-state-point-min-lit-type)
2909;; (c-sc-de c-state-point-min-lit-start)
2910;; (c-sc-de c-state-min-scan-pos)
2911;; (c-sc-de c-state-old-cpp-beg)
2912;; (c-sc-de c-state-old-cpp-end)))
2913;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2914
0ec1d2c5
AM
2915(defun c-invalidate-state-cache-1 (here)
2916 ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
2917 ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
2918 ;; left in a consistent state.
2919 ;;
2920 ;; This is much like `c-whack-state-after', but it never changes a paren
2921 ;; pair element into an open paren element. Doing that would mean that the
2922 ;; new open paren wouldn't have the required preceding paren pair element.
785eecbb 2923 ;;
0ec1d2c5
AM
2924 ;; This function is called from c-after-change.
2925
2926 ;; The cache of non-literals:
2927 (if (< here c-state-nonlit-pos-cache-limit)
2928 (setq c-state-nonlit-pos-cache-limit here))
2929
2930 ;; `c-state-cache':
2931 ;; Case 1: if `here' is in a literal containing point-min, everything
2932 ;; becomes (or is already) nil.
2933 (if (or (null c-state-cache-good-pos)
2934 (< here (c-state-get-min-scan-pos)))
2935 (setq c-state-cache nil
2936 c-state-cache-good-pos nil
2937 c-state-min-scan-pos nil)
2938
2939;;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value below
2940;;; `here'. To maintain its consistency, we may need to insert a new brace
2941;;; pair.
2942 (let ((here-bol (c-point 'bol here))
2943 too-high-pa ; recorded {/(/[ next above here, or nil.
2944 dropped-cons ; was the last removed element a brace pair?
2945 pa)
2946 ;; The easy bit - knock over-the-top bits off `c-state-cache'.
2947 (while (and c-state-cache
2948 (>= (setq pa (c-state-cache-top-paren)) here))
2949 (setq dropped-cons (consp (car c-state-cache))
2950 too-high-pa (c-state-cache-top-lparen)
2951 c-state-cache (cdr c-state-cache)))
2952
2953 ;; Do we need to add in an earlier brace pair, having lopped one off?
2954 (if (and dropped-cons
2955 (< too-high-pa (+ here c-state-cache-too-far)))
2956 (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol))
2957 (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
2958 (c-state-get-min-scan-pos)))))
2959
2960 ;; The brace-pair desert marker:
2961 (when (car c-state-brace-pair-desert)
2962 (if (< here (car c-state-brace-pair-desert))
2963 (setq c-state-brace-pair-desert nil)
2964 (if (< here (cdr c-state-brace-pair-desert))
2965 (setcdr c-state-brace-pair-desert here)))))
2966
2967(defun c-parse-state-1 ()
2968 ;; Find and record all noteworthy parens between some good point earlier in
2969 ;; the file and point. That good point is at least the beginning of the
2970 ;; top-level construct we are in, or the beginning of the preceding
2971 ;; top-level construct if we aren't in one.
2972 ;;
2973 ;; The returned value is a list of the noteworthy parens with the last one
2974 ;; first. If an element in the list is an integer, it's the position of an
2975 ;; open paren (of any type) which has not been closed before the point. If
2976 ;; an element is a cons, it gives the position of a closed BRACE paren
2977 ;; pair[*]; the car is the start brace position and the cdr is the position
2978 ;; following the closing brace. Only the last closed brace paren pair
2979 ;; before each open paren and before the point is recorded, and thus the
2980 ;; state never contains two cons elements in succession. When a close brace
2981 ;; has no matching open brace (e.g., the matching brace is outside the
2982 ;; visible region), it is not represented in the returned value.
2983 ;;
2984 ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
2985 ;; This defun explicitly treats mismatching parens/braces/brackets as
2986 ;; matching. It is the open brace which makes it a "brace" pair.
2987 ;;
2988 ;; If POINT is within a macro, open parens and brace pairs within
2989 ;; THIS macro MIGHT be recorded. This depends on whether their
2990 ;; syntactic properties have been suppressed by
2991 ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
d9e94c22
MS
2992 ;;
2993 ;; Currently no characters which are given paren syntax with the
2994 ;; syntax-table property are recorded, i.e. angle bracket arglist
2995 ;; parens are never present here. Note that this might change.
2996 ;;
0386b551 2997 ;; BUG: This function doesn't cope entirely well with unbalanced
0ec1d2c5
AM
2998 ;; parens in macros. (2008-12-11: this has probably been resolved
2999 ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
3000 ;; following case the brace before the macro isn't balanced with the
3001 ;; one after it:
0386b551
AM
3002 ;;
3003 ;; {
3004 ;; #define X {
3005 ;; }
3006 ;;
0ec1d2c5
AM
3007 ;; Note to maintainers: this function DOES get called with point
3008 ;; within comments and strings, so don't assume it doesn't!
3009 ;;
0386b551 3010 ;; This function might do hidden buffer changes.
0ec1d2c5
AM
3011 (let* ((here (point))
3012 (here-bopl (c-point 'bopl))
3013 strategy ; 'forward, 'backward etc..
3014 ;; Candidate positions to start scanning from:
3015 cache-pos ; highest position below HERE already existing in
3016 ; cache (or 1).
3017 good-pos
3018 start-point
3019 bopl-state
3020 res
3021 scan-backward-pos scan-forward-p) ; used for 'backward.
3022 ;; If POINT-MIN has changed, adjust the cache
3023 (unless (= (point-min) c-state-point-min)
3024 (c-renarrow-state-cache))
3025
3026 ;; Strategy?
3027 (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
3028 strategy (car res)
3029 cache-pos (cadr res)
3030 start-point (nth 2 res))
3031
3032 (when (eq strategy 'BOD)
3033 (setq c-state-cache nil
3034 c-state-cache-good-pos start-point))
3035
3036 ;; SCAN!
3037 (save-restriction
3038 (cond
3039 ((memq strategy '(forward BOD))
0386b551 3040 (narrow-to-region (point-min) here)
0ec1d2c5
AM
3041 (setq res (c-remove-stale-state-cache start-point here-bopl))
3042 (setq cache-pos (car res)
3043 scan-backward-pos (cadr res)
3044 bopl-state (car (cddr res))) ; will be nil if (< here-bopl
3045 ; start-point)
3046 (if scan-backward-pos
3047 (c-append-lower-brace-pair-to-state-cache scan-backward-pos))
3048 (setq good-pos
3049 (c-append-to-state-cache cache-pos))
3050 (setq c-state-cache-good-pos
3051 (if (and bopl-state
3052 (< good-pos (- here c-state-cache-too-far)))
3053 (c-state-cache-non-literal-place here-bopl bopl-state)
3054 good-pos)))
3055
3056 ((eq strategy 'backward)
3057 (setq res (c-remove-stale-state-cache-backwards here cache-pos)
3058 good-pos (car res)
3059 scan-backward-pos (cadr res)
3060 scan-forward-p (car (cddr res)))
3061 (if scan-backward-pos
3062 (c-append-lower-brace-pair-to-state-cache
3063 scan-backward-pos))
3064 (setq c-state-cache-good-pos
3065 (if scan-forward-p
3066 (progn (narrow-to-region (point-min) here)
3067 (c-append-to-state-cache good-pos))
3068
3069 (c-get-cache-scan-pos good-pos))))
3070
3071 (t ; (eq strategy 'IN-LIT)
3072 (setq c-state-cache nil
3073 c-state-cache-good-pos nil)))))
3074
3075 c-state-cache)
3076
3077(defun c-invalidate-state-cache (here)
3078 ;; This is a wrapper over `c-invalidate-state-cache-1'.
3079 ;;
3080 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3081 ;; of all parens in preprocessor constructs, except for any such construct
3082 ;; containing point. We can then call `c-invalidate-state-cache-1' without
3083 ;; worrying further about macros and template delimiters.
3084 (c-with-<->-as-parens-suppressed
3e8f7d91
AM
3085 (if (and c-state-old-cpp-beg
3086 (< c-state-old-cpp-beg here))
0ec1d2c5 3087 (c-with-all-but-one-cpps-commented-out
3e8f7d91
AM
3088 c-state-old-cpp-beg
3089 (min c-state-old-cpp-end here)
0ec1d2c5
AM
3090 (c-invalidate-state-cache-1 here))
3091 (c-with-cpps-commented-out
3092 (c-invalidate-state-cache-1 here)))))
3093
3094(defun c-parse-state ()
3095 ;; This is a wrapper over `c-parse-state-1'. See that function for a
3096 ;; description of the functionality and return value.
3097 ;;
3098 ;; It suppresses the syntactic effect of the < and > (template) brackets and
3099 ;; of all parens in preprocessor constructs, except for any such construct
3100 ;; containing point. We can then call `c-parse-state-1' without worrying
3101 ;; further about macros and template delimiters.
3102 (let (here-cpp-beg here-cpp-end)
3103 (save-excursion
3104 (when (c-beginning-of-macro)
3105 (setq here-cpp-beg (point))
3106 (unless
3107 (> (setq here-cpp-end (c-syntactic-end-of-macro))
3108 here-cpp-beg)
3109 (setq here-cpp-beg nil here-cpp-end nil))))
3110 ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
3111 ;; subsystem.
3112 (prog1
3113 (c-with-<->-as-parens-suppressed
3114 (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
3115 (c-with-all-but-one-cpps-commented-out
3116 here-cpp-beg here-cpp-end
3117 (c-parse-state-1))
3118 (c-with-cpps-commented-out
3119 (c-parse-state-1))))
3e8f7d91
AM
3120 (setq c-state-old-cpp-beg (and here-cpp-beg (copy-marker here-cpp-beg t))
3121 c-state-old-cpp-end (and here-cpp-end (copy-marker here-cpp-end t)))
3122 )))
0ec1d2c5
AM
3123
3124;; Debug tool to catch cache inconsistencies. This is called from
3125;; 000tests.el.
a66cd3ee
MS
3126(defvar c-debug-parse-state nil)
3127(unless (fboundp 'c-real-parse-state)
3128 (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
3129(cc-bytecomp-defun c-real-parse-state)
cd5495ff
AM
3130
3131(defvar c-parse-state-state nil)
3132(defun c-record-parse-state-state ()
3133 (setq c-parse-state-state
3134 (mapcar
3135 (lambda (arg)
3136 (cons arg (symbol-value arg)))
3137 '(c-state-cache
3138 c-state-cache-good-pos
3139 c-state-nonlit-pos-cache
3140 c-state-nonlit-pos-cache-limit
3141 c-state-brace-pair-desert
3142 c-state-point-min
3143 c-state-point-min-lit-type
3144 c-state-point-min-lit-start
3145 c-state-min-scan-pos
3146 c-state-old-cpp-beg
3147 c-state-old-cpp-end))))
3148(defun c-replay-parse-state-state ()
3149 (message
3150 (concat "(setq "
3151 (mapconcat
3152 (lambda (arg)
3153 (format "%s %s%s" (car arg) (if (atom (cdr arg)) "" "'") (cdr arg)))
3154 c-parse-state-state " ")
3155 ")")))
3156
a66cd3ee 3157(defun c-debug-parse-state ()
0ec1d2c5 3158 (let ((here (point)) (res1 (c-real-parse-state)) res2)
0386b551 3159 (let ((c-state-cache nil)
0ec1d2c5
AM
3160 (c-state-cache-good-pos 1)
3161 (c-state-nonlit-pos-cache nil)
3162 (c-state-nonlit-pos-cache-limit 1)
3163 (c-state-brace-pair-desert nil)
3164 (c-state-point-min 1)
3165 (c-state-point-min-lit-type nil)
3166 (c-state-point-min-lit-start nil)
3167 (c-state-min-scan-pos 1)
3168 (c-state-old-cpp-beg nil)
3169 (c-state-old-cpp-end nil))
a66cd3ee
MS
3170 (setq res2 (c-real-parse-state)))
3171 (unless (equal res1 res2)
0386b551
AM
3172 ;; The cache can actually go further back due to the ad-hoc way
3173 ;; the first paren is found, so try to whack off a bit of its
3174 ;; start before complaining.
cd5495ff
AM
3175 ;; (save-excursion
3176 ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
3177 ;; (c-beginning-of-defun-1)
3178 ;; (while (not (or (bobp) (eq (char-after) ?{)))
3179 ;; (c-beginning-of-defun-1))
3180 ;; (unless (equal (c-whack-state-before (point) res1) res2)
3181 ;; (message (concat "c-parse-state inconsistency at %s: "
3182 ;; "using cache: %s, from scratch: %s")
3183 ;; here res1 res2)))
3184 (message (concat "c-parse-state inconsistency at %s: "
3185 "using cache: %s, from scratch: %s")
3186 here res1 res2)
3187 (message "Old state:")
3188 (c-replay-parse-state-state))
3189 (c-record-parse-state-state)
a66cd3ee 3190 res1))
0ec1d2c5 3191
a66cd3ee
MS
3192(defun c-toggle-parse-state-debug (&optional arg)
3193 (interactive "P")
3194 (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
3195 (fset 'c-parse-state (symbol-function (if c-debug-parse-state
3196 'c-debug-parse-state
3197 'c-real-parse-state)))
3198 (c-keep-region-active))
0386b551
AM
3199(when c-debug-parse-state
3200 (c-toggle-parse-state-debug 1))
a66cd3ee 3201
0ec1d2c5 3202\f
d9e94c22
MS
3203(defun c-whack-state-before (bufpos paren-state)
3204 ;; Whack off any state information from PAREN-STATE which lies
3205 ;; before BUFPOS. Not destructive on PAREN-STATE.
d9e94c22
MS
3206 (let* ((newstate (list nil))
3207 (ptr newstate)
3208 car)
3209 (while paren-state
3210 (setq car (car paren-state)
3211 paren-state (cdr paren-state))
3212 (if (< (if (consp car) (car car) car) bufpos)
3213 (setq paren-state nil)
3214 (setcdr ptr (list car))
3215 (setq ptr (cdr ptr))))
3216 (cdr newstate)))
3217
3218(defun c-whack-state-after (bufpos paren-state)
3219 ;; Whack off any state information from PAREN-STATE which lies at or
3220 ;; after BUFPOS. Not destructive on PAREN-STATE.
d9e94c22
MS
3221 (catch 'done
3222 (while paren-state
3223 (let ((car (car paren-state)))
3224 (if (consp car)
3225 ;; just check the car, because in a balanced brace
3226 ;; expression, it must be impossible for the corresponding
3227 ;; close brace to be before point, but the open brace to
3228 ;; be after.
3229 (if (<= bufpos (car car))
3230 nil ; whack it off
3231 (if (< bufpos (cdr car))
3232 ;; its possible that the open brace is before
3233 ;; bufpos, but the close brace is after. In that
3234 ;; case, convert this to a non-cons element. The
3235 ;; rest of the state is before bufpos, so we're
3236 ;; done.
3237 (throw 'done (cons (car car) (cdr paren-state)))
3238 ;; we know that both the open and close braces are
3239 ;; before bufpos, so we also know that everything else
3240 ;; on state is before bufpos.
3241 (throw 'done paren-state)))
3242 (if (<= bufpos car)
3243 nil ; whack it off
3244 ;; it's before bufpos, so everything else should too.
3245 (throw 'done paren-state)))
3246 (setq paren-state (cdr paren-state)))
3247 nil)))
3248
3249(defun c-most-enclosing-brace (paren-state &optional bufpos)
3250 ;; Return the bufpos of the innermost enclosing open paren before
0386b551 3251 ;; bufpos, or nil if none was found.
d9e94c22
MS
3252 (let (enclosingp)
3253 (or bufpos (setq bufpos 134217727))
3254 (while paren-state
3255 (setq enclosingp (car paren-state)
3256 paren-state (cdr paren-state))
3257 (if (or (consp enclosingp)
3258 (>= enclosingp bufpos))
3259 (setq enclosingp nil)
d9e94c22
MS
3260 (setq paren-state nil)))
3261 enclosingp))
3262
0386b551
AM
3263(defun c-least-enclosing-brace (paren-state)
3264 ;; Return the bufpos of the outermost enclosing open paren, or nil
3265 ;; if none was found.
d9e94c22 3266 (let (pos elem)
d9e94c22
MS
3267 (while paren-state
3268 (setq elem (car paren-state)
3269 paren-state (cdr paren-state))
0386b551
AM
3270 (if (integerp elem)
3271 (setq pos elem)))
d9e94c22
MS
3272 pos))
3273
3274(defun c-safe-position (bufpos paren-state)
0386b551
AM
3275 ;; Return the closest "safe" position recorded on PAREN-STATE that
3276 ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
3277 ;; contain any. Return nil if BUFPOS is nil, which is useful to
3278 ;; find the closest limit before a given limit that might be nil.
d9e94c22 3279 ;;
0386b551
AM
3280 ;; A "safe" position is a position at or after a recorded open
3281 ;; paren, or after a recorded close paren. The returned position is
3282 ;; thus either the first position after a close brace, or the first
3283 ;; position after an enclosing paren, or at the enclosing paren in
3284 ;; case BUFPOS is immediately after it.
d9e94c22
MS
3285 (when bufpos
3286 (let (elem)
3287 (catch 'done
3288 (while paren-state
3289 (setq elem (car paren-state))
3290 (if (consp elem)
3291 (cond ((< (cdr elem) bufpos)
3292 (throw 'done (cdr elem)))
3293 ((< (car elem) bufpos)
3294 ;; See below.
3295 (throw 'done (min (1+ (car elem)) bufpos))))
3296 (if (< elem bufpos)
3297 ;; elem is the position at and not after the opening paren, so
3298 ;; we can go forward one more step unless it's equal to
3299 ;; bufpos. This is useful in some cases avoid an extra paren
3300 ;; level between the safe position and bufpos.
3301 (throw 'done (min (1+ elem) bufpos))))
3302 (setq paren-state (cdr paren-state)))))))
3303
3304(defun c-beginning-of-syntax ()
3305 ;; This is used for `font-lock-beginning-of-syntax-function'. It
3306 ;; goes to the closest previous point that is known to be outside
3307 ;; any string literal or comment. `c-state-cache' is used if it has
3308 ;; a position in the vicinity.
3309 (let* ((paren-state c-state-cache)
3310 elem
3311
3312 (pos (catch 'done
3313 ;; Note: Similar code in `c-safe-position'. The
3314 ;; difference is that we accept a safe position at
3315 ;; the point and don't bother to go forward past open
3316 ;; parens.
3317 (while paren-state
3318 (setq elem (car paren-state))
3319 (if (consp elem)
3320 (cond ((<= (cdr elem) (point))
3321 (throw 'done (cdr elem)))
3322 ((<= (car elem) (point))
3323 (throw 'done (car elem))))
3324 (if (<= elem (point))
3325 (throw 'done elem)))
3326 (setq paren-state (cdr paren-state)))
3327 (point-min))))
3328
3329 (if (> pos (- (point) 4000))
3330 (goto-char pos)
3331 ;; The position is far back. Try `c-beginning-of-defun-1'
3332 ;; (although we can't be entirely sure it will go to a position
3333 ;; outside a comment or string in current emacsen). FIXME:
3334 ;; Consult `syntax-ppss' here.
3335 (c-beginning-of-defun-1)
3336 (if (< (point) pos)
3337 (goto-char pos)))))
3338
3339\f
3340;; Tools for scanning identifiers and other tokens.
3341
3342(defun c-on-identifier ()
3343 "Return non-nil if the point is on or directly after an identifier.
3344Keywords are recognized and not considered identifiers. If an
3345identifier is detected, the returned value is its starting position.
0386b551
AM
3346If an identifier ends at the point and another begins at it \(can only
3347happen in Pike) then the point for the preceding one is returned.
d9e94c22 3348
0386b551
AM
3349Note that this function might do hidden buffer changes. See the
3350comment at the start of cc-engine.el for more info."
3351
3352 ;; FIXME: Shouldn't this function handle "operator" in C++?
d9e94c22
MS
3353
3354 (save-excursion
0386b551
AM
3355 (skip-syntax-backward "w_")
3356
3357 (or
3358
3359 ;; Check for a normal (non-keyword) identifier.
3360 (and (looking-at c-symbol-start)
3361 (not (looking-at c-keywords-regexp))
3362 (point))
3363
3364 (when (c-major-mode-is 'pike-mode)
3365 ;; Handle the `<operator> syntax in Pike.
3366 (let ((pos (point)))
3367 (skip-chars-backward "-!%&*+/<=>^|~[]()")
3368 (and (if (< (skip-chars-backward "`") 0)
3369 t
3370 (goto-char pos)
3371 (eq (char-after) ?\`))
3372 (looking-at c-symbol-key)
3373 (>= (match-end 0) pos)
3374 (point))))
3375
3376 ;; Handle the "operator +" syntax in C++.
3377 (when (and c-overloadable-operators-regexp
3378 (= (c-backward-token-2 0) 0))
3379
3380 (cond ((and (looking-at c-overloadable-operators-regexp)
51c9af45 3381 (or (not c-opt-op-identifier-prefix)
0386b551 3382 (and (= (c-backward-token-2 1) 0)
51c9af45 3383 (looking-at c-opt-op-identifier-prefix))))
0386b551
AM
3384 (point))
3385
3386 ((save-excursion
51c9af45
AM
3387 (and c-opt-op-identifier-prefix
3388 (looking-at c-opt-op-identifier-prefix)
0386b551
AM
3389 (= (c-forward-token-2 1) 0)
3390 (looking-at c-overloadable-operators-regexp)))
3391 (point))))
3392
3393 )))
d9e94c22
MS
3394
3395(defsubst c-simple-skip-symbol-backward ()
3396 ;; If the point is at the end of a symbol then skip backward to the
3397 ;; beginning of it. Don't move otherwise. Return non-nil if point
3398 ;; moved.
0386b551
AM
3399 ;;
3400 ;; This function might do hidden buffer changes.
d9e94c22
MS
3401 (or (< (skip-syntax-backward "w_") 0)
3402 (and (c-major-mode-is 'pike-mode)
3403 ;; Handle the `<operator> syntax in Pike.
3404 (let ((pos (point)))
2a15eb73 3405 (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
d9e94c22
MS
3406 (< (skip-chars-backward "`") 0)
3407 (looking-at c-symbol-key)
3408 (>= (match-end 0) pos))
3409 t
3410 (goto-char pos)
3411 nil)))))
3412
0386b551 3413(defun c-beginning-of-current-token (&optional back-limit)
d9e94c22
MS
3414 ;; Move to the beginning of the current token. Do not move if not
3415 ;; in the middle of one. BACK-LIMIT may be used to bound the
3416 ;; backward search; if given it's assumed to be at the boundary
a85fd6da 3417 ;; between two tokens. Return non-nil if the point is moved, nil
580fba94 3418 ;; otherwise.
0386b551
AM
3419 ;;
3420 ;; This function might do hidden buffer changes.
d9e94c22 3421 (let ((start (point)))
580fba94
AM
3422 (if (looking-at "\\w\\|\\s_")
3423 (skip-syntax-backward "w_" back-limit)
3424 (when (< (skip-syntax-backward ".()" back-limit) 0)
3425 (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
3426 (match-end 0))
3427 ;; `c-nonsymbol-token-regexp' should always match
3428 ;; since we've skipped backward over punctuator
3429 ;; or paren syntax, but consume one char in case
3430 ;; it doesn't so that we don't leave point before
3431 ;; some earlier incorrect token.
3432 (1+ (point)))))
3433 (if (<= pos start)
3434 (goto-char pos))))))
3435 (< (point) start)))
d9e94c22 3436
ff959bab 3437(defun c-end-of-current-token (&optional back-limit)
d9e94c22
MS
3438 ;; Move to the end of the current token. Do not move if not in the
3439 ;; middle of one. BACK-LIMIT may be used to bound the backward
3440 ;; search; if given it's assumed to be at the boundary between two
ff959bab 3441 ;; tokens. Return non-nil if the point is moved, nil otherwise.
0386b551
AM
3442 ;;
3443 ;; This function might do hidden buffer changes.
d9e94c22
MS
3444 (let ((start (point)))
3445 (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
3446 (skip-syntax-forward "w_"))
3447 ((< (skip-syntax-backward ".()" back-limit) 0)
3448 (while (progn
3449 (if (looking-at c-nonsymbol-token-regexp)
3450 (goto-char (match-end 0))
3451 ;; `c-nonsymbol-token-regexp' should always match since
3452 ;; we've skipped backward over punctuator or paren
3453 ;; syntax, but move forward in case it doesn't so that
3454 ;; we don't leave point earlier than we started with.
3455 (forward-char))
ff959bab
MS
3456 (< (point) start)))))
3457 (> (point) start)))
d9e94c22
MS
3458
3459(defconst c-jump-syntax-balanced
3460 (if (memq 'gen-string-delim c-emacs-features)
3461 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
3462 "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
3463
3464(defconst c-jump-syntax-unbalanced
3465 (if (memq 'gen-string-delim c-emacs-features)
3466 "\\w\\|\\s_\\|\\s\"\\|\\s|"
3467 "\\w\\|\\s_\\|\\s\""))
3468
3469(defun c-forward-token-2 (&optional count balanced limit)
3470 "Move forward by tokens.
3471A token is defined as all symbols and identifiers which aren't
3472syntactic whitespace \(note that multicharacter tokens like \"==\" are
3473treated properly). Point is always either left at the beginning of a
3474token or not moved at all. COUNT specifies the number of tokens to
3475move; a negative COUNT moves in the opposite direction. A COUNT of 0
3476moves to the next token beginning only if not already at one. If
3477BALANCED is true, move over balanced parens, otherwise move into them.
3478Also, if BALANCED is true, never move out of an enclosing paren.
3479
3480LIMIT sets the limit for the movement and defaults to the point limit.
3481The case when LIMIT is set in the middle of a token, comment or macro
3482is handled correctly, i.e. the point won't be left there.
3483
3484Return the number of tokens left to move \(positive or negative). If
3485BALANCED is true, a move over a balanced paren counts as one. Note
3486that if COUNT is 0 and no appropriate token beginning is found, 1 will
3487be returned. Thus, a return value of 0 guarantees that point is at
3488the requested position and a return value less \(without signs) than
0386b551
AM
3489COUNT guarantees that point is at the beginning of some token.
3490
3491Note that this function might do hidden buffer changes. See the
3492comment at the start of cc-engine.el for more info."
d9e94c22
MS
3493
3494 (or count (setq count 1))
3495 (if (< count 0)
3496 (- (c-backward-token-2 (- count) balanced limit))
3497
3498 (let ((jump-syntax (if balanced
3499 c-jump-syntax-balanced
3500 c-jump-syntax-unbalanced))
3501 (last (point))
3502 (prev (point)))
3503
3504 (if (zerop count)
3505 ;; If count is zero we should jump if in the middle of a token.
3506 (c-end-of-current-token))
3507
3508 (save-restriction
3509 (if limit (narrow-to-region (point-min) limit))
3510 (if (/= (point)
3511 (progn (c-forward-syntactic-ws) (point)))
3512 ;; Skip whitespace. Count this as a move if we did in
3513 ;; fact move.
3514 (setq count (max (1- count) 0)))
3515
3516 (if (eobp)
3517 ;; Moved out of bounds. Make sure the returned count isn't zero.
3518 (progn
3519 (if (zerop count) (setq count 1))
3520 (goto-char last))
3521
3522 ;; Use `condition-case' to avoid having the limit tests
3523 ;; inside the loop.
3524 (condition-case nil
3525 (while (and
3526 (> count 0)
3527 (progn
3528 (setq last (point))
3529 (cond ((looking-at jump-syntax)
3530 (goto-char (scan-sexps (point) 1))
3531 t)
3532 ((looking-at c-nonsymbol-token-regexp)
3533 (goto-char (match-end 0))
3534 t)
3535 ;; `c-nonsymbol-token-regexp' above should always
3536 ;; match if there are correct tokens. Try to
3537 ;; widen to see if the limit was set in the
3538 ;; middle of one, else fall back to treating
3539 ;; the offending thing as a one character token.
3540 ((and limit
3541 (save-restriction
3542 (widen)
3543 (looking-at c-nonsymbol-token-regexp)))
3544 nil)
3545 (t
3546 (forward-char)
3547 t))))
3548 (c-forward-syntactic-ws)
3549 (setq prev last
3550 count (1- count)))
3551 (error (goto-char last)))
3552
3553 (when (eobp)
3554 (goto-char prev)
3555 (setq count (1+ count)))))
3556
3557 count)))
3558
3559(defun c-backward-token-2 (&optional count balanced limit)
3560 "Move backward by tokens.
3561See `c-forward-token-2' for details."
3562
3563 (or count (setq count 1))
3564 (if (< count 0)
3565 (- (c-forward-token-2 (- count) balanced limit))
3566
3567 (or limit (setq limit (point-min)))
3568 (let ((jump-syntax (if balanced
3569 c-jump-syntax-balanced
3570 c-jump-syntax-unbalanced))
3571 (last (point)))
3572
3573 (if (zerop count)
3574 ;; The count is zero so try to skip to the beginning of the
3575 ;; current token.
3576 (if (> (point)
3577 (progn (c-beginning-of-current-token) (point)))
3578 (if (< (point) limit)
3579 ;; The limit is inside the same token, so return 1.
3580 (setq count 1))
3581
3582 ;; We're not in the middle of a token. If there's
3583 ;; whitespace after the point then we must move backward,
3584 ;; so set count to 1 in that case.
3585 (and (looking-at c-syntactic-ws-start)
3586 ;; If we're looking at a '#' that might start a cpp
3587 ;; directive then we have to do a more elaborate check.
3588 (or (/= (char-after) ?#)
3589 (not c-opt-cpp-prefix)
3590 (save-excursion
3591 (and (= (point)
3592 (progn (beginning-of-line)
3593 (looking-at "[ \t]*")
3594 (match-end 0)))
3595 (or (bobp)
3596 (progn (backward-char)
3597 (not (eq (char-before) ?\\)))))))
3598 (setq count 1))))
3599
3600 ;; Use `condition-case' to avoid having to check for buffer
3601 ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
3602 (condition-case nil
3603 (while (and
3604 (> count 0)
3605 (progn
3606 (c-backward-syntactic-ws)
3607 (backward-char)
3608 (if (looking-at jump-syntax)
3609 (goto-char (scan-sexps (1+ (point)) -1))
3610 ;; This can be very inefficient if there's a long
3611 ;; sequence of operator tokens without any separation.
3612 ;; That doesn't happen in practice, anyway.
3613 (c-beginning-of-current-token))
3614 (>= (point) limit)))
3615 (setq last (point)
3616 count (1- count)))
3617 (error (goto-char last)))
3618
3619 (if (< (point) limit)
3620 (goto-char last))
3621
3622 count)))
3623
3624(defun c-forward-token-1 (&optional count balanced limit)
3625 "Like `c-forward-token-2' but doesn't treat multicharacter operator
3626tokens like \"==\" as single tokens, i.e. all sequences of symbol
3627characters are jumped over character by character. This function is
3628for compatibility only; it's only a wrapper over `c-forward-token-2'."
3629 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3630 (c-forward-token-2 count balanced limit)))
3631
3632(defun c-backward-token-1 (&optional count balanced limit)
3633 "Like `c-backward-token-2' but doesn't treat multicharacter operator
3634tokens like \"==\" as single tokens, i.e. all sequences of symbol
3635characters are jumped over character by character. This function is
3636for compatibility only; it's only a wrapper over `c-backward-token-2'."
3637 (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
3638 (c-backward-token-2 count balanced limit)))
3639
3640\f
3641;; Tools for doing searches restricted to syntactically relevant text.
3642
3643(defun c-syntactic-re-search-forward (regexp &optional bound noerror
3644 paren-level not-inside-token
3645 lookbehind-submatch)
3646 "Like `re-search-forward', but only report matches that are found
3647in syntactically significant text. I.e. matches in comments, macros
3648or string literals are ignored. The start point is assumed to be
3649outside any comment, macro or string literal, or else the content of
3650that region is taken as syntactically significant text.
3651
3652If PAREN-LEVEL is non-nil, an additional restriction is added to
2a15eb73
MS
3653ignore matches in nested paren sexps. The search will also not go
3654outside the current list sexp, which has the effect that if the point
3655should be moved to BOUND when no match is found \(i.e. NOERROR is
3656neither nil nor t), then it will be at the closing paren if the end of
3657the current list sexp is encountered first.
d9e94c22
MS
3658
3659If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
3660ignored. Things like multicharacter operators and special symbols
3661\(e.g. \"`()\" in Pike) are handled but currently not floating point
3662constants.
3663
3664If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
3665subexpression in REGEXP. The end of that submatch is used as the
3666position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
3667isn't used or if that subexpression didn't match then the start
3668position of the whole match is used instead. The \"look behind\"
3669subexpression is never tested before the starting position, so it
3670might be a good idea to include \\=\\= as a match alternative in it.
3671
3672Optimization note: Matches might be missed if the \"look behind\"
2a15eb73 3673subexpression can match the end of nonwhite syntactic whitespace,
d9e94c22 3674i.e. the end of comments or cpp directives. This since the function
2a15eb73
MS
3675skips over such things before resuming the search. It's on the other
3676hand not safe to assume that the \"look behind\" subexpression never
3677matches syntactic whitespace.
3678
3679Bug: Unbalanced parens inside cpp directives are currently not handled
3680correctly \(i.e. they don't get ignored as they should) when
0386b551
AM
3681PAREN-LEVEL is set.
3682
3683Note that this function might do hidden buffer changes. See the
3684comment at the start of cc-engine.el for more info."
d9e94c22
MS
3685
3686 (or bound (setq bound (point-max)))
3687 (if paren-level (setq paren-level -1))
3688
3689 ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
3690
3691 (let ((start (point))
2a15eb73
MS
3692 tmp
3693 ;; Start position for the last search.
3694 search-pos
3695 ;; The `parse-partial-sexp' state between the start position
3696 ;; and the point.
3697 state
3698 ;; The current position after the last state update. The next
3699 ;; `parse-partial-sexp' continues from here.
3700 (state-pos (point))
3701 ;; The position at which to check the state and the state
3702 ;; there. This is separate from `state-pos' since we might
3703 ;; need to back up before doing the next search round.
3704 check-pos check-state
3705 ;; Last position known to end a token.
d9e94c22 3706 (last-token-end-pos (point-min))
2a15eb73
MS
3707 ;; Set when a valid match is found.
3708 found)
d9e94c22
MS
3709
3710 (condition-case err
3711 (while
3712 (and
2a15eb73
MS
3713 (progn
3714 (setq search-pos (point))
3715 (re-search-forward regexp bound noerror))
d9e94c22
MS
3716
3717 (progn
2a15eb73
MS
3718 (setq state (parse-partial-sexp
3719 state-pos (match-beginning 0) paren-level nil state)
3720 state-pos (point))
d9e94c22 3721 (if (setq check-pos (and lookbehind-submatch
2a15eb73
MS
3722 (or (not paren-level)
3723 (>= (car state) 0))
d9e94c22
MS
3724 (match-end lookbehind-submatch)))
3725 (setq check-state (parse-partial-sexp
2a15eb73
MS
3726 state-pos check-pos paren-level nil state))
3727 (setq check-pos state-pos
d9e94c22
MS
3728 check-state state))
3729
2a15eb73
MS
3730 ;; NOTE: If we got a look behind subexpression and get
3731 ;; an insignificant match in something that isn't
d9e94c22
MS
3732 ;; syntactic whitespace (i.e. strings or in nested
3733 ;; parentheses), then we can never skip more than a
2a15eb73
MS
3734 ;; single character from the match start position
3735 ;; (i.e. `state-pos' here) before continuing the
3736 ;; search. That since the look behind subexpression
3737 ;; might match the end of the insignificant region in
3738 ;; the next search.
d9e94c22
MS
3739
3740 (cond
d9e94c22
MS
3741 ((elt check-state 7)
3742 ;; Match inside a line comment. Skip to eol. Use
3743 ;; `re-search-forward' instead of `skip-chars-forward' to get
3744 ;; the right bound behavior.
3745 (re-search-forward "[\n\r]" bound noerror))
3746
3747 ((elt check-state 4)
3748 ;; Match inside a block comment. Skip to the '*/'.
3749 (search-forward "*/" bound noerror))
3750
3751 ((and (not (elt check-state 5))
3752 (eq (char-before check-pos) ?/)
2a15eb73 3753 (not (c-get-char-property (1- check-pos) 'syntax-table))
d9e94c22
MS
3754 (memq (char-after check-pos) '(?/ ?*)))
3755 ;; Match in the middle of the opener of a block or line
3756 ;; comment.
3757 (if (= (char-after check-pos) ?/)
3758 (re-search-forward "[\n\r]" bound noerror)
3759 (search-forward "*/" bound noerror)))
3760
2a15eb73
MS
3761 ;; The last `parse-partial-sexp' above might have
3762 ;; stopped short of the real check position if the end
3763 ;; of the current sexp was encountered in paren-level
3764 ;; mode. The checks above are always false in that
3765 ;; case, and since they can do better skipping in
3766 ;; lookbehind-submatch mode, we do them before
3767 ;; checking the paren level.
3768
3769 ((and paren-level
3770 (/= (setq tmp (car check-state)) 0))
3771 ;; Check the paren level first since we're short of the
3772 ;; syntactic checking position if the end of the
3773 ;; current sexp was encountered by `parse-partial-sexp'.
3774 (if (> tmp 0)
3775
3776 ;; Inside a nested paren sexp.
3777 (if lookbehind-submatch
3778 ;; See the NOTE above.
3779 (progn (goto-char state-pos) t)
3780 ;; Skip out of the paren quickly.
3781 (setq state (parse-partial-sexp state-pos bound 0 nil state)
3782 state-pos (point)))
3783
3784 ;; Have exited the current paren sexp.
3785 (if noerror
3786 (progn
3787 ;; The last `parse-partial-sexp' call above
3788 ;; has left us just after the closing paren
3789 ;; in this case, so we can modify the bound
3790 ;; to leave the point at the right position
3791 ;; upon return.
3792 (setq bound (1- (point)))
3793 nil)
3794 (signal 'search-failed (list regexp)))))
3795
3796 ((setq tmp (elt check-state 3))
3797 ;; Match inside a string.
3798 (if (or lookbehind-submatch
3799 (not (integerp tmp)))
3800 ;; See the NOTE above.
3801 (progn (goto-char state-pos) t)
3802 ;; Skip to the end of the string before continuing.
3803 (let ((ender (make-string 1 tmp)) (continue t))
3804 (while (if (search-forward ender bound noerror)
3805 (progn
3806 (setq state (parse-partial-sexp
3807 state-pos (point) nil nil state)
3808 state-pos (point))
3809 (elt state 3))
3810 (setq continue nil)))
3811 continue)))
d9e94c22
MS
3812
3813 ((save-excursion
3814 (save-match-data
3815 (c-beginning-of-macro start)))
3816 ;; Match inside a macro. Skip to the end of it.
3817 (c-end-of-macro)
3818 (cond ((<= (point) bound) t)
3819 (noerror nil)
2a15eb73 3820 (t (signal 'search-failed (list regexp)))))
d9e94c22 3821
2a15eb73
MS
3822 ((and not-inside-token
3823 (or (< check-pos last-token-end-pos)
3824 (< check-pos
3825 (save-excursion
3826 (goto-char check-pos)
3827 (save-match-data
3828 (c-end-of-current-token last-token-end-pos))
3829 (setq last-token-end-pos (point))))))
3830 ;; Inside a token.
3831 (if lookbehind-submatch
3832 ;; See the NOTE above.
3833 (goto-char state-pos)
3834 (goto-char (min last-token-end-pos bound))))
d9e94c22
MS
3835
3836 (t
3837 ;; A real match.
3838 (setq found t)
2a15eb73
MS
3839 nil)))
3840
3841 ;; Should loop to search again, but take care to avoid
3842 ;; looping on the same spot.
3843 (or (/= search-pos (point))
3844 (if (= (point) bound)
3845 (if noerror
3846 nil
3847 (signal 'search-failed (list regexp)))
3848 (forward-char)
3849 t))))
d9e94c22
MS
3850
3851 (error
3852 (goto-char start)
3853 (signal (car err) (cdr err))))
3854
2a15eb73 3855 ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
d9e94c22
MS
3856
3857 (if found
3858 (progn
2a15eb73
MS
3859 (goto-char (match-end 0))
3860 (match-end 0))
d9e94c22
MS
3861
3862 ;; Search failed. Set point as appropriate.
2a15eb73
MS
3863 (if (eq noerror t)
3864 (goto-char start)
3865 (goto-char bound))
d9e94c22
MS
3866 nil)))
3867
47641aac
GM
3868(defvar safe-pos-list) ; bound in c-syntactic-skip-backward
3869
d0fcee66
AM
3870(defsubst c-ssb-lit-begin ()
3871 ;; Return the start of the literal point is in, or nil.
3872 ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
3873 ;; bound in the caller.
3874
3875 ;; Use `parse-partial-sexp' from a safe position down to the point to check
3876 ;; if it's outside comments and strings.
3877 (save-excursion
3878 (let ((pos (point)) safe-pos state pps-end-pos)
3879 ;; Pick a safe position as close to the point as possible.
3880 ;;
3881 ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
3882 ;; position.
47641aac 3883
d0fcee66
AM
3884 (while (and safe-pos-list
3885 (> (car safe-pos-list) (point)))
3886 (setq safe-pos-list (cdr safe-pos-list)))
3887 (unless (setq safe-pos (car-safe safe-pos-list))
3888 (setq safe-pos (max (or (c-safe-position
3889 (point) (or c-state-cache
3890 (c-parse-state)))
3891 0)
3892 (point-min))
3893 safe-pos-list (list safe-pos)))
3894
3895 ;; Cache positions along the way to use if we have to back up more. We
3896 ;; cache every closing paren on the same level. If the paren cache is
3897 ;; relevant in this region then we're typically already on the same
3898 ;; level as the target position. Note that we might cache positions
3899 ;; after opening parens in case safe-pos is in a nested list. That's
3900 ;; both uncommon and harmless.
3901 (while (progn
3902 (setq state (parse-partial-sexp
3903 safe-pos pos 0))
3904 (< (point) pos))
3905 (setq safe-pos (point)
3906 safe-pos-list (cons safe-pos safe-pos-list)))
3907
3908 ;; If the state contains the start of the containing sexp we cache that
3909 ;; position too, so that parse-partial-sexp in the next run has a bigger
3910 ;; chance of starting at the same level as the target position and thus
3911 ;; will get more good safe positions into the list.
3912 (if (elt state 1)
3913 (setq safe-pos (1+ (elt state 1))
3914 safe-pos-list (cons safe-pos safe-pos-list)))
3915
3916 (if (or (elt state 3) (elt state 4))
3917 ;; Inside string or comment. Continue search at the
3918 ;; beginning of it.
3919 (elt state 8)))))
3920
0386b551 3921(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
d9e94c22
MS
3922 "Like `skip-chars-backward' but only look at syntactically relevant chars,
3923i.e. don't stop at positions inside syntactic whitespace or string
3924literals. Preprocessor directives are also ignored, with the exception
3925of the one that the point starts within, if any. If LIMIT is given,
0386b551
AM
3926it's assumed to be at a syntactically relevant position.
3927
3928If PAREN-LEVEL is non-nil, the function won't stop in nested paren
3929sexps, and the search will also not go outside the current paren sexp.
3930However, if LIMIT or the buffer limit is reached inside a nested paren
3931then the point will be left at the limit.
3932
3933Non-nil is returned if the point moved, nil otherwise.
3934
3935Note that this function might do hidden buffer changes. See the
3936comment at the start of cc-engine.el for more info."
d9e94c22
MS
3937
3938 (let ((start (point))
d0fcee66 3939 state-2
d9e94c22
MS
3940 ;; A list of syntactically relevant positions in descending
3941 ;; order. It's used to avoid scanning repeatedly over
3942 ;; potentially large regions with `parse-partial-sexp' to verify
d0fcee66 3943 ;; each position. Used in `c-ssb-lit-begin'
d9e94c22
MS
3944 safe-pos-list
3945 ;; The result from `c-beginning-of-macro' at the start position or the
3946 ;; start position itself if it isn't within a macro. Evaluated on
3947 ;; demand.
0386b551
AM
3948 start-macro-beg
3949 ;; The earliest position after the current one with the same paren
3950 ;; level. Used only when `paren-level' is set.
d0fcee66 3951 lit-beg
0386b551 3952 (paren-level-pos (point)))
d9e94c22 3953
d0fcee66
AM
3954 (while
3955 (progn
3956 ;; The next loop "tries" to find the end point each time round,
3957 ;; loops when it hasn't succeeded.
3958 (while
3959 (and
3960 (< (skip-chars-backward skip-chars limit) 0)
3961
3962 (let ((pos (point)) state-2 pps-end-pos)
3963
3964 (cond
3965 ;; Don't stop inside a literal
3966 ((setq lit-beg (c-ssb-lit-begin))
3967 (goto-char lit-beg)
3968 t)
3969
3970 ((and paren-level
3971 (save-excursion
3972 (setq state-2 (parse-partial-sexp
3973 pos paren-level-pos -1)
3974 pps-end-pos (point))
3975 (/= (car state-2) 0)))
3976 ;; Not at the right level.
3977
3978 (if (and (< (car state-2) 0)
3979 ;; We stop above if we go out of a paren.
3980 ;; Now check whether it precedes or is
3981 ;; nested in the starting sexp.
3982 (save-excursion
3983 (setq state-2
3984 (parse-partial-sexp
3985 pps-end-pos paren-level-pos
3986 nil nil state-2))
3987 (< (car state-2) 0)))
3988
3989 ;; We've stopped short of the starting position
3990 ;; so the hit was inside a nested list. Go up
3991 ;; until we are at the right level.
3992 (condition-case nil
3993 (progn
3994 (goto-char (scan-lists pos -1
3995 (- (car state-2))))
3996 (setq paren-level-pos (point))
3997 (if (and limit (>= limit paren-level-pos))
0386b551 3998 (progn
d0fcee66
AM
3999 (goto-char limit)
4000 nil)
4001 t))
4002 (error
4003 (goto-char (or limit (point-min)))
4004 nil))
4005
4006 ;; The hit was outside the list at the start
4007 ;; position. Go to the start of the list and exit.
4008 (goto-char (1+ (elt state-2 1)))
4009 nil))
4010
4011 ((c-beginning-of-macro limit)
4012 ;; Inside a macro.
4013 (if (< (point)
4014 (or start-macro-beg
4015 (setq start-macro-beg
4016 (save-excursion
4017 (goto-char start)
4018 (c-beginning-of-macro limit)
4019 (point)))))
4020 t
4021
4022 ;; It's inside the same macro we started in so it's
4023 ;; a relevant match.
4024 (goto-char pos)
4025 nil))))))
4026
4027 (> (point)
4028 (progn
4029 ;; Skip syntactic ws afterwards so that we don't stop at the
4030 ;; end of a comment if `skip-chars' is something like "^/".
4031 (c-backward-syntactic-ws)
4032 (point)))))
d9e94c22 4033
0386b551
AM
4034 ;; We might want to extend this with more useful return values in
4035 ;; the future.
4036 (/= (point) start)))
4037
4038;; The following is an alternative implementation of
4039;; `c-syntactic-skip-backward' that uses backward movement to keep
4040;; track of the syntactic context. It turned out to be generally
4041;; slower than the one above which uses forward checks from earlier
4042;; safe positions.
4043;;
4044;;(defconst c-ssb-stop-re
4045;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
4046;; ;; stop at to avoid going into comments and literals.
4047;; (concat
4048;; ;; Match comment end syntax and string literal syntax. Also match
4049;; ;; '/' for block comment endings (not covered by comment end
4050;; ;; syntax).
4051;; "\\s>\\|/\\|\\s\""
4052;; (if (memq 'gen-string-delim c-emacs-features)
4053;; "\\|\\s|"
4054;; "")
4055;; (if (memq 'gen-comment-delim c-emacs-features)
4056;; "\\|\\s!"
4057;; "")))
4058;;
4059;;(defconst c-ssb-stop-paren-re
4060;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
4061;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
4062;;
4063;;(defconst c-ssb-sexp-end-re
4064;; ;; Regexp matching the ending syntax of a complex sexp.
4065;; (concat c-string-limit-regexp "\\|\\s)"))
4066;;
4067;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
4068;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
4069;;i.e. don't stop at positions inside syntactic whitespace or string
4070;;literals. Preprocessor directives are also ignored. However, if the
4071;;point is within a comment, string literal or preprocessor directory to
4072;;begin with, its contents is treated as syntactically relevant chars.
4073;;If LIMIT is given, it limits the backward search and the point will be
4074;;left there if no earlier position is found.
4075;;
4076;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
4077;;sexps, and the search will also not go outside the current paren sexp.
4078;;However, if LIMIT or the buffer limit is reached inside a nested paren
4079;;then the point will be left at the limit.
4080;;
4081;;Non-nil is returned if the point moved, nil otherwise.
4082;;
4083;;Note that this function might do hidden buffer changes. See the
4084;;comment at the start of cc-engine.el for more info."
4085;;
4086;; (save-restriction
4087;; (when limit
4088;; (narrow-to-region limit (point-max)))
4089;;
4090;; (let ((start (point)))
4091;; (catch 'done
4092;; (while (let ((last-pos (point))
4093;; (stop-pos (progn
4094;; (skip-chars-backward skip-chars)
4095;; (point))))
4096;;
4097;; ;; Skip back over the same region as
4098;; ;; `skip-chars-backward' above, but keep to
4099;; ;; syntactically relevant positions.
4100;; (goto-char last-pos)
4101;; (while (and
4102;; ;; `re-search-backward' with a single char regexp
4103;; ;; should be fast.
4104;; (re-search-backward
4105;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
4106;; stop-pos 'move)
4107;;
4108;; (progn
4109;; (cond
4110;; ((looking-at "\\s(")
4111;; ;; `paren-level' is set and we've found the
4112;; ;; start of the containing paren.
4113;; (forward-char)
4114;; (throw 'done t))
4115;;
4116;; ((looking-at c-ssb-sexp-end-re)
4117;; ;; We're at the end of a string literal or paren
4118;; ;; sexp (if `paren-level' is set).
4119;; (forward-char)
4120;; (condition-case nil
4121;; (c-backward-sexp)
4122;; (error
4123;; (goto-char limit)
4124;; (throw 'done t))))
4125;;
4126;; (t
4127;; (forward-char)
4128;; ;; At the end of some syntactic ws or possibly
4129;; ;; after a plain '/' operator.
4130;; (let ((pos (point)))
4131;; (c-backward-syntactic-ws)
4132;; (if (= pos (point))
4133;; ;; Was a plain '/' operator. Go past it.
4134;; (backward-char)))))
4135;;
4136;; (> (point) stop-pos))))
4137;;
4138;; ;; Now the point is either at `stop-pos' or at some
4139;; ;; position further back if `stop-pos' was at a
4140;; ;; syntactically irrelevant place.
4141;;
4142;; ;; Skip additional syntactic ws so that we don't stop
4143;; ;; at the end of a comment if `skip-chars' is
4144;; ;; something like "^/".
4145;; (c-backward-syntactic-ws)
4146;;
4147;; (< (point) stop-pos))))
4148;;
4149;; ;; We might want to extend this with more useful return values
4150;; ;; in the future.
4151;; (/= (point) start))))
d9e94c22
MS
4152
4153\f
4154;; Tools for handling comments and string literals.
4155
4156(defun c-slow-in-literal (&optional lim detect-cpp)
4157 "Return the type of literal point is in, if any.
4158The return value is `c' if in a C-style comment, `c++' if in a C++
4159style comment, `string' if in a string literal, `pound' if DETECT-CPP
4160is non-nil and in a preprocessor line, or nil if somewhere else.
4161Optional LIM is used as the backward limit of the search. If omitted,
4162or nil, `c-beginning-of-defun' is used.
4163
4164The last point calculated is cached if the cache is enabled, i.e. if
4165`c-in-literal-cache' is bound to a two element vector.
4166
0386b551
AM
4167Note that this function might do hidden buffer changes. See the
4168comment at the start of cc-engine.el for more info."
4169
d9e94c22
MS
4170 (if (and (vectorp c-in-literal-cache)
4171 (= (point) (aref c-in-literal-cache 0)))
4172 (aref c-in-literal-cache 1)
4173 (let ((rtn (save-excursion
4174 (let* ((pos (point))
4175 (lim (or lim (progn
4176 (c-beginning-of-syntax)
4177 (point))))
4178 (state (parse-partial-sexp lim pos)))
4179 (cond
4180 ((elt state 3) 'string)
4181 ((elt state 4) (if (elt state 7) 'c++ 'c))
4182 ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
4183 (t nil))))))
4184 ;; cache this result if the cache is enabled
4185 (if (not c-in-literal-cache)
4186 (setq c-in-literal-cache (vector (point) rtn)))
4187 rtn)))
4188
4189;; XEmacs has a built-in function that should make this much quicker.
4190;; I don't think we even need the cache, which makes our lives more
4191;; complicated anyway. In this case, lim is only used to detect
4192;; cpp directives.
4193;;
4194;; Note that there is a bug in Xemacs's buffer-syntactic-context when used in
4195;; conjunction with syntax-table-properties. The bug is present in, e.g.,
4196;; Xemacs 21.4.4. It manifested itself thus:
4197;;
4198;; Starting with an empty AWK Mode buffer, type
4199;; /regexp/ {<C-j>
4200;; Point gets wrongly left at column 0, rather than being indented to tab-width.
4201;;
4202;; AWK Mode is designed such that when the first / is typed, it gets the
4203;; syntax-table property "string fence". When the second / is typed, BOTH /s
4204;; are given the s-t property "string". However, buffer-syntactic-context
4205;; fails to take account of the change of the s-t property on the opening / to
4206;; "string", and reports that the { is within a string started by the second /.
4207;;
4208;; The workaround for this is for the AWK Mode initialisation to switch the
4209;; defalias for c-in-literal to c-slow-in-literal. This will slow down other
4210;; cc-modes in Xemacs whenever an awk-buffer has been initialised.
b414f371 4211;;
d9e94c22
MS
4212;; (Alan Mackenzie, 2003/4/30).
4213
4214(defun c-fast-in-literal (&optional lim detect-cpp)
0386b551 4215 ;; This function might do hidden buffer changes.
d9e94c22
MS
4216 (let ((context (buffer-syntactic-context)))
4217 (cond
4218 ((eq context 'string) 'string)
4219 ((eq context 'comment) 'c++)
4220 ((eq context 'block-comment) 'c)
4221 ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
4222
4223(defalias 'c-in-literal
4224 (if (fboundp 'buffer-syntactic-context)
7bfc3fdb 4225 'c-fast-in-literal ; XEmacs
d9e94c22
MS
4226 'c-slow-in-literal)) ; GNU Emacs
4227
4228;; The defalias above isn't enough to shut up the byte compiler.
4229(cc-bytecomp-defun c-in-literal)
4230
4231(defun c-literal-limits (&optional lim near not-in-delimiter)
4232 "Return a cons of the beginning and end positions of the comment or
4233string surrounding point (including both delimiters), or nil if point
4234isn't in one. If LIM is non-nil, it's used as the \"safe\" position
4235to start parsing from. If NEAR is non-nil, then the limits of any
4236literal next to point is returned. \"Next to\" means there's only
4237spaces and tabs between point and the literal. The search for such a
4238literal is done first in forward direction. If NOT-IN-DELIMITER is
4239non-nil, the case when point is inside a starting delimiter won't be
a85fd6da 4240recognized. This only has effect for comments which have starting
d9e94c22
MS
4241delimiters with more than one character.
4242
0386b551
AM
4243Note that this function might do hidden buffer changes. See the
4244comment at the start of cc-engine.el for more info."
d9e94c22
MS
4245
4246 (save-excursion
4247 (let* ((pos (point))
4248 (lim (or lim (progn
4249 (c-beginning-of-syntax)
4250 (point))))
4251 (state (parse-partial-sexp lim pos)))
4252
0386b551
AM
4253 (cond ((elt state 3) ; String.
4254 (goto-char (elt state 8))
d9e94c22
MS
4255 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4256 (point-max))))
4257
0386b551
AM
4258 ((elt state 4) ; Comment.
4259 (goto-char (elt state 8))
d9e94c22
MS
4260 (cons (point) (progn (c-forward-single-comment) (point))))
4261
4262 ((and (not not-in-delimiter)
4263 (not (elt state 5))
4264 (eq (char-before) ?/)
4265 (looking-at "[/*]"))
4266 ;; We're standing in a comment starter.
4267 (backward-char 1)
4268 (cons (point) (progn (c-forward-single-comment) (point))))
4269
4270 (near
4271 (goto-char pos)
4272
4273 ;; Search forward for a literal.
4274 (skip-chars-forward " \t")
4275
4276 (cond
4277 ((looking-at c-string-limit-regexp) ; String.
4278 (cons (point) (or (c-safe (c-forward-sexp 1) (point))
4279 (point-max))))
4280
4281 ((looking-at c-comment-start-regexp) ; Line or block comment.
4282 (cons (point) (progn (c-forward-single-comment) (point))))
4283
4284 (t
4285 ;; Search backward.
4286 (skip-chars-backward " \t")
4287
4288 (let ((end (point)) beg)
4289 (cond
4290 ((save-excursion
4291 (< (skip-syntax-backward c-string-syntax) 0)) ; String.
4292 (setq beg (c-safe (c-backward-sexp 1) (point))))
4293
4294 ((and (c-safe (forward-char -2) t)
4295 (looking-at "*/"))
4296 ;; Block comment. Due to the nature of line
4297 ;; comments, they will always be covered by the
4298 ;; normal case above.
4299 (goto-char end)
4300 (c-backward-single-comment)
4301 ;; If LIM is bogus, beg will be bogus.
4302 (setq beg (point))))
4303
4304 (if beg (cons beg end))))))
4305 ))))
4306
0386b551
AM
4307;; In case external callers use this; it did have a docstring.
4308(defalias 'c-literal-limits-fast 'c-literal-limits)
d9e94c22 4309
0386b551
AM
4310(defun c-collect-line-comments (range)
4311 "If the argument is a cons of two buffer positions (such as returned by
4312`c-literal-limits'), and that range contains a C++ style line comment,
4313then an extended range is returned that contains all adjacent line
4314comments (i.e. all comments that starts in the same column with no
4315empty lines or non-whitespace characters between them). Otherwise the
4316argument is returned.
d9e94c22 4317
0386b551
AM
4318Note that this function might do hidden buffer changes. See the
4319comment at the start of cc-engine.el for more info."
d9e94c22
MS
4320
4321 (save-excursion
0386b551
AM
4322 (condition-case nil
4323 (if (and (consp range) (progn
4324 (goto-char (car range))
4325 (looking-at c-line-comment-starter)))
b414f371 4326 (let ((col (current-column))
0386b551
AM
4327 (beg (point))
4328 (bopl (c-point 'bopl))
4329 (end (cdr range)))
4330 ;; Got to take care in the backward direction to handle
4331 ;; comments which are preceded by code.
4332 (while (and (c-backward-single-comment)
4333 (>= (point) bopl)
4334 (looking-at c-line-comment-starter)
4335 (= col (current-column)))
4336 (setq beg (point)
4337 bopl (c-point 'bopl)))
4338 (goto-char end)
4339 (while (and (progn (skip-chars-forward " \t")
4340 (looking-at c-line-comment-starter))
4341 (= col (current-column))
4342 (prog1 (zerop (forward-line 1))
4343 (setq end (point)))))
4344 (cons beg end))
4345 range)
4346 (error range))))
d9e94c22
MS
4347
4348(defun c-literal-type (range)
4349 "Convenience function that given the result of `c-literal-limits',
a85fd6da
AM
4350returns nil or the type of literal that the range surrounds, one
4351of the symbols 'c, 'c++ or 'string. It's much faster than using
4352`c-in-literal' and is intended to be used when you need both the
4353type of a literal and its limits.
d9e94c22 4354
0386b551
AM
4355Note that this function might do hidden buffer changes. See the
4356comment at the start of cc-engine.el for more info."
4357
d9e94c22
MS
4358 (if (consp range)
4359 (save-excursion
4360 (goto-char (car range))
4361 (cond ((looking-at c-string-limit-regexp) 'string)
4362 ((or (looking-at "//") ; c++ line comment
4363 (and (looking-at "\\s<") ; comment starter
4364 (looking-at "#"))) ; awk comment.
4365 'c++)
4366 (t 'c))) ; Assuming the range is valid.
4367 range))
4368
4369\f
4370;; `c-find-decl-spots' and accompanying stuff.
4371
4372;; Variables used in `c-find-decl-spots' to cache the search done for
4373;; the first declaration in the last call. When that function starts,
4374;; it needs to back up over syntactic whitespace to look at the last
4375;; token before the region being searched. That can sometimes cause
4376;; moves back and forth over a quite large region of comments and
4377;; macros, which would be repeated for each changed character when
4378;; we're called during fontification, since font-lock refontifies the
4379;; current line for each change. Thus it's worthwhile to cache the
4380;; first match.
4381;;
4382;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
4383;; the syntactic whitespace less or equal to some start position.
4384;; There's no cached value if it's nil.
4385;;
4386;; `c-find-decl-match-pos' is the match position if
4387;; `c-find-decl-prefix-search' matched before the syntactic whitespace
4388;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
4389(defvar c-find-decl-syntactic-pos nil)
4390(make-variable-buffer-local 'c-find-decl-syntactic-pos)
4391(defvar c-find-decl-match-pos nil)
4392(make-variable-buffer-local 'c-find-decl-match-pos)
4393
4394(defsubst c-invalidate-find-decl-cache (change-min-pos)
4395 (and c-find-decl-syntactic-pos
4396 (< change-min-pos c-find-decl-syntactic-pos)
4397 (setq c-find-decl-syntactic-pos nil)))
4398
4399; (defface c-debug-decl-spot-face
4400; '((t (:background "Turquoise")))
4401; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
4402; (defface c-debug-decl-sws-face
4403; '((t (:background "Khaki")))
4404; "Debug face to mark the syntactic whitespace between the declaration
4405; spots and the preceding token end.")
4406
4407(defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
4408 (when (facep 'c-debug-decl-spot-face)
0386b551 4409 `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
d9e94c22
MS
4410 (c-debug-add-face (max match-pos (point-min)) decl-pos
4411 'c-debug-decl-sws-face)
4412 (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
4413 'c-debug-decl-spot-face))))
4414(defmacro c-debug-remove-decl-spot-faces (beg end)
4415 (when (facep 'c-debug-decl-spot-face)
0386b551 4416 `(c-save-buffer-state ()
d9e94c22
MS
4417 (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
4418 (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
4419
4420(defmacro c-find-decl-prefix-search ()
4421 ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
4422 ;; but it contains lots of free variables that refer to things
4423 ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
4424 ;; if there is a match, otherwise at `cfd-limit'.
0386b551
AM
4425 ;;
4426 ;; This macro might do hidden buffer changes.
d9e94c22
MS
4427
4428 '(progn
4429 ;; Find the next property match position if we haven't got one already.
4430 (unless cfd-prop-match
4431 (save-excursion
4432 (while (progn
4433 (goto-char (next-single-property-change
4434 (point) 'c-type nil cfd-limit))
4435 (and (< (point) cfd-limit)
4436 (not (eq (c-get-char-property (1- (point)) 'c-type)
4437 'c-decl-end)))))
4438 (setq cfd-prop-match (point))))
4439
0386b551
AM
4440 ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
4441 ;; got one already.
d9e94c22 4442 (unless cfd-re-match
0386b551
AM
4443
4444 (if (> cfd-re-match-end (point))
4445 (goto-char cfd-re-match-end))
4446
4447 (while (if (setq cfd-re-match-end
4448 (re-search-forward c-decl-prefix-or-start-re
4449 cfd-limit 'move))
4450
4451 ;; Match. Check if it's inside a comment or string literal.
4452 (c-got-face-at
4453 (if (setq cfd-re-match (match-end 1))
4454 ;; Matched the end of a token preceding a decl spot.
4455 (progn
4456 (goto-char cfd-re-match)
4457 (1- cfd-re-match))
4458 ;; Matched a token that start a decl spot.
4459 (goto-char (match-beginning 0))
4460 (point))
4461 c-literal-faces)
4462
4463 ;; No match. Finish up and exit the loop.
4464 (setq cfd-re-match cfd-limit)
4465 nil)
4466
4467 ;; Skip out of comments and string literals.
d9e94c22
MS
4468 (while (progn
4469 (goto-char (next-single-property-change
0386b551 4470 (point) 'face nil cfd-limit))
d9e94c22 4471 (and (< (point) cfd-limit)
0386b551
AM
4472 (c-got-face-at (point) c-literal-faces)))))
4473
4474 ;; If we matched at the decl start, we have to back up over the
4475 ;; preceding syntactic ws to set `cfd-match-pos' and to catch
4476 ;; any decl spots in the syntactic ws.
d9e94c22 4477 (unless cfd-re-match
0386b551
AM
4478 (c-backward-syntactic-ws)
4479 (setq cfd-re-match (point))))
d9e94c22
MS
4480
4481 ;; Choose whichever match is closer to the start.
4482 (if (< cfd-re-match cfd-prop-match)
4483 (setq cfd-match-pos cfd-re-match
4484 cfd-re-match nil)
4485 (setq cfd-match-pos cfd-prop-match
4486 cfd-prop-match nil))
4487
4488 (goto-char cfd-match-pos)
4489
4490 (when (< cfd-match-pos cfd-limit)
4491 ;; Skip forward past comments only so we don't skip macros.
4492 (c-forward-comments)
4493 ;; Set the position to continue at. We can avoid going over
4494 ;; the comments skipped above a second time, but it's possible
4495 ;; that the comment skipping has taken us past `cfd-prop-match'
4496 ;; since the property might be used inside comments.
4497 (setq cfd-continue-pos (if cfd-prop-match
4498 (min cfd-prop-match (point))
4499 (point))))))
4500
4501(defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
0386b551 4502 ;; Call CFD-FUN for each possible spot for a declaration, cast or
a85fd6da
AM
4503 ;; label from the point to CFD-LIMIT.
4504 ;;
4505 ;; CFD-FUN is called with point at the start of the spot. It's
4506 ;; passed two arguments: The first is the end position of the token
4507 ;; preceding the spot, or 0 for the implicit match at bob. The
4508 ;; second is a flag that is t when the match is inside a macro. If
4509 ;; CFD-FUN adds `c-decl-end' properties somewhere below the current
4510 ;; spot, it should return non-nil to ensure that the next search
4511 ;; will find them.
0386b551 4512 ;;
a85fd6da 4513 ;; Such a spot is:
0386b551
AM
4514 ;; o The first token after bob.
4515 ;; o The first token after the end of submatch 1 in
4516 ;; `c-decl-prefix-or-start-re' when that submatch matches.
4517 ;; o The start of each `c-decl-prefix-or-start-re' match when
4518 ;; submatch 1 doesn't match.
5a89f0a7 4519 ;; o The first token after the end of each occurrence of the
0386b551
AM
4520 ;; `c-type' text property with the value `c-decl-end', provided
4521 ;; `c-type-decl-end-used' is set.
4522 ;;
4523 ;; Only a spot that match CFD-DECL-RE and whose face is in the
4524 ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
4525 ;; check is disabled if CFD-FACE-CHECKLIST is nil.
d9e94c22
MS
4526 ;;
4527 ;; If the match is inside a macro then the buffer is narrowed to the
4528 ;; end of it, so that CFD-FUN can investigate the following tokens
4529 ;; without matching something that begins inside a macro and ends
4530 ;; outside it. It's to avoid this work that the CFD-DECL-RE and
4531 ;; CFD-FACE-CHECKLIST checks exist.
4532 ;;
0386b551
AM
4533 ;; The spots are visited approximately in order from top to bottom.
4534 ;; It's however the positions where `c-decl-prefix-or-start-re'
4535 ;; matches and where `c-decl-end' properties are found that are in
4536 ;; order. Since the spots often are at the following token, they
4537 ;; might be visited out of order insofar as more spots are reported
4538 ;; later on within the syntactic whitespace between the match
4539 ;; positions and their spots.
4540 ;;
4541 ;; It's assumed that comments and strings are fontified in the
d9e94c22
MS
4542 ;; searched range.
4543 ;;
4544 ;; This is mainly used in fontification, and so has an elaborate
4545 ;; cache to handle repeated calls from the same start position; see
4546 ;; the variables above.
4547 ;;
4548 ;; All variables in this function begin with `cfd-' to avoid name
4549 ;; collision with the (dynamically bound) variables used in CFD-FUN.
0386b551
AM
4550 ;;
4551 ;; This function might do hidden buffer changes.
d9e94c22 4552
0386b551
AM
4553 (let ((cfd-start-pos (point))
4554 (cfd-buffer-end (point-max))
4555 ;; The end of the token preceding the decl spot last found
4556 ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
4557 ;; no match.
d9e94c22 4558 cfd-re-match
0386b551
AM
4559 ;; The end position of the last `c-decl-prefix-or-start-re'
4560 ;; match. If this is greater than `cfd-continue-pos', the
4561 ;; next regexp search is started here instead.
4562 (cfd-re-match-end (point-min))
4563 ;; The end of the last `c-decl-end' found by
4564 ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
4565 ;; match. If searching for the property isn't needed then we
4566 ;; disable it by setting it to `cfd-limit' directly.
d9e94c22 4567 (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
0386b551
AM
4568 ;; The end of the token preceding the decl spot last found by
4569 ;; `c-find-decl-prefix-search'. 0 for the implicit match at
4570 ;; bob. `cfd-limit' if there's no match. In other words,
4571 ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
d9e94c22
MS
4572 (cfd-match-pos cfd-limit)
4573 ;; The position to continue searching at.
4574 cfd-continue-pos
4575 ;; The position of the last "real" token we've stopped at.
4576 ;; This can be greater than `cfd-continue-pos' when we get
4577 ;; hits inside macros or at `c-decl-end' positions inside
4578 ;; comments.
4579 (cfd-token-pos 0)
4580 ;; The end position of the last entered macro.
4581 (cfd-macro-end 0))
4582
4583 ;; Initialize by finding a syntactically relevant start position
0386b551
AM
4584 ;; before the point, and do the first `c-decl-prefix-or-start-re'
4585 ;; search unless we're at bob.
d9e94c22 4586
0386b551 4587 (let (start-in-literal start-in-macro syntactic-pos)
d9e94c22
MS
4588 ;; Must back up a bit since we look for the end of the previous
4589 ;; statement or declaration, which is earlier than the first
4590 ;; returned match.
4591
0386b551
AM
4592 (cond
4593 ;; First we need to move to a syntactically relevant position.
4594 ;; Begin by backing out of comment or string literals.
4595 ((and
4596 (when (c-got-face-at (point) c-literal-faces)
4597 ;; Try to use the faces to back up to the start of the
4598 ;; literal. FIXME: What if the point is on a declaration
4599 ;; inside a comment?
4600 (while (and (not (bobp))
4601 (c-got-face-at (1- (point)) c-literal-faces))
4602 (goto-char (previous-single-property-change
4603 (point) 'face nil (point-min))))
4604
4605 ;; XEmacs doesn't fontify the quotes surrounding string
4606 ;; literals.
4607 (and (featurep 'xemacs)
4608 (eq (get-text-property (point) 'face)
4609 'font-lock-string-face)
4610 (not (bobp))
4611 (progn (backward-char)
4612 (not (looking-at c-string-limit-regexp)))
4613 (forward-char))
4614
4615 ;; Don't trust the literal to contain only literal faces
4616 ;; (the font lock package might not have fontified the
4617 ;; start of it at all, for instance) so check that we have
4618 ;; arrived at something that looks like a start or else
4619 ;; resort to `c-literal-limits'.
4620 (unless (looking-at c-literal-start-regexp)
4621 (let ((range (c-literal-limits)))
4622 (if range (goto-char (car range)))))
4623
4624 (setq start-in-literal (point)))
4625
4626 ;; The start is in a literal. If the limit is in the same
4627 ;; one we don't have to find a syntactic position etc. We
4628 ;; only check that if the limit is at or before bonl to save
4629 ;; time; it covers the by far most common case when font-lock
4630 ;; refontifies the current line only.
4631 (<= cfd-limit (c-point 'bonl cfd-start-pos))
4632 (save-excursion
4633 (goto-char cfd-start-pos)
4634 (while (progn
4635 (goto-char (next-single-property-change
4636 (point) 'face nil cfd-limit))
4637 (and (< (point) cfd-limit)
4638 (c-got-face-at (point) c-literal-faces))))
4639 (= (point) cfd-limit)))
4640
4641 ;; Completely inside a literal. Set up variables to trig the
4642 ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
4643 ;; find a suitable start position.
4644 (setq cfd-continue-pos start-in-literal))
4645
4646 ;; Check if the region might be completely inside a macro, to
4647 ;; optimize that like the completely-inside-literal above.
4648 ((save-excursion
4649 (and (= (forward-line 1) 0)
4650 (bolp) ; forward-line has funny behavior at eob.
4651 (>= (point) cfd-limit)
4652 (progn (backward-char)
4653 (eq (char-before) ?\\))))
4654 ;; (Maybe) completely inside a macro. Only need to trig the
4655 ;; (< cfd-continue-pos cfd-start-pos) case below to make it
4656 ;; set things up.
4657 (setq cfd-continue-pos (1- cfd-start-pos)
4658 start-in-macro t))
d9e94c22 4659
0386b551
AM
4660 (t
4661 ;; Back out of any macro so we don't miss any declaration
4662 ;; that could follow after it.
4663 (when (c-beginning-of-macro)
4664 (setq start-in-macro t))
4665
4666 ;; Now we're at a proper syntactically relevant position so we
4667 ;; can use the cache. But first clear it if it applied
4668 ;; further down.
4669 (c-invalidate-find-decl-cache cfd-start-pos)
4670
4671 (setq syntactic-pos (point))
4672 (unless (eq syntactic-pos c-find-decl-syntactic-pos)
4673 ;; Don't have to do this if the cache is relevant here,
4674 ;; typically if the same line is refontified again. If
4675 ;; we're just some syntactic whitespace further down we can
4676 ;; still use the cache to limit the skipping.
4677 (c-backward-syntactic-ws c-find-decl-syntactic-pos))
4678
4679 ;; If we hit `c-find-decl-syntactic-pos' and
4680 ;; `c-find-decl-match-pos' is set then we install the cached
4681 ;; values. If we hit `c-find-decl-syntactic-pos' and
4682 ;; `c-find-decl-match-pos' is nil then we know there's no decl
4683 ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
4684 ;; and so we can continue the search from this point. If we
4685 ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
4686 ;; the right spot to begin searching anyway.
4687 (if (and (eq (point) c-find-decl-syntactic-pos)
4688 c-find-decl-match-pos)
d9e94c22
MS
4689 (setq cfd-match-pos c-find-decl-match-pos
4690 cfd-continue-pos syntactic-pos)
0386b551
AM
4691
4692 (setq c-find-decl-syntactic-pos syntactic-pos)
4693
4694 (when (if (bobp)
4695 ;; Always consider bob a match to get the first
4696 ;; declaration in the file. Do this separately instead of
4697 ;; letting `c-decl-prefix-or-start-re' match bob, so that
4698 ;; regexp always can consume at least one character to
4699 ;; ensure that we won't get stuck in an infinite loop.
4700 (setq cfd-re-match 0)
4701 (backward-char)
4702 (c-beginning-of-current-token)
4703 (< (point) cfd-limit))
4704 ;; Do an initial search now. In the bob case above it's
4705 ;; only done to search for a `c-decl-end' spot.
4706 (c-find-decl-prefix-search))
4707
4708 (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
4709 cfd-match-pos)))))
4710
4711 ;; Advance `cfd-continue-pos' if it's before the start position.
4712 ;; The closest continue position that might have effect at or
4713 ;; after the start depends on what we started in. This also
4714 ;; finds a suitable start position in the special cases when the
4715 ;; region is completely within a literal or macro.
4716 (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
4717
4718 (cond
4719 (start-in-macro
4720 ;; If we're in a macro then it's the closest preceding token
4721 ;; in the macro. Check this before `start-in-literal',
4722 ;; since if we're inside a literal in a macro, the preceding
4723 ;; token is earlier than any `c-decl-end' spot inside the
4724 ;; literal (comment).
4725 (goto-char (or start-in-literal cfd-start-pos))
4726 ;; The only syntactic ws in macros are comments.
d9e94c22 4727 (c-backward-comments)
0386b551
AM
4728 (backward-char)
4729 (c-beginning-of-current-token))
4730
4731 (start-in-literal
4732 ;; If we're in a comment it can only be the closest
4733 ;; preceding `c-decl-end' position within that comment, if
4734 ;; any. Go back to the beginning of such a property so that
4735 ;; `c-find-decl-prefix-search' will find the end of it.
4736 ;; (Can't stop at the end and install it directly on
4737 ;; `cfd-prop-match' since that variable might be cleared
4738 ;; after `cfd-fun' below.)
4739 ;;
4740 ;; Note that if the literal is a string then the property
4741 ;; search will simply skip to the beginning of it right
4742 ;; away.
4743 (if (not c-type-decl-end-used)
4744 (goto-char start-in-literal)
4745 (goto-char cfd-start-pos)
4746 (while (progn
4747 (goto-char (previous-single-property-change
4748 (point) 'c-type nil start-in-literal))
4749 (and (> (point) start-in-literal)
4750 (not (eq (c-get-char-property (point) 'c-type)
4751 'c-decl-end))))))
4752
4753 (when (= (point) start-in-literal)
4754 ;; Didn't find any property inside the comment, so we can
4755 ;; skip it entirely. (This won't skip past a string, but
4756 ;; that'll be handled quickly by the next
4757 ;; `c-find-decl-prefix-search' anyway.)
4758 (c-forward-single-comment)
4759 (if (> (point) cfd-limit)
4760 (goto-char cfd-limit))))
d9e94c22 4761
0386b551
AM
4762 (t
4763 ;; If we started in normal code, the only match that might
4764 ;; apply before the start is what we already got in
4765 ;; `cfd-match-pos' so we can continue at the start position.
4766 ;; (Note that we don't get here if the first match is below
4767 ;; it.)
4768 (goto-char cfd-start-pos)))
4769
4770 ;; Delete found matches if they are before our new continue
4771 ;; position, so that `c-find-decl-prefix-search' won't back up
4772 ;; to them later on.
4773 (setq cfd-continue-pos (point))
4774 (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
4775 (setq cfd-re-match nil))
4776 (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
4777 (setq cfd-prop-match nil)))
4778
4779 (if syntactic-pos
4780 ;; This is the normal case and we got a proper syntactic
4781 ;; position. If there's a match then it's always outside
4782 ;; macros and comments, so advance to the next token and set
4783 ;; `cfd-token-pos'. The loop below will later go back using
4784 ;; `cfd-continue-pos' to fix declarations inside the
4785 ;; syntactic ws.
4786 (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
4787 (goto-char syntactic-pos)
4788 (c-forward-syntactic-ws)
4789 (and cfd-continue-pos
4790 (< cfd-continue-pos (point))
4791 (setq cfd-token-pos (point))))
4792
4793 ;; Have one of the special cases when the region is completely
4794 ;; within a literal or macro. `cfd-continue-pos' is set to a
4795 ;; good start position for the search, so do it.
4796 (c-find-decl-prefix-search)))
d9e94c22 4797
51c9af45 4798 ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
d9e94c22
MS
4799
4800 (while (progn
4801 (while (and
4802 (< cfd-match-pos cfd-limit)
4803
4804 (or
4805 ;; Kludge to filter out matches on the "<" that
4806 ;; aren't open parens, for the sake of languages
4807 ;; that got `c-recognize-<>-arglists' set.
4808 (and (eq (char-before cfd-match-pos) ?<)
4809 (not (c-get-char-property (1- cfd-match-pos)
4810 'syntax-table)))
4811
4812 ;; If `cfd-continue-pos' is less or equal to
4813 ;; `cfd-token-pos', we've got a hit inside a macro
4814 ;; that's in the syntactic whitespace before the last
4815 ;; "real" declaration we've checked. If they're equal
4816 ;; we've arrived at the declaration a second time, so
4817 ;; there's nothing to do.
4818 (= cfd-continue-pos cfd-token-pos)
4819
4820 (progn
4821 ;; If `cfd-continue-pos' is less than `cfd-token-pos'
4822 ;; we're still searching for declarations embedded in
4823 ;; the syntactic whitespace. In that case we need
4824 ;; only to skip comments and not macros, since they
4825 ;; can't be nested, and that's already been done in
4826 ;; `c-find-decl-prefix-search'.
4827 (when (> cfd-continue-pos cfd-token-pos)
4828 (c-forward-syntactic-ws)
4829 (setq cfd-token-pos (point)))
4830
4831 ;; Continue if the following token fails the
4832 ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
4833 (when (or (>= (point) cfd-limit)
4834 (not (looking-at cfd-decl-re))
4835 (and cfd-face-checklist
4836 (not (c-got-face-at
4837 (point) cfd-face-checklist))))
4838 (goto-char cfd-continue-pos)
4839 t)))
4840
4841 (< (point) cfd-limit))
4842 (c-find-decl-prefix-search))
4843
4844 (< (point) cfd-limit))
4845
0386b551
AM
4846 (when (and
4847 (>= (point) cfd-start-pos)
d9e94c22 4848
0386b551
AM
4849 (progn
4850 ;; Narrow to the end of the macro if we got a hit inside
4851 ;; one, to avoid recognizing things that start inside the
4852 ;; macro and end outside it.
4853 (when (> cfd-match-pos cfd-macro-end)
4854 ;; Not in the same macro as in the previous round.
4855 (save-excursion
4856 (goto-char cfd-match-pos)
4857 (setq cfd-macro-end
4858 (if (save-excursion (and (c-beginning-of-macro)
4859 (< (point) cfd-match-pos)))
4860 (progn (c-end-of-macro)
4861 (point))
4862 0))))
4863
4864 (if (zerop cfd-macro-end)
4865 t
4866 (if (> cfd-macro-end (point))
4867 (progn (narrow-to-region (point-min) cfd-macro-end)
4868 t)
4869 ;; The matched token was the last thing in the macro,
4870 ;; so the whole match is bogus.
4871 (setq cfd-macro-end 0)
4872 nil))))
d9e94c22
MS
4873
4874 (c-debug-put-decl-spot-faces cfd-match-pos (point))
0386b551
AM
4875 (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
4876 (setq cfd-prop-match nil))
d9e94c22
MS
4877
4878 (when (/= cfd-macro-end 0)
4879 ;; Restore limits if we did macro narrowment above.
4880 (narrow-to-region (point-min) cfd-buffer-end)))
4881
4882 (goto-char cfd-continue-pos)
4883 (if (= cfd-continue-pos cfd-limit)
4884 (setq cfd-match-pos cfd-limit)
4885 (c-find-decl-prefix-search)))))
4886
4887\f
4888;; A cache for found types.
4889
4890;; Buffer local variable that contains an obarray with the types we've
4891;; found. If a declaration is recognized somewhere we record the
4892;; fully qualified identifier in it to recognize it as a type
4893;; elsewhere in the file too. This is not accurate since we do not
4894;; bother with the scoping rules of the languages, but in practice the
4895;; same name is seldom used as both a type and something else in a
4896;; file, and we only use this as a last resort in ambiguous cases (see
0386b551
AM
4897;; `c-forward-decl-or-cast-1').
4898;;
580fba94
AM
4899;; Not every type need be in this cache. However, things which have
4900;; ceased to be types must be removed from it.
4901;;
0386b551
AM
4902;; Template types in C++ are added here too but with the template
4903;; arglist replaced with "<>" in references or "<" for the one in the
4904;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
4905;; "Foo<>::Bar<". This avoids storing very long strings (since C++
4906;; template specs can be fairly sized programs in themselves) and
4907;; improves the hit ratio (it's a type regardless of the template
4908;; args; it's just not the same type, but we're only interested in
4909;; recognizing types, not telling distinct types apart). Note that
4910;; template types in references are added here too; from the example
4911;; above there will also be an entry "Foo<".
d9e94c22
MS
4912(defvar c-found-types nil)
4913(make-variable-buffer-local 'c-found-types)
4914
4915(defsubst c-clear-found-types ()
4916 ;; Clears `c-found-types'.
d9e94c22
MS
4917 (setq c-found-types (make-vector 53 0)))
4918
4919(defun c-add-type (from to)
4920 ;; Add the given region as a type in `c-found-types'. If the region
4921 ;; doesn't match an existing type but there is a type which is equal
4922 ;; to the given one except that the last character is missing, then
4923 ;; the shorter type is removed. That's done to avoid adding all
4924 ;; prefixes of a type as it's being entered and font locked. This
4925 ;; doesn't cover cases like when characters are removed from a type
4926 ;; or added in the middle. We'd need the position of point when the
4927 ;; font locking is invoked to solve this well.
0386b551
AM
4928 ;;
4929 ;; This function might do hidden buffer changes.
4930 (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
4931 (unless (intern-soft type c-found-types)
4932 (unintern (substring type 0 -1) c-found-types)
4933 (intern type c-found-types))))
d9e94c22 4934
580fba94
AM
4935(defun c-unfind-type (name)
4936 ;; Remove the "NAME" from c-found-types, if present.
4937 (unintern name c-found-types))
4938
d9e94c22
MS
4939(defsubst c-check-type (from to)
4940 ;; Return non-nil if the given region contains a type in
4941 ;; `c-found-types'.
0386b551
AM
4942 ;;
4943 ;; This function might do hidden buffer changes.
4944 (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
4945 c-found-types))
d9e94c22
MS
4946
4947(defun c-list-found-types ()
4948 ;; Return all the types in `c-found-types' as a sorted list of
4949 ;; strings.
4950 (let (type-list)
4951 (mapatoms (lambda (type)
4952 (setq type-list (cons (symbol-name type)
4953 type-list)))
4954 c-found-types)
4955 (sort type-list 'string-lessp)))
a66cd3ee 4956
2f42c75f
DN
4957;; Shut up the byte compiler.
4958(defvar c-maybe-stale-found-type)
4959
580fba94
AM
4960(defun c-trim-found-types (beg end old-len)
4961 ;; An after change function which, in conjunction with the info in
4962 ;; c-maybe-stale-found-type (set in c-before-change), removes a type
4963 ;; from `c-found-types', should this type have become stale. For
4964 ;; example, this happens to "foo" when "foo \n bar();" becomes
4965 ;; "foo(); \n bar();". Such stale types, if not removed, foul up
4966 ;; the fontification.
b414f371 4967 ;;
580fba94
AM
4968 ;; Have we, perhaps, added non-ws characters to the front/back of a found
4969 ;; type?
4970 (when (> end beg)
4971 (save-excursion
4972 (when (< end (point-max))
4973 (goto-char end)
4974 (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
4975 (progn (goto-char end)
4976 (c-end-of-current-token)))
4977 (c-unfind-type (buffer-substring-no-properties
4978 end (point)))))
4979 (when (> beg (point-min))
4980 (goto-char beg)
4981 (if (and (c-end-of-current-token) ; only moves when we started in the middle
4982 (progn (goto-char beg)
4983 (c-beginning-of-current-token)))
4984 (c-unfind-type (buffer-substring-no-properties
4985 (point) beg))))))
b414f371 4986
580fba94
AM
4987 (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
4988 (cond
4989 ;; Changing the amount of (already existing) whitespace - don't do anything.
4990 ((and (c-partial-ws-p beg end)
4991 (or (= beg end) ; removal of WS
580fba94
AM
4992 (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
4993
4994 ;; The syntactic relationship which defined a "found type" has been
4995 ;; destroyed.
4996 ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
4997 (c-unfind-type (cadr c-maybe-stale-found-type)))
4998;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
4999 )))
5000
d9e94c22 5001\f
dd969a56
AM
5002;; Setting and removing syntax properties on < and > in languages (C++
5003;; and Java) where they can be template/generic delimiters as well as
5004;; their normal meaning of "less/greater than".
5005
5006;; Normally, < and > have syntax 'punctuation'. When they are found to
5007;; be delimiters, they are marked as such with the category properties
5008;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
5009
5010;; STRATEGY:
5011;;
5012;; It is impossible to determine with certainty whether a <..> pair in
5013;; C++ is two comparison operators or is template delimiters, unless
5014;; one duplicates a lot of a C++ compiler. For example, the following
5015;; code fragment:
5016;;
5017;; foo (a < b, c > d) ;
5018;;
5019;; could be a function call with two integer parameters (each a
5020;; relational expression), or it could be a constructor for class foo
5021;; taking one parameter d of templated type "a < b, c >". They are
5022;; somewhat easier to distinguish in Java.
5023;;
5024;; The strategy now (2010-01) adopted is to mark and unmark < and
5025;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
5026;; individually when their context so indicated. This gave rise to
5027;; intractible problems when one of a matching pair was deleted, or
5028;; pulled into a literal.]
5029;;
5030;; At each buffer change, the syntax-table properties are removed in a
5031;; before-change function and reapplied, when needed, in an
5032;; after-change function. It is far more important that the
5033;; properties get removed when they they are spurious than that they
5034;; be present when wanted.
5035;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5036(defun c-clear-<-pair-props (&optional pos)
5037 ;; POS (default point) is at a < character. If it is marked with
5038 ;; open paren syntax-table text property, remove the property,
5039 ;; together with the close paren property on the matching > (if
5040 ;; any).
5041 (save-excursion
5042 (if pos
5043 (goto-char pos)
5044 (setq pos (point)))
5045 (when (equal (c-get-char-property (point) 'syntax-table)
5046 c-<-as-paren-syntax)
5047 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5048 (c-go-list-forward))
5049 (when (equal (c-get-char-property (1- (point)) 'syntax-table)
5050 c->-as-paren-syntax) ; should always be true.
8a249abc
AM
5051 (c-clear-char-property (1- (point)) 'category))
5052 (c-clear-char-property pos 'category))))
dd969a56
AM
5053
5054(defun c-clear->-pair-props (&optional pos)
5055 ;; POS (default point) is at a > character. If it is marked with
5056 ;; close paren syntax-table property, remove the property, together
5057 ;; with the open paren property on the matching < (if any).
5058 (save-excursion
5059 (if pos
5060 (goto-char pos)
5061 (setq pos (point)))
5062 (when (equal (c-get-char-property (point) 'syntax-table)
5063 c->-as-paren-syntax)
5064 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5065 (c-go-up-list-backward))
5066 (when (equal (c-get-char-property (point) 'syntax-table)
5067 c-<-as-paren-syntax) ; should always be true.
8a249abc
AM
5068 (c-clear-char-property (point) 'category))
5069 (c-clear-char-property pos 'category))))
dd969a56
AM
5070
5071(defun c-clear-<>-pair-props (&optional pos)
5072 ;; POS (default point) is at a < or > character. If it has an
5073 ;; open/close paren syntax-table property, remove this property both
5074 ;; from the current character and its partner (which will also be
5075 ;; thusly marked).
5076 (cond
5077 ((eq (char-after) ?\<)
5078 (c-clear-<-pair-props pos))
5079 ((eq (char-after) ?\>)
5080 (c-clear->-pair-props pos))
5081 (t (c-benign-error
5082 "c-clear-<>-pair-props called from wrong position"))))
5083
5084(defun c-clear-<-pair-props-if-match-after (lim &optional pos)
5085 ;; POS (default point) is at a < character. If it is both marked
5086 ;; with open/close paren syntax-table property, and has a matching >
5087 ;; (also marked) which is after LIM, remove the property both from
43a91810
AM
5088 ;; the current > and its partner. Return t when this happens, nil
5089 ;; when it doesn't.
dd969a56
AM
5090 (save-excursion
5091 (if pos
5092 (goto-char pos)
5093 (setq pos (point)))
5094 (when (equal (c-get-char-property (point) 'syntax-table)
5095 c-<-as-paren-syntax)
5096 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5097 (c-go-list-forward))
5098 (when (and (>= (point) lim)
5099 (equal (c-get-char-property (1- (point)) 'syntax-table)
5100 c->-as-paren-syntax)) ; should always be true.
5101 (c-unmark-<->-as-paren (1- (point)))
43a91810
AM
5102 (c-unmark-<->-as-paren pos))
5103 t)))
dd969a56
AM
5104
5105(defun c-clear->-pair-props-if-match-before (lim &optional pos)
5106 ;; POS (default point) is at a > character. If it is both marked
5107 ;; with open/close paren syntax-table property, and has a matching <
5108 ;; (also marked) which is before LIM, remove the property both from
43a91810
AM
5109 ;; the current < and its partner. Return t when this happens, nil
5110 ;; when it doesn't.
dd969a56
AM
5111 (save-excursion
5112 (if pos
5113 (goto-char pos)
5114 (setq pos (point)))
5115 (when (equal (c-get-char-property (point) 'syntax-table)
5116 c->-as-paren-syntax)
5117 (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
5118 (c-go-up-list-backward))
5119 (when (and (<= (point) lim)
5120 (equal (c-get-char-property (point) 'syntax-table)
5121 c-<-as-paren-syntax)) ; should always be true.
5122 (c-unmark-<->-as-paren (point))
43a91810
AM
5123 (c-unmark-<->-as-paren pos))
5124 t)))
dd969a56 5125
f3b554af
GM
5126;; Set by c-common-init in cc-mode.el.
5127(defvar c-new-BEG)
5128(defvar c-new-END)
5129
dd969a56
AM
5130(defun c-before-change-check-<>-operators (beg end)
5131 ;; Unmark certain pairs of "< .... >" which are currently marked as
5132 ;; template/generic delimiters. (This marking is via syntax-table
5133 ;; text properties).
5134 ;;
5135 ;; These pairs are those which are in the current "statement" (i.e.,
5136 ;; the region between the {, }, or ; before BEG and the one after
5137 ;; END), and which enclose any part of the interval (BEG END).
5138 ;;
5139 ;; Note that in C++ (?and Java), template/generic parens cannot
5140 ;; enclose a brace or semicolon, so we use these as bounds on the
5141 ;; region we must work on.
5142 ;;
5143 ;; This function is called from before-change-functions (via
5144 ;; c-get-state-before-change-functions). Thus the buffer is widened,
5145 ;; and point is undefined, both at entry and exit.
5146 ;;
5147 ;; FIXME!!! This routine ignores the possibility of macros entirely.
5148 ;; 2010-01-29.
5149 (save-excursion
5150 (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits)))
43a91810
AM
5151 (end-lit-limits (progn (goto-char end) (c-literal-limits)))
5152 new-beg new-end need-new-beg need-new-end)
dd969a56
AM
5153 ;; Locate the barrier before the changed region
5154 (goto-char (if beg-lit-limits (car beg-lit-limits) beg))
5155 (c-syntactic-skip-backward "^;{}" (max (- beg 2048) (point-min)))
43a91810 5156 (setq new-beg (point))
dd969a56
AM
5157
5158 ;; Remove the syntax-table properties from each pertinent <...> pair.
5159 ;; Firsly, the ones with the < before beg and > after beg.
5160 (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg)
43a91810
AM
5161 (if (c-clear-<-pair-props-if-match-after beg (1- (point)))
5162 (setq need-new-beg t)))
dd969a56
AM
5163
5164 ;; Locate the barrier after END.
5165 (goto-char (if end-lit-limits (cdr end-lit-limits) end))
5166 (c-syntactic-re-search-forward "[;{}]"
5167 (min (+ end 2048) (point-max)) 'end)
43a91810 5168 (setq new-end (point))
dd969a56
AM
5169
5170 ;; Remove syntax-table properties from the remaining pertinent <...>
5171 ;; pairs, those with a > after end and < before end.
5172 (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end)
43a91810
AM
5173 (if (c-clear->-pair-props-if-match-before end)
5174 (setq need-new-end t)))
5175
5176 ;; Extend the fontification region, if needed.
5177 (when need-new-beg
5178 (goto-char new-beg)
5179 (c-forward-syntactic-ws)
5180 (and (< (point) c-new-BEG) (setq c-new-BEG (point))))
5181
5182 (when need-new-end
5183 (and (> new-end c-new-END) (setq c-new-END new-end))))))
dd969a56
AM
5184
5185
d9e94c22 5186
0386b551
AM
5187(defun c-after-change-check-<>-operators (beg end)
5188 ;; This is called from `after-change-functions' when
5189 ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
5190 ;; chars with paren syntax become part of another operator like "<<"
5191 ;; or ">=".
5192 ;;
5193 ;; This function might do hidden buffer changes.
5194
cb694ab7
AM
5195 (save-excursion
5196 (goto-char beg)
5197 (when (or (looking-at "[<>]")
5198 (< (skip-chars-backward "<>") 0))
5199
0386b551 5200 (goto-char beg)
cb694ab7
AM
5201 (c-beginning-of-current-token)
5202 (when (and (< (point) beg)
5203 (looking-at c-<>-multichar-token-regexp)
5204 (< beg (setq beg (match-end 0))))
5205 (while (progn (skip-chars-forward "^<>" beg)
5206 (< (point) beg))
dd969a56 5207 (c-clear-<>-pair-props)
cb694ab7
AM
5208 (forward-char))))
5209
5210 (when (< beg end)
5211 (goto-char end)
0386b551
AM
5212 (when (or (looking-at "[<>]")
5213 (< (skip-chars-backward "<>") 0))
5214
cb694ab7 5215 (goto-char end)
0386b551 5216 (c-beginning-of-current-token)
cb694ab7 5217 (when (and (< (point) end)
0386b551 5218 (looking-at c-<>-multichar-token-regexp)
cb694ab7
AM
5219 (< end (setq end (match-end 0))))
5220 (while (progn (skip-chars-forward "^<>" end)
5221 (< (point) end))
dd969a56 5222 (c-clear-<>-pair-props)
cb694ab7 5223 (forward-char)))))))
d9e94c22 5224
dd969a56
AM
5225
5226\f
5227;; Handling of small scale constructs like types and names.
5228
d9e94c22
MS
5229;; Dynamically bound variable that instructs `c-forward-type' to also
5230;; treat possible types (i.e. those that it normally returns 'maybe or
5231;; 'found for) as actual types (and always return 'found for them).
5232;; This means that it records them in `c-record-type-identifiers' if
5233;; that is set, and that it adds them to `c-found-types'.
5234(defvar c-promote-possible-types nil)
5235
0386b551
AM
5236;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
5237;; mark up successfully parsed arglists with paren syntax properties on
5238;; the surrounding angle brackets and with `c-<>-arg-sep' in the
5239;; `c-type' property of each argument separating comma.
5240;;
5241;; Setting this variable also makes `c-forward-<>-arglist' recurse into
5242;; all arglists for side effects (i.e. recording types), otherwise it
5243;; exploits any existing paren syntax properties to quickly jump to the
5244;; end of already parsed arglists.
5245;;
5246;; Marking up the arglists is not the default since doing that correctly
5247;; depends on a proper value for `c-restricted-<>-arglists'.
5248(defvar c-parse-and-markup-<>-arglists nil)
5249
d9e94c22 5250;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
037558bf
MS
5251;; not accept arglists that contain binary operators.
5252;;
5253;; This is primarily used to handle C++ template arglists. C++
5254;; disambiguates them by checking whether the preceding name is a
5255;; template or not. We can't do that, so we assume it is a template
5256;; if it can be parsed as one. That usually works well since
5257;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
5258;; in almost all cases would be pointless.
5259;;
5260;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
5261;; should let the comma separate the function arguments instead. And
5262;; in a context where the value of the expression is taken, e.g. in
5263;; "if (a < b || c > d)", it's probably not a template.
5264(defvar c-restricted-<>-arglists nil)
d9e94c22 5265
0386b551
AM
5266;; Dynamically bound variables that instructs
5267;; `c-forward-keyword-clause', `c-forward-<>-arglist',
5268;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
5269;; `c-forward-label' to record the ranges of all the type and
5270;; reference identifiers they encounter. They will build lists on
5271;; these variables where each element is a cons of the buffer
5272;; positions surrounding each identifier. This recording is only
5273;; activated when `c-record-type-identifiers' is non-nil.
d9e94c22
MS
5274;;
5275;; All known types that can't be identifiers are recorded, and also
5276;; other possible types if `c-promote-possible-types' is set.
5277;; Recording is however disabled inside angle bracket arglists that
5278;; are encountered inside names and other angle bracket arglists.
0386b551 5279;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
d9e94c22
MS
5280;; instead.
5281;;
5282;; Only the names in C++ template style references (e.g. "tmpl" in
5283;; "tmpl<a,b>::foo") are recorded as references, other references
5284;; aren't handled here.
0386b551
AM
5285;;
5286;; `c-forward-label' records the label identifier(s) on
5287;; `c-record-ref-identifiers'.
d9e94c22
MS
5288(defvar c-record-type-identifiers nil)
5289(defvar c-record-ref-identifiers nil)
5290
0386b551
AM
5291;; This variable will receive a cons cell of the range of the last
5292;; single identifier symbol stepped over by `c-forward-name' if it's
5293;; successful. This is the range that should be put on one of the
5294;; record lists above by the caller. It's assigned nil if there's no
5295;; such symbol in the name.
d9e94c22
MS
5296(defvar c-last-identifier-range nil)
5297
5298(defmacro c-record-type-id (range)
5299 (if (eq (car-safe range) 'cons)
5300 ;; Always true.
5301 `(setq c-record-type-identifiers
5302 (cons ,range c-record-type-identifiers))
5303 `(let ((range ,range))
5304 (if range
5305 (setq c-record-type-identifiers
5306 (cons range c-record-type-identifiers))))))
5307
5308(defmacro c-record-ref-id (range)
5309 (if (eq (car-safe range) 'cons)
5310 ;; Always true.
5311 `(setq c-record-ref-identifiers
5312 (cons ,range c-record-ref-identifiers))
5313 `(let ((range ,range))
5314 (if range
5315 (setq c-record-ref-identifiers
5316 (cons range c-record-ref-identifiers))))))
5317
5318;; Dynamically bound variable that instructs `c-forward-type' to
5319;; record the ranges of types that only are found. Behaves otherwise
5320;; like `c-record-type-identifiers'.
5321(defvar c-record-found-types nil)
5322
5323(defmacro c-forward-keyword-prefixed-id (type)
5324 ;; Used internally in `c-forward-keyword-clause' to move forward
5325 ;; over a type (if TYPE is 'type) or a name (otherwise) which
5326 ;; possibly is prefixed by keywords and their associated clauses.
5327 ;; Try with a type/name first to not trip up on those that begin
5328 ;; with a keyword. Return t if a known or found type is moved
5329 ;; over. The point is clobbered if nil is returned. If range
5330 ;; recording is enabled, the identifier is recorded on as a type
5331 ;; if TYPE is 'type or as a reference if TYPE is 'ref.
0386b551
AM
5332 ;;
5333 ;; This macro might do hidden buffer changes.
d9e94c22
MS
5334 `(let (res)
5335 (while (if (setq res ,(if (eq type 'type)
5336 `(c-forward-type)
5337 `(c-forward-name)))
5338 nil
5339 (and (looking-at c-keywords-regexp)
0386b551 5340 (c-forward-keyword-clause 1))))
d9e94c22
MS
5341 (when (memq res '(t known found prefix))
5342 ,(when (eq type 'ref)
5343 `(when c-record-type-identifiers
5344 (c-record-ref-id c-last-identifier-range)))
5345 t)))
5346
0386b551 5347(defmacro c-forward-id-comma-list (type update-safe-pos)
d9e94c22
MS
5348 ;; Used internally in `c-forward-keyword-clause' to move forward
5349 ;; over a comma separated list of types or names using
5350 ;; `c-forward-keyword-prefixed-id'.
0386b551
AM
5351 ;;
5352 ;; This macro might do hidden buffer changes.
d9e94c22 5353 `(while (and (progn
0386b551
AM
5354 ,(when update-safe-pos
5355 `(setq safe-pos (point)))
d9e94c22
MS
5356 (eq (char-after) ?,))
5357 (progn
5358 (forward-char)
5359 (c-forward-syntactic-ws)
5360 (c-forward-keyword-prefixed-id ,type)))))
5361
0386b551
AM
5362(defun c-forward-keyword-clause (match)
5363 ;; Submatch MATCH in the current match data is assumed to surround a
5364 ;; token. If it's a keyword, move over it and any immediately
5365 ;; following clauses associated with it, stopping at the start of
5366 ;; the next token. t is returned in that case, otherwise the point
d9e94c22
MS
5367 ;; stays and nil is returned. The kind of clauses that are
5368 ;; recognized are those specified by `c-type-list-kwds',
5369 ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
5370 ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
5371 ;; and `c-<>-arglist-kwds'.
0386b551
AM
5372 ;;
5373 ;; This function records identifier ranges on
5374 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5375 ;; `c-record-type-identifiers' is non-nil.
5376 ;;
5377 ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
5378 ;; apply directly after the keyword, the type list is moved over
5379 ;; only when there is no unaccounted token before it (i.e. a token
5380 ;; that isn't moved over due to some other keyword list). The
5381 ;; identifier ranges in the list are still recorded if that should
5382 ;; be done, though.
5383 ;;
5384 ;; This function might do hidden buffer changes.
5385
5386 (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
5387 ;; The call to `c-forward-<>-arglist' below is made after
5388 ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
5389 ;; are angle bracket arglists and `c-restricted-<>-arglists'
5390 ;; should therefore be nil.
5391 (c-parse-and-markup-<>-arglists t)
5392 c-restricted-<>-arglists)
d9e94c22 5393
d9e94c22 5394 (when kwd-sym
0386b551 5395 (goto-char (match-end match))
d9e94c22
MS
5396 (c-forward-syntactic-ws)
5397 (setq safe-pos (point))
a66cd3ee 5398
d9e94c22
MS
5399 (cond
5400 ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
5401 (c-forward-keyword-prefixed-id type))
5402 ;; There's a type directly after a keyword in `c-type-list-kwds'.
0386b551 5403 (c-forward-id-comma-list type t))
d9e94c22
MS
5404
5405 ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
5406 (c-forward-keyword-prefixed-id ref))
5407 ;; There's a name directly after a keyword in `c-ref-list-kwds'.
0386b551 5408 (c-forward-id-comma-list ref t))
d9e94c22
MS
5409
5410 ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
5411 (eq (char-after) ?\())
5412 ;; There's an open paren after a keyword in `c-paren-any-kwds'.
5413
5414 (forward-char)
5415 (when (and (setq pos (c-up-list-forward))
5416 (eq (char-before pos) ?\)))
5417 (when (and c-record-type-identifiers
5418 (c-keyword-member kwd-sym 'c-paren-type-kwds))
5419 ;; Use `c-forward-type' on every identifier we can find
5420 ;; inside the paren, to record the types.
5421 (while (c-syntactic-re-search-forward c-symbol-start pos t)
5422 (goto-char (match-beginning 0))
5423 (unless (c-forward-type)
5424 (looking-at c-symbol-key) ; Always matches.
5425 (goto-char (match-end 0)))))
5426
5427 (goto-char pos)
5428 (c-forward-syntactic-ws)
5429 (setq safe-pos (point))))
5430
5431 ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
5432 (eq (char-after) ?<)
0386b551 5433 (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
d9e94c22
MS
5434 (c-forward-syntactic-ws)
5435 (setq safe-pos (point)))
5436
5437 ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
449a2b0d
MS
5438 (not (looking-at c-symbol-start))
5439 (c-safe (c-forward-sexp) t))
d9e94c22
MS
5440 (c-forward-syntactic-ws)
5441 (setq safe-pos (point))))
5442
0386b551
AM
5443 (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
5444 (if (eq (char-after) ?:)
5445 ;; If we are at the colon already, we move over the type
5446 ;; list after it.
5447 (progn
5448 (forward-char)
5449 (c-forward-syntactic-ws)
5450 (when (c-forward-keyword-prefixed-id type)
5451 (c-forward-id-comma-list type t)))
5452 ;; Not at the colon, so stop here. But the identifier
5453 ;; ranges in the type list later on should still be
5454 ;; recorded.
5455 (and c-record-type-identifiers
5456 (progn
5457 ;; If a keyword matched both one of the types above and
5458 ;; this one, we match `c-colon-type-list-re' after the
5459 ;; clause matched above.
5460 (goto-char safe-pos)
5461 (looking-at c-colon-type-list-re))
5462 (progn
5463 (goto-char (match-end 0))
5464 (c-forward-syntactic-ws)
5465 (c-forward-keyword-prefixed-id type))
5466 ;; There's a type after the `c-colon-type-list-re' match
5467 ;; after a keyword in `c-colon-type-list-kwds'.
5468 (c-forward-id-comma-list type nil))))
d9e94c22
MS
5469
5470 (goto-char safe-pos)
5471 t)))
5472
f3b554af
GM
5473;; cc-mode requires cc-fonts.
5474(declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
5475
0386b551
AM
5476(defun c-forward-<>-arglist (all-types)
5477 ;; The point is assumed to be at a "<". Try to treat it as the open
b4dc7d98 5478 ;; paren of an angle bracket arglist and move forward to the
0386b551
AM
5479 ;; corresponding ">". If successful, the point is left after the
5480 ;; ">" and t is returned, otherwise the point isn't moved and nil is
d9e94c22
MS
5481 ;; returned. If ALL-TYPES is t then all encountered arguments in
5482 ;; the arglist that might be types are treated as found types.
5483 ;;
0386b551
AM
5484 ;; The variable `c-parse-and-markup-<>-arglists' controls how this
5485 ;; function handles text properties on the angle brackets and argument
5486 ;; separating commas.
d9e94c22 5487 ;;
0386b551
AM
5488 ;; `c-restricted-<>-arglists' controls how lenient the template
5489 ;; arglist recognition should be.
5490 ;;
5491 ;; This function records identifier ranges on
5492 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5493 ;; `c-record-type-identifiers' is non-nil.
5494 ;;
5495 ;; This function might do hidden buffer changes.
d9e94c22
MS
5496
5497 (let ((start (point))
5498 ;; If `c-record-type-identifiers' is set then activate
5499 ;; recording of any found types that constitute an argument in
5500 ;; the arglist.
abfc152b 5501 (c-record-found-types (if c-record-type-identifiers t)))
d9e94c22
MS
5502 (if (catch 'angle-bracket-arglist-escape
5503 (setq c-record-found-types
0386b551 5504 (c-forward-<>-arglist-recur all-types)))
d9e94c22
MS
5505 (progn
5506 (when (consp c-record-found-types)
5507 (setq c-record-type-identifiers
5508 ;; `nconc' doesn't mind that the tail of
5509 ;; `c-record-found-types' is t.
5510 (nconc c-record-found-types c-record-type-identifiers)))
452ea855 5511 (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs))
d9e94c22
MS
5512 t)
5513
5514 (goto-char start)
a66cd3ee 5515 nil)))
785eecbb 5516
0386b551 5517(defun c-forward-<>-arglist-recur (all-types)
d9e94c22 5518 ;; Recursive part of `c-forward-<>-arglist'.
0386b551
AM
5519 ;;
5520 ;; This function might do hidden buffer changes.
d9e94c22
MS
5521
5522 (let ((start (point)) res pos tmp
5523 ;; Cover this so that any recorded found type ranges are
5524 ;; automatically lost if it turns out to not be an angle
5525 ;; bracket arglist. It's propagated through the return value
5526 ;; on successful completion.
5527 (c-record-found-types c-record-found-types)
5528 ;; List that collects the positions after the argument
5529 ;; separating ',' in the arglist.
5530 arg-start-pos)
0386b551
AM
5531 ;; If the '<' has paren open syntax then we've marked it as an angle
5532 ;; bracket arglist before, so skip to the end.
5533 (if (and (not c-parse-and-markup-<>-arglists)
5534 (c-get-char-property (point) 'syntax-table))
5535
5536 (progn
5537 (forward-char)
5538 (if (and (c-go-up-list-forward)
5539 (eq (char-before) ?>))
5540 t
0386b551
AM
5541 ;; Got unmatched paren angle brackets. We don't clear the paren
5542 ;; syntax properties and retry, on the basis that it's very
5543 ;; unlikely that paren angle brackets become operators by code
5544 ;; manipulation. It's far more likely that it doesn't match due
5545 ;; to narrowing or some temporary change.
5546 (goto-char start)
5547 nil))
d9e94c22 5548
abfc152b 5549 (forward-char) ; Forward over the opening '<'.
452ea855 5550
d9e94c22 5551 (unless (looking-at c-<-op-cont-regexp)
abfc152b
AM
5552 ;; go forward one non-alphanumeric character (group) per iteration of
5553 ;; this loop.
22c3ce97 5554 (while (and
d9e94c22 5555 (progn
22c3ce97
AM
5556 (c-forward-syntactic-ws)
5557 (let ((orig-record-found-types c-record-found-types))
5558 (when (or (and c-record-type-identifiers all-types)
5559 (c-major-mode-is 'java-mode))
5560 ;; All encountered identifiers are types, so set the
5561 ;; promote flag and parse the type.
5562 (progn
5563 (c-forward-syntactic-ws)
5564 (if (looking-at "\\?")
5565 (forward-char)
5566 (when (looking-at c-identifier-start)
5567 (let ((c-promote-possible-types t)
5568 (c-record-found-types t))
5569 (c-forward-type))))
5570
5571 (c-forward-syntactic-ws)
5572
5573 (when (or (looking-at "extends")
5574 (looking-at "super"))
5575 (forward-word)
5576 (c-forward-syntactic-ws)
5577 (let ((c-promote-possible-types t)
5578 (c-record-found-types t))
5579 (c-forward-type)
5580 (c-forward-syntactic-ws))))))
5581
abfc152b 5582 (setq pos (point)) ; e.g. first token inside the '<'
22c3ce97 5583
a4ee83cc
AM
5584 ;; Note: These regexps exploit the match order in \| so
5585 ;; that "<>" is matched by "<" rather than "[^>:-]>".
5586 (c-syntactic-re-search-forward
5587 ;; Stop on ',', '|', '&', '+' and '-' to catch
5588 ;; common binary operators that could be between
5589 ;; two comparison expressions "a<b" and "c>d".
5590 "[<;{},|+&-]\\|[>)]"
5591 nil t t))
22c3ce97
AM
5592
5593 (cond
5594 ((eq (char-before) ?>)
d9e94c22
MS
5595 ;; Either an operator starting with '>' or the end of
5596 ;; the angle bracket arglist.
5597
0386b551 5598 (if (looking-at c->-op-cont-regexp)
d9e94c22 5599 (progn
0386b551
AM
5600 (goto-char (match-end 0))
5601 t) ; Continue the loop.
d9e94c22 5602
0386b551
AM
5603 ;; The angle bracket arglist is finished.
5604 (when c-parse-and-markup-<>-arglists
d9e94c22 5605 (while arg-start-pos
0386b551
AM
5606 (c-put-c-type-property (1- (car arg-start-pos))
5607 'c-<>-arg-sep)
d9e94c22
MS
5608 (setq arg-start-pos (cdr arg-start-pos)))
5609 (c-mark-<-as-paren start)
0386b551
AM
5610 (c-mark->-as-paren (1- (point))))
5611 (setq res t)
5612 nil)) ; Exit the loop.
d9e94c22
MS
5613
5614 ((eq (char-before) ?<)
5615 ;; Either an operator starting with '<' or a nested arglist.
d9e94c22
MS
5616 (setq pos (point))
5617 (let (id-start id-end subres keyword-match)
abfc152b
AM
5618 (cond
5619 ;; The '<' begins a multi-char operator.
5620 ((looking-at c-<-op-cont-regexp)
5621 (setq tmp (match-end 0))
5622 (goto-char (match-end 0)))
5623 ;; We're at a nested <.....>
5624 ((progn
5625 (setq tmp pos)
5626 (backward-char) ; to the '<'
5627 (and
5628 (save-excursion
5629 ;; There's always an identifier before an angle
5630 ;; bracket arglist, or a keyword in `c-<>-type-kwds'
5631 ;; or `c-<>-arglist-kwds'.
5632 (c-backward-syntactic-ws)
5633 (setq id-end (point))
5634 (c-simple-skip-symbol-backward)
5635 (when (or (setq keyword-match
5636 (looking-at c-opt-<>-sexp-key))
5637 (not (looking-at c-keywords-regexp)))
5638 (setq id-start (point))))
5639 (setq subres
5640 (let ((c-promote-possible-types t)
5641 (c-record-found-types t))
5642 (c-forward-<>-arglist-recur
5643 (and keyword-match
5644 (c-keyword-member
5645 (c-keyword-sym (match-string 1))
5646 'c-<>-type-kwds)))))))
d9e94c22
MS
5647
5648 ;; It was an angle bracket arglist.
5649 (setq c-record-found-types subres)
5650
5651 ;; Record the identifier before the template as a type
5652 ;; or reference depending on whether the arglist is last
5653 ;; in a qualified identifier.
5654 (when (and c-record-type-identifiers
5655 (not keyword-match))
5656 (if (and c-opt-identifier-concat-key
5657 (progn
5658 (c-forward-syntactic-ws)
5659 (looking-at c-opt-identifier-concat-key)))
5660 (c-record-ref-id (cons id-start id-end))
abfc152b
AM
5661 (c-record-type-id (cons id-start id-end)))))
5662
5663 ;; At a "less than" operator.
5664 (t
5665 (forward-char)
5666 )))
5667 t) ; carry on looping.
22c3ce97
AM
5668
5669 ((and (not c-restricted-<>-arglists)
5670 (or (and (eq (char-before) ?&)
5671 (not (eq (char-after) ?&)))
5672 (eq (char-before) ?,)))
5673 ;; Just another argument. Record the position. The
5674 ;; type check stuff that made us stop at it is at
5675 ;; the top of the loop.
d9e94c22
MS
5676 (setq arg-start-pos (cons (point) arg-start-pos)))
5677
5678 (t
5679 ;; Got a character that can't be in an angle bracket
5680 ;; arglist argument. Abort using `throw', since
5681 ;; it's useless to try to find a surrounding arglist
5682 ;; if we're nested.
5683 (throw 'angle-bracket-arglist-escape nil))))))
d9e94c22
MS
5684 (if res
5685 (or c-record-found-types t)))))
5686
0386b551
AM
5687(defun c-backward-<>-arglist (all-types &optional limit)
5688 ;; The point is assumed to be directly after a ">". Try to treat it
5689 ;; as the close paren of an angle bracket arglist and move back to
5690 ;; the corresponding "<". If successful, the point is left at
5691 ;; the "<" and t is returned, otherwise the point isn't moved and
5692 ;; nil is returned. ALL-TYPES is passed on to
5693 ;; `c-forward-<>-arglist'.
5694 ;;
5695 ;; If the optional LIMIT is given, it bounds the backward search.
5696 ;; It's then assumed to be at a syntactically relevant position.
5697 ;;
5698 ;; This is a wrapper around `c-forward-<>-arglist'. See that
5699 ;; function for more details.
5700
5701 (let ((start (point)))
5702 (backward-char)
5703 (if (and (not c-parse-and-markup-<>-arglists)
5704 (c-get-char-property (point) 'syntax-table))
5705
5706 (if (and (c-go-up-list-backward)
5707 (eq (char-after) ?<))
5708 t
5709 ;; See corresponding note in `c-forward-<>-arglist'.
5710 (goto-char start)
5711 nil)
5712
51c9af45 5713 (while (progn
0386b551
AM
5714 (c-syntactic-skip-backward "^<;{}" limit t)
5715
51c9af45
AM
5716 (and
5717 (if (eq (char-before) ?<)
5718 t
5719 ;; Stopped at bob or a char that isn't allowed in an
5720 ;; arglist, so we've failed.
5721 (goto-char start)
5722 nil)
0386b551 5723
51c9af45
AM
5724 (if (> (point)
5725 (progn (c-beginning-of-current-token)
5726 (point)))
5727 ;; If we moved then the "<" was part of some
5728 ;; multicharacter token.
5729 t
0386b551 5730
51c9af45
AM
5731 (backward-char)
5732 (let ((beg-pos (point)))
5733 (if (c-forward-<>-arglist all-types)
5734 (cond ((= (point) start)
5735 ;; Matched the arglist. Break the while.
5736 (goto-char beg-pos)
5737 nil)
5738 ((> (point) start)
5739 ;; We started from a non-paren ">" inside an
5740 ;; arglist.
5741 (goto-char start)
5742 nil)
5743 (t
5744 ;; Matched a shorter arglist. Can be a nested
5745 ;; one so continue looking.
5746 (goto-char beg-pos)
5747 t))
5748 t))))))
0386b551
AM
5749
5750 (/= (point) start))))
5751
d9e94c22
MS
5752(defun c-forward-name ()
5753 ;; Move forward over a complete name if at the beginning of one,
e15f8aaa
AM
5754 ;; stopping at the next following token. A keyword, as such,
5755 ;; doesn't count as a name. If the point is not at something that
5756 ;; is recognized as a name then it stays put.
5757 ;;
5758 ;; A name could be something as simple as "foo" in C or something as
d9e94c22
MS
5759 ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
5760 ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
5761 ;; int>::*volatile const" in C++ (this function is actually little
5762 ;; more than a `looking-at' call in all modes except those that,
e15f8aaa
AM
5763 ;; like C++, have `c-recognize-<>-arglists' set).
5764 ;;
5765 ;; Return
5766 ;; o - nil if no name is found;
5767 ;; o - 'template if it's an identifier ending with an angle bracket
5768 ;; arglist;
5769 ;; o - 'operator of it's an operator identifier;
5770 ;; o - t if it's some other kind of name.
0386b551
AM
5771 ;;
5772 ;; This function records identifier ranges on
5773 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5774 ;; `c-record-type-identifiers' is non-nil.
5775 ;;
5776 ;; This function might do hidden buffer changes.
d9e94c22 5777
0386b551 5778 (let ((pos (point)) (start (point)) res id-start id-end
d9e94c22
MS
5779 ;; Turn off `c-promote-possible-types' here since we might
5780 ;; call `c-forward-<>-arglist' and we don't want it to promote
5781 ;; every suspect thing in the arglist to a type. We're
5782 ;; typically called from `c-forward-type' in this case, and
5783 ;; the caller only wants the top level type that it finds to
5784 ;; be promoted.
5785 c-promote-possible-types)
5786 (while
5787 (and
5788 (looking-at c-identifier-key)
5789
5790 (progn
5791 ;; Check for keyword. We go to the last symbol in
5792 ;; `c-identifier-key' first.
0386b551
AM
5793 (goto-char (setq id-end (match-end 0)))
5794 (c-simple-skip-symbol-backward)
5795 (setq id-start (point))
d9e94c22
MS
5796
5797 (if (looking-at c-keywords-regexp)
5798 (when (and (c-major-mode-is 'c++-mode)
5799 (looking-at
5800 (cc-eval-when-compile
5801 (concat "\\(operator\\|\\(template\\)\\)"
5802 "\\(" (c-lang-const c-nonsymbol-key c++)
5803 "\\|$\\)")))
5804 (if (match-beginning 2)
5805 ;; "template" is only valid inside an
5806 ;; identifier if preceded by "::".
5807 (save-excursion
5808 (c-backward-syntactic-ws)
5809 (and (c-safe (backward-char 2) t)
5810 (looking-at "::")))
5811 t))
5812
5813 ;; Handle a C++ operator or template identifier.
5814 (goto-char id-end)
5815 (c-forward-syntactic-ws)
5816 (cond ((eq (char-before id-end) ?e)
5817 ;; Got "... ::template".
5818 (let ((subres (c-forward-name)))
5819 (when subres
5820 (setq pos (point)
5821 res subres))))
5822
5823 ((looking-at c-identifier-start)
5824 ;; Got a cast operator.
5825 (when (c-forward-type)
5826 (setq pos (point)
5827 res 'operator)
5828 ;; Now we should match a sequence of either
5829 ;; '*', '&' or a name followed by ":: *",
5830 ;; where each can be followed by a sequence
5831 ;; of `c-opt-type-modifier-key'.
5832 (while (cond ((looking-at "[*&]")
5833 (goto-char (match-end 0))
5834 t)
5835 ((looking-at c-identifier-start)
5836 (and (c-forward-name)
5837 (looking-at "::")
5838 (progn
5839 (goto-char (match-end 0))
5840 (c-forward-syntactic-ws)
5841 (eq (char-after) ?*))
5842 (progn
5843 (forward-char)
5844 t))))
5845 (while (progn
5846 (c-forward-syntactic-ws)
5847 (setq pos (point))
5848 (looking-at c-opt-type-modifier-key))
5849 (goto-char (match-end 1))))))
5850
5851 ((looking-at c-overloadable-operators-regexp)
5852 ;; Got some other operator.
0386b551
AM
5853 (setq c-last-identifier-range
5854 (cons (point) (match-end 0)))
d9e94c22
MS
5855 (goto-char (match-end 0))
5856 (c-forward-syntactic-ws)
5857 (setq pos (point)
5858 res 'operator)))
5859
5860 nil)
5861
0386b551
AM
5862 ;; `id-start' is equal to `id-end' if we've jumped over
5863 ;; an identifier that doesn't end with a symbol token.
5864 ;; That can occur e.g. for Java import directives on the
5865 ;; form "foo.bar.*".
5866 (when (and id-start (/= id-start id-end))
d9e94c22
MS
5867 (setq c-last-identifier-range
5868 (cons id-start id-end)))
5869 (goto-char id-end)
5870 (c-forward-syntactic-ws)
5871 (setq pos (point)
5872 res t)))
5873
5874 (progn
5875 (goto-char pos)
5876 (when (or c-opt-identifier-concat-key
5877 c-recognize-<>-arglists)
5878
5879 (cond
5880 ((and c-opt-identifier-concat-key
5881 (looking-at c-opt-identifier-concat-key))
5882 ;; Got a concatenated identifier. This handles the
5883 ;; cases with tricky syntactic whitespace that aren't
5884 ;; covered in `c-identifier-key'.
5885 (goto-char (match-end 0))
5886 (c-forward-syntactic-ws)
5887 t)
5888
5889 ((and c-recognize-<>-arglists
5890 (eq (char-after) ?<))
5891 ;; Maybe an angle bracket arglist.
452ea855
AM
5892 (when (let ((c-record-type-identifiers t)
5893 (c-record-found-types t))
0386b551
AM
5894 (c-forward-<>-arglist nil))
5895
5896 (c-add-type start (1+ pos))
d9e94c22 5897 (c-forward-syntactic-ws)
0386b551
AM
5898 (setq pos (point)
5899 c-last-identifier-range nil)
5900
d9e94c22
MS
5901 (if (and c-opt-identifier-concat-key
5902 (looking-at c-opt-identifier-concat-key))
0386b551 5903
d9e94c22
MS
5904 ;; Continue if there's an identifier concatenation
5905 ;; operator after the template argument.
5906 (progn
0386b551
AM
5907 (when (and c-record-type-identifiers id-start)
5908 (c-record-ref-id (cons id-start id-end)))
d9e94c22
MS
5909 (forward-char 2)
5910 (c-forward-syntactic-ws)
5911 t)
0386b551
AM
5912
5913 (when (and c-record-type-identifiers id-start)
5914 (c-record-type-id (cons id-start id-end)))
d9e94c22
MS
5915 (setq res 'template)
5916 nil)))
5917 )))))
5918
5919 (goto-char pos)
5920 res))
5921
e15f8aaa 5922(defun c-forward-type (&optional brace-block-too)
d9e94c22 5923 ;; Move forward over a type spec if at the beginning of one,
e15f8aaa
AM
5924 ;; stopping at the next following token. The keyword "typedef"
5925 ;; isn't part of a type spec here.
5926 ;;
5927 ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
5928 ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
5929 ;; The current (2009-03-10) intention is to convert all uses of
5930 ;; `c-forward-type' to call with this parameter set, then to
5931 ;; eliminate it.
5932 ;;
5933 ;; Return
5934 ;; o - t if it's a known type that can't be a name or other
5935 ;; expression;
5936 ;; o - 'known if it's an otherwise known type (according to
5937 ;; `*-font-lock-extra-types');
5938 ;; o - 'prefix if it's a known prefix of a type;
5939 ;; o - 'found if it's a type that matches one in `c-found-types';
5940 ;; o - 'maybe if it's an identfier that might be a type; or
5941 ;; o - nil if it can't be a type (the point isn't moved then).
5942 ;;
5943 ;; The point is assumed to be at the beginning of a token.
d9e94c22
MS
5944 ;;
5945 ;; Note that this function doesn't skip past the brace definition
5946 ;; that might be considered part of the type, e.g.
5947 ;; "enum {a, b, c} foo".
0386b551
AM
5948 ;;
5949 ;; This function records identifier ranges on
5950 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
5951 ;; `c-record-type-identifiers' is non-nil.
5952 ;;
5953 ;; This function might do hidden buffer changes.
0e5cf2b8
AM
5954 (when (and c-recognize-<>-arglists
5955 (looking-at "<"))
452ea855
AM
5956 (c-forward-<>-arglist t)
5957 (c-forward-syntactic-ws))
0386b551
AM
5958
5959 (let ((start (point)) pos res name-res id-start id-end id-range)
d9e94c22
MS
5960
5961 ;; Skip leading type modifiers. If any are found we know it's a
5962 ;; prefix of a type.
e15f8aaa 5963 (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
d9e94c22
MS
5964 (while (looking-at c-opt-type-modifier-key)
5965 (goto-char (match-end 1))
5966 (c-forward-syntactic-ws)
5967 (setq res 'prefix)))
5968
5969 (cond
e15f8aaa
AM
5970 ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
5971 ; "typedef".
d9e94c22
MS
5972 (goto-char (match-end 1))
5973 (c-forward-syntactic-ws)
5974 (setq pos (point))
e15f8aaa
AM
5975
5976 (setq name-res (c-forward-name))
5977 (setq res (not (null name-res)))
5978 (when (eq name-res t)
5979 ;; In many languages the name can be used without the
5980 ;; prefix, so we add it to `c-found-types'.
5981 (c-add-type pos (point))
5982 (when (and c-record-type-identifiers
5983 c-last-identifier-range)
5984 (c-record-type-id c-last-identifier-range)))
5985 (when (and brace-block-too
5986 (memq res '(t nil))
5987 (eq (char-after) ?\{)
5988 (save-excursion
5989 (c-safe
5990 (progn (c-forward-sexp)
5991 (c-forward-syntactic-ws)
5992 (setq pos (point))))))
5993 (goto-char pos)
5994 (setq res t))
5995 (unless res (goto-char start))) ; invalid syntax
d9e94c22
MS
5996
5997 ((progn
5998 (setq pos nil)
5999 (if (looking-at c-identifier-start)
6000 (save-excursion
6001 (setq id-start (point)
0386b551
AM
6002 name-res (c-forward-name))
6003 (when name-res
d9e94c22
MS
6004 (setq id-end (point)
6005 id-range c-last-identifier-range))))
6006 (and (cond ((looking-at c-primitive-type-key)
6007 (setq res t))
6008 ((c-with-syntax-table c-identifier-syntax-table
6009 (looking-at c-known-type-key))
6010 (setq res 'known)))
6011 (or (not id-end)
6012 (>= (save-excursion
6013 (save-match-data
6014 (goto-char (match-end 1))
6015 (c-forward-syntactic-ws)
6016 (setq pos (point))))
6017 id-end)
6018 (setq res nil))))
6019 ;; Looking at a primitive or known type identifier. We've
6020 ;; checked for a name first so that we don't go here if the
6021 ;; known type match only is a prefix of another name.
6022
6023 (setq id-end (match-end 1))
6024
6025 (when (and c-record-type-identifiers
6026 (or c-promote-possible-types (eq res t)))
6027 (c-record-type-id (cons (match-beginning 1) (match-end 1))))
6028
6029 (if (and c-opt-type-component-key
6030 (save-match-data
6031 (looking-at c-opt-type-component-key)))
6032 ;; There might be more keywords for the type.
6033 (let (safe-pos)
0386b551 6034 (c-forward-keyword-clause 1)
d9e94c22
MS
6035 (while (progn
6036 (setq safe-pos (point))
6037 (looking-at c-opt-type-component-key))
6038 (when (and c-record-type-identifiers
6039 (looking-at c-primitive-type-key))
6040 (c-record-type-id (cons (match-beginning 1)
6041 (match-end 1))))
0386b551 6042 (c-forward-keyword-clause 1))
d9e94c22
MS
6043 (if (looking-at c-primitive-type-key)
6044 (progn
6045 (when c-record-type-identifiers
6046 (c-record-type-id (cons (match-beginning 1)
6047 (match-end 1))))
0386b551 6048 (c-forward-keyword-clause 1)
d9e94c22
MS
6049 (setq res t))
6050 (goto-char safe-pos)
6051 (setq res 'prefix)))
0386b551 6052 (unless (save-match-data (c-forward-keyword-clause 1))
d9e94c22
MS
6053 (if pos
6054 (goto-char pos)
6055 (goto-char (match-end 1))
6056 (c-forward-syntactic-ws)))))
6057
0386b551
AM
6058 (name-res
6059 (cond ((eq name-res t)
d9e94c22
MS
6060 ;; A normal identifier.
6061 (goto-char id-end)
6062 (if (or res c-promote-possible-types)
6063 (progn
6064 (c-add-type id-start id-end)
0386b551 6065 (when (and c-record-type-identifiers id-range)
d9e94c22
MS
6066 (c-record-type-id id-range))
6067 (unless res
6068 (setq res 'found)))
6069 (setq res (if (c-check-type id-start id-end)
6070 ;; It's an identifier that has been used as
6071 ;; a type somewhere else.
6072 'found
6073 ;; It's an identifier that might be a type.
6074 'maybe))))
0386b551 6075 ((eq name-res 'template)
d9e94c22
MS
6076 ;; A template is a type.
6077 (goto-char id-end)
6078 (setq res t))
6079 (t
6080 ;; Otherwise it's an operator identifier, which is not a type.
6081 (goto-char start)
6082 (setq res nil)))))
6083
6084 (when res
e15f8aaa 6085 ;; Skip trailing type modifiers. If any are found we know it's
d9e94c22
MS
6086 ;; a type.
6087 (when c-opt-type-modifier-key
e15f8aaa 6088 (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
d9e94c22
MS
6089 (goto-char (match-end 1))
6090 (c-forward-syntactic-ws)
6091 (setq res t)))
d9e94c22
MS
6092 ;; Step over any type suffix operator. Do not let the existence
6093 ;; of these alter the classification of the found type, since
6094 ;; these operators typically are allowed in normal expressions
6095 ;; too.
6096 (when c-opt-type-suffix-key
6097 (while (looking-at c-opt-type-suffix-key)
6098 (goto-char (match-end 1))
6099 (c-forward-syntactic-ws)))
6100
e15f8aaa 6101 (when c-opt-type-concat-key ; Only/mainly for pike.
0386b551
AM
6102 ;; Look for a trailing operator that concatenates the type
6103 ;; with a following one, and if so step past that one through
6104 ;; a recursive call. Note that we don't record concatenated
6105 ;; types in `c-found-types' - it's the component types that
6106 ;; are recorded when appropriate.
d9e94c22
MS
6107 (setq pos (point))
6108 (let* ((c-promote-possible-types (or (memq res '(t known))
6109 c-promote-possible-types))
6110 ;; If we can't promote then set `c-record-found-types' so that
6111 ;; we can merge in the types from the second part afterwards if
6112 ;; it turns out to be a known type there.
6113 (c-record-found-types (and c-record-type-identifiers
0386b551
AM
6114 (not c-promote-possible-types)))
6115 subres)
d9e94c22
MS
6116 (if (and (looking-at c-opt-type-concat-key)
6117
6118 (progn
6119 (goto-char (match-end 1))
6120 (c-forward-syntactic-ws)
0386b551 6121 (setq subres (c-forward-type))))
d9e94c22
MS
6122
6123 (progn
6124 ;; If either operand certainly is a type then both are, but we
6125 ;; don't let the existence of the operator itself promote two
6126 ;; uncertain types to a certain one.
6127 (cond ((eq res t))
0386b551
AM
6128 ((eq subres t)
6129 (unless (eq name-res 'template)
6130 (c-add-type id-start id-end))
6131 (when (and c-record-type-identifiers id-range)
d9e94c22
MS
6132 (c-record-type-id id-range))
6133 (setq res t))
2a15eb73 6134 ((eq res 'known))
0386b551 6135 ((eq subres 'known)
2a15eb73 6136 (setq res 'known))
d9e94c22 6137 ((eq res 'found))
0386b551 6138 ((eq subres 'found)
d9e94c22
MS
6139 (setq res 'found))
6140 (t
6141 (setq res 'maybe)))
6142
6143 (when (and (eq res t)
6144 (consp c-record-found-types))
6145 ;; Merge in the ranges of any types found by the second
6146 ;; `c-forward-type'.
6147 (setq c-record-type-identifiers
6148 ;; `nconc' doesn't mind that the tail of
6149 ;; `c-record-found-types' is t.
6150 (nconc c-record-found-types
6151 c-record-type-identifiers))))
6152
6153 (goto-char pos))))
6154
6155 (when (and c-record-found-types (memq res '(known found)) id-range)
6156 (setq c-record-found-types
6157 (cons id-range c-record-found-types))))
6158
6159 ;;(message "c-forward-type %s -> %s: %s" start (point) res)
6160
6161 res))
6162
452ea855
AM
6163(defun c-forward-annotation ()
6164 ;; Used for Java code only at the moment. Assumes point is on the
6165 ;; @, moves forward an annotation. returns nil if there is no
6166 ;; annotation at point.
6167 (and (looking-at "@")
6168 (progn (forward-char) t)
6169 (c-forward-type)
6170 (progn (c-forward-syntactic-ws) t)
6171 (if (looking-at "(")
6172 (c-go-list-forward)
6173 t)))
6174
785eecbb 6175\f
d9e94c22
MS
6176;; Handling of large scale constructs like statements and declarations.
6177
0386b551
AM
6178;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
6179;; defsubst or perhaps even a defun, but it contains lots of free
6180;; variables that refer to things inside `c-forward-decl-or-cast-1'.
6181(defmacro c-fdoc-shift-type-backward (&optional short)
6182 ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
6183 ;; of types when parsing a declaration, which means that it
6184 ;; sometimes consumes the identifier in the declaration as a type.
6185 ;; This is used to "backtrack" and make the last type be treated as
6186 ;; an identifier instead.
6187 `(progn
6188 ,(unless short
6189 ;; These identifiers are bound only in the inner let.
6190 '(setq identifier-type at-type
6191 identifier-start type-start
6192 got-parens nil
6193 got-identifier t
6194 got-suffix t
6195 got-suffix-after-parens id-start
6196 paren-depth 0))
6197
6198 (if (setq at-type (if (eq backup-at-type 'prefix)
6199 t
6200 backup-at-type))
6201 (setq type-start backup-type-start
6202 id-start backup-id-start)
6203 (setq type-start start-pos
6204 id-start start-pos))
6205
6206 ;; When these flags already are set we've found specifiers that
6207 ;; unconditionally signal these attributes - backtracking doesn't
6208 ;; change that. So keep them set in that case.
6209 (or at-type-decl
6210 (setq at-type-decl backup-at-type-decl))
6211 (or maybe-typeless
6212 (setq maybe-typeless backup-maybe-typeless))
6213
6214 ,(unless short
6215 ;; This identifier is bound only in the inner let.
6216 '(setq start id-start))))
6217
6218(defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
6219 ;; Move forward over a declaration or a cast if at the start of one.
6220 ;; The point is assumed to be at the start of some token. Nil is
6221 ;; returned if no declaration or cast is recognized, and the point
6222 ;; is clobbered in that case.
6223 ;;
6224 ;; If a declaration is parsed:
6225 ;;
6226 ;; The point is left at the first token after the first complete
6227 ;; declarator, if there is one. The return value is a cons where
022d0cf4
AM
6228 ;; the car is the position of the first token in the declarator. (See
6229 ;; below for the cdr.)
0386b551
AM
6230 ;; Some examples:
6231 ;;
6232 ;; void foo (int a, char *b) stuff ...
6233 ;; car ^ ^ point
6234 ;; float (*a)[], b;
6235 ;; car ^ ^ point
6236 ;; unsigned int a = c_style_initializer, b;
6237 ;; car ^ ^ point
6238 ;; unsigned int a (cplusplus_style_initializer), b;
6239 ;; car ^ ^ point (might change)
6240 ;; class Foo : public Bar {}
6241 ;; car ^ ^ point
6242 ;; class PikeClass (int a, string b) stuff ...
6243 ;; car ^ ^ point
6244 ;; enum bool;
6245 ;; car ^ ^ point
6246 ;; enum bool flag;
6247 ;; car ^ ^ point
6248 ;; void cplusplus_function (int x) throw (Bad);
6249 ;; car ^ ^ point
6250 ;; Foo::Foo (int b) : Base (b) {}
6251 ;; car ^ ^ point
e15f8aaa
AM
6252 ;;
6253 ;; The cdr of the return value is non-nil when a
6254 ;; `c-typedef-decl-kwds' specifier is found in the declaration.
6255 ;; Specifically it is a dotted pair (A . B) where B is t when a
6256 ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
6257 ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
6258 ;; specifier is present. I.e., (some of) the declared
6259 ;; identifier(s) are types.
6260 ;;
0386b551
AM
6261 ;; If a cast is parsed:
6262 ;;
6263 ;; The point is left at the first token after the closing paren of
6264 ;; the cast. The return value is `cast'. Note that the start
6265 ;; position must be at the first token inside the cast parenthesis
6266 ;; to recognize it.
6267 ;;
6268 ;; PRECEDING-TOKEN-END is the first position after the preceding
6269 ;; token, i.e. on the other side of the syntactic ws from the point.
6270 ;; Use a value less than or equal to (point-min) if the point is at
6271 ;; the first token in (the visible part of) the buffer.
6272 ;;
6273 ;; CONTEXT is a symbol that describes the context at the point:
022d0cf4 6274 ;; 'decl In a comma-separated declaration context (typically
0386b551
AM
6275 ;; inside a function declaration arglist).
6276 ;; '<> In an angle bracket arglist.
6277 ;; 'arglist Some other type of arglist.
a85fd6da
AM
6278 ;; nil Some other context or unknown context. Includes
6279 ;; within the parens of an if, for, ... construct.
0386b551
AM
6280 ;;
6281 ;; LAST-CAST-END is the first token after the closing paren of a
6282 ;; preceding cast, or nil if none is known. If
6283 ;; `c-forward-decl-or-cast-1' is used in succession, it should be
6284 ;; the position after the closest preceding call where a cast was
6285 ;; matched. In that case it's used to discover chains of casts like
6286 ;; "(a) (b) c".
6287 ;;
6288 ;; This function records identifier ranges on
6289 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
6290 ;; `c-record-type-identifiers' is non-nil.
6291 ;;
6292 ;; This function might do hidden buffer changes.
6293
6294 (let (;; `start-pos' is used below to point to the start of the
6295 ;; first type, i.e. after any leading specifiers. It might
6296 ;; also point at the beginning of the preceding syntactic
6297 ;; whitespace.
6298 (start-pos (point))
6299 ;; Set to the result of `c-forward-type'.
6300 at-type
6301 ;; The position of the first token in what we currently
6302 ;; believe is the type in the declaration or cast, after any
6303 ;; specifiers and their associated clauses.
6304 type-start
6305 ;; The position of the first token in what we currently
6306 ;; believe is the declarator for the first identifier. Set
6307 ;; when the type is found, and moved forward over any
6308 ;; `c-decl-hangon-kwds' and their associated clauses that
6309 ;; occurs after the type.
6310 id-start
6311 ;; These store `at-type', `type-start' and `id-start' of the
6312 ;; identifier before the one in those variables. The previous
6313 ;; identifier might turn out to be the real type in a
6314 ;; declaration if the last one has to be the declarator in it.
6315 ;; If `backup-at-type' is nil then the other variables have
6316 ;; undefined values.
6317 backup-at-type backup-type-start backup-id-start
e15f8aaa
AM
6318 ;; Set if we've found a specifier (apart from "typedef") that makes
6319 ;; the defined identifier(s) types.
0386b551 6320 at-type-decl
e15f8aaa
AM
6321 ;; Set if we've a "typedef" keyword.
6322 at-typedef
0386b551
AM
6323 ;; Set if we've found a specifier that can start a declaration
6324 ;; where there's no type.
6325 maybe-typeless
6326 ;; If a specifier is found that also can be a type prefix,
6327 ;; these flags are set instead of those above. If we need to
6328 ;; back up an identifier, they are copied to the real flag
6329 ;; variables. Thus they only take effect if we fail to
6330 ;; interpret it as a type.
6331 backup-at-type-decl backup-maybe-typeless
6332 ;; Whether we've found a declaration or a cast. We might know
6333 ;; this before we've found the type in it. It's 'ids if we've
6334 ;; found two consecutive identifiers (usually a sure sign, but
6335 ;; we should allow that in labels too), and t if we've found a
6336 ;; specifier keyword (a 100% sure sign).
6337 at-decl-or-cast
6338 ;; Set when we need to back up to parse this as a declaration
6339 ;; but not as a cast.
6340 backup-if-not-cast
6341 ;; For casts, the return position.
6342 cast-end
6343 ;; Save `c-record-type-identifiers' and
6344 ;; `c-record-ref-identifiers' since ranges are recorded
6345 ;; speculatively and should be thrown away if it turns out
6346 ;; that it isn't a declaration or cast.
6347 (save-rec-type-ids c-record-type-identifiers)
6348 (save-rec-ref-ids c-record-ref-identifiers))
6349
452ea855
AM
6350 (while (c-forward-annotation)
6351 (c-forward-syntactic-ws))
6352
0386b551
AM
6353 ;; Check for a type. Unknown symbols are treated as possible
6354 ;; types, but they could also be specifiers disguised through
6355 ;; macros like __INLINE__, so we recognize both types and known
6356 ;; specifiers after them too.
6357 (while
6358 (let* ((start (point)) kwd-sym kwd-clause-end found-type)
6359
6360 ;; Look for a specifier keyword clause.
e3715033
AM
6361 (when (or (looking-at c-prefix-spec-kwds-re)
6362 (and (c-major-mode-is 'java-mode)
6363 (looking-at "@[A-Za-z0-9]+")))
e15f8aaa
AM
6364 (if (looking-at c-typedef-key)
6365 (setq at-typedef t))
0386b551
AM
6366 (setq kwd-sym (c-keyword-sym (match-string 1)))
6367 (save-excursion
6368 (c-forward-keyword-clause 1)
6369 (setq kwd-clause-end (point))))
6370
e15f8aaa 6371 (when (setq found-type (c-forward-type t)) ; brace-block-too
0386b551
AM
6372 ;; Found a known or possible type or a prefix of a known type.
6373
6374 (when at-type
6375 ;; Got two identifiers with nothing but whitespace
6376 ;; between them. That can only happen in declarations.
6377 (setq at-decl-or-cast 'ids)
6378
6379 (when (eq at-type 'found)
6380 ;; If the previous identifier is a found type we
6381 ;; record it as a real one; it might be some sort of
6382 ;; alias for a prefix like "unsigned".
6383 (save-excursion
6384 (goto-char type-start)
6385 (let ((c-promote-possible-types t))
6386 (c-forward-type)))))
6387
6388 (setq backup-at-type at-type
6389 backup-type-start type-start
6390 backup-id-start id-start
6391 at-type found-type
6392 type-start start
6393 id-start (point)
6394 ;; The previous ambiguous specifier/type turned out
6395 ;; to be a type since we've parsed another one after
6396 ;; it, so clear these backup flags.
6397 backup-at-type-decl nil
6398 backup-maybe-typeless nil))
6399
6400 (if kwd-sym
6401 (progn
6402 ;; Handle known specifier keywords and
6403 ;; `c-decl-hangon-kwds' which can occur after known
6404 ;; types.
6405
6406 (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
6407 ;; It's a hang-on keyword that can occur anywhere.
6408 (progn
6409 (setq at-decl-or-cast t)
6410 (if at-type
6411 ;; Move the identifier start position if
6412 ;; we've passed a type.
6413 (setq id-start kwd-clause-end)
6414 ;; Otherwise treat this as a specifier and
6415 ;; move the fallback position.
6416 (setq start-pos kwd-clause-end))
6417 (goto-char kwd-clause-end))
6418
6419 ;; It's an ordinary specifier so we know that
6420 ;; anything before this can't be the type.
6421 (setq backup-at-type nil
6422 start-pos kwd-clause-end)
6423
6424 (if found-type
6425 ;; It's ambiguous whether this keyword is a
6426 ;; specifier or a type prefix, so set the backup
6427 ;; flags. (It's assumed that `c-forward-type'
6428 ;; moved further than `c-forward-keyword-clause'.)
6429 (progn
6430 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
6431 (setq backup-at-type-decl t))
6432 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6433 (setq backup-maybe-typeless t)))
6434
6435 (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
e15f8aaa
AM
6436 ;; This test only happens after we've scanned a type.
6437 ;; So, with valid syntax, kwd-sym can't be 'typedef.
0386b551
AM
6438 (setq at-type-decl t))
6439 (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
6440 (setq maybe-typeless t))
6441
6442 ;; Haven't matched a type so it's an umambiguous
6443 ;; specifier keyword and we know we're in a
6444 ;; declaration.
6445 (setq at-decl-or-cast t)
6446
6447 (goto-char kwd-clause-end))))
6448
6449 ;; If the type isn't known we continue so that we'll jump
6450 ;; over all specifiers and type identifiers. The reason
6451 ;; to do this for a known type prefix is to make things
6452 ;; like "unsigned INT16" work.
6453 (and found-type (not (eq found-type t))))))
6454
6455 (cond
6456 ((eq at-type t)
6457 ;; If a known type was found, we still need to skip over any
6458 ;; hangon keyword clauses after it. Otherwise it has already
6459 ;; been done in the loop above.
6460 (while (looking-at c-decl-hangon-key)
6461 (c-forward-keyword-clause 1))
6462 (setq id-start (point)))
6463
6464 ((eq at-type 'prefix)
6465 ;; A prefix type is itself a primitive type when it's not
6466 ;; followed by another type.
6467 (setq at-type t))
6468
6469 ((not at-type)
6470 ;; Got no type but set things up to continue anyway to handle
6471 ;; the various cases when a declaration doesn't start with a
6472 ;; type.
6473 (setq id-start start-pos))
6474
6475 ((and (eq at-type 'maybe)
6476 (c-major-mode-is 'c++-mode))
6477 ;; If it's C++ then check if the last "type" ends on the form
6478 ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
6479 ;; (con|de)structor.
6480 (save-excursion
6481 (let (name end-2 end-1)
6482 (goto-char id-start)
6483 (c-backward-syntactic-ws)
6484 (setq end-2 (point))
6485 (when (and
6486 (c-simple-skip-symbol-backward)
6487 (progn
6488 (setq name
6489 (buffer-substring-no-properties (point) end-2))
6490 ;; Cheating in the handling of syntactic ws below.
6491 (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
6492 (progn
6493 (setq end-1 (point))
6494 (c-simple-skip-symbol-backward))
6495 (>= (point) type-start)
6496 (equal (buffer-substring-no-properties (point) end-1)
6497 name))
6498 ;; It is a (con|de)structor name. In that case the
6499 ;; declaration is typeless so zap out any preceding
6500 ;; identifier(s) that we might have taken as types.
6501 (goto-char type-start)
6502 (setq at-type nil
6503 backup-at-type nil
6504 id-start type-start))))))
6505
6506 ;; Check for and step over a type decl expression after the thing
6507 ;; that is or might be a type. This can't be skipped since we
6508 ;; need the correct end position of the declarator for
6509 ;; `max-type-decl-end-*'.
6510 (let ((start (point)) (paren-depth 0) pos
6511 ;; True if there's a non-open-paren match of
6512 ;; `c-type-decl-prefix-key'.
6513 got-prefix
6514 ;; True if the declarator is surrounded by a parenthesis pair.
6515 got-parens
6516 ;; True if there is an identifier in the declarator.
6517 got-identifier
6518 ;; True if there's a non-close-paren match of
6519 ;; `c-type-decl-suffix-key'.
6520 got-suffix
6521 ;; True if there's a prefix match outside the outermost
6522 ;; paren pair that surrounds the declarator.
6523 got-prefix-before-parens
1379f2c5 6524 ;; True if there's a suffix match outside the outermost
0386b551
AM
6525 ;; paren pair that surrounds the declarator. The value is
6526 ;; the position of the first suffix match.
6527 got-suffix-after-parens
6528 ;; True if we've parsed the type decl to a token that is
6529 ;; known to end declarations in this context.
6530 at-decl-end
6531 ;; The earlier values of `at-type' and `type-start' if we've
6532 ;; shifted the type backwards.
6533 identifier-type identifier-start
6534 ;; If `c-parse-and-markup-<>-arglists' is set we need to
6535 ;; turn it off during the name skipping below to avoid
6536 ;; getting `c-type' properties that might be bogus. That
6537 ;; can happen since we don't know if
6538 ;; `c-restricted-<>-arglists' will be correct inside the
6539 ;; arglist paren that gets entered.
6540 c-parse-and-markup-<>-arglists)
6541
6542 (goto-char id-start)
6543
6544 ;; Skip over type decl prefix operators. (Note similar code in
6545 ;; `c-font-lock-declarators'.)
6546 (while (and (looking-at c-type-decl-prefix-key)
6547 (if (and (c-major-mode-is 'c++-mode)
05842630 6548 (match-beginning 3))
0386b551
AM
6549 ;; If the second submatch matches in C++ then
6550 ;; we're looking at an identifier that's a
6551 ;; prefix only if it specifies a member pointer.
6552 (when (setq got-identifier (c-forward-name))
6553 (if (looking-at "\\(::\\)")
6554 ;; We only check for a trailing "::" and
6555 ;; let the "*" that should follow be
6556 ;; matched in the next round.
6557 (progn (setq got-identifier nil) t)
6558 ;; It turned out to be the real identifier,
6559 ;; so stop.
6560 nil))
6561 t))
6562
6563 (if (eq (char-after) ?\()
6564 (progn
6565 (setq paren-depth (1+ paren-depth))
6566 (forward-char))
6567 (unless got-prefix-before-parens
6568 (setq got-prefix-before-parens (= paren-depth 0)))
6569 (setq got-prefix t)
6570 (goto-char (match-end 1)))
6571 (c-forward-syntactic-ws))
6572
6573 (setq got-parens (> paren-depth 0))
6574
6575 ;; Skip over an identifier.
6576 (or got-identifier
6577 (and (looking-at c-identifier-start)
6578 (setq got-identifier (c-forward-name))))
6579
6580 ;; Skip over type decl suffix operators.
6581 (while (if (looking-at c-type-decl-suffix-key)
6582
6583 (if (eq (char-after) ?\))
6584 (when (> paren-depth 0)
6585 (setq paren-depth (1- paren-depth))
6586 (forward-char)
6587 t)
6588 (when (if (save-match-data (looking-at "\\s\("))
6589 (c-safe (c-forward-sexp 1) t)
6590 (goto-char (match-end 1))
6591 t)
6592 (when (and (not got-suffix-after-parens)
6593 (= paren-depth 0))
6594 (setq got-suffix-after-parens (match-beginning 0)))
6595 (setq got-suffix t)))
6596
6597 ;; No suffix matched. We might have matched the
6598 ;; identifier as a type and the open paren of a
6599 ;; function arglist as a type decl prefix. In that
6600 ;; case we should "backtrack": Reinterpret the last
6601 ;; type as the identifier, move out of the arglist and
6602 ;; continue searching for suffix operators.
6603 ;;
6604 ;; Do this even if there's no preceding type, to cope
6605 ;; with old style function declarations in K&R C,
6606 ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
6607 ;; style declarations. That isn't applicable in an
6608 ;; arglist context, though.
6609 (when (and (= paren-depth 1)
6610 (not got-prefix-before-parens)
6611 (not (eq at-type t))
6612 (or backup-at-type
6613 maybe-typeless
6614 backup-maybe-typeless
6615 (when c-recognize-typeless-decls
6616 (not context)))
6617 (setq pos (c-up-list-forward (point)))
6618 (eq (char-before pos) ?\)))
6619 (c-fdoc-shift-type-backward)
6620 (goto-char pos)
6621 t))
6622
6623 (c-forward-syntactic-ws))
6624
6625 (when (and (or maybe-typeless backup-maybe-typeless)
6626 (not got-identifier)
6627 (not got-prefix)
6628 at-type)
6629 ;; Have found no identifier but `c-typeless-decl-kwds' has
6630 ;; matched so we know we're inside a declaration. The
6631 ;; preceding type must be the identifier instead.
6632 (c-fdoc-shift-type-backward))
6633
6634 (setq
6635 at-decl-or-cast
6636 (catch 'at-decl-or-cast
6637
a85fd6da 6638 ;; CASE 1
0386b551
AM
6639 (when (> paren-depth 0)
6640 ;; Encountered something inside parens that isn't matched by
6641 ;; the `c-type-decl-*' regexps, so it's not a type decl
6642 ;; expression. Try to skip out to the same paren depth to
6643 ;; not confuse the cast check below.
6644 (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
6645 ;; If we've found a specifier keyword then it's a
6646 ;; declaration regardless.
6647 (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
6648
6649 (setq at-decl-end
6650 (looking-at (cond ((eq context '<>) "[,>]")
6651 (context "[,\)]")
6652 (t "[,;]"))))
6653
6654 ;; Now we've collected info about various characteristics of
6655 ;; the construct we're looking at. Below follows a decision
6656 ;; tree based on that. It's ordered to check more certain
6657 ;; signs before less certain ones.
6658
6659 (if got-identifier
6660 (progn
6661
a85fd6da 6662 ;; CASE 2
0386b551
AM
6663 (when (and (or at-type maybe-typeless)
6664 (not (or got-prefix got-parens)))
6665 ;; Got another identifier directly after the type, so it's a
6666 ;; declaration.
6667 (throw 'at-decl-or-cast t))
6668
6669 (when (and got-parens
6670 (not got-prefix)
6671 (not got-suffix-after-parens)
6672 (or backup-at-type
6673 maybe-typeless
6674 backup-maybe-typeless))
6675 ;; Got a declaration of the form "foo bar (gnu);" where we've
6676 ;; recognized "bar" as the type and "gnu" as the declarator.
6677 ;; In this case it's however more likely that "bar" is the
6678 ;; declarator and "gnu" a function argument or initializer (if
6679 ;; `c-recognize-paren-inits' is set), since the parens around
6680 ;; "gnu" would be superfluous if it's a declarator. Shift the
6681 ;; type one step backward.
6682 (c-fdoc-shift-type-backward)))
6683
6684 ;; Found no identifier.
6685
6686 (if backup-at-type
6687 (progn
6688
b248a85d
AM
6689
6690 ;; CASE 3
6691 (when (= (point) start)
6692 ;; Got a plain list of identifiers. If a colon follows it's
6693 ;; a valid label, or maybe a bitfield. Otherwise the last
6694 ;; one probably is the declared identifier and we should
6695 ;; back up to the previous type, providing it isn't a cast.
452ea855
AM
6696 (if (and (eq (char-after) ?:)
6697 (not (c-major-mode-is 'java-mode)))
b248a85d
AM
6698 (cond
6699 ;; If we've found a specifier keyword then it's a
6700 ;; declaration regardless.
6701 ((eq at-decl-or-cast t)
6702 (throw 'at-decl-or-cast t))
6703 ((and c-has-bitfields
6704 (eq at-decl-or-cast 'ids)) ; bitfield.
6705 (setq backup-if-not-cast t)
6706 (throw 'at-decl-or-cast t)))
6707
6708 (setq backup-if-not-cast t)
6709 (throw 'at-decl-or-cast t)))
0386b551 6710
a85fd6da 6711 ;; CASE 4
0386b551
AM
6712 (when (and got-suffix
6713 (not got-prefix)
6714 (not got-parens))
6715 ;; Got a plain list of identifiers followed by some suffix.
6716 ;; If this isn't a cast then the last identifier probably is
6717 ;; the declared one and we should back up to the previous
6718 ;; type.
6719 (setq backup-if-not-cast t)
6720 (throw 'at-decl-or-cast t)))
6721
a85fd6da 6722 ;; CASE 5
0386b551
AM
6723 (when (eq at-type t)
6724 ;; If the type is known we know that there can't be any
6725 ;; identifier somewhere else, and it's only in declarations in
6726 ;; e.g. function prototypes and in casts that the identifier may
6727 ;; be left out.
6728 (throw 'at-decl-or-cast t))
6729
6730 (when (= (point) start)
6731 ;; Only got a single identifier (parsed as a type so far).
a85fd6da 6732 ;; CASE 6
0386b551
AM
6733 (if (and
6734 ;; Check that the identifier isn't at the start of an
6735 ;; expression.
6736 at-decl-end
6737 (cond
6738 ((eq context 'decl)
6739 ;; Inside an arglist that contains declarations. If K&R
6740 ;; style declarations and parenthesis style initializers
6741 ;; aren't allowed then the single identifier must be a
6742 ;; type, else we require that it's known or found
6743 ;; (primitive types are handled above).
6744 (or (and (not c-recognize-knr-p)
6745 (not c-recognize-paren-inits))
6746 (memq at-type '(known found))))
6747 ((eq context '<>)
6748 ;; Inside a template arglist. Accept known and found
6749 ;; types; other identifiers could just as well be
6750 ;; constants in C++.
6751 (memq at-type '(known found)))))
6752 (throw 'at-decl-or-cast t)
a85fd6da 6753 ;; CASE 7
0386b551
AM
6754 ;; Can't be a valid declaration or cast, but if we've found a
6755 ;; specifier it can't be anything else either, so treat it as
6756 ;; an invalid/unfinished declaration or cast.
6757 (throw 'at-decl-or-cast at-decl-or-cast))))
6758
6759 (if (and got-parens
6760 (not got-prefix)
6761 (not context)
6762 (not (eq at-type t))
6763 (or backup-at-type
6764 maybe-typeless
6765 backup-maybe-typeless
6766 (when c-recognize-typeless-decls
6767 (or (not got-suffix)
6768 (not (looking-at
6769 c-after-suffixed-type-maybe-decl-key))))))
6770 ;; Got an empty paren pair and a preceding type that probably
6771 ;; really is the identifier. Shift the type backwards to make
6772 ;; the last one the identifier. This is analogous to the
6773 ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
6774 ;; above.
6775 ;;
6776 ;; Exception: In addition to the conditions in that
6777 ;; "backtracking" code, do not shift backward if we're not
6778 ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
6779 ;; Since there's no preceding type, the shift would mean that
6780 ;; the declaration is typeless. But if the regexp doesn't match
6781 ;; then we will simply fall through in the tests below and not
6782 ;; recognize it at all, so it's better to try it as an abstract
6783 ;; declarator instead.
6784 (c-fdoc-shift-type-backward)
6785
6786 ;; Still no identifier.
a85fd6da 6787 ;; CASE 8
0386b551
AM
6788 (when (and got-prefix (or got-parens got-suffix))
6789 ;; Require `got-prefix' together with either `got-parens' or
6790 ;; `got-suffix' to recognize it as an abstract declarator:
6791 ;; `got-parens' only is probably an empty function call.
6792 ;; `got-suffix' only can build an ordinary expression together
6793 ;; with the preceding identifier which we've taken as a type.
6794 ;; We could actually accept on `got-prefix' only, but that can
6795 ;; easily occur temporarily while writing an expression so we
6796 ;; avoid that case anyway. We could do a better job if we knew
6797 ;; the point when the fontification was invoked.
6798 (throw 'at-decl-or-cast t))
6799
a85fd6da 6800 ;; CASE 9
0386b551
AM
6801 (when (and at-type
6802 (not got-prefix)
6803 (not got-parens)
6804 got-suffix-after-parens
6805 (eq (char-after got-suffix-after-parens) ?\())
6806 ;; Got a type, no declarator but a paren suffix. I.e. it's a
6807 ;; normal function call afterall (or perhaps a C++ style object
6808 ;; instantiation expression).
6809 (throw 'at-decl-or-cast nil))))
6810
a85fd6da 6811 ;; CASE 10
0386b551
AM
6812 (when at-decl-or-cast
6813 ;; By now we've located the type in the declaration that we know
6814 ;; we're in.
6815 (throw 'at-decl-or-cast t))
6816
a85fd6da 6817 ;; CASE 11
0386b551
AM
6818 (when (and got-identifier
6819 (not context)
6820 (looking-at c-after-suffixed-type-decl-key)
6821 (if (and got-parens
6822 (not got-prefix)
6823 (not got-suffix)
6824 (not (eq at-type t)))
6825 ;; Shift the type backward in the case that there's a
6826 ;; single identifier inside parens. That can only
6827 ;; occur in K&R style function declarations so it's
6828 ;; more likely that it really is a function call.
6829 ;; Therefore we only do this after
6830 ;; `c-after-suffixed-type-decl-key' has matched.
6831 (progn (c-fdoc-shift-type-backward) t)
6832 got-suffix-after-parens))
6833 ;; A declaration according to `c-after-suffixed-type-decl-key'.
6834 (throw 'at-decl-or-cast t))
6835
a85fd6da 6836 ;; CASE 12
0386b551
AM
6837 (when (and (or got-prefix (not got-parens))
6838 (memq at-type '(t known)))
6839 ;; It's a declaration if a known type precedes it and it can't be a
6840 ;; function call.
6841 (throw 'at-decl-or-cast t))
6842
6843 ;; If we get here we can't tell if this is a type decl or a normal
6844 ;; expression by looking at it alone. (That's under the assumption
6845 ;; that normal expressions always can look like type decl expressions,
6846 ;; which isn't really true but the cases where it doesn't hold are so
6847 ;; uncommon (e.g. some placements of "const" in C++) it's not worth
6848 ;; the effort to look for them.)
6849
6850 (unless (or at-decl-end (looking-at "=[^=]"))
6851 ;; If this is a declaration it should end here or its initializer(*)
6852 ;; should start here, so check for allowed separation tokens. Note
6853 ;; that this rule doesn't work e.g. with a K&R arglist after a
6854 ;; function header.
6855 ;;
6856 ;; *) Don't check for C++ style initializers using parens
6857 ;; since those already have been matched as suffixes.
6858 ;;
6859 ;; If `at-decl-or-cast' is then we've found some other sign that
6860 ;; it's a declaration or cast, so then it's probably an
6861 ;; invalid/unfinished one.
6862 (throw 'at-decl-or-cast at-decl-or-cast))
6863
6864 ;; Below are tests that only should be applied when we're certain to
6865 ;; not have parsed halfway through an expression.
6866
a85fd6da 6867 ;; CASE 14
0386b551
AM
6868 (when (memq at-type '(t known))
6869 ;; The expression starts with a known type so treat it as a
6870 ;; declaration.
6871 (throw 'at-decl-or-cast t))
6872
a85fd6da 6873 ;; CASE 15
0386b551
AM
6874 (when (and (c-major-mode-is 'c++-mode)
6875 ;; In C++ we check if the identifier is a known type, since
6876 ;; (con|de)structors use the class name as identifier.
6877 ;; We've always shifted over the identifier as a type and
6878 ;; then backed up again in this case.
6879 identifier-type
6880 (or (memq identifier-type '(found known))
6881 (and (eq (char-after identifier-start) ?~)
6882 ;; `at-type' probably won't be 'found for
6883 ;; destructors since the "~" is then part of the
6884 ;; type name being checked against the list of
6885 ;; known types, so do a check without that
6886 ;; operator.
6887 (or (save-excursion
6888 (goto-char (1+ identifier-start))
6889 (c-forward-syntactic-ws)
6890 (c-with-syntax-table
6891 c-identifier-syntax-table
6892 (looking-at c-known-type-key)))
6893 (save-excursion
6894 (goto-char (1+ identifier-start))
6895 ;; We have already parsed the type earlier,
6896 ;; so it'd be possible to cache the end
6897 ;; position instead of redoing it here, but
6898 ;; then we'd need to keep track of another
6899 ;; position everywhere.
6900 (c-check-type (point)
6901 (progn (c-forward-type)
6902 (point))))))))
6903 (throw 'at-decl-or-cast t))
6904
6905 (if got-identifier
6906 (progn
a85fd6da 6907 ;; CASE 16
0386b551
AM
6908 (when (and got-prefix-before-parens
6909 at-type
6910 (or at-decl-end (looking-at "=[^=]"))
6911 (not context)
6912 (not got-suffix))
6913 ;; Got something like "foo * bar;". Since we're not inside an
6914 ;; arglist it would be a meaningless expression because the
6915 ;; result isn't used. We therefore choose to recognize it as
6916 ;; a declaration. Do not allow a suffix since it could then
6917 ;; be a function call.
6918 (throw 'at-decl-or-cast t))
6919
a85fd6da 6920 ;; CASE 17
0386b551
AM
6921 (when (and (or got-suffix-after-parens
6922 (looking-at "=[^=]"))
6923 (eq at-type 'found)
6924 (not (eq context 'arglist)))
6925 ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
6926 ;; be an odd expression or it could be a declaration. Treat
6927 ;; it as a declaration if "a" has been used as a type
6928 ;; somewhere else (if it's a known type we won't get here).
6929 (throw 'at-decl-or-cast t)))
6930
a85fd6da 6931 ;; CASE 18
0386b551
AM
6932 (when (and context
6933 (or got-prefix
6934 (and (eq context 'decl)
6935 (not c-recognize-paren-inits)
6936 (or got-parens got-suffix))))
6937 ;; Got a type followed by an abstract declarator. If `got-prefix'
6938 ;; is set it's something like "a *" without anything after it. If
6939 ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
6940 ;; or similar, which we accept only if the context rules out
6941 ;; expressions.
6942 (throw 'at-decl-or-cast t)))
6943
6944 ;; If we had a complete symbol table here (which rules out
6945 ;; `c-found-types') we should return t due to the disambiguation rule
6946 ;; (in at least C++) that anything that can be parsed as a declaration
6947 ;; is a declaration. Now we're being more defensive and prefer to
6948 ;; highlight things like "foo (bar);" as a declaration only if we're
6949 ;; inside an arglist that contains declarations.
6950 (eq context 'decl))))
6951
6952 ;; The point is now after the type decl expression.
6953
6954 (cond
6955 ;; Check for a cast.
6956 ((save-excursion
6957 (and
6958 c-cast-parens
6959
6960 ;; Should be the first type/identifier in a cast paren.
6961 (> preceding-token-end (point-min))
6962 (memq (char-before preceding-token-end) c-cast-parens)
6963
6964 ;; The closing paren should follow.
6965 (progn
6966 (c-forward-syntactic-ws)
6967 (looking-at "\\s\)"))
6968
6969 ;; There should be a primary expression after it.
6970 (let (pos)
6971 (forward-char)
6972 (c-forward-syntactic-ws)
6973 (setq cast-end (point))
6974 (and (looking-at c-primary-expr-regexp)
6975 (progn
6976 (setq pos (match-end 0))
6977 (or
6978 ;; Check if the expression begins with a prefix keyword.
6979 (match-beginning 2)
6980 (if (match-beginning 1)
6981 ;; Expression begins with an ambiguous operator. Treat
6982 ;; it as a cast if it's a type decl or if we've
6983 ;; recognized the type somewhere else.
6984 (or at-decl-or-cast
6985 (memq at-type '(t known found)))
6986 ;; Unless it's a keyword, it's the beginning of a primary
6987 ;; expression.
6988 (not (looking-at c-keywords-regexp)))))
6989 ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
6990 ;; that it matched a whole one so that we don't e.g. confuse
6991 ;; the operator '-' with '->'. It's ok if it matches further,
6992 ;; though, since it e.g. can match the float '.5' while the
6993 ;; operator regexp only matches '.'.
6994 (or (not (looking-at c-nonsymbol-token-regexp))
6995 (<= (match-end 0) pos))))
6996
6997 ;; There should either be a cast before it or something that isn't an
6998 ;; identifier or close paren.
6999 (> preceding-token-end (point-min))
7000 (progn
7001 (goto-char (1- preceding-token-end))
7002 (or (eq (point) last-cast-end)
7003 (progn
7004 (c-backward-syntactic-ws)
7005 (if (< (skip-syntax-backward "w_") 0)
7006 ;; It's a symbol. Accept it only if it's one of the
7007 ;; keywords that can precede an expression (without
7008 ;; surrounding parens).
7009 (looking-at c-simple-stmt-key)
7010 (and
7011 ;; Check that it isn't a close paren (block close is ok,
7012 ;; though).
7013 (not (memq (char-before) '(?\) ?\])))
7014 ;; Check that it isn't a nonsymbol identifier.
7015 (not (c-on-identifier)))))))))
7016
7017 ;; Handle the cast.
7018 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7019 (let ((c-promote-possible-types t))
7020 (goto-char type-start)
7021 (c-forward-type)))
7022
7023 (goto-char cast-end)
7024 'cast)
7025
7026 (at-decl-or-cast
7027 ;; We're at a declaration. Highlight the type and the following
7028 ;; declarators.
7029
7030 (when backup-if-not-cast
7031 (c-fdoc-shift-type-backward t))
7032
7033 (when (and (eq context 'decl) (looking-at ","))
7034 ;; Make sure to propagate the `c-decl-arg-start' property to
7035 ;; the next argument if it's set in this one, to cope with
7036 ;; interactive refontification.
7037 (c-put-c-type-property (point) 'c-decl-arg-start))
7038
7039 (when (and c-record-type-identifiers at-type (not (eq at-type t)))
7040 (let ((c-promote-possible-types t))
7041 (save-excursion
7042 (goto-char type-start)
7043 (c-forward-type))))
7044
e15f8aaa
AM
7045 (cons id-start
7046 (and (or at-type-decl at-typedef)
7047 (cons at-type-decl at-typedef))))
0386b551
AM
7048
7049 (t
7050 ;; False alarm. Restore the recorded ranges.
7051 (setq c-record-type-identifiers save-rec-type-ids
7052 c-record-ref-identifiers save-rec-ref-ids)
7053 nil))))
7054
7055(defun c-forward-label (&optional assume-markup preceding-token-end limit)
51c9af45 7056 ;; Assuming that point is at the beginning of a token, check if it starts a
1379f2c5
AM
7057 ;; label and if so move over it and return non-nil (t in default situations,
7058 ;; specific symbols (see below) for interesting situations), otherwise don't
7059 ;; move and return nil. "Label" here means "most things with a colon".
51c9af45
AM
7060 ;;
7061 ;; More precisely, a "label" is regarded as one of:
1379f2c5
AM
7062 ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
7063 ;; (ii) A case label - either the entire construct "case FOO:", or just the
7064 ;; bare "case", should the colon be missing. We return t;
7065 ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
7066 ;; return t;
51c9af45 7067 ;; (iv) One of QT's "extended" C++ variants of
1379f2c5
AM
7068 ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
7069 ;; Returns the symbol `qt-2kwds-colon'.
7070 ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
82ba65cf 7071 ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
51c9af45 7072 ;; colon). Currently (2006-03), this applies only to Objective C's
1379f2c5 7073 ;; keywords "@private", "@protected", and "@public". Returns t.
51c9af45
AM
7074 ;;
7075 ;; One of the things which will NOT be recognised as a label is a bit-field
7076 ;; element of a struct, something like "int foo:5".
7077 ;;
7078 ;; The end of the label is taken to be just after the colon, or the end of
7079 ;; the first submatch in `c-opt-extra-label-key'. The point is directly
7080 ;; after the end on return. The terminating char gets marked with
7081 ;; `c-decl-end' to improve recognition of the following declaration or
7082 ;; statement.
0386b551
AM
7083 ;;
7084 ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
51c9af45 7085 ;; label, if any, has already been marked up like that.
0386b551
AM
7086 ;;
7087 ;; If PRECEDING-TOKEN-END is given, it should be the first position
7088 ;; after the preceding token, i.e. on the other side of the
7089 ;; syntactic ws from the point. Use a value less than or equal to
7090 ;; (point-min) if the point is at the first token in (the visible
7091 ;; part of) the buffer.
7092 ;;
7093 ;; The optional LIMIT limits the forward scan for the colon.
7094 ;;
7095 ;; This function records the ranges of the label symbols on
7096 ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
7097 ;; non-nil.
7098 ;;
7099 ;; This function might do hidden buffer changes.
7100
51c9af45 7101 (let ((start (point))
1379f2c5 7102 label-end
51c9af45 7103 qt-symbol-idx
1379f2c5 7104 macro-start ; if we're in one.
f412a567
AM
7105 label-type
7106 kwd)
0386b551 7107 (cond
b414f371 7108 ;; "case" or "default" (Doesn't apply to AWK).
0386b551
AM
7109 ((looking-at c-label-kwds-regexp)
7110 (let ((kwd-end (match-end 1)))
7111 ;; Record only the keyword itself for fontification, since in
7112 ;; case labels the following is a constant expression and not
7113 ;; a label.
7114 (when c-record-type-identifiers
7115 (c-record-ref-id (cons (match-beginning 1) kwd-end)))
7116
7117 ;; Find the label end.
7118 (goto-char kwd-end)
1379f2c5
AM
7119 (setq label-type
7120 (if (and (c-syntactic-re-search-forward
7121 ;; Stop on chars that aren't allowed in expressions,
7122 ;; and on operator chars that would be meaningless
7123 ;; there. FIXME: This doesn't cope with ?: operators.
7124 "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
7125 limit t t nil 1)
7126 (match-beginning 2))
7127
7128 (progn ; there's a proper :
7129 (goto-char (match-beginning 2)) ; just after the :
7130 (c-put-c-type-property (1- (point)) 'c-decl-end)
7131 t)
7132
7133 ;; It's an unfinished label. We consider the keyword enough
7134 ;; to recognize it as a label, so that it gets fontified.
7135 ;; Leave the point at the end of it, but don't put any
7136 ;; `c-decl-end' marker.
7137 (goto-char kwd-end)
7138 t))))
0386b551 7139
51c9af45 7140 ;; @private, @protected, @public, in Objective C, or similar.
0386b551
AM
7141 ((and c-opt-extra-label-key
7142 (looking-at c-opt-extra-label-key))
7143 ;; For a `c-opt-extra-label-key' match, we record the whole
7144 ;; thing for fontification. That's to get the leading '@' in
7145 ;; Objective-C protection labels fontified.
7146 (goto-char (match-end 1))
7147 (when c-record-type-identifiers
7148 (c-record-ref-id (cons (match-beginning 1) (point))))
7149 (c-put-c-type-property (1- (point)) 'c-decl-end)
1379f2c5 7150 (setq label-type t))
0386b551 7151
51c9af45
AM
7152 ;; All other cases of labels.
7153 ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
0386b551
AM
7154
7155 ;; A colon label must have something before the colon.
7156 (not (eq (char-after) ?:))
7157
7158 ;; Check that we're not after a token that can't precede a label.
7159 (or
7160 ;; Trivially succeeds when there's no preceding token.
7161 (if preceding-token-end
7162 (<= preceding-token-end (point-min))
7163 (save-excursion
7164 (c-backward-syntactic-ws)
7165 (setq preceding-token-end (point))
7166 (bobp)))
7167
7168 ;; Check if we're after a label, if we're after a closing
7169 ;; paren that belong to statement, and with
7170 ;; `c-label-prefix-re'. It's done in different order
7171 ;; depending on `assume-markup' since the checks have
7172 ;; different expensiveness.
7173 (if assume-markup
7174 (or
7175 (eq (c-get-char-property (1- preceding-token-end) 'c-type)
7176 'c-decl-end)
7177
7178 (save-excursion
7179 (goto-char (1- preceding-token-end))
7180 (c-beginning-of-current-token)
51c9af45
AM
7181 (or (looking-at c-label-prefix-re)
7182 (looking-at c-block-stmt-1-key)))
0386b551
AM
7183
7184 (and (eq (char-before preceding-token-end) ?\))
7185 (c-after-conditional)))
7186
7187 (or
7188 (save-excursion
7189 (goto-char (1- preceding-token-end))
7190 (c-beginning-of-current-token)
51c9af45
AM
7191 (or (looking-at c-label-prefix-re)
7192 (looking-at c-block-stmt-1-key)))
0386b551
AM
7193
7194 (cond
7195 ((eq (char-before preceding-token-end) ?\))
7196 (c-after-conditional))
7197
7198 ((eq (char-before preceding-token-end) ?:)
7199 ;; Might be after another label, so check it recursively.
51c9af45
AM
7200 (save-restriction
7201 (save-excursion
7202 (goto-char (1- preceding-token-end))
7203 ;; Essentially the same as the
7204 ;; `c-syntactic-re-search-forward' regexp below.
7205 (setq macro-start
7206 (save-excursion (and (c-beginning-of-macro)
7207 (point))))
7208 (if macro-start (narrow-to-region macro-start (point-max)))
7209 (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
7210 ;; Note: the following should work instead of the
7211 ;; narrow-to-region above. Investigate why not,
7212 ;; sometime. ACM, 2006-03-31.
7213 ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
7214 ;; macro-start t)
7215 (let ((pte (point))
7216 ;; If the caller turned on recording for us,
7217 ;; it shouldn't apply when we check the
7218 ;; preceding label.
7219 c-record-type-identifiers)
7220 ;; A label can't start at a cpp directive. Check for
7221 ;; this, since c-forward-syntactic-ws would foul up on it.
7222 (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
7223 (c-forward-syntactic-ws)
7224 (c-forward-label nil pte start))))))))))
7225
1379f2c5 7226 ;; Point is still at the beginning of the possible label construct.
b414f371 7227 ;;
51c9af45
AM
7228 ;; Check that the next nonsymbol token is ":", or that we're in one
7229 ;; of QT's "slots" declarations. Allow '(' for the sake of macro
7230 ;; arguments. FIXME: Should build this regexp from the language
7231 ;; constants.
1379f2c5
AM
7232 (cond
7233 ;; public: protected: private:
7234 ((and
7235 (c-major-mode-is 'c++-mode)
7236 (search-forward-regexp
7237 "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
7238 (progn (backward-char)
7239 (c-forward-syntactic-ws limit)
7240 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
7241 (forward-char)
7242 (setq label-type t))
7243 ;; QT double keyword like "protected slots:" or goto target.
7244 ((progn (goto-char start) nil))
7245 ((when (c-syntactic-re-search-forward
7246 "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
7247 (backward-char)
7248 (setq label-end (point))
7249 (setq qt-symbol-idx
7250 (and (c-major-mode-is 'c++-mode)
7251 (string-match
7252 "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
b414f371 7253 (buffer-substring start (point)))))
1379f2c5
AM
7254 (c-forward-syntactic-ws limit)
7255 (cond
7256 ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
7257 (forward-char)
7258 (setq label-type
f412a567
AM
7259 (if (or (string= "signals" ; Special QT macro
7260 (setq kwd (buffer-substring-no-properties start label-end)))
7261 (string= "Q_SIGNALS" kwd))
1379f2c5
AM
7262 'qt-1kwd-colon
7263 'goto-target)))
7264 ((and qt-symbol-idx
f412a567 7265 (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
1379f2c5
AM
7266 (progn (c-forward-syntactic-ws limit)
7267 (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
7268 (forward-char)
7269 (setq label-type 'qt-2kwds-colon)))))))
0386b551
AM
7270
7271 (save-restriction
7272 (narrow-to-region start (point))
7273
7274 ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
7275 (catch 'check-label
7276 (goto-char start)
7277 (while (progn
7278 (when (looking-at c-nonlabel-token-key)
7279 (goto-char start)
1379f2c5 7280 (setq label-type nil)
0386b551
AM
7281 (throw 'check-label nil))
7282 (and (c-safe (c-forward-sexp)
7283 (c-forward-syntactic-ws)
7284 t)
7285 (not (eobp)))))
7286
7287 ;; Record the identifiers in the label for fontification, unless
7288 ;; it begins with `c-label-kwds' in which case the following
7289 ;; identifiers are part of a (constant) expression that
7290 ;; shouldn't be fontified.
7291 (when (and c-record-type-identifiers
7292 (progn (goto-char start)
7293 (not (looking-at c-label-kwds-regexp))))
7294 (while (c-syntactic-re-search-forward c-symbol-key nil t)
7295 (c-record-ref-id (cons (match-beginning 0)
7296 (match-end 0)))))
7297
7298 (c-put-c-type-property (1- (point-max)) 'c-decl-end)
82ba65cf 7299 (goto-char (point-max)))))
0386b551
AM
7300
7301 (t
7302 ;; Not a label.
1379f2c5
AM
7303 (goto-char start)))
7304 label-type))
0386b551
AM
7305
7306(defun c-forward-objc-directive ()
7307 ;; Assuming the point is at the beginning of a token, try to move
7308 ;; forward to the end of the Objective-C directive that starts
7309 ;; there. Return t if a directive was fully recognized, otherwise
7310 ;; the point is moved as far as one could be successfully parsed and
7311 ;; nil is returned.
7312 ;;
7313 ;; This function records identifier ranges on
7314 ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
7315 ;; `c-record-type-identifiers' is non-nil.
7316 ;;
7317 ;; This function might do hidden buffer changes.
7318
7319 (let ((start (point))
7320 start-char
7321 (c-promote-possible-types t)
7322 ;; Turn off recognition of angle bracket arglists while parsing
7323 ;; types here since the protocol reference list might then be
7324 ;; considered part of the preceding name or superclass-name.
7325 c-recognize-<>-arglists)
7326
7327 (if (or
7328 (when (looking-at
7329 (eval-when-compile
7330 (c-make-keywords-re t
7331 (append (c-lang-const c-protection-kwds objc)
7332 '("@end"))
7333 'objc-mode)))
7334 (goto-char (match-end 1))
7335 t)
7336
7337 (and
7338 (looking-at
7339 (eval-when-compile
7340 (c-make-keywords-re t
7341 '("@interface" "@implementation" "@protocol")
7342 'objc-mode)))
7343
7344 ;; Handle the name of the class itself.
7345 (progn
cb694ab7
AM
7346; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
7347; at EOB.
7348 (goto-char (match-end 0))
7349 (c-skip-ws-forward)
0386b551
AM
7350 (c-forward-type))
7351
7352 (catch 'break
7353 ;; Look for ": superclass-name" or "( category-name )".
7354 (when (looking-at "[:\(]")
7355 (setq start-char (char-after))
7356 (forward-char)
7357 (c-forward-syntactic-ws)
7358 (unless (c-forward-type) (throw 'break nil))
7359 (when (eq start-char ?\()
7360 (unless (eq (char-after) ?\)) (throw 'break nil))
7361 (forward-char)
7362 (c-forward-syntactic-ws)))
7363
7364 ;; Look for a protocol reference list.
7365 (if (eq (char-after) ?<)
7366 (let ((c-recognize-<>-arglists t)
7367 (c-parse-and-markup-<>-arglists t)
7368 c-restricted-<>-arglists)
7369 (c-forward-<>-arglist t))
7370 t))))
7371
7372 (progn
7373 (c-backward-syntactic-ws)
7374 (c-clear-c-type-property start (1- (point)) 'c-decl-end)
7375 (c-put-c-type-property (1- (point)) 'c-decl-end)
7376 t)
7377
7378 (c-clear-c-type-property start (point) 'c-decl-end)
7379 nil)))
7380
785eecbb
RS
7381(defun c-beginning-of-inheritance-list (&optional lim)
7382 ;; Go to the first non-whitespace after the colon that starts a
7383 ;; multiple inheritance introduction. Optional LIM is the farthest
7384 ;; back we should search.
0386b551
AM
7385 ;;
7386 ;; This function might do hidden buffer changes.
7387 (c-with-syntax-table c++-template-syntax-table
7388 (c-backward-token-2 0 t lim)
7389 (while (and (or (looking-at c-symbol-start)
7390 (looking-at "[<,]\\|::"))
7391 (zerop (c-backward-token-2 1 t lim))))))
785eecbb 7392
785eecbb
RS
7393(defun c-in-method-def-p ()
7394 ;; Return nil if we aren't in a method definition, otherwise the
7395 ;; position of the initial [+-].
0386b551
AM
7396 ;;
7397 ;; This function might do hidden buffer changes.
785eecbb
RS
7398 (save-excursion
7399 (beginning-of-line)
a66cd3ee
MS
7400 (and c-opt-method-key
7401 (looking-at c-opt-method-key)
785eecbb
RS
7402 (point))
7403 ))
7404
a66cd3ee
MS
7405;; Contributed by Kevin Ryde <user42@zip.com.au>.
7406(defun c-in-gcc-asm-p ()
7407 ;; Return non-nil if point is within a gcc \"asm\" block.
7408 ;;
7409 ;; This should be called with point inside an argument list.
7410 ;;
7411 ;; Only one level of enclosing parentheses is considered, so for
7412 ;; instance `nil' is returned when in a function call within an asm
7413 ;; operand.
0386b551
AM
7414 ;;
7415 ;; This function might do hidden buffer changes.
a66cd3ee
MS
7416
7417 (and c-opt-asm-stmt-key
7418 (save-excursion
7419 (beginning-of-line)
7420 (backward-up-list 1)
7421 (c-beginning-of-statement-1 (point-min) nil t)
7422 (looking-at c-opt-asm-stmt-key))))
7423
abb7e5cf 7424(defun c-at-toplevel-p ()
a85fd6da
AM
7425 "Return a determination as to whether point is \"at the top level\".
7426Informally, \"at the top level\" is anywhere where you can write
7427a function.
7428
7429More precisely, being at the top-level means that point is either
7430outside any enclosing block (such as a function definition), or
7431directly inside a class, namespace or other block that contains
7432another declaration level.
abb7e5cf
SM
7433
7434If point is not at the top-level (e.g. it is inside a method
7435definition), then nil is returned. Otherwise, if point is at a
7436top-level not enclosed within a class definition, t is returned.
7437Otherwise, a 2-vector is returned where the zeroth element is the
7438buffer position of the start of the class declaration, and the first
7439element is the buffer position of the enclosing class's opening
0386b551
AM
7440brace.
7441
7442Note that this function might do hidden buffer changes. See the
7443comment at the start of cc-engine.el for more info."
a66cd3ee
MS
7444 (let ((paren-state (c-parse-state)))
7445 (or (not (c-most-enclosing-brace paren-state))
7446 (c-search-uplist-for-classkey paren-state))))
7447
d9e94c22 7448(defun c-just-after-func-arglist-p (&optional lim)
0386b551
AM
7449 ;; Return non-nil if the point is in the region after the argument
7450 ;; list of a function and its opening brace (or semicolon in case it
7451 ;; got no body). If there are K&R style argument declarations in
7452 ;; that region, the point has to be inside the first one for this
7453 ;; function to recognize it.
a66cd3ee 7454 ;;
0386b551
AM
7455 ;; If successful, the point is moved to the first token after the
7456 ;; function header (see `c-forward-decl-or-cast-1' for details) and
7457 ;; the position of the opening paren of the function arglist is
7458 ;; returned.
7459 ;;
7460 ;; The point is clobbered if not successful.
7461 ;;
7462 ;; LIM is used as bound for backward buffer searches.
7463 ;;
7464 ;; This function might do hidden buffer changes.
7465
7466 (let ((beg (point)) end id-start)
7467 (and
7468 (eq (c-beginning-of-statement-1 lim) 'same)
7469
7470 (not (or (c-major-mode-is 'objc-mode)
7471 (c-forward-objc-directive)))
7472
7473 (setq id-start
7474 (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
7475 (< id-start beg)
7476
7477 ;; There should not be a '=' or ',' between beg and the
7478 ;; start of the declaration since that means we were in the
7479 ;; "expression part" of the declaration.
7480 (or (> (point) beg)
7481 (not (looking-at "[=,]")))
7482
7483 (save-excursion
7484 ;; Check that there's an arglist paren in the
7485 ;; declaration.
7486 (goto-char id-start)
7487 (cond ((eq (char-after) ?\()
7488 ;; The declarator is a paren expression, so skip past it
7489 ;; so that we don't get stuck on that instead of the
7490 ;; function arglist.
7491 (c-forward-sexp))
51c9af45
AM
7492 ((and c-opt-op-identifier-prefix
7493 (looking-at c-opt-op-identifier-prefix))
0386b551
AM
7494 ;; Don't trip up on "operator ()".
7495 (c-forward-token-2 2 t)))
7496 (and (< (point) beg)
7497 (c-syntactic-re-search-forward "(" beg t t)
7498 (1- (point)))))))
785eecbb 7499
a66cd3ee
MS
7500(defun c-in-knr-argdecl (&optional lim)
7501 ;; Return the position of the first argument declaration if point is
7502 ;; inside a K&R style argument declaration list, nil otherwise.
7503 ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
7504 ;; position that bounds the backward search for the argument list.
7505 ;;
9cf17ef1
AM
7506 ;; Point must be within a possible K&R region, e.g. just before a top-level
7507 ;; "{". It must be outside of parens and brackets. The test can return
7508 ;; false positives otherwise.
0386b551
AM
7509 ;;
7510 ;; This function might do hidden buffer changes.
d9e94c22 7511
a66cd3ee
MS
7512 (save-excursion
7513 (save-restriction
9cf17ef1
AM
7514 ;; If we're in a macro, our search range is restricted to it. Narrow to
7515 ;; the searchable range.
7516 (let* ((macro-start (c-query-macro-start))
7517 (lim (max (or lim (point-min)) (or macro-start (point-min))))
6b6481ed
AM
7518 before-lparen after-rparen
7519 (pp-count-out 20)) ; Max number of paren/brace constructs before we give up
9cf17ef1
AM
7520 (narrow-to-region lim (c-point 'eol))
7521
7522 ;; Search backwards for the defun's argument list. We give up if we
7523 ;; encounter a "}" (end of a previous defun) or BOB.
7524 ;;
7525 ;; The criterion for a paren structure being the arg list is:
7526 ;; o - there is non-WS stuff after it but before any "{"; AND
7527 ;; o - the token after it isn't a ";" AND
7528 ;; o - it is preceded by either an identifier (the function name) or
7529 ;; a macro expansion like "DEFUN (...)"; AND
7530 ;; o - its content is a non-empty comma-separated list of identifiers
7531 ;; (an empty arg list won't have a knr region).
7532 ;;
7533 ;; The following snippet illustrates these rules:
7534 ;; int foo (bar, baz, yuk)
7535 ;; int bar [] ;
7536 ;; int (*baz) (my_type) ;
7537 ;; int (*) (void) (*yuk) (void) ;
7538 ;; {
7539
7540 (catch 'knr
6b6481ed
AM
7541 (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
7542 (setq pp-count-out (1- pp-count-out))
10489fcb 7543 (c-syntactic-skip-backward "^)]}")
9cf17ef1
AM
7544 (cond ((eq (char-before) ?\))
7545 (setq after-rparen (point)))
10489fcb
AM
7546 ((eq (char-before) ?\])
7547 (setq after-rparen nil))
7548 (t ; either } (hit previous defun) or no more parens/brackets
7549 (throw 'knr nil)))
9cf17ef1
AM
7550
7551 (if after-rparen
7552 ;; We're inside a paren. Could it be our argument list....?
7553 (if
7554 (and
7555 (progn
7556 (goto-char after-rparen)
7557 (unless (c-go-list-backward) (throw 'knr nil)) ;
7558 ;; FIXME!!! What about macros between the parens? 2007/01/20
7559 (setq before-lparen (point)))
d9e94c22 7560
9cf17ef1
AM
7561 ;; It can't be the arg list if next token is ; or {
7562 (progn (goto-char after-rparen)
7563 (c-forward-syntactic-ws)
7564 (not (memq (char-after) '(?\; ?\{))))
d9e94c22 7565
9cf17ef1
AM
7566 ;; Is the thing preceding the list an identifier (the
7567 ;; function name), or a macro expansion?
7568 (progn
7569 (goto-char before-lparen)
7570 (eq (c-backward-token-2) 0)
7571 (or (c-on-identifier)
7572 (and (eq (char-after) ?\))
7573 (c-go-up-list-backward)
7574 (eq (c-backward-token-2) 0)
7575 (c-on-identifier))))
7576
7577 ;; Have we got a non-empty list of comma-separated
7578 ;; identifiers?
7579 (progn
7580 (goto-char before-lparen)
7581 (c-forward-token-2) ; to first token inside parens
7582 (and
7583 (c-on-identifier)
7584 (c-forward-token-2)
7585 (catch 'id-list
7586 (while (eq (char-after) ?\,)
7587 (c-forward-token-2)
7588 (unless (c-on-identifier) (throw 'id-list nil))
7589 (c-forward-token-2))
7590 (eq (char-after) ?\))))))
7591
7592 ;; ...Yes. We've identified the function's argument list.
7593 (throw 'knr
7594 (progn (goto-char after-rparen)
7595 (c-forward-syntactic-ws)
7596 (point)))
7597
7598 ;; ...No. The current parens aren't the function's arg list.
7599 (goto-char before-lparen))
7600
7601 (or (c-go-list-backward) ; backwards over [ .... ]
7602 (throw 'knr nil)))))))))
785eecbb
RS
7603
7604(defun c-skip-conditional ()
7605 ;; skip forward over conditional at point, including any predicate
7606 ;; statements in parentheses. No error checking is performed.
0386b551
AM
7607 ;;
7608 ;; This function might do hidden buffer changes.
0ec8351b
BW
7609 (c-forward-sexp (cond
7610 ;; else if()
a66cd3ee
MS
7611 ((looking-at (concat "\\<else"
7612 "\\([ \t\n]\\|\\\\\n\\)+"
7613 "if\\>\\([^_]\\|$\\)"))
7614 3)
0ec8351b 7615 ;; do, else, try, finally
a66cd3ee
MS
7616 ((looking-at (concat "\\<\\("
7617 "do\\|else\\|try\\|finally"
7618 "\\)\\>\\([^_]\\|$\\)"))
130c507e 7619 1)
ce8c7486 7620 ;; for, if, while, switch, catch, synchronized, foreach
0ec8351b 7621 (t 2))))
785eecbb 7622
a66cd3ee
MS
7623(defun c-after-conditional (&optional lim)
7624 ;; If looking at the token after a conditional then return the
7625 ;; position of its start, otherwise return nil.
0386b551
AM
7626 ;;
7627 ;; This function might do hidden buffer changes.
a66cd3ee 7628 (save-excursion
d9e94c22 7629 (and (zerop (c-backward-token-2 1 t lim))
a66cd3ee
MS
7630 (or (looking-at c-block-stmt-1-key)
7631 (and (eq (char-after) ?\()
d9e94c22 7632 (zerop (c-backward-token-2 1 t lim))
a66cd3ee
MS
7633 (looking-at c-block-stmt-2-key)))
7634 (point))))
7635
0386b551
AM
7636(defun c-after-special-operator-id (&optional lim)
7637 ;; If the point is after an operator identifier that isn't handled
7638 ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
7639 ;; position of the start of that identifier is returned. nil is
7640 ;; returned otherwise. The point may be anywhere in the syntactic
7641 ;; whitespace after the last token of the operator identifier.
7642 ;;
7643 ;; This function might do hidden buffer changes.
7644 (save-excursion
7645 (and c-overloadable-operators-regexp
7646 (zerop (c-backward-token-2 1 nil lim))
7647 (looking-at c-overloadable-operators-regexp)
51c9af45 7648 (or (not c-opt-op-identifier-prefix)
0386b551
AM
7649 (and
7650 (zerop (c-backward-token-2 1 nil lim))
51c9af45 7651 (looking-at c-opt-op-identifier-prefix)))
0386b551
AM
7652 (point))))
7653
a66cd3ee
MS
7654(defsubst c-backward-to-block-anchor (&optional lim)
7655 ;; Assuming point is at a brace that opens a statement block of some
7656 ;; kind, move to the proper anchor point for that block. It might
7657 ;; need to be adjusted further by c-add-stmt-syntax, but the
7658 ;; position at return is suitable as start position for that
7659 ;; function.
0386b551
AM
7660 ;;
7661 ;; This function might do hidden buffer changes.
a66cd3ee
MS
7662 (unless (= (point) (c-point 'boi))
7663 (let ((start (c-after-conditional lim)))
7664 (if start
7665 (goto-char start)))))
7666
037558bf 7667(defsubst c-backward-to-decl-anchor (&optional lim)
a66cd3ee
MS
7668 ;; Assuming point is at a brace that opens the block of a top level
7669 ;; declaration of some kind, move to the proper anchor point for
7670 ;; that block.
0386b551
AM
7671 ;;
7672 ;; This function might do hidden buffer changes.
a66cd3ee 7673 (unless (= (point) (c-point 'boi))
037558bf 7674 (c-beginning-of-statement-1 lim)))
a66cd3ee 7675
ff959bab 7676(defun c-search-decl-header-end ()
a66cd3ee
MS
7677 ;; Search forward for the end of the "header" of the current
7678 ;; declaration. That's the position where the definition body
7679 ;; starts, or the first variable initializer, or the ending
7680 ;; semicolon. I.e. search forward for the closest following
7681 ;; (syntactically relevant) '{', '=' or ';' token. Point is left
7682 ;; _after_ the first found token, or at point-max if none is found.
0386b551
AM
7683 ;;
7684 ;; This function might do hidden buffer changes.
ff959bab
MS
7685
7686 (let ((base (point)))
7687 (if (c-major-mode-is 'c++-mode)
7688
7689 ;; In C++ we need to take special care to handle operator
7690 ;; tokens and those pesky template brackets.
7691 (while (and
7692 (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
7693 (or
7694 (c-end-of-current-token base)
7695 ;; Handle operator identifiers, i.e. ignore any
7696 ;; operator token preceded by "operator".
7697 (save-excursion
7698 (and (c-safe (c-backward-sexp) t)
51c9af45 7699 (looking-at c-opt-op-identifier-prefix)))
ff959bab
MS
7700 (and (eq (char-before) ?<)
7701 (c-with-syntax-table c++-template-syntax-table
7702 (if (c-safe (goto-char (c-up-list-forward (point))))
7703 t
7704 (goto-char (point-max))
7705 nil)))))
7706 (setq base (point)))
7707
7708 (while (and
7709 (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
7710 (c-end-of-current-token base))
7711 (setq base (point))))))
a66cd3ee
MS
7712
7713(defun c-beginning-of-decl-1 (&optional lim)
7714 ;; Go to the beginning of the current declaration, or the beginning
7715 ;; of the previous one if already at the start of it. Point won't
0386b551 7716 ;; be moved out of any surrounding paren. Return a cons cell of the
a66cd3ee
MS
7717 ;; form (MOVE . KNR-POS). MOVE is like the return value from
7718 ;; `c-beginning-of-statement-1'. If point skipped over some K&R
7719 ;; style argument declarations (and they are to be recognized) then
7720 ;; KNR-POS is set to the start of the first such argument
7721 ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
7722 ;; position that bounds the backward search.
7723 ;;
7724 ;; NB: Cases where the declaration continues after the block, as in
7725 ;; "struct foo { ... } bar;", are currently recognized as two
7726 ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
0386b551
AM
7727 ;;
7728 ;; This function might do hidden buffer changes.
a66cd3ee
MS
7729 (catch 'return
7730 (let* ((start (point))
d9e94c22 7731 (last-stmt-start (point))
0386b551 7732 (move (c-beginning-of-statement-1 lim nil t)))
a66cd3ee 7733
a66cd3ee
MS
7734 ;; `c-beginning-of-statement-1' stops at a block start, but we
7735 ;; want to continue if the block doesn't begin a top level
2a15eb73
MS
7736 ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
7737 ;; or an open paren.
d9e94c22 7738 (let ((beg (point)) tentative-move)
51c9af45
AM
7739 ;; Go back one "statement" each time round the loop until we're just
7740 ;; after a ;, }, or :, or at BOB or the start of a macro or start of
7741 ;; an ObjC method. This will move over a multiple declaration whose
7742 ;; components are comma separated.
d9e94c22
MS
7743 (while (and
7744 ;; Must check with c-opt-method-key in ObjC mode.
7745 (not (and c-opt-method-key
7746 (looking-at c-opt-method-key)))
7747 (/= last-stmt-start (point))
7748 (progn
7749 (c-backward-syntactic-ws lim)
7750 (not (memq (char-before) '(?\; ?} ?: nil))))
2a15eb73
MS
7751 (save-excursion
7752 (backward-char)
7753 (not (looking-at "\\s(")))
d9e94c22
MS
7754 ;; Check that we don't move from the first thing in a
7755 ;; macro to its header.
7756 (not (eq (setq tentative-move
0386b551 7757 (c-beginning-of-statement-1 lim nil t))
d9e94c22
MS
7758 'macro)))
7759 (setq last-stmt-start beg
7760 beg (point)
7761 move tentative-move))
7762 (goto-char beg))
7763
7764 (when c-recognize-knr-p
7765 (let ((fallback-pos (point)) knr-argdecl-start)
7766 ;; Handle K&R argdecls. Back up after the "statement" jumped
7767 ;; over by `c-beginning-of-statement-1', unless it was the
7768 ;; function body, in which case we're sitting on the opening
7769 ;; brace now. Then test if we're in a K&R argdecl region and
7770 ;; that we started at the other side of the first argdecl in
7771 ;; it.
7772 (unless (eq (char-after) ?{)
7773 (goto-char last-stmt-start))
7774 (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
7775 (< knr-argdecl-start start)
7776 (progn
7777 (goto-char knr-argdecl-start)
0386b551 7778 (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
d9e94c22
MS
7779 (throw 'return
7780 (cons (if (eq (char-after fallback-pos) ?{)
7781 'previous
7782 'same)
7783 knr-argdecl-start))
7784 (goto-char fallback-pos))))
7785
51c9af45
AM
7786 ;; `c-beginning-of-statement-1' counts each brace block as a separate
7787 ;; statement, so the result will be 'previous if we've moved over any.
7788 ;; So change our result back to 'same if necessary.
7789 ;;
7790 ;; If they were brace list initializers we might not have moved over a
7791 ;; declaration boundary though, so change it to 'same if we've moved
7792 ;; past a '=' before '{', but not ';'. (This ought to be integrated
7793 ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
7794 ;; potentially can search over a large amount of text.). Take special
7795 ;; pains not to get mislead by C++'s "operator=", and the like.
d9e94c22
MS
7796 (if (and (eq move 'previous)
7797 (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
7798 c++-template-syntax-table
7799 (syntax-table))
7800 (save-excursion
51c9af45
AM
7801 (and
7802 (progn
7803 (while ; keep going back to "[;={"s until we either find
7804 ; no more, or get to one which isn't an "operator ="
7805 (and (c-syntactic-re-search-forward "[;={]" start t t t)
7806 (eq (char-before) ?=)
7807 c-overloadable-operators-regexp
7808 c-opt-op-identifier-prefix
7809 (save-excursion
7810 (eq (c-backward-token-2) 0)
7811 (looking-at c-overloadable-operators-regexp)
7812 (eq (c-backward-token-2) 0)
7813 (looking-at c-opt-op-identifier-prefix))))
7814 (eq (char-before) ?=))
7815 (c-syntactic-re-search-forward "[;{]" start t t)
7816 (eq (char-before) ?{)
7817 (c-safe (goto-char (c-up-list-forward (point))) t)
7818 (not (c-syntactic-re-search-forward ";" start t t))))))
d9e94c22
MS
7819 (cons 'same nil)
7820 (cons move nil)))))
a66cd3ee
MS
7821
7822(defun c-end-of-decl-1 ()
7823 ;; Assuming point is at the start of a declaration (as detected by
7824 ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
7825 ;; `c-beginning-of-decl-1', this function handles the case when a
7826 ;; block is followed by identifiers in e.g. struct declarations in C
7827 ;; or C++. If a proper end was found then t is returned, otherwise
7828 ;; point is moved as far as possible within the current sexp and nil
7829 ;; is returned. This function doesn't handle macros; use
7830 ;; `c-end-of-macro' instead in those cases.
0386b551
AM
7831 ;;
7832 ;; This function might do hidden buffer changes.
ce8c7486 7833 (let ((start (point))
a66cd3ee
MS
7834 (decl-syntax-table (if (c-major-mode-is 'c++-mode)
7835 c++-template-syntax-table
7836 (syntax-table))))
7837 (catch 'return
7838 (c-search-decl-header-end)
7839
7840 (when (and c-recognize-knr-p
7841 (eq (char-before) ?\;)
7842 (c-in-knr-argdecl start))
7843 ;; Stopped at the ';' in a K&R argdecl section which is
7844 ;; detected using the same criteria as in
7845 ;; `c-beginning-of-decl-1'. Move to the following block
7846 ;; start.
d9e94c22 7847 (c-syntactic-re-search-forward "{" nil 'move t))
a66cd3ee
MS
7848
7849 (when (eq (char-before) ?{)
7850 ;; Encountered a block in the declaration. Jump over it.
7851 (condition-case nil
7852 (goto-char (c-up-list-forward (point)))
d9e94c22
MS
7853 (error (goto-char (point-max))
7854 (throw 'return nil)))
a66cd3ee
MS
7855 (if (or (not c-opt-block-decls-with-vars-key)
7856 (save-excursion
7857 (c-with-syntax-table decl-syntax-table
7858 (let ((lim (point)))
7859 (goto-char start)
b3cf7e18
MS
7860 (not (and
7861 ;; Check for `c-opt-block-decls-with-vars-key'
7862 ;; before the first paren.
7863 (c-syntactic-re-search-forward
d9e94c22 7864 (concat "[;=\(\[{]\\|\\("
b3cf7e18
MS
7865 c-opt-block-decls-with-vars-key
7866 "\\)")
d9e94c22 7867 lim t t t)
b3cf7e18
MS
7868 (match-beginning 1)
7869 (not (eq (char-before) ?_))
d9e94c22
MS
7870 ;; Check that the first following paren is
7871 ;; the block.
7872 (c-syntactic-re-search-forward "[;=\(\[{]"
7873 lim t t t)
b3cf7e18 7874 (eq (char-before) ?{)))))))
a66cd3ee
MS
7875 ;; The declaration doesn't have any of the
7876 ;; `c-opt-block-decls-with-vars' keywords in the
7877 ;; beginning, so it ends here at the end of the block.
7878 (throw 'return t)))
7879
7880 (c-with-syntax-table decl-syntax-table
7881 (while (progn
7882 (if (eq (char-before) ?\;)
7883 (throw 'return t))
d9e94c22 7884 (c-syntactic-re-search-forward ";" nil 'move t))))
a66cd3ee 7885 nil)))
ce8c7486 7886
0386b551
AM
7887(defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
7888 ;; Assuming the point is at an open brace, check if it starts a
7889 ;; block that contains another declaration level, i.e. that isn't a
7890 ;; statement block or a brace list, and if so return non-nil.
7891 ;;
7892 ;; If the check is successful, the return value is the start of the
7893 ;; keyword that tells what kind of construct it is, i.e. typically
7894 ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
7895 ;; the point will be at the start of the construct, before any
7896 ;; leading specifiers, otherwise it's at the returned position.
7897 ;;
7898 ;; The point is clobbered if the check is unsuccessful.
7899 ;;
7900 ;; CONTAINING-SEXP is the position of the open of the surrounding
7901 ;; paren, or nil if none.
7902 ;;
7903 ;; The optional LIMIT limits the backward search for the start of
7904 ;; the construct. It's assumed to be at a syntactically relevant
7905 ;; position.
7906 ;;
7907 ;; If any template arglists are found in the searched region before
7908 ;; the open brace, they get marked with paren syntax.
7909 ;;
7910 ;; This function might do hidden buffer changes.
7911
7912 (let ((open-brace (point)) kwd-start first-specifier-pos)
7913 (c-syntactic-skip-backward c-block-prefix-charset limit t)
7914
7915 (when (and c-recognize-<>-arglists
7916 (eq (char-before) ?>))
7917 ;; Could be at the end of a template arglist.
7918 (let ((c-parse-and-markup-<>-arglists t)
7919 (c-disallow-comma-in-<>-arglists
7920 (and containing-sexp
7921 (not (eq (char-after containing-sexp) ?{)))))
7922 (while (and
7923 (c-backward-<>-arglist nil limit)
7924 (progn
7925 (c-syntactic-skip-backward c-block-prefix-charset limit t)
7926 (eq (char-before) ?>))))))
7927
7928 ;; Note: Can't get bogus hits inside template arglists below since they
7929 ;; have gotten paren syntax above.
7930 (when (and
7931 ;; If `goto-start' is set we begin by searching for the
7932 ;; first possible position of a leading specifier list.
7933 ;; The `c-decl-block-key' search continues from there since
7934 ;; we know it can't match earlier.
7935 (if goto-start
7936 (when (c-syntactic-re-search-forward c-symbol-start
7937 open-brace t t)
7938 (goto-char (setq first-specifier-pos (match-beginning 0)))
7939 t)
7940 t)
7941
7942 (cond
7943 ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
7944 (goto-char (setq kwd-start (match-beginning 0)))
7945 (or
7946
7947 ;; Found a keyword that can't be a type?
7948 (match-beginning 1)
7949
7950 ;; Can be a type too, in which case it's the return type of a
7951 ;; function (under the assumption that no declaration level
7952 ;; block construct starts with a type).
7953 (not (c-forward-type))
7954
7955 ;; Jumped over a type, but it could be a declaration keyword
7956 ;; followed by the declared identifier that we've jumped over
7957 ;; instead (e.g. in "class Foo {"). If it indeed is a type
7958 ;; then we should be at the declarator now, so check for a
7959 ;; valid declarator start.
7960 ;;
7961 ;; Note: This doesn't cope with the case when a declared
7962 ;; identifier is followed by e.g. '(' in a language where '('
7963 ;; also might be part of a declarator expression. Currently
7964 ;; there's no such language.
7965 (not (or (looking-at c-symbol-start)
7966 (looking-at c-type-decl-prefix-key)))))
7967
7968 ;; In Pike a list of modifiers may be followed by a brace
7969 ;; to make them apply to many identifiers. Note that the
7970 ;; match data will be empty on return in this case.
7971 ((and (c-major-mode-is 'pike-mode)
7972 (progn
7973 (goto-char open-brace)
7974 (= (c-backward-token-2) 0))
7975 (looking-at c-specifier-key)
7976 ;; Use this variant to avoid yet another special regexp.
7977 (c-keyword-member (c-keyword-sym (match-string 1))
7978 'c-modifier-kwds))
7979 (setq kwd-start (point))
7980 t)))
7981
7982 ;; Got a match.
7983
7984 (if goto-start
7985 ;; Back up over any preceding specifiers and their clauses
7986 ;; by going forward from `first-specifier-pos', which is the
7987 ;; earliest possible position where the specifier list can
7988 ;; start.
7989 (progn
7990 (goto-char first-specifier-pos)
7991
7992 (while (< (point) kwd-start)
7993 (if (looking-at c-symbol-key)
7994 ;; Accept any plain symbol token on the ground that
7995 ;; it's a specifier masked through a macro (just
7996 ;; like `c-forward-decl-or-cast-1' skip forward over
7997 ;; such tokens).
7998 ;;
7999 ;; Could be more restrictive wrt invalid keywords,
8000 ;; but that'd only occur in invalid code so there's
8001 ;; no use spending effort on it.
8002 (let ((end (match-end 0)))
8003 (unless (c-forward-keyword-clause 0)
8004 (goto-char end)
8005 (c-forward-syntactic-ws)))
8006
8007 ;; Can't parse a declaration preamble and is still
8008 ;; before `kwd-start'. That means `first-specifier-pos'
8009 ;; was in some earlier construct. Search again.
8010 (if (c-syntactic-re-search-forward c-symbol-start
8011 kwd-start 'move t)
8012 (goto-char (setq first-specifier-pos (match-beginning 0)))
8013 ;; Got no preamble before the block declaration keyword.
8014 (setq first-specifier-pos kwd-start))))
8015
8016 (goto-char first-specifier-pos))
8017 (goto-char kwd-start))
8018
8019 kwd-start)))
ce8c7486 8020
a66cd3ee 8021(defun c-search-uplist-for-classkey (paren-state)
0386b551
AM
8022 ;; Check if the closest containing paren sexp is a declaration
8023 ;; block, returning a 2 element vector in that case. Aref 0
8024 ;; contains the bufpos at boi of the class key line, and aref 1
8025 ;; contains the bufpos of the open brace. This function is an
8026 ;; obsolete wrapper for `c-looking-at-decl-block'.
8027 ;;
8028 ;; This function might do hidden buffer changes.
8029 (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
8030 (when open-paren-pos
8031 (save-excursion
8032 (goto-char open-paren-pos)
8033 (when (and (eq (char-after) ?{)
8034 (c-looking-at-decl-block
8035 (c-safe-position open-paren-pos paren-state)
8036 nil))
8037 (back-to-indentation)
8038 (vector (point) open-paren-pos))))))
785eecbb 8039
a66cd3ee 8040(defun c-inside-bracelist-p (containing-sexp paren-state)
785eecbb
RS
8041 ;; return the buffer position of the beginning of the brace list
8042 ;; statement if we're inside a brace list, otherwise return nil.
8043 ;; CONTAINING-SEXP is the buffer pos of the innermost containing
0386b551 8044 ;; paren. PAREN-STATE is the remainder of the state of enclosing
130c507e 8045 ;; braces
785eecbb
RS
8046 ;;
8047 ;; N.B.: This algorithm can potentially get confused by cpp macros
0386b551 8048 ;; placed in inconvenient locations. It's a trade-off we make for
785eecbb 8049 ;; speed.
0386b551
AM
8050 ;;
8051 ;; This function might do hidden buffer changes.
785eecbb 8052 (or
d9e94c22 8053 ;; This will pick up brace list declarations.
b2acd789
RS
8054 (c-safe
8055 (save-excursion
8056 (goto-char containing-sexp)
0ec8351b 8057 (c-forward-sexp -1)
b2acd789 8058 (let (bracepos)
d9e94c22 8059 (if (and (or (looking-at c-brace-list-key)
0ec8351b 8060 (progn (c-forward-sexp -1)
d9e94c22 8061 (looking-at c-brace-list-key)))
a66cd3ee 8062 (setq bracepos (c-down-list-forward (point)))
b2acd789
RS
8063 (not (c-crosses-statement-barrier-p (point)
8064 (- bracepos 2))))
8065 (point)))))
785eecbb
RS
8066 ;; this will pick up array/aggregate init lists, even if they are nested.
8067 (save-excursion
0ec8351b
BW
8068 (let ((class-key
8069 ;; Pike can have class definitions anywhere, so we must
8070 ;; check for the class key here.
8071 (and (c-major-mode-is 'pike-mode)
a66cd3ee
MS
8072 c-decl-block-key))
8073 bufpos braceassignp lim next-containing)
785eecbb
RS
8074 (while (and (not bufpos)
8075 containing-sexp)
a66cd3ee
MS
8076 (when paren-state
8077 (if (consp (car paren-state))
8078 (setq lim (cdr (car paren-state))
8079 paren-state (cdr paren-state))
8080 (setq lim (car paren-state)))
8081 (when paren-state
8082 (setq next-containing (car paren-state)
8083 paren-state (cdr paren-state))))
785eecbb 8084 (goto-char containing-sexp)
a66cd3ee
MS
8085 (if (c-looking-at-inexpr-block next-containing next-containing)
8086 ;; We're in an in-expression block of some kind. Do not
8087 ;; check nesting. We deliberately set the limit to the
8088 ;; containing sexp, so that c-looking-at-inexpr-block
8089 ;; doesn't check for an identifier before it.
0ec8351b
BW
8090 (setq containing-sexp nil)
8091 ;; see if the open brace is preceded by = or [...] in
8092 ;; this statement, but watch out for operator=
a66cd3ee 8093 (setq braceassignp 'dontknow)
d9e94c22 8094 (c-backward-token-2 1 t lim)
6393fef2 8095 ;; Checks to do only on the first sexp before the brace.
d9e94c22 8096 (when (and c-opt-inexpr-brace-list-key
6393fef2
RS
8097 (eq (char-after) ?\[))
8098 ;; In Java, an initialization brace list may follow
8099 ;; directly after "new Foo[]", so check for a "new"
8100 ;; earlier.
8101 (while (eq braceassignp 'dontknow)
8102 (setq braceassignp
d9e94c22
MS
8103 (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
8104 ((looking-at c-opt-inexpr-brace-list-key) t)
6393fef2
RS
8105 ((looking-at "\\sw\\|\\s_\\|[.[]")
8106 ;; Carry on looking if this is an
8107 ;; identifier (may contain "." in Java)
8108 ;; or another "[]" sexp.
8109 'dontknow)
8110 (t nil)))))
8111 ;; Checks to do on all sexps before the brace, up to the
8112 ;; beginning of the statement.
8113 (while (eq braceassignp 'dontknow)
0ec8351b
BW
8114 (cond ((eq (char-after) ?\;)
8115 (setq braceassignp nil))
8116 ((and class-key
8117 (looking-at class-key))
8118 (setq braceassignp nil))
8119 ((eq (char-after) ?=)
8120 ;; We've seen a =, but must check earlier tokens so
8121 ;; that it isn't something that should be ignored.
8122 (setq braceassignp 'maybe)
8123 (while (and (eq braceassignp 'maybe)
d9e94c22 8124 (zerop (c-backward-token-2 1 t lim)))
0ec8351b
BW
8125 (setq braceassignp
8126 (cond
8127 ;; Check for operator =
51c9af45
AM
8128 ((and c-opt-op-identifier-prefix
8129 (looking-at c-opt-op-identifier-prefix))
0386b551 8130 nil)
130c507e
GM
8131 ;; Check for `<opchar>= in Pike.
8132 ((and (c-major-mode-is 'pike-mode)
8133 (or (eq (char-after) ?`)
8134 ;; Special case for Pikes
8135 ;; `[]=, since '[' is not in
8136 ;; the punctuation class.
8137 (and (eq (char-after) ?\[)
8138 (eq (char-before) ?`))))
8139 nil)
0ec8351b
BW
8140 ((looking-at "\\s.") 'maybe)
8141 ;; make sure we're not in a C++ template
8142 ;; argument assignment
a66cd3ee
MS
8143 ((and
8144 (c-major-mode-is 'c++-mode)
8145 (save-excursion
8146 (let ((here (point))
8147 (pos< (progn
8148 (skip-chars-backward "^<>")
8149 (point))))
8150 (and (eq (char-before) ?<)
8151 (not (c-crosses-statement-barrier-p
8152 pos< here))
8153 (not (c-in-literal))
8154 ))))
0ec8351b 8155 nil)
6393fef2
RS
8156 (t t))))))
8157 (if (and (eq braceassignp 'dontknow)
d9e94c22 8158 (/= (c-backward-token-2 1 t lim) 0))
6393fef2
RS
8159 (setq braceassignp nil)))
8160 (if (not braceassignp)
0ec8351b
BW
8161 (if (eq (char-after) ?\;)
8162 ;; Brace lists can't contain a semicolon, so we're done.
8163 (setq containing-sexp nil)
a66cd3ee
MS
8164 ;; Go up one level.
8165 (setq containing-sexp next-containing
8166 lim nil
8167 next-containing nil))
0ec8351b
BW
8168 ;; we've hit the beginning of the aggregate list
8169 (c-beginning-of-statement-1
a66cd3ee 8170 (c-most-enclosing-brace paren-state))
0ec8351b 8171 (setq bufpos (point))))
a66cd3ee 8172 )
785eecbb
RS
8173 bufpos))
8174 ))
8175
0ec8351b
BW
8176(defun c-looking-at-special-brace-list (&optional lim)
8177