blob: 62149a040ab1bbcb7c8f92296d8e91599d38ce3b [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) {
150 if (ParseObjCProtocolReferences())
151 return 0;
152 }
153 if (attrList) // categories don't support attributes.
154 Diag(Tok, diag::err_objc_no_attributes_on_category);
155
Steve Naroff0bbffd82007-08-22 16:35:03 +0000156 ParseObjCInterfaceDeclList(0/*FIXME*/);
Steve Narofffb367882007-08-20 21:31:48 +0000157
Steve Naroff0bbffd82007-08-22 16:35:03 +0000158 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000159 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000160 ConsumeToken(); // the "end" identifier
Steve Narofffb367882007-08-20 21:31:48 +0000161 return 0;
162 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000163 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000164 return 0;
165 }
166 // Parse a class interface.
167 IdentifierInfo *superClassId = 0;
168 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000169
170 // FIXME: temporary hack to grok class names (until we have sema support).
171 llvm::SmallVector<IdentifierInfo *, 1> ClassName;
172 ClassName.push_back(nameId);
173 Actions.ParsedObjcClassDeclaration(CurScope, &ClassName[0], 1);
Steve Narofffb367882007-08-20 21:31:48 +0000174
175 if (Tok.getKind() == tok::colon) { // a super class is specified.
176 ConsumeToken();
177 if (Tok.getKind() != tok::identifier) {
178 Diag(Tok, diag::err_expected_ident); // missing super class name.
179 return 0;
180 }
181 superClassId = Tok.getIdentifierInfo();
182 superClassLoc = ConsumeToken();
183 }
184 // Next, we need to check for any protocol references.
185 if (Tok.getKind() == tok::less) {
186 if (ParseObjCProtocolReferences())
187 return 0;
188 }
Steve Naroffc4474992007-08-21 21:17:12 +0000189 // FIXME: add Actions.StartObjCClassInterface(nameId, superClassId, ...)
Steve Narofffb367882007-08-20 21:31:48 +0000190 if (Tok.getKind() == tok::l_brace)
Steve Naroffc4474992007-08-21 21:17:12 +0000191 ParseObjCClassInstanceVariables(0/*FIXME*/);
Steve Narofffb367882007-08-20 21:31:48 +0000192
Steve Naroff0bbffd82007-08-22 16:35:03 +0000193 ParseObjCInterfaceDeclList(0/*FIXME*/);
194
195 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000196 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000197 ConsumeToken(); // the "end" identifier
Steve Narofffb367882007-08-20 21:31:48 +0000198 return 0;
199 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000200 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000201 return 0;
202}
203
204/// objc-interface-decl-list:
205/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000206/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000207/// objc-interface-decl-list objc-method-requirement [OBJC2]
208/// 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///
216void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl) {
217 while (1) {
218 if (Tok.getKind() == tok::at) {
219 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000220 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000221
222 if (ocKind == tok::objc_end) { // terminate list
223 return;
224 } else if (ocKind == tok::objc_required) { // protocols only
225 ConsumeToken();
226 continue;
227 } else if (ocKind == tok::objc_optional) { // protocols only
228 ConsumeToken();
229 continue;
230 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000231 ParseObjCPropertyDecl(0/*FIXME*/);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000232 continue;
233 } else {
234 Diag(Tok, diag::err_objc_illegal_interface_qual);
235 ConsumeToken();
236 }
237 }
238 if (Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000239 ParseObjCMethodPrototype(true);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000240 continue;
241 }
242 if (Tok.getKind() == tok::semi)
243 ConsumeToken();
244 else if (Tok.getKind() == tok::eof)
245 return;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000246 else
247 // FIXME: as the name implies, this rule allows function definitions.
248 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000249 ParseDeclarationOrFunctionDefinition();
250 }
251}
252
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000253/// Parse property attribute declarations.
254///
255/// property-attr-decl: '(' property-attrlist ')'
256/// property-attrlist:
257/// property-attribute
258/// property-attrlist ',' property-attribute
259/// property-attribute:
260/// getter '=' identifier
261/// setter '=' identifier ':'
262/// readonly
263/// readwrite
264/// assign
265/// retain
266/// copy
267/// nonatomic
268///
269void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
270 SourceLocation loc = ConsumeParen(); // consume '('
271 while (isObjCPropertyAttribute()) {
272 const IdentifierInfo *II = Tok.getIdentifierInfo();
273 // getter/setter require extra treatment.
274 if (II == ObjcPropertyAttrs[objc_getter] ||
275 II == ObjcPropertyAttrs[objc_setter]) {
276 // skip getter/setter part.
277 SourceLocation loc = ConsumeToken();
278 if (Tok.getKind() == tok::equal) {
279 loc = ConsumeToken();
280 if (Tok.getKind() == tok::identifier) {
281 if (II == ObjcPropertyAttrs[objc_setter]) {
282 loc = ConsumeToken(); // consume method name
283 if (Tok.getKind() != tok::colon) {
284 Diag(loc, diag::err_expected_colon);
285 SkipUntil(tok::r_paren,true,true);
286 break;
287 }
288 }
289 }
290 else {
291 Diag(loc, diag::err_expected_ident);
292 SkipUntil(tok::r_paren,true,true);
293 break;
294 }
295 }
296 else {
297 Diag(loc, diag::err_objc_expected_equal);
298 SkipUntil(tok::r_paren,true,true);
299 break;
300 }
301 }
302 ConsumeToken(); // consume last attribute token
303 if (Tok.getKind() == tok::comma) {
304 loc = ConsumeToken();
305 continue;
306 }
307 if (Tok.getKind() == tok::r_paren)
308 break;
309 Diag(loc, diag::err_expected_rparen);
310 SkipUntil(tok::semi);
311 return;
312 }
313 if (Tok.getKind() == tok::r_paren)
314 ConsumeParen();
315 else {
316 Diag(loc, diag::err_objc_expected_property_attr);
317 SkipUntil(tok::r_paren); // recover from error inside attribute list
318 }
319}
320
321/// Main routine to parse property declaration.
322///
323/// @property property-attr-decl[opt] property-component-decl ';'
324///
325void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
326 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
327 "ParseObjCPropertyDecl(): Expected @property");
328 ConsumeToken(); // the "property" identifier
329 // Parse property attribute list, if any.
330 if (Tok.getKind() == tok::l_paren) {
331 // property has attribute list.
332 ParseObjCPropertyAttribute(0/*FIXME*/);
333 }
334 // Parse declaration portion of @property.
335 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
336 ParseStructDeclaration(interfaceDecl, PropertyDecls);
337 if (Tok.getKind() == tok::semi)
338 ConsumeToken();
339 else {
340 Diag(Tok, diag::err_expected_semi_decl_list);
341 SkipUntil(tok::r_brace, true, true);
342 }
Chris Lattner4b009652007-07-25 00:24:17 +0000343}
Steve Narofffb367882007-08-20 21:31:48 +0000344
Steve Naroff0bbffd82007-08-22 16:35:03 +0000345/// objc-methodproto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000346/// objc-instance-method objc-method-decl objc-method-attributes[opt]
347/// ';'[opt]
348/// objc-class-method objc-method-decl objc-method-attributes[opt] ';'[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000349///
350/// objc-instance-method: '-'
351/// objc-class-method: '+'
352///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000353/// objc-method-attributes: [OBJC2]
354/// __attribute__((deprecated))
355///
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000356void Parser::ParseObjCMethodPrototype(bool decl) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000357 assert((Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) &&
358 "expected +/-");
359
360 tok::TokenKind methodType = Tok.getKind();
361 SourceLocation methodLoc = ConsumeToken();
362
363 // FIXME: deal with "context sensitive" protocol qualifiers in prototypes
364 ParseObjCMethodDecl(methodType, methodLoc);
365
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000366 // If attributes exist after the method, parse them.
Steve Naroffa7f62782007-08-23 19:56:30 +0000367 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000368 ParseAttributes();
369
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000370 if (decl)
371 // Consume the ';'.
372 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000373}
374
375/// objc-selector:
376/// identifier
377/// one of
378/// enum struct union if else while do for switch case default
379/// break continue return goto asm sizeof typeof __alignof
380/// unsigned long const short volatile signed restrict _Complex
381/// in out inout bycopy byref oneway int char float double void _Bool
382///
383IdentifierInfo *Parser::ParseObjCSelector() {
384 tok::TokenKind tKind = Tok.getKind();
385 IdentifierInfo *II = 0;
386
387 if (tKind == tok::identifier ||
388 (tKind >= tok::kw_auto && tKind <= tok::kw__Complex)) {
389 // FIXME: make sure the list of keywords jives with gcc. For example,
390 // the above test does not include in/out/inout/bycopy/byref/oneway.
391 II = Tok.getIdentifierInfo();
392 ConsumeToken();
393 }
394 return II;
395}
396
Steve Naroffa8ee2262007-08-22 23:18:22 +0000397/// objc-type-qualifier: one of
398/// in out inout bycopy byref oneway
399///
Steve Naroffa8ee2262007-08-22 23:18:22 +0000400bool Parser::isObjCTypeQualifier() {
401 if (Tok.getKind() == tok::identifier) {
Chris Lattner32352462007-08-29 22:54:08 +0000402 const IdentifierInfo *II = Tok.getIdentifierInfo();
403 for (unsigned i = 0; i < objc_NumQuals; ++i)
404 if (II == ObjcTypeQuals[i]) return true;
Steve Naroffa8ee2262007-08-22 23:18:22 +0000405 }
406 return false;
407}
408
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000409/// property-attrlist: one of
410/// readonly getter setter assign retain copy nonatomic
411///
412bool Parser::isObjCPropertyAttribute() {
413 if (Tok.getKind() == tok::identifier) {
414 const IdentifierInfo *II = Tok.getIdentifierInfo();
415 for (unsigned i = 0; i < objc_NumAttrs; ++i)
416 if (II == ObjcPropertyAttrs[i]) return true;
417 }
418 return false;
419}
420
Steve Naroff0bbffd82007-08-22 16:35:03 +0000421/// objc-type-name:
422/// '(' objc-type-qualifiers[opt] type-name ')'
423/// '(' objc-type-qualifiers[opt] ')'
424///
425/// objc-type-qualifiers:
426/// objc-type-qualifier
427/// objc-type-qualifiers objc-type-qualifier
428///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000429void Parser::ParseObjCTypeName() {
430 assert(Tok.getKind() == tok::l_paren && "expected (");
431
432 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
433
Steve Naroffa8ee2262007-08-22 23:18:22 +0000434 while (isObjCTypeQualifier())
435 ConsumeToken();
436
Steve Naroff0bbffd82007-08-22 16:35:03 +0000437 if (isTypeSpecifierQualifier()) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000438 //TypeTy *Ty = ParseTypeName();
439 //assert(Ty && "Parser::ParseObjCTypeName(): missing type");
440 ParseTypeName(); // FIXME: when sema support is added.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000441 }
442 if (Tok.getKind() != tok::r_paren) {
443 MatchRHSPunctuation(tok::r_paren, LParenLoc);
444 return;
445 }
446 RParenLoc = ConsumeParen();
447}
448
449/// objc-method-decl:
450/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000451/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000452/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000453/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000454///
455/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000456/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000457/// objc-keyword-selector objc-keyword-decl
458///
459/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000460/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
461/// objc-selector ':' objc-keyword-attributes[opt] identifier
462/// ':' objc-type-name objc-keyword-attributes[opt] identifier
463/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000464///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000465/// objc-parmlist:
466/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000467///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000468/// objc-parms:
469/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000470///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000471/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000472/// , ...
473///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000474/// objc-keyword-attributes: [OBJC2]
475/// __attribute__((unused))
476///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000477void Parser::ParseObjCMethodDecl(tok::TokenKind mType, SourceLocation mLoc) {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000478
479 // Parse the return type.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000480 if (Tok.getKind() == tok::l_paren)
481 ParseObjCTypeName();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000482 IdentifierInfo *selIdent = ParseObjCSelector();
483
484 if (Tok.getKind() == tok::colon) {
485 IdentifierInfo *keywordSelector = selIdent;
486 while (1) {
487 // Each iteration parses a single keyword argument.
488 if (Tok.getKind() != tok::colon) {
489 Diag(Tok, diag::err_expected_colon);
490 break;
491 }
492 ConsumeToken(); // Eat the ':'.
493 if (Tok.getKind() == tok::l_paren) // Parse the argument type.
494 ParseObjCTypeName();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000495
496 // If attributes exist before the argument name, parse them.
Steve Naroffa7f62782007-08-23 19:56:30 +0000497 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
Steve Naroff72f17fb2007-08-22 22:17:26 +0000498 ParseAttributes();
499
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000500 if (Tok.getKind() != tok::identifier) {
501 Diag(Tok, diag::err_expected_ident); // missing argument name.
502 break;
503 }
504 ConsumeToken(); // Eat the identifier.
505 // FIXME: add Actions.BuildObjCKeyword()
506
507 keywordSelector = ParseObjCSelector();
508 if (!keywordSelector && Tok.getKind() != tok::colon)
509 break;
510 // We have a selector or a colon, continue parsing.
511 }
512 // Parse the (optional) parameter list.
513 while (Tok.getKind() == tok::comma) {
514 ConsumeToken();
515 if (Tok.getKind() == tok::ellipsis) {
516 ConsumeToken();
517 break;
518 }
519 ParseDeclaration(Declarator::PrototypeContext);
520 }
521 } else if (!selIdent) {
522 Diag(Tok, diag::err_expected_ident); // missing selector name.
523 }
524 // FIXME: add Actions.BuildMethodSignature().
Steve Naroff0bbffd82007-08-22 16:35:03 +0000525}
526
Steve Narofffb367882007-08-20 21:31:48 +0000527/// objc-protocol-refs:
528/// '<' identifier-list '>'
529///
530bool Parser::ParseObjCProtocolReferences() {
531 assert(Tok.getKind() == tok::less && "expected <");
532
533 ConsumeToken(); // the "<"
534 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
535
536 while (1) {
537 if (Tok.getKind() != tok::identifier) {
538 Diag(Tok, diag::err_expected_ident);
539 SkipUntil(tok::greater);
540 return true;
541 }
542 ProtocolRefs.push_back(Tok.getIdentifierInfo());
543 ConsumeToken();
544
545 if (Tok.getKind() != tok::comma)
546 break;
547 ConsumeToken();
548 }
549 // Consume the '>'.
550 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
551}
552
553/// objc-class-instance-variables:
554/// '{' objc-instance-variable-decl-list[opt] '}'
555///
556/// objc-instance-variable-decl-list:
557/// objc-visibility-spec
558/// objc-instance-variable-decl ';'
559/// ';'
560/// objc-instance-variable-decl-list objc-visibility-spec
561/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
562/// objc-instance-variable-decl-list ';'
563///
564/// objc-visibility-spec:
565/// @private
566/// @protected
567/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000568/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000569///
570/// objc-instance-variable-decl:
571/// struct-declaration
572///
Steve Naroffc4474992007-08-21 21:17:12 +0000573void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
574 assert(Tok.getKind() == tok::l_brace && "expected {");
575
576 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
577 llvm::SmallVector<DeclTy*, 32> IvarDecls;
578
579 // While we still have something to read, read the instance variables.
580 while (Tok.getKind() != tok::r_brace &&
581 Tok.getKind() != tok::eof) {
582 // Each iteration of this loop reads one objc-instance-variable-decl.
583
584 // Check for extraneous top-level semicolon.
585 if (Tok.getKind() == tok::semi) {
586 Diag(Tok, diag::ext_extra_struct_semi);
587 ConsumeToken();
588 continue;
589 }
590 // Set the default visibility to private.
591 tok::ObjCKeywordKind visibility = tok::objc_private;
592 if (Tok.getKind() == tok::at) { // parse objc-visibility-spec
593 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000594 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000595 case tok::objc_private:
596 case tok::objc_public:
597 case tok::objc_protected:
598 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000599 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000600 ConsumeToken();
601 continue;
602 default:
603 Diag(Tok, diag::err_objc_illegal_visibility_spec);
604 ConsumeToken();
605 continue;
606 }
607 }
608 ParseStructDeclaration(interfaceDecl, IvarDecls);
609
610 if (Tok.getKind() == tok::semi) {
611 ConsumeToken();
612 } else if (Tok.getKind() == tok::r_brace) {
613 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
614 break;
615 } else {
616 Diag(Tok, diag::err_expected_semi_decl_list);
617 // Skip to end of block or statement
618 SkipUntil(tok::r_brace, true, true);
619 }
620 }
621 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
622 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000623}
Steve Narofffb367882007-08-20 21:31:48 +0000624
625/// objc-protocol-declaration:
626/// objc-protocol-definition
627/// objc-protocol-forward-reference
628///
629/// objc-protocol-definition:
630/// @protocol identifier
631/// objc-protocol-refs[opt]
632/// objc-methodprotolist
633/// @end
634///
635/// objc-protocol-forward-reference:
636/// @protocol identifier-list ';'
637///
638/// "@protocol identifier ;" should be resolved as "@protocol
639/// identifier-list ;": objc-methodprotolist may not start with a
640/// semicolon in the first alternative if objc-protocol-refs are omitted.
641
Steve Naroff72f17fb2007-08-22 22:17:26 +0000642Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000643 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000644 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
645 ConsumeToken(); // the "protocol" identifier
646
647 if (Tok.getKind() != tok::identifier) {
648 Diag(Tok, diag::err_expected_ident); // missing protocol name.
649 return 0;
650 }
651 // Save the protocol name, then consume it.
652 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
653 SourceLocation nameLoc = ConsumeToken();
654
655 if (Tok.getKind() == tok::semi) { // forward declaration.
656 ConsumeToken();
657 return 0; // FIXME: add protocolName
658 }
659 if (Tok.getKind() == tok::comma) { // list of forward declarations.
660 // Parse the list of forward declarations.
661 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
662 ProtocolRefs.push_back(protocolName);
663
664 while (1) {
665 ConsumeToken(); // the ','
666 if (Tok.getKind() != tok::identifier) {
667 Diag(Tok, diag::err_expected_ident);
668 SkipUntil(tok::semi);
669 return 0;
670 }
671 ProtocolRefs.push_back(Tok.getIdentifierInfo());
672 ConsumeToken(); // the identifier
673
674 if (Tok.getKind() != tok::comma)
675 break;
676 }
677 // Consume the ';'.
678 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
679 return 0;
680 return 0; // FIXME
681 }
682 // Last, and definitely not least, parse a protocol declaration.
683 if (Tok.getKind() == tok::less) {
684 if (ParseObjCProtocolReferences())
685 return 0;
686 }
687 ParseObjCInterfaceDeclList(0/*FIXME*/);
688
689 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000690 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000691 ConsumeToken(); // the "end" identifier
692 return 0;
693 }
694 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000695 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000696}
Steve Narofffb367882007-08-20 21:31:48 +0000697
698/// objc-implementation:
699/// objc-class-implementation-prologue
700/// objc-category-implementation-prologue
701///
702/// objc-class-implementation-prologue:
703/// @implementation identifier objc-superclass[opt]
704/// objc-class-instance-variables[opt]
705///
706/// objc-category-implementation-prologue:
707/// @implementation identifier ( identifier )
708
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000709Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
710 SourceLocation atLoc) {
711 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
712 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
713 ConsumeToken(); // the "implementation" identifier
714
715 if (Tok.getKind() != tok::identifier) {
716 Diag(Tok, diag::err_expected_ident); // missing class or category name.
717 return 0;
718 }
719 // We have a class or category name - consume it.
720 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
721
722 if (Tok.getKind() == tok::l_paren) {
723 // we have a category implementation.
724 SourceLocation lparenLoc = ConsumeParen();
725 SourceLocation categoryLoc, rparenLoc;
726 IdentifierInfo *categoryId = 0;
727
728 if (Tok.getKind() == tok::identifier) {
729 categoryId = Tok.getIdentifierInfo();
730 categoryLoc = ConsumeToken();
731 } else {
732 Diag(Tok, diag::err_expected_ident); // missing category name.
733 return 0;
734 }
735 if (Tok.getKind() != tok::r_paren) {
736 Diag(Tok, diag::err_expected_rparen);
737 SkipUntil(tok::r_paren, false); // don't stop at ';'
738 return 0;
739 }
740 rparenLoc = ConsumeParen();
741 return 0;
742 }
743 // We have a class implementation
744 if (Tok.getKind() == tok::colon) {
745 // We have a super class
746 ConsumeToken();
747 if (Tok.getKind() != tok::identifier) {
748 Diag(Tok, diag::err_expected_ident); // missing super class name.
749 return 0;
750 }
751 ConsumeToken(); // Consume super class name
752 }
753 if (Tok.getKind() == tok::l_brace)
754 ParseObjCClassInstanceVariables(0/*FIXME*/); // we have ivars
755
Steve Narofffb367882007-08-20 21:31:48 +0000756 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000757}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000758Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
759 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
760 "ParseObjCAtEndDeclaration(): Expected @end");
761 ConsumeToken(); // the "end" identifier
762
Steve Narofffb367882007-08-20 21:31:48 +0000763 return 0;
764}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000765
766/// compatibility-alias-decl:
767/// @compatibility_alias alias-name class-name ';'
768///
769Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
770 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
771 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
772 ConsumeToken(); // consume compatibility_alias
773 if (Tok.getKind() != tok::identifier) {
774 Diag(Tok, diag::err_expected_ident);
775 return 0;
776 }
777 ConsumeToken(); // consume alias-name
778 if (Tok.getKind() != tok::identifier) {
779 Diag(Tok, diag::err_expected_ident);
780 return 0;
781 }
782 ConsumeToken(); // consume class-name;
783 if (Tok.getKind() != tok::semi)
784 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
Steve Narofffb367882007-08-20 21:31:48 +0000785 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000786}
787
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000788/// property-synthesis:
789/// @synthesize property-ivar-list ';'
790///
791/// property-ivar-list:
792/// property-ivar
793/// property-ivar-list ',' property-ivar
794///
795/// property-ivar:
796/// identifier
797/// identifier '=' identifier
798///
799Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
800 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
801 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
802 SourceLocation loc = ConsumeToken(); // consume dynamic
803 if (Tok.getKind() != tok::identifier) {
804 Diag(Tok, diag::err_expected_ident);
805 return 0;
806 }
807 while (Tok.getKind() == tok::identifier) {
808 ConsumeToken(); // consume property name
809 if (Tok.getKind() == tok::equal) {
810 // property '=' ivar-name
811 ConsumeToken(); // consume '='
812 if (Tok.getKind() != tok::identifier) {
813 Diag(Tok, diag::err_expected_ident);
814 break;
815 }
816 ConsumeToken(); // consume ivar-name
817 }
818 if (Tok.getKind() != tok::comma)
819 break;
820 ConsumeToken(); // consume ','
821 }
822 if (Tok.getKind() != tok::semi)
823 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
824 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000825}
826
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000827/// property-dynamic:
828/// @dynamic property-list
829///
830/// property-list:
831/// identifier
832/// property-list ',' identifier
833///
834Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
835 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
836 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
837 SourceLocation loc = ConsumeToken(); // consume dynamic
838 if (Tok.getKind() != tok::identifier) {
839 Diag(Tok, diag::err_expected_ident);
840 return 0;
841 }
842 while (Tok.getKind() == tok::identifier) {
843 ConsumeToken(); // consume property name
844 if (Tok.getKind() != tok::comma)
845 break;
846 ConsumeToken(); // consume ','
847 }
848 if (Tok.getKind() != tok::semi)
849 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
850 return 0;
851}
852
853/// objc-method-def: objc-methodproto ';'[opt] '{' body '}'
854///
855void Parser::ParseObjCInstanceMethodDefinition() {
856 assert(Tok.getKind() == tok::minus &&
857 "ParseObjCInstanceMethodDefinition(): Expected '-'");
858 ParseObjCMethodPrototype(false);
859 // parse optional ';'
860 if (Tok.getKind() == tok::semi)
861 ConsumeToken();
862
863 if (Tok.getKind() != tok::l_brace) {
864 Diag (Tok, diag::err_expected_lbrace);
865 return;
866 }
867
868 StmtResult FnBody = ParseCompoundStatementBody();
869}
870
871/// objc-method-def: objc-methodproto ';'[opt] '{' body '}'
872///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000873void Parser::ParseObjCClassMethodDefinition() {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000874 assert(Tok.getKind() == tok::plus &&
875 "ParseObjCClassMethodDefinition(): Expected '+'");
876 ParseObjCMethodPrototype(false);
877 // parse optional ';'
878 if (Tok.getKind() == tok::semi)
879 ConsumeToken();
880 if (Tok.getKind() != tok::l_brace) {
881 Diag (Tok, diag::err_expected_lbrace);
882 return;
883 }
884
885 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +0000886}
Anders Carlssona66cad42007-08-21 17:43:55 +0000887
888Parser::ExprResult Parser::ParseObjCExpression() {
889 SourceLocation AtLoc = ConsumeToken(); // the "@"
890
891 switch (Tok.getKind()) {
892 case tok::string_literal: // primary-expression: string-literal
893 case tok::wide_string_literal:
894 return ParseObjCStringLiteral();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000895 default:
896 break;
897 }
898
899 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlsson8be1d402007-08-22 15:14:15 +0000900 case tok::objc_encode:
901 return ParseObjCEncodeExpression();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000902 case tok::objc_protocol:
903 return ParseObjCProtocolExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +0000904 default:
905 Diag(AtLoc, diag::err_unexpected_at);
906 SkipUntil(tok::semi);
907 break;
908 }
909
910 return 0;
911}
912
913Parser::ExprResult Parser::ParseObjCStringLiteral() {
914 ExprResult Res = ParseStringLiteralExpression();
915
916 if (Res.isInvalid) return Res;
917
918 return Actions.ParseObjCStringLiteral(Res.Val);
919}
Anders Carlsson8be1d402007-08-22 15:14:15 +0000920
921/// objc-encode-expression:
922/// @encode ( type-name )
923Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff87c329f2007-08-23 18:16:40 +0000924 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +0000925
926 SourceLocation EncLoc = ConsumeToken();
927
928 if (Tok.getKind() != tok::l_paren) {
929 Diag(Tok, diag::err_expected_lparen_after, "@encode");
930 return true;
931 }
932
933 SourceLocation LParenLoc = ConsumeParen();
934
935 TypeTy *Ty = ParseTypeName();
936
Anders Carlsson92faeb82007-08-23 15:31:37 +0000937 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +0000938
939 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +0000940 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +0000941}
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000942
943/// objc-protocol-expression
944/// @protocol ( protocol-name )
945
946Parser::ExprResult Parser::ParseObjCProtocolExpression()
947{
948 SourceLocation ProtoLoc = ConsumeToken();
949
950 if (Tok.getKind() != tok::l_paren) {
951 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
952 return true;
953 }
954
955 SourceLocation LParenLoc = ConsumeParen();
956
957 if (Tok.getKind() != tok::identifier) {
958 Diag(Tok, diag::err_expected_ident);
959 return true;
960 }
961
962 // FIXME: Do something with the protocol name
963 ConsumeToken();
964
Anders Carlsson92faeb82007-08-23 15:31:37 +0000965 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000966
967 // FIXME
968 return 0;
969}