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