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