blob: eb58ebc0e1a4b7c373b256425617c159808e1178 [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
Chris Lattner038a3e32008-10-20 05:46:22 +0000337 // If we don't have an @ directive, parse it as a function definition.
338 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000339 // The code below does not consume '}'s because it is afraid of eating the
340 // end of a namespace. Because of the way this code is structured, an
341 // erroneous r_brace would cause an infinite loop if not handled here.
342 if (Tok.is(tok::r_brace))
343 break;
Mike Stump11289f42009-09-09 15:08:12 +0000344
Steve Narofff1bc45b2007-08-22 18:35:33 +0000345 // FIXME: as the name implies, this rule allows function definitions.
346 // We could pass a flag or check for functions during semantic analysis.
Alexis Hunt96d5c762009-11-21 08:43:09 +0000347 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattner038a3e32008-10-20 05:46:22 +0000348 continue;
349 }
Mike Stump11289f42009-09-09 15:08:12 +0000350
Chris Lattner038a3e32008-10-20 05:46:22 +0000351 // Otherwise, we have an @ directive, eat the @.
352 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000353 if (Tok.is(tok::code_completion)) {
354 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
355 ConsumeToken();
356 break;
357 }
358
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000359 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000360
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000361 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000362 AtEnd.setBegin(AtLoc);
363 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000364 break;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000365 }
Mike Stump11289f42009-09-09 15:08:12 +0000366
Chris Lattnerda9fb152008-10-20 06:10:06 +0000367 // Eat the identifier.
368 ConsumeToken();
369
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000370 switch (DirectiveKind) {
371 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000372 // FIXME: If someone forgets an @end on a protocol, this loop will
373 // continue to eat up tons of stuff and spew lots of nonsense errors. It
374 // would probably be better to bail out if we saw an @class or @interface
375 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000376 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000377 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000378 SkipUntil(tok::r_brace, tok::at);
379 break;
Mike Stump11289f42009-09-09 15:08:12 +0000380
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000381 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000382 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000383 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000384 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000385 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000386 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000387 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000388 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000389 break;
Mike Stump11289f42009-09-09 15:08:12 +0000390
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000391 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000392 if (!getLang().ObjC2)
393 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
394
Chris Lattner038a3e32008-10-20 05:46:22 +0000395 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000396 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000397 if (Tok.is(tok::l_paren))
Douglas Gregorc8537c52009-11-19 07:41:15 +0000398 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
399 allMethods.data(), allMethods.size());
Mike Stump11289f42009-09-09 15:08:12 +0000400
John McCall064d77b2009-12-03 22:31:13 +0000401 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
402 OCDS, AtLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000403
Chris Lattner038a3e32008-10-20 05:46:22 +0000404 // Parse all the comma separated declarators.
405 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000406 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000407
Chris Lattnere0d3fe92008-10-20 06:15:13 +0000408 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
409 tok::at);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000410 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000411 }
Steve Naroff99264b42007-08-22 16:35:03 +0000412 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000413
414 // We break out of the big loop in two cases: when we see @end or when we see
415 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000416 if (Tok.is(tok::code_completion)) {
417 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
418 ConsumeToken();
419 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerda9fb152008-10-20 06:10:06 +0000420 ConsumeToken(); // the "end" identifier
421 else
422 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000423
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000424 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000425 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenekc7c64312010-01-07 01:20:12 +0000426 Actions.ActOnAtEnd(AtEnd, interfaceDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000427 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000428 allProperties.data(), allProperties.size(),
429 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000430}
431
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000432/// Parse property attribute declarations.
433///
434/// property-attr-decl: '(' property-attrlist ')'
435/// property-attrlist:
436/// property-attribute
437/// property-attrlist ',' property-attribute
438/// property-attribute:
439/// getter '=' identifier
440/// setter '=' identifier ':'
441/// readonly
442/// readwrite
443/// assign
444/// retain
445/// copy
446/// nonatomic
447///
Douglas Gregorc8537c52009-11-19 07:41:15 +0000448void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl,
449 DeclPtrTy *Methods,
450 unsigned NumMethods) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000451 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000452 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000453
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000454 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000455 if (Tok.is(tok::code_completion)) {
Douglas Gregor36029f42009-11-18 23:08:07 +0000456 Actions.CodeCompleteObjCPropertyFlags(CurScope, DS);
Steve Naroff936354c2009-10-08 21:55:05 +0000457 ConsumeToken();
458 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000459 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000460
Chris Lattner76619232008-10-20 07:22:18 +0000461 // If this is not an identifier at all, bail out early.
462 if (II == 0) {
463 MatchRHSPunctuation(tok::r_paren, LHSLoc);
464 return;
465 }
Mike Stump11289f42009-09-09 15:08:12 +0000466
Chris Lattner1db33542008-10-20 07:37:22 +0000467 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000468
Chris Lattner68e48682008-11-20 04:42:34 +0000469 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000470 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000471 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000472 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner68e48682008-11-20 04:42:34 +0000473 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000474 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000475 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000476 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner68e48682008-11-20 04:42:34 +0000477 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000478 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000479 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000480 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner68e48682008-11-20 04:42:34 +0000481 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattner825bca12008-10-20 07:39:53 +0000482 // getter/setter require extra treatment.
Chris Lattner1db33542008-10-20 07:37:22 +0000483 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
484 tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000485 return;
Mike Stump11289f42009-09-09 15:08:12 +0000486
Douglas Gregorc8537c52009-11-19 07:41:15 +0000487 if (Tok.is(tok::code_completion)) {
488 if (II->getNameStart()[0] == 's')
489 Actions.CodeCompleteObjCPropertySetter(CurScope, ClassDecl,
490 Methods, NumMethods);
491 else
492 Actions.CodeCompleteObjCPropertyGetter(CurScope, ClassDecl,
493 Methods, NumMethods);
494 ConsumeToken();
495 }
496
Chris Lattnerbeca7702008-10-20 07:24:39 +0000497 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000498 Diag(Tok, diag::err_expected_ident);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000499 SkipUntil(tok::r_paren);
500 return;
501 }
Mike Stump11289f42009-09-09 15:08:12 +0000502
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000503 if (II->getNameStart()[0] == 's') {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000504 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
505 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000506 ConsumeToken(); // consume method name
Mike Stump11289f42009-09-09 15:08:12 +0000507
Chris Lattner1db33542008-10-20 07:37:22 +0000508 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
509 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000510 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000511 } else {
512 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
513 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000514 ConsumeToken(); // consume method name
Chris Lattnerbeca7702008-10-20 07:24:39 +0000515 }
Chris Lattner825bca12008-10-20 07:39:53 +0000516 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000517 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000518 SkipUntil(tok::r_paren);
519 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000520 }
Mike Stump11289f42009-09-09 15:08:12 +0000521
Chris Lattner1db33542008-10-20 07:37:22 +0000522 if (Tok.isNot(tok::comma))
523 break;
Mike Stump11289f42009-09-09 15:08:12 +0000524
Chris Lattner1db33542008-10-20 07:37:22 +0000525 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000526 }
Mike Stump11289f42009-09-09 15:08:12 +0000527
Chris Lattner1db33542008-10-20 07:37:22 +0000528 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000529}
530
Steve Naroff09bf8152007-09-06 21:24:23 +0000531/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000532/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000533/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000534///
535/// objc-instance-method: '-'
536/// objc-class-method: '+'
537///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000538/// objc-method-attributes: [OBJC2]
539/// __attribute__((deprecated))
540///
Mike Stump11289f42009-09-09 15:08:12 +0000541Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000542 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000543 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000544
Mike Stump11289f42009-09-09 15:08:12 +0000545 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000546 SourceLocation mLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000547
Chris Lattner83f095c2009-03-28 19:18:32 +0000548 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff09bf8152007-09-06 21:24:23 +0000549 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000550 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000551 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000552}
553
554/// objc-selector:
555/// identifier
556/// one of
557/// enum struct union if else while do for switch case default
558/// break continue return goto asm sizeof typeof __alignof
559/// unsigned long const short volatile signed restrict _Complex
560/// in out inout bycopy byref oneway int char float double void _Bool
561///
Chris Lattner4f472a32009-04-11 18:13:45 +0000562IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000563 switch (Tok.getKind()) {
564 default:
565 return 0;
566 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000567 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000568 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000569 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000570 case tok::kw_break:
571 case tok::kw_case:
572 case tok::kw_catch:
573 case tok::kw_char:
574 case tok::kw_class:
575 case tok::kw_const:
576 case tok::kw_const_cast:
577 case tok::kw_continue:
578 case tok::kw_default:
579 case tok::kw_delete:
580 case tok::kw_do:
581 case tok::kw_double:
582 case tok::kw_dynamic_cast:
583 case tok::kw_else:
584 case tok::kw_enum:
585 case tok::kw_explicit:
586 case tok::kw_export:
587 case tok::kw_extern:
588 case tok::kw_false:
589 case tok::kw_float:
590 case tok::kw_for:
591 case tok::kw_friend:
592 case tok::kw_goto:
593 case tok::kw_if:
594 case tok::kw_inline:
595 case tok::kw_int:
596 case tok::kw_long:
597 case tok::kw_mutable:
598 case tok::kw_namespace:
599 case tok::kw_new:
600 case tok::kw_operator:
601 case tok::kw_private:
602 case tok::kw_protected:
603 case tok::kw_public:
604 case tok::kw_register:
605 case tok::kw_reinterpret_cast:
606 case tok::kw_restrict:
607 case tok::kw_return:
608 case tok::kw_short:
609 case tok::kw_signed:
610 case tok::kw_sizeof:
611 case tok::kw_static:
612 case tok::kw_static_cast:
613 case tok::kw_struct:
614 case tok::kw_switch:
615 case tok::kw_template:
616 case tok::kw_this:
617 case tok::kw_throw:
618 case tok::kw_true:
619 case tok::kw_try:
620 case tok::kw_typedef:
621 case tok::kw_typeid:
622 case tok::kw_typename:
623 case tok::kw_typeof:
624 case tok::kw_union:
625 case tok::kw_unsigned:
626 case tok::kw_using:
627 case tok::kw_virtual:
628 case tok::kw_void:
629 case tok::kw_volatile:
630 case tok::kw_wchar_t:
631 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000632 case tok::kw__Bool:
633 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000634 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000635 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000636 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000637 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000638 }
Steve Naroff99264b42007-08-22 16:35:03 +0000639}
640
Fariborz Jahanian83615522008-01-02 22:54:34 +0000641/// objc-for-collection-in: 'in'
642///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000643bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000644 // FIXME: May have to do additional look-ahead to only allow for
645 // valid tokens following an 'in'; such as an identifier, unary operators,
646 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000647 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000648 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000649}
650
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000651/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000652/// qualifier list and builds their bitmask representation in the input
653/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000654///
655/// objc-type-qualifiers:
656/// objc-type-qualifier
657/// objc-type-qualifiers objc-type-qualifier
658///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000659void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner61511e12007-12-12 06:56:32 +0000660 while (1) {
Chris Lattner5e530bc2007-12-27 19:57:00 +0000661 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000662 return;
Mike Stump11289f42009-09-09 15:08:12 +0000663
Chris Lattner61511e12007-12-12 06:56:32 +0000664 const IdentifierInfo *II = Tok.getIdentifierInfo();
665 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000666 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000667 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000668
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000669 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000670 switch (i) {
671 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000672 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
673 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
674 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
675 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
676 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
677 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000678 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000679 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000680 ConsumeToken();
681 II = 0;
682 break;
683 }
Mike Stump11289f42009-09-09 15:08:12 +0000684
Chris Lattner61511e12007-12-12 06:56:32 +0000685 // If this wasn't a recognized qualifier, bail out.
686 if (II) return;
687 }
688}
689
690/// objc-type-name:
691/// '(' objc-type-qualifiers[opt] type-name ')'
692/// '(' objc-type-qualifiers[opt] ')'
693///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000694Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000695 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000696
Chris Lattnerb7954432008-10-22 03:52:06 +0000697 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000698 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000699
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000700 // Parse type qualifiers, in, inout, etc.
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000701 ParseObjCTypeQualifierList(DS);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000702
Chris Lattnerb7954432008-10-22 03:52:06 +0000703 TypeTy *Ty = 0;
Douglas Gregor220cac52009-02-18 17:45:20 +0000704 if (isTypeSpecifierQualifier()) {
705 TypeResult TypeSpec = ParseTypeName();
706 if (!TypeSpec.isInvalid())
707 Ty = TypeSpec.get();
708 }
Mike Stump11289f42009-09-09 15:08:12 +0000709
Steve Naroff90255b42008-10-21 14:15:04 +0000710 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000711 ConsumeParen();
712 else if (Tok.getLocation() == TypeStartLoc) {
713 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000714 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000715 SkipUntil(tok::r_paren);
716 } else {
717 // Otherwise, we found *something*, but didn't get a ')' in the right
718 // place. Emit an error then return what we have as the type.
719 MatchRHSPunctuation(tok::r_paren, LParenLoc);
720 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000721 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000722}
723
724/// objc-method-decl:
725/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000726/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000727/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000728/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000729///
730/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000731/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000732/// objc-keyword-selector objc-keyword-decl
733///
734/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000735/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
736/// objc-selector ':' objc-keyword-attributes[opt] identifier
737/// ':' objc-type-name objc-keyword-attributes[opt] identifier
738/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000739///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000740/// objc-parmlist:
741/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000742///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000743/// objc-parms:
744/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000745///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000746/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000747/// , ...
748///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000749/// objc-keyword-attributes: [OBJC2]
750/// __attribute__((unused))
751///
Chris Lattner83f095c2009-03-28 19:18:32 +0000752Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
753 tok::TokenKind mType,
754 DeclPtrTy IDecl,
755 tok::ObjCKeywordKind MethodImplKind) {
John McCall28a6aea2009-11-04 02:18:39 +0000756 ParsingDeclRAIIObject PD(*this);
757
Chris Lattner2ebb1782008-08-23 01:48:03 +0000758 // Parse the return type if present.
Chris Lattner5700fab2007-10-07 02:00:24 +0000759 TypeTy *ReturnType = 0;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000760 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000761 if (Tok.is(tok::l_paren))
Fariborz Jahanian7a9c4742007-10-31 23:53:01 +0000762 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump11289f42009-09-09 15:08:12 +0000763
Steve Naroff161a92b2007-10-26 20:53:56 +0000764 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000765 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000766
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000767 // An unnamed colon is valid.
768 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000769 Diag(Tok, diag::err_expected_selector_for_method)
770 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000771 // Skip until we get a ; or {}.
772 SkipUntil(tok::r_brace);
Chris Lattner83f095c2009-03-28 19:18:32 +0000773 return DeclPtrTy();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000774 }
Mike Stump11289f42009-09-09 15:08:12 +0000775
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000776 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattner0ef13522007-10-09 17:51:17 +0000777 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000778 // If attributes exist after the method, parse them.
779 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000780 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000781 MethodAttrs = ParseGNUAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattner5700fab2007-10-07 02:00:24 +0000783 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall28a6aea2009-11-04 02:18:39 +0000784 DeclPtrTy Result
785 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanianb5605172007-11-09 19:52:12 +0000786 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000787 0, CargNames, MethodAttrs,
788 MethodImplKind);
John McCall28a6aea2009-11-04 02:18:39 +0000789 PD.complete(Result);
790 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000791 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000792
Steve Narofff73590d2007-09-27 14:38:14 +0000793 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000794 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000795
Chris Lattner5700fab2007-10-07 02:00:24 +0000796 while (1) {
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000797 Action::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000798
Chris Lattner5700fab2007-10-07 02:00:24 +0000799 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000800 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000801 Diag(Tok, diag::err_expected_colon);
802 break;
803 }
804 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000805
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000806 ArgInfo.Type = 0;
807 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
808 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
809
Chris Lattner5700fab2007-10-07 02:00:24 +0000810 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000811 ArgInfo.ArgAttrs = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +0000812 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000813 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000814
Chris Lattner0ef13522007-10-09 17:51:17 +0000815 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000816 Diag(Tok, diag::err_expected_ident); // missing argument name.
817 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000818 }
Mike Stump11289f42009-09-09 15:08:12 +0000819
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000820 ArgInfo.Name = Tok.getIdentifierInfo();
821 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000822 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000823
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000824 ArgInfos.push_back(ArgInfo);
825 KeyIdents.push_back(SelIdent);
826
Chris Lattner5700fab2007-10-07 02:00:24 +0000827 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000828 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000829 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000830 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000831 break;
832 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000833 }
Mike Stump11289f42009-09-09 15:08:12 +0000834
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000835 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000836
Chris Lattner5700fab2007-10-07 02:00:24 +0000837 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000838 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000839 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000840 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000841 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000842 ConsumeToken();
843 break;
844 }
Chris Lattner5700fab2007-10-07 02:00:24 +0000845 DeclSpec DS;
846 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000847 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000848 Declarator ParmDecl(DS, Declarator::PrototypeContext);
849 ParseDeclarator(ParmDecl);
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000850 CargNames.push_back(ParmDecl);
Chris Lattner5700fab2007-10-07 02:00:24 +0000851 }
Mike Stump11289f42009-09-09 15:08:12 +0000852
Chris Lattner5700fab2007-10-07 02:00:24 +0000853 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000854 // If attributes exist after the method, parse them.
Chris Lattner5700fab2007-10-07 02:00:24 +0000855 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000856 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000857 MethodAttrs = ParseGNUAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000858
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000859 if (KeyIdents.size() == 0)
860 return DeclPtrTy();
Chris Lattner5700fab2007-10-07 02:00:24 +0000861 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
862 &KeyIdents[0]);
John McCall28a6aea2009-11-04 02:18:39 +0000863 DeclPtrTy Result
864 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000865 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000866 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000867 MethodImplKind, isVariadic);
John McCall28a6aea2009-11-04 02:18:39 +0000868 PD.complete(Result);
869 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +0000870}
871
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000872/// objc-protocol-refs:
873/// '<' identifier-list '>'
874///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000875bool Parser::
Chris Lattner83f095c2009-03-28 19:18:32 +0000876ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000877 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
878 bool WarnOnDeclarations,
879 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +0000880 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +0000881
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000882 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +0000883
Chris Lattner3bbae002008-07-26 04:03:38 +0000884 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +0000885
Chris Lattner3bbae002008-07-26 04:03:38 +0000886 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +0000887 if (Tok.is(tok::code_completion)) {
888 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
889 ProtocolIdents.size());
890 ConsumeToken();
891 }
892
Chris Lattner3bbae002008-07-26 04:03:38 +0000893 if (Tok.isNot(tok::identifier)) {
894 Diag(Tok, diag::err_expected_ident);
895 SkipUntil(tok::greater);
896 return true;
897 }
898 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
899 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000900 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +0000901 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000902
Chris Lattner3bbae002008-07-26 04:03:38 +0000903 if (Tok.isNot(tok::comma))
904 break;
905 ConsumeToken();
906 }
Mike Stump11289f42009-09-09 15:08:12 +0000907
Chris Lattner3bbae002008-07-26 04:03:38 +0000908 // Consume the '>'.
909 if (Tok.isNot(tok::greater)) {
910 Diag(Tok, diag::err_expected_greater);
911 return true;
912 }
Mike Stump11289f42009-09-09 15:08:12 +0000913
Chris Lattner3bbae002008-07-26 04:03:38 +0000914 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +0000915
Chris Lattner3bbae002008-07-26 04:03:38 +0000916 // Convert the list of protocols identifiers into a list of protocol decls.
917 Actions.FindProtocolDeclaration(WarnOnDeclarations,
918 &ProtocolIdents[0], ProtocolIdents.size(),
919 Protocols);
920 return false;
921}
922
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000923/// objc-class-instance-variables:
924/// '{' objc-instance-variable-decl-list[opt] '}'
925///
926/// objc-instance-variable-decl-list:
927/// objc-visibility-spec
928/// objc-instance-variable-decl ';'
929/// ';'
930/// objc-instance-variable-decl-list objc-visibility-spec
931/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
932/// objc-instance-variable-decl-list ';'
933///
934/// objc-visibility-spec:
935/// @private
936/// @protected
937/// @public
Steve Naroff00433d32007-08-21 21:17:12 +0000938/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000939///
940/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +0000941/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000942///
Chris Lattner83f095c2009-03-28 19:18:32 +0000943void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff33a1e802007-10-29 21:38:07 +0000944 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000945 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner83f095c2009-03-28 19:18:32 +0000946 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000947
Douglas Gregor45a33ec2009-01-12 18:45:55 +0000948 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000949
Steve Naroff00433d32007-08-21 21:17:12 +0000950 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +0000951
Fariborz Jahanian2dfcec12008-04-29 23:03:51 +0000952 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroff00433d32007-08-21 21:17:12 +0000953 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +0000954 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000955 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +0000956
Steve Naroff00433d32007-08-21 21:17:12 +0000957 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +0000958 if (Tok.is(tok::semi)) {
Chris Lattner3e4fac72009-11-06 06:40:12 +0000959 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner3c7b86f2009-12-06 17:36:05 +0000960 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +0000961 ConsumeToken();
962 continue;
963 }
Mike Stump11289f42009-09-09 15:08:12 +0000964
Steve Naroff00433d32007-08-21 21:17:12 +0000965 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +0000966 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +0000967 ConsumeToken(); // eat the @ sign
Steve Naroff7c348172007-08-23 18:16:40 +0000968 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +0000969 case tok::objc_private:
970 case tok::objc_public:
971 case tok::objc_protected:
972 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +0000973 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +0000974 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000975 continue;
Steve Naroff00433d32007-08-21 21:17:12 +0000976 default:
977 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +0000978 continue;
979 }
980 }
Mike Stump11289f42009-09-09 15:08:12 +0000981
John McCallcfefb6d2009-11-03 02:38:08 +0000982 struct ObjCIvarCallback : FieldCallback {
983 Parser &P;
984 DeclPtrTy IDecl;
985 tok::ObjCKeywordKind visibility;
986 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
987
988 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
989 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
990 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
991 }
992
993 DeclPtrTy invoke(FieldDeclarator &FD) {
994 // Install the declarator into the interface decl.
995 DeclPtrTy Field
996 = P.Actions.ActOnIvar(P.CurScope,
997 FD.D.getDeclSpec().getSourceRange().getBegin(),
998 IDecl, FD.D, FD.BitfieldSize, visibility);
999 AllIvarDecls.push_back(Field);
1000 return Field;
1001 }
1002 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1003
Chris Lattnera12405b2008-04-10 06:46:29 +00001004 // Parse all the comma separated declarators.
1005 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +00001006 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001007
Chris Lattner0ef13522007-10-09 17:51:17 +00001008 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001009 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001010 } else {
1011 Diag(Tok, diag::err_expected_semi_decl_list);
1012 // Skip to end of block or statement
1013 SkipUntil(tok::r_brace, true, true);
1014 }
1015 }
Steve Naroff33a1e802007-10-29 21:38:07 +00001016 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001017 // Call ActOnFields() even if we don't have any decls. This is useful
1018 // for code rewriting tools that need to be aware of the empty list.
1019 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +00001020 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001021 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001022 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001023}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001024
1025/// objc-protocol-declaration:
1026/// objc-protocol-definition
1027/// objc-protocol-forward-reference
1028///
1029/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001030/// @protocol identifier
1031/// objc-protocol-refs[opt]
1032/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001033/// @end
1034///
1035/// objc-protocol-forward-reference:
1036/// @protocol identifier-list ';'
1037///
1038/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001039/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001040/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattner83f095c2009-03-28 19:18:32 +00001041Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1042 AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +00001043 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001044 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1045 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001046
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001047 if (Tok.is(tok::code_completion)) {
1048 Actions.CodeCompleteObjCProtocolDecl(CurScope);
1049 ConsumeToken();
1050 }
1051
Chris Lattner0ef13522007-10-09 17:51:17 +00001052 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001053 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001054 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001055 }
1056 // Save the protocol name, then consume it.
1057 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1058 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001059
Chris Lattner0ef13522007-10-09 17:51:17 +00001060 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001061 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001062 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001063 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001064 attrList);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001065 }
Mike Stump11289f42009-09-09 15:08:12 +00001066
Chris Lattner0ef13522007-10-09 17:51:17 +00001067 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001068 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1069 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1070
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001071 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001072 while (1) {
1073 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001074 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001075 Diag(Tok, diag::err_expected_ident);
1076 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +00001077 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001078 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001079 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1080 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001081 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001082
Chris Lattner0ef13522007-10-09 17:51:17 +00001083 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001084 break;
1085 }
1086 // Consume the ';'.
1087 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattner83f095c2009-03-28 19:18:32 +00001088 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001089
Steve Naroff93eb5f12007-10-10 17:32:04 +00001090 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001091 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001092 ProtocolRefs.size(),
1093 attrList);
Chris Lattnerd7352d62008-07-21 22:17:28 +00001094 }
Mike Stump11289f42009-09-09 15:08:12 +00001095
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001096 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001097 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001098
Chris Lattner83f095c2009-03-28 19:18:32 +00001099 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001100 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001101 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001102 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1103 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +00001104 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001105
Chris Lattner83f095c2009-03-28 19:18:32 +00001106 DeclPtrTy ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001107 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001108 ProtocolRefs.data(),
1109 ProtocolRefs.size(),
Daniel Dunbar26e2ab42008-09-26 04:48:09 +00001110 EndProtoLoc, attrList);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001111 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001112 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001113}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001114
1115/// objc-implementation:
1116/// objc-class-implementation-prologue
1117/// objc-category-implementation-prologue
1118///
1119/// objc-class-implementation-prologue:
1120/// @implementation identifier objc-superclass[opt]
1121/// objc-class-instance-variables[opt]
1122///
1123/// objc-category-implementation-prologue:
1124/// @implementation identifier ( identifier )
Chris Lattner83f095c2009-03-28 19:18:32 +00001125Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001126 SourceLocation atLoc) {
1127 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1128 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1129 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001130
Douglas Gregor49c22a72009-11-18 16:26:39 +00001131 // Code completion after '@implementation'.
1132 if (Tok.is(tok::code_completion)) {
1133 Actions.CodeCompleteObjCImplementationDecl(CurScope);
1134 ConsumeToken();
1135 }
1136
Chris Lattner0ef13522007-10-09 17:51:17 +00001137 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001138 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001139 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001140 }
1141 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001142 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001143 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001144
1145 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001146 // we have a category implementation.
1147 SourceLocation lparenLoc = ConsumeParen();
1148 SourceLocation categoryLoc, rparenLoc;
1149 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001150
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001151 if (Tok.is(tok::code_completion)) {
1152 Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
1153 ConsumeToken();
1154 }
1155
Chris Lattner0ef13522007-10-09 17:51:17 +00001156 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001157 categoryId = Tok.getIdentifierInfo();
1158 categoryLoc = ConsumeToken();
1159 } else {
1160 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001161 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001162 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001163 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001164 Diag(Tok, diag::err_expected_rparen);
1165 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001166 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001167 }
1168 rparenLoc = ConsumeParen();
Chris Lattner83f095c2009-03-28 19:18:32 +00001169 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001170 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001171 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001172 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001173 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001174 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001175 }
1176 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001177 SourceLocation superClassLoc;
1178 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001179 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001180 // We have a super class
1181 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001182 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001183 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001184 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001185 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001186 superClassId = Tok.getIdentifierInfo();
1187 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001188 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001189 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001190 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001191 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001192
Steve Naroff33a1e802007-10-29 21:38:07 +00001193 if (Tok.is(tok::l_brace)) // we have ivars
1194 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001195 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001196 PendingObjCImpDecl.push_back(ObjCImpDecl);
1197
Chris Lattner83f095c2009-03-28 19:18:32 +00001198 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001199}
Steve Naroff33a1e802007-10-29 21:38:07 +00001200
Ted Kremenekc7c64312010-01-07 01:20:12 +00001201Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001202 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1203 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattner83f095c2009-03-28 19:18:32 +00001204 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001205 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001206 if (ObjCImpDecl) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001207 Actions.ActOnAtEnd(atEnd, ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001208 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001209 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001210 }
Ted Kremenekc7c64312010-01-07 01:20:12 +00001211 else {
1212 // missing @implementation
1213 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1214 }
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001215 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001216}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001217
Fariborz Jahanianbc02a102009-11-17 17:15:16 +00001218Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001219 if (PendingObjCImpDecl.empty())
1220 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1221 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
Ted Kremenekc7c64312010-01-07 01:20:12 +00001222 Actions.ActOnAtEnd(SourceRange(), ImpDecl);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001223 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1224}
1225
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001226/// compatibility-alias-decl:
1227/// @compatibility_alias alias-name class-name ';'
1228///
Chris Lattner83f095c2009-03-28 19:18:32 +00001229Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001230 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1231 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1232 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001233 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001234 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001235 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001236 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001237 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1238 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001239 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001240 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001241 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001242 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001243 IdentifierInfo *classId = Tok.getIdentifierInfo();
1244 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1245 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001246 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattner83f095c2009-03-28 19:18:32 +00001247 return DeclPtrTy();
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001248 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001249 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1250 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001251}
1252
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001253/// property-synthesis:
1254/// @synthesize property-ivar-list ';'
1255///
1256/// property-ivar-list:
1257/// property-ivar
1258/// property-ivar-list ',' property-ivar
1259///
1260/// property-ivar:
1261/// identifier
1262/// identifier '=' identifier
1263///
Chris Lattner83f095c2009-03-28 19:18:32 +00001264Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001265 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1266 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001267 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001268
Douglas Gregor88e72a02009-11-18 19:45:45 +00001269 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001270 if (Tok.is(tok::code_completion)) {
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001271 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
Douglas Gregor5d649882009-11-18 22:32:06 +00001272 ConsumeToken();
1273 }
1274
Douglas Gregor88e72a02009-11-18 19:45:45 +00001275 if (Tok.isNot(tok::identifier)) {
1276 Diag(Tok, diag::err_synthesized_property_name);
1277 SkipUntil(tok::semi);
1278 return DeclPtrTy();
1279 }
1280
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001281 IdentifierInfo *propertyIvar = 0;
1282 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1283 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattner0ef13522007-10-09 17:51:17 +00001284 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001285 // property '=' ivar-name
1286 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001287
1288 if (Tok.is(tok::code_completion)) {
1289 Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1290 ObjCImpDecl);
1291 ConsumeToken();
1292 }
1293
Chris Lattner0ef13522007-10-09 17:51:17 +00001294 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001295 Diag(Tok, diag::err_expected_ident);
1296 break;
1297 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001298 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001299 ConsumeToken(); // consume ivar-name
1300 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001301 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1302 propertyId, propertyIvar);
Chris Lattner0ef13522007-10-09 17:51:17 +00001303 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001304 break;
1305 ConsumeToken(); // consume ','
1306 }
Douglas Gregor88e72a02009-11-18 19:45:45 +00001307 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001308 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregor88e72a02009-11-18 19:45:45 +00001309 SkipUntil(tok::semi);
1310 }
Fariborz Jahanian95239112009-11-06 21:48:47 +00001311 else
1312 ConsumeToken(); // consume ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001313 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001314}
1315
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001316/// property-dynamic:
1317/// @dynamic property-list
1318///
1319/// property-list:
1320/// identifier
1321/// property-list ',' identifier
1322///
Chris Lattner83f095c2009-03-28 19:18:32 +00001323Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001324 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1325 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1326 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001327 while (true) {
1328 if (Tok.is(tok::code_completion)) {
1329 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1330 ConsumeToken();
1331 }
1332
1333 if (Tok.isNot(tok::identifier)) {
1334 Diag(Tok, diag::err_expected_ident);
1335 SkipUntil(tok::semi);
1336 return DeclPtrTy();
1337 }
1338
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001339 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1340 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1341 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1342 propertyId, 0);
1343
Chris Lattner0ef13522007-10-09 17:51:17 +00001344 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001345 break;
1346 ConsumeToken(); // consume ','
1347 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001348 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001349 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattner83f095c2009-03-28 19:18:32 +00001350 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001351}
Mike Stump11289f42009-09-09 15:08:12 +00001352
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001353/// objc-throw-statement:
1354/// throw expression[opt];
1355///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001356Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001357 OwningExprResult Res(Actions);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001358 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001359 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001360 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001361 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001362 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001363 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001364 }
1365 }
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001366 ConsumeToken(); // consume ';'
Steve Naroff5ee2c022009-02-11 20:05:44 +00001367 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001368}
1369
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001370/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001371/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001372///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001373Parser::OwningStmtResult
1374Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001375 ConsumeToken(); // consume synchronized
1376 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001377 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001378 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001379 }
1380 ConsumeParen(); // '('
Sebastian Redl59b5e512008-12-11 21:36:32 +00001381 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001382 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001383 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001384 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001385 }
1386 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001387 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001388 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001389 }
1390 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001391 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001392 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001393 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001394 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001395 // Enter a scope to hold everything within the compound stmt. Compound
1396 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001397 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001398
Sebastian Redl042ad952008-12-11 19:30:53 +00001399 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001400
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001401 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001402 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001403 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl726a0d92009-02-05 15:02:23 +00001404 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001405}
1406
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001407/// objc-try-catch-statement:
1408/// @try compound-statement objc-catch-list[opt]
1409/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1410///
1411/// objc-catch-list:
1412/// @catch ( parameter-declaration ) compound-statement
1413/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1414/// catch-parameter-declaration:
1415/// parameter-declaration
1416/// '...' [OBJC2]
1417///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001418Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001419 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001420
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001421 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001422 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001423 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001424 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001425 }
Sebastian Redlc13f2682008-12-09 20:22:58 +00001426 OwningStmtResult CatchStmts(Actions);
1427 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001428 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl042ad952008-12-11 19:30:53 +00001429 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001430 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001431 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001432 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001433
Chris Lattner0ef13522007-10-09 17:51:17 +00001434 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001435 // At this point, we need to lookahead to determine if this @ is the start
1436 // of an @catch or @finally. We don't want to consume the @ token if this
1437 // is an @try or @encode or something else.
1438 Token AfterAt = GetLookAheadToken(1);
1439 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1440 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1441 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001442
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001443 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001444 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001445 DeclPtrTy FirstPart;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001446 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001447 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001448 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001449 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001450 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001451 DeclSpec DS;
1452 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001453 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001454 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001455 // we must accept either 'declarator' or 'abstract-declarator' here.
1456 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1457 ParseDeclarator(ParmDecl);
1458
1459 // Inform the actions module about the parameter declarator, so it
1460 // gets added to the current scope.
1461 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001462 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001463 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001464
Steve Naroff65a00892009-04-07 22:56:58 +00001465 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001466
Steve Naroff65a00892009-04-07 22:56:58 +00001467 if (Tok.is(tok::r_paren))
1468 RParenLoc = ConsumeParen();
1469 else // Skip over garbage, until we get to ')'. Eat the ')'.
1470 SkipUntil(tok::r_paren, true, false);
1471
Sebastian Redlc13f2682008-12-09 20:22:58 +00001472 OwningStmtResult CatchBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001473 if (Tok.is(tok::l_brace))
1474 CatchBody = ParseCompoundStatementBody();
1475 else
1476 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001477 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001478 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001479 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff371b8fb2009-03-03 19:52:17 +00001480 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001481 move(CatchStmts));
Steve Naroffe6016792008-02-05 21:27:35 +00001482 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001483 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1484 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001485 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001486 }
1487 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001488 } else {
1489 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001490 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001491 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001492
Sebastian Redlc13f2682008-12-09 20:22:58 +00001493 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001494 if (Tok.is(tok::l_brace))
1495 FinallyBody = ParseCompoundStatementBody();
1496 else
1497 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001498 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001499 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001500 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +00001501 move(FinallyBody));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001502 catch_or_finally_seen = true;
1503 break;
1504 }
1505 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001506 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001507 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001508 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001509 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001510 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1511 move(FinallyStmt));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001512}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001513
Steve Naroff09bf8152007-09-06 21:24:23 +00001514/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001515///
Chris Lattner83f095c2009-03-28 19:18:32 +00001516Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1517 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001518
Chris Lattnereae6cb62009-03-05 08:00:35 +00001519 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1520 PP.getSourceManager(),
1521 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001522
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001523 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001524 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001525 if (ObjCImpDecl) {
1526 Diag(Tok, diag::warn_semicolon_before_method_body)
Chris Lattner3c7b86f2009-12-06 17:36:05 +00001527 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001528 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001529 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001530 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001531
Steve Naroffbb875722007-11-11 19:54:21 +00001532 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001533 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001534 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001535
Steve Naroffbb875722007-11-11 19:54:21 +00001536 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1537 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001538
Steve Naroffbb875722007-11-11 19:54:21 +00001539 // If we didn't find the '{', bail out.
1540 if (Tok.isNot(tok::l_brace))
Chris Lattner83f095c2009-03-28 19:18:32 +00001541 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001542 }
Steve Naroffbb875722007-11-11 19:54:21 +00001543 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001544
Steve Naroffbb875722007-11-11 19:54:21 +00001545 // Enter a scope for the method body.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001546 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001547
Steve Naroffbb875722007-11-11 19:54:21 +00001548 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001549 // specified Declarator for the method.
Steve Naroff70f41d62009-02-28 16:59:13 +00001550 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001551
1552 OwningStmtResult FnBody(ParseCompoundStatementBody());
1553
Steve Naroffbb875722007-11-11 19:54:21 +00001554 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001555 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001556 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1557 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001558
Steve Naroffb94d7f62009-03-02 22:00:56 +00001559 // TODO: Pass argument information.
1560 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump11289f42009-09-09 15:08:12 +00001561
Steve Naroffbb875722007-11-11 19:54:21 +00001562 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001563 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001564
Steve Naroff7b8fa472007-11-13 23:01:27 +00001565 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001566}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001567
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001568Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001569 if (Tok.is(tok::code_completion)) {
1570 Actions.CodeCompleteObjCAtStatement(CurScope);
1571 ConsumeToken();
1572 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001573 }
1574
1575 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001576 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001577
1578 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001579 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001580
1581 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001582 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001583
Sebastian Redl90893182008-12-11 22:33:27 +00001584 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001585 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001586 // If the expression is invalid, skip ahead to the next semicolon. Not
1587 // doing this opens us up to the possibility of infinite loops if
1588 // ParseExpression does not consume any tokens.
1589 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001590 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001591 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001592
Steve Naroffe6016792008-02-05 21:27:35 +00001593 // Otherwise, eat the semicolon.
1594 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlssonafb2dad2009-12-16 02:09:40 +00001595 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Steve Naroffe6016792008-02-05 21:27:35 +00001596}
1597
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001598Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001599 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001600 case tok::code_completion:
1601 Actions.CodeCompleteObjCAtExpression(CurScope);
1602 ConsumeToken();
1603 return ExprError();
1604
Chris Lattnere002fbe2007-12-12 01:04:12 +00001605 case tok::string_literal: // primary-expression: string-literal
1606 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001607 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001608 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001609 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001610 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001611
Chris Lattner197a3012008-08-05 06:19:09 +00001612 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1613 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001614 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001615 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001616 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001617 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001618 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001619 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001620 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001621 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001622 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001623}
1624
Mike Stump11289f42009-09-09 15:08:12 +00001625/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001626/// '[' objc-receiver objc-message-args ']'
1627///
1628/// objc-receiver:
1629/// expression
1630/// class-name
1631/// type-name
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001632Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001633 assert(Tok.is(tok::l_square) && "'[' expected");
1634 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1635
1636 // Parse receiver
Chris Lattnere10c6da2008-01-25 19:25:00 +00001637 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner8f697062008-01-25 18:59:06 +00001638 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00001639 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1640 SourceLocation NameLoc = ConsumeToken();
1641 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1642 ExprArg(Actions));
1643 }
Chris Lattner8f697062008-01-25 18:59:06 +00001644 }
1645
Sebastian Redl59b5e512008-12-11 21:36:32 +00001646 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001647 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00001648 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001649 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00001650 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001651
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001652 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001653 0, move(Res));
Chris Lattner8f697062008-01-25 18:59:06 +00001654}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001655
Chris Lattner8f697062008-01-25 18:59:06 +00001656/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1657/// the rest of a message expression.
Mike Stump11289f42009-09-09 15:08:12 +00001658///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001659/// objc-message-args:
1660/// objc-selector
1661/// objc-keywordarg-list
1662///
1663/// objc-keywordarg-list:
1664/// objc-keywordarg
1665/// objc-keywordarg-list objc-keywordarg
1666///
Mike Stump11289f42009-09-09 15:08:12 +00001667/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001668/// selector-name[opt] ':' objc-keywordexpr
1669///
1670/// objc-keywordexpr:
1671/// nonempty-expr-list
1672///
1673/// nonempty-expr-list:
1674/// assignment-expression
1675/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001676///
1677Parser::OwningExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00001678Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff9e4ac112008-11-19 15:54:23 +00001679 SourceLocation NameLoc,
Chris Lattner8f697062008-01-25 18:59:06 +00001680 IdentifierInfo *ReceiverName,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001681 ExprArg ReceiverExpr) {
Steve Naroffeae65032009-11-07 02:08:14 +00001682 if (Tok.is(tok::code_completion)) {
1683 if (ReceiverName)
Douglas Gregor1b605f72009-11-19 01:08:35 +00001684 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1685 0, 0);
Steve Naroffeae65032009-11-07 02:08:14 +00001686 else
Douglas Gregor1b605f72009-11-19 01:08:35 +00001687 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1688 0, 0);
Steve Naroffeae65032009-11-07 02:08:14 +00001689 ConsumeToken();
1690 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00001691
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001692 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001693 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001694 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00001695
Anders Carlsson978f08d2009-02-14 18:21:46 +00001696 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00001697
Steve Narofff73590d2007-09-27 14:38:14 +00001698 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00001699 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00001700
Chris Lattner0ef13522007-10-09 17:51:17 +00001701 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001702 while (1) {
1703 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00001704 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00001705
Chris Lattner0ef13522007-10-09 17:51:17 +00001706 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001707 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00001708 // We must manually skip to a ']', otherwise the expression skipper will
1709 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1710 // the enclosing expression.
1711 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001712 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001713 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001714
Steve Narofff73590d2007-09-27 14:38:14 +00001715 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001716 /// Parse the expression after ':'
Sebastian Redl59b5e512008-12-11 21:36:32 +00001717 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001718 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001719 // We must manually skip to a ']', otherwise the expression skipper will
1720 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1721 // the enclosing expression.
1722 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001723 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00001724 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001725
Steve Naroff486760a2007-09-17 20:25:27 +00001726 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001727 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001728
Douglas Gregor1b605f72009-11-19 01:08:35 +00001729 // Code completion after each argument.
1730 if (Tok.is(tok::code_completion)) {
1731 if (ReceiverName)
1732 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1733 KeyIdents.data(),
1734 KeyIdents.size());
1735 else
1736 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1737 KeyIdents.data(),
1738 KeyIdents.size());
1739 ConsumeToken();
1740 }
1741
Steve Naroff486760a2007-09-17 20:25:27 +00001742 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00001743 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001744 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001745 break;
1746 // We have a selector or a colon, continue parsing.
1747 }
1748 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00001749 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001750 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00001751 /// Parse the expression after ','
Sebastian Redl59b5e512008-12-11 21:36:32 +00001752 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001753 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001754 // We must manually skip to a ']', otherwise the expression skipper will
1755 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1756 // the enclosing expression.
1757 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001758 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001759 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001760
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001761 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001762 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001763 }
1764 } else if (!selIdent) {
1765 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001766
Chris Lattner197a3012008-08-05 06:19:09 +00001767 // We must manually skip to a ']', otherwise the expression skipper will
1768 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1769 // the enclosing expression.
1770 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001771 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001772 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001773
Chris Lattner0ef13522007-10-09 17:51:17 +00001774 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001775 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00001776 // We must manually skip to a ']', otherwise the expression skipper will
1777 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1778 // the enclosing expression.
1779 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001780 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001781 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001782
Chris Lattner8f697062008-01-25 18:59:06 +00001783 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001784
Steve Naroffe61bfa82007-10-05 18:42:47 +00001785 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00001786 if (nKeys == 0)
1787 KeyIdents.push_back(selIdent);
1788 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001789
Chris Lattner5700fab2007-10-07 02:00:24 +00001790 // We've just parsed a keyword message.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001791 if (ReceiverName)
1792 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump11289f42009-09-09 15:08:12 +00001793 LBracLoc, NameLoc, SelectorLoc,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001794 RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001795 KeyExprs.take(), KeyExprs.size()));
1796 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001797 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001798 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001799}
1800
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001801Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redld65cea82008-12-11 22:51:44 +00001802 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001803 if (Res.isInvalid()) return move(Res);
1804
Chris Lattnere002fbe2007-12-12 01:04:12 +00001805 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1806 // expressions. At this point, we know that the only valid thing that starts
1807 // with '@' is an @"".
1808 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00001809 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00001810 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001811 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001812
Chris Lattnere002fbe2007-12-12 01:04:12 +00001813 while (Tok.is(tok::at)) {
1814 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00001815
Sebastian Redlc13f2682008-12-09 20:22:58 +00001816 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001817 if (!isTokenStringLiteral())
1818 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001819
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001820 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001821 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001822 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001823
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001824 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00001825 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001826
1827 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1828 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00001829}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001830
1831/// objc-encode-expression:
1832/// @encode ( type-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001833Parser::OwningExprResult
1834Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00001835 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001836
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001837 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001838
Chris Lattner197a3012008-08-05 06:19:09 +00001839 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001840 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1841
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001842 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001843
Douglas Gregor220cac52009-02-18 17:45:20 +00001844 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001845
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001846 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001847
Douglas Gregor220cac52009-02-18 17:45:20 +00001848 if (Ty.isInvalid())
1849 return ExprError();
1850
Mike Stump11289f42009-09-09 15:08:12 +00001851 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00001852 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001853}
Anders Carlssone01493d2007-08-23 15:25:28 +00001854
1855/// objc-protocol-expression
1856/// @protocol ( protocol-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001857Parser::OwningExprResult
1858Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00001859 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001860
Chris Lattner197a3012008-08-05 06:19:09 +00001861 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001862 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1863
Anders Carlssone01493d2007-08-23 15:25:28 +00001864 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001865
Chris Lattner197a3012008-08-05 06:19:09 +00001866 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001867 return ExprError(Diag(Tok, diag::err_expected_ident));
1868
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001869 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00001870 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001871
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001872 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00001873
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001874 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1875 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00001876}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001877
1878/// objc-selector-expression
1879/// @selector '(' objc-keyword-selector ')'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001880Parser::OwningExprResult
1881Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001882 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001883
Chris Lattner197a3012008-08-05 06:19:09 +00001884 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001885 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1886
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001887 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001888 SourceLocation LParenLoc = ConsumeParen();
1889 SourceLocation sLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001890 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001891 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1892 return ExprError(Diag(Tok, diag::err_expected_ident));
1893
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001894 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00001895 unsigned nColons = 0;
1896 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001897 while (1) {
Chris Lattner197a3012008-08-05 06:19:09 +00001898 if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001899 return ExprError(Diag(Tok, diag::err_expected_colon));
1900
Chris Lattner5e530bc2007-12-27 19:57:00 +00001901 nColons++;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001902 ConsumeToken(); // Eat the ':'.
1903 if (Tok.is(tok::r_paren))
1904 break;
1905 // Check for another keyword selector.
1906 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001907 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001908 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001909 if (!SelIdent && Tok.isNot(tok::colon))
1910 break;
1911 }
Steve Naroff152dd812007-12-05 22:21:29 +00001912 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001913 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00001914 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001915 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1916 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00001917 }