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