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" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 29 | Parser::DeclTy *Parser::ParseObjCAtDirectives() { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 30 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
| 31 | |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 32 | switch (Tok.getObjCKeywordID()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 33 | case tok::objc_class: |
| 34 | return ParseObjCAtClassDeclaration(AtLoc); |
| 35 | case tok::objc_interface: |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 36 | return ParseObjCAtInterfaceDeclaration(AtLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 37 | case tok::objc_protocol: |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 38 | return ParseObjCAtProtocolDeclaration(AtLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 39 | case tok::objc_implementation: |
| 40 | return ParseObjCAtImplementationDeclaration(); |
| 41 | case tok::objc_end: |
| 42 | return ParseObjCAtEndDeclaration(); |
| 43 | case tok::objc_compatibility_alias: |
| 44 | return ParseObjCAtAliasDeclaration(); |
| 45 | default: |
| 46 | Diag(AtLoc, diag::err_unexpected_at); |
| 47 | SkipUntil(tok::semi); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 48 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | /// |
| 53 | /// objc-class-declaration: |
| 54 | /// '@' 'class' identifier-list ';' |
| 55 | /// |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 56 | Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 57 | ConsumeToken(); // the identifier "class" |
| 58 | llvm::SmallVector<IdentifierInfo *, 8> ClassNames; |
| 59 | |
| 60 | while (1) { |
| 61 | if (Tok.getKind() != tok::identifier) { |
| 62 | Diag(Tok, diag::err_expected_ident); |
| 63 | SkipUntil(tok::semi); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 64 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 65 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 66 | ClassNames.push_back(Tok.getIdentifierInfo()); |
| 67 | ConsumeToken(); |
| 68 | |
| 69 | if (Tok.getKind() != tok::comma) |
| 70 | break; |
| 71 | |
| 72 | ConsumeToken(); |
| 73 | } |
| 74 | |
| 75 | // Consume the ';'. |
| 76 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class")) |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 77 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 78 | |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 79 | return Actions.ParsedObjcClassDeclaration(CurScope, |
| 80 | &ClassNames[0], ClassNames.size()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 83 | /// |
| 84 | /// objc-interface: |
| 85 | /// objc-class-interface-attributes[opt] objc-class-interface |
| 86 | /// objc-category-interface |
| 87 | /// |
| 88 | /// objc-class-interface: |
| 89 | /// '@' 'interface' identifier objc-superclass[opt] |
| 90 | /// objc-protocol-refs[opt] |
| 91 | /// objc-class-instance-variables[opt] |
| 92 | /// objc-interface-decl-list |
| 93 | /// @end |
| 94 | /// |
| 95 | /// objc-category-interface: |
| 96 | /// '@' 'interface' identifier '(' identifier[opt] ')' |
| 97 | /// objc-protocol-refs[opt] |
| 98 | /// objc-interface-decl-list |
| 99 | /// @end |
| 100 | /// |
| 101 | /// objc-superclass: |
| 102 | /// ':' identifier |
| 103 | /// |
| 104 | /// objc-class-interface-attributes: |
| 105 | /// __attribute__((visibility("default"))) |
| 106 | /// __attribute__((visibility("hidden"))) |
| 107 | /// __attribute__((deprecated)) |
| 108 | /// __attribute__((unavailable)) |
| 109 | /// __attribute__((objc_exception)) - used by NSException on 64-bit |
| 110 | /// |
| 111 | Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration( |
| 112 | SourceLocation atLoc, AttributeList *attrList) { |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 113 | assert(Tok.isObjCAtKeyword(tok::objc_interface) && |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 114 | "ParseObjCAtInterfaceDeclaration(): Expected @interface"); |
| 115 | ConsumeToken(); // the "interface" identifier |
| 116 | |
| 117 | if (Tok.getKind() != tok::identifier) { |
| 118 | Diag(Tok, diag::err_expected_ident); // missing class or category name. |
| 119 | return 0; |
| 120 | } |
| 121 | // We have a class or category name - consume it. |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 122 | IdentifierInfo *nameId = Tok.getIdentifierInfo(); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 123 | SourceLocation nameLoc = ConsumeToken(); |
| 124 | |
| 125 | if (Tok.getKind() == tok::l_paren) { // we have a category |
| 126 | SourceLocation lparenLoc = ConsumeParen(); |
| 127 | SourceLocation categoryLoc, rparenLoc; |
| 128 | IdentifierInfo *categoryId = 0; |
| 129 | |
| 130 | // OBJC2: The cateogry name is optional (not an error). |
| 131 | if (Tok.getKind() == tok::identifier) { |
| 132 | categoryId = Tok.getIdentifierInfo(); |
| 133 | categoryLoc = ConsumeToken(); |
| 134 | } |
| 135 | if (Tok.getKind() != tok::r_paren) { |
| 136 | Diag(Tok, diag::err_expected_rparen); |
| 137 | SkipUntil(tok::r_paren, false); // don't stop at ';' |
| 138 | return 0; |
| 139 | } |
| 140 | rparenLoc = ConsumeParen(); |
| 141 | // Next, we need to check for any protocol references. |
| 142 | if (Tok.getKind() == tok::less) { |
| 143 | if (ParseObjCProtocolReferences()) |
| 144 | return 0; |
| 145 | } |
| 146 | if (attrList) // categories don't support attributes. |
| 147 | Diag(Tok, diag::err_objc_no_attributes_on_category); |
| 148 | |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 149 | ParseObjCInterfaceDeclList(0/*FIXME*/); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 150 | |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 151 | // The @ sign was already consumed by ParseObjCInterfaceDeclList(). |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 152 | if (Tok.isObjCAtKeyword(tok::objc_end)) { |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 153 | ConsumeToken(); // the "end" identifier |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 154 | return 0; |
| 155 | } |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 156 | Diag(Tok, diag::err_objc_missing_end); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | // Parse a class interface. |
| 160 | IdentifierInfo *superClassId = 0; |
| 161 | SourceLocation superClassLoc; |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 162 | |
| 163 | // FIXME: temporary hack to grok class names (until we have sema support). |
| 164 | llvm::SmallVector<IdentifierInfo *, 1> ClassName; |
| 165 | ClassName.push_back(nameId); |
| 166 | Actions.ParsedObjcClassDeclaration(CurScope, &ClassName[0], 1); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 167 | |
| 168 | if (Tok.getKind() == tok::colon) { // a super class is specified. |
| 169 | ConsumeToken(); |
| 170 | if (Tok.getKind() != tok::identifier) { |
| 171 | Diag(Tok, diag::err_expected_ident); // missing super class name. |
| 172 | return 0; |
| 173 | } |
| 174 | superClassId = Tok.getIdentifierInfo(); |
| 175 | superClassLoc = ConsumeToken(); |
| 176 | } |
| 177 | // Next, we need to check for any protocol references. |
| 178 | if (Tok.getKind() == tok::less) { |
| 179 | if (ParseObjCProtocolReferences()) |
| 180 | return 0; |
| 181 | } |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 182 | // FIXME: add Actions.StartObjCClassInterface(nameId, superClassId, ...) |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 183 | if (Tok.getKind() == tok::l_brace) |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 184 | ParseObjCClassInstanceVariables(0/*FIXME*/); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 185 | |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 186 | ParseObjCInterfaceDeclList(0/*FIXME*/); |
| 187 | |
| 188 | // The @ sign was already consumed by ParseObjCInterfaceDeclList(). |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 189 | if (Tok.isObjCAtKeyword(tok::objc_end)) { |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 190 | ConsumeToken(); // the "end" identifier |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 191 | return 0; |
| 192 | } |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 193 | Diag(Tok, diag::err_objc_missing_end); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /// objc-interface-decl-list: |
| 198 | /// empty |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 199 | /// objc-interface-decl-list objc-property-decl [OBJC2] |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 200 | /// objc-interface-decl-list objc-method-requirement [OBJC2] |
| 201 | /// objc-interface-decl-list objc-method-proto |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 202 | /// objc-interface-decl-list declaration |
| 203 | /// objc-interface-decl-list ';' |
| 204 | /// |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 205 | /// objc-method-requirement: [OBJC2] |
| 206 | /// @required |
| 207 | /// @optional |
| 208 | /// |
| 209 | void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl) { |
| 210 | while (1) { |
| 211 | if (Tok.getKind() == tok::at) { |
| 212 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 213 | tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID(); |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 214 | |
| 215 | if (ocKind == tok::objc_end) { // terminate list |
| 216 | return; |
| 217 | } else if (ocKind == tok::objc_required) { // protocols only |
| 218 | ConsumeToken(); |
| 219 | continue; |
| 220 | } else if (ocKind == tok::objc_optional) { // protocols only |
| 221 | ConsumeToken(); |
| 222 | continue; |
| 223 | } else if (ocKind == tok::objc_property) { |
| 224 | ParseObjCPropertyDecl(AtLoc); |
| 225 | continue; |
| 226 | } else { |
| 227 | Diag(Tok, diag::err_objc_illegal_interface_qual); |
| 228 | ConsumeToken(); |
| 229 | } |
| 230 | } |
| 231 | if (Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) { |
| 232 | ParseObjCMethodPrototype(); |
| 233 | continue; |
| 234 | } |
| 235 | if (Tok.getKind() == tok::semi) |
| 236 | ConsumeToken(); |
| 237 | else if (Tok.getKind() == tok::eof) |
| 238 | return; |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 239 | else |
| 240 | // FIXME: as the name implies, this rule allows function definitions. |
| 241 | // We could pass a flag or check for functions during semantic analysis. |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 242 | ParseDeclarationOrFunctionDefinition(); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void Parser::ParseObjCPropertyDecl(SourceLocation atLoc) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 247 | assert(0 && "Unimp"); |
| 248 | } |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 249 | |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 250 | /// objc-methodproto: |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 251 | /// objc-instance-method objc-method-decl objc-method-attributes[opt] ';' |
| 252 | /// objc-class-method objc-method-decl objc-method-attributes[opt] ';' |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 253 | /// |
| 254 | /// objc-instance-method: '-' |
| 255 | /// objc-class-method: '+' |
| 256 | /// |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 257 | /// objc-method-attributes: [OBJC2] |
| 258 | /// __attribute__((deprecated)) |
| 259 | /// |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 260 | void Parser::ParseObjCMethodPrototype() { |
| 261 | assert((Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) && |
| 262 | "expected +/-"); |
| 263 | |
| 264 | tok::TokenKind methodType = Tok.getKind(); |
| 265 | SourceLocation methodLoc = ConsumeToken(); |
| 266 | |
| 267 | // FIXME: deal with "context sensitive" protocol qualifiers in prototypes |
| 268 | ParseObjCMethodDecl(methodType, methodLoc); |
| 269 | |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 270 | // If attributes exist after the method, parse them. |
| 271 | if (Tok.getKind() == tok::kw___attribute) |
| 272 | ParseAttributes(); |
| 273 | |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 274 | // Consume the ';'. |
| 275 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "method proto"); |
| 276 | } |
| 277 | |
| 278 | /// objc-selector: |
| 279 | /// identifier |
| 280 | /// one of |
| 281 | /// enum struct union if else while do for switch case default |
| 282 | /// break continue return goto asm sizeof typeof __alignof |
| 283 | /// unsigned long const short volatile signed restrict _Complex |
| 284 | /// in out inout bycopy byref oneway int char float double void _Bool |
| 285 | /// |
| 286 | IdentifierInfo *Parser::ParseObjCSelector() { |
| 287 | tok::TokenKind tKind = Tok.getKind(); |
| 288 | IdentifierInfo *II = 0; |
| 289 | |
| 290 | if (tKind == tok::identifier || |
| 291 | (tKind >= tok::kw_auto && tKind <= tok::kw__Complex)) { |
| 292 | // FIXME: make sure the list of keywords jives with gcc. For example, |
| 293 | // the above test does not include in/out/inout/bycopy/byref/oneway. |
| 294 | II = Tok.getIdentifierInfo(); |
| 295 | ConsumeToken(); |
| 296 | } |
| 297 | return II; |
| 298 | } |
| 299 | |
Steve Naroff | a8ee226 | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 300 | /// objc-type-qualifier: one of |
| 301 | /// in out inout bycopy byref oneway |
| 302 | /// |
| 303 | /// FIXME: remove the string compares... |
| 304 | bool Parser::isObjCTypeQualifier() { |
| 305 | if (Tok.getKind() == tok::identifier) { |
| 306 | const char *qual = Tok.getIdentifierInfo()->getName(); |
| 307 | return (strcmp(qual, "in") == 0) || (strcmp(qual, "out") == 0) || |
| 308 | (strcmp(qual, "inout") == 0) || (strcmp(qual, "oneway") == 0) || |
| 309 | (strcmp(qual, "bycopy") == 0) || (strcmp(qual, "byref") == 0); |
| 310 | } |
| 311 | return false; |
| 312 | } |
| 313 | |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 314 | /// objc-type-name: |
| 315 | /// '(' objc-type-qualifiers[opt] type-name ')' |
| 316 | /// '(' objc-type-qualifiers[opt] ')' |
| 317 | /// |
| 318 | /// objc-type-qualifiers: |
| 319 | /// objc-type-qualifier |
| 320 | /// objc-type-qualifiers objc-type-qualifier |
| 321 | /// |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 322 | void Parser::ParseObjCTypeName() { |
| 323 | assert(Tok.getKind() == tok::l_paren && "expected ("); |
| 324 | |
| 325 | SourceLocation LParenLoc = ConsumeParen(), RParenLoc; |
| 326 | |
Steve Naroff | a8ee226 | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 327 | while (isObjCTypeQualifier()) |
| 328 | ConsumeToken(); |
| 329 | |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 330 | if (isTypeSpecifierQualifier()) { |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 331 | //TypeTy *Ty = ParseTypeName(); |
| 332 | //assert(Ty && "Parser::ParseObjCTypeName(): missing type"); |
| 333 | ParseTypeName(); // FIXME: when sema support is added. |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 334 | } |
| 335 | if (Tok.getKind() != tok::r_paren) { |
| 336 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 337 | return; |
| 338 | } |
| 339 | RParenLoc = ConsumeParen(); |
| 340 | } |
| 341 | |
| 342 | /// objc-method-decl: |
| 343 | /// objc-selector |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 344 | /// objc-keyword-selector objc-parmlist[opt] |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 345 | /// objc-type-name objc-selector |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 346 | /// objc-type-name objc-keyword-selector objc-parmlist[opt] |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 347 | /// |
| 348 | /// objc-keyword-selector: |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 349 | /// objc-keyword-decl |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 350 | /// objc-keyword-selector objc-keyword-decl |
| 351 | /// |
| 352 | /// objc-keyword-decl: |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 353 | /// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier |
| 354 | /// objc-selector ':' objc-keyword-attributes[opt] identifier |
| 355 | /// ':' objc-type-name objc-keyword-attributes[opt] identifier |
| 356 | /// ':' objc-keyword-attributes[opt] identifier |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 357 | /// |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 358 | /// objc-parmlist: |
| 359 | /// objc-parms objc-ellipsis[opt] |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 360 | /// |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 361 | /// objc-parms: |
| 362 | /// objc-parms , parameter-declaration |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 363 | /// |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 364 | /// objc-ellipsis: |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 365 | /// , ... |
| 366 | /// |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 367 | /// objc-keyword-attributes: [OBJC2] |
| 368 | /// __attribute__((unused)) |
| 369 | /// |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 370 | void Parser::ParseObjCMethodDecl(tok::TokenKind mType, SourceLocation mLoc) { |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 371 | |
| 372 | // Parse the return type. |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 373 | if (Tok.getKind() == tok::l_paren) |
| 374 | ParseObjCTypeName(); |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 375 | IdentifierInfo *selIdent = ParseObjCSelector(); |
| 376 | |
| 377 | if (Tok.getKind() == tok::colon) { |
| 378 | IdentifierInfo *keywordSelector = selIdent; |
| 379 | while (1) { |
| 380 | // Each iteration parses a single keyword argument. |
| 381 | if (Tok.getKind() != tok::colon) { |
| 382 | Diag(Tok, diag::err_expected_colon); |
| 383 | break; |
| 384 | } |
| 385 | ConsumeToken(); // Eat the ':'. |
| 386 | if (Tok.getKind() == tok::l_paren) // Parse the argument type. |
| 387 | ParseObjCTypeName(); |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 388 | |
| 389 | // If attributes exist before the argument name, parse them. |
| 390 | if (Tok.getKind() == tok::kw___attribute) |
| 391 | ParseAttributes(); |
| 392 | |
Steve Naroff | 09a0c4c | 2007-08-22 18:35:33 +0000 | [diff] [blame] | 393 | if (Tok.getKind() != tok::identifier) { |
| 394 | Diag(Tok, diag::err_expected_ident); // missing argument name. |
| 395 | break; |
| 396 | } |
| 397 | ConsumeToken(); // Eat the identifier. |
| 398 | // FIXME: add Actions.BuildObjCKeyword() |
| 399 | |
| 400 | keywordSelector = ParseObjCSelector(); |
| 401 | if (!keywordSelector && Tok.getKind() != tok::colon) |
| 402 | break; |
| 403 | // We have a selector or a colon, continue parsing. |
| 404 | } |
| 405 | // Parse the (optional) parameter list. |
| 406 | while (Tok.getKind() == tok::comma) { |
| 407 | ConsumeToken(); |
| 408 | if (Tok.getKind() == tok::ellipsis) { |
| 409 | ConsumeToken(); |
| 410 | break; |
| 411 | } |
| 412 | ParseDeclaration(Declarator::PrototypeContext); |
| 413 | } |
| 414 | } else if (!selIdent) { |
| 415 | Diag(Tok, diag::err_expected_ident); // missing selector name. |
| 416 | } |
| 417 | // FIXME: add Actions.BuildMethodSignature(). |
Steve Naroff | 0bbffd8 | 2007-08-22 16:35:03 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 420 | /// objc-protocol-refs: |
| 421 | /// '<' identifier-list '>' |
| 422 | /// |
| 423 | bool Parser::ParseObjCProtocolReferences() { |
| 424 | assert(Tok.getKind() == tok::less && "expected <"); |
| 425 | |
| 426 | ConsumeToken(); // the "<" |
| 427 | llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs; |
| 428 | |
| 429 | while (1) { |
| 430 | if (Tok.getKind() != tok::identifier) { |
| 431 | Diag(Tok, diag::err_expected_ident); |
| 432 | SkipUntil(tok::greater); |
| 433 | return true; |
| 434 | } |
| 435 | ProtocolRefs.push_back(Tok.getIdentifierInfo()); |
| 436 | ConsumeToken(); |
| 437 | |
| 438 | if (Tok.getKind() != tok::comma) |
| 439 | break; |
| 440 | ConsumeToken(); |
| 441 | } |
| 442 | // Consume the '>'. |
| 443 | return ExpectAndConsume(tok::greater, diag::err_expected_greater); |
| 444 | } |
| 445 | |
| 446 | /// objc-class-instance-variables: |
| 447 | /// '{' objc-instance-variable-decl-list[opt] '}' |
| 448 | /// |
| 449 | /// objc-instance-variable-decl-list: |
| 450 | /// objc-visibility-spec |
| 451 | /// objc-instance-variable-decl ';' |
| 452 | /// ';' |
| 453 | /// objc-instance-variable-decl-list objc-visibility-spec |
| 454 | /// objc-instance-variable-decl-list objc-instance-variable-decl ';' |
| 455 | /// objc-instance-variable-decl-list ';' |
| 456 | /// |
| 457 | /// objc-visibility-spec: |
| 458 | /// @private |
| 459 | /// @protected |
| 460 | /// @public |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 461 | /// @package [OBJC2] |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 462 | /// |
| 463 | /// objc-instance-variable-decl: |
| 464 | /// struct-declaration |
| 465 | /// |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 466 | void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) { |
| 467 | assert(Tok.getKind() == tok::l_brace && "expected {"); |
| 468 | |
| 469 | SourceLocation LBraceLoc = ConsumeBrace(); // the "{" |
| 470 | llvm::SmallVector<DeclTy*, 32> IvarDecls; |
| 471 | |
| 472 | // While we still have something to read, read the instance variables. |
| 473 | while (Tok.getKind() != tok::r_brace && |
| 474 | Tok.getKind() != tok::eof) { |
| 475 | // Each iteration of this loop reads one objc-instance-variable-decl. |
| 476 | |
| 477 | // Check for extraneous top-level semicolon. |
| 478 | if (Tok.getKind() == tok::semi) { |
| 479 | Diag(Tok, diag::ext_extra_struct_semi); |
| 480 | ConsumeToken(); |
| 481 | continue; |
| 482 | } |
| 483 | // Set the default visibility to private. |
| 484 | tok::ObjCKeywordKind visibility = tok::objc_private; |
| 485 | if (Tok.getKind() == tok::at) { // parse objc-visibility-spec |
| 486 | ConsumeToken(); // eat the @ sign |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 487 | switch (Tok.getObjCKeywordID()) { |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 488 | case tok::objc_private: |
| 489 | case tok::objc_public: |
| 490 | case tok::objc_protected: |
| 491 | case tok::objc_package: |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 492 | visibility = Tok.getObjCKeywordID(); |
Steve Naroff | c447499 | 2007-08-21 21:17:12 +0000 | [diff] [blame] | 493 | ConsumeToken(); |
| 494 | continue; |
| 495 | default: |
| 496 | Diag(Tok, diag::err_objc_illegal_visibility_spec); |
| 497 | ConsumeToken(); |
| 498 | continue; |
| 499 | } |
| 500 | } |
| 501 | ParseStructDeclaration(interfaceDecl, IvarDecls); |
| 502 | |
| 503 | if (Tok.getKind() == tok::semi) { |
| 504 | ConsumeToken(); |
| 505 | } else if (Tok.getKind() == tok::r_brace) { |
| 506 | Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list); |
| 507 | break; |
| 508 | } else { |
| 509 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 510 | // Skip to end of block or statement |
| 511 | SkipUntil(tok::r_brace, true, true); |
| 512 | } |
| 513 | } |
| 514 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 515 | return; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 516 | } |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 517 | |
| 518 | /// objc-protocol-declaration: |
| 519 | /// objc-protocol-definition |
| 520 | /// objc-protocol-forward-reference |
| 521 | /// |
| 522 | /// objc-protocol-definition: |
| 523 | /// @protocol identifier |
| 524 | /// objc-protocol-refs[opt] |
| 525 | /// objc-methodprotolist |
| 526 | /// @end |
| 527 | /// |
| 528 | /// objc-protocol-forward-reference: |
| 529 | /// @protocol identifier-list ';' |
| 530 | /// |
| 531 | /// "@protocol identifier ;" should be resolved as "@protocol |
| 532 | /// identifier-list ;": objc-methodprotolist may not start with a |
| 533 | /// semicolon in the first alternative if objc-protocol-refs are omitted. |
| 534 | |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 535 | Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) { |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 536 | assert(Tok.isObjCAtKeyword(tok::objc_protocol) && |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 537 | "ParseObjCAtProtocolDeclaration(): Expected @protocol"); |
| 538 | ConsumeToken(); // the "protocol" identifier |
| 539 | |
| 540 | if (Tok.getKind() != tok::identifier) { |
| 541 | Diag(Tok, diag::err_expected_ident); // missing protocol name. |
| 542 | return 0; |
| 543 | } |
| 544 | // Save the protocol name, then consume it. |
| 545 | IdentifierInfo *protocolName = Tok.getIdentifierInfo(); |
| 546 | SourceLocation nameLoc = ConsumeToken(); |
| 547 | |
| 548 | if (Tok.getKind() == tok::semi) { // forward declaration. |
| 549 | ConsumeToken(); |
| 550 | return 0; // FIXME: add protocolName |
| 551 | } |
| 552 | if (Tok.getKind() == tok::comma) { // list of forward declarations. |
| 553 | // Parse the list of forward declarations. |
| 554 | llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs; |
| 555 | ProtocolRefs.push_back(protocolName); |
| 556 | |
| 557 | while (1) { |
| 558 | ConsumeToken(); // the ',' |
| 559 | if (Tok.getKind() != tok::identifier) { |
| 560 | Diag(Tok, diag::err_expected_ident); |
| 561 | SkipUntil(tok::semi); |
| 562 | return 0; |
| 563 | } |
| 564 | ProtocolRefs.push_back(Tok.getIdentifierInfo()); |
| 565 | ConsumeToken(); // the identifier |
| 566 | |
| 567 | if (Tok.getKind() != tok::comma) |
| 568 | break; |
| 569 | } |
| 570 | // Consume the ';'. |
| 571 | if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol")) |
| 572 | return 0; |
| 573 | return 0; // FIXME |
| 574 | } |
| 575 | // Last, and definitely not least, parse a protocol declaration. |
| 576 | if (Tok.getKind() == tok::less) { |
| 577 | if (ParseObjCProtocolReferences()) |
| 578 | return 0; |
| 579 | } |
| 580 | ParseObjCInterfaceDeclList(0/*FIXME*/); |
| 581 | |
| 582 | // The @ sign was already consumed by ParseObjCInterfaceDeclList(). |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 583 | if (Tok.isObjCAtKeyword(tok::objc_end)) { |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 584 | ConsumeToken(); // the "end" identifier |
| 585 | return 0; |
| 586 | } |
| 587 | Diag(Tok, diag::err_objc_missing_end); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 588 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 589 | } |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 590 | |
| 591 | /// objc-implementation: |
| 592 | /// objc-class-implementation-prologue |
| 593 | /// objc-category-implementation-prologue |
| 594 | /// |
| 595 | /// objc-class-implementation-prologue: |
| 596 | /// @implementation identifier objc-superclass[opt] |
| 597 | /// objc-class-instance-variables[opt] |
| 598 | /// |
| 599 | /// objc-category-implementation-prologue: |
| 600 | /// @implementation identifier ( identifier ) |
| 601 | |
| 602 | Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration() { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 603 | assert(0 && "Unimp"); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 604 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 605 | } |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 606 | Parser::DeclTy *Parser::ParseObjCAtEndDeclaration() { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 607 | assert(0 && "Unimp"); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 608 | return 0; |
| 609 | } |
| 610 | Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration() { |
| 611 | assert(0 && "Unimp"); |
| 612 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 615 | void Parser::ParseObjCInstanceMethodDefinition() { |
| 616 | assert(0 && "Parser::ParseObjCInstanceMethodDefinition():: Unimp"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Steve Naroff | 72f17fb | 2007-08-22 22:17:26 +0000 | [diff] [blame] | 619 | void Parser::ParseObjCClassMethodDefinition() { |
| 620 | assert(0 && "Parser::ParseObjCClassMethodDefinition():: Unimp"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 621 | } |
Anders Carlsson | a66cad4 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 622 | |
| 623 | Parser::ExprResult Parser::ParseObjCExpression() { |
| 624 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
| 625 | |
| 626 | switch (Tok.getKind()) { |
| 627 | case tok::string_literal: // primary-expression: string-literal |
| 628 | case tok::wide_string_literal: |
| 629 | return ParseObjCStringLiteral(); |
Anders Carlsson | 2996b4e | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 630 | default: |
| 631 | break; |
| 632 | } |
| 633 | |
| 634 | switch (Tok.getIdentifierInfo()->getObjCKeywordID()) { |
Anders Carlsson | 8be1d40 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 635 | case tok::objc_encode: |
| 636 | return ParseObjCEncodeExpression(); |
Anders Carlsson | 2996b4e | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 637 | case tok::objc_protocol: |
| 638 | return ParseObjCProtocolExpression(); |
Anders Carlsson | a66cad4 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 639 | default: |
| 640 | Diag(AtLoc, diag::err_unexpected_at); |
| 641 | SkipUntil(tok::semi); |
| 642 | break; |
| 643 | } |
| 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | Parser::ExprResult Parser::ParseObjCStringLiteral() { |
| 649 | ExprResult Res = ParseStringLiteralExpression(); |
| 650 | |
| 651 | if (Res.isInvalid) return Res; |
| 652 | |
| 653 | return Actions.ParseObjCStringLiteral(Res.Val); |
| 654 | } |
Anders Carlsson | 8be1d40 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 655 | |
| 656 | /// objc-encode-expression: |
| 657 | /// @encode ( type-name ) |
| 658 | Parser::ExprResult Parser::ParseObjCEncodeExpression() { |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 659 | assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!"); |
Anders Carlsson | 8be1d40 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 660 | |
| 661 | SourceLocation EncLoc = ConsumeToken(); |
| 662 | |
| 663 | if (Tok.getKind() != tok::l_paren) { |
| 664 | Diag(Tok, diag::err_expected_lparen_after, "@encode"); |
| 665 | return true; |
| 666 | } |
| 667 | |
| 668 | SourceLocation LParenLoc = ConsumeParen(); |
| 669 | |
| 670 | TypeTy *Ty = ParseTypeName(); |
| 671 | |
Anders Carlsson | 92faeb8 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 672 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Anders Carlsson | 8be1d40 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 673 | |
| 674 | return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty, |
Anders Carlsson | 92faeb8 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 675 | RParenLoc); |
Anders Carlsson | 8be1d40 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 676 | } |
Anders Carlsson | 2996b4e | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 677 | |
| 678 | /// objc-protocol-expression |
| 679 | /// @protocol ( protocol-name ) |
| 680 | |
| 681 | Parser::ExprResult Parser::ParseObjCProtocolExpression() |
| 682 | { |
| 683 | SourceLocation ProtoLoc = ConsumeToken(); |
| 684 | |
| 685 | if (Tok.getKind() != tok::l_paren) { |
| 686 | Diag(Tok, diag::err_expected_lparen_after, "@protocol"); |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | SourceLocation LParenLoc = ConsumeParen(); |
| 691 | |
| 692 | if (Tok.getKind() != tok::identifier) { |
| 693 | Diag(Tok, diag::err_expected_ident); |
| 694 | return true; |
| 695 | } |
| 696 | |
| 697 | // FIXME: Do something with the protocol name |
| 698 | ConsumeToken(); |
| 699 | |
Anders Carlsson | 92faeb8 | 2007-08-23 15:31:37 +0000 | [diff] [blame] | 700 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Anders Carlsson | 2996b4e | 2007-08-23 15:25:28 +0000 | [diff] [blame] | 701 | |
| 702 | // FIXME |
| 703 | return 0; |
| 704 | } |