1 //===--- ParseObjC.cpp - Objective C Parsing ------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the Objective-C portions of the Parser interface. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Parse/ParseDiagnostic.h" 15 #include "clang/Parse/Parser.h" 16 #include "RAIIObjectsForParser.h" 17 #include "clang/Sema/DeclSpec.h" 18 #include "clang/Sema/PrettyDeclStackTrace.h" 19 #include "clang/Sema/Scope.h" 20 #include "llvm/ADT/SmallVector.h" 21 using namespace clang; 22 23 24 /// ParseObjCAtDirectives - Handle parts of the external-declaration production: 25 /// external-declaration: [C99 6.9] 26 /// [OBJC] objc-class-definition 27 /// [OBJC] objc-class-declaration 28 /// [OBJC] objc-alias-declaration 29 /// [OBJC] objc-protocol-definition 30 /// [OBJC] objc-method-definition 31 /// [OBJC] '@' 'end' 32 Decl *Parser::ParseObjCAtDirectives() { 33 SourceLocation AtLoc = ConsumeToken(); // the "@" 34 35 if (Tok.is(tok::code_completion)) { 36 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, false); 37 ConsumeCodeCompletionToken(); 38 } 39 40 switch (Tok.getObjCKeywordID()) { 41 case tok::objc_class: 42 return ParseObjCAtClassDeclaration(AtLoc); 43 case tok::objc_interface: 44 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 /// 68 Decl *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 /// 127 Decl *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. 244 struct 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 /// 311 void 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 /// 473 void 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 // getter/setter require extra treatment. 508 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "", 509 tok::r_paren)) 510 return; 511 512 if (Tok.is(tok::code_completion)) { 513 if (II->getNameStart()[0] == 's') 514 Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl, 515 Methods, NumMethods); 516 else 517 Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl, 518 Methods, NumMethods); 519 ConsumeCodeCompletionToken(); 520 } 521 522 if (Tok.isNot(tok::identifier)) { 523 Diag(Tok, diag::err_expected_ident); 524 SkipUntil(tok::r_paren); 525 return; 526 } 527 528 if (II->getNameStart()[0] == 's') { 529 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter); 530 DS.setSetterName(Tok.getIdentifierInfo()); 531 ConsumeToken(); // consume method name 532 533 if (ExpectAndConsume(tok::colon, 534 diag::err_expected_colon_after_setter_name, "", 535 tok::r_paren)) 536 return; 537 } else { 538 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter); 539 DS.setGetterName(Tok.getIdentifierInfo()); 540 ConsumeToken(); // consume method name 541 } 542 } else { 543 Diag(AttrName, diag::err_objc_expected_property_attr) << II; 544 SkipUntil(tok::r_paren); 545 return; 546 } 547 548 if (Tok.isNot(tok::comma)) 549 break; 550 551 ConsumeToken(); 552 } 553 554 MatchRHSPunctuation(tok::r_paren, LHSLoc); 555 } 556 557 /// objc-method-proto: 558 /// objc-instance-method objc-method-decl objc-method-attributes[opt] 559 /// objc-class-method objc-method-decl objc-method-attributes[opt] 560 /// 561 /// objc-instance-method: '-' 562 /// objc-class-method: '+' 563 /// 564 /// objc-method-attributes: [OBJC2] 565 /// __attribute__((deprecated)) 566 /// 567 Decl *Parser::ParseObjCMethodPrototype(Decl *IDecl, 568 tok::ObjCKeywordKind MethodImplKind) { 569 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-"); 570 571 tok::TokenKind methodType = Tok.getKind(); 572 SourceLocation mLoc = ConsumeToken(); 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 589 switch (Tok.getKind()) { 590 default: 591 return 0; 592 case tok::ampamp: 593 case tok::ampequal: 594 case tok::amp: 595 case tok::pipe: 596 case tok::tilde: 597 case tok::exclaim: 598 case tok::exclaimequal: 599 case tok::pipepipe: 600 case tok::pipeequal: 601 case tok::caret: 602 case tok::caretequal: { 603 std::string ThisTok(PP.getSpelling(Tok)); 604 if (isalpha(ThisTok[0])) { 605 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data()); 606 Tok.setKind(tok::identifier); 607 SelectorLoc = ConsumeToken(); 608 return II; 609 } 610 return 0; 611 } 612 613 case tok::identifier: 614 case tok::kw_asm: 615 case tok::kw_auto: 616 case tok::kw_bool: 617 case tok::kw_break: 618 case tok::kw_case: 619 case tok::kw_catch: 620 case tok::kw_char: 621 case tok::kw_class: 622 case tok::kw_const: 623 case tok::kw_const_cast: 624 case tok::kw_continue: 625 case tok::kw_default: 626 case tok::kw_delete: 627 case tok::kw_do: 628 case tok::kw_double: 629 case tok::kw_dynamic_cast: 630 case tok::kw_else: 631 case tok::kw_enum: 632 case tok::kw_explicit: 633 case tok::kw_export: 634 case tok::kw_extern: 635 case tok::kw_false: 636 case tok::kw_float: 637 case tok::kw_for: 638 case tok::kw_friend: 639 case tok::kw_goto: 640 case tok::kw_if: 641 case tok::kw_inline: 642 case tok::kw_int: 643 case tok::kw_long: 644 case tok::kw_mutable: 645 case tok::kw_namespace: 646 case tok::kw_new: 647 case tok::kw_operator: 648 case tok::kw_private: 649 case tok::kw_protected: 650 case tok::kw_public: 651 case tok::kw_register: 652 case tok::kw_reinterpret_cast: 653 case tok::kw_restrict: 654 case tok::kw_return: 655 case tok::kw_short: 656 case tok::kw_signed: 657 case tok::kw_sizeof: 658 case tok::kw_static: 659 case tok::kw_static_cast: 660 case tok::kw_struct: 661 case tok::kw_switch: 662 case tok::kw_template: 663 case tok::kw_this: 664 case tok::kw_throw: 665 case tok::kw_true: 666 case tok::kw_try: 667 case tok::kw_typedef: 668 case tok::kw_typeid: 669 case tok::kw_typename: 670 case tok::kw_typeof: 671 case tok::kw_union: 672 case tok::kw_unsigned: 673 case tok::kw_using: 674 case tok::kw_virtual: 675 case tok::kw_void: 676 case tok::kw_volatile: 677 case tok::kw_wchar_t: 678 case tok::kw_while: 679 case tok::kw__Bool: 680 case tok::kw__Complex: 681 case tok::kw___alignof: 682 IdentifierInfo *II = Tok.getIdentifierInfo(); 683 SelectorLoc = ConsumeToken(); 684 return II; 685 } 686 } 687 688 /// objc-for-collection-in: 'in' 689 /// 690 bool Parser::isTokIdentifier_in() const { 691 // FIXME: May have to do additional look-ahead to only allow for 692 // valid tokens following an 'in'; such as an identifier, unary operators, 693 // '[' etc. 694 return (getLang().ObjC2 && Tok.is(tok::identifier) && 695 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]); 696 } 697 698 /// ParseObjCTypeQualifierList - This routine parses the objective-c's type 699 /// qualifier list and builds their bitmask representation in the input 700 /// argument. 701 /// 702 /// objc-type-qualifiers: 703 /// objc-type-qualifier 704 /// objc-type-qualifiers objc-type-qualifier 705 /// 706 void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS, bool IsParameter) { 707 while (1) { 708 if (Tok.is(tok::code_completion)) { 709 Actions.CodeCompleteObjCPassingType(getCurScope(), DS); 710 ConsumeCodeCompletionToken(); 711 } 712 713 if (Tok.isNot(tok::identifier)) 714 return; 715 716 const IdentifierInfo *II = Tok.getIdentifierInfo(); 717 for (unsigned i = 0; i != objc_NumQuals; ++i) { 718 if (II != ObjCTypeQuals[i]) 719 continue; 720 721 ObjCDeclSpec::ObjCDeclQualifier Qual; 722 switch (i) { 723 default: assert(0 && "Unknown decl qualifier"); 724 case objc_in: Qual = ObjCDeclSpec::DQ_In; break; 725 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break; 726 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break; 727 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break; 728 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break; 729 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break; 730 } 731 DS.setObjCDeclQualifier(Qual); 732 ConsumeToken(); 733 II = 0; 734 break; 735 } 736 737 // If this wasn't a recognized qualifier, bail out. 738 if (II) return; 739 } 740 } 741 742 /// objc-type-name: 743 /// '(' objc-type-qualifiers[opt] type-name ')' 744 /// '(' objc-type-qualifiers[opt] ')' 745 /// 746 ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS, bool IsParameter) { 747 assert(Tok.is(tok::l_paren) && "expected ("); 748 749 SourceLocation LParenLoc = ConsumeParen(); 750 SourceLocation TypeStartLoc = Tok.getLocation(); 751 752 // Parse type qualifiers, in, inout, etc. 753 ParseObjCTypeQualifierList(DS, IsParameter); 754 755 ParsedType Ty; 756 if (isTypeSpecifierQualifier()) { 757 TypeResult TypeSpec = ParseTypeName(); 758 if (!TypeSpec.isInvalid()) 759 Ty = TypeSpec.get(); 760 } 761 762 if (Tok.is(tok::r_paren)) 763 ConsumeParen(); 764 else if (Tok.getLocation() == TypeStartLoc) { 765 // If we didn't eat any tokens, then this isn't a type. 766 Diag(Tok, diag::err_expected_type); 767 SkipUntil(tok::r_paren); 768 } else { 769 // Otherwise, we found *something*, but didn't get a ')' in the right 770 // place. Emit an error then return what we have as the type. 771 MatchRHSPunctuation(tok::r_paren, LParenLoc); 772 } 773 return Ty; 774 } 775 776 /// objc-method-decl: 777 /// objc-selector 778 /// objc-keyword-selector objc-parmlist[opt] 779 /// objc-type-name objc-selector 780 /// objc-type-name objc-keyword-selector objc-parmlist[opt] 781 /// 782 /// objc-keyword-selector: 783 /// objc-keyword-decl 784 /// objc-keyword-selector objc-keyword-decl 785 /// 786 /// objc-keyword-decl: 787 /// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier 788 /// objc-selector ':' objc-keyword-attributes[opt] identifier 789 /// ':' objc-type-name objc-keyword-attributes[opt] identifier 790 /// ':' objc-keyword-attributes[opt] identifier 791 /// 792 /// objc-parmlist: 793 /// objc-parms objc-ellipsis[opt] 794 /// 795 /// objc-parms: 796 /// objc-parms , parameter-declaration 797 /// 798 /// objc-ellipsis: 799 /// , ... 800 /// 801 /// objc-keyword-attributes: [OBJC2] 802 /// __attribute__((unused)) 803 /// 804 Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc, 805 tok::TokenKind mType, 806 Decl *IDecl, 807 tok::ObjCKeywordKind MethodImplKind) { 808 ParsingDeclRAIIObject PD(*this); 809 810 if (Tok.is(tok::code_completion)) { 811 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus, 812 /*ReturnType=*/ ParsedType(), IDecl); 813 ConsumeCodeCompletionToken(); 814 } 815 816 // Parse the return type if present. 817 ParsedType ReturnType; 818 ObjCDeclSpec DSRet; 819 if (Tok.is(tok::l_paren)) 820 ReturnType = ParseObjCTypeName(DSRet, false); 821 822 // If attributes exist before the method, parse them. 823 llvm::OwningPtr<AttributeList> MethodAttrs; 824 if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) 825 MethodAttrs.reset(ParseGNUAttributes()); 826 827 if (Tok.is(tok::code_completion)) { 828 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus, 829 ReturnType, IDecl); 830 ConsumeCodeCompletionToken(); 831 } 832 833 // Now parse the selector. 834 SourceLocation selLoc; 835 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc); 836 837 // An unnamed colon is valid. 838 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name. 839 Diag(Tok, diag::err_expected_selector_for_method) 840 << SourceRange(mLoc, Tok.getLocation()); 841 // Skip until we get a ; or {}. 842 SkipUntil(tok::r_brace); 843 return 0; 844 } 845 846 llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo; 847 if (Tok.isNot(tok::colon)) { 848 // If attributes exist after the method, parse them. 849 if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) 850 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(), 851 ParseGNUAttributes())); 852 853 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent); 854 Decl *Result 855 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(), 856 mType, IDecl, DSRet, ReturnType, Sel, 857 0, 858 CParamInfo.data(), CParamInfo.size(), 859 MethodAttrs.get(), 860 MethodImplKind); 861 PD.complete(Result); 862 return Result; 863 } 864 865 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; 866 llvm::SmallVector<Sema::ObjCArgInfo, 12> ArgInfos; 867 868 while (1) { 869 Sema::ObjCArgInfo ArgInfo; 870 871 // Each iteration parses a single keyword argument. 872 if (Tok.isNot(tok::colon)) { 873 Diag(Tok, diag::err_expected_colon); 874 break; 875 } 876 ConsumeToken(); // Eat the ':'. 877 878 ArgInfo.Type = ParsedType(); 879 if (Tok.is(tok::l_paren)) // Parse the argument type if present. 880 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, true); 881 882 // If attributes exist before the argument name, parse them. 883 ArgInfo.ArgAttrs = 0; 884 if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) 885 ArgInfo.ArgAttrs = ParseGNUAttributes(); 886 887 // Code completion for the next piece of the selector. 888 if (Tok.is(tok::code_completion)) { 889 ConsumeCodeCompletionToken(); 890 KeyIdents.push_back(SelIdent); 891 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(), 892 mType == tok::minus, 893 /*AtParameterName=*/true, 894 ReturnType, 895 KeyIdents.data(), 896 KeyIdents.size()); 897 KeyIdents.pop_back(); 898 break; 899 } 900 901 if (Tok.isNot(tok::identifier)) { 902 Diag(Tok, diag::err_expected_ident); // missing argument name. 903 break; 904 } 905 906 ArgInfo.Name = Tok.getIdentifierInfo(); 907 ArgInfo.NameLoc = Tok.getLocation(); 908 ConsumeToken(); // Eat the identifier. 909 910 ArgInfos.push_back(ArgInfo); 911 KeyIdents.push_back(SelIdent); 912 913 // Code completion for the next piece of the selector. 914 if (Tok.is(tok::code_completion)) { 915 ConsumeCodeCompletionToken(); 916 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(), 917 mType == tok::minus, 918 /*AtParameterName=*/false, 919 ReturnType, 920 KeyIdents.data(), 921 KeyIdents.size()); 922 break; 923 } 924 925 // Check for another keyword selector. 926 SourceLocation Loc; 927 SelIdent = ParseObjCSelectorPiece(Loc); 928 if (!SelIdent && Tok.isNot(tok::colon)) 929 break; 930 // We have a selector or a colon, continue parsing. 931 } 932 933 bool isVariadic = false; 934 935 // Parse the (optional) parameter list. 936 while (Tok.is(tok::comma)) { 937 ConsumeToken(); 938 if (Tok.is(tok::ellipsis)) { 939 isVariadic = true; 940 ConsumeToken(); 941 break; 942 } 943 DeclSpec DS; 944 ParseDeclarationSpecifiers(DS); 945 // Parse the declarator. 946 Declarator ParmDecl(DS, Declarator::PrototypeContext); 947 ParseDeclarator(ParmDecl); 948 IdentifierInfo *ParmII = ParmDecl.getIdentifier(); 949 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); 950 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, 951 ParmDecl.getIdentifierLoc(), 952 Param, 953 0)); 954 955 } 956 957 // FIXME: Add support for optional parmameter list... 958 // If attributes exist after the method, parse them. 959 if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) 960 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(), 961 ParseGNUAttributes())); 962 963 if (KeyIdents.size() == 0) 964 return 0; 965 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(), 966 &KeyIdents[0]); 967 Decl *Result 968 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(), 969 mType, IDecl, DSRet, ReturnType, Sel, 970 &ArgInfos[0], 971 CParamInfo.data(), CParamInfo.size(), 972 MethodAttrs.get(), 973 MethodImplKind, isVariadic); 974 PD.complete(Result); 975 976 // Delete referenced AttributeList objects. 977 for (llvm::SmallVectorImpl<Sema::ObjCArgInfo>::iterator 978 I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I) 979 delete I->ArgAttrs; 980 981 return Result; 982 } 983 984 /// objc-protocol-refs: 985 /// '<' identifier-list '>' 986 /// 987 bool Parser:: 988 ParseObjCProtocolReferences(llvm::SmallVectorImpl<Decl *> &Protocols, 989 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs, 990 bool WarnOnDeclarations, 991 SourceLocation &LAngleLoc, SourceLocation &EndLoc) { 992 assert(Tok.is(tok::less) && "expected <"); 993 994 LAngleLoc = ConsumeToken(); // the "<" 995 996 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents; 997 998 while (1) { 999 if (Tok.is(tok::code_completion)) { 1000 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(), 1001 ProtocolIdents.size()); 1002 ConsumeCodeCompletionToken(); 1003 } 1004 1005 if (Tok.isNot(tok::identifier)) { 1006 Diag(Tok, diag::err_expected_ident); 1007 SkipUntil(tok::greater); 1008 return true; 1009 } 1010 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(), 1011 Tok.getLocation())); 1012 ProtocolLocs.push_back(Tok.getLocation()); 1013 ConsumeToken(); 1014 1015 if (Tok.isNot(tok::comma)) 1016 break; 1017 ConsumeToken(); 1018 } 1019 1020 // Consume the '>'. 1021 if (Tok.isNot(tok::greater)) { 1022 Diag(Tok, diag::err_expected_greater); 1023 return true; 1024 } 1025 1026 EndLoc = ConsumeAnyToken(); 1027 1028 // Convert the list of protocols identifiers into a list of protocol decls. 1029 Actions.FindProtocolDeclaration(WarnOnDeclarations, 1030 &ProtocolIdents[0], ProtocolIdents.size(), 1031 Protocols); 1032 return false; 1033 } 1034 1035 /// objc-class-instance-variables: 1036 /// '{' objc-instance-variable-decl-list[opt] '}' 1037 /// 1038 /// objc-instance-variable-decl-list: 1039 /// objc-visibility-spec 1040 /// objc-instance-variable-decl ';' 1041 /// ';' 1042 /// objc-instance-variable-decl-list objc-visibility-spec 1043 /// objc-instance-variable-decl-list objc-instance-variable-decl ';' 1044 /// objc-instance-variable-decl-list ';' 1045 /// 1046 /// objc-visibility-spec: 1047 /// @private 1048 /// @protected 1049 /// @public 1050 /// @package [OBJC2] 1051 /// 1052 /// objc-instance-variable-decl: 1053 /// struct-declaration 1054 /// 1055 void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl, 1056 tok::ObjCKeywordKind visibility, 1057 SourceLocation atLoc) { 1058 assert(Tok.is(tok::l_brace) && "expected {"); 1059 llvm::SmallVector<Decl *, 32> AllIvarDecls; 1060 1061 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope); 1062 1063 SourceLocation LBraceLoc = ConsumeBrace(); // the "{" 1064 1065 // While we still have something to read, read the instance variables. 1066 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 1067 // Each iteration of this loop reads one objc-instance-variable-decl. 1068 1069 // Check for extraneous top-level semicolon. 1070 if (Tok.is(tok::semi)) { 1071 Diag(Tok, diag::ext_extra_ivar_semi) 1072 << FixItHint::CreateRemoval(Tok.getLocation()); 1073 ConsumeToken(); 1074 continue; 1075 } 1076 1077 // Set the default visibility to private. 1078 if (Tok.is(tok::at)) { // parse objc-visibility-spec 1079 ConsumeToken(); // eat the @ sign 1080 1081 if (Tok.is(tok::code_completion)) { 1082 Actions.CodeCompleteObjCAtVisibility(getCurScope()); 1083 ConsumeCodeCompletionToken(); 1084 } 1085 1086 switch (Tok.getObjCKeywordID()) { 1087 case tok::objc_private: 1088 case tok::objc_public: 1089 case tok::objc_protected: 1090 case tok::objc_package: 1091 visibility = Tok.getObjCKeywordID(); 1092 ConsumeToken(); 1093 continue; 1094 default: 1095 Diag(Tok, diag::err_objc_illegal_visibility_spec); 1096 continue; 1097 } 1098 } 1099 1100 if (Tok.is(tok::code_completion)) { 1101 Actions.CodeCompleteOrdinaryName(getCurScope(), 1102 Sema::PCC_ObjCInstanceVariableList); 1103 ConsumeCodeCompletionToken(); 1104 } 1105 1106 struct ObjCIvarCallback : FieldCallback { 1107 Parser &P; 1108 Decl *IDecl; 1109 tok::ObjCKeywordKind visibility; 1110 llvm::SmallVectorImpl<Decl *> &AllIvarDecls; 1111 1112 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V, 1113 llvm::SmallVectorImpl<Decl *> &AllIvarDecls) : 1114 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) { 1115 } 1116 1117 Decl *invoke(FieldDeclarator &FD) { 1118 // Install the declarator into the interface decl. 1119 Decl *Field 1120 = P.Actions.ActOnIvar(P.getCurScope(), 1121 FD.D.getDeclSpec().getSourceRange().getBegin(), 1122 IDecl, FD.D, FD.BitfieldSize, visibility); 1123 if (Field) 1124 AllIvarDecls.push_back(Field); 1125 return Field; 1126 } 1127 } Callback(*this, interfaceDecl, visibility, AllIvarDecls); 1128 1129 // Parse all the comma separated declarators. 1130 DeclSpec DS; 1131 ParseStructDeclaration(DS, Callback); 1132 1133 if (Tok.is(tok::semi)) { 1134 ConsumeToken(); 1135 } else { 1136 Diag(Tok, diag::err_expected_semi_decl_list); 1137 // Skip to end of block or statement 1138 SkipUntil(tok::r_brace, true, true); 1139 } 1140 } 1141 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); 1142 Actions.ActOnLastBitfield(RBraceLoc, interfaceDecl, AllIvarDecls); 1143 // Call ActOnFields() even if we don't have any decls. This is useful 1144 // for code rewriting tools that need to be aware of the empty list. 1145 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl, 1146 AllIvarDecls.data(), AllIvarDecls.size(), 1147 LBraceLoc, RBraceLoc, 0); 1148 return; 1149 } 1150 1151 /// objc-protocol-declaration: 1152 /// objc-protocol-definition 1153 /// objc-protocol-forward-reference 1154 /// 1155 /// objc-protocol-definition: 1156 /// @protocol identifier 1157 /// objc-protocol-refs[opt] 1158 /// objc-interface-decl-list 1159 /// @end 1160 /// 1161 /// objc-protocol-forward-reference: 1162 /// @protocol identifier-list ';' 1163 /// 1164 /// "@protocol identifier ;" should be resolved as "@protocol 1165 /// identifier-list ;": objc-interface-decl-list may not start with a 1166 /// semicolon in the first alternative if objc-protocol-refs are omitted. 1167 Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc, 1168 AttributeList *attrList) { 1169 assert(Tok.isObjCAtKeyword(tok::objc_protocol) && 1170 "ParseObjCAtProtocolDeclaration(): Expected @protocol"); 1171 ConsumeToken(); // the "protocol" identifier 1172 1173 if (Tok.is(tok::code_completion)) { 1174 Actions.CodeCompleteObjCProtocolDecl(getCurScope()); 1175 ConsumeCodeCompletionToken(); 1176 } 1177 1178 if (Tok.isNot(tok::identifier)) { 1179 Diag(Tok, diag::err_expected_ident); // missing protocol name. 1180 return 0; 1181 } 1182 // Save the protocol name, then consume it. 1183 IdentifierInfo *protocolName = Tok.getIdentifierInfo(); 1184 SourceLocation nameLoc = ConsumeToken(); 1185 1186 if (Tok.is(tok::semi)) { // forward declaration of one protocol. 1187 IdentifierLocPair ProtoInfo(protocolName, nameLoc); 1188 ConsumeToken(); 1189 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1, 1190 attrList); 1191 } 1192 1193 if (Tok.is(tok::comma)) { // list of forward declarations. 1194 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs; 1195 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc)); 1196 1197 // Parse the list of forward declarations. 1198 while (1) { 1199 ConsumeToken(); // the ',' 1200 if (Tok.isNot(tok::identifier)) { 1201 Diag(Tok, diag::err_expected_ident); 1202 SkipUntil(tok::semi); 1203 return 0; 1204 } 1205 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(), 1206 Tok.getLocation())); 1207 ConsumeToken(); // the identifier 1208 1209 if (Tok.isNot(tok::comma)) 1210 break; 1211 } 1212 // Consume the ';'. 1213 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol")) 1214 return 0; 1215 1216 return Actions.ActOnForwardProtocolDeclaration(AtLoc, 1217 &ProtocolRefs[0], 1218 ProtocolRefs.size(), 1219 attrList); 1220 } 1221 1222 // Last, and definitely not least, parse a protocol declaration. 1223 SourceLocation LAngleLoc, EndProtoLoc; 1224 1225 llvm::SmallVector<Decl *, 8> ProtocolRefs; 1226 llvm::SmallVector<SourceLocation, 8> ProtocolLocs; 1227 if (Tok.is(tok::less) && 1228 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, 1229 LAngleLoc, EndProtoLoc)) 1230 return 0; 1231 1232 Decl *ProtoType = 1233 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc, 1234 ProtocolRefs.data(), 1235 ProtocolRefs.size(), 1236 ProtocolLocs.data(), 1237 EndProtoLoc, attrList); 1238 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol); 1239 return ProtoType; 1240 } 1241 1242 /// objc-implementation: 1243 /// objc-class-implementation-prologue 1244 /// objc-category-implementation-prologue 1245 /// 1246 /// objc-class-implementation-prologue: 1247 /// @implementation identifier objc-superclass[opt] 1248 /// objc-class-instance-variables[opt] 1249 /// 1250 /// objc-category-implementation-prologue: 1251 /// @implementation identifier ( identifier ) 1252 Decl *Parser::ParseObjCAtImplementationDeclaration( 1253 SourceLocation atLoc) { 1254 assert(Tok.isObjCAtKeyword(tok::objc_implementation) && 1255 "ParseObjCAtImplementationDeclaration(): Expected @implementation"); 1256 ConsumeToken(); // the "implementation" identifier 1257 1258 // Code completion after '@implementation'. 1259 if (Tok.is(tok::code_completion)) { 1260 Actions.CodeCompleteObjCImplementationDecl(getCurScope()); 1261 ConsumeCodeCompletionToken(); 1262 } 1263 1264 if (Tok.isNot(tok::identifier)) { 1265 Diag(Tok, diag::err_expected_ident); // missing class or category name. 1266 return 0; 1267 } 1268 // We have a class or category name - consume it. 1269 IdentifierInfo *nameId = Tok.getIdentifierInfo(); 1270 SourceLocation nameLoc = ConsumeToken(); // consume class or category name 1271 1272 if (Tok.is(tok::l_paren)) { 1273 // we have a category implementation. 1274 SourceLocation lparenLoc = ConsumeParen(); 1275 SourceLocation categoryLoc, rparenLoc; 1276 IdentifierInfo *categoryId = 0; 1277 1278 if (Tok.is(tok::code_completion)) { 1279 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc); 1280 ConsumeCodeCompletionToken(); 1281 } 1282 1283 if (Tok.is(tok::identifier)) { 1284 categoryId = Tok.getIdentifierInfo(); 1285 categoryLoc = ConsumeToken(); 1286 } else { 1287 Diag(Tok, diag::err_expected_ident); // missing category name. 1288 return 0; 1289 } 1290 if (Tok.isNot(tok::r_paren)) { 1291 Diag(Tok, diag::err_expected_rparen); 1292 SkipUntil(tok::r_paren, false); // don't stop at ';' 1293 return 0; 1294 } 1295 rparenLoc = ConsumeParen(); 1296 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation( 1297 atLoc, nameId, nameLoc, categoryId, 1298 categoryLoc); 1299 ObjCImpDecl = ImplCatType; 1300 PendingObjCImpDecl.push_back(ObjCImpDecl); 1301 return 0; 1302 } 1303 // We have a class implementation 1304 SourceLocation superClassLoc; 1305 IdentifierInfo *superClassId = 0; 1306 if (Tok.is(tok::colon)) { 1307 // We have a super class 1308 ConsumeToken(); 1309 if (Tok.isNot(tok::identifier)) { 1310 Diag(Tok, diag::err_expected_ident); // missing super class name. 1311 return 0; 1312 } 1313 superClassId = Tok.getIdentifierInfo(); 1314 superClassLoc = ConsumeToken(); // Consume super class name 1315 } 1316 Decl *ImplClsType = Actions.ActOnStartClassImplementation( 1317 atLoc, nameId, nameLoc, 1318 superClassId, superClassLoc); 1319 1320 if (Tok.is(tok::l_brace)) // we have ivars 1321 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, 1322 tok::objc_private, atLoc); 1323 ObjCImpDecl = ImplClsType; 1324 PendingObjCImpDecl.push_back(ObjCImpDecl); 1325 1326 return 0; 1327 } 1328 1329 Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) { 1330 assert(Tok.isObjCAtKeyword(tok::objc_end) && 1331 "ParseObjCAtEndDeclaration(): Expected @end"); 1332 Decl *Result = ObjCImpDecl; 1333 ConsumeToken(); // the "end" identifier 1334 if (ObjCImpDecl) { 1335 Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl); 1336 ObjCImpDecl = 0; 1337 PendingObjCImpDecl.pop_back(); 1338 } 1339 else { 1340 // missing @implementation 1341 Diag(atEnd.getBegin(), diag::warn_expected_implementation); 1342 } 1343 return Result; 1344 } 1345 1346 Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() { 1347 Actions.DiagnoseUseOfUnimplementedSelectors(); 1348 if (PendingObjCImpDecl.empty()) 1349 return Actions.ConvertDeclToDeclGroup(0); 1350 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val(); 1351 Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl); 1352 return Actions.ConvertDeclToDeclGroup(ImpDecl); 1353 } 1354 1355 /// compatibility-alias-decl: 1356 /// @compatibility_alias alias-name class-name ';' 1357 /// 1358 Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) { 1359 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) && 1360 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias"); 1361 ConsumeToken(); // consume compatibility_alias 1362 if (Tok.isNot(tok::identifier)) { 1363 Diag(Tok, diag::err_expected_ident); 1364 return 0; 1365 } 1366 IdentifierInfo *aliasId = Tok.getIdentifierInfo(); 1367 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name 1368 if (Tok.isNot(tok::identifier)) { 1369 Diag(Tok, diag::err_expected_ident); 1370 return 0; 1371 } 1372 IdentifierInfo *classId = Tok.getIdentifierInfo(); 1373 SourceLocation classLoc = ConsumeToken(); // consume class-name; 1374 if (Tok.isNot(tok::semi)) { 1375 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias"; 1376 return 0; 1377 } 1378 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc, 1379 classId, classLoc); 1380 } 1381 1382 /// property-synthesis: 1383 /// @synthesize property-ivar-list ';' 1384 /// 1385 /// property-ivar-list: 1386 /// property-ivar 1387 /// property-ivar-list ',' property-ivar 1388 /// 1389 /// property-ivar: 1390 /// identifier 1391 /// identifier '=' identifier 1392 /// 1393 Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) { 1394 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) && 1395 "ParseObjCPropertyDynamic(): Expected '@synthesize'"); 1396 SourceLocation loc = ConsumeToken(); // consume synthesize 1397 1398 while (true) { 1399 if (Tok.is(tok::code_completion)) { 1400 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl); 1401 ConsumeCodeCompletionToken(); 1402 } 1403 1404 if (Tok.isNot(tok::identifier)) { 1405 Diag(Tok, diag::err_synthesized_property_name); 1406 SkipUntil(tok::semi); 1407 return 0; 1408 } 1409 1410 IdentifierInfo *propertyIvar = 0; 1411 IdentifierInfo *propertyId = Tok.getIdentifierInfo(); 1412 SourceLocation propertyLoc = ConsumeToken(); // consume property name 1413 if (Tok.is(tok::equal)) { 1414 // property '=' ivar-name 1415 ConsumeToken(); // consume '=' 1416 1417 if (Tok.is(tok::code_completion)) { 1418 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId, 1419 ObjCImpDecl); 1420 ConsumeCodeCompletionToken(); 1421 } 1422 1423 if (Tok.isNot(tok::identifier)) { 1424 Diag(Tok, diag::err_expected_ident); 1425 break; 1426 } 1427 propertyIvar = Tok.getIdentifierInfo(); 1428 ConsumeToken(); // consume ivar-name 1429 } 1430 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl, 1431 propertyId, propertyIvar); 1432 if (Tok.isNot(tok::comma)) 1433 break; 1434 ConsumeToken(); // consume ',' 1435 } 1436 if (Tok.isNot(tok::semi)) { 1437 Diag(Tok, diag::err_expected_semi_after) << "@synthesize"; 1438 SkipUntil(tok::semi); 1439 } 1440 else 1441 ConsumeToken(); // consume ';' 1442 return 0; 1443 } 1444 1445 /// property-dynamic: 1446 /// @dynamic property-list 1447 /// 1448 /// property-list: 1449 /// identifier 1450 /// property-list ',' identifier 1451 /// 1452 Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) { 1453 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) && 1454 "ParseObjCPropertyDynamic(): Expected '@dynamic'"); 1455 SourceLocation loc = ConsumeToken(); // consume dynamic 1456 while (true) { 1457 if (Tok.is(tok::code_completion)) { 1458 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl); 1459 ConsumeCodeCompletionToken(); 1460 } 1461 1462 if (Tok.isNot(tok::identifier)) { 1463 Diag(Tok, diag::err_expected_ident); 1464 SkipUntil(tok::semi); 1465 return 0; 1466 } 1467 1468 IdentifierInfo *propertyId = Tok.getIdentifierInfo(); 1469 SourceLocation propertyLoc = ConsumeToken(); // consume property name 1470 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl, 1471 propertyId, 0); 1472 1473 if (Tok.isNot(tok::comma)) 1474 break; 1475 ConsumeToken(); // consume ',' 1476 } 1477 if (Tok.isNot(tok::semi)) { 1478 Diag(Tok, diag::err_expected_semi_after) << "@dynamic"; 1479 SkipUntil(tok::semi); 1480 } 1481 else 1482 ConsumeToken(); // consume ';' 1483 return 0; 1484 } 1485 1486 /// objc-throw-statement: 1487 /// throw expression[opt]; 1488 /// 1489 StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) { 1490 ExprResult Res; 1491 ConsumeToken(); // consume throw 1492 if (Tok.isNot(tok::semi)) { 1493 Res = ParseExpression(); 1494 if (Res.isInvalid()) { 1495 SkipUntil(tok::semi); 1496 return StmtError(); 1497 } 1498 } 1499 // consume ';' 1500 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw"); 1501 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope()); 1502 } 1503 1504 /// objc-synchronized-statement: 1505 /// @synchronized '(' expression ')' compound-statement 1506 /// 1507 StmtResult 1508 Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) { 1509 ConsumeToken(); // consume synchronized 1510 if (Tok.isNot(tok::l_paren)) { 1511 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized"; 1512 return StmtError(); 1513 } 1514 ConsumeParen(); // '(' 1515 ExprResult Res(ParseExpression()); 1516 if (Res.isInvalid()) { 1517 SkipUntil(tok::semi); 1518 return StmtError(); 1519 } 1520 if (Tok.isNot(tok::r_paren)) { 1521 Diag(Tok, diag::err_expected_lbrace); 1522 return StmtError(); 1523 } 1524 ConsumeParen(); // ')' 1525 if (Tok.isNot(tok::l_brace)) { 1526 Diag(Tok, diag::err_expected_lbrace); 1527 return StmtError(); 1528 } 1529 // Enter a scope to hold everything within the compound stmt. Compound 1530 // statements can always hold declarations. 1531 ParseScope BodyScope(this, Scope::DeclScope); 1532 1533 StmtResult SynchBody(ParseCompoundStatementBody()); 1534 1535 BodyScope.Exit(); 1536 if (SynchBody.isInvalid()) 1537 SynchBody = Actions.ActOnNullStmt(Tok.getLocation()); 1538 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.take(), SynchBody.take()); 1539 } 1540 1541 /// objc-try-catch-statement: 1542 /// @try compound-statement objc-catch-list[opt] 1543 /// @try compound-statement objc-catch-list[opt] @finally compound-statement 1544 /// 1545 /// objc-catch-list: 1546 /// @catch ( parameter-declaration ) compound-statement 1547 /// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement 1548 /// catch-parameter-declaration: 1549 /// parameter-declaration 1550 /// '...' [OBJC2] 1551 /// 1552 StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) { 1553 bool catch_or_finally_seen = false; 1554 1555 ConsumeToken(); // consume try 1556 if (Tok.isNot(tok::l_brace)) { 1557 Diag(Tok, diag::err_expected_lbrace); 1558 return StmtError(); 1559 } 1560 StmtVector CatchStmts(Actions); 1561 StmtResult FinallyStmt; 1562 ParseScope TryScope(this, Scope::DeclScope); 1563 StmtResult TryBody(ParseCompoundStatementBody()); 1564 TryScope.Exit(); 1565 if (TryBody.isInvalid()) 1566 TryBody = Actions.ActOnNullStmt(Tok.getLocation()); 1567 1568 while (Tok.is(tok::at)) { 1569 // At this point, we need to lookahead to determine if this @ is the start 1570 // of an @catch or @finally. We don't want to consume the @ token if this 1571 // is an @try or @encode or something else. 1572 Token AfterAt = GetLookAheadToken(1); 1573 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) && 1574 !AfterAt.isObjCAtKeyword(tok::objc_finally)) 1575 break; 1576 1577 SourceLocation AtCatchFinallyLoc = ConsumeToken(); 1578 if (Tok.isObjCAtKeyword(tok::objc_catch)) { 1579 Decl *FirstPart = 0; 1580 ConsumeToken(); // consume catch 1581 if (Tok.is(tok::l_paren)) { 1582 ConsumeParen(); 1583 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope); 1584 if (Tok.isNot(tok::ellipsis)) { 1585 DeclSpec DS; 1586 ParseDeclarationSpecifiers(DS); 1587 // For some odd reason, the name of the exception variable is 1588 // optional. As a result, we need to use "PrototypeContext", because 1589 // we must accept either 'declarator' or 'abstract-declarator' here. 1590 Declarator ParmDecl(DS, Declarator::PrototypeContext); 1591 ParseDeclarator(ParmDecl); 1592 1593 // Inform the actions module about the declarator, so it 1594 // gets added to the current scope. 1595 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl); 1596 } else 1597 ConsumeToken(); // consume '...' 1598 1599 SourceLocation RParenLoc; 1600 1601 if (Tok.is(tok::r_paren)) 1602 RParenLoc = ConsumeParen(); 1603 else // Skip over garbage, until we get to ')'. Eat the ')'. 1604 SkipUntil(tok::r_paren, true, false); 1605 1606 StmtResult CatchBody(true); 1607 if (Tok.is(tok::l_brace)) 1608 CatchBody = ParseCompoundStatementBody(); 1609 else 1610 Diag(Tok, diag::err_expected_lbrace); 1611 if (CatchBody.isInvalid()) 1612 CatchBody = Actions.ActOnNullStmt(Tok.getLocation()); 1613 1614 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, 1615 RParenLoc, 1616 FirstPart, 1617 CatchBody.take()); 1618 if (!Catch.isInvalid()) 1619 CatchStmts.push_back(Catch.release()); 1620 1621 } else { 1622 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after) 1623 << "@catch clause"; 1624 return StmtError(); 1625 } 1626 catch_or_finally_seen = true; 1627 } else { 1628 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?"); 1629 ConsumeToken(); // consume finally 1630 ParseScope FinallyScope(this, Scope::DeclScope); 1631 1632 StmtResult FinallyBody(true); 1633 if (Tok.is(tok::l_brace)) 1634 FinallyBody = ParseCompoundStatementBody(); 1635 else 1636 Diag(Tok, diag::err_expected_lbrace); 1637 if (FinallyBody.isInvalid()) 1638 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation()); 1639 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc, 1640 FinallyBody.take()); 1641 catch_or_finally_seen = true; 1642 break; 1643 } 1644 } 1645 if (!catch_or_finally_seen) { 1646 Diag(atLoc, diag::err_missing_catch_finally); 1647 return StmtError(); 1648 } 1649 1650 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(), 1651 move_arg(CatchStmts), 1652 FinallyStmt.take()); 1653 } 1654 1655 /// objc-method-def: objc-method-proto ';'[opt] '{' body '}' 1656 /// 1657 Decl *Parser::ParseObjCMethodDefinition() { 1658 Decl *MDecl = ParseObjCMethodPrototype(ObjCImpDecl); 1659 1660 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(), 1661 "parsing Objective-C method"); 1662 1663 // parse optional ';' 1664 if (Tok.is(tok::semi)) { 1665 if (ObjCImpDecl) { 1666 Diag(Tok, diag::warn_semicolon_before_method_body) 1667 << FixItHint::CreateRemoval(Tok.getLocation()); 1668 } 1669 ConsumeToken(); 1670 } 1671 1672 // We should have an opening brace now. 1673 if (Tok.isNot(tok::l_brace)) { 1674 Diag(Tok, diag::err_expected_method_body); 1675 1676 // Skip over garbage, until we get to '{'. Don't eat the '{'. 1677 SkipUntil(tok::l_brace, true, true); 1678 1679 // If we didn't find the '{', bail out. 1680 if (Tok.isNot(tok::l_brace)) 1681 return 0; 1682 } 1683 SourceLocation BraceLoc = Tok.getLocation(); 1684 1685 // Enter a scope for the method body. 1686 ParseScope BodyScope(this, 1687 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope); 1688 1689 // Tell the actions module that we have entered a method definition with the 1690 // specified Declarator for the method. 1691 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl); 1692 1693 StmtResult FnBody(ParseCompoundStatementBody()); 1694 1695 // If the function body could not be parsed, make a bogus compoundstmt. 1696 if (FnBody.isInvalid()) 1697 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 1698 MultiStmtArg(Actions), false); 1699 1700 // TODO: Pass argument information. 1701 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take()); 1702 1703 // Leave the function body scope. 1704 BodyScope.Exit(); 1705 1706 return MDecl; 1707 } 1708 1709 StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) { 1710 if (Tok.is(tok::code_completion)) { 1711 Actions.CodeCompleteObjCAtStatement(getCurScope()); 1712 ConsumeCodeCompletionToken(); 1713 return StmtError(); 1714 } 1715 1716 if (Tok.isObjCAtKeyword(tok::objc_try)) 1717 return ParseObjCTryStmt(AtLoc); 1718 1719 if (Tok.isObjCAtKeyword(tok::objc_throw)) 1720 return ParseObjCThrowStmt(AtLoc); 1721 1722 if (Tok.isObjCAtKeyword(tok::objc_synchronized)) 1723 return ParseObjCSynchronizedStmt(AtLoc); 1724 1725 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc)); 1726 if (Res.isInvalid()) { 1727 // If the expression is invalid, skip ahead to the next semicolon. Not 1728 // doing this opens us up to the possibility of infinite loops if 1729 // ParseExpression does not consume any tokens. 1730 SkipUntil(tok::semi); 1731 return StmtError(); 1732 } 1733 1734 // Otherwise, eat the semicolon. 1735 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr); 1736 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take())); 1737 } 1738 1739 ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) { 1740 switch (Tok.getKind()) { 1741 case tok::code_completion: 1742 Actions.CodeCompleteObjCAtExpression(getCurScope()); 1743 ConsumeCodeCompletionToken(); 1744 return ExprError(); 1745 1746 case tok::string_literal: // primary-expression: string-literal 1747 case tok::wide_string_literal: 1748 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc)); 1749 default: 1750 if (Tok.getIdentifierInfo() == 0) 1751 return ExprError(Diag(AtLoc, diag::err_unexpected_at)); 1752 1753 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) { 1754 case tok::objc_encode: 1755 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc)); 1756 case tok::objc_protocol: 1757 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc)); 1758 case tok::objc_selector: 1759 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc)); 1760 default: 1761 return ExprError(Diag(AtLoc, diag::err_unexpected_at)); 1762 } 1763 } 1764 } 1765 1766 /// \brirg Parse the receiver of an Objective-C++ message send. 1767 /// 1768 /// This routine parses the receiver of a message send in 1769 /// Objective-C++ either as a type or as an expression. Note that this 1770 /// routine must not be called to parse a send to 'super', since it 1771 /// has no way to return such a result. 1772 /// 1773 /// \param IsExpr Whether the receiver was parsed as an expression. 1774 /// 1775 /// \param TypeOrExpr If the receiver was parsed as an expression (\c 1776 /// IsExpr is true), the parsed expression. If the receiver was parsed 1777 /// as a type (\c IsExpr is false), the parsed type. 1778 /// 1779 /// \returns True if an error occurred during parsing or semantic 1780 /// analysis, in which case the arguments do not have valid 1781 /// values. Otherwise, returns false for a successful parse. 1782 /// 1783 /// objc-receiver: [C++] 1784 /// 'super' [not parsed here] 1785 /// expression 1786 /// simple-type-specifier 1787 /// typename-specifier 1788 bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) { 1789 InMessageExpressionRAIIObject InMessage(*this, true); 1790 1791 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) || 1792 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)) 1793 TryAnnotateTypeOrScopeToken(); 1794 1795 if (!isCXXSimpleTypeSpecifier()) { 1796 // objc-receiver: 1797 // expression 1798 ExprResult Receiver = ParseExpression(); 1799 if (Receiver.isInvalid()) 1800 return true; 1801 1802 IsExpr = true; 1803 TypeOrExpr = Receiver.take(); 1804 return false; 1805 } 1806 1807 // objc-receiver: 1808 // typename-specifier 1809 // simple-type-specifier 1810 // expression (that starts with one of the above) 1811 DeclSpec DS; 1812 ParseCXXSimpleTypeSpecifier(DS); 1813 1814 if (Tok.is(tok::l_paren)) { 1815 // If we see an opening parentheses at this point, we are 1816 // actually parsing an expression that starts with a 1817 // function-style cast, e.g., 1818 // 1819 // postfix-expression: 1820 // simple-type-specifier ( expression-list [opt] ) 1821 // typename-specifier ( expression-list [opt] ) 1822 // 1823 // Parse the remainder of this case, then the (optional) 1824 // postfix-expression suffix, followed by the (optional) 1825 // right-hand side of the binary expression. We have an 1826 // instance method. 1827 ExprResult Receiver = ParseCXXTypeConstructExpression(DS); 1828 if (!Receiver.isInvalid()) 1829 Receiver = ParsePostfixExpressionSuffix(Receiver.take()); 1830 if (!Receiver.isInvalid()) 1831 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma); 1832 if (Receiver.isInvalid()) 1833 return true; 1834 1835 IsExpr = true; 1836 TypeOrExpr = Receiver.take(); 1837 return false; 1838 } 1839 1840 // We have a class message. Turn the simple-type-specifier or 1841 // typename-specifier we parsed into a type and parse the 1842 // remainder of the class message. 1843 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); 1844 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); 1845 if (Type.isInvalid()) 1846 return true; 1847 1848 IsExpr = false; 1849 TypeOrExpr = Type.get().getAsOpaquePtr(); 1850 return false; 1851 } 1852 1853 /// \brief Determine whether the parser is currently referring to a an 1854 /// Objective-C message send, using a simplified heuristic to avoid overhead. 1855 /// 1856 /// This routine will only return true for a subset of valid message-send 1857 /// expressions. 1858 bool Parser::isSimpleObjCMessageExpression() { 1859 assert(Tok.is(tok::l_square) && getLang().ObjC1 && 1860 "Incorrect start for isSimpleObjCMessageExpression"); 1861 return GetLookAheadToken(1).is(tok::identifier) && 1862 GetLookAheadToken(2).is(tok::identifier); 1863 } 1864 1865 /// objc-message-expr: 1866 /// '[' objc-receiver objc-message-args ']' 1867 /// 1868 /// objc-receiver: [C] 1869 /// 'super' 1870 /// expression 1871 /// class-name 1872 /// type-name 1873 /// 1874 ExprResult Parser::ParseObjCMessageExpression() { 1875 assert(Tok.is(tok::l_square) && "'[' expected"); 1876 SourceLocation LBracLoc = ConsumeBracket(); // consume '[' 1877 1878 if (Tok.is(tok::code_completion)) { 1879 Actions.CodeCompleteObjCMessageReceiver(getCurScope()); 1880 ConsumeCodeCompletionToken(); 1881 SkipUntil(tok::r_square); 1882 return ExprError(); 1883 } 1884 1885 InMessageExpressionRAIIObject InMessage(*this, true); 1886 1887 if (getLang().CPlusPlus) { 1888 // We completely separate the C and C++ cases because C++ requires 1889 // more complicated (read: slower) parsing. 1890 1891 // Handle send to super. 1892 // FIXME: This doesn't benefit from the same typo-correction we 1893 // get in Objective-C. 1894 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super && 1895 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope()) 1896 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), 1897 ParsedType(), 0); 1898 1899 // Parse the receiver, which is either a type or an expression. 1900 bool IsExpr; 1901 void *TypeOrExpr; 1902 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) { 1903 SkipUntil(tok::r_square); 1904 return ExprError(); 1905 } 1906 1907 if (IsExpr) 1908 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 1909 ParsedType(), 1910 static_cast<Expr*>(TypeOrExpr)); 1911 1912 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 1913 ParsedType::getFromOpaquePtr(TypeOrExpr), 1914 0); 1915 } 1916 1917 if (Tok.is(tok::identifier)) { 1918 IdentifierInfo *Name = Tok.getIdentifierInfo(); 1919 SourceLocation NameLoc = Tok.getLocation(); 1920 ParsedType ReceiverType; 1921 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc, 1922 Name == Ident_super, 1923 NextToken().is(tok::period), 1924 ReceiverType)) { 1925 case Sema::ObjCSuperMessage: 1926 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), 1927 ParsedType(), 0); 1928 1929 case Sema::ObjCClassMessage: 1930 if (!ReceiverType) { 1931 SkipUntil(tok::r_square); 1932 return ExprError(); 1933 } 1934 1935 ConsumeToken(); // the type name 1936 1937 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 1938 ReceiverType, 0); 1939 1940 case Sema::ObjCInstanceMessage: 1941 // Fall through to parse an expression. 1942 break; 1943 } 1944 } 1945 1946 // Otherwise, an arbitrary expression can be the receiver of a send. 1947 ExprResult Res(ParseExpression()); 1948 if (Res.isInvalid()) { 1949 SkipUntil(tok::r_square); 1950 return move(Res); 1951 } 1952 1953 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 1954 ParsedType(), Res.take()); 1955 } 1956 1957 /// \brief Parse the remainder of an Objective-C message following the 1958 /// '[' objc-receiver. 1959 /// 1960 /// This routine handles sends to super, class messages (sent to a 1961 /// class name), and instance messages (sent to an object), and the 1962 /// target is represented by \p SuperLoc, \p ReceiverType, or \p 1963 /// ReceiverExpr, respectively. Only one of these parameters may have 1964 /// a valid value. 1965 /// 1966 /// \param LBracLoc The location of the opening '['. 1967 /// 1968 /// \param SuperLoc If this is a send to 'super', the location of the 1969 /// 'super' keyword that indicates a send to the superclass. 1970 /// 1971 /// \param ReceiverType If this is a class message, the type of the 1972 /// class we are sending a message to. 1973 /// 1974 /// \param ReceiverExpr If this is an instance message, the expression 1975 /// used to compute the receiver object. 1976 /// 1977 /// objc-message-args: 1978 /// objc-selector 1979 /// objc-keywordarg-list 1980 /// 1981 /// objc-keywordarg-list: 1982 /// objc-keywordarg 1983 /// objc-keywordarg-list objc-keywordarg 1984 /// 1985 /// objc-keywordarg: 1986 /// selector-name[opt] ':' objc-keywordexpr 1987 /// 1988 /// objc-keywordexpr: 1989 /// nonempty-expr-list 1990 /// 1991 /// nonempty-expr-list: 1992 /// assignment-expression 1993 /// nonempty-expr-list , assignment-expression 1994 /// 1995 ExprResult 1996 Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc, 1997 SourceLocation SuperLoc, 1998 ParsedType ReceiverType, 1999 ExprArg ReceiverExpr) { 2000 InMessageExpressionRAIIObject InMessage(*this, true); 2001 2002 if (Tok.is(tok::code_completion)) { 2003 if (SuperLoc.isValid()) 2004 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0); 2005 else if (ReceiverType) 2006 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0); 2007 else 2008 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr, 2009 0, 0); 2010 ConsumeCodeCompletionToken(); 2011 } 2012 2013 // Parse objc-selector 2014 SourceLocation Loc; 2015 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc); 2016 2017 SourceLocation SelectorLoc = Loc; 2018 2019 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; 2020 ExprVector KeyExprs(Actions); 2021 2022 if (Tok.is(tok::colon)) { 2023 while (1) { 2024 // Each iteration parses a single keyword argument. 2025 KeyIdents.push_back(selIdent); 2026 2027 if (Tok.isNot(tok::colon)) { 2028 Diag(Tok, diag::err_expected_colon); 2029 // We must manually skip to a ']', otherwise the expression skipper will 2030 // stop at the ']' when it skips to the ';'. We want it to skip beyond 2031 // the enclosing expression. 2032 SkipUntil(tok::r_square); 2033 return ExprError(); 2034 } 2035 2036 ConsumeToken(); // Eat the ':'. 2037 /// Parse the expression after ':' 2038 ExprResult Res(ParseAssignmentExpression()); 2039 if (Res.isInvalid()) { 2040 // We must manually skip to a ']', otherwise the expression skipper will 2041 // stop at the ']' when it skips to the ';'. We want it to skip beyond 2042 // the enclosing expression. 2043 SkipUntil(tok::r_square); 2044 return move(Res); 2045 } 2046 2047 // We have a valid expression. 2048 KeyExprs.push_back(Res.release()); 2049 2050 // Code completion after each argument. 2051 if (Tok.is(tok::code_completion)) { 2052 if (SuperLoc.isValid()) 2053 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 2054 KeyIdents.data(), 2055 KeyIdents.size()); 2056 else if (ReceiverType) 2057 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 2058 KeyIdents.data(), 2059 KeyIdents.size()); 2060 else 2061 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr, 2062 KeyIdents.data(), 2063 KeyIdents.size()); 2064 ConsumeCodeCompletionToken(); 2065 } 2066 2067 // Check for another keyword selector. 2068 selIdent = ParseObjCSelectorPiece(Loc); 2069 if (!selIdent && Tok.isNot(tok::colon)) 2070 break; 2071 // We have a selector or a colon, continue parsing. 2072 } 2073 // Parse the, optional, argument list, comma separated. 2074 while (Tok.is(tok::comma)) { 2075 ConsumeToken(); // Eat the ','. 2076 /// Parse the expression after ',' 2077 ExprResult Res(ParseAssignmentExpression()); 2078 if (Res.isInvalid()) { 2079 // We must manually skip to a ']', otherwise the expression skipper will 2080 // stop at the ']' when it skips to the ';'. We want it to skip beyond 2081 // the enclosing expression. 2082 SkipUntil(tok::r_square); 2083 return move(Res); 2084 } 2085 2086 // We have a valid expression. 2087 KeyExprs.push_back(Res.release()); 2088 } 2089 } else if (!selIdent) { 2090 Diag(Tok, diag::err_expected_ident); // missing selector name. 2091 2092 // We must manually skip to a ']', otherwise the expression skipper will 2093 // stop at the ']' when it skips to the ';'. We want it to skip beyond 2094 // the enclosing expression. 2095 SkipUntil(tok::r_square); 2096 return ExprError(); 2097 } 2098 2099 if (Tok.isNot(tok::r_square)) { 2100 if (Tok.is(tok::identifier)) 2101 Diag(Tok, diag::err_expected_colon); 2102 else 2103 Diag(Tok, diag::err_expected_rsquare); 2104 // We must manually skip to a ']', otherwise the expression skipper will 2105 // stop at the ']' when it skips to the ';'. We want it to skip beyond 2106 // the enclosing expression. 2107 SkipUntil(tok::r_square); 2108 return ExprError(); 2109 } 2110 2111 SourceLocation RBracLoc = ConsumeBracket(); // consume ']' 2112 2113 unsigned nKeys = KeyIdents.size(); 2114 if (nKeys == 0) 2115 KeyIdents.push_back(selIdent); 2116 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]); 2117 2118 if (SuperLoc.isValid()) 2119 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel, 2120 LBracLoc, SelectorLoc, RBracLoc, 2121 MultiExprArg(Actions, 2122 KeyExprs.take(), 2123 KeyExprs.size())); 2124 else if (ReceiverType) 2125 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel, 2126 LBracLoc, SelectorLoc, RBracLoc, 2127 MultiExprArg(Actions, 2128 KeyExprs.take(), 2129 KeyExprs.size())); 2130 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel, 2131 LBracLoc, SelectorLoc, RBracLoc, 2132 MultiExprArg(Actions, 2133 KeyExprs.take(), 2134 KeyExprs.size())); 2135 } 2136 2137 ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) { 2138 ExprResult Res(ParseStringLiteralExpression()); 2139 if (Res.isInvalid()) return move(Res); 2140 2141 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string 2142 // expressions. At this point, we know that the only valid thing that starts 2143 // with '@' is an @"". 2144 llvm::SmallVector<SourceLocation, 4> AtLocs; 2145 ExprVector AtStrings(Actions); 2146 AtLocs.push_back(AtLoc); 2147 AtStrings.push_back(Res.release()); 2148 2149 while (Tok.is(tok::at)) { 2150 AtLocs.push_back(ConsumeToken()); // eat the @. 2151 2152 // Invalid unless there is a string literal. 2153 if (!isTokenStringLiteral()) 2154 return ExprError(Diag(Tok, diag::err_objc_concat_string)); 2155 2156 ExprResult Lit(ParseStringLiteralExpression()); 2157 if (Lit.isInvalid()) 2158 return move(Lit); 2159 2160 AtStrings.push_back(Lit.release()); 2161 } 2162 2163 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(), 2164 AtStrings.size())); 2165 } 2166 2167 /// objc-encode-expression: 2168 /// @encode ( type-name ) 2169 ExprResult 2170 Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) { 2171 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!"); 2172 2173 SourceLocation EncLoc = ConsumeToken(); 2174 2175 if (Tok.isNot(tok::l_paren)) 2176 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode"); 2177 2178 SourceLocation LParenLoc = ConsumeParen(); 2179 2180 TypeResult Ty = ParseTypeName(); 2181 2182 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 2183 2184 if (Ty.isInvalid()) 2185 return ExprError(); 2186 2187 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, 2188 Ty.get(), RParenLoc)); 2189 } 2190 2191 /// objc-protocol-expression 2192 /// @protocol ( protocol-name ) 2193 ExprResult 2194 Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) { 2195 SourceLocation ProtoLoc = ConsumeToken(); 2196 2197 if (Tok.isNot(tok::l_paren)) 2198 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol"); 2199 2200 SourceLocation LParenLoc = ConsumeParen(); 2201 2202 if (Tok.isNot(tok::identifier)) 2203 return ExprError(Diag(Tok, diag::err_expected_ident)); 2204 2205 IdentifierInfo *protocolId = Tok.getIdentifierInfo(); 2206 ConsumeToken(); 2207 2208 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 2209 2210 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc, 2211 LParenLoc, RParenLoc)); 2212 } 2213 2214 /// objc-selector-expression 2215 /// @selector '(' objc-keyword-selector ')' 2216 ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) { 2217 SourceLocation SelectorLoc = ConsumeToken(); 2218 2219 if (Tok.isNot(tok::l_paren)) 2220 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector"); 2221 2222 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; 2223 SourceLocation LParenLoc = ConsumeParen(); 2224 SourceLocation sLoc; 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 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc); 2235 if (!SelIdent && // missing selector name. 2236 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon)) 2237 return ExprError(Diag(Tok, diag::err_expected_ident)); 2238 2239 KeyIdents.push_back(SelIdent); 2240 unsigned nColons = 0; 2241 if (Tok.isNot(tok::r_paren)) { 2242 while (1) { 2243 if (Tok.is(tok::coloncolon)) { // Handle :: in C++. 2244 ++nColons; 2245 KeyIdents.push_back(0); 2246 } else if (Tok.isNot(tok::colon)) 2247 return ExprError(Diag(Tok, diag::err_expected_colon)); 2248 2249 ++nColons; 2250 ConsumeToken(); // Eat the ':'. 2251 if (Tok.is(tok::r_paren)) 2252 break; 2253 2254 if (Tok.is(tok::code_completion)) { 2255 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(), 2256 KeyIdents.size()); 2257 ConsumeCodeCompletionToken(); 2258 MatchRHSPunctuation(tok::r_paren, LParenLoc); 2259 return ExprError(); 2260 } 2261 2262 // Check for another keyword selector. 2263 SourceLocation Loc; 2264 SelIdent = ParseObjCSelectorPiece(Loc); 2265 KeyIdents.push_back(SelIdent); 2266 if (!SelIdent && Tok.isNot(tok::colon)) 2267 break; 2268 } 2269 } 2270 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 2271 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]); 2272 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, 2273 LParenLoc, RParenLoc)); 2274 } 2275