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