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