blob: c2cf61d36777744a4e1b8cf1835554ce3c3d37cc [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
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000157 ParseObjCInterfaceDeclList(0, tok::objc_not_keyword/*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
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000193 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
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///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000216void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
217 tok::ObjCKeywordKind contextKey) {
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000218 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000219 tok::ObjCKeywordKind pi = tok::objc_not_keyword;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000220 while (1) {
221 if (Tok.getKind() == tok::at) {
222 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000223 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000224
225 if (ocKind == tok::objc_end) { // terminate list
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000226 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000227 } else if (ocKind == tok::objc_required) { // protocols only
228 ConsumeToken();
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000229 pi = ocKind;
230 if (contextKey != tok::objc_protocol)
231 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000232 } else if (ocKind == tok::objc_optional) { // protocols only
233 ConsumeToken();
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000234 pi = ocKind;
235 if (contextKey != tok::objc_protocol)
236 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000237 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000238 ParseObjCPropertyDecl(interfaceDecl);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000239 continue;
240 } else {
241 Diag(Tok, diag::err_objc_illegal_interface_qual);
242 ConsumeToken();
243 }
244 }
245 if (Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) {
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000246 DeclTy *methodPrototype = ParseObjCMethodPrototype(interfaceDecl, pi);
247 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000248 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
249 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000250 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
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)
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000256 break;
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 Naroff81f1bba2007-09-06 21:24:23 +0000260 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000261 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000262 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000263 /// Insert collected methods declarations into the @interface object.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000264 Actions.ObjcAddMethodsToClass(interfaceDecl,&allMethods[0],allMethods.size());
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000265 return;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000266}
267
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000268/// Parse property attribute declarations.
269///
270/// property-attr-decl: '(' property-attrlist ')'
271/// property-attrlist:
272/// property-attribute
273/// property-attrlist ',' property-attribute
274/// property-attribute:
275/// getter '=' identifier
276/// setter '=' identifier ':'
277/// readonly
278/// readwrite
279/// assign
280/// retain
281/// copy
282/// nonatomic
283///
284void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
285 SourceLocation loc = ConsumeParen(); // consume '('
286 while (isObjCPropertyAttribute()) {
287 const IdentifierInfo *II = Tok.getIdentifierInfo();
288 // getter/setter require extra treatment.
289 if (II == ObjcPropertyAttrs[objc_getter] ||
290 II == ObjcPropertyAttrs[objc_setter]) {
291 // skip getter/setter part.
292 SourceLocation loc = ConsumeToken();
293 if (Tok.getKind() == tok::equal) {
294 loc = ConsumeToken();
295 if (Tok.getKind() == tok::identifier) {
296 if (II == ObjcPropertyAttrs[objc_setter]) {
297 loc = ConsumeToken(); // consume method name
298 if (Tok.getKind() != tok::colon) {
299 Diag(loc, diag::err_expected_colon);
300 SkipUntil(tok::r_paren,true,true);
301 break;
302 }
303 }
304 }
305 else {
306 Diag(loc, diag::err_expected_ident);
307 SkipUntil(tok::r_paren,true,true);
308 break;
309 }
310 }
311 else {
312 Diag(loc, diag::err_objc_expected_equal);
313 SkipUntil(tok::r_paren,true,true);
314 break;
315 }
316 }
317 ConsumeToken(); // consume last attribute token
318 if (Tok.getKind() == tok::comma) {
319 loc = ConsumeToken();
320 continue;
321 }
322 if (Tok.getKind() == tok::r_paren)
323 break;
324 Diag(loc, diag::err_expected_rparen);
325 SkipUntil(tok::semi);
326 return;
327 }
328 if (Tok.getKind() == tok::r_paren)
329 ConsumeParen();
330 else {
331 Diag(loc, diag::err_objc_expected_property_attr);
332 SkipUntil(tok::r_paren); // recover from error inside attribute list
333 }
334}
335
336/// Main routine to parse property declaration.
337///
338/// @property property-attr-decl[opt] property-component-decl ';'
339///
340void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
341 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
342 "ParseObjCPropertyDecl(): Expected @property");
343 ConsumeToken(); // the "property" identifier
344 // Parse property attribute list, if any.
345 if (Tok.getKind() == tok::l_paren) {
346 // property has attribute list.
347 ParseObjCPropertyAttribute(0/*FIXME*/);
348 }
349 // Parse declaration portion of @property.
350 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
351 ParseStructDeclaration(interfaceDecl, PropertyDecls);
352 if (Tok.getKind() == tok::semi)
353 ConsumeToken();
354 else {
355 Diag(Tok, diag::err_expected_semi_decl_list);
356 SkipUntil(tok::r_brace, true, true);
357 }
Chris Lattner4b009652007-07-25 00:24:17 +0000358}
Steve Narofffb367882007-08-20 21:31:48 +0000359
Steve Naroff81f1bba2007-09-06 21:24:23 +0000360/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000361/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000362/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000363///
364/// objc-instance-method: '-'
365/// objc-class-method: '+'
366///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000367/// objc-method-attributes: [OBJC2]
368/// __attribute__((deprecated))
369///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000370Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
371 tok::ObjCKeywordKind& pi) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000372 assert((Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) &&
373 "expected +/-");
374
375 tok::TokenKind methodType = Tok.getKind();
376 SourceLocation methodLoc = ConsumeToken();
377
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000378 DeclTy *MDecl = ParseObjCMethodDecl(IDecl, pi, methodType, methodLoc);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000379 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000380 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000381 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000382}
383
384/// objc-selector:
385/// identifier
386/// one of
387/// enum struct union if else while do for switch case default
388/// break continue return goto asm sizeof typeof __alignof
389/// unsigned long const short volatile signed restrict _Complex
390/// in out inout bycopy byref oneway int char float double void _Bool
391///
392IdentifierInfo *Parser::ParseObjCSelector() {
393 tok::TokenKind tKind = Tok.getKind();
394 IdentifierInfo *II = 0;
395
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000396 if (tKind == tok::identifier || tKind == tok::kw_typeof ||
397 tKind == tok::kw___alignof ||
Steve Naroff0bbffd82007-08-22 16:35:03 +0000398 (tKind >= tok::kw_auto && tKind <= tok::kw__Complex)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000399 II = Tok.getIdentifierInfo();
400 ConsumeToken();
401 }
402 return II;
403}
404
Steve Naroffa8ee2262007-08-22 23:18:22 +0000405/// objc-type-qualifier: one of
406/// in out inout bycopy byref oneway
407///
Steve Naroffa8ee2262007-08-22 23:18:22 +0000408bool Parser::isObjCTypeQualifier() {
409 if (Tok.getKind() == tok::identifier) {
Chris Lattner32352462007-08-29 22:54:08 +0000410 const IdentifierInfo *II = Tok.getIdentifierInfo();
411 for (unsigned i = 0; i < objc_NumQuals; ++i)
412 if (II == ObjcTypeQuals[i]) return true;
Steve Naroffa8ee2262007-08-22 23:18:22 +0000413 }
414 return false;
415}
416
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000417/// property-attrlist: one of
418/// readonly getter setter assign retain copy nonatomic
419///
420bool Parser::isObjCPropertyAttribute() {
421 if (Tok.getKind() == tok::identifier) {
422 const IdentifierInfo *II = Tok.getIdentifierInfo();
423 for (unsigned i = 0; i < objc_NumAttrs; ++i)
424 if (II == ObjcPropertyAttrs[i]) return true;
425 }
426 return false;
427}
428
Steve Naroff0bbffd82007-08-22 16:35:03 +0000429/// objc-type-name:
430/// '(' objc-type-qualifiers[opt] type-name ')'
431/// '(' objc-type-qualifiers[opt] ')'
432///
433/// objc-type-qualifiers:
434/// objc-type-qualifier
435/// objc-type-qualifiers objc-type-qualifier
436///
Steve Naroff304ed392007-09-05 23:30:30 +0000437Parser::TypeTy *Parser::ParseObjCTypeName() {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000438 assert(Tok.getKind() == tok::l_paren && "expected (");
439
440 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Steve Naroff304ed392007-09-05 23:30:30 +0000441 TypeTy *Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000442
Steve Naroffa8ee2262007-08-22 23:18:22 +0000443 while (isObjCTypeQualifier())
444 ConsumeToken();
445
Steve Naroff0bbffd82007-08-22 16:35:03 +0000446 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000447 Ty = ParseTypeName();
448 // FIXME: back when Sema support is in place...
449 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000450 }
451 if (Tok.getKind() != tok::r_paren) {
452 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000453 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000454 }
455 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000456 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000457}
458
459/// objc-method-decl:
460/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000461/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000462/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000463/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000464///
465/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000466/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000467/// objc-keyword-selector objc-keyword-decl
468///
469/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000470/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
471/// objc-selector ':' objc-keyword-attributes[opt] identifier
472/// ':' objc-type-name objc-keyword-attributes[opt] identifier
473/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000474///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000475/// objc-parmlist:
476/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000477///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000478/// objc-parms:
479/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000480///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000481/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000482/// , ...
483///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000484/// objc-keyword-attributes: [OBJC2]
485/// __attribute__((unused))
486///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000487Parser::DeclTy *Parser::ParseObjCMethodDecl(DeclTy *IDecl,
488 tok::ObjCKeywordKind& pi,
489 tok::TokenKind mType, SourceLocation mLoc) {
490
Steve Naroff304ed392007-09-05 23:30:30 +0000491 TypeTy *ReturnType = 0;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000492 AttributeList *methodAttrs = 0;
Steve Naroff304ed392007-09-05 23:30:30 +0000493
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000494 // Parse the return type.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000495 if (Tok.getKind() == tok::l_paren)
Steve Naroff304ed392007-09-05 23:30:30 +0000496 ReturnType = ParseObjCTypeName();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000497 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff304ed392007-09-05 23:30:30 +0000498
Steve Naroff253118b2007-09-17 20:25:27 +0000499 llvm::SmallVector<ObjcKeywordDecl, 12> KeyInfo;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000500
501 if (Tok.getKind() == tok::colon) {
Steve Naroff304ed392007-09-05 23:30:30 +0000502
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000503 while (1) {
Steve Naroff253118b2007-09-17 20:25:27 +0000504 ObjcKeywordDecl KeyInfoDecl;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000505 KeyInfoDecl.SelectorName = selIdent;
Steve Naroff304ed392007-09-05 23:30:30 +0000506
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000507 // Each iteration parses a single keyword argument.
508 if (Tok.getKind() != tok::colon) {
509 Diag(Tok, diag::err_expected_colon);
510 break;
511 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000512 KeyInfoDecl.ColonLoc = ConsumeToken(); // Eat the ':'.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000513 if (Tok.getKind() == tok::l_paren) // Parse the argument type.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000514 KeyInfoDecl.TypeInfo = ParseObjCTypeName();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000515
516 // If attributes exist before the argument name, parse them.
Steve Naroffa7f62782007-08-23 19:56:30 +0000517 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000518 KeyInfoDecl.AttrList = ParseAttributes();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000519
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000520 if (Tok.getKind() != tok::identifier) {
521 Diag(Tok, diag::err_expected_ident); // missing argument name.
522 break;
523 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000524 KeyInfoDecl.ArgumentName = Tok.getIdentifierInfo();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000525 ConsumeToken(); // Eat the identifier.
Steve Naroff304ed392007-09-05 23:30:30 +0000526
Steve Naroff253118b2007-09-17 20:25:27 +0000527 // Rather than call out to the actions, package up the info locally,
528 // like we do for Declarator.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000529 KeyInfo.push_back(KeyInfoDecl);
Steve Naroff253118b2007-09-17 20:25:27 +0000530
531 // Check for another keyword selector.
Steve Naroff304ed392007-09-05 23:30:30 +0000532 selIdent = ParseObjCSelector();
533 if (!selIdent && Tok.getKind() != tok::colon)
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000534 break;
535 // We have a selector or a colon, continue parsing.
536 }
537 // Parse the (optional) parameter list.
538 while (Tok.getKind() == tok::comma) {
539 ConsumeToken();
540 if (Tok.getKind() == tok::ellipsis) {
541 ConsumeToken();
542 break;
543 }
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000544 // Parse the c-style argument declaration-specifier.
545 DeclSpec DS;
546 ParseDeclarationSpecifiers(DS);
547 // Parse the declarator.
548 Declarator ParmDecl(DS, Declarator::PrototypeContext);
549 ParseDeclarator(ParmDecl);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000550 }
Steve Naroff304ed392007-09-05 23:30:30 +0000551 // FIXME: Add support for optional parmameter list...
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();
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000555 return Actions.ObjcBuildMethodDeclaration(IDecl, pi, mLoc, mType,
556 ReturnType,
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000557 &KeyInfo[0], KeyInfo.size(),
558 methodAttrs);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000559 } else if (!selIdent) {
560 Diag(Tok, diag::err_expected_ident); // missing selector name.
561 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000562 // If attributes exist after the method, parse them.
563 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
564 methodAttrs = ParseAttributes();
565
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000566 return Actions.ObjcBuildMethodDeclaration(IDecl, pi,
567 mLoc, mType, ReturnType,
568 selIdent, methodAttrs);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000569}
570
Steve Narofffb367882007-08-20 21:31:48 +0000571/// objc-protocol-refs:
572/// '<' identifier-list '>'
573///
Steve Naroff304ed392007-09-05 23:30:30 +0000574bool Parser::ParseObjCProtocolReferences(
575 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Steve Narofffb367882007-08-20 21:31:48 +0000576 assert(Tok.getKind() == tok::less && "expected <");
577
578 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000579
580 while (1) {
581 if (Tok.getKind() != tok::identifier) {
582 Diag(Tok, diag::err_expected_ident);
583 SkipUntil(tok::greater);
584 return true;
585 }
586 ProtocolRefs.push_back(Tok.getIdentifierInfo());
587 ConsumeToken();
588
589 if (Tok.getKind() != tok::comma)
590 break;
591 ConsumeToken();
592 }
593 // Consume the '>'.
594 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
595}
596
597/// objc-class-instance-variables:
598/// '{' objc-instance-variable-decl-list[opt] '}'
599///
600/// objc-instance-variable-decl-list:
601/// objc-visibility-spec
602/// objc-instance-variable-decl ';'
603/// ';'
604/// objc-instance-variable-decl-list objc-visibility-spec
605/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
606/// objc-instance-variable-decl-list ';'
607///
608/// objc-visibility-spec:
609/// @private
610/// @protected
611/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000612/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000613///
614/// objc-instance-variable-decl:
615/// struct-declaration
616///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000617void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Steve Naroffc4474992007-08-21 21:17:12 +0000618 assert(Tok.getKind() == tok::l_brace && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000619 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000620 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
621 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000622
623 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000624
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000625 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000626 // While we still have something to read, read the instance variables.
627 while (Tok.getKind() != tok::r_brace &&
628 Tok.getKind() != tok::eof) {
629 // Each iteration of this loop reads one objc-instance-variable-decl.
630
631 // Check for extraneous top-level semicolon.
632 if (Tok.getKind() == tok::semi) {
633 Diag(Tok, diag::ext_extra_struct_semi);
634 ConsumeToken();
635 continue;
636 }
637 // Set the default visibility to private.
Steve Naroffc4474992007-08-21 21:17:12 +0000638 if (Tok.getKind() == tok::at) { // parse objc-visibility-spec
639 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000640 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000641 case tok::objc_private:
642 case tok::objc_public:
643 case tok::objc_protected:
644 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000645 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000646 ConsumeToken();
647 continue;
648 default:
649 Diag(Tok, diag::err_objc_illegal_visibility_spec);
650 ConsumeToken();
651 continue;
652 }
653 }
654 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000655 for (unsigned i = 0; i < IvarDecls.size(); i++) {
656 AllIvarDecls.push_back(IvarDecls[i]);
657 AllVisibilities.push_back(visibility);
658 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000659 IvarDecls.clear();
660
Steve Naroffc4474992007-08-21 21:17:12 +0000661 if (Tok.getKind() == tok::semi) {
662 ConsumeToken();
663 } else if (Tok.getKind() == tok::r_brace) {
664 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
665 break;
666 } else {
667 Diag(Tok, diag::err_expected_semi_decl_list);
668 // Skip to end of block or statement
669 SkipUntil(tok::r_brace, true, true);
670 }
671 }
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000672 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Steve Naroff0acc9c92007-09-15 18:49:24 +0000673 Actions.ActOnFields(LBraceLoc, interfaceDecl,
674 &AllIvarDecls[0], AllIvarDecls.size(),
675 &AllVisibilities[0]);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000676 }
Steve Naroffc4474992007-08-21 21:17:12 +0000677 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
678 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000679}
Steve Narofffb367882007-08-20 21:31:48 +0000680
681/// objc-protocol-declaration:
682/// objc-protocol-definition
683/// objc-protocol-forward-reference
684///
685/// objc-protocol-definition:
686/// @protocol identifier
687/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000688/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000689/// @end
690///
691/// objc-protocol-forward-reference:
692/// @protocol identifier-list ';'
693///
694/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000695/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000696/// semicolon in the first alternative if objc-protocol-refs are omitted.
697
Steve Naroff72f17fb2007-08-22 22:17:26 +0000698Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000699 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000700 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
701 ConsumeToken(); // the "protocol" identifier
702
703 if (Tok.getKind() != tok::identifier) {
704 Diag(Tok, diag::err_expected_ident); // missing protocol name.
705 return 0;
706 }
707 // Save the protocol name, then consume it.
708 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
709 SourceLocation nameLoc = ConsumeToken();
710
711 if (Tok.getKind() == tok::semi) { // forward declaration.
712 ConsumeToken();
713 return 0; // FIXME: add protocolName
714 }
715 if (Tok.getKind() == tok::comma) { // list of forward declarations.
716 // Parse the list of forward declarations.
717 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
718 ProtocolRefs.push_back(protocolName);
719
720 while (1) {
721 ConsumeToken(); // the ','
722 if (Tok.getKind() != tok::identifier) {
723 Diag(Tok, diag::err_expected_ident);
724 SkipUntil(tok::semi);
725 return 0;
726 }
727 ProtocolRefs.push_back(Tok.getIdentifierInfo());
728 ConsumeToken(); // the identifier
729
730 if (Tok.getKind() != tok::comma)
731 break;
732 }
733 // Consume the ';'.
734 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
735 return 0;
736 return 0; // FIXME
737 }
738 // Last, and definitely not least, parse a protocol declaration.
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000739 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000740 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000741 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000742 return 0;
743 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000744
745 DeclTy *ProtoType = Actions.ObjcStartProtoInterface(AtLoc,
746 protocolName, nameLoc,
747 &ProtocolRefs[0],
748 ProtocolRefs.size());
749 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000750
751 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000752 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000753 ConsumeToken(); // the "end" identifier
754 return 0;
755 }
756 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000757 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000758}
Steve Narofffb367882007-08-20 21:31:48 +0000759
760/// objc-implementation:
761/// objc-class-implementation-prologue
762/// objc-category-implementation-prologue
763///
764/// objc-class-implementation-prologue:
765/// @implementation identifier objc-superclass[opt]
766/// objc-class-instance-variables[opt]
767///
768/// objc-category-implementation-prologue:
769/// @implementation identifier ( identifier )
770
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000771Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
772 SourceLocation atLoc) {
773 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
774 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
775 ConsumeToken(); // the "implementation" identifier
776
777 if (Tok.getKind() != tok::identifier) {
778 Diag(Tok, diag::err_expected_ident); // missing class or category name.
779 return 0;
780 }
781 // We have a class or category name - consume it.
782 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
783
784 if (Tok.getKind() == tok::l_paren) {
785 // we have a category implementation.
786 SourceLocation lparenLoc = ConsumeParen();
787 SourceLocation categoryLoc, rparenLoc;
788 IdentifierInfo *categoryId = 0;
789
790 if (Tok.getKind() == tok::identifier) {
791 categoryId = Tok.getIdentifierInfo();
792 categoryLoc = ConsumeToken();
793 } else {
794 Diag(Tok, diag::err_expected_ident); // missing category name.
795 return 0;
796 }
797 if (Tok.getKind() != tok::r_paren) {
798 Diag(Tok, diag::err_expected_rparen);
799 SkipUntil(tok::r_paren, false); // don't stop at ';'
800 return 0;
801 }
802 rparenLoc = ConsumeParen();
803 return 0;
804 }
805 // We have a class implementation
806 if (Tok.getKind() == tok::colon) {
807 // We have a super class
808 ConsumeToken();
809 if (Tok.getKind() != tok::identifier) {
810 Diag(Tok, diag::err_expected_ident); // missing super class name.
811 return 0;
812 }
813 ConsumeToken(); // Consume super class name
814 }
815 if (Tok.getKind() == tok::l_brace)
Steve Naroff81f1bba2007-09-06 21:24:23 +0000816 ParseObjCClassInstanceVariables(0/*FIXME*/); // we have ivars
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000817
Steve Narofffb367882007-08-20 21:31:48 +0000818 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000819}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000820Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
821 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
822 "ParseObjCAtEndDeclaration(): Expected @end");
823 ConsumeToken(); // the "end" identifier
824
Steve Narofffb367882007-08-20 21:31:48 +0000825 return 0;
826}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000827
828/// compatibility-alias-decl:
829/// @compatibility_alias alias-name class-name ';'
830///
831Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
832 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
833 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
834 ConsumeToken(); // consume compatibility_alias
835 if (Tok.getKind() != tok::identifier) {
836 Diag(Tok, diag::err_expected_ident);
837 return 0;
838 }
839 ConsumeToken(); // consume alias-name
840 if (Tok.getKind() != tok::identifier) {
841 Diag(Tok, diag::err_expected_ident);
842 return 0;
843 }
844 ConsumeToken(); // consume class-name;
845 if (Tok.getKind() != tok::semi)
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000846 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Steve Narofffb367882007-08-20 21:31:48 +0000847 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000848}
849
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000850/// property-synthesis:
851/// @synthesize property-ivar-list ';'
852///
853/// property-ivar-list:
854/// property-ivar
855/// property-ivar-list ',' property-ivar
856///
857/// property-ivar:
858/// identifier
859/// identifier '=' identifier
860///
861Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
862 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
863 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
864 SourceLocation loc = ConsumeToken(); // consume dynamic
865 if (Tok.getKind() != tok::identifier) {
866 Diag(Tok, diag::err_expected_ident);
867 return 0;
868 }
869 while (Tok.getKind() == tok::identifier) {
870 ConsumeToken(); // consume property name
871 if (Tok.getKind() == tok::equal) {
872 // property '=' ivar-name
873 ConsumeToken(); // consume '='
874 if (Tok.getKind() != tok::identifier) {
875 Diag(Tok, diag::err_expected_ident);
876 break;
877 }
878 ConsumeToken(); // consume ivar-name
879 }
880 if (Tok.getKind() != tok::comma)
881 break;
882 ConsumeToken(); // consume ','
883 }
884 if (Tok.getKind() != tok::semi)
885 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
886 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000887}
888
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000889/// property-dynamic:
890/// @dynamic property-list
891///
892/// property-list:
893/// identifier
894/// property-list ',' identifier
895///
896Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
897 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
898 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
899 SourceLocation loc = ConsumeToken(); // consume dynamic
900 if (Tok.getKind() != tok::identifier) {
901 Diag(Tok, diag::err_expected_ident);
902 return 0;
903 }
904 while (Tok.getKind() == tok::identifier) {
905 ConsumeToken(); // consume property name
906 if (Tok.getKind() != tok::comma)
907 break;
908 ConsumeToken(); // consume ','
909 }
910 if (Tok.getKind() != tok::semi)
911 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
912 return 0;
913}
914
Steve Naroff81f1bba2007-09-06 21:24:23 +0000915/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000916///
917void Parser::ParseObjCInstanceMethodDefinition() {
918 assert(Tok.getKind() == tok::minus &&
919 "ParseObjCInstanceMethodDefinition(): Expected '-'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000920 // FIXME: @optional/@protocol??
921 tok::ObjCKeywordKind pi = tok::objc_not_keyword;
922 ParseObjCMethodPrototype(ObjcImpDecl, pi);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000923 // parse optional ';'
924 if (Tok.getKind() == tok::semi)
925 ConsumeToken();
926
927 if (Tok.getKind() != tok::l_brace) {
928 Diag (Tok, diag::err_expected_lbrace);
929 return;
930 }
931
932 StmtResult FnBody = ParseCompoundStatementBody();
933}
934
Steve Naroff81f1bba2007-09-06 21:24:23 +0000935/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000936///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000937void Parser::ParseObjCClassMethodDefinition() {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000938 assert(Tok.getKind() == tok::plus &&
939 "ParseObjCClassMethodDefinition(): Expected '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000940 // FIXME: @optional/@protocol??
941 tok::ObjCKeywordKind pi = tok::objc_not_keyword;
942 ParseObjCMethodPrototype(ObjcImpDecl, pi);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000943 // parse optional ';'
944 if (Tok.getKind() == tok::semi)
945 ConsumeToken();
946 if (Tok.getKind() != tok::l_brace) {
947 Diag (Tok, diag::err_expected_lbrace);
948 return;
949 }
950
951 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +0000952}
Anders Carlssona66cad42007-08-21 17:43:55 +0000953
954Parser::ExprResult Parser::ParseObjCExpression() {
955 SourceLocation AtLoc = ConsumeToken(); // the "@"
956
957 switch (Tok.getKind()) {
958 case tok::string_literal: // primary-expression: string-literal
959 case tok::wide_string_literal:
960 return ParseObjCStringLiteral();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000961 default:
962 break;
963 }
964
965 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlsson8be1d402007-08-22 15:14:15 +0000966 case tok::objc_encode:
967 return ParseObjCEncodeExpression();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000968 case tok::objc_protocol:
969 return ParseObjCProtocolExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +0000970 default:
971 Diag(AtLoc, diag::err_unexpected_at);
972 SkipUntil(tok::semi);
973 break;
974 }
975
976 return 0;
977}
978
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000979/// objc-message-expr:
980/// '[' objc-receiver objc-message-args ']'
981///
982/// objc-receiver:
983/// expression
984/// class-name
985/// type-name
986///
987/// objc-message-args:
988/// objc-selector
989/// objc-keywordarg-list
990///
991/// objc-keywordarg-list:
992/// objc-keywordarg
993/// objc-keywordarg-list objc-keywordarg
994///
995/// objc-keywordarg:
996/// selector-name[opt] ':' objc-keywordexpr
997///
998/// objc-keywordexpr:
999/// nonempty-expr-list
1000///
1001/// nonempty-expr-list:
1002/// assignment-expression
1003/// nonempty-expr-list , assignment-expression
1004///
1005Parser::ExprResult Parser::ParseObjCMessageExpression() {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001006 assert(Tok.getKind() == tok::l_square && "'[' expected");
1007 SourceLocation Loc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001008 IdentifierInfo *ReceiverName = 0;
1009 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001010 // Parse receiver
Steve Narofff0c31dd2007-09-16 16:16:00 +00001011 if (Tok.getKind() == tok::identifier &&
Steve Naroff253118b2007-09-17 20:25:27 +00001012 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1013 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001014 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001015 } else {
1016 ExprResult Res = ParseAssignmentExpression();
1017 if (Res.isInvalid) {
1018 SkipUntil(tok::identifier);
1019 return Res;
1020 }
1021 ReceiverExpr = Res.Val;
1022 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001023 // Parse objc-selector
1024 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff253118b2007-09-17 20:25:27 +00001025 llvm::SmallVector<ObjcKeywordMessage, 12> KeyInfo;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001026 if (Tok.getKind() == tok::colon) {
1027 while (1) {
1028 // Each iteration parses a single keyword argument.
Steve Naroff253118b2007-09-17 20:25:27 +00001029 ObjcKeywordMessage KeyInfoMess;
1030 KeyInfoMess.SelectorName = selIdent;
1031
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001032 if (Tok.getKind() != tok::colon) {
1033 Diag(Tok, diag::err_expected_colon);
1034 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001035 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001036 }
Steve Naroff253118b2007-09-17 20:25:27 +00001037 KeyInfoMess.ColonLoc = ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001038 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001039 ExprResult Res = ParseAssignmentExpression();
1040 if (Res.isInvalid) {
1041 SkipUntil(tok::identifier);
1042 return Res;
1043 }
1044 // We have a valid expression.
1045 KeyInfoMess.KeywordExpr = Res.Val;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001046
Steve Naroff253118b2007-09-17 20:25:27 +00001047 // Rather than call out to the actions, package up the info locally,
1048 // like we do for Declarator.
1049 KeyInfo.push_back(KeyInfoMess);
1050
1051 // Check for another keyword selector.
1052 selIdent = ParseObjCSelector();
1053 if (!selIdent && Tok.getKind() != tok::colon)
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001054 break;
1055 // We have a selector or a colon, continue parsing.
1056 }
1057 // Parse the, optional, argument list, comma separated.
1058 while (Tok.getKind() == tok::comma) {
1059 ConsumeToken();
1060 /// Parse the expression after ','
1061 ParseAssignmentExpression();
1062 }
1063 } else if (!selIdent) {
1064 Diag(Tok, diag::err_expected_ident); // missing selector name.
1065 SkipUntil(tok::semi);
1066 return 0;
1067 }
1068 if (Tok.getKind() != tok::r_square) {
1069 Diag(Tok, diag::err_expected_rsquare);
1070 SkipUntil(tok::semi);
1071 return 0;
1072 }
1073 ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001074
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001075 if (KeyInfo.size()) {
1076 // We've just parsed a keyword message.
1077 if (ReceiverName)
1078 return Actions.ActOnKeywordMessage(ReceiverName,
1079 &KeyInfo[0], KeyInfo.size());
1080 return Actions.ActOnKeywordMessage(ReceiverExpr,
1081 &KeyInfo[0], KeyInfo.size());
1082 }
1083 // We've just parsed a unary message (a message with no arguments).
Steve Naroff253118b2007-09-17 20:25:27 +00001084 if (ReceiverName)
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001085 return Actions.ActOnUnaryMessage(ReceiverName, selIdent);
1086 return Actions.ActOnUnaryMessage(ReceiverExpr, selIdent);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001087}
1088
Anders Carlssona66cad42007-08-21 17:43:55 +00001089Parser::ExprResult Parser::ParseObjCStringLiteral() {
1090 ExprResult Res = ParseStringLiteralExpression();
1091
1092 if (Res.isInvalid) return Res;
1093
1094 return Actions.ParseObjCStringLiteral(Res.Val);
1095}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001096
1097/// objc-encode-expression:
1098/// @encode ( type-name )
1099Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff87c329f2007-08-23 18:16:40 +00001100 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001101
1102 SourceLocation EncLoc = ConsumeToken();
1103
1104 if (Tok.getKind() != tok::l_paren) {
1105 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1106 return true;
1107 }
1108
1109 SourceLocation LParenLoc = ConsumeParen();
1110
1111 TypeTy *Ty = ParseTypeName();
1112
Anders Carlsson92faeb82007-08-23 15:31:37 +00001113 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001114
1115 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001116 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001117}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001118
1119/// objc-protocol-expression
1120/// @protocol ( protocol-name )
1121
1122Parser::ExprResult Parser::ParseObjCProtocolExpression()
1123{
1124 SourceLocation ProtoLoc = ConsumeToken();
1125
1126 if (Tok.getKind() != tok::l_paren) {
1127 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1128 return true;
1129 }
1130
1131 SourceLocation LParenLoc = ConsumeParen();
1132
1133 if (Tok.getKind() != tok::identifier) {
1134 Diag(Tok, diag::err_expected_ident);
1135 return true;
1136 }
1137
1138 // FIXME: Do something with the protocol name
1139 ConsumeToken();
1140
Anders Carlsson92faeb82007-08-23 15:31:37 +00001141 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001142
1143 // FIXME
1144 return 0;
1145}