blob: 6369817ad5467b40cb45612fdb6d8ee8fa8abf9c [file] [log] [blame]
Ted Kremenek42730c52008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
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"
Fariborz Jahanian06798362007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/Diagnostic.h"
18#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21
22/// ParseExternalDeclaration:
23/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroff5b96e2e2007-10-29 21:39:29 +000025/// [OBJC] objc-class-declaration
26/// [OBJC] objc-alias-declaration
27/// [OBJC] objc-protocol-definition
28/// [OBJC] objc-method-definition
29/// [OBJC] '@' 'end'
Steve Narofffb367882007-08-20 21:31:48 +000030Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Chris Lattner4b009652007-07-25 00:24:17 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
32
Steve Naroff87c329f2007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Chris Lattner818350c2008-08-23 02:02:23 +000034 case tok::objc_class:
35 return ParseObjCAtClassDeclaration(AtLoc);
36 case tok::objc_interface:
37 return ParseObjCAtInterfaceDeclaration(AtLoc);
38 case tok::objc_protocol:
39 return ParseObjCAtProtocolDeclaration(AtLoc);
40 case tok::objc_implementation:
41 return ParseObjCAtImplementationDeclaration(AtLoc);
42 case tok::objc_end:
43 return ParseObjCAtEndDeclaration(AtLoc);
44 case tok::objc_compatibility_alias:
45 return ParseObjCAtAliasDeclaration(AtLoc);
46 case tok::objc_synthesize:
47 return ParseObjCPropertySynthesize(AtLoc);
48 case tok::objc_dynamic:
49 return ParseObjCPropertyDynamic(AtLoc);
50 default:
51 Diag(AtLoc, diag::err_unexpected_at);
52 SkipUntil(tok::semi);
53 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000054 }
55}
56
57///
58/// objc-class-declaration:
59/// '@' 'class' identifier-list ';'
60///
Steve Narofffb367882007-08-20 21:31:48 +000061Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000062 ConsumeToken(); // the identifier "class"
63 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
64
65 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +000066 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +000067 Diag(Tok, diag::err_expected_ident);
68 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000069 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000070 }
Chris Lattner4b009652007-07-25 00:24:17 +000071 ClassNames.push_back(Tok.getIdentifierInfo());
72 ConsumeToken();
73
Chris Lattnera1d2bb72007-10-09 17:51:17 +000074 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +000075 break;
76
77 ConsumeToken();
78 }
79
80 // Consume the ';'.
81 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Narofffb367882007-08-20 21:31:48 +000082 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000083
Steve Naroff415c1832007-10-10 17:32:04 +000084 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +000085 &ClassNames[0], ClassNames.size());
Chris Lattner4b009652007-07-25 00:24:17 +000086}
87
Steve Narofffb367882007-08-20 21:31:48 +000088///
89/// objc-interface:
90/// objc-class-interface-attributes[opt] objc-class-interface
91/// objc-category-interface
92///
93/// objc-class-interface:
94/// '@' 'interface' identifier objc-superclass[opt]
95/// objc-protocol-refs[opt]
96/// objc-class-instance-variables[opt]
97/// objc-interface-decl-list
98/// @end
99///
100/// objc-category-interface:
101/// '@' 'interface' identifier '(' identifier[opt] ')'
102/// objc-protocol-refs[opt]
103/// objc-interface-decl-list
104/// @end
105///
106/// objc-superclass:
107/// ':' identifier
108///
109/// objc-class-interface-attributes:
110/// __attribute__((visibility("default")))
111/// __attribute__((visibility("hidden")))
112/// __attribute__((deprecated))
113/// __attribute__((unavailable))
114/// __attribute__((objc_exception)) - used by NSException on 64-bit
115///
116Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
117 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000118 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Narofffb367882007-08-20 21:31:48 +0000119 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
120 ConsumeToken(); // the "interface" identifier
121
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000122 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000123 Diag(Tok, diag::err_expected_ident); // missing class or category name.
124 return 0;
125 }
126 // We have a class or category name - consume it.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000127 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Narofffb367882007-08-20 21:31:48 +0000128 SourceLocation nameLoc = ConsumeToken();
129
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000130 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Narofffb367882007-08-20 21:31:48 +0000131 SourceLocation lparenLoc = ConsumeParen();
132 SourceLocation categoryLoc, rparenLoc;
133 IdentifierInfo *categoryId = 0;
134
Steve Naroffa7f62782007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000136 if (Tok.is(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
141 return 0;
Steve Narofffb367882007-08-20 21:31:48 +0000142 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::r_paren)) {
Steve Narofffb367882007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_rparen);
145 SkipUntil(tok::r_paren, false); // don't stop at ';'
146 return 0;
147 }
148 rparenLoc = ConsumeParen();
Chris Lattner45142b92008-07-26 04:07:02 +0000149
Steve Narofffb367882007-08-20 21:31:48 +0000150 // Next, we need to check for any protocol references.
Chris Lattner45142b92008-07-26 04:07:02 +0000151 SourceLocation EndProtoLoc;
152 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
153 if (Tok.is(tok::less) &&
154 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
155 return 0;
156
Steve Narofffb367882007-08-20 21:31:48 +0000157 if (attrList) // categories don't support attributes.
158 Diag(Tok, diag::err_objc_no_attributes_on_category);
159
Steve Naroff415c1832007-10-10 17:32:04 +0000160 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff25aace82007-10-03 21:00:46 +0000161 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff667f1682007-10-30 13:30:57 +0000162 &ProtocolRefs[0], ProtocolRefs.size(),
Chris Lattner45142b92008-07-26 04:07:02 +0000163 EndProtoLoc);
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000164
165 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnera40577e2008-10-20 06:10:06 +0000166 return CategoryType;
Steve Narofffb367882007-08-20 21:31:48 +0000167 }
168 // Parse a class interface.
169 IdentifierInfo *superClassId = 0;
170 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000171
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000172 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Narofffb367882007-08-20 21:31:48 +0000173 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000174 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000175 Diag(Tok, diag::err_expected_ident); // missing super class name.
176 return 0;
177 }
178 superClassId = Tok.getIdentifierInfo();
179 superClassLoc = ConsumeToken();
180 }
181 // Next, we need to check for any protocol references.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000182 llvm::SmallVector<Action::DeclTy*, 8> ProtocolRefs;
183 SourceLocation EndProtoLoc;
184 if (Tok.is(tok::less) &&
185 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
186 return 0;
187
188 DeclTy *ClsType =
189 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
190 superClassId, superClassLoc,
191 &ProtocolRefs[0], ProtocolRefs.size(),
192 EndProtoLoc, attrList);
Steve Naroff304ed392007-09-05 23:30:30 +0000193
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000194 if (Tok.is(tok::l_brace))
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000195 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Narofffb367882007-08-20 21:31:48 +0000196
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000197 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnera40577e2008-10-20 06:10:06 +0000198 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000199}
200
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000201/// constructSetterName - Return the setter name for the given
202/// identifier, i.e. "set" + Name where the initial character of Name
203/// has been capitalized.
204static IdentifierInfo *constructSetterName(IdentifierTable &Idents,
205 const IdentifierInfo *Name) {
206 unsigned N = Name->getLength();
207 char *SelectorName = new char[3 + N];
208 memcpy(SelectorName, "set", 3);
209 memcpy(&SelectorName[3], Name->getName(), N);
210 SelectorName[3] = toupper(SelectorName[3]);
211
212 IdentifierInfo *Setter =
213 &Idents.get(SelectorName, &SelectorName[3 + N]);
214 delete[] SelectorName;
215 return Setter;
216}
217
Steve Narofffb367882007-08-20 21:31:48 +0000218/// objc-interface-decl-list:
219/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000220/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000221/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000222/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000223/// objc-interface-decl-list declaration
224/// objc-interface-decl-list ';'
225///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000226/// objc-method-requirement: [OBJC2]
227/// @required
228/// @optional
229///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000230void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000231 tok::ObjCKeywordKind contextKey) {
Ted Kremenek8c945b12008-06-06 16:45:15 +0000232 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000233 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000234 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000235
Chris Lattnera40577e2008-10-20 06:10:06 +0000236 SourceLocation AtEndLoc;
237
Steve Naroff0bbffd82007-08-22 16:35:03 +0000238 while (1) {
Chris Lattnere48b46b2008-10-20 05:46:22 +0000239 // If this is a method prototype, parse it.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000240 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
241 DeclTy *methodPrototype =
242 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000243 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000244 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
245 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000246 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000247 continue;
248 }
Fariborz Jahanian5d175c32007-12-11 18:34:51 +0000249
Chris Lattnere48b46b2008-10-20 05:46:22 +0000250 // Ignore excess semicolons.
251 if (Tok.is(tok::semi)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000252 ConsumeToken();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000253 continue;
254 }
255
Chris Lattnera40577e2008-10-20 06:10:06 +0000256 // If we got to the end of the file, exit the loop.
Chris Lattnere48b46b2008-10-20 05:46:22 +0000257 if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000258 break;
Chris Lattnere48b46b2008-10-20 05:46:22 +0000259
260 // If we don't have an @ directive, parse it as a function definition.
261 if (Tok.isNot(tok::at)) {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000262 // FIXME: as the name implies, this rule allows function definitions.
263 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000264 ParseDeclarationOrFunctionDefinition();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000265 continue;
266 }
267
268 // Otherwise, we have an @ directive, eat the @.
269 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnercba730b2008-10-20 05:57:40 +0000270 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000271
Chris Lattnercba730b2008-10-20 05:57:40 +0000272 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere48b46b2008-10-20 05:46:22 +0000273 AtEndLoc = AtLoc;
274 break;
Chris Lattnera40577e2008-10-20 06:10:06 +0000275 }
Chris Lattnere48b46b2008-10-20 05:46:22 +0000276
Chris Lattnera40577e2008-10-20 06:10:06 +0000277 // Eat the identifier.
278 ConsumeToken();
279
Chris Lattnercba730b2008-10-20 05:57:40 +0000280 switch (DirectiveKind) {
281 default:
Chris Lattnera40577e2008-10-20 06:10:06 +0000282 // FIXME: If someone forgets an @end on a protocol, this loop will
283 // continue to eat up tons of stuff and spew lots of nonsense errors. It
284 // would probably be better to bail out if we saw an @class or @interface
285 // or something like that.
Chris Lattnercba730b2008-10-20 05:57:40 +0000286 Diag(Tok, diag::err_objc_illegal_interface_qual);
Chris Lattnera40577e2008-10-20 06:10:06 +0000287 // Skip until we see an '@' or '}' or ';'.
Chris Lattnercba730b2008-10-20 05:57:40 +0000288 SkipUntil(tok::r_brace, tok::at);
289 break;
290
291 case tok::objc_required:
Chris Lattnercba730b2008-10-20 05:57:40 +0000292 case tok::objc_optional:
Chris Lattnercba730b2008-10-20 05:57:40 +0000293 // This is only valid on protocols.
Chris Lattnera40577e2008-10-20 06:10:06 +0000294 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere48b46b2008-10-20 05:46:22 +0000295 if (contextKey != tok::objc_protocol)
Chris Lattnera40577e2008-10-20 06:10:06 +0000296 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnercba730b2008-10-20 05:57:40 +0000297 else
Chris Lattnera40577e2008-10-20 06:10:06 +0000298 MethodImplKind = DirectiveKind;
Chris Lattnercba730b2008-10-20 05:57:40 +0000299 break;
300
301 case tok::objc_property:
Chris Lattnere48b46b2008-10-20 05:46:22 +0000302 ObjCDeclSpec OCDS;
Chris Lattnere48b46b2008-10-20 05:46:22 +0000303 // Parse property attribute list, if any.
304 if (Tok.is(tok::l_paren)) {
305 // property has attribute list.
306 ParseObjCPropertyAttribute(OCDS);
307 }
308 // Parse all the comma separated declarators.
309 DeclSpec DS;
310 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
311 ParseStructDeclaration(DS, FieldDeclarators);
312
313 if (Tok.is(tok::semi))
314 ConsumeToken();
315 else {
316 Diag(Tok, diag::err_expected_semi_decl_list);
317 SkipUntil(tok::r_brace, true, true);
318 }
319 // Convert them all to property declarations.
320 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
321 FieldDeclarator &FD = FieldDeclarators[i];
322 // Install the property declarator into interfaceDecl.
323 Selector GetterSel =
324 PP.getSelectorTable().getNullarySelector(OCDS.getGetterName()
325 ? OCDS.getGetterName()
326 : FD.D.getIdentifier());
327 IdentifierInfo *SetterName = OCDS.getSetterName();
328 if (!SetterName)
329 SetterName = constructSetterName(PP.getIdentifierTable(),
330 FD.D.getIdentifier());
331 Selector SetterSel =
332 PP.getSelectorTable().getUnarySelector(SetterName);
333 DeclTy *Property = Actions.ActOnProperty(CurScope,
334 AtLoc, FD, OCDS,
335 GetterSel, SetterSel,
336 MethodImplKind);
337 allProperties.push_back(Property);
338 }
Chris Lattnercba730b2008-10-20 05:57:40 +0000339 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000340 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000341 }
Chris Lattnera40577e2008-10-20 06:10:06 +0000342
343 // We break out of the big loop in two cases: when we see @end or when we see
344 // EOF. In the former case, eat the @end. In the later case, emit an error.
345 if (Tok.isObjCAtKeyword(tok::objc_end))
346 ConsumeToken(); // the "end" identifier
347 else
348 Diag(Tok, diag::err_objc_missing_end);
349
Chris Lattnercba730b2008-10-20 05:57:40 +0000350 // Insert collected methods declarations into the @interface object.
Chris Lattnera40577e2008-10-20 06:10:06 +0000351 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8c945b12008-06-06 16:45:15 +0000352 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
353 allMethods.empty() ? 0 : &allMethods[0],
354 allMethods.size(),
355 allProperties.empty() ? 0 : &allProperties[0],
356 allProperties.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000357}
358
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000359/// Parse property attribute declarations.
360///
361/// property-attr-decl: '(' property-attrlist ')'
362/// property-attrlist:
363/// property-attribute
364/// property-attrlist ',' property-attribute
365/// property-attribute:
366/// getter '=' identifier
367/// setter '=' identifier ':'
368/// readonly
369/// readwrite
370/// assign
371/// retain
372/// copy
373/// nonatomic
374///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000375void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000376 SourceLocation loc = ConsumeParen(); // consume '('
377 while (isObjCPropertyAttribute()) {
378 const IdentifierInfo *II = Tok.getIdentifierInfo();
379 // getter/setter require extra treatment.
Ted Kremenek42730c52008-01-07 19:49:32 +0000380 if (II == ObjCPropertyAttrs[objc_getter] ||
381 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000382 // skip getter/setter part.
383 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000384 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000385 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000386 if (Tok.is(tok::identifier)) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000387 if (II == ObjCPropertyAttrs[objc_setter]) {
388 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000389 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000390 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000391 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000392 Diag(loc, diag::err_expected_colon);
393 SkipUntil(tok::r_paren,true,true);
394 break;
395 }
396 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000397 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000398 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000399 DS.setGetterName(Tok.getIdentifierInfo());
400 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000401 }
402 else {
403 Diag(loc, diag::err_expected_ident);
Chris Lattner847f5c12007-12-27 19:57:00 +0000404 SkipUntil(tok::r_paren,true,true);
405 break;
406 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000407 }
408 else {
409 Diag(loc, diag::err_objc_expected_equal);
410 SkipUntil(tok::r_paren,true,true);
411 break;
412 }
413 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000414
Ted Kremenek42730c52008-01-07 19:49:32 +0000415 else if (II == ObjCPropertyAttrs[objc_readonly])
416 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
417 else if (II == ObjCPropertyAttrs[objc_assign])
418 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
419 else if (II == ObjCPropertyAttrs[objc_readwrite])
420 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
421 else if (II == ObjCPropertyAttrs[objc_retain])
422 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
423 else if (II == ObjCPropertyAttrs[objc_copy])
424 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
425 else if (II == ObjCPropertyAttrs[objc_nonatomic])
426 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000427
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000428 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000429 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000430 loc = ConsumeToken();
431 continue;
432 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000433 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000434 break;
435 Diag(loc, diag::err_expected_rparen);
436 SkipUntil(tok::semi);
437 return;
438 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000439 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000440 ConsumeParen();
441 else {
442 Diag(loc, diag::err_objc_expected_property_attr);
443 SkipUntil(tok::r_paren); // recover from error inside attribute list
444 }
445}
446
Steve Naroff81f1bba2007-09-06 21:24:23 +0000447/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000448/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000449/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000450///
451/// objc-instance-method: '-'
452/// objc-class-method: '+'
453///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000454/// objc-method-attributes: [OBJC2]
455/// __attribute__((deprecated))
456///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000457Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000458 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000459 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000460
461 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000462 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000463
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000464 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000465 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000466 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000467 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000468}
469
470/// objc-selector:
471/// identifier
472/// one of
473/// enum struct union if else while do for switch case default
474/// break continue return goto asm sizeof typeof __alignof
475/// unsigned long const short volatile signed restrict _Complex
476/// in out inout bycopy byref oneway int char float double void _Bool
477///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000478IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000479 switch (Tok.getKind()) {
480 default:
481 return 0;
482 case tok::identifier:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000483 case tok::kw_asm:
Chris Lattnerd031a452007-10-07 02:00:24 +0000484 case tok::kw_auto:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000485 case tok::kw_bool:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000486 case tok::kw_break:
487 case tok::kw_case:
488 case tok::kw_catch:
489 case tok::kw_char:
490 case tok::kw_class:
491 case tok::kw_const:
492 case tok::kw_const_cast:
493 case tok::kw_continue:
494 case tok::kw_default:
495 case tok::kw_delete:
496 case tok::kw_do:
497 case tok::kw_double:
498 case tok::kw_dynamic_cast:
499 case tok::kw_else:
500 case tok::kw_enum:
501 case tok::kw_explicit:
502 case tok::kw_export:
503 case tok::kw_extern:
504 case tok::kw_false:
505 case tok::kw_float:
506 case tok::kw_for:
507 case tok::kw_friend:
508 case tok::kw_goto:
509 case tok::kw_if:
510 case tok::kw_inline:
511 case tok::kw_int:
512 case tok::kw_long:
513 case tok::kw_mutable:
514 case tok::kw_namespace:
515 case tok::kw_new:
516 case tok::kw_operator:
517 case tok::kw_private:
518 case tok::kw_protected:
519 case tok::kw_public:
520 case tok::kw_register:
521 case tok::kw_reinterpret_cast:
522 case tok::kw_restrict:
523 case tok::kw_return:
524 case tok::kw_short:
525 case tok::kw_signed:
526 case tok::kw_sizeof:
527 case tok::kw_static:
528 case tok::kw_static_cast:
529 case tok::kw_struct:
530 case tok::kw_switch:
531 case tok::kw_template:
532 case tok::kw_this:
533 case tok::kw_throw:
534 case tok::kw_true:
535 case tok::kw_try:
536 case tok::kw_typedef:
537 case tok::kw_typeid:
538 case tok::kw_typename:
539 case tok::kw_typeof:
540 case tok::kw_union:
541 case tok::kw_unsigned:
542 case tok::kw_using:
543 case tok::kw_virtual:
544 case tok::kw_void:
545 case tok::kw_volatile:
546 case tok::kw_wchar_t:
547 case tok::kw_while:
Chris Lattnerd031a452007-10-07 02:00:24 +0000548 case tok::kw__Bool:
549 case tok::kw__Complex:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000550 case tok::kw___alignof:
Chris Lattnerd031a452007-10-07 02:00:24 +0000551 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000552 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000553 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000554 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000555}
556
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000557/// property-attrlist: one of
558/// readonly getter setter assign retain copy nonatomic
559///
560bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000561 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000562 const IdentifierInfo *II = Tok.getIdentifierInfo();
563 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremenek42730c52008-01-07 19:49:32 +0000564 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000565 }
566 return false;
567}
568
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000569/// objc-for-collection-in: 'in'
570///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000571bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000572 // FIXME: May have to do additional look-ahead to only allow for
573 // valid tokens following an 'in'; such as an identifier, unary operators,
574 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000575 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner818350c2008-08-23 02:02:23 +0000576 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000577}
578
Ted Kremenek42730c52008-01-07 19:49:32 +0000579/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000580/// qualifier list and builds their bitmask representation in the input
581/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000582///
583/// objc-type-qualifiers:
584/// objc-type-qualifier
585/// objc-type-qualifiers objc-type-qualifier
586///
Ted Kremenek42730c52008-01-07 19:49:32 +0000587void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000588 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000589 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000590 return;
591
592 const IdentifierInfo *II = Tok.getIdentifierInfo();
593 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000594 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000595 continue;
596
Ted Kremenek42730c52008-01-07 19:49:32 +0000597 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000598 switch (i) {
599 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000600 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
601 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
602 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
603 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
604 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
605 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000606 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000607 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000608 ConsumeToken();
609 II = 0;
610 break;
611 }
612
613 // If this wasn't a recognized qualifier, bail out.
614 if (II) return;
615 }
616}
617
618/// objc-type-name:
619/// '(' objc-type-qualifiers[opt] type-name ')'
620/// '(' objc-type-qualifiers[opt] ')'
621///
Ted Kremenek42730c52008-01-07 19:49:32 +0000622Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000623 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000624
625 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattnerb5769332008-08-23 01:48:03 +0000626 SourceLocation TypeStartLoc = Tok.getLocation();
Chris Lattner265c8172007-09-27 15:15:46 +0000627 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000628
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000629 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000630 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000631
Steve Naroff0bbffd82007-08-22 16:35:03 +0000632 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000633 Ty = ParseTypeName();
634 // FIXME: back when Sema support is in place...
635 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000636 }
Chris Lattnerb5769332008-08-23 01:48:03 +0000637
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000638 if (Tok.isNot(tok::r_paren)) {
Chris Lattnerb5769332008-08-23 01:48:03 +0000639 // If we didn't eat any tokens, then this isn't a type.
640 if (Tok.getLocation() == TypeStartLoc) {
641 Diag(Tok.getLocation(), diag::err_expected_type);
642 SkipUntil(tok::r_brace);
643 } else {
644 // Otherwise, we found *something*, but didn't get a ')' in the right
645 // place. Emit an error then return what we have as the type.
646 MatchRHSPunctuation(tok::r_paren, LParenLoc);
647 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000648 }
649 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000650 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000651}
652
653/// objc-method-decl:
654/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000655/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000656/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000657/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000658///
659/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000660/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000661/// objc-keyword-selector objc-keyword-decl
662///
663/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000664/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
665/// objc-selector ':' objc-keyword-attributes[opt] identifier
666/// ':' objc-type-name objc-keyword-attributes[opt] identifier
667/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000668///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000669/// objc-parmlist:
670/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000671///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000672/// objc-parms:
673/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000674///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000675/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000676/// , ...
677///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000678/// objc-keyword-attributes: [OBJC2]
679/// __attribute__((unused))
680///
Steve Naroff3774dd92007-10-26 20:53:56 +0000681Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000682 tok::TokenKind mType,
683 DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000684 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000685{
Chris Lattnerb5769332008-08-23 01:48:03 +0000686 // Parse the return type if present.
Chris Lattnerd031a452007-10-07 02:00:24 +0000687 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000688 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000689 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000690 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnerb5769332008-08-23 01:48:03 +0000691
Steve Naroff3774dd92007-10-26 20:53:56 +0000692 SourceLocation selLoc;
693 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerb5769332008-08-23 01:48:03 +0000694
695 if (!SelIdent) { // missing selector name.
696 Diag(Tok.getLocation(), diag::err_expected_selector_for_method,
697 SourceRange(mLoc, Tok.getLocation()));
698 // Skip until we get a ; or {}.
699 SkipUntil(tok::r_brace);
700 return 0;
701 }
702
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000703 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000704 // If attributes exist after the method, parse them.
705 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000706 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000707 MethodAttrs = ParseAttributes();
708
709 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000710 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000711 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000712 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000713 }
Steve Naroff304ed392007-09-05 23:30:30 +0000714
Steve Naroff4ed9d662007-09-27 14:38:14 +0000715 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
716 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremenek42730c52008-01-07 19:49:32 +0000717 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000718 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000719
720 Action::TypeTy *TypeInfo;
721 while (1) {
722 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000723
Chris Lattnerd031a452007-10-07 02:00:24 +0000724 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000725 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000726 Diag(Tok, diag::err_expected_colon);
727 break;
728 }
729 ConsumeToken(); // Eat the ':'.
Ted Kremenek42730c52008-01-07 19:49:32 +0000730 ObjCDeclSpec DSType;
Chris Lattnerb5769332008-08-23 01:48:03 +0000731 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000732 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerd031a452007-10-07 02:00:24 +0000733 else
734 TypeInfo = 0;
735 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000736 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000737
Chris Lattnerd031a452007-10-07 02:00:24 +0000738 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000739 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000740 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000741
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000742 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000743 Diag(Tok, diag::err_expected_ident); // missing argument name.
744 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000745 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000746 ArgNames.push_back(Tok.getIdentifierInfo());
747 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000748
Chris Lattnerd031a452007-10-07 02:00:24 +0000749 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000750 SourceLocation Loc;
751 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000752 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000753 break;
754 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000755 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000756
Steve Naroff29fe7462007-11-15 12:35:21 +0000757 bool isVariadic = false;
758
Chris Lattnerd031a452007-10-07 02:00:24 +0000759 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000760 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000761 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000762 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000763 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000764 ConsumeToken();
765 break;
766 }
Steve Naroff29fe7462007-11-15 12:35:21 +0000767 // FIXME: implement this...
Chris Lattnerd031a452007-10-07 02:00:24 +0000768 // Parse the c-style argument declaration-specifier.
769 DeclSpec DS;
770 ParseDeclarationSpecifiers(DS);
771 // Parse the declarator.
772 Declarator ParmDecl(DS, Declarator::PrototypeContext);
773 ParseDeclarator(ParmDecl);
774 }
775
776 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000777 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000778 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000779 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000780 MethodAttrs = ParseAttributes();
781
782 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
783 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000784 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000785 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000786 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff29fe7462007-11-15 12:35:21 +0000787 &ArgNames[0], MethodAttrs,
788 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000789}
790
Steve Narofffb367882007-08-20 21:31:48 +0000791/// objc-protocol-refs:
792/// '<' identifier-list '>'
793///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000794bool Parser::
Chris Lattner2bdedd62008-07-26 04:03:38 +0000795ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
796 bool WarnOnDeclarations, SourceLocation &EndLoc) {
797 assert(Tok.is(tok::less) && "expected <");
798
799 ConsumeToken(); // the "<"
800
801 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
802
803 while (1) {
804 if (Tok.isNot(tok::identifier)) {
805 Diag(Tok, diag::err_expected_ident);
806 SkipUntil(tok::greater);
807 return true;
808 }
809 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
810 Tok.getLocation()));
811 ConsumeToken();
812
813 if (Tok.isNot(tok::comma))
814 break;
815 ConsumeToken();
816 }
817
818 // Consume the '>'.
819 if (Tok.isNot(tok::greater)) {
820 Diag(Tok, diag::err_expected_greater);
821 return true;
822 }
823
824 EndLoc = ConsumeAnyToken();
825
826 // Convert the list of protocols identifiers into a list of protocol decls.
827 Actions.FindProtocolDeclaration(WarnOnDeclarations,
828 &ProtocolIdents[0], ProtocolIdents.size(),
829 Protocols);
830 return false;
831}
832
Steve Narofffb367882007-08-20 21:31:48 +0000833/// objc-class-instance-variables:
834/// '{' objc-instance-variable-decl-list[opt] '}'
835///
836/// objc-instance-variable-decl-list:
837/// objc-visibility-spec
838/// objc-instance-variable-decl ';'
839/// ';'
840/// objc-instance-variable-decl-list objc-visibility-spec
841/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
842/// objc-instance-variable-decl-list ';'
843///
844/// objc-visibility-spec:
845/// @private
846/// @protected
847/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000848/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000849///
850/// objc-instance-variable-decl:
851/// struct-declaration
852///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000853void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
854 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000855 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000856 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000857 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
858
Steve Naroffc4474992007-08-21 21:17:12 +0000859 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000860
Fariborz Jahanian7c420a72008-04-29 23:03:51 +0000861 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffc4474992007-08-21 21:17:12 +0000862 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000863 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000864 // Each iteration of this loop reads one objc-instance-variable-decl.
865
866 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000867 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000868 Diag(Tok, diag::ext_extra_struct_semi);
869 ConsumeToken();
870 continue;
871 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000872
Steve Naroffc4474992007-08-21 21:17:12 +0000873 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000874 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000875 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000876 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000877 case tok::objc_private:
878 case tok::objc_public:
879 case tok::objc_protected:
880 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000881 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000882 ConsumeToken();
883 continue;
884 default:
885 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000886 continue;
887 }
888 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000889
890 // Parse all the comma separated declarators.
891 DeclSpec DS;
892 FieldDeclarators.clear();
893 ParseStructDeclaration(DS, FieldDeclarators);
894
895 // Convert them all to fields.
896 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
897 FieldDeclarator &FD = FieldDeclarators[i];
898 // Install the declarator into interfaceDecl.
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000899 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattner3dd8d392008-04-10 06:46:29 +0000900 DS.getSourceRange().getBegin(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000901 FD.D, FD.BitfieldSize, visibility);
Chris Lattner3dd8d392008-04-10 06:46:29 +0000902 AllIvarDecls.push_back(Field);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000903 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000904
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000905 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000906 ConsumeToken();
Steve Naroffc4474992007-08-21 21:17:12 +0000907 } else {
908 Diag(Tok, diag::err_expected_semi_decl_list);
909 // Skip to end of block or statement
910 SkipUntil(tok::r_brace, true, true);
911 }
912 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000913 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000914 // Call ActOnFields() even if we don't have any decls. This is useful
915 // for code rewriting tools that need to be aware of the empty list.
916 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
917 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +0000918 LBraceLoc, RBraceLoc, 0);
Steve Naroffc4474992007-08-21 21:17:12 +0000919 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000920}
Steve Narofffb367882007-08-20 21:31:48 +0000921
922/// objc-protocol-declaration:
923/// objc-protocol-definition
924/// objc-protocol-forward-reference
925///
926/// objc-protocol-definition:
927/// @protocol identifier
928/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000929/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000930/// @end
931///
932/// objc-protocol-forward-reference:
933/// @protocol identifier-list ';'
934///
935/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000936/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000937/// semicolon in the first alternative if objc-protocol-refs are omitted.
Daniel Dunbar28680d12008-09-26 04:48:09 +0000938Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
939 AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000940 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000941 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
942 ConsumeToken(); // the "protocol" identifier
943
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000944 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000945 Diag(Tok, diag::err_expected_ident); // missing protocol name.
946 return 0;
947 }
948 // Save the protocol name, then consume it.
949 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
950 SourceLocation nameLoc = ConsumeToken();
951
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000952 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000953 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000954 ConsumeToken();
Chris Lattnere705e5e2008-07-21 22:17:28 +0000955 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000956 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000957
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000958 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000959 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
960 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
961
Steve Naroff72f17fb2007-08-22 22:17:26 +0000962 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000963 while (1) {
964 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000965 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000966 Diag(Tok, diag::err_expected_ident);
967 SkipUntil(tok::semi);
968 return 0;
969 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000970 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
971 Tok.getLocation()));
Steve Naroff72f17fb2007-08-22 22:17:26 +0000972 ConsumeToken(); // the identifier
973
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000974 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000975 break;
976 }
977 // Consume the ';'.
978 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
979 return 0;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000980
Steve Naroff415c1832007-10-10 17:32:04 +0000981 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000982 &ProtocolRefs[0],
983 ProtocolRefs.size());
Chris Lattnere705e5e2008-07-21 22:17:28 +0000984 }
985
Steve Naroff72f17fb2007-08-22 22:17:26 +0000986 // Last, and definitely not least, parse a protocol declaration.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000987 SourceLocation EndProtoLoc;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000988
Chris Lattner2bdedd62008-07-26 04:03:38 +0000989 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000990 if (Tok.is(tok::less) &&
Chris Lattner2bdedd62008-07-26 04:03:38 +0000991 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnere705e5e2008-07-21 22:17:28 +0000992 return 0;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000993
Chris Lattner2bdedd62008-07-26 04:03:38 +0000994 DeclTy *ProtoType =
995 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
996 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar28680d12008-09-26 04:48:09 +0000997 EndProtoLoc, attrList);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000998 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnera40577e2008-10-20 06:10:06 +0000999 return ProtoType;
Chris Lattner4b009652007-07-25 00:24:17 +00001000}
Steve Narofffb367882007-08-20 21:31:48 +00001001
1002/// objc-implementation:
1003/// objc-class-implementation-prologue
1004/// objc-category-implementation-prologue
1005///
1006/// objc-class-implementation-prologue:
1007/// @implementation identifier objc-superclass[opt]
1008/// objc-class-instance-variables[opt]
1009///
1010/// objc-category-implementation-prologue:
1011/// @implementation identifier ( identifier )
1012
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001013Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
1014 SourceLocation atLoc) {
1015 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1016 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1017 ConsumeToken(); // the "implementation" identifier
1018
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001019 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001020 Diag(Tok, diag::err_expected_ident); // missing class or category name.
1021 return 0;
1022 }
1023 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001024 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001025 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1026
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001027 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001028 // we have a category implementation.
1029 SourceLocation lparenLoc = ConsumeParen();
1030 SourceLocation categoryLoc, rparenLoc;
1031 IdentifierInfo *categoryId = 0;
1032
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001033 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001034 categoryId = Tok.getIdentifierInfo();
1035 categoryLoc = ConsumeToken();
1036 } else {
1037 Diag(Tok, diag::err_expected_ident); // missing category name.
1038 return 0;
1039 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001040 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001041 Diag(Tok, diag::err_expected_rparen);
1042 SkipUntil(tok::r_paren, false); // don't stop at ';'
1043 return 0;
1044 }
1045 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +00001046 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +00001047 atLoc, nameId, nameLoc, categoryId,
1048 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001049 ObjCImpDecl = ImplCatType;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001050 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001051 }
1052 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001053 SourceLocation superClassLoc;
1054 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001055 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001056 // We have a super class
1057 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001058 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001059 Diag(Tok, diag::err_expected_ident); // missing super class name.
1060 return 0;
1061 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001062 superClassId = Tok.getIdentifierInfo();
1063 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001064 }
Steve Naroff415c1832007-10-10 17:32:04 +00001065 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +00001066 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001067 superClassId, superClassLoc);
1068
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001069 if (Tok.is(tok::l_brace)) // we have ivars
1070 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001071 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001072
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001073 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001074}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001075
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001076Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1077 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1078 "ParseObjCAtEndDeclaration(): Expected @end");
1079 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001080 if (ObjCImpDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +00001081 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001082 else
1083 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremenek42730c52008-01-07 19:49:32 +00001084 return ObjCImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +00001085}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001086
1087/// compatibility-alias-decl:
1088/// @compatibility_alias alias-name class-name ';'
1089///
1090Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1091 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1092 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1093 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001094 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001095 Diag(Tok, diag::err_expected_ident);
1096 return 0;
1097 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001098 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1099 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001100 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001101 Diag(Tok, diag::err_expected_ident);
1102 return 0;
1103 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001104 IdentifierInfo *classId = Tok.getIdentifierInfo();
1105 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1106 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +00001107 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001108 return 0;
1109 }
1110 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1111 aliasId, aliasLoc,
1112 classId, classLoc);
1113 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +00001114}
1115
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001116/// property-synthesis:
1117/// @synthesize property-ivar-list ';'
1118///
1119/// property-ivar-list:
1120/// property-ivar
1121/// property-ivar-list ',' property-ivar
1122///
1123/// property-ivar:
1124/// identifier
1125/// identifier '=' identifier
1126///
1127Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1128 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1129 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001130 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001131 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001132 Diag(Tok, diag::err_expected_ident);
1133 return 0;
1134 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001135 while (Tok.is(tok::identifier)) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001136 IdentifierInfo *propertyIvar = 0;
1137 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1138 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001139 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001140 // property '=' ivar-name
1141 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001142 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001143 Diag(Tok, diag::err_expected_ident);
1144 break;
1145 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001146 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001147 ConsumeToken(); // consume ivar-name
1148 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001149 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1150 propertyId, propertyIvar);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001151 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001152 break;
1153 ConsumeToken(); // consume ','
1154 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001155 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001156 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1157 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001158}
1159
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001160/// property-dynamic:
1161/// @dynamic property-list
1162///
1163/// property-list:
1164/// identifier
1165/// property-list ',' identifier
1166///
1167Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1168 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1169 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1170 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001171 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001172 Diag(Tok, diag::err_expected_ident);
1173 return 0;
1174 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001175 while (Tok.is(tok::identifier)) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001176 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1177 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1178 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1179 propertyId, 0);
1180
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001181 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001182 break;
1183 ConsumeToken(); // consume ','
1184 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001185 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001186 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1187 return 0;
1188}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001189
1190/// objc-throw-statement:
1191/// throw expression[opt];
1192///
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001193Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1194 ExprResult Res;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001195 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001196 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001197 Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001198 if (Res.isInvalid) {
1199 SkipUntil(tok::semi);
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001200 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001201 }
1202 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001203 ConsumeToken(); // consume ';'
Ted Kremenek42730c52008-01-07 19:49:32 +00001204 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001205}
1206
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001207/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001208/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001209///
1210Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001211 ConsumeToken(); // consume synchronized
1212 if (Tok.isNot(tok::l_paren)) {
1213 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1214 return true;
1215 }
1216 ConsumeParen(); // '('
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001217 ExprResult Res = ParseExpression();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001218 if (Res.isInvalid) {
1219 SkipUntil(tok::semi);
1220 return true;
1221 }
1222 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001223 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001224 return true;
1225 }
1226 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001227 if (Tok.isNot(tok::l_brace)) {
1228 Diag (Tok, diag::err_expected_lbrace);
1229 return true;
1230 }
Steve Naroff70337ac2008-06-04 20:36:13 +00001231 // Enter a scope to hold everything within the compound stmt. Compound
1232 // statements can always hold declarations.
1233 EnterScope(Scope::DeclScope);
1234
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001235 StmtResult SynchBody = ParseCompoundStatementBody();
Steve Naroff70337ac2008-06-04 20:36:13 +00001236
1237 ExitScope();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001238 if (SynchBody.isInvalid)
1239 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1240 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001241}
1242
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001243/// objc-try-catch-statement:
1244/// @try compound-statement objc-catch-list[opt]
1245/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1246///
1247/// objc-catch-list:
1248/// @catch ( parameter-declaration ) compound-statement
1249/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1250/// catch-parameter-declaration:
1251/// parameter-declaration
1252/// '...' [OBJC2]
1253///
Chris Lattner80712392008-03-10 06:06:04 +00001254Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001255 bool catch_or_finally_seen = false;
Steve Naroffc949a462008-02-05 21:27:35 +00001256
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001257 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001258 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001259 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001260 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001261 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001262 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001263 StmtResult FinallyStmt;
Ted Kremenekba849be2008-09-26 17:32:47 +00001264 EnterScope(Scope::DeclScope);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001265 StmtResult TryBody = ParseCompoundStatementBody();
Ted Kremenekba849be2008-09-26 17:32:47 +00001266 ExitScope();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001267 if (TryBody.isInvalid)
1268 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner80712392008-03-10 06:06:04 +00001269
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001270 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001271 // At this point, we need to lookahead to determine if this @ is the start
1272 // of an @catch or @finally. We don't want to consume the @ token if this
1273 // is an @try or @encode or something else.
1274 Token AfterAt = GetLookAheadToken(1);
1275 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1276 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1277 break;
1278
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001279 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001280 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001281 StmtTy *FirstPart = 0;
1282 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001283 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001284 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001285 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001286 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001287 DeclSpec DS;
1288 ParseDeclarationSpecifiers(DS);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001289 // For some odd reason, the name of the exception variable is
1290 // optional. As a result, we need to use PrototypeContext.
1291 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001292 ParseDeclarator(DeclaratorInfo);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001293 if (DeclaratorInfo.getIdentifier()) {
1294 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +00001295 DeclaratorInfo, 0);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001296 StmtResult stmtResult =
1297 Actions.ActOnDeclStmt(aBlockVarDecl,
1298 DS.getSourceRange().getBegin(),
1299 DeclaratorInfo.getSourceRange().getEnd());
1300 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
1301 }
Steve Naroffc949a462008-02-05 21:27:35 +00001302 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001303 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001304 SourceLocation RParenLoc = ConsumeParen();
Chris Lattner8027be62008-02-14 19:27:54 +00001305
1306 StmtResult CatchBody(true);
1307 if (Tok.is(tok::l_brace))
1308 CatchBody = ParseCompoundStatementBody();
1309 else
1310 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001311 if (CatchBody.isInvalid)
1312 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001313 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001314 FirstPart, CatchBody.Val, CatchStmts.Val);
1315 ExitScope();
Steve Naroffc949a462008-02-05 21:27:35 +00001316 } else {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001317 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1318 "@catch clause");
Fariborz Jahanian70952482007-11-01 21:12:44 +00001319 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001320 }
1321 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001322 } else {
1323 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001324 ConsumeToken(); // consume finally
Ted Kremenek3637b892008-09-26 00:31:16 +00001325 EnterScope(Scope::DeclScope);
1326
Chris Lattner8027be62008-02-14 19:27:54 +00001327
1328 StmtResult FinallyBody(true);
1329 if (Tok.is(tok::l_brace))
1330 FinallyBody = ParseCompoundStatementBody();
1331 else
1332 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001333 if (FinallyBody.isInvalid)
1334 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001335 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001336 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001337 catch_or_finally_seen = true;
Ted Kremenek3637b892008-09-26 00:31:16 +00001338 ExitScope();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001339 break;
1340 }
1341 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001342 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001343 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001344 return true;
1345 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001346 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001347 FinallyStmt.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001348}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001349
Steve Naroff81f1bba2007-09-06 21:24:23 +00001350/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001351///
Steve Naroff18c83382007-11-13 23:01:27 +00001352Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremenek42730c52008-01-07 19:49:32 +00001353 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001354 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001355 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001356 ConsumeToken();
1357
Steve Naroff9191a9e82007-11-11 19:54:21 +00001358 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001359 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001360 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001361
1362 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1363 SkipUntil(tok::l_brace, true, true);
1364
1365 // If we didn't find the '{', bail out.
1366 if (Tok.isNot(tok::l_brace))
Steve Naroff18c83382007-11-13 23:01:27 +00001367 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001368 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001369 SourceLocation BraceLoc = Tok.getLocation();
1370
1371 // Enter a scope for the method body.
1372 EnterScope(Scope::FnScope|Scope::DeclScope);
1373
1374 // Tell the actions module that we have entered a method definition with the
Steve Naroff3ac43f92008-07-25 17:57:26 +00001375 // specified Declarator for the method.
1376 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001377
1378 StmtResult FnBody = ParseCompoundStatementBody();
1379
1380 // If the function body could not be parsed, make a bogus compoundstmt.
1381 if (FnBody.isInvalid)
1382 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1383
1384 // Leave the function body scope.
1385 ExitScope();
1386
1387 // TODO: Pass argument information.
Steve Naroff3ac43f92008-07-25 17:57:26 +00001388 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff18c83382007-11-13 23:01:27 +00001389 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001390}
Anders Carlssona66cad42007-08-21 17:43:55 +00001391
Steve Naroffc949a462008-02-05 21:27:35 +00001392Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1393 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001394 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001395 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1396 return ParseObjCThrowStmt(AtLoc);
1397 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1398 return ParseObjCSynchronizedStmt(AtLoc);
1399 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1400 if (Res.isInvalid) {
1401 // If the expression is invalid, skip ahead to the next semicolon. Not
1402 // doing this opens us up to the possibility of infinite loops if
1403 // ParseExpression does not consume any tokens.
1404 SkipUntil(tok::semi);
1405 return true;
1406 }
1407 // Otherwise, eat the semicolon.
1408 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1409 return Actions.ActOnExprStmt(Res.Val);
1410}
1411
Steve Narofffb9dd752007-10-15 20:55:58 +00001412Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001413 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001414 case tok::string_literal: // primary-expression: string-literal
1415 case tok::wide_string_literal:
1416 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1417 default:
Chris Lattnerf9311a92008-08-05 06:19:09 +00001418 if (Tok.getIdentifierInfo() == 0)
1419 return Diag(AtLoc, diag::err_unexpected_at);
1420
1421 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1422 case tok::objc_encode:
1423 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1424 case tok::objc_protocol:
1425 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
1426 case tok::objc_selector:
1427 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
1428 default:
1429 return Diag(AtLoc, diag::err_unexpected_at);
1430 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001431 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001432}
1433
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001434/// objc-message-expr:
1435/// '[' objc-receiver objc-message-args ']'
1436///
1437/// objc-receiver:
1438/// expression
1439/// class-name
1440/// type-name
Chris Lattnered27a532008-01-25 18:59:06 +00001441Parser::ExprResult Parser::ParseObjCMessageExpression() {
1442 assert(Tok.is(tok::l_square) && "'[' expected");
1443 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1444
1445 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001446 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001447 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1448 ConsumeToken();
1449 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1450 }
1451
Chris Lattnerc185e1a2008-09-19 17:44:00 +00001452 ExprResult Res = ParseExpression();
Chris Lattnered27a532008-01-25 18:59:06 +00001453 if (Res.isInvalid) {
Chris Lattnere69015d2008-01-25 19:43:26 +00001454 SkipUntil(tok::r_square);
Chris Lattnered27a532008-01-25 18:59:06 +00001455 return Res;
1456 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001457
Chris Lattnered27a532008-01-25 18:59:06 +00001458 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1459}
1460
1461/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1462/// the rest of a message expression.
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001463///
1464/// objc-message-args:
1465/// objc-selector
1466/// objc-keywordarg-list
1467///
1468/// objc-keywordarg-list:
1469/// objc-keywordarg
1470/// objc-keywordarg-list objc-keywordarg
1471///
1472/// objc-keywordarg:
1473/// selector-name[opt] ':' objc-keywordexpr
1474///
1475/// objc-keywordexpr:
1476/// nonempty-expr-list
1477///
1478/// nonempty-expr-list:
1479/// assignment-expression
1480/// nonempty-expr-list , assignment-expression
1481///
Chris Lattnered27a532008-01-25 18:59:06 +00001482Parser::ExprResult
1483Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1484 IdentifierInfo *ReceiverName,
1485 ExprTy *ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001486 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001487 SourceLocation Loc;
1488 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001489
1490 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1491 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1492
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001493 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001494 while (1) {
1495 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001496 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001497
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001498 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001499 Diag(Tok, diag::err_expected_colon);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001500 // We must manually skip to a ']', otherwise the expression skipper will
1501 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1502 // the enclosing expression.
1503 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001504 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001505 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001506
Steve Naroff4ed9d662007-09-27 14:38:14 +00001507 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001508 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001509 ExprResult Res = ParseAssignmentExpression();
1510 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001511 // We must manually skip to a ']', otherwise the expression skipper will
1512 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1513 // the enclosing expression.
1514 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001515 return Res;
1516 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001517
Steve Naroff253118b2007-09-17 20:25:27 +00001518 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001519 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001520
1521 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001522 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001523 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001524 break;
1525 // We have a selector or a colon, continue parsing.
1526 }
1527 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001528 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001529 ConsumeToken(); // Eat the ','.
1530 /// Parse the expression after ','
1531 ExprResult Res = ParseAssignmentExpression();
1532 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001533 // We must manually skip to a ']', otherwise the expression skipper will
1534 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1535 // the enclosing expression.
1536 SkipUntil(tok::r_square);
Steve Naroff9f176d12007-11-15 13:05:42 +00001537 return Res;
1538 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001539
Steve Naroff9f176d12007-11-15 13:05:42 +00001540 // We have a valid expression.
1541 KeyExprs.push_back(Res.Val);
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001542 }
1543 } else if (!selIdent) {
1544 Diag(Tok, diag::err_expected_ident); // missing selector name.
Chris Lattnerf9311a92008-08-05 06:19:09 +00001545
1546 // We must manually skip to a ']', otherwise the expression skipper will
1547 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1548 // the enclosing expression.
1549 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001550 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001551 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001552
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001553 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001554 Diag(Tok, diag::err_expected_rsquare);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001555 // We must manually skip to a ']', otherwise the expression skipper will
1556 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1557 // the enclosing expression.
1558 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001559 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001560 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001561
Chris Lattnered27a532008-01-25 18:59:06 +00001562 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001563
Steve Narofff9e80db2007-10-05 18:42:47 +00001564 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001565 if (nKeys == 0)
1566 KeyIdents.push_back(selIdent);
1567 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1568
1569 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001570 if (ReceiverName)
Fariborz Jahanian2ce5dc52007-11-12 20:13:27 +00001571 return Actions.ActOnClassMessage(CurScope,
Chris Lattnered27a532008-01-25 18:59:06 +00001572 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001573 &KeyExprs[0], KeyExprs.size());
Chris Lattnered27a532008-01-25 18:59:06 +00001574 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001575 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001576}
1577
Steve Naroff0add5d22007-11-03 11:27:19 +00001578Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001579 ExprResult Res = ParseStringLiteralExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001580 if (Res.isInvalid) return Res;
Chris Lattnerddd3e632007-12-12 01:04:12 +00001581
1582 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1583 // expressions. At this point, we know that the only valid thing that starts
1584 // with '@' is an @"".
1585 llvm::SmallVector<SourceLocation, 4> AtLocs;
1586 llvm::SmallVector<ExprTy*, 4> AtStrings;
1587 AtLocs.push_back(AtLoc);
1588 AtStrings.push_back(Res.Val);
1589
1590 while (Tok.is(tok::at)) {
1591 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001592
Chris Lattnerddd3e632007-12-12 01:04:12 +00001593 ExprResult Res(true); // Invalid unless there is a string literal.
1594 if (isTokenStringLiteral())
1595 Res = ParseStringLiteralExpression();
1596 else
1597 Diag(Tok, diag::err_objc_concat_string);
1598
1599 if (Res.isInvalid) {
1600 while (!AtStrings.empty()) {
1601 Actions.DeleteExpr(AtStrings.back());
1602 AtStrings.pop_back();
1603 }
1604 return Res;
1605 }
1606
1607 AtStrings.push_back(Res.Val);
1608 }
Fariborz Jahanian1a442d32007-12-12 23:55:49 +00001609
Chris Lattnerddd3e632007-12-12 01:04:12 +00001610 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1611 AtStrings.size());
Anders Carlssona66cad42007-08-21 17:43:55 +00001612}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001613
1614/// objc-encode-expression:
1615/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001616Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001617 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001618
1619 SourceLocation EncLoc = ConsumeToken();
1620
Chris Lattnerf9311a92008-08-05 06:19:09 +00001621 if (Tok.isNot(tok::l_paren))
1622 return Diag(Tok, diag::err_expected_lparen_after, "@encode");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001623
1624 SourceLocation LParenLoc = ConsumeParen();
1625
1626 TypeTy *Ty = ParseTypeName();
1627
Anders Carlsson92faeb82007-08-23 15:31:37 +00001628 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001629
Chris Lattnercfd61c82007-10-16 22:51:17 +00001630 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001631 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001632}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001633
1634/// objc-protocol-expression
1635/// @protocol ( protocol-name )
1636
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001637Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001638{
1639 SourceLocation ProtoLoc = ConsumeToken();
1640
Chris Lattnerf9311a92008-08-05 06:19:09 +00001641 if (Tok.isNot(tok::l_paren))
1642 return Diag(Tok, diag::err_expected_lparen_after, "@protocol");
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001643
1644 SourceLocation LParenLoc = ConsumeParen();
1645
Chris Lattnerf9311a92008-08-05 06:19:09 +00001646 if (Tok.isNot(tok::identifier))
1647 return Diag(Tok, diag::err_expected_ident);
1648
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001649 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001650 ConsumeToken();
1651
Anders Carlsson92faeb82007-08-23 15:31:37 +00001652 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001653
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001654 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1655 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001656}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001657
1658/// objc-selector-expression
1659/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001660Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001661{
1662 SourceLocation SelectorLoc = ConsumeToken();
1663
Chris Lattnerf9311a92008-08-05 06:19:09 +00001664 if (Tok.isNot(tok::l_paren))
1665 return Diag(Tok, diag::err_expected_lparen_after, "@selector");
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001666
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001667 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001668 SourceLocation LParenLoc = ConsumeParen();
1669 SourceLocation sLoc;
1670 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001671 if (!SelIdent && Tok.isNot(tok::colon))
1672 return Diag(Tok, diag::err_expected_ident); // missing selector name.
1673
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001674 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001675 unsigned nColons = 0;
1676 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001677 while (1) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001678 if (Tok.isNot(tok::colon))
1679 return Diag(Tok, diag::err_expected_colon);
1680
Chris Lattner847f5c12007-12-27 19:57:00 +00001681 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001682 ConsumeToken(); // Eat the ':'.
1683 if (Tok.is(tok::r_paren))
1684 break;
1685 // Check for another keyword selector.
1686 SourceLocation Loc;
1687 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001688 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001689 if (!SelIdent && Tok.isNot(tok::colon))
1690 break;
1691 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001692 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001693 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001694 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001695 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001696 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001697 }