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