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