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