blob: cc5a74d7c6f8870aa2f1929dcd9cb1ae8f03cfb1 [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
Chris Lattner9019ae52008-10-20 06:15:13 +0000313 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
314 tok::at);
315
Chris Lattnere48b46b2008-10-20 05:46:22 +0000316 // Convert them all to property declarations.
317 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
318 FieldDeclarator &FD = FieldDeclarators[i];
319 // Install the property declarator into interfaceDecl.
320 Selector GetterSel =
Chris Lattner9019ae52008-10-20 06:15:13 +0000321 PP.getSelectorTable().getNullarySelector(OCDS.getGetterName()
322 ? OCDS.getGetterName()
323 : FD.D.getIdentifier());
Chris Lattnere48b46b2008-10-20 05:46:22 +0000324 IdentifierInfo *SetterName = OCDS.getSetterName();
325 if (!SetterName)
326 SetterName = constructSetterName(PP.getIdentifierTable(),
327 FD.D.getIdentifier());
328 Selector SetterSel =
329 PP.getSelectorTable().getUnarySelector(SetterName);
330 DeclTy *Property = Actions.ActOnProperty(CurScope,
331 AtLoc, FD, OCDS,
332 GetterSel, SetterSel,
333 MethodImplKind);
334 allProperties.push_back(Property);
335 }
Chris Lattnercba730b2008-10-20 05:57:40 +0000336 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000337 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000338 }
Chris Lattnera40577e2008-10-20 06:10:06 +0000339
340 // We break out of the big loop in two cases: when we see @end or when we see
341 // EOF. In the former case, eat the @end. In the later case, emit an error.
342 if (Tok.isObjCAtKeyword(tok::objc_end))
343 ConsumeToken(); // the "end" identifier
344 else
345 Diag(Tok, diag::err_objc_missing_end);
346
Chris Lattnercba730b2008-10-20 05:57:40 +0000347 // Insert collected methods declarations into the @interface object.
Chris Lattnera40577e2008-10-20 06:10:06 +0000348 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8c945b12008-06-06 16:45:15 +0000349 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
350 allMethods.empty() ? 0 : &allMethods[0],
351 allMethods.size(),
352 allProperties.empty() ? 0 : &allProperties[0],
353 allProperties.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000354}
355
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000356/// Parse property attribute declarations.
357///
358/// property-attr-decl: '(' property-attrlist ')'
359/// property-attrlist:
360/// property-attribute
361/// property-attrlist ',' property-attribute
362/// property-attribute:
363/// getter '=' identifier
364/// setter '=' identifier ':'
365/// readonly
366/// readwrite
367/// assign
368/// retain
369/// copy
370/// nonatomic
371///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000372void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000373 SourceLocation loc = ConsumeParen(); // consume '('
374 while (isObjCPropertyAttribute()) {
375 const IdentifierInfo *II = Tok.getIdentifierInfo();
376 // getter/setter require extra treatment.
Ted Kremenek42730c52008-01-07 19:49:32 +0000377 if (II == ObjCPropertyAttrs[objc_getter] ||
378 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000379 // skip getter/setter part.
380 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000381 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000382 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000383 if (Tok.is(tok::identifier)) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000384 if (II == ObjCPropertyAttrs[objc_setter]) {
385 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000386 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000387 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000388 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000389 Diag(loc, diag::err_expected_colon);
390 SkipUntil(tok::r_paren,true,true);
391 break;
392 }
393 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000394 else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000395 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000396 DS.setGetterName(Tok.getIdentifierInfo());
397 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000398 }
399 else {
400 Diag(loc, diag::err_expected_ident);
Chris Lattner847f5c12007-12-27 19:57:00 +0000401 SkipUntil(tok::r_paren,true,true);
402 break;
403 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000404 }
405 else {
406 Diag(loc, diag::err_objc_expected_equal);
407 SkipUntil(tok::r_paren,true,true);
408 break;
409 }
410 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000411
Ted Kremenek42730c52008-01-07 19:49:32 +0000412 else if (II == ObjCPropertyAttrs[objc_readonly])
413 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
414 else if (II == ObjCPropertyAttrs[objc_assign])
415 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
416 else if (II == ObjCPropertyAttrs[objc_readwrite])
417 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
418 else if (II == ObjCPropertyAttrs[objc_retain])
419 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
420 else if (II == ObjCPropertyAttrs[objc_copy])
421 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
422 else if (II == ObjCPropertyAttrs[objc_nonatomic])
423 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000424
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000425 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000426 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000427 loc = ConsumeToken();
428 continue;
429 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000430 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000431 break;
432 Diag(loc, diag::err_expected_rparen);
433 SkipUntil(tok::semi);
434 return;
435 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000436 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000437 ConsumeParen();
438 else {
439 Diag(loc, diag::err_objc_expected_property_attr);
440 SkipUntil(tok::r_paren); // recover from error inside attribute list
441 }
442}
443
Steve Naroff81f1bba2007-09-06 21:24:23 +0000444/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000445/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000446/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000447///
448/// objc-instance-method: '-'
449/// objc-class-method: '+'
450///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000451/// objc-method-attributes: [OBJC2]
452/// __attribute__((deprecated))
453///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000454Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000455 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000456 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000457
458 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000459 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000460
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000461 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000462 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000463 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000464 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000465}
466
467/// objc-selector:
468/// identifier
469/// one of
470/// enum struct union if else while do for switch case default
471/// break continue return goto asm sizeof typeof __alignof
472/// unsigned long const short volatile signed restrict _Complex
473/// in out inout bycopy byref oneway int char float double void _Bool
474///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000475IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000476 switch (Tok.getKind()) {
477 default:
478 return 0;
479 case tok::identifier:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000480 case tok::kw_asm:
Chris Lattnerd031a452007-10-07 02:00:24 +0000481 case tok::kw_auto:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000482 case tok::kw_bool:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000483 case tok::kw_break:
484 case tok::kw_case:
485 case tok::kw_catch:
486 case tok::kw_char:
487 case tok::kw_class:
488 case tok::kw_const:
489 case tok::kw_const_cast:
490 case tok::kw_continue:
491 case tok::kw_default:
492 case tok::kw_delete:
493 case tok::kw_do:
494 case tok::kw_double:
495 case tok::kw_dynamic_cast:
496 case tok::kw_else:
497 case tok::kw_enum:
498 case tok::kw_explicit:
499 case tok::kw_export:
500 case tok::kw_extern:
501 case tok::kw_false:
502 case tok::kw_float:
503 case tok::kw_for:
504 case tok::kw_friend:
505 case tok::kw_goto:
506 case tok::kw_if:
507 case tok::kw_inline:
508 case tok::kw_int:
509 case tok::kw_long:
510 case tok::kw_mutable:
511 case tok::kw_namespace:
512 case tok::kw_new:
513 case tok::kw_operator:
514 case tok::kw_private:
515 case tok::kw_protected:
516 case tok::kw_public:
517 case tok::kw_register:
518 case tok::kw_reinterpret_cast:
519 case tok::kw_restrict:
520 case tok::kw_return:
521 case tok::kw_short:
522 case tok::kw_signed:
523 case tok::kw_sizeof:
524 case tok::kw_static:
525 case tok::kw_static_cast:
526 case tok::kw_struct:
527 case tok::kw_switch:
528 case tok::kw_template:
529 case tok::kw_this:
530 case tok::kw_throw:
531 case tok::kw_true:
532 case tok::kw_try:
533 case tok::kw_typedef:
534 case tok::kw_typeid:
535 case tok::kw_typename:
536 case tok::kw_typeof:
537 case tok::kw_union:
538 case tok::kw_unsigned:
539 case tok::kw_using:
540 case tok::kw_virtual:
541 case tok::kw_void:
542 case tok::kw_volatile:
543 case tok::kw_wchar_t:
544 case tok::kw_while:
Chris Lattnerd031a452007-10-07 02:00:24 +0000545 case tok::kw__Bool:
546 case tok::kw__Complex:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000547 case tok::kw___alignof:
Chris Lattnerd031a452007-10-07 02:00:24 +0000548 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000549 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000550 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000551 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000552}
553
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000554/// property-attrlist: one of
555/// readonly getter setter assign retain copy nonatomic
556///
557bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000558 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000559 const IdentifierInfo *II = Tok.getIdentifierInfo();
560 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremenek42730c52008-01-07 19:49:32 +0000561 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000562 }
563 return false;
564}
565
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000566/// objc-for-collection-in: 'in'
567///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000568bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000569 // FIXME: May have to do additional look-ahead to only allow for
570 // valid tokens following an 'in'; such as an identifier, unary operators,
571 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000572 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner818350c2008-08-23 02:02:23 +0000573 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000574}
575
Ted Kremenek42730c52008-01-07 19:49:32 +0000576/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000577/// qualifier list and builds their bitmask representation in the input
578/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000579///
580/// objc-type-qualifiers:
581/// objc-type-qualifier
582/// objc-type-qualifiers objc-type-qualifier
583///
Ted Kremenek42730c52008-01-07 19:49:32 +0000584void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000585 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000586 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000587 return;
588
589 const IdentifierInfo *II = Tok.getIdentifierInfo();
590 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000591 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000592 continue;
593
Ted Kremenek42730c52008-01-07 19:49:32 +0000594 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000595 switch (i) {
596 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000597 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
598 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
599 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
600 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
601 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
602 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000603 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000604 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000605 ConsumeToken();
606 II = 0;
607 break;
608 }
609
610 // If this wasn't a recognized qualifier, bail out.
611 if (II) return;
612 }
613}
614
615/// objc-type-name:
616/// '(' objc-type-qualifiers[opt] type-name ')'
617/// '(' objc-type-qualifiers[opt] ')'
618///
Ted Kremenek42730c52008-01-07 19:49:32 +0000619Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000620 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000621
622 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattnerb5769332008-08-23 01:48:03 +0000623 SourceLocation TypeStartLoc = Tok.getLocation();
Chris Lattner265c8172007-09-27 15:15:46 +0000624 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000625
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000626 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000627 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000628
Steve Naroff0bbffd82007-08-22 16:35:03 +0000629 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000630 Ty = ParseTypeName();
631 // FIXME: back when Sema support is in place...
632 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000633 }
Chris Lattnerb5769332008-08-23 01:48:03 +0000634
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000635 if (Tok.isNot(tok::r_paren)) {
Chris Lattnerb5769332008-08-23 01:48:03 +0000636 // If we didn't eat any tokens, then this isn't a type.
637 if (Tok.getLocation() == TypeStartLoc) {
638 Diag(Tok.getLocation(), diag::err_expected_type);
639 SkipUntil(tok::r_brace);
640 } else {
641 // Otherwise, we found *something*, but didn't get a ')' in the right
642 // place. Emit an error then return what we have as the type.
643 MatchRHSPunctuation(tok::r_paren, LParenLoc);
644 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000645 }
646 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000647 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000648}
649
650/// objc-method-decl:
651/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000652/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000653/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000654/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000655///
656/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000657/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000658/// objc-keyword-selector objc-keyword-decl
659///
660/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000661/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
662/// objc-selector ':' objc-keyword-attributes[opt] identifier
663/// ':' objc-type-name objc-keyword-attributes[opt] identifier
664/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000665///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000666/// objc-parmlist:
667/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000668///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000669/// objc-parms:
670/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000671///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000672/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000673/// , ...
674///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000675/// objc-keyword-attributes: [OBJC2]
676/// __attribute__((unused))
677///
Steve Naroff3774dd92007-10-26 20:53:56 +0000678Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000679 tok::TokenKind mType,
680 DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000681 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000682{
Chris Lattnerb5769332008-08-23 01:48:03 +0000683 // Parse the return type if present.
Chris Lattnerd031a452007-10-07 02:00:24 +0000684 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000685 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000686 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000687 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnerb5769332008-08-23 01:48:03 +0000688
Steve Naroff3774dd92007-10-26 20:53:56 +0000689 SourceLocation selLoc;
690 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerb5769332008-08-23 01:48:03 +0000691
692 if (!SelIdent) { // missing selector name.
693 Diag(Tok.getLocation(), diag::err_expected_selector_for_method,
694 SourceRange(mLoc, Tok.getLocation()));
695 // Skip until we get a ; or {}.
696 SkipUntil(tok::r_brace);
697 return 0;
698 }
699
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000700 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000701 // If attributes exist after the method, parse them.
702 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000703 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000704 MethodAttrs = ParseAttributes();
705
706 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000707 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000708 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000709 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000710 }
Steve Naroff304ed392007-09-05 23:30:30 +0000711
Steve Naroff4ed9d662007-09-27 14:38:14 +0000712 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
713 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremenek42730c52008-01-07 19:49:32 +0000714 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000715 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000716
717 Action::TypeTy *TypeInfo;
718 while (1) {
719 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000720
Chris Lattnerd031a452007-10-07 02:00:24 +0000721 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000722 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000723 Diag(Tok, diag::err_expected_colon);
724 break;
725 }
726 ConsumeToken(); // Eat the ':'.
Ted Kremenek42730c52008-01-07 19:49:32 +0000727 ObjCDeclSpec DSType;
Chris Lattnerb5769332008-08-23 01:48:03 +0000728 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000729 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerd031a452007-10-07 02:00:24 +0000730 else
731 TypeInfo = 0;
732 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000733 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000734
Chris Lattnerd031a452007-10-07 02:00:24 +0000735 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000736 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000737 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000738
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000739 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000740 Diag(Tok, diag::err_expected_ident); // missing argument name.
741 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000742 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000743 ArgNames.push_back(Tok.getIdentifierInfo());
744 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000745
Chris Lattnerd031a452007-10-07 02:00:24 +0000746 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000747 SourceLocation Loc;
748 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000749 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000750 break;
751 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000752 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000753
Steve Naroff29fe7462007-11-15 12:35:21 +0000754 bool isVariadic = false;
755
Chris Lattnerd031a452007-10-07 02:00:24 +0000756 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000757 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000758 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000759 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000760 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000761 ConsumeToken();
762 break;
763 }
Steve Naroff29fe7462007-11-15 12:35:21 +0000764 // FIXME: implement this...
Chris Lattnerd031a452007-10-07 02:00:24 +0000765 // Parse the c-style argument declaration-specifier.
766 DeclSpec DS;
767 ParseDeclarationSpecifiers(DS);
768 // Parse the declarator.
769 Declarator ParmDecl(DS, Declarator::PrototypeContext);
770 ParseDeclarator(ParmDecl);
771 }
772
773 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000774 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000775 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000776 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000777 MethodAttrs = ParseAttributes();
778
779 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
780 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000781 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000782 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000783 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff29fe7462007-11-15 12:35:21 +0000784 &ArgNames[0], MethodAttrs,
785 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000786}
787
Steve Narofffb367882007-08-20 21:31:48 +0000788/// objc-protocol-refs:
789/// '<' identifier-list '>'
790///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000791bool Parser::
Chris Lattner2bdedd62008-07-26 04:03:38 +0000792ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
793 bool WarnOnDeclarations, SourceLocation &EndLoc) {
794 assert(Tok.is(tok::less) && "expected <");
795
796 ConsumeToken(); // the "<"
797
798 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
799
800 while (1) {
801 if (Tok.isNot(tok::identifier)) {
802 Diag(Tok, diag::err_expected_ident);
803 SkipUntil(tok::greater);
804 return true;
805 }
806 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
807 Tok.getLocation()));
808 ConsumeToken();
809
810 if (Tok.isNot(tok::comma))
811 break;
812 ConsumeToken();
813 }
814
815 // Consume the '>'.
816 if (Tok.isNot(tok::greater)) {
817 Diag(Tok, diag::err_expected_greater);
818 return true;
819 }
820
821 EndLoc = ConsumeAnyToken();
822
823 // Convert the list of protocols identifiers into a list of protocol decls.
824 Actions.FindProtocolDeclaration(WarnOnDeclarations,
825 &ProtocolIdents[0], ProtocolIdents.size(),
826 Protocols);
827 return false;
828}
829
Steve Narofffb367882007-08-20 21:31:48 +0000830/// objc-class-instance-variables:
831/// '{' objc-instance-variable-decl-list[opt] '}'
832///
833/// objc-instance-variable-decl-list:
834/// objc-visibility-spec
835/// objc-instance-variable-decl ';'
836/// ';'
837/// objc-instance-variable-decl-list objc-visibility-spec
838/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
839/// objc-instance-variable-decl-list ';'
840///
841/// objc-visibility-spec:
842/// @private
843/// @protected
844/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000845/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000846///
847/// objc-instance-variable-decl:
848/// struct-declaration
849///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000850void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
851 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000852 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000853 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000854 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
855
Steve Naroffc4474992007-08-21 21:17:12 +0000856 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000857
Fariborz Jahanian7c420a72008-04-29 23:03:51 +0000858 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffc4474992007-08-21 21:17:12 +0000859 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000860 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000861 // Each iteration of this loop reads one objc-instance-variable-decl.
862
863 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000864 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000865 Diag(Tok, diag::ext_extra_struct_semi);
866 ConsumeToken();
867 continue;
868 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000869
Steve Naroffc4474992007-08-21 21:17:12 +0000870 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000871 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000872 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000873 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000874 case tok::objc_private:
875 case tok::objc_public:
876 case tok::objc_protected:
877 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000878 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000879 ConsumeToken();
880 continue;
881 default:
882 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000883 continue;
884 }
885 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000886
887 // Parse all the comma separated declarators.
888 DeclSpec DS;
889 FieldDeclarators.clear();
890 ParseStructDeclaration(DS, FieldDeclarators);
891
892 // Convert them all to fields.
893 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
894 FieldDeclarator &FD = FieldDeclarators[i];
895 // Install the declarator into interfaceDecl.
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000896 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattner3dd8d392008-04-10 06:46:29 +0000897 DS.getSourceRange().getBegin(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000898 FD.D, FD.BitfieldSize, visibility);
Chris Lattner3dd8d392008-04-10 06:46:29 +0000899 AllIvarDecls.push_back(Field);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000900 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000901
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000902 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000903 ConsumeToken();
Steve Naroffc4474992007-08-21 21:17:12 +0000904 } else {
905 Diag(Tok, diag::err_expected_semi_decl_list);
906 // Skip to end of block or statement
907 SkipUntil(tok::r_brace, true, true);
908 }
909 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000910 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000911 // Call ActOnFields() even if we don't have any decls. This is useful
912 // for code rewriting tools that need to be aware of the empty list.
913 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
914 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +0000915 LBraceLoc, RBraceLoc, 0);
Steve Naroffc4474992007-08-21 21:17:12 +0000916 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000917}
Steve Narofffb367882007-08-20 21:31:48 +0000918
919/// objc-protocol-declaration:
920/// objc-protocol-definition
921/// objc-protocol-forward-reference
922///
923/// objc-protocol-definition:
924/// @protocol identifier
925/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000926/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000927/// @end
928///
929/// objc-protocol-forward-reference:
930/// @protocol identifier-list ';'
931///
932/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000933/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000934/// semicolon in the first alternative if objc-protocol-refs are omitted.
Daniel Dunbar28680d12008-09-26 04:48:09 +0000935Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
936 AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000937 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000938 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
939 ConsumeToken(); // the "protocol" identifier
940
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000941 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000942 Diag(Tok, diag::err_expected_ident); // missing protocol name.
943 return 0;
944 }
945 // Save the protocol name, then consume it.
946 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
947 SourceLocation nameLoc = ConsumeToken();
948
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000949 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000950 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000951 ConsumeToken();
Chris Lattnere705e5e2008-07-21 22:17:28 +0000952 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000953 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000954
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000955 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000956 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
957 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
958
Steve Naroff72f17fb2007-08-22 22:17:26 +0000959 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000960 while (1) {
961 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000962 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000963 Diag(Tok, diag::err_expected_ident);
964 SkipUntil(tok::semi);
965 return 0;
966 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000967 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
968 Tok.getLocation()));
Steve Naroff72f17fb2007-08-22 22:17:26 +0000969 ConsumeToken(); // the identifier
970
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000971 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000972 break;
973 }
974 // Consume the ';'.
975 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
976 return 0;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000977
Steve Naroff415c1832007-10-10 17:32:04 +0000978 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000979 &ProtocolRefs[0],
980 ProtocolRefs.size());
Chris Lattnere705e5e2008-07-21 22:17:28 +0000981 }
982
Steve Naroff72f17fb2007-08-22 22:17:26 +0000983 // Last, and definitely not least, parse a protocol declaration.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000984 SourceLocation EndProtoLoc;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000985
Chris Lattner2bdedd62008-07-26 04:03:38 +0000986 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000987 if (Tok.is(tok::less) &&
Chris Lattner2bdedd62008-07-26 04:03:38 +0000988 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnere705e5e2008-07-21 22:17:28 +0000989 return 0;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000990
Chris Lattner2bdedd62008-07-26 04:03:38 +0000991 DeclTy *ProtoType =
992 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
993 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar28680d12008-09-26 04:48:09 +0000994 EndProtoLoc, attrList);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000995 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnera40577e2008-10-20 06:10:06 +0000996 return ProtoType;
Chris Lattner4b009652007-07-25 00:24:17 +0000997}
Steve Narofffb367882007-08-20 21:31:48 +0000998
999/// objc-implementation:
1000/// objc-class-implementation-prologue
1001/// objc-category-implementation-prologue
1002///
1003/// objc-class-implementation-prologue:
1004/// @implementation identifier objc-superclass[opt]
1005/// objc-class-instance-variables[opt]
1006///
1007/// objc-category-implementation-prologue:
1008/// @implementation identifier ( identifier )
1009
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001010Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
1011 SourceLocation atLoc) {
1012 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1013 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1014 ConsumeToken(); // the "implementation" identifier
1015
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001016 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001017 Diag(Tok, diag::err_expected_ident); // missing class or category name.
1018 return 0;
1019 }
1020 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001021 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001022 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1023
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001024 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001025 // we have a category implementation.
1026 SourceLocation lparenLoc = ConsumeParen();
1027 SourceLocation categoryLoc, rparenLoc;
1028 IdentifierInfo *categoryId = 0;
1029
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001030 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001031 categoryId = Tok.getIdentifierInfo();
1032 categoryLoc = ConsumeToken();
1033 } else {
1034 Diag(Tok, diag::err_expected_ident); // missing category name.
1035 return 0;
1036 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001037 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001038 Diag(Tok, diag::err_expected_rparen);
1039 SkipUntil(tok::r_paren, false); // don't stop at ';'
1040 return 0;
1041 }
1042 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +00001043 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +00001044 atLoc, nameId, nameLoc, categoryId,
1045 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001046 ObjCImpDecl = ImplCatType;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001047 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001048 }
1049 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001050 SourceLocation superClassLoc;
1051 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001052 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001053 // We have a super class
1054 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001055 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001056 Diag(Tok, diag::err_expected_ident); // missing super class name.
1057 return 0;
1058 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001059 superClassId = Tok.getIdentifierInfo();
1060 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001061 }
Steve Naroff415c1832007-10-10 17:32:04 +00001062 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +00001063 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001064 superClassId, superClassLoc);
1065
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001066 if (Tok.is(tok::l_brace)) // we have ivars
1067 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001068 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001069
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001070 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001071}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001072
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001073Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1074 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1075 "ParseObjCAtEndDeclaration(): Expected @end");
1076 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001077 if (ObjCImpDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +00001078 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001079 else
1080 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremenek42730c52008-01-07 19:49:32 +00001081 return ObjCImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +00001082}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001083
1084/// compatibility-alias-decl:
1085/// @compatibility_alias alias-name class-name ';'
1086///
1087Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1088 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1089 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1090 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001091 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001092 Diag(Tok, diag::err_expected_ident);
1093 return 0;
1094 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001095 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1096 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001097 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001098 Diag(Tok, diag::err_expected_ident);
1099 return 0;
1100 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001101 IdentifierInfo *classId = Tok.getIdentifierInfo();
1102 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1103 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +00001104 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001105 return 0;
1106 }
1107 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1108 aliasId, aliasLoc,
1109 classId, classLoc);
1110 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +00001111}
1112
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001113/// property-synthesis:
1114/// @synthesize property-ivar-list ';'
1115///
1116/// property-ivar-list:
1117/// property-ivar
1118/// property-ivar-list ',' property-ivar
1119///
1120/// property-ivar:
1121/// identifier
1122/// identifier '=' identifier
1123///
1124Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1125 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1126 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001127 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001128 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001129 Diag(Tok, diag::err_expected_ident);
1130 return 0;
1131 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001132 while (Tok.is(tok::identifier)) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001133 IdentifierInfo *propertyIvar = 0;
1134 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1135 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001136 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001137 // property '=' ivar-name
1138 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001139 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001140 Diag(Tok, diag::err_expected_ident);
1141 break;
1142 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001143 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001144 ConsumeToken(); // consume ivar-name
1145 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001146 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1147 propertyId, propertyIvar);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001148 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001149 break;
1150 ConsumeToken(); // consume ','
1151 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001152 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001153 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1154 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001155}
1156
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001157/// property-dynamic:
1158/// @dynamic property-list
1159///
1160/// property-list:
1161/// identifier
1162/// property-list ',' identifier
1163///
1164Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1165 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1166 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1167 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001168 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001169 Diag(Tok, diag::err_expected_ident);
1170 return 0;
1171 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001172 while (Tok.is(tok::identifier)) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001173 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1174 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1175 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1176 propertyId, 0);
1177
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001178 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001179 break;
1180 ConsumeToken(); // consume ','
1181 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001182 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001183 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1184 return 0;
1185}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001186
1187/// objc-throw-statement:
1188/// throw expression[opt];
1189///
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001190Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1191 ExprResult Res;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001192 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001193 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001194 Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001195 if (Res.isInvalid) {
1196 SkipUntil(tok::semi);
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001197 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001198 }
1199 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001200 ConsumeToken(); // consume ';'
Ted Kremenek42730c52008-01-07 19:49:32 +00001201 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001202}
1203
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001204/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001205/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001206///
1207Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001208 ConsumeToken(); // consume synchronized
1209 if (Tok.isNot(tok::l_paren)) {
1210 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1211 return true;
1212 }
1213 ConsumeParen(); // '('
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001214 ExprResult Res = ParseExpression();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001215 if (Res.isInvalid) {
1216 SkipUntil(tok::semi);
1217 return true;
1218 }
1219 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001220 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001221 return true;
1222 }
1223 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001224 if (Tok.isNot(tok::l_brace)) {
1225 Diag (Tok, diag::err_expected_lbrace);
1226 return true;
1227 }
Steve Naroff70337ac2008-06-04 20:36:13 +00001228 // Enter a scope to hold everything within the compound stmt. Compound
1229 // statements can always hold declarations.
1230 EnterScope(Scope::DeclScope);
1231
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001232 StmtResult SynchBody = ParseCompoundStatementBody();
Steve Naroff70337ac2008-06-04 20:36:13 +00001233
1234 ExitScope();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001235 if (SynchBody.isInvalid)
1236 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1237 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001238}
1239
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001240/// objc-try-catch-statement:
1241/// @try compound-statement objc-catch-list[opt]
1242/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1243///
1244/// objc-catch-list:
1245/// @catch ( parameter-declaration ) compound-statement
1246/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1247/// catch-parameter-declaration:
1248/// parameter-declaration
1249/// '...' [OBJC2]
1250///
Chris Lattner80712392008-03-10 06:06:04 +00001251Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001252 bool catch_or_finally_seen = false;
Steve Naroffc949a462008-02-05 21:27:35 +00001253
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001254 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001255 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001256 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001257 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001258 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001259 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001260 StmtResult FinallyStmt;
Ted Kremenekba849be2008-09-26 17:32:47 +00001261 EnterScope(Scope::DeclScope);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001262 StmtResult TryBody = ParseCompoundStatementBody();
Ted Kremenekba849be2008-09-26 17:32:47 +00001263 ExitScope();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001264 if (TryBody.isInvalid)
1265 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner80712392008-03-10 06:06:04 +00001266
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001267 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001268 // At this point, we need to lookahead to determine if this @ is the start
1269 // of an @catch or @finally. We don't want to consume the @ token if this
1270 // is an @try or @encode or something else.
1271 Token AfterAt = GetLookAheadToken(1);
1272 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1273 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1274 break;
1275
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001276 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001277 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001278 StmtTy *FirstPart = 0;
1279 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001280 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001281 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001282 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001283 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001284 DeclSpec DS;
1285 ParseDeclarationSpecifiers(DS);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001286 // For some odd reason, the name of the exception variable is
1287 // optional. As a result, we need to use PrototypeContext.
1288 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001289 ParseDeclarator(DeclaratorInfo);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001290 if (DeclaratorInfo.getIdentifier()) {
1291 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +00001292 DeclaratorInfo, 0);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001293 StmtResult stmtResult =
1294 Actions.ActOnDeclStmt(aBlockVarDecl,
1295 DS.getSourceRange().getBegin(),
1296 DeclaratorInfo.getSourceRange().getEnd());
1297 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
1298 }
Steve Naroffc949a462008-02-05 21:27:35 +00001299 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001300 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001301 SourceLocation RParenLoc = ConsumeParen();
Chris Lattner8027be62008-02-14 19:27:54 +00001302
1303 StmtResult CatchBody(true);
1304 if (Tok.is(tok::l_brace))
1305 CatchBody = ParseCompoundStatementBody();
1306 else
1307 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001308 if (CatchBody.isInvalid)
1309 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001310 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001311 FirstPart, CatchBody.Val, CatchStmts.Val);
1312 ExitScope();
Steve Naroffc949a462008-02-05 21:27:35 +00001313 } else {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001314 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1315 "@catch clause");
Fariborz Jahanian70952482007-11-01 21:12:44 +00001316 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001317 }
1318 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001319 } else {
1320 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001321 ConsumeToken(); // consume finally
Ted Kremenek3637b892008-09-26 00:31:16 +00001322 EnterScope(Scope::DeclScope);
1323
Chris Lattner8027be62008-02-14 19:27:54 +00001324
1325 StmtResult FinallyBody(true);
1326 if (Tok.is(tok::l_brace))
1327 FinallyBody = ParseCompoundStatementBody();
1328 else
1329 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001330 if (FinallyBody.isInvalid)
1331 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001332 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001333 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001334 catch_or_finally_seen = true;
Ted Kremenek3637b892008-09-26 00:31:16 +00001335 ExitScope();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001336 break;
1337 }
1338 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001339 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001340 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001341 return true;
1342 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001343 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001344 FinallyStmt.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001345}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001346
Steve Naroff81f1bba2007-09-06 21:24:23 +00001347/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001348///
Steve Naroff18c83382007-11-13 23:01:27 +00001349Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremenek42730c52008-01-07 19:49:32 +00001350 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001351 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001352 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001353 ConsumeToken();
1354
Steve Naroff9191a9e82007-11-11 19:54:21 +00001355 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001356 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001357 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001358
1359 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1360 SkipUntil(tok::l_brace, true, true);
1361
1362 // If we didn't find the '{', bail out.
1363 if (Tok.isNot(tok::l_brace))
Steve Naroff18c83382007-11-13 23:01:27 +00001364 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001365 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001366 SourceLocation BraceLoc = Tok.getLocation();
1367
1368 // Enter a scope for the method body.
1369 EnterScope(Scope::FnScope|Scope::DeclScope);
1370
1371 // Tell the actions module that we have entered a method definition with the
Steve Naroff3ac43f92008-07-25 17:57:26 +00001372 // specified Declarator for the method.
1373 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001374
1375 StmtResult FnBody = ParseCompoundStatementBody();
1376
1377 // If the function body could not be parsed, make a bogus compoundstmt.
1378 if (FnBody.isInvalid)
1379 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1380
1381 // Leave the function body scope.
1382 ExitScope();
1383
1384 // TODO: Pass argument information.
Steve Naroff3ac43f92008-07-25 17:57:26 +00001385 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff18c83382007-11-13 23:01:27 +00001386 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001387}
Anders Carlssona66cad42007-08-21 17:43:55 +00001388
Steve Naroffc949a462008-02-05 21:27:35 +00001389Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1390 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001391 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001392 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1393 return ParseObjCThrowStmt(AtLoc);
1394 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1395 return ParseObjCSynchronizedStmt(AtLoc);
1396 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1397 if (Res.isInvalid) {
1398 // If the expression is invalid, skip ahead to the next semicolon. Not
1399 // doing this opens us up to the possibility of infinite loops if
1400 // ParseExpression does not consume any tokens.
1401 SkipUntil(tok::semi);
1402 return true;
1403 }
1404 // Otherwise, eat the semicolon.
1405 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1406 return Actions.ActOnExprStmt(Res.Val);
1407}
1408
Steve Narofffb9dd752007-10-15 20:55:58 +00001409Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001410 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001411 case tok::string_literal: // primary-expression: string-literal
1412 case tok::wide_string_literal:
1413 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1414 default:
Chris Lattnerf9311a92008-08-05 06:19:09 +00001415 if (Tok.getIdentifierInfo() == 0)
1416 return Diag(AtLoc, diag::err_unexpected_at);
1417
1418 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1419 case tok::objc_encode:
1420 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1421 case tok::objc_protocol:
1422 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
1423 case tok::objc_selector:
1424 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
1425 default:
1426 return Diag(AtLoc, diag::err_unexpected_at);
1427 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001428 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001429}
1430
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001431/// objc-message-expr:
1432/// '[' objc-receiver objc-message-args ']'
1433///
1434/// objc-receiver:
1435/// expression
1436/// class-name
1437/// type-name
Chris Lattnered27a532008-01-25 18:59:06 +00001438Parser::ExprResult Parser::ParseObjCMessageExpression() {
1439 assert(Tok.is(tok::l_square) && "'[' expected");
1440 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1441
1442 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001443 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001444 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1445 ConsumeToken();
1446 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1447 }
1448
Chris Lattnerc185e1a2008-09-19 17:44:00 +00001449 ExprResult Res = ParseExpression();
Chris Lattnered27a532008-01-25 18:59:06 +00001450 if (Res.isInvalid) {
Chris Lattnere69015d2008-01-25 19:43:26 +00001451 SkipUntil(tok::r_square);
Chris Lattnered27a532008-01-25 18:59:06 +00001452 return Res;
1453 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001454
Chris Lattnered27a532008-01-25 18:59:06 +00001455 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1456}
1457
1458/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1459/// the rest of a message expression.
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001460///
1461/// objc-message-args:
1462/// objc-selector
1463/// objc-keywordarg-list
1464///
1465/// objc-keywordarg-list:
1466/// objc-keywordarg
1467/// objc-keywordarg-list objc-keywordarg
1468///
1469/// objc-keywordarg:
1470/// selector-name[opt] ':' objc-keywordexpr
1471///
1472/// objc-keywordexpr:
1473/// nonempty-expr-list
1474///
1475/// nonempty-expr-list:
1476/// assignment-expression
1477/// nonempty-expr-list , assignment-expression
1478///
Chris Lattnered27a532008-01-25 18:59:06 +00001479Parser::ExprResult
1480Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1481 IdentifierInfo *ReceiverName,
1482 ExprTy *ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001483 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001484 SourceLocation Loc;
1485 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001486
1487 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1488 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1489
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001490 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001491 while (1) {
1492 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001493 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001494
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001495 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001496 Diag(Tok, diag::err_expected_colon);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001497 // We must manually skip to a ']', otherwise the expression skipper will
1498 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1499 // the enclosing expression.
1500 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001501 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001502 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001503
Steve Naroff4ed9d662007-09-27 14:38:14 +00001504 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001505 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001506 ExprResult Res = ParseAssignmentExpression();
1507 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001508 // We must manually skip to a ']', otherwise the expression skipper will
1509 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1510 // the enclosing expression.
1511 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001512 return Res;
1513 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001514
Steve Naroff253118b2007-09-17 20:25:27 +00001515 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001516 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001517
1518 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001519 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001520 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001521 break;
1522 // We have a selector or a colon, continue parsing.
1523 }
1524 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001525 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001526 ConsumeToken(); // Eat the ','.
1527 /// Parse the expression after ','
1528 ExprResult Res = ParseAssignmentExpression();
1529 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001530 // We must manually skip to a ']', otherwise the expression skipper will
1531 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1532 // the enclosing expression.
1533 SkipUntil(tok::r_square);
Steve Naroff9f176d12007-11-15 13:05:42 +00001534 return Res;
1535 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001536
Steve Naroff9f176d12007-11-15 13:05:42 +00001537 // We have a valid expression.
1538 KeyExprs.push_back(Res.Val);
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001539 }
1540 } else if (!selIdent) {
1541 Diag(Tok, diag::err_expected_ident); // missing selector name.
Chris Lattnerf9311a92008-08-05 06:19:09 +00001542
1543 // We must manually skip to a ']', otherwise the expression skipper will
1544 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1545 // the enclosing expression.
1546 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001547 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001548 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001549
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001550 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001551 Diag(Tok, diag::err_expected_rsquare);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001552 // We must manually skip to a ']', otherwise the expression skipper will
1553 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1554 // the enclosing expression.
1555 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001556 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001557 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001558
Chris Lattnered27a532008-01-25 18:59:06 +00001559 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001560
Steve Narofff9e80db2007-10-05 18:42:47 +00001561 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001562 if (nKeys == 0)
1563 KeyIdents.push_back(selIdent);
1564 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1565
1566 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001567 if (ReceiverName)
Fariborz Jahanian2ce5dc52007-11-12 20:13:27 +00001568 return Actions.ActOnClassMessage(CurScope,
Chris Lattnered27a532008-01-25 18:59:06 +00001569 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001570 &KeyExprs[0], KeyExprs.size());
Chris Lattnered27a532008-01-25 18:59:06 +00001571 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001572 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001573}
1574
Steve Naroff0add5d22007-11-03 11:27:19 +00001575Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001576 ExprResult Res = ParseStringLiteralExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001577 if (Res.isInvalid) return Res;
Chris Lattnerddd3e632007-12-12 01:04:12 +00001578
1579 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1580 // expressions. At this point, we know that the only valid thing that starts
1581 // with '@' is an @"".
1582 llvm::SmallVector<SourceLocation, 4> AtLocs;
1583 llvm::SmallVector<ExprTy*, 4> AtStrings;
1584 AtLocs.push_back(AtLoc);
1585 AtStrings.push_back(Res.Val);
1586
1587 while (Tok.is(tok::at)) {
1588 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001589
Chris Lattnerddd3e632007-12-12 01:04:12 +00001590 ExprResult Res(true); // Invalid unless there is a string literal.
1591 if (isTokenStringLiteral())
1592 Res = ParseStringLiteralExpression();
1593 else
1594 Diag(Tok, diag::err_objc_concat_string);
1595
1596 if (Res.isInvalid) {
1597 while (!AtStrings.empty()) {
1598 Actions.DeleteExpr(AtStrings.back());
1599 AtStrings.pop_back();
1600 }
1601 return Res;
1602 }
1603
1604 AtStrings.push_back(Res.Val);
1605 }
Fariborz Jahanian1a442d32007-12-12 23:55:49 +00001606
Chris Lattnerddd3e632007-12-12 01:04:12 +00001607 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1608 AtStrings.size());
Anders Carlssona66cad42007-08-21 17:43:55 +00001609}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001610
1611/// objc-encode-expression:
1612/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001613Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001614 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001615
1616 SourceLocation EncLoc = ConsumeToken();
1617
Chris Lattnerf9311a92008-08-05 06:19:09 +00001618 if (Tok.isNot(tok::l_paren))
1619 return Diag(Tok, diag::err_expected_lparen_after, "@encode");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001620
1621 SourceLocation LParenLoc = ConsumeParen();
1622
1623 TypeTy *Ty = ParseTypeName();
1624
Anders Carlsson92faeb82007-08-23 15:31:37 +00001625 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001626
Chris Lattnercfd61c82007-10-16 22:51:17 +00001627 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001628 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001629}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001630
1631/// objc-protocol-expression
1632/// @protocol ( protocol-name )
1633
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001634Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001635{
1636 SourceLocation ProtoLoc = ConsumeToken();
1637
Chris Lattnerf9311a92008-08-05 06:19:09 +00001638 if (Tok.isNot(tok::l_paren))
1639 return Diag(Tok, diag::err_expected_lparen_after, "@protocol");
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001640
1641 SourceLocation LParenLoc = ConsumeParen();
1642
Chris Lattnerf9311a92008-08-05 06:19:09 +00001643 if (Tok.isNot(tok::identifier))
1644 return Diag(Tok, diag::err_expected_ident);
1645
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001646 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001647 ConsumeToken();
1648
Anders Carlsson92faeb82007-08-23 15:31:37 +00001649 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001650
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001651 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1652 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001653}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001654
1655/// objc-selector-expression
1656/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001657Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001658{
1659 SourceLocation SelectorLoc = ConsumeToken();
1660
Chris Lattnerf9311a92008-08-05 06:19:09 +00001661 if (Tok.isNot(tok::l_paren))
1662 return Diag(Tok, diag::err_expected_lparen_after, "@selector");
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001663
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001664 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001665 SourceLocation LParenLoc = ConsumeParen();
1666 SourceLocation sLoc;
1667 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001668 if (!SelIdent && Tok.isNot(tok::colon))
1669 return Diag(Tok, diag::err_expected_ident); // missing selector name.
1670
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001671 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001672 unsigned nColons = 0;
1673 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001674 while (1) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001675 if (Tok.isNot(tok::colon))
1676 return Diag(Tok, diag::err_expected_colon);
1677
Chris Lattner847f5c12007-12-27 19:57:00 +00001678 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001679 ConsumeToken(); // Eat the ':'.
1680 if (Tok.is(tok::r_paren))
1681 break;
1682 // Check for another keyword selector.
1683 SourceLocation Loc;
1684 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001685 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001686 if (!SelIdent && Tok.isNot(tok::colon))
1687 break;
1688 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001689 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001690 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001691 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001692 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001693 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001694 }