blob: 39772a3d6a9701fa88fdf1ddc0c0944a1f3c826d [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:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000040 return 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 Narofffb367882007-08-20 21:31:48 +000083 return Actions.ParsedObjcClassDeclaration(CurScope,
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 Naroff304ed392007-09-05 23:30:30 +0000157 llvm::SmallVector<DeclTy*, 64> MethodDecls;
158 ParseObjCInterfaceDeclList(0/*FIXME*/, MethodDecls);
Steve Narofffb367882007-08-20 21:31:48 +0000159
Steve Naroff0bbffd82007-08-22 16:35:03 +0000160 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000161 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000162 ConsumeToken(); // the "end" identifier
Steve Narofffb367882007-08-20 21:31:48 +0000163 return 0;
164 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000165 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000166 return 0;
167 }
168 // Parse a class interface.
169 IdentifierInfo *superClassId = 0;
170 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000171
172 // FIXME: temporary hack to grok class names (until we have sema support).
173 llvm::SmallVector<IdentifierInfo *, 1> ClassName;
174 ClassName.push_back(nameId);
175 Actions.ParsedObjcClassDeclaration(CurScope, &ClassName[0], 1);
Steve Narofffb367882007-08-20 21:31:48 +0000176
177 if (Tok.getKind() == tok::colon) { // a super class is specified.
178 ConsumeToken();
179 if (Tok.getKind() != tok::identifier) {
180 Diag(Tok, diag::err_expected_ident); // missing super class name.
181 return 0;
182 }
183 superClassId = Tok.getIdentifierInfo();
184 superClassLoc = ConsumeToken();
185 }
186 // Next, we need to check for any protocol references.
Steve Naroff304ed392007-09-05 23:30:30 +0000187 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofffb367882007-08-20 21:31:48 +0000188 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000189 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000190 return 0;
191 }
Steve Naroff304ed392007-09-05 23:30:30 +0000192 DeclTy *ClsType = Actions.ObjcStartClassInterface(atLoc, nameId, nameLoc,
193 superClassId, superClassLoc, &ProtocolRefs[0],
194 ProtocolRefs.size(), attrList);
195
196 llvm::SmallVector<DeclTy*, 32> IvarDecls;
Steve Narofffb367882007-08-20 21:31:48 +0000197 if (Tok.getKind() == tok::l_brace)
Steve Naroff304ed392007-09-05 23:30:30 +0000198 ParseObjCClassInstanceVariables(ClsType, IvarDecls);
Steve Narofffb367882007-08-20 21:31:48 +0000199
Steve Naroff304ed392007-09-05 23:30:30 +0000200 llvm::SmallVector<DeclTy*, 64> MethodDecls;
201 ParseObjCInterfaceDeclList(ClsType, MethodDecls);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000202
203 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000204 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000205 ConsumeToken(); // the "end" identifier
Steve Narofffb367882007-08-20 21:31:48 +0000206 return 0;
207 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000208 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000209 return 0;
210}
211
212/// objc-interface-decl-list:
213/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000214/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000215/// objc-interface-decl-list objc-method-requirement [OBJC2]
216/// objc-interface-decl-list objc-method-proto
Steve Narofffb367882007-08-20 21:31:48 +0000217/// objc-interface-decl-list declaration
218/// objc-interface-decl-list ';'
219///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000220/// objc-method-requirement: [OBJC2]
221/// @required
222/// @optional
223///
Steve Naroff304ed392007-09-05 23:30:30 +0000224void Parser::ParseObjCInterfaceDeclList(
225 DeclTy *interfaceDecl, llvm::SmallVectorImpl<DeclTy*> &MethodDecls) {
226 DeclTy *IDecl = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000227 while (1) {
228 if (Tok.getKind() == tok::at) {
229 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000230 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000231
232 if (ocKind == tok::objc_end) { // terminate list
233 return;
234 } else if (ocKind == tok::objc_required) { // protocols only
235 ConsumeToken();
236 continue;
237 } else if (ocKind == tok::objc_optional) { // protocols only
238 ConsumeToken();
239 continue;
240 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000241 ParseObjCPropertyDecl(0/*FIXME*/);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000242 continue;
243 } else {
244 Diag(Tok, diag::err_objc_illegal_interface_qual);
245 ConsumeToken();
246 }
247 }
248 if (Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) {
Steve Naroff304ed392007-09-05 23:30:30 +0000249 IDecl = ParseObjCMethodPrototype(true);
250 MethodDecls.push_back(IDecl);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000251 continue;
252 }
253 if (Tok.getKind() == tok::semi)
254 ConsumeToken();
255 else if (Tok.getKind() == tok::eof)
256 return;
Steve Naroff304ed392007-09-05 23:30:30 +0000257 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000258 // FIXME: as the name implies, this rule allows function definitions.
259 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff304ed392007-09-05 23:30:30 +0000260 IDecl = ParseDeclarationOrFunctionDefinition();
261 MethodDecls.push_back(IDecl);
262 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000263 }
264}
265
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000266/// Parse property attribute declarations.
267///
268/// property-attr-decl: '(' property-attrlist ')'
269/// property-attrlist:
270/// property-attribute
271/// property-attrlist ',' property-attribute
272/// property-attribute:
273/// getter '=' identifier
274/// setter '=' identifier ':'
275/// readonly
276/// readwrite
277/// assign
278/// retain
279/// copy
280/// nonatomic
281///
282void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
283 SourceLocation loc = ConsumeParen(); // consume '('
284 while (isObjCPropertyAttribute()) {
285 const IdentifierInfo *II = Tok.getIdentifierInfo();
286 // getter/setter require extra treatment.
287 if (II == ObjcPropertyAttrs[objc_getter] ||
288 II == ObjcPropertyAttrs[objc_setter]) {
289 // skip getter/setter part.
290 SourceLocation loc = ConsumeToken();
291 if (Tok.getKind() == tok::equal) {
292 loc = ConsumeToken();
293 if (Tok.getKind() == tok::identifier) {
294 if (II == ObjcPropertyAttrs[objc_setter]) {
295 loc = ConsumeToken(); // consume method name
296 if (Tok.getKind() != tok::colon) {
297 Diag(loc, diag::err_expected_colon);
298 SkipUntil(tok::r_paren,true,true);
299 break;
300 }
301 }
302 }
303 else {
304 Diag(loc, diag::err_expected_ident);
305 SkipUntil(tok::r_paren,true,true);
306 break;
307 }
308 }
309 else {
310 Diag(loc, diag::err_objc_expected_equal);
311 SkipUntil(tok::r_paren,true,true);
312 break;
313 }
314 }
315 ConsumeToken(); // consume last attribute token
316 if (Tok.getKind() == tok::comma) {
317 loc = ConsumeToken();
318 continue;
319 }
320 if (Tok.getKind() == tok::r_paren)
321 break;
322 Diag(loc, diag::err_expected_rparen);
323 SkipUntil(tok::semi);
324 return;
325 }
326 if (Tok.getKind() == tok::r_paren)
327 ConsumeParen();
328 else {
329 Diag(loc, diag::err_objc_expected_property_attr);
330 SkipUntil(tok::r_paren); // recover from error inside attribute list
331 }
332}
333
334/// Main routine to parse property declaration.
335///
336/// @property property-attr-decl[opt] property-component-decl ';'
337///
338void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
339 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
340 "ParseObjCPropertyDecl(): Expected @property");
341 ConsumeToken(); // the "property" identifier
342 // Parse property attribute list, if any.
343 if (Tok.getKind() == tok::l_paren) {
344 // property has attribute list.
345 ParseObjCPropertyAttribute(0/*FIXME*/);
346 }
347 // Parse declaration portion of @property.
348 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
349 ParseStructDeclaration(interfaceDecl, PropertyDecls);
350 if (Tok.getKind() == tok::semi)
351 ConsumeToken();
352 else {
353 Diag(Tok, diag::err_expected_semi_decl_list);
354 SkipUntil(tok::r_brace, true, true);
355 }
Chris Lattner4b009652007-07-25 00:24:17 +0000356}
Steve Narofffb367882007-08-20 21:31:48 +0000357
Steve Naroff0bbffd82007-08-22 16:35:03 +0000358/// objc-methodproto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000359/// objc-instance-method objc-method-decl objc-method-attributes[opt]
360/// ';'[opt]
361/// objc-class-method objc-method-decl objc-method-attributes[opt] ';'[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000362///
363/// objc-instance-method: '-'
364/// objc-class-method: '+'
365///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000366/// objc-method-attributes: [OBJC2]
367/// __attribute__((deprecated))
368///
Steve Naroff304ed392007-09-05 23:30:30 +0000369Parser::DeclTy *Parser::ParseObjCMethodPrototype(bool decl) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000370 assert((Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) &&
371 "expected +/-");
372
373 tok::TokenKind methodType = Tok.getKind();
374 SourceLocation methodLoc = ConsumeToken();
375
376 // FIXME: deal with "context sensitive" protocol qualifiers in prototypes
Steve Naroff304ed392007-09-05 23:30:30 +0000377 DeclTy *MDecl = ParseObjCMethodDecl(methodType, methodLoc);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000378
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000379 if (decl)
380 // Consume the ';'.
381 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "method proto");
Steve Naroff304ed392007-09-05 23:30:30 +0000382 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000383}
384
385/// objc-selector:
386/// identifier
387/// one of
388/// enum struct union if else while do for switch case default
389/// break continue return goto asm sizeof typeof __alignof
390/// unsigned long const short volatile signed restrict _Complex
391/// in out inout bycopy byref oneway int char float double void _Bool
392///
393IdentifierInfo *Parser::ParseObjCSelector() {
394 tok::TokenKind tKind = Tok.getKind();
395 IdentifierInfo *II = 0;
396
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000397 if (tKind == tok::identifier || tKind == tok::kw_typeof ||
398 tKind == tok::kw___alignof ||
Steve Naroff0bbffd82007-08-22 16:35:03 +0000399 (tKind >= tok::kw_auto && tKind <= tok::kw__Complex)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000400 II = Tok.getIdentifierInfo();
401 ConsumeToken();
402 }
403 return II;
404}
405
Steve Naroffa8ee2262007-08-22 23:18:22 +0000406/// objc-type-qualifier: one of
407/// in out inout bycopy byref oneway
408///
Steve Naroffa8ee2262007-08-22 23:18:22 +0000409bool Parser::isObjCTypeQualifier() {
410 if (Tok.getKind() == tok::identifier) {
Chris Lattner32352462007-08-29 22:54:08 +0000411 const IdentifierInfo *II = Tok.getIdentifierInfo();
412 for (unsigned i = 0; i < objc_NumQuals; ++i)
413 if (II == ObjcTypeQuals[i]) return true;
Steve Naroffa8ee2262007-08-22 23:18:22 +0000414 }
415 return false;
416}
417
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000418/// property-attrlist: one of
419/// readonly getter setter assign retain copy nonatomic
420///
421bool Parser::isObjCPropertyAttribute() {
422 if (Tok.getKind() == tok::identifier) {
423 const IdentifierInfo *II = Tok.getIdentifierInfo();
424 for (unsigned i = 0; i < objc_NumAttrs; ++i)
425 if (II == ObjcPropertyAttrs[i]) return true;
426 }
427 return false;
428}
429
Steve Naroff0bbffd82007-08-22 16:35:03 +0000430/// objc-type-name:
431/// '(' objc-type-qualifiers[opt] type-name ')'
432/// '(' objc-type-qualifiers[opt] ')'
433///
434/// objc-type-qualifiers:
435/// objc-type-qualifier
436/// objc-type-qualifiers objc-type-qualifier
437///
Steve Naroff304ed392007-09-05 23:30:30 +0000438Parser::TypeTy *Parser::ParseObjCTypeName() {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000439 assert(Tok.getKind() == tok::l_paren && "expected (");
440
441 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Steve Naroff304ed392007-09-05 23:30:30 +0000442 TypeTy *Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000443
Steve Naroffa8ee2262007-08-22 23:18:22 +0000444 while (isObjCTypeQualifier())
445 ConsumeToken();
446
Steve Naroff0bbffd82007-08-22 16:35:03 +0000447 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000448 Ty = ParseTypeName();
449 // FIXME: back when Sema support is in place...
450 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000451 }
452 if (Tok.getKind() != tok::r_paren) {
453 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000454 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000455 }
456 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000457 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000458}
459
460/// objc-method-decl:
461/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000462/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000463/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000464/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000465///
466/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000467/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000468/// objc-keyword-selector objc-keyword-decl
469///
470/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000471/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
472/// objc-selector ':' objc-keyword-attributes[opt] identifier
473/// ':' objc-type-name objc-keyword-attributes[opt] identifier
474/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000475///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000476/// objc-parmlist:
477/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000478///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000479/// objc-parms:
480/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000481///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000482/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000483/// , ...
484///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000485/// objc-keyword-attributes: [OBJC2]
486/// __attribute__((unused))
487///
Steve Naroff304ed392007-09-05 23:30:30 +0000488Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType, SourceLocation mLoc) {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000489
Steve Naroff304ed392007-09-05 23:30:30 +0000490 TypeTy *ReturnType = 0;
491
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000492 // Parse the return type.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000493 if (Tok.getKind() == tok::l_paren)
Steve Naroff304ed392007-09-05 23:30:30 +0000494 ReturnType = ParseObjCTypeName();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000495 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff304ed392007-09-05 23:30:30 +0000496
497 llvm::SmallVector<ObjcKeywordInfo, 12> KeyInfo;
498 int KeySlot = 0;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000499
500 if (Tok.getKind() == tok::colon) {
Steve Naroff304ed392007-09-05 23:30:30 +0000501
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000502 while (1) {
Steve Naroff304ed392007-09-05 23:30:30 +0000503 KeyInfo[KeySlot].SelectorName = selIdent;
504
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000505 // Each iteration parses a single keyword argument.
506 if (Tok.getKind() != tok::colon) {
507 Diag(Tok, diag::err_expected_colon);
508 break;
509 }
Steve Naroff304ed392007-09-05 23:30:30 +0000510 KeyInfo[KeySlot].ColonLoc = ConsumeToken(); // Eat the ':'.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000511 if (Tok.getKind() == tok::l_paren) // Parse the argument type.
Steve Naroff304ed392007-09-05 23:30:30 +0000512 KeyInfo[KeySlot].TypeInfo = ParseObjCTypeName();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000513
514 // If attributes exist before the argument name, parse them.
Steve Naroffa7f62782007-08-23 19:56:30 +0000515 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
Steve Naroff304ed392007-09-05 23:30:30 +0000516 KeyInfo[KeySlot].AttrList = ParseAttributes();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000517
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000518 if (Tok.getKind() != tok::identifier) {
519 Diag(Tok, diag::err_expected_ident); // missing argument name.
520 break;
521 }
Steve Naroff304ed392007-09-05 23:30:30 +0000522 KeyInfo[KeySlot].ArgumentName = Tok.getIdentifierInfo();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000523 ConsumeToken(); // Eat the identifier.
Steve Naroff304ed392007-09-05 23:30:30 +0000524
525 // Rather than call out to the actions, try packaging up the info
526 // locally, like we do for Declarator.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000527 // FIXME: add Actions.BuildObjCKeyword()
528
Steve Naroff304ed392007-09-05 23:30:30 +0000529 selIdent = ParseObjCSelector();
530 if (!selIdent && Tok.getKind() != tok::colon)
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000531 break;
532 // We have a selector or a colon, continue parsing.
Steve Naroff304ed392007-09-05 23:30:30 +0000533 KeySlot++;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000534 }
535 // Parse the (optional) parameter list.
536 while (Tok.getKind() == tok::comma) {
537 ConsumeToken();
538 if (Tok.getKind() == tok::ellipsis) {
539 ConsumeToken();
540 break;
541 }
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000542 // Parse the c-style argument declaration-specifier.
543 DeclSpec DS;
544 ParseDeclarationSpecifiers(DS);
545 // Parse the declarator.
546 Declarator ParmDecl(DS, Declarator::PrototypeContext);
547 ParseDeclarator(ParmDecl);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000548 }
Steve Naroff304ed392007-09-05 23:30:30 +0000549 AttributeList *methodAttrs = 0;
550 // If attributes exist after the method, parse them.
551 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
552 methodAttrs = ParseAttributes();
553
554 // FIXME: Add support for optional parmameter list...
555 return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType,
556 &KeyInfo[0], KeyInfo.size(),
557 methodAttrs);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000558 } else if (!selIdent) {
559 Diag(Tok, diag::err_expected_ident); // missing selector name.
560 }
Steve Naroff304ed392007-09-05 23:30:30 +0000561 AttributeList *methodAttrs = 0;
562 // If attributes exist after the method, parse them.
563 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
564 methodAttrs = ParseAttributes();
565 return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType, selIdent,
566 methodAttrs);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000567}
568
Steve Narofffb367882007-08-20 21:31:48 +0000569/// objc-protocol-refs:
570/// '<' identifier-list '>'
571///
Steve Naroff304ed392007-09-05 23:30:30 +0000572bool Parser::ParseObjCProtocolReferences(
573 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Steve Narofffb367882007-08-20 21:31:48 +0000574 assert(Tok.getKind() == tok::less && "expected <");
575
576 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000577
578 while (1) {
579 if (Tok.getKind() != tok::identifier) {
580 Diag(Tok, diag::err_expected_ident);
581 SkipUntil(tok::greater);
582 return true;
583 }
584 ProtocolRefs.push_back(Tok.getIdentifierInfo());
585 ConsumeToken();
586
587 if (Tok.getKind() != tok::comma)
588 break;
589 ConsumeToken();
590 }
591 // Consume the '>'.
592 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
593}
594
595/// objc-class-instance-variables:
596/// '{' objc-instance-variable-decl-list[opt] '}'
597///
598/// objc-instance-variable-decl-list:
599/// objc-visibility-spec
600/// objc-instance-variable-decl ';'
601/// ';'
602/// objc-instance-variable-decl-list objc-visibility-spec
603/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
604/// objc-instance-variable-decl-list ';'
605///
606/// objc-visibility-spec:
607/// @private
608/// @protected
609/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000610/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000611///
612/// objc-instance-variable-decl:
613/// struct-declaration
614///
Steve Naroff304ed392007-09-05 23:30:30 +0000615void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
616 llvm::SmallVectorImpl<DeclTy*> &IvarDecls) {
Steve Naroffc4474992007-08-21 21:17:12 +0000617 assert(Tok.getKind() == tok::l_brace && "expected {");
618
619 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000620
621 // While we still have something to read, read the instance variables.
622 while (Tok.getKind() != tok::r_brace &&
623 Tok.getKind() != tok::eof) {
624 // Each iteration of this loop reads one objc-instance-variable-decl.
625
626 // Check for extraneous top-level semicolon.
627 if (Tok.getKind() == tok::semi) {
628 Diag(Tok, diag::ext_extra_struct_semi);
629 ConsumeToken();
630 continue;
631 }
632 // Set the default visibility to private.
633 tok::ObjCKeywordKind visibility = tok::objc_private;
634 if (Tok.getKind() == tok::at) { // parse objc-visibility-spec
635 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000636 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000637 case tok::objc_private:
638 case tok::objc_public:
639 case tok::objc_protected:
640 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000641 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000642 ConsumeToken();
643 continue;
644 default:
645 Diag(Tok, diag::err_objc_illegal_visibility_spec);
646 ConsumeToken();
647 continue;
648 }
649 }
650 ParseStructDeclaration(interfaceDecl, IvarDecls);
651
652 if (Tok.getKind() == tok::semi) {
653 ConsumeToken();
654 } else if (Tok.getKind() == tok::r_brace) {
655 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
656 break;
657 } else {
658 Diag(Tok, diag::err_expected_semi_decl_list);
659 // Skip to end of block or statement
660 SkipUntil(tok::r_brace, true, true);
661 }
662 }
663 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
664 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000665}
Steve Narofffb367882007-08-20 21:31:48 +0000666
667/// objc-protocol-declaration:
668/// objc-protocol-definition
669/// objc-protocol-forward-reference
670///
671/// objc-protocol-definition:
672/// @protocol identifier
673/// objc-protocol-refs[opt]
674/// objc-methodprotolist
675/// @end
676///
677/// objc-protocol-forward-reference:
678/// @protocol identifier-list ';'
679///
680/// "@protocol identifier ;" should be resolved as "@protocol
681/// identifier-list ;": objc-methodprotolist may not start with a
682/// semicolon in the first alternative if objc-protocol-refs are omitted.
683
Steve Naroff72f17fb2007-08-22 22:17:26 +0000684Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000685 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000686 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
687 ConsumeToken(); // the "protocol" identifier
688
689 if (Tok.getKind() != tok::identifier) {
690 Diag(Tok, diag::err_expected_ident); // missing protocol name.
691 return 0;
692 }
693 // Save the protocol name, then consume it.
694 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
695 SourceLocation nameLoc = ConsumeToken();
696
697 if (Tok.getKind() == tok::semi) { // forward declaration.
698 ConsumeToken();
699 return 0; // FIXME: add protocolName
700 }
701 if (Tok.getKind() == tok::comma) { // list of forward declarations.
702 // Parse the list of forward declarations.
703 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
704 ProtocolRefs.push_back(protocolName);
705
706 while (1) {
707 ConsumeToken(); // the ','
708 if (Tok.getKind() != tok::identifier) {
709 Diag(Tok, diag::err_expected_ident);
710 SkipUntil(tok::semi);
711 return 0;
712 }
713 ProtocolRefs.push_back(Tok.getIdentifierInfo());
714 ConsumeToken(); // the identifier
715
716 if (Tok.getKind() != tok::comma)
717 break;
718 }
719 // Consume the ';'.
720 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
721 return 0;
722 return 0; // FIXME
723 }
724 // Last, and definitely not least, parse a protocol declaration.
725 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000726 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
727 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000728 return 0;
729 }
Steve Naroff304ed392007-09-05 23:30:30 +0000730 llvm::SmallVector<DeclTy*, 64> MethodDecls;
731 ParseObjCInterfaceDeclList(0/*FIXME*/, MethodDecls);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000732
733 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000734 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000735 ConsumeToken(); // the "end" identifier
736 return 0;
737 }
738 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000739 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000740}
Steve Narofffb367882007-08-20 21:31:48 +0000741
742/// objc-implementation:
743/// objc-class-implementation-prologue
744/// objc-category-implementation-prologue
745///
746/// objc-class-implementation-prologue:
747/// @implementation identifier objc-superclass[opt]
748/// objc-class-instance-variables[opt]
749///
750/// objc-category-implementation-prologue:
751/// @implementation identifier ( identifier )
752
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000753Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
754 SourceLocation atLoc) {
755 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
756 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
757 ConsumeToken(); // the "implementation" identifier
758
759 if (Tok.getKind() != tok::identifier) {
760 Diag(Tok, diag::err_expected_ident); // missing class or category name.
761 return 0;
762 }
763 // We have a class or category name - consume it.
764 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
765
766 if (Tok.getKind() == tok::l_paren) {
767 // we have a category implementation.
768 SourceLocation lparenLoc = ConsumeParen();
769 SourceLocation categoryLoc, rparenLoc;
770 IdentifierInfo *categoryId = 0;
771
772 if (Tok.getKind() == tok::identifier) {
773 categoryId = Tok.getIdentifierInfo();
774 categoryLoc = ConsumeToken();
775 } else {
776 Diag(Tok, diag::err_expected_ident); // missing category name.
777 return 0;
778 }
779 if (Tok.getKind() != tok::r_paren) {
780 Diag(Tok, diag::err_expected_rparen);
781 SkipUntil(tok::r_paren, false); // don't stop at ';'
782 return 0;
783 }
784 rparenLoc = ConsumeParen();
785 return 0;
786 }
787 // We have a class implementation
788 if (Tok.getKind() == tok::colon) {
789 // We have a super class
790 ConsumeToken();
791 if (Tok.getKind() != tok::identifier) {
792 Diag(Tok, diag::err_expected_ident); // missing super class name.
793 return 0;
794 }
795 ConsumeToken(); // Consume super class name
796 }
Steve Naroff304ed392007-09-05 23:30:30 +0000797 llvm::SmallVector<DeclTy*, 32> IvarDecls;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000798 if (Tok.getKind() == tok::l_brace)
Steve Naroff304ed392007-09-05 23:30:30 +0000799 ParseObjCClassInstanceVariables(0/*FIXME*/, IvarDecls); // we have ivars
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000800
Steve Narofffb367882007-08-20 21:31:48 +0000801 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000802}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000803Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
804 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
805 "ParseObjCAtEndDeclaration(): Expected @end");
806 ConsumeToken(); // the "end" identifier
807
Steve Narofffb367882007-08-20 21:31:48 +0000808 return 0;
809}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000810
811/// compatibility-alias-decl:
812/// @compatibility_alias alias-name class-name ';'
813///
814Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
815 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
816 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
817 ConsumeToken(); // consume compatibility_alias
818 if (Tok.getKind() != tok::identifier) {
819 Diag(Tok, diag::err_expected_ident);
820 return 0;
821 }
822 ConsumeToken(); // consume alias-name
823 if (Tok.getKind() != tok::identifier) {
824 Diag(Tok, diag::err_expected_ident);
825 return 0;
826 }
827 ConsumeToken(); // consume class-name;
828 if (Tok.getKind() != tok::semi)
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000829 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Steve Narofffb367882007-08-20 21:31:48 +0000830 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000831}
832
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000833/// property-synthesis:
834/// @synthesize property-ivar-list ';'
835///
836/// property-ivar-list:
837/// property-ivar
838/// property-ivar-list ',' property-ivar
839///
840/// property-ivar:
841/// identifier
842/// identifier '=' identifier
843///
844Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
845 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
846 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
847 SourceLocation loc = ConsumeToken(); // consume dynamic
848 if (Tok.getKind() != tok::identifier) {
849 Diag(Tok, diag::err_expected_ident);
850 return 0;
851 }
852 while (Tok.getKind() == tok::identifier) {
853 ConsumeToken(); // consume property name
854 if (Tok.getKind() == tok::equal) {
855 // property '=' ivar-name
856 ConsumeToken(); // consume '='
857 if (Tok.getKind() != tok::identifier) {
858 Diag(Tok, diag::err_expected_ident);
859 break;
860 }
861 ConsumeToken(); // consume ivar-name
862 }
863 if (Tok.getKind() != tok::comma)
864 break;
865 ConsumeToken(); // consume ','
866 }
867 if (Tok.getKind() != tok::semi)
868 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
869 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000870}
871
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000872/// property-dynamic:
873/// @dynamic property-list
874///
875/// property-list:
876/// identifier
877/// property-list ',' identifier
878///
879Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
880 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
881 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
882 SourceLocation loc = ConsumeToken(); // consume dynamic
883 if (Tok.getKind() != tok::identifier) {
884 Diag(Tok, diag::err_expected_ident);
885 return 0;
886 }
887 while (Tok.getKind() == tok::identifier) {
888 ConsumeToken(); // consume property name
889 if (Tok.getKind() != tok::comma)
890 break;
891 ConsumeToken(); // consume ','
892 }
893 if (Tok.getKind() != tok::semi)
894 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
895 return 0;
896}
897
898/// objc-method-def: objc-methodproto ';'[opt] '{' body '}'
899///
900void Parser::ParseObjCInstanceMethodDefinition() {
901 assert(Tok.getKind() == tok::minus &&
902 "ParseObjCInstanceMethodDefinition(): Expected '-'");
903 ParseObjCMethodPrototype(false);
904 // parse optional ';'
905 if (Tok.getKind() == tok::semi)
906 ConsumeToken();
907
908 if (Tok.getKind() != tok::l_brace) {
909 Diag (Tok, diag::err_expected_lbrace);
910 return;
911 }
912
913 StmtResult FnBody = ParseCompoundStatementBody();
914}
915
916/// objc-method-def: objc-methodproto ';'[opt] '{' body '}'
917///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000918void Parser::ParseObjCClassMethodDefinition() {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000919 assert(Tok.getKind() == tok::plus &&
920 "ParseObjCClassMethodDefinition(): Expected '+'");
921 ParseObjCMethodPrototype(false);
922 // parse optional ';'
923 if (Tok.getKind() == tok::semi)
924 ConsumeToken();
925 if (Tok.getKind() != tok::l_brace) {
926 Diag (Tok, diag::err_expected_lbrace);
927 return;
928 }
929
930 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +0000931}
Anders Carlssona66cad42007-08-21 17:43:55 +0000932
933Parser::ExprResult Parser::ParseObjCExpression() {
934 SourceLocation AtLoc = ConsumeToken(); // the "@"
935
936 switch (Tok.getKind()) {
937 case tok::string_literal: // primary-expression: string-literal
938 case tok::wide_string_literal:
939 return ParseObjCStringLiteral();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000940 default:
941 break;
942 }
943
944 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlsson8be1d402007-08-22 15:14:15 +0000945 case tok::objc_encode:
946 return ParseObjCEncodeExpression();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000947 case tok::objc_protocol:
948 return ParseObjCProtocolExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +0000949 default:
950 Diag(AtLoc, diag::err_unexpected_at);
951 SkipUntil(tok::semi);
952 break;
953 }
954
955 return 0;
956}
957
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000958/// objc-message-expr:
959/// '[' objc-receiver objc-message-args ']'
960///
961/// objc-receiver:
962/// expression
963/// class-name
964/// type-name
965///
966/// objc-message-args:
967/// objc-selector
968/// objc-keywordarg-list
969///
970/// objc-keywordarg-list:
971/// objc-keywordarg
972/// objc-keywordarg-list objc-keywordarg
973///
974/// objc-keywordarg:
975/// selector-name[opt] ':' objc-keywordexpr
976///
977/// objc-keywordexpr:
978/// nonempty-expr-list
979///
980/// nonempty-expr-list:
981/// assignment-expression
982/// nonempty-expr-list , assignment-expression
983///
984Parser::ExprResult Parser::ParseObjCMessageExpression() {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +0000985 assert(Tok.getKind() == tok::l_square && "'[' expected");
986 SourceLocation Loc = ConsumeBracket(); // consume '['
987 // Parse receiver
988 // FIXME: receiver as type-name/class-name
989 ParseAssignmentExpression();
990 // Parse objc-selector
991 IdentifierInfo *selIdent = ParseObjCSelector();
992 if (Tok.getKind() == tok::colon) {
993 while (1) {
994 // Each iteration parses a single keyword argument.
995 if (Tok.getKind() != tok::colon) {
996 Diag(Tok, diag::err_expected_colon);
997 SkipUntil(tok::semi);
998 return 0;
999 }
1000 ConsumeToken(); // Eat the ':'.
1001 /// Parse the expression after ':'
1002 ParseAssignmentExpression();
1003 IdentifierInfo *keywordSelector = ParseObjCSelector();
1004
1005 if (!keywordSelector && Tok.getKind() != tok::colon)
1006 break;
1007 // We have a selector or a colon, continue parsing.
1008 }
1009 // Parse the, optional, argument list, comma separated.
1010 while (Tok.getKind() == tok::comma) {
1011 ConsumeToken();
1012 /// Parse the expression after ','
1013 ParseAssignmentExpression();
1014 }
1015 } else if (!selIdent) {
1016 Diag(Tok, diag::err_expected_ident); // missing selector name.
1017 SkipUntil(tok::semi);
1018 return 0;
1019 }
1020 if (Tok.getKind() != tok::r_square) {
1021 Diag(Tok, diag::err_expected_rsquare);
1022 SkipUntil(tok::semi);
1023 return 0;
1024 }
1025 ConsumeBracket(); // consume ']'
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001026 return 0;
1027}
1028
Anders Carlssona66cad42007-08-21 17:43:55 +00001029Parser::ExprResult Parser::ParseObjCStringLiteral() {
1030 ExprResult Res = ParseStringLiteralExpression();
1031
1032 if (Res.isInvalid) return Res;
1033
1034 return Actions.ParseObjCStringLiteral(Res.Val);
1035}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001036
1037/// objc-encode-expression:
1038/// @encode ( type-name )
1039Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff87c329f2007-08-23 18:16:40 +00001040 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001041
1042 SourceLocation EncLoc = ConsumeToken();
1043
1044 if (Tok.getKind() != tok::l_paren) {
1045 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1046 return true;
1047 }
1048
1049 SourceLocation LParenLoc = ConsumeParen();
1050
1051 TypeTy *Ty = ParseTypeName();
1052
Anders Carlsson92faeb82007-08-23 15:31:37 +00001053 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001054
1055 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001056 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001057}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001058
1059/// objc-protocol-expression
1060/// @protocol ( protocol-name )
1061
1062Parser::ExprResult Parser::ParseObjCProtocolExpression()
1063{
1064 SourceLocation ProtoLoc = ConsumeToken();
1065
1066 if (Tok.getKind() != tok::l_paren) {
1067 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1068 return true;
1069 }
1070
1071 SourceLocation LParenLoc = ConsumeParen();
1072
1073 if (Tok.getKind() != tok::identifier) {
1074 Diag(Tok, diag::err_expected_ident);
1075 return true;
1076 }
1077
1078 // FIXME: Do something with the protocol name
1079 ConsumeToken();
1080
Anders Carlsson92faeb82007-08-23 15:31:37 +00001081 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001082
1083 // FIXME
1084 return 0;
1085}