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