blob: fa3c35683ee449a24c1ff194f47803d652ee29c0 [file] [log] [blame]
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerda59c2f2006-11-05 02:08:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Steve Narofff1bc45b2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian9e63b982007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner60f36222009-01-29 05:15:15 +000017#include "clang/Parse/ParseDiagnostic.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000018#include "llvm/ADT/SmallVector.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000019using namespace clang;
20
21
Chris Lattner3a907162008-12-08 21:53:24 +000022/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000023/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroffe0933392007-10-29 21:39:29 +000025/// [OBJC] objc-class-declaration
26/// [OBJC] objc-alias-declaration
27/// [OBJC] objc-protocol-definition
28/// [OBJC] objc-method-definition
29/// [OBJC] '@' 'end'
Chris Lattner83f095c2009-03-28 19:18:32 +000030Parser::DeclPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000032
Douglas Gregorf48706c2009-12-07 09:27:33 +000033 if (Tok.is(tok::code_completion)) {
34 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, false);
35 ConsumeToken();
36 }
37
Steve Naroff7c348172007-08-23 18:16:40 +000038 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000039 case tok::objc_class:
40 return ParseObjCAtClassDeclaration(AtLoc);
41 case tok::objc_interface:
42 return ParseObjCAtInterfaceDeclaration(AtLoc);
43 case tok::objc_protocol:
44 return ParseObjCAtProtocolDeclaration(AtLoc);
45 case tok::objc_implementation:
46 return ParseObjCAtImplementationDeclaration(AtLoc);
47 case tok::objc_end:
48 return ParseObjCAtEndDeclaration(AtLoc);
49 case tok::objc_compatibility_alias:
50 return ParseObjCAtAliasDeclaration(AtLoc);
51 case tok::objc_synthesize:
52 return ParseObjCPropertySynthesize(AtLoc);
53 case tok::objc_dynamic:
54 return ParseObjCPropertyDynamic(AtLoc);
55 default:
56 Diag(AtLoc, diag::err_unexpected_at);
57 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +000058 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +000059 }
60}
61
62///
Mike Stump11289f42009-09-09 15:08:12 +000063/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000064/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000065///
Chris Lattner83f095c2009-03-28 19:18:32 +000066Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000067 ConsumeToken(); // the identifier "class"
Chris Lattner23b7eb62007-06-15 23:05:46 +000068 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremeneka26da852009-11-17 23:12:20 +000069 llvm::SmallVector<SourceLocation, 8> ClassLocs;
70
Mike Stump11289f42009-09-09 15:08:12 +000071
Chris Lattnerda59c2f2006-11-05 02:08:13 +000072 while (1) {
Chris Lattner0ef13522007-10-09 17:51:17 +000073 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +000074 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000075 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +000076 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +000077 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +000078 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +000079 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000080 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +000081
Chris Lattner0ef13522007-10-09 17:51:17 +000082 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +000083 break;
Mike Stump11289f42009-09-09 15:08:12 +000084
Chris Lattnerda59c2f2006-11-05 02:08:13 +000085 ConsumeToken();
86 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattnerda59c2f2006-11-05 02:08:13 +000088 // Consume the ';'.
89 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattner83f095c2009-03-28 19:18:32 +000090 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +000091
Ted Kremeneka26da852009-11-17 23:12:20 +000092 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
93 ClassLocs.data(),
94 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000095}
96
Steve Naroff1eb1ad62007-08-20 21:31:48 +000097///
98/// objc-interface:
99/// objc-class-interface-attributes[opt] objc-class-interface
100/// objc-category-interface
101///
102/// objc-class-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000103/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000104/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000105/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000106/// objc-interface-decl-list
107/// @end
108///
109/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000110/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000111/// objc-protocol-refs[opt]
112/// objc-interface-decl-list
113/// @end
114///
115/// objc-superclass:
116/// ':' identifier
117///
118/// objc-class-interface-attributes:
119/// __attribute__((visibility("default")))
120/// __attribute__((visibility("hidden")))
121/// __attribute__((deprecated))
122/// __attribute__((unavailable))
123/// __attribute__((objc_exception)) - used by NSException on 64-bit
124///
Chris Lattner83f095c2009-03-28 19:18:32 +0000125Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000126 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +0000127 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000128 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
129 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000130
Douglas Gregor49c22a72009-11-18 16:26:39 +0000131 // Code completion after '@interface'.
132 if (Tok.is(tok::code_completion)) {
133 Actions.CodeCompleteObjCInterfaceDecl(CurScope);
134 ConsumeToken();
135 }
136
Chris Lattner0ef13522007-10-09 17:51:17 +0000137 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000138 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000139 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000140 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000141
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000142 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000143 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000144 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000145
Chris Lattner0ef13522007-10-09 17:51:17 +0000146 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000147 SourceLocation lparenLoc = ConsumeParen();
148 SourceLocation categoryLoc, rparenLoc;
149 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000150
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000151 if (Tok.is(tok::code_completion)) {
152 Actions.CodeCompleteObjCInterfaceCategory(CurScope, nameId);
153 ConsumeToken();
154 }
155
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000156 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000157 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000158 categoryId = Tok.getIdentifierInfo();
159 categoryLoc = ConsumeToken();
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000160 } else if (!getLang().ObjC2) {
161 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000162 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000163 }
Chris Lattner0ef13522007-10-09 17:51:17 +0000164 if (Tok.isNot(tok::r_paren)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000165 Diag(Tok, diag::err_expected_rparen);
166 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +0000167 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000168 }
169 rparenLoc = ConsumeParen();
Mike Stump11289f42009-09-09 15:08:12 +0000170
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000171 // Next, we need to check for any protocol references.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000172 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000173 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000174 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerb1f3c942008-07-26 04:07:02 +0000175 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000176 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
177 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000178 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000179
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000180 if (attrList) // categories don't support attributes.
181 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump11289f42009-09-09 15:08:12 +0000182
Jay Foad7d0479f2009-05-21 09:52:38 +0000183 DeclPtrTy CategoryType =
Mike Stump11289f42009-09-09 15:08:12 +0000184 Actions.ActOnStartCategoryInterface(atLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000185 nameId, nameLoc,
186 categoryId, categoryLoc,
187 ProtocolRefs.data(),
188 ProtocolRefs.size(),
189 EndProtoLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000190
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000191 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000192 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000193 }
194 // Parse a class interface.
195 IdentifierInfo *superClassId = 0;
196 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000197
Chris Lattner0ef13522007-10-09 17:51:17 +0000198 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000199 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000200
201 // Code completion of superclass names.
202 if (Tok.is(tok::code_completion)) {
203 Actions.CodeCompleteObjCSuperclass(CurScope, nameId);
204 ConsumeToken();
205 }
206
Chris Lattner0ef13522007-10-09 17:51:17 +0000207 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000208 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000209 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000210 }
211 superClassId = Tok.getIdentifierInfo();
212 superClassLoc = ConsumeToken();
213 }
214 // Next, we need to check for any protocol references.
Chris Lattner83f095c2009-03-28 19:18:32 +0000215 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000216 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
217 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000218 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000219 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
220 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000221 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000222
223 DeclPtrTy ClsType =
224 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000225 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000226 ProtocolRefs.data(), ProtocolRefs.size(),
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000227 EndProtoLoc, attrList);
Mike Stump11289f42009-09-09 15:08:12 +0000228
Chris Lattner0ef13522007-10-09 17:51:17 +0000229 if (Tok.is(tok::l_brace))
Steve Naroff33a1e802007-10-29 21:38:07 +0000230 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000231
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000232 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000233 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000234}
235
John McCall064d77b2009-12-03 22:31:13 +0000236/// The Objective-C property callback. This should be defined where
237/// it's used, but instead it's been lifted to here to support VS2005.
238struct Parser::ObjCPropertyCallback : FieldCallback {
239 Parser &P;
240 DeclPtrTy IDecl;
241 llvm::SmallVectorImpl<DeclPtrTy> &Props;
242 ObjCDeclSpec &OCDS;
243 SourceLocation AtLoc;
244 tok::ObjCKeywordKind MethodImplKind;
245
246 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
247 llvm::SmallVectorImpl<DeclPtrTy> &Props,
248 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
249 tok::ObjCKeywordKind MethodImplKind) :
250 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
251 MethodImplKind(MethodImplKind) {
252 }
253
254 DeclPtrTy invoke(FieldDeclarator &FD) {
255 if (FD.D.getIdentifier() == 0) {
256 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
257 << FD.D.getSourceRange();
258 return DeclPtrTy();
259 }
260 if (FD.BitfieldSize) {
261 P.Diag(AtLoc, diag::err_objc_property_bitfield)
262 << FD.D.getSourceRange();
263 return DeclPtrTy();
264 }
265
266 // Install the property declarator into interfaceDecl.
267 IdentifierInfo *SelName =
268 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
269
270 Selector GetterSel =
271 P.PP.getSelectorTable().getNullarySelector(SelName);
272 IdentifierInfo *SetterName = OCDS.getSetterName();
273 Selector SetterSel;
274 if (SetterName)
275 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
276 else
277 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
278 P.PP.getSelectorTable(),
279 FD.D.getIdentifier());
280 bool isOverridingProperty = false;
281 DeclPtrTy Property =
282 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
283 GetterSel, SetterSel, IDecl,
284 &isOverridingProperty,
285 MethodImplKind);
286 if (!isOverridingProperty)
287 Props.push_back(Property);
288
289 return Property;
290 }
291};
292
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000293/// objc-interface-decl-list:
294/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000295/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000296/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000297/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000298/// objc-interface-decl-list declaration
299/// objc-interface-decl-list ';'
300///
Steve Naroff99264b42007-08-22 16:35:03 +0000301/// objc-method-requirement: [OBJC2]
302/// @required
303/// @optional
304///
Chris Lattner83f095c2009-03-28 19:18:32 +0000305void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattner5e530bc2007-12-27 19:57:00 +0000306 tok::ObjCKeywordKind contextKey) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000307 llvm::SmallVector<DeclPtrTy, 32> allMethods;
308 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000309 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000310 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000311
Ted Kremenekc7c64312010-01-07 01:20:12 +0000312 SourceRange AtEnd;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000313
Steve Naroff99264b42007-08-22 16:35:03 +0000314 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000315 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000316 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump11289f42009-09-09 15:08:12 +0000317 DeclPtrTy methodPrototype =
Chris Lattner0ef13522007-10-09 17:51:17 +0000318 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000319 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000320 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
321 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000322 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
323 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000324 continue;
325 }
Mike Stump11289f42009-09-09 15:08:12 +0000326
Chris Lattner038a3e32008-10-20 05:46:22 +0000327 // Ignore excess semicolons.
328 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000329 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000330 continue;
331 }
Mike Stump11289f42009-09-09 15:08:12 +0000332
Chris Lattnerda9fb152008-10-20 06:10:06 +0000333 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000334 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000335 break;
Mike Stump11289f42009-09-09 15:08:12 +0000336
Douglas Gregorf1934162010-01-13 21:24:21 +0000337 // Code completion within an Objective-C interface.
338 if (Tok.is(tok::code_completion)) {
339 Actions.CodeCompleteOrdinaryName(CurScope,
340 ObjCImpDecl? Action::CCC_ObjCImplementation
341 : Action::CCC_ObjCInterface);
342 ConsumeToken();
343 }
344
Chris Lattner038a3e32008-10-20 05:46:22 +0000345 // If we don't have an @ directive, parse it as a function definition.
346 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000347 // The code below does not consume '}'s because it is afraid of eating the
348 // end of a namespace. Because of the way this code is structured, an
349 // erroneous r_brace would cause an infinite loop if not handled here.
350 if (Tok.is(tok::r_brace))
351 break;
Mike Stump11289f42009-09-09 15:08:12 +0000352
Steve Narofff1bc45b2007-08-22 18:35:33 +0000353 // FIXME: as the name implies, this rule allows function definitions.
354 // We could pass a flag or check for functions during semantic analysis.
Alexis Hunt96d5c762009-11-21 08:43:09 +0000355 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattner038a3e32008-10-20 05:46:22 +0000356 continue;
357 }
Mike Stump11289f42009-09-09 15:08:12 +0000358
Chris Lattner038a3e32008-10-20 05:46:22 +0000359 // Otherwise, we have an @ directive, eat the @.
360 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000361 if (Tok.is(tok::code_completion)) {
362 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
363 ConsumeToken();
364 break;
365 }
366
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000367 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000368
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000369 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000370 AtEnd.setBegin(AtLoc);
371 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000372 break;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000373 }
Mike Stump11289f42009-09-09 15:08:12 +0000374
Chris Lattnerda9fb152008-10-20 06:10:06 +0000375 // Eat the identifier.
376 ConsumeToken();
377
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000378 switch (DirectiveKind) {
379 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000380 // FIXME: If someone forgets an @end on a protocol, this loop will
381 // continue to eat up tons of stuff and spew lots of nonsense errors. It
382 // would probably be better to bail out if we saw an @class or @interface
383 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000384 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000385 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000386 SkipUntil(tok::r_brace, tok::at);
387 break;
Mike Stump11289f42009-09-09 15:08:12 +0000388
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000389 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000390 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000391 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000392 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000393 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000394 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000395 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000396 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000397 break;
Mike Stump11289f42009-09-09 15:08:12 +0000398
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000399 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000400 if (!getLang().ObjC2)
401 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
402
Chris Lattner038a3e32008-10-20 05:46:22 +0000403 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000404 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000405 if (Tok.is(tok::l_paren))
Douglas Gregorc8537c52009-11-19 07:41:15 +0000406 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
407 allMethods.data(), allMethods.size());
Mike Stump11289f42009-09-09 15:08:12 +0000408
John McCall064d77b2009-12-03 22:31:13 +0000409 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
410 OCDS, AtLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000411
Chris Lattner038a3e32008-10-20 05:46:22 +0000412 // Parse all the comma separated declarators.
413 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000414 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000415
Chris Lattnere0d3fe92008-10-20 06:15:13 +0000416 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
417 tok::at);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000418 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000419 }
Steve Naroff99264b42007-08-22 16:35:03 +0000420 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000421
422 // We break out of the big loop in two cases: when we see @end or when we see
423 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000424 if (Tok.is(tok::code_completion)) {
425 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
426 ConsumeToken();
427 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerda9fb152008-10-20 06:10:06 +0000428 ConsumeToken(); // the "end" identifier
429 else
430 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000431
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000432 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000433 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenekc7c64312010-01-07 01:20:12 +0000434 Actions.ActOnAtEnd(AtEnd, interfaceDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000435 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000436 allProperties.data(), allProperties.size(),
437 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000438}
439
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000440/// Parse property attribute declarations.
441///
442/// property-attr-decl: '(' property-attrlist ')'
443/// property-attrlist:
444/// property-attribute
445/// property-attrlist ',' property-attribute
446/// property-attribute:
447/// getter '=' identifier
448/// setter '=' identifier ':'
449/// readonly
450/// readwrite
451/// assign
452/// retain
453/// copy
454/// nonatomic
455///
Douglas Gregorc8537c52009-11-19 07:41:15 +0000456void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl,
457 DeclPtrTy *Methods,
458 unsigned NumMethods) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000459 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000460 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000461
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000462 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000463 if (Tok.is(tok::code_completion)) {
Douglas Gregor36029f42009-11-18 23:08:07 +0000464 Actions.CodeCompleteObjCPropertyFlags(CurScope, DS);
Steve Naroff936354c2009-10-08 21:55:05 +0000465 ConsumeToken();
466 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000467 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000468
Chris Lattner76619232008-10-20 07:22:18 +0000469 // If this is not an identifier at all, bail out early.
470 if (II == 0) {
471 MatchRHSPunctuation(tok::r_paren, LHSLoc);
472 return;
473 }
Mike Stump11289f42009-09-09 15:08:12 +0000474
Chris Lattner1db33542008-10-20 07:37:22 +0000475 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000476
Chris Lattner68e48682008-11-20 04:42:34 +0000477 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000478 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000479 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000480 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner68e48682008-11-20 04:42:34 +0000481 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000482 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000483 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000484 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner68e48682008-11-20 04:42:34 +0000485 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000486 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000487 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000488 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner68e48682008-11-20 04:42:34 +0000489 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattner825bca12008-10-20 07:39:53 +0000490 // getter/setter require extra treatment.
Chris Lattner1db33542008-10-20 07:37:22 +0000491 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
492 tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000493 return;
Mike Stump11289f42009-09-09 15:08:12 +0000494
Douglas Gregorc8537c52009-11-19 07:41:15 +0000495 if (Tok.is(tok::code_completion)) {
496 if (II->getNameStart()[0] == 's')
497 Actions.CodeCompleteObjCPropertySetter(CurScope, ClassDecl,
498 Methods, NumMethods);
499 else
500 Actions.CodeCompleteObjCPropertyGetter(CurScope, ClassDecl,
501 Methods, NumMethods);
502 ConsumeToken();
503 }
504
Chris Lattnerbeca7702008-10-20 07:24:39 +0000505 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000506 Diag(Tok, diag::err_expected_ident);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000507 SkipUntil(tok::r_paren);
508 return;
509 }
Mike Stump11289f42009-09-09 15:08:12 +0000510
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000511 if (II->getNameStart()[0] == 's') {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000512 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
513 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000514 ConsumeToken(); // consume method name
Mike Stump11289f42009-09-09 15:08:12 +0000515
Chris Lattner1db33542008-10-20 07:37:22 +0000516 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
517 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000518 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000519 } else {
520 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
521 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000522 ConsumeToken(); // consume method name
Chris Lattnerbeca7702008-10-20 07:24:39 +0000523 }
Chris Lattner825bca12008-10-20 07:39:53 +0000524 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000525 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000526 SkipUntil(tok::r_paren);
527 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000528 }
Mike Stump11289f42009-09-09 15:08:12 +0000529
Chris Lattner1db33542008-10-20 07:37:22 +0000530 if (Tok.isNot(tok::comma))
531 break;
Mike Stump11289f42009-09-09 15:08:12 +0000532
Chris Lattner1db33542008-10-20 07:37:22 +0000533 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000534 }
Mike Stump11289f42009-09-09 15:08:12 +0000535
Chris Lattner1db33542008-10-20 07:37:22 +0000536 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000537}
538
Steve Naroff09bf8152007-09-06 21:24:23 +0000539/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000540/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000541/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000542///
543/// objc-instance-method: '-'
544/// objc-class-method: '+'
545///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000546/// objc-method-attributes: [OBJC2]
547/// __attribute__((deprecated))
548///
Mike Stump11289f42009-09-09 15:08:12 +0000549Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000550 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000551 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000552
Mike Stump11289f42009-09-09 15:08:12 +0000553 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000554 SourceLocation mLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000555
Chris Lattner83f095c2009-03-28 19:18:32 +0000556 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff09bf8152007-09-06 21:24:23 +0000557 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000558 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000559 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000560}
561
562/// objc-selector:
563/// identifier
564/// one of
565/// enum struct union if else while do for switch case default
566/// break continue return goto asm sizeof typeof __alignof
567/// unsigned long const short volatile signed restrict _Complex
568/// in out inout bycopy byref oneway int char float double void _Bool
569///
Chris Lattner4f472a32009-04-11 18:13:45 +0000570IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000571 switch (Tok.getKind()) {
572 default:
573 return 0;
574 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000575 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000576 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000577 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000578 case tok::kw_break:
579 case tok::kw_case:
580 case tok::kw_catch:
581 case tok::kw_char:
582 case tok::kw_class:
583 case tok::kw_const:
584 case tok::kw_const_cast:
585 case tok::kw_continue:
586 case tok::kw_default:
587 case tok::kw_delete:
588 case tok::kw_do:
589 case tok::kw_double:
590 case tok::kw_dynamic_cast:
591 case tok::kw_else:
592 case tok::kw_enum:
593 case tok::kw_explicit:
594 case tok::kw_export:
595 case tok::kw_extern:
596 case tok::kw_false:
597 case tok::kw_float:
598 case tok::kw_for:
599 case tok::kw_friend:
600 case tok::kw_goto:
601 case tok::kw_if:
602 case tok::kw_inline:
603 case tok::kw_int:
604 case tok::kw_long:
605 case tok::kw_mutable:
606 case tok::kw_namespace:
607 case tok::kw_new:
608 case tok::kw_operator:
609 case tok::kw_private:
610 case tok::kw_protected:
611 case tok::kw_public:
612 case tok::kw_register:
613 case tok::kw_reinterpret_cast:
614 case tok::kw_restrict:
615 case tok::kw_return:
616 case tok::kw_short:
617 case tok::kw_signed:
618 case tok::kw_sizeof:
619 case tok::kw_static:
620 case tok::kw_static_cast:
621 case tok::kw_struct:
622 case tok::kw_switch:
623 case tok::kw_template:
624 case tok::kw_this:
625 case tok::kw_throw:
626 case tok::kw_true:
627 case tok::kw_try:
628 case tok::kw_typedef:
629 case tok::kw_typeid:
630 case tok::kw_typename:
631 case tok::kw_typeof:
632 case tok::kw_union:
633 case tok::kw_unsigned:
634 case tok::kw_using:
635 case tok::kw_virtual:
636 case tok::kw_void:
637 case tok::kw_volatile:
638 case tok::kw_wchar_t:
639 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000640 case tok::kw__Bool:
641 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000642 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000643 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000644 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000645 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000646 }
Steve Naroff99264b42007-08-22 16:35:03 +0000647}
648
Fariborz Jahanian83615522008-01-02 22:54:34 +0000649/// objc-for-collection-in: 'in'
650///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000651bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000652 // FIXME: May have to do additional look-ahead to only allow for
653 // valid tokens following an 'in'; such as an identifier, unary operators,
654 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000655 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000656 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000657}
658
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000659/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000660/// qualifier list and builds their bitmask representation in the input
661/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000662///
663/// objc-type-qualifiers:
664/// objc-type-qualifier
665/// objc-type-qualifiers objc-type-qualifier
666///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000667void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner61511e12007-12-12 06:56:32 +0000668 while (1) {
Chris Lattner5e530bc2007-12-27 19:57:00 +0000669 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000670 return;
Mike Stump11289f42009-09-09 15:08:12 +0000671
Chris Lattner61511e12007-12-12 06:56:32 +0000672 const IdentifierInfo *II = Tok.getIdentifierInfo();
673 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000674 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000675 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000676
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000677 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000678 switch (i) {
679 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000680 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
681 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
682 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
683 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
684 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
685 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000686 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000687 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000688 ConsumeToken();
689 II = 0;
690 break;
691 }
Mike Stump11289f42009-09-09 15:08:12 +0000692
Chris Lattner61511e12007-12-12 06:56:32 +0000693 // If this wasn't a recognized qualifier, bail out.
694 if (II) return;
695 }
696}
697
698/// objc-type-name:
699/// '(' objc-type-qualifiers[opt] type-name ')'
700/// '(' objc-type-qualifiers[opt] ')'
701///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000702Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000703 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000704
Chris Lattnerb7954432008-10-22 03:52:06 +0000705 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000706 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000707
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000708 // Parse type qualifiers, in, inout, etc.
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000709 ParseObjCTypeQualifierList(DS);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000710
Chris Lattnerb7954432008-10-22 03:52:06 +0000711 TypeTy *Ty = 0;
Douglas Gregor220cac52009-02-18 17:45:20 +0000712 if (isTypeSpecifierQualifier()) {
713 TypeResult TypeSpec = ParseTypeName();
714 if (!TypeSpec.isInvalid())
715 Ty = TypeSpec.get();
716 }
Mike Stump11289f42009-09-09 15:08:12 +0000717
Steve Naroff90255b42008-10-21 14:15:04 +0000718 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000719 ConsumeParen();
720 else if (Tok.getLocation() == TypeStartLoc) {
721 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000722 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000723 SkipUntil(tok::r_paren);
724 } else {
725 // Otherwise, we found *something*, but didn't get a ')' in the right
726 // place. Emit an error then return what we have as the type.
727 MatchRHSPunctuation(tok::r_paren, LParenLoc);
728 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000729 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000730}
731
732/// objc-method-decl:
733/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000734/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000735/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000736/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000737///
738/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000739/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000740/// objc-keyword-selector objc-keyword-decl
741///
742/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000743/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
744/// objc-selector ':' objc-keyword-attributes[opt] identifier
745/// ':' objc-type-name objc-keyword-attributes[opt] identifier
746/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000747///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000748/// objc-parmlist:
749/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000750///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000751/// objc-parms:
752/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000753///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000754/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000755/// , ...
756///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000757/// objc-keyword-attributes: [OBJC2]
758/// __attribute__((unused))
759///
Chris Lattner83f095c2009-03-28 19:18:32 +0000760Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
761 tok::TokenKind mType,
762 DeclPtrTy IDecl,
763 tok::ObjCKeywordKind MethodImplKind) {
John McCall28a6aea2009-11-04 02:18:39 +0000764 ParsingDeclRAIIObject PD(*this);
765
Chris Lattner2ebb1782008-08-23 01:48:03 +0000766 // Parse the return type if present.
Chris Lattner5700fab2007-10-07 02:00:24 +0000767 TypeTy *ReturnType = 0;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000768 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000769 if (Tok.is(tok::l_paren))
Fariborz Jahanian7a9c4742007-10-31 23:53:01 +0000770 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump11289f42009-09-09 15:08:12 +0000771
Steve Naroff161a92b2007-10-26 20:53:56 +0000772 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000773 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000774
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000775 // An unnamed colon is valid.
776 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000777 Diag(Tok, diag::err_expected_selector_for_method)
778 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000779 // Skip until we get a ; or {}.
780 SkipUntil(tok::r_brace);
Chris Lattner83f095c2009-03-28 19:18:32 +0000781 return DeclPtrTy();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000782 }
Mike Stump11289f42009-09-09 15:08:12 +0000783
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000784 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattner0ef13522007-10-09 17:51:17 +0000785 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000786 // If attributes exist after the method, parse them.
787 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000788 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000789 MethodAttrs = ParseGNUAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000790
Chris Lattner5700fab2007-10-07 02:00:24 +0000791 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall28a6aea2009-11-04 02:18:39 +0000792 DeclPtrTy Result
793 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanianb5605172007-11-09 19:52:12 +0000794 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000795 0, CargNames, MethodAttrs,
796 MethodImplKind);
John McCall28a6aea2009-11-04 02:18:39 +0000797 PD.complete(Result);
798 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000799 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000800
Steve Narofff73590d2007-09-27 14:38:14 +0000801 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000802 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000803
Chris Lattner5700fab2007-10-07 02:00:24 +0000804 while (1) {
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000805 Action::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000806
Chris Lattner5700fab2007-10-07 02:00:24 +0000807 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000808 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000809 Diag(Tok, diag::err_expected_colon);
810 break;
811 }
812 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000813
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000814 ArgInfo.Type = 0;
815 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
816 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
817
Chris Lattner5700fab2007-10-07 02:00:24 +0000818 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000819 ArgInfo.ArgAttrs = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +0000820 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000821 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000822
Chris Lattner0ef13522007-10-09 17:51:17 +0000823 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000824 Diag(Tok, diag::err_expected_ident); // missing argument name.
825 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000826 }
Mike Stump11289f42009-09-09 15:08:12 +0000827
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000828 ArgInfo.Name = Tok.getIdentifierInfo();
829 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000830 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000831
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000832 ArgInfos.push_back(ArgInfo);
833 KeyIdents.push_back(SelIdent);
834
Chris Lattner5700fab2007-10-07 02:00:24 +0000835 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000836 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000837 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000838 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000839 break;
840 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000841 }
Mike Stump11289f42009-09-09 15:08:12 +0000842
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000843 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000844
Chris Lattner5700fab2007-10-07 02:00:24 +0000845 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000846 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000847 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000848 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000849 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000850 ConsumeToken();
851 break;
852 }
Chris Lattner5700fab2007-10-07 02:00:24 +0000853 DeclSpec DS;
854 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000855 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000856 Declarator ParmDecl(DS, Declarator::PrototypeContext);
857 ParseDeclarator(ParmDecl);
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000858 CargNames.push_back(ParmDecl);
Chris Lattner5700fab2007-10-07 02:00:24 +0000859 }
Mike Stump11289f42009-09-09 15:08:12 +0000860
Chris Lattner5700fab2007-10-07 02:00:24 +0000861 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000862 // If attributes exist after the method, parse them.
Chris Lattner5700fab2007-10-07 02:00:24 +0000863 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000864 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000865 MethodAttrs = ParseGNUAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000866
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000867 if (KeyIdents.size() == 0)
868 return DeclPtrTy();
Chris Lattner5700fab2007-10-07 02:00:24 +0000869 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
870 &KeyIdents[0]);
John McCall28a6aea2009-11-04 02:18:39 +0000871 DeclPtrTy Result
872 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000873 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000874 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000875 MethodImplKind, isVariadic);
John McCall28a6aea2009-11-04 02:18:39 +0000876 PD.complete(Result);
877 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +0000878}
879
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000880/// objc-protocol-refs:
881/// '<' identifier-list '>'
882///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000883bool Parser::
Chris Lattner83f095c2009-03-28 19:18:32 +0000884ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000885 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
886 bool WarnOnDeclarations,
887 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +0000888 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +0000889
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000890 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +0000891
Chris Lattner3bbae002008-07-26 04:03:38 +0000892 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +0000893
Chris Lattner3bbae002008-07-26 04:03:38 +0000894 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +0000895 if (Tok.is(tok::code_completion)) {
896 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
897 ProtocolIdents.size());
898 ConsumeToken();
899 }
900
Chris Lattner3bbae002008-07-26 04:03:38 +0000901 if (Tok.isNot(tok::identifier)) {
902 Diag(Tok, diag::err_expected_ident);
903 SkipUntil(tok::greater);
904 return true;
905 }
906 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
907 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000908 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +0000909 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000910
Chris Lattner3bbae002008-07-26 04:03:38 +0000911 if (Tok.isNot(tok::comma))
912 break;
913 ConsumeToken();
914 }
Mike Stump11289f42009-09-09 15:08:12 +0000915
Chris Lattner3bbae002008-07-26 04:03:38 +0000916 // Consume the '>'.
917 if (Tok.isNot(tok::greater)) {
918 Diag(Tok, diag::err_expected_greater);
919 return true;
920 }
Mike Stump11289f42009-09-09 15:08:12 +0000921
Chris Lattner3bbae002008-07-26 04:03:38 +0000922 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +0000923
Chris Lattner3bbae002008-07-26 04:03:38 +0000924 // Convert the list of protocols identifiers into a list of protocol decls.
925 Actions.FindProtocolDeclaration(WarnOnDeclarations,
926 &ProtocolIdents[0], ProtocolIdents.size(),
927 Protocols);
928 return false;
929}
930
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000931/// objc-class-instance-variables:
932/// '{' objc-instance-variable-decl-list[opt] '}'
933///
934/// objc-instance-variable-decl-list:
935/// objc-visibility-spec
936/// objc-instance-variable-decl ';'
937/// ';'
938/// objc-instance-variable-decl-list objc-visibility-spec
939/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
940/// objc-instance-variable-decl-list ';'
941///
942/// objc-visibility-spec:
943/// @private
944/// @protected
945/// @public
Steve Naroff00433d32007-08-21 21:17:12 +0000946/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000947///
948/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +0000949/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000950///
Chris Lattner83f095c2009-03-28 19:18:32 +0000951void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff33a1e802007-10-29 21:38:07 +0000952 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000953 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner83f095c2009-03-28 19:18:32 +0000954 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000955
Douglas Gregor45a33ec2009-01-12 18:45:55 +0000956 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000957
Steve Naroff00433d32007-08-21 21:17:12 +0000958 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +0000959
Fariborz Jahanian2dfcec12008-04-29 23:03:51 +0000960 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroff00433d32007-08-21 21:17:12 +0000961 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +0000962 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000963 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +0000964
Steve Naroff00433d32007-08-21 21:17:12 +0000965 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +0000966 if (Tok.is(tok::semi)) {
Chris Lattner3e4fac72009-11-06 06:40:12 +0000967 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner3c7b86f2009-12-06 17:36:05 +0000968 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +0000969 ConsumeToken();
970 continue;
971 }
Mike Stump11289f42009-09-09 15:08:12 +0000972
Steve Naroff00433d32007-08-21 21:17:12 +0000973 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +0000974 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +0000975 ConsumeToken(); // eat the @ sign
Steve Naroff7c348172007-08-23 18:16:40 +0000976 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +0000977 case tok::objc_private:
978 case tok::objc_public:
979 case tok::objc_protected:
980 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +0000981 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +0000982 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000983 continue;
Steve Naroff00433d32007-08-21 21:17:12 +0000984 default:
985 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +0000986 continue;
987 }
988 }
Mike Stump11289f42009-09-09 15:08:12 +0000989
John McCallcfefb6d2009-11-03 02:38:08 +0000990 struct ObjCIvarCallback : FieldCallback {
991 Parser &P;
992 DeclPtrTy IDecl;
993 tok::ObjCKeywordKind visibility;
994 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
995
996 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
997 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
998 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
999 }
1000
1001 DeclPtrTy invoke(FieldDeclarator &FD) {
1002 // Install the declarator into the interface decl.
1003 DeclPtrTy Field
1004 = P.Actions.ActOnIvar(P.CurScope,
1005 FD.D.getDeclSpec().getSourceRange().getBegin(),
1006 IDecl, FD.D, FD.BitfieldSize, visibility);
1007 AllIvarDecls.push_back(Field);
1008 return Field;
1009 }
1010 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1011
Chris Lattnera12405b2008-04-10 06:46:29 +00001012 // Parse all the comma separated declarators.
1013 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +00001014 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001015
Chris Lattner0ef13522007-10-09 17:51:17 +00001016 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001017 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001018 } else {
1019 Diag(Tok, diag::err_expected_semi_decl_list);
1020 // Skip to end of block or statement
1021 SkipUntil(tok::r_brace, true, true);
1022 }
1023 }
Steve Naroff33a1e802007-10-29 21:38:07 +00001024 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001025 // Call ActOnFields() even if we don't have any decls. This is useful
1026 // for code rewriting tools that need to be aware of the empty list.
1027 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +00001028 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001029 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001030 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001031}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001032
1033/// objc-protocol-declaration:
1034/// objc-protocol-definition
1035/// objc-protocol-forward-reference
1036///
1037/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001038/// @protocol identifier
1039/// objc-protocol-refs[opt]
1040/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001041/// @end
1042///
1043/// objc-protocol-forward-reference:
1044/// @protocol identifier-list ';'
1045///
1046/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001047/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001048/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattner83f095c2009-03-28 19:18:32 +00001049Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1050 AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +00001051 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001052 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1053 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001054
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001055 if (Tok.is(tok::code_completion)) {
1056 Actions.CodeCompleteObjCProtocolDecl(CurScope);
1057 ConsumeToken();
1058 }
1059
Chris Lattner0ef13522007-10-09 17:51:17 +00001060 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001061 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001062 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001063 }
1064 // Save the protocol name, then consume it.
1065 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1066 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001067
Chris Lattner0ef13522007-10-09 17:51:17 +00001068 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001069 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001070 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001071 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001072 attrList);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001073 }
Mike Stump11289f42009-09-09 15:08:12 +00001074
Chris Lattner0ef13522007-10-09 17:51:17 +00001075 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001076 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1077 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1078
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001079 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001080 while (1) {
1081 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001082 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001083 Diag(Tok, diag::err_expected_ident);
1084 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +00001085 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001086 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001087 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1088 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001089 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001090
Chris Lattner0ef13522007-10-09 17:51:17 +00001091 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001092 break;
1093 }
1094 // Consume the ';'.
1095 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattner83f095c2009-03-28 19:18:32 +00001096 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001097
Steve Naroff93eb5f12007-10-10 17:32:04 +00001098 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001099 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001100 ProtocolRefs.size(),
1101 attrList);
Chris Lattnerd7352d62008-07-21 22:17:28 +00001102 }
Mike Stump11289f42009-09-09 15:08:12 +00001103
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001104 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001105 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001106
Chris Lattner83f095c2009-03-28 19:18:32 +00001107 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001108 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001109 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001110 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1111 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +00001112 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001113
Chris Lattner83f095c2009-03-28 19:18:32 +00001114 DeclPtrTy ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001115 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001116 ProtocolRefs.data(),
1117 ProtocolRefs.size(),
Daniel Dunbar26e2ab42008-09-26 04:48:09 +00001118 EndProtoLoc, attrList);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001119 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001120 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001121}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001122
1123/// objc-implementation:
1124/// objc-class-implementation-prologue
1125/// objc-category-implementation-prologue
1126///
1127/// objc-class-implementation-prologue:
1128/// @implementation identifier objc-superclass[opt]
1129/// objc-class-instance-variables[opt]
1130///
1131/// objc-category-implementation-prologue:
1132/// @implementation identifier ( identifier )
Chris Lattner83f095c2009-03-28 19:18:32 +00001133Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001134 SourceLocation atLoc) {
1135 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1136 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1137 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001138
Douglas Gregor49c22a72009-11-18 16:26:39 +00001139 // Code completion after '@implementation'.
1140 if (Tok.is(tok::code_completion)) {
1141 Actions.CodeCompleteObjCImplementationDecl(CurScope);
1142 ConsumeToken();
1143 }
1144
Chris Lattner0ef13522007-10-09 17:51:17 +00001145 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001146 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001147 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001148 }
1149 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001150 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001151 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001152
1153 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001154 // we have a category implementation.
1155 SourceLocation lparenLoc = ConsumeParen();
1156 SourceLocation categoryLoc, rparenLoc;
1157 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001158
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001159 if (Tok.is(tok::code_completion)) {
1160 Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
1161 ConsumeToken();
1162 }
1163
Chris Lattner0ef13522007-10-09 17:51:17 +00001164 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001165 categoryId = Tok.getIdentifierInfo();
1166 categoryLoc = ConsumeToken();
1167 } else {
1168 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001169 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001170 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001171 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001172 Diag(Tok, diag::err_expected_rparen);
1173 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001174 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001175 }
1176 rparenLoc = ConsumeParen();
Chris Lattner83f095c2009-03-28 19:18:32 +00001177 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001178 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001179 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001180 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001181 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001182 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001183 }
1184 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001185 SourceLocation superClassLoc;
1186 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001187 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001188 // We have a super class
1189 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001190 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001191 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001192 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001193 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001194 superClassId = Tok.getIdentifierInfo();
1195 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001196 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001197 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001198 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001199 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001200
Steve Naroff33a1e802007-10-29 21:38:07 +00001201 if (Tok.is(tok::l_brace)) // we have ivars
1202 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001203 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001204 PendingObjCImpDecl.push_back(ObjCImpDecl);
1205
Chris Lattner83f095c2009-03-28 19:18:32 +00001206 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001207}
Steve Naroff33a1e802007-10-29 21:38:07 +00001208
Ted Kremenekc7c64312010-01-07 01:20:12 +00001209Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001210 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1211 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattner83f095c2009-03-28 19:18:32 +00001212 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001213 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001214 if (ObjCImpDecl) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001215 Actions.ActOnAtEnd(atEnd, ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001216 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001217 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001218 }
Ted Kremenekc7c64312010-01-07 01:20:12 +00001219 else {
1220 // missing @implementation
1221 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1222 }
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001223 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001224}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001225
Fariborz Jahanianbc02a102009-11-17 17:15:16 +00001226Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001227 if (PendingObjCImpDecl.empty())
1228 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1229 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
Ted Kremenekc7c64312010-01-07 01:20:12 +00001230 Actions.ActOnAtEnd(SourceRange(), ImpDecl);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001231 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1232}
1233
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001234/// compatibility-alias-decl:
1235/// @compatibility_alias alias-name class-name ';'
1236///
Chris Lattner83f095c2009-03-28 19:18:32 +00001237Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001238 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1239 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1240 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001241 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001242 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001243 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001244 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001245 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1246 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001247 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001248 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001249 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001250 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001251 IdentifierInfo *classId = Tok.getIdentifierInfo();
1252 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1253 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001254 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattner83f095c2009-03-28 19:18:32 +00001255 return DeclPtrTy();
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001256 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001257 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1258 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001259}
1260
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001261/// property-synthesis:
1262/// @synthesize property-ivar-list ';'
1263///
1264/// property-ivar-list:
1265/// property-ivar
1266/// property-ivar-list ',' property-ivar
1267///
1268/// property-ivar:
1269/// identifier
1270/// identifier '=' identifier
1271///
Chris Lattner83f095c2009-03-28 19:18:32 +00001272Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001273 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1274 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001275 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001276
Douglas Gregor88e72a02009-11-18 19:45:45 +00001277 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001278 if (Tok.is(tok::code_completion)) {
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001279 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
Douglas Gregor5d649882009-11-18 22:32:06 +00001280 ConsumeToken();
1281 }
1282
Douglas Gregor88e72a02009-11-18 19:45:45 +00001283 if (Tok.isNot(tok::identifier)) {
1284 Diag(Tok, diag::err_synthesized_property_name);
1285 SkipUntil(tok::semi);
1286 return DeclPtrTy();
1287 }
1288
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001289 IdentifierInfo *propertyIvar = 0;
1290 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1291 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattner0ef13522007-10-09 17:51:17 +00001292 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001293 // property '=' ivar-name
1294 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001295
1296 if (Tok.is(tok::code_completion)) {
1297 Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1298 ObjCImpDecl);
1299 ConsumeToken();
1300 }
1301
Chris Lattner0ef13522007-10-09 17:51:17 +00001302 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001303 Diag(Tok, diag::err_expected_ident);
1304 break;
1305 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001306 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001307 ConsumeToken(); // consume ivar-name
1308 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001309 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1310 propertyId, propertyIvar);
Chris Lattner0ef13522007-10-09 17:51:17 +00001311 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001312 break;
1313 ConsumeToken(); // consume ','
1314 }
Douglas Gregor88e72a02009-11-18 19:45:45 +00001315 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001316 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregor88e72a02009-11-18 19:45:45 +00001317 SkipUntil(tok::semi);
1318 }
Fariborz Jahanian95239112009-11-06 21:48:47 +00001319 else
1320 ConsumeToken(); // consume ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001321 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001322}
1323
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001324/// property-dynamic:
1325/// @dynamic property-list
1326///
1327/// property-list:
1328/// identifier
1329/// property-list ',' identifier
1330///
Chris Lattner83f095c2009-03-28 19:18:32 +00001331Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001332 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1333 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1334 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001335 while (true) {
1336 if (Tok.is(tok::code_completion)) {
1337 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1338 ConsumeToken();
1339 }
1340
1341 if (Tok.isNot(tok::identifier)) {
1342 Diag(Tok, diag::err_expected_ident);
1343 SkipUntil(tok::semi);
1344 return DeclPtrTy();
1345 }
1346
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001347 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1348 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1349 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1350 propertyId, 0);
1351
Chris Lattner0ef13522007-10-09 17:51:17 +00001352 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001353 break;
1354 ConsumeToken(); // consume ','
1355 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001356 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001357 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattner83f095c2009-03-28 19:18:32 +00001358 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001359}
Mike Stump11289f42009-09-09 15:08:12 +00001360
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001361/// objc-throw-statement:
1362/// throw expression[opt];
1363///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001364Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001365 OwningExprResult Res(Actions);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001366 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001367 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001368 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001369 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001370 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001371 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001372 }
1373 }
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001374 ConsumeToken(); // consume ';'
Steve Naroff5ee2c022009-02-11 20:05:44 +00001375 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001376}
1377
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001378/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001379/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001380///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001381Parser::OwningStmtResult
1382Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001383 ConsumeToken(); // consume synchronized
1384 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001385 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001386 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001387 }
1388 ConsumeParen(); // '('
Sebastian Redl59b5e512008-12-11 21:36:32 +00001389 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001390 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001391 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001392 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001393 }
1394 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001395 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001396 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001397 }
1398 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001399 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001400 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001401 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001402 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001403 // Enter a scope to hold everything within the compound stmt. Compound
1404 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001405 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001406
Sebastian Redl042ad952008-12-11 19:30:53 +00001407 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001408
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001409 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001410 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001411 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl726a0d92009-02-05 15:02:23 +00001412 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001413}
1414
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001415/// objc-try-catch-statement:
1416/// @try compound-statement objc-catch-list[opt]
1417/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1418///
1419/// objc-catch-list:
1420/// @catch ( parameter-declaration ) compound-statement
1421/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1422/// catch-parameter-declaration:
1423/// parameter-declaration
1424/// '...' [OBJC2]
1425///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001426Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001427 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001428
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001429 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001430 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001431 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001432 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001433 }
Sebastian Redlc13f2682008-12-09 20:22:58 +00001434 OwningStmtResult CatchStmts(Actions);
1435 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001436 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl042ad952008-12-11 19:30:53 +00001437 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001438 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001439 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001440 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001441
Chris Lattner0ef13522007-10-09 17:51:17 +00001442 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001443 // At this point, we need to lookahead to determine if this @ is the start
1444 // of an @catch or @finally. We don't want to consume the @ token if this
1445 // is an @try or @encode or something else.
1446 Token AfterAt = GetLookAheadToken(1);
1447 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1448 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1449 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001450
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001451 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001452 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001453 DeclPtrTy FirstPart;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001454 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001455 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001456 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001457 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001458 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001459 DeclSpec DS;
1460 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001461 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001462 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001463 // we must accept either 'declarator' or 'abstract-declarator' here.
1464 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1465 ParseDeclarator(ParmDecl);
1466
1467 // Inform the actions module about the parameter declarator, so it
1468 // gets added to the current scope.
1469 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001470 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001471 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001472
Steve Naroff65a00892009-04-07 22:56:58 +00001473 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001474
Steve Naroff65a00892009-04-07 22:56:58 +00001475 if (Tok.is(tok::r_paren))
1476 RParenLoc = ConsumeParen();
1477 else // Skip over garbage, until we get to ')'. Eat the ')'.
1478 SkipUntil(tok::r_paren, true, false);
1479
Sebastian Redlc13f2682008-12-09 20:22:58 +00001480 OwningStmtResult CatchBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001481 if (Tok.is(tok::l_brace))
1482 CatchBody = ParseCompoundStatementBody();
1483 else
1484 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001485 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001486 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001487 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff371b8fb2009-03-03 19:52:17 +00001488 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001489 move(CatchStmts));
Steve Naroffe6016792008-02-05 21:27:35 +00001490 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001491 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1492 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001493 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001494 }
1495 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001496 } else {
1497 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001498 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001499 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001500
Sebastian Redlc13f2682008-12-09 20:22:58 +00001501 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001502 if (Tok.is(tok::l_brace))
1503 FinallyBody = ParseCompoundStatementBody();
1504 else
1505 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001506 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001507 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001508 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +00001509 move(FinallyBody));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001510 catch_or_finally_seen = true;
1511 break;
1512 }
1513 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001514 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001515 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001516 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001517 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001518 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1519 move(FinallyStmt));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001520}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001521
Steve Naroff09bf8152007-09-06 21:24:23 +00001522/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001523///
Chris Lattner83f095c2009-03-28 19:18:32 +00001524Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1525 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001526
Chris Lattnereae6cb62009-03-05 08:00:35 +00001527 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1528 PP.getSourceManager(),
1529 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001530
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001531 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001532 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001533 if (ObjCImpDecl) {
1534 Diag(Tok, diag::warn_semicolon_before_method_body)
Chris Lattner3c7b86f2009-12-06 17:36:05 +00001535 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001536 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001537 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001538 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001539
Steve Naroffbb875722007-11-11 19:54:21 +00001540 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001541 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001542 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001543
Steve Naroffbb875722007-11-11 19:54:21 +00001544 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1545 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001546
Steve Naroffbb875722007-11-11 19:54:21 +00001547 // If we didn't find the '{', bail out.
1548 if (Tok.isNot(tok::l_brace))
Chris Lattner83f095c2009-03-28 19:18:32 +00001549 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001550 }
Steve Naroffbb875722007-11-11 19:54:21 +00001551 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001552
Steve Naroffbb875722007-11-11 19:54:21 +00001553 // Enter a scope for the method body.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001554 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001555
Steve Naroffbb875722007-11-11 19:54:21 +00001556 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001557 // specified Declarator for the method.
Steve Naroff70f41d62009-02-28 16:59:13 +00001558 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001559
1560 OwningStmtResult FnBody(ParseCompoundStatementBody());
1561
Steve Naroffbb875722007-11-11 19:54:21 +00001562 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001563 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001564 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1565 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001566
Steve Naroffb94d7f62009-03-02 22:00:56 +00001567 // TODO: Pass argument information.
1568 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump11289f42009-09-09 15:08:12 +00001569
Steve Naroffbb875722007-11-11 19:54:21 +00001570 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001571 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001572
Steve Naroff7b8fa472007-11-13 23:01:27 +00001573 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001574}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001575
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001576Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001577 if (Tok.is(tok::code_completion)) {
1578 Actions.CodeCompleteObjCAtStatement(CurScope);
1579 ConsumeToken();
1580 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001581 }
1582
1583 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001584 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001585
1586 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001587 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001588
1589 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001590 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001591
Sebastian Redl90893182008-12-11 22:33:27 +00001592 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001593 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001594 // If the expression is invalid, skip ahead to the next semicolon. Not
1595 // doing this opens us up to the possibility of infinite loops if
1596 // ParseExpression does not consume any tokens.
1597 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001598 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001599 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001600
Steve Naroffe6016792008-02-05 21:27:35 +00001601 // Otherwise, eat the semicolon.
1602 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlssonafb2dad2009-12-16 02:09:40 +00001603 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Steve Naroffe6016792008-02-05 21:27:35 +00001604}
1605
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001606Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001607 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001608 case tok::code_completion:
1609 Actions.CodeCompleteObjCAtExpression(CurScope);
1610 ConsumeToken();
1611 return ExprError();
1612
Chris Lattnere002fbe2007-12-12 01:04:12 +00001613 case tok::string_literal: // primary-expression: string-literal
1614 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001615 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001616 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001617 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001618 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001619
Chris Lattner197a3012008-08-05 06:19:09 +00001620 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1621 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001622 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001623 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001624 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001625 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001626 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001627 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001628 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001629 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001630 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001631}
1632
Mike Stump11289f42009-09-09 15:08:12 +00001633/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001634/// '[' objc-receiver objc-message-args ']'
1635///
1636/// objc-receiver:
1637/// expression
1638/// class-name
1639/// type-name
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001640Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001641 assert(Tok.is(tok::l_square) && "'[' expected");
1642 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1643
1644 // Parse receiver
Chris Lattnere10c6da2008-01-25 19:25:00 +00001645 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner8f697062008-01-25 18:59:06 +00001646 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00001647 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1648 SourceLocation NameLoc = ConsumeToken();
1649 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1650 ExprArg(Actions));
1651 }
Chris Lattner8f697062008-01-25 18:59:06 +00001652 }
1653
Sebastian Redl59b5e512008-12-11 21:36:32 +00001654 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001655 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00001656 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001657 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00001658 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001659
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001660 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001661 0, move(Res));
Chris Lattner8f697062008-01-25 18:59:06 +00001662}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001663
Chris Lattner8f697062008-01-25 18:59:06 +00001664/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1665/// the rest of a message expression.
Mike Stump11289f42009-09-09 15:08:12 +00001666///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001667/// objc-message-args:
1668/// objc-selector
1669/// objc-keywordarg-list
1670///
1671/// objc-keywordarg-list:
1672/// objc-keywordarg
1673/// objc-keywordarg-list objc-keywordarg
1674///
Mike Stump11289f42009-09-09 15:08:12 +00001675/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001676/// selector-name[opt] ':' objc-keywordexpr
1677///
1678/// objc-keywordexpr:
1679/// nonempty-expr-list
1680///
1681/// nonempty-expr-list:
1682/// assignment-expression
1683/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001684///
1685Parser::OwningExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00001686Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff9e4ac112008-11-19 15:54:23 +00001687 SourceLocation NameLoc,
Chris Lattner8f697062008-01-25 18:59:06 +00001688 IdentifierInfo *ReceiverName,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001689 ExprArg ReceiverExpr) {
Steve Naroffeae65032009-11-07 02:08:14 +00001690 if (Tok.is(tok::code_completion)) {
1691 if (ReceiverName)
Douglas Gregor1b605f72009-11-19 01:08:35 +00001692 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1693 0, 0);
Steve Naroffeae65032009-11-07 02:08:14 +00001694 else
Douglas Gregor1b605f72009-11-19 01:08:35 +00001695 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1696 0, 0);
Steve Naroffeae65032009-11-07 02:08:14 +00001697 ConsumeToken();
1698 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00001699
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001700 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001701 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001702 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00001703
Anders Carlsson978f08d2009-02-14 18:21:46 +00001704 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00001705
Steve Narofff73590d2007-09-27 14:38:14 +00001706 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00001707 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00001708
Chris Lattner0ef13522007-10-09 17:51:17 +00001709 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001710 while (1) {
1711 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00001712 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00001713
Chris Lattner0ef13522007-10-09 17:51:17 +00001714 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001715 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00001716 // We must manually skip to a ']', otherwise the expression skipper will
1717 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1718 // the enclosing expression.
1719 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001720 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001721 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001722
Steve Narofff73590d2007-09-27 14:38:14 +00001723 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001724 /// Parse the expression after ':'
Sebastian Redl59b5e512008-12-11 21:36:32 +00001725 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001726 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001727 // We must manually skip to a ']', otherwise the expression skipper will
1728 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1729 // the enclosing expression.
1730 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001731 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00001732 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001733
Steve Naroff486760a2007-09-17 20:25:27 +00001734 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001735 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001736
Douglas Gregor1b605f72009-11-19 01:08:35 +00001737 // Code completion after each argument.
1738 if (Tok.is(tok::code_completion)) {
1739 if (ReceiverName)
1740 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1741 KeyIdents.data(),
1742 KeyIdents.size());
1743 else
1744 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1745 KeyIdents.data(),
1746 KeyIdents.size());
1747 ConsumeToken();
1748 }
1749
Steve Naroff486760a2007-09-17 20:25:27 +00001750 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00001751 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001752 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001753 break;
1754 // We have a selector or a colon, continue parsing.
1755 }
1756 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00001757 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001758 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00001759 /// Parse the expression after ','
Sebastian Redl59b5e512008-12-11 21:36:32 +00001760 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001761 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001762 // We must manually skip to a ']', otherwise the expression skipper will
1763 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1764 // the enclosing expression.
1765 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001766 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001767 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001768
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001769 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001770 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001771 }
1772 } else if (!selIdent) {
1773 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001774
Chris Lattner197a3012008-08-05 06:19:09 +00001775 // We must manually skip to a ']', otherwise the expression skipper will
1776 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1777 // the enclosing expression.
1778 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001779 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001780 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001781
Chris Lattner0ef13522007-10-09 17:51:17 +00001782 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001783 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00001784 // We must manually skip to a ']', otherwise the expression skipper will
1785 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1786 // the enclosing expression.
1787 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001788 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001789 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001790
Chris Lattner8f697062008-01-25 18:59:06 +00001791 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001792
Steve Naroffe61bfa82007-10-05 18:42:47 +00001793 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00001794 if (nKeys == 0)
1795 KeyIdents.push_back(selIdent);
1796 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001797
Chris Lattner5700fab2007-10-07 02:00:24 +00001798 // We've just parsed a keyword message.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001799 if (ReceiverName)
1800 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump11289f42009-09-09 15:08:12 +00001801 LBracLoc, NameLoc, SelectorLoc,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001802 RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001803 KeyExprs.take(), KeyExprs.size()));
1804 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001805 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001806 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001807}
1808
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001809Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redld65cea82008-12-11 22:51:44 +00001810 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001811 if (Res.isInvalid()) return move(Res);
1812
Chris Lattnere002fbe2007-12-12 01:04:12 +00001813 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1814 // expressions. At this point, we know that the only valid thing that starts
1815 // with '@' is an @"".
1816 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00001817 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00001818 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001819 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001820
Chris Lattnere002fbe2007-12-12 01:04:12 +00001821 while (Tok.is(tok::at)) {
1822 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00001823
Sebastian Redlc13f2682008-12-09 20:22:58 +00001824 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001825 if (!isTokenStringLiteral())
1826 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001827
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001828 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001829 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001830 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001831
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001832 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00001833 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001834
1835 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1836 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00001837}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001838
1839/// objc-encode-expression:
1840/// @encode ( type-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001841Parser::OwningExprResult
1842Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00001843 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001844
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001845 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001846
Chris Lattner197a3012008-08-05 06:19:09 +00001847 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001848 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1849
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001850 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001851
Douglas Gregor220cac52009-02-18 17:45:20 +00001852 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001853
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001854 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001855
Douglas Gregor220cac52009-02-18 17:45:20 +00001856 if (Ty.isInvalid())
1857 return ExprError();
1858
Mike Stump11289f42009-09-09 15:08:12 +00001859 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00001860 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001861}
Anders Carlssone01493d2007-08-23 15:25:28 +00001862
1863/// objc-protocol-expression
1864/// @protocol ( protocol-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001865Parser::OwningExprResult
1866Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00001867 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001868
Chris Lattner197a3012008-08-05 06:19:09 +00001869 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001870 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1871
Anders Carlssone01493d2007-08-23 15:25:28 +00001872 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001873
Chris Lattner197a3012008-08-05 06:19:09 +00001874 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001875 return ExprError(Diag(Tok, diag::err_expected_ident));
1876
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001877 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00001878 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001879
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001880 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00001881
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001882 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1883 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00001884}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001885
1886/// objc-selector-expression
1887/// @selector '(' objc-keyword-selector ')'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001888Parser::OwningExprResult
1889Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001890 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001891
Chris Lattner197a3012008-08-05 06:19:09 +00001892 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001893 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1894
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001895 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001896 SourceLocation LParenLoc = ConsumeParen();
1897 SourceLocation sLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001898 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001899 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1900 return ExprError(Diag(Tok, diag::err_expected_ident));
1901
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001902 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00001903 unsigned nColons = 0;
1904 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001905 while (1) {
Chris Lattner197a3012008-08-05 06:19:09 +00001906 if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001907 return ExprError(Diag(Tok, diag::err_expected_colon));
1908
Chris Lattner5e530bc2007-12-27 19:57:00 +00001909 nColons++;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001910 ConsumeToken(); // Eat the ':'.
1911 if (Tok.is(tok::r_paren))
1912 break;
1913 // Check for another keyword selector.
1914 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001915 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001916 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001917 if (!SelIdent && Tok.isNot(tok::colon))
1918 break;
1919 }
Steve Naroff152dd812007-12-05 22:21:29 +00001920 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001921 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00001922 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001923 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1924 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00001925 }