ParseTentative.cpp revision 25d0a0f67d9e949ffbfc57bf487012f5cbfd886e
1//===--- ParseTentative.cpp - Ambiguity Resolution Parsing ----------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file implements the tentative parsing portions of the Parser
11//  interfaces, for ambiguity resolution.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
16#include "clang/Parse/ParseDiagnostic.h"
17#include "clang/Sema/ParsedTemplate.h"
18using namespace clang;
19
20/// isCXXDeclarationStatement - C++-specialized function that disambiguates
21/// between a declaration or an expression statement, when parsing function
22/// bodies. Returns true for declaration, false for expression.
23///
24///         declaration-statement:
25///           block-declaration
26///
27///         block-declaration:
28///           simple-declaration
29///           asm-definition
30///           namespace-alias-definition
31///           using-declaration
32///           using-directive
33/// [C++0x]   static_assert-declaration
34///
35///         asm-definition:
36///           'asm' '(' string-literal ')' ';'
37///
38///         namespace-alias-definition:
39///           'namespace' identifier = qualified-namespace-specifier ';'
40///
41///         using-declaration:
42///           'using' typename[opt] '::'[opt] nested-name-specifier
43///                 unqualified-id ';'
44///           'using' '::' unqualified-id ;
45///
46///         using-directive:
47///           'using' 'namespace' '::'[opt] nested-name-specifier[opt]
48///                 namespace-name ';'
49///
50bool Parser::isCXXDeclarationStatement() {
51  switch (Tok.getKind()) {
52    // asm-definition
53  case tok::kw_asm:
54    // namespace-alias-definition
55  case tok::kw_namespace:
56    // using-declaration
57    // using-directive
58  case tok::kw_using:
59    // static_assert-declaration
60  case tok::kw_static_assert:
61  case tok::kw__Static_assert:
62    return true;
63    // simple-declaration
64  default:
65    return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
66  }
67}
68
69/// isCXXSimpleDeclaration - C++-specialized function that disambiguates
70/// between a simple-declaration or an expression-statement.
71/// If during the disambiguation process a parsing error is encountered,
72/// the function returns true to let the declaration parsing code handle it.
73/// Returns false if the statement is disambiguated as expression.
74///
75/// simple-declaration:
76///   decl-specifier-seq init-declarator-list[opt] ';'
77///
78/// (if AllowForRangeDecl specified)
79/// for ( for-range-declaration : for-range-initializer ) statement
80/// for-range-declaration:
81///    attribute-specifier-seqopt type-specifier-seq declarator
82bool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) {
83  // C++ 6.8p1:
84  // There is an ambiguity in the grammar involving expression-statements and
85  // declarations: An expression-statement with a function-style explicit type
86  // conversion (5.2.3) as its leftmost subexpression can be indistinguishable
87  // from a declaration where the first declarator starts with a '('. In those
88  // cases the statement is a declaration. [Note: To disambiguate, the whole
89  // statement might have to be examined to determine if it is an
90  // expression-statement or a declaration].
91
92  // C++ 6.8p3:
93  // The disambiguation is purely syntactic; that is, the meaning of the names
94  // occurring in such a statement, beyond whether they are type-names or not,
95  // is not generally used in or changed by the disambiguation. Class
96  // templates are instantiated as necessary to determine if a qualified name
97  // is a type-name. Disambiguation precedes parsing, and a statement
98  // disambiguated as a declaration may be an ill-formed declaration.
99
100  // We don't have to parse all of the decl-specifier-seq part. There's only
101  // an ambiguity if the first decl-specifier is
102  // simple-type-specifier/typename-specifier followed by a '(', which may
103  // indicate a function-style cast expression.
104  // isCXXDeclarationSpecifier will return TPResult::Ambiguous() only in such
105  // a case.
106
107  TPResult TPR = isCXXDeclarationSpecifier();
108  if (TPR != TPResult::Ambiguous())
109    return TPR != TPResult::False(); // Returns true for TPResult::True() or
110                                     // TPResult::Error().
111
112  // FIXME: Add statistics about the number of ambiguous statements encountered
113  // and how they were resolved (number of declarations+number of expressions).
114
115  // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
116  // We need tentative parsing...
117
118  TentativeParsingAction PA(*this);
119  TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
120  PA.Revert();
121
122  // In case of an error, let the declaration parsing code handle it.
123  if (TPR == TPResult::Error())
124    return true;
125
126  // Declarations take precedence over expressions.
127  if (TPR == TPResult::Ambiguous())
128    TPR = TPResult::True();
129
130  assert(TPR == TPResult::True() || TPR == TPResult::False());
131  return TPR == TPResult::True();
132}
133
134/// simple-declaration:
135///   decl-specifier-seq init-declarator-list[opt] ';'
136///
137/// (if AllowForRangeDecl specified)
138/// for ( for-range-declaration : for-range-initializer ) statement
139/// for-range-declaration:
140///    attribute-specifier-seqopt type-specifier-seq declarator
141///
142Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
143  // We know that we have a simple-type-specifier/typename-specifier followed
144  // by a '('.
145  assert(isCXXDeclarationSpecifier() == TPResult::Ambiguous());
146
147  if (Tok.is(tok::kw_typeof))
148    TryParseTypeofSpecifier();
149  else {
150    ConsumeToken();
151
152    if (getLang().ObjC1 && Tok.is(tok::less))
153      TryParseProtocolQualifiers();
154  }
155
156  assert(Tok.is(tok::l_paren) && "Expected '('");
157
158  TPResult TPR = TryParseInitDeclaratorList();
159  if (TPR != TPResult::Ambiguous())
160    return TPR;
161
162  if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
163    return TPResult::False();
164
165  return TPResult::Ambiguous();
166}
167
168///       init-declarator-list:
169///         init-declarator
170///         init-declarator-list ',' init-declarator
171///
172///       init-declarator:
173///         declarator initializer[opt]
174/// [GNU]   declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
175///
176/// initializer:
177///   '=' initializer-clause
178///   '(' expression-list ')'
179///
180/// initializer-clause:
181///   assignment-expression
182///   '{' initializer-list ','[opt] '}'
183///   '{' '}'
184///
185Parser::TPResult Parser::TryParseInitDeclaratorList() {
186  while (1) {
187    // declarator
188    TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
189    if (TPR != TPResult::Ambiguous())
190      return TPR;
191
192    // [GNU] simple-asm-expr[opt] attributes[opt]
193    if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
194      return TPResult::True();
195
196    // initializer[opt]
197    if (Tok.is(tok::l_paren)) {
198      // Parse through the parens.
199      ConsumeParen();
200      if (!SkipUntil(tok::r_paren))
201        return TPResult::Error();
202    } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
203      // MSVC and g++ won't examine the rest of declarators if '=' is
204      // encountered; they just conclude that we have a declaration.
205      // EDG parses the initializer completely, which is the proper behavior
206      // for this case.
207      //
208      // At present, Clang follows MSVC and g++, since the parser does not have
209      // the ability to parse an expression fully without recording the
210      // results of that parse.
211      // Also allow 'in' after on objective-c declaration as in:
212      // for (int (^b)(void) in array). Ideally this should be done in the
213      // context of parsing for-init-statement of a foreach statement only. But,
214      // in any other context 'in' is invalid after a declaration and parser
215      // issues the error regardless of outcome of this decision.
216      // FIXME. Change if above assumption does not hold.
217      return TPResult::True();
218    }
219
220    if (Tok.isNot(tok::comma))
221      break;
222    ConsumeToken(); // the comma.
223  }
224
225  return TPResult::Ambiguous();
226}
227
228/// isCXXConditionDeclaration - Disambiguates between a declaration or an
229/// expression for a condition of a if/switch/while/for statement.
230/// If during the disambiguation process a parsing error is encountered,
231/// the function returns true to let the declaration parsing code handle it.
232///
233///       condition:
234///         expression
235///         type-specifier-seq declarator '=' assignment-expression
236/// [C++11] type-specifier-seq declarator '=' initializer-clause
237/// [C++11] type-specifier-seq declarator braced-init-list
238/// [GNU]   type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
239///             '=' assignment-expression
240///
241bool Parser::isCXXConditionDeclaration() {
242  TPResult TPR = isCXXDeclarationSpecifier();
243  if (TPR != TPResult::Ambiguous())
244    return TPR != TPResult::False(); // Returns true for TPResult::True() or
245                                     // TPResult::Error().
246
247  // FIXME: Add statistics about the number of ambiguous statements encountered
248  // and how they were resolved (number of declarations+number of expressions).
249
250  // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
251  // We need tentative parsing...
252
253  TentativeParsingAction PA(*this);
254
255  // type-specifier-seq
256  if (Tok.is(tok::kw_typeof))
257    TryParseTypeofSpecifier();
258  else {
259    ConsumeToken();
260
261    if (getLang().ObjC1 && Tok.is(tok::less))
262      TryParseProtocolQualifiers();
263  }
264  assert(Tok.is(tok::l_paren) && "Expected '('");
265
266  // declarator
267  TPR = TryParseDeclarator(false/*mayBeAbstract*/);
268
269  // In case of an error, let the declaration parsing code handle it.
270  if (TPR == TPResult::Error())
271    TPR = TPResult::True();
272
273  if (TPR == TPResult::Ambiguous()) {
274    // '='
275    // [GNU] simple-asm-expr[opt] attributes[opt]
276    if (Tok.is(tok::equal)  ||
277        Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
278      TPR = TPResult::True();
279    else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace))
280      TPR = TPResult::True();
281    else
282      TPR = TPResult::False();
283  }
284
285  PA.Revert();
286
287  assert(TPR == TPResult::True() || TPR == TPResult::False());
288  return TPR == TPResult::True();
289}
290
291  /// \brief Determine whether the next set of tokens contains a type-id.
292  ///
293  /// The context parameter states what context we're parsing right
294  /// now, which affects how this routine copes with the token
295  /// following the type-id. If the context is TypeIdInParens, we have
296  /// already parsed the '(' and we will cease lookahead when we hit
297  /// the corresponding ')'. If the context is
298  /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
299  /// before this template argument, and will cease lookahead when we
300  /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
301  /// and false for an expression.  If during the disambiguation
302  /// process a parsing error is encountered, the function returns
303  /// true to let the declaration parsing code handle it.
304  ///
305  /// type-id:
306  ///   type-specifier-seq abstract-declarator[opt]
307  ///
308bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
309
310  isAmbiguous = false;
311
312  // C++ 8.2p2:
313  // The ambiguity arising from the similarity between a function-style cast and
314  // a type-id can occur in different contexts. The ambiguity appears as a
315  // choice between a function-style cast expression and a declaration of a
316  // type. The resolution is that any construct that could possibly be a type-id
317  // in its syntactic context shall be considered a type-id.
318
319  TPResult TPR = isCXXDeclarationSpecifier();
320  if (TPR != TPResult::Ambiguous())
321    return TPR != TPResult::False(); // Returns true for TPResult::True() or
322                                     // TPResult::Error().
323
324  // FIXME: Add statistics about the number of ambiguous statements encountered
325  // and how they were resolved (number of declarations+number of expressions).
326
327  // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
328  // We need tentative parsing...
329
330  TentativeParsingAction PA(*this);
331
332  // type-specifier-seq
333  if (Tok.is(tok::kw_typeof))
334    TryParseTypeofSpecifier();
335  else {
336    ConsumeToken();
337
338    if (getLang().ObjC1 && Tok.is(tok::less))
339      TryParseProtocolQualifiers();
340  }
341
342  assert(Tok.is(tok::l_paren) && "Expected '('");
343
344  // declarator
345  TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
346
347  // In case of an error, let the declaration parsing code handle it.
348  if (TPR == TPResult::Error())
349    TPR = TPResult::True();
350
351  if (TPR == TPResult::Ambiguous()) {
352    // We are supposed to be inside parens, so if after the abstract declarator
353    // we encounter a ')' this is a type-id, otherwise it's an expression.
354    if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
355      TPR = TPResult::True();
356      isAmbiguous = true;
357
358    // We are supposed to be inside a template argument, so if after
359    // the abstract declarator we encounter a '>', '>>' (in C++0x), or
360    // ',', this is a type-id. Otherwise, it's an expression.
361    } else if (Context == TypeIdAsTemplateArgument &&
362               (Tok.is(tok::greater) || Tok.is(tok::comma) ||
363                (getLang().CPlusPlus0x && Tok.is(tok::greatergreater)))) {
364      TPR = TPResult::True();
365      isAmbiguous = true;
366
367    } else
368      TPR = TPResult::False();
369  }
370
371  PA.Revert();
372
373  assert(TPR == TPResult::True() || TPR == TPResult::False());
374  return TPR == TPResult::True();
375}
376
377/// isCXX0XAttributeSpecifier - returns true if this is a C++0x
378/// attribute-specifier. By default, unless in Obj-C++, only a cursory check is
379/// performed that will simply return true if a [[ is seen. Currently C++ has no
380/// syntactical ambiguities from this check, but it may inhibit error recovery.
381/// If CheckClosing is true, a check is made for closing ]] brackets.
382///
383/// If given, After is set to the token after the attribute-specifier so that
384/// appropriate parsing decisions can be made; it is left untouched if false is
385/// returned.
386///
387/// FIXME: If an error is in the closing ]] brackets, the program assumes
388/// the absence of an attribute-specifier, which can cause very yucky errors
389/// to occur.
390///
391/// [C++0x] attribute-specifier:
392///         '[' '[' attribute-list ']' ']'
393///         alignment-specifier
394///
395/// [C++0x] attribute-list:
396///         attribute[opt]
397///         attribute-list ',' attribute[opt]
398///
399/// [C++0x] attribute:
400///         attribute-token attribute-argument-clause[opt]
401///
402/// [C++0x] attribute-token:
403///         identifier
404///         attribute-scoped-token
405///
406/// [C++0x] attribute-scoped-token:
407///         attribute-namespace '::' identifier
408///
409/// [C++0x] attribute-namespace:
410///         identifier
411///
412/// [C++0x] attribute-argument-clause:
413///         '(' balanced-token-seq ')'
414///
415/// [C++0x] balanced-token-seq:
416///         balanced-token
417///         balanced-token-seq balanced-token
418///
419/// [C++0x] balanced-token:
420///         '(' balanced-token-seq ')'
421///         '[' balanced-token-seq ']'
422///         '{' balanced-token-seq '}'
423///         any token but '(', ')', '[', ']', '{', or '}'
424bool Parser::isCXX0XAttributeSpecifier (bool CheckClosing,
425                                        tok::TokenKind *After) {
426  if (Tok.is(tok::kw_alignas))
427    return true;
428
429  if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
430    return false;
431
432  // No tentative parsing if we don't need to look for ]]
433  if (!CheckClosing && !getLang().ObjC1)
434    return true;
435
436  struct TentativeReverter {
437    TentativeParsingAction PA;
438
439    TentativeReverter (Parser& P)
440      : PA(P)
441    {}
442    ~TentativeReverter () {
443      PA.Revert();
444    }
445  } R(*this);
446
447  // Opening brackets were checked for above.
448  ConsumeBracket();
449  ConsumeBracket();
450
451  // SkipUntil will handle balanced tokens, which are guaranteed in attributes.
452  SkipUntil(tok::r_square, false);
453
454  if (Tok.isNot(tok::r_square))
455    return false;
456  ConsumeBracket();
457
458  if (After)
459    *After = Tok.getKind();
460
461  return true;
462}
463
464///         declarator:
465///           direct-declarator
466///           ptr-operator declarator
467///
468///         direct-declarator:
469///           declarator-id
470///           direct-declarator '(' parameter-declaration-clause ')'
471///                 cv-qualifier-seq[opt] exception-specification[opt]
472///           direct-declarator '[' constant-expression[opt] ']'
473///           '(' declarator ')'
474/// [GNU]     '(' attributes declarator ')'
475///
476///         abstract-declarator:
477///           ptr-operator abstract-declarator[opt]
478///           direct-abstract-declarator
479///           ...
480///
481///         direct-abstract-declarator:
482///           direct-abstract-declarator[opt]
483///           '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
484///                 exception-specification[opt]
485///           direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
486///           '(' abstract-declarator ')'
487///
488///         ptr-operator:
489///           '*' cv-qualifier-seq[opt]
490///           '&'
491/// [C++0x]   '&&'                                                        [TODO]
492///           '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
493///
494///         cv-qualifier-seq:
495///           cv-qualifier cv-qualifier-seq[opt]
496///
497///         cv-qualifier:
498///           'const'
499///           'volatile'
500///
501///         declarator-id:
502///           '...'[opt] id-expression
503///
504///         id-expression:
505///           unqualified-id
506///           qualified-id                                                [TODO]
507///
508///         unqualified-id:
509///           identifier
510///           operator-function-id                                        [TODO]
511///           conversion-function-id                                      [TODO]
512///           '~' class-name                                              [TODO]
513///           template-id                                                 [TODO]
514///
515Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
516                                            bool mayHaveIdentifier) {
517  // declarator:
518  //   direct-declarator
519  //   ptr-operator declarator
520
521  while (1) {
522    if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
523      if (TryAnnotateCXXScopeToken(true))
524        return TPResult::Error();
525
526    if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
527        Tok.is(tok::ampamp) ||
528        (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
529      // ptr-operator
530      ConsumeToken();
531      while (Tok.is(tok::kw_const)    ||
532             Tok.is(tok::kw_volatile) ||
533             Tok.is(tok::kw_restrict))
534        ConsumeToken();
535    } else {
536      break;
537    }
538  }
539
540  // direct-declarator:
541  // direct-abstract-declarator:
542  if (Tok.is(tok::ellipsis))
543    ConsumeToken();
544
545  if ((Tok.is(tok::identifier) ||
546       (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) &&
547      mayHaveIdentifier) {
548    // declarator-id
549    if (Tok.is(tok::annot_cxxscope))
550      ConsumeToken();
551    ConsumeToken();
552  } else if (Tok.is(tok::l_paren)) {
553    ConsumeParen();
554    if (mayBeAbstract &&
555        (Tok.is(tok::r_paren) ||       // 'int()' is a function.
556         Tok.is(tok::ellipsis) ||      // 'int(...)' is a function.
557         isDeclarationSpecifier())) {   // 'int(int)' is a function.
558      // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
559      //        exception-specification[opt]
560      TPResult TPR = TryParseFunctionDeclarator();
561      if (TPR != TPResult::Ambiguous())
562        return TPR;
563    } else {
564      // '(' declarator ')'
565      // '(' attributes declarator ')'
566      // '(' abstract-declarator ')'
567      if (Tok.is(tok::kw___attribute) ||
568          Tok.is(tok::kw___declspec) ||
569          Tok.is(tok::kw___cdecl) ||
570          Tok.is(tok::kw___stdcall) ||
571          Tok.is(tok::kw___fastcall) ||
572          Tok.is(tok::kw___thiscall) ||
573          Tok.is(tok::kw___unaligned))
574        return TPResult::True(); // attributes indicate declaration
575      TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
576      if (TPR != TPResult::Ambiguous())
577        return TPR;
578      if (Tok.isNot(tok::r_paren))
579        return TPResult::False();
580      ConsumeParen();
581    }
582  } else if (!mayBeAbstract) {
583    return TPResult::False();
584  }
585
586  while (1) {
587    TPResult TPR(TPResult::Ambiguous());
588
589    // abstract-declarator: ...
590    if (Tok.is(tok::ellipsis))
591      ConsumeToken();
592
593    if (Tok.is(tok::l_paren)) {
594      // Check whether we have a function declarator or a possible ctor-style
595      // initializer that follows the declarator. Note that ctor-style
596      // initializers are not possible in contexts where abstract declarators
597      // are allowed.
598      if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/))
599        break;
600
601      // direct-declarator '(' parameter-declaration-clause ')'
602      //        cv-qualifier-seq[opt] exception-specification[opt]
603      ConsumeParen();
604      TPR = TryParseFunctionDeclarator();
605    } else if (Tok.is(tok::l_square)) {
606      // direct-declarator '[' constant-expression[opt] ']'
607      // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
608      TPR = TryParseBracketDeclarator();
609    } else {
610      break;
611    }
612
613    if (TPR != TPResult::Ambiguous())
614      return TPR;
615  }
616
617  return TPResult::Ambiguous();
618}
619
620Parser::TPResult
621Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
622  switch (Kind) {
623  // Obviously starts an expression.
624  case tok::numeric_constant:
625  case tok::char_constant:
626  case tok::wide_char_constant:
627  case tok::utf16_char_constant:
628  case tok::utf32_char_constant:
629  case tok::string_literal:
630  case tok::wide_string_literal:
631  case tok::utf8_string_literal:
632  case tok::utf16_string_literal:
633  case tok::utf32_string_literal:
634  case tok::l_square:
635  case tok::l_paren:
636  case tok::amp:
637  case tok::ampamp:
638  case tok::star:
639  case tok::plus:
640  case tok::plusplus:
641  case tok::minus:
642  case tok::minusminus:
643  case tok::tilde:
644  case tok::exclaim:
645  case tok::kw_sizeof:
646  case tok::kw___func__:
647  case tok::kw_const_cast:
648  case tok::kw_delete:
649  case tok::kw_dynamic_cast:
650  case tok::kw_false:
651  case tok::kw_new:
652  case tok::kw_operator:
653  case tok::kw_reinterpret_cast:
654  case tok::kw_static_cast:
655  case tok::kw_this:
656  case tok::kw_throw:
657  case tok::kw_true:
658  case tok::kw_typeid:
659  case tok::kw_alignof:
660  case tok::kw_noexcept:
661  case tok::kw_nullptr:
662  case tok::kw___null:
663  case tok::kw___alignof:
664  case tok::kw___builtin_choose_expr:
665  case tok::kw___builtin_offsetof:
666  case tok::kw___builtin_types_compatible_p:
667  case tok::kw___builtin_va_arg:
668  case tok::kw___imag:
669  case tok::kw___real:
670  case tok::kw___FUNCTION__:
671  case tok::kw___PRETTY_FUNCTION__:
672  case tok::kw___has_nothrow_assign:
673  case tok::kw___has_nothrow_copy:
674  case tok::kw___has_nothrow_constructor:
675  case tok::kw___has_trivial_assign:
676  case tok::kw___has_trivial_copy:
677  case tok::kw___has_trivial_constructor:
678  case tok::kw___has_trivial_destructor:
679  case tok::kw___has_virtual_destructor:
680  case tok::kw___is_abstract:
681  case tok::kw___is_base_of:
682  case tok::kw___is_class:
683  case tok::kw___is_convertible_to:
684  case tok::kw___is_empty:
685  case tok::kw___is_enum:
686  case tok::kw___is_final:
687  case tok::kw___is_literal:
688  case tok::kw___is_literal_type:
689  case tok::kw___is_pod:
690  case tok::kw___is_polymorphic:
691  case tok::kw___is_trivial:
692  case tok::kw___is_trivially_assignable:
693  case tok::kw___is_trivially_copyable:
694  case tok::kw___is_union:
695  case tok::kw___uuidof:
696    return TPResult::True();
697
698  // Obviously starts a type-specifier-seq:
699  case tok::kw_char:
700  case tok::kw_const:
701  case tok::kw_double:
702  case tok::kw_enum:
703  case tok::kw_half:
704  case tok::kw_float:
705  case tok::kw_int:
706  case tok::kw_long:
707  case tok::kw___int64:
708  case tok::kw_restrict:
709  case tok::kw_short:
710  case tok::kw_signed:
711  case tok::kw_struct:
712  case tok::kw_union:
713  case tok::kw_unsigned:
714  case tok::kw_void:
715  case tok::kw_volatile:
716  case tok::kw__Bool:
717  case tok::kw__Complex:
718  case tok::kw_class:
719  case tok::kw_typename:
720  case tok::kw_wchar_t:
721  case tok::kw_char16_t:
722  case tok::kw_char32_t:
723  case tok::kw___underlying_type:
724  case tok::kw_thread_local:
725  case tok::kw__Decimal32:
726  case tok::kw__Decimal64:
727  case tok::kw__Decimal128:
728  case tok::kw___thread:
729  case tok::kw_typeof:
730  case tok::kw___cdecl:
731  case tok::kw___stdcall:
732  case tok::kw___fastcall:
733  case tok::kw___thiscall:
734  case tok::kw___unaligned:
735  case tok::kw___vector:
736  case tok::kw___pixel:
737  case tok::kw__Atomic:
738    return TPResult::False();
739
740  default:
741    break;
742  }
743
744  return TPResult::Ambiguous();
745}
746
747/// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
748/// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
749/// be either a decl-specifier or a function-style cast, and TPResult::Error()
750/// if a parsing error was found and reported.
751///
752///         decl-specifier:
753///           storage-class-specifier
754///           type-specifier
755///           function-specifier
756///           'friend'
757///           'typedef'
758/// [C++0x]   'constexpr'
759/// [GNU]     attributes declaration-specifiers[opt]
760///
761///         storage-class-specifier:
762///           'register'
763///           'static'
764///           'extern'
765///           'mutable'
766///           'auto'
767/// [GNU]     '__thread'
768///
769///         function-specifier:
770///           'inline'
771///           'virtual'
772///           'explicit'
773///
774///         typedef-name:
775///           identifier
776///
777///         type-specifier:
778///           simple-type-specifier
779///           class-specifier
780///           enum-specifier
781///           elaborated-type-specifier
782///           typename-specifier
783///           cv-qualifier
784///
785///         simple-type-specifier:
786///           '::'[opt] nested-name-specifier[opt] type-name
787///           '::'[opt] nested-name-specifier 'template'
788///                 simple-template-id                              [TODO]
789///           'char'
790///           'wchar_t'
791///           'bool'
792///           'short'
793///           'int'
794///           'long'
795///           'signed'
796///           'unsigned'
797///           'float'
798///           'double'
799///           'void'
800/// [GNU]     typeof-specifier
801/// [GNU]     '_Complex'
802/// [C++0x]   'auto'                                                [TODO]
803/// [C++0x]   'decltype' ( expression )
804///
805///         type-name:
806///           class-name
807///           enum-name
808///           typedef-name
809///
810///         elaborated-type-specifier:
811///           class-key '::'[opt] nested-name-specifier[opt] identifier
812///           class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
813///               simple-template-id
814///           'enum' '::'[opt] nested-name-specifier[opt] identifier
815///
816///         enum-name:
817///           identifier
818///
819///         enum-specifier:
820///           'enum' identifier[opt] '{' enumerator-list[opt] '}'
821///           'enum' identifier[opt] '{' enumerator-list ',' '}'
822///
823///         class-specifier:
824///           class-head '{' member-specification[opt] '}'
825///
826///         class-head:
827///           class-key identifier[opt] base-clause[opt]
828///           class-key nested-name-specifier identifier base-clause[opt]
829///           class-key nested-name-specifier[opt] simple-template-id
830///               base-clause[opt]
831///
832///         class-key:
833///           'class'
834///           'struct'
835///           'union'
836///
837///         cv-qualifier:
838///           'const'
839///           'volatile'
840/// [GNU]     restrict
841///
842Parser::TPResult
843Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult) {
844  switch (Tok.getKind()) {
845  case tok::identifier:   // foo::bar
846    // Check for need to substitute AltiVec __vector keyword
847    // for "vector" identifier.
848    if (TryAltiVecVectorToken())
849      return TPResult::True();
850    // Fall through.
851  case tok::kw_typename:  // typename T::type
852    // Annotate typenames and C++ scope specifiers.  If we get one, just
853    // recurse to handle whatever we get.
854    if (TryAnnotateTypeOrScopeToken())
855      return TPResult::Error();
856    if (Tok.is(tok::identifier))
857      return TPResult::False();
858    return isCXXDeclarationSpecifier(BracedCastResult);
859
860  case tok::coloncolon: {    // ::foo::bar
861    const Token &Next = NextToken();
862    if (Next.is(tok::kw_new) ||    // ::new
863        Next.is(tok::kw_delete))   // ::delete
864      return TPResult::False();
865  }
866    // Fall through.
867  case tok::kw_decltype:
868    // Annotate typenames and C++ scope specifiers.  If we get one, just
869    // recurse to handle whatever we get.
870    if (TryAnnotateTypeOrScopeToken())
871      return TPResult::Error();
872    return isCXXDeclarationSpecifier(BracedCastResult);
873
874    // decl-specifier:
875    //   storage-class-specifier
876    //   type-specifier
877    //   function-specifier
878    //   'friend'
879    //   'typedef'
880    //   'constexpr'
881  case tok::kw_friend:
882  case tok::kw_typedef:
883  case tok::kw_constexpr:
884    // storage-class-specifier
885  case tok::kw_register:
886  case tok::kw_static:
887  case tok::kw_extern:
888  case tok::kw_mutable:
889  case tok::kw_auto:
890  case tok::kw___thread:
891    // function-specifier
892  case tok::kw_inline:
893  case tok::kw_virtual:
894  case tok::kw_explicit:
895
896    // Modules
897  case tok::kw___module_private__:
898
899    // type-specifier:
900    //   simple-type-specifier
901    //   class-specifier
902    //   enum-specifier
903    //   elaborated-type-specifier
904    //   typename-specifier
905    //   cv-qualifier
906
907    // class-specifier
908    // elaborated-type-specifier
909  case tok::kw_class:
910  case tok::kw_struct:
911  case tok::kw_union:
912    // enum-specifier
913  case tok::kw_enum:
914    // cv-qualifier
915  case tok::kw_const:
916  case tok::kw_volatile:
917
918    // GNU
919  case tok::kw_restrict:
920  case tok::kw__Complex:
921  case tok::kw___attribute:
922    return TPResult::True();
923
924    // Microsoft
925  case tok::kw___declspec:
926  case tok::kw___cdecl:
927  case tok::kw___stdcall:
928  case tok::kw___fastcall:
929  case tok::kw___thiscall:
930  case tok::kw___w64:
931  case tok::kw___ptr64:
932  case tok::kw___ptr32:
933  case tok::kw___forceinline:
934  case tok::kw___unaligned:
935    return TPResult::True();
936
937    // Borland
938  case tok::kw___pascal:
939    return TPResult::True();
940
941    // AltiVec
942  case tok::kw___vector:
943    return TPResult::True();
944
945  case tok::annot_template_id: {
946    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
947    if (TemplateId->Kind != TNK_Type_template)
948      return TPResult::False();
949    CXXScopeSpec SS;
950    AnnotateTemplateIdTokenAsType();
951    assert(Tok.is(tok::annot_typename));
952    goto case_typename;
953  }
954
955  case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
956    // We've already annotated a scope; try to annotate a type.
957    if (TryAnnotateTypeOrScopeToken())
958      return TPResult::Error();
959    if (!Tok.is(tok::annot_typename)) {
960      // If the next token is an identifier or a type qualifier, then this
961      // can't possibly be a valid expression either.
962      if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
963        CXXScopeSpec SS;
964        Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
965                                                     Tok.getAnnotationRange(),
966                                                     SS);
967        if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
968          TentativeParsingAction PA(*this);
969          ConsumeToken();
970          ConsumeToken();
971          bool isIdentifier = Tok.is(tok::identifier);
972          TPResult TPR = TPResult::False();
973          if (!isIdentifier)
974            TPR = isCXXDeclarationSpecifier(BracedCastResult);
975          PA.Revert();
976
977          if (isIdentifier ||
978              TPR == TPResult::True() || TPR == TPResult::Error())
979            return TPResult::Error();
980        }
981      }
982      return TPResult::False();
983    }
984    // If that succeeded, fallthrough into the generic simple-type-id case.
985
986    // The ambiguity resides in a simple-type-specifier/typename-specifier
987    // followed by a '('. The '(' could either be the start of:
988    //
989    //   direct-declarator:
990    //     '(' declarator ')'
991    //
992    //   direct-abstract-declarator:
993    //     '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
994    //              exception-specification[opt]
995    //     '(' abstract-declarator ')'
996    //
997    // or part of a function-style cast expression:
998    //
999    //     simple-type-specifier '(' expression-list[opt] ')'
1000    //
1001
1002    // simple-type-specifier:
1003
1004  case tok::annot_typename:
1005  case_typename:
1006    // In Objective-C, we might have a protocol-qualified type.
1007    if (getLang().ObjC1 && NextToken().is(tok::less)) {
1008      // Tentatively parse the
1009      TentativeParsingAction PA(*this);
1010      ConsumeToken(); // The type token
1011
1012      TPResult TPR = TryParseProtocolQualifiers();
1013      bool isFollowedByParen = Tok.is(tok::l_paren);
1014      bool isFollowedByBrace = Tok.is(tok::l_brace);
1015
1016      PA.Revert();
1017
1018      if (TPR == TPResult::Error())
1019        return TPResult::Error();
1020
1021      if (isFollowedByParen)
1022        return TPResult::Ambiguous();
1023
1024      if (getLang().CPlusPlus0x && isFollowedByBrace)
1025        return BracedCastResult;
1026
1027      return TPResult::True();
1028    }
1029
1030  case tok::kw_char:
1031  case tok::kw_wchar_t:
1032  case tok::kw_char16_t:
1033  case tok::kw_char32_t:
1034  case tok::kw_bool:
1035  case tok::kw_short:
1036  case tok::kw_int:
1037  case tok::kw_long:
1038  case tok::kw___int64:
1039  case tok::kw_signed:
1040  case tok::kw_unsigned:
1041  case tok::kw_half:
1042  case tok::kw_float:
1043  case tok::kw_double:
1044  case tok::kw_void:
1045  case tok::annot_decltype:
1046    if (NextToken().is(tok::l_paren))
1047      return TPResult::Ambiguous();
1048
1049    // This is a function-style cast in all cases we disambiguate other than
1050    // one:
1051    //   struct S {
1052    //     enum E : int { a = 4 }; // enum
1053    //     enum E : int { 4 };     // bit-field
1054    //   };
1055    if (getLang().CPlusPlus0x && NextToken().is(tok::l_brace))
1056      return BracedCastResult;
1057
1058    if (isStartOfObjCClassMessageMissingOpenBracket())
1059      return TPResult::False();
1060
1061    return TPResult::True();
1062
1063  // GNU typeof support.
1064  case tok::kw_typeof: {
1065    if (NextToken().isNot(tok::l_paren))
1066      return TPResult::True();
1067
1068    TentativeParsingAction PA(*this);
1069
1070    TPResult TPR = TryParseTypeofSpecifier();
1071    bool isFollowedByParen = Tok.is(tok::l_paren);
1072    bool isFollowedByBrace = Tok.is(tok::l_brace);
1073
1074    PA.Revert();
1075
1076    if (TPR == TPResult::Error())
1077      return TPResult::Error();
1078
1079    if (isFollowedByParen)
1080      return TPResult::Ambiguous();
1081
1082    if (getLang().CPlusPlus0x && isFollowedByBrace)
1083      return BracedCastResult;
1084
1085    return TPResult::True();
1086  }
1087
1088  // C++0x type traits support
1089  case tok::kw___underlying_type:
1090    return TPResult::True();
1091
1092  // C11 _Atomic
1093  case tok::kw__Atomic:
1094    return TPResult::True();
1095
1096  default:
1097    return TPResult::False();
1098  }
1099}
1100
1101/// [GNU] typeof-specifier:
1102///         'typeof' '(' expressions ')'
1103///         'typeof' '(' type-name ')'
1104///
1105Parser::TPResult Parser::TryParseTypeofSpecifier() {
1106  assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1107  ConsumeToken();
1108
1109  assert(Tok.is(tok::l_paren) && "Expected '('");
1110  // Parse through the parens after 'typeof'.
1111  ConsumeParen();
1112  if (!SkipUntil(tok::r_paren))
1113    return TPResult::Error();
1114
1115  return TPResult::Ambiguous();
1116}
1117
1118/// [ObjC] protocol-qualifiers:
1119////         '<' identifier-list '>'
1120Parser::TPResult Parser::TryParseProtocolQualifiers() {
1121  assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1122  ConsumeToken();
1123  do {
1124    if (Tok.isNot(tok::identifier))
1125      return TPResult::Error();
1126    ConsumeToken();
1127
1128    if (Tok.is(tok::comma)) {
1129      ConsumeToken();
1130      continue;
1131    }
1132
1133    if (Tok.is(tok::greater)) {
1134      ConsumeToken();
1135      return TPResult::Ambiguous();
1136    }
1137  } while (false);
1138
1139  return TPResult::Error();
1140}
1141
1142Parser::TPResult Parser::TryParseDeclarationSpecifier() {
1143  TPResult TPR = isCXXDeclarationSpecifier();
1144  if (TPR != TPResult::Ambiguous())
1145    return TPR;
1146
1147  if (Tok.is(tok::kw_typeof))
1148    TryParseTypeofSpecifier();
1149  else {
1150    ConsumeToken();
1151
1152    if (getLang().ObjC1 && Tok.is(tok::less))
1153      TryParseProtocolQualifiers();
1154  }
1155
1156  assert(Tok.is(tok::l_paren) && "Expected '('!");
1157  return TPResult::Ambiguous();
1158}
1159
1160/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1161/// a constructor-style initializer, when parsing declaration statements.
1162/// Returns true for function declarator and false for constructor-style
1163/// initializer.
1164/// If during the disambiguation process a parsing error is encountered,
1165/// the function returns true to let the declaration parsing code handle it.
1166///
1167/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1168///         exception-specification[opt]
1169///
1170bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) {
1171
1172  // C++ 8.2p1:
1173  // The ambiguity arising from the similarity between a function-style cast and
1174  // a declaration mentioned in 6.8 can also occur in the context of a
1175  // declaration. In that context, the choice is between a function declaration
1176  // with a redundant set of parentheses around a parameter name and an object
1177  // declaration with a function-style cast as the initializer. Just as for the
1178  // ambiguities mentioned in 6.8, the resolution is to consider any construct
1179  // that could possibly be a declaration a declaration.
1180
1181  TentativeParsingAction PA(*this);
1182
1183  ConsumeParen();
1184  TPResult TPR = TryParseParameterDeclarationClause();
1185  if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1186    TPR = TPResult::False();
1187
1188  SourceLocation TPLoc = Tok.getLocation();
1189  PA.Revert();
1190
1191  // In case of an error, let the declaration parsing code handle it.
1192  if (TPR == TPResult::Error())
1193    return true;
1194
1195  if (TPR == TPResult::Ambiguous()) {
1196    // Function declarator has precedence over constructor-style initializer.
1197    // Emit a warning just in case the author intended a variable definition.
1198    if (warnIfAmbiguous)
1199      Diag(Tok, diag::warn_parens_disambiguated_as_function_decl)
1200        << SourceRange(Tok.getLocation(), TPLoc);
1201    return true;
1202  }
1203
1204  return TPR == TPResult::True();
1205}
1206
1207/// parameter-declaration-clause:
1208///   parameter-declaration-list[opt] '...'[opt]
1209///   parameter-declaration-list ',' '...'
1210///
1211/// parameter-declaration-list:
1212///   parameter-declaration
1213///   parameter-declaration-list ',' parameter-declaration
1214///
1215/// parameter-declaration:
1216///   decl-specifier-seq declarator attributes[opt]
1217///   decl-specifier-seq declarator attributes[opt] '=' assignment-expression
1218///   decl-specifier-seq abstract-declarator[opt] attributes[opt]
1219///   decl-specifier-seq abstract-declarator[opt] attributes[opt]
1220///     '=' assignment-expression
1221///
1222Parser::TPResult Parser::TryParseParameterDeclarationClause() {
1223
1224  if (Tok.is(tok::r_paren))
1225    return TPResult::True();
1226
1227  //   parameter-declaration-list[opt] '...'[opt]
1228  //   parameter-declaration-list ',' '...'
1229  //
1230  // parameter-declaration-list:
1231  //   parameter-declaration
1232  //   parameter-declaration-list ',' parameter-declaration
1233  //
1234  while (1) {
1235    // '...'[opt]
1236    if (Tok.is(tok::ellipsis)) {
1237      ConsumeToken();
1238      return TPResult::True(); // '...' is a sign of a function declarator.
1239    }
1240
1241    ParsedAttributes attrs(AttrFactory);
1242    MaybeParseMicrosoftAttributes(attrs);
1243
1244    // decl-specifier-seq
1245    // A parameter-declaration's initializer must be preceded by an '=', so
1246    // decl-specifier-seq '{' is not a parameter in C++11.
1247    TPResult TPR = TryParseDeclarationSpecifier();
1248    if (TPR != TPResult::Ambiguous())
1249      return TPR;
1250
1251    // declarator
1252    // abstract-declarator[opt]
1253    TPR = TryParseDeclarator(true/*mayBeAbstract*/);
1254    if (TPR != TPResult::Ambiguous())
1255      return TPR;
1256
1257    // [GNU] attributes[opt]
1258    if (Tok.is(tok::kw___attribute))
1259      return TPResult::True();
1260
1261    if (Tok.is(tok::equal)) {
1262      // '=' assignment-expression
1263      // Parse through assignment-expression.
1264      tok::TokenKind StopToks[2] ={ tok::comma, tok::r_paren };
1265      if (!SkipUntil(StopToks, 2, true/*StopAtSemi*/, true/*DontConsume*/))
1266        return TPResult::Error();
1267    }
1268
1269    if (Tok.is(tok::ellipsis)) {
1270      ConsumeToken();
1271      return TPResult::True(); // '...' is a sign of a function declarator.
1272    }
1273
1274    if (Tok.isNot(tok::comma))
1275      break;
1276    ConsumeToken(); // the comma.
1277  }
1278
1279  return TPResult::Ambiguous();
1280}
1281
1282/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1283/// parsing as a function declarator.
1284/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1285/// return TPResult::Ambiguous(), otherwise it will return either False() or
1286/// Error().
1287///
1288/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1289///         exception-specification[opt]
1290///
1291/// exception-specification:
1292///   'throw' '(' type-id-list[opt] ')'
1293///
1294Parser::TPResult Parser::TryParseFunctionDeclarator() {
1295
1296  // The '(' is already parsed.
1297
1298  TPResult TPR = TryParseParameterDeclarationClause();
1299  if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1300    TPR = TPResult::False();
1301
1302  if (TPR == TPResult::False() || TPR == TPResult::Error())
1303    return TPR;
1304
1305  // Parse through the parens.
1306  if (!SkipUntil(tok::r_paren))
1307    return TPResult::Error();
1308
1309  // cv-qualifier-seq
1310  while (Tok.is(tok::kw_const)    ||
1311         Tok.is(tok::kw_volatile) ||
1312         Tok.is(tok::kw_restrict)   )
1313    ConsumeToken();
1314
1315  // ref-qualifier[opt]
1316  if (Tok.is(tok::amp) || Tok.is(tok::ampamp))
1317    ConsumeToken();
1318
1319  // exception-specification
1320  if (Tok.is(tok::kw_throw)) {
1321    ConsumeToken();
1322    if (Tok.isNot(tok::l_paren))
1323      return TPResult::Error();
1324
1325    // Parse through the parens after 'throw'.
1326    ConsumeParen();
1327    if (!SkipUntil(tok::r_paren))
1328      return TPResult::Error();
1329  }
1330  if (Tok.is(tok::kw_noexcept)) {
1331    ConsumeToken();
1332    // Possibly an expression as well.
1333    if (Tok.is(tok::l_paren)) {
1334      // Find the matching rparen.
1335      ConsumeParen();
1336      if (!SkipUntil(tok::r_paren))
1337        return TPResult::Error();
1338    }
1339  }
1340
1341  return TPResult::Ambiguous();
1342}
1343
1344/// '[' constant-expression[opt] ']'
1345///
1346Parser::TPResult Parser::TryParseBracketDeclarator() {
1347  ConsumeBracket();
1348  if (!SkipUntil(tok::r_square))
1349    return TPResult::Error();
1350
1351  return TPResult::Ambiguous();
1352}
1353