clang  10.0.0git
ParseTentative.cpp
Go to the documentation of this file.
1 //===--- ParseTentative.cpp - Ambiguity Resolution Parsing ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the tentative parsing portions of the Parser
10 // interfaces, for ambiguity resolution.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Parse/Parser.h"
17 using namespace clang;
18 
19 /// isCXXDeclarationStatement - C++-specialized function that disambiguates
20 /// between a declaration or an expression statement, when parsing function
21 /// bodies. Returns true for declaration, false for expression.
22 ///
23 /// declaration-statement:
24 /// block-declaration
25 ///
26 /// block-declaration:
27 /// simple-declaration
28 /// asm-definition
29 /// namespace-alias-definition
30 /// using-declaration
31 /// using-directive
32 /// [C++0x] static_assert-declaration
33 ///
34 /// asm-definition:
35 /// 'asm' '(' string-literal ')' ';'
36 ///
37 /// namespace-alias-definition:
38 /// 'namespace' identifier = qualified-namespace-specifier ';'
39 ///
40 /// using-declaration:
41 /// 'using' typename[opt] '::'[opt] nested-name-specifier
42 /// unqualified-id ';'
43 /// 'using' '::' unqualified-id ;
44 ///
45 /// using-directive:
46 /// 'using' 'namespace' '::'[opt] nested-name-specifier[opt]
47 /// namespace-name ';'
48 ///
49 bool Parser::isCXXDeclarationStatement() {
50  switch (Tok.getKind()) {
51  // asm-definition
52  case tok::kw_asm:
53  // namespace-alias-definition
54  case tok::kw_namespace:
55  // using-declaration
56  // using-directive
57  case tok::kw_using:
58  // static_assert-declaration
59  case tok::kw_static_assert:
60  case tok::kw__Static_assert:
61  return true;
62  // simple-declaration
63  default:
64  return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
65  }
66 }
67 
68 /// isCXXSimpleDeclaration - C++-specialized function that disambiguates
69 /// between a simple-declaration or an expression-statement.
70 /// If during the disambiguation process a parsing error is encountered,
71 /// the function returns true to let the declaration parsing code handle it.
72 /// Returns false if the statement is disambiguated as expression.
73 ///
74 /// simple-declaration:
75 /// decl-specifier-seq init-declarator-list[opt] ';'
76 /// decl-specifier-seq ref-qualifier[opt] '[' identifier-list ']'
77 /// brace-or-equal-initializer ';' [C++17]
78 ///
79 /// (if AllowForRangeDecl specified)
80 /// for ( for-range-declaration : for-range-initializer ) statement
81 ///
82 /// for-range-declaration:
83 /// decl-specifier-seq declarator
84 /// decl-specifier-seq ref-qualifier[opt] '[' identifier-list ']'
85 ///
86 /// In any of the above cases there can be a preceding attribute-specifier-seq,
87 /// but the caller is expected to handle that.
88 bool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) {
89  // C++ 6.8p1:
90  // There is an ambiguity in the grammar involving expression-statements and
91  // declarations: An expression-statement with a function-style explicit type
92  // conversion (5.2.3) as its leftmost subexpression can be indistinguishable
93  // from a declaration where the first declarator starts with a '('. In those
94  // cases the statement is a declaration. [Note: To disambiguate, the whole
95  // statement might have to be examined to determine if it is an
96  // expression-statement or a declaration].
97 
98  // C++ 6.8p3:
99  // The disambiguation is purely syntactic; that is, the meaning of the names
100  // occurring in such a statement, beyond whether they are type-names or not,
101  // is not generally used in or changed by the disambiguation. Class
102  // templates are instantiated as necessary to determine if a qualified name
103  // is a type-name. Disambiguation precedes parsing, and a statement
104  // disambiguated as a declaration may be an ill-formed declaration.
105 
106  // We don't have to parse all of the decl-specifier-seq part. There's only
107  // an ambiguity if the first decl-specifier is
108  // simple-type-specifier/typename-specifier followed by a '(', which may
109  // indicate a function-style cast expression.
110  // isCXXDeclarationSpecifier will return TPResult::Ambiguous only in such
111  // a case.
112 
113  bool InvalidAsDeclaration = false;
114  TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
115  &InvalidAsDeclaration);
116  if (TPR != TPResult::Ambiguous)
117  return TPR != TPResult::False; // Returns true for TPResult::True or
118  // TPResult::Error.
119 
120  // FIXME: TryParseSimpleDeclaration doesn't look past the first initializer,
121  // and so gets some cases wrong. We can't carry on if we've already seen
122  // something which makes this statement invalid as a declaration in this case,
123  // since it can cause us to misparse valid code. Revisit this once
124  // TryParseInitDeclaratorList is fixed.
125  if (InvalidAsDeclaration)
126  return false;
127 
128  // FIXME: Add statistics about the number of ambiguous statements encountered
129  // and how they were resolved (number of declarations+number of expressions).
130 
131  // Ok, we have a simple-type-specifier/typename-specifier followed by a '(',
132  // or an identifier which doesn't resolve as anything. We need tentative
133  // parsing...
134 
135  {
136  RevertingTentativeParsingAction PA(*this);
137  TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
138  }
139 
140  // In case of an error, let the declaration parsing code handle it.
141  if (TPR == TPResult::Error)
142  return true;
143 
144  // Declarations take precedence over expressions.
145  if (TPR == TPResult::Ambiguous)
146  TPR = TPResult::True;
147 
148  assert(TPR == TPResult::True || TPR == TPResult::False);
149  return TPR == TPResult::True;
150 }
151 
152 /// Try to consume a token sequence that we've already identified as
153 /// (potentially) starting a decl-specifier.
154 Parser::TPResult Parser::TryConsumeDeclarationSpecifier() {
155  switch (Tok.getKind()) {
156  case tok::kw__Atomic:
157  if (NextToken().isNot(tok::l_paren)) {
158  ConsumeToken();
159  break;
160  }
161  LLVM_FALLTHROUGH;
162  case tok::kw_typeof:
163  case tok::kw___attribute:
164  case tok::kw___underlying_type: {
165  ConsumeToken();
166  if (Tok.isNot(tok::l_paren))
167  return TPResult::Error;
168  ConsumeParen();
169  if (!SkipUntil(tok::r_paren))
170  return TPResult::Error;
171  break;
172  }
173 
174  case tok::kw_class:
175  case tok::kw_struct:
176  case tok::kw_union:
177  case tok::kw___interface:
178  case tok::kw_enum:
179  // elaborated-type-specifier:
180  // class-key attribute-specifier-seq[opt]
181  // nested-name-specifier[opt] identifier
182  // class-key nested-name-specifier[opt] template[opt] simple-template-id
183  // enum nested-name-specifier[opt] identifier
184  //
185  // FIXME: We don't support class-specifiers nor enum-specifiers here.
186  ConsumeToken();
187 
188  // Skip attributes.
189  while (Tok.isOneOf(tok::l_square, tok::kw___attribute, tok::kw___declspec,
190  tok::kw_alignas)) {
191  if (Tok.is(tok::l_square)) {
192  ConsumeBracket();
193  if (!SkipUntil(tok::r_square))
194  return TPResult::Error;
195  } else {
196  ConsumeToken();
197  if (Tok.isNot(tok::l_paren))
198  return TPResult::Error;
199  ConsumeParen();
200  if (!SkipUntil(tok::r_paren))
201  return TPResult::Error;
202  }
203  }
204 
206  return TPResult::Error;
207  if (Tok.is(tok::annot_cxxscope))
208  ConsumeAnnotationToken();
209  if (Tok.is(tok::identifier))
210  ConsumeToken();
211  else if (Tok.is(tok::annot_template_id))
212  ConsumeAnnotationToken();
213  else
214  return TPResult::Error;
215  break;
216 
217  case tok::annot_cxxscope:
218  ConsumeAnnotationToken();
219  LLVM_FALLTHROUGH;
220  default:
221  ConsumeAnyToken();
222 
223  if (getLangOpts().ObjC && Tok.is(tok::less))
224  return TryParseProtocolQualifiers();
225  break;
226  }
227 
228  return TPResult::Ambiguous;
229 }
230 
231 /// simple-declaration:
232 /// decl-specifier-seq init-declarator-list[opt] ';'
233 ///
234 /// (if AllowForRangeDecl specified)
235 /// for ( for-range-declaration : for-range-initializer ) statement
236 /// for-range-declaration:
237 /// attribute-specifier-seqopt type-specifier-seq declarator
238 ///
239 Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
240  if (TryConsumeDeclarationSpecifier() == TPResult::Error)
241  return TPResult::Error;
242 
243  // Two decl-specifiers in a row conclusively disambiguate this as being a
244  // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the
245  // overwhelmingly common case that the next token is a '('.
246  if (Tok.isNot(tok::l_paren)) {
247  TPResult TPR = isCXXDeclarationSpecifier();
248  if (TPR == TPResult::Ambiguous)
249  return TPResult::True;
250  if (TPR == TPResult::True || TPR == TPResult::Error)
251  return TPR;
252  assert(TPR == TPResult::False);
253  }
254 
255  TPResult TPR = TryParseInitDeclaratorList();
256  if (TPR != TPResult::Ambiguous)
257  return TPR;
258 
259  if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
260  return TPResult::False;
261 
262  return TPResult::Ambiguous;
263 }
264 
265 /// Tentatively parse an init-declarator-list in order to disambiguate it from
266 /// an expression.
267 ///
268 /// init-declarator-list:
269 /// init-declarator
270 /// init-declarator-list ',' init-declarator
271 ///
272 /// init-declarator:
273 /// declarator initializer[opt]
274 /// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
275 ///
276 /// initializer:
277 /// brace-or-equal-initializer
278 /// '(' expression-list ')'
279 ///
280 /// brace-or-equal-initializer:
281 /// '=' initializer-clause
282 /// [C++11] braced-init-list
283 ///
284 /// initializer-clause:
285 /// assignment-expression
286 /// braced-init-list
287 ///
288 /// braced-init-list:
289 /// '{' initializer-list ','[opt] '}'
290 /// '{' '}'
291 ///
292 Parser::TPResult Parser::TryParseInitDeclaratorList() {
293  while (1) {
294  // declarator
295  TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
296  if (TPR != TPResult::Ambiguous)
297  return TPR;
298 
299  // [GNU] simple-asm-expr[opt] attributes[opt]
300  if (Tok.isOneOf(tok::kw_asm, tok::kw___attribute))
301  return TPResult::True;
302 
303  // initializer[opt]
304  if (Tok.is(tok::l_paren)) {
305  // Parse through the parens.
306  ConsumeParen();
307  if (!SkipUntil(tok::r_paren, StopAtSemi))
308  return TPResult::Error;
309  } else if (Tok.is(tok::l_brace)) {
310  // A left-brace here is sufficient to disambiguate the parse; an
311  // expression can never be followed directly by a braced-init-list.
312  return TPResult::True;
313  } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
314  // MSVC and g++ won't examine the rest of declarators if '=' is
315  // encountered; they just conclude that we have a declaration.
316  // EDG parses the initializer completely, which is the proper behavior
317  // for this case.
318  //
319  // At present, Clang follows MSVC and g++, since the parser does not have
320  // the ability to parse an expression fully without recording the
321  // results of that parse.
322  // FIXME: Handle this case correctly.
323  //
324  // Also allow 'in' after an Objective-C declaration as in:
325  // for (int (^b)(void) in array). Ideally this should be done in the
326  // context of parsing for-init-statement of a foreach statement only. But,
327  // in any other context 'in' is invalid after a declaration and parser
328  // issues the error regardless of outcome of this decision.
329  // FIXME: Change if above assumption does not hold.
330  return TPResult::True;
331  }
332 
333  if (!TryConsumeToken(tok::comma))
334  break;
335  }
336 
337  return TPResult::Ambiguous;
338 }
339 
342  bool CanBeExpression = true;
343  bool CanBeCondition = true;
346 
348  bool CanBeForRangeDecl)
349  : P(P), CanBeInitStatement(CanBeInitStatement),
350  CanBeForRangeDecl(CanBeForRangeDecl) {}
351 
352  bool resolved() {
353  return CanBeExpression + CanBeCondition + CanBeInitStatement +
354  CanBeForRangeDecl < 2;
355  }
356 
358  CanBeExpression = false;
359 
360  if (!resolved()) {
361  // FIXME: Unify the parsing codepaths for condition variables and
362  // simple-declarations so that we don't need to eagerly figure out which
363  // kind we have here. (Just parse init-declarators until we reach a
364  // semicolon or right paren.)
365  RevertingTentativeParsingAction PA(P);
366  if (CanBeForRangeDecl) {
367  // Skip until we hit a ')', ';', or a ':' with no matching '?'.
368  // The final case is a for range declaration, the rest are not.
369  while (true) {
370  unsigned QuestionColonDepth = 0;
371  P.SkipUntil({tok::r_paren, tok::semi, tok::question, tok::colon},
373  if (P.Tok.is(tok::question))
374  ++QuestionColonDepth;
375  else if (P.Tok.is(tok::colon)) {
376  if (QuestionColonDepth)
377  --QuestionColonDepth;
378  else {
379  CanBeCondition = CanBeInitStatement = false;
380  return;
381  }
382  } else {
383  CanBeForRangeDecl = false;
384  break;
385  }
386  P.ConsumeToken();
387  }
388  } else {
389  // Just skip until we hit a ')' or ';'.
390  P.SkipUntil(tok::r_paren, tok::semi, StopBeforeMatch);
391  }
392  if (P.Tok.isNot(tok::r_paren))
393  CanBeCondition = CanBeForRangeDecl = false;
394  if (P.Tok.isNot(tok::semi))
395  CanBeInitStatement = false;
396  }
397  }
398 
400  CanBeCondition = false;
401  return resolved();
402  }
403 
405  CanBeForRangeDecl = false;
406  return resolved();
407  }
408 
409  bool update(TPResult IsDecl) {
410  switch (IsDecl) {
411  case TPResult::True:
412  markNotExpression();
413  assert(resolved() && "can't continue after tentative parsing bails out");
414  break;
415  case TPResult::False:
416  CanBeCondition = CanBeInitStatement = CanBeForRangeDecl = false;
417  break;
418  case TPResult::Ambiguous:
419  break;
420  case TPResult::Error:
421  CanBeExpression = CanBeCondition = CanBeInitStatement =
422  CanBeForRangeDecl = false;
423  break;
424  }
425  return resolved();
426  }
427 
428  ConditionOrInitStatement result() const {
429  assert(CanBeExpression + CanBeCondition + CanBeInitStatement +
430  CanBeForRangeDecl < 2 &&
431  "result called but not yet resolved");
432  if (CanBeExpression)
433  return ConditionOrInitStatement::Expression;
434  if (CanBeCondition)
435  return ConditionOrInitStatement::ConditionDecl;
436  if (CanBeInitStatement)
437  return ConditionOrInitStatement::InitStmtDecl;
438  if (CanBeForRangeDecl)
439  return ConditionOrInitStatement::ForRangeDecl;
440  return ConditionOrInitStatement::Error;
441  }
442 };
443 
444 /// Disambiguates between a declaration in a condition, a
445 /// simple-declaration in an init-statement, and an expression for
446 /// a condition of a if/switch statement.
447 ///
448 /// condition:
449 /// expression
450 /// type-specifier-seq declarator '=' assignment-expression
451 /// [C++11] type-specifier-seq declarator '=' initializer-clause
452 /// [C++11] type-specifier-seq declarator braced-init-list
453 /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
454 /// '=' assignment-expression
455 /// simple-declaration:
456 /// decl-specifier-seq init-declarator-list[opt] ';'
457 ///
458 /// Note that, unlike isCXXSimpleDeclaration, we must disambiguate all the way
459 /// to the ';' to disambiguate cases like 'int(x))' (an expression) from
460 /// 'int(x);' (a simple-declaration in an init-statement).
461 Parser::ConditionOrInitStatement
462 Parser::isCXXConditionDeclarationOrInitStatement(bool CanBeInitStatement,
463  bool CanBeForRangeDecl) {
464  ConditionDeclarationOrInitStatementState State(*this, CanBeInitStatement,
465  CanBeForRangeDecl);
466 
467  if (State.update(isCXXDeclarationSpecifier()))
468  return State.result();
469 
470  // It might be a declaration; we need tentative parsing.
471  RevertingTentativeParsingAction PA(*this);
472 
473  // FIXME: A tag definition unambiguously tells us this is an init-statement.
474  if (State.update(TryConsumeDeclarationSpecifier()))
475  return State.result();
476  assert(Tok.is(tok::l_paren) && "Expected '('");
477 
478  while (true) {
479  // Consume a declarator.
480  if (State.update(TryParseDeclarator(false/*mayBeAbstract*/)))
481  return State.result();
482 
483  // Attributes, asm label, or an initializer imply this is not an expression.
484  // FIXME: Disambiguate properly after an = instead of assuming that it's a
485  // valid declaration.
486  if (Tok.isOneOf(tok::equal, tok::kw_asm, tok::kw___attribute) ||
487  (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))) {
488  State.markNotExpression();
489  return State.result();
490  }
491 
492  // A colon here identifies a for-range declaration.
493  if (State.CanBeForRangeDecl && Tok.is(tok::colon))
494  return ConditionOrInitStatement::ForRangeDecl;
495 
496  // At this point, it can't be a condition any more, because a condition
497  // must have a brace-or-equal-initializer.
498  if (State.markNotCondition())
499  return State.result();
500 
501  // Likewise, it can't be a for-range declaration any more.
502  if (State.markNotForRangeDecl())
503  return State.result();
504 
505  // A parenthesized initializer could be part of an expression or a
506  // simple-declaration.
507  if (Tok.is(tok::l_paren)) {
508  ConsumeParen();
509  SkipUntil(tok::r_paren, StopAtSemi);
510  }
511 
512  if (!TryConsumeToken(tok::comma))
513  break;
514  }
515 
516  // We reached the end. If it can now be some kind of decl, then it is.
517  if (State.CanBeCondition && Tok.is(tok::r_paren))
518  return ConditionOrInitStatement::ConditionDecl;
519  else if (State.CanBeInitStatement && Tok.is(tok::semi))
520  return ConditionOrInitStatement::InitStmtDecl;
521  else
522  return ConditionOrInitStatement::Expression;
523 }
524 
525  /// Determine whether the next set of tokens contains a type-id.
526  ///
527  /// The context parameter states what context we're parsing right
528  /// now, which affects how this routine copes with the token
529  /// following the type-id. If the context is TypeIdInParens, we have
530  /// already parsed the '(' and we will cease lookahead when we hit
531  /// the corresponding ')'. If the context is
532  /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
533  /// before this template argument, and will cease lookahead when we
534  /// hit a '>', '>>' (in C++0x), or ','; or, in C++0x, an ellipsis immediately
535  /// preceding such. Returns true for a type-id and false for an expression.
536  /// If during the disambiguation process a parsing error is encountered,
537  /// the function returns true to let the declaration parsing code handle it.
538  ///
539  /// type-id:
540  /// type-specifier-seq abstract-declarator[opt]
541  ///
542 bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
543 
544  isAmbiguous = false;
545 
546  // C++ 8.2p2:
547  // The ambiguity arising from the similarity between a function-style cast and
548  // a type-id can occur in different contexts. The ambiguity appears as a
549  // choice between a function-style cast expression and a declaration of a
550  // type. The resolution is that any construct that could possibly be a type-id
551  // in its syntactic context shall be considered a type-id.
552 
553  TPResult TPR = isCXXDeclarationSpecifier();
554  if (TPR != TPResult::Ambiguous)
555  return TPR != TPResult::False; // Returns true for TPResult::True or
556  // TPResult::Error.
557 
558  // FIXME: Add statistics about the number of ambiguous statements encountered
559  // and how they were resolved (number of declarations+number of expressions).
560 
561  // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
562  // We need tentative parsing...
563 
564  RevertingTentativeParsingAction PA(*this);
565 
566  // type-specifier-seq
567  TryConsumeDeclarationSpecifier();
568  assert(Tok.is(tok::l_paren) && "Expected '('");
569 
570  // declarator
571  TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
572 
573  // In case of an error, let the declaration parsing code handle it.
574  if (TPR == TPResult::Error)
575  TPR = TPResult::True;
576 
577  if (TPR == TPResult::Ambiguous) {
578  // We are supposed to be inside parens, so if after the abstract declarator
579  // we encounter a ')' this is a type-id, otherwise it's an expression.
580  if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
581  TPR = TPResult::True;
582  isAmbiguous = true;
583 
584  // We are supposed to be inside a template argument, so if after
585  // the abstract declarator we encounter a '>', '>>' (in C++0x), or
586  // ','; or, in C++0x, an ellipsis immediately preceding such, this
587  // is a type-id. Otherwise, it's an expression.
588  } else if (Context == TypeIdAsTemplateArgument &&
589  (Tok.isOneOf(tok::greater, tok::comma) ||
590  (getLangOpts().CPlusPlus11 &&
591  (Tok.isOneOf(tok::greatergreater,
592  tok::greatergreatergreater) ||
593  (Tok.is(tok::ellipsis) &&
594  NextToken().isOneOf(tok::greater, tok::greatergreater,
595  tok::greatergreatergreater,
596  tok::comma)))))) {
597  TPR = TPResult::True;
598  isAmbiguous = true;
599 
600  } else
601  TPR = TPResult::False;
602  }
603 
604  assert(TPR == TPResult::True || TPR == TPResult::False);
605  return TPR == TPResult::True;
606 }
607 
608 /// Returns true if this is a C++11 attribute-specifier. Per
609 /// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
610 /// always introduce an attribute. In Objective-C++11, this rule does not
611 /// apply if either '[' begins a message-send.
612 ///
613 /// If Disambiguate is true, we try harder to determine whether a '[[' starts
614 /// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
615 ///
616 /// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
617 /// Obj-C message send or the start of an attribute. Otherwise, we assume it
618 /// is not an Obj-C message send.
619 ///
620 /// C++11 [dcl.attr.grammar]:
621 ///
622 /// attribute-specifier:
623 /// '[' '[' attribute-list ']' ']'
624 /// alignment-specifier
625 ///
626 /// attribute-list:
627 /// attribute[opt]
628 /// attribute-list ',' attribute[opt]
629 /// attribute '...'
630 /// attribute-list ',' attribute '...'
631 ///
632 /// attribute:
633 /// attribute-token attribute-argument-clause[opt]
634 ///
635 /// attribute-token:
636 /// identifier
637 /// identifier '::' identifier
638 ///
639 /// attribute-argument-clause:
640 /// '(' balanced-token-seq ')'
641 Parser::CXX11AttributeKind
642 Parser::isCXX11AttributeSpecifier(bool Disambiguate,
643  bool OuterMightBeMessageSend) {
644  if (Tok.is(tok::kw_alignas))
645  return CAK_AttributeSpecifier;
646 
647  if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
648  return CAK_NotAttributeSpecifier;
649 
650  // No tentative parsing if we don't need to look for ']]' or a lambda.
651  if (!Disambiguate && !getLangOpts().ObjC)
652  return CAK_AttributeSpecifier;
653 
654  // '[[using ns: ...]]' is an attribute.
655  if (GetLookAheadToken(2).is(tok::kw_using))
656  return CAK_AttributeSpecifier;
657 
658  RevertingTentativeParsingAction PA(*this);
659 
660  // Opening brackets were checked for above.
661  ConsumeBracket();
662 
663  if (!getLangOpts().ObjC) {
664  ConsumeBracket();
665 
666  bool IsAttribute = SkipUntil(tok::r_square);
667  IsAttribute &= Tok.is(tok::r_square);
668 
669  return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
670  }
671 
672  // In Obj-C++11, we need to distinguish four situations:
673  // 1a) int x[[attr]]; C++11 attribute.
674  // 1b) [[attr]]; C++11 statement attribute.
675  // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
676  // 3a) int x[[obj get]]; Message send in array size/index.
677  // 3b) [[Class alloc] init]; Message send in message send.
678  // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
679  // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
680 
681  // Check to see if this is a lambda-expression.
682  // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
683  // into the tentative attribute parse below.
684  {
685  RevertingTentativeParsingAction LambdaTPA(*this);
686  LambdaIntroducer Intro;
687  LambdaIntroducerTentativeParse Tentative;
688  if (ParseLambdaIntroducer(Intro, &Tentative)) {
689  // We hit a hard error after deciding this was not an attribute.
690  // FIXME: Don't parse and annotate expressions when disambiguating
691  // against an attribute.
692  return CAK_NotAttributeSpecifier;
693  }
694 
695  switch (Tentative) {
696  case LambdaIntroducerTentativeParse::MessageSend:
697  // Case 3: The inner construct is definitely a message send, so the
698  // outer construct is definitely not an attribute.
699  return CAK_NotAttributeSpecifier;
700 
701  case LambdaIntroducerTentativeParse::Success:
702  case LambdaIntroducerTentativeParse::Incomplete:
703  // This is a lambda-introducer or attribute-specifier.
704  if (Tok.is(tok::r_square))
705  // Case 1: C++11 attribute.
706  return CAK_AttributeSpecifier;
707 
708  if (OuterMightBeMessageSend)
709  // Case 4: Lambda in message send.
710  return CAK_NotAttributeSpecifier;
711 
712  // Case 2: Lambda in array size / index.
713  return CAK_InvalidAttributeSpecifier;
714 
715  case LambdaIntroducerTentativeParse::Invalid:
716  // No idea what this is; we couldn't parse it as a lambda-introducer.
717  // Might still be an attribute-specifier or a message send.
718  break;
719  }
720  }
721 
722  ConsumeBracket();
723 
724  // If we don't have a lambda-introducer, then we have an attribute or a
725  // message-send.
726  bool IsAttribute = true;
727  while (Tok.isNot(tok::r_square)) {
728  if (Tok.is(tok::comma)) {
729  // Case 1: Stray commas can only occur in attributes.
730  return CAK_AttributeSpecifier;
731  }
732 
733  // Parse the attribute-token, if present.
734  // C++11 [dcl.attr.grammar]:
735  // If a keyword or an alternative token that satisfies the syntactic
736  // requirements of an identifier is contained in an attribute-token,
737  // it is considered an identifier.
738  SourceLocation Loc;
739  if (!TryParseCXX11AttributeIdentifier(Loc)) {
740  IsAttribute = false;
741  break;
742  }
743  if (Tok.is(tok::coloncolon)) {
744  ConsumeToken();
745  if (!TryParseCXX11AttributeIdentifier(Loc)) {
746  IsAttribute = false;
747  break;
748  }
749  }
750 
751  // Parse the attribute-argument-clause, if present.
752  if (Tok.is(tok::l_paren)) {
753  ConsumeParen();
754  if (!SkipUntil(tok::r_paren)) {
755  IsAttribute = false;
756  break;
757  }
758  }
759 
760  TryConsumeToken(tok::ellipsis);
761 
762  if (!TryConsumeToken(tok::comma))
763  break;
764  }
765 
766  // An attribute must end ']]'.
767  if (IsAttribute) {
768  if (Tok.is(tok::r_square)) {
769  ConsumeBracket();
770  IsAttribute = Tok.is(tok::r_square);
771  } else {
772  IsAttribute = false;
773  }
774  }
775 
776  if (IsAttribute)
777  // Case 1: C++11 statement attribute.
778  return CAK_AttributeSpecifier;
779 
780  // Case 3: Message send.
781  return CAK_NotAttributeSpecifier;
782 }
783 
784 Parser::TPResult Parser::TryParsePtrOperatorSeq() {
785  while (true) {
787  return TPResult::Error;
788 
789  if (Tok.isOneOf(tok::star, tok::amp, tok::caret, tok::ampamp) ||
790  (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
791  // ptr-operator
792  ConsumeAnyToken();
793  while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict,
794  tok::kw__Nonnull, tok::kw__Nullable,
795  tok::kw__Null_unspecified))
796  ConsumeToken();
797  } else {
798  return TPResult::True;
799  }
800  }
801 }
802 
803 /// operator-function-id:
804 /// 'operator' operator
805 ///
806 /// operator: one of
807 /// new delete new[] delete[] + - * / % ^ [...]
808 ///
809 /// conversion-function-id:
810 /// 'operator' conversion-type-id
811 ///
812 /// conversion-type-id:
813 /// type-specifier-seq conversion-declarator[opt]
814 ///
815 /// conversion-declarator:
816 /// ptr-operator conversion-declarator[opt]
817 ///
818 /// literal-operator-id:
819 /// 'operator' string-literal identifier
820 /// 'operator' user-defined-string-literal
821 Parser::TPResult Parser::TryParseOperatorId() {
822  assert(Tok.is(tok::kw_operator));
823  ConsumeToken();
824 
825  // Maybe this is an operator-function-id.
826  switch (Tok.getKind()) {
827  case tok::kw_new: case tok::kw_delete:
828  ConsumeToken();
829  if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
830  ConsumeBracket();
831  ConsumeBracket();
832  }
833  return TPResult::True;
834 
835 #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
836  case tok::Token:
837 #define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
838 #include "clang/Basic/OperatorKinds.def"
839  ConsumeToken();
840  return TPResult::True;
841 
842  case tok::l_square:
843  if (NextToken().is(tok::r_square)) {
844  ConsumeBracket();
845  ConsumeBracket();
846  return TPResult::True;
847  }
848  break;
849 
850  case tok::l_paren:
851  if (NextToken().is(tok::r_paren)) {
852  ConsumeParen();
853  ConsumeParen();
854  return TPResult::True;
855  }
856  break;
857 
858  default:
859  break;
860  }
861 
862  // Maybe this is a literal-operator-id.
863  if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
864  bool FoundUDSuffix = false;
865  do {
866  FoundUDSuffix |= Tok.hasUDSuffix();
867  ConsumeStringToken();
868  } while (isTokenStringLiteral());
869 
870  if (!FoundUDSuffix) {
871  if (Tok.is(tok::identifier))
872  ConsumeToken();
873  else
874  return TPResult::Error;
875  }
876  return TPResult::True;
877  }
878 
879  // Maybe this is a conversion-function-id.
880  bool AnyDeclSpecifiers = false;
881  while (true) {
882  TPResult TPR = isCXXDeclarationSpecifier();
883  if (TPR == TPResult::Error)
884  return TPR;
885  if (TPR == TPResult::False) {
886  if (!AnyDeclSpecifiers)
887  return TPResult::Error;
888  break;
889  }
890  if (TryConsumeDeclarationSpecifier() == TPResult::Error)
891  return TPResult::Error;
892  AnyDeclSpecifiers = true;
893  }
894  return TryParsePtrOperatorSeq();
895 }
896 
897 /// declarator:
898 /// direct-declarator
899 /// ptr-operator declarator
900 ///
901 /// direct-declarator:
902 /// declarator-id
903 /// direct-declarator '(' parameter-declaration-clause ')'
904 /// cv-qualifier-seq[opt] exception-specification[opt]
905 /// direct-declarator '[' constant-expression[opt] ']'
906 /// '(' declarator ')'
907 /// [GNU] '(' attributes declarator ')'
908 ///
909 /// abstract-declarator:
910 /// ptr-operator abstract-declarator[opt]
911 /// direct-abstract-declarator
912 ///
913 /// direct-abstract-declarator:
914 /// direct-abstract-declarator[opt]
915 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
916 /// exception-specification[opt]
917 /// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
918 /// '(' abstract-declarator ')'
919 /// [C++0x] ...
920 ///
921 /// ptr-operator:
922 /// '*' cv-qualifier-seq[opt]
923 /// '&'
924 /// [C++0x] '&&' [TODO]
925 /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
926 ///
927 /// cv-qualifier-seq:
928 /// cv-qualifier cv-qualifier-seq[opt]
929 ///
930 /// cv-qualifier:
931 /// 'const'
932 /// 'volatile'
933 ///
934 /// declarator-id:
935 /// '...'[opt] id-expression
936 ///
937 /// id-expression:
938 /// unqualified-id
939 /// qualified-id [TODO]
940 ///
941 /// unqualified-id:
942 /// identifier
943 /// operator-function-id
944 /// conversion-function-id
945 /// literal-operator-id
946 /// '~' class-name [TODO]
947 /// '~' decltype-specifier [TODO]
948 /// template-id [TODO]
949 ///
950 Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
951  bool mayHaveIdentifier,
952  bool mayHaveDirectInit) {
953  // declarator:
954  // direct-declarator
955  // ptr-operator declarator
956  if (TryParsePtrOperatorSeq() == TPResult::Error)
957  return TPResult::Error;
958 
959  // direct-declarator:
960  // direct-abstract-declarator:
961  if (Tok.is(tok::ellipsis))
962  ConsumeToken();
963 
964  if ((Tok.isOneOf(tok::identifier, tok::kw_operator) ||
965  (Tok.is(tok::annot_cxxscope) && (NextToken().is(tok::identifier) ||
966  NextToken().is(tok::kw_operator)))) &&
967  mayHaveIdentifier) {
968  // declarator-id
969  if (Tok.is(tok::annot_cxxscope))
970  ConsumeAnnotationToken();
971  else if (Tok.is(tok::identifier))
972  TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
973  if (Tok.is(tok::kw_operator)) {
974  if (TryParseOperatorId() == TPResult::Error)
975  return TPResult::Error;
976  } else
977  ConsumeToken();
978  } else if (Tok.is(tok::l_paren)) {
979  ConsumeParen();
980  if (mayBeAbstract &&
981  (Tok.is(tok::r_paren) || // 'int()' is a function.
982  // 'int(...)' is a function.
983  (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
984  isDeclarationSpecifier())) { // 'int(int)' is a function.
985  // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
986  // exception-specification[opt]
987  TPResult TPR = TryParseFunctionDeclarator();
988  if (TPR != TPResult::Ambiguous)
989  return TPR;
990  } else {
991  // '(' declarator ')'
992  // '(' attributes declarator ')'
993  // '(' abstract-declarator ')'
994  if (Tok.isOneOf(tok::kw___attribute, tok::kw___declspec, tok::kw___cdecl,
995  tok::kw___stdcall, tok::kw___fastcall, tok::kw___thiscall,
996  tok::kw___regcall, tok::kw___vectorcall))
997  return TPResult::True; // attributes indicate declaration
998  TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
999  if (TPR != TPResult::Ambiguous)
1000  return TPR;
1001  if (Tok.isNot(tok::r_paren))
1002  return TPResult::False;
1003  ConsumeParen();
1004  }
1005  } else if (!mayBeAbstract) {
1006  return TPResult::False;
1007  }
1008 
1009  if (mayHaveDirectInit)
1010  return TPResult::Ambiguous;
1011 
1012  while (1) {
1013  TPResult TPR(TPResult::Ambiguous);
1014 
1015  if (Tok.is(tok::l_paren)) {
1016  // Check whether we have a function declarator or a possible ctor-style
1017  // initializer that follows the declarator. Note that ctor-style
1018  // initializers are not possible in contexts where abstract declarators
1019  // are allowed.
1020  if (!mayBeAbstract && !isCXXFunctionDeclarator())
1021  break;
1022 
1023  // direct-declarator '(' parameter-declaration-clause ')'
1024  // cv-qualifier-seq[opt] exception-specification[opt]
1025  ConsumeParen();
1026  TPR = TryParseFunctionDeclarator();
1027  } else if (Tok.is(tok::l_square)) {
1028  // direct-declarator '[' constant-expression[opt] ']'
1029  // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
1030  TPR = TryParseBracketDeclarator();
1031  } else if (Tok.is(tok::kw_requires)) {
1032  // declarator requires-clause
1033  // A requires clause indicates a function declaration.
1034  TPR = TPResult::True;
1035  } else {
1036  break;
1037  }
1038 
1039  if (TPR != TPResult::Ambiguous)
1040  return TPR;
1041  }
1042 
1043  return TPResult::Ambiguous;
1044 }
1045 
1046 Parser::TPResult
1047 Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
1048  switch (Kind) {
1049  // Obviously starts an expression.
1050  case tok::numeric_constant:
1051  case tok::char_constant:
1052  case tok::wide_char_constant:
1053  case tok::utf8_char_constant:
1054  case tok::utf16_char_constant:
1055  case tok::utf32_char_constant:
1056  case tok::string_literal:
1057  case tok::wide_string_literal:
1058  case tok::utf8_string_literal:
1059  case tok::utf16_string_literal:
1060  case tok::utf32_string_literal:
1061  case tok::l_square:
1062  case tok::l_paren:
1063  case tok::amp:
1064  case tok::ampamp:
1065  case tok::star:
1066  case tok::plus:
1067  case tok::plusplus:
1068  case tok::minus:
1069  case tok::minusminus:
1070  case tok::tilde:
1071  case tok::exclaim:
1072  case tok::kw_sizeof:
1073  case tok::kw___func__:
1074  case tok::kw_const_cast:
1075  case tok::kw_delete:
1076  case tok::kw_dynamic_cast:
1077  case tok::kw_false:
1078  case tok::kw_new:
1079  case tok::kw_operator:
1080  case tok::kw_reinterpret_cast:
1081  case tok::kw_static_cast:
1082  case tok::kw_this:
1083  case tok::kw_throw:
1084  case tok::kw_true:
1085  case tok::kw_typeid:
1086  case tok::kw_alignof:
1087  case tok::kw_noexcept:
1088  case tok::kw_nullptr:
1089  case tok::kw__Alignof:
1090  case tok::kw___null:
1091  case tok::kw___alignof:
1092  case tok::kw___builtin_choose_expr:
1093  case tok::kw___builtin_offsetof:
1094  case tok::kw___builtin_va_arg:
1095  case tok::kw___imag:
1096  case tok::kw___real:
1097  case tok::kw___FUNCTION__:
1098  case tok::kw___FUNCDNAME__:
1099  case tok::kw___FUNCSIG__:
1100  case tok::kw_L__FUNCTION__:
1101  case tok::kw_L__FUNCSIG__:
1102  case tok::kw___PRETTY_FUNCTION__:
1103  case tok::kw___uuidof:
1104 #define TYPE_TRAIT(N,Spelling,K) \
1105  case tok::kw_##Spelling:
1106 #include "clang/Basic/TokenKinds.def"
1107  return TPResult::True;
1108 
1109  // Obviously starts a type-specifier-seq:
1110  case tok::kw_char:
1111  case tok::kw_const:
1112  case tok::kw_double:
1113  case tok::kw__Float16:
1114  case tok::kw___float128:
1115  case tok::kw_enum:
1116  case tok::kw_half:
1117  case tok::kw_float:
1118  case tok::kw_int:
1119  case tok::kw_long:
1120  case tok::kw___int64:
1121  case tok::kw___int128:
1122  case tok::kw_restrict:
1123  case tok::kw_short:
1124  case tok::kw_signed:
1125  case tok::kw_struct:
1126  case tok::kw_union:
1127  case tok::kw_unsigned:
1128  case tok::kw_void:
1129  case tok::kw_volatile:
1130  case tok::kw__Bool:
1131  case tok::kw__Complex:
1132  case tok::kw_class:
1133  case tok::kw_typename:
1134  case tok::kw_wchar_t:
1135  case tok::kw_char8_t:
1136  case tok::kw_char16_t:
1137  case tok::kw_char32_t:
1138  case tok::kw__Decimal32:
1139  case tok::kw__Decimal64:
1140  case tok::kw__Decimal128:
1141  case tok::kw___interface:
1142  case tok::kw___thread:
1143  case tok::kw_thread_local:
1144  case tok::kw__Thread_local:
1145  case tok::kw_typeof:
1146  case tok::kw___underlying_type:
1147  case tok::kw___cdecl:
1148  case tok::kw___stdcall:
1149  case tok::kw___fastcall:
1150  case tok::kw___thiscall:
1151  case tok::kw___regcall:
1152  case tok::kw___vectorcall:
1153  case tok::kw___unaligned:
1154  case tok::kw___vector:
1155  case tok::kw___pixel:
1156  case tok::kw___bool:
1157  case tok::kw__Atomic:
1158 #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
1159 #include "clang/Basic/OpenCLImageTypes.def"
1160  case tok::kw___unknown_anytype:
1161  return TPResult::False;
1162 
1163  default:
1164  break;
1165  }
1166 
1167  return TPResult::Ambiguous;
1168 }
1169 
1170 bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
1171  return std::find(TentativelyDeclaredIdentifiers.begin(),
1172  TentativelyDeclaredIdentifiers.end(), II)
1173  != TentativelyDeclaredIdentifiers.end();
1174 }
1175 
1176 namespace {
1177 class TentativeParseCCC final : public CorrectionCandidateCallback {
1178 public:
1179  TentativeParseCCC(const Token &Next) {
1180  WantRemainingKeywords = false;
1181  WantTypeSpecifiers = Next.isOneOf(tok::l_paren, tok::r_paren, tok::greater,
1182  tok::l_brace, tok::identifier);
1183  }
1184 
1185  bool ValidateCandidate(const TypoCorrection &Candidate) override {
1186  // Reject any candidate that only resolves to instance members since they
1187  // aren't viable as standalone identifiers instead of member references.
1188  if (Candidate.isResolved() && !Candidate.isKeyword() &&
1189  llvm::all_of(Candidate,
1190  [](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
1191  return false;
1192 
1194  }
1195 
1196  std::unique_ptr<CorrectionCandidateCallback> clone() override {
1197  return std::make_unique<TentativeParseCCC>(*this);
1198  }
1199 };
1200 }
1201 /// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
1202 /// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
1203 /// be either a decl-specifier or a function-style cast, and TPResult::Error
1204 /// if a parsing error was found and reported.
1205 ///
1206 /// If InvalidAsDeclSpec is not null, some cases that would be ill-formed as
1207 /// declaration specifiers but possibly valid as some other kind of construct
1208 /// return TPResult::Ambiguous instead of TPResult::False. When this happens,
1209 /// the intent is to keep trying to disambiguate, on the basis that we might
1210 /// find a better reason to treat this construct as a declaration later on.
1211 /// When this happens and the name could possibly be valid in some other
1212 /// syntactic context, *InvalidAsDeclSpec is set to 'true'. The current cases
1213 /// that trigger this are:
1214 ///
1215 /// * When parsing X::Y (with no 'typename') where X is dependent
1216 /// * When parsing X<Y> where X is undeclared
1217 ///
1218 /// decl-specifier:
1219 /// storage-class-specifier
1220 /// type-specifier
1221 /// function-specifier
1222 /// 'friend'
1223 /// 'typedef'
1224 /// [C++11] 'constexpr'
1225 /// [C++20] 'consteval'
1226 /// [GNU] attributes declaration-specifiers[opt]
1227 ///
1228 /// storage-class-specifier:
1229 /// 'register'
1230 /// 'static'
1231 /// 'extern'
1232 /// 'mutable'
1233 /// 'auto'
1234 /// [GNU] '__thread'
1235 /// [C++11] 'thread_local'
1236 /// [C11] '_Thread_local'
1237 ///
1238 /// function-specifier:
1239 /// 'inline'
1240 /// 'virtual'
1241 /// 'explicit'
1242 ///
1243 /// typedef-name:
1244 /// identifier
1245 ///
1246 /// type-specifier:
1247 /// simple-type-specifier
1248 /// class-specifier
1249 /// enum-specifier
1250 /// elaborated-type-specifier
1251 /// typename-specifier
1252 /// cv-qualifier
1253 ///
1254 /// simple-type-specifier:
1255 /// '::'[opt] nested-name-specifier[opt] type-name
1256 /// '::'[opt] nested-name-specifier 'template'
1257 /// simple-template-id [TODO]
1258 /// 'char'
1259 /// 'wchar_t'
1260 /// 'bool'
1261 /// 'short'
1262 /// 'int'
1263 /// 'long'
1264 /// 'signed'
1265 /// 'unsigned'
1266 /// 'float'
1267 /// 'double'
1268 /// 'void'
1269 /// [GNU] typeof-specifier
1270 /// [GNU] '_Complex'
1271 /// [C++11] 'auto'
1272 /// [GNU] '__auto_type'
1273 /// [C++11] 'decltype' ( expression )
1274 /// [C++1y] 'decltype' ( 'auto' )
1275 ///
1276 /// type-name:
1277 /// class-name
1278 /// enum-name
1279 /// typedef-name
1280 ///
1281 /// elaborated-type-specifier:
1282 /// class-key '::'[opt] nested-name-specifier[opt] identifier
1283 /// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
1284 /// simple-template-id
1285 /// 'enum' '::'[opt] nested-name-specifier[opt] identifier
1286 ///
1287 /// enum-name:
1288 /// identifier
1289 ///
1290 /// enum-specifier:
1291 /// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
1292 /// 'enum' identifier[opt] '{' enumerator-list ',' '}'
1293 ///
1294 /// class-specifier:
1295 /// class-head '{' member-specification[opt] '}'
1296 ///
1297 /// class-head:
1298 /// class-key identifier[opt] base-clause[opt]
1299 /// class-key nested-name-specifier identifier base-clause[opt]
1300 /// class-key nested-name-specifier[opt] simple-template-id
1301 /// base-clause[opt]
1302 ///
1303 /// class-key:
1304 /// 'class'
1305 /// 'struct'
1306 /// 'union'
1307 ///
1308 /// cv-qualifier:
1309 /// 'const'
1310 /// 'volatile'
1311 /// [GNU] restrict
1312 ///
1313 Parser::TPResult
1314 Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
1315  bool *InvalidAsDeclSpec) {
1316  auto IsPlaceholderSpecifier = [&] (TemplateIdAnnotation *TemplateId,
1317  int Lookahead) {
1318  // We have a placeholder-constraint (we check for 'auto' or 'decltype' to
1319  // distinguish 'C<int>;' from 'C<int> auto c = 1;')
1320  return TemplateId->Kind == TNK_Concept_template &&
1321  GetLookAheadToken(Lookahead + 1).isOneOf(tok::kw_auto, tok::kw_decltype,
1322  // If we have an identifier here, the user probably forgot the
1323  // 'auto' in the placeholder constraint, e.g. 'C<int> x = 2;'
1324  // This will be diagnosed nicely later, so disambiguate as a
1325  // declaration.
1326  tok::identifier);
1327  };
1328  switch (Tok.getKind()) {
1329  case tok::identifier: {
1330  // Check for need to substitute AltiVec __vector keyword
1331  // for "vector" identifier.
1332  if (TryAltiVecVectorToken())
1333  return TPResult::True;
1334 
1335  const Token &Next = NextToken();
1336  // In 'foo bar', 'foo' is always a type name outside of Objective-C.
1337  if (!getLangOpts().ObjC && Next.is(tok::identifier))
1338  return TPResult::True;
1339 
1340  if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
1341  // Determine whether this is a valid expression. If not, we will hit
1342  // a parse error one way or another. In that case, tell the caller that
1343  // this is ambiguous. Typo-correct to type and expression keywords and
1344  // to types and identifiers, in order to try to recover from errors.
1345  TentativeParseCCC CCC(Next);
1346  switch (TryAnnotateName(&CCC)) {
1347  case ANK_Error:
1348  return TPResult::Error;
1349  case ANK_TentativeDecl:
1350  return TPResult::False;
1351  case ANK_TemplateName:
1352  // In C++17, this could be a type template for class template argument
1353  // deduction. Try to form a type annotation for it. If we're in a
1354  // template template argument, we'll undo this when checking the
1355  // validity of the argument.
1356  if (getLangOpts().CPlusPlus17) {
1358  return TPResult::Error;
1359  if (Tok.isNot(tok::identifier))
1360  break;
1361  }
1362 
1363  // A bare type template-name which can't be a template template
1364  // argument is an error, and was probably intended to be a type.
1365  return GreaterThanIsOperator ? TPResult::True : TPResult::False;
1366  case ANK_Unresolved:
1367  return InvalidAsDeclSpec ? TPResult::Ambiguous : TPResult::False;
1368  case ANK_Success:
1369  break;
1370  }
1371  assert(Tok.isNot(tok::identifier) &&
1372  "TryAnnotateName succeeded without producing an annotation");
1373  } else {
1374  // This might possibly be a type with a dependent scope specifier and
1375  // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1376  // since it will annotate as a primary expression, and we want to use the
1377  // "missing 'typename'" logic.
1379  return TPResult::Error;
1380  // If annotation failed, assume it's a non-type.
1381  // FIXME: If this happens due to an undeclared identifier, treat it as
1382  // ambiguous.
1383  if (Tok.is(tok::identifier))
1384  return TPResult::False;
1385  }
1386 
1387  // We annotated this token as something. Recurse to handle whatever we got.
1388  return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
1389  }
1390 
1391  case tok::kw_typename: // typename T::type
1392  // Annotate typenames and C++ scope specifiers. If we get one, just
1393  // recurse to handle whatever we get.
1395  return TPResult::Error;
1396  return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
1397 
1398  case tok::coloncolon: { // ::foo::bar
1399  const Token &Next = NextToken();
1400  if (Next.isOneOf(tok::kw_new, // ::new
1401  tok::kw_delete)) // ::delete
1402  return TPResult::False;
1403  LLVM_FALLTHROUGH;
1404  }
1405  case tok::kw___super:
1406  case tok::kw_decltype:
1407  // Annotate typenames and C++ scope specifiers. If we get one, just
1408  // recurse to handle whatever we get.
1410  return TPResult::Error;
1411  return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
1412 
1413  // decl-specifier:
1414  // storage-class-specifier
1415  // type-specifier
1416  // function-specifier
1417  // 'friend'
1418  // 'typedef'
1419  // 'constexpr'
1420  case tok::kw_friend:
1421  case tok::kw_typedef:
1422  case tok::kw_constexpr:
1423  case tok::kw_consteval:
1424  case tok::kw_constinit:
1425  // storage-class-specifier
1426  case tok::kw_register:
1427  case tok::kw_static:
1428  case tok::kw_extern:
1429  case tok::kw_mutable:
1430  case tok::kw_auto:
1431  case tok::kw___thread:
1432  case tok::kw_thread_local:
1433  case tok::kw__Thread_local:
1434  // function-specifier
1435  case tok::kw_inline:
1436  case tok::kw_virtual:
1437  case tok::kw_explicit:
1438 
1439  // Modules
1440  case tok::kw___module_private__:
1441 
1442  // Debugger support
1443  case tok::kw___unknown_anytype:
1444 
1445  // type-specifier:
1446  // simple-type-specifier
1447  // class-specifier
1448  // enum-specifier
1449  // elaborated-type-specifier
1450  // typename-specifier
1451  // cv-qualifier
1452 
1453  // class-specifier
1454  // elaborated-type-specifier
1455  case tok::kw_class:
1456  case tok::kw_struct:
1457  case tok::kw_union:
1458  case tok::kw___interface:
1459  // enum-specifier
1460  case tok::kw_enum:
1461  // cv-qualifier
1462  case tok::kw_const:
1463  case tok::kw_volatile:
1464  return TPResult::True;
1465 
1466  // OpenCL address space qualifiers
1467  case tok::kw_private:
1468  if (!getLangOpts().OpenCL)
1469  return TPResult::False;
1470  LLVM_FALLTHROUGH;
1471  case tok::kw___private:
1472  case tok::kw___local:
1473  case tok::kw___global:
1474  case tok::kw___constant:
1475  case tok::kw___generic:
1476  // OpenCL access qualifiers
1477  case tok::kw___read_only:
1478  case tok::kw___write_only:
1479  case tok::kw___read_write:
1480  // OpenCL pipe
1481  case tok::kw_pipe:
1482 
1483  // GNU
1484  case tok::kw_restrict:
1485  case tok::kw__Complex:
1486  case tok::kw___attribute:
1487  case tok::kw___auto_type:
1488  return TPResult::True;
1489 
1490  // Microsoft
1491  case tok::kw___declspec:
1492  case tok::kw___cdecl:
1493  case tok::kw___stdcall:
1494  case tok::kw___fastcall:
1495  case tok::kw___thiscall:
1496  case tok::kw___regcall:
1497  case tok::kw___vectorcall:
1498  case tok::kw___w64:
1499  case tok::kw___sptr:
1500  case tok::kw___uptr:
1501  case tok::kw___ptr64:
1502  case tok::kw___ptr32:
1503  case tok::kw___forceinline:
1504  case tok::kw___unaligned:
1505  case tok::kw__Nonnull:
1506  case tok::kw__Nullable:
1507  case tok::kw__Null_unspecified:
1508  case tok::kw___kindof:
1509  return TPResult::True;
1510 
1511  // Borland
1512  case tok::kw___pascal:
1513  return TPResult::True;
1514 
1515  // AltiVec
1516  case tok::kw___vector:
1517  return TPResult::True;
1518 
1519  case tok::annot_template_id: {
1520  TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1521  // If lookup for the template-name found nothing, don't assume we have a
1522  // definitive disambiguation result yet.
1523  if (TemplateId->Kind == TNK_Undeclared_template && InvalidAsDeclSpec) {
1524  // 'template-id(' can be a valid expression but not a valid decl spec if
1525  // the template-name is not declared, but we don't consider this to be a
1526  // definitive disambiguation. In any other context, it's an error either
1527  // way.
1528  *InvalidAsDeclSpec = NextToken().is(tok::l_paren);
1529  return TPResult::Ambiguous;
1530  }
1531  if (IsPlaceholderSpecifier(TemplateId, /*Lookahead=*/0))
1532  return TPResult::True;
1533  if (TemplateId->Kind != TNK_Type_template)
1534  return TPResult::False;
1535  CXXScopeSpec SS;
1536  AnnotateTemplateIdTokenAsType(SS);
1537  assert(Tok.is(tok::annot_typename));
1538  goto case_typename;
1539  }
1540 
1541  case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1542  // We've already annotated a scope; try to annotate a type.
1544  return TPResult::Error;
1545  if (!Tok.is(tok::annot_typename)) {
1546  if (Tok.is(tok::annot_cxxscope) &&
1547  NextToken().is(tok::annot_template_id)) {
1548  TemplateIdAnnotation *TemplateId =
1549  takeTemplateIdAnnotation(NextToken());
1550  if (IsPlaceholderSpecifier(TemplateId, /*Lookahead=*/1))
1551  return TPResult::True;
1552  }
1553  // If the next token is an identifier or a type qualifier, then this
1554  // can't possibly be a valid expression either.
1555  if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1556  CXXScopeSpec SS;
1557  Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1558  Tok.getAnnotationRange(),
1559  SS);
1560  if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
1561  RevertingTentativeParsingAction PA(*this);
1562  ConsumeAnnotationToken();
1563  ConsumeToken();
1564  bool isIdentifier = Tok.is(tok::identifier);
1565  TPResult TPR = TPResult::False;
1566  if (!isIdentifier)
1567  TPR = isCXXDeclarationSpecifier(BracedCastResult,
1568  InvalidAsDeclSpec);
1569 
1570  if (isIdentifier ||
1571  TPR == TPResult::True || TPR == TPResult::Error)
1572  return TPResult::Error;
1573 
1574  if (InvalidAsDeclSpec) {
1575  // We can't tell whether this is a missing 'typename' or a valid
1576  // expression.
1577  *InvalidAsDeclSpec = true;
1578  return TPResult::Ambiguous;
1579  } else {
1580  // In MS mode, if InvalidAsDeclSpec is not provided, and the tokens
1581  // are or the form *) or &) *> or &> &&>, this can't be an expression.
1582  // The typename must be missing.
1583  if (getLangOpts().MSVCCompat) {
1584  if (((Tok.is(tok::amp) || Tok.is(tok::star)) &&
1585  (NextToken().is(tok::r_paren) ||
1586  NextToken().is(tok::greater))) ||
1587  (Tok.is(tok::ampamp) && NextToken().is(tok::greater)))
1588  return TPResult::True;
1589  }
1590  }
1591  } else {
1592  // Try to resolve the name. If it doesn't exist, assume it was
1593  // intended to name a type and keep disambiguating.
1594  switch (TryAnnotateName()) {
1595  case ANK_Error:
1596  return TPResult::Error;
1597  case ANK_TentativeDecl:
1598  return TPResult::False;
1599  case ANK_TemplateName:
1600  // In C++17, this could be a type template for class template
1601  // argument deduction.
1602  if (getLangOpts().CPlusPlus17) {
1604  return TPResult::Error;
1605  if (Tok.isNot(tok::identifier))
1606  break;
1607  }
1608 
1609  // A bare type template-name which can't be a template template
1610  // argument is an error, and was probably intended to be a type.
1611  // In C++17, this could be class template argument deduction.
1612  return (getLangOpts().CPlusPlus17 || GreaterThanIsOperator)
1613  ? TPResult::True
1614  : TPResult::False;
1615  case ANK_Unresolved:
1616  return InvalidAsDeclSpec ? TPResult::Ambiguous : TPResult::False;
1617  case ANK_Success:
1618  break;
1619  }
1620 
1621  // Annotated it, check again.
1622  assert(Tok.isNot(tok::annot_cxxscope) ||
1623  NextToken().isNot(tok::identifier));
1624  return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
1625  }
1626  }
1627  return TPResult::False;
1628  }
1629  // If that succeeded, fallthrough into the generic simple-type-id case.
1630  LLVM_FALLTHROUGH;
1631 
1632  // The ambiguity resides in a simple-type-specifier/typename-specifier
1633  // followed by a '('. The '(' could either be the start of:
1634  //
1635  // direct-declarator:
1636  // '(' declarator ')'
1637  //
1638  // direct-abstract-declarator:
1639  // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1640  // exception-specification[opt]
1641  // '(' abstract-declarator ')'
1642  //
1643  // or part of a function-style cast expression:
1644  //
1645  // simple-type-specifier '(' expression-list[opt] ')'
1646  //
1647 
1648  // simple-type-specifier:
1649 
1650  case tok::annot_typename:
1651  case_typename:
1652  // In Objective-C, we might have a protocol-qualified type.
1653  if (getLangOpts().ObjC && NextToken().is(tok::less)) {
1654  // Tentatively parse the protocol qualifiers.
1655  RevertingTentativeParsingAction PA(*this);
1656  ConsumeAnyToken(); // The type token
1657 
1658  TPResult TPR = TryParseProtocolQualifiers();
1659  bool isFollowedByParen = Tok.is(tok::l_paren);
1660  bool isFollowedByBrace = Tok.is(tok::l_brace);
1661 
1662  if (TPR == TPResult::Error)
1663  return TPResult::Error;
1664 
1665  if (isFollowedByParen)
1666  return TPResult::Ambiguous;
1667 
1668  if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
1669  return BracedCastResult;
1670 
1671  return TPResult::True;
1672  }
1673  LLVM_FALLTHROUGH;
1674 
1675  case tok::kw_char:
1676  case tok::kw_wchar_t:
1677  case tok::kw_char8_t:
1678  case tok::kw_char16_t:
1679  case tok::kw_char32_t:
1680  case tok::kw_bool:
1681  case tok::kw_short:
1682  case tok::kw_int:
1683  case tok::kw_long:
1684  case tok::kw___int64:
1685  case tok::kw___int128:
1686  case tok::kw_signed:
1687  case tok::kw_unsigned:
1688  case tok::kw_half:
1689  case tok::kw_float:
1690  case tok::kw_double:
1691  case tok::kw__Float16:
1692  case tok::kw___float128:
1693  case tok::kw_void:
1694  case tok::annot_decltype:
1695 #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
1696 #include "clang/Basic/OpenCLImageTypes.def"
1697  if (NextToken().is(tok::l_paren))
1698  return TPResult::Ambiguous;
1699 
1700  // This is a function-style cast in all cases we disambiguate other than
1701  // one:
1702  // struct S {
1703  // enum E : int { a = 4 }; // enum
1704  // enum E : int { 4 }; // bit-field
1705  // };
1706  if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
1707  return BracedCastResult;
1708 
1709  if (isStartOfObjCClassMessageMissingOpenBracket())
1710  return TPResult::False;
1711 
1712  return TPResult::True;
1713 
1714  // GNU typeof support.
1715  case tok::kw_typeof: {
1716  if (NextToken().isNot(tok::l_paren))
1717  return TPResult::True;
1718 
1719  RevertingTentativeParsingAction PA(*this);
1720 
1721  TPResult TPR = TryParseTypeofSpecifier();
1722  bool isFollowedByParen = Tok.is(tok::l_paren);
1723  bool isFollowedByBrace = Tok.is(tok::l_brace);
1724 
1725  if (TPR == TPResult::Error)
1726  return TPResult::Error;
1727 
1728  if (isFollowedByParen)
1729  return TPResult::Ambiguous;
1730 
1731  if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
1732  return BracedCastResult;
1733 
1734  return TPResult::True;
1735  }
1736 
1737  // C++0x type traits support
1738  case tok::kw___underlying_type:
1739  return TPResult::True;
1740 
1741  // C11 _Atomic
1742  case tok::kw__Atomic:
1743  return TPResult::True;
1744 
1745  default:
1746  return TPResult::False;
1747  }
1748 }
1749 
1750 bool Parser::isCXXDeclarationSpecifierAType() {
1751  switch (Tok.getKind()) {
1752  // typename-specifier
1753  case tok::annot_decltype:
1754  case tok::annot_template_id:
1755  case tok::annot_typename:
1756  case tok::kw_typeof:
1757  case tok::kw___underlying_type:
1758  return true;
1759 
1760  // elaborated-type-specifier
1761  case tok::kw_class:
1762  case tok::kw_struct:
1763  case tok::kw_union:
1764  case tok::kw___interface:
1765  case tok::kw_enum:
1766  return true;
1767 
1768  // simple-type-specifier
1769  case tok::kw_char:
1770  case tok::kw_wchar_t:
1771  case tok::kw_char8_t:
1772  case tok::kw_char16_t:
1773  case tok::kw_char32_t:
1774  case tok::kw_bool:
1775  case tok::kw_short:
1776  case tok::kw_int:
1777  case tok::kw_long:
1778  case tok::kw___int64:
1779  case tok::kw___int128:
1780  case tok::kw_signed:
1781  case tok::kw_unsigned:
1782  case tok::kw_half:
1783  case tok::kw_float:
1784  case tok::kw_double:
1785  case tok::kw__Float16:
1786  case tok::kw___float128:
1787  case tok::kw_void:
1788  case tok::kw___unknown_anytype:
1789  case tok::kw___auto_type:
1790 #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
1791 #include "clang/Basic/OpenCLImageTypes.def"
1792  return true;
1793 
1794  case tok::kw_auto:
1795  return getLangOpts().CPlusPlus11;
1796 
1797  case tok::kw__Atomic:
1798  // "_Atomic foo"
1799  return NextToken().is(tok::l_paren);
1800 
1801  default:
1802  return false;
1803  }
1804 }
1805 
1806 /// [GNU] typeof-specifier:
1807 /// 'typeof' '(' expressions ')'
1808 /// 'typeof' '(' type-name ')'
1809 ///
1810 Parser::TPResult Parser::TryParseTypeofSpecifier() {
1811  assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1812  ConsumeToken();
1813 
1814  assert(Tok.is(tok::l_paren) && "Expected '('");
1815  // Parse through the parens after 'typeof'.
1816  ConsumeParen();
1817  if (!SkipUntil(tok::r_paren, StopAtSemi))
1818  return TPResult::Error;
1819 
1820  return TPResult::Ambiguous;
1821 }
1822 
1823 /// [ObjC] protocol-qualifiers:
1824 //// '<' identifier-list '>'
1825 Parser::TPResult Parser::TryParseProtocolQualifiers() {
1826  assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1827  ConsumeToken();
1828  do {
1829  if (Tok.isNot(tok::identifier))
1830  return TPResult::Error;
1831  ConsumeToken();
1832 
1833  if (Tok.is(tok::comma)) {
1834  ConsumeToken();
1835  continue;
1836  }
1837 
1838  if (Tok.is(tok::greater)) {
1839  ConsumeToken();
1840  return TPResult::Ambiguous;
1841  }
1842  } while (false);
1843 
1844  return TPResult::Error;
1845 }
1846 
1847 /// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1848 /// a constructor-style initializer, when parsing declaration statements.
1849 /// Returns true for function declarator and false for constructor-style
1850 /// initializer.
1851 /// If during the disambiguation process a parsing error is encountered,
1852 /// the function returns true to let the declaration parsing code handle it.
1853 ///
1854 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1855 /// exception-specification[opt]
1856 ///
1857 bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1858 
1859  // C++ 8.2p1:
1860  // The ambiguity arising from the similarity between a function-style cast and
1861  // a declaration mentioned in 6.8 can also occur in the context of a
1862  // declaration. In that context, the choice is between a function declaration
1863  // with a redundant set of parentheses around a parameter name and an object
1864  // declaration with a function-style cast as the initializer. Just as for the
1865  // ambiguities mentioned in 6.8, the resolution is to consider any construct
1866  // that could possibly be a declaration a declaration.
1867 
1868  RevertingTentativeParsingAction PA(*this);
1869 
1870  ConsumeParen();
1871  bool InvalidAsDeclaration = false;
1872  TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
1873  if (TPR == TPResult::Ambiguous) {
1874  if (Tok.isNot(tok::r_paren))
1875  TPR = TPResult::False;
1876  else {
1877  const Token &Next = NextToken();
1878  if (Next.isOneOf(tok::amp, tok::ampamp, tok::kw_const, tok::kw_volatile,
1879  tok::kw_throw, tok::kw_noexcept, tok::l_square,
1880  tok::l_brace, tok::kw_try, tok::equal, tok::arrow) ||
1881  isCXX11VirtSpecifier(Next))
1882  // The next token cannot appear after a constructor-style initializer,
1883  // and can appear next in a function definition. This must be a function
1884  // declarator.
1885  TPR = TPResult::True;
1886  else if (InvalidAsDeclaration)
1887  // Use the absence of 'typename' as a tie-breaker.
1888  TPR = TPResult::False;
1889  }
1890  }
1891 
1892  if (IsAmbiguous && TPR == TPResult::Ambiguous)
1893  *IsAmbiguous = true;
1894 
1895  // In case of an error, let the declaration parsing code handle it.
1896  return TPR != TPResult::False;
1897 }
1898 
1899 /// parameter-declaration-clause:
1900 /// parameter-declaration-list[opt] '...'[opt]
1901 /// parameter-declaration-list ',' '...'
1902 ///
1903 /// parameter-declaration-list:
1904 /// parameter-declaration
1905 /// parameter-declaration-list ',' parameter-declaration
1906 ///
1907 /// parameter-declaration:
1908 /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1909 /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1910 /// '=' assignment-expression
1911 /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1912 /// attributes[opt]
1913 /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1914 /// attributes[opt] '=' assignment-expression
1915 ///
1916 Parser::TPResult
1917 Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
1918  bool VersusTemplateArgument) {
1919 
1920  if (Tok.is(tok::r_paren))
1921  return TPResult::Ambiguous;
1922 
1923  // parameter-declaration-list[opt] '...'[opt]
1924  // parameter-declaration-list ',' '...'
1925  //
1926  // parameter-declaration-list:
1927  // parameter-declaration
1928  // parameter-declaration-list ',' parameter-declaration
1929  //
1930  while (1) {
1931  // '...'[opt]
1932  if (Tok.is(tok::ellipsis)) {
1933  ConsumeToken();
1934  if (Tok.is(tok::r_paren))
1935  return TPResult::True; // '...)' is a sign of a function declarator.
1936  else
1937  return TPResult::False;
1938  }
1939 
1940  // An attribute-specifier-seq here is a sign of a function declarator.
1941  if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1942  /*OuterMightBeMessageSend*/true))
1943  return TPResult::True;
1944 
1945  ParsedAttributes attrs(AttrFactory);
1946  MaybeParseMicrosoftAttributes(attrs);
1947 
1948  // decl-specifier-seq
1949  // A parameter-declaration's initializer must be preceded by an '=', so
1950  // decl-specifier-seq '{' is not a parameter in C++11.
1951  TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
1952  InvalidAsDeclaration);
1953  // A declaration-specifier (not followed by '(' or '{') means this can't be
1954  // an expression, but it could still be a template argument.
1955  if (TPR != TPResult::Ambiguous &&
1956  !(VersusTemplateArgument && TPR == TPResult::True))
1957  return TPR;
1958 
1959  bool SeenType = false;
1960  do {
1961  SeenType |= isCXXDeclarationSpecifierAType();
1962  if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1963  return TPResult::Error;
1964 
1965  // If we see a parameter name, this can't be a template argument.
1966  if (SeenType && Tok.is(tok::identifier))
1967  return TPResult::True;
1968 
1969  TPR = isCXXDeclarationSpecifier(TPResult::False,
1970  InvalidAsDeclaration);
1971  if (TPR == TPResult::Error)
1972  return TPR;
1973 
1974  // Two declaration-specifiers means this can't be an expression.
1975  if (TPR == TPResult::True && !VersusTemplateArgument)
1976  return TPR;
1977  } while (TPR != TPResult::False);
1978 
1979  // declarator
1980  // abstract-declarator[opt]
1981  TPR = TryParseDeclarator(true/*mayBeAbstract*/);
1982  if (TPR != TPResult::Ambiguous)
1983  return TPR;
1984 
1985  // [GNU] attributes[opt]
1986  if (Tok.is(tok::kw___attribute))
1987  return TPResult::True;
1988 
1989  // If we're disambiguating a template argument in a default argument in
1990  // a class definition versus a parameter declaration, an '=' here
1991  // disambiguates the parse one way or the other.
1992  // If this is a parameter, it must have a default argument because
1993  // (a) the previous parameter did, and
1994  // (b) this must be the first declaration of the function, so we can't
1995  // inherit any default arguments from elsewhere.
1996  // If we see an ')', then we've reached the end of a
1997  // parameter-declaration-clause, and the last param is missing its default
1998  // argument.
1999  if (VersusTemplateArgument)
2000  return Tok.isOneOf(tok::equal, tok::r_paren) ? TPResult::True
2001  : TPResult::False;
2002 
2003  if (Tok.is(tok::equal)) {
2004  // '=' assignment-expression
2005  // Parse through assignment-expression.
2006  // FIXME: assignment-expression may contain an unparenthesized comma.
2007  if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
2008  return TPResult::Error;
2009  }
2010 
2011  if (Tok.is(tok::ellipsis)) {
2012  ConsumeToken();
2013  if (Tok.is(tok::r_paren))
2014  return TPResult::True; // '...)' is a sign of a function declarator.
2015  else
2016  return TPResult::False;
2017  }
2018 
2019  if (!TryConsumeToken(tok::comma))
2020  break;
2021  }
2022 
2023  return TPResult::Ambiguous;
2024 }
2025 
2026 /// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
2027 /// parsing as a function declarator.
2028 /// If TryParseFunctionDeclarator fully parsed the function declarator, it will
2029 /// return TPResult::Ambiguous, otherwise it will return either False() or
2030 /// Error().
2031 ///
2032 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
2033 /// exception-specification[opt]
2034 ///
2035 /// exception-specification:
2036 /// 'throw' '(' type-id-list[opt] ')'
2037 ///
2038 Parser::TPResult Parser::TryParseFunctionDeclarator() {
2039  // The '(' is already parsed.
2040 
2041  TPResult TPR = TryParseParameterDeclarationClause();
2042  if (TPR == TPResult::Ambiguous && Tok.isNot(tok::r_paren))
2043  TPR = TPResult::False;
2044 
2045  if (TPR == TPResult::False || TPR == TPResult::Error)
2046  return TPR;
2047 
2048  // Parse through the parens.
2049  if (!SkipUntil(tok::r_paren, StopAtSemi))
2050  return TPResult::Error;
2051 
2052  // cv-qualifier-seq
2053  while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw___unaligned,
2054  tok::kw_restrict))
2055  ConsumeToken();
2056 
2057  // ref-qualifier[opt]
2058  if (Tok.isOneOf(tok::amp, tok::ampamp))
2059  ConsumeToken();
2060 
2061  // exception-specification
2062  if (Tok.is(tok::kw_throw)) {
2063  ConsumeToken();
2064  if (Tok.isNot(tok::l_paren))
2065  return TPResult::Error;
2066 
2067  // Parse through the parens after 'throw'.
2068  ConsumeParen();
2069  if (!SkipUntil(tok::r_paren, StopAtSemi))
2070  return TPResult::Error;
2071  }
2072  if (Tok.is(tok::kw_noexcept)) {
2073  ConsumeToken();
2074  // Possibly an expression as well.
2075  if (Tok.is(tok::l_paren)) {
2076  // Find the matching rparen.
2077  ConsumeParen();
2078  if (!SkipUntil(tok::r_paren, StopAtSemi))
2079  return TPResult::Error;
2080  }
2081  }
2082 
2083  return TPResult::Ambiguous;
2084 }
2085 
2086 /// '[' constant-expression[opt] ']'
2087 ///
2088 Parser::TPResult Parser::TryParseBracketDeclarator() {
2089  ConsumeBracket();
2090 
2091  // A constant-expression cannot begin with a '{', but the
2092  // expr-or-braced-init-list of a postfix-expression can.
2093  if (Tok.is(tok::l_brace))
2094  return TPResult::False;
2095 
2096  if (!SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch))
2097  return TPResult::Error;
2098 
2099  // If we hit a comma before the ']', this is not a constant-expression,
2100  // but might still be the expr-or-braced-init-list of a postfix-expression.
2101  if (Tok.isNot(tok::r_square))
2102  return TPResult::False;
2103 
2104  ConsumeBracket();
2105  return TPResult::Ambiguous;
2106 }
2107 
2108 /// Determine whether we might be looking at the '<' template-argument-list '>'
2109 /// of a template-id or simple-template-id, rather than a less-than comparison.
2110 /// This will often fail and produce an ambiguity, but should never be wrong
2111 /// if it returns True or False.
2112 Parser::TPResult Parser::isTemplateArgumentList(unsigned TokensToSkip) {
2113  if (!TokensToSkip) {
2114  if (Tok.isNot(tok::less))
2115  return TPResult::False;
2116  if (NextToken().is(tok::greater))
2117  return TPResult::True;
2118  }
2119 
2120  RevertingTentativeParsingAction PA(*this);
2121 
2122  while (TokensToSkip) {
2123  ConsumeAnyToken();
2124  --TokensToSkip;
2125  }
2126 
2127  if (!TryConsumeToken(tok::less))
2128  return TPResult::False;
2129 
2130  // We can't do much to tell an expression apart from a template-argument,
2131  // but one good distinguishing factor is that a "decl-specifier" not
2132  // followed by '(' or '{' can't appear in an expression.
2133  bool InvalidAsTemplateArgumentList = false;
2134  if (isCXXDeclarationSpecifier(TPResult::False,
2135  &InvalidAsTemplateArgumentList) ==
2136  TPResult::True)
2137  return TPResult::True;
2138  if (InvalidAsTemplateArgumentList)
2139  return TPResult::False;
2140 
2141  // FIXME: In many contexts, X<thing1, Type> can only be a
2142  // template-argument-list. But that's not true in general:
2143  //
2144  // using b = int;
2145  // void f() {
2146  // int a = A<B, b, c = C>D; // OK, declares b, not a template-id.
2147  //
2148  // X<Y<0, int> // ', int>' might be end of X's template argument list
2149  //
2150  // We might be able to disambiguate a few more cases if we're careful.
2151 
2152  // A template-argument-list must be terminated by a '>'.
2153  if (SkipUntil({tok::greater, tok::greatergreater, tok::greatergreatergreater},
2155  return TPResult::Ambiguous;
2156  return TPResult::False;
2157 }
2158 
2159 /// Determine whether we might be looking at the '(' of a C++20 explicit(bool)
2160 /// in an earlier language mode.
2161 Parser::TPResult Parser::isExplicitBool() {
2162  assert(Tok.is(tok::l_paren) && "expected to be looking at a '(' token");
2163 
2164  RevertingTentativeParsingAction PA(*this);
2165  ConsumeParen();
2166 
2167  // We can only have 'explicit' on a constructor, conversion function, or
2168  // deduction guide. The declarator of a deduction guide cannot be
2169  // parenthesized, so we know this isn't a deduction guide. So the only
2170  // thing we need to check for is some number of parens followed by either
2171  // the current class name or 'operator'.
2172  while (Tok.is(tok::l_paren))
2173  ConsumeParen();
2174 
2176  return TPResult::Error;
2177 
2178  // Class-scope constructor and conversion function names can't really be
2179  // qualified, but we get better diagnostics if we assume they can be.
2180  CXXScopeSpec SS;
2181  if (Tok.is(tok::annot_cxxscope)) {
2182  Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2183  Tok.getAnnotationRange(),
2184  SS);
2185  ConsumeAnnotationToken();
2186  }
2187 
2188  // 'explicit(operator' might be explicit(bool) or the declaration of a
2189  // conversion function, but it's probably a conversion function.
2190  if (Tok.is(tok::kw_operator))
2191  return TPResult::Ambiguous;
2192 
2193  // If this can't be a constructor name, it can only be explicit(bool).
2194  if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
2195  return TPResult::True;
2196  if (!Actions.isCurrentClassName(Tok.is(tok::identifier)
2197  ? *Tok.getIdentifierInfo()
2198  : *takeTemplateIdAnnotation(Tok)->Name,
2199  getCurScope(), &SS))
2200  return TPResult::True;
2201  // Formally, we must have a right-paren after the constructor name to match
2202  // the grammar for a constructor. But clang permits a parenthesized
2203  // constructor declarator, so also allow a constructor declarator to follow
2204  // with no ')' token after the constructor name.
2205  if (!NextToken().is(tok::r_paren) &&
2206  !isConstructorDeclarator(/*Unqualified=*/SS.isEmpty(),
2207  /*DeductionGuide=*/false))
2208  return TPResult::True;
2209 
2210  // Might be explicit(bool) or a parenthesized constructor name.
2211  return TPResult::Ambiguous;
2212 }
Simple class containing the result of Sema::CorrectTypo.
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {...
Definition: Token.h:97
bool isEmpty() const
No scope specifier.
Definition: DeclSpec.h:189
TemplateNameKind Kind
The kind of template that Template refers to.
Parser - This implements a parser for the C family of languages.
Definition: Parser.h:57
tok::TokenKind getKind() const
Definition: Token.h:92
bool SkipUntil(tok::TokenKind T, SkipUntilFlags Flags=static_cast< SkipUntilFlags >(0))
SkipUntil - Read tokens until we get to the specified token, then consume it (unless StopBeforeMatch ...
Definition: Parser.h:1118
bool CanBeExpression
Information about a template-id annotation token.
const Token & NextToken()
NextToken - This peeks ahead one token and returns it without consuming it.
Definition: Parser.h:759
bool TryConsumeToken(tok::TokenKind Expected)
Definition: Parser.h:460
One of these records is kept for each identifier that is lexed.
Lookup for the name failed, but we&#39;re assuming it was a template name anyway.
Definition: TemplateKinds.h:50
LineState State
Base class for callback objects used by Sema::CorrectTypo to check the validity of a potential typo c...
Token - This structure provides full information about a lexed token.
Definition: Token.h:34
bool isNot(T Kind) const
Definition: FormatToken.h:330
ConditionDeclarationOrInitStatementState(Parser &P, bool CanBeInitStatement, bool CanBeForRangeDecl)
Represents a C++ nested-name-specifier or a global scope specifier.
Definition: DeclSpec.h:63
SourceLocation ConsumeAnyToken(bool ConsumeCodeCompletionTok=false)
ConsumeAnyToken - Dispatch to the right Consume* method based on the current token type...
Definition: Parser.h:480
bool isOneOf(A K1, B K2) const
Definition: FormatToken.h:323
virtual bool ValidateCandidate(const TypoCorrection &candidate)
Simple predicate used by the default RankCandidate to determine whether to return an edit distance of...
The name refers to a concept.
Definition: TemplateKinds.h:52
const LangOptions & getLangOpts() const
Definition: Parser.h:407
NestedNameSpecifier * getScopeRep() const
Retrieve the representation of the nested-name-specifier.
Definition: DeclSpec.h:76
Kind
Stop skipping at semicolon.
Definition: Parser.h:1098
Encodes a location in the source.
bool TryAnnotateTypeOrScopeToken()
TryAnnotateTypeOrScopeToken - If the current token position is on a typename (possibly qualified in C...
Definition: Parser.cpp:1800
bool is(tok::TokenKind Kind) const
Definition: FormatToken.h:314
bool TryAnnotateOptionalCXXScopeToken(bool EnteringContext=false)
Definition: Parser.h:815
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:24
Scope * getCurScope() const
Definition: Parser.h:414
bool isNot(tok::TokenKind K) const
Definition: Token.h:98
Dataflow Directional Tag Classes.
bool isDependent() const
Whether this nested name specifier refers to a dependent type or not.
bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const
Definition: Token.h:99
The name refers to a template whose specialization produces a type.
Definition: TemplateKinds.h:30
SourceLocation ConsumeToken()
ConsumeToken - Consume the current &#39;peek token&#39; and lex the next one.
Definition: Parser.h:452
Represents a complete lambda introducer.
Definition: DeclSpec.h:2636
This represents a decl that may have a name.
Definition: Decl.h:223
ParsedAttributes - A collection of parsed attributes.
Definition: ParsedAttr.h:824
bool isCXXInstanceMember() const
Determine whether the given declaration is an instance member of a C++ class.
Definition: Decl.cpp:1790
Stop skipping at specified token, but don&#39;t skip the token itself.
Definition: Parser.h:1100