blob: d1c9be233fe07df454297cba6fb19da304cb645b [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"
Chris Lattner500d3292009-01-29 05:15:15 +000017#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21
Chris Lattner891dca62008-12-08 21:53:24 +000022/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000023/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000025/// [OBJC] objc-class-declaration
26/// [OBJC] objc-alias-declaration
27/// [OBJC] objc-protocol-definition
28/// [OBJC] objc-method-definition
29/// [OBJC] '@' 'end'
Chris Lattnerb28317a2009-03-28 19:18:32 +000030Parser::DeclPtrTy Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +000032
Douglas Gregorc464ae82009-12-07 09:27:33 +000033 if (Tok.is(tok::code_completion)) {
34 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, false);
35 ConsumeToken();
36 }
37
Steve Naroff861cf3e2007-08-23 18:16:40 +000038 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000039 case tok::objc_class:
40 return ParseObjCAtClassDeclaration(AtLoc);
41 case tok::objc_interface:
42 return ParseObjCAtInterfaceDeclaration(AtLoc);
43 case tok::objc_protocol:
44 return ParseObjCAtProtocolDeclaration(AtLoc);
45 case tok::objc_implementation:
46 return ParseObjCAtImplementationDeclaration(AtLoc);
47 case tok::objc_end:
48 return ParseObjCAtEndDeclaration(AtLoc);
49 case tok::objc_compatibility_alias:
50 return ParseObjCAtAliasDeclaration(AtLoc);
51 case tok::objc_synthesize:
52 return ParseObjCPropertySynthesize(AtLoc);
53 case tok::objc_dynamic:
54 return ParseObjCPropertyDynamic(AtLoc);
55 default:
56 Diag(AtLoc, diag::err_unexpected_at);
57 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000058 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000059 }
60}
61
62///
Mike Stump1eb44332009-09-09 15:08:12 +000063/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000064/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000065///
Chris Lattnerb28317a2009-03-28 19:18:32 +000066Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000067 ConsumeToken(); // the identifier "class"
68 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremenekc09cba62009-11-17 23:12:20 +000069 llvm::SmallVector<SourceLocation, 8> ClassLocs;
70
Mike Stump1eb44332009-09-09 15:08:12 +000071
Reid Spencer5f016e22007-07-11 17:01:13 +000072 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000073 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000074 Diag(Tok, diag::err_expected_ident);
75 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000076 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000077 }
Reid Spencer5f016e22007-07-11 17:01:13 +000078 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +000079 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000080 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattnerdf195262007-10-09 17:51:17 +000082 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000083 break;
Mike Stump1eb44332009-09-09 15:08:12 +000084
Reid Spencer5f016e22007-07-11 17:01:13 +000085 ConsumeToken();
86 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Reid Spencer5f016e22007-07-11 17:01:13 +000088 // Consume the ';'.
89 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattnerb28317a2009-03-28 19:18:32 +000090 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +000091
Ted Kremenekc09cba62009-11-17 23:12:20 +000092 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
93 ClassLocs.data(),
94 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000095}
96
Steve Naroffdac269b2007-08-20 21:31:48 +000097///
98/// objc-interface:
99/// objc-class-interface-attributes[opt] objc-class-interface
100/// objc-category-interface
101///
102/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000103/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000104/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000105/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000106/// objc-interface-decl-list
107/// @end
108///
109/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000110/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000111/// objc-protocol-refs[opt]
112/// objc-interface-decl-list
113/// @end
114///
115/// objc-superclass:
116/// ':' identifier
117///
118/// objc-class-interface-attributes:
119/// __attribute__((visibility("default")))
120/// __attribute__((visibility("hidden")))
121/// __attribute__((deprecated))
122/// __attribute__((unavailable))
123/// __attribute__((objc_exception)) - used by NSException on 64-bit
124///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000125Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroffdac269b2007-08-20 21:31:48 +0000126 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000127 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000128 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
129 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000131 // Code completion after '@interface'.
132 if (Tok.is(tok::code_completion)) {
133 Actions.CodeCompleteObjCInterfaceDecl(CurScope);
134 ConsumeToken();
135 }
136
Chris Lattnerdf195262007-10-09 17:51:17 +0000137 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000138 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000139 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000140 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000141
Steve Naroffdac269b2007-08-20 21:31:48 +0000142 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000143 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000144 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Chris Lattnerdf195262007-10-09 17:51:17 +0000146 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000147 SourceLocation lparenLoc = ConsumeParen();
148 SourceLocation categoryLoc, rparenLoc;
149 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000151 if (Tok.is(tok::code_completion)) {
152 Actions.CodeCompleteObjCInterfaceCategory(CurScope, nameId);
153 ConsumeToken();
154 }
155
Steve Naroff527fe232007-08-23 19:56:30 +0000156 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000157 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000158 categoryId = Tok.getIdentifierInfo();
159 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000160 } else if (!getLang().ObjC2) {
161 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000162 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000163 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000164 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000165 Diag(Tok, diag::err_expected_rparen);
166 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +0000167 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000168 }
169 rparenLoc = ConsumeParen();
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Steve Naroffdac269b2007-08-20 21:31:48 +0000171 // Next, we need to check for any protocol references.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000172 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000173 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000174 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000175 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000176 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
177 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000178 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Steve Naroffdac269b2007-08-20 21:31:48 +0000180 if (attrList) // categories don't support attributes.
181 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Jay Foadbeaaccd2009-05-21 09:52:38 +0000183 DeclPtrTy CategoryType =
Mike Stump1eb44332009-09-09 15:08:12 +0000184 Actions.ActOnStartCategoryInterface(atLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000185 nameId, nameLoc,
186 categoryId, categoryLoc,
187 ProtocolRefs.data(),
188 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000189 ProtocolLocs.data(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000190 EndProtoLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000192 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000193 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000194 }
195 // Parse a class interface.
196 IdentifierInfo *superClassId = 0;
197 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000198
Chris Lattnerdf195262007-10-09 17:51:17 +0000199 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000200 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000201
202 // Code completion of superclass names.
203 if (Tok.is(tok::code_completion)) {
204 Actions.CodeCompleteObjCSuperclass(CurScope, nameId);
205 ConsumeToken();
206 }
207
Chris Lattnerdf195262007-10-09 17:51:17 +0000208 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000209 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000210 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000211 }
212 superClassId = Tok.getIdentifierInfo();
213 superClassLoc = ConsumeToken();
214 }
215 // Next, we need to check for any protocol references.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000216 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000217 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
218 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000219 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000220 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
221 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000222 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000223
224 DeclPtrTy ClsType =
225 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000226 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000227 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000228 ProtocolLocs.data(),
Chris Lattner06036d32008-07-26 04:13:19 +0000229 EndProtoLoc, attrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Chris Lattnerdf195262007-10-09 17:51:17 +0000231 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000232 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000233
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000234 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000235 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000236}
237
John McCalld0014542009-12-03 22:31:13 +0000238/// The Objective-C property callback. This should be defined where
239/// it's used, but instead it's been lifted to here to support VS2005.
240struct Parser::ObjCPropertyCallback : FieldCallback {
241 Parser &P;
242 DeclPtrTy IDecl;
243 llvm::SmallVectorImpl<DeclPtrTy> &Props;
244 ObjCDeclSpec &OCDS;
245 SourceLocation AtLoc;
246 tok::ObjCKeywordKind MethodImplKind;
247
248 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
249 llvm::SmallVectorImpl<DeclPtrTy> &Props,
250 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
251 tok::ObjCKeywordKind MethodImplKind) :
252 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
253 MethodImplKind(MethodImplKind) {
254 }
255
256 DeclPtrTy invoke(FieldDeclarator &FD) {
257 if (FD.D.getIdentifier() == 0) {
258 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
259 << FD.D.getSourceRange();
260 return DeclPtrTy();
261 }
262 if (FD.BitfieldSize) {
263 P.Diag(AtLoc, diag::err_objc_property_bitfield)
264 << FD.D.getSourceRange();
265 return DeclPtrTy();
266 }
267
268 // Install the property declarator into interfaceDecl.
269 IdentifierInfo *SelName =
270 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
271
272 Selector GetterSel =
273 P.PP.getSelectorTable().getNullarySelector(SelName);
274 IdentifierInfo *SetterName = OCDS.getSetterName();
275 Selector SetterSel;
276 if (SetterName)
277 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
278 else
279 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
280 P.PP.getSelectorTable(),
281 FD.D.getIdentifier());
282 bool isOverridingProperty = false;
283 DeclPtrTy Property =
284 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
285 GetterSel, SetterSel, IDecl,
286 &isOverridingProperty,
287 MethodImplKind);
288 if (!isOverridingProperty)
289 Props.push_back(Property);
290
291 return Property;
292 }
293};
294
Steve Naroffdac269b2007-08-20 21:31:48 +0000295/// objc-interface-decl-list:
296/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000297/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000298/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000299/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000300/// objc-interface-decl-list declaration
301/// objc-interface-decl-list ';'
302///
Steve Naroff294494e2007-08-22 16:35:03 +0000303/// objc-method-requirement: [OBJC2]
304/// @required
305/// @optional
306///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000307void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000308 tok::ObjCKeywordKind contextKey) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000309 llvm::SmallVector<DeclPtrTy, 32> allMethods;
310 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner682bf922009-03-29 16:50:03 +0000311 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000312 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Ted Kremenek782f2f52010-01-07 01:20:12 +0000314 SourceRange AtEnd;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000315
Steve Naroff294494e2007-08-22 16:35:03 +0000316 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000317 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000318 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000319 DeclPtrTy methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000320 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000321 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000322 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
323 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000324 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
325 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000326 continue;
327 }
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Chris Lattnere82a10f2008-10-20 05:46:22 +0000329 // Ignore excess semicolons.
330 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000331 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000332 continue;
333 }
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Chris Lattnerbc662af2008-10-20 06:10:06 +0000335 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000336 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000337 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000339 // Code completion within an Objective-C interface.
340 if (Tok.is(tok::code_completion)) {
341 Actions.CodeCompleteOrdinaryName(CurScope,
342 ObjCImpDecl? Action::CCC_ObjCImplementation
343 : Action::CCC_ObjCInterface);
344 ConsumeToken();
345 }
346
Chris Lattnere82a10f2008-10-20 05:46:22 +0000347 // If we don't have an @ directive, parse it as a function definition.
348 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000349 // The code below does not consume '}'s because it is afraid of eating the
350 // end of a namespace. Because of the way this code is structured, an
351 // erroneous r_brace would cause an infinite loop if not handled here.
352 if (Tok.is(tok::r_brace))
353 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Steve Naroff4985ace2007-08-22 18:35:33 +0000355 // FIXME: as the name implies, this rule allows function definitions.
356 // We could pass a flag or check for functions during semantic analysis.
Sean Huntbbd37c62009-11-21 08:43:09 +0000357 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000358 continue;
359 }
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Chris Lattnere82a10f2008-10-20 05:46:22 +0000361 // Otherwise, we have an @ directive, eat the @.
362 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000363 if (Tok.is(tok::code_completion)) {
364 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
365 ConsumeToken();
366 break;
367 }
368
Chris Lattnera2449b22008-10-20 05:57:40 +0000369 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Chris Lattnera2449b22008-10-20 05:57:40 +0000371 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000372 AtEnd.setBegin(AtLoc);
373 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000374 break;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Chris Lattnerbc662af2008-10-20 06:10:06 +0000377 // Eat the identifier.
378 ConsumeToken();
379
Chris Lattnera2449b22008-10-20 05:57:40 +0000380 switch (DirectiveKind) {
381 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000382 // FIXME: If someone forgets an @end on a protocol, this loop will
383 // continue to eat up tons of stuff and spew lots of nonsense errors. It
384 // would probably be better to bail out if we saw an @class or @interface
385 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000386 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000387 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000388 SkipUntil(tok::r_brace, tok::at);
389 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Chris Lattnera2449b22008-10-20 05:57:40 +0000391 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000392 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000393 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000394 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000395 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000396 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000397 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000398 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000399 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Chris Lattnera2449b22008-10-20 05:57:40 +0000401 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000402 if (!getLang().ObjC2)
403 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
404
Chris Lattnere82a10f2008-10-20 05:46:22 +0000405 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000406 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000407 if (Tok.is(tok::l_paren))
Douglas Gregor4ad96852009-11-19 07:41:15 +0000408 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
409 allMethods.data(), allMethods.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000410
John McCalld0014542009-12-03 22:31:13 +0000411 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
412 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000413
Chris Lattnere82a10f2008-10-20 05:46:22 +0000414 // Parse all the comma separated declarators.
415 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000416 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000418 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
419 tok::at);
Chris Lattnera2449b22008-10-20 05:57:40 +0000420 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000421 }
Steve Naroff294494e2007-08-22 16:35:03 +0000422 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000423
424 // We break out of the big loop in two cases: when we see @end or when we see
425 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000426 if (Tok.is(tok::code_completion)) {
427 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
428 ConsumeToken();
429 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000430 ConsumeToken(); // the "end" identifier
431 else
432 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Chris Lattnera2449b22008-10-20 05:57:40 +0000434 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000435 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek782f2f52010-01-07 01:20:12 +0000436 Actions.ActOnAtEnd(AtEnd, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000437 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000438 allProperties.data(), allProperties.size(),
439 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000440}
441
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000442/// Parse property attribute declarations.
443///
444/// property-attr-decl: '(' property-attrlist ')'
445/// property-attrlist:
446/// property-attribute
447/// property-attrlist ',' property-attribute
448/// property-attribute:
449/// getter '=' identifier
450/// setter '=' identifier ':'
451/// readonly
452/// readwrite
453/// assign
454/// retain
455/// copy
456/// nonatomic
457///
Douglas Gregor4ad96852009-11-19 07:41:15 +0000458void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl,
459 DeclPtrTy *Methods,
460 unsigned NumMethods) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000461 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000462 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000464 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000465 if (Tok.is(tok::code_completion)) {
Douglas Gregora93b1082009-11-18 23:08:07 +0000466 Actions.CodeCompleteObjCPropertyFlags(CurScope, DS);
Steve Naroffece8e712009-10-08 21:55:05 +0000467 ConsumeToken();
468 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000469 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000471 // If this is not an identifier at all, bail out early.
472 if (II == 0) {
473 MatchRHSPunctuation(tok::r_paren, LHSLoc);
474 return;
475 }
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Chris Lattner156b0612008-10-20 07:37:22 +0000477 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Chris Lattner92e62b02008-11-20 04:42:34 +0000479 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000480 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000481 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000482 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000483 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000484 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000485 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000486 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000487 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000488 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000489 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000490 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000491 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000492 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000493 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
494 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000495 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Douglas Gregor4ad96852009-11-19 07:41:15 +0000497 if (Tok.is(tok::code_completion)) {
498 if (II->getNameStart()[0] == 's')
499 Actions.CodeCompleteObjCPropertySetter(CurScope, ClassDecl,
500 Methods, NumMethods);
501 else
502 Actions.CodeCompleteObjCPropertyGetter(CurScope, ClassDecl,
503 Methods, NumMethods);
504 ConsumeToken();
505 }
506
Chris Lattner8ca329c2008-10-20 07:24:39 +0000507 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000508 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000509 SkipUntil(tok::r_paren);
510 return;
511 }
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Daniel Dunbare013d682009-10-18 20:26:12 +0000513 if (II->getNameStart()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000514 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
515 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000516 ConsumeToken(); // consume method name
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000518 if (ExpectAndConsume(tok::colon,
519 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000520 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000521 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000522 } else {
523 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
524 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000525 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000526 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000527 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000528 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000529 SkipUntil(tok::r_paren);
530 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000531 }
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Chris Lattner156b0612008-10-20 07:37:22 +0000533 if (Tok.isNot(tok::comma))
534 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Chris Lattner156b0612008-10-20 07:37:22 +0000536 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000537 }
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Chris Lattner156b0612008-10-20 07:37:22 +0000539 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000540}
541
Steve Naroff3536b442007-09-06 21:24:23 +0000542/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000543/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000544/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000545///
546/// objc-instance-method: '-'
547/// objc-class-method: '+'
548///
Steve Naroff4985ace2007-08-22 18:35:33 +0000549/// objc-method-attributes: [OBJC2]
550/// __attribute__((deprecated))
551///
Mike Stump1eb44332009-09-09 15:08:12 +0000552Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000553 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000554 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000555
Mike Stump1eb44332009-09-09 15:08:12 +0000556 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000557 SourceLocation mLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattnerb28317a2009-03-28 19:18:32 +0000559 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000560 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000561 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000562 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000563}
564
565/// objc-selector:
566/// identifier
567/// one of
568/// enum struct union if else while do for switch case default
569/// break continue return goto asm sizeof typeof __alignof
570/// unsigned long const short volatile signed restrict _Complex
571/// in out inout bycopy byref oneway int char float double void _Bool
572///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000573IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000574 switch (Tok.getKind()) {
575 default:
576 return 0;
577 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000578 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000579 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000580 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000581 case tok::kw_break:
582 case tok::kw_case:
583 case tok::kw_catch:
584 case tok::kw_char:
585 case tok::kw_class:
586 case tok::kw_const:
587 case tok::kw_const_cast:
588 case tok::kw_continue:
589 case tok::kw_default:
590 case tok::kw_delete:
591 case tok::kw_do:
592 case tok::kw_double:
593 case tok::kw_dynamic_cast:
594 case tok::kw_else:
595 case tok::kw_enum:
596 case tok::kw_explicit:
597 case tok::kw_export:
598 case tok::kw_extern:
599 case tok::kw_false:
600 case tok::kw_float:
601 case tok::kw_for:
602 case tok::kw_friend:
603 case tok::kw_goto:
604 case tok::kw_if:
605 case tok::kw_inline:
606 case tok::kw_int:
607 case tok::kw_long:
608 case tok::kw_mutable:
609 case tok::kw_namespace:
610 case tok::kw_new:
611 case tok::kw_operator:
612 case tok::kw_private:
613 case tok::kw_protected:
614 case tok::kw_public:
615 case tok::kw_register:
616 case tok::kw_reinterpret_cast:
617 case tok::kw_restrict:
618 case tok::kw_return:
619 case tok::kw_short:
620 case tok::kw_signed:
621 case tok::kw_sizeof:
622 case tok::kw_static:
623 case tok::kw_static_cast:
624 case tok::kw_struct:
625 case tok::kw_switch:
626 case tok::kw_template:
627 case tok::kw_this:
628 case tok::kw_throw:
629 case tok::kw_true:
630 case tok::kw_try:
631 case tok::kw_typedef:
632 case tok::kw_typeid:
633 case tok::kw_typename:
634 case tok::kw_typeof:
635 case tok::kw_union:
636 case tok::kw_unsigned:
637 case tok::kw_using:
638 case tok::kw_virtual:
639 case tok::kw_void:
640 case tok::kw_volatile:
641 case tok::kw_wchar_t:
642 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000643 case tok::kw__Bool:
644 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000645 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000646 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000647 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000648 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000649 }
Steve Naroff294494e2007-08-22 16:35:03 +0000650}
651
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000652/// objc-for-collection-in: 'in'
653///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000654bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000655 // FIXME: May have to do additional look-ahead to only allow for
656 // valid tokens following an 'in'; such as an identifier, unary operators,
657 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000658 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000659 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000660}
661
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000662/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000663/// qualifier list and builds their bitmask representation in the input
664/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000665///
666/// objc-type-qualifiers:
667/// objc-type-qualifier
668/// objc-type-qualifiers objc-type-qualifier
669///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000670void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000671 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000672 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000673 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Chris Lattnere8b724d2007-12-12 06:56:32 +0000675 const IdentifierInfo *II = Tok.getIdentifierInfo();
676 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000677 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000678 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000680 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000681 switch (i) {
682 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000683 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
684 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
685 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
686 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
687 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
688 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000689 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000690 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000691 ConsumeToken();
692 II = 0;
693 break;
694 }
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Chris Lattnere8b724d2007-12-12 06:56:32 +0000696 // If this wasn't a recognized qualifier, bail out.
697 if (II) return;
698 }
699}
700
701/// objc-type-name:
702/// '(' objc-type-qualifiers[opt] type-name ')'
703/// '(' objc-type-qualifiers[opt] ')'
704///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000705Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000706 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Chris Lattner4a76b292008-10-22 03:52:06 +0000708 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000709 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000711 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000712 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000713
Chris Lattner4a76b292008-10-22 03:52:06 +0000714 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000715 if (isTypeSpecifierQualifier()) {
716 TypeResult TypeSpec = ParseTypeName();
717 if (!TypeSpec.isInvalid())
718 Ty = TypeSpec.get();
719 }
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Steve Naroffd7333c22008-10-21 14:15:04 +0000721 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000722 ConsumeParen();
723 else if (Tok.getLocation() == TypeStartLoc) {
724 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000725 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000726 SkipUntil(tok::r_paren);
727 } else {
728 // Otherwise, we found *something*, but didn't get a ')' in the right
729 // place. Emit an error then return what we have as the type.
730 MatchRHSPunctuation(tok::r_paren, LParenLoc);
731 }
Steve Narofff28b2642007-09-05 23:30:30 +0000732 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000733}
734
735/// objc-method-decl:
736/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000737/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000738/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000739/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000740///
741/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000742/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000743/// objc-keyword-selector objc-keyword-decl
744///
745/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000746/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
747/// objc-selector ':' objc-keyword-attributes[opt] identifier
748/// ':' objc-type-name objc-keyword-attributes[opt] identifier
749/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000750///
Steve Naroff4985ace2007-08-22 18:35:33 +0000751/// objc-parmlist:
752/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000753///
Steve Naroff4985ace2007-08-22 18:35:33 +0000754/// objc-parms:
755/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000756///
Steve Naroff4985ace2007-08-22 18:35:33 +0000757/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000758/// , ...
759///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000760/// objc-keyword-attributes: [OBJC2]
761/// __attribute__((unused))
762///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000763Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
764 tok::TokenKind mType,
765 DeclPtrTy IDecl,
766 tok::ObjCKeywordKind MethodImplKind) {
John McCall54abf7d2009-11-04 02:18:39 +0000767 ParsingDeclRAIIObject PD(*this);
768
Chris Lattnere8904e92008-08-23 01:48:03 +0000769 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000770 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000771 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000772 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000773 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Steve Naroffbef11852007-10-26 20:53:56 +0000775 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000776 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000777
Steve Naroff84c43102009-02-11 20:43:13 +0000778 // An unnamed colon is valid.
779 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000780 Diag(Tok, diag::err_expected_selector_for_method)
781 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000782 // Skip until we get a ; or {}.
783 SkipUntil(tok::r_brace);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000784 return DeclPtrTy();
Chris Lattnere8904e92008-08-23 01:48:03 +0000785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000787 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000788 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000789 // If attributes exist after the method, parse them.
Ted Kremenek1e377652010-02-11 02:19:13 +0000790 llvm::OwningPtr<AttributeList> MethodAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000791 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +0000792 MethodAttrs.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Chris Lattnerff384912007-10-07 02:00:24 +0000794 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall54abf7d2009-11-04 02:18:39 +0000795 DeclPtrTy Result
796 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000797 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1e377652010-02-11 02:19:13 +0000798 0, CargNames, MethodAttrs.get(),
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000799 MethodImplKind);
John McCall54abf7d2009-11-04 02:18:39 +0000800 PD.complete(Result);
801 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000802 }
Steve Narofff28b2642007-09-05 23:30:30 +0000803
Steve Naroff68d331a2007-09-27 14:38:14 +0000804 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnere294d3f2009-04-11 18:57:04 +0000805 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Chris Lattnerff384912007-10-07 02:00:24 +0000807 while (1) {
Chris Lattnere294d3f2009-04-11 18:57:04 +0000808 Action::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Chris Lattnerff384912007-10-07 02:00:24 +0000810 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000811 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000812 Diag(Tok, diag::err_expected_colon);
813 break;
814 }
815 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Chris Lattnere294d3f2009-04-11 18:57:04 +0000817 ArgInfo.Type = 0;
818 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
819 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
820
Chris Lattnerff384912007-10-07 02:00:24 +0000821 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000822 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000823 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000824 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000825
Chris Lattnerdf195262007-10-09 17:51:17 +0000826 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000827 Diag(Tok, diag::err_expected_ident); // missing argument name.
828 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000829 }
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Chris Lattnere294d3f2009-04-11 18:57:04 +0000831 ArgInfo.Name = Tok.getIdentifierInfo();
832 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000833 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Chris Lattnere294d3f2009-04-11 18:57:04 +0000835 ArgInfos.push_back(ArgInfo);
836 KeyIdents.push_back(SelIdent);
837
Chris Lattnerff384912007-10-07 02:00:24 +0000838 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000839 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000840 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000841 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000842 break;
843 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000844 }
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Steve Naroff335eafa2007-11-15 12:35:21 +0000846 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Chris Lattnerff384912007-10-07 02:00:24 +0000848 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000849 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000850 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000851 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000852 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000853 ConsumeToken();
854 break;
855 }
Chris Lattnerff384912007-10-07 02:00:24 +0000856 DeclSpec DS;
857 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000858 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000859 Declarator ParmDecl(DS, Declarator::PrototypeContext);
860 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000861 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000862 }
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Chris Lattnerff384912007-10-07 02:00:24 +0000864 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000865 // If attributes exist after the method, parse them.
Ted Kremenek1e377652010-02-11 02:19:13 +0000866 llvm::OwningPtr<AttributeList> MethodAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000867 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +0000868 MethodAttrs.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000870 if (KeyIdents.size() == 0)
871 return DeclPtrTy();
Chris Lattnerff384912007-10-07 02:00:24 +0000872 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
873 &KeyIdents[0]);
John McCall54abf7d2009-11-04 02:18:39 +0000874 DeclPtrTy Result
875 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000876 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1e377652010-02-11 02:19:13 +0000877 &ArgInfos[0], CargNames,
878 MethodAttrs.get(),
Steve Naroff335eafa2007-11-15 12:35:21 +0000879 MethodImplKind, isVariadic);
John McCall54abf7d2009-11-04 02:18:39 +0000880 PD.complete(Result);
Ted Kremenek1e377652010-02-11 02:19:13 +0000881
882 // Delete referenced AttributeList objects.
883 for (llvm::SmallVectorImpl<Action::ObjCArgInfo>::iterator
884 I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I)
885 delete I->ArgAttrs;
886
John McCall54abf7d2009-11-04 02:18:39 +0000887 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +0000888}
889
Steve Naroffdac269b2007-08-20 21:31:48 +0000890/// objc-protocol-refs:
891/// '<' identifier-list '>'
892///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000893bool Parser::
Chris Lattnerb28317a2009-03-28 19:18:32 +0000894ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000895 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
896 bool WarnOnDeclarations,
897 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000898 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000900 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Chris Lattnere13b9592008-07-26 04:03:38 +0000902 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Chris Lattnere13b9592008-07-26 04:03:38 +0000904 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +0000905 if (Tok.is(tok::code_completion)) {
906 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
907 ProtocolIdents.size());
908 ConsumeToken();
909 }
910
Chris Lattnere13b9592008-07-26 04:03:38 +0000911 if (Tok.isNot(tok::identifier)) {
912 Diag(Tok, diag::err_expected_ident);
913 SkipUntil(tok::greater);
914 return true;
915 }
916 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
917 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000918 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +0000919 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Chris Lattnere13b9592008-07-26 04:03:38 +0000921 if (Tok.isNot(tok::comma))
922 break;
923 ConsumeToken();
924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Chris Lattnere13b9592008-07-26 04:03:38 +0000926 // Consume the '>'.
927 if (Tok.isNot(tok::greater)) {
928 Diag(Tok, diag::err_expected_greater);
929 return true;
930 }
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Chris Lattnere13b9592008-07-26 04:03:38 +0000932 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Chris Lattnere13b9592008-07-26 04:03:38 +0000934 // Convert the list of protocols identifiers into a list of protocol decls.
935 Actions.FindProtocolDeclaration(WarnOnDeclarations,
936 &ProtocolIdents[0], ProtocolIdents.size(),
937 Protocols);
938 return false;
939}
940
Steve Naroffdac269b2007-08-20 21:31:48 +0000941/// objc-class-instance-variables:
942/// '{' objc-instance-variable-decl-list[opt] '}'
943///
944/// objc-instance-variable-decl-list:
945/// objc-visibility-spec
946/// objc-instance-variable-decl ';'
947/// ';'
948/// objc-instance-variable-decl-list objc-visibility-spec
949/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
950/// objc-instance-variable-decl-list ';'
951///
952/// objc-visibility-spec:
953/// @private
954/// @protected
955/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000956/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000957///
958/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +0000959/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +0000960///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000961void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff60fccee2007-10-29 21:38:07 +0000962 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000963 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +0000964 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000965
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000966 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000967
Steve Naroffddbff782007-08-21 21:17:12 +0000968 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000970 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000971 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000972 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000973 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Steve Naroffddbff782007-08-21 21:17:12 +0000975 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000976 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +0000977 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner29d9c1a2009-12-06 17:36:05 +0000978 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +0000979 ConsumeToken();
980 continue;
981 }
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Steve Naroffddbff782007-08-21 21:17:12 +0000983 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000984 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000985 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +0000986
987 if (Tok.is(tok::code_completion)) {
988 Actions.CodeCompleteObjCAtVisibility(CurScope);
989 ConsumeToken();
990 }
991
Steve Naroff861cf3e2007-08-23 18:16:40 +0000992 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000993 case tok::objc_private:
994 case tok::objc_public:
995 case tok::objc_protected:
996 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000997 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000998 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000999 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001000 default:
1001 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001002 continue;
1003 }
1004 }
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001006 if (Tok.is(tok::code_completion)) {
1007 Actions.CodeCompleteOrdinaryName(CurScope,
1008 Action::CCC_ObjCInstanceVariableList);
1009 ConsumeToken();
1010 }
1011
John McCallbdd563e2009-11-03 02:38:08 +00001012 struct ObjCIvarCallback : FieldCallback {
1013 Parser &P;
1014 DeclPtrTy IDecl;
1015 tok::ObjCKeywordKind visibility;
1016 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
1017
1018 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
1019 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
1020 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1021 }
1022
1023 DeclPtrTy invoke(FieldDeclarator &FD) {
1024 // Install the declarator into the interface decl.
1025 DeclPtrTy Field
1026 = P.Actions.ActOnIvar(P.CurScope,
1027 FD.D.getDeclSpec().getSourceRange().getBegin(),
1028 IDecl, FD.D, FD.BitfieldSize, visibility);
1029 AllIvarDecls.push_back(Field);
1030 return Field;
1031 }
1032 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1033
Chris Lattnere1359422008-04-10 06:46:29 +00001034 // Parse all the comma separated declarators.
1035 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +00001036 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Chris Lattnerdf195262007-10-09 17:51:17 +00001038 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001039 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001040 } else {
1041 Diag(Tok, diag::err_expected_semi_decl_list);
1042 // Skip to end of block or statement
1043 SkipUntil(tok::r_brace, true, true);
1044 }
1045 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001046 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +00001047 // Call ActOnFields() even if we don't have any decls. This is useful
1048 // for code rewriting tools that need to be aware of the empty list.
1049 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001050 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001051 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001052 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001053}
Steve Naroffdac269b2007-08-20 21:31:48 +00001054
1055/// objc-protocol-declaration:
1056/// objc-protocol-definition
1057/// objc-protocol-forward-reference
1058///
1059/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001060/// @protocol identifier
1061/// objc-protocol-refs[opt]
1062/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001063/// @end
1064///
1065/// objc-protocol-forward-reference:
1066/// @protocol identifier-list ';'
1067///
1068/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001069/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001070/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001071Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1072 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001073 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001074 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1075 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Douglas Gregor083128f2009-11-18 04:49:41 +00001077 if (Tok.is(tok::code_completion)) {
1078 Actions.CodeCompleteObjCProtocolDecl(CurScope);
1079 ConsumeToken();
1080 }
1081
Chris Lattnerdf195262007-10-09 17:51:17 +00001082 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001083 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001084 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001085 }
1086 // Save the protocol name, then consume it.
1087 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1088 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Chris Lattnerdf195262007-10-09 17:51:17 +00001090 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001091 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001092 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001093 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001094 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001095 }
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Chris Lattnerdf195262007-10-09 17:51:17 +00001097 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001098 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1099 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1100
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001101 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001102 while (1) {
1103 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001104 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001105 Diag(Tok, diag::err_expected_ident);
1106 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001107 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001108 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001109 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1110 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001111 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001112
Chris Lattnerdf195262007-10-09 17:51:17 +00001113 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001114 break;
1115 }
1116 // Consume the ';'.
1117 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001118 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Steve Naroffe440eb82007-10-10 17:32:04 +00001120 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001121 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001122 ProtocolRefs.size(),
1123 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001124 }
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001126 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001127 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001128
Chris Lattnerb28317a2009-03-28 19:18:32 +00001129 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001130 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001131 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001132 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1133 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001134 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Chris Lattnerb28317a2009-03-28 19:18:32 +00001136 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001137 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001138 ProtocolRefs.data(),
1139 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001140 ProtocolLocs.data(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001141 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001142 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001143 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001144}
Steve Naroffdac269b2007-08-20 21:31:48 +00001145
1146/// objc-implementation:
1147/// objc-class-implementation-prologue
1148/// objc-category-implementation-prologue
1149///
1150/// objc-class-implementation-prologue:
1151/// @implementation identifier objc-superclass[opt]
1152/// objc-class-instance-variables[opt]
1153///
1154/// objc-category-implementation-prologue:
1155/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001156Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001157 SourceLocation atLoc) {
1158 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1159 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1160 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001162 // Code completion after '@implementation'.
1163 if (Tok.is(tok::code_completion)) {
1164 Actions.CodeCompleteObjCImplementationDecl(CurScope);
1165 ConsumeToken();
1166 }
1167
Chris Lattnerdf195262007-10-09 17:51:17 +00001168 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001169 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001170 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001171 }
1172 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001173 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001174 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001175
1176 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001177 // we have a category implementation.
1178 SourceLocation lparenLoc = ConsumeParen();
1179 SourceLocation categoryLoc, rparenLoc;
1180 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001182 if (Tok.is(tok::code_completion)) {
1183 Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
1184 ConsumeToken();
1185 }
1186
Chris Lattnerdf195262007-10-09 17:51:17 +00001187 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001188 categoryId = Tok.getIdentifierInfo();
1189 categoryLoc = ConsumeToken();
1190 } else {
1191 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001192 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001193 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001194 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001195 Diag(Tok, diag::err_expected_rparen);
1196 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001197 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001198 }
1199 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001200 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001201 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001202 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001203 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001204 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001205 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001206 }
1207 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001208 SourceLocation superClassLoc;
1209 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001210 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001211 // We have a super class
1212 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001213 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001214 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001215 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001216 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001217 superClassId = Tok.getIdentifierInfo();
1218 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001219 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001220 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001221 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001222 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Steve Naroff60fccee2007-10-29 21:38:07 +00001224 if (Tok.is(tok::l_brace)) // we have ivars
1225 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001226 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001227 PendingObjCImpDecl.push_back(ObjCImpDecl);
1228
Chris Lattnerb28317a2009-03-28 19:18:32 +00001229 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001230}
Steve Naroff60fccee2007-10-29 21:38:07 +00001231
Ted Kremenek782f2f52010-01-07 01:20:12 +00001232Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001233 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1234 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001235 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001236 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001237 if (ObjCImpDecl) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001238 Actions.ActOnAtEnd(atEnd, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001239 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001240 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001241 }
Ted Kremenek782f2f52010-01-07 01:20:12 +00001242 else {
1243 // missing @implementation
1244 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1245 }
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001246 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001247}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001248
Fariborz Jahanian1ac71042009-11-17 17:15:16 +00001249Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001250 if (PendingObjCImpDecl.empty())
1251 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1252 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
Ted Kremenek782f2f52010-01-07 01:20:12 +00001253 Actions.ActOnAtEnd(SourceRange(), ImpDecl);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001254 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1255}
1256
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001257/// compatibility-alias-decl:
1258/// @compatibility_alias alias-name class-name ';'
1259///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001260Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001261 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1262 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1263 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001264 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001265 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001266 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001267 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001268 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1269 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001270 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001271 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001272 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001273 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001274 IdentifierInfo *classId = Tok.getIdentifierInfo();
1275 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1276 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001277 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001278 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001279 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001280 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1281 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001282}
1283
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001284/// property-synthesis:
1285/// @synthesize property-ivar-list ';'
1286///
1287/// property-ivar-list:
1288/// property-ivar
1289/// property-ivar-list ',' property-ivar
1290///
1291/// property-ivar:
1292/// identifier
1293/// identifier '=' identifier
1294///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001295Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001296 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1297 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001298 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Douglas Gregorb328c422009-11-18 19:45:45 +00001300 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001301 if (Tok.is(tok::code_completion)) {
Douglas Gregor424b2a52009-11-18 22:56:13 +00001302 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
Douglas Gregor322328b2009-11-18 22:32:06 +00001303 ConsumeToken();
1304 }
1305
Douglas Gregorb328c422009-11-18 19:45:45 +00001306 if (Tok.isNot(tok::identifier)) {
1307 Diag(Tok, diag::err_synthesized_property_name);
1308 SkipUntil(tok::semi);
1309 return DeclPtrTy();
1310 }
1311
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001312 IdentifierInfo *propertyIvar = 0;
1313 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1314 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001315 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001316 // property '=' ivar-name
1317 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001318
1319 if (Tok.is(tok::code_completion)) {
1320 Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1321 ObjCImpDecl);
1322 ConsumeToken();
1323 }
1324
Chris Lattnerdf195262007-10-09 17:51:17 +00001325 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001326 Diag(Tok, diag::err_expected_ident);
1327 break;
1328 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001329 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001330 ConsumeToken(); // consume ivar-name
1331 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001332 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1333 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001334 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001335 break;
1336 ConsumeToken(); // consume ','
1337 }
Douglas Gregorb328c422009-11-18 19:45:45 +00001338 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001339 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregorb328c422009-11-18 19:45:45 +00001340 SkipUntil(tok::semi);
1341 }
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001342 else
1343 ConsumeToken(); // consume ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001344 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001345}
1346
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001347/// property-dynamic:
1348/// @dynamic property-list
1349///
1350/// property-list:
1351/// identifier
1352/// property-list ',' identifier
1353///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001354Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001355 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1356 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1357 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001358 while (true) {
1359 if (Tok.is(tok::code_completion)) {
1360 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1361 ConsumeToken();
1362 }
1363
1364 if (Tok.isNot(tok::identifier)) {
1365 Diag(Tok, diag::err_expected_ident);
1366 SkipUntil(tok::semi);
1367 return DeclPtrTy();
1368 }
1369
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001370 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1371 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1372 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1373 propertyId, 0);
1374
Chris Lattnerdf195262007-10-09 17:51:17 +00001375 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001376 break;
1377 ConsumeToken(); // consume ','
1378 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001379 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001380 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001381 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001382}
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001384/// objc-throw-statement:
1385/// throw expression[opt];
1386///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001387Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001388 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001389 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001390 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001391 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001392 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001393 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001394 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001395 }
1396 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001397 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001398 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001399}
1400
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001401/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001402/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001403///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001404Parser::OwningStmtResult
1405Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001406 ConsumeToken(); // consume synchronized
1407 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001408 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001409 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001410 }
1411 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001412 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001413 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001414 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001415 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001416 }
1417 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001418 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001419 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001420 }
1421 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001422 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001423 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001424 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001425 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001426 // Enter a scope to hold everything within the compound stmt. Compound
1427 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001428 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001429
Sebastian Redl61364dd2008-12-11 19:30:53 +00001430 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001431
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001432 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001433 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001434 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001435 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001436}
1437
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001438/// objc-try-catch-statement:
1439/// @try compound-statement objc-catch-list[opt]
1440/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1441///
1442/// objc-catch-list:
1443/// @catch ( parameter-declaration ) compound-statement
1444/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1445/// catch-parameter-declaration:
1446/// parameter-declaration
1447/// '...' [OBJC2]
1448///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001449Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001450 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001451
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001452 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001453 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001454 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001455 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001456 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001457 OwningStmtResult CatchStmts(Actions);
1458 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001459 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001460 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001461 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001462 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001463 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001464
Chris Lattnerdf195262007-10-09 17:51:17 +00001465 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001466 // At this point, we need to lookahead to determine if this @ is the start
1467 // of an @catch or @finally. We don't want to consume the @ token if this
1468 // is an @try or @encode or something else.
1469 Token AfterAt = GetLookAheadToken(1);
1470 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1471 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1472 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001473
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001474 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001475 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001476 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001477 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001478 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001479 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001480 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001481 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001482 DeclSpec DS;
1483 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001484 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001485 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001486 // we must accept either 'declarator' or 'abstract-declarator' here.
1487 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1488 ParseDeclarator(ParmDecl);
1489
1490 // Inform the actions module about the parameter declarator, so it
1491 // gets added to the current scope.
Fariborz Jahaniand219a3a2010-02-03 00:32:51 +00001492 // FIXME. Probably can build a VarDecl and avoid setting DeclContext.
Steve Naroff7ba138a2009-03-03 19:52:17 +00001493 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Fariborz Jahanian2f764f12010-02-03 00:01:43 +00001494 Actions.ActOnObjCCatchParam(FirstPart);
Steve Naroff64515f32008-02-05 21:27:35 +00001495 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001496 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Steve Naroff93a25952009-04-07 22:56:58 +00001498 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Steve Naroff93a25952009-04-07 22:56:58 +00001500 if (Tok.is(tok::r_paren))
1501 RParenLoc = ConsumeParen();
1502 else // Skip over garbage, until we get to ')'. Eat the ')'.
1503 SkipUntil(tok::r_paren, true, false);
1504
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001505 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001506 if (Tok.is(tok::l_brace))
1507 CatchBody = ParseCompoundStatementBody();
1508 else
1509 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001510 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001511 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001512 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001513 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001514 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001515 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001516 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1517 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001518 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001519 }
1520 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001521 } else {
1522 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001523 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001524 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001525
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001526 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001527 if (Tok.is(tok::l_brace))
1528 FinallyBody = ParseCompoundStatementBody();
1529 else
1530 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001531 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001532 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001533 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001534 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001535 catch_or_finally_seen = true;
1536 break;
1537 }
1538 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001539 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001540 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001541 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001542 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001543 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1544 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001545}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001546
Steve Naroff3536b442007-09-06 21:24:23 +00001547/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001548///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001549Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1550 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Chris Lattner49f28ca2009-03-05 08:00:35 +00001552 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1553 PP.getSourceManager(),
1554 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001556 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001557 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001558 if (ObjCImpDecl) {
1559 Diag(Tok, diag::warn_semicolon_before_method_body)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001560 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001561 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001562 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001563 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001564
Steve Naroff409be832007-11-11 19:54:21 +00001565 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001566 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001567 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Steve Naroff409be832007-11-11 19:54:21 +00001569 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1570 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Steve Naroff409be832007-11-11 19:54:21 +00001572 // If we didn't find the '{', bail out.
1573 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001574 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001575 }
Steve Naroff409be832007-11-11 19:54:21 +00001576 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Steve Naroff409be832007-11-11 19:54:21 +00001578 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001579 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Steve Naroff409be832007-11-11 19:54:21 +00001581 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001582 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001583 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001584
1585 OwningStmtResult FnBody(ParseCompoundStatementBody());
1586
Steve Naroff409be832007-11-11 19:54:21 +00001587 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001588 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001589 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1590 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001591
Steve Naroff32ce8372009-03-02 22:00:56 +00001592 // TODO: Pass argument information.
1593 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Steve Naroff409be832007-11-11 19:54:21 +00001595 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001596 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001597
Steve Naroff71c0a952007-11-13 23:01:27 +00001598 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001599}
Anders Carlsson55085182007-08-21 17:43:55 +00001600
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001601Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001602 if (Tok.is(tok::code_completion)) {
1603 Actions.CodeCompleteObjCAtStatement(CurScope);
1604 ConsumeToken();
1605 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001606 }
1607
1608 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001609 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001610
1611 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001612 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001613
1614 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001615 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001616
Sebastian Redld8c4e152008-12-11 22:33:27 +00001617 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001618 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001619 // If the expression is invalid, skip ahead to the next semicolon. Not
1620 // doing this opens us up to the possibility of infinite loops if
1621 // ParseExpression does not consume any tokens.
1622 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001623 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001624 }
Chris Lattner5d803162009-12-07 16:33:19 +00001625
Steve Naroff64515f32008-02-05 21:27:35 +00001626 // Otherwise, eat the semicolon.
1627 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson5ee56e92009-12-16 02:09:40 +00001628 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001629}
1630
Sebastian Redl1d922962008-12-13 15:32:12 +00001631Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001632 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001633 case tok::code_completion:
1634 Actions.CodeCompleteObjCAtExpression(CurScope);
1635 ConsumeToken();
1636 return ExprError();
1637
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001638 case tok::string_literal: // primary-expression: string-literal
1639 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001640 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001641 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001642 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001643 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001644
Chris Lattner4fef81d2008-08-05 06:19:09 +00001645 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1646 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001647 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001648 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001649 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001650 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001651 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001652 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001653 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001654 }
Anders Carlsson55085182007-08-21 17:43:55 +00001655 }
Anders Carlsson55085182007-08-21 17:43:55 +00001656}
1657
Mike Stump1eb44332009-09-09 15:08:12 +00001658/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001659/// '[' objc-receiver objc-message-args ']'
1660///
1661/// objc-receiver:
1662/// expression
1663/// class-name
1664/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001665Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001666 assert(Tok.is(tok::l_square) && "'[' expected");
1667 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1668
1669 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001670 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001671 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001672 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1673 SourceLocation NameLoc = ConsumeToken();
1674 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1675 ExprArg(Actions));
1676 }
Chris Lattner699b6612008-01-25 18:59:06 +00001677 }
1678
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001679 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001680 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001681 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001682 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001683 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001684
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001685 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001686 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001687}
Sebastian Redl1d922962008-12-13 15:32:12 +00001688
Chris Lattner699b6612008-01-25 18:59:06 +00001689/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1690/// the rest of a message expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001691///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001692/// objc-message-args:
1693/// objc-selector
1694/// objc-keywordarg-list
1695///
1696/// objc-keywordarg-list:
1697/// objc-keywordarg
1698/// objc-keywordarg-list objc-keywordarg
1699///
Mike Stump1eb44332009-09-09 15:08:12 +00001700/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001701/// selector-name[opt] ':' objc-keywordexpr
1702///
1703/// objc-keywordexpr:
1704/// nonempty-expr-list
1705///
1706/// nonempty-expr-list:
1707/// assignment-expression
1708/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001709///
1710Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001711Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001712 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001713 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001714 ExprArg ReceiverExpr) {
Steve Naroffc4df6d22009-11-07 02:08:14 +00001715 if (Tok.is(tok::code_completion)) {
1716 if (ReceiverName)
Douglas Gregord3c68542009-11-19 01:08:35 +00001717 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1718 0, 0);
Steve Naroffc4df6d22009-11-07 02:08:14 +00001719 else
Douglas Gregord3c68542009-11-19 01:08:35 +00001720 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1721 0, 0);
Steve Naroffc4df6d22009-11-07 02:08:14 +00001722 ConsumeToken();
1723 }
Douglas Gregord3c68542009-11-19 01:08:35 +00001724
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001725 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001726 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001727 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001728
Anders Carlssonff975cf2009-02-14 18:21:46 +00001729 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Steve Naroff68d331a2007-09-27 14:38:14 +00001731 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001732 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001733
Chris Lattnerdf195262007-10-09 17:51:17 +00001734 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001735 while (1) {
1736 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001737 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001738
Chris Lattnerdf195262007-10-09 17:51:17 +00001739 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001740 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001741 // We must manually skip to a ']', otherwise the expression skipper will
1742 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1743 // the enclosing expression.
1744 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001745 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001746 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001747
Steve Naroff68d331a2007-09-27 14:38:14 +00001748 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001749 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001750 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001751 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001752 // We must manually skip to a ']', otherwise the expression skipper will
1753 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1754 // the enclosing expression.
1755 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001756 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001757 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001758
Steve Naroff37387c92007-09-17 20:25:27 +00001759 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001760 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001761
Douglas Gregord3c68542009-11-19 01:08:35 +00001762 // Code completion after each argument.
1763 if (Tok.is(tok::code_completion)) {
1764 if (ReceiverName)
1765 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1766 KeyIdents.data(),
1767 KeyIdents.size());
1768 else
1769 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1770 KeyIdents.data(),
1771 KeyIdents.size());
1772 ConsumeToken();
1773 }
1774
Steve Naroff37387c92007-09-17 20:25:27 +00001775 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00001776 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001777 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001778 break;
1779 // We have a selector or a colon, continue parsing.
1780 }
1781 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001782 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001783 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00001784 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001785 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001786 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001787 // We must manually skip to a ']', otherwise the expression skipper will
1788 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1789 // the enclosing expression.
1790 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001791 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001792 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001793
Steve Naroff49f109c2007-11-15 13:05:42 +00001794 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001795 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001796 }
1797 } else if (!selIdent) {
1798 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001799
Chris Lattner4fef81d2008-08-05 06:19:09 +00001800 // We must manually skip to a ']', otherwise the expression skipper will
1801 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1802 // the enclosing expression.
1803 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001804 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001805 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001806
Chris Lattnerdf195262007-10-09 17:51:17 +00001807 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001808 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001809 // We must manually skip to a ']', otherwise the expression skipper will
1810 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1811 // the enclosing expression.
1812 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001813 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001814 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001815
Chris Lattner699b6612008-01-25 18:59:06 +00001816 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001817
Steve Naroff29238a02007-10-05 18:42:47 +00001818 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001819 if (nKeys == 0)
1820 KeyIdents.push_back(selIdent);
1821 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001822
Chris Lattnerff384912007-10-07 02:00:24 +00001823 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001824 if (ReceiverName)
1825 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump1eb44332009-09-09 15:08:12 +00001826 LBracLoc, NameLoc, SelectorLoc,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001827 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001828 KeyExprs.take(), KeyExprs.size()));
1829 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001830 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001831 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001832}
1833
Sebastian Redl1d922962008-12-13 15:32:12 +00001834Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001835 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001836 if (Res.isInvalid()) return move(Res);
1837
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001838 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1839 // expressions. At this point, we know that the only valid thing that starts
1840 // with '@' is an @"".
1841 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001842 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001843 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001844 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001845
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001846 while (Tok.is(tok::at)) {
1847 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001848
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001849 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001850 if (!isTokenStringLiteral())
1851 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001852
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001853 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001854 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001855 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001856
Sebastian Redleffa8d12008-12-10 00:02:53 +00001857 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001858 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001859
1860 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1861 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001862}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001863
1864/// objc-encode-expression:
1865/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001866Parser::OwningExprResult
1867Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001868 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001869
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001870 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001871
Chris Lattner4fef81d2008-08-05 06:19:09 +00001872 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001873 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1874
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001875 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001876
Douglas Gregor809070a2009-02-18 17:45:20 +00001877 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001878
Anders Carlsson4988ae32007-08-23 15:31:37 +00001879 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001880
Douglas Gregor809070a2009-02-18 17:45:20 +00001881 if (Ty.isInvalid())
1882 return ExprError();
1883
Mike Stump1eb44332009-09-09 15:08:12 +00001884 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00001885 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001886}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001887
1888/// objc-protocol-expression
1889/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001890Parser::OwningExprResult
1891Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001892 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001893
Chris Lattner4fef81d2008-08-05 06:19:09 +00001894 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001895 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1896
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001897 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001898
Chris Lattner4fef81d2008-08-05 06:19:09 +00001899 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001900 return ExprError(Diag(Tok, diag::err_expected_ident));
1901
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001902 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001903 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001904
Anders Carlsson4988ae32007-08-23 15:31:37 +00001905 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001906
Sebastian Redl1d922962008-12-13 15:32:12 +00001907 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1908 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001909}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001910
1911/// objc-selector-expression
1912/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001913Parser::OwningExprResult
1914Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001915 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001916
Chris Lattner4fef81d2008-08-05 06:19:09 +00001917 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001918 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1919
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001920 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001921 SourceLocation LParenLoc = ConsumeParen();
1922 SourceLocation sLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001923 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001924 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1925 return ExprError(Diag(Tok, diag::err_expected_ident));
1926
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001927 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001928 unsigned nColons = 0;
1929 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001930 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001931 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001932 return ExprError(Diag(Tok, diag::err_expected_colon));
1933
Chris Lattnercb53b362007-12-27 19:57:00 +00001934 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001935 ConsumeToken(); // Eat the ':'.
1936 if (Tok.is(tok::r_paren))
1937 break;
1938 // Check for another keyword selector.
1939 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001940 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001941 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001942 if (!SelIdent && Tok.isNot(tok::colon))
1943 break;
1944 }
Steve Naroff887407e2007-12-05 22:21:29 +00001945 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001946 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001947 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001948 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1949 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001950 }