blob: 5e2363535320f8fd5354d98a642a43f7b0888e21 [file] [log] [blame]
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerda59c2f2006-11-05 02:08: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 Narofff1bc45b2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian9e63b982007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner60f36222009-01-29 05:15:15 +000017#include "clang/Parse/ParseDiagnostic.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000018#include "llvm/ADT/SmallVector.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000019using namespace clang;
20
21
Chris Lattner3a907162008-12-08 21:53:24 +000022/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000023/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroffe0933392007-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 Lattner83f095c2009-03-28 19:18:32 +000030Parser::DeclPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000032
Douglas Gregorf48706c2009-12-07 09:27:33 +000033 if (Tok.is(tok::code_completion)) {
34 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, false);
35 ConsumeToken();
36 }
37
Steve Naroff7c348172007-08-23 18:16:40 +000038 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-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 Lattner83f095c2009-03-28 19:18:32 +000058 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +000059 }
60}
61
62///
Mike Stump11289f42009-09-09 15:08:12 +000063/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000064/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000065///
Chris Lattner83f095c2009-03-28 19:18:32 +000066Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000067 ConsumeToken(); // the identifier "class"
Chris Lattner23b7eb62007-06-15 23:05:46 +000068 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremeneka26da852009-11-17 23:12:20 +000069 llvm::SmallVector<SourceLocation, 8> ClassLocs;
70
Mike Stump11289f42009-09-09 15:08:12 +000071
Chris Lattnerda59c2f2006-11-05 02:08:13 +000072 while (1) {
Chris Lattner0ef13522007-10-09 17:51:17 +000073 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +000074 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000075 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +000076 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +000077 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +000078 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +000079 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000080 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +000081
Chris Lattner0ef13522007-10-09 17:51:17 +000082 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +000083 break;
Mike Stump11289f42009-09-09 15:08:12 +000084
Chris Lattnerda59c2f2006-11-05 02:08:13 +000085 ConsumeToken();
86 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattnerda59c2f2006-11-05 02:08:13 +000088 // Consume the ';'.
89 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattner83f095c2009-03-28 19:18:32 +000090 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +000091
Ted Kremeneka26da852009-11-17 23:12:20 +000092 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
93 ClassLocs.data(),
94 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000095}
96
Steve Naroff1eb1ad62007-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 Stump11289f42009-09-09 15:08:12 +0000103/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000104/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000105/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000106/// objc-interface-decl-list
107/// @end
108///
109/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000110/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-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 Lattner83f095c2009-03-28 19:18:32 +0000125Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000126 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +0000127 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000128 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
129 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000130
Douglas Gregor49c22a72009-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 Lattner0ef13522007-10-09 17:51:17 +0000137 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000138 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000139 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000140 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000141
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000142 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000143 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000144 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000145
Chris Lattner0ef13522007-10-09 17:51:17 +0000146 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000147 SourceLocation lparenLoc = ConsumeParen();
148 SourceLocation categoryLoc, rparenLoc;
149 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000150
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000151 if (Tok.is(tok::code_completion)) {
152 Actions.CodeCompleteObjCInterfaceCategory(CurScope, nameId);
153 ConsumeToken();
154 }
155
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000156 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000157 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000158 categoryId = Tok.getIdentifierInfo();
159 categoryLoc = ConsumeToken();
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000160 } else if (!getLang().ObjC2) {
161 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000162 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000163 }
Chris Lattner0ef13522007-10-09 17:51:17 +0000164 if (Tok.isNot(tok::r_paren)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000165 Diag(Tok, diag::err_expected_rparen);
166 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +0000167 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000168 }
169 rparenLoc = ConsumeParen();
Mike Stump11289f42009-09-09 15:08:12 +0000170
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000171 // Next, we need to check for any protocol references.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000172 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000173 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000174 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerb1f3c942008-07-26 04:07:02 +0000175 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000176 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
177 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000178 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000179
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000180 if (attrList) // categories don't support attributes.
181 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump11289f42009-09-09 15:08:12 +0000182
Jay Foad7d0479f2009-05-21 09:52:38 +0000183 DeclPtrTy CategoryType =
Mike Stump11289f42009-09-09 15:08:12 +0000184 Actions.ActOnStartCategoryInterface(atLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000185 nameId, nameLoc,
186 categoryId, categoryLoc,
187 ProtocolRefs.data(),
188 ProtocolRefs.size(),
189 EndProtoLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000190
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000191 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000192 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000193 }
194 // Parse a class interface.
195 IdentifierInfo *superClassId = 0;
196 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000197
Chris Lattner0ef13522007-10-09 17:51:17 +0000198 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000199 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000200
201 // Code completion of superclass names.
202 if (Tok.is(tok::code_completion)) {
203 Actions.CodeCompleteObjCSuperclass(CurScope, nameId);
204 ConsumeToken();
205 }
206
Chris Lattner0ef13522007-10-09 17:51:17 +0000207 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000208 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000209 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000210 }
211 superClassId = Tok.getIdentifierInfo();
212 superClassLoc = ConsumeToken();
213 }
214 // Next, we need to check for any protocol references.
Chris Lattner83f095c2009-03-28 19:18:32 +0000215 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000216 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
217 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000218 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000219 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
220 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000221 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000222
223 DeclPtrTy ClsType =
224 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000225 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000226 ProtocolRefs.data(), ProtocolRefs.size(),
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000227 EndProtoLoc, attrList);
Mike Stump11289f42009-09-09 15:08:12 +0000228
Chris Lattner0ef13522007-10-09 17:51:17 +0000229 if (Tok.is(tok::l_brace))
Steve Naroff33a1e802007-10-29 21:38:07 +0000230 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000231
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000232 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000233 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000234}
235
John McCall064d77b2009-12-03 22:31:13 +0000236/// The Objective-C property callback. This should be defined where
237/// it's used, but instead it's been lifted to here to support VS2005.
238struct Parser::ObjCPropertyCallback : FieldCallback {
239 Parser &P;
240 DeclPtrTy IDecl;
241 llvm::SmallVectorImpl<DeclPtrTy> &Props;
242 ObjCDeclSpec &OCDS;
243 SourceLocation AtLoc;
244 tok::ObjCKeywordKind MethodImplKind;
245
246 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
247 llvm::SmallVectorImpl<DeclPtrTy> &Props,
248 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
249 tok::ObjCKeywordKind MethodImplKind) :
250 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
251 MethodImplKind(MethodImplKind) {
252 }
253
254 DeclPtrTy invoke(FieldDeclarator &FD) {
255 if (FD.D.getIdentifier() == 0) {
256 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
257 << FD.D.getSourceRange();
258 return DeclPtrTy();
259 }
260 if (FD.BitfieldSize) {
261 P.Diag(AtLoc, diag::err_objc_property_bitfield)
262 << FD.D.getSourceRange();
263 return DeclPtrTy();
264 }
265
266 // Install the property declarator into interfaceDecl.
267 IdentifierInfo *SelName =
268 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
269
270 Selector GetterSel =
271 P.PP.getSelectorTable().getNullarySelector(SelName);
272 IdentifierInfo *SetterName = OCDS.getSetterName();
273 Selector SetterSel;
274 if (SetterName)
275 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
276 else
277 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
278 P.PP.getSelectorTable(),
279 FD.D.getIdentifier());
280 bool isOverridingProperty = false;
281 DeclPtrTy Property =
282 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
283 GetterSel, SetterSel, IDecl,
284 &isOverridingProperty,
285 MethodImplKind);
286 if (!isOverridingProperty)
287 Props.push_back(Property);
288
289 return Property;
290 }
291};
292
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000293/// objc-interface-decl-list:
294/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000295/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000296/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000297/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000298/// objc-interface-decl-list declaration
299/// objc-interface-decl-list ';'
300///
Steve Naroff99264b42007-08-22 16:35:03 +0000301/// objc-method-requirement: [OBJC2]
302/// @required
303/// @optional
304///
Chris Lattner83f095c2009-03-28 19:18:32 +0000305void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattner5e530bc2007-12-27 19:57:00 +0000306 tok::ObjCKeywordKind contextKey) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000307 llvm::SmallVector<DeclPtrTy, 32> allMethods;
308 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000309 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000310 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000311
Ted Kremenekc7c64312010-01-07 01:20:12 +0000312 SourceRange AtEnd;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000313
Steve Naroff99264b42007-08-22 16:35:03 +0000314 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000315 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000316 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump11289f42009-09-09 15:08:12 +0000317 DeclPtrTy methodPrototype =
Chris Lattner0ef13522007-10-09 17:51:17 +0000318 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000319 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000320 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
321 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000322 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
323 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000324 continue;
325 }
Mike Stump11289f42009-09-09 15:08:12 +0000326
Chris Lattner038a3e32008-10-20 05:46:22 +0000327 // Ignore excess semicolons.
328 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000329 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000330 continue;
331 }
Mike Stump11289f42009-09-09 15:08:12 +0000332
Chris Lattnerda9fb152008-10-20 06:10:06 +0000333 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000334 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000335 break;
Mike Stump11289f42009-09-09 15:08:12 +0000336
Douglas Gregorf1934162010-01-13 21:24:21 +0000337 // Code completion within an Objective-C interface.
338 if (Tok.is(tok::code_completion)) {
339 Actions.CodeCompleteOrdinaryName(CurScope,
340 ObjCImpDecl? Action::CCC_ObjCImplementation
341 : Action::CCC_ObjCInterface);
342 ConsumeToken();
343 }
344
Chris Lattner038a3e32008-10-20 05:46:22 +0000345 // If we don't have an @ directive, parse it as a function definition.
346 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000347 // The code below does not consume '}'s because it is afraid of eating the
348 // end of a namespace. Because of the way this code is structured, an
349 // erroneous r_brace would cause an infinite loop if not handled here.
350 if (Tok.is(tok::r_brace))
351 break;
Mike Stump11289f42009-09-09 15:08:12 +0000352
Steve Narofff1bc45b2007-08-22 18:35:33 +0000353 // FIXME: as the name implies, this rule allows function definitions.
354 // We could pass a flag or check for functions during semantic analysis.
Alexis Hunt96d5c762009-11-21 08:43:09 +0000355 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattner038a3e32008-10-20 05:46:22 +0000356 continue;
357 }
Mike Stump11289f42009-09-09 15:08:12 +0000358
Chris Lattner038a3e32008-10-20 05:46:22 +0000359 // Otherwise, we have an @ directive, eat the @.
360 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000361 if (Tok.is(tok::code_completion)) {
362 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
363 ConsumeToken();
364 break;
365 }
366
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000367 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000368
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000369 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000370 AtEnd.setBegin(AtLoc);
371 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000372 break;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000373 }
Mike Stump11289f42009-09-09 15:08:12 +0000374
Chris Lattnerda9fb152008-10-20 06:10:06 +0000375 // Eat the identifier.
376 ConsumeToken();
377
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000378 switch (DirectiveKind) {
379 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000380 // FIXME: If someone forgets an @end on a protocol, this loop will
381 // continue to eat up tons of stuff and spew lots of nonsense errors. It
382 // would probably be better to bail out if we saw an @class or @interface
383 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000384 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000385 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000386 SkipUntil(tok::r_brace, tok::at);
387 break;
Mike Stump11289f42009-09-09 15:08:12 +0000388
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000389 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000390 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000391 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000392 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000393 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000394 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000395 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000396 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000397 break;
Mike Stump11289f42009-09-09 15:08:12 +0000398
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000399 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000400 if (!getLang().ObjC2)
401 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
402
Chris Lattner038a3e32008-10-20 05:46:22 +0000403 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000404 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000405 if (Tok.is(tok::l_paren))
Douglas Gregorc8537c52009-11-19 07:41:15 +0000406 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
407 allMethods.data(), allMethods.size());
Mike Stump11289f42009-09-09 15:08:12 +0000408
John McCall064d77b2009-12-03 22:31:13 +0000409 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
410 OCDS, AtLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000411
Chris Lattner038a3e32008-10-20 05:46:22 +0000412 // Parse all the comma separated declarators.
413 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000414 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000415
Chris Lattnere0d3fe92008-10-20 06:15:13 +0000416 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
417 tok::at);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000418 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000419 }
Steve Naroff99264b42007-08-22 16:35:03 +0000420 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000421
422 // We break out of the big loop in two cases: when we see @end or when we see
423 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000424 if (Tok.is(tok::code_completion)) {
425 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
426 ConsumeToken();
427 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerda9fb152008-10-20 06:10:06 +0000428 ConsumeToken(); // the "end" identifier
429 else
430 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000431
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000432 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000433 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenekc7c64312010-01-07 01:20:12 +0000434 Actions.ActOnAtEnd(AtEnd, interfaceDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000435 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000436 allProperties.data(), allProperties.size(),
437 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000438}
439
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000440/// Parse property attribute declarations.
441///
442/// property-attr-decl: '(' property-attrlist ')'
443/// property-attrlist:
444/// property-attribute
445/// property-attrlist ',' property-attribute
446/// property-attribute:
447/// getter '=' identifier
448/// setter '=' identifier ':'
449/// readonly
450/// readwrite
451/// assign
452/// retain
453/// copy
454/// nonatomic
455///
Douglas Gregorc8537c52009-11-19 07:41:15 +0000456void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl,
457 DeclPtrTy *Methods,
458 unsigned NumMethods) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000459 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000460 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000461
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000462 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000463 if (Tok.is(tok::code_completion)) {
Douglas Gregor36029f42009-11-18 23:08:07 +0000464 Actions.CodeCompleteObjCPropertyFlags(CurScope, DS);
Steve Naroff936354c2009-10-08 21:55:05 +0000465 ConsumeToken();
466 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000467 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000468
Chris Lattner76619232008-10-20 07:22:18 +0000469 // If this is not an identifier at all, bail out early.
470 if (II == 0) {
471 MatchRHSPunctuation(tok::r_paren, LHSLoc);
472 return;
473 }
Mike Stump11289f42009-09-09 15:08:12 +0000474
Chris Lattner1db33542008-10-20 07:37:22 +0000475 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000476
Chris Lattner68e48682008-11-20 04:42:34 +0000477 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000478 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000479 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000480 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner68e48682008-11-20 04:42:34 +0000481 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000482 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000483 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000484 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner68e48682008-11-20 04:42:34 +0000485 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000486 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000487 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000488 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner68e48682008-11-20 04:42:34 +0000489 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattner825bca12008-10-20 07:39:53 +0000490 // getter/setter require extra treatment.
Chris Lattner1db33542008-10-20 07:37:22 +0000491 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
492 tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000493 return;
Mike Stump11289f42009-09-09 15:08:12 +0000494
Douglas Gregorc8537c52009-11-19 07:41:15 +0000495 if (Tok.is(tok::code_completion)) {
496 if (II->getNameStart()[0] == 's')
497 Actions.CodeCompleteObjCPropertySetter(CurScope, ClassDecl,
498 Methods, NumMethods);
499 else
500 Actions.CodeCompleteObjCPropertyGetter(CurScope, ClassDecl,
501 Methods, NumMethods);
502 ConsumeToken();
503 }
504
Chris Lattnerbeca7702008-10-20 07:24:39 +0000505 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000506 Diag(Tok, diag::err_expected_ident);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000507 SkipUntil(tok::r_paren);
508 return;
509 }
Mike Stump11289f42009-09-09 15:08:12 +0000510
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000511 if (II->getNameStart()[0] == 's') {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000512 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
513 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000514 ConsumeToken(); // consume method name
Mike Stump11289f42009-09-09 15:08:12 +0000515
Chris Lattner1db33542008-10-20 07:37:22 +0000516 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
517 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000518 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000519 } else {
520 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
521 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000522 ConsumeToken(); // consume method name
Chris Lattnerbeca7702008-10-20 07:24:39 +0000523 }
Chris Lattner825bca12008-10-20 07:39:53 +0000524 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000525 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000526 SkipUntil(tok::r_paren);
527 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000528 }
Mike Stump11289f42009-09-09 15:08:12 +0000529
Chris Lattner1db33542008-10-20 07:37:22 +0000530 if (Tok.isNot(tok::comma))
531 break;
Mike Stump11289f42009-09-09 15:08:12 +0000532
Chris Lattner1db33542008-10-20 07:37:22 +0000533 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000534 }
Mike Stump11289f42009-09-09 15:08:12 +0000535
Chris Lattner1db33542008-10-20 07:37:22 +0000536 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000537}
538
Steve Naroff09bf8152007-09-06 21:24:23 +0000539/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000540/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000541/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000542///
543/// objc-instance-method: '-'
544/// objc-class-method: '+'
545///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000546/// objc-method-attributes: [OBJC2]
547/// __attribute__((deprecated))
548///
Mike Stump11289f42009-09-09 15:08:12 +0000549Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000550 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000551 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000552
Mike Stump11289f42009-09-09 15:08:12 +0000553 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000554 SourceLocation mLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000555
Chris Lattner83f095c2009-03-28 19:18:32 +0000556 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff09bf8152007-09-06 21:24:23 +0000557 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000558 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000559 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000560}
561
562/// objc-selector:
563/// identifier
564/// one of
565/// enum struct union if else while do for switch case default
566/// break continue return goto asm sizeof typeof __alignof
567/// unsigned long const short volatile signed restrict _Complex
568/// in out inout bycopy byref oneway int char float double void _Bool
569///
Chris Lattner4f472a32009-04-11 18:13:45 +0000570IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000571 switch (Tok.getKind()) {
572 default:
573 return 0;
574 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000575 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000576 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000577 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000578 case tok::kw_break:
579 case tok::kw_case:
580 case tok::kw_catch:
581 case tok::kw_char:
582 case tok::kw_class:
583 case tok::kw_const:
584 case tok::kw_const_cast:
585 case tok::kw_continue:
586 case tok::kw_default:
587 case tok::kw_delete:
588 case tok::kw_do:
589 case tok::kw_double:
590 case tok::kw_dynamic_cast:
591 case tok::kw_else:
592 case tok::kw_enum:
593 case tok::kw_explicit:
594 case tok::kw_export:
595 case tok::kw_extern:
596 case tok::kw_false:
597 case tok::kw_float:
598 case tok::kw_for:
599 case tok::kw_friend:
600 case tok::kw_goto:
601 case tok::kw_if:
602 case tok::kw_inline:
603 case tok::kw_int:
604 case tok::kw_long:
605 case tok::kw_mutable:
606 case tok::kw_namespace:
607 case tok::kw_new:
608 case tok::kw_operator:
609 case tok::kw_private:
610 case tok::kw_protected:
611 case tok::kw_public:
612 case tok::kw_register:
613 case tok::kw_reinterpret_cast:
614 case tok::kw_restrict:
615 case tok::kw_return:
616 case tok::kw_short:
617 case tok::kw_signed:
618 case tok::kw_sizeof:
619 case tok::kw_static:
620 case tok::kw_static_cast:
621 case tok::kw_struct:
622 case tok::kw_switch:
623 case tok::kw_template:
624 case tok::kw_this:
625 case tok::kw_throw:
626 case tok::kw_true:
627 case tok::kw_try:
628 case tok::kw_typedef:
629 case tok::kw_typeid:
630 case tok::kw_typename:
631 case tok::kw_typeof:
632 case tok::kw_union:
633 case tok::kw_unsigned:
634 case tok::kw_using:
635 case tok::kw_virtual:
636 case tok::kw_void:
637 case tok::kw_volatile:
638 case tok::kw_wchar_t:
639 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000640 case tok::kw__Bool:
641 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000642 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000643 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000644 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000645 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000646 }
Steve Naroff99264b42007-08-22 16:35:03 +0000647}
648
Fariborz Jahanian83615522008-01-02 22:54:34 +0000649/// objc-for-collection-in: 'in'
650///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000651bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000652 // FIXME: May have to do additional look-ahead to only allow for
653 // valid tokens following an 'in'; such as an identifier, unary operators,
654 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000655 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000656 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000657}
658
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000659/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000660/// qualifier list and builds their bitmask representation in the input
661/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000662///
663/// objc-type-qualifiers:
664/// objc-type-qualifier
665/// objc-type-qualifiers objc-type-qualifier
666///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000667void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner61511e12007-12-12 06:56:32 +0000668 while (1) {
Chris Lattner5e530bc2007-12-27 19:57:00 +0000669 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000670 return;
Mike Stump11289f42009-09-09 15:08:12 +0000671
Chris Lattner61511e12007-12-12 06:56:32 +0000672 const IdentifierInfo *II = Tok.getIdentifierInfo();
673 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000674 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000675 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000676
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000677 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000678 switch (i) {
679 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000680 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
681 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
682 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
683 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
684 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
685 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000686 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000687 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000688 ConsumeToken();
689 II = 0;
690 break;
691 }
Mike Stump11289f42009-09-09 15:08:12 +0000692
Chris Lattner61511e12007-12-12 06:56:32 +0000693 // If this wasn't a recognized qualifier, bail out.
694 if (II) return;
695 }
696}
697
698/// objc-type-name:
699/// '(' objc-type-qualifiers[opt] type-name ')'
700/// '(' objc-type-qualifiers[opt] ')'
701///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000702Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000703 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000704
Chris Lattnerb7954432008-10-22 03:52:06 +0000705 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000706 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000707
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000708 // Parse type qualifiers, in, inout, etc.
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000709 ParseObjCTypeQualifierList(DS);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000710
Chris Lattnerb7954432008-10-22 03:52:06 +0000711 TypeTy *Ty = 0;
Douglas Gregor220cac52009-02-18 17:45:20 +0000712 if (isTypeSpecifierQualifier()) {
713 TypeResult TypeSpec = ParseTypeName();
714 if (!TypeSpec.isInvalid())
715 Ty = TypeSpec.get();
716 }
Mike Stump11289f42009-09-09 15:08:12 +0000717
Steve Naroff90255b42008-10-21 14:15:04 +0000718 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000719 ConsumeParen();
720 else if (Tok.getLocation() == TypeStartLoc) {
721 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000722 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000723 SkipUntil(tok::r_paren);
724 } else {
725 // Otherwise, we found *something*, but didn't get a ')' in the right
726 // place. Emit an error then return what we have as the type.
727 MatchRHSPunctuation(tok::r_paren, LParenLoc);
728 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000729 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000730}
731
732/// objc-method-decl:
733/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000734/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000735/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000736/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000737///
738/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000739/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000740/// objc-keyword-selector objc-keyword-decl
741///
742/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000743/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
744/// objc-selector ':' objc-keyword-attributes[opt] identifier
745/// ':' objc-type-name objc-keyword-attributes[opt] identifier
746/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000747///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000748/// objc-parmlist:
749/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000750///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000751/// objc-parms:
752/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000753///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000754/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000755/// , ...
756///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000757/// objc-keyword-attributes: [OBJC2]
758/// __attribute__((unused))
759///
Chris Lattner83f095c2009-03-28 19:18:32 +0000760Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
761 tok::TokenKind mType,
762 DeclPtrTy IDecl,
763 tok::ObjCKeywordKind MethodImplKind) {
John McCall28a6aea2009-11-04 02:18:39 +0000764 ParsingDeclRAIIObject PD(*this);
765
Chris Lattner2ebb1782008-08-23 01:48:03 +0000766 // Parse the return type if present.
Chris Lattner5700fab2007-10-07 02:00:24 +0000767 TypeTy *ReturnType = 0;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000768 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000769 if (Tok.is(tok::l_paren))
Fariborz Jahanian7a9c4742007-10-31 23:53:01 +0000770 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump11289f42009-09-09 15:08:12 +0000771
Steve Naroff161a92b2007-10-26 20:53:56 +0000772 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000773 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000774
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000775 // An unnamed colon is valid.
776 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000777 Diag(Tok, diag::err_expected_selector_for_method)
778 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000779 // Skip until we get a ; or {}.
780 SkipUntil(tok::r_brace);
Chris Lattner83f095c2009-03-28 19:18:32 +0000781 return DeclPtrTy();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000782 }
Mike Stump11289f42009-09-09 15:08:12 +0000783
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000784 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattner0ef13522007-10-09 17:51:17 +0000785 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000786 // If attributes exist after the method, parse them.
787 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000788 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000789 MethodAttrs = ParseGNUAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000790
Chris Lattner5700fab2007-10-07 02:00:24 +0000791 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall28a6aea2009-11-04 02:18:39 +0000792 DeclPtrTy Result
793 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanianb5605172007-11-09 19:52:12 +0000794 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000795 0, CargNames, MethodAttrs,
796 MethodImplKind);
John McCall28a6aea2009-11-04 02:18:39 +0000797 PD.complete(Result);
798 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000799 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000800
Steve Narofff73590d2007-09-27 14:38:14 +0000801 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000802 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000803
Chris Lattner5700fab2007-10-07 02:00:24 +0000804 while (1) {
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000805 Action::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000806
Chris Lattner5700fab2007-10-07 02:00:24 +0000807 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000808 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000809 Diag(Tok, diag::err_expected_colon);
810 break;
811 }
812 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000813
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000814 ArgInfo.Type = 0;
815 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
816 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
817
Chris Lattner5700fab2007-10-07 02:00:24 +0000818 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000819 ArgInfo.ArgAttrs = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +0000820 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000821 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000822
Chris Lattner0ef13522007-10-09 17:51:17 +0000823 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000824 Diag(Tok, diag::err_expected_ident); // missing argument name.
825 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000826 }
Mike Stump11289f42009-09-09 15:08:12 +0000827
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000828 ArgInfo.Name = Tok.getIdentifierInfo();
829 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000830 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000831
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000832 ArgInfos.push_back(ArgInfo);
833 KeyIdents.push_back(SelIdent);
834
Chris Lattner5700fab2007-10-07 02:00:24 +0000835 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000836 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000837 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000838 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000839 break;
840 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000841 }
Mike Stump11289f42009-09-09 15:08:12 +0000842
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000843 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000844
Chris Lattner5700fab2007-10-07 02:00:24 +0000845 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000846 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000847 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000848 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000849 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000850 ConsumeToken();
851 break;
852 }
Chris Lattner5700fab2007-10-07 02:00:24 +0000853 DeclSpec DS;
854 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000855 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000856 Declarator ParmDecl(DS, Declarator::PrototypeContext);
857 ParseDeclarator(ParmDecl);
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000858 CargNames.push_back(ParmDecl);
Chris Lattner5700fab2007-10-07 02:00:24 +0000859 }
Mike Stump11289f42009-09-09 15:08:12 +0000860
Chris Lattner5700fab2007-10-07 02:00:24 +0000861 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000862 // If attributes exist after the method, parse them.
Chris Lattner5700fab2007-10-07 02:00:24 +0000863 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000864 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000865 MethodAttrs = ParseGNUAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000866
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000867 if (KeyIdents.size() == 0)
868 return DeclPtrTy();
Chris Lattner5700fab2007-10-07 02:00:24 +0000869 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
870 &KeyIdents[0]);
John McCall28a6aea2009-11-04 02:18:39 +0000871 DeclPtrTy Result
872 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000873 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000874 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000875 MethodImplKind, isVariadic);
John McCall28a6aea2009-11-04 02:18:39 +0000876 PD.complete(Result);
877 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +0000878}
879
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000880/// objc-protocol-refs:
881/// '<' identifier-list '>'
882///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000883bool Parser::
Chris Lattner83f095c2009-03-28 19:18:32 +0000884ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000885 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
886 bool WarnOnDeclarations,
887 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +0000888 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +0000889
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000890 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +0000891
Chris Lattner3bbae002008-07-26 04:03:38 +0000892 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +0000893
Chris Lattner3bbae002008-07-26 04:03:38 +0000894 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +0000895 if (Tok.is(tok::code_completion)) {
896 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
897 ProtocolIdents.size());
898 ConsumeToken();
899 }
900
Chris Lattner3bbae002008-07-26 04:03:38 +0000901 if (Tok.isNot(tok::identifier)) {
902 Diag(Tok, diag::err_expected_ident);
903 SkipUntil(tok::greater);
904 return true;
905 }
906 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
907 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000908 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +0000909 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000910
Chris Lattner3bbae002008-07-26 04:03:38 +0000911 if (Tok.isNot(tok::comma))
912 break;
913 ConsumeToken();
914 }
Mike Stump11289f42009-09-09 15:08:12 +0000915
Chris Lattner3bbae002008-07-26 04:03:38 +0000916 // Consume the '>'.
917 if (Tok.isNot(tok::greater)) {
918 Diag(Tok, diag::err_expected_greater);
919 return true;
920 }
Mike Stump11289f42009-09-09 15:08:12 +0000921
Chris Lattner3bbae002008-07-26 04:03:38 +0000922 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +0000923
Chris Lattner3bbae002008-07-26 04:03:38 +0000924 // Convert the list of protocols identifiers into a list of protocol decls.
925 Actions.FindProtocolDeclaration(WarnOnDeclarations,
926 &ProtocolIdents[0], ProtocolIdents.size(),
927 Protocols);
928 return false;
929}
930
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000931/// objc-class-instance-variables:
932/// '{' objc-instance-variable-decl-list[opt] '}'
933///
934/// objc-instance-variable-decl-list:
935/// objc-visibility-spec
936/// objc-instance-variable-decl ';'
937/// ';'
938/// objc-instance-variable-decl-list objc-visibility-spec
939/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
940/// objc-instance-variable-decl-list ';'
941///
942/// objc-visibility-spec:
943/// @private
944/// @protected
945/// @public
Steve Naroff00433d32007-08-21 21:17:12 +0000946/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000947///
948/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +0000949/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000950///
Chris Lattner83f095c2009-03-28 19:18:32 +0000951void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff33a1e802007-10-29 21:38:07 +0000952 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000953 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner83f095c2009-03-28 19:18:32 +0000954 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000955
Douglas Gregor45a33ec2009-01-12 18:45:55 +0000956 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000957
Steve Naroff00433d32007-08-21 21:17:12 +0000958 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +0000959
Fariborz Jahanian2dfcec12008-04-29 23:03:51 +0000960 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroff00433d32007-08-21 21:17:12 +0000961 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +0000962 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000963 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +0000964
Steve Naroff00433d32007-08-21 21:17:12 +0000965 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +0000966 if (Tok.is(tok::semi)) {
Chris Lattner3e4fac72009-11-06 06:40:12 +0000967 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner3c7b86f2009-12-06 17:36:05 +0000968 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +0000969 ConsumeToken();
970 continue;
971 }
Mike Stump11289f42009-09-09 15:08:12 +0000972
Steve Naroff00433d32007-08-21 21:17:12 +0000973 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +0000974 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +0000975 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +0000976
977 if (Tok.is(tok::code_completion)) {
978 Actions.CodeCompleteObjCAtVisibility(CurScope);
979 ConsumeToken();
980 }
981
Steve Naroff7c348172007-08-23 18:16:40 +0000982 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +0000983 case tok::objc_private:
984 case tok::objc_public:
985 case tok::objc_protected:
986 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +0000987 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +0000988 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000989 continue;
Steve Naroff00433d32007-08-21 21:17:12 +0000990 default:
991 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +0000992 continue;
993 }
994 }
Mike Stump11289f42009-09-09 15:08:12 +0000995
Douglas Gregor48d46252010-01-13 21:54:15 +0000996 if (Tok.is(tok::code_completion)) {
997 Actions.CodeCompleteOrdinaryName(CurScope,
998 Action::CCC_ObjCInstanceVariableList);
999 ConsumeToken();
1000 }
1001
John McCallcfefb6d2009-11-03 02:38:08 +00001002 struct ObjCIvarCallback : FieldCallback {
1003 Parser &P;
1004 DeclPtrTy IDecl;
1005 tok::ObjCKeywordKind visibility;
1006 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
1007
1008 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
1009 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
1010 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1011 }
1012
1013 DeclPtrTy invoke(FieldDeclarator &FD) {
1014 // Install the declarator into the interface decl.
1015 DeclPtrTy Field
1016 = P.Actions.ActOnIvar(P.CurScope,
1017 FD.D.getDeclSpec().getSourceRange().getBegin(),
1018 IDecl, FD.D, FD.BitfieldSize, visibility);
1019 AllIvarDecls.push_back(Field);
1020 return Field;
1021 }
1022 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1023
Chris Lattnera12405b2008-04-10 06:46:29 +00001024 // Parse all the comma separated declarators.
1025 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +00001026 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001027
Chris Lattner0ef13522007-10-09 17:51:17 +00001028 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001029 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001030 } else {
1031 Diag(Tok, diag::err_expected_semi_decl_list);
1032 // Skip to end of block or statement
1033 SkipUntil(tok::r_brace, true, true);
1034 }
1035 }
Steve Naroff33a1e802007-10-29 21:38:07 +00001036 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001037 // Call ActOnFields() even if we don't have any decls. This is useful
1038 // for code rewriting tools that need to be aware of the empty list.
1039 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +00001040 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001041 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001042 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001043}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001044
1045/// objc-protocol-declaration:
1046/// objc-protocol-definition
1047/// objc-protocol-forward-reference
1048///
1049/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001050/// @protocol identifier
1051/// objc-protocol-refs[opt]
1052/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001053/// @end
1054///
1055/// objc-protocol-forward-reference:
1056/// @protocol identifier-list ';'
1057///
1058/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001059/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001060/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattner83f095c2009-03-28 19:18:32 +00001061Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1062 AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +00001063 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001064 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1065 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001066
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001067 if (Tok.is(tok::code_completion)) {
1068 Actions.CodeCompleteObjCProtocolDecl(CurScope);
1069 ConsumeToken();
1070 }
1071
Chris Lattner0ef13522007-10-09 17:51:17 +00001072 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001073 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001074 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001075 }
1076 // Save the protocol name, then consume it.
1077 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1078 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001079
Chris Lattner0ef13522007-10-09 17:51:17 +00001080 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001081 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001082 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001083 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001084 attrList);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001085 }
Mike Stump11289f42009-09-09 15:08:12 +00001086
Chris Lattner0ef13522007-10-09 17:51:17 +00001087 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001088 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1089 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1090
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001091 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001092 while (1) {
1093 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001094 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001095 Diag(Tok, diag::err_expected_ident);
1096 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +00001097 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001098 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001099 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1100 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001101 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001102
Chris Lattner0ef13522007-10-09 17:51:17 +00001103 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001104 break;
1105 }
1106 // Consume the ';'.
1107 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattner83f095c2009-03-28 19:18:32 +00001108 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001109
Steve Naroff93eb5f12007-10-10 17:32:04 +00001110 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001111 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001112 ProtocolRefs.size(),
1113 attrList);
Chris Lattnerd7352d62008-07-21 22:17:28 +00001114 }
Mike Stump11289f42009-09-09 15:08:12 +00001115
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001116 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001117 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001118
Chris Lattner83f095c2009-03-28 19:18:32 +00001119 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001120 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001121 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001122 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1123 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +00001124 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001125
Chris Lattner83f095c2009-03-28 19:18:32 +00001126 DeclPtrTy ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001127 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001128 ProtocolRefs.data(),
1129 ProtocolRefs.size(),
Daniel Dunbar26e2ab42008-09-26 04:48:09 +00001130 EndProtoLoc, attrList);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001131 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001132 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001133}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001134
1135/// objc-implementation:
1136/// objc-class-implementation-prologue
1137/// objc-category-implementation-prologue
1138///
1139/// objc-class-implementation-prologue:
1140/// @implementation identifier objc-superclass[opt]
1141/// objc-class-instance-variables[opt]
1142///
1143/// objc-category-implementation-prologue:
1144/// @implementation identifier ( identifier )
Chris Lattner83f095c2009-03-28 19:18:32 +00001145Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001146 SourceLocation atLoc) {
1147 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1148 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1149 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001150
Douglas Gregor49c22a72009-11-18 16:26:39 +00001151 // Code completion after '@implementation'.
1152 if (Tok.is(tok::code_completion)) {
1153 Actions.CodeCompleteObjCImplementationDecl(CurScope);
1154 ConsumeToken();
1155 }
1156
Chris Lattner0ef13522007-10-09 17:51:17 +00001157 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001158 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001159 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001160 }
1161 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001162 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001163 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001164
1165 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001166 // we have a category implementation.
1167 SourceLocation lparenLoc = ConsumeParen();
1168 SourceLocation categoryLoc, rparenLoc;
1169 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001170
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001171 if (Tok.is(tok::code_completion)) {
1172 Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
1173 ConsumeToken();
1174 }
1175
Chris Lattner0ef13522007-10-09 17:51:17 +00001176 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001177 categoryId = Tok.getIdentifierInfo();
1178 categoryLoc = ConsumeToken();
1179 } else {
1180 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001181 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001182 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001183 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001184 Diag(Tok, diag::err_expected_rparen);
1185 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001186 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001187 }
1188 rparenLoc = ConsumeParen();
Chris Lattner83f095c2009-03-28 19:18:32 +00001189 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001190 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001191 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001192 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001193 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001194 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001195 }
1196 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001197 SourceLocation superClassLoc;
1198 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001199 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001200 // We have a super class
1201 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001202 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001203 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001204 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001205 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001206 superClassId = Tok.getIdentifierInfo();
1207 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001208 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001209 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001210 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001211 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001212
Steve Naroff33a1e802007-10-29 21:38:07 +00001213 if (Tok.is(tok::l_brace)) // we have ivars
1214 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001215 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001216 PendingObjCImpDecl.push_back(ObjCImpDecl);
1217
Chris Lattner83f095c2009-03-28 19:18:32 +00001218 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001219}
Steve Naroff33a1e802007-10-29 21:38:07 +00001220
Ted Kremenekc7c64312010-01-07 01:20:12 +00001221Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001222 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1223 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattner83f095c2009-03-28 19:18:32 +00001224 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001225 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001226 if (ObjCImpDecl) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001227 Actions.ActOnAtEnd(atEnd, ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001228 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001229 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001230 }
Ted Kremenekc7c64312010-01-07 01:20:12 +00001231 else {
1232 // missing @implementation
1233 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1234 }
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001235 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001236}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001237
Fariborz Jahanianbc02a102009-11-17 17:15:16 +00001238Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001239 if (PendingObjCImpDecl.empty())
1240 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1241 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
Ted Kremenekc7c64312010-01-07 01:20:12 +00001242 Actions.ActOnAtEnd(SourceRange(), ImpDecl);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001243 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1244}
1245
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001246/// compatibility-alias-decl:
1247/// @compatibility_alias alias-name class-name ';'
1248///
Chris Lattner83f095c2009-03-28 19:18:32 +00001249Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001250 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1251 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1252 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001253 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001254 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001255 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001256 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001257 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1258 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001259 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001260 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001261 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001262 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001263 IdentifierInfo *classId = Tok.getIdentifierInfo();
1264 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1265 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001266 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattner83f095c2009-03-28 19:18:32 +00001267 return DeclPtrTy();
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001268 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001269 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1270 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001271}
1272
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001273/// property-synthesis:
1274/// @synthesize property-ivar-list ';'
1275///
1276/// property-ivar-list:
1277/// property-ivar
1278/// property-ivar-list ',' property-ivar
1279///
1280/// property-ivar:
1281/// identifier
1282/// identifier '=' identifier
1283///
Chris Lattner83f095c2009-03-28 19:18:32 +00001284Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001285 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1286 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001287 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001288
Douglas Gregor88e72a02009-11-18 19:45:45 +00001289 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001290 if (Tok.is(tok::code_completion)) {
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001291 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
Douglas Gregor5d649882009-11-18 22:32:06 +00001292 ConsumeToken();
1293 }
1294
Douglas Gregor88e72a02009-11-18 19:45:45 +00001295 if (Tok.isNot(tok::identifier)) {
1296 Diag(Tok, diag::err_synthesized_property_name);
1297 SkipUntil(tok::semi);
1298 return DeclPtrTy();
1299 }
1300
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001301 IdentifierInfo *propertyIvar = 0;
1302 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1303 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattner0ef13522007-10-09 17:51:17 +00001304 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001305 // property '=' ivar-name
1306 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001307
1308 if (Tok.is(tok::code_completion)) {
1309 Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1310 ObjCImpDecl);
1311 ConsumeToken();
1312 }
1313
Chris Lattner0ef13522007-10-09 17:51:17 +00001314 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001315 Diag(Tok, diag::err_expected_ident);
1316 break;
1317 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001318 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001319 ConsumeToken(); // consume ivar-name
1320 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001321 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1322 propertyId, propertyIvar);
Chris Lattner0ef13522007-10-09 17:51:17 +00001323 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001324 break;
1325 ConsumeToken(); // consume ','
1326 }
Douglas Gregor88e72a02009-11-18 19:45:45 +00001327 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001328 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregor88e72a02009-11-18 19:45:45 +00001329 SkipUntil(tok::semi);
1330 }
Fariborz Jahanian95239112009-11-06 21:48:47 +00001331 else
1332 ConsumeToken(); // consume ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001333 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001334}
1335
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001336/// property-dynamic:
1337/// @dynamic property-list
1338///
1339/// property-list:
1340/// identifier
1341/// property-list ',' identifier
1342///
Chris Lattner83f095c2009-03-28 19:18:32 +00001343Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001344 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1345 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1346 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001347 while (true) {
1348 if (Tok.is(tok::code_completion)) {
1349 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1350 ConsumeToken();
1351 }
1352
1353 if (Tok.isNot(tok::identifier)) {
1354 Diag(Tok, diag::err_expected_ident);
1355 SkipUntil(tok::semi);
1356 return DeclPtrTy();
1357 }
1358
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001359 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1360 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1361 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1362 propertyId, 0);
1363
Chris Lattner0ef13522007-10-09 17:51:17 +00001364 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001365 break;
1366 ConsumeToken(); // consume ','
1367 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001368 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001369 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattner83f095c2009-03-28 19:18:32 +00001370 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001371}
Mike Stump11289f42009-09-09 15:08:12 +00001372
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001373/// objc-throw-statement:
1374/// throw expression[opt];
1375///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001376Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001377 OwningExprResult Res(Actions);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001378 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001379 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001380 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001381 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001382 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001383 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001384 }
1385 }
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001386 ConsumeToken(); // consume ';'
Steve Naroff5ee2c022009-02-11 20:05:44 +00001387 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001388}
1389
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001390/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001391/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001392///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001393Parser::OwningStmtResult
1394Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001395 ConsumeToken(); // consume synchronized
1396 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001397 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001398 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001399 }
1400 ConsumeParen(); // '('
Sebastian Redl59b5e512008-12-11 21:36:32 +00001401 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001402 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001403 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001404 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001405 }
1406 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001407 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001408 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001409 }
1410 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001411 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001412 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001413 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001414 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001415 // Enter a scope to hold everything within the compound stmt. Compound
1416 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001417 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001418
Sebastian Redl042ad952008-12-11 19:30:53 +00001419 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001420
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001421 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001422 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001423 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl726a0d92009-02-05 15:02:23 +00001424 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001425}
1426
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001427/// objc-try-catch-statement:
1428/// @try compound-statement objc-catch-list[opt]
1429/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1430///
1431/// objc-catch-list:
1432/// @catch ( parameter-declaration ) compound-statement
1433/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1434/// catch-parameter-declaration:
1435/// parameter-declaration
1436/// '...' [OBJC2]
1437///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001438Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001439 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001440
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001441 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001442 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001443 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001444 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001445 }
Sebastian Redlc13f2682008-12-09 20:22:58 +00001446 OwningStmtResult CatchStmts(Actions);
1447 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001448 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl042ad952008-12-11 19:30:53 +00001449 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001450 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001451 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001452 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001453
Chris Lattner0ef13522007-10-09 17:51:17 +00001454 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001455 // At this point, we need to lookahead to determine if this @ is the start
1456 // of an @catch or @finally. We don't want to consume the @ token if this
1457 // is an @try or @encode or something else.
1458 Token AfterAt = GetLookAheadToken(1);
1459 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1460 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1461 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001462
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001463 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001464 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001465 DeclPtrTy FirstPart;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001466 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001467 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001468 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001469 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001470 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001471 DeclSpec DS;
1472 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001473 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001474 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001475 // we must accept either 'declarator' or 'abstract-declarator' here.
1476 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1477 ParseDeclarator(ParmDecl);
1478
1479 // Inform the actions module about the parameter declarator, so it
1480 // gets added to the current scope.
1481 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001482 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001483 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001484
Steve Naroff65a00892009-04-07 22:56:58 +00001485 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001486
Steve Naroff65a00892009-04-07 22:56:58 +00001487 if (Tok.is(tok::r_paren))
1488 RParenLoc = ConsumeParen();
1489 else // Skip over garbage, until we get to ')'. Eat the ')'.
1490 SkipUntil(tok::r_paren, true, false);
1491
Sebastian Redlc13f2682008-12-09 20:22:58 +00001492 OwningStmtResult CatchBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001493 if (Tok.is(tok::l_brace))
1494 CatchBody = ParseCompoundStatementBody();
1495 else
1496 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001497 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001498 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001499 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff371b8fb2009-03-03 19:52:17 +00001500 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001501 move(CatchStmts));
Steve Naroffe6016792008-02-05 21:27:35 +00001502 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001503 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1504 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001505 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001506 }
1507 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001508 } else {
1509 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001510 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001511 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001512
Sebastian Redlc13f2682008-12-09 20:22:58 +00001513 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001514 if (Tok.is(tok::l_brace))
1515 FinallyBody = ParseCompoundStatementBody();
1516 else
1517 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001518 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001519 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001520 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +00001521 move(FinallyBody));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001522 catch_or_finally_seen = true;
1523 break;
1524 }
1525 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001526 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001527 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001528 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001529 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001530 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1531 move(FinallyStmt));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001532}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001533
Steve Naroff09bf8152007-09-06 21:24:23 +00001534/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001535///
Chris Lattner83f095c2009-03-28 19:18:32 +00001536Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1537 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001538
Chris Lattnereae6cb62009-03-05 08:00:35 +00001539 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1540 PP.getSourceManager(),
1541 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001542
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001543 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001544 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001545 if (ObjCImpDecl) {
1546 Diag(Tok, diag::warn_semicolon_before_method_body)
Chris Lattner3c7b86f2009-12-06 17:36:05 +00001547 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001548 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001549 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001550 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001551
Steve Naroffbb875722007-11-11 19:54:21 +00001552 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001553 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001554 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001555
Steve Naroffbb875722007-11-11 19:54:21 +00001556 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1557 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001558
Steve Naroffbb875722007-11-11 19:54:21 +00001559 // If we didn't find the '{', bail out.
1560 if (Tok.isNot(tok::l_brace))
Chris Lattner83f095c2009-03-28 19:18:32 +00001561 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001562 }
Steve Naroffbb875722007-11-11 19:54:21 +00001563 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001564
Steve Naroffbb875722007-11-11 19:54:21 +00001565 // Enter a scope for the method body.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001566 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001567
Steve Naroffbb875722007-11-11 19:54:21 +00001568 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001569 // specified Declarator for the method.
Steve Naroff70f41d62009-02-28 16:59:13 +00001570 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001571
1572 OwningStmtResult FnBody(ParseCompoundStatementBody());
1573
Steve Naroffbb875722007-11-11 19:54:21 +00001574 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001575 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001576 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1577 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001578
Steve Naroffb94d7f62009-03-02 22:00:56 +00001579 // TODO: Pass argument information.
1580 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump11289f42009-09-09 15:08:12 +00001581
Steve Naroffbb875722007-11-11 19:54:21 +00001582 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001583 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001584
Steve Naroff7b8fa472007-11-13 23:01:27 +00001585 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001586}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001587
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001588Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001589 if (Tok.is(tok::code_completion)) {
1590 Actions.CodeCompleteObjCAtStatement(CurScope);
1591 ConsumeToken();
1592 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001593 }
1594
1595 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001596 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001597
1598 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001599 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001600
1601 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001602 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001603
Sebastian Redl90893182008-12-11 22:33:27 +00001604 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001605 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001606 // If the expression is invalid, skip ahead to the next semicolon. Not
1607 // doing this opens us up to the possibility of infinite loops if
1608 // ParseExpression does not consume any tokens.
1609 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001610 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001611 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001612
Steve Naroffe6016792008-02-05 21:27:35 +00001613 // Otherwise, eat the semicolon.
1614 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlssonafb2dad2009-12-16 02:09:40 +00001615 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Steve Naroffe6016792008-02-05 21:27:35 +00001616}
1617
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001618Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001619 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001620 case tok::code_completion:
1621 Actions.CodeCompleteObjCAtExpression(CurScope);
1622 ConsumeToken();
1623 return ExprError();
1624
Chris Lattnere002fbe2007-12-12 01:04:12 +00001625 case tok::string_literal: // primary-expression: string-literal
1626 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001627 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001628 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001629 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001630 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001631
Chris Lattner197a3012008-08-05 06:19:09 +00001632 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1633 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001634 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001635 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001636 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001637 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001638 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001639 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001640 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001641 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001642 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001643}
1644
Mike Stump11289f42009-09-09 15:08:12 +00001645/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001646/// '[' objc-receiver objc-message-args ']'
1647///
1648/// objc-receiver:
1649/// expression
1650/// class-name
1651/// type-name
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001652Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001653 assert(Tok.is(tok::l_square) && "'[' expected");
1654 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1655
1656 // Parse receiver
Chris Lattnere10c6da2008-01-25 19:25:00 +00001657 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner8f697062008-01-25 18:59:06 +00001658 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00001659 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1660 SourceLocation NameLoc = ConsumeToken();
1661 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1662 ExprArg(Actions));
1663 }
Chris Lattner8f697062008-01-25 18:59:06 +00001664 }
1665
Sebastian Redl59b5e512008-12-11 21:36:32 +00001666 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001667 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00001668 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001669 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00001670 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001671
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001672 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001673 0, move(Res));
Chris Lattner8f697062008-01-25 18:59:06 +00001674}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001675
Chris Lattner8f697062008-01-25 18:59:06 +00001676/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1677/// the rest of a message expression.
Mike Stump11289f42009-09-09 15:08:12 +00001678///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001679/// objc-message-args:
1680/// objc-selector
1681/// objc-keywordarg-list
1682///
1683/// objc-keywordarg-list:
1684/// objc-keywordarg
1685/// objc-keywordarg-list objc-keywordarg
1686///
Mike Stump11289f42009-09-09 15:08:12 +00001687/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001688/// selector-name[opt] ':' objc-keywordexpr
1689///
1690/// objc-keywordexpr:
1691/// nonempty-expr-list
1692///
1693/// nonempty-expr-list:
1694/// assignment-expression
1695/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001696///
1697Parser::OwningExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00001698Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff9e4ac112008-11-19 15:54:23 +00001699 SourceLocation NameLoc,
Chris Lattner8f697062008-01-25 18:59:06 +00001700 IdentifierInfo *ReceiverName,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001701 ExprArg ReceiverExpr) {
Steve Naroffeae65032009-11-07 02:08:14 +00001702 if (Tok.is(tok::code_completion)) {
1703 if (ReceiverName)
Douglas Gregor1b605f72009-11-19 01:08:35 +00001704 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1705 0, 0);
Steve Naroffeae65032009-11-07 02:08:14 +00001706 else
Douglas Gregor1b605f72009-11-19 01:08:35 +00001707 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1708 0, 0);
Steve Naroffeae65032009-11-07 02:08:14 +00001709 ConsumeToken();
1710 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00001711
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001712 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001713 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001714 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00001715
Anders Carlsson978f08d2009-02-14 18:21:46 +00001716 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00001717
Steve Narofff73590d2007-09-27 14:38:14 +00001718 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00001719 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00001720
Chris Lattner0ef13522007-10-09 17:51:17 +00001721 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001722 while (1) {
1723 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00001724 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00001725
Chris Lattner0ef13522007-10-09 17:51:17 +00001726 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001727 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00001728 // We must manually skip to a ']', otherwise the expression skipper will
1729 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1730 // the enclosing expression.
1731 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001732 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001733 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001734
Steve Narofff73590d2007-09-27 14:38:14 +00001735 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001736 /// Parse the expression after ':'
Sebastian Redl59b5e512008-12-11 21:36:32 +00001737 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001738 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001739 // We must manually skip to a ']', otherwise the expression skipper will
1740 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1741 // the enclosing expression.
1742 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001743 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00001744 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001745
Steve Naroff486760a2007-09-17 20:25:27 +00001746 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001747 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001748
Douglas Gregor1b605f72009-11-19 01:08:35 +00001749 // Code completion after each argument.
1750 if (Tok.is(tok::code_completion)) {
1751 if (ReceiverName)
1752 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1753 KeyIdents.data(),
1754 KeyIdents.size());
1755 else
1756 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1757 KeyIdents.data(),
1758 KeyIdents.size());
1759 ConsumeToken();
1760 }
1761
Steve Naroff486760a2007-09-17 20:25:27 +00001762 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00001763 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001764 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001765 break;
1766 // We have a selector or a colon, continue parsing.
1767 }
1768 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00001769 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001770 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00001771 /// Parse the expression after ','
Sebastian Redl59b5e512008-12-11 21:36:32 +00001772 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001773 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001774 // We must manually skip to a ']', otherwise the expression skipper will
1775 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1776 // the enclosing expression.
1777 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001778 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001779 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001780
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001781 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001782 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001783 }
1784 } else if (!selIdent) {
1785 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001786
Chris Lattner197a3012008-08-05 06:19:09 +00001787 // We must manually skip to a ']', otherwise the expression skipper will
1788 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1789 // the enclosing expression.
1790 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001791 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001792 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001793
Chris Lattner0ef13522007-10-09 17:51:17 +00001794 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001795 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00001796 // We must manually skip to a ']', otherwise the expression skipper will
1797 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1798 // the enclosing expression.
1799 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001800 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001801 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001802
Chris Lattner8f697062008-01-25 18:59:06 +00001803 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001804
Steve Naroffe61bfa82007-10-05 18:42:47 +00001805 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00001806 if (nKeys == 0)
1807 KeyIdents.push_back(selIdent);
1808 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001809
Chris Lattner5700fab2007-10-07 02:00:24 +00001810 // We've just parsed a keyword message.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001811 if (ReceiverName)
1812 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump11289f42009-09-09 15:08:12 +00001813 LBracLoc, NameLoc, SelectorLoc,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001814 RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001815 KeyExprs.take(), KeyExprs.size()));
1816 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001817 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001818 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001819}
1820
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001821Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redld65cea82008-12-11 22:51:44 +00001822 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001823 if (Res.isInvalid()) return move(Res);
1824
Chris Lattnere002fbe2007-12-12 01:04:12 +00001825 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1826 // expressions. At this point, we know that the only valid thing that starts
1827 // with '@' is an @"".
1828 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00001829 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00001830 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001831 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001832
Chris Lattnere002fbe2007-12-12 01:04:12 +00001833 while (Tok.is(tok::at)) {
1834 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00001835
Sebastian Redlc13f2682008-12-09 20:22:58 +00001836 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001837 if (!isTokenStringLiteral())
1838 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001839
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001840 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001841 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001842 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001843
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001844 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00001845 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001846
1847 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1848 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00001849}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001850
1851/// objc-encode-expression:
1852/// @encode ( type-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001853Parser::OwningExprResult
1854Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00001855 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001856
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001857 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001858
Chris Lattner197a3012008-08-05 06:19:09 +00001859 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001860 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1861
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001862 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001863
Douglas Gregor220cac52009-02-18 17:45:20 +00001864 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001865
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001866 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001867
Douglas Gregor220cac52009-02-18 17:45:20 +00001868 if (Ty.isInvalid())
1869 return ExprError();
1870
Mike Stump11289f42009-09-09 15:08:12 +00001871 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00001872 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001873}
Anders Carlssone01493d2007-08-23 15:25:28 +00001874
1875/// objc-protocol-expression
1876/// @protocol ( protocol-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001877Parser::OwningExprResult
1878Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00001879 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001880
Chris Lattner197a3012008-08-05 06:19:09 +00001881 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001882 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1883
Anders Carlssone01493d2007-08-23 15:25:28 +00001884 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001885
Chris Lattner197a3012008-08-05 06:19:09 +00001886 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001887 return ExprError(Diag(Tok, diag::err_expected_ident));
1888
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001889 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00001890 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001891
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001892 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00001893
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001894 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1895 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00001896}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001897
1898/// objc-selector-expression
1899/// @selector '(' objc-keyword-selector ')'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001900Parser::OwningExprResult
1901Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001902 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001903
Chris Lattner197a3012008-08-05 06:19:09 +00001904 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001905 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1906
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001907 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001908 SourceLocation LParenLoc = ConsumeParen();
1909 SourceLocation sLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001910 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001911 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1912 return ExprError(Diag(Tok, diag::err_expected_ident));
1913
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001914 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00001915 unsigned nColons = 0;
1916 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001917 while (1) {
Chris Lattner197a3012008-08-05 06:19:09 +00001918 if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001919 return ExprError(Diag(Tok, diag::err_expected_colon));
1920
Chris Lattner5e530bc2007-12-27 19:57:00 +00001921 nColons++;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001922 ConsumeToken(); // Eat the ':'.
1923 if (Tok.is(tok::r_paren))
1924 break;
1925 // Check for another keyword selector.
1926 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001927 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001928 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001929 if (!SelIdent && Tok.isNot(tok::colon))
1930 break;
1931 }
Steve Naroff152dd812007-12-05 22:21:29 +00001932 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001933 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00001934 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001935 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1936 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00001937 }