ParseTentative.cpp revision 3eb1c546857087b5e1377d172f37868ade960664
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/Basic/Diagnostic.h"
17using 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                          [TODO]
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/// [C++0x] static_assert-declaration:                           [TODO]
50/// [C++0x]   static_assert '(' constant-expression ',' string-literal ')' ';'
51///
52bool Parser::isCXXDeclarationStatement() {
53  switch (Tok.getKind()) {
54    // asm-definition
55  case tok::kw_asm:
56    // namespace-alias-definition
57  case tok::kw_namespace:
58    // using-declaration
59    // using-directive
60  case tok::kw_using:
61    return true;
62  default:
63    // simple-declaration
64    return isCXXSimpleDeclaration();
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///
77bool Parser::isCXXSimpleDeclaration() {
78  // C++ 6.8p1:
79  // There is an ambiguity in the grammar involving expression-statements and
80  // declarations: An expression-statement with a function-style explicit type
81  // conversion (5.2.3) as its leftmost subexpression can be indistinguishable
82  // from a declaration where the first declarator starts with a '('. In those
83  // cases the statement is a declaration. [Note: To disambiguate, the whole
84  // statement might have to be examined to determine if it is an
85  // expression-statement or a declaration].
86
87  // C++ 6.8p3:
88  // The disambiguation is purely syntactic; that is, the meaning of the names
89  // occurring in such a statement, beyond whether they are type-names or not,
90  // is not generally used in or changed by the disambiguation. Class
91  // templates are instantiated as necessary to determine if a qualified name
92  // is a type-name. Disambiguation precedes parsing, and a statement
93  // disambiguated as a declaration may be an ill-formed declaration.
94
95  // We don't have to parse all of the decl-specifier-seq part. There's only
96  // an ambiguity if the first decl-specifier is
97  // simple-type-specifier/typename-specifier followed by a '(', which may
98  // indicate a function-style cast expression.
99  // isCXXDeclarationSpecifier will return TPResult::Ambiguous() only in such
100  // a case.
101
102  TPResult TPR = isCXXDeclarationSpecifier();
103  if (TPR != TPResult::Ambiguous())
104    return TPR != TPResult::False(); // Returns true for TPResult::True() or
105                                     // TPResult::Error().
106
107  // FIXME: Add statistics about the number of ambiguous statements encountered
108  // and how they were resolved (number of declarations+number of expressions).
109
110  // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
111  // We need tentative parsing...
112
113  TentativeParsingAction PA(*this);
114
115  TPR = TryParseSimpleDeclaration();
116  SourceLocation TentativeParseLoc = Tok.getLocation();
117
118  PA.Revert();
119
120  // In case of an error, let the declaration parsing code handle it.
121  if (TPR == TPResult::Error())
122    return true;
123
124  // Declarations take precedence over expressions.
125  if (TPR == TPResult::Ambiguous())
126    TPR = TPResult::True();
127
128  assert(TPR == TPResult::True() || TPR == TPResult::False());
129  return TPR == TPResult::True();
130}
131
132/// simple-declaration:
133///   decl-specifier-seq init-declarator-list[opt] ';'
134///
135Parser::TPResult Parser::TryParseSimpleDeclaration() {
136  // We know that we have a simple-type-specifier/typename-specifier followed
137  // by a '('.
138  assert(isCXXDeclarationSpecifier() == TPResult::Ambiguous());
139
140  if (Tok.is(tok::kw_typeof))
141    TryParseTypeofSpecifier();
142  else
143    ConsumeToken();
144
145  assert(Tok.is(tok::l_paren) && "Expected '('");
146
147  TPResult TPR = TryParseInitDeclaratorList();
148  if (TPR != TPResult::Ambiguous())
149    return TPR;
150
151  if (Tok.isNot(tok::semi))
152    return TPResult::False();
153
154  return TPResult::Ambiguous();
155}
156
157///       init-declarator-list:
158///         init-declarator
159///         init-declarator-list ',' init-declarator
160///
161///       init-declarator:
162///         declarator initializer[opt]
163/// [GNU]   declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
164///
165/// initializer:
166///   '=' initializer-clause
167///   '(' expression-list ')'
168///
169/// initializer-clause:
170///   assignment-expression
171///   '{' initializer-list ','[opt] '}'
172///   '{' '}'
173///
174Parser::TPResult Parser::TryParseInitDeclaratorList() {
175  // GCC only examines the first declarator for disambiguation:
176  // i.e:
177  // int(x), ++x; // GCC regards it as ill-formed declaration.
178  //
179  // Comeau and MSVC will regard the above statement as correct expression.
180  // Clang examines all of the declarators and also regards the above statement
181  // as correct expression.
182
183  while (1) {
184    // declarator
185    TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
186    if (TPR != TPResult::Ambiguous())
187      return TPR;
188
189    // [GNU] simple-asm-expr[opt] attributes[opt]
190    if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
191      return TPResult::True();
192
193    // initializer[opt]
194    if (Tok.is(tok::l_paren)) {
195      // Parse through the parens.
196      ConsumeParen();
197      if (!SkipUntil(tok::r_paren))
198        return TPResult::Error();
199    } else if (Tok.is(tok::equal)) {
200      // MSVC won't examine the rest of declarators if '=' is encountered, it
201      // will conclude that it is a declaration.
202      // Comeau and Clang will examine the rest of declarators.
203      // Note that "int(x) = {0}, ++x;" will be interpreted as ill-formed
204      // expression.
205      //
206      // Parse through the initializer-clause.
207      SkipUntil(tok::comma, true/*StopAtSemi*/, true/*DontConsume*/);
208    }
209
210    if (Tok.isNot(tok::comma))
211      break;
212    ConsumeToken(); // the comma.
213  }
214
215  return TPResult::Ambiguous();
216}
217
218/// isCXXConditionDeclaration - Disambiguates between a declaration or an
219/// expression for a condition of a if/switch/while/for statement.
220/// If during the disambiguation process a parsing error is encountered,
221/// the function returns true to let the declaration parsing code handle it.
222///
223///       condition:
224///         expression
225///         type-specifier-seq declarator '=' assignment-expression
226/// [GNU]   type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
227///             '=' assignment-expression
228///
229bool Parser::isCXXConditionDeclaration() {
230  TPResult TPR = isCXXDeclarationSpecifier();
231  if (TPR != TPResult::Ambiguous())
232    return TPR != TPResult::False(); // Returns true for TPResult::True() or
233                                     // TPResult::Error().
234
235  // FIXME: Add statistics about the number of ambiguous statements encountered
236  // and how they were resolved (number of declarations+number of expressions).
237
238  // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
239  // We need tentative parsing...
240
241  TentativeParsingAction PA(*this);
242
243  // type-specifier-seq
244  if (Tok.is(tok::kw_typeof))
245    TryParseTypeofSpecifier();
246  else
247    ConsumeToken();
248  assert(Tok.is(tok::l_paren) && "Expected '('");
249
250  // declarator
251  TPR = TryParseDeclarator(false/*mayBeAbstract*/);
252
253  // In case of an error, let the declaration parsing code handle it.
254  if (TPR == TPResult::Error())
255    TPR = TPResult::True();
256
257  if (TPR == TPResult::Ambiguous()) {
258    // '='
259    // [GNU] simple-asm-expr[opt] attributes[opt]
260    if (Tok.is(tok::equal)  ||
261        Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
262      TPR = TPResult::True();
263    else
264      TPR = TPResult::False();
265  }
266
267  PA.Revert();
268
269  assert(TPR == TPResult::True() || TPR == TPResult::False());
270  return TPR == TPResult::True();
271}
272
273/// isCXXTypeIdInParens - Assumes that a '(' was parsed and now we want to
274/// know whether the parens contain an expression or a type-id.
275/// Returns true for a type-id and false for an expression.
276/// If during the disambiguation process a parsing error is encountered,
277/// the function returns true to let the declaration parsing code handle it.
278///
279/// type-id:
280///   type-specifier-seq abstract-declarator[opt]
281///
282bool Parser::isCXXTypeIdInParens() {
283
284  // C++ 8.2p2:
285  // The ambiguity arising from the similarity between a function-style cast and
286  // a type-id can occur in different contexts. The ambiguity appears as a
287  // choice between a function-style cast expression and a declaration of a
288  // type. The resolution is that any construct that could possibly be a type-id
289  // in its syntactic context shall be considered a type-id.
290
291  TPResult TPR = isCXXDeclarationSpecifier();
292  if (TPR != TPResult::Ambiguous())
293    return TPR != TPResult::False(); // Returns true for TPResult::True() or
294                                     // TPResult::Error().
295
296  // FIXME: Add statistics about the number of ambiguous statements encountered
297  // and how they were resolved (number of declarations+number of expressions).
298
299  // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
300  // We need tentative parsing...
301
302  TentativeParsingAction PA(*this);
303
304  // type-specifier-seq
305  if (Tok.is(tok::kw_typeof))
306    TryParseTypeofSpecifier();
307  else
308    ConsumeToken();
309  assert(Tok.is(tok::l_paren) && "Expected '('");
310
311  // declarator
312  TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
313
314  // In case of an error, let the declaration parsing code handle it.
315  if (TPR == TPResult::Error())
316    TPR = TPResult::True();
317
318  if (TPR == TPResult::Ambiguous()) {
319    // We are supposed to be inside parens, so if after the abstract declarator
320    // we encounter a ')' this is a type-id, otherwise it's an expression.
321    if (Tok.is(tok::r_paren))
322      TPR = TPResult::True();
323    else
324      TPR = TPResult::False();
325  }
326
327  PA.Revert();
328
329  assert(TPR == TPResult::True() || TPR == TPResult::False());
330  return TPR == TPResult::True();
331}
332
333///         declarator:
334///           direct-declarator
335///           ptr-operator declarator
336///
337///         direct-declarator:
338///           declarator-id
339///           direct-declarator '(' parameter-declaration-clause ')'
340///                 cv-qualifier-seq[opt] exception-specification[opt]
341///           direct-declarator '[' constant-expression[opt] ']'
342///           '(' declarator ')'
343/// [GNU]     '(' attributes declarator ')'
344///
345///         abstract-declarator:
346///           ptr-operator abstract-declarator[opt]
347///           direct-abstract-declarator
348///
349///         direct-abstract-declarator:
350///           direct-abstract-declarator[opt]
351///           '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
352///                 exception-specification[opt]
353///           direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
354///           '(' abstract-declarator ')'
355///
356///         ptr-operator:
357///           '*' cv-qualifier-seq[opt]
358///           '&'
359/// [C++0x]   '&&'                                                        [TODO]
360///           '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]   [TODO]
361///
362///         cv-qualifier-seq:
363///           cv-qualifier cv-qualifier-seq[opt]
364///
365///         cv-qualifier:
366///           'const'
367///           'volatile'
368///
369///         declarator-id:
370///           id-expression
371///
372///         id-expression:
373///           unqualified-id
374///           qualified-id                                                [TODO]
375///
376///         unqualified-id:
377///           identifier
378///           operator-function-id                                        [TODO]
379///           conversion-function-id                                      [TODO]
380///           '~' class-name                                              [TODO]
381///           template-id                                                 [TODO]
382///
383Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
384                                            bool mayHaveIdentifier) {
385  // declarator:
386  //   direct-declarator
387  //   ptr-operator declarator
388
389  while (1) {
390    if (Tok.is(tok::star) || Tok.is(tok::amp) ||
391        (Tok.is(tok::caret) && getLang().Blocks)) {
392      // ptr-operator
393      ConsumeToken();
394      while (Tok.is(tok::kw_const)    ||
395             Tok.is(tok::kw_volatile) ||
396             Tok.is(tok::kw_restrict)   )
397        ConsumeToken();
398    } else {
399      break;
400    }
401  }
402
403  // direct-declarator:
404  // direct-abstract-declarator:
405
406  if (Tok.is(tok::identifier) && mayHaveIdentifier) {
407    // declarator-id
408    ConsumeToken();
409  } else if (Tok.is(tok::l_paren)) {
410    ConsumeParen();
411    if (mayBeAbstract &&
412        (Tok.is(tok::r_paren) ||       // 'int()' is a function.
413         Tok.is(tok::ellipsis) ||      // 'int(...)' is a function.
414         isDeclarationSpecifier())) {   // 'int(int)' is a function.
415      // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
416      //        exception-specification[opt]
417      TPResult TPR = TryParseFunctionDeclarator();
418      if (TPR != TPResult::Ambiguous())
419        return TPR;
420    } else {
421      // '(' declarator ')'
422      // '(' attributes declarator ')'
423      // '(' abstract-declarator ')'
424      if (Tok.is(tok::kw___attribute))
425        return TPResult::True(); // attributes indicate declaration
426      TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
427      if (TPR != TPResult::Ambiguous())
428        return TPR;
429      if (Tok.isNot(tok::r_paren))
430        return TPResult::False();
431      ConsumeParen();
432    }
433  } else if (!mayBeAbstract) {
434    return TPResult::False();
435  }
436
437  while (1) {
438    TPResult TPR(TPResult::Ambiguous());
439
440    if (Tok.is(tok::l_paren)) {
441      // Check whether we have a function declarator or a possible ctor-style
442      // initializer that follows the declarator. Note that ctor-style
443      // initializers are not possible in contexts where abstract declarators
444      // are allowed.
445      if (!mayBeAbstract && !isCXXFunctionDeclarator(false/*warnIfAmbiguous*/))
446        break;
447
448      // direct-declarator '(' parameter-declaration-clause ')'
449      //        cv-qualifier-seq[opt] exception-specification[opt]
450      ConsumeParen();
451      TPR = TryParseFunctionDeclarator();
452    } else if (Tok.is(tok::l_square)) {
453      // direct-declarator '[' constant-expression[opt] ']'
454      // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
455      TPR = TryParseBracketDeclarator();
456    } else {
457      break;
458    }
459
460    if (TPR != TPResult::Ambiguous())
461      return TPR;
462  }
463
464  return TPResult::Ambiguous();
465}
466
467/// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
468/// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
469/// be either a decl-specifier or a function-style cast, and TPResult::Error()
470/// if a parsing error was found and reported.
471///
472///         decl-specifier:
473///           storage-class-specifier
474///           type-specifier
475///           function-specifier
476///           'friend'
477///           'typedef'
478/// [GNU]     attributes declaration-specifiers[opt]
479///
480///         storage-class-specifier:
481///           'register'
482///           'static'
483///           'extern'
484///           'mutable'
485///           'auto'
486/// [GNU]     '__thread'
487///
488///         function-specifier:
489///           'inline'
490///           'virtual'
491///           'explicit'
492///
493///         typedef-name:
494///           identifier
495///
496///         type-specifier:
497///           simple-type-specifier
498///           class-specifier
499///           enum-specifier
500///           elaborated-type-specifier
501///           typename-specifier                                    [TODO]
502///           cv-qualifier
503///
504///         simple-type-specifier:
505///           '::'[opt] nested-name-specifier[opt] type-name        [TODO]
506///           '::'[opt] nested-name-specifier 'template'
507///                 simple-template-id                              [TODO]
508///           'char'
509///           'wchar_t'
510///           'bool'
511///           'short'
512///           'int'
513///           'long'
514///           'signed'
515///           'unsigned'
516///           'float'
517///           'double'
518///           'void'
519/// [GNU]     typeof-specifier
520/// [GNU]     '_Complex'
521/// [C++0x]   'auto'                                                [TODO]
522///
523///         type-name:
524///           class-name
525///           enum-name
526///           typedef-name
527///
528///         elaborated-type-specifier:
529///           class-key '::'[opt] nested-name-specifier[opt] identifier
530///           class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
531///               simple-template-id
532///           'enum' '::'[opt] nested-name-specifier[opt] identifier
533///
534///         enum-name:
535///           identifier
536///
537///         enum-specifier:
538///           'enum' identifier[opt] '{' enumerator-list[opt] '}'
539///           'enum' identifier[opt] '{' enumerator-list ',' '}'
540///
541///         class-specifier:
542///           class-head '{' member-specification[opt] '}'
543///
544///         class-head:
545///           class-key identifier[opt] base-clause[opt]
546///           class-key nested-name-specifier identifier base-clause[opt]
547///           class-key nested-name-specifier[opt] simple-template-id
548///               base-clause[opt]
549///
550///         class-key:
551///           'class'
552///           'struct'
553///           'union'
554///
555///         cv-qualifier:
556///           'const'
557///           'volatile'
558/// [GNU]     restrict
559///
560Parser::TPResult Parser::isCXXDeclarationSpecifier() {
561  // Annotate typenames and C++ scope specifiers.
562  TryAnnotateTypeOrScopeToken();
563
564  switch (Tok.getKind()) {
565    // decl-specifier:
566    //   storage-class-specifier
567    //   type-specifier
568    //   function-specifier
569    //   'friend'
570    //   'typedef'
571
572  case tok::kw_friend:
573  case tok::kw_typedef:
574    // storage-class-specifier
575  case tok::kw_register:
576  case tok::kw_static:
577  case tok::kw_extern:
578  case tok::kw_mutable:
579  case tok::kw_auto:
580  case tok::kw___thread:
581    // function-specifier
582  case tok::kw_inline:
583  case tok::kw_virtual:
584  case tok::kw_explicit:
585
586    // type-specifier:
587    //   simple-type-specifier
588    //   class-specifier
589    //   enum-specifier
590    //   elaborated-type-specifier
591    //   typename-specifier
592    //   cv-qualifier
593
594    // class-specifier
595    // elaborated-type-specifier
596  case tok::kw_class:
597  case tok::kw_struct:
598  case tok::kw_union:
599    // enum-specifier
600  case tok::kw_enum:
601    // cv-qualifier
602  case tok::kw_const:
603  case tok::kw_volatile:
604
605    // GNU
606  case tok::kw_restrict:
607  case tok::kw__Complex:
608  case tok::kw___attribute:
609    return TPResult::True();
610
611    // The ambiguity resides in a simple-type-specifier/typename-specifier
612    // followed by a '('. The '(' could either be the start of:
613    //
614    //   direct-declarator:
615    //     '(' declarator ')'
616    //
617    //   direct-abstract-declarator:
618    //     '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
619    //              exception-specification[opt]
620    //     '(' abstract-declarator ')'
621    //
622    // or part of a function-style cast expression:
623    //
624    //     simple-type-specifier '(' expression-list[opt] ')'
625    //
626
627    // simple-type-specifier:
628
629  case tok::kw_char:
630  case tok::kw_wchar_t:
631  case tok::kw_bool:
632  case tok::kw_short:
633  case tok::kw_int:
634  case tok::kw_long:
635  case tok::kw_signed:
636  case tok::kw_unsigned:
637  case tok::kw_float:
638  case tok::kw_double:
639  case tok::kw_void:
640  case tok::annot_qualtypename:
641    if (NextToken().is(tok::l_paren))
642      return TPResult::Ambiguous();
643
644    return TPResult::True();
645
646    // GNU typeof support.
647  case tok::kw_typeof: {
648    if (NextToken().isNot(tok::l_paren))
649      return TPResult::True();
650
651    TentativeParsingAction PA(*this);
652
653    TPResult TPR = TryParseTypeofSpecifier();
654    bool isFollowedByParen = Tok.is(tok::l_paren);
655
656    PA.Revert();
657
658    if (TPR == TPResult::Error())
659      return TPResult::Error();
660
661    if (isFollowedByParen)
662      return TPResult::Ambiguous();
663
664    return TPResult::True();
665  }
666
667  default:
668    return TPResult::False();
669  }
670}
671
672/// [GNU] typeof-specifier:
673///         'typeof' '(' expressions ')'
674///         'typeof' '(' type-name ')'
675///
676Parser::TPResult Parser::TryParseTypeofSpecifier() {
677  assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
678  ConsumeToken();
679
680  assert(Tok.is(tok::l_paren) && "Expected '('");
681  // Parse through the parens after 'typeof'.
682  ConsumeParen();
683  if (!SkipUntil(tok::r_paren))
684    return TPResult::Error();
685
686  return TPResult::Ambiguous();
687}
688
689Parser::TPResult Parser::TryParseDeclarationSpecifier() {
690  TPResult TPR = isCXXDeclarationSpecifier();
691  if (TPR != TPResult::Ambiguous())
692    return TPR;
693
694  if (Tok.is(tok::kw_typeof))
695    TryParseTypeofSpecifier();
696  else
697    ConsumeToken();
698
699  assert(Tok.is(tok::l_paren) && "Expected '('!");
700  return TPResult::Ambiguous();
701}
702
703/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
704/// a constructor-style initializer, when parsing declaration statements.
705/// Returns true for function declarator and false for constructor-style
706/// initializer.
707/// If during the disambiguation process a parsing error is encountered,
708/// the function returns true to let the declaration parsing code handle it.
709///
710/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
711///         exception-specification[opt]
712///
713bool Parser::isCXXFunctionDeclarator(bool warnIfAmbiguous) {
714
715  // C++ 8.2p1:
716  // The ambiguity arising from the similarity between a function-style cast and
717  // a declaration mentioned in 6.8 can also occur in the context of a
718  // declaration. In that context, the choice is between a function declaration
719  // with a redundant set of parentheses around a parameter name and an object
720  // declaration with a function-style cast as the initializer. Just as for the
721  // ambiguities mentioned in 6.8, the resolution is to consider any construct
722  // that could possibly be a declaration a declaration.
723
724  TentativeParsingAction PA(*this);
725
726  ConsumeParen();
727  TPResult TPR = TryParseParameterDeclarationClause();
728  if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
729    TPR = TPResult::False();
730
731  SourceLocation TPLoc = Tok.getLocation();
732  PA.Revert();
733
734  // In case of an error, let the declaration parsing code handle it.
735  if (TPR == TPResult::Error())
736    return true;
737
738  if (TPR == TPResult::Ambiguous()) {
739    // Function declarator has precedence over constructor-style initializer.
740    // Emit a warning just in case the author intended a variable definition.
741    if (warnIfAmbiguous)
742      Diag(Tok, diag::warn_parens_disambiguated_as_function_decl)
743        << SourceRange(Tok.getLocation(), TPLoc);
744    return true;
745  }
746
747  return TPR == TPResult::True();
748}
749
750/// parameter-declaration-clause:
751///   parameter-declaration-list[opt] '...'[opt]
752///   parameter-declaration-list ',' '...'
753///
754/// parameter-declaration-list:
755///   parameter-declaration
756///   parameter-declaration-list ',' parameter-declaration
757///
758/// parameter-declaration:
759///   decl-specifier-seq declarator
760///   decl-specifier-seq declarator '=' assignment-expression
761///   decl-specifier-seq abstract-declarator[opt]
762///   decl-specifier-seq abstract-declarator[opt] '=' assignment-expression
763///
764Parser::TPResult Parser::TryParseParameterDeclarationClause() {
765
766  if (Tok.is(tok::r_paren))
767    return TPResult::True();
768
769  //   parameter-declaration-list[opt] '...'[opt]
770  //   parameter-declaration-list ',' '...'
771  //
772  // parameter-declaration-list:
773  //   parameter-declaration
774  //   parameter-declaration-list ',' parameter-declaration
775  //
776  while (1) {
777    // '...'[opt]
778    if (Tok.is(tok::ellipsis)) {
779      ConsumeToken();
780      return TPResult::True(); // '...' is a sign of a function declarator.
781    }
782
783    // decl-specifier-seq
784    TPResult TPR = TryParseDeclarationSpecifier();
785    if (TPR != TPResult::Ambiguous())
786      return TPR;
787
788    // declarator
789    // abstract-declarator[opt]
790    TPR = TryParseDeclarator(true/*mayBeAbstract*/);
791    if (TPR != TPResult::Ambiguous())
792      return TPR;
793
794    if (Tok.is(tok::equal)) {
795      // '=' assignment-expression
796      // Parse through assignment-expression.
797      tok::TokenKind StopToks[3] ={ tok::comma, tok::ellipsis, tok::r_paren };
798      if (!SkipUntil(StopToks, 3, true/*StopAtSemi*/, true/*DontConsume*/))
799        return TPResult::Error();
800    }
801
802    if (Tok.is(tok::ellipsis)) {
803      ConsumeToken();
804      return TPResult::True(); // '...' is a sign of a function declarator.
805    }
806
807    if (Tok.isNot(tok::comma))
808      break;
809    ConsumeToken(); // the comma.
810  }
811
812  return TPResult::Ambiguous();
813}
814
815/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
816/// parsing as a function declarator.
817/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
818/// return TPResult::Ambiguous(), otherwise it will return either False() or
819/// Error().
820///
821/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
822///         exception-specification[opt]
823///
824/// exception-specification:
825///   'throw' '(' type-id-list[opt] ')'
826///
827Parser::TPResult Parser::TryParseFunctionDeclarator() {
828
829  // The '(' is already parsed.
830
831  TPResult TPR = TryParseParameterDeclarationClause();
832  if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
833    TPR = TPResult::False();
834
835  if (TPR == TPResult::False() || TPR == TPResult::Error())
836    return TPR;
837
838  // Parse through the parens.
839  if (!SkipUntil(tok::r_paren))
840    return TPResult::Error();
841
842  // cv-qualifier-seq
843  while (Tok.is(tok::kw_const)    ||
844         Tok.is(tok::kw_volatile) ||
845         Tok.is(tok::kw_restrict)   )
846    ConsumeToken();
847
848  // exception-specification
849  if (Tok.is(tok::kw_throw)) {
850    ConsumeToken();
851    if (Tok.isNot(tok::l_paren))
852      return TPResult::Error();
853
854    // Parse through the parens after 'throw'.
855    ConsumeParen();
856    if (!SkipUntil(tok::r_paren))
857      return TPResult::Error();
858  }
859
860  return TPResult::Ambiguous();
861}
862
863/// '[' constant-expression[opt] ']'
864///
865Parser::TPResult Parser::TryParseBracketDeclarator() {
866  ConsumeBracket();
867  if (!SkipUntil(tok::r_square))
868    return TPResult::Error();
869
870  return TPResult::Ambiguous();
871}
872