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