blob: ca722fa68dc5d30fc970df635b6d80ee57e68486 [file] [log] [blame]
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff4985ace2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000017#include "AstGuard.h"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/SmallVector.h"
20using namespace clang;
21
22
Chris Lattner891dca62008-12-08 21:53:24 +000023/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000024/// external-declaration: [C99 6.9]
25/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000026/// [OBJC] objc-class-declaration
27/// [OBJC] objc-alias-declaration
28/// [OBJC] objc-protocol-definition
29/// [OBJC] objc-method-definition
30/// [OBJC] '@' 'end'
Steve Naroffdac269b2007-08-20 21:31:48 +000031Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 SourceLocation AtLoc = ConsumeToken(); // the "@"
33
Steve Naroff861cf3e2007-08-23 18:16:40 +000034 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000035 case tok::objc_class:
36 return ParseObjCAtClassDeclaration(AtLoc);
37 case tok::objc_interface:
38 return ParseObjCAtInterfaceDeclaration(AtLoc);
39 case tok::objc_protocol:
40 return ParseObjCAtProtocolDeclaration(AtLoc);
41 case tok::objc_implementation:
42 return ParseObjCAtImplementationDeclaration(AtLoc);
43 case tok::objc_end:
44 return ParseObjCAtEndDeclaration(AtLoc);
45 case tok::objc_compatibility_alias:
46 return ParseObjCAtAliasDeclaration(AtLoc);
47 case tok::objc_synthesize:
48 return ParseObjCPropertySynthesize(AtLoc);
49 case tok::objc_dynamic:
50 return ParseObjCPropertyDynamic(AtLoc);
51 default:
52 Diag(AtLoc, diag::err_unexpected_at);
53 SkipUntil(tok::semi);
54 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000055 }
56}
57
58///
59/// objc-class-declaration:
60/// '@' 'class' identifier-list ';'
61///
Steve Naroffdac269b2007-08-20 21:31:48 +000062Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000063 ConsumeToken(); // the identifier "class"
64 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
65
66 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000067 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000068 Diag(Tok, diag::err_expected_ident);
69 SkipUntil(tok::semi);
Steve Naroffdac269b2007-08-20 21:31:48 +000070 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000071 }
Reid Spencer5f016e22007-07-11 17:01:13 +000072 ClassNames.push_back(Tok.getIdentifierInfo());
73 ConsumeToken();
74
Chris Lattnerdf195262007-10-09 17:51:17 +000075 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000076 break;
77
78 ConsumeToken();
79 }
80
81 // Consume the ';'.
82 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Naroffdac269b2007-08-20 21:31:48 +000083 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000084
Steve Naroffe440eb82007-10-10 17:32:04 +000085 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff3536b442007-09-06 21:24:23 +000086 &ClassNames[0], ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000087}
88
Steve Naroffdac269b2007-08-20 21:31:48 +000089///
90/// objc-interface:
91/// objc-class-interface-attributes[opt] objc-class-interface
92/// objc-category-interface
93///
94/// objc-class-interface:
95/// '@' 'interface' identifier objc-superclass[opt]
96/// objc-protocol-refs[opt]
97/// objc-class-instance-variables[opt]
98/// objc-interface-decl-list
99/// @end
100///
101/// objc-category-interface:
102/// '@' 'interface' identifier '(' identifier[opt] ')'
103/// objc-protocol-refs[opt]
104/// objc-interface-decl-list
105/// @end
106///
107/// objc-superclass:
108/// ':' identifier
109///
110/// objc-class-interface-attributes:
111/// __attribute__((visibility("default")))
112/// __attribute__((visibility("hidden")))
113/// __attribute__((deprecated))
114/// __attribute__((unavailable))
115/// __attribute__((objc_exception)) - used by NSException on 64-bit
116///
117Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
118 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000119 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000120 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
121 ConsumeToken(); // the "interface" identifier
122
Chris Lattnerdf195262007-10-09 17:51:17 +0000123 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000124 Diag(Tok, diag::err_expected_ident); // missing class or category name.
125 return 0;
126 }
127 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000128 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000129 SourceLocation nameLoc = ConsumeToken();
130
Chris Lattnerdf195262007-10-09 17:51:17 +0000131 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000132 SourceLocation lparenLoc = ConsumeParen();
133 SourceLocation categoryLoc, rparenLoc;
134 IdentifierInfo *categoryId = 0;
135
Steve Naroff527fe232007-08-23 19:56:30 +0000136 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000137 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000138 categoryId = Tok.getIdentifierInfo();
139 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000140 } else if (!getLang().ObjC2) {
141 Diag(Tok, diag::err_expected_ident); // missing category name.
142 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000143 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000144 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000145 Diag(Tok, diag::err_expected_rparen);
146 SkipUntil(tok::r_paren, false); // don't stop at ';'
147 return 0;
148 }
149 rparenLoc = ConsumeParen();
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000150
Steve Naroffdac269b2007-08-20 21:31:48 +0000151 // Next, we need to check for any protocol references.
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000152 SourceLocation EndProtoLoc;
153 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
154 if (Tok.is(tok::less) &&
155 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
156 return 0;
157
Steve Naroffdac269b2007-08-20 21:31:48 +0000158 if (attrList) // categories don't support attributes.
159 Diag(Tok, diag::err_objc_no_attributes_on_category);
160
Steve Naroffe440eb82007-10-10 17:32:04 +0000161 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff3a165b02007-10-03 21:00:46 +0000162 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff423cb562007-10-30 13:30:57 +0000163 &ProtocolRefs[0], ProtocolRefs.size(),
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000164 EndProtoLoc);
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000165
166 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000167 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000168 }
169 // Parse a class interface.
170 IdentifierInfo *superClassId = 0;
171 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000172
Chris Lattnerdf195262007-10-09 17:51:17 +0000173 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000174 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000175 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000176 Diag(Tok, diag::err_expected_ident); // missing super class name.
177 return 0;
178 }
179 superClassId = Tok.getIdentifierInfo();
180 superClassLoc = ConsumeToken();
181 }
182 // Next, we need to check for any protocol references.
Chris Lattner06036d32008-07-26 04:13:19 +0000183 llvm::SmallVector<Action::DeclTy*, 8> ProtocolRefs;
184 SourceLocation EndProtoLoc;
185 if (Tok.is(tok::less) &&
186 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
187 return 0;
188
189 DeclTy *ClsType =
190 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
191 superClassId, superClassLoc,
192 &ProtocolRefs[0], ProtocolRefs.size(),
193 EndProtoLoc, attrList);
Steve Narofff28b2642007-09-05 23:30:30 +0000194
Chris Lattnerdf195262007-10-09 17:51:17 +0000195 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000196 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000197
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000198 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000199 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000200}
201
202/// objc-interface-decl-list:
203/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000204/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000205/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000206/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000207/// objc-interface-decl-list declaration
208/// objc-interface-decl-list ';'
209///
Steve Naroff294494e2007-08-22 16:35:03 +0000210/// objc-method-requirement: [OBJC2]
211/// @required
212/// @optional
213///
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000214void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000215 tok::ObjCKeywordKind contextKey) {
Ted Kremenek8a779312008-06-06 16:45:15 +0000216 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000217 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian38e24c72009-03-18 22:33:24 +0000218 llvm::SmallVector<DeclTy*, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000219 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff60fccee2007-10-29 21:38:07 +0000220
Chris Lattnerbc662af2008-10-20 06:10:06 +0000221 SourceLocation AtEndLoc;
222
Steve Naroff294494e2007-08-22 16:35:03 +0000223 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000224 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000225 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
226 DeclTy *methodPrototype =
227 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000228 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000229 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
230 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000231 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
232 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000233 continue;
234 }
Fariborz Jahanianf366b4c2007-12-11 18:34:51 +0000235
Chris Lattnere82a10f2008-10-20 05:46:22 +0000236 // Ignore excess semicolons.
237 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000238 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000239 continue;
240 }
241
Chris Lattnerbc662af2008-10-20 06:10:06 +0000242 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000243 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000244 break;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000245
246 // If we don't have an @ directive, parse it as a function definition.
247 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000248 // The code below does not consume '}'s because it is afraid of eating the
249 // end of a namespace. Because of the way this code is structured, an
250 // erroneous r_brace would cause an infinite loop if not handled here.
251 if (Tok.is(tok::r_brace))
252 break;
253
Steve Naroff4985ace2007-08-22 18:35:33 +0000254 // FIXME: as the name implies, this rule allows function definitions.
255 // We could pass a flag or check for functions during semantic analysis.
Fariborz Jahanian38e24c72009-03-18 22:33:24 +0000256 DeclTy *VFDecl = ParseDeclarationOrFunctionDefinition();
257 allTUVariables.push_back(VFDecl);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000258 continue;
259 }
260
261 // Otherwise, we have an @ directive, eat the @.
262 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnera2449b22008-10-20 05:57:40 +0000263 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000264
Chris Lattnera2449b22008-10-20 05:57:40 +0000265 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere82a10f2008-10-20 05:46:22 +0000266 AtEndLoc = AtLoc;
267 break;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000268 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000269
Chris Lattnerbc662af2008-10-20 06:10:06 +0000270 // Eat the identifier.
271 ConsumeToken();
272
Chris Lattnera2449b22008-10-20 05:57:40 +0000273 switch (DirectiveKind) {
274 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000275 // FIXME: If someone forgets an @end on a protocol, this loop will
276 // continue to eat up tons of stuff and spew lots of nonsense errors. It
277 // would probably be better to bail out if we saw an @class or @interface
278 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000279 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000280 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000281 SkipUntil(tok::r_brace, tok::at);
282 break;
283
284 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000285 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000286 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000287 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000288 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000289 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000290 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000291 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000292 break;
293
294 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000295 if (!getLang().ObjC2)
296 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
297
Chris Lattnere82a10f2008-10-20 05:46:22 +0000298 ObjCDeclSpec OCDS;
Chris Lattnere82a10f2008-10-20 05:46:22 +0000299 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000300 if (Tok.is(tok::l_paren))
Chris Lattnere82a10f2008-10-20 05:46:22 +0000301 ParseObjCPropertyAttribute(OCDS);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000302
Chris Lattnere82a10f2008-10-20 05:46:22 +0000303 // Parse all the comma separated declarators.
304 DeclSpec DS;
305 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
306 ParseStructDeclaration(DS, FieldDeclarators);
307
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000308 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
309 tok::at);
310
Chris Lattnere82a10f2008-10-20 05:46:22 +0000311 // Convert them all to property declarations.
312 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
313 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattnerda3253d2008-10-20 06:33:53 +0000314 if (FD.D.getIdentifier() == 0) {
Chris Lattneref708fd2008-11-18 07:50:21 +0000315 Diag(AtLoc, diag::err_objc_property_requires_field_name)
316 << FD.D.getSourceRange();
Chris Lattnerda3253d2008-10-20 06:33:53 +0000317 continue;
318 }
Fariborz Jahanian573acde2009-01-17 23:21:10 +0000319 if (FD.BitfieldSize) {
320 Diag(AtLoc, diag::err_objc_property_bitfield)
321 << FD.D.getSourceRange();
322 continue;
323 }
Chris Lattnerda3253d2008-10-20 06:33:53 +0000324
Chris Lattnere82a10f2008-10-20 05:46:22 +0000325 // Install the property declarator into interfaceDecl.
Chris Lattnerda3253d2008-10-20 06:33:53 +0000326 IdentifierInfo *SelName =
327 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
328
Chris Lattnere82a10f2008-10-20 05:46:22 +0000329 Selector GetterSel =
Chris Lattnerda3253d2008-10-20 06:33:53 +0000330 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000331 IdentifierInfo *SetterName = OCDS.getSetterName();
Fariborz Jahanian2e050f12009-03-12 22:34:11 +0000332 Selector SetterSel;
333 if (SetterName)
334 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
335 else
336 SetterSel = SelectorTable::constructSetterName(PP.getIdentifierTable(),
337 PP.getSelectorTable(),
338 FD.D.getIdentifier());
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000339 bool isOverridingProperty = false;
Chris Lattnerda3253d2008-10-20 06:33:53 +0000340 DeclTy *Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
341 GetterSel, SetterSel,
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000342 interfaceDecl,
343 &isOverridingProperty,
Chris Lattnerda3253d2008-10-20 06:33:53 +0000344 MethodImplKind);
Fariborz Jahanian8cf0bb32008-11-26 20:01:34 +0000345 if (!isOverridingProperty)
346 allProperties.push_back(Property);
Chris Lattnere82a10f2008-10-20 05:46:22 +0000347 }
Chris Lattnera2449b22008-10-20 05:57:40 +0000348 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000349 }
Steve Naroff294494e2007-08-22 16:35:03 +0000350 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000351
352 // We break out of the big loop in two cases: when we see @end or when we see
353 // EOF. In the former case, eat the @end. In the later case, emit an error.
354 if (Tok.isObjCAtKeyword(tok::objc_end))
355 ConsumeToken(); // the "end" identifier
356 else
357 Diag(Tok, diag::err_objc_missing_end);
358
Chris Lattnera2449b22008-10-20 05:57:40 +0000359 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000360 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8a779312008-06-06 16:45:15 +0000361 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
362 allMethods.empty() ? 0 : &allMethods[0],
363 allMethods.size(),
364 allProperties.empty() ? 0 : &allProperties[0],
Fariborz Jahanian38e24c72009-03-18 22:33:24 +0000365 allProperties.size(),
366 allTUVariables.empty() ? 0 :
367 &allTUVariables[0],
368 allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000369}
370
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000371/// Parse property attribute declarations.
372///
373/// property-attr-decl: '(' property-attrlist ')'
374/// property-attrlist:
375/// property-attribute
376/// property-attrlist ',' property-attribute
377/// property-attribute:
378/// getter '=' identifier
379/// setter '=' identifier ':'
380/// readonly
381/// readwrite
382/// assign
383/// retain
384/// copy
385/// nonatomic
386///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000387void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000388 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000389 SourceLocation LHSLoc = ConsumeParen(); // consume '('
390
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000391 while (1) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000392 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000393
394 // If this is not an identifier at all, bail out early.
395 if (II == 0) {
396 MatchRHSPunctuation(tok::r_paren, LHSLoc);
397 return;
398 }
399
Chris Lattner156b0612008-10-20 07:37:22 +0000400 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
401
Chris Lattner92e62b02008-11-20 04:42:34 +0000402 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000403 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000404 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000405 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000406 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000407 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000408 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000409 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000410 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000411 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000412 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000413 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000414 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000415 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000416 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
417 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000418 return;
Chris Lattner156b0612008-10-20 07:37:22 +0000419
Chris Lattner8ca329c2008-10-20 07:24:39 +0000420 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000421 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000422 SkipUntil(tok::r_paren);
423 return;
424 }
425
Chris Lattner5fd80fa2008-10-20 07:43:01 +0000426 if (II->getName()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000427 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
428 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000429 ConsumeToken(); // consume method name
430
431 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
432 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000433 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000434 } else {
435 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
436 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000437 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000438 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000439 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000440 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000441 SkipUntil(tok::r_paren);
442 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000443 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000444
Chris Lattner156b0612008-10-20 07:37:22 +0000445 if (Tok.isNot(tok::comma))
446 break;
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000447
Chris Lattner156b0612008-10-20 07:37:22 +0000448 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000449 }
Chris Lattner156b0612008-10-20 07:37:22 +0000450
451 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000452}
453
Steve Naroff3536b442007-09-06 21:24:23 +0000454/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000455/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000456/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000457///
458/// objc-instance-method: '-'
459/// objc-class-method: '+'
460///
Steve Naroff4985ace2007-08-22 18:35:33 +0000461/// objc-method-attributes: [OBJC2]
462/// __attribute__((deprecated))
463///
Fariborz Jahanian00933592007-09-18 00:25:23 +0000464Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000465 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000466 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000467
468 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000469 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000470
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000471 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000472 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000473 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000474 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000475}
476
477/// objc-selector:
478/// identifier
479/// one of
480/// enum struct union if else while do for switch case default
481/// break continue return goto asm sizeof typeof __alignof
482/// unsigned long const short volatile signed restrict _Complex
483/// in out inout bycopy byref oneway int char float double void _Bool
484///
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000485IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000486 switch (Tok.getKind()) {
487 default:
488 return 0;
489 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000490 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000491 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000492 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000493 case tok::kw_break:
494 case tok::kw_case:
495 case tok::kw_catch:
496 case tok::kw_char:
497 case tok::kw_class:
498 case tok::kw_const:
499 case tok::kw_const_cast:
500 case tok::kw_continue:
501 case tok::kw_default:
502 case tok::kw_delete:
503 case tok::kw_do:
504 case tok::kw_double:
505 case tok::kw_dynamic_cast:
506 case tok::kw_else:
507 case tok::kw_enum:
508 case tok::kw_explicit:
509 case tok::kw_export:
510 case tok::kw_extern:
511 case tok::kw_false:
512 case tok::kw_float:
513 case tok::kw_for:
514 case tok::kw_friend:
515 case tok::kw_goto:
516 case tok::kw_if:
517 case tok::kw_inline:
518 case tok::kw_int:
519 case tok::kw_long:
520 case tok::kw_mutable:
521 case tok::kw_namespace:
522 case tok::kw_new:
523 case tok::kw_operator:
524 case tok::kw_private:
525 case tok::kw_protected:
526 case tok::kw_public:
527 case tok::kw_register:
528 case tok::kw_reinterpret_cast:
529 case tok::kw_restrict:
530 case tok::kw_return:
531 case tok::kw_short:
532 case tok::kw_signed:
533 case tok::kw_sizeof:
534 case tok::kw_static:
535 case tok::kw_static_cast:
536 case tok::kw_struct:
537 case tok::kw_switch:
538 case tok::kw_template:
539 case tok::kw_this:
540 case tok::kw_throw:
541 case tok::kw_true:
542 case tok::kw_try:
543 case tok::kw_typedef:
544 case tok::kw_typeid:
545 case tok::kw_typename:
546 case tok::kw_typeof:
547 case tok::kw_union:
548 case tok::kw_unsigned:
549 case tok::kw_using:
550 case tok::kw_virtual:
551 case tok::kw_void:
552 case tok::kw_volatile:
553 case tok::kw_wchar_t:
554 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000555 case tok::kw__Bool:
556 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000557 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000558 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000559 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000560 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000561 }
Steve Naroff294494e2007-08-22 16:35:03 +0000562}
563
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000564/// objc-for-collection-in: 'in'
565///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000566bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000567 // FIXME: May have to do additional look-ahead to only allow for
568 // valid tokens following an 'in'; such as an identifier, unary operators,
569 // '[' etc.
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000570 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000571 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000572}
573
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000574/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000575/// qualifier list and builds their bitmask representation in the input
576/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000577///
578/// objc-type-qualifiers:
579/// objc-type-qualifier
580/// objc-type-qualifiers objc-type-qualifier
581///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000582void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000583 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000584 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000585 return;
586
587 const IdentifierInfo *II = Tok.getIdentifierInfo();
588 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000589 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000590 continue;
591
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000592 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000593 switch (i) {
594 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000595 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
596 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
597 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
598 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
599 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
600 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000601 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000602 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000603 ConsumeToken();
604 II = 0;
605 break;
606 }
607
608 // If this wasn't a recognized qualifier, bail out.
609 if (II) return;
610 }
611}
612
613/// objc-type-name:
614/// '(' objc-type-qualifiers[opt] type-name ')'
615/// '(' objc-type-qualifiers[opt] ')'
616///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000617Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000618 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000619
Chris Lattner4a76b292008-10-22 03:52:06 +0000620 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000621 SourceLocation TypeStartLoc = Tok.getLocation();
Steve Naroff294494e2007-08-22 16:35:03 +0000622
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000623 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000624 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000625
Chris Lattner4a76b292008-10-22 03:52:06 +0000626 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000627 if (isTypeSpecifierQualifier()) {
628 TypeResult TypeSpec = ParseTypeName();
629 if (!TypeSpec.isInvalid())
630 Ty = TypeSpec.get();
631 }
Chris Lattnere8904e92008-08-23 01:48:03 +0000632
Steve Naroffd7333c22008-10-21 14:15:04 +0000633 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000634 ConsumeParen();
635 else if (Tok.getLocation() == TypeStartLoc) {
636 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000637 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000638 SkipUntil(tok::r_paren);
639 } else {
640 // Otherwise, we found *something*, but didn't get a ')' in the right
641 // place. Emit an error then return what we have as the type.
642 MatchRHSPunctuation(tok::r_paren, LParenLoc);
643 }
Steve Narofff28b2642007-09-05 23:30:30 +0000644 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000645}
646
647/// objc-method-decl:
648/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000649/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000650/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000651/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000652///
653/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000654/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000655/// objc-keyword-selector objc-keyword-decl
656///
657/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000658/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
659/// objc-selector ':' objc-keyword-attributes[opt] identifier
660/// ':' objc-type-name objc-keyword-attributes[opt] identifier
661/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000662///
Steve Naroff4985ace2007-08-22 18:35:33 +0000663/// objc-parmlist:
664/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000665///
Steve Naroff4985ace2007-08-22 18:35:33 +0000666/// objc-parms:
667/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000668///
Steve Naroff4985ace2007-08-22 18:35:33 +0000669/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000670/// , ...
671///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000672/// objc-keyword-attributes: [OBJC2]
673/// __attribute__((unused))
674///
Steve Naroffbef11852007-10-26 20:53:56 +0000675Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000676 tok::TokenKind mType,
677 DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000678 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff68d331a2007-09-27 14:38:14 +0000679{
Chris Lattnere8904e92008-08-23 01:48:03 +0000680 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000681 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000682 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000683 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000684 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnere8904e92008-08-23 01:48:03 +0000685
Steve Naroffbef11852007-10-26 20:53:56 +0000686 SourceLocation selLoc;
687 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000688
Steve Naroff84c43102009-02-11 20:43:13 +0000689 // An unnamed colon is valid.
690 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000691 Diag(Tok, diag::err_expected_selector_for_method)
692 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000693 // Skip until we get a ; or {}.
694 SkipUntil(tok::r_brace);
695 return 0;
696 }
697
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000698 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000699 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000700 // If attributes exist after the method, parse them.
701 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000702 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000703 MethodAttrs = ParseAttributes();
704
705 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000706 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000707 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000708 0, 0, 0, CargNames,
709 MethodAttrs, MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000710 }
Steve Narofff28b2642007-09-05 23:30:30 +0000711
Steve Naroff68d331a2007-09-27 14:38:14 +0000712 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
713 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000714 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff68d331a2007-09-27 14:38:14 +0000715 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000716
717 Action::TypeTy *TypeInfo;
718 while (1) {
719 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000720
Chris Lattnerff384912007-10-07 02:00:24 +0000721 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000722 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000723 Diag(Tok, diag::err_expected_colon);
724 break;
725 }
726 ConsumeToken(); // Eat the ':'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000727 ObjCDeclSpec DSType;
Chris Lattnere8904e92008-08-23 01:48:03 +0000728 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000729 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerff384912007-10-07 02:00:24 +0000730 else
731 TypeInfo = 0;
732 KeyTypes.push_back(TypeInfo);
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000733 ArgTypeQuals.push_back(DSType);
Steve Narofff28b2642007-09-05 23:30:30 +0000734
Chris Lattnerff384912007-10-07 02:00:24 +0000735 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000736 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000737 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000738
Chris Lattnerdf195262007-10-09 17:51:17 +0000739 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000740 Diag(Tok, diag::err_expected_ident); // missing argument name.
741 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000742 }
Chris Lattnerff384912007-10-07 02:00:24 +0000743 ArgNames.push_back(Tok.getIdentifierInfo());
744 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000745
Chris Lattnerff384912007-10-07 02:00:24 +0000746 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000747 SourceLocation Loc;
748 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000749 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000750 break;
751 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000752 }
Chris Lattnerff384912007-10-07 02:00:24 +0000753
Steve Naroff335eafa2007-11-15 12:35:21 +0000754 bool isVariadic = false;
755
Chris Lattnerff384912007-10-07 02:00:24 +0000756 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000757 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000758 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000759 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000760 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000761 ConsumeToken();
762 break;
763 }
Chris Lattnerff384912007-10-07 02:00:24 +0000764 DeclSpec DS;
765 ParseDeclarationSpecifiers(DS);
766 // Parse the declarator.
767 Declarator ParmDecl(DS, Declarator::PrototypeContext);
768 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000769 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000770 }
771
772 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000773 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000774 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000775 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000776 MethodAttrs = ParseAttributes();
777
778 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
779 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000780 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000781 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000782 &ArgTypeQuals[0], &KeyTypes[0],
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000783 &ArgNames[0], CargNames,
784 MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000785 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000786}
787
Steve Naroffdac269b2007-08-20 21:31:48 +0000788/// objc-protocol-refs:
789/// '<' identifier-list '>'
790///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000791bool Parser::
Chris Lattnere13b9592008-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 Naroffdac269b2007-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 Naroffddbff782007-08-21 21:17:12 +0000845/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000846///
847/// objc-instance-variable-decl:
848/// struct-declaration
849///
Steve Naroff60fccee2007-10-29 21:38:07 +0000850void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
851 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000852 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000853 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000854 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
855
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000856 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000857
Steve Naroffddbff782007-08-21 21:17:12 +0000858 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000859
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000860 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000861 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000862 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000863 // Each iteration of this loop reads one objc-instance-variable-decl.
864
865 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000866 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000867 Diag(Tok, diag::ext_extra_struct_semi);
868 ConsumeToken();
869 continue;
870 }
Chris Lattnere1359422008-04-10 06:46:29 +0000871
Steve Naroffddbff782007-08-21 21:17:12 +0000872 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000873 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000874 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000875 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000876 case tok::objc_private:
877 case tok::objc_public:
878 case tok::objc_protected:
879 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000880 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000881 ConsumeToken();
882 continue;
883 default:
884 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000885 continue;
886 }
887 }
Chris Lattnere1359422008-04-10 06:46:29 +0000888
889 // Parse all the comma separated declarators.
890 DeclSpec DS;
891 FieldDeclarators.clear();
892 ParseStructDeclaration(DS, FieldDeclarators);
893
894 // Convert them all to fields.
895 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
896 FieldDeclarator &FD = FieldDeclarators[i];
897 // Install the declarator into interfaceDecl.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000898 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattnere1359422008-04-10 06:46:29 +0000899 DS.getSourceRange().getBegin(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000900 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000901 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000902 }
Steve Naroff3536b442007-09-06 21:24:23 +0000903
Chris Lattnerdf195262007-10-09 17:51:17 +0000904 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000905 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000906 } else {
907 Diag(Tok, diag::err_expected_semi_decl_list);
908 // Skip to end of block or statement
909 SkipUntil(tok::r_brace, true, true);
910 }
911 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000912 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000913 // Call ActOnFields() even if we don't have any decls. This is useful
914 // for code rewriting tools that need to be aware of the empty list.
915 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
916 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000917 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +0000918 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000919}
Steve Naroffdac269b2007-08-20 21:31:48 +0000920
921/// objc-protocol-declaration:
922/// objc-protocol-definition
923/// objc-protocol-forward-reference
924///
925/// objc-protocol-definition:
926/// @protocol identifier
927/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000928/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000929/// @end
930///
931/// objc-protocol-forward-reference:
932/// @protocol identifier-list ';'
933///
934/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000935/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000936/// semicolon in the first alternative if objc-protocol-refs are omitted.
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000937Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
938 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000939 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000940 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
941 ConsumeToken(); // the "protocol" identifier
942
Chris Lattnerdf195262007-10-09 17:51:17 +0000943 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000944 Diag(Tok, diag::err_expected_ident); // missing protocol name.
945 return 0;
946 }
947 // Save the protocol name, then consume it.
948 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
949 SourceLocation nameLoc = ConsumeToken();
950
Chris Lattnerdf195262007-10-09 17:51:17 +0000951 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000952 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000953 ConsumeToken();
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000954 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
955 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000956 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000957
Chris Lattnerdf195262007-10-09 17:51:17 +0000958 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000959 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
960 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
961
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000962 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000963 while (1) {
964 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000965 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000966 Diag(Tok, diag::err_expected_ident);
967 SkipUntil(tok::semi);
968 return 0;
969 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000970 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
971 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000972 ConsumeToken(); // the identifier
973
Chris Lattnerdf195262007-10-09 17:51:17 +0000974 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-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 Lattner7caeabd2008-07-21 22:17:28 +0000980
Steve Naroffe440eb82007-10-10 17:32:04 +0000981 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000982 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000983 ProtocolRefs.size(),
984 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +0000985 }
986
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000987 // Last, and definitely not least, parse a protocol declaration.
Chris Lattnere13b9592008-07-26 04:03:38 +0000988 SourceLocation EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000989
Chris Lattnere13b9592008-07-26 04:03:38 +0000990 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000991 if (Tok.is(tok::less) &&
Chris Lattnere13b9592008-07-26 04:03:38 +0000992 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattner7caeabd2008-07-21 22:17:28 +0000993 return 0;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000994
Chris Lattnere13b9592008-07-26 04:03:38 +0000995 DeclTy *ProtoType =
996 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
997 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000998 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000999 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001000 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001001}
Steve Naroffdac269b2007-08-20 21:31:48 +00001002
1003/// objc-implementation:
1004/// objc-class-implementation-prologue
1005/// objc-category-implementation-prologue
1006///
1007/// objc-class-implementation-prologue:
1008/// @implementation identifier objc-superclass[opt]
1009/// objc-class-instance-variables[opt]
1010///
1011/// objc-category-implementation-prologue:
1012/// @implementation identifier ( identifier )
1013
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001014Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
1015 SourceLocation atLoc) {
1016 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1017 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1018 ConsumeToken(); // the "implementation" identifier
1019
Chris Lattnerdf195262007-10-09 17:51:17 +00001020 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001021 Diag(Tok, diag::err_expected_ident); // missing class or category name.
1022 return 0;
1023 }
1024 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001025 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001026 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1027
Chris Lattnerdf195262007-10-09 17:51:17 +00001028 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001029 // we have a category implementation.
1030 SourceLocation lparenLoc = ConsumeParen();
1031 SourceLocation categoryLoc, rparenLoc;
1032 IdentifierInfo *categoryId = 0;
1033
Chris Lattnerdf195262007-10-09 17:51:17 +00001034 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001035 categoryId = Tok.getIdentifierInfo();
1036 categoryLoc = ConsumeToken();
1037 } else {
1038 Diag(Tok, diag::err_expected_ident); // missing category name.
1039 return 0;
1040 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001041 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001042 Diag(Tok, diag::err_expected_rparen);
1043 SkipUntil(tok::r_paren, false); // don't stop at ';'
1044 return 0;
1045 }
1046 rparenLoc = ConsumeParen();
Steve Naroffe440eb82007-10-10 17:32:04 +00001047 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001048 atLoc, nameId, nameLoc, categoryId,
1049 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001050 ObjCImpDecl = ImplCatType;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +00001051 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001052 }
1053 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001054 SourceLocation superClassLoc;
1055 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001056 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001057 // We have a super class
1058 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001059 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001060 Diag(Tok, diag::err_expected_ident); // missing super class name.
1061 return 0;
1062 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001063 superClassId = Tok.getIdentifierInfo();
1064 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001065 }
Steve Naroffe440eb82007-10-10 17:32:04 +00001066 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001067 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001068 superClassId, superClassLoc);
1069
Steve Naroff60fccee2007-10-29 21:38:07 +00001070 if (Tok.is(tok::l_brace)) // we have ivars
1071 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001072 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001073
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +00001074 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001075}
Steve Naroff60fccee2007-10-29 21:38:07 +00001076
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001077Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1078 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1079 "ParseObjCAtEndDeclaration(): Expected @end");
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001080 DeclTy *Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001081 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001082 if (ObjCImpDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001083 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001084 ObjCImpDecl = 0;
1085 }
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001086 else
1087 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001088 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001089}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001090
1091/// compatibility-alias-decl:
1092/// @compatibility_alias alias-name class-name ';'
1093///
1094Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1095 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1096 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1097 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001098 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001099 Diag(Tok, diag::err_expected_ident);
1100 return 0;
1101 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001102 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1103 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001104 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001105 Diag(Tok, diag::err_expected_ident);
1106 return 0;
1107 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001108 IdentifierInfo *classId = Tok.getIdentifierInfo();
1109 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1110 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001111 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001112 return 0;
1113 }
1114 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1115 aliasId, aliasLoc,
1116 classId, classLoc);
1117 return ClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001118}
1119
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001120/// property-synthesis:
1121/// @synthesize property-ivar-list ';'
1122///
1123/// property-ivar-list:
1124/// property-ivar
1125/// property-ivar-list ',' property-ivar
1126///
1127/// property-ivar:
1128/// identifier
1129/// identifier '=' identifier
1130///
1131Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1132 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1133 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001134 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001135 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001136 Diag(Tok, diag::err_expected_ident);
1137 return 0;
1138 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001139 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001140 IdentifierInfo *propertyIvar = 0;
1141 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1142 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001143 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001144 // property '=' ivar-name
1145 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001146 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001147 Diag(Tok, diag::err_expected_ident);
1148 break;
1149 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001150 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001151 ConsumeToken(); // consume ivar-name
1152 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001153 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1154 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001155 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001156 break;
1157 ConsumeToken(); // consume ','
1158 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001159 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001160 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001161 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001162}
1163
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001164/// property-dynamic:
1165/// @dynamic property-list
1166///
1167/// property-list:
1168/// identifier
1169/// property-list ',' identifier
1170///
1171Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1172 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1173 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1174 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001175 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001176 Diag(Tok, diag::err_expected_ident);
1177 return 0;
1178 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001179 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001180 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1181 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1182 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1183 propertyId, 0);
1184
Chris Lattnerdf195262007-10-09 17:51:17 +00001185 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001186 break;
1187 ConsumeToken(); // consume ','
1188 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001189 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001190 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001191 return 0;
1192}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001193
1194/// objc-throw-statement:
1195/// throw expression[opt];
1196///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001197Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001198 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001199 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001200 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001201 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001202 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001203 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001204 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001205 }
1206 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001207 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001208 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001209}
1210
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001211/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001212/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001213///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001214Parser::OwningStmtResult
1215Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001216 ConsumeToken(); // consume synchronized
1217 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001218 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001219 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001220 }
1221 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001222 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001223 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001224 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001225 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001226 }
1227 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001228 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001229 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001230 }
1231 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001232 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001233 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001234 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001235 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001236 // Enter a scope to hold everything within the compound stmt. Compound
1237 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001238 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001239
Sebastian Redl61364dd2008-12-11 19:30:53 +00001240 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001241
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001242 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001243 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001244 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001245 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001246}
1247
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001248/// objc-try-catch-statement:
1249/// @try compound-statement objc-catch-list[opt]
1250/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1251///
1252/// objc-catch-list:
1253/// @catch ( parameter-declaration ) compound-statement
1254/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1255/// catch-parameter-declaration:
1256/// parameter-declaration
1257/// '...' [OBJC2]
1258///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001259Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001260 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001261
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001262 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001263 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001264 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001265 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001266 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001267 OwningStmtResult CatchStmts(Actions);
1268 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001269 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001270 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001271 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001272 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001273 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001274
Chris Lattnerdf195262007-10-09 17:51:17 +00001275 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001276 // At this point, we need to lookahead to determine if this @ is the start
1277 // of an @catch or @finally. We don't want to consume the @ token if this
1278 // is an @try or @encode or something else.
1279 Token AfterAt = GetLookAheadToken(1);
1280 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1281 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1282 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001283
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001284 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001285 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00001286 DeclTy *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001287 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001288 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001289 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001290 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001291 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001292 DeclSpec DS;
1293 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001294 // For some odd reason, the name of the exception variable is
Steve Naroff7ba138a2009-03-03 19:52:17 +00001295 // optional. As a result, we need to use "PrototypeContext", because
1296 // we must accept either 'declarator' or 'abstract-declarator' here.
1297 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1298 ParseDeclarator(ParmDecl);
1299
1300 // Inform the actions module about the parameter declarator, so it
1301 // gets added to the current scope.
1302 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001303 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001304 ConsumeToken(); // consume '...'
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001305 SourceLocation RParenLoc = ConsumeParen();
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001306
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001307 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001308 if (Tok.is(tok::l_brace))
1309 CatchBody = ParseCompoundStatementBody();
1310 else
1311 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001312 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001313 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001314 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001315 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001316 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001317 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001318 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1319 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001320 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001321 }
1322 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001323 } else {
1324 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001325 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001326 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001327
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001328 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001329 if (Tok.is(tok::l_brace))
1330 FinallyBody = ParseCompoundStatementBody();
1331 else
1332 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001333 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001334 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001335 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001336 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001337 catch_or_finally_seen = true;
1338 break;
1339 }
1340 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001341 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001342 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001343 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001344 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001345 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1346 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001347}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001348
Steve Naroff3536b442007-09-06 21:24:23 +00001349/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001350///
Steve Naroff71c0a952007-11-13 23:01:27 +00001351Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001352 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Chris Lattner73e80f62009-03-05 02:03:49 +00001353
Chris Lattner49f28ca2009-03-05 08:00:35 +00001354 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1355 PP.getSourceManager(),
1356 "parsing Objective-C method");
Chris Lattner73e80f62009-03-05 02:03:49 +00001357
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001358 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001359 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001360 ConsumeToken();
1361
Steve Naroff409be832007-11-11 19:54:21 +00001362 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001363 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001364 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001365
1366 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1367 SkipUntil(tok::l_brace, true, true);
1368
1369 // If we didn't find the '{', bail out.
1370 if (Tok.isNot(tok::l_brace))
Steve Naroff71c0a952007-11-13 23:01:27 +00001371 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001372 }
Steve Naroff409be832007-11-11 19:54:21 +00001373 SourceLocation BraceLoc = Tok.getLocation();
1374
1375 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001376 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Steve Naroff409be832007-11-11 19:54:21 +00001377
1378 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001379 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001380 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001381
1382 OwningStmtResult FnBody(ParseCompoundStatementBody());
1383
Steve Naroff409be832007-11-11 19:54:21 +00001384 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001385 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001386 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1387 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001388
Steve Naroff32ce8372009-03-02 22:00:56 +00001389 // TODO: Pass argument information.
1390 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
1391
Steve Naroff409be832007-11-11 19:54:21 +00001392 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001393 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001394
Steve Naroff71c0a952007-11-13 23:01:27 +00001395 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001396}
Anders Carlsson55085182007-08-21 17:43:55 +00001397
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001398Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001399 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001400 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001401 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1402 return ParseObjCThrowStmt(AtLoc);
1403 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1404 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001405 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001406 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001407 // If the expression is invalid, skip ahead to the next semicolon. Not
1408 // doing this opens us up to the possibility of infinite loops if
1409 // ParseExpression does not consume any tokens.
1410 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001411 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001412 }
1413 // Otherwise, eat the semicolon.
1414 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001415 return Actions.ActOnExprStmt(move(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001416}
1417
Sebastian Redl1d922962008-12-13 15:32:12 +00001418Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001419 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001420 case tok::string_literal: // primary-expression: string-literal
1421 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001422 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001423 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001424 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001425 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001426
Chris Lattner4fef81d2008-08-05 06:19:09 +00001427 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1428 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001429 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001430 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001431 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001432 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001433 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001434 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001435 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001436 }
Anders Carlsson55085182007-08-21 17:43:55 +00001437 }
Anders Carlsson55085182007-08-21 17:43:55 +00001438}
1439
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001440/// objc-message-expr:
1441/// '[' objc-receiver objc-message-args ']'
1442///
1443/// objc-receiver:
1444/// expression
1445/// class-name
1446/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001447Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001448 assert(Tok.is(tok::l_square) && "'[' expected");
1449 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1450
1451 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001452 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001453 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Steve Naroff5cb93b82008-11-19 15:54:23 +00001454 SourceLocation NameLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001455 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1456 ExprArg(Actions));
Chris Lattner699b6612008-01-25 18:59:06 +00001457 }
1458
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001459 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001460 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001461 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001462 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001463 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001464
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001465 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001466 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001467}
Sebastian Redl1d922962008-12-13 15:32:12 +00001468
Chris Lattner699b6612008-01-25 18:59:06 +00001469/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1470/// the rest of a message expression.
Sebastian Redl1d922962008-12-13 15:32:12 +00001471///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001472/// objc-message-args:
1473/// objc-selector
1474/// objc-keywordarg-list
1475///
1476/// objc-keywordarg-list:
1477/// objc-keywordarg
1478/// objc-keywordarg-list objc-keywordarg
1479///
1480/// objc-keywordarg:
1481/// selector-name[opt] ':' objc-keywordexpr
1482///
1483/// objc-keywordexpr:
1484/// nonempty-expr-list
1485///
1486/// nonempty-expr-list:
1487/// assignment-expression
1488/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001489///
1490Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001491Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001492 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001493 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001494 ExprArg ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001495 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001496 SourceLocation Loc;
1497 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001498
Anders Carlssonff975cf2009-02-14 18:21:46 +00001499 SourceLocation SelectorLoc = Loc;
1500
Steve Naroff68d331a2007-09-27 14:38:14 +00001501 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001502 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001503
Chris Lattnerdf195262007-10-09 17:51:17 +00001504 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001505 while (1) {
1506 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001507 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001508
Chris Lattnerdf195262007-10-09 17:51:17 +00001509 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001510 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-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);
Sebastian Redl1d922962008-12-13 15:32:12 +00001515 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001516 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001517
Steve Naroff68d331a2007-09-27 14:38:14 +00001518 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001519 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001520 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001521 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001522 // We must manually skip to a ']', otherwise the expression skipper will
1523 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1524 // the enclosing expression.
1525 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001526 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001527 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001528
Steve Naroff37387c92007-09-17 20:25:27 +00001529 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001530 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001531
Steve Naroff37387c92007-09-17 20:25:27 +00001532 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001533 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001534 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001535 break;
1536 // We have a selector or a colon, continue parsing.
1537 }
1538 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001539 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001540 ConsumeToken(); // Eat the ','.
1541 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001542 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001543 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001544 // We must manually skip to a ']', otherwise the expression skipper will
1545 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1546 // the enclosing expression.
1547 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001548 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001549 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001550
Steve Naroff49f109c2007-11-15 13:05:42 +00001551 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001552 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001553 }
1554 } else if (!selIdent) {
1555 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001556
Chris Lattner4fef81d2008-08-05 06:19:09 +00001557 // We must manually skip to a ']', otherwise the expression skipper will
1558 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1559 // the enclosing expression.
1560 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001561 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001562 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001563
Chris Lattnerdf195262007-10-09 17:51:17 +00001564 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001565 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001566 // We must manually skip to a ']', otherwise the expression skipper will
1567 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1568 // the enclosing expression.
1569 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001570 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001571 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001572
Chris Lattner699b6612008-01-25 18:59:06 +00001573 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001574
Steve Naroff29238a02007-10-05 18:42:47 +00001575 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001576 if (nKeys == 0)
1577 KeyIdents.push_back(selIdent);
1578 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001579
Chris Lattnerff384912007-10-07 02:00:24 +00001580 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001581 if (ReceiverName)
1582 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001583 LBracLoc, NameLoc, SelectorLoc,
1584 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001585 KeyExprs.take(), KeyExprs.size()));
1586 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001587 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001588 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001589}
1590
Sebastian Redl1d922962008-12-13 15:32:12 +00001591Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001592 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001593 if (Res.isInvalid()) return move(Res);
1594
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001595 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1596 // expressions. At this point, we know that the only valid thing that starts
1597 // with '@' is an @"".
1598 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001599 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001600 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001601 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001602
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001603 while (Tok.is(tok::at)) {
1604 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001605
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001606 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001607 if (!isTokenStringLiteral())
1608 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001609
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001610 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001611 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001612 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001613
Sebastian Redleffa8d12008-12-10 00:02:53 +00001614 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001615 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001616
1617 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1618 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001619}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001620
1621/// objc-encode-expression:
1622/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001623Parser::OwningExprResult
1624Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001625 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001626
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001627 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001628
Chris Lattner4fef81d2008-08-05 06:19:09 +00001629 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001630 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1631
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001632 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001633
Douglas Gregor809070a2009-02-18 17:45:20 +00001634 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001635
Anders Carlsson4988ae32007-08-23 15:31:37 +00001636 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001637
Douglas Gregor809070a2009-02-18 17:45:20 +00001638 if (Ty.isInvalid())
1639 return ExprError();
1640
1641 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
1642 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001643}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001644
1645/// objc-protocol-expression
1646/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001647Parser::OwningExprResult
1648Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001649 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001650
Chris Lattner4fef81d2008-08-05 06:19:09 +00001651 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001652 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1653
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001654 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001655
Chris Lattner4fef81d2008-08-05 06:19:09 +00001656 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001657 return ExprError(Diag(Tok, diag::err_expected_ident));
1658
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001659 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001660 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001661
Anders Carlsson4988ae32007-08-23 15:31:37 +00001662 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001663
Sebastian Redl1d922962008-12-13 15:32:12 +00001664 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1665 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001666}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001667
1668/// objc-selector-expression
1669/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001670Parser::OwningExprResult
1671Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001672 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001673
Chris Lattner4fef81d2008-08-05 06:19:09 +00001674 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001675 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1676
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001677 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001678 SourceLocation LParenLoc = ConsumeParen();
1679 SourceLocation sLoc;
1680 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001681 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1682 return ExprError(Diag(Tok, diag::err_expected_ident));
1683
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001684 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001685 unsigned nColons = 0;
1686 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001687 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001688 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001689 return ExprError(Diag(Tok, diag::err_expected_colon));
1690
Chris Lattnercb53b362007-12-27 19:57:00 +00001691 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001692 ConsumeToken(); // Eat the ':'.
1693 if (Tok.is(tok::r_paren))
1694 break;
1695 // Check for another keyword selector.
1696 SourceLocation Loc;
1697 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001698 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001699 if (!SelIdent && Tok.isNot(tok::colon))
1700 break;
1701 }
Steve Naroff887407e2007-12-05 22:21:29 +00001702 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001703 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001704 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001705 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1706 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001707 }