blob: e837c765bff260a8e3e0a6fa48afa0f2d9c4d438 [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
Chris Lattner156b0612008-10-20 07:37:22 +0000518 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
519 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000520 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000521 } else {
522 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
523 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000524 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000525 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000526 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000527 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000528 SkipUntil(tok::r_paren);
529 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000530 }
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Chris Lattner156b0612008-10-20 07:37:22 +0000532 if (Tok.isNot(tok::comma))
533 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Chris Lattner156b0612008-10-20 07:37:22 +0000535 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000536 }
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Chris Lattner156b0612008-10-20 07:37:22 +0000538 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000539}
540
Steve Naroff3536b442007-09-06 21:24:23 +0000541/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000542/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000543/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000544///
545/// objc-instance-method: '-'
546/// objc-class-method: '+'
547///
Steve Naroff4985ace2007-08-22 18:35:33 +0000548/// objc-method-attributes: [OBJC2]
549/// __attribute__((deprecated))
550///
Mike Stump1eb44332009-09-09 15:08:12 +0000551Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000552 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000553 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000554
Mike Stump1eb44332009-09-09 15:08:12 +0000555 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000556 SourceLocation mLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattnerb28317a2009-03-28 19:18:32 +0000558 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000559 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000560 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000561 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000562}
563
564/// objc-selector:
565/// identifier
566/// one of
567/// enum struct union if else while do for switch case default
568/// break continue return goto asm sizeof typeof __alignof
569/// unsigned long const short volatile signed restrict _Complex
570/// in out inout bycopy byref oneway int char float double void _Bool
571///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000572IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000573 switch (Tok.getKind()) {
574 default:
575 return 0;
576 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000577 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000578 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000579 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000580 case tok::kw_break:
581 case tok::kw_case:
582 case tok::kw_catch:
583 case tok::kw_char:
584 case tok::kw_class:
585 case tok::kw_const:
586 case tok::kw_const_cast:
587 case tok::kw_continue:
588 case tok::kw_default:
589 case tok::kw_delete:
590 case tok::kw_do:
591 case tok::kw_double:
592 case tok::kw_dynamic_cast:
593 case tok::kw_else:
594 case tok::kw_enum:
595 case tok::kw_explicit:
596 case tok::kw_export:
597 case tok::kw_extern:
598 case tok::kw_false:
599 case tok::kw_float:
600 case tok::kw_for:
601 case tok::kw_friend:
602 case tok::kw_goto:
603 case tok::kw_if:
604 case tok::kw_inline:
605 case tok::kw_int:
606 case tok::kw_long:
607 case tok::kw_mutable:
608 case tok::kw_namespace:
609 case tok::kw_new:
610 case tok::kw_operator:
611 case tok::kw_private:
612 case tok::kw_protected:
613 case tok::kw_public:
614 case tok::kw_register:
615 case tok::kw_reinterpret_cast:
616 case tok::kw_restrict:
617 case tok::kw_return:
618 case tok::kw_short:
619 case tok::kw_signed:
620 case tok::kw_sizeof:
621 case tok::kw_static:
622 case tok::kw_static_cast:
623 case tok::kw_struct:
624 case tok::kw_switch:
625 case tok::kw_template:
626 case tok::kw_this:
627 case tok::kw_throw:
628 case tok::kw_true:
629 case tok::kw_try:
630 case tok::kw_typedef:
631 case tok::kw_typeid:
632 case tok::kw_typename:
633 case tok::kw_typeof:
634 case tok::kw_union:
635 case tok::kw_unsigned:
636 case tok::kw_using:
637 case tok::kw_virtual:
638 case tok::kw_void:
639 case tok::kw_volatile:
640 case tok::kw_wchar_t:
641 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000642 case tok::kw__Bool:
643 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000644 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000645 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000646 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000647 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000648 }
Steve Naroff294494e2007-08-22 16:35:03 +0000649}
650
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000651/// objc-for-collection-in: 'in'
652///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000653bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000654 // FIXME: May have to do additional look-ahead to only allow for
655 // valid tokens following an 'in'; such as an identifier, unary operators,
656 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000657 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000658 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000659}
660
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000661/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000662/// qualifier list and builds their bitmask representation in the input
663/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000664///
665/// objc-type-qualifiers:
666/// objc-type-qualifier
667/// objc-type-qualifiers objc-type-qualifier
668///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000669void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000670 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000671 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000672 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Chris Lattnere8b724d2007-12-12 06:56:32 +0000674 const IdentifierInfo *II = Tok.getIdentifierInfo();
675 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000676 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000677 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000679 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000680 switch (i) {
681 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000682 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
683 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
684 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
685 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
686 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
687 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000688 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000689 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000690 ConsumeToken();
691 II = 0;
692 break;
693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Chris Lattnere8b724d2007-12-12 06:56:32 +0000695 // If this wasn't a recognized qualifier, bail out.
696 if (II) return;
697 }
698}
699
700/// objc-type-name:
701/// '(' objc-type-qualifiers[opt] type-name ')'
702/// '(' objc-type-qualifiers[opt] ')'
703///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000704Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000705 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Chris Lattner4a76b292008-10-22 03:52:06 +0000707 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000708 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000710 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000711 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000712
Chris Lattner4a76b292008-10-22 03:52:06 +0000713 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000714 if (isTypeSpecifierQualifier()) {
715 TypeResult TypeSpec = ParseTypeName();
716 if (!TypeSpec.isInvalid())
717 Ty = TypeSpec.get();
718 }
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Steve Naroffd7333c22008-10-21 14:15:04 +0000720 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000721 ConsumeParen();
722 else if (Tok.getLocation() == TypeStartLoc) {
723 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000724 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000725 SkipUntil(tok::r_paren);
726 } else {
727 // Otherwise, we found *something*, but didn't get a ')' in the right
728 // place. Emit an error then return what we have as the type.
729 MatchRHSPunctuation(tok::r_paren, LParenLoc);
730 }
Steve Narofff28b2642007-09-05 23:30:30 +0000731 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000732}
733
734/// objc-method-decl:
735/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000736/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000737/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000738/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000739///
740/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000741/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000742/// objc-keyword-selector objc-keyword-decl
743///
744/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000745/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
746/// objc-selector ':' objc-keyword-attributes[opt] identifier
747/// ':' objc-type-name objc-keyword-attributes[opt] identifier
748/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000749///
Steve Naroff4985ace2007-08-22 18:35:33 +0000750/// objc-parmlist:
751/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000752///
Steve Naroff4985ace2007-08-22 18:35:33 +0000753/// objc-parms:
754/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000755///
Steve Naroff4985ace2007-08-22 18:35:33 +0000756/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000757/// , ...
758///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000759/// objc-keyword-attributes: [OBJC2]
760/// __attribute__((unused))
761///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000762Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
763 tok::TokenKind mType,
764 DeclPtrTy IDecl,
765 tok::ObjCKeywordKind MethodImplKind) {
John McCall54abf7d2009-11-04 02:18:39 +0000766 ParsingDeclRAIIObject PD(*this);
767
Chris Lattnere8904e92008-08-23 01:48:03 +0000768 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000769 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000770 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000771 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000772 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Steve Naroffbef11852007-10-26 20:53:56 +0000774 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000775 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000776
Steve Naroff84c43102009-02-11 20:43:13 +0000777 // An unnamed colon is valid.
778 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000779 Diag(Tok, diag::err_expected_selector_for_method)
780 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000781 // Skip until we get a ; or {}.
782 SkipUntil(tok::r_brace);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000783 return DeclPtrTy();
Chris Lattnere8904e92008-08-23 01:48:03 +0000784 }
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000786 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000787 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000788 // If attributes exist after the method, parse them.
789 AttributeList *MethodAttrs = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000790 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000791 MethodAttrs = ParseGNUAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Chris Lattnerff384912007-10-07 02:00:24 +0000793 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall54abf7d2009-11-04 02:18:39 +0000794 DeclPtrTy Result
795 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000796 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000797 0, CargNames, MethodAttrs,
798 MethodImplKind);
John McCall54abf7d2009-11-04 02:18:39 +0000799 PD.complete(Result);
800 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000801 }
Steve Narofff28b2642007-09-05 23:30:30 +0000802
Steve Naroff68d331a2007-09-27 14:38:14 +0000803 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnere294d3f2009-04-11 18:57:04 +0000804 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Chris Lattnerff384912007-10-07 02:00:24 +0000806 while (1) {
Chris Lattnere294d3f2009-04-11 18:57:04 +0000807 Action::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Chris Lattnerff384912007-10-07 02:00:24 +0000809 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000810 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000811 Diag(Tok, diag::err_expected_colon);
812 break;
813 }
814 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Chris Lattnere294d3f2009-04-11 18:57:04 +0000816 ArgInfo.Type = 0;
817 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
818 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
819
Chris Lattnerff384912007-10-07 02:00:24 +0000820 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000821 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000822 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000823 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000824
Chris Lattnerdf195262007-10-09 17:51:17 +0000825 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000826 Diag(Tok, diag::err_expected_ident); // missing argument name.
827 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Chris Lattnere294d3f2009-04-11 18:57:04 +0000830 ArgInfo.Name = Tok.getIdentifierInfo();
831 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000832 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Chris Lattnere294d3f2009-04-11 18:57:04 +0000834 ArgInfos.push_back(ArgInfo);
835 KeyIdents.push_back(SelIdent);
836
Chris Lattnerff384912007-10-07 02:00:24 +0000837 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000838 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000839 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000840 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000841 break;
842 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000843 }
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Steve Naroff335eafa2007-11-15 12:35:21 +0000845 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Chris Lattnerff384912007-10-07 02:00:24 +0000847 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000848 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000849 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000850 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000851 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000852 ConsumeToken();
853 break;
854 }
Chris Lattnerff384912007-10-07 02:00:24 +0000855 DeclSpec DS;
856 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000857 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000858 Declarator ParmDecl(DS, Declarator::PrototypeContext);
859 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000860 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000861 }
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Chris Lattnerff384912007-10-07 02:00:24 +0000863 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000864 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000865 AttributeList *MethodAttrs = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000866 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000867 MethodAttrs = ParseGNUAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000869 if (KeyIdents.size() == 0)
870 return DeclPtrTy();
Chris Lattnerff384912007-10-07 02:00:24 +0000871 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
872 &KeyIdents[0]);
John McCall54abf7d2009-11-04 02:18:39 +0000873 DeclPtrTy Result
874 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000875 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000876 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000877 MethodImplKind, isVariadic);
John McCall54abf7d2009-11-04 02:18:39 +0000878 PD.complete(Result);
879 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +0000880}
881
Steve Naroffdac269b2007-08-20 21:31:48 +0000882/// objc-protocol-refs:
883/// '<' identifier-list '>'
884///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000885bool Parser::
Chris Lattnerb28317a2009-03-28 19:18:32 +0000886ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000887 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
888 bool WarnOnDeclarations,
889 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000890 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000892 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Chris Lattnere13b9592008-07-26 04:03:38 +0000894 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Chris Lattnere13b9592008-07-26 04:03:38 +0000896 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +0000897 if (Tok.is(tok::code_completion)) {
898 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
899 ProtocolIdents.size());
900 ConsumeToken();
901 }
902
Chris Lattnere13b9592008-07-26 04:03:38 +0000903 if (Tok.isNot(tok::identifier)) {
904 Diag(Tok, diag::err_expected_ident);
905 SkipUntil(tok::greater);
906 return true;
907 }
908 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
909 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000910 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +0000911 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Chris Lattnere13b9592008-07-26 04:03:38 +0000913 if (Tok.isNot(tok::comma))
914 break;
915 ConsumeToken();
916 }
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Chris Lattnere13b9592008-07-26 04:03:38 +0000918 // Consume the '>'.
919 if (Tok.isNot(tok::greater)) {
920 Diag(Tok, diag::err_expected_greater);
921 return true;
922 }
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Chris Lattnere13b9592008-07-26 04:03:38 +0000924 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Chris Lattnere13b9592008-07-26 04:03:38 +0000926 // Convert the list of protocols identifiers into a list of protocol decls.
927 Actions.FindProtocolDeclaration(WarnOnDeclarations,
928 &ProtocolIdents[0], ProtocolIdents.size(),
929 Protocols);
930 return false;
931}
932
Steve Naroffdac269b2007-08-20 21:31:48 +0000933/// objc-class-instance-variables:
934/// '{' objc-instance-variable-decl-list[opt] '}'
935///
936/// objc-instance-variable-decl-list:
937/// objc-visibility-spec
938/// objc-instance-variable-decl ';'
939/// ';'
940/// objc-instance-variable-decl-list objc-visibility-spec
941/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
942/// objc-instance-variable-decl-list ';'
943///
944/// objc-visibility-spec:
945/// @private
946/// @protected
947/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000948/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000949///
950/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +0000951/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +0000952///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000953void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff60fccee2007-10-29 21:38:07 +0000954 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000955 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +0000956 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000957
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000958 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000959
Steve Naroffddbff782007-08-21 21:17:12 +0000960 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000962 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000963 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000964 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000965 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Steve Naroffddbff782007-08-21 21:17:12 +0000967 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000968 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +0000969 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner29d9c1a2009-12-06 17:36:05 +0000970 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +0000971 ConsumeToken();
972 continue;
973 }
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Steve Naroffddbff782007-08-21 21:17:12 +0000975 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000976 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000977 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +0000978
979 if (Tok.is(tok::code_completion)) {
980 Actions.CodeCompleteObjCAtVisibility(CurScope);
981 ConsumeToken();
982 }
983
Steve Naroff861cf3e2007-08-23 18:16:40 +0000984 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000985 case tok::objc_private:
986 case tok::objc_public:
987 case tok::objc_protected:
988 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000989 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000990 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000991 continue;
Steve Naroffddbff782007-08-21 21:17:12 +0000992 default:
993 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000994 continue;
995 }
996 }
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Douglas Gregorc38c3e12010-01-13 21:54:15 +0000998 if (Tok.is(tok::code_completion)) {
999 Actions.CodeCompleteOrdinaryName(CurScope,
1000 Action::CCC_ObjCInstanceVariableList);
1001 ConsumeToken();
1002 }
1003
John McCallbdd563e2009-11-03 02:38:08 +00001004 struct ObjCIvarCallback : FieldCallback {
1005 Parser &P;
1006 DeclPtrTy IDecl;
1007 tok::ObjCKeywordKind visibility;
1008 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
1009
1010 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
1011 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
1012 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1013 }
1014
1015 DeclPtrTy invoke(FieldDeclarator &FD) {
1016 // Install the declarator into the interface decl.
1017 DeclPtrTy Field
1018 = P.Actions.ActOnIvar(P.CurScope,
1019 FD.D.getDeclSpec().getSourceRange().getBegin(),
1020 IDecl, FD.D, FD.BitfieldSize, visibility);
1021 AllIvarDecls.push_back(Field);
1022 return Field;
1023 }
1024 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1025
Chris Lattnere1359422008-04-10 06:46:29 +00001026 // Parse all the comma separated declarators.
1027 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +00001028 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Chris Lattnerdf195262007-10-09 17:51:17 +00001030 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001031 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001032 } else {
1033 Diag(Tok, diag::err_expected_semi_decl_list);
1034 // Skip to end of block or statement
1035 SkipUntil(tok::r_brace, true, true);
1036 }
1037 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001038 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +00001039 // Call ActOnFields() even if we don't have any decls. This is useful
1040 // for code rewriting tools that need to be aware of the empty list.
1041 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001042 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001043 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001044 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001045}
Steve Naroffdac269b2007-08-20 21:31:48 +00001046
1047/// objc-protocol-declaration:
1048/// objc-protocol-definition
1049/// objc-protocol-forward-reference
1050///
1051/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001052/// @protocol identifier
1053/// objc-protocol-refs[opt]
1054/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001055/// @end
1056///
1057/// objc-protocol-forward-reference:
1058/// @protocol identifier-list ';'
1059///
1060/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001061/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001062/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001063Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1064 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001065 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001066 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1067 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Douglas Gregor083128f2009-11-18 04:49:41 +00001069 if (Tok.is(tok::code_completion)) {
1070 Actions.CodeCompleteObjCProtocolDecl(CurScope);
1071 ConsumeToken();
1072 }
1073
Chris Lattnerdf195262007-10-09 17:51:17 +00001074 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001075 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001076 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001077 }
1078 // Save the protocol name, then consume it.
1079 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1080 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Chris Lattnerdf195262007-10-09 17:51:17 +00001082 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001083 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001084 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001085 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001086 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001087 }
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Chris Lattnerdf195262007-10-09 17:51:17 +00001089 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001090 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1091 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1092
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001093 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001094 while (1) {
1095 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001096 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001097 Diag(Tok, diag::err_expected_ident);
1098 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001099 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001100 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001101 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1102 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001103 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Chris Lattnerdf195262007-10-09 17:51:17 +00001105 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001106 break;
1107 }
1108 // Consume the ';'.
1109 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001110 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Steve Naroffe440eb82007-10-10 17:32:04 +00001112 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001113 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001114 ProtocolRefs.size(),
1115 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001116 }
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001118 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001119 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001120
Chris Lattnerb28317a2009-03-28 19:18:32 +00001121 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001122 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001123 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001124 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1125 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001126 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Chris Lattnerb28317a2009-03-28 19:18:32 +00001128 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001129 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001130 ProtocolRefs.data(),
1131 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001132 ProtocolLocs.data(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001133 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001134 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001135 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001136}
Steve Naroffdac269b2007-08-20 21:31:48 +00001137
1138/// objc-implementation:
1139/// objc-class-implementation-prologue
1140/// objc-category-implementation-prologue
1141///
1142/// objc-class-implementation-prologue:
1143/// @implementation identifier objc-superclass[opt]
1144/// objc-class-instance-variables[opt]
1145///
1146/// objc-category-implementation-prologue:
1147/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001148Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001149 SourceLocation atLoc) {
1150 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1151 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1152 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001154 // Code completion after '@implementation'.
1155 if (Tok.is(tok::code_completion)) {
1156 Actions.CodeCompleteObjCImplementationDecl(CurScope);
1157 ConsumeToken();
1158 }
1159
Chris Lattnerdf195262007-10-09 17:51:17 +00001160 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001161 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001162 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001163 }
1164 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001165 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001166 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001167
1168 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001169 // we have a category implementation.
1170 SourceLocation lparenLoc = ConsumeParen();
1171 SourceLocation categoryLoc, rparenLoc;
1172 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001174 if (Tok.is(tok::code_completion)) {
1175 Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
1176 ConsumeToken();
1177 }
1178
Chris Lattnerdf195262007-10-09 17:51:17 +00001179 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001180 categoryId = Tok.getIdentifierInfo();
1181 categoryLoc = ConsumeToken();
1182 } else {
1183 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001184 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001185 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001186 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001187 Diag(Tok, diag::err_expected_rparen);
1188 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001189 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001190 }
1191 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001192 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001193 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001194 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001195 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001196 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001197 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001198 }
1199 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001200 SourceLocation superClassLoc;
1201 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001202 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001203 // We have a super class
1204 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001205 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001206 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001207 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001208 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001209 superClassId = Tok.getIdentifierInfo();
1210 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001211 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001212 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001213 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001214 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Steve Naroff60fccee2007-10-29 21:38:07 +00001216 if (Tok.is(tok::l_brace)) // we have ivars
1217 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001218 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001219 PendingObjCImpDecl.push_back(ObjCImpDecl);
1220
Chris Lattnerb28317a2009-03-28 19:18:32 +00001221 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001222}
Steve Naroff60fccee2007-10-29 21:38:07 +00001223
Ted Kremenek782f2f52010-01-07 01:20:12 +00001224Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001225 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1226 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001227 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001228 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001229 if (ObjCImpDecl) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001230 Actions.ActOnAtEnd(atEnd, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001231 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001232 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001233 }
Ted Kremenek782f2f52010-01-07 01:20:12 +00001234 else {
1235 // missing @implementation
1236 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1237 }
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001238 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001239}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001240
Fariborz Jahanian1ac71042009-11-17 17:15:16 +00001241Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001242 if (PendingObjCImpDecl.empty())
1243 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1244 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
Ted Kremenek782f2f52010-01-07 01:20:12 +00001245 Actions.ActOnAtEnd(SourceRange(), ImpDecl);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001246 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1247}
1248
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001249/// compatibility-alias-decl:
1250/// @compatibility_alias alias-name class-name ';'
1251///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001252Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001253 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1254 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1255 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001256 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001257 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001258 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001259 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001260 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1261 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001262 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001263 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001264 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001265 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001266 IdentifierInfo *classId = Tok.getIdentifierInfo();
1267 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1268 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001269 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001270 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001271 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001272 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1273 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001274}
1275
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001276/// property-synthesis:
1277/// @synthesize property-ivar-list ';'
1278///
1279/// property-ivar-list:
1280/// property-ivar
1281/// property-ivar-list ',' property-ivar
1282///
1283/// property-ivar:
1284/// identifier
1285/// identifier '=' identifier
1286///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001287Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001288 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1289 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001290 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Douglas Gregorb328c422009-11-18 19:45:45 +00001292 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001293 if (Tok.is(tok::code_completion)) {
Douglas Gregor424b2a52009-11-18 22:56:13 +00001294 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
Douglas Gregor322328b2009-11-18 22:32:06 +00001295 ConsumeToken();
1296 }
1297
Douglas Gregorb328c422009-11-18 19:45:45 +00001298 if (Tok.isNot(tok::identifier)) {
1299 Diag(Tok, diag::err_synthesized_property_name);
1300 SkipUntil(tok::semi);
1301 return DeclPtrTy();
1302 }
1303
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001304 IdentifierInfo *propertyIvar = 0;
1305 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1306 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001307 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001308 // property '=' ivar-name
1309 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001310
1311 if (Tok.is(tok::code_completion)) {
1312 Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1313 ObjCImpDecl);
1314 ConsumeToken();
1315 }
1316
Chris Lattnerdf195262007-10-09 17:51:17 +00001317 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001318 Diag(Tok, diag::err_expected_ident);
1319 break;
1320 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001321 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001322 ConsumeToken(); // consume ivar-name
1323 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001324 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1325 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001326 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001327 break;
1328 ConsumeToken(); // consume ','
1329 }
Douglas Gregorb328c422009-11-18 19:45:45 +00001330 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001331 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregorb328c422009-11-18 19:45:45 +00001332 SkipUntil(tok::semi);
1333 }
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001334 else
1335 ConsumeToken(); // consume ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001336 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001337}
1338
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001339/// property-dynamic:
1340/// @dynamic property-list
1341///
1342/// property-list:
1343/// identifier
1344/// property-list ',' identifier
1345///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001346Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001347 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1348 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1349 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001350 while (true) {
1351 if (Tok.is(tok::code_completion)) {
1352 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1353 ConsumeToken();
1354 }
1355
1356 if (Tok.isNot(tok::identifier)) {
1357 Diag(Tok, diag::err_expected_ident);
1358 SkipUntil(tok::semi);
1359 return DeclPtrTy();
1360 }
1361
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001362 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1363 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1364 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1365 propertyId, 0);
1366
Chris Lattnerdf195262007-10-09 17:51:17 +00001367 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001368 break;
1369 ConsumeToken(); // consume ','
1370 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001371 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001372 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001373 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001374}
Mike Stump1eb44332009-09-09 15:08:12 +00001375
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001376/// objc-throw-statement:
1377/// throw expression[opt];
1378///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001379Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001380 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001381 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001382 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001383 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001384 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001385 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001386 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001387 }
1388 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001389 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001390 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001391}
1392
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001393/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001394/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001395///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001396Parser::OwningStmtResult
1397Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001398 ConsumeToken(); // consume synchronized
1399 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001400 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001401 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001402 }
1403 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001404 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001405 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001406 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001407 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001408 }
1409 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001410 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001411 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001412 }
1413 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001414 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001415 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001416 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001417 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001418 // Enter a scope to hold everything within the compound stmt. Compound
1419 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001420 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001421
Sebastian Redl61364dd2008-12-11 19:30:53 +00001422 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001423
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001424 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001425 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001426 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001427 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001428}
1429
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001430/// objc-try-catch-statement:
1431/// @try compound-statement objc-catch-list[opt]
1432/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1433///
1434/// objc-catch-list:
1435/// @catch ( parameter-declaration ) compound-statement
1436/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1437/// catch-parameter-declaration:
1438/// parameter-declaration
1439/// '...' [OBJC2]
1440///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001441Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001442 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001443
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001444 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001445 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001446 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001447 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001448 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001449 OwningStmtResult CatchStmts(Actions);
1450 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001451 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001452 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001453 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001454 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001455 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001456
Chris Lattnerdf195262007-10-09 17:51:17 +00001457 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001458 // At this point, we need to lookahead to determine if this @ is the start
1459 // of an @catch or @finally. We don't want to consume the @ token if this
1460 // is an @try or @encode or something else.
1461 Token AfterAt = GetLookAheadToken(1);
1462 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1463 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1464 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001465
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001466 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001467 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001468 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001469 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001470 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001471 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001472 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001473 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001474 DeclSpec DS;
1475 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001476 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001477 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001478 // we must accept either 'declarator' or 'abstract-declarator' here.
1479 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1480 ParseDeclarator(ParmDecl);
1481
1482 // Inform the actions module about the parameter declarator, so it
1483 // gets added to the current scope.
1484 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Fariborz Jahanian2f764f12010-02-03 00:01:43 +00001485 Actions.ActOnObjCCatchParam(FirstPart);
Steve Naroff64515f32008-02-05 21:27:35 +00001486 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001487 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Steve Naroff93a25952009-04-07 22:56:58 +00001489 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Steve Naroff93a25952009-04-07 22:56:58 +00001491 if (Tok.is(tok::r_paren))
1492 RParenLoc = ConsumeParen();
1493 else // Skip over garbage, until we get to ')'. Eat the ')'.
1494 SkipUntil(tok::r_paren, true, false);
1495
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001496 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001497 if (Tok.is(tok::l_brace))
1498 CatchBody = ParseCompoundStatementBody();
1499 else
1500 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001501 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001502 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001503 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001504 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001505 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001506 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001507 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1508 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001509 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001510 }
1511 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001512 } else {
1513 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001514 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001515 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001516
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001517 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001518 if (Tok.is(tok::l_brace))
1519 FinallyBody = ParseCompoundStatementBody();
1520 else
1521 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001522 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001523 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001524 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001525 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001526 catch_or_finally_seen = true;
1527 break;
1528 }
1529 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001530 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001531 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001532 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001533 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001534 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1535 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001536}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001537
Steve Naroff3536b442007-09-06 21:24:23 +00001538/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001539///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001540Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1541 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001542
Chris Lattner49f28ca2009-03-05 08:00:35 +00001543 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1544 PP.getSourceManager(),
1545 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001547 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001548 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001549 if (ObjCImpDecl) {
1550 Diag(Tok, diag::warn_semicolon_before_method_body)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001551 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001552 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001553 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001554 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001555
Steve Naroff409be832007-11-11 19:54:21 +00001556 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001557 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001558 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Steve Naroff409be832007-11-11 19:54:21 +00001560 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1561 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Steve Naroff409be832007-11-11 19:54:21 +00001563 // If we didn't find the '{', bail out.
1564 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001565 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001566 }
Steve Naroff409be832007-11-11 19:54:21 +00001567 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Steve Naroff409be832007-11-11 19:54:21 +00001569 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001570 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Steve Naroff409be832007-11-11 19:54:21 +00001572 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001573 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001574 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001575
1576 OwningStmtResult FnBody(ParseCompoundStatementBody());
1577
Steve Naroff409be832007-11-11 19:54:21 +00001578 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001579 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001580 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1581 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001582
Steve Naroff32ce8372009-03-02 22:00:56 +00001583 // TODO: Pass argument information.
1584 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Steve Naroff409be832007-11-11 19:54:21 +00001586 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001587 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001588
Steve Naroff71c0a952007-11-13 23:01:27 +00001589 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001590}
Anders Carlsson55085182007-08-21 17:43:55 +00001591
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001592Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001593 if (Tok.is(tok::code_completion)) {
1594 Actions.CodeCompleteObjCAtStatement(CurScope);
1595 ConsumeToken();
1596 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001597 }
1598
1599 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001600 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001601
1602 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001603 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001604
1605 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001606 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001607
Sebastian Redld8c4e152008-12-11 22:33:27 +00001608 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001609 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001610 // If the expression is invalid, skip ahead to the next semicolon. Not
1611 // doing this opens us up to the possibility of infinite loops if
1612 // ParseExpression does not consume any tokens.
1613 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001614 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001615 }
Chris Lattner5d803162009-12-07 16:33:19 +00001616
Steve Naroff64515f32008-02-05 21:27:35 +00001617 // Otherwise, eat the semicolon.
1618 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson5ee56e92009-12-16 02:09:40 +00001619 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001620}
1621
Sebastian Redl1d922962008-12-13 15:32:12 +00001622Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001623 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001624 case tok::code_completion:
1625 Actions.CodeCompleteObjCAtExpression(CurScope);
1626 ConsumeToken();
1627 return ExprError();
1628
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001629 case tok::string_literal: // primary-expression: string-literal
1630 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001631 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001632 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001633 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001634 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001635
Chris Lattner4fef81d2008-08-05 06:19:09 +00001636 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1637 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001638 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001639 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001640 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001641 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001642 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001643 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001644 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001645 }
Anders Carlsson55085182007-08-21 17:43:55 +00001646 }
Anders Carlsson55085182007-08-21 17:43:55 +00001647}
1648
Mike Stump1eb44332009-09-09 15:08:12 +00001649/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001650/// '[' objc-receiver objc-message-args ']'
1651///
1652/// objc-receiver:
1653/// expression
1654/// class-name
1655/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001656Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001657 assert(Tok.is(tok::l_square) && "'[' expected");
1658 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1659
1660 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001661 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001662 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001663 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1664 SourceLocation NameLoc = ConsumeToken();
1665 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1666 ExprArg(Actions));
1667 }
Chris Lattner699b6612008-01-25 18:59:06 +00001668 }
1669
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001670 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001671 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001672 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001673 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001674 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001675
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001676 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001677 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001678}
Sebastian Redl1d922962008-12-13 15:32:12 +00001679
Chris Lattner699b6612008-01-25 18:59:06 +00001680/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1681/// the rest of a message expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001682///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001683/// objc-message-args:
1684/// objc-selector
1685/// objc-keywordarg-list
1686///
1687/// objc-keywordarg-list:
1688/// objc-keywordarg
1689/// objc-keywordarg-list objc-keywordarg
1690///
Mike Stump1eb44332009-09-09 15:08:12 +00001691/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001692/// selector-name[opt] ':' objc-keywordexpr
1693///
1694/// objc-keywordexpr:
1695/// nonempty-expr-list
1696///
1697/// nonempty-expr-list:
1698/// assignment-expression
1699/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001700///
1701Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001702Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001703 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001704 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001705 ExprArg ReceiverExpr) {
Steve Naroffc4df6d22009-11-07 02:08:14 +00001706 if (Tok.is(tok::code_completion)) {
1707 if (ReceiverName)
Douglas Gregord3c68542009-11-19 01:08:35 +00001708 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1709 0, 0);
Steve Naroffc4df6d22009-11-07 02:08:14 +00001710 else
Douglas Gregord3c68542009-11-19 01:08:35 +00001711 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1712 0, 0);
Steve Naroffc4df6d22009-11-07 02:08:14 +00001713 ConsumeToken();
1714 }
Douglas Gregord3c68542009-11-19 01:08:35 +00001715
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001716 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001717 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001718 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001719
Anders Carlssonff975cf2009-02-14 18:21:46 +00001720 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00001721
Steve Naroff68d331a2007-09-27 14:38:14 +00001722 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001723 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001724
Chris Lattnerdf195262007-10-09 17:51:17 +00001725 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001726 while (1) {
1727 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001728 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001729
Chris Lattnerdf195262007-10-09 17:51:17 +00001730 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001731 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001732 // We must manually skip to a ']', otherwise the expression skipper will
1733 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1734 // the enclosing expression.
1735 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001736 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001737 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001738
Steve Naroff68d331a2007-09-27 14:38:14 +00001739 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001740 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001741 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001742 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001743 // We must manually skip to a ']', otherwise the expression skipper will
1744 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1745 // the enclosing expression.
1746 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001747 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001748 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001749
Steve Naroff37387c92007-09-17 20:25:27 +00001750 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001751 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001752
Douglas Gregord3c68542009-11-19 01:08:35 +00001753 // Code completion after each argument.
1754 if (Tok.is(tok::code_completion)) {
1755 if (ReceiverName)
1756 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1757 KeyIdents.data(),
1758 KeyIdents.size());
1759 else
1760 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1761 KeyIdents.data(),
1762 KeyIdents.size());
1763 ConsumeToken();
1764 }
1765
Steve Naroff37387c92007-09-17 20:25:27 +00001766 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00001767 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001768 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001769 break;
1770 // We have a selector or a colon, continue parsing.
1771 }
1772 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001773 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001774 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00001775 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001776 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001777 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001778 // We must manually skip to a ']', otherwise the expression skipper will
1779 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1780 // the enclosing expression.
1781 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001782 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001783 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001784
Steve Naroff49f109c2007-11-15 13:05:42 +00001785 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001786 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001787 }
1788 } else if (!selIdent) {
1789 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001790
Chris Lattner4fef81d2008-08-05 06:19:09 +00001791 // We must manually skip to a ']', otherwise the expression skipper will
1792 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1793 // the enclosing expression.
1794 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001795 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001796 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001797
Chris Lattnerdf195262007-10-09 17:51:17 +00001798 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001799 Diag(Tok, diag::err_expected_rsquare);
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 Lattner699b6612008-01-25 18:59:06 +00001807 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001808
Steve Naroff29238a02007-10-05 18:42:47 +00001809 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001810 if (nKeys == 0)
1811 KeyIdents.push_back(selIdent);
1812 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001813
Chris Lattnerff384912007-10-07 02:00:24 +00001814 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001815 if (ReceiverName)
1816 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump1eb44332009-09-09 15:08:12 +00001817 LBracLoc, NameLoc, SelectorLoc,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001818 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001819 KeyExprs.take(), KeyExprs.size()));
1820 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001821 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001822 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001823}
1824
Sebastian Redl1d922962008-12-13 15:32:12 +00001825Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001826 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001827 if (Res.isInvalid()) return move(Res);
1828
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001829 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1830 // expressions. At this point, we know that the only valid thing that starts
1831 // with '@' is an @"".
1832 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001833 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001834 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001835 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001836
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001837 while (Tok.is(tok::at)) {
1838 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001839
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001840 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001841 if (!isTokenStringLiteral())
1842 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001843
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001844 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001845 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001846 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001847
Sebastian Redleffa8d12008-12-10 00:02:53 +00001848 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001849 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001850
1851 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1852 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001853}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001854
1855/// objc-encode-expression:
1856/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001857Parser::OwningExprResult
1858Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001859 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001860
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001861 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001862
Chris Lattner4fef81d2008-08-05 06:19:09 +00001863 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001864 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1865
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001866 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001867
Douglas Gregor809070a2009-02-18 17:45:20 +00001868 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001869
Anders Carlsson4988ae32007-08-23 15:31:37 +00001870 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001871
Douglas Gregor809070a2009-02-18 17:45:20 +00001872 if (Ty.isInvalid())
1873 return ExprError();
1874
Mike Stump1eb44332009-09-09 15:08:12 +00001875 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00001876 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001877}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001878
1879/// objc-protocol-expression
1880/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001881Parser::OwningExprResult
1882Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001883 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001884
Chris Lattner4fef81d2008-08-05 06:19:09 +00001885 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001886 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1887
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001888 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001889
Chris Lattner4fef81d2008-08-05 06:19:09 +00001890 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001891 return ExprError(Diag(Tok, diag::err_expected_ident));
1892
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001893 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001894 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001895
Anders Carlsson4988ae32007-08-23 15:31:37 +00001896 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001897
Sebastian Redl1d922962008-12-13 15:32:12 +00001898 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1899 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001900}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001901
1902/// objc-selector-expression
1903/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001904Parser::OwningExprResult
1905Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001906 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001907
Chris Lattner4fef81d2008-08-05 06:19:09 +00001908 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001909 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1910
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001911 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001912 SourceLocation LParenLoc = ConsumeParen();
1913 SourceLocation sLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001914 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001915 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1916 return ExprError(Diag(Tok, diag::err_expected_ident));
1917
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001918 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001919 unsigned nColons = 0;
1920 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001921 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001922 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001923 return ExprError(Diag(Tok, diag::err_expected_colon));
1924
Chris Lattnercb53b362007-12-27 19:57:00 +00001925 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001926 ConsumeToken(); // Eat the ':'.
1927 if (Tok.is(tok::r_paren))
1928 break;
1929 // Check for another keyword selector.
1930 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001931 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001932 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001933 if (!SelIdent && Tok.isNot(tok::colon))
1934 break;
1935 }
Steve Naroff887407e2007-12-05 22:21:29 +00001936 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001937 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001938 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001939 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1940 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001941 }