blob: 8fa071a3fe8d28830e64cad01270cf2cb22e0131 [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
Steve Naroff0acc9c92007-09-15 18:49:24 +0000662 Actions.ActOnFields(LBraceLoc, interfaceDecl,
663 &AllIvarDecls[0], AllIvarDecls.size(),
664 &AllVisibilities[0]);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000665 }
Steve Naroffc4474992007-08-21 21:17:12 +0000666 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
667 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000668}
Steve Narofffb367882007-08-20 21:31:48 +0000669
670/// objc-protocol-declaration:
671/// objc-protocol-definition
672/// objc-protocol-forward-reference
673///
674/// objc-protocol-definition:
675/// @protocol identifier
676/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000677/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000678/// @end
679///
680/// objc-protocol-forward-reference:
681/// @protocol identifier-list ';'
682///
683/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000684/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000685/// semicolon in the first alternative if objc-protocol-refs are omitted.
686
Steve Naroff72f17fb2007-08-22 22:17:26 +0000687Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000688 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000689 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
690 ConsumeToken(); // the "protocol" identifier
691
692 if (Tok.getKind() != tok::identifier) {
693 Diag(Tok, diag::err_expected_ident); // missing protocol name.
694 return 0;
695 }
696 // Save the protocol name, then consume it.
697 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
698 SourceLocation nameLoc = ConsumeToken();
699
700 if (Tok.getKind() == tok::semi) { // forward declaration.
701 ConsumeToken();
702 return 0; // FIXME: add protocolName
703 }
704 if (Tok.getKind() == tok::comma) { // list of forward declarations.
705 // Parse the list of forward declarations.
706 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
707 ProtocolRefs.push_back(protocolName);
708
709 while (1) {
710 ConsumeToken(); // the ','
711 if (Tok.getKind() != tok::identifier) {
712 Diag(Tok, diag::err_expected_ident);
713 SkipUntil(tok::semi);
714 return 0;
715 }
716 ProtocolRefs.push_back(Tok.getIdentifierInfo());
717 ConsumeToken(); // the identifier
718
719 if (Tok.getKind() != tok::comma)
720 break;
721 }
722 // Consume the ';'.
723 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
724 return 0;
725 return 0; // FIXME
726 }
727 // Last, and definitely not least, parse a protocol declaration.
728 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000729 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
730 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000731 return 0;
732 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000733 ParseObjCInterfaceDeclList(0/*FIXME*/);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000734
735 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000736 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000737 ConsumeToken(); // the "end" identifier
738 return 0;
739 }
740 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000741 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000742}
Steve Narofffb367882007-08-20 21:31:48 +0000743
744/// objc-implementation:
745/// objc-class-implementation-prologue
746/// objc-category-implementation-prologue
747///
748/// objc-class-implementation-prologue:
749/// @implementation identifier objc-superclass[opt]
750/// objc-class-instance-variables[opt]
751///
752/// objc-category-implementation-prologue:
753/// @implementation identifier ( identifier )
754
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000755Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
756 SourceLocation atLoc) {
757 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
758 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
759 ConsumeToken(); // the "implementation" identifier
760
761 if (Tok.getKind() != tok::identifier) {
762 Diag(Tok, diag::err_expected_ident); // missing class or category name.
763 return 0;
764 }
765 // We have a class or category name - consume it.
766 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
767
768 if (Tok.getKind() == tok::l_paren) {
769 // we have a category implementation.
770 SourceLocation lparenLoc = ConsumeParen();
771 SourceLocation categoryLoc, rparenLoc;
772 IdentifierInfo *categoryId = 0;
773
774 if (Tok.getKind() == tok::identifier) {
775 categoryId = Tok.getIdentifierInfo();
776 categoryLoc = ConsumeToken();
777 } else {
778 Diag(Tok, diag::err_expected_ident); // missing category name.
779 return 0;
780 }
781 if (Tok.getKind() != tok::r_paren) {
782 Diag(Tok, diag::err_expected_rparen);
783 SkipUntil(tok::r_paren, false); // don't stop at ';'
784 return 0;
785 }
786 rparenLoc = ConsumeParen();
787 return 0;
788 }
789 // We have a class implementation
790 if (Tok.getKind() == tok::colon) {
791 // We have a super class
792 ConsumeToken();
793 if (Tok.getKind() != tok::identifier) {
794 Diag(Tok, diag::err_expected_ident); // missing super class name.
795 return 0;
796 }
797 ConsumeToken(); // Consume super class name
798 }
799 if (Tok.getKind() == tok::l_brace)
Steve Naroff81f1bba2007-09-06 21:24:23 +0000800 ParseObjCClassInstanceVariables(0/*FIXME*/); // we have ivars
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000801
Steve Narofffb367882007-08-20 21:31:48 +0000802 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000803}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000804Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
805 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
806 "ParseObjCAtEndDeclaration(): Expected @end");
807 ConsumeToken(); // the "end" identifier
808
Steve Narofffb367882007-08-20 21:31:48 +0000809 return 0;
810}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000811
812/// compatibility-alias-decl:
813/// @compatibility_alias alias-name class-name ';'
814///
815Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
816 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
817 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
818 ConsumeToken(); // consume compatibility_alias
819 if (Tok.getKind() != tok::identifier) {
820 Diag(Tok, diag::err_expected_ident);
821 return 0;
822 }
823 ConsumeToken(); // consume alias-name
824 if (Tok.getKind() != tok::identifier) {
825 Diag(Tok, diag::err_expected_ident);
826 return 0;
827 }
828 ConsumeToken(); // consume class-name;
829 if (Tok.getKind() != tok::semi)
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000830 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Steve Narofffb367882007-08-20 21:31:48 +0000831 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000832}
833
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000834/// property-synthesis:
835/// @synthesize property-ivar-list ';'
836///
837/// property-ivar-list:
838/// property-ivar
839/// property-ivar-list ',' property-ivar
840///
841/// property-ivar:
842/// identifier
843/// identifier '=' identifier
844///
845Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
846 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
847 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
848 SourceLocation loc = ConsumeToken(); // consume dynamic
849 if (Tok.getKind() != tok::identifier) {
850 Diag(Tok, diag::err_expected_ident);
851 return 0;
852 }
853 while (Tok.getKind() == tok::identifier) {
854 ConsumeToken(); // consume property name
855 if (Tok.getKind() == tok::equal) {
856 // property '=' ivar-name
857 ConsumeToken(); // consume '='
858 if (Tok.getKind() != tok::identifier) {
859 Diag(Tok, diag::err_expected_ident);
860 break;
861 }
862 ConsumeToken(); // consume ivar-name
863 }
864 if (Tok.getKind() != tok::comma)
865 break;
866 ConsumeToken(); // consume ','
867 }
868 if (Tok.getKind() != tok::semi)
869 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
870 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000871}
872
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000873/// property-dynamic:
874/// @dynamic property-list
875///
876/// property-list:
877/// identifier
878/// property-list ',' identifier
879///
880Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
881 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
882 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
883 SourceLocation loc = ConsumeToken(); // consume dynamic
884 if (Tok.getKind() != tok::identifier) {
885 Diag(Tok, diag::err_expected_ident);
886 return 0;
887 }
888 while (Tok.getKind() == tok::identifier) {
889 ConsumeToken(); // consume property name
890 if (Tok.getKind() != tok::comma)
891 break;
892 ConsumeToken(); // consume ','
893 }
894 if (Tok.getKind() != tok::semi)
895 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
896 return 0;
897}
898
Steve Naroff81f1bba2007-09-06 21:24:23 +0000899/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000900///
901void Parser::ParseObjCInstanceMethodDefinition() {
902 assert(Tok.getKind() == tok::minus &&
903 "ParseObjCInstanceMethodDefinition(): Expected '-'");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000904 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000905 // parse optional ';'
906 if (Tok.getKind() == tok::semi)
907 ConsumeToken();
908
909 if (Tok.getKind() != tok::l_brace) {
910 Diag (Tok, diag::err_expected_lbrace);
911 return;
912 }
913
914 StmtResult FnBody = ParseCompoundStatementBody();
915}
916
Steve Naroff81f1bba2007-09-06 21:24:23 +0000917/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000918///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000919void Parser::ParseObjCClassMethodDefinition() {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000920 assert(Tok.getKind() == tok::plus &&
921 "ParseObjCClassMethodDefinition(): Expected '+'");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000922 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000923 // parse optional ';'
924 if (Tok.getKind() == tok::semi)
925 ConsumeToken();
926 if (Tok.getKind() != tok::l_brace) {
927 Diag (Tok, diag::err_expected_lbrace);
928 return;
929 }
930
931 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +0000932}
Anders Carlssona66cad42007-08-21 17:43:55 +0000933
934Parser::ExprResult Parser::ParseObjCExpression() {
935 SourceLocation AtLoc = ConsumeToken(); // the "@"
936
937 switch (Tok.getKind()) {
938 case tok::string_literal: // primary-expression: string-literal
939 case tok::wide_string_literal:
940 return ParseObjCStringLiteral();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000941 default:
942 break;
943 }
944
945 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlsson8be1d402007-08-22 15:14:15 +0000946 case tok::objc_encode:
947 return ParseObjCEncodeExpression();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000948 case tok::objc_protocol:
949 return ParseObjCProtocolExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +0000950 default:
951 Diag(AtLoc, diag::err_unexpected_at);
952 SkipUntil(tok::semi);
953 break;
954 }
955
956 return 0;
957}
958
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000959/// objc-message-expr:
960/// '[' objc-receiver objc-message-args ']'
961///
962/// objc-receiver:
963/// expression
964/// class-name
965/// type-name
966///
967/// objc-message-args:
968/// objc-selector
969/// objc-keywordarg-list
970///
971/// objc-keywordarg-list:
972/// objc-keywordarg
973/// objc-keywordarg-list objc-keywordarg
974///
975/// objc-keywordarg:
976/// selector-name[opt] ':' objc-keywordexpr
977///
978/// objc-keywordexpr:
979/// nonempty-expr-list
980///
981/// nonempty-expr-list:
982/// assignment-expression
983/// nonempty-expr-list , assignment-expression
984///
985Parser::ExprResult Parser::ParseObjCMessageExpression() {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +0000986 assert(Tok.getKind() == tok::l_square && "'[' expected");
987 SourceLocation Loc = ConsumeBracket(); // consume '['
988 // Parse receiver
989 // FIXME: receiver as type-name/class-name
990 ParseAssignmentExpression();
991 // Parse objc-selector
992 IdentifierInfo *selIdent = ParseObjCSelector();
993 if (Tok.getKind() == tok::colon) {
994 while (1) {
995 // Each iteration parses a single keyword argument.
996 if (Tok.getKind() != tok::colon) {
997 Diag(Tok, diag::err_expected_colon);
998 SkipUntil(tok::semi);
999 return 0;
1000 }
1001 ConsumeToken(); // Eat the ':'.
1002 /// Parse the expression after ':'
1003 ParseAssignmentExpression();
1004 IdentifierInfo *keywordSelector = ParseObjCSelector();
1005
1006 if (!keywordSelector && Tok.getKind() != tok::colon)
1007 break;
1008 // We have a selector or a colon, continue parsing.
1009 }
1010 // Parse the, optional, argument list, comma separated.
1011 while (Tok.getKind() == tok::comma) {
1012 ConsumeToken();
1013 /// Parse the expression after ','
1014 ParseAssignmentExpression();
1015 }
1016 } else if (!selIdent) {
1017 Diag(Tok, diag::err_expected_ident); // missing selector name.
1018 SkipUntil(tok::semi);
1019 return 0;
1020 }
1021 if (Tok.getKind() != tok::r_square) {
1022 Diag(Tok, diag::err_expected_rsquare);
1023 SkipUntil(tok::semi);
1024 return 0;
1025 }
1026 ConsumeBracket(); // consume ']'
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001027 return 0;
1028}
1029
Anders Carlssona66cad42007-08-21 17:43:55 +00001030Parser::ExprResult Parser::ParseObjCStringLiteral() {
1031 ExprResult Res = ParseStringLiteralExpression();
1032
1033 if (Res.isInvalid) return Res;
1034
1035 return Actions.ParseObjCStringLiteral(Res.Val);
1036}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001037
1038/// objc-encode-expression:
1039/// @encode ( type-name )
1040Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff87c329f2007-08-23 18:16:40 +00001041 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001042
1043 SourceLocation EncLoc = ConsumeToken();
1044
1045 if (Tok.getKind() != tok::l_paren) {
1046 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1047 return true;
1048 }
1049
1050 SourceLocation LParenLoc = ConsumeParen();
1051
1052 TypeTy *Ty = ParseTypeName();
1053
Anders Carlsson92faeb82007-08-23 15:31:37 +00001054 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001055
1056 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001057 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001058}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001059
1060/// objc-protocol-expression
1061/// @protocol ( protocol-name )
1062
1063Parser::ExprResult Parser::ParseObjCProtocolExpression()
1064{
1065 SourceLocation ProtoLoc = ConsumeToken();
1066
1067 if (Tok.getKind() != tok::l_paren) {
1068 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1069 return true;
1070 }
1071
1072 SourceLocation LParenLoc = ConsumeParen();
1073
1074 if (Tok.getKind() != tok::identifier) {
1075 Diag(Tok, diag::err_expected_ident);
1076 return true;
1077 }
1078
1079 // FIXME: Do something with the protocol name
1080 ConsumeToken();
1081
Anders Carlsson92faeb82007-08-23 15:31:37 +00001082 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001083
1084 // FIXME
1085 return 0;
1086}