Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- ParseObjc.cpp - Objective C Parsing ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Steve Naroff and is distributed under |
| 6 | // the University of Illinois Open Source 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" |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 15 | #include "clang/Parse/DeclSpec.h" |
Fariborz Jahanian | 0679836 | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 16 | #include "clang/Parse/Scope.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 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 |
Steve Naroff | 5b96e2e | 2007-10-29 21:39:29 +0000 | [diff] [blame] | 25 | /// [OBJC] objc-class-declaration |
| 26 | /// [OBJC] objc-alias-declaration |
| 27 | /// [OBJC] objc-protocol-definition |
| 28 | /// [OBJC] objc-method-definition |
| 29 | /// [OBJC] '@' 'end' |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 30 | Parser::DeclTy *Parser::ParseObjCAtDirectives() { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 31 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
| 32 | |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 33 | switch (Tok.getObjCKeywordID()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 34 | case tok::objc_class: |
| 35 | return ParseObjCAtClassDeclaration(AtLoc); |
| 36 | case tok::objc_interface: |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 37 | return ParseObjCAtInterfaceDeclaration(AtLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 38 | case tok::objc_protocol: |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 39 | return ParseObjCAtProtocolDeclaration(AtLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 40 | case tok::objc_implementation: |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 41 | return ObjcImpDecl = ParseObjCAtImplementationDeclaration(AtLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 42 | case tok::objc_end: |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 43 | return ParseObjCAtEndDeclaration(AtLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 44 | case tok::objc_compatibility_alias: |
Fariborz Jahanian | b62aff3 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 45 | return ParseObjCAtAliasDeclaration(AtLoc); |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 46 | case tok::objc_synthesize: |
| 47 | return ParseObjCPropertySynthesize(AtLoc); |
| 48 | case tok::objc_dynamic: |
| 49 | return ParseObjCPropertyDynamic(AtLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 50 | default: |
| 51 | Diag(AtLoc, diag::err_unexpected_at); |
| 52 | SkipUntil(tok::semi); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 53 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | /// |
| 58 | /// objc-class-declaration: |
| 59 | /// '@' 'class' identifier-list ';' |
| 60 | /// |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 61 | Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 62 | ConsumeToken(); // the identifier "class" |
| 63 | llvm::SmallVector<IdentifierInfo *, 8> ClassNames; |
| 64 | |
| 65 | while (1) { |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 66 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 67 | Diag(Tok, diag::err_expected_ident); |
| 68 | SkipUntil(tok::semi); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 69 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 70 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 71 | ClassNames.push_back(Tok.getIdentifierInfo()); |
| 72 | ConsumeToken(); |
| 73 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 74 | if (Tok.isNot(tok::comma)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 75 | break; |
| 76 | |
| 77 | ConsumeToken(); |
| 78 | } |
| 79 | |
| 80 | // Consume the ';'. |
| 81 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class")) |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 82 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 83 | |
Steve Naroff | 415c183 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 84 | return Actions.ActOnForwardClassDeclaration(atLoc, |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 85 | &ClassNames[0], ClassNames.size()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 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) { |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 118 | assert(Tok.isObjCAtKeyword(tok::objc_interface) && |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 119 | "ParseObjCAtInterfaceDeclaration(): Expected @interface"); |
| 120 | ConsumeToken(); // the "interface" identifier |
| 121 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 122 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 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. |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 127 | IdentifierInfo *nameId = Tok.getIdentifierInfo(); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 128 | SourceLocation nameLoc = ConsumeToken(); |
| 129 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 130 | if (Tok.is(tok::l_paren)) { // we have a category. |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 131 | SourceLocation lparenLoc = ConsumeParen(); |
| 132 | SourceLocation categoryLoc, rparenLoc; |
| 133 | IdentifierInfo *categoryId = 0; |
Fariborz Jahanian | f25220e | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 134 | llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs; |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 135 | |
Steve Naroff | a7f6278 | 2007-08-23 19:56:30 +0000 | [diff] [blame] | 136 | // For ObjC2, the category name is optional (not an error). |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 137 | if (Tok.is(tok::identifier)) { |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 138 | categoryId = Tok.getIdentifierInfo(); |
| 139 | categoryLoc = ConsumeToken(); |
Steve Naroff | a7f6278 | 2007-08-23 19:56:30 +0000 | [diff] [blame] | 140 | } else if (!getLang().ObjC2) { |
| 141 | Diag(Tok, diag::err_expected_ident); // missing category name. |
| 142 | return 0; |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 143 | } |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 144 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 145 | Diag(Tok, diag::err_expected_rparen); |
| 146 | SkipUntil(tok::r_paren, false); // don't stop at ';' |
| 147 | return 0; |
| 148 | } |
| 149 | rparenLoc = ConsumeParen(); |
Steve Naroff | ef20ed3 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 150 | SourceLocation endProtoLoc; |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 151 | // Next, we need to check for any protocol references. |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 152 | if (Tok.is(tok::less)) { |
Steve Naroff | ef20ed3 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 153 | if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc)) |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | if (attrList) // categories don't support attributes. |
| 157 | Diag(Tok, diag::err_objc_no_attributes_on_category); |
| 158 | |
Steve Naroff | 415c183 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 159 | DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc, |
Steve Naroff | 25aace8 | 2007-10-03 21:00:46 +0000 | [diff] [blame] | 160 | nameId, nameLoc, categoryId, categoryLoc, |
Steve Naroff | 667f168 | 2007-10-30 13:30:57 +0000 | [diff] [blame] | 161 | &ProtocolRefs[0], ProtocolRefs.size(), |
| 162 | endProtoLoc); |
Fariborz Jahanian | f25220e | 2007-09-18 20:26:58 +0000 | [diff] [blame] | 163 | |
| 164 | ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 165 | |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 166 | // The @ sign was already consumed by ParseObjCInterfaceDeclList(). |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 167 | if (Tok.isObjCAtKeyword(tok::objc_end)) { |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 168 | ConsumeToken(); // the "end" identifier |
Fariborz Jahanian | ac20be2 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 169 | return CategoryType; |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 170 | } |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 171 | Diag(Tok, diag::err_objc_missing_end); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | // Parse a class interface. |
| 175 | IdentifierInfo *superClassId = 0; |
| 176 | SourceLocation superClassLoc; |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 177 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 178 | if (Tok.is(tok::colon)) { // a super class is specified. |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 179 | ConsumeToken(); |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 180 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 181 | Diag(Tok, diag::err_expected_ident); // missing super class name. |
| 182 | return 0; |
| 183 | } |
| 184 | superClassId = Tok.getIdentifierInfo(); |
| 185 | superClassLoc = ConsumeToken(); |
| 186 | } |
| 187 | // Next, we need to check for any protocol references. |
Steve Naroff | 304ed39 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 188 | llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs; |
Steve Naroff | ef20ed3 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 189 | SourceLocation endProtoLoc; |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 190 | if (Tok.is(tok::less)) { |
Steve Naroff | ef20ed3 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 191 | if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc)) |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 192 | return 0; |
| 193 | } |
Steve Naroff | 415c183 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 194 | DeclTy *ClsType = Actions.ActOnStartClassInterface( |
Fariborz Jahanian | c091b5d | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 195 | atLoc, nameId, nameLoc, |
Steve Naroff | 304ed39 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 196 | superClassId, superClassLoc, &ProtocolRefs[0], |
Steve Naroff | ef20ed3 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 197 | ProtocolRefs.size(), endProtoLoc, attrList); |
Steve Naroff | 304ed39 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 198 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 199 | if (Tok.is(tok::l_brace)) |
Steve Naroff | 1a7fa7b | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 200 | ParseObjCClassInstanceVariables(ClsType, atLoc); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 201 | |
Fariborz Jahanian | 63ca8ae | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 202 | ParseObjCInterfaceDeclList(ClsType, tok::objc_interface); |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 203 | |
| 204 | // The @ sign was already consumed by ParseObjCInterfaceDeclList(). |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 205 | if (Tok.isObjCAtKeyword(tok::objc_end)) { |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 206 | ConsumeToken(); // the "end" identifier |
Steve Naroff | faed3bf | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 207 | return ClsType; |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 208 | } |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 209 | Diag(Tok, diag::err_objc_missing_end); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | /// objc-interface-decl-list: |
| 214 | /// empty |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 215 | /// objc-interface-decl-list objc-property-decl [OBJC2] |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 216 | /// objc-interface-decl-list objc-method-requirement [OBJC2] |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 217 | /// objc-interface-decl-list objc-method-proto ';' |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 218 | /// objc-interface-decl-list declaration |
| 219 | /// objc-interface-decl-list ';' |
| 220 | /// |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 221 | /// objc-method-requirement: [OBJC2] |
| 222 | /// @required |
| 223 | /// @optional |
| 224 | /// |
Fariborz Jahanian | 63ca8ae | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 225 | void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl, |
| 226 | tok::ObjCKeywordKind contextKey) { |
Fariborz Jahanian | 3dc7cbc | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 227 | llvm::SmallVector<DeclTy*, 32> allMethods; |
Fariborz Jahanian | 8b5ab6f | 2007-09-18 00:25:23 +0000 | [diff] [blame] | 228 | tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword; |
Steve Naroff | 1a7fa7b | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 229 | SourceLocation AtEndLoc; |
| 230 | |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 231 | while (1) { |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 232 | if (Tok.is(tok::at)) { |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 233 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 234 | tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID(); |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 235 | |
| 236 | if (ocKind == tok::objc_end) { // terminate list |
Steve Naroff | 1a7fa7b | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 237 | AtEndLoc = AtLoc; |
Fariborz Jahanian | 3dc7cbc | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 238 | break; |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 239 | } else if (ocKind == tok::objc_required) { // protocols only |
| 240 | ConsumeToken(); |
Fariborz Jahanian | 8b5ab6f | 2007-09-18 00:25:23 +0000 | [diff] [blame] | 241 | MethodImplKind = ocKind; |
Fariborz Jahanian | 63ca8ae | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 242 | if (contextKey != tok::objc_protocol) |
| 243 | Diag(AtLoc, diag::err_objc_protocol_required); |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 244 | } else if (ocKind == tok::objc_optional) { // protocols only |
| 245 | ConsumeToken(); |
Fariborz Jahanian | 8b5ab6f | 2007-09-18 00:25:23 +0000 | [diff] [blame] | 246 | MethodImplKind = ocKind; |
Fariborz Jahanian | 63ca8ae | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 247 | if (contextKey != tok::objc_protocol) |
| 248 | Diag(AtLoc, diag::err_objc_protocol_optional); |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 249 | } else if (ocKind == tok::objc_property) { |
Fariborz Jahanian | 86f74a4 | 2007-09-12 18:23:47 +0000 | [diff] [blame] | 250 | ParseObjCPropertyDecl(interfaceDecl); |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 251 | continue; |
| 252 | } else { |
| 253 | Diag(Tok, diag::err_objc_illegal_interface_qual); |
| 254 | ConsumeToken(); |
| 255 | } |
| 256 | } |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 257 | if (Tok.is(tok::minus) || Tok.is(tok::plus)) { |
| 258 | DeclTy *methodPrototype = |
| 259 | ParseObjCMethodPrototype(interfaceDecl, MethodImplKind); |
Fariborz Jahanian | 63ca8ae | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 260 | allMethods.push_back(methodPrototype); |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 261 | // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for |
| 262 | // method definitions. |
Steve Naroff | aa1b6d4 | 2007-09-17 15:07:43 +0000 | [diff] [blame] | 263 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto"); |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 264 | continue; |
| 265 | } |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 266 | if (Tok.is(tok::semi)) |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 267 | ConsumeToken(); |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 268 | else if (Tok.is(tok::eof)) |
Fariborz Jahanian | 3dc7cbc | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 269 | break; |
Steve Naroff | 304ed39 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 270 | else { |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 271 | // FIXME: as the name implies, this rule allows function definitions. |
| 272 | // We could pass a flag or check for functions during semantic analysis. |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 273 | ParseDeclarationOrFunctionDefinition(); |
Steve Naroff | 304ed39 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 274 | } |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 275 | } |
Steve Naroff | 1ccf463 | 2007-10-30 03:43:13 +0000 | [diff] [blame] | 276 | /// Insert collected methods declarations into the @interface object. |
| 277 | /// This action is executed even if we don't have any methods (so the @end |
| 278 | /// can be recorded properly). |
| 279 | Actions.ActOnAddMethodsToObjcDecl(CurScope, interfaceDecl, &allMethods[0], |
| 280 | allMethods.size(), AtEndLoc); |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Fariborz Jahanian | 6668b8c | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 283 | /// Parse property attribute declarations. |
| 284 | /// |
| 285 | /// property-attr-decl: '(' property-attrlist ')' |
| 286 | /// property-attrlist: |
| 287 | /// property-attribute |
| 288 | /// property-attrlist ',' property-attribute |
| 289 | /// property-attribute: |
| 290 | /// getter '=' identifier |
| 291 | /// setter '=' identifier ':' |
| 292 | /// readonly |
| 293 | /// readwrite |
| 294 | /// assign |
| 295 | /// retain |
| 296 | /// copy |
| 297 | /// nonatomic |
| 298 | /// |
| 299 | void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) { |
| 300 | SourceLocation loc = ConsumeParen(); // consume '(' |
| 301 | while (isObjCPropertyAttribute()) { |
| 302 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 303 | // getter/setter require extra treatment. |
| 304 | if (II == ObjcPropertyAttrs[objc_getter] || |
| 305 | II == ObjcPropertyAttrs[objc_setter]) { |
| 306 | // skip getter/setter part. |
| 307 | SourceLocation loc = ConsumeToken(); |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 308 | if (Tok.is(tok::equal)) { |
Fariborz Jahanian | 6668b8c | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 309 | loc = ConsumeToken(); |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 310 | if (Tok.is(tok::identifier)) { |
Fariborz Jahanian | 6668b8c | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 311 | if (II == ObjcPropertyAttrs[objc_setter]) { |
| 312 | loc = ConsumeToken(); // consume method name |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 313 | if (Tok.isNot(tok::colon)) { |
Fariborz Jahanian | 6668b8c | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 314 | Diag(loc, diag::err_expected_colon); |
| 315 | SkipUntil(tok::r_paren,true,true); |
| 316 | break; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | else { |
| 321 | Diag(loc, diag::err_expected_ident); |
| 322 | SkipUntil(tok::r_paren,true,true); |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | else { |
| 327 | Diag(loc, diag::err_objc_expected_equal); |
| 328 | SkipUntil(tok::r_paren,true,true); |
| 329 | break; |
| 330 | } |
| 331 | } |
| 332 | ConsumeToken(); // consume last attribute token |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 333 | if (Tok.is(tok::comma)) { |
Fariborz Jahanian | 6668b8c | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 334 | loc = ConsumeToken(); |
| 335 | continue; |
| 336 | } |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 337 | if (Tok.is(tok::r_paren)) |
Fariborz Jahanian | 6668b8c | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 338 | break; |
| 339 | Diag(loc, diag::err_expected_rparen); |
| 340 | SkipUntil(tok::semi); |
| 341 | return; |
| 342 | } |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 343 | if (Tok.is(tok::r_paren)) |
Fariborz Jahanian | 6668b8c | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 344 | ConsumeParen(); |
| 345 | else { |
| 346 | Diag(loc, diag::err_objc_expected_property_attr); |
| 347 | SkipUntil(tok::r_paren); // recover from error inside attribute list |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /// Main routine to parse property declaration. |
| 352 | /// |
| 353 | /// @property property-attr-decl[opt] property-component-decl ';' |
| 354 | /// |
| 355 | void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) { |
| 356 | assert(Tok.isObjCAtKeyword(tok::objc_property) && |
| 357 | "ParseObjCPropertyDecl(): Expected @property"); |
| 358 | ConsumeToken(); // the "property" identifier |
| 359 | // Parse property attribute list, if any. |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 360 | if (Tok.is(tok::l_paren)) { |
Fariborz Jahanian | 6668b8c | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 361 | // property has attribute list. |
| 362 | ParseObjCPropertyAttribute(0/*FIXME*/); |
| 363 | } |
| 364 | // Parse declaration portion of @property. |
| 365 | llvm::SmallVector<DeclTy*, 32> PropertyDecls; |
| 366 | ParseStructDeclaration(interfaceDecl, PropertyDecls); |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 367 | if (Tok.is(tok::semi)) |
Fariborz Jahanian | 6668b8c | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 368 | ConsumeToken(); |
| 369 | else { |
| 370 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 371 | SkipUntil(tok::r_brace, true, true); |
| 372 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 373 | } |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 374 | |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 375 | /// objc-method-proto: |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 376 | /// objc-instance-method objc-method-decl objc-method-attributes[opt] |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 377 | /// objc-class-method objc-method-decl objc-method-attributes[opt] |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 378 | /// |
| 379 | /// objc-instance-method: '-' |
| 380 | /// objc-class-method: '+' |
| 381 | /// |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 382 | /// objc-method-attributes: [OBJC2] |
| 383 | /// __attribute__((deprecated)) |
| 384 | /// |
Fariborz Jahanian | 8b5ab6f | 2007-09-18 00:25:23 +0000 | [diff] [blame] | 385 | Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl, |
| 386 | tok::ObjCKeywordKind MethodImplKind) { |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 387 | assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-"); |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 388 | |
| 389 | tok::TokenKind methodType = Tok.getKind(); |
Steve Naroff | 3774dd9 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 390 | SourceLocation mLoc = ConsumeToken(); |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 391 | |
Steve Naroff | 3774dd9 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 392 | DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind); |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 393 | // Since this rule is used for both method declarations and definitions, |
Steve Naroff | faed3bf | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 394 | // the caller is (optionally) responsible for consuming the ';'. |
Steve Naroff | 304ed39 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 395 | return MDecl; |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /// objc-selector: |
| 399 | /// identifier |
| 400 | /// one of |
| 401 | /// enum struct union if else while do for switch case default |
| 402 | /// break continue return goto asm sizeof typeof __alignof |
| 403 | /// unsigned long const short volatile signed restrict _Complex |
| 404 | /// in out inout bycopy byref oneway int char float double void _Bool |
| 405 | /// |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 406 | IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) { |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 407 | switch (Tok.getKind()) { |
| 408 | default: |
| 409 | return 0; |
| 410 | case tok::identifier: |
| 411 | case tok::kw_typeof: |
| 412 | case tok::kw___alignof: |
| 413 | case tok::kw_auto: |
| 414 | case tok::kw_break: |
| 415 | case tok::kw_case: |
| 416 | case tok::kw_char: |
| 417 | case tok::kw_const: |
| 418 | case tok::kw_continue: |
| 419 | case tok::kw_default: |
| 420 | case tok::kw_do: |
| 421 | case tok::kw_double: |
| 422 | case tok::kw_else: |
| 423 | case tok::kw_enum: |
| 424 | case tok::kw_extern: |
| 425 | case tok::kw_float: |
| 426 | case tok::kw_for: |
| 427 | case tok::kw_goto: |
| 428 | case tok::kw_if: |
| 429 | case tok::kw_inline: |
| 430 | case tok::kw_int: |
| 431 | case tok::kw_long: |
| 432 | case tok::kw_register: |
| 433 | case tok::kw_restrict: |
| 434 | case tok::kw_return: |
| 435 | case tok::kw_short: |
| 436 | case tok::kw_signed: |
| 437 | case tok::kw_sizeof: |
| 438 | case tok::kw_static: |
| 439 | case tok::kw_struct: |
| 440 | case tok::kw_switch: |
| 441 | case tok::kw_typedef: |
| 442 | case tok::kw_union: |
| 443 | case tok::kw_unsigned: |
| 444 | case tok::kw_void: |
| 445 | case tok::kw_volatile: |
| 446 | case tok::kw_while: |
| 447 | case tok::kw__Bool: |
| 448 | case tok::kw__Complex: |
| 449 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 450 | SelectorLoc = ConsumeToken(); |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 451 | return II; |
Fariborz Jahanian | 171ceb5 | 2007-09-27 19:52:15 +0000 | [diff] [blame] | 452 | } |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Fariborz Jahanian | 6668b8c | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 455 | /// property-attrlist: one of |
| 456 | /// readonly getter setter assign retain copy nonatomic |
| 457 | /// |
| 458 | bool Parser::isObjCPropertyAttribute() { |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 459 | if (Tok.is(tok::identifier)) { |
Fariborz Jahanian | 6668b8c | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 460 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 461 | for (unsigned i = 0; i < objc_NumAttrs; ++i) |
| 462 | if (II == ObjcPropertyAttrs[i]) return true; |
| 463 | } |
| 464 | return false; |
| 465 | } |
| 466 | |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 467 | /// objc-type-name: |
| 468 | /// '(' objc-type-qualifiers[opt] type-name ')' |
| 469 | /// '(' objc-type-qualifiers[opt] ')' |
| 470 | /// |
| 471 | /// objc-type-qualifiers: |
| 472 | /// objc-type-qualifier |
| 473 | /// objc-type-qualifiers objc-type-qualifier |
| 474 | /// |
Fariborz Jahanian | 2fd0daa | 2007-10-31 23:53:01 +0000 | [diff] [blame] | 475 | Parser::TypeTy *Parser::ParseObjCTypeName(ObjcDeclSpec &DS) { |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 476 | assert(Tok.is(tok::l_paren) && "expected ("); |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 477 | |
| 478 | SourceLocation LParenLoc = ConsumeParen(), RParenLoc; |
Chris Lattner | 265c817 | 2007-09-27 15:15:46 +0000 | [diff] [blame] | 479 | TypeTy *Ty = 0; |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 480 | |
Fariborz Jahanian | 6dab49b | 2007-10-31 21:59:43 +0000 | [diff] [blame] | 481 | // Parse type qualifiers, in, inout, etc. |
Fariborz Jahanian | 6dab49b | 2007-10-31 21:59:43 +0000 | [diff] [blame] | 482 | ParseObjcTypeQualifierList(DS); |
Steve Naroff | a8ee226 | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 483 | |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 484 | if (isTypeSpecifierQualifier()) { |
Steve Naroff | 304ed39 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 485 | Ty = ParseTypeName(); |
| 486 | // FIXME: back when Sema support is in place... |
| 487 | // assert(Ty && "Parser::ParseObjCTypeName(): missing type"); |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 488 | } |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 489 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 490 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 304ed39 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 491 | return 0; // FIXME: decide how we want to handle this error... |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 492 | } |
| 493 | RParenLoc = ConsumeParen(); |
Steve Naroff | 304ed39 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 494 | return Ty; |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /// objc-method-decl: |
| 498 | /// objc-selector |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 499 | /// objc-keyword-selector objc-parmlist[opt] |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 500 | /// objc-type-name objc-selector |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 501 | /// objc-type-name objc-keyword-selector objc-parmlist[opt] |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 502 | /// |
| 503 | /// objc-keyword-selector: |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 504 | /// objc-keyword-decl |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 505 | /// objc-keyword-selector objc-keyword-decl |
| 506 | /// |
| 507 | /// objc-keyword-decl: |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 508 | /// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier |
| 509 | /// objc-selector ':' objc-keyword-attributes[opt] identifier |
| 510 | /// ':' objc-type-name objc-keyword-attributes[opt] identifier |
| 511 | /// ':' objc-keyword-attributes[opt] identifier |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 512 | /// |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 513 | /// objc-parmlist: |
| 514 | /// objc-parms objc-ellipsis[opt] |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 515 | /// |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 516 | /// objc-parms: |
| 517 | /// objc-parms , parameter-declaration |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 518 | /// |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 519 | /// objc-ellipsis: |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 520 | /// , ... |
| 521 | /// |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 522 | /// objc-keyword-attributes: [OBJC2] |
| 523 | /// __attribute__((unused)) |
| 524 | /// |
Steve Naroff | 3774dd9 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 525 | Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc, |
| 526 | tok::TokenKind mType, |
Steve Naroff | 4ed9d66 | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 527 | tok::ObjCKeywordKind MethodImplKind) |
| 528 | { |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 529 | // Parse the return type. |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 530 | TypeTy *ReturnType = 0; |
Fariborz Jahanian | 2fd0daa | 2007-10-31 23:53:01 +0000 | [diff] [blame] | 531 | ObjcDeclSpec DSRet; |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 532 | if (Tok.is(tok::l_paren)) |
Fariborz Jahanian | 2fd0daa | 2007-10-31 23:53:01 +0000 | [diff] [blame] | 533 | ReturnType = ParseObjCTypeName(DSRet); |
Steve Naroff | 3774dd9 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 534 | SourceLocation selLoc; |
| 535 | IdentifierInfo *SelIdent = ParseObjCSelector(selLoc); |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 536 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 537 | if (!SelIdent) { |
| 538 | Diag(Tok, diag::err_expected_ident); // missing selector name. |
| 539 | // FIXME: this creates a unary selector with a null identifier, is this |
| 540 | // ok?? Maybe we should skip to the next semicolon or something. |
| 541 | } |
| 542 | |
| 543 | // If attributes exist after the method, parse them. |
| 544 | AttributeList *MethodAttrs = 0; |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 545 | if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 546 | MethodAttrs = ParseAttributes(); |
| 547 | |
| 548 | Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent); |
Steve Naroff | 3774dd9 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 549 | return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(), |
Fariborz Jahanian | 2fd0daa | 2007-10-31 23:53:01 +0000 | [diff] [blame] | 550 | mType, DSRet, ReturnType, Sel, |
| 551 | 0, 0, 0, MethodAttrs, MethodImplKind); |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 552 | } |
Steve Naroff | 304ed39 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 553 | |
Steve Naroff | 4ed9d66 | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 554 | llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; |
| 555 | llvm::SmallVector<Action::TypeTy *, 12> KeyTypes; |
Fariborz Jahanian | 2fd0daa | 2007-10-31 23:53:01 +0000 | [diff] [blame] | 556 | llvm::SmallVector<ObjcDeclSpec, 12> ArgTypeQuals; |
Steve Naroff | 4ed9d66 | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 557 | llvm::SmallVector<IdentifierInfo *, 12> ArgNames; |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 558 | |
| 559 | Action::TypeTy *TypeInfo; |
| 560 | while (1) { |
| 561 | KeyIdents.push_back(SelIdent); |
Steve Naroff | 4ed9d66 | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 562 | |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 563 | // Each iteration parses a single keyword argument. |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 564 | if (Tok.isNot(tok::colon)) { |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 565 | Diag(Tok, diag::err_expected_colon); |
| 566 | break; |
| 567 | } |
| 568 | ConsumeToken(); // Eat the ':'. |
Fariborz Jahanian | 2fd0daa | 2007-10-31 23:53:01 +0000 | [diff] [blame] | 569 | ObjcDeclSpec DSType; |
| 570 | if (Tok.is(tok::l_paren)) { // Parse the argument type. |
| 571 | TypeInfo = ParseObjCTypeName(DSType); |
| 572 | } |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 573 | else |
| 574 | TypeInfo = 0; |
| 575 | KeyTypes.push_back(TypeInfo); |
Fariborz Jahanian | 2fd0daa | 2007-10-31 23:53:01 +0000 | [diff] [blame] | 576 | ArgTypeQuals.push_back(DSType); |
Steve Naroff | 304ed39 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 577 | |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 578 | // If attributes exist before the argument name, parse them. |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 579 | if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 580 | ParseAttributes(); // FIXME: pass attributes through. |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 581 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 582 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 583 | Diag(Tok, diag::err_expected_ident); // missing argument name. |
| 584 | break; |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 585 | } |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 586 | ArgNames.push_back(Tok.getIdentifierInfo()); |
| 587 | ConsumeToken(); // Eat the identifier. |
Steve Naroff | f9e80db | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 588 | |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 589 | // Check for another keyword selector. |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 590 | SourceLocation Loc; |
| 591 | SelIdent = ParseObjCSelector(Loc); |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 592 | if (!SelIdent && Tok.isNot(tok::colon)) |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 593 | break; |
| 594 | // We have a selector or a colon, continue parsing. |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 595 | } |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 596 | |
| 597 | // Parse the (optional) parameter list. |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 598 | while (Tok.is(tok::comma)) { |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 599 | ConsumeToken(); |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 600 | if (Tok.is(tok::ellipsis)) { |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 601 | ConsumeToken(); |
| 602 | break; |
| 603 | } |
| 604 | // Parse the c-style argument declaration-specifier. |
| 605 | DeclSpec DS; |
| 606 | ParseDeclarationSpecifiers(DS); |
| 607 | // Parse the declarator. |
| 608 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 609 | ParseDeclarator(ParmDecl); |
| 610 | } |
| 611 | |
| 612 | // FIXME: Add support for optional parmameter list... |
Fariborz Jahanian | 3dc7cbc | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 613 | // If attributes exist after the method, parse them. |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 614 | AttributeList *MethodAttrs = 0; |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 615 | if (getLang().ObjC2 && Tok.is(tok::kw___attribute)) |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 616 | MethodAttrs = ParseAttributes(); |
| 617 | |
| 618 | Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(), |
| 619 | &KeyIdents[0]); |
Steve Naroff | 3774dd9 | 2007-10-26 20:53:56 +0000 | [diff] [blame] | 620 | return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(), |
Fariborz Jahanian | 2fd0daa | 2007-10-31 23:53:01 +0000 | [diff] [blame] | 621 | mType, DSRet, ReturnType, Sel, |
| 622 | &ArgTypeQuals[0], &KeyTypes[0], |
| 623 | &ArgNames[0], |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 624 | MethodAttrs, MethodImplKind); |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Fariborz Jahanian | c04aff1 | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 627 | /// CmpProtocolVals - Comparison predicate for sorting protocols. |
| 628 | static bool CmpProtocolVals(const IdentifierInfo* const& lhs, |
| 629 | const IdentifierInfo* const& rhs) { |
| 630 | return strcmp(lhs->getName(), rhs->getName()) < 0; |
| 631 | } |
| 632 | |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 633 | /// objc-protocol-refs: |
| 634 | /// '<' identifier-list '>' |
| 635 | /// |
Steve Naroff | 304ed39 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 636 | bool Parser::ParseObjCProtocolReferences( |
Steve Naroff | ef20ed3 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 637 | llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc) |
| 638 | { |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 639 | assert(Tok.is(tok::less) && "expected <"); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 640 | |
| 641 | ConsumeToken(); // the "<" |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 642 | |
| 643 | while (1) { |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 644 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 645 | Diag(Tok, diag::err_expected_ident); |
| 646 | SkipUntil(tok::greater); |
| 647 | return true; |
| 648 | } |
| 649 | ProtocolRefs.push_back(Tok.getIdentifierInfo()); |
| 650 | ConsumeToken(); |
| 651 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 652 | if (Tok.isNot(tok::comma)) |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 653 | break; |
| 654 | ConsumeToken(); |
| 655 | } |
Fariborz Jahanian | c04aff1 | 2007-10-08 23:06:41 +0000 | [diff] [blame] | 656 | |
| 657 | // Sort protocols, keyed by name. |
| 658 | // Later on, we remove duplicates. |
| 659 | std::stable_sort(ProtocolRefs.begin(), ProtocolRefs.end(), CmpProtocolVals); |
| 660 | |
| 661 | // Make protocol names unique. |
| 662 | ProtocolRefs.erase(std::unique(ProtocolRefs.begin(), ProtocolRefs.end()), |
| 663 | ProtocolRefs.end()); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 664 | // Consume the '>'. |
Steve Naroff | ef20ed3 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 665 | if (Tok.is(tok::greater)) { |
| 666 | endLoc = ConsumeAnyToken(); |
| 667 | return false; |
| 668 | } |
| 669 | Diag(Tok, diag::err_expected_greater); |
| 670 | return true; |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /// objc-class-instance-variables: |
| 674 | /// '{' objc-instance-variable-decl-list[opt] '}' |
| 675 | /// |
| 676 | /// objc-instance-variable-decl-list: |
| 677 | /// objc-visibility-spec |
| 678 | /// objc-instance-variable-decl ';' |
| 679 | /// ';' |
| 680 | /// objc-instance-variable-decl-list objc-visibility-spec |
| 681 | /// objc-instance-variable-decl-list objc-instance-variable-decl ';' |
| 682 | /// objc-instance-variable-decl-list ';' |
| 683 | /// |
| 684 | /// objc-visibility-spec: |
| 685 | /// @private |
| 686 | /// @protected |
| 687 | /// @public |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 688 | /// @package [OBJC2] |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 689 | /// |
| 690 | /// objc-instance-variable-decl: |
| 691 | /// struct-declaration |
| 692 | /// |
Steve Naroff | 1a7fa7b | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 693 | void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl, |
| 694 | SourceLocation atLoc) { |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 695 | assert(Tok.is(tok::l_brace) && "expected {"); |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 696 | llvm::SmallVector<DeclTy*, 16> IvarDecls; |
Fariborz Jahanian | 3957dae | 2007-09-13 20:56:13 +0000 | [diff] [blame] | 697 | llvm::SmallVector<DeclTy*, 32> AllIvarDecls; |
| 698 | llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities; |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 699 | |
| 700 | SourceLocation LBraceLoc = ConsumeBrace(); // the "{" |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 701 | |
Fariborz Jahanian | 3957dae | 2007-09-13 20:56:13 +0000 | [diff] [blame] | 702 | tok::ObjCKeywordKind visibility = tok::objc_private; |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 703 | // While we still have something to read, read the instance variables. |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 704 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 705 | // Each iteration of this loop reads one objc-instance-variable-decl. |
| 706 | |
| 707 | // Check for extraneous top-level semicolon. |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 708 | if (Tok.is(tok::semi)) { |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 709 | Diag(Tok, diag::ext_extra_struct_semi); |
| 710 | ConsumeToken(); |
| 711 | continue; |
| 712 | } |
| 713 | // Set the default visibility to private. |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 714 | if (Tok.is(tok::at)) { // parse objc-visibility-spec |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 715 | ConsumeToken(); // eat the @ sign |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 716 | switch (Tok.getObjCKeywordID()) { |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 717 | case tok::objc_private: |
| 718 | case tok::objc_public: |
| 719 | case tok::objc_protected: |
| 720 | case tok::objc_package: |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 721 | visibility = Tok.getObjCKeywordID(); |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 722 | ConsumeToken(); |
| 723 | continue; |
| 724 | default: |
| 725 | Diag(Tok, diag::err_objc_illegal_visibility_spec); |
| 726 | ConsumeToken(); |
| 727 | continue; |
| 728 | } |
| 729 | } |
| 730 | ParseStructDeclaration(interfaceDecl, IvarDecls); |
Fariborz Jahanian | 3957dae | 2007-09-13 20:56:13 +0000 | [diff] [blame] | 731 | for (unsigned i = 0; i < IvarDecls.size(); i++) { |
| 732 | AllIvarDecls.push_back(IvarDecls[i]); |
| 733 | AllVisibilities.push_back(visibility); |
| 734 | } |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 735 | IvarDecls.clear(); |
| 736 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 737 | if (Tok.is(tok::semi)) { |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 738 | ConsumeToken(); |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 739 | } else if (Tok.is(tok::r_brace)) { |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 740 | Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list); |
| 741 | break; |
| 742 | } else { |
| 743 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 744 | // Skip to end of block or statement |
| 745 | SkipUntil(tok::r_brace, true, true); |
| 746 | } |
| 747 | } |
Steve Naroff | 1a7fa7b | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 748 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Steve Naroff | 809b4f0 | 2007-10-31 22:11:35 +0000 | [diff] [blame] | 749 | // Call ActOnFields() even if we don't have any decls. This is useful |
| 750 | // for code rewriting tools that need to be aware of the empty list. |
| 751 | Actions.ActOnFields(CurScope, atLoc, interfaceDecl, |
| 752 | &AllIvarDecls[0], AllIvarDecls.size(), |
| 753 | LBraceLoc, RBraceLoc, &AllVisibilities[0]); |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 754 | return; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 755 | } |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 756 | |
| 757 | /// objc-protocol-declaration: |
| 758 | /// objc-protocol-definition |
| 759 | /// objc-protocol-forward-reference |
| 760 | /// |
| 761 | /// objc-protocol-definition: |
| 762 | /// @protocol identifier |
| 763 | /// objc-protocol-refs[opt] |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 764 | /// objc-interface-decl-list |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 765 | /// @end |
| 766 | /// |
| 767 | /// objc-protocol-forward-reference: |
| 768 | /// @protocol identifier-list ';' |
| 769 | /// |
| 770 | /// "@protocol identifier ;" should be resolved as "@protocol |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 771 | /// identifier-list ;": objc-interface-decl-list may not start with a |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 772 | /// semicolon in the first alternative if objc-protocol-refs are omitted. |
| 773 | |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 774 | Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) { |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 775 | assert(Tok.isObjCAtKeyword(tok::objc_protocol) && |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 776 | "ParseObjCAtProtocolDeclaration(): Expected @protocol"); |
| 777 | ConsumeToken(); // the "protocol" identifier |
| 778 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 779 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 780 | Diag(Tok, diag::err_expected_ident); // missing protocol name. |
| 781 | return 0; |
| 782 | } |
| 783 | // Save the protocol name, then consume it. |
| 784 | IdentifierInfo *protocolName = Tok.getIdentifierInfo(); |
| 785 | SourceLocation nameLoc = ConsumeToken(); |
| 786 | |
Fariborz Jahanian | c716c94 | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 787 | llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs; |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 788 | if (Tok.is(tok::semi)) { // forward declaration of one protocol. |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 789 | ConsumeToken(); |
Fariborz Jahanian | c716c94 | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 790 | ProtocolRefs.push_back(protocolName); |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 791 | } |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 792 | if (Tok.is(tok::comma)) { // list of forward declarations. |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 793 | // Parse the list of forward declarations. |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 794 | ProtocolRefs.push_back(protocolName); |
| 795 | |
| 796 | while (1) { |
| 797 | ConsumeToken(); // the ',' |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 798 | if (Tok.isNot(tok::identifier)) { |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 799 | Diag(Tok, diag::err_expected_ident); |
| 800 | SkipUntil(tok::semi); |
| 801 | return 0; |
| 802 | } |
| 803 | ProtocolRefs.push_back(Tok.getIdentifierInfo()); |
| 804 | ConsumeToken(); // the identifier |
| 805 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 806 | if (Tok.isNot(tok::comma)) |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 807 | break; |
| 808 | } |
| 809 | // Consume the ';'. |
| 810 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol")) |
| 811 | return 0; |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 812 | } |
Fariborz Jahanian | c716c94 | 2007-09-21 15:40:54 +0000 | [diff] [blame] | 813 | if (ProtocolRefs.size() > 0) |
Steve Naroff | 415c183 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 814 | return Actions.ActOnForwardProtocolDeclaration(AtLoc, |
Steve Naroff | b4dfe36 | 2007-10-02 22:39:18 +0000 | [diff] [blame] | 815 | &ProtocolRefs[0], |
| 816 | ProtocolRefs.size()); |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 817 | // Last, and definitely not least, parse a protocol declaration. |
Steve Naroff | ef20ed3 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 818 | SourceLocation endProtoLoc; |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 819 | if (Tok.is(tok::less)) { |
Steve Naroff | ef20ed3 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 820 | if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc)) |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 821 | return 0; |
| 822 | } |
Fariborz Jahanian | 63ca8ae | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 823 | |
Steve Naroff | 415c183 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 824 | DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc, |
Fariborz Jahanian | 63ca8ae | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 825 | protocolName, nameLoc, |
| 826 | &ProtocolRefs[0], |
Steve Naroff | ef20ed3 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 827 | ProtocolRefs.size(), endProtoLoc); |
Fariborz Jahanian | 63ca8ae | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 828 | ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol); |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 829 | |
| 830 | // The @ sign was already consumed by ParseObjCInterfaceDeclList(). |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 831 | if (Tok.isObjCAtKeyword(tok::objc_end)) { |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 832 | ConsumeToken(); // the "end" identifier |
Fariborz Jahanian | ac20be2 | 2007-10-08 18:53:38 +0000 | [diff] [blame] | 833 | return ProtoType; |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 834 | } |
| 835 | Diag(Tok, diag::err_objc_missing_end); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 836 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 837 | } |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 838 | |
| 839 | /// objc-implementation: |
| 840 | /// objc-class-implementation-prologue |
| 841 | /// objc-category-implementation-prologue |
| 842 | /// |
| 843 | /// objc-class-implementation-prologue: |
| 844 | /// @implementation identifier objc-superclass[opt] |
| 845 | /// objc-class-instance-variables[opt] |
| 846 | /// |
| 847 | /// objc-category-implementation-prologue: |
| 848 | /// @implementation identifier ( identifier ) |
| 849 | |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 850 | Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration( |
| 851 | SourceLocation atLoc) { |
| 852 | assert(Tok.isObjCAtKeyword(tok::objc_implementation) && |
| 853 | "ParseObjCAtImplementationDeclaration(): Expected @implementation"); |
| 854 | ConsumeToken(); // the "implementation" identifier |
| 855 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 856 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 857 | Diag(Tok, diag::err_expected_ident); // missing class or category name. |
| 858 | return 0; |
| 859 | } |
| 860 | // We have a class or category name - consume it. |
Fariborz Jahanian | c091b5d | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 861 | IdentifierInfo *nameId = Tok.getIdentifierInfo(); |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 862 | SourceLocation nameLoc = ConsumeToken(); // consume class or category name |
| 863 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 864 | if (Tok.is(tok::l_paren)) { |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 865 | // we have a category implementation. |
| 866 | SourceLocation lparenLoc = ConsumeParen(); |
| 867 | SourceLocation categoryLoc, rparenLoc; |
| 868 | IdentifierInfo *categoryId = 0; |
| 869 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 870 | if (Tok.is(tok::identifier)) { |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 871 | categoryId = Tok.getIdentifierInfo(); |
| 872 | categoryLoc = ConsumeToken(); |
| 873 | } else { |
| 874 | Diag(Tok, diag::err_expected_ident); // missing category name. |
| 875 | return 0; |
| 876 | } |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 877 | if (Tok.isNot(tok::r_paren)) { |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 878 | Diag(Tok, diag::err_expected_rparen); |
| 879 | SkipUntil(tok::r_paren, false); // don't stop at ';' |
| 880 | return 0; |
| 881 | } |
| 882 | rparenLoc = ConsumeParen(); |
Steve Naroff | 415c183 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 883 | DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation( |
Fariborz Jahanian | a91aa32 | 2007-10-02 16:38:50 +0000 | [diff] [blame] | 884 | atLoc, nameId, nameLoc, categoryId, |
| 885 | categoryLoc); |
| 886 | return ImplCatType; |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 887 | } |
| 888 | // We have a class implementation |
Fariborz Jahanian | c091b5d | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 889 | SourceLocation superClassLoc; |
| 890 | IdentifierInfo *superClassId = 0; |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 891 | if (Tok.is(tok::colon)) { |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 892 | // We have a super class |
| 893 | ConsumeToken(); |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 894 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 895 | Diag(Tok, diag::err_expected_ident); // missing super class name. |
| 896 | return 0; |
| 897 | } |
Fariborz Jahanian | c091b5d | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 898 | superClassId = Tok.getIdentifierInfo(); |
| 899 | superClassLoc = ConsumeToken(); // Consume super class name |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 900 | } |
Steve Naroff | 415c183 | 2007-10-10 17:32:04 +0000 | [diff] [blame] | 901 | DeclTy *ImplClsType = Actions.ActOnStartClassImplementation( |
Steve Naroff | 25aace8 | 2007-10-03 21:00:46 +0000 | [diff] [blame] | 902 | atLoc, nameId, nameLoc, |
Fariborz Jahanian | c091b5d | 2007-09-25 18:38:09 +0000 | [diff] [blame] | 903 | superClassId, superClassLoc); |
| 904 | |
Steve Naroff | 1a7fa7b | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 905 | if (Tok.is(tok::l_brace)) // we have ivars |
| 906 | ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc); |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 907 | |
Fariborz Jahanian | 1e4e82f | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 908 | return ImplClsType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 909 | } |
Steve Naroff | 1a7fa7b | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 910 | |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 911 | Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) { |
| 912 | assert(Tok.isObjCAtKeyword(tok::objc_end) && |
| 913 | "ParseObjCAtEndDeclaration(): Expected @end"); |
| 914 | ConsumeToken(); // the "end" identifier |
Fariborz Jahanian | 1e4e82f | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 915 | if (ObjcImpDecl) { |
| 916 | // Checking is not necessary except that a parse error might have caused |
| 917 | // @implementation not to have been parsed to completion and ObjcImpDecl |
| 918 | // could be 0. |
| 919 | /// Insert collected methods declarations into the @interface object. |
Steve Naroff | 25aace8 | 2007-10-03 21:00:46 +0000 | [diff] [blame] | 920 | Actions.ActOnAddMethodsToObjcDecl(CurScope, ObjcImpDecl, |
Steve Naroff | 1a7fa7b | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 921 | &AllImplMethods[0], AllImplMethods.size(), |
| 922 | atLoc); |
Fariborz Jahanian | 1e4e82f | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 923 | ObjcImpDecl = 0; |
| 924 | AllImplMethods.clear(); |
| 925 | } |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 926 | |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 927 | return 0; |
| 928 | } |
Fariborz Jahanian | b62aff3 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 929 | |
| 930 | /// compatibility-alias-decl: |
| 931 | /// @compatibility_alias alias-name class-name ';' |
| 932 | /// |
| 933 | Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) { |
| 934 | assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) && |
| 935 | "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias"); |
| 936 | ConsumeToken(); // consume compatibility_alias |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 937 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | b62aff3 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 938 | Diag(Tok, diag::err_expected_ident); |
| 939 | return 0; |
| 940 | } |
Fariborz Jahanian | 05d212a | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 941 | IdentifierInfo *aliasId = Tok.getIdentifierInfo(); |
| 942 | SourceLocation aliasLoc = ConsumeToken(); // consume alias-name |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 943 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | b62aff3 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 944 | Diag(Tok, diag::err_expected_ident); |
| 945 | return 0; |
| 946 | } |
Fariborz Jahanian | 05d212a | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 947 | IdentifierInfo *classId = Tok.getIdentifierInfo(); |
| 948 | SourceLocation classLoc = ConsumeToken(); // consume class-name; |
| 949 | if (Tok.isNot(tok::semi)) { |
Fariborz Jahanian | 6c30fa6 | 2007-09-04 21:42:12 +0000 | [diff] [blame] | 950 | Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias"); |
Fariborz Jahanian | 05d212a | 2007-10-11 23:42:27 +0000 | [diff] [blame] | 951 | return 0; |
| 952 | } |
| 953 | DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc, |
| 954 | aliasId, aliasLoc, |
| 955 | classId, classLoc); |
| 956 | return ClsType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 959 | /// property-synthesis: |
| 960 | /// @synthesize property-ivar-list ';' |
| 961 | /// |
| 962 | /// property-ivar-list: |
| 963 | /// property-ivar |
| 964 | /// property-ivar-list ',' property-ivar |
| 965 | /// |
| 966 | /// property-ivar: |
| 967 | /// identifier |
| 968 | /// identifier '=' identifier |
| 969 | /// |
| 970 | Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) { |
| 971 | assert(Tok.isObjCAtKeyword(tok::objc_synthesize) && |
| 972 | "ParseObjCPropertyDynamic(): Expected '@synthesize'"); |
| 973 | SourceLocation loc = ConsumeToken(); // consume dynamic |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 974 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 975 | Diag(Tok, diag::err_expected_ident); |
| 976 | return 0; |
| 977 | } |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 978 | while (Tok.is(tok::identifier)) { |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 979 | ConsumeToken(); // consume property name |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 980 | if (Tok.is(tok::equal)) { |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 981 | // property '=' ivar-name |
| 982 | ConsumeToken(); // consume '=' |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 983 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 984 | Diag(Tok, diag::err_expected_ident); |
| 985 | break; |
| 986 | } |
| 987 | ConsumeToken(); // consume ivar-name |
| 988 | } |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 989 | if (Tok.isNot(tok::comma)) |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 990 | break; |
| 991 | ConsumeToken(); // consume ',' |
| 992 | } |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 993 | if (Tok.isNot(tok::semi)) |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 994 | Diag(Tok, diag::err_expected_semi_after, "@synthesize"); |
| 995 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 998 | /// property-dynamic: |
| 999 | /// @dynamic property-list |
| 1000 | /// |
| 1001 | /// property-list: |
| 1002 | /// identifier |
| 1003 | /// property-list ',' identifier |
| 1004 | /// |
| 1005 | Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) { |
| 1006 | assert(Tok.isObjCAtKeyword(tok::objc_dynamic) && |
| 1007 | "ParseObjCPropertyDynamic(): Expected '@dynamic'"); |
| 1008 | SourceLocation loc = ConsumeToken(); // consume dynamic |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1009 | if (Tok.isNot(tok::identifier)) { |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1010 | Diag(Tok, diag::err_expected_ident); |
| 1011 | return 0; |
| 1012 | } |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1013 | while (Tok.is(tok::identifier)) { |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1014 | ConsumeToken(); // consume property name |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1015 | if (Tok.isNot(tok::comma)) |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1016 | break; |
| 1017 | ConsumeToken(); // consume ',' |
| 1018 | } |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1019 | if (Tok.isNot(tok::semi)) |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1020 | Diag(Tok, diag::err_expected_semi_after, "@dynamic"); |
| 1021 | return 0; |
| 1022 | } |
Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1023 | |
| 1024 | /// objc-throw-statement: |
| 1025 | /// throw expression[opt]; |
| 1026 | /// |
Fariborz Jahanian | 37c9c61 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 1027 | Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation atLoc) { |
Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1028 | ConsumeToken(); // consume throw |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1029 | if (Tok.isNot(tok::semi)) { |
Fariborz Jahanian | 37c9c61 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 1030 | ExprResult Res = ParseExpression(); |
Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1031 | if (Res.isInvalid) { |
| 1032 | SkipUntil(tok::semi); |
| 1033 | return 0; |
| 1034 | } |
| 1035 | } |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | /// objc-try-catch-statement: |
| 1040 | /// @try compound-statement objc-catch-list[opt] |
| 1041 | /// @try compound-statement objc-catch-list[opt] @finally compound-statement |
| 1042 | /// |
| 1043 | /// objc-catch-list: |
| 1044 | /// @catch ( parameter-declaration ) compound-statement |
| 1045 | /// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement |
| 1046 | /// catch-parameter-declaration: |
| 1047 | /// parameter-declaration |
| 1048 | /// '...' [OBJC2] |
| 1049 | /// |
Fariborz Jahanian | 7095248 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 1050 | Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) { |
Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1051 | bool catch_or_finally_seen = false; |
| 1052 | ConsumeToken(); // consume try |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1053 | if (Tok.isNot(tok::l_brace)) { |
Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1054 | Diag (Tok, diag::err_expected_lbrace); |
Fariborz Jahanian | 7095248 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 1055 | return true; |
Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1056 | } |
Fariborz Jahanian | 0679836 | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1057 | StmtResult CatchStmts; |
Fariborz Jahanian | de3abf8 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 1058 | StmtResult FinallyStmt; |
Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1059 | StmtResult TryBody = ParseCompoundStatementBody(); |
Fariborz Jahanian | b8bf607 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 1060 | if (TryBody.isInvalid) |
| 1061 | TryBody = Actions.ActOnNullStmt(Tok.getLocation()); |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1062 | while (Tok.is(tok::at)) { |
Fariborz Jahanian | de3abf8 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 1063 | SourceLocation AtCatchFinallyLoc = ConsumeToken(); |
Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1064 | if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) { |
Fariborz Jahanian | 0679836 | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1065 | StmtTy *FirstPart = 0; |
| 1066 | ConsumeToken(); // consume catch |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1067 | if (Tok.is(tok::l_paren)) { |
Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1068 | ConsumeParen(); |
Fariborz Jahanian | 0679836 | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1069 | EnterScope(Scope::DeclScope); |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1070 | if (Tok.isNot(tok::ellipsis)) { |
Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1071 | DeclSpec DS; |
| 1072 | ParseDeclarationSpecifiers(DS); |
Fariborz Jahanian | 0679836 | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1073 | // FIXME: Is BlockContext right? |
| 1074 | Declarator DeclaratorInfo(DS, Declarator::BlockContext); |
| 1075 | ParseDeclarator(DeclaratorInfo); |
Fariborz Jahanian | 59e9e04 | 2007-11-02 18:16:07 +0000 | [diff] [blame^] | 1076 | DeclTy * aBlockVarDecl = Actions.ActOnDeclarator(CurScope, |
| 1077 | DeclaratorInfo, 0); |
| 1078 | StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl); |
Fariborz Jahanian | 0679836 | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1079 | FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val; |
Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1080 | } |
| 1081 | else |
| 1082 | ConsumeToken(); // consume '...' |
Fariborz Jahanian | 0679836 | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1083 | SourceLocation RParenLoc = ConsumeParen(); |
Fariborz Jahanian | 7095248 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 1084 | StmtResult CatchBody = ParseCompoundStatementBody(); |
Fariborz Jahanian | 0679836 | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1085 | if (CatchBody.isInvalid) |
| 1086 | CatchBody = Actions.ActOnNullStmt(Tok.getLocation()); |
Fariborz Jahanian | de3abf8 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 1087 | CatchStmts = Actions.ActOnObjcAtCatchStmt(AtCatchFinallyLoc, RParenLoc, |
Fariborz Jahanian | 0679836 | 2007-11-01 23:59:59 +0000 | [diff] [blame] | 1088 | FirstPart, CatchBody.Val, CatchStmts.Val); |
| 1089 | ExitScope(); |
Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1090 | } |
| 1091 | else { |
Fariborz Jahanian | de3abf8 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 1092 | Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after, |
| 1093 | "@catch clause"); |
Fariborz Jahanian | 7095248 | 2007-11-01 21:12:44 +0000 | [diff] [blame] | 1094 | return true; |
Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1095 | } |
| 1096 | catch_or_finally_seen = true; |
| 1097 | } |
| 1098 | else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) { |
| 1099 | ConsumeToken(); // consume finally |
| 1100 | StmtResult FinallyBody = ParseCompoundStatementBody(); |
Fariborz Jahanian | de3abf8 | 2007-11-02 00:18:53 +0000 | [diff] [blame] | 1101 | if (FinallyBody.isInvalid) |
| 1102 | FinallyBody = Actions.ActOnNullStmt(Tok.getLocation()); |
| 1103 | FinallyStmt = Actions.ActOnObjcAtFinallyStmt(AtCatchFinallyLoc, |
| 1104 | FinallyBody.Val); |
Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1105 | catch_or_finally_seen = true; |
| 1106 | break; |
| 1107 | } |
| 1108 | } |
Fariborz Jahanian | b8bf607 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 1109 | if (!catch_or_finally_seen) { |
Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1110 | Diag(atLoc, diag::err_missing_catch_finally); |
Fariborz Jahanian | b8bf607 | 2007-11-02 15:39:31 +0000 | [diff] [blame] | 1111 | return true; |
| 1112 | } |
| 1113 | return Actions.ActOnObjcAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val, |
| 1114 | FinallyStmt.Val); |
Fariborz Jahanian | 64b864e | 2007-09-19 19:14:32 +0000 | [diff] [blame] | 1115 | } |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1116 | |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 1117 | /// objc-method-def: objc-method-proto ';'[opt] '{' body '}' |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1118 | /// |
| 1119 | void Parser::ParseObjCInstanceMethodDefinition() { |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1120 | assert(Tok.is(tok::minus) && "Method definitions should start with '-'"); |
| 1121 | |
Fariborz Jahanian | 63ca8ae | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 1122 | // FIXME: @optional/@protocol?? |
Fariborz Jahanian | 1e4e82f | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 1123 | AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl)); |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1124 | // parse optional ';' |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1125 | if (Tok.is(tok::semi)) |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1126 | ConsumeToken(); |
| 1127 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1128 | if (Tok.isNot(tok::l_brace)) { |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1129 | Diag (Tok, diag::err_expected_lbrace); |
| 1130 | return; |
| 1131 | } |
| 1132 | |
| 1133 | StmtResult FnBody = ParseCompoundStatementBody(); |
| 1134 | } |
| 1135 | |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 1136 | /// objc-method-def: objc-method-proto ';'[opt] '{' body '}' |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1137 | /// |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 1138 | void Parser::ParseObjCClassMethodDefinition() { |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1139 | assert(Tok.is(tok::plus) && "Class method definitions should start with '+'"); |
Fariborz Jahanian | 63ca8ae | 2007-09-17 21:07:36 +0000 | [diff] [blame] | 1140 | // FIXME: @optional/@protocol?? |
Fariborz Jahanian | 1e4e82f | 2007-09-27 18:57:03 +0000 | [diff] [blame] | 1141 | AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl)); |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1142 | // parse optional ';' |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1143 | if (Tok.is(tok::semi)) |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1144 | ConsumeToken(); |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1145 | if (Tok.isNot(tok::l_brace)) { |
Fariborz Jahanian | 027c23b | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 1146 | Diag (Tok, diag::err_expected_lbrace); |
| 1147 | return; |
| 1148 | } |
| 1149 | |
| 1150 | StmtResult FnBody = ParseCompoundStatementBody(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1151 | } |
Anders Carlsson | a66cad4 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1152 | |
Steve Naroff | fb9dd75 | 2007-10-15 20:55:58 +0000 | [diff] [blame] | 1153 | Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) { |
Anders Carlsson | a66cad4 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1154 | |
| 1155 | switch (Tok.getKind()) { |
| 1156 | case tok::string_literal: // primary-expression: string-literal |
| 1157 | case tok::wide_string_literal: |
Fariborz Jahanian | 37c9c61 | 2007-10-04 20:19:06 +0000 | [diff] [blame] | 1158 | return ParsePostfixExpressionSuffix(ParseObjCStringLiteral()); |
Anders Carlsson | 2996b4e | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1159 | default: |
| 1160 | break; |
| 1161 | } |
| 1162 | |
| 1163 | switch (Tok.getIdentifierInfo()->getObjCKeywordID()) { |
Chris Lattner | cfd61c8 | 2007-10-16 22:51:17 +0000 | [diff] [blame] | 1164 | case tok::objc_encode: |
| 1165 | return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc)); |
| 1166 | case tok::objc_protocol: |
Fariborz Jahanian | 957448a | 2007-10-16 23:21:02 +0000 | [diff] [blame] | 1167 | return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc)); |
Chris Lattner | cfd61c8 | 2007-10-16 22:51:17 +0000 | [diff] [blame] | 1168 | case tok::objc_selector: |
Fariborz Jahanian | 957448a | 2007-10-16 23:21:02 +0000 | [diff] [blame] | 1169 | return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc)); |
Chris Lattner | cfd61c8 | 2007-10-16 22:51:17 +0000 | [diff] [blame] | 1170 | default: |
| 1171 | Diag(AtLoc, diag::err_unexpected_at); |
| 1172 | SkipUntil(tok::semi); |
| 1173 | break; |
Anders Carlsson | a66cad4 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | return 0; |
| 1177 | } |
| 1178 | |
Fariborz Jahanian | 1e534dc | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 1179 | /// objc-message-expr: |
| 1180 | /// '[' objc-receiver objc-message-args ']' |
| 1181 | /// |
| 1182 | /// objc-receiver: |
| 1183 | /// expression |
| 1184 | /// class-name |
| 1185 | /// type-name |
| 1186 | /// |
| 1187 | /// objc-message-args: |
| 1188 | /// objc-selector |
| 1189 | /// objc-keywordarg-list |
| 1190 | /// |
| 1191 | /// objc-keywordarg-list: |
| 1192 | /// objc-keywordarg |
| 1193 | /// objc-keywordarg-list objc-keywordarg |
| 1194 | /// |
| 1195 | /// objc-keywordarg: |
| 1196 | /// selector-name[opt] ':' objc-keywordexpr |
| 1197 | /// |
| 1198 | /// objc-keywordexpr: |
| 1199 | /// nonempty-expr-list |
| 1200 | /// |
| 1201 | /// nonempty-expr-list: |
| 1202 | /// assignment-expression |
| 1203 | /// nonempty-expr-list , assignment-expression |
| 1204 | /// |
| 1205 | Parser::ExprResult Parser::ParseObjCMessageExpression() { |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1206 | assert(Tok.is(tok::l_square) && "'[' expected"); |
Steve Naroff | c39ca26 | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1207 | SourceLocation LBracloc = ConsumeBracket(); // consume '[' |
Steve Naroff | 253118b | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1208 | IdentifierInfo *ReceiverName = 0; |
| 1209 | ExprTy *ReceiverExpr = 0; |
Fariborz Jahanian | d4462f9 | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1210 | // Parse receiver |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1211 | if (Tok.is(tok::identifier) && |
Steve Naroff | 253118b | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1212 | Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) { |
| 1213 | ReceiverName = Tok.getIdentifierInfo(); |
Steve Naroff | f0c31dd | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 1214 | ConsumeToken(); |
Steve Naroff | 253118b | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1215 | } else { |
| 1216 | ExprResult Res = ParseAssignmentExpression(); |
| 1217 | if (Res.isInvalid) { |
| 1218 | SkipUntil(tok::identifier); |
| 1219 | return Res; |
| 1220 | } |
| 1221 | ReceiverExpr = Res.Val; |
| 1222 | } |
Fariborz Jahanian | d4462f9 | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1223 | // Parse objc-selector |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1224 | SourceLocation Loc; |
| 1225 | IdentifierInfo *selIdent = ParseObjCSelector(Loc); |
Steve Naroff | 4ed9d66 | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1226 | |
| 1227 | llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; |
| 1228 | llvm::SmallVector<Action::ExprTy *, 12> KeyExprs; |
| 1229 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1230 | if (Tok.is(tok::colon)) { |
Fariborz Jahanian | d4462f9 | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1231 | while (1) { |
| 1232 | // Each iteration parses a single keyword argument. |
Steve Naroff | 4ed9d66 | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1233 | KeyIdents.push_back(selIdent); |
Steve Naroff | 253118b | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1234 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1235 | if (Tok.isNot(tok::colon)) { |
Fariborz Jahanian | d4462f9 | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1236 | Diag(Tok, diag::err_expected_colon); |
| 1237 | SkipUntil(tok::semi); |
Steve Naroff | 253118b | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1238 | return true; |
Fariborz Jahanian | d4462f9 | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1239 | } |
Steve Naroff | 4ed9d66 | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1240 | ConsumeToken(); // Eat the ':'. |
Fariborz Jahanian | d4462f9 | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1241 | /// Parse the expression after ':' |
Steve Naroff | 253118b | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1242 | ExprResult Res = ParseAssignmentExpression(); |
| 1243 | if (Res.isInvalid) { |
| 1244 | SkipUntil(tok::identifier); |
| 1245 | return Res; |
| 1246 | } |
| 1247 | // We have a valid expression. |
Steve Naroff | 4ed9d66 | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1248 | KeyExprs.push_back(Res.Val); |
Steve Naroff | 253118b | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1249 | |
| 1250 | // Check for another keyword selector. |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1251 | selIdent = ParseObjCSelector(Loc); |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1252 | if (!selIdent && Tok.isNot(tok::colon)) |
Fariborz Jahanian | d4462f9 | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1253 | break; |
| 1254 | // We have a selector or a colon, continue parsing. |
| 1255 | } |
| 1256 | // Parse the, optional, argument list, comma separated. |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1257 | while (Tok.is(tok::comma)) { |
Fariborz Jahanian | d4462f9 | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1258 | ConsumeToken(); |
| 1259 | /// Parse the expression after ',' |
| 1260 | ParseAssignmentExpression(); |
| 1261 | } |
| 1262 | } else if (!selIdent) { |
| 1263 | Diag(Tok, diag::err_expected_ident); // missing selector name. |
| 1264 | SkipUntil(tok::semi); |
| 1265 | return 0; |
| 1266 | } |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1268 | if (Tok.isNot(tok::r_square)) { |
Fariborz Jahanian | d4462f9 | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 1269 | Diag(Tok, diag::err_expected_rsquare); |
| 1270 | SkipUntil(tok::semi); |
| 1271 | return 0; |
| 1272 | } |
Steve Naroff | c39ca26 | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1273 | SourceLocation RBracloc = ConsumeBracket(); // consume ']' |
Steve Naroff | 253118b | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1274 | |
Steve Naroff | f9e80db | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 1275 | unsigned nKeys = KeyIdents.size(); |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1276 | if (nKeys == 0) |
| 1277 | KeyIdents.push_back(selIdent); |
| 1278 | Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]); |
| 1279 | |
| 1280 | // We've just parsed a keyword message. |
Steve Naroff | 253118b | 2007-09-17 20:25:27 +0000 | [diff] [blame] | 1281 | if (ReceiverName) |
Chris Lattner | d031a45 | 2007-10-07 02:00:24 +0000 | [diff] [blame] | 1282 | return Actions.ActOnClassMessage(ReceiverName, Sel, LBracloc, RBracloc, |
| 1283 | &KeyExprs[0]); |
| 1284 | return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc, |
| 1285 | &KeyExprs[0]); |
Fariborz Jahanian | 1e534dc | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Anders Carlsson | a66cad4 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1288 | Parser::ExprResult Parser::ParseObjCStringLiteral() { |
| 1289 | ExprResult Res = ParseStringLiteralExpression(); |
| 1290 | |
| 1291 | if (Res.isInvalid) return Res; |
| 1292 | |
| 1293 | return Actions.ParseObjCStringLiteral(Res.Val); |
| 1294 | } |
Anders Carlsson | 8be1d40 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1295 | |
| 1296 | /// objc-encode-expression: |
| 1297 | /// @encode ( type-name ) |
Chris Lattner | cfd61c8 | 2007-10-16 22:51:17 +0000 | [diff] [blame] | 1298 | Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) { |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 1299 | assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!"); |
Anders Carlsson | 8be1d40 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1300 | |
| 1301 | SourceLocation EncLoc = ConsumeToken(); |
| 1302 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1303 | if (Tok.isNot(tok::l_paren)) { |
Anders Carlsson | 8be1d40 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1304 | Diag(Tok, diag::err_expected_lparen_after, "@encode"); |
| 1305 | return true; |
| 1306 | } |
| 1307 | |
| 1308 | SourceLocation LParenLoc = ConsumeParen(); |
| 1309 | |
| 1310 | TypeTy *Ty = ParseTypeName(); |
| 1311 | |
Anders Carlsson | 92faeb8 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 1312 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Anders Carlsson | 8be1d40 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1313 | |
Chris Lattner | cfd61c8 | 2007-10-16 22:51:17 +0000 | [diff] [blame] | 1314 | return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty, |
Anders Carlsson | 92faeb8 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 1315 | RParenLoc); |
Anders Carlsson | 8be1d40 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1316 | } |
Anders Carlsson | 2996b4e | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1317 | |
| 1318 | /// objc-protocol-expression |
| 1319 | /// @protocol ( protocol-name ) |
| 1320 | |
Fariborz Jahanian | 957448a | 2007-10-16 23:21:02 +0000 | [diff] [blame] | 1321 | Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) |
Anders Carlsson | 2996b4e | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1322 | { |
| 1323 | SourceLocation ProtoLoc = ConsumeToken(); |
| 1324 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1325 | if (Tok.isNot(tok::l_paren)) { |
Anders Carlsson | 2996b4e | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1326 | Diag(Tok, diag::err_expected_lparen_after, "@protocol"); |
| 1327 | return true; |
| 1328 | } |
| 1329 | |
| 1330 | SourceLocation LParenLoc = ConsumeParen(); |
| 1331 | |
Chris Lattner | a1d2bb7 | 2007-10-09 17:51:17 +0000 | [diff] [blame] | 1332 | if (Tok.isNot(tok::identifier)) { |
Anders Carlsson | 2996b4e | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1333 | Diag(Tok, diag::err_expected_ident); |
| 1334 | return true; |
| 1335 | } |
Fariborz Jahanian | b391e6e | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1336 | IdentifierInfo *protocolId = Tok.getIdentifierInfo(); |
Anders Carlsson | 2996b4e | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1337 | ConsumeToken(); |
| 1338 | |
Anders Carlsson | 92faeb8 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 1339 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Anders Carlsson | 2996b4e | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1340 | |
Fariborz Jahanian | b391e6e | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1341 | return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc, |
| 1342 | LParenLoc, RParenLoc); |
Anders Carlsson | 2996b4e | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1343 | } |
Fariborz Jahanian | 056c6b0 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1344 | |
| 1345 | /// objc-selector-expression |
| 1346 | /// @selector '(' objc-keyword-selector ')' |
Fariborz Jahanian | 957448a | 2007-10-16 23:21:02 +0000 | [diff] [blame] | 1347 | Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) |
Fariborz Jahanian | 056c6b0 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1348 | { |
| 1349 | SourceLocation SelectorLoc = ConsumeToken(); |
| 1350 | |
| 1351 | if (Tok.isNot(tok::l_paren)) { |
| 1352 | Diag(Tok, diag::err_expected_lparen_after, "@selector"); |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1356 | llvm::SmallVector<IdentifierInfo *, 12> KeyIdents; |
Fariborz Jahanian | 056c6b0 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1357 | SourceLocation LParenLoc = ConsumeParen(); |
| 1358 | SourceLocation sLoc; |
| 1359 | IdentifierInfo *SelIdent = ParseObjCSelector(sLoc); |
| 1360 | if (!SelIdent && Tok.isNot(tok::colon)) { |
| 1361 | |
| 1362 | Diag(Tok, diag::err_expected_ident); // missing selector name. |
| 1363 | return 0; |
| 1364 | } |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1365 | KeyIdents.push_back(SelIdent); |
Fariborz Jahanian | 056c6b0 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1366 | if (Tok.isNot(tok::r_paren)) |
| 1367 | while (1) { |
| 1368 | if (Tok.isNot(tok::colon)) { |
| 1369 | Diag(Tok, diag::err_expected_colon); |
| 1370 | break; |
| 1371 | } |
| 1372 | ConsumeToken(); // Eat the ':'. |
| 1373 | if (Tok.is(tok::r_paren)) |
| 1374 | break; |
| 1375 | // Check for another keyword selector. |
| 1376 | SourceLocation Loc; |
| 1377 | SelIdent = ParseObjCSelector(Loc); |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1378 | KeyIdents.push_back(SelIdent); |
Fariborz Jahanian | 056c6b0 | 2007-10-15 23:39:13 +0000 | [diff] [blame] | 1379 | if (!SelIdent && Tok.isNot(tok::colon)) |
| 1380 | break; |
| 1381 | } |
| 1382 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1383 | Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(), |
| 1384 | &KeyIdents[0]); |
Fariborz Jahanian | 957448a | 2007-10-16 23:21:02 +0000 | [diff] [blame] | 1385 | return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc, |
Fariborz Jahanian | f807c20 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1386 | RParenLoc); |
Gabor Greif | a823dd1 | 2007-10-19 15:38:32 +0000 | [diff] [blame] | 1387 | } |