blob: 6557c4165289b1fa4afd5e071b48bfafed20339e [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseObjc.cpp - Objective C Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Steve Naroff and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Steve Naroff09a0c4c2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Basic/Diagnostic.h"
17#include "llvm/ADT/SmallVector.h"
18using namespace clang;
19
20
21/// ParseExternalDeclaration:
22/// external-declaration: [C99 6.9]
23/// [OBJC] objc-class-definition
24/// [OBJC] objc-class-declaration [TODO]
25/// [OBJC] objc-alias-declaration [TODO]
26/// [OBJC] objc-protocol-definition [TODO]
27/// [OBJC] objc-method-definition [TODO]
28/// [OBJC] '@' 'end' [TODO]
Steve Narofffb367882007-08-20 21:31:48 +000029Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Chris Lattner4b009652007-07-25 00:24:17 +000030 SourceLocation AtLoc = ConsumeToken(); // the "@"
31
Steve Naroff87c329f2007-08-23 18:16:40 +000032 switch (Tok.getObjCKeywordID()) {
Chris Lattner4b009652007-07-25 00:24:17 +000033 case tok::objc_class:
34 return ParseObjCAtClassDeclaration(AtLoc);
35 case tok::objc_interface:
Steve Narofffb367882007-08-20 21:31:48 +000036 return ParseObjCAtInterfaceDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000037 case tok::objc_protocol:
Steve Naroff72f17fb2007-08-22 22:17:26 +000038 return ParseObjCAtProtocolDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000039 case tok::objc_implementation:
Steve Naroff81f1bba2007-09-06 21:24:23 +000040 return ObjcImpDecl = ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000041 case tok::objc_end:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000042 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000043 case tok::objc_compatibility_alias:
Fariborz Jahanianb62aff32007-09-04 19:26:51 +000044 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000045 case tok::objc_synthesize:
46 return ParseObjCPropertySynthesize(AtLoc);
47 case tok::objc_dynamic:
48 return ParseObjCPropertyDynamic(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000049 default:
50 Diag(AtLoc, diag::err_unexpected_at);
51 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000052 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000053 }
54}
55
56///
57/// objc-class-declaration:
58/// '@' 'class' identifier-list ';'
59///
Steve Narofffb367882007-08-20 21:31:48 +000060Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000061 ConsumeToken(); // the identifier "class"
62 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
63
64 while (1) {
65 if (Tok.getKind() != tok::identifier) {
66 Diag(Tok, diag::err_expected_ident);
67 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000068 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000069 }
Chris Lattner4b009652007-07-25 00:24:17 +000070 ClassNames.push_back(Tok.getIdentifierInfo());
71 ConsumeToken();
72
73 if (Tok.getKind() != tok::comma)
74 break;
75
76 ConsumeToken();
77 }
78
79 // Consume the ';'.
80 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Narofffb367882007-08-20 21:31:48 +000081 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000082
Steve Naroff81f1bba2007-09-06 21:24:23 +000083 return Actions.ObjcClassDeclaration(CurScope, atLoc,
84 &ClassNames[0], ClassNames.size());
Chris Lattner4b009652007-07-25 00:24:17 +000085}
86
Steve Narofffb367882007-08-20 21:31:48 +000087///
88/// objc-interface:
89/// objc-class-interface-attributes[opt] objc-class-interface
90/// objc-category-interface
91///
92/// objc-class-interface:
93/// '@' 'interface' identifier objc-superclass[opt]
94/// objc-protocol-refs[opt]
95/// objc-class-instance-variables[opt]
96/// objc-interface-decl-list
97/// @end
98///
99/// objc-category-interface:
100/// '@' 'interface' identifier '(' identifier[opt] ')'
101/// objc-protocol-refs[opt]
102/// objc-interface-decl-list
103/// @end
104///
105/// objc-superclass:
106/// ':' identifier
107///
108/// objc-class-interface-attributes:
109/// __attribute__((visibility("default")))
110/// __attribute__((visibility("hidden")))
111/// __attribute__((deprecated))
112/// __attribute__((unavailable))
113/// __attribute__((objc_exception)) - used by NSException on 64-bit
114///
115Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
116 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000117 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Narofffb367882007-08-20 21:31:48 +0000118 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
119 ConsumeToken(); // the "interface" identifier
120
121 if (Tok.getKind() != tok::identifier) {
122 Diag(Tok, diag::err_expected_ident); // missing class or category name.
123 return 0;
124 }
125 // We have a class or category name - consume it.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000126 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Narofffb367882007-08-20 21:31:48 +0000127 SourceLocation nameLoc = ConsumeToken();
128
Steve Naroffa7f62782007-08-23 19:56:30 +0000129 if (Tok.getKind() == tok::l_paren) { // we have a category.
Steve Narofffb367882007-08-20 21:31:48 +0000130 SourceLocation lparenLoc = ConsumeParen();
131 SourceLocation categoryLoc, rparenLoc;
132 IdentifierInfo *categoryId = 0;
133
Steve Naroffa7f62782007-08-23 19:56:30 +0000134 // For ObjC2, the category name is optional (not an error).
Steve Narofffb367882007-08-20 21:31:48 +0000135 if (Tok.getKind() == tok::identifier) {
136 categoryId = Tok.getIdentifierInfo();
137 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000138 } else if (!getLang().ObjC2) {
139 Diag(Tok, diag::err_expected_ident); // missing category name.
140 return 0;
Steve Narofffb367882007-08-20 21:31:48 +0000141 }
142 if (Tok.getKind() != tok::r_paren) {
143 Diag(Tok, diag::err_expected_rparen);
144 SkipUntil(tok::r_paren, false); // don't stop at ';'
145 return 0;
146 }
147 rparenLoc = ConsumeParen();
148 // Next, we need to check for any protocol references.
149 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000150 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
151 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000152 return 0;
153 }
154 if (attrList) // categories don't support attributes.
155 Diag(Tok, diag::err_objc_no_attributes_on_category);
156
Steve Naroff81f1bba2007-09-06 21:24:23 +0000157 ParseObjCInterfaceDeclList(0/*FIXME*/);
Steve Narofffb367882007-08-20 21:31:48 +0000158
Steve Naroff0bbffd82007-08-22 16:35:03 +0000159 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000160 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000161 ConsumeToken(); // the "end" identifier
Steve Narofffb367882007-08-20 21:31:48 +0000162 return 0;
163 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000164 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000165 return 0;
166 }
167 // Parse a class interface.
168 IdentifierInfo *superClassId = 0;
169 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000170
Steve Narofffb367882007-08-20 21:31:48 +0000171 if (Tok.getKind() == tok::colon) { // a super class is specified.
172 ConsumeToken();
173 if (Tok.getKind() != tok::identifier) {
174 Diag(Tok, diag::err_expected_ident); // missing super class name.
175 return 0;
176 }
177 superClassId = Tok.getIdentifierInfo();
178 superClassLoc = ConsumeToken();
179 }
180 // Next, we need to check for any protocol references.
Steve Naroff304ed392007-09-05 23:30:30 +0000181 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofffb367882007-08-20 21:31:48 +0000182 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000183 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000184 return 0;
185 }
Steve Naroff304ed392007-09-05 23:30:30 +0000186 DeclTy *ClsType = Actions.ObjcStartClassInterface(atLoc, nameId, nameLoc,
187 superClassId, superClassLoc, &ProtocolRefs[0],
188 ProtocolRefs.size(), attrList);
189
Steve Narofffb367882007-08-20 21:31:48 +0000190 if (Tok.getKind() == tok::l_brace)
Steve Naroff81f1bba2007-09-06 21:24:23 +0000191 ParseObjCClassInstanceVariables(ClsType);
Steve Narofffb367882007-08-20 21:31:48 +0000192
Steve Naroff81f1bba2007-09-06 21:24:23 +0000193 ParseObjCInterfaceDeclList(ClsType);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000194
195 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000196 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000197 ConsumeToken(); // the "end" identifier
Steve Narofffb367882007-08-20 21:31:48 +0000198 return 0;
199 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000200 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000201 return 0;
202}
203
204/// objc-interface-decl-list:
205/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000206/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000207/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000208/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000209/// objc-interface-decl-list declaration
210/// objc-interface-decl-list ';'
211///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000212/// objc-method-requirement: [OBJC2]
213/// @required
214/// @optional
215///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000216void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000217 while (1) {
218 if (Tok.getKind() == tok::at) {
219 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000220 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000221
222 if (ocKind == tok::objc_end) { // terminate list
223 return;
224 } else if (ocKind == tok::objc_required) { // protocols only
225 ConsumeToken();
226 continue;
227 } else if (ocKind == tok::objc_optional) { // protocols only
228 ConsumeToken();
229 continue;
230 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000231 ParseObjCPropertyDecl(0/*FIXME*/);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000232 continue;
233 } else {
234 Diag(Tok, diag::err_objc_illegal_interface_qual);
235 ConsumeToken();
236 }
237 }
238 if (Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000239 ParseObjCMethodPrototype(interfaceDecl);
240 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
241 // method definitions.
242 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000243 continue;
244 }
245 if (Tok.getKind() == tok::semi)
246 ConsumeToken();
247 else if (Tok.getKind() == tok::eof)
248 return;
Steve Naroff304ed392007-09-05 23:30:30 +0000249 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000250 // FIXME: as the name implies, this rule allows function definitions.
251 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000252 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000253 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000254 }
255}
256
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000257/// Parse property attribute declarations.
258///
259/// property-attr-decl: '(' property-attrlist ')'
260/// property-attrlist:
261/// property-attribute
262/// property-attrlist ',' property-attribute
263/// property-attribute:
264/// getter '=' identifier
265/// setter '=' identifier ':'
266/// readonly
267/// readwrite
268/// assign
269/// retain
270/// copy
271/// nonatomic
272///
273void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
274 SourceLocation loc = ConsumeParen(); // consume '('
275 while (isObjCPropertyAttribute()) {
276 const IdentifierInfo *II = Tok.getIdentifierInfo();
277 // getter/setter require extra treatment.
278 if (II == ObjcPropertyAttrs[objc_getter] ||
279 II == ObjcPropertyAttrs[objc_setter]) {
280 // skip getter/setter part.
281 SourceLocation loc = ConsumeToken();
282 if (Tok.getKind() == tok::equal) {
283 loc = ConsumeToken();
284 if (Tok.getKind() == tok::identifier) {
285 if (II == ObjcPropertyAttrs[objc_setter]) {
286 loc = ConsumeToken(); // consume method name
287 if (Tok.getKind() != tok::colon) {
288 Diag(loc, diag::err_expected_colon);
289 SkipUntil(tok::r_paren,true,true);
290 break;
291 }
292 }
293 }
294 else {
295 Diag(loc, diag::err_expected_ident);
296 SkipUntil(tok::r_paren,true,true);
297 break;
298 }
299 }
300 else {
301 Diag(loc, diag::err_objc_expected_equal);
302 SkipUntil(tok::r_paren,true,true);
303 break;
304 }
305 }
306 ConsumeToken(); // consume last attribute token
307 if (Tok.getKind() == tok::comma) {
308 loc = ConsumeToken();
309 continue;
310 }
311 if (Tok.getKind() == tok::r_paren)
312 break;
313 Diag(loc, diag::err_expected_rparen);
314 SkipUntil(tok::semi);
315 return;
316 }
317 if (Tok.getKind() == tok::r_paren)
318 ConsumeParen();
319 else {
320 Diag(loc, diag::err_objc_expected_property_attr);
321 SkipUntil(tok::r_paren); // recover from error inside attribute list
322 }
323}
324
325/// Main routine to parse property declaration.
326///
327/// @property property-attr-decl[opt] property-component-decl ';'
328///
329void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
330 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
331 "ParseObjCPropertyDecl(): Expected @property");
332 ConsumeToken(); // the "property" identifier
333 // Parse property attribute list, if any.
334 if (Tok.getKind() == tok::l_paren) {
335 // property has attribute list.
336 ParseObjCPropertyAttribute(0/*FIXME*/);
337 }
338 // Parse declaration portion of @property.
339 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
340 ParseStructDeclaration(interfaceDecl, PropertyDecls);
341 if (Tok.getKind() == tok::semi)
342 ConsumeToken();
343 else {
344 Diag(Tok, diag::err_expected_semi_decl_list);
345 SkipUntil(tok::r_brace, true, true);
346 }
Chris Lattner4b009652007-07-25 00:24:17 +0000347}
Steve Narofffb367882007-08-20 21:31:48 +0000348
Steve Naroff81f1bba2007-09-06 21:24:23 +0000349/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000350/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000351/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000352///
353/// objc-instance-method: '-'
354/// objc-class-method: '+'
355///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000356/// objc-method-attributes: [OBJC2]
357/// __attribute__((deprecated))
358///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000359Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *CDecl) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000360 assert((Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) &&
361 "expected +/-");
362
363 tok::TokenKind methodType = Tok.getKind();
364 SourceLocation methodLoc = ConsumeToken();
365
Steve Naroff304ed392007-09-05 23:30:30 +0000366 DeclTy *MDecl = ParseObjCMethodDecl(methodType, methodLoc);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000367
368 AttributeList *methodAttrs = 0;
369 // If attributes exist after the method, parse them.
370 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
371 methodAttrs = ParseAttributes();
372
373 if (CDecl)
374 Actions.ObjcAddMethod(CDecl, MDecl, methodAttrs);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000375
Steve Naroff81f1bba2007-09-06 21:24:23 +0000376 // Since this rule is used for both method declarations and definitions,
377 // the caller is responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000378 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000379}
380
381/// objc-selector:
382/// identifier
383/// one of
384/// enum struct union if else while do for switch case default
385/// break continue return goto asm sizeof typeof __alignof
386/// unsigned long const short volatile signed restrict _Complex
387/// in out inout bycopy byref oneway int char float double void _Bool
388///
389IdentifierInfo *Parser::ParseObjCSelector() {
390 tok::TokenKind tKind = Tok.getKind();
391 IdentifierInfo *II = 0;
392
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000393 if (tKind == tok::identifier || tKind == tok::kw_typeof ||
394 tKind == tok::kw___alignof ||
Steve Naroff0bbffd82007-08-22 16:35:03 +0000395 (tKind >= tok::kw_auto && tKind <= tok::kw__Complex)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000396 II = Tok.getIdentifierInfo();
397 ConsumeToken();
398 }
399 return II;
400}
401
Steve Naroffa8ee2262007-08-22 23:18:22 +0000402/// objc-type-qualifier: one of
403/// in out inout bycopy byref oneway
404///
Steve Naroffa8ee2262007-08-22 23:18:22 +0000405bool Parser::isObjCTypeQualifier() {
406 if (Tok.getKind() == tok::identifier) {
Chris Lattner32352462007-08-29 22:54:08 +0000407 const IdentifierInfo *II = Tok.getIdentifierInfo();
408 for (unsigned i = 0; i < objc_NumQuals; ++i)
409 if (II == ObjcTypeQuals[i]) return true;
Steve Naroffa8ee2262007-08-22 23:18:22 +0000410 }
411 return false;
412}
413
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000414/// property-attrlist: one of
415/// readonly getter setter assign retain copy nonatomic
416///
417bool Parser::isObjCPropertyAttribute() {
418 if (Tok.getKind() == tok::identifier) {
419 const IdentifierInfo *II = Tok.getIdentifierInfo();
420 for (unsigned i = 0; i < objc_NumAttrs; ++i)
421 if (II == ObjcPropertyAttrs[i]) return true;
422 }
423 return false;
424}
425
Steve Naroff0bbffd82007-08-22 16:35:03 +0000426/// objc-type-name:
427/// '(' objc-type-qualifiers[opt] type-name ')'
428/// '(' objc-type-qualifiers[opt] ')'
429///
430/// objc-type-qualifiers:
431/// objc-type-qualifier
432/// objc-type-qualifiers objc-type-qualifier
433///
Steve Naroff304ed392007-09-05 23:30:30 +0000434Parser::TypeTy *Parser::ParseObjCTypeName() {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000435 assert(Tok.getKind() == tok::l_paren && "expected (");
436
437 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Steve Naroff304ed392007-09-05 23:30:30 +0000438 TypeTy *Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000439
Steve Naroffa8ee2262007-08-22 23:18:22 +0000440 while (isObjCTypeQualifier())
441 ConsumeToken();
442
Steve Naroff0bbffd82007-08-22 16:35:03 +0000443 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000444 Ty = ParseTypeName();
445 // FIXME: back when Sema support is in place...
446 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000447 }
448 if (Tok.getKind() != tok::r_paren) {
449 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000450 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000451 }
452 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000453 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000454}
455
456/// objc-method-decl:
457/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000458/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000459/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000460/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000461///
462/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000463/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000464/// objc-keyword-selector objc-keyword-decl
465///
466/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000467/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
468/// objc-selector ':' objc-keyword-attributes[opt] identifier
469/// ':' objc-type-name objc-keyword-attributes[opt] identifier
470/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000471///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000472/// objc-parmlist:
473/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000474///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000475/// objc-parms:
476/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000477///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000478/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000479/// , ...
480///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000481/// objc-keyword-attributes: [OBJC2]
482/// __attribute__((unused))
483///
Steve Naroff304ed392007-09-05 23:30:30 +0000484Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType, SourceLocation mLoc) {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000485
Steve Naroff304ed392007-09-05 23:30:30 +0000486 TypeTy *ReturnType = 0;
487
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000488 // Parse the return type.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000489 if (Tok.getKind() == tok::l_paren)
Steve Naroff304ed392007-09-05 23:30:30 +0000490 ReturnType = ParseObjCTypeName();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000491 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff304ed392007-09-05 23:30:30 +0000492
493 llvm::SmallVector<ObjcKeywordInfo, 12> KeyInfo;
494 int KeySlot = 0;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000495
496 if (Tok.getKind() == tok::colon) {
Steve Naroff304ed392007-09-05 23:30:30 +0000497
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000498 while (1) {
Steve Naroff304ed392007-09-05 23:30:30 +0000499 KeyInfo[KeySlot].SelectorName = selIdent;
500
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000501 // Each iteration parses a single keyword argument.
502 if (Tok.getKind() != tok::colon) {
503 Diag(Tok, diag::err_expected_colon);
504 break;
505 }
Steve Naroff304ed392007-09-05 23:30:30 +0000506 KeyInfo[KeySlot].ColonLoc = ConsumeToken(); // Eat the ':'.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000507 if (Tok.getKind() == tok::l_paren) // Parse the argument type.
Steve Naroff304ed392007-09-05 23:30:30 +0000508 KeyInfo[KeySlot].TypeInfo = ParseObjCTypeName();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000509
510 // If attributes exist before the argument name, parse them.
Steve Naroffa7f62782007-08-23 19:56:30 +0000511 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
Steve Naroff304ed392007-09-05 23:30:30 +0000512 KeyInfo[KeySlot].AttrList = ParseAttributes();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000513
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000514 if (Tok.getKind() != tok::identifier) {
515 Diag(Tok, diag::err_expected_ident); // missing argument name.
516 break;
517 }
Steve Naroff304ed392007-09-05 23:30:30 +0000518 KeyInfo[KeySlot].ArgumentName = Tok.getIdentifierInfo();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000519 ConsumeToken(); // Eat the identifier.
Steve Naroff304ed392007-09-05 23:30:30 +0000520
521 // Rather than call out to the actions, try packaging up the info
522 // locally, like we do for Declarator.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000523 // FIXME: add Actions.BuildObjCKeyword()
524
Steve Naroff304ed392007-09-05 23:30:30 +0000525 selIdent = ParseObjCSelector();
526 if (!selIdent && Tok.getKind() != tok::colon)
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000527 break;
528 // We have a selector or a colon, continue parsing.
Steve Naroff304ed392007-09-05 23:30:30 +0000529 KeySlot++;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000530 }
531 // Parse the (optional) parameter list.
532 while (Tok.getKind() == tok::comma) {
533 ConsumeToken();
534 if (Tok.getKind() == tok::ellipsis) {
535 ConsumeToken();
536 break;
537 }
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000538 // Parse the c-style argument declaration-specifier.
539 DeclSpec DS;
540 ParseDeclarationSpecifiers(DS);
541 // Parse the declarator.
542 Declarator ParmDecl(DS, Declarator::PrototypeContext);
543 ParseDeclarator(ParmDecl);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000544 }
Steve Naroff304ed392007-09-05 23:30:30 +0000545 // FIXME: Add support for optional parmameter list...
546 return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType,
Steve Naroff81f1bba2007-09-06 21:24:23 +0000547 &KeyInfo[0], KeyInfo.size());
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000548 } else if (!selIdent) {
549 Diag(Tok, diag::err_expected_ident); // missing selector name.
550 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000551 return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType, selIdent);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000552}
553
Steve Narofffb367882007-08-20 21:31:48 +0000554/// objc-protocol-refs:
555/// '<' identifier-list '>'
556///
Steve Naroff304ed392007-09-05 23:30:30 +0000557bool Parser::ParseObjCProtocolReferences(
558 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Steve Narofffb367882007-08-20 21:31:48 +0000559 assert(Tok.getKind() == tok::less && "expected <");
560
561 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000562
563 while (1) {
564 if (Tok.getKind() != tok::identifier) {
565 Diag(Tok, diag::err_expected_ident);
566 SkipUntil(tok::greater);
567 return true;
568 }
569 ProtocolRefs.push_back(Tok.getIdentifierInfo());
570 ConsumeToken();
571
572 if (Tok.getKind() != tok::comma)
573 break;
574 ConsumeToken();
575 }
576 // Consume the '>'.
577 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
578}
579
580/// objc-class-instance-variables:
581/// '{' objc-instance-variable-decl-list[opt] '}'
582///
583/// objc-instance-variable-decl-list:
584/// objc-visibility-spec
585/// objc-instance-variable-decl ';'
586/// ';'
587/// objc-instance-variable-decl-list objc-visibility-spec
588/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
589/// objc-instance-variable-decl-list ';'
590///
591/// objc-visibility-spec:
592/// @private
593/// @protected
594/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000595/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000596///
597/// objc-instance-variable-decl:
598/// struct-declaration
599///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000600void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Steve Naroffc4474992007-08-21 21:17:12 +0000601 assert(Tok.getKind() == tok::l_brace && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000602 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Steve Naroffc4474992007-08-21 21:17:12 +0000603
604 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000605
606 // While we still have something to read, read the instance variables.
607 while (Tok.getKind() != tok::r_brace &&
608 Tok.getKind() != tok::eof) {
609 // Each iteration of this loop reads one objc-instance-variable-decl.
610
611 // Check for extraneous top-level semicolon.
612 if (Tok.getKind() == tok::semi) {
613 Diag(Tok, diag::ext_extra_struct_semi);
614 ConsumeToken();
615 continue;
616 }
617 // Set the default visibility to private.
618 tok::ObjCKeywordKind visibility = tok::objc_private;
619 if (Tok.getKind() == tok::at) { // parse objc-visibility-spec
620 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000621 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000622 case tok::objc_private:
623 case tok::objc_public:
624 case tok::objc_protected:
625 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000626 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000627 ConsumeToken();
628 continue;
629 default:
630 Diag(Tok, diag::err_objc_illegal_visibility_spec);
631 ConsumeToken();
632 continue;
633 }
634 }
635 ParseStructDeclaration(interfaceDecl, IvarDecls);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000636 for (unsigned i = 0; i < IvarDecls.size(); i++)
637 Actions.ObjcAddInstanceVariable(interfaceDecl, IvarDecls[i], visibility);
638 IvarDecls.clear();
639
Steve Naroffc4474992007-08-21 21:17:12 +0000640 if (Tok.getKind() == tok::semi) {
641 ConsumeToken();
642 } else if (Tok.getKind() == tok::r_brace) {
643 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
644 break;
645 } else {
646 Diag(Tok, diag::err_expected_semi_decl_list);
647 // Skip to end of block or statement
648 SkipUntil(tok::r_brace, true, true);
649 }
650 }
651 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
652 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000653}
Steve Narofffb367882007-08-20 21:31:48 +0000654
655/// objc-protocol-declaration:
656/// objc-protocol-definition
657/// objc-protocol-forward-reference
658///
659/// objc-protocol-definition:
660/// @protocol identifier
661/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000662/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000663/// @end
664///
665/// objc-protocol-forward-reference:
666/// @protocol identifier-list ';'
667///
668/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000669/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000670/// semicolon in the first alternative if objc-protocol-refs are omitted.
671
Steve Naroff72f17fb2007-08-22 22:17:26 +0000672Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000673 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000674 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
675 ConsumeToken(); // the "protocol" identifier
676
677 if (Tok.getKind() != tok::identifier) {
678 Diag(Tok, diag::err_expected_ident); // missing protocol name.
679 return 0;
680 }
681 // Save the protocol name, then consume it.
682 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
683 SourceLocation nameLoc = ConsumeToken();
684
685 if (Tok.getKind() == tok::semi) { // forward declaration.
686 ConsumeToken();
687 return 0; // FIXME: add protocolName
688 }
689 if (Tok.getKind() == tok::comma) { // list of forward declarations.
690 // Parse the list of forward declarations.
691 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
692 ProtocolRefs.push_back(protocolName);
693
694 while (1) {
695 ConsumeToken(); // the ','
696 if (Tok.getKind() != tok::identifier) {
697 Diag(Tok, diag::err_expected_ident);
698 SkipUntil(tok::semi);
699 return 0;
700 }
701 ProtocolRefs.push_back(Tok.getIdentifierInfo());
702 ConsumeToken(); // the identifier
703
704 if (Tok.getKind() != tok::comma)
705 break;
706 }
707 // Consume the ';'.
708 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
709 return 0;
710 return 0; // FIXME
711 }
712 // Last, and definitely not least, parse a protocol declaration.
713 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000714 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
715 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000716 return 0;
717 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000718 ParseObjCInterfaceDeclList(0/*FIXME*/);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000719
720 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000721 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000722 ConsumeToken(); // the "end" identifier
723 return 0;
724 }
725 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000726 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000727}
Steve Narofffb367882007-08-20 21:31:48 +0000728
729/// objc-implementation:
730/// objc-class-implementation-prologue
731/// objc-category-implementation-prologue
732///
733/// objc-class-implementation-prologue:
734/// @implementation identifier objc-superclass[opt]
735/// objc-class-instance-variables[opt]
736///
737/// objc-category-implementation-prologue:
738/// @implementation identifier ( identifier )
739
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000740Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
741 SourceLocation atLoc) {
742 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
743 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
744 ConsumeToken(); // the "implementation" identifier
745
746 if (Tok.getKind() != tok::identifier) {
747 Diag(Tok, diag::err_expected_ident); // missing class or category name.
748 return 0;
749 }
750 // We have a class or category name - consume it.
751 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
752
753 if (Tok.getKind() == tok::l_paren) {
754 // we have a category implementation.
755 SourceLocation lparenLoc = ConsumeParen();
756 SourceLocation categoryLoc, rparenLoc;
757 IdentifierInfo *categoryId = 0;
758
759 if (Tok.getKind() == tok::identifier) {
760 categoryId = Tok.getIdentifierInfo();
761 categoryLoc = ConsumeToken();
762 } else {
763 Diag(Tok, diag::err_expected_ident); // missing category name.
764 return 0;
765 }
766 if (Tok.getKind() != tok::r_paren) {
767 Diag(Tok, diag::err_expected_rparen);
768 SkipUntil(tok::r_paren, false); // don't stop at ';'
769 return 0;
770 }
771 rparenLoc = ConsumeParen();
772 return 0;
773 }
774 // We have a class implementation
775 if (Tok.getKind() == tok::colon) {
776 // We have a super class
777 ConsumeToken();
778 if (Tok.getKind() != tok::identifier) {
779 Diag(Tok, diag::err_expected_ident); // missing super class name.
780 return 0;
781 }
782 ConsumeToken(); // Consume super class name
783 }
784 if (Tok.getKind() == tok::l_brace)
Steve Naroff81f1bba2007-09-06 21:24:23 +0000785 ParseObjCClassInstanceVariables(0/*FIXME*/); // we have ivars
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000786
Steve Narofffb367882007-08-20 21:31:48 +0000787 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000788}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000789Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
790 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
791 "ParseObjCAtEndDeclaration(): Expected @end");
792 ConsumeToken(); // the "end" identifier
793
Steve Narofffb367882007-08-20 21:31:48 +0000794 return 0;
795}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000796
797/// compatibility-alias-decl:
798/// @compatibility_alias alias-name class-name ';'
799///
800Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
801 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
802 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
803 ConsumeToken(); // consume compatibility_alias
804 if (Tok.getKind() != tok::identifier) {
805 Diag(Tok, diag::err_expected_ident);
806 return 0;
807 }
808 ConsumeToken(); // consume alias-name
809 if (Tok.getKind() != tok::identifier) {
810 Diag(Tok, diag::err_expected_ident);
811 return 0;
812 }
813 ConsumeToken(); // consume class-name;
814 if (Tok.getKind() != tok::semi)
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000815 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Steve Narofffb367882007-08-20 21:31:48 +0000816 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000817}
818
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000819/// property-synthesis:
820/// @synthesize property-ivar-list ';'
821///
822/// property-ivar-list:
823/// property-ivar
824/// property-ivar-list ',' property-ivar
825///
826/// property-ivar:
827/// identifier
828/// identifier '=' identifier
829///
830Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
831 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
832 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
833 SourceLocation loc = ConsumeToken(); // consume dynamic
834 if (Tok.getKind() != tok::identifier) {
835 Diag(Tok, diag::err_expected_ident);
836 return 0;
837 }
838 while (Tok.getKind() == tok::identifier) {
839 ConsumeToken(); // consume property name
840 if (Tok.getKind() == tok::equal) {
841 // property '=' ivar-name
842 ConsumeToken(); // consume '='
843 if (Tok.getKind() != tok::identifier) {
844 Diag(Tok, diag::err_expected_ident);
845 break;
846 }
847 ConsumeToken(); // consume ivar-name
848 }
849 if (Tok.getKind() != tok::comma)
850 break;
851 ConsumeToken(); // consume ','
852 }
853 if (Tok.getKind() != tok::semi)
854 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
855 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000856}
857
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000858/// property-dynamic:
859/// @dynamic property-list
860///
861/// property-list:
862/// identifier
863/// property-list ',' identifier
864///
865Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
866 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
867 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
868 SourceLocation loc = ConsumeToken(); // consume dynamic
869 if (Tok.getKind() != tok::identifier) {
870 Diag(Tok, diag::err_expected_ident);
871 return 0;
872 }
873 while (Tok.getKind() == tok::identifier) {
874 ConsumeToken(); // consume property name
875 if (Tok.getKind() != tok::comma)
876 break;
877 ConsumeToken(); // consume ','
878 }
879 if (Tok.getKind() != tok::semi)
880 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
881 return 0;
882}
883
Steve Naroff81f1bba2007-09-06 21:24:23 +0000884/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000885///
886void Parser::ParseObjCInstanceMethodDefinition() {
887 assert(Tok.getKind() == tok::minus &&
888 "ParseObjCInstanceMethodDefinition(): Expected '-'");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000889 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000890 // parse optional ';'
891 if (Tok.getKind() == tok::semi)
892 ConsumeToken();
893
894 if (Tok.getKind() != tok::l_brace) {
895 Diag (Tok, diag::err_expected_lbrace);
896 return;
897 }
898
899 StmtResult FnBody = ParseCompoundStatementBody();
900}
901
Steve Naroff81f1bba2007-09-06 21:24:23 +0000902/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000903///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000904void Parser::ParseObjCClassMethodDefinition() {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000905 assert(Tok.getKind() == tok::plus &&
906 "ParseObjCClassMethodDefinition(): Expected '+'");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000907 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000908 // parse optional ';'
909 if (Tok.getKind() == tok::semi)
910 ConsumeToken();
911 if (Tok.getKind() != tok::l_brace) {
912 Diag (Tok, diag::err_expected_lbrace);
913 return;
914 }
915
916 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +0000917}
Anders Carlssona66cad42007-08-21 17:43:55 +0000918
919Parser::ExprResult Parser::ParseObjCExpression() {
920 SourceLocation AtLoc = ConsumeToken(); // the "@"
921
922 switch (Tok.getKind()) {
923 case tok::string_literal: // primary-expression: string-literal
924 case tok::wide_string_literal:
925 return ParseObjCStringLiteral();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000926 default:
927 break;
928 }
929
930 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlsson8be1d402007-08-22 15:14:15 +0000931 case tok::objc_encode:
932 return ParseObjCEncodeExpression();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000933 case tok::objc_protocol:
934 return ParseObjCProtocolExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +0000935 default:
936 Diag(AtLoc, diag::err_unexpected_at);
937 SkipUntil(tok::semi);
938 break;
939 }
940
941 return 0;
942}
943
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000944/// objc-message-expr:
945/// '[' objc-receiver objc-message-args ']'
946///
947/// objc-receiver:
948/// expression
949/// class-name
950/// type-name
951///
952/// objc-message-args:
953/// objc-selector
954/// objc-keywordarg-list
955///
956/// objc-keywordarg-list:
957/// objc-keywordarg
958/// objc-keywordarg-list objc-keywordarg
959///
960/// objc-keywordarg:
961/// selector-name[opt] ':' objc-keywordexpr
962///
963/// objc-keywordexpr:
964/// nonempty-expr-list
965///
966/// nonempty-expr-list:
967/// assignment-expression
968/// nonempty-expr-list , assignment-expression
969///
970Parser::ExprResult Parser::ParseObjCMessageExpression() {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +0000971 assert(Tok.getKind() == tok::l_square && "'[' expected");
972 SourceLocation Loc = ConsumeBracket(); // consume '['
973 // Parse receiver
974 // FIXME: receiver as type-name/class-name
975 ParseAssignmentExpression();
976 // Parse objc-selector
977 IdentifierInfo *selIdent = ParseObjCSelector();
978 if (Tok.getKind() == tok::colon) {
979 while (1) {
980 // Each iteration parses a single keyword argument.
981 if (Tok.getKind() != tok::colon) {
982 Diag(Tok, diag::err_expected_colon);
983 SkipUntil(tok::semi);
984 return 0;
985 }
986 ConsumeToken(); // Eat the ':'.
987 /// Parse the expression after ':'
988 ParseAssignmentExpression();
989 IdentifierInfo *keywordSelector = ParseObjCSelector();
990
991 if (!keywordSelector && Tok.getKind() != tok::colon)
992 break;
993 // We have a selector or a colon, continue parsing.
994 }
995 // Parse the, optional, argument list, comma separated.
996 while (Tok.getKind() == tok::comma) {
997 ConsumeToken();
998 /// Parse the expression after ','
999 ParseAssignmentExpression();
1000 }
1001 } else if (!selIdent) {
1002 Diag(Tok, diag::err_expected_ident); // missing selector name.
1003 SkipUntil(tok::semi);
1004 return 0;
1005 }
1006 if (Tok.getKind() != tok::r_square) {
1007 Diag(Tok, diag::err_expected_rsquare);
1008 SkipUntil(tok::semi);
1009 return 0;
1010 }
1011 ConsumeBracket(); // consume ']'
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001012 return 0;
1013}
1014
Anders Carlssona66cad42007-08-21 17:43:55 +00001015Parser::ExprResult Parser::ParseObjCStringLiteral() {
1016 ExprResult Res = ParseStringLiteralExpression();
1017
1018 if (Res.isInvalid) return Res;
1019
1020 return Actions.ParseObjCStringLiteral(Res.Val);
1021}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001022
1023/// objc-encode-expression:
1024/// @encode ( type-name )
1025Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff87c329f2007-08-23 18:16:40 +00001026 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001027
1028 SourceLocation EncLoc = ConsumeToken();
1029
1030 if (Tok.getKind() != tok::l_paren) {
1031 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1032 return true;
1033 }
1034
1035 SourceLocation LParenLoc = ConsumeParen();
1036
1037 TypeTy *Ty = ParseTypeName();
1038
Anders Carlsson92faeb82007-08-23 15:31:37 +00001039 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001040
1041 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001042 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001043}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001044
1045/// objc-protocol-expression
1046/// @protocol ( protocol-name )
1047
1048Parser::ExprResult Parser::ParseObjCProtocolExpression()
1049{
1050 SourceLocation ProtoLoc = ConsumeToken();
1051
1052 if (Tok.getKind() != tok::l_paren) {
1053 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1054 return true;
1055 }
1056
1057 SourceLocation LParenLoc = ConsumeParen();
1058
1059 if (Tok.getKind() != tok::identifier) {
1060 Diag(Tok, diag::err_expected_ident);
1061 return true;
1062 }
1063
1064 // FIXME: Do something with the protocol name
1065 ConsumeToken();
1066
Anders Carlsson92faeb82007-08-23 15:31:37 +00001067 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001068
1069 // FIXME
1070 return 0;
1071}