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