blob: 803fe2f6707cea710ded399067c7f216891c3a28 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff4985ace2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroffdac269b2007-08-20 21:31:48 +000029Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000030 SourceLocation AtLoc = ConsumeToken(); // the "@"
31
Steve Naroff861cf3e2007-08-23 18:16:40 +000032 switch (Tok.getObjCKeywordID()) {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 case tok::objc_class:
34 return ParseObjCAtClassDeclaration(AtLoc);
35 case tok::objc_interface:
Steve Naroffdac269b2007-08-20 21:31:48 +000036 return ParseObjCAtInterfaceDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000037 case tok::objc_protocol:
Steve Naroff7ef58fd2007-08-22 22:17:26 +000038 return ParseObjCAtProtocolDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000039 case tok::objc_implementation:
Steve Naroff3536b442007-09-06 21:24:23 +000040 return ObjcImpDecl = ParseObjCAtImplementationDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000041 case tok::objc_end:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +000042 return ParseObjCAtEndDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000043 case tok::objc_compatibility_alias:
Fariborz Jahaniane992af02007-09-04 19:26:51 +000044 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +000045 case tok::objc_synthesize:
46 return ParseObjCPropertySynthesize(AtLoc);
47 case tok::objc_dynamic:
48 return ParseObjCPropertyDynamic(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000049 default:
50 Diag(AtLoc, diag::err_unexpected_at);
51 SkipUntil(tok::semi);
Steve Naroffdac269b2007-08-20 21:31:48 +000052 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000053 }
54}
55
56///
57/// objc-class-declaration:
58/// '@' 'class' identifier-list ';'
59///
Steve Naroffdac269b2007-08-20 21:31:48 +000060Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroffdac269b2007-08-20 21:31:48 +000068 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000069 }
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroffdac269b2007-08-20 21:31:48 +000081 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000082
Steve Naroff3536b442007-09-06 21:24:23 +000083 return Actions.ObjcClassDeclaration(CurScope, atLoc,
84 &ClassNames[0], ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000085}
86
Steve Naroffdac269b2007-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 Naroff861cf3e2007-08-23 18:16:40 +0000117 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-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 Naroff7ef58fd2007-08-22 22:17:26 +0000126 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000127 SourceLocation nameLoc = ConsumeToken();
128
Steve Naroff527fe232007-08-23 19:56:30 +0000129 if (Tok.getKind() == tok::l_paren) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000130 SourceLocation lparenLoc = ConsumeParen();
131 SourceLocation categoryLoc, rparenLoc;
132 IdentifierInfo *categoryId = 0;
133
Steve Naroff527fe232007-08-23 19:56:30 +0000134 // For ObjC2, the category name is optional (not an error).
Steve Naroffdac269b2007-08-20 21:31:48 +0000135 if (Tok.getKind() == tok::identifier) {
136 categoryId = Tok.getIdentifierInfo();
137 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000138 } else if (!getLang().ObjC2) {
139 Diag(Tok, diag::err_expected_ident); // missing category name.
140 return 0;
Steve Naroffdac269b2007-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 Narofff28b2642007-09-05 23:30:30 +0000150 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
151 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroffdac269b2007-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 Naroff3536b442007-09-06 21:24:23 +0000157 ParseObjCInterfaceDeclList(0/*FIXME*/);
Steve Naroffdac269b2007-08-20 21:31:48 +0000158
Steve Naroff294494e2007-08-22 16:35:03 +0000159 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000160 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000161 ConsumeToken(); // the "end" identifier
Steve Naroffdac269b2007-08-20 21:31:48 +0000162 return 0;
163 }
Steve Naroff294494e2007-08-22 16:35:03 +0000164 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000165 return 0;
166 }
167 // Parse a class interface.
168 IdentifierInfo *superClassId = 0;
169 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000170
Steve Naroffdac269b2007-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 Narofff28b2642007-09-05 23:30:30 +0000181 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffdac269b2007-08-20 21:31:48 +0000182 if (Tok.getKind() == tok::less) {
Steve Narofff28b2642007-09-05 23:30:30 +0000183 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroffdac269b2007-08-20 21:31:48 +0000184 return 0;
185 }
Steve Narofff28b2642007-09-05 23:30:30 +0000186 DeclTy *ClsType = Actions.ObjcStartClassInterface(atLoc, nameId, nameLoc,
187 superClassId, superClassLoc, &ProtocolRefs[0],
188 ProtocolRefs.size(), attrList);
189
Steve Naroffdac269b2007-08-20 21:31:48 +0000190 if (Tok.getKind() == tok::l_brace)
Steve Naroff3536b442007-09-06 21:24:23 +0000191 ParseObjCClassInstanceVariables(ClsType);
Steve Naroffdac269b2007-08-20 21:31:48 +0000192
Steve Naroff3536b442007-09-06 21:24:23 +0000193 ParseObjCInterfaceDeclList(ClsType);
Steve Naroff294494e2007-08-22 16:35:03 +0000194
195 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000196 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000197 ConsumeToken(); // the "end" identifier
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000198 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000199 }
Steve Naroff294494e2007-08-22 16:35:03 +0000200 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000201 return 0;
202}
203
204/// objc-interface-decl-list:
205/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000206/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000207/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000208/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000209/// objc-interface-decl-list declaration
210/// objc-interface-decl-list ';'
211///
Steve Naroff294494e2007-08-22 16:35:03 +0000212/// objc-method-requirement: [OBJC2]
213/// @required
214/// @optional
215///
Steve Naroff3536b442007-09-06 21:24:23 +0000216void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl) {
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000217 llvm::SmallVector<DeclTy*, 32> allMethods;
Steve Naroff294494e2007-08-22 16:35:03 +0000218 while (1) {
219 if (Tok.getKind() == tok::at) {
220 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff861cf3e2007-08-23 18:16:40 +0000221 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff294494e2007-08-22 16:35:03 +0000222
223 if (ocKind == tok::objc_end) { // terminate list
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000224 break;
Steve Naroff294494e2007-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 Jahaniand0f97d12007-08-31 16:11:31 +0000232 ParseObjCPropertyDecl(0/*FIXME*/);
Steve Naroff294494e2007-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 Jahaniane3a2ca72007-09-10 20:33:04 +0000240 allMethods.push_back(ParseObjCMethodPrototype(interfaceDecl));
Steve Naroff3536b442007-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 Naroff294494e2007-08-22 16:35:03 +0000244 continue;
245 }
246 if (Tok.getKind() == tok::semi)
247 ConsumeToken();
248 else if (Tok.getKind() == tok::eof)
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000249 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000250 else {
Steve Naroff4985ace2007-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 Naroff3536b442007-09-06 21:24:23 +0000253 ParseDeclarationOrFunctionDefinition();
Steve Narofff28b2642007-09-05 23:30:30 +0000254 }
Steve Naroff294494e2007-08-22 16:35:03 +0000255 }
Fariborz Jahaniane3a2ca72007-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 Naroff294494e2007-08-22 16:35:03 +0000260}
261
Fariborz Jahaniand0f97d12007-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 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000352}
Steve Naroffdac269b2007-08-20 21:31:48 +0000353
Steve Naroff3536b442007-09-06 21:24:23 +0000354/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000355/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000356/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000357///
358/// objc-instance-method: '-'
359/// objc-class-method: '+'
360///
Steve Naroff4985ace2007-08-22 18:35:33 +0000361/// objc-method-attributes: [OBJC2]
362/// __attribute__((deprecated))
363///
Steve Naroff3536b442007-09-06 21:24:23 +0000364Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *CDecl) {
Steve Naroff294494e2007-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 Narofff28b2642007-09-05 23:30:30 +0000371 DeclTy *MDecl = ParseObjCMethodDecl(methodType, methodLoc);
Steve Naroff3536b442007-09-06 21:24:23 +0000372 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000373 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000374 return MDecl;
Steve Naroff294494e2007-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 Jahanian0ccb27d2007-09-05 19:52:07 +0000389 if (tKind == tok::identifier || tKind == tok::kw_typeof ||
390 tKind == tok::kw___alignof ||
Steve Naroff294494e2007-08-22 16:35:03 +0000391 (tKind >= tok::kw_auto && tKind <= tok::kw__Complex)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000392 II = Tok.getIdentifierInfo();
393 ConsumeToken();
394 }
395 return II;
396}
397
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000398/// objc-type-qualifier: one of
399/// in out inout bycopy byref oneway
400///
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000401bool Parser::isObjCTypeQualifier() {
402 if (Tok.getKind() == tok::identifier) {
Chris Lattner34870da2007-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 Naroff4fa7afd2007-08-22 23:18:22 +0000406 }
407 return false;
408}
409
Fariborz Jahaniand0f97d12007-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 Naroff294494e2007-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 Narofff28b2642007-09-05 23:30:30 +0000430Parser::TypeTy *Parser::ParseObjCTypeName() {
Steve Naroff294494e2007-08-22 16:35:03 +0000431 assert(Tok.getKind() == tok::l_paren && "expected (");
432
433 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Steve Narofff28b2642007-09-05 23:30:30 +0000434 TypeTy *Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000435
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000436 while (isObjCTypeQualifier())
437 ConsumeToken();
438
Steve Naroff294494e2007-08-22 16:35:03 +0000439 if (isTypeSpecifierQualifier()) {
Steve Narofff28b2642007-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 Naroff294494e2007-08-22 16:35:03 +0000443 }
444 if (Tok.getKind() != tok::r_paren) {
445 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Narofff28b2642007-09-05 23:30:30 +0000446 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff294494e2007-08-22 16:35:03 +0000447 }
448 RParenLoc = ConsumeParen();
Steve Narofff28b2642007-09-05 23:30:30 +0000449 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000450}
451
452/// objc-method-decl:
453/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000454/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000455/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000456/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000457///
458/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000459/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000460/// objc-keyword-selector objc-keyword-decl
461///
462/// objc-keyword-decl:
Steve Naroff7ef58fd2007-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 Naroff294494e2007-08-22 16:35:03 +0000467///
Steve Naroff4985ace2007-08-22 18:35:33 +0000468/// objc-parmlist:
469/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000470///
Steve Naroff4985ace2007-08-22 18:35:33 +0000471/// objc-parms:
472/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000473///
Steve Naroff4985ace2007-08-22 18:35:33 +0000474/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000475/// , ...
476///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000477/// objc-keyword-attributes: [OBJC2]
478/// __attribute__((unused))
479///
Steve Narofff28b2642007-09-05 23:30:30 +0000480Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType, SourceLocation mLoc) {
Steve Naroff4985ace2007-08-22 18:35:33 +0000481
Steve Narofff28b2642007-09-05 23:30:30 +0000482 TypeTy *ReturnType = 0;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000483 AttributeList *methodAttrs = 0;
Steve Narofff28b2642007-09-05 23:30:30 +0000484
Steve Naroff4985ace2007-08-22 18:35:33 +0000485 // Parse the return type.
Steve Naroff294494e2007-08-22 16:35:03 +0000486 if (Tok.getKind() == tok::l_paren)
Steve Narofff28b2642007-09-05 23:30:30 +0000487 ReturnType = ParseObjCTypeName();
Steve Naroff4985ace2007-08-22 18:35:33 +0000488 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Narofff28b2642007-09-05 23:30:30 +0000489
490 llvm::SmallVector<ObjcKeywordInfo, 12> KeyInfo;
Steve Naroff4985ace2007-08-22 18:35:33 +0000491
492 if (Tok.getKind() == tok::colon) {
Steve Narofff28b2642007-09-05 23:30:30 +0000493
Steve Naroff4985ace2007-08-22 18:35:33 +0000494 while (1) {
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000495 ObjcKeywordInfo KeyInfoDecl;
496 KeyInfoDecl.SelectorName = selIdent;
Steve Narofff28b2642007-09-05 23:30:30 +0000497
Steve Naroff4985ace2007-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 Jahaniane3a2ca72007-09-10 20:33:04 +0000503 KeyInfoDecl.ColonLoc = ConsumeToken(); // Eat the ':'.
Steve Naroff4985ace2007-08-22 18:35:33 +0000504 if (Tok.getKind() == tok::l_paren) // Parse the argument type.
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000505 KeyInfoDecl.TypeInfo = ParseObjCTypeName();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000506
507 // If attributes exist before the argument name, parse them.
Steve Naroff527fe232007-08-23 19:56:30 +0000508 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000509 KeyInfoDecl.AttrList = ParseAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000510
Steve Naroff4985ace2007-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 Jahaniane3a2ca72007-09-10 20:33:04 +0000515 KeyInfoDecl.ArgumentName = Tok.getIdentifierInfo();
Steve Naroff4985ace2007-08-22 18:35:33 +0000516 ConsumeToken(); // Eat the identifier.
Steve Narofff28b2642007-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 Naroff4985ace2007-08-22 18:35:33 +0000520 // FIXME: add Actions.BuildObjCKeyword()
521
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000522 KeyInfo.push_back(KeyInfoDecl);
Steve Narofff28b2642007-09-05 23:30:30 +0000523 selIdent = ParseObjCSelector();
524 if (!selIdent && Tok.getKind() != tok::colon)
Steve Naroff4985ace2007-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 Jahanian0ccb27d2007-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 Naroff4985ace2007-08-22 18:35:33 +0000541 }
Steve Narofff28b2642007-09-05 23:30:30 +0000542 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-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 Narofff28b2642007-09-05 23:30:30 +0000546 return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType,
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000547 &KeyInfo[0], KeyInfo.size(),
548 methodAttrs);
Steve Naroff4985ace2007-08-22 18:35:33 +0000549 } else if (!selIdent) {
550 Diag(Tok, diag::err_expected_ident); // missing selector name.
551 }
Fariborz Jahaniane3a2ca72007-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 Naroff294494e2007-08-22 16:35:03 +0000558}
559
Steve Naroffdac269b2007-08-20 21:31:48 +0000560/// objc-protocol-refs:
561/// '<' identifier-list '>'
562///
Steve Narofff28b2642007-09-05 23:30:30 +0000563bool Parser::ParseObjCProtocolReferences(
564 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000565 assert(Tok.getKind() == tok::less && "expected <");
566
567 ConsumeToken(); // the "<"
Steve Naroffdac269b2007-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 Naroffddbff782007-08-21 21:17:12 +0000601/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000602///
603/// objc-instance-variable-decl:
604/// struct-declaration
605///
Steve Naroff3536b442007-09-06 21:24:23 +0000606void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Steve Naroffddbff782007-08-21 21:17:12 +0000607 assert(Tok.getKind() == tok::l_brace && "expected {");
Steve Naroff3536b442007-09-06 21:24:23 +0000608 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Steve Naroffddbff782007-08-21 21:17:12 +0000609
610 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000611
612 // While we still have something to read, read the instance variables.
613 while (Tok.getKind() != tok::r_brace &&
614 Tok.getKind() != tok::eof) {
615 // Each iteration of this loop reads one objc-instance-variable-decl.
616
617 // Check for extraneous top-level semicolon.
618 if (Tok.getKind() == tok::semi) {
619 Diag(Tok, diag::ext_extra_struct_semi);
620 ConsumeToken();
621 continue;
622 }
623 // Set the default visibility to private.
624 tok::ObjCKeywordKind visibility = tok::objc_private;
625 if (Tok.getKind() == tok::at) { // parse objc-visibility-spec
626 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000627 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000628 case tok::objc_private:
629 case tok::objc_public:
630 case tok::objc_protected:
631 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000632 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000633 ConsumeToken();
634 continue;
635 default:
636 Diag(Tok, diag::err_objc_illegal_visibility_spec);
637 ConsumeToken();
638 continue;
639 }
640 }
641 ParseStructDeclaration(interfaceDecl, IvarDecls);
Steve Naroff3536b442007-09-06 21:24:23 +0000642 for (unsigned i = 0; i < IvarDecls.size(); i++)
643 Actions.ObjcAddInstanceVariable(interfaceDecl, IvarDecls[i], visibility);
644 IvarDecls.clear();
645
Steve Naroffddbff782007-08-21 21:17:12 +0000646 if (Tok.getKind() == tok::semi) {
647 ConsumeToken();
648 } else if (Tok.getKind() == tok::r_brace) {
649 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
650 break;
651 } else {
652 Diag(Tok, diag::err_expected_semi_decl_list);
653 // Skip to end of block or statement
654 SkipUntil(tok::r_brace, true, true);
655 }
656 }
657 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
658 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000659}
Steve Naroffdac269b2007-08-20 21:31:48 +0000660
661/// objc-protocol-declaration:
662/// objc-protocol-definition
663/// objc-protocol-forward-reference
664///
665/// objc-protocol-definition:
666/// @protocol identifier
667/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000668/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000669/// @end
670///
671/// objc-protocol-forward-reference:
672/// @protocol identifier-list ';'
673///
674/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000675/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000676/// semicolon in the first alternative if objc-protocol-refs are omitted.
677
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000678Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000679 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000680 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
681 ConsumeToken(); // the "protocol" identifier
682
683 if (Tok.getKind() != tok::identifier) {
684 Diag(Tok, diag::err_expected_ident); // missing protocol name.
685 return 0;
686 }
687 // Save the protocol name, then consume it.
688 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
689 SourceLocation nameLoc = ConsumeToken();
690
691 if (Tok.getKind() == tok::semi) { // forward declaration.
692 ConsumeToken();
693 return 0; // FIXME: add protocolName
694 }
695 if (Tok.getKind() == tok::comma) { // list of forward declarations.
696 // Parse the list of forward declarations.
697 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
698 ProtocolRefs.push_back(protocolName);
699
700 while (1) {
701 ConsumeToken(); // the ','
702 if (Tok.getKind() != tok::identifier) {
703 Diag(Tok, diag::err_expected_ident);
704 SkipUntil(tok::semi);
705 return 0;
706 }
707 ProtocolRefs.push_back(Tok.getIdentifierInfo());
708 ConsumeToken(); // the identifier
709
710 if (Tok.getKind() != tok::comma)
711 break;
712 }
713 // Consume the ';'.
714 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
715 return 0;
716 return 0; // FIXME
717 }
718 // Last, and definitely not least, parse a protocol declaration.
719 if (Tok.getKind() == tok::less) {
Steve Narofff28b2642007-09-05 23:30:30 +0000720 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
721 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000722 return 0;
723 }
Steve Naroff3536b442007-09-06 21:24:23 +0000724 ParseObjCInterfaceDeclList(0/*FIXME*/);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000725
726 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000727 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000728 ConsumeToken(); // the "end" identifier
729 return 0;
730 }
731 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000732 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000733}
Steve Naroffdac269b2007-08-20 21:31:48 +0000734
735/// objc-implementation:
736/// objc-class-implementation-prologue
737/// objc-category-implementation-prologue
738///
739/// objc-class-implementation-prologue:
740/// @implementation identifier objc-superclass[opt]
741/// objc-class-instance-variables[opt]
742///
743/// objc-category-implementation-prologue:
744/// @implementation identifier ( identifier )
745
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000746Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
747 SourceLocation atLoc) {
748 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
749 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
750 ConsumeToken(); // the "implementation" identifier
751
752 if (Tok.getKind() != tok::identifier) {
753 Diag(Tok, diag::err_expected_ident); // missing class or category name.
754 return 0;
755 }
756 // We have a class or category name - consume it.
757 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
758
759 if (Tok.getKind() == tok::l_paren) {
760 // we have a category implementation.
761 SourceLocation lparenLoc = ConsumeParen();
762 SourceLocation categoryLoc, rparenLoc;
763 IdentifierInfo *categoryId = 0;
764
765 if (Tok.getKind() == tok::identifier) {
766 categoryId = Tok.getIdentifierInfo();
767 categoryLoc = ConsumeToken();
768 } else {
769 Diag(Tok, diag::err_expected_ident); // missing category name.
770 return 0;
771 }
772 if (Tok.getKind() != tok::r_paren) {
773 Diag(Tok, diag::err_expected_rparen);
774 SkipUntil(tok::r_paren, false); // don't stop at ';'
775 return 0;
776 }
777 rparenLoc = ConsumeParen();
778 return 0;
779 }
780 // We have a class implementation
781 if (Tok.getKind() == tok::colon) {
782 // We have a super class
783 ConsumeToken();
784 if (Tok.getKind() != tok::identifier) {
785 Diag(Tok, diag::err_expected_ident); // missing super class name.
786 return 0;
787 }
788 ConsumeToken(); // Consume super class name
789 }
790 if (Tok.getKind() == tok::l_brace)
Steve Naroff3536b442007-09-06 21:24:23 +0000791 ParseObjCClassInstanceVariables(0/*FIXME*/); // we have ivars
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000792
Steve Naroffdac269b2007-08-20 21:31:48 +0000793 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000794}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000795Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
796 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
797 "ParseObjCAtEndDeclaration(): Expected @end");
798 ConsumeToken(); // the "end" identifier
799
Steve Naroffdac269b2007-08-20 21:31:48 +0000800 return 0;
801}
Fariborz Jahaniane992af02007-09-04 19:26:51 +0000802
803/// compatibility-alias-decl:
804/// @compatibility_alias alias-name class-name ';'
805///
806Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
807 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
808 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
809 ConsumeToken(); // consume compatibility_alias
810 if (Tok.getKind() != tok::identifier) {
811 Diag(Tok, diag::err_expected_ident);
812 return 0;
813 }
814 ConsumeToken(); // consume alias-name
815 if (Tok.getKind() != tok::identifier) {
816 Diag(Tok, diag::err_expected_ident);
817 return 0;
818 }
819 ConsumeToken(); // consume class-name;
820 if (Tok.getKind() != tok::semi)
Fariborz Jahanian8cd8c662007-09-04 21:42:12 +0000821 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Steve Naroffdac269b2007-08-20 21:31:48 +0000822 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000823}
824
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000825/// property-synthesis:
826/// @synthesize property-ivar-list ';'
827///
828/// property-ivar-list:
829/// property-ivar
830/// property-ivar-list ',' property-ivar
831///
832/// property-ivar:
833/// identifier
834/// identifier '=' identifier
835///
836Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
837 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
838 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
839 SourceLocation loc = ConsumeToken(); // consume dynamic
840 if (Tok.getKind() != tok::identifier) {
841 Diag(Tok, diag::err_expected_ident);
842 return 0;
843 }
844 while (Tok.getKind() == tok::identifier) {
845 ConsumeToken(); // consume property name
846 if (Tok.getKind() == tok::equal) {
847 // property '=' ivar-name
848 ConsumeToken(); // consume '='
849 if (Tok.getKind() != tok::identifier) {
850 Diag(Tok, diag::err_expected_ident);
851 break;
852 }
853 ConsumeToken(); // consume ivar-name
854 }
855 if (Tok.getKind() != tok::comma)
856 break;
857 ConsumeToken(); // consume ','
858 }
859 if (Tok.getKind() != tok::semi)
860 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
861 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000862}
863
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000864/// property-dynamic:
865/// @dynamic property-list
866///
867/// property-list:
868/// identifier
869/// property-list ',' identifier
870///
871Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
872 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
873 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
874 SourceLocation loc = ConsumeToken(); // consume dynamic
875 if (Tok.getKind() != tok::identifier) {
876 Diag(Tok, diag::err_expected_ident);
877 return 0;
878 }
879 while (Tok.getKind() == tok::identifier) {
880 ConsumeToken(); // consume property name
881 if (Tok.getKind() != tok::comma)
882 break;
883 ConsumeToken(); // consume ','
884 }
885 if (Tok.getKind() != tok::semi)
886 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
887 return 0;
888}
889
Steve Naroff3536b442007-09-06 21:24:23 +0000890/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000891///
892void Parser::ParseObjCInstanceMethodDefinition() {
893 assert(Tok.getKind() == tok::minus &&
894 "ParseObjCInstanceMethodDefinition(): Expected '-'");
Steve Naroff3536b442007-09-06 21:24:23 +0000895 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000896 // parse optional ';'
897 if (Tok.getKind() == tok::semi)
898 ConsumeToken();
899
900 if (Tok.getKind() != tok::l_brace) {
901 Diag (Tok, diag::err_expected_lbrace);
902 return;
903 }
904
905 StmtResult FnBody = ParseCompoundStatementBody();
906}
907
Steve Naroff3536b442007-09-06 21:24:23 +0000908/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000909///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000910void Parser::ParseObjCClassMethodDefinition() {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000911 assert(Tok.getKind() == tok::plus &&
912 "ParseObjCClassMethodDefinition(): Expected '+'");
Steve Naroff3536b442007-09-06 21:24:23 +0000913 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000914 // parse optional ';'
915 if (Tok.getKind() == tok::semi)
916 ConsumeToken();
917 if (Tok.getKind() != tok::l_brace) {
918 Diag (Tok, diag::err_expected_lbrace);
919 return;
920 }
921
922 StmtResult FnBody = ParseCompoundStatementBody();
Reid Spencer5f016e22007-07-11 17:01:13 +0000923}
Anders Carlsson55085182007-08-21 17:43:55 +0000924
925Parser::ExprResult Parser::ParseObjCExpression() {
926 SourceLocation AtLoc = ConsumeToken(); // the "@"
927
928 switch (Tok.getKind()) {
929 case tok::string_literal: // primary-expression: string-literal
930 case tok::wide_string_literal:
931 return ParseObjCStringLiteral();
Anders Carlsson29b2cb12007-08-23 15:25:28 +0000932 default:
933 break;
934 }
935
936 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000937 case tok::objc_encode:
938 return ParseObjCEncodeExpression();
Anders Carlsson29b2cb12007-08-23 15:25:28 +0000939 case tok::objc_protocol:
940 return ParseObjCProtocolExpression();
Anders Carlsson55085182007-08-21 17:43:55 +0000941 default:
942 Diag(AtLoc, diag::err_unexpected_at);
943 SkipUntil(tok::semi);
944 break;
945 }
946
947 return 0;
948}
949
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000950/// objc-message-expr:
951/// '[' objc-receiver objc-message-args ']'
952///
953/// objc-receiver:
954/// expression
955/// class-name
956/// type-name
957///
958/// objc-message-args:
959/// objc-selector
960/// objc-keywordarg-list
961///
962/// objc-keywordarg-list:
963/// objc-keywordarg
964/// objc-keywordarg-list objc-keywordarg
965///
966/// objc-keywordarg:
967/// selector-name[opt] ':' objc-keywordexpr
968///
969/// objc-keywordexpr:
970/// nonempty-expr-list
971///
972/// nonempty-expr-list:
973/// assignment-expression
974/// nonempty-expr-list , assignment-expression
975///
976Parser::ExprResult Parser::ParseObjCMessageExpression() {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +0000977 assert(Tok.getKind() == tok::l_square && "'[' expected");
978 SourceLocation Loc = ConsumeBracket(); // consume '['
979 // Parse receiver
980 // FIXME: receiver as type-name/class-name
981 ParseAssignmentExpression();
982 // Parse objc-selector
983 IdentifierInfo *selIdent = ParseObjCSelector();
984 if (Tok.getKind() == tok::colon) {
985 while (1) {
986 // Each iteration parses a single keyword argument.
987 if (Tok.getKind() != tok::colon) {
988 Diag(Tok, diag::err_expected_colon);
989 SkipUntil(tok::semi);
990 return 0;
991 }
992 ConsumeToken(); // Eat the ':'.
993 /// Parse the expression after ':'
994 ParseAssignmentExpression();
995 IdentifierInfo *keywordSelector = ParseObjCSelector();
996
997 if (!keywordSelector && Tok.getKind() != tok::colon)
998 break;
999 // We have a selector or a colon, continue parsing.
1000 }
1001 // Parse the, optional, argument list, comma separated.
1002 while (Tok.getKind() == tok::comma) {
1003 ConsumeToken();
1004 /// Parse the expression after ','
1005 ParseAssignmentExpression();
1006 }
1007 } else if (!selIdent) {
1008 Diag(Tok, diag::err_expected_ident); // missing selector name.
1009 SkipUntil(tok::semi);
1010 return 0;
1011 }
1012 if (Tok.getKind() != tok::r_square) {
1013 Diag(Tok, diag::err_expected_rsquare);
1014 SkipUntil(tok::semi);
1015 return 0;
1016 }
1017 ConsumeBracket(); // consume ']'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001018 return 0;
1019}
1020
Anders Carlsson55085182007-08-21 17:43:55 +00001021Parser::ExprResult Parser::ParseObjCStringLiteral() {
1022 ExprResult Res = ParseStringLiteralExpression();
1023
1024 if (Res.isInvalid) return Res;
1025
1026 return Actions.ParseObjCStringLiteral(Res.Val);
1027}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001028
1029/// objc-encode-expression:
1030/// @encode ( type-name )
1031Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001032 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001033
1034 SourceLocation EncLoc = ConsumeToken();
1035
1036 if (Tok.getKind() != tok::l_paren) {
1037 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1038 return true;
1039 }
1040
1041 SourceLocation LParenLoc = ConsumeParen();
1042
1043 TypeTy *Ty = ParseTypeName();
1044
Anders Carlsson4988ae32007-08-23 15:31:37 +00001045 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001046
1047 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson4988ae32007-08-23 15:31:37 +00001048 RParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001049}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001050
1051/// objc-protocol-expression
1052/// @protocol ( protocol-name )
1053
1054Parser::ExprResult Parser::ParseObjCProtocolExpression()
1055{
1056 SourceLocation ProtoLoc = ConsumeToken();
1057
1058 if (Tok.getKind() != tok::l_paren) {
1059 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1060 return true;
1061 }
1062
1063 SourceLocation LParenLoc = ConsumeParen();
1064
1065 if (Tok.getKind() != tok::identifier) {
1066 Diag(Tok, diag::err_expected_ident);
1067 return true;
1068 }
1069
1070 // FIXME: Do something with the protocol name
1071 ConsumeToken();
1072
Anders Carlsson4988ae32007-08-23 15:31:37 +00001073 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001074
1075 // FIXME
1076 return 0;
1077}