1 //===--- ParseObjC.cpp - Objective C 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 Objective-C portions of the Parser interface. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Parse/ParseDiagnostic.h" 15 #include "clang/Parse/Parser.h" 16 #include "RAIIObjectsForParser.h" 17 #include "clang/Sema/DeclSpec.h" 18 #include "clang/Sema/PrettyDeclStackTrace.h" 19 #include "clang/Sema/Scope.h" 20 #include "llvm/ADT/SmallVector.h" 21 using namespace clang; 22 23 24 /// ParseObjCAtDirectives - Handle parts of the external-declaration production: 25 /// external-declaration: [C99 6.9] 26 /// [OBJC] objc-class-definition 27 /// [OBJC] objc-class-declaration 28 /// [OBJC] objc-alias-declaration 29 /// [OBJC] objc-protocol-definition 30 /// [OBJC] objc-method-definition 31 /// [OBJC] '@' 'end' 32 Decl *Parser::ParseObjCAtDirectives() { 33 SourceLocation AtLoc = ConsumeToken(); // the "@" 34 35 if (Tok.is(tok::code_completion)) { 36 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, false); 37 ConsumeCodeCompletionToken(); 38 } 39 40 switch (Tok.getObjCKeywordID()) { 41 case tok::objc_class: 42 return ParseObjCAtClassDeclaration(AtLoc); 43 case tok::objc_interface: { 44 ParsedAttributes attrs(AttrFactory); 45 return ParseObjCAtInterfaceDeclaration(AtLoc, attrs); 46 } 47 case tok::objc_protocol: { 48 ParsedAttributes attrs(AttrFactory); 49 return ParseObjCAtProtocolDeclaration(AtLoc, attrs); 50 } 51 case tok::objc_implementation: 52 return ParseObjCAtImplementationDeclaration(AtLoc); 53 case tok::objc_end: 54 return ParseObjCAtEndDeclaration(AtLoc); 55 case tok::objc_compatibility_alias: 56 return ParseObjCAtAliasDeclaration(AtLoc); 57 case tok::objc_synthesize: 58 return ParseObjCPropertySynthesize(AtLoc); 59 case tok::objc_dynamic: 60 return ParseObjCPropertyDynamic(AtLoc); 61 default: 62 Diag(AtLoc, diag::err_unexpected_at); 63 SkipUntil(tok::semi); 64 return 0; 65 } 66 } 67 68 /// 69 /// objc-class-declaration: 70 /// '@' 'class' identifier-list ';' 71 /// 72 Decl *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) { 73 ConsumeToken(); // the identifier "class" 74 llvm::SmallVector<IdentifierInfo *, 8> ClassNames; 75 llvm::SmallVector<SourceLocation, 8> ClassLocs; 76 77 78 while (1) { 79 if (Tok.isNot(tok::identifier)) { 80 Diag(Tok, diag::err_expected_ident); 81 SkipUntil(tok::semi); 82 return 0; 83 } 84 ClassNames.push_back(Tok.getIdentifierInfo()); 85 ClassLocs.push_back(Tok.getLocation()); 86 ConsumeToken(); 87 88 if (Tok.isNot(tok::comma)) 89 break; 90 91 ConsumeToken(); 92 } 93 94 // Consume the ';'. 95 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class")) 96 return 0; 97 98 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(), 99 ClassLocs.data(), 100 ClassNames.size()); 101 } 102 103 /// 104 /// objc-interface: 105 /// objc-class-interface-attributes[opt] objc-class-interface 106 /// objc-category-interface 107 /// 108 /// objc-class-interface: 109 /// '@' 'interface' identifier objc-superclass[opt] 110 /// objc-protocol-refs[opt] 111 /// objc-class-instance-variables[opt] 112 /// objc-interface-decl-list 113 /// @end 114 /// 115 /// objc-category-interface: 116 /// '@' 'interface' identifier '(' identifier[opt] ')' 117 /// objc-protocol-refs[opt] 118 /// objc-interface-decl-list 119 /// @end 120 /// 121 /// objc-superclass: 122 /// ':' identifier 123 /// 124 /// objc-class-interface-attributes: 125 /// __attribute__((visibility("default"))) 126 /// __attribute__((visibility("hidden"))) 127 /// __attribute__((deprecated)) 128 /// __attribute__((unavailable)) 129 /// __attribute__((objc_exception)) - used by NSException on 64-bit 130 /// 131 Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation atLoc, 132 ParsedAttributes &attrs) { 133 assert(Tok.isObjCAtKeyword(tok::objc_interface) && 134 "ParseObjCAtInterfaceDeclaration(): Expected @interface"); 135 ConsumeToken(); // the "interface" identifier 136 137 // Code completion after '@interface'. 138 if (Tok.is(tok::code_completion)) { 139 Actions.CodeCompleteObjCInterfaceDecl(getCurScope()); 140 ConsumeCodeCompletionToken(); 141 } 142 143 if (Tok.isNot(tok::identifier)) { 144 Diag(Tok, diag::err_expected_ident); // missing class or category name. 145 return 0; 146 } 147 148 // We have a class or category name - consume it. 149 IdentifierInfo *nameId = Tok.getIdentifierInfo(); 150 SourceLocation nameLoc = ConsumeToken(); 151 if (Tok.is(tok::l_paren) && 152 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category. 153 // TODO(dgregor): Use the return value from the next line to provide better 154 // recovery. 155 ConsumeParen(); 156 SourceLocation categoryLoc, rparenLoc; 157 IdentifierInfo *categoryId = 0; 158 if (Tok.is(tok::code_completion)) { 159 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc); 160 ConsumeCodeCompletionToken(); 161 } 162 163 // For ObjC2, the category name is optional (not an error). 164 if (Tok.is(tok::identifier)) { 165 categoryId = Tok.getIdentifierInfo(); 166 categoryLoc = ConsumeToken(); 167 } 168 else if (!getLang().ObjC2) { 169 Diag(Tok, diag::err_expected_ident); // missing category name. 170 return 0; 171 } 172 if (Tok.isNot(tok::r_paren)) { 173 Diag(Tok, diag::err_expected_rparen); 174 SkipUntil(tok::r_paren, false); // don't stop at ';' 175 return 0; 176 } 177 rparenLoc = ConsumeParen(); 178 // Next, we need to check for any protocol references. 179 SourceLocation LAngleLoc, EndProtoLoc; 180 llvm::SmallVector<Decl *, 8> ProtocolRefs; 181 llvm::SmallVector<SourceLocation, 8> ProtocolLocs; 182 if (Tok.is(tok::less) && 183 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, 184 LAngleLoc, EndProtoLoc)) 185 return 0; 186 187 if (!attrs.empty()) // categories don't support attributes. 188 Diag(Tok, diag::err_objc_no_attributes_on_category); 189 190 Decl *CategoryType = 191 Actions.ActOnStartCategoryInterface(atLoc, 192 nameId, nameLoc, 193 categoryId, categoryLoc, 194 ProtocolRefs.data(), 195 ProtocolRefs.size(), 196 ProtocolLocs.data(), 197 EndProtoLoc); 198 if (Tok.is(tok::l_brace)) 199 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, 200 atLoc); 201 202 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword); 203 return CategoryType; 204 } 205 // Parse a class interface. 206 IdentifierInfo *superClassId = 0; 207 SourceLocation superClassLoc; 208 209 if (Tok.is(tok::colon)) { // a super class is specified. 210 ConsumeToken(); 211 212 // Code completion of superclass names. 213 if (Tok.is(tok::code_completion)) { 214 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc); 215 ConsumeCodeCompletionToken(); 216 } 217 218 if (Tok.isNot(tok::identifier)) { 219 Diag(Tok, diag::err_expected_ident); // missing super class name. 220 return 0; 221 } 222 superClassId = Tok.getIdentifierInfo(); 223 superClassLoc = ConsumeToken(); 224 } 225 // Next, we need to check for any protocol references. 226 llvm::SmallVector<Decl *, 8> ProtocolRefs; 227 llvm::SmallVector<SourceLocation, 8> ProtocolLocs; 228 SourceLocation LAngleLoc, EndProtoLoc; 229 if (Tok.is(tok::less) && 230 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, 231 LAngleLoc, EndProtoLoc)) 232 return 0; 233 234 Decl *ClsType = 235 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc, 236 superClassId, superClassLoc, 237 ProtocolRefs.data(), ProtocolRefs.size(), 238 ProtocolLocs.data(), 239 EndProtoLoc, attrs.getList()); 240 241 if (Tok.is(tok::l_brace)) 242 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc); 243 244 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface); 245 return ClsType; 246 } 247 248 /// The Objective-C property callback. This should be defined where 249 /// it's used, but instead it's been lifted to here to support VS2005. 250 struct Parser::ObjCPropertyCallback : FieldCallback { 251 Parser &P; 252 Decl *IDecl; 253 llvm::SmallVectorImpl<Decl *> &Props; 254 ObjCDeclSpec &OCDS; 255 SourceLocation AtLoc; 256 tok::ObjCKeywordKind MethodImplKind; 257 258 ObjCPropertyCallback(Parser &P, Decl *IDecl, 259 llvm::SmallVectorImpl<Decl *> &Props, 260 ObjCDeclSpec &OCDS, SourceLocation AtLoc, 261 tok::ObjCKeywordKind MethodImplKind) : 262 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc), 263 MethodImplKind(MethodImplKind) { 264 } 265 266 Decl *invoke(FieldDeclarator &FD) { 267 if (FD.D.getIdentifier() == 0) { 268 P.Diag(AtLoc, diag::err_objc_property_requires_field_name) 269 << FD.D.getSourceRange(); 270 return 0; 271 } 272 if (FD.BitfieldSize) { 273 P.Diag(AtLoc, diag::err_objc_property_bitfield) 274 << FD.D.getSourceRange(); 275 return 0; 276 } 277 278 // Install the property declarator into interfaceDecl. 279 IdentifierInfo *SelName = 280 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier(); 281 282 Selector GetterSel = 283 P.PP.getSelectorTable().getNullarySelector(SelName); 284 IdentifierInfo *SetterName = OCDS.getSetterName(); 285 Selector SetterSel; 286 if (SetterName) 287 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName); 288 else 289 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(), 290 P.PP.getSelectorTable(), 291 FD.D.getIdentifier()); 292 bool isOverridingProperty = false; 293 Decl *Property = 294 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS, 295 GetterSel, SetterSel, IDecl, 296 &isOverridingProperty, 297 MethodImplKind); 298 if (!isOverridingProperty) 299 Props.push_back(Property); 300 301 return Property; 302 } 303 }; 304 305 /// objc-interface-decl-list: 306 /// empty 307 /// objc-interface-decl-list objc-property-decl [OBJC2] 308 /// objc-interface-decl-list objc-method-requirement [OBJC2] 309 /// objc-interface-decl-list objc-method-proto ';' 310 /// objc-interface-decl-list declaration 311 /// objc-interface-decl-list ';' 312 /// 313 /// objc-method-requirement: [OBJC2] 314 /// @required 315 /// @optional 316 /// 317 void Parser::ParseObjCInterfaceDeclList(Decl *interfaceDecl, 318 tok::ObjCKeywordKind contextKey) { 319 llvm::SmallVector<Decl *, 32> allMethods; 320 llvm::SmallVector<Decl *, 16> allProperties; 321 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables; 322 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword; 323 324 SourceRange AtEnd; 325 326 while (1) { 327 // If this is a method prototype, parse it. 328 if (Tok.is(tok::minus) || Tok.is(tok::plus)) { 329 Decl *methodPrototype = 330 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind, false); 331 allMethods.push_back(methodPrototype); 332 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for 333 // method definitions. 334 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto, 335 "", tok::semi); 336 continue; 337 } 338 if (Tok.is(tok::l_paren)) { 339 Diag(Tok, diag::err_expected_minus_or_plus); 340 ParseObjCMethodDecl(Tok.getLocation(), 341 tok::minus, 342 interfaceDecl, 343 MethodImplKind, false); 344 continue; 345 } 346 // Ignore excess semicolons. 347 if (Tok.is(tok::semi)) { 348 ConsumeToken(); 349 continue; 350 } 351 352 // If we got to the end of the file, exit the loop. 353 if (Tok.is(tok::eof)) 354 break; 355 356 // Code completion within an Objective-C interface. 357 if (Tok.is(tok::code_completion)) { 358 Actions.CodeCompleteOrdinaryName(getCurScope(), 359 ObjCImpDecl? Sema::PCC_ObjCImplementation 360 : Sema::PCC_ObjCInterface); 361 ConsumeCodeCompletionToken(); 362 } 363 364 // If we don't have an @ directive, parse it as a function definition. 365 if (Tok.isNot(tok::at)) { 366 // The code below does not consume '}'s because it is afraid of eating the 367 // end of a namespace. Because of the way this code is structured, an 368 // erroneous r_brace would cause an infinite loop if not handled here. 369 if (Tok.is(tok::r_brace)) 370 break; 371 372 // FIXME: as the name implies, this rule allows function definitions. 373 // We could pass a flag or check for functions during semantic analysis. 374 ParsedAttributes attrs(AttrFactory); 375 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs)); 376 continue; 377 } 378 379 // Otherwise, we have an @ directive, eat the @. 380 SourceLocation AtLoc = ConsumeToken(); // the "@" 381 if (Tok.is(tok::code_completion)) { 382 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true); 383 ConsumeCodeCompletionToken(); 384 break; 385 } 386 387 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID(); 388 389 if (DirectiveKind == tok::objc_end) { // @end -> terminate list 390 AtEnd.setBegin(AtLoc); 391 AtEnd.setEnd(Tok.getLocation()); 392 break; 393 } else if (DirectiveKind == tok::objc_not_keyword) { 394 Diag(Tok, diag::err_objc_unknown_at); 395 SkipUntil(tok::semi); 396 continue; 397 } 398 399 // Eat the identifier. 400 ConsumeToken(); 401 402 switch (DirectiveKind) { 403 default: 404 // FIXME: If someone forgets an @end on a protocol, this loop will 405 // continue to eat up tons of stuff and spew lots of nonsense errors. It 406 // would probably be better to bail out if we saw an @class or @interface 407 // or something like that. 408 Diag(AtLoc, diag::err_objc_illegal_interface_qual); 409 // Skip until we see an '@' or '}' or ';'. 410 SkipUntil(tok::r_brace, tok::at); 411 break; 412 413 case tok::objc_implementation: 414 case tok::objc_interface: 415 Diag(Tok, diag::err_objc_missing_end); 416 ConsumeToken(); 417 break; 418 419 case tok::objc_required: 420 case tok::objc_optional: 421 // This is only valid on protocols. 422 // FIXME: Should this check for ObjC2 being enabled? 423 if (contextKey != tok::objc_protocol) 424 Diag(AtLoc, diag::err_objc_directive_only_in_protocol); 425 else 426 MethodImplKind = DirectiveKind; 427 break; 428 429 case tok::objc_property: 430 if (!getLang().ObjC2) 431 Diag(AtLoc, diag::err_objc_properties_require_objc2); 432 433 ObjCDeclSpec OCDS; 434 // Parse property attribute list, if any. 435 if (Tok.is(tok::l_paren)) 436 ParseObjCPropertyAttribute(OCDS, interfaceDecl); 437 438 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties, 439 OCDS, AtLoc, MethodImplKind); 440 441 // Parse all the comma separated declarators. 442 DeclSpec DS(AttrFactory); 443 ParseStructDeclaration(DS, Callback); 444 445 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); 446 break; 447 } 448 } 449 450 // We break out of the big loop in two cases: when we see @end or when we see 451 // EOF. In the former case, eat the @end. In the later case, emit an error. 452 if (Tok.is(tok::code_completion)) { 453 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true); 454 ConsumeCodeCompletionToken(); 455 } else if (Tok.isObjCAtKeyword(tok::objc_end)) 456 ConsumeToken(); // the "end" identifier 457 else 458 Diag(Tok, diag::err_objc_missing_end); 459 460 // Insert collected methods declarations into the @interface object. 461 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit. 462 Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl, 463 allMethods.data(), allMethods.size(), 464 allProperties.data(), allProperties.size(), 465 allTUVariables.data(), allTUVariables.size()); 466 } 467 468 /// Parse property attribute declarations. 469 /// 470 /// property-attr-decl: '(' property-attrlist ')' 471 /// property-attrlist: 472 /// property-attribute 473 /// property-attrlist ',' property-attribute 474 /// property-attribute: 475 /// getter '=' identifier 476 /// setter '=' identifier ':' 477 /// readonly 478 /// readwrite 479 /// assign 480 /// retain 481 /// copy 482 /// nonatomic 483 /// 484 void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, Decl *ClassDecl) { 485 assert(Tok.getKind() == tok::l_paren); 486 SourceLocation LHSLoc = ConsumeParen(); // consume '(' 487 488 while (1) { 489 if (Tok.is(tok::code_completion)) { 490 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS); 491 ConsumeCodeCompletionToken(); 492 } 493 const IdentifierInfo *II = Tok.getIdentifierInfo(); 494 495 // If this is not an identifier at all, bail out early. 496 if (II == 0) { 497 MatchRHSPunctuation(tok::r_paren, LHSLoc); 498 return; 499 } 500 501 SourceLocation AttrName = ConsumeToken(); // consume last attribute name 502 503 if (II->isStr("readonly")) 504 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly); 505 else if (II->isStr("assign")) 506 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign); 507 else if (II->isStr("readwrite")) 508 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite); 509 else if (II->isStr("retain")) 510 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain); 511 else if (II->isStr("copy")) 512 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy); 513 else if (II->isStr("nonatomic")) 514 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic); 515 else if (II->isStr("atomic")) 516 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic); 517 else if (II->isStr("getter") || II->isStr("setter")) { 518 bool IsSetter = II->getNameStart()[0] == 's'; 519 520 // getter/setter require extra treatment. 521 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter : 522 diag::err_objc_expected_equal_for_getter; 523 524 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren)) 525 return; 526 527 if (Tok.is(tok::code_completion)) { 528 if (IsSetter) 529 Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl); 530 else 531 Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl); 532 ConsumeCodeCompletionToken(); 533 } 534 535 536 SourceLocation SelLoc; 537 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc); 538 539 if (!SelIdent) { 540 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter) 541 << IsSetter; 542 SkipUntil(tok::r_paren); 543 return; 544 } 545 546 if (IsSetter) { 547 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter); 548 DS.setSetterName(SelIdent); 549 550 if (ExpectAndConsume(tok::colon, 551 diag::err_expected_colon_after_setter_name, "", 552 tok::r_paren)) 553 return; 554 } else { 555 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter); 556 DS.setGetterName(SelIdent); 557 } 558 } else { 559 Diag(AttrName, diag::err_objc_expected_property_attr) << II; 560 SkipUntil(tok::r_paren); 561 return; 562 } 563 564 if (Tok.isNot(tok::comma)) 565 break; 566 567 ConsumeToken(); 568 } 569 570 MatchRHSPunctuation(tok::r_paren, LHSLoc); 571 } 572 573 /// objc-method-proto: 574 /// objc-instance-method objc-method-decl objc-method-attributes[opt] 575 /// objc-class-method objc-method-decl objc-method-attributes[opt] 576 /// 577 /// objc-instance-method: '-' 578 /// objc-class-method: '+' 579 /// 580 /// objc-method-attributes: [OBJC2] 581 /// __attribute__((deprecated)) 582 /// 583 Decl *Parser::ParseObjCMethodPrototype(Decl *IDecl, 584 tok::ObjCKeywordKind MethodImplKind, 585 bool MethodDefinition) { 586 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-"); 587 588 tok::TokenKind methodType = Tok.getKind(); 589 SourceLocation mLoc = ConsumeToken(); 590 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind, 591 MethodDefinition); 592 // Since this rule is used for both method declarations and definitions, 593 // the caller is (optionally) responsible for consuming the ';'. 594 return MDecl; 595 } 596 597 /// objc-selector: 598 /// identifier 599 /// one of 600 /// enum struct union if else while do for switch case default 601 /// break continue return goto asm sizeof typeof __alignof 602 /// unsigned long const short volatile signed restrict _Complex 603 /// in out inout bycopy byref oneway int char float double void _Bool 604 /// 605 IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) { 606 607 switch (Tok.getKind()) { 608 default: 609 return 0; 610 case tok::ampamp: 611 case tok::ampequal: 612 case tok::amp: 613 case tok::pipe: 614 case tok::tilde: 615 case tok::exclaim: 616 case tok::exclaimequal: 617 case tok::pipepipe: 618 case tok::pipeequal: 619 case tok::caret: 620 case tok::caretequal: { 621 std::string ThisTok(PP.getSpelling(Tok)); 622 if (isalpha(ThisTok[0])) { 623 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data()); 624 Tok.setKind(tok::identifier); 625 SelectorLoc = ConsumeToken(); 626 return II; 627 } 628 return 0; 629 } 630 631 case tok::identifier: 632 case tok::kw_asm: 633 case tok::kw_auto: 634 case tok::kw_bool: 635 case tok::kw_break: 636 case tok::kw_case: 637 case tok::kw_catch: 638 case tok::kw_char: 639 case tok::kw_class: 640 case tok::kw_const: 641 case tok::kw_const_cast: 642 case tok::kw_continue: 643 case tok::kw_default: 644 case tok::kw_delete: 645 case tok::kw_do: 646 case tok::kw_double: 647 case tok::kw_dynamic_cast: 648 case tok::kw_else: 649 case tok::kw_enum: 650 case tok::kw_explicit: 651 case tok::kw_export: 652 case tok::kw_extern: 653 case tok::kw_false: 654 case tok::kw_float: 655 case tok::kw_for: 656 case tok::kw_friend: 657 case tok::kw_goto: 658 case tok::kw_if: 659 case tok::kw_inline: 660 case tok::kw_int: 661 case tok::kw_long: 662 case tok::kw_mutable: 663 case tok::kw_namespace: 664 case tok::kw_new: 665 case tok::kw_operator: 666 case tok::kw_private: 667 case tok::kw_protected: 668 case tok::kw_public: 669 case tok::kw_register: 670 case tok::kw_reinterpret_cast: 671 case tok::kw_restrict: 672 case tok::kw_return: 673 case tok::kw_short: 674 case tok::kw_signed: 675 case tok::kw_sizeof: 676 case tok::kw_static: 677 case tok::kw_static_cast: 678 case tok::kw_struct: 679 case tok::kw_switch: 680 case tok::kw_template: 681 case tok::kw_this: 682 case tok::kw_throw: 683 case tok::kw_true: 684 case tok::kw_try: 685 case tok::kw_typedef: 686 case tok::kw_typeid: 687 case tok::kw_typename: 688 case tok::kw_typeof: 689 case tok::kw_union: 690 case tok::kw_unsigned: 691 case tok::kw_using: 692 case tok::kw_virtual: 693 case tok::kw_void: 694 case tok::kw_volatile: 695 case tok::kw_wchar_t: 696 case tok::kw_while: 697 case tok::kw__Bool: 698 case tok::kw__Complex: 699 case tok::kw___alignof: 700 IdentifierInfo *II = Tok.getIdentifierInfo(); 701 SelectorLoc = ConsumeToken(); 702 return II; 703 } 704 } 705 706 /// objc-for-collection-in: 'in' 707 /// 708 bool Parser::isTokIdentifier_in() const { 709 // FIXME: May have to do additional look-ahead to only allow for 710 // valid tokens following an 'in'; such as an identifier, unary operators, 711 // '[' etc. 712 return (getLang().ObjC2 && Tok.is(tok::identifier) && 713 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]); 714 } 715 716 /// ParseObjCTypeQualifierList - This routine parses the objective-c's type 717 /// qualifier list and builds their bitmask representation in the input 718 /// argument. 719 /// 720 /// objc-type-qualifiers: 721 /// objc-type-qualifier 722 /// objc-type-qualifiers objc-type-qualifier 723 /// 724 void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS, 725 ObjCTypeNameContext Context) { 726 while (1) { 727 if (Tok.is(tok::code_completion)) { 728 Actions.CodeCompleteObjCPassingType(getCurScope(), DS, 729 Context == OTN_ParameterType); 730 ConsumeCodeCompletionToken(); 731 } 732 733 if (Tok.isNot(tok::identifier)) 734 return; 735 736 const IdentifierInfo *II = Tok.getIdentifierInfo(); 737 for (unsigned i = 0; i != objc_NumQuals; ++i) { 738 if (II != ObjCTypeQuals[i]) 739 continue; 740 741 ObjCDeclSpec::ObjCDeclQualifier Qual; 742 switch (i) { 743 default: assert(0 && "Unknown decl qualifier"); 744 case objc_in: Qual = ObjCDeclSpec::DQ_In; break; 745 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break; 746 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break; 747 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break; 748 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break; 749 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break; 750 } 751 DS.setObjCDeclQualifier(Qual); 752 ConsumeToken(); 753 II = 0; 754 break; 755 } 756 757 // If this wasn't a recognized qualifier, bail out. 758 if (II) return; 759 } 760 } 761 762 /// objc-type-name: 763 /// '(' objc-type-qualifiers[opt] type-name ')' 764 /// '(' objc-type-qualifiers[opt] ')' 765 /// 766 ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS, 767 ObjCTypeNameContext Context) { 768 assert(Tok.is(tok::l_paren) && "expected ("); 769 770 SourceLocation LParenLoc = ConsumeParen(); 771 SourceLocation TypeStartLoc = Tok.getLocation(); 772 773 // Parse type qualifiers, in, inout, etc. 774 ParseObjCTypeQualifierList(DS, Context); 775 776 ParsedType Ty; 777 if (isTypeSpecifierQualifier()) { 778 TypeResult TypeSpec = ParseTypeName(0, Declarator::ObjCPrototypeContext); 779 if (!TypeSpec.isInvalid()) 780 Ty = TypeSpec.get(); 781 } 782 783 if (Tok.is(tok::r_paren)) 784 ConsumeParen(); 785 else if (Tok.getLocation() == TypeStartLoc) { 786 // If we didn't eat any tokens, then this isn't a type. 787 Diag(Tok, diag::err_expected_type); 788 SkipUntil(tok::r_paren); 789 } else { 790 // Otherwise, we found *something*, but didn't get a ')' in the right 791 // place. Emit an error then return what we have as the type. 792 MatchRHSPunctuation(tok::r_paren, LParenLoc); 793 } 794 return Ty; 795 } 796 797 /// objc-method-decl: 798 /// objc-selector 799 /// objc-keyword-selector objc-parmlist[opt] 800 /// objc-type-name objc-selector 801 /// objc-type-name objc-keyword-selector objc-parmlist[opt] 802 /// 803 /// objc-keyword-selector: 804 /// objc-keyword-decl 805 /// objc-keyword-selector objc-keyword-decl 806 /// 807 /// objc-keyword-decl: 808 /// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier 809 /// objc-selector ':' objc-keyword-attributes[opt] identifier 810 /// ':' objc-type-name objc-keyword-attributes[opt] identifier 811 /// ':' objc-keyword-attributes[opt] identifier 812 /// 813 /// objc-parmlist: 814 /// objc-parms objc-ellipsis[opt] 815 /// 816 /// objc-parms: 817 /// objc-parms , parameter-declaration 818 /// 819 /// objc-ellipsis: 820 /// , ... 821 /// 822 /// objc-keyword-attributes: [OBJC2] 823 /// __attribute__((unused)) 824 /// 825 Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc, 826 tok::TokenKind mType, 827 Decl *IDecl, 828 tok::ObjCKeywordKind MethodImplKind, 829 bool MethodDefinition) { 830 ParsingDeclRAIIObject PD(*this); 831 832 if (Tok.is(tok::code_completion)) { 833 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus, 834 /*ReturnType=*/ ParsedType(), IDecl); 835 ConsumeCodeCompletionToken(); 836 } 837 838 // Parse the return type if present. 839 ParsedType ReturnType; 840 ObjCDeclSpec DSRet; 841 if (Tok.is(tok::l_paren)) 842 ReturnType = ParseObjCTypeName(DSRet, OTN_ResultType); 843 844 // If attributes exist before the method, parse them. 845 ParsedAttributes methodAttrs(AttrFactory); 846 if (getLang().ObjC2) 847 MaybeParseGNUAttributes(methodAttrs); 848 849 if (Tok.is(tok::code_completion)) { 850 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus, 851 ReturnType, IDecl); 852 ConsumeCodeCompletionToken(); 853 } 854 855 // Now parse the selector. 856 SourceLocation selLoc; 857 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc); 858 859 // An unnamed colon is valid. 860 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name. 861 Diag(Tok, diag::err_expected_selector_for_method) 862 << SourceRange(mLoc, Tok.getLocation()); 863 // Skip until we get a ; or {}. 864 SkipUntil(tok::r_brace); 865 return 0; 866 } 867 868 llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo; 869 if (Tok.isNot(tok::colon)) { 870 // If attributes exist after the method, parse them. 871 if (getLang().ObjC2) 872 MaybeParseGNUAttributes(methodAttrs); 873 874 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent); 875 Decl *Result 876 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(), 877 mType, IDecl, DSRet, ReturnType, Sel, 878 0, 879 CParamInfo.data(), CParamInfo.size(), 880 methodAttrs.getList(), MethodImplKind, 881 false, MethodDefinition); 882 PD.complete(Result); 883 return Result; 884 } 885 886 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; 887 llvm::SmallVector<Sema::ObjCArgInfo, 12> ArgInfos; 888 ParseScope PrototypeScope(this, 889 Scope::FunctionPrototypeScope|Scope::DeclScope); 890 891 AttributePool allParamAttrs(AttrFactory); 892 893 while (1) { 894 ParsedAttributes paramAttrs(AttrFactory); 895 Sema::ObjCArgInfo ArgInfo; 896 897 // Each iteration parses a single keyword argument. 898 if (Tok.isNot(tok::colon)) { 899 Diag(Tok, diag::err_expected_colon); 900 break; 901 } 902 ConsumeToken(); // Eat the ':'. 903 904 ArgInfo.Type = ParsedType(); 905 if (Tok.is(tok::l_paren)) // Parse the argument type if present. 906 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, OTN_ParameterType); 907 908 // If attributes exist before the argument name, parse them. 909 ArgInfo.ArgAttrs = 0; 910 if (getLang().ObjC2) { 911 MaybeParseGNUAttributes(paramAttrs); 912 ArgInfo.ArgAttrs = paramAttrs.getList(); 913 } 914 915 // Code completion for the next piece of the selector. 916 if (Tok.is(tok::code_completion)) { 917 ConsumeCodeCompletionToken(); 918 KeyIdents.push_back(SelIdent); 919 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(), 920 mType == tok::minus, 921 /*AtParameterName=*/true, 922 ReturnType, 923 KeyIdents.data(), 924 KeyIdents.size()); 925 KeyIdents.pop_back(); 926 break; 927 } 928 929 if (Tok.isNot(tok::identifier)) { 930 Diag(Tok, diag::err_expected_ident); // missing argument name. 931 break; 932 } 933 934 ArgInfo.Name = Tok.getIdentifierInfo(); 935 ArgInfo.NameLoc = Tok.getLocation(); 936 ConsumeToken(); // Eat the identifier. 937 938 ArgInfos.push_back(ArgInfo); 939 KeyIdents.push_back(SelIdent); 940 941 // Make sure the attributes persist. 942 allParamAttrs.takeAllFrom(paramAttrs.getPool()); 943 944 // Code completion for the next piece of the selector. 945 if (Tok.is(tok::code_completion)) { 946 ConsumeCodeCompletionToken(); 947 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(), 948 mType == tok::minus, 949 /*AtParameterName=*/false, 950 ReturnType, 951 KeyIdents.data(), 952 KeyIdents.size()); 953 break; 954 } 955 956 // Check for another keyword selector. 957 SourceLocation Loc; 958 SelIdent = ParseObjCSelectorPiece(Loc); 959 if (!SelIdent && Tok.isNot(tok::colon)) 960 break; 961 // We have a selector or a colon, continue parsing. 962 } 963 964 bool isVariadic = false; 965 966 // Parse the (optional) parameter list. 967 while (Tok.is(tok::comma)) { 968 ConsumeToken(); 969 if (Tok.is(tok::ellipsis)) { 970 isVariadic = true; 971 ConsumeToken(); 972 break; 973 } 974 DeclSpec DS(AttrFactory); 975 ParseDeclarationSpecifiers(DS); 976 // Parse the declarator. 977 Declarator ParmDecl(DS, Declarator::PrototypeContext); 978 ParseDeclarator(ParmDecl); 979 IdentifierInfo *ParmII = ParmDecl.getIdentifier(); 980 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); 981 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, 982 ParmDecl.getIdentifierLoc(), 983 Param, 984 0)); 985 986 } 987 988 // FIXME: Add support for optional parameter list... 989 // If attributes exist after the method, parse them. 990 if (getLang().ObjC2) 991 MaybeParseGNUAttributes(methodAttrs); 992 993 if (KeyIdents.size() == 0) { 994 // Leave prototype scope. 995 PrototypeScope.Exit(); 996 return 0; 997 } 998 999 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(), 1000 &KeyIdents[0]); 1001 Decl *Result 1002 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(), 1003 mType, IDecl, DSRet, ReturnType, Sel, 1004 &ArgInfos[0], 1005 CParamInfo.data(), CParamInfo.size(), 1006 methodAttrs.getList(), 1007 MethodImplKind, isVariadic, MethodDefinition); 1008 // Leave prototype scope. 1009 PrototypeScope.Exit(); 1010 1011 PD.complete(Result); 1012 return Result; 1013 } 1014 1015 /// objc-protocol-refs: 1016 /// '<' identifier-list '>' 1017 /// 1018 bool Parser:: 1019 ParseObjCProtocolReferences(llvm::SmallVectorImpl<Decl *> &Protocols, 1020 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs, 1021 bool WarnOnDeclarations, 1022 SourceLocation &LAngleLoc, SourceLocation &EndLoc) { 1023 assert(Tok.is(tok::less) && "expected <"); 1024 1025 LAngleLoc = ConsumeToken(); // the "<" 1026 1027 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents; 1028 1029 while (1) { 1030 if (Tok.is(tok::code_completion)) { 1031 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(), 1032 ProtocolIdents.size()); 1033 ConsumeCodeCompletionToken(); 1034 } 1035 1036 if (Tok.isNot(tok::identifier)) { 1037 Diag(Tok, diag::err_expected_ident); 1038 SkipUntil(tok::greater); 1039 return true; 1040 } 1041 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(), 1042 Tok.getLocation())); 1043 ProtocolLocs.push_back(Tok.getLocation()); 1044 ConsumeToken(); 1045 1046 if (Tok.isNot(tok::comma)) 1047 break; 1048 ConsumeToken(); 1049 } 1050 1051 // Consume the '>'. 1052 if (Tok.isNot(tok::greater)) { 1053 Diag(Tok, diag::err_expected_greater); 1054 return true; 1055 } 1056 1057 EndLoc = ConsumeAnyToken(); 1058 1059 // Convert the list of protocols identifiers into a list of protocol decls. 1060 Actions.FindProtocolDeclaration(WarnOnDeclarations, 1061 &ProtocolIdents[0], ProtocolIdents.size(), 1062 Protocols); 1063 return false; 1064 } 1065 1066 /// \brief Parse the Objective-C protocol qualifiers that follow a typename 1067 /// in a decl-specifier-seq, starting at the '<'. 1068 bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) { 1069 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'"); 1070 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C"); 1071 SourceLocation LAngleLoc, EndProtoLoc; 1072 llvm::SmallVector<Decl *, 8> ProtocolDecl; 1073 llvm::SmallVector<SourceLocation, 8> ProtocolLocs; 1074 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false, 1075 LAngleLoc, EndProtoLoc); 1076 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(), 1077 ProtocolLocs.data(), LAngleLoc); 1078 if (EndProtoLoc.isValid()) 1079 DS.SetRangeEnd(EndProtoLoc); 1080 return Result; 1081 } 1082 1083 1084 /// objc-class-instance-variables: 1085 /// '{' objc-instance-variable-decl-list[opt] '}' 1086 /// 1087 /// objc-instance-variable-decl-list: 1088 /// objc-visibility-spec 1089 /// objc-instance-variable-decl ';' 1090 /// ';' 1091 /// objc-instance-variable-decl-list objc-visibility-spec 1092 /// objc-instance-variable-decl-list objc-instance-variable-decl ';' 1093 /// objc-instance-variable-decl-list ';' 1094 /// 1095 /// objc-visibility-spec: 1096 /// @private 1097 /// @protected 1098 /// @public 1099 /// @package [OBJC2] 1100 /// 1101 /// objc-instance-variable-decl: 1102 /// struct-declaration 1103 /// 1104 void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl, 1105 tok::ObjCKeywordKind visibility, 1106 SourceLocation atLoc) { 1107 assert(Tok.is(tok::l_brace) && "expected {"); 1108 llvm::SmallVector<Decl *, 32> AllIvarDecls; 1109 1110 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope); 1111 1112 SourceLocation LBraceLoc = ConsumeBrace(); // the "{" 1113 1114 // While we still have something to read, read the instance variables. 1115 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 1116 // Each iteration of this loop reads one objc-instance-variable-decl. 1117 1118 // Check for extraneous top-level semicolon. 1119 if (Tok.is(tok::semi)) { 1120 Diag(Tok, diag::ext_extra_ivar_semi) 1121 << FixItHint::CreateRemoval(Tok.getLocation()); 1122 ConsumeToken(); 1123 continue; 1124 } 1125 1126 // Set the default visibility to private. 1127 if (Tok.is(tok::at)) { // parse objc-visibility-spec 1128 ConsumeToken(); // eat the @ sign 1129 1130 if (Tok.is(tok::code_completion)) { 1131 Actions.CodeCompleteObjCAtVisibility(getCurScope()); 1132 ConsumeCodeCompletionToken(); 1133 } 1134 1135 switch (Tok.getObjCKeywordID()) { 1136 case tok::objc_private: 1137 case tok::objc_public: 1138 case tok::objc_protected: 1139 case tok::objc_package: 1140 visibility = Tok.getObjCKeywordID(); 1141 ConsumeToken(); 1142 continue; 1143 default: 1144 Diag(Tok, diag::err_objc_illegal_visibility_spec); 1145 continue; 1146 } 1147 } 1148 1149 if (Tok.is(tok::code_completion)) { 1150 Actions.CodeCompleteOrdinaryName(getCurScope(), 1151 Sema::PCC_ObjCInstanceVariableList); 1152 ConsumeCodeCompletionToken(); 1153 } 1154 1155 struct ObjCIvarCallback : FieldCallback { 1156 Parser &P; 1157 Decl *IDecl; 1158 tok::ObjCKeywordKind visibility; 1159 llvm::SmallVectorImpl<Decl *> &AllIvarDecls; 1160 1161 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V, 1162 llvm::SmallVectorImpl<Decl *> &AllIvarDecls) : 1163 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) { 1164 } 1165 1166 Decl *invoke(FieldDeclarator &FD) { 1167 // Install the declarator into the interface decl. 1168 Decl *Field 1169 = P.Actions.ActOnIvar(P.getCurScope(), 1170 FD.D.getDeclSpec().getSourceRange().getBegin(), 1171 IDecl, FD.D, FD.BitfieldSize, visibility); 1172 if (Field) 1173 AllIvarDecls.push_back(Field); 1174 return Field; 1175 } 1176 } Callback(*this, interfaceDecl, visibility, AllIvarDecls); 1177 1178 // Parse all the comma separated declarators. 1179 DeclSpec DS(AttrFactory); 1180 ParseStructDeclaration(DS, Callback); 1181 1182 if (Tok.is(tok::semi)) { 1183 ConsumeToken(); 1184 } else { 1185 Diag(Tok, diag::err_expected_semi_decl_list); 1186 // Skip to end of block or statement 1187 SkipUntil(tok::r_brace, true, true); 1188 } 1189 } 1190 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); 1191 Actions.ActOnLastBitfield(RBraceLoc, interfaceDecl, AllIvarDecls); 1192 // Call ActOnFields() even if we don't have any decls. This is useful 1193 // for code rewriting tools that need to be aware of the empty list. 1194 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl, 1195 AllIvarDecls.data(), AllIvarDecls.size(), 1196 LBraceLoc, RBraceLoc, 0); 1197 return; 1198 } 1199 1200 /// objc-protocol-declaration: 1201 /// objc-protocol-definition 1202 /// objc-protocol-forward-reference 1203 /// 1204 /// objc-protocol-definition: 1205 /// @protocol identifier 1206 /// objc-protocol-refs[opt] 1207 /// objc-interface-decl-list 1208 /// @end 1209 /// 1210 /// objc-protocol-forward-reference: 1211 /// @protocol identifier-list ';' 1212 /// 1213 /// "@protocol identifier ;" should be resolved as "@protocol 1214 /// identifier-list ;": objc-interface-decl-list may not start with a 1215 /// semicolon in the first alternative if objc-protocol-refs are omitted. 1216 Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc, 1217 ParsedAttributes &attrs) { 1218 assert(Tok.isObjCAtKeyword(tok::objc_protocol) && 1219 "ParseObjCAtProtocolDeclaration(): Expected @protocol"); 1220 ConsumeToken(); // the "protocol" identifier 1221 1222 if (Tok.is(tok::code_completion)) { 1223 Actions.CodeCompleteObjCProtocolDecl(getCurScope()); 1224 ConsumeCodeCompletionToken(); 1225 } 1226 1227 if (Tok.isNot(tok::identifier)) { 1228 Diag(Tok, diag::err_expected_ident); // missing protocol name. 1229 return 0; 1230 } 1231 // Save the protocol name, then consume it. 1232 IdentifierInfo *protocolName = Tok.getIdentifierInfo(); 1233 SourceLocation nameLoc = ConsumeToken(); 1234 1235 if (Tok.is(tok::semi)) { // forward declaration of one protocol. 1236 IdentifierLocPair ProtoInfo(protocolName, nameLoc); 1237 ConsumeToken(); 1238 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1, 1239 attrs.getList()); 1240 } 1241 1242 if (Tok.is(tok::comma)) { // list of forward declarations. 1243 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs; 1244 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc)); 1245 1246 // Parse the list of forward declarations. 1247 while (1) { 1248 ConsumeToken(); // the ',' 1249 if (Tok.isNot(tok::identifier)) { 1250 Diag(Tok, diag::err_expected_ident); 1251 SkipUntil(tok::semi); 1252 return 0; 1253 } 1254 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(), 1255 Tok.getLocation())); 1256 ConsumeToken(); // the identifier 1257 1258 if (Tok.isNot(tok::comma)) 1259 break; 1260 } 1261 // Consume the ';'. 1262 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol")) 1263 return 0; 1264 1265 return Actions.ActOnForwardProtocolDeclaration(AtLoc, 1266 &ProtocolRefs[0], 1267 ProtocolRefs.size(), 1268 attrs.getList()); 1269 } 1270 1271 // Last, and definitely not least, parse a protocol declaration. 1272 SourceLocation LAngleLoc, EndProtoLoc; 1273 1274 llvm::SmallVector<Decl *, 8> ProtocolRefs; 1275 llvm::SmallVector<SourceLocation, 8> ProtocolLocs; 1276 if (Tok.is(tok::less) && 1277 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, 1278 LAngleLoc, EndProtoLoc)) 1279 return 0; 1280 1281 Decl *ProtoType = 1282 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc, 1283 ProtocolRefs.data(), 1284 ProtocolRefs.size(), 1285 ProtocolLocs.data(), 1286 EndProtoLoc, attrs.getList()); 1287 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol); 1288 return ProtoType; 1289 } 1290 1291 /// objc-implementation: 1292 /// objc-class-implementation-prologue 1293 /// objc-category-implementation-prologue 1294 /// 1295 /// objc-class-implementation-prologue: 1296 /// @implementation identifier objc-superclass[opt] 1297 /// objc-class-instance-variables[opt] 1298 /// 1299 /// objc-category-implementation-prologue: 1300 /// @implementation identifier ( identifier ) 1301 Decl *Parser::ParseObjCAtImplementationDeclaration( 1302 SourceLocation atLoc) { 1303 assert(Tok.isObjCAtKeyword(tok::objc_implementation) && 1304 "ParseObjCAtImplementationDeclaration(): Expected @implementation"); 1305 ConsumeToken(); // the "implementation" identifier 1306 1307 // Code completion after '@implementation'. 1308 if (Tok.is(tok::code_completion)) { 1309 Actions.CodeCompleteObjCImplementationDecl(getCurScope()); 1310 ConsumeCodeCompletionToken(); 1311 } 1312 1313 if (Tok.isNot(tok::identifier)) { 1314 Diag(Tok, diag::err_expected_ident); // missing class or category name. 1315 return 0; 1316 } 1317 // We have a class or category name - consume it. 1318 IdentifierInfo *nameId = Tok.getIdentifierInfo(); 1319 SourceLocation nameLoc = ConsumeToken(); // consume class or category name 1320 1321 if (Tok.is(tok::l_paren)) { 1322 // we have a category implementation. 1323 ConsumeParen(); 1324 SourceLocation categoryLoc, rparenLoc; 1325 IdentifierInfo *categoryId = 0; 1326 1327 if (Tok.is(tok::code_completion)) { 1328 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc); 1329 ConsumeCodeCompletionToken(); 1330 } 1331 1332 if (Tok.is(tok::identifier)) { 1333 categoryId = Tok.getIdentifierInfo(); 1334 categoryLoc = ConsumeToken(); 1335 } else { 1336 Diag(Tok, diag::err_expected_ident); // missing category name. 1337 return 0; 1338 } 1339 if (Tok.isNot(tok::r_paren)) { 1340 Diag(Tok, diag::err_expected_rparen); 1341 SkipUntil(tok::r_paren, false); // don't stop at ';' 1342 return 0; 1343 } 1344 rparenLoc = ConsumeParen(); 1345 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation( 1346 atLoc, nameId, nameLoc, categoryId, 1347 categoryLoc); 1348 ObjCImpDecl = ImplCatType; 1349 PendingObjCImpDecl.push_back(ObjCImpDecl); 1350 return 0; 1351 } 1352 // We have a class implementation 1353 SourceLocation superClassLoc; 1354 IdentifierInfo *superClassId = 0; 1355 if (Tok.is(tok::colon)) { 1356 // We have a super class 1357 ConsumeToken(); 1358 if (Tok.isNot(tok::identifier)) { 1359 Diag(Tok, diag::err_expected_ident); // missing super class name. 1360 return 0; 1361 } 1362 superClassId = Tok.getIdentifierInfo(); 1363 superClassLoc = ConsumeToken(); // Consume super class name 1364 } 1365 Decl *ImplClsType = Actions.ActOnStartClassImplementation( 1366 atLoc, nameId, nameLoc, 1367 superClassId, superClassLoc); 1368 1369 if (Tok.is(tok::l_brace)) // we have ivars 1370 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, 1371 tok::objc_private, atLoc); 1372 ObjCImpDecl = ImplClsType; 1373 PendingObjCImpDecl.push_back(ObjCImpDecl); 1374 1375 return 0; 1376 } 1377 1378 Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) { 1379 assert(Tok.isObjCAtKeyword(tok::objc_end) && 1380 "ParseObjCAtEndDeclaration(): Expected @end"); 1381 Decl *Result = ObjCImpDecl; 1382 ConsumeToken(); // the "end" identifier 1383 if (ObjCImpDecl) { 1384 Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl); 1385 ObjCImpDecl = 0; 1386 PendingObjCImpDecl.pop_back(); 1387 } 1388 else { 1389 // missing @implementation 1390 Diag(atEnd.getBegin(), diag::warn_expected_implementation); 1391 } 1392 return Result; 1393 } 1394 1395 Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() { 1396 Actions.DiagnoseUseOfUnimplementedSelectors(); 1397 if (PendingObjCImpDecl.empty()) 1398 return Actions.ConvertDeclToDeclGroup(0); 1399 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val(); 1400 Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl); 1401 return Actions.ConvertDeclToDeclGroup(ImpDecl); 1402 } 1403 1404 /// compatibility-alias-decl: 1405 /// @compatibility_alias alias-name class-name ';' 1406 /// 1407 Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) { 1408 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) && 1409 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias"); 1410 ConsumeToken(); // consume compatibility_alias 1411 if (Tok.isNot(tok::identifier)) { 1412 Diag(Tok, diag::err_expected_ident); 1413 return 0; 1414 } 1415 IdentifierInfo *aliasId = Tok.getIdentifierInfo(); 1416 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name 1417 if (Tok.isNot(tok::identifier)) { 1418 Diag(Tok, diag::err_expected_ident); 1419 return 0; 1420 } 1421 IdentifierInfo *classId = Tok.getIdentifierInfo(); 1422 SourceLocation classLoc = ConsumeToken(); // consume class-name; 1423 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, 1424 "@compatibility_alias"); 1425 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc, 1426 classId, classLoc); 1427 } 1428 1429 /// property-synthesis: 1430 /// @synthesize property-ivar-list ';' 1431 /// 1432 /// property-ivar-list: 1433 /// property-ivar 1434 /// property-ivar-list ',' property-ivar 1435 /// 1436 /// property-ivar: 1437 /// identifier 1438 /// identifier '=' identifier 1439 /// 1440 Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) { 1441 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) && 1442 "ParseObjCPropertyDynamic(): Expected '@synthesize'"); 1443 ConsumeToken(); // consume synthesize 1444 1445 while (true) { 1446 if (Tok.is(tok::code_completion)) { 1447 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl); 1448 ConsumeCodeCompletionToken(); 1449 } 1450 1451 if (Tok.isNot(tok::identifier)) { 1452 Diag(Tok, diag::err_synthesized_property_name); 1453 SkipUntil(tok::semi); 1454 return 0; 1455 } 1456 1457 IdentifierInfo *propertyIvar = 0; 1458 IdentifierInfo *propertyId = Tok.getIdentifierInfo(); 1459 SourceLocation propertyLoc = ConsumeToken(); // consume property name 1460 SourceLocation propertyIvarLoc; 1461 if (Tok.is(tok::equal)) { 1462 // property '=' ivar-name 1463 ConsumeToken(); // consume '=' 1464 1465 if (Tok.is(tok::code_completion)) { 1466 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId, 1467 ObjCImpDecl); 1468 ConsumeCodeCompletionToken(); 1469 } 1470 1471 if (Tok.isNot(tok::identifier)) { 1472 Diag(Tok, diag::err_expected_ident); 1473 break; 1474 } 1475 propertyIvar = Tok.getIdentifierInfo(); 1476 propertyIvarLoc = ConsumeToken(); // consume ivar-name 1477 } 1478 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl, 1479 propertyId, propertyIvar, propertyIvarLoc); 1480 if (Tok.isNot(tok::comma)) 1481 break; 1482 ConsumeToken(); // consume ',' 1483 } 1484 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize"); 1485 return 0; 1486 } 1487 1488 /// property-dynamic: 1489 /// @dynamic property-list 1490 /// 1491 /// property-list: 1492 /// identifier 1493 /// property-list ',' identifier 1494 /// 1495 Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) { 1496 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) && 1497 "ParseObjCPropertyDynamic(): Expected '@dynamic'"); 1498 ConsumeToken(); // consume dynamic 1499 while (true) { 1500 if (Tok.is(tok::code_completion)) { 1501 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl); 1502 ConsumeCodeCompletionToken(); 1503 } 1504 1505 if (Tok.isNot(tok::identifier)) { 1506 Diag(Tok, diag::err_expected_ident); 1507 SkipUntil(tok::semi); 1508 return 0; 1509 } 1510 1511 IdentifierInfo *propertyId = Tok.getIdentifierInfo(); 1512 SourceLocation propertyLoc = ConsumeToken(); // consume property name 1513 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl, 1514 propertyId, 0, SourceLocation()); 1515 1516 if (Tok.isNot(tok::comma)) 1517 break; 1518 ConsumeToken(); // consume ',' 1519 } 1520 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic"); 1521 return 0; 1522 } 1523 1524 /// objc-throw-statement: 1525 /// throw expression[opt]; 1526 /// 1527 StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) { 1528 ExprResult Res; 1529 ConsumeToken(); // consume throw 1530 if (Tok.isNot(tok::semi)) { 1531 Res = ParseExpression(); 1532 if (Res.isInvalid()) { 1533 SkipUntil(tok::semi); 1534 return StmtError(); 1535 } 1536 } 1537 // consume ';' 1538 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw"); 1539 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope()); 1540 } 1541 1542 /// objc-synchronized-statement: 1543 /// @synchronized '(' expression ')' compound-statement 1544 /// 1545 StmtResult 1546 Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) { 1547 ConsumeToken(); // consume synchronized 1548 if (Tok.isNot(tok::l_paren)) { 1549 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized"; 1550 return StmtError(); 1551 } 1552 ConsumeParen(); // '(' 1553 ExprResult Res(ParseExpression()); 1554 if (Res.isInvalid()) { 1555 SkipUntil(tok::semi); 1556 return StmtError(); 1557 } 1558 if (Tok.isNot(tok::r_paren)) { 1559 Diag(Tok, diag::err_expected_lbrace); 1560 return StmtError(); 1561 } 1562 ConsumeParen(); // ')' 1563 if (Tok.isNot(tok::l_brace)) { 1564 Diag(Tok, diag::err_expected_lbrace); 1565 return StmtError(); 1566 } 1567 // Enter a scope to hold everything within the compound stmt. Compound 1568 // statements can always hold declarations. 1569 ParseScope BodyScope(this, Scope::DeclScope); 1570 1571 StmtResult SynchBody(ParseCompoundStatementBody()); 1572 1573 BodyScope.Exit(); 1574 if (SynchBody.isInvalid()) 1575 SynchBody = Actions.ActOnNullStmt(Tok.getLocation()); 1576 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.take(), SynchBody.take()); 1577 } 1578 1579 /// objc-try-catch-statement: 1580 /// @try compound-statement objc-catch-list[opt] 1581 /// @try compound-statement objc-catch-list[opt] @finally compound-statement 1582 /// 1583 /// objc-catch-list: 1584 /// @catch ( parameter-declaration ) compound-statement 1585 /// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement 1586 /// catch-parameter-declaration: 1587 /// parameter-declaration 1588 /// '...' [OBJC2] 1589 /// 1590 StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) { 1591 bool catch_or_finally_seen = false; 1592 1593 ConsumeToken(); // consume try 1594 if (Tok.isNot(tok::l_brace)) { 1595 Diag(Tok, diag::err_expected_lbrace); 1596 return StmtError(); 1597 } 1598 StmtVector CatchStmts(Actions); 1599 StmtResult FinallyStmt; 1600 ParseScope TryScope(this, Scope::DeclScope); 1601 StmtResult TryBody(ParseCompoundStatementBody()); 1602 TryScope.Exit(); 1603 if (TryBody.isInvalid()) 1604 TryBody = Actions.ActOnNullStmt(Tok.getLocation()); 1605 1606 while (Tok.is(tok::at)) { 1607 // At this point, we need to lookahead to determine if this @ is the start 1608 // of an @catch or @finally. We don't want to consume the @ token if this 1609 // is an @try or @encode or something else. 1610 Token AfterAt = GetLookAheadToken(1); 1611 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) && 1612 !AfterAt.isObjCAtKeyword(tok::objc_finally)) 1613 break; 1614 1615 SourceLocation AtCatchFinallyLoc = ConsumeToken(); 1616 if (Tok.isObjCAtKeyword(tok::objc_catch)) { 1617 Decl *FirstPart = 0; 1618 ConsumeToken(); // consume catch 1619 if (Tok.is(tok::l_paren)) { 1620 ConsumeParen(); 1621 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope); 1622 if (Tok.isNot(tok::ellipsis)) { 1623 DeclSpec DS(AttrFactory); 1624 ParseDeclarationSpecifiers(DS); 1625 // For some odd reason, the name of the exception variable is 1626 // optional. As a result, we need to use "PrototypeContext", because 1627 // we must accept either 'declarator' or 'abstract-declarator' here. 1628 Declarator ParmDecl(DS, Declarator::PrototypeContext); 1629 ParseDeclarator(ParmDecl); 1630 1631 // Inform the actions module about the declarator, so it 1632 // gets added to the current scope. 1633 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl); 1634 } else 1635 ConsumeToken(); // consume '...' 1636 1637 SourceLocation RParenLoc; 1638 1639 if (Tok.is(tok::r_paren)) 1640 RParenLoc = ConsumeParen(); 1641 else // Skip over garbage, until we get to ')'. Eat the ')'. 1642 SkipUntil(tok::r_paren, true, false); 1643 1644 StmtResult CatchBody(true); 1645 if (Tok.is(tok::l_brace)) 1646 CatchBody = ParseCompoundStatementBody(); 1647 else 1648 Diag(Tok, diag::err_expected_lbrace); 1649 if (CatchBody.isInvalid()) 1650 CatchBody = Actions.ActOnNullStmt(Tok.getLocation()); 1651 1652 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, 1653 RParenLoc, 1654 FirstPart, 1655 CatchBody.take()); 1656 if (!Catch.isInvalid()) 1657 CatchStmts.push_back(Catch.release()); 1658 1659 } else { 1660 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after) 1661 << "@catch clause"; 1662 return StmtError(); 1663 } 1664 catch_or_finally_seen = true; 1665 } else { 1666 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?"); 1667 ConsumeToken(); // consume finally 1668 ParseScope FinallyScope(this, Scope::DeclScope); 1669 1670 StmtResult FinallyBody(true); 1671 if (Tok.is(tok::l_brace)) 1672 FinallyBody = ParseCompoundStatementBody(); 1673 else 1674 Diag(Tok, diag::err_expected_lbrace); 1675 if (FinallyBody.isInvalid()) 1676 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation()); 1677 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc, 1678 FinallyBody.take()); 1679 catch_or_finally_seen = true; 1680 break; 1681 } 1682 } 1683 if (!catch_or_finally_seen) { 1684 Diag(atLoc, diag::err_missing_catch_finally); 1685 return StmtError(); 1686 } 1687 1688 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(), 1689 move_arg(CatchStmts), 1690 FinallyStmt.take()); 1691 } 1692 1693 /// objc-method-def: objc-method-proto ';'[opt] '{' body '}' 1694 /// 1695 Decl *Parser::ParseObjCMethodDefinition() { 1696 Decl *MDecl = ParseObjCMethodPrototype(ObjCImpDecl); 1697 1698 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(), 1699 "parsing Objective-C method"); 1700 1701 // parse optional ';' 1702 if (Tok.is(tok::semi)) { 1703 if (ObjCImpDecl) { 1704 Diag(Tok, diag::warn_semicolon_before_method_body) 1705 << FixItHint::CreateRemoval(Tok.getLocation()); 1706 } 1707 ConsumeToken(); 1708 } 1709 1710 // We should have an opening brace now. 1711 if (Tok.isNot(tok::l_brace)) { 1712 Diag(Tok, diag::err_expected_method_body); 1713 1714 // Skip over garbage, until we get to '{'. Don't eat the '{'. 1715 SkipUntil(tok::l_brace, true, true); 1716 1717 // If we didn't find the '{', bail out. 1718 if (Tok.isNot(tok::l_brace)) 1719 return 0; 1720 } 1721 SourceLocation BraceLoc = Tok.getLocation(); 1722 1723 // Enter a scope for the method body. 1724 ParseScope BodyScope(this, 1725 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope); 1726 1727 // Tell the actions module that we have entered a method definition with the 1728 // specified Declarator for the method. 1729 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl); 1730 1731 if (PP.isCodeCompletionEnabled()) { 1732 if (trySkippingFunctionBodyForCodeCompletion()) { 1733 BodyScope.Exit(); 1734 return Actions.ActOnFinishFunctionBody(MDecl, 0); 1735 } 1736 } 1737 1738 StmtResult FnBody(ParseCompoundStatementBody()); 1739 1740 // If the function body could not be parsed, make a bogus compoundstmt. 1741 if (FnBody.isInvalid()) 1742 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 1743 MultiStmtArg(Actions), false); 1744 1745 // Leave the function body scope. 1746 BodyScope.Exit(); 1747 1748 // TODO: Pass argument information. 1749 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take()); 1750 return MDecl; 1751 } 1752 1753 StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) { 1754 if (Tok.is(tok::code_completion)) { 1755 Actions.CodeCompleteObjCAtStatement(getCurScope()); 1756 ConsumeCodeCompletionToken(); 1757 return StmtError(); 1758 } 1759 1760 if (Tok.isObjCAtKeyword(tok::objc_try)) 1761 return ParseObjCTryStmt(AtLoc); 1762 1763 if (Tok.isObjCAtKeyword(tok::objc_throw)) 1764 return ParseObjCThrowStmt(AtLoc); 1765 1766 if (Tok.isObjCAtKeyword(tok::objc_synchronized)) 1767 return ParseObjCSynchronizedStmt(AtLoc); 1768 1769 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc)); 1770 if (Res.isInvalid()) { 1771 // If the expression is invalid, skip ahead to the next semicolon. Not 1772 // doing this opens us up to the possibility of infinite loops if 1773 // ParseExpression does not consume any tokens. 1774 SkipUntil(tok::semi); 1775 return StmtError(); 1776 } 1777 1778 // Otherwise, eat the semicolon. 1779 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr); 1780 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take())); 1781 } 1782 1783 ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) { 1784 switch (Tok.getKind()) { 1785 case tok::code_completion: 1786 Actions.CodeCompleteObjCAtExpression(getCurScope()); 1787 ConsumeCodeCompletionToken(); 1788 return ExprError(); 1789 1790 case tok::string_literal: // primary-expression: string-literal 1791 case tok::wide_string_literal: 1792 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc)); 1793 default: 1794 if (Tok.getIdentifierInfo() == 0) 1795 return ExprError(Diag(AtLoc, diag::err_unexpected_at)); 1796 1797 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) { 1798 case tok::objc_encode: 1799 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc)); 1800 case tok::objc_protocol: 1801 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc)); 1802 case tok::objc_selector: 1803 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc)); 1804 default: 1805 return ExprError(Diag(AtLoc, diag::err_unexpected_at)); 1806 } 1807 } 1808 } 1809 1810 /// \brirg Parse the receiver of an Objective-C++ message send. 1811 /// 1812 /// This routine parses the receiver of a message send in 1813 /// Objective-C++ either as a type or as an expression. Note that this 1814 /// routine must not be called to parse a send to 'super', since it 1815 /// has no way to return such a result. 1816 /// 1817 /// \param IsExpr Whether the receiver was parsed as an expression. 1818 /// 1819 /// \param TypeOrExpr If the receiver was parsed as an expression (\c 1820 /// IsExpr is true), the parsed expression. If the receiver was parsed 1821 /// as a type (\c IsExpr is false), the parsed type. 1822 /// 1823 /// \returns True if an error occurred during parsing or semantic 1824 /// analysis, in which case the arguments do not have valid 1825 /// values. Otherwise, returns false for a successful parse. 1826 /// 1827 /// objc-receiver: [C++] 1828 /// 'super' [not parsed here] 1829 /// expression 1830 /// simple-type-specifier 1831 /// typename-specifier 1832 bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) { 1833 InMessageExpressionRAIIObject InMessage(*this, true); 1834 1835 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) || 1836 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)) 1837 TryAnnotateTypeOrScopeToken(); 1838 1839 if (!isCXXSimpleTypeSpecifier()) { 1840 // objc-receiver: 1841 // expression 1842 ExprResult Receiver = ParseExpression(); 1843 if (Receiver.isInvalid()) 1844 return true; 1845 1846 IsExpr = true; 1847 TypeOrExpr = Receiver.take(); 1848 return false; 1849 } 1850 1851 // objc-receiver: 1852 // typename-specifier 1853 // simple-type-specifier 1854 // expression (that starts with one of the above) 1855 DeclSpec DS(AttrFactory); 1856 ParseCXXSimpleTypeSpecifier(DS); 1857 1858 if (Tok.is(tok::l_paren)) { 1859 // If we see an opening parentheses at this point, we are 1860 // actually parsing an expression that starts with a 1861 // function-style cast, e.g., 1862 // 1863 // postfix-expression: 1864 // simple-type-specifier ( expression-list [opt] ) 1865 // typename-specifier ( expression-list [opt] ) 1866 // 1867 // Parse the remainder of this case, then the (optional) 1868 // postfix-expression suffix, followed by the (optional) 1869 // right-hand side of the binary expression. We have an 1870 // instance method. 1871 ExprResult Receiver = ParseCXXTypeConstructExpression(DS); 1872 if (!Receiver.isInvalid()) 1873 Receiver = ParsePostfixExpressionSuffix(Receiver.take()); 1874 if (!Receiver.isInvalid()) 1875 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma); 1876 if (Receiver.isInvalid()) 1877 return true; 1878 1879 IsExpr = true; 1880 TypeOrExpr = Receiver.take(); 1881 return false; 1882 } 1883 1884 // We have a class message. Turn the simple-type-specifier or 1885 // typename-specifier we parsed into a type and parse the 1886 // remainder of the class message. 1887 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); 1888 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); 1889 if (Type.isInvalid()) 1890 return true; 1891 1892 IsExpr = false; 1893 TypeOrExpr = Type.get().getAsOpaquePtr(); 1894 return false; 1895 } 1896 1897 /// \brief Determine whether the parser is currently referring to a an 1898 /// Objective-C message send, using a simplified heuristic to avoid overhead. 1899 /// 1900 /// This routine will only return true for a subset of valid message-send 1901 /// expressions. 1902 bool Parser::isSimpleObjCMessageExpression() { 1903 assert(Tok.is(tok::l_square) && getLang().ObjC1 && 1904 "Incorrect start for isSimpleObjCMessageExpression"); 1905 return GetLookAheadToken(1).is(tok::identifier) && 1906 GetLookAheadToken(2).is(tok::identifier); 1907 } 1908 1909 bool Parser::isStartOfObjCClassMessageMissingOpenBracket() { 1910 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) || 1911 InMessageExpression) 1912 return false; 1913 1914 1915 ParsedType Type; 1916 1917 if (Tok.is(tok::annot_typename)) 1918 Type = getTypeAnnotation(Tok); 1919 else if (Tok.is(tok::identifier)) 1920 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(), 1921 getCurScope()); 1922 else 1923 return false; 1924 1925 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) { 1926 const Token &AfterNext = GetLookAheadToken(2); 1927 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) { 1928 if (Tok.is(tok::identifier)) 1929 TryAnnotateTypeOrScopeToken(); 1930 1931 return Tok.is(tok::annot_typename); 1932 } 1933 } 1934 1935 return false; 1936 } 1937 1938 /// objc-message-expr: 1939 /// '[' objc-receiver objc-message-args ']' 1940 /// 1941 /// objc-receiver: [C] 1942 /// 'super' 1943 /// expression 1944 /// class-name 1945 /// type-name 1946 /// 1947 ExprResult Parser::ParseObjCMessageExpression() { 1948 assert(Tok.is(tok::l_square) && "'[' expected"); 1949 SourceLocation LBracLoc = ConsumeBracket(); // consume '[' 1950 1951 if (Tok.is(tok::code_completion)) { 1952 Actions.CodeCompleteObjCMessageReceiver(getCurScope()); 1953 ConsumeCodeCompletionToken(); 1954 SkipUntil(tok::r_square); 1955 return ExprError(); 1956 } 1957 1958 InMessageExpressionRAIIObject InMessage(*this, true); 1959 1960 if (getLang().CPlusPlus) { 1961 // We completely separate the C and C++ cases because C++ requires 1962 // more complicated (read: slower) parsing. 1963 1964 // Handle send to super. 1965 // FIXME: This doesn't benefit from the same typo-correction we 1966 // get in Objective-C. 1967 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super && 1968 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope()) 1969 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), 1970 ParsedType(), 0); 1971 1972 // Parse the receiver, which is either a type or an expression. 1973 bool IsExpr; 1974 void *TypeOrExpr = NULL; 1975 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) { 1976 SkipUntil(tok::r_square); 1977 return ExprError(); 1978 } 1979 1980 if (IsExpr) 1981 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 1982 ParsedType(), 1983 static_cast<Expr*>(TypeOrExpr)); 1984 1985 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 1986 ParsedType::getFromOpaquePtr(TypeOrExpr), 1987 0); 1988 } 1989 1990 if (Tok.is(tok::identifier)) { 1991 IdentifierInfo *Name = Tok.getIdentifierInfo(); 1992 SourceLocation NameLoc = Tok.getLocation(); 1993 ParsedType ReceiverType; 1994 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc, 1995 Name == Ident_super, 1996 NextToken().is(tok::period), 1997 ReceiverType)) { 1998 case Sema::ObjCSuperMessage: 1999 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), 2000 ParsedType(), 0); 2001 2002 case Sema::ObjCClassMessage: 2003 if (!ReceiverType) { 2004 SkipUntil(tok::r_square); 2005 return ExprError(); 2006 } 2007 2008 ConsumeToken(); // the type name 2009 2010 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 2011 ReceiverType, 0); 2012 2013 case Sema::ObjCInstanceMessage: 2014 // Fall through to parse an expression. 2015 break; 2016 } 2017 } 2018 2019 // Otherwise, an arbitrary expression can be the receiver of a send. 2020 ExprResult Res(ParseExpression()); 2021 if (Res.isInvalid()) { 2022 SkipUntil(tok::r_square); 2023 return move(Res); 2024 } 2025 2026 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 2027 ParsedType(), Res.take()); 2028 } 2029 2030 /// \brief Parse the remainder of an Objective-C message following the 2031 /// '[' objc-receiver. 2032 /// 2033 /// This routine handles sends to super, class messages (sent to a 2034 /// class name), and instance messages (sent to an object), and the 2035 /// target is represented by \p SuperLoc, \p ReceiverType, or \p 2036 /// ReceiverExpr, respectively. Only one of these parameters may have 2037 /// a valid value. 2038 /// 2039 /// \param LBracLoc The location of the opening '['. 2040 /// 2041 /// \param SuperLoc If this is a send to 'super', the location of the 2042 /// 'super' keyword that indicates a send to the superclass. 2043 /// 2044 /// \param ReceiverType If this is a class message, the type of the 2045 /// class we are sending a message to. 2046 /// 2047 /// \param ReceiverExpr If this is an instance message, the expression 2048 /// used to compute the receiver object. 2049 /// 2050 /// objc-message-args: 2051 /// objc-selector 2052 /// objc-keywordarg-list 2053 /// 2054 /// objc-keywordarg-list: 2055 /// objc-keywordarg 2056 /// objc-keywordarg-list objc-keywordarg 2057 /// 2058 /// objc-keywordarg: 2059 /// selector-name[opt] ':' objc-keywordexpr 2060 /// 2061 /// objc-keywordexpr: 2062 /// nonempty-expr-list 2063 /// 2064 /// nonempty-expr-list: 2065 /// assignment-expression 2066 /// nonempty-expr-list , assignment-expression 2067 /// 2068 ExprResult 2069 Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc, 2070 SourceLocation SuperLoc, 2071 ParsedType ReceiverType, 2072 ExprArg ReceiverExpr) { 2073 InMessageExpressionRAIIObject InMessage(*this, true); 2074 2075 if (Tok.is(tok::code_completion)) { 2076 if (SuperLoc.isValid()) 2077 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0, 2078 false); 2079 else if (ReceiverType) 2080 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0, 2081 false); 2082 else 2083 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr, 2084 0, 0, false); 2085 ConsumeCodeCompletionToken(); 2086 } 2087 2088 // Parse objc-selector 2089 SourceLocation Loc; 2090 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc); 2091 2092 SourceLocation SelectorLoc = Loc; 2093 2094 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; 2095 ExprVector KeyExprs(Actions); 2096 2097 if (Tok.is(tok::colon)) { 2098 while (1) { 2099 // Each iteration parses a single keyword argument. 2100 KeyIdents.push_back(selIdent); 2101 2102 if (Tok.isNot(tok::colon)) { 2103 Diag(Tok, diag::err_expected_colon); 2104 // We must manually skip to a ']', otherwise the expression skipper will 2105 // stop at the ']' when it skips to the ';'. We want it to skip beyond 2106 // the enclosing expression. 2107 SkipUntil(tok::r_square); 2108 return ExprError(); 2109 } 2110 2111 ConsumeToken(); // Eat the ':'. 2112 /// Parse the expression after ':' 2113 2114 if (Tok.is(tok::code_completion)) { 2115 if (SuperLoc.isValid()) 2116 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 2117 KeyIdents.data(), 2118 KeyIdents.size(), 2119 /*AtArgumentEpression=*/true); 2120 else if (ReceiverType) 2121 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 2122 KeyIdents.data(), 2123 KeyIdents.size(), 2124 /*AtArgumentEpression=*/true); 2125 else 2126 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr, 2127 KeyIdents.data(), 2128 KeyIdents.size(), 2129 /*AtArgumentEpression=*/true); 2130 2131 ConsumeCodeCompletionToken(); 2132 SkipUntil(tok::r_square); 2133 return ExprError(); 2134 } 2135 2136 ExprResult Res(ParseAssignmentExpression()); 2137 if (Res.isInvalid()) { 2138 // We must manually skip to a ']', otherwise the expression skipper will 2139 // stop at the ']' when it skips to the ';'. We want it to skip beyond 2140 // the enclosing expression. 2141 SkipUntil(tok::r_square); 2142 return move(Res); 2143 } 2144 2145 // We have a valid expression. 2146 KeyExprs.push_back(Res.release()); 2147 2148 // Code completion after each argument. 2149 if (Tok.is(tok::code_completion)) { 2150 if (SuperLoc.isValid()) 2151 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 2152 KeyIdents.data(), 2153 KeyIdents.size(), 2154 /*AtArgumentEpression=*/false); 2155 else if (ReceiverType) 2156 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 2157 KeyIdents.data(), 2158 KeyIdents.size(), 2159 /*AtArgumentEpression=*/false); 2160 else 2161 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr, 2162 KeyIdents.data(), 2163 KeyIdents.size(), 2164 /*AtArgumentEpression=*/false); 2165 ConsumeCodeCompletionToken(); 2166 SkipUntil(tok::r_square); 2167 return ExprError(); 2168 } 2169 2170 // Check for another keyword selector. 2171 selIdent = ParseObjCSelectorPiece(Loc); 2172 if (!selIdent && Tok.isNot(tok::colon)) 2173 break; 2174 // We have a selector or a colon, continue parsing. 2175 } 2176 // Parse the, optional, argument list, comma separated. 2177 while (Tok.is(tok::comma)) { 2178 ConsumeToken(); // Eat the ','. 2179 /// Parse the expression after ',' 2180 ExprResult Res(ParseAssignmentExpression()); 2181 if (Res.isInvalid()) { 2182 // We must manually skip to a ']', otherwise the expression skipper will 2183 // stop at the ']' when it skips to the ';'. We want it to skip beyond 2184 // the enclosing expression. 2185 SkipUntil(tok::r_square); 2186 return move(Res); 2187 } 2188 2189 // We have a valid expression. 2190 KeyExprs.push_back(Res.release()); 2191 } 2192 } else if (!selIdent) { 2193 Diag(Tok, diag::err_expected_ident); // missing selector name. 2194 2195 // We must manually skip to a ']', otherwise the expression skipper will 2196 // stop at the ']' when it skips to the ';'. We want it to skip beyond 2197 // the enclosing expression. 2198 SkipUntil(tok::r_square); 2199 return ExprError(); 2200 } 2201 2202 if (Tok.isNot(tok::r_square)) { 2203 if (Tok.is(tok::identifier)) 2204 Diag(Tok, diag::err_expected_colon); 2205 else 2206 Diag(Tok, diag::err_expected_rsquare); 2207 // We must manually skip to a ']', otherwise the expression skipper will 2208 // stop at the ']' when it skips to the ';'. We want it to skip beyond 2209 // the enclosing expression. 2210 SkipUntil(tok::r_square); 2211 return ExprError(); 2212 } 2213 2214 SourceLocation RBracLoc = ConsumeBracket(); // consume ']' 2215 2216 unsigned nKeys = KeyIdents.size(); 2217 if (nKeys == 0) 2218 KeyIdents.push_back(selIdent); 2219 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]); 2220 2221 if (SuperLoc.isValid()) 2222 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel, 2223 LBracLoc, SelectorLoc, RBracLoc, 2224 MultiExprArg(Actions, 2225 KeyExprs.take(), 2226 KeyExprs.size())); 2227 else if (ReceiverType) 2228 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel, 2229 LBracLoc, SelectorLoc, RBracLoc, 2230 MultiExprArg(Actions, 2231 KeyExprs.take(), 2232 KeyExprs.size())); 2233 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel, 2234 LBracLoc, SelectorLoc, RBracLoc, 2235 MultiExprArg(Actions, 2236 KeyExprs.take(), 2237 KeyExprs.size())); 2238 } 2239 2240 ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) { 2241 ExprResult Res(ParseStringLiteralExpression()); 2242 if (Res.isInvalid()) return move(Res); 2243 2244 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string 2245 // expressions. At this point, we know that the only valid thing that starts 2246 // with '@' is an @"". 2247 llvm::SmallVector<SourceLocation, 4> AtLocs; 2248 ExprVector AtStrings(Actions); 2249 AtLocs.push_back(AtLoc); 2250 AtStrings.push_back(Res.release()); 2251 2252 while (Tok.is(tok::at)) { 2253 AtLocs.push_back(ConsumeToken()); // eat the @. 2254 2255 // Invalid unless there is a string literal. 2256 if (!isTokenStringLiteral()) 2257 return ExprError(Diag(Tok, diag::err_objc_concat_string)); 2258 2259 ExprResult Lit(ParseStringLiteralExpression()); 2260 if (Lit.isInvalid()) 2261 return move(Lit); 2262 2263 AtStrings.push_back(Lit.release()); 2264 } 2265 2266 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(), 2267 AtStrings.size())); 2268 } 2269 2270 /// objc-encode-expression: 2271 /// @encode ( type-name ) 2272 ExprResult 2273 Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) { 2274 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!"); 2275 2276 SourceLocation EncLoc = ConsumeToken(); 2277 2278 if (Tok.isNot(tok::l_paren)) 2279 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode"); 2280 2281 SourceLocation LParenLoc = ConsumeParen(); 2282 2283 TypeResult Ty = ParseTypeName(); 2284 2285 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 2286 2287 if (Ty.isInvalid()) 2288 return ExprError(); 2289 2290 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, 2291 Ty.get(), RParenLoc)); 2292 } 2293 2294 /// objc-protocol-expression 2295 /// @protocol ( protocol-name ) 2296 ExprResult 2297 Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) { 2298 SourceLocation ProtoLoc = ConsumeToken(); 2299 2300 if (Tok.isNot(tok::l_paren)) 2301 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol"); 2302 2303 SourceLocation LParenLoc = ConsumeParen(); 2304 2305 if (Tok.isNot(tok::identifier)) 2306 return ExprError(Diag(Tok, diag::err_expected_ident)); 2307 2308 IdentifierInfo *protocolId = Tok.getIdentifierInfo(); 2309 ConsumeToken(); 2310 2311 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 2312 2313 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc, 2314 LParenLoc, RParenLoc)); 2315 } 2316 2317 /// objc-selector-expression 2318 /// @selector '(' objc-keyword-selector ')' 2319 ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) { 2320 SourceLocation SelectorLoc = ConsumeToken(); 2321 2322 if (Tok.isNot(tok::l_paren)) 2323 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector"); 2324 2325 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; 2326 SourceLocation LParenLoc = ConsumeParen(); 2327 SourceLocation sLoc; 2328 2329 if (Tok.is(tok::code_completion)) { 2330 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(), 2331 KeyIdents.size()); 2332 ConsumeCodeCompletionToken(); 2333 MatchRHSPunctuation(tok::r_paren, LParenLoc); 2334 return ExprError(); 2335 } 2336 2337 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc); 2338 if (!SelIdent && // missing selector name. 2339 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon)) 2340 return ExprError(Diag(Tok, diag::err_expected_ident)); 2341 2342 KeyIdents.push_back(SelIdent); 2343 unsigned nColons = 0; 2344 if (Tok.isNot(tok::r_paren)) { 2345 while (1) { 2346 if (Tok.is(tok::coloncolon)) { // Handle :: in C++. 2347 ++nColons; 2348 KeyIdents.push_back(0); 2349 } else if (Tok.isNot(tok::colon)) 2350 return ExprError(Diag(Tok, diag::err_expected_colon)); 2351 2352 ++nColons; 2353 ConsumeToken(); // Eat the ':'. 2354 if (Tok.is(tok::r_paren)) 2355 break; 2356 2357 if (Tok.is(tok::code_completion)) { 2358 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(), 2359 KeyIdents.size()); 2360 ConsumeCodeCompletionToken(); 2361 MatchRHSPunctuation(tok::r_paren, LParenLoc); 2362 return ExprError(); 2363 } 2364 2365 // Check for another keyword selector. 2366 SourceLocation Loc; 2367 SelIdent = ParseObjCSelectorPiece(Loc); 2368 KeyIdents.push_back(SelIdent); 2369 if (!SelIdent && Tok.isNot(tok::colon)) 2370 break; 2371 } 2372 } 2373 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 2374 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]); 2375 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, 2376 LParenLoc, RParenLoc)); 2377 } 2378