blob: d3b8d8dccc2a9267db978470c38a707d5f69f977 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseObjc.cpp - Objective C Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Steve Naroff and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Steve Naroff09a0c4c2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Basic/Diagnostic.h"
17#include "llvm/ADT/SmallVector.h"
18using namespace clang;
19
20
21/// ParseExternalDeclaration:
22/// external-declaration: [C99 6.9]
23/// [OBJC] objc-class-definition
24/// [OBJC] objc-class-declaration [TODO]
25/// [OBJC] objc-alias-declaration [TODO]
26/// [OBJC] objc-protocol-definition [TODO]
27/// [OBJC] objc-method-definition [TODO]
28/// [OBJC] '@' 'end' [TODO]
Steve Narofffb367882007-08-20 21:31:48 +000029Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Chris Lattner4b009652007-07-25 00:24:17 +000030 SourceLocation AtLoc = ConsumeToken(); // the "@"
31
Steve Naroff87c329f2007-08-23 18:16:40 +000032 switch (Tok.getObjCKeywordID()) {
Chris Lattner4b009652007-07-25 00:24:17 +000033 case tok::objc_class:
34 return ParseObjCAtClassDeclaration(AtLoc);
35 case tok::objc_interface:
Steve Narofffb367882007-08-20 21:31:48 +000036 return ParseObjCAtInterfaceDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000037 case tok::objc_protocol:
Steve Naroff72f17fb2007-08-22 22:17:26 +000038 return ParseObjCAtProtocolDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000039 case tok::objc_implementation:
Steve Naroff81f1bba2007-09-06 21:24:23 +000040 return ObjcImpDecl = ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000041 case tok::objc_end:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000042 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000043 case tok::objc_compatibility_alias:
Fariborz Jahanianb62aff32007-09-04 19:26:51 +000044 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000045 case tok::objc_synthesize:
46 return ParseObjCPropertySynthesize(AtLoc);
47 case tok::objc_dynamic:
48 return ParseObjCPropertyDynamic(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000049 default:
50 Diag(AtLoc, diag::err_unexpected_at);
51 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000052 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000053 }
54}
55
56///
57/// objc-class-declaration:
58/// '@' 'class' identifier-list ';'
59///
Steve Narofffb367882007-08-20 21:31:48 +000060Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000061 ConsumeToken(); // the identifier "class"
62 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
63
64 while (1) {
65 if (Tok.getKind() != tok::identifier) {
66 Diag(Tok, diag::err_expected_ident);
67 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000068 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000069 }
Chris Lattner4b009652007-07-25 00:24:17 +000070 ClassNames.push_back(Tok.getIdentifierInfo());
71 ConsumeToken();
72
73 if (Tok.getKind() != tok::comma)
74 break;
75
76 ConsumeToken();
77 }
78
79 // Consume the ';'.
80 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Narofffb367882007-08-20 21:31:48 +000081 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000082
Steve Naroff81f1bba2007-09-06 21:24:23 +000083 return Actions.ObjcClassDeclaration(CurScope, atLoc,
84 &ClassNames[0], ClassNames.size());
Chris Lattner4b009652007-07-25 00:24:17 +000085}
86
Steve Narofffb367882007-08-20 21:31:48 +000087///
88/// objc-interface:
89/// objc-class-interface-attributes[opt] objc-class-interface
90/// objc-category-interface
91///
92/// objc-class-interface:
93/// '@' 'interface' identifier objc-superclass[opt]
94/// objc-protocol-refs[opt]
95/// objc-class-instance-variables[opt]
96/// objc-interface-decl-list
97/// @end
98///
99/// objc-category-interface:
100/// '@' 'interface' identifier '(' identifier[opt] ')'
101/// objc-protocol-refs[opt]
102/// objc-interface-decl-list
103/// @end
104///
105/// objc-superclass:
106/// ':' identifier
107///
108/// objc-class-interface-attributes:
109/// __attribute__((visibility("default")))
110/// __attribute__((visibility("hidden")))
111/// __attribute__((deprecated))
112/// __attribute__((unavailable))
113/// __attribute__((objc_exception)) - used by NSException on 64-bit
114///
115Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
116 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000117 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Narofffb367882007-08-20 21:31:48 +0000118 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
119 ConsumeToken(); // the "interface" identifier
120
121 if (Tok.getKind() != tok::identifier) {
122 Diag(Tok, diag::err_expected_ident); // missing class or category name.
123 return 0;
124 }
125 // We have a class or category name - consume it.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000126 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Narofffb367882007-08-20 21:31:48 +0000127 SourceLocation nameLoc = ConsumeToken();
128
Steve Naroffa7f62782007-08-23 19:56:30 +0000129 if (Tok.getKind() == tok::l_paren) { // we have a category.
Steve Narofffb367882007-08-20 21:31:48 +0000130 SourceLocation lparenLoc = ConsumeParen();
131 SourceLocation categoryLoc, rparenLoc;
132 IdentifierInfo *categoryId = 0;
133
Steve Naroffa7f62782007-08-23 19:56:30 +0000134 // For ObjC2, the category name is optional (not an error).
Steve Narofffb367882007-08-20 21:31:48 +0000135 if (Tok.getKind() == tok::identifier) {
136 categoryId = Tok.getIdentifierInfo();
137 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000138 } else if (!getLang().ObjC2) {
139 Diag(Tok, diag::err_expected_ident); // missing category name.
140 return 0;
Steve Narofffb367882007-08-20 21:31:48 +0000141 }
142 if (Tok.getKind() != tok::r_paren) {
143 Diag(Tok, diag::err_expected_rparen);
144 SkipUntil(tok::r_paren, false); // don't stop at ';'
145 return 0;
146 }
147 rparenLoc = ConsumeParen();
148 // Next, we need to check for any protocol references.
149 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000150 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
151 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000152 return 0;
153 }
154 if (attrList) // categories don't support attributes.
155 Diag(Tok, diag::err_objc_no_attributes_on_category);
156
Steve Naroff81f1bba2007-09-06 21:24:23 +0000157 ParseObjCInterfaceDeclList(0/*FIXME*/);
Steve Narofffb367882007-08-20 21:31:48 +0000158
Steve Naroff0bbffd82007-08-22 16:35:03 +0000159 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000160 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000161 ConsumeToken(); // the "end" identifier
Steve Narofffb367882007-08-20 21:31:48 +0000162 return 0;
163 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000164 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000165 return 0;
166 }
167 // Parse a class interface.
168 IdentifierInfo *superClassId = 0;
169 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000170
Steve Narofffb367882007-08-20 21:31:48 +0000171 if (Tok.getKind() == tok::colon) { // a super class is specified.
172 ConsumeToken();
173 if (Tok.getKind() != tok::identifier) {
174 Diag(Tok, diag::err_expected_ident); // missing super class name.
175 return 0;
176 }
177 superClassId = Tok.getIdentifierInfo();
178 superClassLoc = ConsumeToken();
179 }
180 // Next, we need to check for any protocol references.
Steve Naroff304ed392007-09-05 23:30:30 +0000181 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofffb367882007-08-20 21:31:48 +0000182 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000183 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000184 return 0;
185 }
Steve Naroff304ed392007-09-05 23:30:30 +0000186 DeclTy *ClsType = Actions.ObjcStartClassInterface(atLoc, nameId, nameLoc,
187 superClassId, superClassLoc, &ProtocolRefs[0],
188 ProtocolRefs.size(), attrList);
189
Steve Narofffb367882007-08-20 21:31:48 +0000190 if (Tok.getKind() == tok::l_brace)
Steve Naroff81f1bba2007-09-06 21:24:23 +0000191 ParseObjCClassInstanceVariables(ClsType);
Steve Narofffb367882007-08-20 21:31:48 +0000192
Steve Naroff81f1bba2007-09-06 21:24:23 +0000193 ParseObjCInterfaceDeclList(ClsType);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000194
195 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000196 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000197 ConsumeToken(); // the "end" identifier
Steve Narofffaed3bf2007-09-10 20:51:04 +0000198 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000199 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000200 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000201 return 0;
202}
203
204/// objc-interface-decl-list:
205/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000206/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000207/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000208/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000209/// objc-interface-decl-list declaration
210/// objc-interface-decl-list ';'
211///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000212/// objc-method-requirement: [OBJC2]
213/// @required
214/// @optional
215///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000216void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl) {
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000217 llvm::SmallVector<DeclTy*, 32> allMethods;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000218 while (1) {
219 if (Tok.getKind() == tok::at) {
220 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000221 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000222
223 if (ocKind == tok::objc_end) { // terminate list
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000224 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000225 } else if (ocKind == tok::objc_required) { // protocols only
226 ConsumeToken();
227 continue;
228 } else if (ocKind == tok::objc_optional) { // protocols only
229 ConsumeToken();
230 continue;
231 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000232 ParseObjCPropertyDecl(interfaceDecl);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000233 continue;
234 } else {
235 Diag(Tok, diag::err_objc_illegal_interface_qual);
236 ConsumeToken();
237 }
238 }
239 if (Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) {
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000240 allMethods.push_back(ParseObjCMethodPrototype(interfaceDecl));
Steve Naroff81f1bba2007-09-06 21:24:23 +0000241 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
242 // method definitions.
243 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000244 continue;
245 }
246 if (Tok.getKind() == tok::semi)
247 ConsumeToken();
248 else if (Tok.getKind() == tok::eof)
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000249 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000250 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000251 // FIXME: as the name implies, this rule allows function definitions.
252 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000253 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000254 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000255 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000256
257 /// Insert collected methods declarations into the @interface object.
258 Actions.ObjcAddMethodsToClass(interfaceDecl, &allMethods[0], allMethods.size());
259 return;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000260}
261
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000262/// Parse property attribute declarations.
263///
264/// property-attr-decl: '(' property-attrlist ')'
265/// property-attrlist:
266/// property-attribute
267/// property-attrlist ',' property-attribute
268/// property-attribute:
269/// getter '=' identifier
270/// setter '=' identifier ':'
271/// readonly
272/// readwrite
273/// assign
274/// retain
275/// copy
276/// nonatomic
277///
278void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
279 SourceLocation loc = ConsumeParen(); // consume '('
280 while (isObjCPropertyAttribute()) {
281 const IdentifierInfo *II = Tok.getIdentifierInfo();
282 // getter/setter require extra treatment.
283 if (II == ObjcPropertyAttrs[objc_getter] ||
284 II == ObjcPropertyAttrs[objc_setter]) {
285 // skip getter/setter part.
286 SourceLocation loc = ConsumeToken();
287 if (Tok.getKind() == tok::equal) {
288 loc = ConsumeToken();
289 if (Tok.getKind() == tok::identifier) {
290 if (II == ObjcPropertyAttrs[objc_setter]) {
291 loc = ConsumeToken(); // consume method name
292 if (Tok.getKind() != tok::colon) {
293 Diag(loc, diag::err_expected_colon);
294 SkipUntil(tok::r_paren,true,true);
295 break;
296 }
297 }
298 }
299 else {
300 Diag(loc, diag::err_expected_ident);
301 SkipUntil(tok::r_paren,true,true);
302 break;
303 }
304 }
305 else {
306 Diag(loc, diag::err_objc_expected_equal);
307 SkipUntil(tok::r_paren,true,true);
308 break;
309 }
310 }
311 ConsumeToken(); // consume last attribute token
312 if (Tok.getKind() == tok::comma) {
313 loc = ConsumeToken();
314 continue;
315 }
316 if (Tok.getKind() == tok::r_paren)
317 break;
318 Diag(loc, diag::err_expected_rparen);
319 SkipUntil(tok::semi);
320 return;
321 }
322 if (Tok.getKind() == tok::r_paren)
323 ConsumeParen();
324 else {
325 Diag(loc, diag::err_objc_expected_property_attr);
326 SkipUntil(tok::r_paren); // recover from error inside attribute list
327 }
328}
329
330/// Main routine to parse property declaration.
331///
332/// @property property-attr-decl[opt] property-component-decl ';'
333///
334void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
335 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
336 "ParseObjCPropertyDecl(): Expected @property");
337 ConsumeToken(); // the "property" identifier
338 // Parse property attribute list, if any.
339 if (Tok.getKind() == tok::l_paren) {
340 // property has attribute list.
341 ParseObjCPropertyAttribute(0/*FIXME*/);
342 }
343 // Parse declaration portion of @property.
344 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
345 ParseStructDeclaration(interfaceDecl, PropertyDecls);
346 if (Tok.getKind() == tok::semi)
347 ConsumeToken();
348 else {
349 Diag(Tok, diag::err_expected_semi_decl_list);
350 SkipUntil(tok::r_brace, true, true);
351 }
Chris Lattner4b009652007-07-25 00:24:17 +0000352}
Steve Narofffb367882007-08-20 21:31:48 +0000353
Steve Naroff81f1bba2007-09-06 21:24:23 +0000354/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000355/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000356/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000357///
358/// objc-instance-method: '-'
359/// objc-class-method: '+'
360///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000361/// objc-method-attributes: [OBJC2]
362/// __attribute__((deprecated))
363///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000364Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *CDecl) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000365 assert((Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) &&
366 "expected +/-");
367
368 tok::TokenKind methodType = Tok.getKind();
369 SourceLocation methodLoc = ConsumeToken();
370
Steve Naroff304ed392007-09-05 23:30:30 +0000371 DeclTy *MDecl = ParseObjCMethodDecl(methodType, methodLoc);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000372 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000373 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000374 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000375}
376
377/// objc-selector:
378/// identifier
379/// one of
380/// enum struct union if else while do for switch case default
381/// break continue return goto asm sizeof typeof __alignof
382/// unsigned long const short volatile signed restrict _Complex
383/// in out inout bycopy byref oneway int char float double void _Bool
384///
385IdentifierInfo *Parser::ParseObjCSelector() {
386 tok::TokenKind tKind = Tok.getKind();
387 IdentifierInfo *II = 0;
388
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000389 if (tKind == tok::identifier || tKind == tok::kw_typeof ||
390 tKind == tok::kw___alignof ||
Steve Naroff0bbffd82007-08-22 16:35:03 +0000391 (tKind >= tok::kw_auto && tKind <= tok::kw__Complex)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000392 II = Tok.getIdentifierInfo();
393 ConsumeToken();
394 }
395 return II;
396}
397
Steve Naroffa8ee2262007-08-22 23:18:22 +0000398/// objc-type-qualifier: one of
399/// in out inout bycopy byref oneway
400///
Steve Naroffa8ee2262007-08-22 23:18:22 +0000401bool Parser::isObjCTypeQualifier() {
402 if (Tok.getKind() == tok::identifier) {
Chris Lattner32352462007-08-29 22:54:08 +0000403 const IdentifierInfo *II = Tok.getIdentifierInfo();
404 for (unsigned i = 0; i < objc_NumQuals; ++i)
405 if (II == ObjcTypeQuals[i]) return true;
Steve Naroffa8ee2262007-08-22 23:18:22 +0000406 }
407 return false;
408}
409
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000410/// property-attrlist: one of
411/// readonly getter setter assign retain copy nonatomic
412///
413bool Parser::isObjCPropertyAttribute() {
414 if (Tok.getKind() == tok::identifier) {
415 const IdentifierInfo *II = Tok.getIdentifierInfo();
416 for (unsigned i = 0; i < objc_NumAttrs; ++i)
417 if (II == ObjcPropertyAttrs[i]) return true;
418 }
419 return false;
420}
421
Steve Naroff0bbffd82007-08-22 16:35:03 +0000422/// objc-type-name:
423/// '(' objc-type-qualifiers[opt] type-name ')'
424/// '(' objc-type-qualifiers[opt] ')'
425///
426/// objc-type-qualifiers:
427/// objc-type-qualifier
428/// objc-type-qualifiers objc-type-qualifier
429///
Steve Naroff304ed392007-09-05 23:30:30 +0000430Parser::TypeTy *Parser::ParseObjCTypeName() {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000431 assert(Tok.getKind() == tok::l_paren && "expected (");
432
433 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Steve Naroff304ed392007-09-05 23:30:30 +0000434 TypeTy *Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000435
Steve Naroffa8ee2262007-08-22 23:18:22 +0000436 while (isObjCTypeQualifier())
437 ConsumeToken();
438
Steve Naroff0bbffd82007-08-22 16:35:03 +0000439 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000440 Ty = ParseTypeName();
441 // FIXME: back when Sema support is in place...
442 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000443 }
444 if (Tok.getKind() != tok::r_paren) {
445 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000446 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000447 }
448 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000449 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000450}
451
452/// objc-method-decl:
453/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000454/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000455/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000456/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000457///
458/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000459/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000460/// objc-keyword-selector objc-keyword-decl
461///
462/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000463/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
464/// objc-selector ':' objc-keyword-attributes[opt] identifier
465/// ':' objc-type-name objc-keyword-attributes[opt] identifier
466/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000467///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000468/// objc-parmlist:
469/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000470///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000471/// objc-parms:
472/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000473///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000474/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000475/// , ...
476///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000477/// objc-keyword-attributes: [OBJC2]
478/// __attribute__((unused))
479///
Steve Naroff304ed392007-09-05 23:30:30 +0000480Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType, SourceLocation mLoc) {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000481
Steve Naroff304ed392007-09-05 23:30:30 +0000482 TypeTy *ReturnType = 0;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000483 AttributeList *methodAttrs = 0;
Steve Naroff304ed392007-09-05 23:30:30 +0000484
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000485 // Parse the return type.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000486 if (Tok.getKind() == tok::l_paren)
Steve Naroff304ed392007-09-05 23:30:30 +0000487 ReturnType = ParseObjCTypeName();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000488 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff304ed392007-09-05 23:30:30 +0000489
490 llvm::SmallVector<ObjcKeywordInfo, 12> KeyInfo;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000491
492 if (Tok.getKind() == tok::colon) {
Steve Naroff304ed392007-09-05 23:30:30 +0000493
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000494 while (1) {
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000495 ObjcKeywordInfo KeyInfoDecl;
496 KeyInfoDecl.SelectorName = selIdent;
Steve Naroff304ed392007-09-05 23:30:30 +0000497
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000498 // Each iteration parses a single keyword argument.
499 if (Tok.getKind() != tok::colon) {
500 Diag(Tok, diag::err_expected_colon);
501 break;
502 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000503 KeyInfoDecl.ColonLoc = ConsumeToken(); // Eat the ':'.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000504 if (Tok.getKind() == tok::l_paren) // Parse the argument type.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000505 KeyInfoDecl.TypeInfo = ParseObjCTypeName();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000506
507 // If attributes exist before the argument name, parse them.
Steve Naroffa7f62782007-08-23 19:56:30 +0000508 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000509 KeyInfoDecl.AttrList = ParseAttributes();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000510
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000511 if (Tok.getKind() != tok::identifier) {
512 Diag(Tok, diag::err_expected_ident); // missing argument name.
513 break;
514 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000515 KeyInfoDecl.ArgumentName = Tok.getIdentifierInfo();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000516 ConsumeToken(); // Eat the identifier.
Steve Naroff304ed392007-09-05 23:30:30 +0000517
518 // Rather than call out to the actions, try packaging up the info
519 // locally, like we do for Declarator.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000520 // FIXME: add Actions.BuildObjCKeyword()
521
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000522 KeyInfo.push_back(KeyInfoDecl);
Steve Naroff304ed392007-09-05 23:30:30 +0000523 selIdent = ParseObjCSelector();
524 if (!selIdent && Tok.getKind() != tok::colon)
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000525 break;
526 // We have a selector or a colon, continue parsing.
527 }
528 // Parse the (optional) parameter list.
529 while (Tok.getKind() == tok::comma) {
530 ConsumeToken();
531 if (Tok.getKind() == tok::ellipsis) {
532 ConsumeToken();
533 break;
534 }
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000535 // Parse the c-style argument declaration-specifier.
536 DeclSpec DS;
537 ParseDeclarationSpecifiers(DS);
538 // Parse the declarator.
539 Declarator ParmDecl(DS, Declarator::PrototypeContext);
540 ParseDeclarator(ParmDecl);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000541 }
Steve Naroff304ed392007-09-05 23:30:30 +0000542 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000543 // If attributes exist after the method, parse them.
544 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
545 methodAttrs = ParseAttributes();
Steve Naroff304ed392007-09-05 23:30:30 +0000546 return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType,
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000547 &KeyInfo[0], KeyInfo.size(),
548 methodAttrs);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000549 } else if (!selIdent) {
550 Diag(Tok, diag::err_expected_ident); // missing selector name.
551 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000552 // If attributes exist after the method, parse them.
553 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
554 methodAttrs = ParseAttributes();
555
556 return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType, selIdent,
557 methodAttrs);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000558}
559
Steve Narofffb367882007-08-20 21:31:48 +0000560/// objc-protocol-refs:
561/// '<' identifier-list '>'
562///
Steve Naroff304ed392007-09-05 23:30:30 +0000563bool Parser::ParseObjCProtocolReferences(
564 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Steve Narofffb367882007-08-20 21:31:48 +0000565 assert(Tok.getKind() == tok::less && "expected <");
566
567 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000568
569 while (1) {
570 if (Tok.getKind() != tok::identifier) {
571 Diag(Tok, diag::err_expected_ident);
572 SkipUntil(tok::greater);
573 return true;
574 }
575 ProtocolRefs.push_back(Tok.getIdentifierInfo());
576 ConsumeToken();
577
578 if (Tok.getKind() != tok::comma)
579 break;
580 ConsumeToken();
581 }
582 // Consume the '>'.
583 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
584}
585
586/// objc-class-instance-variables:
587/// '{' objc-instance-variable-decl-list[opt] '}'
588///
589/// objc-instance-variable-decl-list:
590/// objc-visibility-spec
591/// objc-instance-variable-decl ';'
592/// ';'
593/// objc-instance-variable-decl-list objc-visibility-spec
594/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
595/// objc-instance-variable-decl-list ';'
596///
597/// objc-visibility-spec:
598/// @private
599/// @protected
600/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000601/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000602///
603/// objc-instance-variable-decl:
604/// struct-declaration
605///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000606void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Steve Naroffc4474992007-08-21 21:17:12 +0000607 assert(Tok.getKind() == tok::l_brace && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000608 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000609 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
610 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000611
612 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000613
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000614 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000615 // While we still have something to read, read the instance variables.
616 while (Tok.getKind() != tok::r_brace &&
617 Tok.getKind() != tok::eof) {
618 // Each iteration of this loop reads one objc-instance-variable-decl.
619
620 // Check for extraneous top-level semicolon.
621 if (Tok.getKind() == tok::semi) {
622 Diag(Tok, diag::ext_extra_struct_semi);
623 ConsumeToken();
624 continue;
625 }
626 // Set the default visibility to private.
Steve Naroffc4474992007-08-21 21:17:12 +0000627 if (Tok.getKind() == tok::at) { // parse objc-visibility-spec
628 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000629 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000630 case tok::objc_private:
631 case tok::objc_public:
632 case tok::objc_protected:
633 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000634 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000635 ConsumeToken();
636 continue;
637 default:
638 Diag(Tok, diag::err_objc_illegal_visibility_spec);
639 ConsumeToken();
640 continue;
641 }
642 }
643 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000644 for (unsigned i = 0; i < IvarDecls.size(); i++) {
645 AllIvarDecls.push_back(IvarDecls[i]);
646 AllVisibilities.push_back(visibility);
647 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000648 IvarDecls.clear();
649
Steve Naroffc4474992007-08-21 21:17:12 +0000650 if (Tok.getKind() == tok::semi) {
651 ConsumeToken();
652 } else if (Tok.getKind() == tok::r_brace) {
653 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
654 break;
655 } else {
656 Diag(Tok, diag::err_expected_semi_decl_list);
657 // Skip to end of block or statement
658 SkipUntil(tok::r_brace, true, true);
659 }
660 }
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000661 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
662 Actions.ObjcAddInstanceVariable(interfaceDecl,
663 &AllIvarDecls[0], AllIvarDecls.size(), &AllVisibilities[0]);
664 }
Steve Naroffc4474992007-08-21 21:17:12 +0000665 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
666 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000667}
Steve Narofffb367882007-08-20 21:31:48 +0000668
669/// objc-protocol-declaration:
670/// objc-protocol-definition
671/// objc-protocol-forward-reference
672///
673/// objc-protocol-definition:
674/// @protocol identifier
675/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000676/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000677/// @end
678///
679/// objc-protocol-forward-reference:
680/// @protocol identifier-list ';'
681///
682/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000683/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000684/// semicolon in the first alternative if objc-protocol-refs are omitted.
685
Steve Naroff72f17fb2007-08-22 22:17:26 +0000686Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000687 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000688 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
689 ConsumeToken(); // the "protocol" identifier
690
691 if (Tok.getKind() != tok::identifier) {
692 Diag(Tok, diag::err_expected_ident); // missing protocol name.
693 return 0;
694 }
695 // Save the protocol name, then consume it.
696 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
697 SourceLocation nameLoc = ConsumeToken();
698
699 if (Tok.getKind() == tok::semi) { // forward declaration.
700 ConsumeToken();
701 return 0; // FIXME: add protocolName
702 }
703 if (Tok.getKind() == tok::comma) { // list of forward declarations.
704 // Parse the list of forward declarations.
705 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
706 ProtocolRefs.push_back(protocolName);
707
708 while (1) {
709 ConsumeToken(); // the ','
710 if (Tok.getKind() != tok::identifier) {
711 Diag(Tok, diag::err_expected_ident);
712 SkipUntil(tok::semi);
713 return 0;
714 }
715 ProtocolRefs.push_back(Tok.getIdentifierInfo());
716 ConsumeToken(); // the identifier
717
718 if (Tok.getKind() != tok::comma)
719 break;
720 }
721 // Consume the ';'.
722 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
723 return 0;
724 return 0; // FIXME
725 }
726 // Last, and definitely not least, parse a protocol declaration.
727 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000728 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
729 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000730 return 0;
731 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000732 ParseObjCInterfaceDeclList(0/*FIXME*/);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000733
734 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000735 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000736 ConsumeToken(); // the "end" identifier
737 return 0;
738 }
739 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000740 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000741}
Steve Narofffb367882007-08-20 21:31:48 +0000742
743/// objc-implementation:
744/// objc-class-implementation-prologue
745/// objc-category-implementation-prologue
746///
747/// objc-class-implementation-prologue:
748/// @implementation identifier objc-superclass[opt]
749/// objc-class-instance-variables[opt]
750///
751/// objc-category-implementation-prologue:
752/// @implementation identifier ( identifier )
753
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000754Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
755 SourceLocation atLoc) {
756 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
757 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
758 ConsumeToken(); // the "implementation" identifier
759
760 if (Tok.getKind() != tok::identifier) {
761 Diag(Tok, diag::err_expected_ident); // missing class or category name.
762 return 0;
763 }
764 // We have a class or category name - consume it.
765 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
766
767 if (Tok.getKind() == tok::l_paren) {
768 // we have a category implementation.
769 SourceLocation lparenLoc = ConsumeParen();
770 SourceLocation categoryLoc, rparenLoc;
771 IdentifierInfo *categoryId = 0;
772
773 if (Tok.getKind() == tok::identifier) {
774 categoryId = Tok.getIdentifierInfo();
775 categoryLoc = ConsumeToken();
776 } else {
777 Diag(Tok, diag::err_expected_ident); // missing category name.
778 return 0;
779 }
780 if (Tok.getKind() != tok::r_paren) {
781 Diag(Tok, diag::err_expected_rparen);
782 SkipUntil(tok::r_paren, false); // don't stop at ';'
783 return 0;
784 }
785 rparenLoc = ConsumeParen();
786 return 0;
787 }
788 // We have a class implementation
789 if (Tok.getKind() == tok::colon) {
790 // We have a super class
791 ConsumeToken();
792 if (Tok.getKind() != tok::identifier) {
793 Diag(Tok, diag::err_expected_ident); // missing super class name.
794 return 0;
795 }
796 ConsumeToken(); // Consume super class name
797 }
798 if (Tok.getKind() == tok::l_brace)
Steve Naroff81f1bba2007-09-06 21:24:23 +0000799 ParseObjCClassInstanceVariables(0/*FIXME*/); // we have ivars
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000800
Steve Narofffb367882007-08-20 21:31:48 +0000801 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000802}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000803Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
804 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
805 "ParseObjCAtEndDeclaration(): Expected @end");
806 ConsumeToken(); // the "end" identifier
807
Steve Narofffb367882007-08-20 21:31:48 +0000808 return 0;
809}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000810
811/// compatibility-alias-decl:
812/// @compatibility_alias alias-name class-name ';'
813///
814Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
815 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
816 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
817 ConsumeToken(); // consume compatibility_alias
818 if (Tok.getKind() != tok::identifier) {
819 Diag(Tok, diag::err_expected_ident);
820 return 0;
821 }
822 ConsumeToken(); // consume alias-name
823 if (Tok.getKind() != tok::identifier) {
824 Diag(Tok, diag::err_expected_ident);
825 return 0;
826 }
827 ConsumeToken(); // consume class-name;
828 if (Tok.getKind() != tok::semi)
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000829 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Steve Narofffb367882007-08-20 21:31:48 +0000830 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000831}
832
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000833/// property-synthesis:
834/// @synthesize property-ivar-list ';'
835///
836/// property-ivar-list:
837/// property-ivar
838/// property-ivar-list ',' property-ivar
839///
840/// property-ivar:
841/// identifier
842/// identifier '=' identifier
843///
844Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
845 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
846 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
847 SourceLocation loc = ConsumeToken(); // consume dynamic
848 if (Tok.getKind() != tok::identifier) {
849 Diag(Tok, diag::err_expected_ident);
850 return 0;
851 }
852 while (Tok.getKind() == tok::identifier) {
853 ConsumeToken(); // consume property name
854 if (Tok.getKind() == tok::equal) {
855 // property '=' ivar-name
856 ConsumeToken(); // consume '='
857 if (Tok.getKind() != tok::identifier) {
858 Diag(Tok, diag::err_expected_ident);
859 break;
860 }
861 ConsumeToken(); // consume ivar-name
862 }
863 if (Tok.getKind() != tok::comma)
864 break;
865 ConsumeToken(); // consume ','
866 }
867 if (Tok.getKind() != tok::semi)
868 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
869 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000870}
871
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000872/// property-dynamic:
873/// @dynamic property-list
874///
875/// property-list:
876/// identifier
877/// property-list ',' identifier
878///
879Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
880 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
881 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
882 SourceLocation loc = ConsumeToken(); // consume dynamic
883 if (Tok.getKind() != tok::identifier) {
884 Diag(Tok, diag::err_expected_ident);
885 return 0;
886 }
887 while (Tok.getKind() == tok::identifier) {
888 ConsumeToken(); // consume property name
889 if (Tok.getKind() != tok::comma)
890 break;
891 ConsumeToken(); // consume ','
892 }
893 if (Tok.getKind() != tok::semi)
894 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
895 return 0;
896}
897
Steve Naroff81f1bba2007-09-06 21:24:23 +0000898/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000899///
900void Parser::ParseObjCInstanceMethodDefinition() {
901 assert(Tok.getKind() == tok::minus &&
902 "ParseObjCInstanceMethodDefinition(): Expected '-'");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000903 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000904 // parse optional ';'
905 if (Tok.getKind() == tok::semi)
906 ConsumeToken();
907
908 if (Tok.getKind() != tok::l_brace) {
909 Diag (Tok, diag::err_expected_lbrace);
910 return;
911 }
912
913 StmtResult FnBody = ParseCompoundStatementBody();
914}
915
Steve Naroff81f1bba2007-09-06 21:24:23 +0000916/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000917///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000918void Parser::ParseObjCClassMethodDefinition() {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000919 assert(Tok.getKind() == tok::plus &&
920 "ParseObjCClassMethodDefinition(): Expected '+'");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000921 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000922 // parse optional ';'
923 if (Tok.getKind() == tok::semi)
924 ConsumeToken();
925 if (Tok.getKind() != tok::l_brace) {
926 Diag (Tok, diag::err_expected_lbrace);
927 return;
928 }
929
930 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +0000931}
Anders Carlssona66cad42007-08-21 17:43:55 +0000932
933Parser::ExprResult Parser::ParseObjCExpression() {
934 SourceLocation AtLoc = ConsumeToken(); // the "@"
935
936 switch (Tok.getKind()) {
937 case tok::string_literal: // primary-expression: string-literal
938 case tok::wide_string_literal:
939 return ParseObjCStringLiteral();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000940 default:
941 break;
942 }
943
944 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlsson8be1d402007-08-22 15:14:15 +0000945 case tok::objc_encode:
946 return ParseObjCEncodeExpression();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000947 case tok::objc_protocol:
948 return ParseObjCProtocolExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +0000949 default:
950 Diag(AtLoc, diag::err_unexpected_at);
951 SkipUntil(tok::semi);
952 break;
953 }
954
955 return 0;
956}
957
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000958/// objc-message-expr:
959/// '[' objc-receiver objc-message-args ']'
960///
961/// objc-receiver:
962/// expression
963/// class-name
964/// type-name
965///
966/// objc-message-args:
967/// objc-selector
968/// objc-keywordarg-list
969///
970/// objc-keywordarg-list:
971/// objc-keywordarg
972/// objc-keywordarg-list objc-keywordarg
973///
974/// objc-keywordarg:
975/// selector-name[opt] ':' objc-keywordexpr
976///
977/// objc-keywordexpr:
978/// nonempty-expr-list
979///
980/// nonempty-expr-list:
981/// assignment-expression
982/// nonempty-expr-list , assignment-expression
983///
984Parser::ExprResult Parser::ParseObjCMessageExpression() {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +0000985 assert(Tok.getKind() == tok::l_square && "'[' expected");
986 SourceLocation Loc = ConsumeBracket(); // consume '['
987 // Parse receiver
988 // FIXME: receiver as type-name/class-name
989 ParseAssignmentExpression();
990 // Parse objc-selector
991 IdentifierInfo *selIdent = ParseObjCSelector();
992 if (Tok.getKind() == tok::colon) {
993 while (1) {
994 // Each iteration parses a single keyword argument.
995 if (Tok.getKind() != tok::colon) {
996 Diag(Tok, diag::err_expected_colon);
997 SkipUntil(tok::semi);
998 return 0;
999 }
1000 ConsumeToken(); // Eat the ':'.
1001 /// Parse the expression after ':'
1002 ParseAssignmentExpression();
1003 IdentifierInfo *keywordSelector = ParseObjCSelector();
1004
1005 if (!keywordSelector && Tok.getKind() != tok::colon)
1006 break;
1007 // We have a selector or a colon, continue parsing.
1008 }
1009 // Parse the, optional, argument list, comma separated.
1010 while (Tok.getKind() == tok::comma) {
1011 ConsumeToken();
1012 /// Parse the expression after ','
1013 ParseAssignmentExpression();
1014 }
1015 } else if (!selIdent) {
1016 Diag(Tok, diag::err_expected_ident); // missing selector name.
1017 SkipUntil(tok::semi);
1018 return 0;
1019 }
1020 if (Tok.getKind() != tok::r_square) {
1021 Diag(Tok, diag::err_expected_rsquare);
1022 SkipUntil(tok::semi);
1023 return 0;
1024 }
1025 ConsumeBracket(); // consume ']'
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001026 return 0;
1027}
1028
Anders Carlssona66cad42007-08-21 17:43:55 +00001029Parser::ExprResult Parser::ParseObjCStringLiteral() {
1030 ExprResult Res = ParseStringLiteralExpression();
1031
1032 if (Res.isInvalid) return Res;
1033
1034 return Actions.ParseObjCStringLiteral(Res.Val);
1035}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001036
1037/// objc-encode-expression:
1038/// @encode ( type-name )
1039Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff87c329f2007-08-23 18:16:40 +00001040 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001041
1042 SourceLocation EncLoc = ConsumeToken();
1043
1044 if (Tok.getKind() != tok::l_paren) {
1045 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1046 return true;
1047 }
1048
1049 SourceLocation LParenLoc = ConsumeParen();
1050
1051 TypeTy *Ty = ParseTypeName();
1052
Anders Carlsson92faeb82007-08-23 15:31:37 +00001053 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001054
1055 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001056 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001057}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001058
1059/// objc-protocol-expression
1060/// @protocol ( protocol-name )
1061
1062Parser::ExprResult Parser::ParseObjCProtocolExpression()
1063{
1064 SourceLocation ProtoLoc = ConsumeToken();
1065
1066 if (Tok.getKind() != tok::l_paren) {
1067 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1068 return true;
1069 }
1070
1071 SourceLocation LParenLoc = ConsumeParen();
1072
1073 if (Tok.getKind() != tok::identifier) {
1074 Diag(Tok, diag::err_expected_ident);
1075 return true;
1076 }
1077
1078 // FIXME: Do something with the protocol name
1079 ConsumeToken();
1080
Anders Carlsson92faeb82007-08-23 15:31:37 +00001081 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001082
1083 // FIXME
1084 return 0;
1085}