blob: e396f375e7c42dfb54a97d33357ce4062bfc2aae [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 Jahaniane55cd002007-09-12 18:23:47 +0000232 ParseObjCPropertyDecl(interfaceDecl);
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.
Steve Naroffd16245b2007-09-17 15:07:43 +0000243 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 /// Insert collected methods declarations into the @interface object.
Steve Naroffd16245b2007-09-17 15:07:43 +0000257 Actions.ObjcAddMethodsToClass(interfaceDecl,&allMethods[0],allMethods.size());
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000258 return;
Steve Naroff294494e2007-08-22 16:35:03 +0000259}
260
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000261/// Parse property attribute declarations.
262///
263/// property-attr-decl: '(' property-attrlist ')'
264/// property-attrlist:
265/// property-attribute
266/// property-attrlist ',' property-attribute
267/// property-attribute:
268/// getter '=' identifier
269/// setter '=' identifier ':'
270/// readonly
271/// readwrite
272/// assign
273/// retain
274/// copy
275/// nonatomic
276///
277void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
278 SourceLocation loc = ConsumeParen(); // consume '('
279 while (isObjCPropertyAttribute()) {
280 const IdentifierInfo *II = Tok.getIdentifierInfo();
281 // getter/setter require extra treatment.
282 if (II == ObjcPropertyAttrs[objc_getter] ||
283 II == ObjcPropertyAttrs[objc_setter]) {
284 // skip getter/setter part.
285 SourceLocation loc = ConsumeToken();
286 if (Tok.getKind() == tok::equal) {
287 loc = ConsumeToken();
288 if (Tok.getKind() == tok::identifier) {
289 if (II == ObjcPropertyAttrs[objc_setter]) {
290 loc = ConsumeToken(); // consume method name
291 if (Tok.getKind() != tok::colon) {
292 Diag(loc, diag::err_expected_colon);
293 SkipUntil(tok::r_paren,true,true);
294 break;
295 }
296 }
297 }
298 else {
299 Diag(loc, diag::err_expected_ident);
300 SkipUntil(tok::r_paren,true,true);
301 break;
302 }
303 }
304 else {
305 Diag(loc, diag::err_objc_expected_equal);
306 SkipUntil(tok::r_paren,true,true);
307 break;
308 }
309 }
310 ConsumeToken(); // consume last attribute token
311 if (Tok.getKind() == tok::comma) {
312 loc = ConsumeToken();
313 continue;
314 }
315 if (Tok.getKind() == tok::r_paren)
316 break;
317 Diag(loc, diag::err_expected_rparen);
318 SkipUntil(tok::semi);
319 return;
320 }
321 if (Tok.getKind() == tok::r_paren)
322 ConsumeParen();
323 else {
324 Diag(loc, diag::err_objc_expected_property_attr);
325 SkipUntil(tok::r_paren); // recover from error inside attribute list
326 }
327}
328
329/// Main routine to parse property declaration.
330///
331/// @property property-attr-decl[opt] property-component-decl ';'
332///
333void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
334 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
335 "ParseObjCPropertyDecl(): Expected @property");
336 ConsumeToken(); // the "property" identifier
337 // Parse property attribute list, if any.
338 if (Tok.getKind() == tok::l_paren) {
339 // property has attribute list.
340 ParseObjCPropertyAttribute(0/*FIXME*/);
341 }
342 // Parse declaration portion of @property.
343 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
344 ParseStructDeclaration(interfaceDecl, PropertyDecls);
345 if (Tok.getKind() == tok::semi)
346 ConsumeToken();
347 else {
348 Diag(Tok, diag::err_expected_semi_decl_list);
349 SkipUntil(tok::r_brace, true, true);
350 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000351}
Steve Naroffdac269b2007-08-20 21:31:48 +0000352
Steve Naroff3536b442007-09-06 21:24:23 +0000353/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000354/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000355/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000356///
357/// objc-instance-method: '-'
358/// objc-class-method: '+'
359///
Steve Naroff4985ace2007-08-22 18:35:33 +0000360/// objc-method-attributes: [OBJC2]
361/// __attribute__((deprecated))
362///
Steve Naroff3536b442007-09-06 21:24:23 +0000363Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *CDecl) {
Steve Naroff294494e2007-08-22 16:35:03 +0000364 assert((Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) &&
365 "expected +/-");
366
367 tok::TokenKind methodType = Tok.getKind();
368 SourceLocation methodLoc = ConsumeToken();
369
Steve Narofff28b2642007-09-05 23:30:30 +0000370 DeclTy *MDecl = ParseObjCMethodDecl(methodType, methodLoc);
Steve Naroff3536b442007-09-06 21:24:23 +0000371 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000372 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000373 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000374}
375
376/// objc-selector:
377/// identifier
378/// one of
379/// enum struct union if else while do for switch case default
380/// break continue return goto asm sizeof typeof __alignof
381/// unsigned long const short volatile signed restrict _Complex
382/// in out inout bycopy byref oneway int char float double void _Bool
383///
384IdentifierInfo *Parser::ParseObjCSelector() {
385 tok::TokenKind tKind = Tok.getKind();
386 IdentifierInfo *II = 0;
387
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000388 if (tKind == tok::identifier || tKind == tok::kw_typeof ||
389 tKind == tok::kw___alignof ||
Steve Naroff294494e2007-08-22 16:35:03 +0000390 (tKind >= tok::kw_auto && tKind <= tok::kw__Complex)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000391 II = Tok.getIdentifierInfo();
392 ConsumeToken();
393 }
394 return II;
395}
396
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000397/// objc-type-qualifier: one of
398/// in out inout bycopy byref oneway
399///
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000400bool Parser::isObjCTypeQualifier() {
401 if (Tok.getKind() == tok::identifier) {
Chris Lattner34870da2007-08-29 22:54:08 +0000402 const IdentifierInfo *II = Tok.getIdentifierInfo();
403 for (unsigned i = 0; i < objc_NumQuals; ++i)
404 if (II == ObjcTypeQuals[i]) return true;
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000405 }
406 return false;
407}
408
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000409/// property-attrlist: one of
410/// readonly getter setter assign retain copy nonatomic
411///
412bool Parser::isObjCPropertyAttribute() {
413 if (Tok.getKind() == tok::identifier) {
414 const IdentifierInfo *II = Tok.getIdentifierInfo();
415 for (unsigned i = 0; i < objc_NumAttrs; ++i)
416 if (II == ObjcPropertyAttrs[i]) return true;
417 }
418 return false;
419}
420
Steve Naroff294494e2007-08-22 16:35:03 +0000421/// objc-type-name:
422/// '(' objc-type-qualifiers[opt] type-name ')'
423/// '(' objc-type-qualifiers[opt] ')'
424///
425/// objc-type-qualifiers:
426/// objc-type-qualifier
427/// objc-type-qualifiers objc-type-qualifier
428///
Steve Narofff28b2642007-09-05 23:30:30 +0000429Parser::TypeTy *Parser::ParseObjCTypeName() {
Steve Naroff294494e2007-08-22 16:35:03 +0000430 assert(Tok.getKind() == tok::l_paren && "expected (");
431
432 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Steve Narofff28b2642007-09-05 23:30:30 +0000433 TypeTy *Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000434
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000435 while (isObjCTypeQualifier())
436 ConsumeToken();
437
Steve Naroff294494e2007-08-22 16:35:03 +0000438 if (isTypeSpecifierQualifier()) {
Steve Narofff28b2642007-09-05 23:30:30 +0000439 Ty = ParseTypeName();
440 // FIXME: back when Sema support is in place...
441 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff294494e2007-08-22 16:35:03 +0000442 }
443 if (Tok.getKind() != tok::r_paren) {
444 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Narofff28b2642007-09-05 23:30:30 +0000445 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff294494e2007-08-22 16:35:03 +0000446 }
447 RParenLoc = ConsumeParen();
Steve Narofff28b2642007-09-05 23:30:30 +0000448 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000449}
450
451/// objc-method-decl:
452/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000453/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000454/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000455/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000456///
457/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000458/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000459/// objc-keyword-selector objc-keyword-decl
460///
461/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000462/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
463/// objc-selector ':' objc-keyword-attributes[opt] identifier
464/// ':' objc-type-name objc-keyword-attributes[opt] identifier
465/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000466///
Steve Naroff4985ace2007-08-22 18:35:33 +0000467/// objc-parmlist:
468/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000469///
Steve Naroff4985ace2007-08-22 18:35:33 +0000470/// objc-parms:
471/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000472///
Steve Naroff4985ace2007-08-22 18:35:33 +0000473/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000474/// , ...
475///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000476/// objc-keyword-attributes: [OBJC2]
477/// __attribute__((unused))
478///
Steve Naroffd16245b2007-09-17 15:07:43 +0000479Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType,
480 SourceLocation mLoc) {
Steve Narofff28b2642007-09-05 23:30:30 +0000481 TypeTy *ReturnType = 0;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000482 AttributeList *methodAttrs = 0;
Steve Narofff28b2642007-09-05 23:30:30 +0000483
Steve Naroff4985ace2007-08-22 18:35:33 +0000484 // Parse the return type.
Steve Naroff294494e2007-08-22 16:35:03 +0000485 if (Tok.getKind() == tok::l_paren)
Steve Narofff28b2642007-09-05 23:30:30 +0000486 ReturnType = ParseObjCTypeName();
Steve Naroff4985ace2007-08-22 18:35:33 +0000487 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Narofff28b2642007-09-05 23:30:30 +0000488
489 llvm::SmallVector<ObjcKeywordInfo, 12> KeyInfo;
Steve Naroff4985ace2007-08-22 18:35:33 +0000490
491 if (Tok.getKind() == tok::colon) {
Steve Narofff28b2642007-09-05 23:30:30 +0000492
Steve Naroff4985ace2007-08-22 18:35:33 +0000493 while (1) {
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000494 ObjcKeywordInfo KeyInfoDecl;
495 KeyInfoDecl.SelectorName = selIdent;
Steve Narofff28b2642007-09-05 23:30:30 +0000496
Steve Naroff4985ace2007-08-22 18:35:33 +0000497 // Each iteration parses a single keyword argument.
498 if (Tok.getKind() != tok::colon) {
499 Diag(Tok, diag::err_expected_colon);
500 break;
501 }
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000502 KeyInfoDecl.ColonLoc = ConsumeToken(); // Eat the ':'.
Steve Naroff4985ace2007-08-22 18:35:33 +0000503 if (Tok.getKind() == tok::l_paren) // Parse the argument type.
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000504 KeyInfoDecl.TypeInfo = ParseObjCTypeName();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000505
506 // If attributes exist before the argument name, parse them.
Steve Naroff527fe232007-08-23 19:56:30 +0000507 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000508 KeyInfoDecl.AttrList = ParseAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000509
Steve Naroff4985ace2007-08-22 18:35:33 +0000510 if (Tok.getKind() != tok::identifier) {
511 Diag(Tok, diag::err_expected_ident); // missing argument name.
512 break;
513 }
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000514 KeyInfoDecl.ArgumentName = Tok.getIdentifierInfo();
Steve Naroff4985ace2007-08-22 18:35:33 +0000515 ConsumeToken(); // Eat the identifier.
Steve Narofff28b2642007-09-05 23:30:30 +0000516
517 // Rather than call out to the actions, try packaging up the info
518 // locally, like we do for Declarator.
Steve Naroff4985ace2007-08-22 18:35:33 +0000519 // FIXME: add Actions.BuildObjCKeyword()
520
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000521 KeyInfo.push_back(KeyInfoDecl);
Steve Narofff28b2642007-09-05 23:30:30 +0000522 selIdent = ParseObjCSelector();
523 if (!selIdent && Tok.getKind() != tok::colon)
Steve Naroff4985ace2007-08-22 18:35:33 +0000524 break;
525 // We have a selector or a colon, continue parsing.
526 }
527 // Parse the (optional) parameter list.
528 while (Tok.getKind() == tok::comma) {
529 ConsumeToken();
530 if (Tok.getKind() == tok::ellipsis) {
531 ConsumeToken();
532 break;
533 }
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000534 // Parse the c-style argument declaration-specifier.
535 DeclSpec DS;
536 ParseDeclarationSpecifiers(DS);
537 // Parse the declarator.
538 Declarator ParmDecl(DS, Declarator::PrototypeContext);
539 ParseDeclarator(ParmDecl);
Steve Naroff4985ace2007-08-22 18:35:33 +0000540 }
Steve Narofff28b2642007-09-05 23:30:30 +0000541 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000542 // If attributes exist after the method, parse them.
543 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
544 methodAttrs = ParseAttributes();
Steve Narofff28b2642007-09-05 23:30:30 +0000545 return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType,
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000546 &KeyInfo[0], KeyInfo.size(),
547 methodAttrs);
Steve Naroff4985ace2007-08-22 18:35:33 +0000548 } else if (!selIdent) {
549 Diag(Tok, diag::err_expected_ident); // missing selector name.
550 }
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000551 // If attributes exist after the method, parse them.
552 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
553 methodAttrs = ParseAttributes();
554
555 return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType, selIdent,
556 methodAttrs);
Steve Naroff294494e2007-08-22 16:35:03 +0000557}
558
Steve Naroffdac269b2007-08-20 21:31:48 +0000559/// objc-protocol-refs:
560/// '<' identifier-list '>'
561///
Steve Narofff28b2642007-09-05 23:30:30 +0000562bool Parser::ParseObjCProtocolReferences(
563 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000564 assert(Tok.getKind() == tok::less && "expected <");
565
566 ConsumeToken(); // the "<"
Steve Naroffdac269b2007-08-20 21:31:48 +0000567
568 while (1) {
569 if (Tok.getKind() != tok::identifier) {
570 Diag(Tok, diag::err_expected_ident);
571 SkipUntil(tok::greater);
572 return true;
573 }
574 ProtocolRefs.push_back(Tok.getIdentifierInfo());
575 ConsumeToken();
576
577 if (Tok.getKind() != tok::comma)
578 break;
579 ConsumeToken();
580 }
581 // Consume the '>'.
582 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
583}
584
585/// objc-class-instance-variables:
586/// '{' objc-instance-variable-decl-list[opt] '}'
587///
588/// objc-instance-variable-decl-list:
589/// objc-visibility-spec
590/// objc-instance-variable-decl ';'
591/// ';'
592/// objc-instance-variable-decl-list objc-visibility-spec
593/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
594/// objc-instance-variable-decl-list ';'
595///
596/// objc-visibility-spec:
597/// @private
598/// @protected
599/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000600/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000601///
602/// objc-instance-variable-decl:
603/// struct-declaration
604///
Steve Naroff3536b442007-09-06 21:24:23 +0000605void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Steve Naroffddbff782007-08-21 21:17:12 +0000606 assert(Tok.getKind() == tok::l_brace && "expected {");
Steve Naroff3536b442007-09-06 21:24:23 +0000607 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000608 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
609 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffddbff782007-08-21 21:17:12 +0000610
611 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000612
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000613 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffddbff782007-08-21 21:17:12 +0000614 // While we still have something to read, read the instance variables.
615 while (Tok.getKind() != tok::r_brace &&
616 Tok.getKind() != tok::eof) {
617 // Each iteration of this loop reads one objc-instance-variable-decl.
618
619 // Check for extraneous top-level semicolon.
620 if (Tok.getKind() == tok::semi) {
621 Diag(Tok, diag::ext_extra_struct_semi);
622 ConsumeToken();
623 continue;
624 }
625 // Set the default visibility to private.
Steve Naroffddbff782007-08-21 21:17:12 +0000626 if (Tok.getKind() == tok::at) { // parse objc-visibility-spec
627 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000628 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000629 case tok::objc_private:
630 case tok::objc_public:
631 case tok::objc_protected:
632 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000633 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000634 ConsumeToken();
635 continue;
636 default:
637 Diag(Tok, diag::err_objc_illegal_visibility_spec);
638 ConsumeToken();
639 continue;
640 }
641 }
642 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000643 for (unsigned i = 0; i < IvarDecls.size(); i++) {
644 AllIvarDecls.push_back(IvarDecls[i]);
645 AllVisibilities.push_back(visibility);
646 }
Steve Naroff3536b442007-09-06 21:24:23 +0000647 IvarDecls.clear();
648
Steve Naroffddbff782007-08-21 21:17:12 +0000649 if (Tok.getKind() == tok::semi) {
650 ConsumeToken();
651 } else if (Tok.getKind() == tok::r_brace) {
652 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
653 break;
654 } else {
655 Diag(Tok, diag::err_expected_semi_decl_list);
656 // Skip to end of block or statement
657 SkipUntil(tok::r_brace, true, true);
658 }
659 }
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000660 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Steve Naroff08d92e42007-09-15 18:49:24 +0000661 Actions.ActOnFields(LBraceLoc, interfaceDecl,
662 &AllIvarDecls[0], AllIvarDecls.size(),
663 &AllVisibilities[0]);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000664 }
Steve Naroffddbff782007-08-21 21:17:12 +0000665 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
666 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000667}
Steve Naroffdac269b2007-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 Naroff3536b442007-09-06 21:24:23 +0000676/// objc-interface-decl-list
Steve Naroffdac269b2007-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 Naroff3536b442007-09-06 21:24:23 +0000683/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000684/// semicolon in the first alternative if objc-protocol-refs are omitted.
685
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000686Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000687 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-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 Narofff28b2642007-09-05 23:30:30 +0000728 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
729 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000730 return 0;
731 }
Steve Naroff3536b442007-09-06 21:24:23 +0000732 ParseObjCInterfaceDeclList(0/*FIXME*/);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000733
734 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000735 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000736 ConsumeToken(); // the "end" identifier
737 return 0;
738 }
739 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000740 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000741}
Steve Naroffdac269b2007-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 Jahanianac00b7f2007-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 Naroff3536b442007-09-06 21:24:23 +0000799 ParseObjCClassInstanceVariables(0/*FIXME*/); // we have ivars
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000800
Steve Naroffdac269b2007-08-20 21:31:48 +0000801 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000802}
Fariborz Jahanianac00b7f2007-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 Naroffdac269b2007-08-20 21:31:48 +0000808 return 0;
809}
Fariborz Jahaniane992af02007-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 Jahanian8cd8c662007-09-04 21:42:12 +0000829 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Steve Naroffdac269b2007-08-20 21:31:48 +0000830 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000831}
832
Fariborz Jahanianac00b7f2007-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000870}
871
Fariborz Jahanianac00b7f2007-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 Naroff3536b442007-09-06 21:24:23 +0000898/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000899///
900void Parser::ParseObjCInstanceMethodDefinition() {
901 assert(Tok.getKind() == tok::minus &&
902 "ParseObjCInstanceMethodDefinition(): Expected '-'");
Steve Naroff3536b442007-09-06 21:24:23 +0000903 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanianac00b7f2007-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 Naroff3536b442007-09-06 21:24:23 +0000916/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000917///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000918void Parser::ParseObjCClassMethodDefinition() {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000919 assert(Tok.getKind() == tok::plus &&
920 "ParseObjCClassMethodDefinition(): Expected '+'");
Steve Naroff3536b442007-09-06 21:24:23 +0000921 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanianac00b7f2007-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();
Reid Spencer5f016e22007-07-11 17:01:13 +0000931}
Anders Carlsson55085182007-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 Carlsson29b2cb12007-08-23 15:25:28 +0000940 default:
941 break;
942 }
943
944 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlssonf9bcf012007-08-22 15:14:15 +0000945 case tok::objc_encode:
946 return ParseObjCEncodeExpression();
Anders Carlsson29b2cb12007-08-23 15:25:28 +0000947 case tok::objc_protocol:
948 return ParseObjCProtocolExpression();
Anders Carlsson55085182007-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 Jahanian0ccb27d2007-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 Jahaniana65ff6c2007-09-05 23:08:20 +0000985 assert(Tok.getKind() == tok::l_square && "'[' expected");
986 SourceLocation Loc = ConsumeBracket(); // consume '['
987 // Parse receiver
Steve Naroff8c9f13e2007-09-16 16:16:00 +0000988 if (Tok.getKind() == tok::identifier &&
989 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope))
990 ConsumeToken();
991 else
992 ParseAssignmentExpression();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +0000993 // Parse objc-selector
994 IdentifierInfo *selIdent = ParseObjCSelector();
995 if (Tok.getKind() == tok::colon) {
996 while (1) {
997 // Each iteration parses a single keyword argument.
998 if (Tok.getKind() != tok::colon) {
999 Diag(Tok, diag::err_expected_colon);
1000 SkipUntil(tok::semi);
1001 return 0;
1002 }
1003 ConsumeToken(); // Eat the ':'.
1004 /// Parse the expression after ':'
1005 ParseAssignmentExpression();
1006 IdentifierInfo *keywordSelector = ParseObjCSelector();
1007
1008 if (!keywordSelector && Tok.getKind() != tok::colon)
1009 break;
1010 // We have a selector or a colon, continue parsing.
1011 }
1012 // Parse the, optional, argument list, comma separated.
1013 while (Tok.getKind() == tok::comma) {
1014 ConsumeToken();
1015 /// Parse the expression after ','
1016 ParseAssignmentExpression();
1017 }
1018 } else if (!selIdent) {
1019 Diag(Tok, diag::err_expected_ident); // missing selector name.
1020 SkipUntil(tok::semi);
1021 return 0;
1022 }
1023 if (Tok.getKind() != tok::r_square) {
1024 Diag(Tok, diag::err_expected_rsquare);
1025 SkipUntil(tok::semi);
1026 return 0;
1027 }
1028 ConsumeBracket(); // consume ']'
Steve Naroff8c9f13e2007-09-16 16:16:00 +00001029 return 0; // FIXME: return a message expr AST!
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001030}
1031
Anders Carlsson55085182007-08-21 17:43:55 +00001032Parser::ExprResult Parser::ParseObjCStringLiteral() {
1033 ExprResult Res = ParseStringLiteralExpression();
1034
1035 if (Res.isInvalid) return Res;
1036
1037 return Actions.ParseObjCStringLiteral(Res.Val);
1038}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001039
1040/// objc-encode-expression:
1041/// @encode ( type-name )
1042Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001043 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001044
1045 SourceLocation EncLoc = ConsumeToken();
1046
1047 if (Tok.getKind() != tok::l_paren) {
1048 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1049 return true;
1050 }
1051
1052 SourceLocation LParenLoc = ConsumeParen();
1053
1054 TypeTy *Ty = ParseTypeName();
1055
Anders Carlsson4988ae32007-08-23 15:31:37 +00001056 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001057
1058 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson4988ae32007-08-23 15:31:37 +00001059 RParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001060}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001061
1062/// objc-protocol-expression
1063/// @protocol ( protocol-name )
1064
1065Parser::ExprResult Parser::ParseObjCProtocolExpression()
1066{
1067 SourceLocation ProtoLoc = ConsumeToken();
1068
1069 if (Tok.getKind() != tok::l_paren) {
1070 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1071 return true;
1072 }
1073
1074 SourceLocation LParenLoc = ConsumeParen();
1075
1076 if (Tok.getKind() != tok::identifier) {
1077 Diag(Tok, diag::err_expected_ident);
1078 return true;
1079 }
1080
1081 // FIXME: Do something with the protocol name
1082 ConsumeToken();
1083
Anders Carlsson4988ae32007-08-23 15:31:37 +00001084 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001085
1086 // FIXME
1087 return 0;
1088}