blob: b3cf983bbc332ef7d1c378328fcc6fdd2b15b0f2 [file] [log] [blame]
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001//===--- 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 bool InvalidAsDeclaration = false;
108 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False(),
109 &InvalidAsDeclaration);
110 if (TPR != TPResult::Ambiguous())
111 return TPR != TPResult::False(); // Returns true for TPResult::True() or
112 // TPResult::Error().
113
114 // FIXME: TryParseSimpleDeclaration doesn't look past the first initializer,
115 // and so gets some cases wrong. We can't carry on if we've already seen
116 // something which makes this statement invalid as a declaration in this case,
117 // since it can cause us to misparse valid code. Revisit this once
118 // TryParseInitDeclaratorList is fixed.
119 if (InvalidAsDeclaration)
120 return false;
121
122 // FIXME: Add statistics about the number of ambiguous statements encountered
123 // and how they were resolved (number of declarations+number of expressions).
124
125 // Ok, we have a simple-type-specifier/typename-specifier followed by a '(',
126 // or an identifier which doesn't resolve as anything. We need tentative
127 // parsing...
128
129 TentativeParsingAction PA(*this);
130 TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
131 PA.Revert();
132
133 // In case of an error, let the declaration parsing code handle it.
134 if (TPR == TPResult::Error())
135 return true;
136
137 // Declarations take precedence over expressions.
138 if (TPR == TPResult::Ambiguous())
139 TPR = TPResult::True();
140
141 assert(TPR == TPResult::True() || TPR == TPResult::False());
142 return TPR == TPResult::True();
143}
144
145/// simple-declaration:
146/// decl-specifier-seq init-declarator-list[opt] ';'
147///
148/// (if AllowForRangeDecl specified)
149/// for ( for-range-declaration : for-range-initializer ) statement
150/// for-range-declaration:
151/// attribute-specifier-seqopt type-specifier-seq declarator
152///
153Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
154 if (Tok.is(tok::kw_typeof))
155 TryParseTypeofSpecifier();
156 else {
157 if (Tok.is(tok::annot_cxxscope))
158 ConsumeToken();
159 ConsumeToken();
160
161 if (getLangOpts().ObjC1 && Tok.is(tok::less))
162 TryParseProtocolQualifiers();
163 }
164
165 // Two decl-specifiers in a row conclusively disambiguate this as being a
166 // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the
167 // overwhelmingly common case that the next token is a '('.
168 if (Tok.isNot(tok::l_paren)) {
169 TPResult TPR = isCXXDeclarationSpecifier();
170 if (TPR == TPResult::Ambiguous())
171 return TPResult::True();
172 if (TPR == TPResult::True() || TPR == TPResult::Error())
173 return TPR;
174 assert(TPR == TPResult::False());
175 }
176
177 TPResult TPR = TryParseInitDeclaratorList();
178 if (TPR != TPResult::Ambiguous())
179 return TPR;
180
181 if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon)))
182 return TPResult::False();
183
184 return TPResult::Ambiguous();
185}
186
187/// init-declarator-list:
188/// init-declarator
189/// init-declarator-list ',' init-declarator
190///
191/// init-declarator:
192/// declarator initializer[opt]
193/// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
194///
195/// initializer:
196/// '=' initializer-clause
197/// '(' expression-list ')'
198///
199/// initializer-clause:
200/// assignment-expression
201/// '{' initializer-list ','[opt] '}'
202/// '{' '}'
203///
204Parser::TPResult Parser::TryParseInitDeclaratorList() {
205 while (1) {
206 // declarator
207 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
208 if (TPR != TPResult::Ambiguous())
209 return TPR;
210
211 // [GNU] simple-asm-expr[opt] attributes[opt]
212 if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
213 return TPResult::True();
214
215 // initializer[opt]
216 if (Tok.is(tok::l_paren)) {
217 // Parse through the parens.
218 ConsumeParen();
219 if (!SkipUntil(tok::r_paren))
220 return TPResult::Error();
221 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) {
222 // MSVC and g++ won't examine the rest of declarators if '=' is
223 // encountered; they just conclude that we have a declaration.
224 // EDG parses the initializer completely, which is the proper behavior
225 // for this case.
226 //
227 // At present, Clang follows MSVC and g++, since the parser does not have
228 // the ability to parse an expression fully without recording the
229 // results of that parse.
230 // Also allow 'in' after on objective-c declaration as in:
231 // for (int (^b)(void) in array). Ideally this should be done in the
232 // context of parsing for-init-statement of a foreach statement only. But,
233 // in any other context 'in' is invalid after a declaration and parser
234 // issues the error regardless of outcome of this decision.
235 // FIXME. Change if above assumption does not hold.
236 return TPResult::True();
237 }
238
239 if (Tok.isNot(tok::comma))
240 break;
241 ConsumeToken(); // the comma.
242 }
243
244 return TPResult::Ambiguous();
245}
246
247/// isCXXConditionDeclaration - Disambiguates between a declaration or an
248/// expression for a condition of a if/switch/while/for statement.
249/// If during the disambiguation process a parsing error is encountered,
250/// the function returns true to let the declaration parsing code handle it.
251///
252/// condition:
253/// expression
254/// type-specifier-seq declarator '=' assignment-expression
255/// [C++11] type-specifier-seq declarator '=' initializer-clause
256/// [C++11] type-specifier-seq declarator braced-init-list
257/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
258/// '=' assignment-expression
259///
260bool Parser::isCXXConditionDeclaration() {
261 TPResult TPR = isCXXDeclarationSpecifier();
262 if (TPR != TPResult::Ambiguous())
263 return TPR != TPResult::False(); // Returns true for TPResult::True() or
264 // TPResult::Error().
265
266 // FIXME: Add statistics about the number of ambiguous statements encountered
267 // and how they were resolved (number of declarations+number of expressions).
268
269 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
270 // We need tentative parsing...
271
272 TentativeParsingAction PA(*this);
273
274 // type-specifier-seq
275 if (Tok.is(tok::kw_typeof))
276 TryParseTypeofSpecifier();
277 else {
278 ConsumeToken();
279
280 if (getLangOpts().ObjC1 && Tok.is(tok::less))
281 TryParseProtocolQualifiers();
282 }
283 assert(Tok.is(tok::l_paren) && "Expected '('");
284
285 // declarator
286 TPR = TryParseDeclarator(false/*mayBeAbstract*/);
287
288 // In case of an error, let the declaration parsing code handle it.
289 if (TPR == TPResult::Error())
290 TPR = TPResult::True();
291
292 if (TPR == TPResult::Ambiguous()) {
293 // '='
294 // [GNU] simple-asm-expr[opt] attributes[opt]
295 if (Tok.is(tok::equal) ||
296 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute))
297 TPR = TPResult::True();
Richard Smith80ad52f2013-01-02 11:42:31 +0000298 else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000299 TPR = TPResult::True();
300 else
301 TPR = TPResult::False();
302 }
303
304 PA.Revert();
305
306 assert(TPR == TPResult::True() || TPR == TPResult::False());
307 return TPR == TPResult::True();
308}
309
310 /// \brief Determine whether the next set of tokens contains a type-id.
311 ///
312 /// The context parameter states what context we're parsing right
313 /// now, which affects how this routine copes with the token
314 /// following the type-id. If the context is TypeIdInParens, we have
315 /// already parsed the '(' and we will cease lookahead when we hit
316 /// the corresponding ')'. If the context is
317 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
318 /// before this template argument, and will cease lookahead when we
319 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id
320 /// and false for an expression. If during the disambiguation
321 /// process a parsing error is encountered, the function returns
322 /// true to let the declaration parsing code handle it.
323 ///
324 /// type-id:
325 /// type-specifier-seq abstract-declarator[opt]
326 ///
327bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
328
329 isAmbiguous = false;
330
331 // C++ 8.2p2:
332 // The ambiguity arising from the similarity between a function-style cast and
333 // a type-id can occur in different contexts. The ambiguity appears as a
334 // choice between a function-style cast expression and a declaration of a
335 // type. The resolution is that any construct that could possibly be a type-id
336 // in its syntactic context shall be considered a type-id.
337
338 TPResult TPR = isCXXDeclarationSpecifier();
339 if (TPR != TPResult::Ambiguous())
340 return TPR != TPResult::False(); // Returns true for TPResult::True() or
341 // TPResult::Error().
342
343 // FIXME: Add statistics about the number of ambiguous statements encountered
344 // and how they were resolved (number of declarations+number of expressions).
345
346 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
347 // We need tentative parsing...
348
349 TentativeParsingAction PA(*this);
350
351 // type-specifier-seq
352 if (Tok.is(tok::kw_typeof))
353 TryParseTypeofSpecifier();
354 else {
355 ConsumeToken();
356
357 if (getLangOpts().ObjC1 && Tok.is(tok::less))
358 TryParseProtocolQualifiers();
359 }
360
361 assert(Tok.is(tok::l_paren) && "Expected '('");
362
363 // declarator
364 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
365
366 // In case of an error, let the declaration parsing code handle it.
367 if (TPR == TPResult::Error())
368 TPR = TPResult::True();
369
370 if (TPR == TPResult::Ambiguous()) {
371 // We are supposed to be inside parens, so if after the abstract declarator
372 // we encounter a ')' this is a type-id, otherwise it's an expression.
373 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) {
374 TPR = TPResult::True();
375 isAmbiguous = true;
376
377 // We are supposed to be inside a template argument, so if after
378 // the abstract declarator we encounter a '>', '>>' (in C++0x), or
379 // ',', this is a type-id. Otherwise, it's an expression.
380 } else if (Context == TypeIdAsTemplateArgument &&
381 (Tok.is(tok::greater) || Tok.is(tok::comma) ||
Richard Smith80ad52f2013-01-02 11:42:31 +0000382 (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)))) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000383 TPR = TPResult::True();
384 isAmbiguous = true;
385
386 } else
387 TPR = TPResult::False();
388 }
389
390 PA.Revert();
391
392 assert(TPR == TPResult::True() || TPR == TPResult::False());
393 return TPR == TPResult::True();
394}
395
396/// \brief Returns true if this is a C++11 attribute-specifier. Per
397/// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
398/// always introduce an attribute. In Objective-C++11, this rule does not
399/// apply if either '[' begins a message-send.
400///
401/// If Disambiguate is true, we try harder to determine whether a '[[' starts
402/// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
403///
404/// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
405/// Obj-C message send or the start of an attribute. Otherwise, we assume it
406/// is not an Obj-C message send.
407///
408/// C++11 [dcl.attr.grammar]:
409///
410/// attribute-specifier:
411/// '[' '[' attribute-list ']' ']'
412/// alignment-specifier
413///
414/// attribute-list:
415/// attribute[opt]
416/// attribute-list ',' attribute[opt]
417/// attribute '...'
418/// attribute-list ',' attribute '...'
419///
420/// attribute:
421/// attribute-token attribute-argument-clause[opt]
422///
423/// attribute-token:
424/// identifier
425/// identifier '::' identifier
426///
427/// attribute-argument-clause:
428/// '(' balanced-token-seq ')'
429Parser::CXX11AttributeKind
430Parser::isCXX11AttributeSpecifier(bool Disambiguate,
431 bool OuterMightBeMessageSend) {
432 if (Tok.is(tok::kw_alignas))
433 return CAK_AttributeSpecifier;
434
435 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square))
436 return CAK_NotAttributeSpecifier;
437
438 // No tentative parsing if we don't need to look for ']]' or a lambda.
439 if (!Disambiguate && !getLangOpts().ObjC1)
440 return CAK_AttributeSpecifier;
441
442 TentativeParsingAction PA(*this);
443
444 // Opening brackets were checked for above.
445 ConsumeBracket();
446
447 // Outside Obj-C++11, treat anything with a matching ']]' as an attribute.
448 if (!getLangOpts().ObjC1) {
449 ConsumeBracket();
450
451 bool IsAttribute = SkipUntil(tok::r_square, false);
452 IsAttribute &= Tok.is(tok::r_square);
453
454 PA.Revert();
455
456 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier;
457 }
458
459 // In Obj-C++11, we need to distinguish four situations:
460 // 1a) int x[[attr]]; C++11 attribute.
461 // 1b) [[attr]]; C++11 statement attribute.
462 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index.
463 // 3a) int x[[obj get]]; Message send in array size/index.
464 // 3b) [[Class alloc] init]; Message send in message send.
465 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send.
466 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
467
468 // If we have a lambda-introducer, then this is definitely not a message send.
469 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
470 // into the tentative attribute parse below.
471 LambdaIntroducer Intro;
472 if (!TryParseLambdaIntroducer(Intro)) {
473 // A lambda cannot end with ']]', and an attribute must.
474 bool IsAttribute = Tok.is(tok::r_square);
475
476 PA.Revert();
477
478 if (IsAttribute)
479 // Case 1: C++11 attribute.
480 return CAK_AttributeSpecifier;
481
482 if (OuterMightBeMessageSend)
483 // Case 4: Lambda in message send.
484 return CAK_NotAttributeSpecifier;
485
486 // Case 2: Lambda in array size / index.
487 return CAK_InvalidAttributeSpecifier;
488 }
489
490 ConsumeBracket();
491
492 // If we don't have a lambda-introducer, then we have an attribute or a
493 // message-send.
494 bool IsAttribute = true;
495 while (Tok.isNot(tok::r_square)) {
496 if (Tok.is(tok::comma)) {
497 // Case 1: Stray commas can only occur in attributes.
498 PA.Revert();
499 return CAK_AttributeSpecifier;
500 }
501
502 // Parse the attribute-token, if present.
503 // C++11 [dcl.attr.grammar]:
504 // If a keyword or an alternative token that satisfies the syntactic
505 // requirements of an identifier is contained in an attribute-token,
506 // it is considered an identifier.
507 SourceLocation Loc;
508 if (!TryParseCXX11AttributeIdentifier(Loc)) {
509 IsAttribute = false;
510 break;
511 }
512 if (Tok.is(tok::coloncolon)) {
513 ConsumeToken();
514 if (!TryParseCXX11AttributeIdentifier(Loc)) {
515 IsAttribute = false;
516 break;
517 }
518 }
519
520 // Parse the attribute-argument-clause, if present.
521 if (Tok.is(tok::l_paren)) {
522 ConsumeParen();
523 if (!SkipUntil(tok::r_paren, false)) {
524 IsAttribute = false;
525 break;
526 }
527 }
528
529 if (Tok.is(tok::ellipsis))
530 ConsumeToken();
531
532 if (Tok.isNot(tok::comma))
533 break;
534
535 ConsumeToken();
536 }
537
538 // An attribute must end ']]'.
539 if (IsAttribute) {
540 if (Tok.is(tok::r_square)) {
541 ConsumeBracket();
542 IsAttribute = Tok.is(tok::r_square);
543 } else {
544 IsAttribute = false;
545 }
546 }
547
548 PA.Revert();
549
550 if (IsAttribute)
551 // Case 1: C++11 statement attribute.
552 return CAK_AttributeSpecifier;
553
554 // Case 3: Message send.
555 return CAK_NotAttributeSpecifier;
556}
557
558/// declarator:
559/// direct-declarator
560/// ptr-operator declarator
561///
562/// direct-declarator:
563/// declarator-id
564/// direct-declarator '(' parameter-declaration-clause ')'
565/// cv-qualifier-seq[opt] exception-specification[opt]
566/// direct-declarator '[' constant-expression[opt] ']'
567/// '(' declarator ')'
568/// [GNU] '(' attributes declarator ')'
569///
570/// abstract-declarator:
571/// ptr-operator abstract-declarator[opt]
572/// direct-abstract-declarator
573/// ...
574///
575/// direct-abstract-declarator:
576/// direct-abstract-declarator[opt]
577/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
578/// exception-specification[opt]
579/// direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
580/// '(' abstract-declarator ')'
581///
582/// ptr-operator:
583/// '*' cv-qualifier-seq[opt]
584/// '&'
585/// [C++0x] '&&' [TODO]
586/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
587///
588/// cv-qualifier-seq:
589/// cv-qualifier cv-qualifier-seq[opt]
590///
591/// cv-qualifier:
592/// 'const'
593/// 'volatile'
594///
595/// declarator-id:
596/// '...'[opt] id-expression
597///
598/// id-expression:
599/// unqualified-id
600/// qualified-id [TODO]
601///
602/// unqualified-id:
603/// identifier
604/// operator-function-id [TODO]
605/// conversion-function-id [TODO]
606/// '~' class-name [TODO]
607/// template-id [TODO]
608///
609Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
610 bool mayHaveIdentifier) {
611 // declarator:
612 // direct-declarator
613 // ptr-operator declarator
614
615 while (1) {
616 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier))
617 if (TryAnnotateCXXScopeToken(true))
618 return TPResult::Error();
619
620 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) ||
621 Tok.is(tok::ampamp) ||
622 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) {
623 // ptr-operator
624 ConsumeToken();
625 while (Tok.is(tok::kw_const) ||
626 Tok.is(tok::kw_volatile) ||
627 Tok.is(tok::kw_restrict))
628 ConsumeToken();
629 } else {
630 break;
631 }
632 }
633
634 // direct-declarator:
635 // direct-abstract-declarator:
636 if (Tok.is(tok::ellipsis))
637 ConsumeToken();
638
639 if ((Tok.is(tok::identifier) ||
640 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) &&
641 mayHaveIdentifier) {
642 // declarator-id
643 if (Tok.is(tok::annot_cxxscope))
644 ConsumeToken();
645 else
646 TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
647 ConsumeToken();
648 } else if (Tok.is(tok::l_paren)) {
649 ConsumeParen();
650 if (mayBeAbstract &&
651 (Tok.is(tok::r_paren) || // 'int()' is a function.
652 // 'int(...)' is a function.
653 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) ||
654 isDeclarationSpecifier())) { // 'int(int)' is a function.
655 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
656 // exception-specification[opt]
657 TPResult TPR = TryParseFunctionDeclarator();
658 if (TPR != TPResult::Ambiguous())
659 return TPR;
660 } else {
661 // '(' declarator ')'
662 // '(' attributes declarator ')'
663 // '(' abstract-declarator ')'
664 if (Tok.is(tok::kw___attribute) ||
665 Tok.is(tok::kw___declspec) ||
666 Tok.is(tok::kw___cdecl) ||
667 Tok.is(tok::kw___stdcall) ||
668 Tok.is(tok::kw___fastcall) ||
669 Tok.is(tok::kw___thiscall) ||
670 Tok.is(tok::kw___unaligned))
671 return TPResult::True(); // attributes indicate declaration
672 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
673 if (TPR != TPResult::Ambiguous())
674 return TPR;
675 if (Tok.isNot(tok::r_paren))
676 return TPResult::False();
677 ConsumeParen();
678 }
679 } else if (!mayBeAbstract) {
680 return TPResult::False();
681 }
682
683 while (1) {
684 TPResult TPR(TPResult::Ambiguous());
685
686 // abstract-declarator: ...
687 if (Tok.is(tok::ellipsis))
688 ConsumeToken();
689
690 if (Tok.is(tok::l_paren)) {
691 // Check whether we have a function declarator or a possible ctor-style
692 // initializer that follows the declarator. Note that ctor-style
693 // initializers are not possible in contexts where abstract declarators
694 // are allowed.
695 if (!mayBeAbstract && !isCXXFunctionDeclarator())
696 break;
697
698 // direct-declarator '(' parameter-declaration-clause ')'
699 // cv-qualifier-seq[opt] exception-specification[opt]
700 ConsumeParen();
701 TPR = TryParseFunctionDeclarator();
702 } else if (Tok.is(tok::l_square)) {
703 // direct-declarator '[' constant-expression[opt] ']'
704 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
705 TPR = TryParseBracketDeclarator();
706 } else {
707 break;
708 }
709
710 if (TPR != TPResult::Ambiguous())
711 return TPR;
712 }
713
714 return TPResult::Ambiguous();
715}
716
717Parser::TPResult
718Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
719 switch (Kind) {
720 // Obviously starts an expression.
721 case tok::numeric_constant:
722 case tok::char_constant:
723 case tok::wide_char_constant:
724 case tok::utf16_char_constant:
725 case tok::utf32_char_constant:
726 case tok::string_literal:
727 case tok::wide_string_literal:
728 case tok::utf8_string_literal:
729 case tok::utf16_string_literal:
730 case tok::utf32_string_literal:
731 case tok::l_square:
732 case tok::l_paren:
733 case tok::amp:
734 case tok::ampamp:
735 case tok::star:
736 case tok::plus:
737 case tok::plusplus:
738 case tok::minus:
739 case tok::minusminus:
740 case tok::tilde:
741 case tok::exclaim:
742 case tok::kw_sizeof:
743 case tok::kw___func__:
744 case tok::kw_const_cast:
745 case tok::kw_delete:
746 case tok::kw_dynamic_cast:
747 case tok::kw_false:
748 case tok::kw_new:
749 case tok::kw_operator:
750 case tok::kw_reinterpret_cast:
751 case tok::kw_static_cast:
752 case tok::kw_this:
753 case tok::kw_throw:
754 case tok::kw_true:
755 case tok::kw_typeid:
756 case tok::kw_alignof:
757 case tok::kw_noexcept:
758 case tok::kw_nullptr:
759 case tok::kw__Alignof:
760 case tok::kw___null:
761 case tok::kw___alignof:
762 case tok::kw___builtin_choose_expr:
763 case tok::kw___builtin_offsetof:
764 case tok::kw___builtin_types_compatible_p:
765 case tok::kw___builtin_va_arg:
766 case tok::kw___imag:
767 case tok::kw___real:
768 case tok::kw___FUNCTION__:
769 case tok::kw_L__FUNCTION__:
770 case tok::kw___PRETTY_FUNCTION__:
771 case tok::kw___has_nothrow_assign:
772 case tok::kw___has_nothrow_copy:
773 case tok::kw___has_nothrow_constructor:
774 case tok::kw___has_trivial_assign:
775 case tok::kw___has_trivial_copy:
776 case tok::kw___has_trivial_constructor:
777 case tok::kw___has_trivial_destructor:
778 case tok::kw___has_virtual_destructor:
779 case tok::kw___is_abstract:
780 case tok::kw___is_base_of:
781 case tok::kw___is_class:
782 case tok::kw___is_convertible_to:
783 case tok::kw___is_empty:
784 case tok::kw___is_enum:
785 case tok::kw___is_interface_class:
786 case tok::kw___is_final:
787 case tok::kw___is_literal:
788 case tok::kw___is_literal_type:
789 case tok::kw___is_pod:
790 case tok::kw___is_polymorphic:
791 case tok::kw___is_trivial:
792 case tok::kw___is_trivially_assignable:
793 case tok::kw___is_trivially_constructible:
794 case tok::kw___is_trivially_copyable:
795 case tok::kw___is_union:
796 case tok::kw___uuidof:
797 return TPResult::True();
798
799 // Obviously starts a type-specifier-seq:
800 case tok::kw_char:
801 case tok::kw_const:
802 case tok::kw_double:
803 case tok::kw_enum:
804 case tok::kw_half:
805 case tok::kw_float:
806 case tok::kw_int:
807 case tok::kw_long:
808 case tok::kw___int64:
809 case tok::kw___int128:
810 case tok::kw_restrict:
811 case tok::kw_short:
812 case tok::kw_signed:
813 case tok::kw_struct:
814 case tok::kw_union:
815 case tok::kw_unsigned:
816 case tok::kw_void:
817 case tok::kw_volatile:
818 case tok::kw__Bool:
819 case tok::kw__Complex:
820 case tok::kw_class:
821 case tok::kw_typename:
822 case tok::kw_wchar_t:
823 case tok::kw_char16_t:
824 case tok::kw_char32_t:
825 case tok::kw___underlying_type:
826 case tok::kw_thread_local:
827 case tok::kw__Decimal32:
828 case tok::kw__Decimal64:
829 case tok::kw__Decimal128:
830 case tok::kw___thread:
831 case tok::kw_typeof:
832 case tok::kw___cdecl:
833 case tok::kw___stdcall:
834 case tok::kw___fastcall:
835 case tok::kw___thiscall:
836 case tok::kw___unaligned:
837 case tok::kw___vector:
838 case tok::kw___pixel:
839 case tok::kw__Atomic:
Guy Benyeib13621d2012-12-18 14:38:23 +0000840 case tok::kw_image1d_t:
841 case tok::kw_image1d_array_t:
842 case tok::kw_image1d_buffer_t:
843 case tok::kw_image2d_t:
844 case tok::kw_image2d_array_t:
845 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +0000846 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +0000847 case tok::kw_event_t:
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000848 case tok::kw___unknown_anytype:
849 return TPResult::False();
850
851 default:
852 break;
853 }
854
855 return TPResult::Ambiguous();
856}
857
858bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
859 return std::find(TentativelyDeclaredIdentifiers.begin(),
860 TentativelyDeclaredIdentifiers.end(), II)
861 != TentativelyDeclaredIdentifiers.end();
862}
863
864/// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration
865/// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could
866/// be either a decl-specifier or a function-style cast, and TPResult::Error()
867/// if a parsing error was found and reported.
868///
869/// If HasMissingTypename is provided, a name with a dependent scope specifier
870/// will be treated as ambiguous if the 'typename' keyword is missing. If this
871/// happens, *HasMissingTypename will be set to 'true'. This will also be used
872/// as an indicator that undeclared identifiers (which will trigger a later
873/// parse error) should be treated as types. Returns TPResult::Ambiguous() in
874/// such cases.
875///
876/// decl-specifier:
877/// storage-class-specifier
878/// type-specifier
879/// function-specifier
880/// 'friend'
881/// 'typedef'
882/// [C++0x] 'constexpr'
883/// [GNU] attributes declaration-specifiers[opt]
884///
885/// storage-class-specifier:
886/// 'register'
887/// 'static'
888/// 'extern'
889/// 'mutable'
890/// 'auto'
891/// [GNU] '__thread'
892///
893/// function-specifier:
894/// 'inline'
895/// 'virtual'
896/// 'explicit'
897///
898/// typedef-name:
899/// identifier
900///
901/// type-specifier:
902/// simple-type-specifier
903/// class-specifier
904/// enum-specifier
905/// elaborated-type-specifier
906/// typename-specifier
907/// cv-qualifier
908///
909/// simple-type-specifier:
910/// '::'[opt] nested-name-specifier[opt] type-name
911/// '::'[opt] nested-name-specifier 'template'
912/// simple-template-id [TODO]
913/// 'char'
914/// 'wchar_t'
915/// 'bool'
916/// 'short'
917/// 'int'
918/// 'long'
919/// 'signed'
920/// 'unsigned'
921/// 'float'
922/// 'double'
923/// 'void'
924/// [GNU] typeof-specifier
925/// [GNU] '_Complex'
926/// [C++0x] 'auto' [TODO]
927/// [C++0x] 'decltype' ( expression )
928///
929/// type-name:
930/// class-name
931/// enum-name
932/// typedef-name
933///
934/// elaborated-type-specifier:
935/// class-key '::'[opt] nested-name-specifier[opt] identifier
936/// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
937/// simple-template-id
938/// 'enum' '::'[opt] nested-name-specifier[opt] identifier
939///
940/// enum-name:
941/// identifier
942///
943/// enum-specifier:
944/// 'enum' identifier[opt] '{' enumerator-list[opt] '}'
945/// 'enum' identifier[opt] '{' enumerator-list ',' '}'
946///
947/// class-specifier:
948/// class-head '{' member-specification[opt] '}'
949///
950/// class-head:
951/// class-key identifier[opt] base-clause[opt]
952/// class-key nested-name-specifier identifier base-clause[opt]
953/// class-key nested-name-specifier[opt] simple-template-id
954/// base-clause[opt]
955///
956/// class-key:
957/// 'class'
958/// 'struct'
959/// 'union'
960///
961/// cv-qualifier:
962/// 'const'
963/// 'volatile'
964/// [GNU] restrict
965///
966Parser::TPResult
967Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
968 bool *HasMissingTypename) {
969 switch (Tok.getKind()) {
970 case tok::identifier: {
971 // Check for need to substitute AltiVec __vector keyword
972 // for "vector" identifier.
973 if (TryAltiVecVectorToken())
974 return TPResult::True();
975
976 const Token &Next = NextToken();
977 // In 'foo bar', 'foo' is always a type name outside of Objective-C.
978 if (!getLangOpts().ObjC1 && Next.is(tok::identifier))
979 return TPResult::True();
980
981 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) {
982 // Determine whether this is a valid expression. If not, we will hit
983 // a parse error one way or another. In that case, tell the caller that
984 // this is ambiguous. Typo-correct to type and expression keywords and
985 // to types and identifiers, in order to try to recover from errors.
986 CorrectionCandidateCallback TypoCorrection;
987 TypoCorrection.WantRemainingKeywords = false;
988 switch (TryAnnotateName(false /* no nested name specifier */,
989 &TypoCorrection)) {
990 case ANK_Error:
991 return TPResult::Error();
992 case ANK_TentativeDecl:
993 return TPResult::False();
994 case ANK_TemplateName:
995 // A bare type template-name which can't be a template template
996 // argument is an error, and was probably intended to be a type.
997 return GreaterThanIsOperator ? TPResult::True() : TPResult::False();
998 case ANK_Unresolved:
999 return HasMissingTypename ? TPResult::Ambiguous() : TPResult::False();
1000 case ANK_Success:
1001 break;
1002 }
1003 assert(Tok.isNot(tok::identifier) &&
1004 "TryAnnotateName succeeded without producing an annotation");
1005 } else {
1006 // This might possibly be a type with a dependent scope specifier and
1007 // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1008 // since it will annotate as a primary expression, and we want to use the
1009 // "missing 'typename'" logic.
1010 if (TryAnnotateTypeOrScopeToken())
1011 return TPResult::Error();
1012 // If annotation failed, assume it's a non-type.
1013 // FIXME: If this happens due to an undeclared identifier, treat it as
1014 // ambiguous.
1015 if (Tok.is(tok::identifier))
1016 return TPResult::False();
1017 }
1018
1019 // We annotated this token as something. Recurse to handle whatever we got.
1020 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1021 }
1022
1023 case tok::kw_typename: // typename T::type
1024 // Annotate typenames and C++ scope specifiers. If we get one, just
1025 // recurse to handle whatever we get.
1026 if (TryAnnotateTypeOrScopeToken())
1027 return TPResult::Error();
1028 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1029
1030 case tok::coloncolon: { // ::foo::bar
1031 const Token &Next = NextToken();
1032 if (Next.is(tok::kw_new) || // ::new
1033 Next.is(tok::kw_delete)) // ::delete
1034 return TPResult::False();
1035 }
1036 // Fall through.
1037 case tok::kw_decltype:
1038 // Annotate typenames and C++ scope specifiers. If we get one, just
1039 // recurse to handle whatever we get.
1040 if (TryAnnotateTypeOrScopeToken())
1041 return TPResult::Error();
1042 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename);
1043
1044 // decl-specifier:
1045 // storage-class-specifier
1046 // type-specifier
1047 // function-specifier
1048 // 'friend'
1049 // 'typedef'
1050 // 'constexpr'
1051 case tok::kw_friend:
1052 case tok::kw_typedef:
1053 case tok::kw_constexpr:
1054 // storage-class-specifier
1055 case tok::kw_register:
1056 case tok::kw_static:
1057 case tok::kw_extern:
1058 case tok::kw_mutable:
1059 case tok::kw_auto:
1060 case tok::kw___thread:
1061 // function-specifier
1062 case tok::kw_inline:
1063 case tok::kw_virtual:
1064 case tok::kw_explicit:
1065
1066 // Modules
1067 case tok::kw___module_private__:
1068
1069 // Debugger support
1070 case tok::kw___unknown_anytype:
1071
1072 // type-specifier:
1073 // simple-type-specifier
1074 // class-specifier
1075 // enum-specifier
1076 // elaborated-type-specifier
1077 // typename-specifier
1078 // cv-qualifier
1079
1080 // class-specifier
1081 // elaborated-type-specifier
1082 case tok::kw_class:
1083 case tok::kw_struct:
1084 case tok::kw_union:
1085 // enum-specifier
1086 case tok::kw_enum:
1087 // cv-qualifier
1088 case tok::kw_const:
1089 case tok::kw_volatile:
1090
1091 // GNU
1092 case tok::kw_restrict:
1093 case tok::kw__Complex:
1094 case tok::kw___attribute:
1095 return TPResult::True();
1096
1097 // Microsoft
1098 case tok::kw___declspec:
1099 case tok::kw___cdecl:
1100 case tok::kw___stdcall:
1101 case tok::kw___fastcall:
1102 case tok::kw___thiscall:
1103 case tok::kw___w64:
1104 case tok::kw___ptr64:
1105 case tok::kw___ptr32:
1106 case tok::kw___forceinline:
1107 case tok::kw___unaligned:
1108 return TPResult::True();
1109
1110 // Borland
1111 case tok::kw___pascal:
1112 return TPResult::True();
1113
1114 // AltiVec
1115 case tok::kw___vector:
1116 return TPResult::True();
1117
1118 case tok::annot_template_id: {
1119 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1120 if (TemplateId->Kind != TNK_Type_template)
1121 return TPResult::False();
1122 CXXScopeSpec SS;
1123 AnnotateTemplateIdTokenAsType();
1124 assert(Tok.is(tok::annot_typename));
1125 goto case_typename;
1126 }
1127
1128 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1129 // We've already annotated a scope; try to annotate a type.
1130 if (TryAnnotateTypeOrScopeToken())
1131 return TPResult::Error();
1132 if (!Tok.is(tok::annot_typename)) {
1133 // If the next token is an identifier or a type qualifier, then this
1134 // can't possibly be a valid expression either.
1135 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1136 CXXScopeSpec SS;
1137 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1138 Tok.getAnnotationRange(),
1139 SS);
1140 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) {
1141 TentativeParsingAction PA(*this);
1142 ConsumeToken();
1143 ConsumeToken();
1144 bool isIdentifier = Tok.is(tok::identifier);
1145 TPResult TPR = TPResult::False();
1146 if (!isIdentifier)
1147 TPR = isCXXDeclarationSpecifier(BracedCastResult,
1148 HasMissingTypename);
1149 PA.Revert();
1150
1151 if (isIdentifier ||
1152 TPR == TPResult::True() || TPR == TPResult::Error())
1153 return TPResult::Error();
1154
1155 if (HasMissingTypename) {
1156 // We can't tell whether this is a missing 'typename' or a valid
1157 // expression.
1158 *HasMissingTypename = true;
1159 return TPResult::Ambiguous();
1160 }
1161 } else {
1162 // Try to resolve the name. If it doesn't exist, assume it was
1163 // intended to name a type and keep disambiguating.
1164 switch (TryAnnotateName(false /* SS is not dependent */)) {
1165 case ANK_Error:
1166 return TPResult::Error();
1167 case ANK_TentativeDecl:
1168 return TPResult::False();
1169 case ANK_TemplateName:
1170 // A bare type template-name which can't be a template template
1171 // argument is an error, and was probably intended to be a type.
1172 return GreaterThanIsOperator ? TPResult::True() : TPResult::False();
1173 case ANK_Unresolved:
1174 return HasMissingTypename ? TPResult::Ambiguous()
1175 : TPResult::False();
1176 case ANK_Success:
1177 // Annotated it, check again.
1178 assert(Tok.isNot(tok::annot_cxxscope) ||
1179 NextToken().isNot(tok::identifier));
1180 return isCXXDeclarationSpecifier(BracedCastResult,
1181 HasMissingTypename);
1182 }
1183 }
1184 }
1185 return TPResult::False();
1186 }
1187 // If that succeeded, fallthrough into the generic simple-type-id case.
1188
1189 // The ambiguity resides in a simple-type-specifier/typename-specifier
1190 // followed by a '('. The '(' could either be the start of:
1191 //
1192 // direct-declarator:
1193 // '(' declarator ')'
1194 //
1195 // direct-abstract-declarator:
1196 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1197 // exception-specification[opt]
1198 // '(' abstract-declarator ')'
1199 //
1200 // or part of a function-style cast expression:
1201 //
1202 // simple-type-specifier '(' expression-list[opt] ')'
1203 //
1204
1205 // simple-type-specifier:
1206
1207 case tok::annot_typename:
1208 case_typename:
1209 // In Objective-C, we might have a protocol-qualified type.
1210 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) {
1211 // Tentatively parse the
1212 TentativeParsingAction PA(*this);
1213 ConsumeToken(); // The type token
1214
1215 TPResult TPR = TryParseProtocolQualifiers();
1216 bool isFollowedByParen = Tok.is(tok::l_paren);
1217 bool isFollowedByBrace = Tok.is(tok::l_brace);
1218
1219 PA.Revert();
1220
1221 if (TPR == TPResult::Error())
1222 return TPResult::Error();
1223
1224 if (isFollowedByParen)
1225 return TPResult::Ambiguous();
1226
Richard Smith80ad52f2013-01-02 11:42:31 +00001227 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001228 return BracedCastResult;
1229
1230 return TPResult::True();
1231 }
1232
1233 case tok::kw_char:
1234 case tok::kw_wchar_t:
1235 case tok::kw_char16_t:
1236 case tok::kw_char32_t:
1237 case tok::kw_bool:
1238 case tok::kw_short:
1239 case tok::kw_int:
1240 case tok::kw_long:
1241 case tok::kw___int64:
1242 case tok::kw___int128:
1243 case tok::kw_signed:
1244 case tok::kw_unsigned:
1245 case tok::kw_half:
1246 case tok::kw_float:
1247 case tok::kw_double:
1248 case tok::kw_void:
1249 case tok::annot_decltype:
1250 if (NextToken().is(tok::l_paren))
1251 return TPResult::Ambiguous();
1252
1253 // This is a function-style cast in all cases we disambiguate other than
1254 // one:
1255 // struct S {
1256 // enum E : int { a = 4 }; // enum
1257 // enum E : int { 4 }; // bit-field
1258 // };
Richard Smith80ad52f2013-01-02 11:42:31 +00001259 if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001260 return BracedCastResult;
1261
1262 if (isStartOfObjCClassMessageMissingOpenBracket())
1263 return TPResult::False();
1264
1265 return TPResult::True();
1266
1267 // GNU typeof support.
1268 case tok::kw_typeof: {
1269 if (NextToken().isNot(tok::l_paren))
1270 return TPResult::True();
1271
1272 TentativeParsingAction PA(*this);
1273
1274 TPResult TPR = TryParseTypeofSpecifier();
1275 bool isFollowedByParen = Tok.is(tok::l_paren);
1276 bool isFollowedByBrace = Tok.is(tok::l_brace);
1277
1278 PA.Revert();
1279
1280 if (TPR == TPResult::Error())
1281 return TPResult::Error();
1282
1283 if (isFollowedByParen)
1284 return TPResult::Ambiguous();
1285
Richard Smith80ad52f2013-01-02 11:42:31 +00001286 if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001287 return BracedCastResult;
1288
1289 return TPResult::True();
1290 }
1291
1292 // C++0x type traits support
1293 case tok::kw___underlying_type:
1294 return TPResult::True();
1295
1296 // C11 _Atomic
1297 case tok::kw__Atomic:
1298 return TPResult::True();
1299
1300 default:
1301 return TPResult::False();
1302 }
1303}
1304
1305/// [GNU] typeof-specifier:
1306/// 'typeof' '(' expressions ')'
1307/// 'typeof' '(' type-name ')'
1308///
1309Parser::TPResult Parser::TryParseTypeofSpecifier() {
1310 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1311 ConsumeToken();
1312
1313 assert(Tok.is(tok::l_paren) && "Expected '('");
1314 // Parse through the parens after 'typeof'.
1315 ConsumeParen();
1316 if (!SkipUntil(tok::r_paren))
1317 return TPResult::Error();
1318
1319 return TPResult::Ambiguous();
1320}
1321
1322/// [ObjC] protocol-qualifiers:
1323//// '<' identifier-list '>'
1324Parser::TPResult Parser::TryParseProtocolQualifiers() {
1325 assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1326 ConsumeToken();
1327 do {
1328 if (Tok.isNot(tok::identifier))
1329 return TPResult::Error();
1330 ConsumeToken();
1331
1332 if (Tok.is(tok::comma)) {
1333 ConsumeToken();
1334 continue;
1335 }
1336
1337 if (Tok.is(tok::greater)) {
1338 ConsumeToken();
1339 return TPResult::Ambiguous();
1340 }
1341 } while (false);
1342
1343 return TPResult::Error();
1344}
1345
1346Parser::TPResult
1347Parser::TryParseDeclarationSpecifier(bool *HasMissingTypename) {
1348 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False(),
1349 HasMissingTypename);
1350 if (TPR != TPResult::Ambiguous())
1351 return TPR;
1352
1353 if (Tok.is(tok::kw_typeof))
1354 TryParseTypeofSpecifier();
1355 else {
1356 if (Tok.is(tok::annot_cxxscope))
1357 ConsumeToken();
1358 ConsumeToken();
1359
1360 if (getLangOpts().ObjC1 && Tok.is(tok::less))
1361 TryParseProtocolQualifiers();
1362 }
1363
1364 return TPResult::Ambiguous();
1365}
1366
1367/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1368/// a constructor-style initializer, when parsing declaration statements.
1369/// Returns true for function declarator and false for constructor-style
1370/// initializer.
1371/// If during the disambiguation process a parsing error is encountered,
1372/// the function returns true to let the declaration parsing code handle it.
1373///
1374/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1375/// exception-specification[opt]
1376///
1377bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1378
1379 // C++ 8.2p1:
1380 // The ambiguity arising from the similarity between a function-style cast and
1381 // a declaration mentioned in 6.8 can also occur in the context of a
1382 // declaration. In that context, the choice is between a function declaration
1383 // with a redundant set of parentheses around a parameter name and an object
1384 // declaration with a function-style cast as the initializer. Just as for the
1385 // ambiguities mentioned in 6.8, the resolution is to consider any construct
1386 // that could possibly be a declaration a declaration.
1387
1388 TentativeParsingAction PA(*this);
1389
1390 ConsumeParen();
1391 bool InvalidAsDeclaration = false;
1392 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
1393 if (TPR == TPResult::Ambiguous()) {
1394 if (Tok.isNot(tok::r_paren))
1395 TPR = TPResult::False();
1396 else {
1397 const Token &Next = NextToken();
1398 if (Next.is(tok::amp) || Next.is(tok::ampamp) ||
1399 Next.is(tok::kw_const) || Next.is(tok::kw_volatile) ||
1400 Next.is(tok::kw_throw) || Next.is(tok::kw_noexcept) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001401 Next.is(tok::l_square) || isCXX11VirtSpecifier(Next) ||
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001402 Next.is(tok::l_brace) || Next.is(tok::kw_try) ||
1403 Next.is(tok::equal) || Next.is(tok::arrow))
1404 // The next token cannot appear after a constructor-style initializer,
1405 // and can appear next in a function definition. This must be a function
1406 // declarator.
1407 TPR = TPResult::True();
1408 else if (InvalidAsDeclaration)
1409 // Use the absence of 'typename' as a tie-breaker.
1410 TPR = TPResult::False();
1411 }
1412 }
1413
1414 PA.Revert();
1415
1416 if (IsAmbiguous && TPR == TPResult::Ambiguous())
1417 *IsAmbiguous = true;
1418
1419 // In case of an error, let the declaration parsing code handle it.
1420 return TPR != TPResult::False();
1421}
1422
1423/// parameter-declaration-clause:
1424/// parameter-declaration-list[opt] '...'[opt]
1425/// parameter-declaration-list ',' '...'
1426///
1427/// parameter-declaration-list:
1428/// parameter-declaration
1429/// parameter-declaration-list ',' parameter-declaration
1430///
1431/// parameter-declaration:
1432/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1433/// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1434/// '=' assignment-expression
1435/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1436/// attributes[opt]
1437/// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1438/// attributes[opt] '=' assignment-expression
1439///
1440Parser::TPResult
1441Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration) {
1442
1443 if (Tok.is(tok::r_paren))
1444 return TPResult::Ambiguous();
1445
1446 // parameter-declaration-list[opt] '...'[opt]
1447 // parameter-declaration-list ',' '...'
1448 //
1449 // parameter-declaration-list:
1450 // parameter-declaration
1451 // parameter-declaration-list ',' parameter-declaration
1452 //
1453 while (1) {
1454 // '...'[opt]
1455 if (Tok.is(tok::ellipsis)) {
1456 ConsumeToken();
1457 if (Tok.is(tok::r_paren))
1458 return TPResult::True(); // '...)' is a sign of a function declarator.
1459 else
1460 return TPResult::False();
1461 }
1462
1463 // An attribute-specifier-seq here is a sign of a function declarator.
1464 if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1465 /*OuterMightBeMessageSend*/true))
1466 return TPResult::True();
1467
1468 ParsedAttributes attrs(AttrFactory);
1469 MaybeParseMicrosoftAttributes(attrs);
1470
1471 // decl-specifier-seq
1472 // A parameter-declaration's initializer must be preceded by an '=', so
1473 // decl-specifier-seq '{' is not a parameter in C++11.
1474 TPResult TPR = TryParseDeclarationSpecifier(InvalidAsDeclaration);
1475 if (TPR != TPResult::Ambiguous())
1476 return TPR;
1477
1478 // declarator
1479 // abstract-declarator[opt]
1480 TPR = TryParseDeclarator(true/*mayBeAbstract*/);
1481 if (TPR != TPResult::Ambiguous())
1482 return TPR;
1483
1484 // [GNU] attributes[opt]
1485 if (Tok.is(tok::kw___attribute))
1486 return TPResult::True();
1487
1488 if (Tok.is(tok::equal)) {
1489 // '=' assignment-expression
1490 // Parse through assignment-expression.
1491 if (!SkipUntil(tok::comma, tok::r_paren, true/*StopAtSemi*/,
1492 true/*DontConsume*/))
1493 return TPResult::Error();
1494 }
1495
1496 if (Tok.is(tok::ellipsis)) {
1497 ConsumeToken();
1498 if (Tok.is(tok::r_paren))
1499 return TPResult::True(); // '...)' is a sign of a function declarator.
1500 else
1501 return TPResult::False();
1502 }
1503
1504 if (Tok.isNot(tok::comma))
1505 break;
1506 ConsumeToken(); // the comma.
1507 }
1508
1509 return TPResult::Ambiguous();
1510}
1511
1512/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
1513/// parsing as a function declarator.
1514/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
1515/// return TPResult::Ambiguous(), otherwise it will return either False() or
1516/// Error().
1517///
1518/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1519/// exception-specification[opt]
1520///
1521/// exception-specification:
1522/// 'throw' '(' type-id-list[opt] ')'
1523///
1524Parser::TPResult Parser::TryParseFunctionDeclarator() {
1525
1526 // The '(' is already parsed.
1527
1528 TPResult TPR = TryParseParameterDeclarationClause();
1529 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren))
1530 TPR = TPResult::False();
1531
1532 if (TPR == TPResult::False() || TPR == TPResult::Error())
1533 return TPR;
1534
1535 // Parse through the parens.
1536 if (!SkipUntil(tok::r_paren))
1537 return TPResult::Error();
1538
1539 // cv-qualifier-seq
1540 while (Tok.is(tok::kw_const) ||
1541 Tok.is(tok::kw_volatile) ||
1542 Tok.is(tok::kw_restrict) )
1543 ConsumeToken();
1544
1545 // ref-qualifier[opt]
1546 if (Tok.is(tok::amp) || Tok.is(tok::ampamp))
1547 ConsumeToken();
1548
1549 // exception-specification
1550 if (Tok.is(tok::kw_throw)) {
1551 ConsumeToken();
1552 if (Tok.isNot(tok::l_paren))
1553 return TPResult::Error();
1554
1555 // Parse through the parens after 'throw'.
1556 ConsumeParen();
1557 if (!SkipUntil(tok::r_paren))
1558 return TPResult::Error();
1559 }
1560 if (Tok.is(tok::kw_noexcept)) {
1561 ConsumeToken();
1562 // Possibly an expression as well.
1563 if (Tok.is(tok::l_paren)) {
1564 // Find the matching rparen.
1565 ConsumeParen();
1566 if (!SkipUntil(tok::r_paren))
1567 return TPResult::Error();
1568 }
1569 }
1570
1571 return TPResult::Ambiguous();
1572}
1573
1574/// '[' constant-expression[opt] ']'
1575///
1576Parser::TPResult Parser::TryParseBracketDeclarator() {
1577 ConsumeBracket();
1578 if (!SkipUntil(tok::r_square))
1579 return TPResult::Error();
1580
1581 return TPResult::Ambiguous();
1582}