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