Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 15 | #include "clang/Parse/DeclSpec.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
| 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | using namespace clang; |
| 19 | |
| 20 | |
| 21 | /// ParseExternalDeclaration: |
| 22 | /// external-declaration: [C99 6.9] |
| 23 | /// [OBJC] objc-class-definition |
| 24 | /// [OBJC] objc-class-declaration [TODO] |
| 25 | /// [OBJC] objc-alias-declaration [TODO] |
| 26 | /// [OBJC] objc-protocol-definition [TODO] |
| 27 | /// [OBJC] objc-method-definition [TODO] |
| 28 | /// [OBJC] '@' 'end' [TODO] |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 29 | Parser::DeclTy *Parser::ParseObjCAtDirectives() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
| 31 | |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 32 | switch (Tok.getObjCKeywordID()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | case tok::objc_class: |
| 34 | return ParseObjCAtClassDeclaration(AtLoc); |
| 35 | case tok::objc_interface: |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 36 | return ParseObjCAtInterfaceDeclaration(AtLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | case tok::objc_protocol: |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 38 | return ParseObjCAtProtocolDeclaration(AtLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | case tok::objc_implementation: |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 40 | return ObjcImpDecl = ParseObjCAtImplementationDeclaration(AtLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | case tok::objc_end: |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 42 | return ParseObjCAtEndDeclaration(AtLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | case tok::objc_compatibility_alias: |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 44 | return ParseObjCAtAliasDeclaration(AtLoc); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 45 | case tok::objc_synthesize: |
| 46 | return ParseObjCPropertySynthesize(AtLoc); |
| 47 | case tok::objc_dynamic: |
| 48 | return ParseObjCPropertyDynamic(AtLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | default: |
| 50 | Diag(AtLoc, diag::err_unexpected_at); |
| 51 | SkipUntil(tok::semi); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 52 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | /// |
| 57 | /// objc-class-declaration: |
| 58 | /// '@' 'class' identifier-list ';' |
| 59 | /// |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 60 | Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | ConsumeToken(); // the identifier "class" |
| 62 | llvm::SmallVector<IdentifierInfo *, 8> ClassNames; |
| 63 | |
| 64 | while (1) { |
| 65 | if (Tok.getKind() != tok::identifier) { |
| 66 | Diag(Tok, diag::err_expected_ident); |
| 67 | SkipUntil(tok::semi); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 68 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 69 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | ClassNames.push_back(Tok.getIdentifierInfo()); |
| 71 | ConsumeToken(); |
| 72 | |
| 73 | if (Tok.getKind() != tok::comma) |
| 74 | break; |
| 75 | |
| 76 | ConsumeToken(); |
| 77 | } |
| 78 | |
| 79 | // Consume the ';'. |
| 80 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class")) |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 81 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 83 | return Actions.ObjcClassDeclaration(CurScope, atLoc, |
| 84 | &ClassNames[0], ClassNames.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 87 | /// |
| 88 | /// objc-interface: |
| 89 | /// objc-class-interface-attributes[opt] objc-class-interface |
| 90 | /// objc-category-interface |
| 91 | /// |
| 92 | /// objc-class-interface: |
| 93 | /// '@' 'interface' identifier objc-superclass[opt] |
| 94 | /// objc-protocol-refs[opt] |
| 95 | /// objc-class-instance-variables[opt] |
| 96 | /// objc-interface-decl-list |
| 97 | /// @end |
| 98 | /// |
| 99 | /// objc-category-interface: |
| 100 | /// '@' 'interface' identifier '(' identifier[opt] ')' |
| 101 | /// objc-protocol-refs[opt] |
| 102 | /// objc-interface-decl-list |
| 103 | /// @end |
| 104 | /// |
| 105 | /// objc-superclass: |
| 106 | /// ':' identifier |
| 107 | /// |
| 108 | /// objc-class-interface-attributes: |
| 109 | /// __attribute__((visibility("default"))) |
| 110 | /// __attribute__((visibility("hidden"))) |
| 111 | /// __attribute__((deprecated)) |
| 112 | /// __attribute__((unavailable)) |
| 113 | /// __attribute__((objc_exception)) - used by NSException on 64-bit |
| 114 | /// |
| 115 | Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration( |
| 116 | SourceLocation atLoc, AttributeList *attrList) { |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 117 | assert(Tok.isObjCAtKeyword(tok::objc_interface) && |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 118 | "ParseObjCAtInterfaceDeclaration(): Expected @interface"); |
| 119 | ConsumeToken(); // the "interface" identifier |
| 120 | |
| 121 | if (Tok.getKind() != tok::identifier) { |
| 122 | Diag(Tok, diag::err_expected_ident); // missing class or category name. |
| 123 | return 0; |
| 124 | } |
| 125 | // We have a class or category name - consume it. |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 126 | IdentifierInfo *nameId = Tok.getIdentifierInfo(); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 127 | SourceLocation nameLoc = ConsumeToken(); |
| 128 | |
Steve Naroff | 527fe23 | 2007-08-23 19:56:30 +0000 | [diff] [blame] | 129 | if (Tok.getKind() == tok::l_paren) { // we have a category. |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 130 | SourceLocation lparenLoc = ConsumeParen(); |
| 131 | SourceLocation categoryLoc, rparenLoc; |
| 132 | IdentifierInfo *categoryId = 0; |
| 133 | |
Steve Naroff | 527fe23 | 2007-08-23 19:56:30 +0000 | [diff] [blame] | 134 | // For ObjC2, the category name is optional (not an error). |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 135 | if (Tok.getKind() == tok::identifier) { |
| 136 | categoryId = Tok.getIdentifierInfo(); |
| 137 | categoryLoc = ConsumeToken(); |
Steve Naroff | 527fe23 | 2007-08-23 19:56:30 +0000 | [diff] [blame] | 138 | } else if (!getLang().ObjC2) { |
| 139 | Diag(Tok, diag::err_expected_ident); // missing category name. |
| 140 | return 0; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 141 | } |
| 142 | if (Tok.getKind() != tok::r_paren) { |
| 143 | Diag(Tok, diag::err_expected_rparen); |
| 144 | SkipUntil(tok::r_paren, false); // don't stop at ';' |
| 145 | return 0; |
| 146 | } |
| 147 | rparenLoc = ConsumeParen(); |
| 148 | // Next, we need to check for any protocol references. |
| 149 | if (Tok.getKind() == tok::less) { |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 150 | llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs; |
| 151 | if (ParseObjCProtocolReferences(ProtocolRefs)) |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | if (attrList) // categories don't support attributes. |
| 155 | Diag(Tok, diag::err_objc_no_attributes_on_category); |
| 156 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 157 | ParseObjCInterfaceDeclList(0/*FIXME*/); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 158 | |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 159 | // The @ sign was already consumed by ParseObjCInterfaceDeclList(). |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 160 | if (Tok.isObjCAtKeyword(tok::objc_end)) { |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 161 | ConsumeToken(); // the "end" identifier |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 162 | return 0; |
| 163 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 164 | Diag(Tok, diag::err_objc_missing_end); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | // Parse a class interface. |
| 168 | IdentifierInfo *superClassId = 0; |
| 169 | SourceLocation superClassLoc; |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 170 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 171 | if (Tok.getKind() == tok::colon) { // a super class is specified. |
| 172 | ConsumeToken(); |
| 173 | if (Tok.getKind() != tok::identifier) { |
| 174 | Diag(Tok, diag::err_expected_ident); // missing super class name. |
| 175 | return 0; |
| 176 | } |
| 177 | superClassId = Tok.getIdentifierInfo(); |
| 178 | superClassLoc = ConsumeToken(); |
| 179 | } |
| 180 | // Next, we need to check for any protocol references. |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 181 | llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 182 | if (Tok.getKind() == tok::less) { |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 183 | if (ParseObjCProtocolReferences(ProtocolRefs)) |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 184 | return 0; |
| 185 | } |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 186 | DeclTy *ClsType = Actions.ObjcStartClassInterface(atLoc, nameId, nameLoc, |
| 187 | superClassId, superClassLoc, &ProtocolRefs[0], |
| 188 | ProtocolRefs.size(), attrList); |
| 189 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 190 | if (Tok.getKind() == tok::l_brace) |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 191 | ParseObjCClassInstanceVariables(ClsType); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 192 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 193 | ParseObjCInterfaceDeclList(ClsType); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 194 | |
| 195 | // The @ sign was already consumed by ParseObjCInterfaceDeclList(). |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 196 | if (Tok.isObjCAtKeyword(tok::objc_end)) { |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 197 | ConsumeToken(); // the "end" identifier |
Steve Naroff | 2bd42fa | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 198 | return ClsType; |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 199 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 200 | Diag(Tok, diag::err_objc_missing_end); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | /// objc-interface-decl-list: |
| 205 | /// empty |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 206 | /// objc-interface-decl-list objc-property-decl [OBJC2] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 207 | /// objc-interface-decl-list objc-method-requirement [OBJC2] |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 208 | /// objc-interface-decl-list objc-method-proto ';' |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 209 | /// objc-interface-decl-list declaration |
| 210 | /// objc-interface-decl-list ';' |
| 211 | /// |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 212 | /// objc-method-requirement: [OBJC2] |
| 213 | /// @required |
| 214 | /// @optional |
| 215 | /// |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 216 | void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl) { |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 217 | llvm::SmallVector<DeclTy*, 32> allMethods; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 218 | while (1) { |
| 219 | if (Tok.getKind() == tok::at) { |
| 220 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 221 | tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID(); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 222 | |
| 223 | if (ocKind == tok::objc_end) { // terminate list |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 224 | break; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 225 | } else if (ocKind == tok::objc_required) { // protocols only |
| 226 | ConsumeToken(); |
| 227 | continue; |
| 228 | } else if (ocKind == tok::objc_optional) { // protocols only |
| 229 | ConsumeToken(); |
| 230 | continue; |
| 231 | } else if (ocKind == tok::objc_property) { |
Fariborz Jahanian | e55cd00 | 2007-09-12 18:23:47 +0000 | [diff] [blame] | 232 | ParseObjCPropertyDecl(interfaceDecl); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 233 | continue; |
| 234 | } else { |
| 235 | Diag(Tok, diag::err_objc_illegal_interface_qual); |
| 236 | ConsumeToken(); |
| 237 | } |
| 238 | } |
| 239 | if (Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) { |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 240 | allMethods.push_back(ParseObjCMethodPrototype(interfaceDecl)); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 241 | // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for |
| 242 | // method definitions. |
Steve Naroff | d16245b | 2007-09-17 15:07:43 +0000 | [diff] [blame^] | 243 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto"); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 244 | continue; |
| 245 | } |
| 246 | if (Tok.getKind() == tok::semi) |
| 247 | ConsumeToken(); |
| 248 | else if (Tok.getKind() == tok::eof) |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 249 | break; |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 250 | else { |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 251 | // FIXME: as the name implies, this rule allows function definitions. |
| 252 | // We could pass a flag or check for functions during semantic analysis. |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 253 | ParseDeclarationOrFunctionDefinition(); |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 254 | } |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 255 | } |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 256 | /// Insert collected methods declarations into the @interface object. |
Steve Naroff | d16245b | 2007-09-17 15:07:43 +0000 | [diff] [blame^] | 257 | Actions.ObjcAddMethodsToClass(interfaceDecl,&allMethods[0],allMethods.size()); |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 258 | return; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 261 | /// Parse property attribute declarations. |
| 262 | /// |
| 263 | /// property-attr-decl: '(' property-attrlist ')' |
| 264 | /// property-attrlist: |
| 265 | /// property-attribute |
| 266 | /// property-attrlist ',' property-attribute |
| 267 | /// property-attribute: |
| 268 | /// getter '=' identifier |
| 269 | /// setter '=' identifier ':' |
| 270 | /// readonly |
| 271 | /// readwrite |
| 272 | /// assign |
| 273 | /// retain |
| 274 | /// copy |
| 275 | /// nonatomic |
| 276 | /// |
| 277 | void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) { |
| 278 | SourceLocation loc = ConsumeParen(); // consume '(' |
| 279 | while (isObjCPropertyAttribute()) { |
| 280 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 281 | // getter/setter require extra treatment. |
| 282 | if (II == ObjcPropertyAttrs[objc_getter] || |
| 283 | II == ObjcPropertyAttrs[objc_setter]) { |
| 284 | // skip getter/setter part. |
| 285 | SourceLocation loc = ConsumeToken(); |
| 286 | if (Tok.getKind() == tok::equal) { |
| 287 | loc = ConsumeToken(); |
| 288 | if (Tok.getKind() == tok::identifier) { |
| 289 | if (II == ObjcPropertyAttrs[objc_setter]) { |
| 290 | loc = ConsumeToken(); // consume method name |
| 291 | if (Tok.getKind() != tok::colon) { |
| 292 | Diag(loc, diag::err_expected_colon); |
| 293 | SkipUntil(tok::r_paren,true,true); |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | else { |
| 299 | Diag(loc, diag::err_expected_ident); |
| 300 | SkipUntil(tok::r_paren,true,true); |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | else { |
| 305 | Diag(loc, diag::err_objc_expected_equal); |
| 306 | SkipUntil(tok::r_paren,true,true); |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | ConsumeToken(); // consume last attribute token |
| 311 | if (Tok.getKind() == tok::comma) { |
| 312 | loc = ConsumeToken(); |
| 313 | continue; |
| 314 | } |
| 315 | if (Tok.getKind() == tok::r_paren) |
| 316 | break; |
| 317 | Diag(loc, diag::err_expected_rparen); |
| 318 | SkipUntil(tok::semi); |
| 319 | return; |
| 320 | } |
| 321 | if (Tok.getKind() == tok::r_paren) |
| 322 | ConsumeParen(); |
| 323 | else { |
| 324 | Diag(loc, diag::err_objc_expected_property_attr); |
| 325 | SkipUntil(tok::r_paren); // recover from error inside attribute list |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | /// Main routine to parse property declaration. |
| 330 | /// |
| 331 | /// @property property-attr-decl[opt] property-component-decl ';' |
| 332 | /// |
| 333 | void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) { |
| 334 | assert(Tok.isObjCAtKeyword(tok::objc_property) && |
| 335 | "ParseObjCPropertyDecl(): Expected @property"); |
| 336 | ConsumeToken(); // the "property" identifier |
| 337 | // Parse property attribute list, if any. |
| 338 | if (Tok.getKind() == tok::l_paren) { |
| 339 | // property has attribute list. |
| 340 | ParseObjCPropertyAttribute(0/*FIXME*/); |
| 341 | } |
| 342 | // Parse declaration portion of @property. |
| 343 | llvm::SmallVector<DeclTy*, 32> PropertyDecls; |
| 344 | ParseStructDeclaration(interfaceDecl, PropertyDecls); |
| 345 | if (Tok.getKind() == tok::semi) |
| 346 | ConsumeToken(); |
| 347 | else { |
| 348 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 349 | SkipUntil(tok::r_brace, true, true); |
| 350 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 351 | } |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 352 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 353 | /// objc-method-proto: |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 354 | /// objc-instance-method objc-method-decl objc-method-attributes[opt] |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 355 | /// objc-class-method objc-method-decl objc-method-attributes[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 356 | /// |
| 357 | /// objc-instance-method: '-' |
| 358 | /// objc-class-method: '+' |
| 359 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 360 | /// objc-method-attributes: [OBJC2] |
| 361 | /// __attribute__((deprecated)) |
| 362 | /// |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 363 | Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *CDecl) { |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 364 | assert((Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) && |
| 365 | "expected +/-"); |
| 366 | |
| 367 | tok::TokenKind methodType = Tok.getKind(); |
| 368 | SourceLocation methodLoc = ConsumeToken(); |
| 369 | |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 370 | DeclTy *MDecl = ParseObjCMethodDecl(methodType, methodLoc); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 371 | // Since this rule is used for both method declarations and definitions, |
Steve Naroff | 2bd42fa | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 372 | // the caller is (optionally) responsible for consuming the ';'. |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 373 | return MDecl; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /// objc-selector: |
| 377 | /// identifier |
| 378 | /// one of |
| 379 | /// enum struct union if else while do for switch case default |
| 380 | /// break continue return goto asm sizeof typeof __alignof |
| 381 | /// unsigned long const short volatile signed restrict _Complex |
| 382 | /// in out inout bycopy byref oneway int char float double void _Bool |
| 383 | /// |
| 384 | IdentifierInfo *Parser::ParseObjCSelector() { |
| 385 | tok::TokenKind tKind = Tok.getKind(); |
| 386 | IdentifierInfo *II = 0; |
| 387 | |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 388 | if (tKind == tok::identifier || tKind == tok::kw_typeof || |
| 389 | tKind == tok::kw___alignof || |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 390 | (tKind >= tok::kw_auto && tKind <= tok::kw__Complex)) { |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 391 | II = Tok.getIdentifierInfo(); |
| 392 | ConsumeToken(); |
| 393 | } |
| 394 | return II; |
| 395 | } |
| 396 | |
Steve Naroff | 4fa7afd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 397 | /// objc-type-qualifier: one of |
| 398 | /// in out inout bycopy byref oneway |
| 399 | /// |
Steve Naroff | 4fa7afd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 400 | bool Parser::isObjCTypeQualifier() { |
| 401 | if (Tok.getKind() == tok::identifier) { |
Chris Lattner | 34870da | 2007-08-29 22:54:08 +0000 | [diff] [blame] | 402 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 403 | for (unsigned i = 0; i < objc_NumQuals; ++i) |
| 404 | if (II == ObjcTypeQuals[i]) return true; |
Steve Naroff | 4fa7afd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 405 | } |
| 406 | return false; |
| 407 | } |
| 408 | |
Fariborz Jahanian | d0f97d1 | 2007-08-31 16:11:31 +0000 | [diff] [blame] | 409 | /// property-attrlist: one of |
| 410 | /// readonly getter setter assign retain copy nonatomic |
| 411 | /// |
| 412 | bool Parser::isObjCPropertyAttribute() { |
| 413 | if (Tok.getKind() == tok::identifier) { |
| 414 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 415 | for (unsigned i = 0; i < objc_NumAttrs; ++i) |
| 416 | if (II == ObjcPropertyAttrs[i]) return true; |
| 417 | } |
| 418 | return false; |
| 419 | } |
| 420 | |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 421 | /// objc-type-name: |
| 422 | /// '(' objc-type-qualifiers[opt] type-name ')' |
| 423 | /// '(' objc-type-qualifiers[opt] ')' |
| 424 | /// |
| 425 | /// objc-type-qualifiers: |
| 426 | /// objc-type-qualifier |
| 427 | /// objc-type-qualifiers objc-type-qualifier |
| 428 | /// |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 429 | Parser::TypeTy *Parser::ParseObjCTypeName() { |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 430 | assert(Tok.getKind() == tok::l_paren && "expected ("); |
| 431 | |
| 432 | SourceLocation LParenLoc = ConsumeParen(), RParenLoc; |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 433 | TypeTy *Ty; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 434 | |
Steve Naroff | 4fa7afd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 435 | while (isObjCTypeQualifier()) |
| 436 | ConsumeToken(); |
| 437 | |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 438 | if (isTypeSpecifierQualifier()) { |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 439 | Ty = ParseTypeName(); |
| 440 | // FIXME: back when Sema support is in place... |
| 441 | // assert(Ty && "Parser::ParseObjCTypeName(): missing type"); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 442 | } |
| 443 | if (Tok.getKind() != tok::r_paren) { |
| 444 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 445 | return 0; // FIXME: decide how we want to handle this error... |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 446 | } |
| 447 | RParenLoc = ConsumeParen(); |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 448 | return Ty; |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /// objc-method-decl: |
| 452 | /// objc-selector |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 453 | /// objc-keyword-selector objc-parmlist[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 454 | /// objc-type-name objc-selector |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 455 | /// objc-type-name objc-keyword-selector objc-parmlist[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 456 | /// |
| 457 | /// objc-keyword-selector: |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 458 | /// objc-keyword-decl |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 459 | /// objc-keyword-selector objc-keyword-decl |
| 460 | /// |
| 461 | /// objc-keyword-decl: |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 462 | /// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier |
| 463 | /// objc-selector ':' objc-keyword-attributes[opt] identifier |
| 464 | /// ':' objc-type-name objc-keyword-attributes[opt] identifier |
| 465 | /// ':' objc-keyword-attributes[opt] identifier |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 466 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 467 | /// objc-parmlist: |
| 468 | /// objc-parms objc-ellipsis[opt] |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 469 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 470 | /// objc-parms: |
| 471 | /// objc-parms , parameter-declaration |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 472 | /// |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 473 | /// objc-ellipsis: |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 474 | /// , ... |
| 475 | /// |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 476 | /// objc-keyword-attributes: [OBJC2] |
| 477 | /// __attribute__((unused)) |
| 478 | /// |
Steve Naroff | d16245b | 2007-09-17 15:07:43 +0000 | [diff] [blame^] | 479 | Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType, |
| 480 | SourceLocation mLoc) { |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 481 | TypeTy *ReturnType = 0; |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 482 | AttributeList *methodAttrs = 0; |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 483 | |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 484 | // Parse the return type. |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 485 | if (Tok.getKind() == tok::l_paren) |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 486 | ReturnType = ParseObjCTypeName(); |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 487 | IdentifierInfo *selIdent = ParseObjCSelector(); |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 488 | |
| 489 | llvm::SmallVector<ObjcKeywordInfo, 12> KeyInfo; |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 490 | |
| 491 | if (Tok.getKind() == tok::colon) { |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 492 | |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 493 | while (1) { |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 494 | ObjcKeywordInfo KeyInfoDecl; |
| 495 | KeyInfoDecl.SelectorName = selIdent; |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 496 | |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 497 | // Each iteration parses a single keyword argument. |
| 498 | if (Tok.getKind() != tok::colon) { |
| 499 | Diag(Tok, diag::err_expected_colon); |
| 500 | break; |
| 501 | } |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 502 | KeyInfoDecl.ColonLoc = ConsumeToken(); // Eat the ':'. |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 503 | if (Tok.getKind() == tok::l_paren) // Parse the argument type. |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 504 | KeyInfoDecl.TypeInfo = ParseObjCTypeName(); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 505 | |
| 506 | // If attributes exist before the argument name, parse them. |
Steve Naroff | 527fe23 | 2007-08-23 19:56:30 +0000 | [diff] [blame] | 507 | if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute) |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 508 | KeyInfoDecl.AttrList = ParseAttributes(); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 509 | |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 510 | if (Tok.getKind() != tok::identifier) { |
| 511 | Diag(Tok, diag::err_expected_ident); // missing argument name. |
| 512 | break; |
| 513 | } |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 514 | KeyInfoDecl.ArgumentName = Tok.getIdentifierInfo(); |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 515 | ConsumeToken(); // Eat the identifier. |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 516 | |
| 517 | // Rather than call out to the actions, try packaging up the info |
| 518 | // locally, like we do for Declarator. |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 519 | // FIXME: add Actions.BuildObjCKeyword() |
| 520 | |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 521 | KeyInfo.push_back(KeyInfoDecl); |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 522 | selIdent = ParseObjCSelector(); |
| 523 | if (!selIdent && Tok.getKind() != tok::colon) |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 524 | break; |
| 525 | // We have a selector or a colon, continue parsing. |
| 526 | } |
| 527 | // Parse the (optional) parameter list. |
| 528 | while (Tok.getKind() == tok::comma) { |
| 529 | ConsumeToken(); |
| 530 | if (Tok.getKind() == tok::ellipsis) { |
| 531 | ConsumeToken(); |
| 532 | break; |
| 533 | } |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 534 | // Parse the c-style argument declaration-specifier. |
| 535 | DeclSpec DS; |
| 536 | ParseDeclarationSpecifiers(DS); |
| 537 | // Parse the declarator. |
| 538 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 539 | ParseDeclarator(ParmDecl); |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 540 | } |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 541 | // FIXME: Add support for optional parmameter list... |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 542 | // If attributes exist after the method, parse them. |
| 543 | if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute) |
| 544 | methodAttrs = ParseAttributes(); |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 545 | return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType, |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 546 | &KeyInfo[0], KeyInfo.size(), |
| 547 | methodAttrs); |
Steve Naroff | 4985ace | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 548 | } else if (!selIdent) { |
| 549 | Diag(Tok, diag::err_expected_ident); // missing selector name. |
| 550 | } |
Fariborz Jahanian | e3a2ca7 | 2007-09-10 20:33:04 +0000 | [diff] [blame] | 551 | // If attributes exist after the method, parse them. |
| 552 | if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute) |
| 553 | methodAttrs = ParseAttributes(); |
| 554 | |
| 555 | return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType, selIdent, |
| 556 | methodAttrs); |
Steve Naroff | 294494e | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 559 | /// objc-protocol-refs: |
| 560 | /// '<' identifier-list '>' |
| 561 | /// |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 562 | bool Parser::ParseObjCProtocolReferences( |
| 563 | llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) { |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 564 | assert(Tok.getKind() == tok::less && "expected <"); |
| 565 | |
| 566 | ConsumeToken(); // the "<" |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 567 | |
| 568 | while (1) { |
| 569 | if (Tok.getKind() != tok::identifier) { |
| 570 | Diag(Tok, diag::err_expected_ident); |
| 571 | SkipUntil(tok::greater); |
| 572 | return true; |
| 573 | } |
| 574 | ProtocolRefs.push_back(Tok.getIdentifierInfo()); |
| 575 | ConsumeToken(); |
| 576 | |
| 577 | if (Tok.getKind() != tok::comma) |
| 578 | break; |
| 579 | ConsumeToken(); |
| 580 | } |
| 581 | // Consume the '>'. |
| 582 | return ExpectAndConsume(tok::greater, diag::err_expected_greater); |
| 583 | } |
| 584 | |
| 585 | /// objc-class-instance-variables: |
| 586 | /// '{' objc-instance-variable-decl-list[opt] '}' |
| 587 | /// |
| 588 | /// objc-instance-variable-decl-list: |
| 589 | /// objc-visibility-spec |
| 590 | /// objc-instance-variable-decl ';' |
| 591 | /// ';' |
| 592 | /// objc-instance-variable-decl-list objc-visibility-spec |
| 593 | /// objc-instance-variable-decl-list objc-instance-variable-decl ';' |
| 594 | /// objc-instance-variable-decl-list ';' |
| 595 | /// |
| 596 | /// objc-visibility-spec: |
| 597 | /// @private |
| 598 | /// @protected |
| 599 | /// @public |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 600 | /// @package [OBJC2] |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 601 | /// |
| 602 | /// objc-instance-variable-decl: |
| 603 | /// struct-declaration |
| 604 | /// |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 605 | void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 606 | assert(Tok.getKind() == tok::l_brace && "expected {"); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 607 | llvm::SmallVector<DeclTy*, 16> IvarDecls; |
Fariborz Jahanian | 7d6402f | 2007-09-13 20:56:13 +0000 | [diff] [blame] | 608 | llvm::SmallVector<DeclTy*, 32> AllIvarDecls; |
| 609 | llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities; |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 610 | |
| 611 | SourceLocation LBraceLoc = ConsumeBrace(); // the "{" |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 612 | |
Fariborz Jahanian | 7d6402f | 2007-09-13 20:56:13 +0000 | [diff] [blame] | 613 | tok::ObjCKeywordKind visibility = tok::objc_private; |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 614 | // While we still have something to read, read the instance variables. |
| 615 | while (Tok.getKind() != tok::r_brace && |
| 616 | Tok.getKind() != tok::eof) { |
| 617 | // Each iteration of this loop reads one objc-instance-variable-decl. |
| 618 | |
| 619 | // Check for extraneous top-level semicolon. |
| 620 | if (Tok.getKind() == tok::semi) { |
| 621 | Diag(Tok, diag::ext_extra_struct_semi); |
| 622 | ConsumeToken(); |
| 623 | continue; |
| 624 | } |
| 625 | // Set the default visibility to private. |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 626 | if (Tok.getKind() == tok::at) { // parse objc-visibility-spec |
| 627 | ConsumeToken(); // eat the @ sign |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 628 | switch (Tok.getObjCKeywordID()) { |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 629 | case tok::objc_private: |
| 630 | case tok::objc_public: |
| 631 | case tok::objc_protected: |
| 632 | case tok::objc_package: |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 633 | visibility = Tok.getObjCKeywordID(); |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 634 | ConsumeToken(); |
| 635 | continue; |
| 636 | default: |
| 637 | Diag(Tok, diag::err_objc_illegal_visibility_spec); |
| 638 | ConsumeToken(); |
| 639 | continue; |
| 640 | } |
| 641 | } |
| 642 | ParseStructDeclaration(interfaceDecl, IvarDecls); |
Fariborz Jahanian | 7d6402f | 2007-09-13 20:56:13 +0000 | [diff] [blame] | 643 | for (unsigned i = 0; i < IvarDecls.size(); i++) { |
| 644 | AllIvarDecls.push_back(IvarDecls[i]); |
| 645 | AllVisibilities.push_back(visibility); |
| 646 | } |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 647 | IvarDecls.clear(); |
| 648 | |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 649 | if (Tok.getKind() == tok::semi) { |
| 650 | ConsumeToken(); |
| 651 | } else if (Tok.getKind() == tok::r_brace) { |
| 652 | Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list); |
| 653 | break; |
| 654 | } else { |
| 655 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 656 | // Skip to end of block or statement |
| 657 | SkipUntil(tok::r_brace, true, true); |
| 658 | } |
| 659 | } |
Fariborz Jahanian | 7d6402f | 2007-09-13 20:56:13 +0000 | [diff] [blame] | 660 | if (AllIvarDecls.size()) { // Check for {} - no ivars in braces |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 661 | Actions.ActOnFields(LBraceLoc, interfaceDecl, |
| 662 | &AllIvarDecls[0], AllIvarDecls.size(), |
| 663 | &AllVisibilities[0]); |
Fariborz Jahanian | 7d6402f | 2007-09-13 20:56:13 +0000 | [diff] [blame] | 664 | } |
Steve Naroff | ddbff78 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 665 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 666 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 667 | } |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 668 | |
| 669 | /// objc-protocol-declaration: |
| 670 | /// objc-protocol-definition |
| 671 | /// objc-protocol-forward-reference |
| 672 | /// |
| 673 | /// objc-protocol-definition: |
| 674 | /// @protocol identifier |
| 675 | /// objc-protocol-refs[opt] |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 676 | /// objc-interface-decl-list |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 677 | /// @end |
| 678 | /// |
| 679 | /// objc-protocol-forward-reference: |
| 680 | /// @protocol identifier-list ';' |
| 681 | /// |
| 682 | /// "@protocol identifier ;" should be resolved as "@protocol |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 683 | /// identifier-list ;": objc-interface-decl-list may not start with a |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 684 | /// semicolon in the first alternative if objc-protocol-refs are omitted. |
| 685 | |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 686 | Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) { |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 687 | assert(Tok.isObjCAtKeyword(tok::objc_protocol) && |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 688 | "ParseObjCAtProtocolDeclaration(): Expected @protocol"); |
| 689 | ConsumeToken(); // the "protocol" identifier |
| 690 | |
| 691 | if (Tok.getKind() != tok::identifier) { |
| 692 | Diag(Tok, diag::err_expected_ident); // missing protocol name. |
| 693 | return 0; |
| 694 | } |
| 695 | // Save the protocol name, then consume it. |
| 696 | IdentifierInfo *protocolName = Tok.getIdentifierInfo(); |
| 697 | SourceLocation nameLoc = ConsumeToken(); |
| 698 | |
| 699 | if (Tok.getKind() == tok::semi) { // forward declaration. |
| 700 | ConsumeToken(); |
| 701 | return 0; // FIXME: add protocolName |
| 702 | } |
| 703 | if (Tok.getKind() == tok::comma) { // list of forward declarations. |
| 704 | // Parse the list of forward declarations. |
| 705 | llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs; |
| 706 | ProtocolRefs.push_back(protocolName); |
| 707 | |
| 708 | while (1) { |
| 709 | ConsumeToken(); // the ',' |
| 710 | if (Tok.getKind() != tok::identifier) { |
| 711 | Diag(Tok, diag::err_expected_ident); |
| 712 | SkipUntil(tok::semi); |
| 713 | return 0; |
| 714 | } |
| 715 | ProtocolRefs.push_back(Tok.getIdentifierInfo()); |
| 716 | ConsumeToken(); // the identifier |
| 717 | |
| 718 | if (Tok.getKind() != tok::comma) |
| 719 | break; |
| 720 | } |
| 721 | // Consume the ';'. |
| 722 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol")) |
| 723 | return 0; |
| 724 | return 0; // FIXME |
| 725 | } |
| 726 | // Last, and definitely not least, parse a protocol declaration. |
| 727 | if (Tok.getKind() == tok::less) { |
Steve Naroff | f28b264 | 2007-09-05 23:30:30 +0000 | [diff] [blame] | 728 | llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs; |
| 729 | if (ParseObjCProtocolReferences(ProtocolRefs)) |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 730 | return 0; |
| 731 | } |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 732 | ParseObjCInterfaceDeclList(0/*FIXME*/); |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 733 | |
| 734 | // The @ sign was already consumed by ParseObjCInterfaceDeclList(). |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 735 | if (Tok.isObjCAtKeyword(tok::objc_end)) { |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 736 | ConsumeToken(); // the "end" identifier |
| 737 | return 0; |
| 738 | } |
| 739 | Diag(Tok, diag::err_objc_missing_end); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 740 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 741 | } |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 742 | |
| 743 | /// objc-implementation: |
| 744 | /// objc-class-implementation-prologue |
| 745 | /// objc-category-implementation-prologue |
| 746 | /// |
| 747 | /// objc-class-implementation-prologue: |
| 748 | /// @implementation identifier objc-superclass[opt] |
| 749 | /// objc-class-instance-variables[opt] |
| 750 | /// |
| 751 | /// objc-category-implementation-prologue: |
| 752 | /// @implementation identifier ( identifier ) |
| 753 | |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 754 | Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration( |
| 755 | SourceLocation atLoc) { |
| 756 | assert(Tok.isObjCAtKeyword(tok::objc_implementation) && |
| 757 | "ParseObjCAtImplementationDeclaration(): Expected @implementation"); |
| 758 | ConsumeToken(); // the "implementation" identifier |
| 759 | |
| 760 | if (Tok.getKind() != tok::identifier) { |
| 761 | Diag(Tok, diag::err_expected_ident); // missing class or category name. |
| 762 | return 0; |
| 763 | } |
| 764 | // We have a class or category name - consume it. |
| 765 | SourceLocation nameLoc = ConsumeToken(); // consume class or category name |
| 766 | |
| 767 | if (Tok.getKind() == tok::l_paren) { |
| 768 | // we have a category implementation. |
| 769 | SourceLocation lparenLoc = ConsumeParen(); |
| 770 | SourceLocation categoryLoc, rparenLoc; |
| 771 | IdentifierInfo *categoryId = 0; |
| 772 | |
| 773 | if (Tok.getKind() == tok::identifier) { |
| 774 | categoryId = Tok.getIdentifierInfo(); |
| 775 | categoryLoc = ConsumeToken(); |
| 776 | } else { |
| 777 | Diag(Tok, diag::err_expected_ident); // missing category name. |
| 778 | return 0; |
| 779 | } |
| 780 | if (Tok.getKind() != tok::r_paren) { |
| 781 | Diag(Tok, diag::err_expected_rparen); |
| 782 | SkipUntil(tok::r_paren, false); // don't stop at ';' |
| 783 | return 0; |
| 784 | } |
| 785 | rparenLoc = ConsumeParen(); |
| 786 | return 0; |
| 787 | } |
| 788 | // We have a class implementation |
| 789 | if (Tok.getKind() == tok::colon) { |
| 790 | // We have a super class |
| 791 | ConsumeToken(); |
| 792 | if (Tok.getKind() != tok::identifier) { |
| 793 | Diag(Tok, diag::err_expected_ident); // missing super class name. |
| 794 | return 0; |
| 795 | } |
| 796 | ConsumeToken(); // Consume super class name |
| 797 | } |
| 798 | if (Tok.getKind() == tok::l_brace) |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 799 | ParseObjCClassInstanceVariables(0/*FIXME*/); // we have ivars |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 800 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 801 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 802 | } |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 803 | Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) { |
| 804 | assert(Tok.isObjCAtKeyword(tok::objc_end) && |
| 805 | "ParseObjCAtEndDeclaration(): Expected @end"); |
| 806 | ConsumeToken(); // the "end" identifier |
| 807 | |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 808 | return 0; |
| 809 | } |
Fariborz Jahanian | e992af0 | 2007-09-04 19:26:51 +0000 | [diff] [blame] | 810 | |
| 811 | /// compatibility-alias-decl: |
| 812 | /// @compatibility_alias alias-name class-name ';' |
| 813 | /// |
| 814 | Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) { |
| 815 | assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) && |
| 816 | "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias"); |
| 817 | ConsumeToken(); // consume compatibility_alias |
| 818 | if (Tok.getKind() != tok::identifier) { |
| 819 | Diag(Tok, diag::err_expected_ident); |
| 820 | return 0; |
| 821 | } |
| 822 | ConsumeToken(); // consume alias-name |
| 823 | if (Tok.getKind() != tok::identifier) { |
| 824 | Diag(Tok, diag::err_expected_ident); |
| 825 | return 0; |
| 826 | } |
| 827 | ConsumeToken(); // consume class-name; |
| 828 | if (Tok.getKind() != tok::semi) |
Fariborz Jahanian | 8cd8c66 | 2007-09-04 21:42:12 +0000 | [diff] [blame] | 829 | Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias"); |
Steve Naroff | dac269b | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 830 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 833 | /// property-synthesis: |
| 834 | /// @synthesize property-ivar-list ';' |
| 835 | /// |
| 836 | /// property-ivar-list: |
| 837 | /// property-ivar |
| 838 | /// property-ivar-list ',' property-ivar |
| 839 | /// |
| 840 | /// property-ivar: |
| 841 | /// identifier |
| 842 | /// identifier '=' identifier |
| 843 | /// |
| 844 | Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) { |
| 845 | assert(Tok.isObjCAtKeyword(tok::objc_synthesize) && |
| 846 | "ParseObjCPropertyDynamic(): Expected '@synthesize'"); |
| 847 | SourceLocation loc = ConsumeToken(); // consume dynamic |
| 848 | if (Tok.getKind() != tok::identifier) { |
| 849 | Diag(Tok, diag::err_expected_ident); |
| 850 | return 0; |
| 851 | } |
| 852 | while (Tok.getKind() == tok::identifier) { |
| 853 | ConsumeToken(); // consume property name |
| 854 | if (Tok.getKind() == tok::equal) { |
| 855 | // property '=' ivar-name |
| 856 | ConsumeToken(); // consume '=' |
| 857 | if (Tok.getKind() != tok::identifier) { |
| 858 | Diag(Tok, diag::err_expected_ident); |
| 859 | break; |
| 860 | } |
| 861 | ConsumeToken(); // consume ivar-name |
| 862 | } |
| 863 | if (Tok.getKind() != tok::comma) |
| 864 | break; |
| 865 | ConsumeToken(); // consume ',' |
| 866 | } |
| 867 | if (Tok.getKind() != tok::semi) |
| 868 | Diag(Tok, diag::err_expected_semi_after, "@synthesize"); |
| 869 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 872 | /// property-dynamic: |
| 873 | /// @dynamic property-list |
| 874 | /// |
| 875 | /// property-list: |
| 876 | /// identifier |
| 877 | /// property-list ',' identifier |
| 878 | /// |
| 879 | Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) { |
| 880 | assert(Tok.isObjCAtKeyword(tok::objc_dynamic) && |
| 881 | "ParseObjCPropertyDynamic(): Expected '@dynamic'"); |
| 882 | SourceLocation loc = ConsumeToken(); // consume dynamic |
| 883 | if (Tok.getKind() != tok::identifier) { |
| 884 | Diag(Tok, diag::err_expected_ident); |
| 885 | return 0; |
| 886 | } |
| 887 | while (Tok.getKind() == tok::identifier) { |
| 888 | ConsumeToken(); // consume property name |
| 889 | if (Tok.getKind() != tok::comma) |
| 890 | break; |
| 891 | ConsumeToken(); // consume ',' |
| 892 | } |
| 893 | if (Tok.getKind() != tok::semi) |
| 894 | Diag(Tok, diag::err_expected_semi_after, "@dynamic"); |
| 895 | return 0; |
| 896 | } |
| 897 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 898 | /// objc-method-def: objc-method-proto ';'[opt] '{' body '}' |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 899 | /// |
| 900 | void Parser::ParseObjCInstanceMethodDefinition() { |
| 901 | assert(Tok.getKind() == tok::minus && |
| 902 | "ParseObjCInstanceMethodDefinition(): Expected '-'"); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 903 | ParseObjCMethodPrototype(ObjcImpDecl); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 904 | // parse optional ';' |
| 905 | if (Tok.getKind() == tok::semi) |
| 906 | ConsumeToken(); |
| 907 | |
| 908 | if (Tok.getKind() != tok::l_brace) { |
| 909 | Diag (Tok, diag::err_expected_lbrace); |
| 910 | return; |
| 911 | } |
| 912 | |
| 913 | StmtResult FnBody = ParseCompoundStatementBody(); |
| 914 | } |
| 915 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 916 | /// objc-method-def: objc-method-proto ';'[opt] '{' body '}' |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 917 | /// |
Steve Naroff | 7ef58fd | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 918 | void Parser::ParseObjCClassMethodDefinition() { |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 919 | assert(Tok.getKind() == tok::plus && |
| 920 | "ParseObjCClassMethodDefinition(): Expected '+'"); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 921 | ParseObjCMethodPrototype(ObjcImpDecl); |
Fariborz Jahanian | ac00b7f | 2007-09-01 00:26:16 +0000 | [diff] [blame] | 922 | // parse optional ';' |
| 923 | if (Tok.getKind() == tok::semi) |
| 924 | ConsumeToken(); |
| 925 | if (Tok.getKind() != tok::l_brace) { |
| 926 | Diag (Tok, diag::err_expected_lbrace); |
| 927 | return; |
| 928 | } |
| 929 | |
| 930 | StmtResult FnBody = ParseCompoundStatementBody(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 931 | } |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 932 | |
| 933 | Parser::ExprResult Parser::ParseObjCExpression() { |
| 934 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
| 935 | |
| 936 | switch (Tok.getKind()) { |
| 937 | case tok::string_literal: // primary-expression: string-literal |
| 938 | case tok::wide_string_literal: |
| 939 | return ParseObjCStringLiteral(); |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 940 | default: |
| 941 | break; |
| 942 | } |
| 943 | |
| 944 | switch (Tok.getIdentifierInfo()->getObjCKeywordID()) { |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 945 | case tok::objc_encode: |
| 946 | return ParseObjCEncodeExpression(); |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 947 | case tok::objc_protocol: |
| 948 | return ParseObjCProtocolExpression(); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 949 | default: |
| 950 | Diag(AtLoc, diag::err_unexpected_at); |
| 951 | SkipUntil(tok::semi); |
| 952 | break; |
| 953 | } |
| 954 | |
| 955 | return 0; |
| 956 | } |
| 957 | |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 958 | /// objc-message-expr: |
| 959 | /// '[' objc-receiver objc-message-args ']' |
| 960 | /// |
| 961 | /// objc-receiver: |
| 962 | /// expression |
| 963 | /// class-name |
| 964 | /// type-name |
| 965 | /// |
| 966 | /// objc-message-args: |
| 967 | /// objc-selector |
| 968 | /// objc-keywordarg-list |
| 969 | /// |
| 970 | /// objc-keywordarg-list: |
| 971 | /// objc-keywordarg |
| 972 | /// objc-keywordarg-list objc-keywordarg |
| 973 | /// |
| 974 | /// objc-keywordarg: |
| 975 | /// selector-name[opt] ':' objc-keywordexpr |
| 976 | /// |
| 977 | /// objc-keywordexpr: |
| 978 | /// nonempty-expr-list |
| 979 | /// |
| 980 | /// nonempty-expr-list: |
| 981 | /// assignment-expression |
| 982 | /// nonempty-expr-list , assignment-expression |
| 983 | /// |
| 984 | Parser::ExprResult Parser::ParseObjCMessageExpression() { |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 985 | assert(Tok.getKind() == tok::l_square && "'[' expected"); |
| 986 | SourceLocation Loc = ConsumeBracket(); // consume '[' |
| 987 | // Parse receiver |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 988 | if (Tok.getKind() == tok::identifier && |
| 989 | Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) |
| 990 | ConsumeToken(); |
| 991 | else |
| 992 | ParseAssignmentExpression(); |
Fariborz Jahanian | a65ff6c | 2007-09-05 23:08:20 +0000 | [diff] [blame] | 993 | // Parse objc-selector |
| 994 | IdentifierInfo *selIdent = ParseObjCSelector(); |
| 995 | if (Tok.getKind() == tok::colon) { |
| 996 | while (1) { |
| 997 | // Each iteration parses a single keyword argument. |
| 998 | if (Tok.getKind() != tok::colon) { |
| 999 | Diag(Tok, diag::err_expected_colon); |
| 1000 | SkipUntil(tok::semi); |
| 1001 | return 0; |
| 1002 | } |
| 1003 | ConsumeToken(); // Eat the ':'. |
| 1004 | /// Parse the expression after ':' |
| 1005 | ParseAssignmentExpression(); |
| 1006 | IdentifierInfo *keywordSelector = ParseObjCSelector(); |
| 1007 | |
| 1008 | if (!keywordSelector && Tok.getKind() != tok::colon) |
| 1009 | break; |
| 1010 | // We have a selector or a colon, continue parsing. |
| 1011 | } |
| 1012 | // Parse the, optional, argument list, comma separated. |
| 1013 | while (Tok.getKind() == tok::comma) { |
| 1014 | ConsumeToken(); |
| 1015 | /// Parse the expression after ',' |
| 1016 | ParseAssignmentExpression(); |
| 1017 | } |
| 1018 | } else if (!selIdent) { |
| 1019 | Diag(Tok, diag::err_expected_ident); // missing selector name. |
| 1020 | SkipUntil(tok::semi); |
| 1021 | return 0; |
| 1022 | } |
| 1023 | if (Tok.getKind() != tok::r_square) { |
| 1024 | Diag(Tok, diag::err_expected_rsquare); |
| 1025 | SkipUntil(tok::semi); |
| 1026 | return 0; |
| 1027 | } |
| 1028 | ConsumeBracket(); // consume ']' |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 1029 | return 0; // FIXME: return a message expr AST! |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1032 | Parser::ExprResult Parser::ParseObjCStringLiteral() { |
| 1033 | ExprResult Res = ParseStringLiteralExpression(); |
| 1034 | |
| 1035 | if (Res.isInvalid) return Res; |
| 1036 | |
| 1037 | return Actions.ParseObjCStringLiteral(Res.Val); |
| 1038 | } |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1039 | |
| 1040 | /// objc-encode-expression: |
| 1041 | /// @encode ( type-name ) |
| 1042 | Parser::ExprResult Parser::ParseObjCEncodeExpression() { |
Steve Naroff | 861cf3e | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 1043 | assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!"); |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1044 | |
| 1045 | SourceLocation EncLoc = ConsumeToken(); |
| 1046 | |
| 1047 | if (Tok.getKind() != tok::l_paren) { |
| 1048 | Diag(Tok, diag::err_expected_lparen_after, "@encode"); |
| 1049 | return true; |
| 1050 | } |
| 1051 | |
| 1052 | SourceLocation LParenLoc = ConsumeParen(); |
| 1053 | |
| 1054 | TypeTy *Ty = ParseTypeName(); |
| 1055 | |
Anders Carlsson | 4988ae3 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 1056 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1057 | |
| 1058 | return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty, |
Anders Carlsson | 4988ae3 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 1059 | RParenLoc); |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1060 | } |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1061 | |
| 1062 | /// objc-protocol-expression |
| 1063 | /// @protocol ( protocol-name ) |
| 1064 | |
| 1065 | Parser::ExprResult Parser::ParseObjCProtocolExpression() |
| 1066 | { |
| 1067 | SourceLocation ProtoLoc = ConsumeToken(); |
| 1068 | |
| 1069 | if (Tok.getKind() != tok::l_paren) { |
| 1070 | Diag(Tok, diag::err_expected_lparen_after, "@protocol"); |
| 1071 | return true; |
| 1072 | } |
| 1073 | |
| 1074 | SourceLocation LParenLoc = ConsumeParen(); |
| 1075 | |
| 1076 | if (Tok.getKind() != tok::identifier) { |
| 1077 | Diag(Tok, diag::err_expected_ident); |
| 1078 | return true; |
| 1079 | } |
| 1080 | |
| 1081 | // FIXME: Do something with the protocol name |
| 1082 | ConsumeToken(); |
| 1083 | |
Anders Carlsson | 4988ae3 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 1084 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Anders Carlsson | 29b2cb1 | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 1085 | |
| 1086 | // FIXME |
| 1087 | return 0; |
| 1088 | } |