clang API Documentation
00001 //===--- ParseTentative.cpp - Ambiguity Resolution Parsing ----------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the tentative parsing portions of the Parser 00011 // interfaces, for ambiguity resolution. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "clang/Parse/Parser.h" 00016 #include "clang/Parse/ParseDiagnostic.h" 00017 #include "clang/Sema/ParsedTemplate.h" 00018 using namespace clang; 00019 00020 /// isCXXDeclarationStatement - C++-specialized function that disambiguates 00021 /// between a declaration or an expression statement, when parsing function 00022 /// bodies. Returns true for declaration, false for expression. 00023 /// 00024 /// declaration-statement: 00025 /// block-declaration 00026 /// 00027 /// block-declaration: 00028 /// simple-declaration 00029 /// asm-definition 00030 /// namespace-alias-definition 00031 /// using-declaration 00032 /// using-directive 00033 /// [C++0x] static_assert-declaration 00034 /// 00035 /// asm-definition: 00036 /// 'asm' '(' string-literal ')' ';' 00037 /// 00038 /// namespace-alias-definition: 00039 /// 'namespace' identifier = qualified-namespace-specifier ';' 00040 /// 00041 /// using-declaration: 00042 /// 'using' typename[opt] '::'[opt] nested-name-specifier 00043 /// unqualified-id ';' 00044 /// 'using' '::' unqualified-id ; 00045 /// 00046 /// using-directive: 00047 /// 'using' 'namespace' '::'[opt] nested-name-specifier[opt] 00048 /// namespace-name ';' 00049 /// 00050 bool Parser::isCXXDeclarationStatement() { 00051 switch (Tok.getKind()) { 00052 // asm-definition 00053 case tok::kw_asm: 00054 // namespace-alias-definition 00055 case tok::kw_namespace: 00056 // using-declaration 00057 // using-directive 00058 case tok::kw_using: 00059 // static_assert-declaration 00060 case tok::kw_static_assert: 00061 case tok::kw__Static_assert: 00062 return true; 00063 // simple-declaration 00064 default: 00065 return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false); 00066 } 00067 } 00068 00069 /// isCXXSimpleDeclaration - C++-specialized function that disambiguates 00070 /// between a simple-declaration or an expression-statement. 00071 /// If during the disambiguation process a parsing error is encountered, 00072 /// the function returns true to let the declaration parsing code handle it. 00073 /// Returns false if the statement is disambiguated as expression. 00074 /// 00075 /// simple-declaration: 00076 /// decl-specifier-seq init-declarator-list[opt] ';' 00077 /// 00078 /// (if AllowForRangeDecl specified) 00079 /// for ( for-range-declaration : for-range-initializer ) statement 00080 /// for-range-declaration: 00081 /// attribute-specifier-seqopt type-specifier-seq declarator 00082 bool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) { 00083 // C++ 6.8p1: 00084 // There is an ambiguity in the grammar involving expression-statements and 00085 // declarations: An expression-statement with a function-style explicit type 00086 // conversion (5.2.3) as its leftmost subexpression can be indistinguishable 00087 // from a declaration where the first declarator starts with a '('. In those 00088 // cases the statement is a declaration. [Note: To disambiguate, the whole 00089 // statement might have to be examined to determine if it is an 00090 // expression-statement or a declaration]. 00091 00092 // C++ 6.8p3: 00093 // The disambiguation is purely syntactic; that is, the meaning of the names 00094 // occurring in such a statement, beyond whether they are type-names or not, 00095 // is not generally used in or changed by the disambiguation. Class 00096 // templates are instantiated as necessary to determine if a qualified name 00097 // is a type-name. Disambiguation precedes parsing, and a statement 00098 // disambiguated as a declaration may be an ill-formed declaration. 00099 00100 // We don't have to parse all of the decl-specifier-seq part. There's only 00101 // an ambiguity if the first decl-specifier is 00102 // simple-type-specifier/typename-specifier followed by a '(', which may 00103 // indicate a function-style cast expression. 00104 // isCXXDeclarationSpecifier will return TPResult::Ambiguous() only in such 00105 // a case. 00106 00107 bool InvalidAsDeclaration = false; 00108 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False(), 00109 &InvalidAsDeclaration); 00110 if (TPR != TPResult::Ambiguous()) 00111 return TPR != TPResult::False(); // Returns true for TPResult::True() or 00112 // TPResult::Error(). 00113 00114 // FIXME: TryParseSimpleDeclaration doesn't look past the first initializer, 00115 // and so gets some cases wrong. We can't carry on if we've already seen 00116 // something which makes this statement invalid as a declaration in this case, 00117 // since it can cause us to misparse valid code. Revisit this once 00118 // TryParseInitDeclaratorList is fixed. 00119 if (InvalidAsDeclaration) 00120 return false; 00121 00122 // FIXME: Add statistics about the number of ambiguous statements encountered 00123 // and how they were resolved (number of declarations+number of expressions). 00124 00125 // Ok, we have a simple-type-specifier/typename-specifier followed by a '(', 00126 // or an identifier which doesn't resolve as anything. We need tentative 00127 // parsing... 00128 00129 TentativeParsingAction PA(*this); 00130 TPR = TryParseSimpleDeclaration(AllowForRangeDecl); 00131 PA.Revert(); 00132 00133 // In case of an error, let the declaration parsing code handle it. 00134 if (TPR == TPResult::Error()) 00135 return true; 00136 00137 // Declarations take precedence over expressions. 00138 if (TPR == TPResult::Ambiguous()) 00139 TPR = TPResult::True(); 00140 00141 assert(TPR == TPResult::True() || TPR == TPResult::False()); 00142 return TPR == TPResult::True(); 00143 } 00144 00145 /// simple-declaration: 00146 /// decl-specifier-seq init-declarator-list[opt] ';' 00147 /// 00148 /// (if AllowForRangeDecl specified) 00149 /// for ( for-range-declaration : for-range-initializer ) statement 00150 /// for-range-declaration: 00151 /// attribute-specifier-seqopt type-specifier-seq declarator 00152 /// 00153 Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) { 00154 if (Tok.is(tok::kw_typeof)) 00155 TryParseTypeofSpecifier(); 00156 else { 00157 if (Tok.is(tok::annot_cxxscope)) 00158 ConsumeToken(); 00159 ConsumeToken(); 00160 00161 if (getLangOpts().ObjC1 && Tok.is(tok::less)) 00162 TryParseProtocolQualifiers(); 00163 } 00164 00165 // Two decl-specifiers in a row conclusively disambiguate this as being a 00166 // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the 00167 // overwhelmingly common case that the next token is a '('. 00168 if (Tok.isNot(tok::l_paren)) { 00169 TPResult TPR = isCXXDeclarationSpecifier(); 00170 if (TPR == TPResult::Ambiguous()) 00171 return TPResult::True(); 00172 if (TPR == TPResult::True() || TPR == TPResult::Error()) 00173 return TPR; 00174 assert(TPR == TPResult::False()); 00175 } 00176 00177 TPResult TPR = TryParseInitDeclaratorList(); 00178 if (TPR != TPResult::Ambiguous()) 00179 return TPR; 00180 00181 if (Tok.isNot(tok::semi) && (!AllowForRangeDecl || Tok.isNot(tok::colon))) 00182 return TPResult::False(); 00183 00184 return TPResult::Ambiguous(); 00185 } 00186 00187 /// Tentatively parse an init-declarator-list in order to disambiguate it from 00188 /// an expression. 00189 /// 00190 /// init-declarator-list: 00191 /// init-declarator 00192 /// init-declarator-list ',' init-declarator 00193 /// 00194 /// init-declarator: 00195 /// declarator initializer[opt] 00196 /// [GNU] declarator simple-asm-expr[opt] attributes[opt] initializer[opt] 00197 /// 00198 /// initializer: 00199 /// brace-or-equal-initializer 00200 /// '(' expression-list ')' 00201 /// 00202 /// brace-or-equal-initializer: 00203 /// '=' initializer-clause 00204 /// [C++11] braced-init-list 00205 /// 00206 /// initializer-clause: 00207 /// assignment-expression 00208 /// braced-init-list 00209 /// 00210 /// braced-init-list: 00211 /// '{' initializer-list ','[opt] '}' 00212 /// '{' '}' 00213 /// 00214 Parser::TPResult Parser::TryParseInitDeclaratorList() { 00215 while (1) { 00216 // declarator 00217 TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/); 00218 if (TPR != TPResult::Ambiguous()) 00219 return TPR; 00220 00221 // [GNU] simple-asm-expr[opt] attributes[opt] 00222 if (Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute)) 00223 return TPResult::True(); 00224 00225 // initializer[opt] 00226 if (Tok.is(tok::l_paren)) { 00227 // Parse through the parens. 00228 ConsumeParen(); 00229 if (!SkipUntil(tok::r_paren)) 00230 return TPResult::Error(); 00231 } else if (Tok.is(tok::l_brace)) { 00232 // A left-brace here is sufficient to disambiguate the parse; an 00233 // expression can never be followed directly by a braced-init-list. 00234 return TPResult::True(); 00235 } else if (Tok.is(tok::equal) || isTokIdentifier_in()) { 00236 // MSVC and g++ won't examine the rest of declarators if '=' is 00237 // encountered; they just conclude that we have a declaration. 00238 // EDG parses the initializer completely, which is the proper behavior 00239 // for this case. 00240 // 00241 // At present, Clang follows MSVC and g++, since the parser does not have 00242 // the ability to parse an expression fully without recording the 00243 // results of that parse. 00244 // Also allow 'in' after on objective-c declaration as in: 00245 // for (int (^b)(void) in array). Ideally this should be done in the 00246 // context of parsing for-init-statement of a foreach statement only. But, 00247 // in any other context 'in' is invalid after a declaration and parser 00248 // issues the error regardless of outcome of this decision. 00249 // FIXME. Change if above assumption does not hold. 00250 return TPResult::True(); 00251 } 00252 00253 if (Tok.isNot(tok::comma)) 00254 break; 00255 ConsumeToken(); // the comma. 00256 } 00257 00258 return TPResult::Ambiguous(); 00259 } 00260 00261 /// isCXXConditionDeclaration - Disambiguates between a declaration or an 00262 /// expression for a condition of a if/switch/while/for statement. 00263 /// If during the disambiguation process a parsing error is encountered, 00264 /// the function returns true to let the declaration parsing code handle it. 00265 /// 00266 /// condition: 00267 /// expression 00268 /// type-specifier-seq declarator '=' assignment-expression 00269 /// [C++11] type-specifier-seq declarator '=' initializer-clause 00270 /// [C++11] type-specifier-seq declarator braced-init-list 00271 /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt] 00272 /// '=' assignment-expression 00273 /// 00274 bool Parser::isCXXConditionDeclaration() { 00275 TPResult TPR = isCXXDeclarationSpecifier(); 00276 if (TPR != TPResult::Ambiguous()) 00277 return TPR != TPResult::False(); // Returns true for TPResult::True() or 00278 // TPResult::Error(). 00279 00280 // FIXME: Add statistics about the number of ambiguous statements encountered 00281 // and how they were resolved (number of declarations+number of expressions). 00282 00283 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('. 00284 // We need tentative parsing... 00285 00286 TentativeParsingAction PA(*this); 00287 00288 // type-specifier-seq 00289 if (Tok.is(tok::kw_typeof)) 00290 TryParseTypeofSpecifier(); 00291 else { 00292 ConsumeToken(); 00293 00294 if (getLangOpts().ObjC1 && Tok.is(tok::less)) 00295 TryParseProtocolQualifiers(); 00296 } 00297 assert(Tok.is(tok::l_paren) && "Expected '('"); 00298 00299 // declarator 00300 TPR = TryParseDeclarator(false/*mayBeAbstract*/); 00301 00302 // In case of an error, let the declaration parsing code handle it. 00303 if (TPR == TPResult::Error()) 00304 TPR = TPResult::True(); 00305 00306 if (TPR == TPResult::Ambiguous()) { 00307 // '=' 00308 // [GNU] simple-asm-expr[opt] attributes[opt] 00309 if (Tok.is(tok::equal) || 00310 Tok.is(tok::kw_asm) || Tok.is(tok::kw___attribute)) 00311 TPR = TPResult::True(); 00312 else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) 00313 TPR = TPResult::True(); 00314 else 00315 TPR = TPResult::False(); 00316 } 00317 00318 PA.Revert(); 00319 00320 assert(TPR == TPResult::True() || TPR == TPResult::False()); 00321 return TPR == TPResult::True(); 00322 } 00323 00324 /// \brief Determine whether the next set of tokens contains a type-id. 00325 /// 00326 /// The context parameter states what context we're parsing right 00327 /// now, which affects how this routine copes with the token 00328 /// following the type-id. If the context is TypeIdInParens, we have 00329 /// already parsed the '(' and we will cease lookahead when we hit 00330 /// the corresponding ')'. If the context is 00331 /// TypeIdAsTemplateArgument, we've already parsed the '<' or ',' 00332 /// before this template argument, and will cease lookahead when we 00333 /// hit a '>', '>>' (in C++0x), or ','. Returns true for a type-id 00334 /// and false for an expression. If during the disambiguation 00335 /// process a parsing error is encountered, the function returns 00336 /// true to let the declaration parsing code handle it. 00337 /// 00338 /// type-id: 00339 /// type-specifier-seq abstract-declarator[opt] 00340 /// 00341 bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) { 00342 00343 isAmbiguous = false; 00344 00345 // C++ 8.2p2: 00346 // The ambiguity arising from the similarity between a function-style cast and 00347 // a type-id can occur in different contexts. The ambiguity appears as a 00348 // choice between a function-style cast expression and a declaration of a 00349 // type. The resolution is that any construct that could possibly be a type-id 00350 // in its syntactic context shall be considered a type-id. 00351 00352 TPResult TPR = isCXXDeclarationSpecifier(); 00353 if (TPR != TPResult::Ambiguous()) 00354 return TPR != TPResult::False(); // Returns true for TPResult::True() or 00355 // TPResult::Error(). 00356 00357 // FIXME: Add statistics about the number of ambiguous statements encountered 00358 // and how they were resolved (number of declarations+number of expressions). 00359 00360 // Ok, we have a simple-type-specifier/typename-specifier followed by a '('. 00361 // We need tentative parsing... 00362 00363 TentativeParsingAction PA(*this); 00364 00365 // type-specifier-seq 00366 if (Tok.is(tok::kw_typeof)) 00367 TryParseTypeofSpecifier(); 00368 else { 00369 ConsumeToken(); 00370 00371 if (getLangOpts().ObjC1 && Tok.is(tok::less)) 00372 TryParseProtocolQualifiers(); 00373 } 00374 00375 assert(Tok.is(tok::l_paren) && "Expected '('"); 00376 00377 // declarator 00378 TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/); 00379 00380 // In case of an error, let the declaration parsing code handle it. 00381 if (TPR == TPResult::Error()) 00382 TPR = TPResult::True(); 00383 00384 if (TPR == TPResult::Ambiguous()) { 00385 // We are supposed to be inside parens, so if after the abstract declarator 00386 // we encounter a ')' this is a type-id, otherwise it's an expression. 00387 if (Context == TypeIdInParens && Tok.is(tok::r_paren)) { 00388 TPR = TPResult::True(); 00389 isAmbiguous = true; 00390 00391 // We are supposed to be inside a template argument, so if after 00392 // the abstract declarator we encounter a '>', '>>' (in C++0x), or 00393 // ',', this is a type-id. Otherwise, it's an expression. 00394 } else if (Context == TypeIdAsTemplateArgument && 00395 (Tok.is(tok::greater) || Tok.is(tok::comma) || 00396 (getLangOpts().CPlusPlus11 && Tok.is(tok::greatergreater)))) { 00397 TPR = TPResult::True(); 00398 isAmbiguous = true; 00399 00400 } else 00401 TPR = TPResult::False(); 00402 } 00403 00404 PA.Revert(); 00405 00406 assert(TPR == TPResult::True() || TPR == TPResult::False()); 00407 return TPR == TPResult::True(); 00408 } 00409 00410 /// \brief Returns true if this is a C++11 attribute-specifier. Per 00411 /// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens 00412 /// always introduce an attribute. In Objective-C++11, this rule does not 00413 /// apply if either '[' begins a message-send. 00414 /// 00415 /// If Disambiguate is true, we try harder to determine whether a '[[' starts 00416 /// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not. 00417 /// 00418 /// If OuterMightBeMessageSend is true, we assume the outer '[' is either an 00419 /// Obj-C message send or the start of an attribute. Otherwise, we assume it 00420 /// is not an Obj-C message send. 00421 /// 00422 /// C++11 [dcl.attr.grammar]: 00423 /// 00424 /// attribute-specifier: 00425 /// '[' '[' attribute-list ']' ']' 00426 /// alignment-specifier 00427 /// 00428 /// attribute-list: 00429 /// attribute[opt] 00430 /// attribute-list ',' attribute[opt] 00431 /// attribute '...' 00432 /// attribute-list ',' attribute '...' 00433 /// 00434 /// attribute: 00435 /// attribute-token attribute-argument-clause[opt] 00436 /// 00437 /// attribute-token: 00438 /// identifier 00439 /// identifier '::' identifier 00440 /// 00441 /// attribute-argument-clause: 00442 /// '(' balanced-token-seq ')' 00443 Parser::CXX11AttributeKind 00444 Parser::isCXX11AttributeSpecifier(bool Disambiguate, 00445 bool OuterMightBeMessageSend) { 00446 if (Tok.is(tok::kw_alignas)) 00447 return CAK_AttributeSpecifier; 00448 00449 if (Tok.isNot(tok::l_square) || NextToken().isNot(tok::l_square)) 00450 return CAK_NotAttributeSpecifier; 00451 00452 // No tentative parsing if we don't need to look for ']]' or a lambda. 00453 if (!Disambiguate && !getLangOpts().ObjC1) 00454 return CAK_AttributeSpecifier; 00455 00456 TentativeParsingAction PA(*this); 00457 00458 // Opening brackets were checked for above. 00459 ConsumeBracket(); 00460 00461 // Outside Obj-C++11, treat anything with a matching ']]' as an attribute. 00462 if (!getLangOpts().ObjC1) { 00463 ConsumeBracket(); 00464 00465 bool IsAttribute = SkipUntil(tok::r_square, false); 00466 IsAttribute &= Tok.is(tok::r_square); 00467 00468 PA.Revert(); 00469 00470 return IsAttribute ? CAK_AttributeSpecifier : CAK_InvalidAttributeSpecifier; 00471 } 00472 00473 // In Obj-C++11, we need to distinguish four situations: 00474 // 1a) int x[[attr]]; C++11 attribute. 00475 // 1b) [[attr]]; C++11 statement attribute. 00476 // 2) int x[[obj](){ return 1; }()]; Lambda in array size/index. 00477 // 3a) int x[[obj get]]; Message send in array size/index. 00478 // 3b) [[Class alloc] init]; Message send in message send. 00479 // 4) [[obj]{ return self; }() doStuff]; Lambda in message send. 00480 // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted. 00481 00482 // If we have a lambda-introducer, then this is definitely not a message send. 00483 // FIXME: If this disambiguation is too slow, fold the tentative lambda parse 00484 // into the tentative attribute parse below. 00485 LambdaIntroducer Intro; 00486 if (!TryParseLambdaIntroducer(Intro)) { 00487 // A lambda cannot end with ']]', and an attribute must. 00488 bool IsAttribute = Tok.is(tok::r_square); 00489 00490 PA.Revert(); 00491 00492 if (IsAttribute) 00493 // Case 1: C++11 attribute. 00494 return CAK_AttributeSpecifier; 00495 00496 if (OuterMightBeMessageSend) 00497 // Case 4: Lambda in message send. 00498 return CAK_NotAttributeSpecifier; 00499 00500 // Case 2: Lambda in array size / index. 00501 return CAK_InvalidAttributeSpecifier; 00502 } 00503 00504 ConsumeBracket(); 00505 00506 // If we don't have a lambda-introducer, then we have an attribute or a 00507 // message-send. 00508 bool IsAttribute = true; 00509 while (Tok.isNot(tok::r_square)) { 00510 if (Tok.is(tok::comma)) { 00511 // Case 1: Stray commas can only occur in attributes. 00512 PA.Revert(); 00513 return CAK_AttributeSpecifier; 00514 } 00515 00516 // Parse the attribute-token, if present. 00517 // C++11 [dcl.attr.grammar]: 00518 // If a keyword or an alternative token that satisfies the syntactic 00519 // requirements of an identifier is contained in an attribute-token, 00520 // it is considered an identifier. 00521 SourceLocation Loc; 00522 if (!TryParseCXX11AttributeIdentifier(Loc)) { 00523 IsAttribute = false; 00524 break; 00525 } 00526 if (Tok.is(tok::coloncolon)) { 00527 ConsumeToken(); 00528 if (!TryParseCXX11AttributeIdentifier(Loc)) { 00529 IsAttribute = false; 00530 break; 00531 } 00532 } 00533 00534 // Parse the attribute-argument-clause, if present. 00535 if (Tok.is(tok::l_paren)) { 00536 ConsumeParen(); 00537 if (!SkipUntil(tok::r_paren, false)) { 00538 IsAttribute = false; 00539 break; 00540 } 00541 } 00542 00543 if (Tok.is(tok::ellipsis)) 00544 ConsumeToken(); 00545 00546 if (Tok.isNot(tok::comma)) 00547 break; 00548 00549 ConsumeToken(); 00550 } 00551 00552 // An attribute must end ']]'. 00553 if (IsAttribute) { 00554 if (Tok.is(tok::r_square)) { 00555 ConsumeBracket(); 00556 IsAttribute = Tok.is(tok::r_square); 00557 } else { 00558 IsAttribute = false; 00559 } 00560 } 00561 00562 PA.Revert(); 00563 00564 if (IsAttribute) 00565 // Case 1: C++11 statement attribute. 00566 return CAK_AttributeSpecifier; 00567 00568 // Case 3: Message send. 00569 return CAK_NotAttributeSpecifier; 00570 } 00571 00572 /// declarator: 00573 /// direct-declarator 00574 /// ptr-operator declarator 00575 /// 00576 /// direct-declarator: 00577 /// declarator-id 00578 /// direct-declarator '(' parameter-declaration-clause ')' 00579 /// cv-qualifier-seq[opt] exception-specification[opt] 00580 /// direct-declarator '[' constant-expression[opt] ']' 00581 /// '(' declarator ')' 00582 /// [GNU] '(' attributes declarator ')' 00583 /// 00584 /// abstract-declarator: 00585 /// ptr-operator abstract-declarator[opt] 00586 /// direct-abstract-declarator 00587 /// ... 00588 /// 00589 /// direct-abstract-declarator: 00590 /// direct-abstract-declarator[opt] 00591 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] 00592 /// exception-specification[opt] 00593 /// direct-abstract-declarator[opt] '[' constant-expression[opt] ']' 00594 /// '(' abstract-declarator ')' 00595 /// 00596 /// ptr-operator: 00597 /// '*' cv-qualifier-seq[opt] 00598 /// '&' 00599 /// [C++0x] '&&' [TODO] 00600 /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] 00601 /// 00602 /// cv-qualifier-seq: 00603 /// cv-qualifier cv-qualifier-seq[opt] 00604 /// 00605 /// cv-qualifier: 00606 /// 'const' 00607 /// 'volatile' 00608 /// 00609 /// declarator-id: 00610 /// '...'[opt] id-expression 00611 /// 00612 /// id-expression: 00613 /// unqualified-id 00614 /// qualified-id [TODO] 00615 /// 00616 /// unqualified-id: 00617 /// identifier 00618 /// operator-function-id [TODO] 00619 /// conversion-function-id [TODO] 00620 /// '~' class-name [TODO] 00621 /// template-id [TODO] 00622 /// 00623 Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract, 00624 bool mayHaveIdentifier) { 00625 // declarator: 00626 // direct-declarator 00627 // ptr-operator declarator 00628 00629 while (1) { 00630 if (Tok.is(tok::coloncolon) || Tok.is(tok::identifier)) 00631 if (TryAnnotateCXXScopeToken(true)) 00632 return TPResult::Error(); 00633 00634 if (Tok.is(tok::star) || Tok.is(tok::amp) || Tok.is(tok::caret) || 00635 Tok.is(tok::ampamp) || 00636 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::star))) { 00637 // ptr-operator 00638 ConsumeToken(); 00639 while (Tok.is(tok::kw_const) || 00640 Tok.is(tok::kw_volatile) || 00641 Tok.is(tok::kw_restrict)) 00642 ConsumeToken(); 00643 } else { 00644 break; 00645 } 00646 } 00647 00648 // direct-declarator: 00649 // direct-abstract-declarator: 00650 if (Tok.is(tok::ellipsis)) 00651 ConsumeToken(); 00652 00653 if ((Tok.is(tok::identifier) || 00654 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) && 00655 mayHaveIdentifier) { 00656 // declarator-id 00657 if (Tok.is(tok::annot_cxxscope)) 00658 ConsumeToken(); 00659 else 00660 TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo()); 00661 ConsumeToken(); 00662 } else if (Tok.is(tok::l_paren)) { 00663 ConsumeParen(); 00664 if (mayBeAbstract && 00665 (Tok.is(tok::r_paren) || // 'int()' is a function. 00666 // 'int(...)' is a function. 00667 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren)) || 00668 isDeclarationSpecifier())) { // 'int(int)' is a function. 00669 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] 00670 // exception-specification[opt] 00671 TPResult TPR = TryParseFunctionDeclarator(); 00672 if (TPR != TPResult::Ambiguous()) 00673 return TPR; 00674 } else { 00675 // '(' declarator ')' 00676 // '(' attributes declarator ')' 00677 // '(' abstract-declarator ')' 00678 if (Tok.is(tok::kw___attribute) || 00679 Tok.is(tok::kw___declspec) || 00680 Tok.is(tok::kw___cdecl) || 00681 Tok.is(tok::kw___stdcall) || 00682 Tok.is(tok::kw___fastcall) || 00683 Tok.is(tok::kw___thiscall) || 00684 Tok.is(tok::kw___unaligned)) 00685 return TPResult::True(); // attributes indicate declaration 00686 TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier); 00687 if (TPR != TPResult::Ambiguous()) 00688 return TPR; 00689 if (Tok.isNot(tok::r_paren)) 00690 return TPResult::False(); 00691 ConsumeParen(); 00692 } 00693 } else if (!mayBeAbstract) { 00694 return TPResult::False(); 00695 } 00696 00697 while (1) { 00698 TPResult TPR(TPResult::Ambiguous()); 00699 00700 // abstract-declarator: ... 00701 if (Tok.is(tok::ellipsis)) 00702 ConsumeToken(); 00703 00704 if (Tok.is(tok::l_paren)) { 00705 // Check whether we have a function declarator or a possible ctor-style 00706 // initializer that follows the declarator. Note that ctor-style 00707 // initializers are not possible in contexts where abstract declarators 00708 // are allowed. 00709 if (!mayBeAbstract && !isCXXFunctionDeclarator()) 00710 break; 00711 00712 // direct-declarator '(' parameter-declaration-clause ')' 00713 // cv-qualifier-seq[opt] exception-specification[opt] 00714 ConsumeParen(); 00715 TPR = TryParseFunctionDeclarator(); 00716 } else if (Tok.is(tok::l_square)) { 00717 // direct-declarator '[' constant-expression[opt] ']' 00718 // direct-abstract-declarator[opt] '[' constant-expression[opt] ']' 00719 TPR = TryParseBracketDeclarator(); 00720 } else { 00721 break; 00722 } 00723 00724 if (TPR != TPResult::Ambiguous()) 00725 return TPR; 00726 } 00727 00728 return TPResult::Ambiguous(); 00729 } 00730 00731 Parser::TPResult 00732 Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) { 00733 switch (Kind) { 00734 // Obviously starts an expression. 00735 case tok::numeric_constant: 00736 case tok::char_constant: 00737 case tok::wide_char_constant: 00738 case tok::utf16_char_constant: 00739 case tok::utf32_char_constant: 00740 case tok::string_literal: 00741 case tok::wide_string_literal: 00742 case tok::utf8_string_literal: 00743 case tok::utf16_string_literal: 00744 case tok::utf32_string_literal: 00745 case tok::l_square: 00746 case tok::l_paren: 00747 case tok::amp: 00748 case tok::ampamp: 00749 case tok::star: 00750 case tok::plus: 00751 case tok::plusplus: 00752 case tok::minus: 00753 case tok::minusminus: 00754 case tok::tilde: 00755 case tok::exclaim: 00756 case tok::kw_sizeof: 00757 case tok::kw___func__: 00758 case tok::kw_const_cast: 00759 case tok::kw_delete: 00760 case tok::kw_dynamic_cast: 00761 case tok::kw_false: 00762 case tok::kw_new: 00763 case tok::kw_operator: 00764 case tok::kw_reinterpret_cast: 00765 case tok::kw_static_cast: 00766 case tok::kw_this: 00767 case tok::kw_throw: 00768 case tok::kw_true: 00769 case tok::kw_typeid: 00770 case tok::kw_alignof: 00771 case tok::kw_noexcept: 00772 case tok::kw_nullptr: 00773 case tok::kw__Alignof: 00774 case tok::kw___null: 00775 case tok::kw___alignof: 00776 case tok::kw___builtin_choose_expr: 00777 case tok::kw___builtin_offsetof: 00778 case tok::kw___builtin_types_compatible_p: 00779 case tok::kw___builtin_va_arg: 00780 case tok::kw___imag: 00781 case tok::kw___real: 00782 case tok::kw___FUNCTION__: 00783 case tok::kw_L__FUNCTION__: 00784 case tok::kw___PRETTY_FUNCTION__: 00785 case tok::kw___has_nothrow_assign: 00786 case tok::kw___has_nothrow_copy: 00787 case tok::kw___has_nothrow_constructor: 00788 case tok::kw___has_trivial_assign: 00789 case tok::kw___has_trivial_copy: 00790 case tok::kw___has_trivial_constructor: 00791 case tok::kw___has_trivial_destructor: 00792 case tok::kw___has_virtual_destructor: 00793 case tok::kw___is_abstract: 00794 case tok::kw___is_base_of: 00795 case tok::kw___is_class: 00796 case tok::kw___is_convertible_to: 00797 case tok::kw___is_empty: 00798 case tok::kw___is_enum: 00799 case tok::kw___is_interface_class: 00800 case tok::kw___is_final: 00801 case tok::kw___is_literal: 00802 case tok::kw___is_literal_type: 00803 case tok::kw___is_pod: 00804 case tok::kw___is_polymorphic: 00805 case tok::kw___is_trivial: 00806 case tok::kw___is_trivially_assignable: 00807 case tok::kw___is_trivially_constructible: 00808 case tok::kw___is_trivially_copyable: 00809 case tok::kw___is_union: 00810 case tok::kw___uuidof: 00811 return TPResult::True(); 00812 00813 // Obviously starts a type-specifier-seq: 00814 case tok::kw_char: 00815 case tok::kw_const: 00816 case tok::kw_double: 00817 case tok::kw_enum: 00818 case tok::kw_half: 00819 case tok::kw_float: 00820 case tok::kw_int: 00821 case tok::kw_long: 00822 case tok::kw___int64: 00823 case tok::kw___int128: 00824 case tok::kw_restrict: 00825 case tok::kw_short: 00826 case tok::kw_signed: 00827 case tok::kw_struct: 00828 case tok::kw_union: 00829 case tok::kw_unsigned: 00830 case tok::kw_void: 00831 case tok::kw_volatile: 00832 case tok::kw__Bool: 00833 case tok::kw__Complex: 00834 case tok::kw_class: 00835 case tok::kw_typename: 00836 case tok::kw_wchar_t: 00837 case tok::kw_char16_t: 00838 case tok::kw_char32_t: 00839 case tok::kw___underlying_type: 00840 case tok::kw__Decimal32: 00841 case tok::kw__Decimal64: 00842 case tok::kw__Decimal128: 00843 case tok::kw___thread: 00844 case tok::kw_thread_local: 00845 case tok::kw__Thread_local: 00846 case tok::kw_typeof: 00847 case tok::kw___cdecl: 00848 case tok::kw___stdcall: 00849 case tok::kw___fastcall: 00850 case tok::kw___thiscall: 00851 case tok::kw___unaligned: 00852 case tok::kw___vector: 00853 case tok::kw___pixel: 00854 case tok::kw__Atomic: 00855 case tok::kw_image1d_t: 00856 case tok::kw_image1d_array_t: 00857 case tok::kw_image1d_buffer_t: 00858 case tok::kw_image2d_t: 00859 case tok::kw_image2d_array_t: 00860 case tok::kw_image3d_t: 00861 case tok::kw_sampler_t: 00862 case tok::kw_event_t: 00863 case tok::kw___unknown_anytype: 00864 return TPResult::False(); 00865 00866 default: 00867 break; 00868 } 00869 00870 return TPResult::Ambiguous(); 00871 } 00872 00873 bool Parser::isTentativelyDeclared(IdentifierInfo *II) { 00874 return std::find(TentativelyDeclaredIdentifiers.begin(), 00875 TentativelyDeclaredIdentifiers.end(), II) 00876 != TentativelyDeclaredIdentifiers.end(); 00877 } 00878 00879 /// isCXXDeclarationSpecifier - Returns TPResult::True() if it is a declaration 00880 /// specifier, TPResult::False() if it is not, TPResult::Ambiguous() if it could 00881 /// be either a decl-specifier or a function-style cast, and TPResult::Error() 00882 /// if a parsing error was found and reported. 00883 /// 00884 /// If HasMissingTypename is provided, a name with a dependent scope specifier 00885 /// will be treated as ambiguous if the 'typename' keyword is missing. If this 00886 /// happens, *HasMissingTypename will be set to 'true'. This will also be used 00887 /// as an indicator that undeclared identifiers (which will trigger a later 00888 /// parse error) should be treated as types. Returns TPResult::Ambiguous() in 00889 /// such cases. 00890 /// 00891 /// decl-specifier: 00892 /// storage-class-specifier 00893 /// type-specifier 00894 /// function-specifier 00895 /// 'friend' 00896 /// 'typedef' 00897 /// [C++11] 'constexpr' 00898 /// [GNU] attributes declaration-specifiers[opt] 00899 /// 00900 /// storage-class-specifier: 00901 /// 'register' 00902 /// 'static' 00903 /// 'extern' 00904 /// 'mutable' 00905 /// 'auto' 00906 /// [GNU] '__thread' 00907 /// [C++11] 'thread_local' 00908 /// [C11] '_Thread_local' 00909 /// 00910 /// function-specifier: 00911 /// 'inline' 00912 /// 'virtual' 00913 /// 'explicit' 00914 /// 00915 /// typedef-name: 00916 /// identifier 00917 /// 00918 /// type-specifier: 00919 /// simple-type-specifier 00920 /// class-specifier 00921 /// enum-specifier 00922 /// elaborated-type-specifier 00923 /// typename-specifier 00924 /// cv-qualifier 00925 /// 00926 /// simple-type-specifier: 00927 /// '::'[opt] nested-name-specifier[opt] type-name 00928 /// '::'[opt] nested-name-specifier 'template' 00929 /// simple-template-id [TODO] 00930 /// 'char' 00931 /// 'wchar_t' 00932 /// 'bool' 00933 /// 'short' 00934 /// 'int' 00935 /// 'long' 00936 /// 'signed' 00937 /// 'unsigned' 00938 /// 'float' 00939 /// 'double' 00940 /// 'void' 00941 /// [GNU] typeof-specifier 00942 /// [GNU] '_Complex' 00943 /// [C++11] 'auto' 00944 /// [C++11] 'decltype' ( expression ) 00945 /// [C++1y] 'decltype' ( 'auto' ) 00946 /// 00947 /// type-name: 00948 /// class-name 00949 /// enum-name 00950 /// typedef-name 00951 /// 00952 /// elaborated-type-specifier: 00953 /// class-key '::'[opt] nested-name-specifier[opt] identifier 00954 /// class-key '::'[opt] nested-name-specifier[opt] 'template'[opt] 00955 /// simple-template-id 00956 /// 'enum' '::'[opt] nested-name-specifier[opt] identifier 00957 /// 00958 /// enum-name: 00959 /// identifier 00960 /// 00961 /// enum-specifier: 00962 /// 'enum' identifier[opt] '{' enumerator-list[opt] '}' 00963 /// 'enum' identifier[opt] '{' enumerator-list ',' '}' 00964 /// 00965 /// class-specifier: 00966 /// class-head '{' member-specification[opt] '}' 00967 /// 00968 /// class-head: 00969 /// class-key identifier[opt] base-clause[opt] 00970 /// class-key nested-name-specifier identifier base-clause[opt] 00971 /// class-key nested-name-specifier[opt] simple-template-id 00972 /// base-clause[opt] 00973 /// 00974 /// class-key: 00975 /// 'class' 00976 /// 'struct' 00977 /// 'union' 00978 /// 00979 /// cv-qualifier: 00980 /// 'const' 00981 /// 'volatile' 00982 /// [GNU] restrict 00983 /// 00984 Parser::TPResult 00985 Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult, 00986 bool *HasMissingTypename) { 00987 switch (Tok.getKind()) { 00988 case tok::identifier: { 00989 // Check for need to substitute AltiVec __vector keyword 00990 // for "vector" identifier. 00991 if (TryAltiVecVectorToken()) 00992 return TPResult::True(); 00993 00994 const Token &Next = NextToken(); 00995 // In 'foo bar', 'foo' is always a type name outside of Objective-C. 00996 if (!getLangOpts().ObjC1 && Next.is(tok::identifier)) 00997 return TPResult::True(); 00998 00999 if (Next.isNot(tok::coloncolon) && Next.isNot(tok::less)) { 01000 // Determine whether this is a valid expression. If not, we will hit 01001 // a parse error one way or another. In that case, tell the caller that 01002 // this is ambiguous. Typo-correct to type and expression keywords and 01003 // to types and identifiers, in order to try to recover from errors. 01004 CorrectionCandidateCallback TypoCorrection; 01005 TypoCorrection.WantRemainingKeywords = false; 01006 TypoCorrection.WantTypeSpecifiers = Next.isNot(tok::arrow); 01007 switch (TryAnnotateName(false /* no nested name specifier */, 01008 &TypoCorrection)) { 01009 case ANK_Error: 01010 return TPResult::Error(); 01011 case ANK_TentativeDecl: 01012 return TPResult::False(); 01013 case ANK_TemplateName: 01014 // A bare type template-name which can't be a template template 01015 // argument is an error, and was probably intended to be a type. 01016 return GreaterThanIsOperator ? TPResult::True() : TPResult::False(); 01017 case ANK_Unresolved: 01018 return HasMissingTypename ? TPResult::Ambiguous() : TPResult::False(); 01019 case ANK_Success: 01020 break; 01021 } 01022 assert(Tok.isNot(tok::identifier) && 01023 "TryAnnotateName succeeded without producing an annotation"); 01024 } else { 01025 // This might possibly be a type with a dependent scope specifier and 01026 // a missing 'typename' keyword. Don't use TryAnnotateName in this case, 01027 // since it will annotate as a primary expression, and we want to use the 01028 // "missing 'typename'" logic. 01029 if (TryAnnotateTypeOrScopeToken()) 01030 return TPResult::Error(); 01031 // If annotation failed, assume it's a non-type. 01032 // FIXME: If this happens due to an undeclared identifier, treat it as 01033 // ambiguous. 01034 if (Tok.is(tok::identifier)) 01035 return TPResult::False(); 01036 } 01037 01038 // We annotated this token as something. Recurse to handle whatever we got. 01039 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename); 01040 } 01041 01042 case tok::kw_typename: // typename T::type 01043 // Annotate typenames and C++ scope specifiers. If we get one, just 01044 // recurse to handle whatever we get. 01045 if (TryAnnotateTypeOrScopeToken()) 01046 return TPResult::Error(); 01047 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename); 01048 01049 case tok::coloncolon: { // ::foo::bar 01050 const Token &Next = NextToken(); 01051 if (Next.is(tok::kw_new) || // ::new 01052 Next.is(tok::kw_delete)) // ::delete 01053 return TPResult::False(); 01054 } 01055 // Fall through. 01056 case tok::kw_decltype: 01057 // Annotate typenames and C++ scope specifiers. If we get one, just 01058 // recurse to handle whatever we get. 01059 if (TryAnnotateTypeOrScopeToken()) 01060 return TPResult::Error(); 01061 return isCXXDeclarationSpecifier(BracedCastResult, HasMissingTypename); 01062 01063 // decl-specifier: 01064 // storage-class-specifier 01065 // type-specifier 01066 // function-specifier 01067 // 'friend' 01068 // 'typedef' 01069 // 'constexpr' 01070 case tok::kw_friend: 01071 case tok::kw_typedef: 01072 case tok::kw_constexpr: 01073 // storage-class-specifier 01074 case tok::kw_register: 01075 case tok::kw_static: 01076 case tok::kw_extern: 01077 case tok::kw_mutable: 01078 case tok::kw_auto: 01079 case tok::kw___thread: 01080 case tok::kw_thread_local: 01081 case tok::kw__Thread_local: 01082 // function-specifier 01083 case tok::kw_inline: 01084 case tok::kw_virtual: 01085 case tok::kw_explicit: 01086 01087 // Modules 01088 case tok::kw___module_private__: 01089 01090 // Debugger support 01091 case tok::kw___unknown_anytype: 01092 01093 // type-specifier: 01094 // simple-type-specifier 01095 // class-specifier 01096 // enum-specifier 01097 // elaborated-type-specifier 01098 // typename-specifier 01099 // cv-qualifier 01100 01101 // class-specifier 01102 // elaborated-type-specifier 01103 case tok::kw_class: 01104 case tok::kw_struct: 01105 case tok::kw_union: 01106 // enum-specifier 01107 case tok::kw_enum: 01108 // cv-qualifier 01109 case tok::kw_const: 01110 case tok::kw_volatile: 01111 01112 // GNU 01113 case tok::kw_restrict: 01114 case tok::kw__Complex: 01115 case tok::kw___attribute: 01116 return TPResult::True(); 01117 01118 // Microsoft 01119 case tok::kw___declspec: 01120 case tok::kw___cdecl: 01121 case tok::kw___stdcall: 01122 case tok::kw___fastcall: 01123 case tok::kw___thiscall: 01124 case tok::kw___w64: 01125 case tok::kw___ptr64: 01126 case tok::kw___ptr32: 01127 case tok::kw___forceinline: 01128 case tok::kw___unaligned: 01129 return TPResult::True(); 01130 01131 // Borland 01132 case tok::kw___pascal: 01133 return TPResult::True(); 01134 01135 // AltiVec 01136 case tok::kw___vector: 01137 return TPResult::True(); 01138 01139 case tok::annot_template_id: { 01140 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 01141 if (TemplateId->Kind != TNK_Type_template) 01142 return TPResult::False(); 01143 CXXScopeSpec SS; 01144 AnnotateTemplateIdTokenAsType(); 01145 assert(Tok.is(tok::annot_typename)); 01146 goto case_typename; 01147 } 01148 01149 case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed 01150 // We've already annotated a scope; try to annotate a type. 01151 if (TryAnnotateTypeOrScopeToken()) 01152 return TPResult::Error(); 01153 if (!Tok.is(tok::annot_typename)) { 01154 // If the next token is an identifier or a type qualifier, then this 01155 // can't possibly be a valid expression either. 01156 if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) { 01157 CXXScopeSpec SS; 01158 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), 01159 Tok.getAnnotationRange(), 01160 SS); 01161 if (SS.getScopeRep() && SS.getScopeRep()->isDependent()) { 01162 TentativeParsingAction PA(*this); 01163 ConsumeToken(); 01164 ConsumeToken(); 01165 bool isIdentifier = Tok.is(tok::identifier); 01166 TPResult TPR = TPResult::False(); 01167 if (!isIdentifier) 01168 TPR = isCXXDeclarationSpecifier(BracedCastResult, 01169 HasMissingTypename); 01170 PA.Revert(); 01171 01172 if (isIdentifier || 01173 TPR == TPResult::True() || TPR == TPResult::Error()) 01174 return TPResult::Error(); 01175 01176 if (HasMissingTypename) { 01177 // We can't tell whether this is a missing 'typename' or a valid 01178 // expression. 01179 *HasMissingTypename = true; 01180 return TPResult::Ambiguous(); 01181 } 01182 } else { 01183 // Try to resolve the name. If it doesn't exist, assume it was 01184 // intended to name a type and keep disambiguating. 01185 switch (TryAnnotateName(false /* SS is not dependent */)) { 01186 case ANK_Error: 01187 return TPResult::Error(); 01188 case ANK_TentativeDecl: 01189 return TPResult::False(); 01190 case ANK_TemplateName: 01191 // A bare type template-name which can't be a template template 01192 // argument is an error, and was probably intended to be a type. 01193 return GreaterThanIsOperator ? TPResult::True() : TPResult::False(); 01194 case ANK_Unresolved: 01195 return HasMissingTypename ? TPResult::Ambiguous() 01196 : TPResult::False(); 01197 case ANK_Success: 01198 // Annotated it, check again. 01199 assert(Tok.isNot(tok::annot_cxxscope) || 01200 NextToken().isNot(tok::identifier)); 01201 return isCXXDeclarationSpecifier(BracedCastResult, 01202 HasMissingTypename); 01203 } 01204 } 01205 } 01206 return TPResult::False(); 01207 } 01208 // If that succeeded, fallthrough into the generic simple-type-id case. 01209 01210 // The ambiguity resides in a simple-type-specifier/typename-specifier 01211 // followed by a '('. The '(' could either be the start of: 01212 // 01213 // direct-declarator: 01214 // '(' declarator ')' 01215 // 01216 // direct-abstract-declarator: 01217 // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] 01218 // exception-specification[opt] 01219 // '(' abstract-declarator ')' 01220 // 01221 // or part of a function-style cast expression: 01222 // 01223 // simple-type-specifier '(' expression-list[opt] ')' 01224 // 01225 01226 // simple-type-specifier: 01227 01228 case tok::annot_typename: 01229 case_typename: 01230 // In Objective-C, we might have a protocol-qualified type. 01231 if (getLangOpts().ObjC1 && NextToken().is(tok::less)) { 01232 // Tentatively parse the 01233 TentativeParsingAction PA(*this); 01234 ConsumeToken(); // The type token 01235 01236 TPResult TPR = TryParseProtocolQualifiers(); 01237 bool isFollowedByParen = Tok.is(tok::l_paren); 01238 bool isFollowedByBrace = Tok.is(tok::l_brace); 01239 01240 PA.Revert(); 01241 01242 if (TPR == TPResult::Error()) 01243 return TPResult::Error(); 01244 01245 if (isFollowedByParen) 01246 return TPResult::Ambiguous(); 01247 01248 if (getLangOpts().CPlusPlus11 && isFollowedByBrace) 01249 return BracedCastResult; 01250 01251 return TPResult::True(); 01252 } 01253 01254 case tok::kw_char: 01255 case tok::kw_wchar_t: 01256 case tok::kw_char16_t: 01257 case tok::kw_char32_t: 01258 case tok::kw_bool: 01259 case tok::kw_short: 01260 case tok::kw_int: 01261 case tok::kw_long: 01262 case tok::kw___int64: 01263 case tok::kw___int128: 01264 case tok::kw_signed: 01265 case tok::kw_unsigned: 01266 case tok::kw_half: 01267 case tok::kw_float: 01268 case tok::kw_double: 01269 case tok::kw_void: 01270 case tok::annot_decltype: 01271 if (NextToken().is(tok::l_paren)) 01272 return TPResult::Ambiguous(); 01273 01274 // This is a function-style cast in all cases we disambiguate other than 01275 // one: 01276 // struct S { 01277 // enum E : int { a = 4 }; // enum 01278 // enum E : int { 4 }; // bit-field 01279 // }; 01280 if (getLangOpts().CPlusPlus11 && NextToken().is(tok::l_brace)) 01281 return BracedCastResult; 01282 01283 if (isStartOfObjCClassMessageMissingOpenBracket()) 01284 return TPResult::False(); 01285 01286 return TPResult::True(); 01287 01288 // GNU typeof support. 01289 case tok::kw_typeof: { 01290 if (NextToken().isNot(tok::l_paren)) 01291 return TPResult::True(); 01292 01293 TentativeParsingAction PA(*this); 01294 01295 TPResult TPR = TryParseTypeofSpecifier(); 01296 bool isFollowedByParen = Tok.is(tok::l_paren); 01297 bool isFollowedByBrace = Tok.is(tok::l_brace); 01298 01299 PA.Revert(); 01300 01301 if (TPR == TPResult::Error()) 01302 return TPResult::Error(); 01303 01304 if (isFollowedByParen) 01305 return TPResult::Ambiguous(); 01306 01307 if (getLangOpts().CPlusPlus11 && isFollowedByBrace) 01308 return BracedCastResult; 01309 01310 return TPResult::True(); 01311 } 01312 01313 // C++0x type traits support 01314 case tok::kw___underlying_type: 01315 return TPResult::True(); 01316 01317 // C11 _Atomic 01318 case tok::kw__Atomic: 01319 return TPResult::True(); 01320 01321 default: 01322 return TPResult::False(); 01323 } 01324 } 01325 01326 /// [GNU] typeof-specifier: 01327 /// 'typeof' '(' expressions ')' 01328 /// 'typeof' '(' type-name ')' 01329 /// 01330 Parser::TPResult Parser::TryParseTypeofSpecifier() { 01331 assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!"); 01332 ConsumeToken(); 01333 01334 assert(Tok.is(tok::l_paren) && "Expected '('"); 01335 // Parse through the parens after 'typeof'. 01336 ConsumeParen(); 01337 if (!SkipUntil(tok::r_paren)) 01338 return TPResult::Error(); 01339 01340 return TPResult::Ambiguous(); 01341 } 01342 01343 /// [ObjC] protocol-qualifiers: 01344 //// '<' identifier-list '>' 01345 Parser::TPResult Parser::TryParseProtocolQualifiers() { 01346 assert(Tok.is(tok::less) && "Expected '<' for qualifier list"); 01347 ConsumeToken(); 01348 do { 01349 if (Tok.isNot(tok::identifier)) 01350 return TPResult::Error(); 01351 ConsumeToken(); 01352 01353 if (Tok.is(tok::comma)) { 01354 ConsumeToken(); 01355 continue; 01356 } 01357 01358 if (Tok.is(tok::greater)) { 01359 ConsumeToken(); 01360 return TPResult::Ambiguous(); 01361 } 01362 } while (false); 01363 01364 return TPResult::Error(); 01365 } 01366 01367 Parser::TPResult 01368 Parser::TryParseDeclarationSpecifier(bool *HasMissingTypename) { 01369 TPResult TPR = isCXXDeclarationSpecifier(TPResult::False(), 01370 HasMissingTypename); 01371 if (TPR != TPResult::Ambiguous()) 01372 return TPR; 01373 01374 if (Tok.is(tok::kw_typeof)) 01375 TryParseTypeofSpecifier(); 01376 else { 01377 if (Tok.is(tok::annot_cxxscope)) 01378 ConsumeToken(); 01379 ConsumeToken(); 01380 01381 if (getLangOpts().ObjC1 && Tok.is(tok::less)) 01382 TryParseProtocolQualifiers(); 01383 } 01384 01385 return TPResult::Ambiguous(); 01386 } 01387 01388 /// isCXXFunctionDeclarator - Disambiguates between a function declarator or 01389 /// a constructor-style initializer, when parsing declaration statements. 01390 /// Returns true for function declarator and false for constructor-style 01391 /// initializer. 01392 /// If during the disambiguation process a parsing error is encountered, 01393 /// the function returns true to let the declaration parsing code handle it. 01394 /// 01395 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] 01396 /// exception-specification[opt] 01397 /// 01398 bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) { 01399 01400 // C++ 8.2p1: 01401 // The ambiguity arising from the similarity between a function-style cast and 01402 // a declaration mentioned in 6.8 can also occur in the context of a 01403 // declaration. In that context, the choice is between a function declaration 01404 // with a redundant set of parentheses around a parameter name and an object 01405 // declaration with a function-style cast as the initializer. Just as for the 01406 // ambiguities mentioned in 6.8, the resolution is to consider any construct 01407 // that could possibly be a declaration a declaration. 01408 01409 TentativeParsingAction PA(*this); 01410 01411 ConsumeParen(); 01412 bool InvalidAsDeclaration = false; 01413 TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration); 01414 if (TPR == TPResult::Ambiguous()) { 01415 if (Tok.isNot(tok::r_paren)) 01416 TPR = TPResult::False(); 01417 else { 01418 const Token &Next = NextToken(); 01419 if (Next.is(tok::amp) || Next.is(tok::ampamp) || 01420 Next.is(tok::kw_const) || Next.is(tok::kw_volatile) || 01421 Next.is(tok::kw_throw) || Next.is(tok::kw_noexcept) || 01422 Next.is(tok::l_square) || isCXX11VirtSpecifier(Next) || 01423 Next.is(tok::l_brace) || Next.is(tok::kw_try) || 01424 Next.is(tok::equal) || Next.is(tok::arrow)) 01425 // The next token cannot appear after a constructor-style initializer, 01426 // and can appear next in a function definition. This must be a function 01427 // declarator. 01428 TPR = TPResult::True(); 01429 else if (InvalidAsDeclaration) 01430 // Use the absence of 'typename' as a tie-breaker. 01431 TPR = TPResult::False(); 01432 } 01433 } 01434 01435 PA.Revert(); 01436 01437 if (IsAmbiguous && TPR == TPResult::Ambiguous()) 01438 *IsAmbiguous = true; 01439 01440 // In case of an error, let the declaration parsing code handle it. 01441 return TPR != TPResult::False(); 01442 } 01443 01444 /// parameter-declaration-clause: 01445 /// parameter-declaration-list[opt] '...'[opt] 01446 /// parameter-declaration-list ',' '...' 01447 /// 01448 /// parameter-declaration-list: 01449 /// parameter-declaration 01450 /// parameter-declaration-list ',' parameter-declaration 01451 /// 01452 /// parameter-declaration: 01453 /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt] 01454 /// attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt] 01455 /// '=' assignment-expression 01456 /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt] 01457 /// attributes[opt] 01458 /// attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt] 01459 /// attributes[opt] '=' assignment-expression 01460 /// 01461 Parser::TPResult 01462 Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration) { 01463 01464 if (Tok.is(tok::r_paren)) 01465 return TPResult::Ambiguous(); 01466 01467 // parameter-declaration-list[opt] '...'[opt] 01468 // parameter-declaration-list ',' '...' 01469 // 01470 // parameter-declaration-list: 01471 // parameter-declaration 01472 // parameter-declaration-list ',' parameter-declaration 01473 // 01474 while (1) { 01475 // '...'[opt] 01476 if (Tok.is(tok::ellipsis)) { 01477 ConsumeToken(); 01478 if (Tok.is(tok::r_paren)) 01479 return TPResult::True(); // '...)' is a sign of a function declarator. 01480 else 01481 return TPResult::False(); 01482 } 01483 01484 // An attribute-specifier-seq here is a sign of a function declarator. 01485 if (isCXX11AttributeSpecifier(/*Disambiguate*/false, 01486 /*OuterMightBeMessageSend*/true)) 01487 return TPResult::True(); 01488 01489 ParsedAttributes attrs(AttrFactory); 01490 MaybeParseMicrosoftAttributes(attrs); 01491 01492 // decl-specifier-seq 01493 // A parameter-declaration's initializer must be preceded by an '=', so 01494 // decl-specifier-seq '{' is not a parameter in C++11. 01495 TPResult TPR = TryParseDeclarationSpecifier(InvalidAsDeclaration); 01496 if (TPR != TPResult::Ambiguous()) 01497 return TPR; 01498 01499 // declarator 01500 // abstract-declarator[opt] 01501 TPR = TryParseDeclarator(true/*mayBeAbstract*/); 01502 if (TPR != TPResult::Ambiguous()) 01503 return TPR; 01504 01505 // [GNU] attributes[opt] 01506 if (Tok.is(tok::kw___attribute)) 01507 return TPResult::True(); 01508 01509 if (Tok.is(tok::equal)) { 01510 // '=' assignment-expression 01511 // Parse through assignment-expression. 01512 if (!SkipUntil(tok::comma, tok::r_paren, true/*StopAtSemi*/, 01513 true/*DontConsume*/)) 01514 return TPResult::Error(); 01515 } 01516 01517 if (Tok.is(tok::ellipsis)) { 01518 ConsumeToken(); 01519 if (Tok.is(tok::r_paren)) 01520 return TPResult::True(); // '...)' is a sign of a function declarator. 01521 else 01522 return TPResult::False(); 01523 } 01524 01525 if (Tok.isNot(tok::comma)) 01526 break; 01527 ConsumeToken(); // the comma. 01528 } 01529 01530 return TPResult::Ambiguous(); 01531 } 01532 01533 /// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue 01534 /// parsing as a function declarator. 01535 /// If TryParseFunctionDeclarator fully parsed the function declarator, it will 01536 /// return TPResult::Ambiguous(), otherwise it will return either False() or 01537 /// Error(). 01538 /// 01539 /// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt] 01540 /// exception-specification[opt] 01541 /// 01542 /// exception-specification: 01543 /// 'throw' '(' type-id-list[opt] ')' 01544 /// 01545 Parser::TPResult Parser::TryParseFunctionDeclarator() { 01546 01547 // The '(' is already parsed. 01548 01549 TPResult TPR = TryParseParameterDeclarationClause(); 01550 if (TPR == TPResult::Ambiguous() && Tok.isNot(tok::r_paren)) 01551 TPR = TPResult::False(); 01552 01553 if (TPR == TPResult::False() || TPR == TPResult::Error()) 01554 return TPR; 01555 01556 // Parse through the parens. 01557 if (!SkipUntil(tok::r_paren)) 01558 return TPResult::Error(); 01559 01560 // cv-qualifier-seq 01561 while (Tok.is(tok::kw_const) || 01562 Tok.is(tok::kw_volatile) || 01563 Tok.is(tok::kw_restrict) ) 01564 ConsumeToken(); 01565 01566 // ref-qualifier[opt] 01567 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) 01568 ConsumeToken(); 01569 01570 // exception-specification 01571 if (Tok.is(tok::kw_throw)) { 01572 ConsumeToken(); 01573 if (Tok.isNot(tok::l_paren)) 01574 return TPResult::Error(); 01575 01576 // Parse through the parens after 'throw'. 01577 ConsumeParen(); 01578 if (!SkipUntil(tok::r_paren)) 01579 return TPResult::Error(); 01580 } 01581 if (Tok.is(tok::kw_noexcept)) { 01582 ConsumeToken(); 01583 // Possibly an expression as well. 01584 if (Tok.is(tok::l_paren)) { 01585 // Find the matching rparen. 01586 ConsumeParen(); 01587 if (!SkipUntil(tok::r_paren)) 01588 return TPResult::Error(); 01589 } 01590 } 01591 01592 return TPResult::Ambiguous(); 01593 } 01594 01595 /// '[' constant-expression[opt] ']' 01596 /// 01597 Parser::TPResult Parser::TryParseBracketDeclarator() { 01598 ConsumeBracket(); 01599 if (!SkipUntil(tok::r_square)) 01600 return TPResult::Error(); 01601 01602 return TPResult::Ambiguous(); 01603 }