blob: ef65204e8c36b1aa67914eeebbcb79a3a3f13435 [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(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000189 ProtocolLocs.data(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000190 EndProtoLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000191
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000192 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000193 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000194 }
195 // Parse a class interface.
196 IdentifierInfo *superClassId = 0;
197 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000198
Chris Lattner0ef13522007-10-09 17:51:17 +0000199 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000200 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000201
202 // Code completion of superclass names.
203 if (Tok.is(tok::code_completion)) {
204 Actions.CodeCompleteObjCSuperclass(CurScope, nameId);
205 ConsumeToken();
206 }
207
Chris Lattner0ef13522007-10-09 17:51:17 +0000208 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000209 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000210 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000211 }
212 superClassId = Tok.getIdentifierInfo();
213 superClassLoc = ConsumeToken();
214 }
215 // Next, we need to check for any protocol references.
Chris Lattner83f095c2009-03-28 19:18:32 +0000216 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000217 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
218 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000219 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000220 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
221 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000222 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000223
224 DeclPtrTy ClsType =
225 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000226 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000227 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000228 ProtocolLocs.data(),
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000229 EndProtoLoc, attrList);
Mike Stump11289f42009-09-09 15:08:12 +0000230
Chris Lattner0ef13522007-10-09 17:51:17 +0000231 if (Tok.is(tok::l_brace))
Steve Naroff33a1e802007-10-29 21:38:07 +0000232 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000233
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000234 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000235 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000236}
237
John McCall064d77b2009-12-03 22:31:13 +0000238/// The Objective-C property callback. This should be defined where
239/// it's used, but instead it's been lifted to here to support VS2005.
240struct Parser::ObjCPropertyCallback : FieldCallback {
241 Parser &P;
242 DeclPtrTy IDecl;
243 llvm::SmallVectorImpl<DeclPtrTy> &Props;
244 ObjCDeclSpec &OCDS;
245 SourceLocation AtLoc;
246 tok::ObjCKeywordKind MethodImplKind;
247
248 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
249 llvm::SmallVectorImpl<DeclPtrTy> &Props,
250 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
251 tok::ObjCKeywordKind MethodImplKind) :
252 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
253 MethodImplKind(MethodImplKind) {
254 }
255
256 DeclPtrTy invoke(FieldDeclarator &FD) {
257 if (FD.D.getIdentifier() == 0) {
258 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
259 << FD.D.getSourceRange();
260 return DeclPtrTy();
261 }
262 if (FD.BitfieldSize) {
263 P.Diag(AtLoc, diag::err_objc_property_bitfield)
264 << FD.D.getSourceRange();
265 return DeclPtrTy();
266 }
267
268 // Install the property declarator into interfaceDecl.
269 IdentifierInfo *SelName =
270 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
271
272 Selector GetterSel =
273 P.PP.getSelectorTable().getNullarySelector(SelName);
274 IdentifierInfo *SetterName = OCDS.getSetterName();
275 Selector SetterSel;
276 if (SetterName)
277 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
278 else
279 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
280 P.PP.getSelectorTable(),
281 FD.D.getIdentifier());
282 bool isOverridingProperty = false;
283 DeclPtrTy Property =
284 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
285 GetterSel, SetterSel, IDecl,
286 &isOverridingProperty,
287 MethodImplKind);
288 if (!isOverridingProperty)
289 Props.push_back(Property);
290
291 return Property;
292 }
293};
294
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000295/// objc-interface-decl-list:
296/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000297/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000298/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000299/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000300/// objc-interface-decl-list declaration
301/// objc-interface-decl-list ';'
302///
Steve Naroff99264b42007-08-22 16:35:03 +0000303/// objc-method-requirement: [OBJC2]
304/// @required
305/// @optional
306///
Chris Lattner83f095c2009-03-28 19:18:32 +0000307void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattner5e530bc2007-12-27 19:57:00 +0000308 tok::ObjCKeywordKind contextKey) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000309 llvm::SmallVector<DeclPtrTy, 32> allMethods;
310 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000311 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000312 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000313
Ted Kremenekc7c64312010-01-07 01:20:12 +0000314 SourceRange AtEnd;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000315
Steve Naroff99264b42007-08-22 16:35:03 +0000316 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000317 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000318 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump11289f42009-09-09 15:08:12 +0000319 DeclPtrTy methodPrototype =
Chris Lattner0ef13522007-10-09 17:51:17 +0000320 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000321 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000322 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
323 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000324 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
325 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000326 continue;
327 }
Mike Stump11289f42009-09-09 15:08:12 +0000328
Chris Lattner038a3e32008-10-20 05:46:22 +0000329 // Ignore excess semicolons.
330 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000331 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000332 continue;
333 }
Mike Stump11289f42009-09-09 15:08:12 +0000334
Chris Lattnerda9fb152008-10-20 06:10:06 +0000335 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000336 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000337 break;
Mike Stump11289f42009-09-09 15:08:12 +0000338
Douglas Gregorf1934162010-01-13 21:24:21 +0000339 // Code completion within an Objective-C interface.
340 if (Tok.is(tok::code_completion)) {
341 Actions.CodeCompleteOrdinaryName(CurScope,
342 ObjCImpDecl? Action::CCC_ObjCImplementation
343 : Action::CCC_ObjCInterface);
344 ConsumeToken();
345 }
346
Chris Lattner038a3e32008-10-20 05:46:22 +0000347 // If we don't have an @ directive, parse it as a function definition.
348 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000349 // The code below does not consume '}'s because it is afraid of eating the
350 // end of a namespace. Because of the way this code is structured, an
351 // erroneous r_brace would cause an infinite loop if not handled here.
352 if (Tok.is(tok::r_brace))
353 break;
Mike Stump11289f42009-09-09 15:08:12 +0000354
Steve Narofff1bc45b2007-08-22 18:35:33 +0000355 // FIXME: as the name implies, this rule allows function definitions.
356 // We could pass a flag or check for functions during semantic analysis.
Alexis Hunt96d5c762009-11-21 08:43:09 +0000357 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattner038a3e32008-10-20 05:46:22 +0000358 continue;
359 }
Mike Stump11289f42009-09-09 15:08:12 +0000360
Chris Lattner038a3e32008-10-20 05:46:22 +0000361 // Otherwise, we have an @ directive, eat the @.
362 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000363 if (Tok.is(tok::code_completion)) {
364 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
365 ConsumeToken();
366 break;
367 }
368
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000369 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000370
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000371 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000372 AtEnd.setBegin(AtLoc);
373 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000374 break;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000375 }
Mike Stump11289f42009-09-09 15:08:12 +0000376
Chris Lattnerda9fb152008-10-20 06:10:06 +0000377 // Eat the identifier.
378 ConsumeToken();
379
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000380 switch (DirectiveKind) {
381 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000382 // FIXME: If someone forgets an @end on a protocol, this loop will
383 // continue to eat up tons of stuff and spew lots of nonsense errors. It
384 // would probably be better to bail out if we saw an @class or @interface
385 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000386 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000387 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000388 SkipUntil(tok::r_brace, tok::at);
389 break;
Mike Stump11289f42009-09-09 15:08:12 +0000390
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000391 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000392 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000393 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000394 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000395 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000396 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000397 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000398 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000399 break;
Mike Stump11289f42009-09-09 15:08:12 +0000400
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000401 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000402 if (!getLang().ObjC2)
403 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
404
Chris Lattner038a3e32008-10-20 05:46:22 +0000405 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000406 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000407 if (Tok.is(tok::l_paren))
Douglas Gregorc8537c52009-11-19 07:41:15 +0000408 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
409 allMethods.data(), allMethods.size());
Mike Stump11289f42009-09-09 15:08:12 +0000410
John McCall064d77b2009-12-03 22:31:13 +0000411 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
412 OCDS, AtLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000413
Chris Lattner038a3e32008-10-20 05:46:22 +0000414 // Parse all the comma separated declarators.
415 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000416 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000417
Chris Lattnere0d3fe92008-10-20 06:15:13 +0000418 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
419 tok::at);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000420 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000421 }
Steve Naroff99264b42007-08-22 16:35:03 +0000422 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000423
424 // We break out of the big loop in two cases: when we see @end or when we see
425 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000426 if (Tok.is(tok::code_completion)) {
427 Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
428 ConsumeToken();
429 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerda9fb152008-10-20 06:10:06 +0000430 ConsumeToken(); // the "end" identifier
431 else
432 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000433
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000434 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000435 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenekc7c64312010-01-07 01:20:12 +0000436 Actions.ActOnAtEnd(AtEnd, interfaceDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000437 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000438 allProperties.data(), allProperties.size(),
439 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000440}
441
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000442/// Parse property attribute declarations.
443///
444/// property-attr-decl: '(' property-attrlist ')'
445/// property-attrlist:
446/// property-attribute
447/// property-attrlist ',' property-attribute
448/// property-attribute:
449/// getter '=' identifier
450/// setter '=' identifier ':'
451/// readonly
452/// readwrite
453/// assign
454/// retain
455/// copy
456/// nonatomic
457///
Douglas Gregorc8537c52009-11-19 07:41:15 +0000458void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl,
459 DeclPtrTy *Methods,
460 unsigned NumMethods) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000461 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000462 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000463
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000464 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000465 if (Tok.is(tok::code_completion)) {
Douglas Gregor36029f42009-11-18 23:08:07 +0000466 Actions.CodeCompleteObjCPropertyFlags(CurScope, DS);
Steve Naroff936354c2009-10-08 21:55:05 +0000467 ConsumeToken();
468 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000469 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000470
Chris Lattner76619232008-10-20 07:22:18 +0000471 // If this is not an identifier at all, bail out early.
472 if (II == 0) {
473 MatchRHSPunctuation(tok::r_paren, LHSLoc);
474 return;
475 }
Mike Stump11289f42009-09-09 15:08:12 +0000476
Chris Lattner1db33542008-10-20 07:37:22 +0000477 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000478
Chris Lattner68e48682008-11-20 04:42:34 +0000479 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000480 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000481 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000482 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner68e48682008-11-20 04:42:34 +0000483 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000484 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000485 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000486 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner68e48682008-11-20 04:42:34 +0000487 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000488 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000489 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000490 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner68e48682008-11-20 04:42:34 +0000491 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattner825bca12008-10-20 07:39:53 +0000492 // getter/setter require extra treatment.
Chris Lattner1db33542008-10-20 07:37:22 +0000493 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
494 tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000495 return;
Mike Stump11289f42009-09-09 15:08:12 +0000496
Douglas Gregorc8537c52009-11-19 07:41:15 +0000497 if (Tok.is(tok::code_completion)) {
498 if (II->getNameStart()[0] == 's')
499 Actions.CodeCompleteObjCPropertySetter(CurScope, ClassDecl,
500 Methods, NumMethods);
501 else
502 Actions.CodeCompleteObjCPropertyGetter(CurScope, ClassDecl,
503 Methods, NumMethods);
504 ConsumeToken();
505 }
506
Chris Lattnerbeca7702008-10-20 07:24:39 +0000507 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000508 Diag(Tok, diag::err_expected_ident);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000509 SkipUntil(tok::r_paren);
510 return;
511 }
Mike Stump11289f42009-09-09 15:08:12 +0000512
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000513 if (II->getNameStart()[0] == 's') {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000514 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
515 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000516 ConsumeToken(); // consume method name
Mike Stump11289f42009-09-09 15:08:12 +0000517
Chris Lattner1db33542008-10-20 07:37:22 +0000518 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
519 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000520 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000521 } else {
522 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
523 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000524 ConsumeToken(); // consume method name
Chris Lattnerbeca7702008-10-20 07:24:39 +0000525 }
Chris Lattner825bca12008-10-20 07:39:53 +0000526 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000527 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000528 SkipUntil(tok::r_paren);
529 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000530 }
Mike Stump11289f42009-09-09 15:08:12 +0000531
Chris Lattner1db33542008-10-20 07:37:22 +0000532 if (Tok.isNot(tok::comma))
533 break;
Mike Stump11289f42009-09-09 15:08:12 +0000534
Chris Lattner1db33542008-10-20 07:37:22 +0000535 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000536 }
Mike Stump11289f42009-09-09 15:08:12 +0000537
Chris Lattner1db33542008-10-20 07:37:22 +0000538 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000539}
540
Steve Naroff09bf8152007-09-06 21:24:23 +0000541/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000542/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000543/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000544///
545/// objc-instance-method: '-'
546/// objc-class-method: '+'
547///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000548/// objc-method-attributes: [OBJC2]
549/// __attribute__((deprecated))
550///
Mike Stump11289f42009-09-09 15:08:12 +0000551Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000552 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000553 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000554
Mike Stump11289f42009-09-09 15:08:12 +0000555 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000556 SourceLocation mLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000557
Chris Lattner83f095c2009-03-28 19:18:32 +0000558 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff09bf8152007-09-06 21:24:23 +0000559 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000560 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000561 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000562}
563
564/// objc-selector:
565/// identifier
566/// one of
567/// enum struct union if else while do for switch case default
568/// break continue return goto asm sizeof typeof __alignof
569/// unsigned long const short volatile signed restrict _Complex
570/// in out inout bycopy byref oneway int char float double void _Bool
571///
Chris Lattner4f472a32009-04-11 18:13:45 +0000572IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000573 switch (Tok.getKind()) {
574 default:
575 return 0;
576 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000577 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000578 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000579 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000580 case tok::kw_break:
581 case tok::kw_case:
582 case tok::kw_catch:
583 case tok::kw_char:
584 case tok::kw_class:
585 case tok::kw_const:
586 case tok::kw_const_cast:
587 case tok::kw_continue:
588 case tok::kw_default:
589 case tok::kw_delete:
590 case tok::kw_do:
591 case tok::kw_double:
592 case tok::kw_dynamic_cast:
593 case tok::kw_else:
594 case tok::kw_enum:
595 case tok::kw_explicit:
596 case tok::kw_export:
597 case tok::kw_extern:
598 case tok::kw_false:
599 case tok::kw_float:
600 case tok::kw_for:
601 case tok::kw_friend:
602 case tok::kw_goto:
603 case tok::kw_if:
604 case tok::kw_inline:
605 case tok::kw_int:
606 case tok::kw_long:
607 case tok::kw_mutable:
608 case tok::kw_namespace:
609 case tok::kw_new:
610 case tok::kw_operator:
611 case tok::kw_private:
612 case tok::kw_protected:
613 case tok::kw_public:
614 case tok::kw_register:
615 case tok::kw_reinterpret_cast:
616 case tok::kw_restrict:
617 case tok::kw_return:
618 case tok::kw_short:
619 case tok::kw_signed:
620 case tok::kw_sizeof:
621 case tok::kw_static:
622 case tok::kw_static_cast:
623 case tok::kw_struct:
624 case tok::kw_switch:
625 case tok::kw_template:
626 case tok::kw_this:
627 case tok::kw_throw:
628 case tok::kw_true:
629 case tok::kw_try:
630 case tok::kw_typedef:
631 case tok::kw_typeid:
632 case tok::kw_typename:
633 case tok::kw_typeof:
634 case tok::kw_union:
635 case tok::kw_unsigned:
636 case tok::kw_using:
637 case tok::kw_virtual:
638 case tok::kw_void:
639 case tok::kw_volatile:
640 case tok::kw_wchar_t:
641 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000642 case tok::kw__Bool:
643 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000644 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000645 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000646 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000647 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000648 }
Steve Naroff99264b42007-08-22 16:35:03 +0000649}
650
Fariborz Jahanian83615522008-01-02 22:54:34 +0000651/// objc-for-collection-in: 'in'
652///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000653bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000654 // FIXME: May have to do additional look-ahead to only allow for
655 // valid tokens following an 'in'; such as an identifier, unary operators,
656 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000657 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000658 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000659}
660
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000661/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000662/// qualifier list and builds their bitmask representation in the input
663/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000664///
665/// objc-type-qualifiers:
666/// objc-type-qualifier
667/// objc-type-qualifiers objc-type-qualifier
668///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000669void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner61511e12007-12-12 06:56:32 +0000670 while (1) {
Chris Lattner5e530bc2007-12-27 19:57:00 +0000671 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000672 return;
Mike Stump11289f42009-09-09 15:08:12 +0000673
Chris Lattner61511e12007-12-12 06:56:32 +0000674 const IdentifierInfo *II = Tok.getIdentifierInfo();
675 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000676 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000677 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000678
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000679 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000680 switch (i) {
681 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000682 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
683 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
684 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
685 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
686 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
687 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000688 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000689 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000690 ConsumeToken();
691 II = 0;
692 break;
693 }
Mike Stump11289f42009-09-09 15:08:12 +0000694
Chris Lattner61511e12007-12-12 06:56:32 +0000695 // If this wasn't a recognized qualifier, bail out.
696 if (II) return;
697 }
698}
699
700/// objc-type-name:
701/// '(' objc-type-qualifiers[opt] type-name ')'
702/// '(' objc-type-qualifiers[opt] ')'
703///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000704Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000705 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000706
Chris Lattnerb7954432008-10-22 03:52:06 +0000707 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000708 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000709
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000710 // Parse type qualifiers, in, inout, etc.
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000711 ParseObjCTypeQualifierList(DS);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000712
Chris Lattnerb7954432008-10-22 03:52:06 +0000713 TypeTy *Ty = 0;
Douglas Gregor220cac52009-02-18 17:45:20 +0000714 if (isTypeSpecifierQualifier()) {
715 TypeResult TypeSpec = ParseTypeName();
716 if (!TypeSpec.isInvalid())
717 Ty = TypeSpec.get();
718 }
Mike Stump11289f42009-09-09 15:08:12 +0000719
Steve Naroff90255b42008-10-21 14:15:04 +0000720 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000721 ConsumeParen();
722 else if (Tok.getLocation() == TypeStartLoc) {
723 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000724 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000725 SkipUntil(tok::r_paren);
726 } else {
727 // Otherwise, we found *something*, but didn't get a ')' in the right
728 // place. Emit an error then return what we have as the type.
729 MatchRHSPunctuation(tok::r_paren, LParenLoc);
730 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000731 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000732}
733
734/// objc-method-decl:
735/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000736/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000737/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000738/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000739///
740/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000741/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000742/// objc-keyword-selector objc-keyword-decl
743///
744/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000745/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
746/// objc-selector ':' objc-keyword-attributes[opt] identifier
747/// ':' objc-type-name objc-keyword-attributes[opt] identifier
748/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000749///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000750/// objc-parmlist:
751/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000752///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000753/// objc-parms:
754/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000755///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000756/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000757/// , ...
758///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000759/// objc-keyword-attributes: [OBJC2]
760/// __attribute__((unused))
761///
Chris Lattner83f095c2009-03-28 19:18:32 +0000762Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
763 tok::TokenKind mType,
764 DeclPtrTy IDecl,
765 tok::ObjCKeywordKind MethodImplKind) {
John McCall28a6aea2009-11-04 02:18:39 +0000766 ParsingDeclRAIIObject PD(*this);
767
Chris Lattner2ebb1782008-08-23 01:48:03 +0000768 // Parse the return type if present.
Chris Lattner5700fab2007-10-07 02:00:24 +0000769 TypeTy *ReturnType = 0;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000770 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000771 if (Tok.is(tok::l_paren))
Fariborz Jahanian7a9c4742007-10-31 23:53:01 +0000772 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump11289f42009-09-09 15:08:12 +0000773
Steve Naroff161a92b2007-10-26 20:53:56 +0000774 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000775 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000776
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000777 // An unnamed colon is valid.
778 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000779 Diag(Tok, diag::err_expected_selector_for_method)
780 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000781 // Skip until we get a ; or {}.
782 SkipUntil(tok::r_brace);
Chris Lattner83f095c2009-03-28 19:18:32 +0000783 return DeclPtrTy();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000784 }
Mike Stump11289f42009-09-09 15:08:12 +0000785
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000786 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattner0ef13522007-10-09 17:51:17 +0000787 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000788 // If attributes exist after the method, parse them.
789 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000790 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000791 MethodAttrs = ParseGNUAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000792
Chris Lattner5700fab2007-10-07 02:00:24 +0000793 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall28a6aea2009-11-04 02:18:39 +0000794 DeclPtrTy Result
795 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanianb5605172007-11-09 19:52:12 +0000796 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000797 0, CargNames, MethodAttrs,
798 MethodImplKind);
John McCall28a6aea2009-11-04 02:18:39 +0000799 PD.complete(Result);
800 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000801 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000802
Steve Narofff73590d2007-09-27 14:38:14 +0000803 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000804 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000805
Chris Lattner5700fab2007-10-07 02:00:24 +0000806 while (1) {
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000807 Action::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000808
Chris Lattner5700fab2007-10-07 02:00:24 +0000809 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000810 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000811 Diag(Tok, diag::err_expected_colon);
812 break;
813 }
814 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000815
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000816 ArgInfo.Type = 0;
817 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
818 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
819
Chris Lattner5700fab2007-10-07 02:00:24 +0000820 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000821 ArgInfo.ArgAttrs = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +0000822 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000823 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000824
Chris Lattner0ef13522007-10-09 17:51:17 +0000825 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000826 Diag(Tok, diag::err_expected_ident); // missing argument name.
827 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000828 }
Mike Stump11289f42009-09-09 15:08:12 +0000829
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000830 ArgInfo.Name = Tok.getIdentifierInfo();
831 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000832 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000833
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000834 ArgInfos.push_back(ArgInfo);
835 KeyIdents.push_back(SelIdent);
836
Chris Lattner5700fab2007-10-07 02:00:24 +0000837 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000838 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000839 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000840 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000841 break;
842 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000843 }
Mike Stump11289f42009-09-09 15:08:12 +0000844
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000845 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000846
Chris Lattner5700fab2007-10-07 02:00:24 +0000847 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000848 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000849 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000850 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000851 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000852 ConsumeToken();
853 break;
854 }
Chris Lattner5700fab2007-10-07 02:00:24 +0000855 DeclSpec DS;
856 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000857 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000858 Declarator ParmDecl(DS, Declarator::PrototypeContext);
859 ParseDeclarator(ParmDecl);
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000860 CargNames.push_back(ParmDecl);
Chris Lattner5700fab2007-10-07 02:00:24 +0000861 }
Mike Stump11289f42009-09-09 15:08:12 +0000862
Chris Lattner5700fab2007-10-07 02:00:24 +0000863 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000864 // If attributes exist after the method, parse them.
Chris Lattner5700fab2007-10-07 02:00:24 +0000865 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000866 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +0000867 MethodAttrs = ParseGNUAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000868
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000869 if (KeyIdents.size() == 0)
870 return DeclPtrTy();
Chris Lattner5700fab2007-10-07 02:00:24 +0000871 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
872 &KeyIdents[0]);
John McCall28a6aea2009-11-04 02:18:39 +0000873 DeclPtrTy Result
874 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000875 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000876 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000877 MethodImplKind, isVariadic);
John McCall28a6aea2009-11-04 02:18:39 +0000878 PD.complete(Result);
879 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +0000880}
881
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000882/// objc-protocol-refs:
883/// '<' identifier-list '>'
884///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000885bool Parser::
Chris Lattner83f095c2009-03-28 19:18:32 +0000886ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000887 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
888 bool WarnOnDeclarations,
889 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +0000890 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +0000891
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000892 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +0000893
Chris Lattner3bbae002008-07-26 04:03:38 +0000894 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +0000895
Chris Lattner3bbae002008-07-26 04:03:38 +0000896 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +0000897 if (Tok.is(tok::code_completion)) {
898 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
899 ProtocolIdents.size());
900 ConsumeToken();
901 }
902
Chris Lattner3bbae002008-07-26 04:03:38 +0000903 if (Tok.isNot(tok::identifier)) {
904 Diag(Tok, diag::err_expected_ident);
905 SkipUntil(tok::greater);
906 return true;
907 }
908 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
909 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000910 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +0000911 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000912
Chris Lattner3bbae002008-07-26 04:03:38 +0000913 if (Tok.isNot(tok::comma))
914 break;
915 ConsumeToken();
916 }
Mike Stump11289f42009-09-09 15:08:12 +0000917
Chris Lattner3bbae002008-07-26 04:03:38 +0000918 // Consume the '>'.
919 if (Tok.isNot(tok::greater)) {
920 Diag(Tok, diag::err_expected_greater);
921 return true;
922 }
Mike Stump11289f42009-09-09 15:08:12 +0000923
Chris Lattner3bbae002008-07-26 04:03:38 +0000924 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +0000925
Chris Lattner3bbae002008-07-26 04:03:38 +0000926 // Convert the list of protocols identifiers into a list of protocol decls.
927 Actions.FindProtocolDeclaration(WarnOnDeclarations,
928 &ProtocolIdents[0], ProtocolIdents.size(),
929 Protocols);
930 return false;
931}
932
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000933/// objc-class-instance-variables:
934/// '{' objc-instance-variable-decl-list[opt] '}'
935///
936/// objc-instance-variable-decl-list:
937/// objc-visibility-spec
938/// objc-instance-variable-decl ';'
939/// ';'
940/// objc-instance-variable-decl-list objc-visibility-spec
941/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
942/// objc-instance-variable-decl-list ';'
943///
944/// objc-visibility-spec:
945/// @private
946/// @protected
947/// @public
Steve Naroff00433d32007-08-21 21:17:12 +0000948/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000949///
950/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +0000951/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000952///
Chris Lattner83f095c2009-03-28 19:18:32 +0000953void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff33a1e802007-10-29 21:38:07 +0000954 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000955 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner83f095c2009-03-28 19:18:32 +0000956 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000957
Douglas Gregor45a33ec2009-01-12 18:45:55 +0000958 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000959
Steve Naroff00433d32007-08-21 21:17:12 +0000960 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +0000961
Fariborz Jahanian2dfcec12008-04-29 23:03:51 +0000962 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroff00433d32007-08-21 21:17:12 +0000963 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +0000964 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000965 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +0000966
Steve Naroff00433d32007-08-21 21:17:12 +0000967 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +0000968 if (Tok.is(tok::semi)) {
Chris Lattner3e4fac72009-11-06 06:40:12 +0000969 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner3c7b86f2009-12-06 17:36:05 +0000970 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +0000971 ConsumeToken();
972 continue;
973 }
Mike Stump11289f42009-09-09 15:08:12 +0000974
Steve Naroff00433d32007-08-21 21:17:12 +0000975 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +0000976 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +0000977 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +0000978
979 if (Tok.is(tok::code_completion)) {
980 Actions.CodeCompleteObjCAtVisibility(CurScope);
981 ConsumeToken();
982 }
983
Steve Naroff7c348172007-08-23 18:16:40 +0000984 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +0000985 case tok::objc_private:
986 case tok::objc_public:
987 case tok::objc_protected:
988 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +0000989 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +0000990 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000991 continue;
Steve Naroff00433d32007-08-21 21:17:12 +0000992 default:
993 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +0000994 continue;
995 }
996 }
Mike Stump11289f42009-09-09 15:08:12 +0000997
Douglas Gregor48d46252010-01-13 21:54:15 +0000998 if (Tok.is(tok::code_completion)) {
999 Actions.CodeCompleteOrdinaryName(CurScope,
1000 Action::CCC_ObjCInstanceVariableList);
1001 ConsumeToken();
1002 }
1003
John McCallcfefb6d2009-11-03 02:38:08 +00001004 struct ObjCIvarCallback : FieldCallback {
1005 Parser &P;
1006 DeclPtrTy IDecl;
1007 tok::ObjCKeywordKind visibility;
1008 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
1009
1010 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
1011 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
1012 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1013 }
1014
1015 DeclPtrTy invoke(FieldDeclarator &FD) {
1016 // Install the declarator into the interface decl.
1017 DeclPtrTy Field
1018 = P.Actions.ActOnIvar(P.CurScope,
1019 FD.D.getDeclSpec().getSourceRange().getBegin(),
1020 IDecl, FD.D, FD.BitfieldSize, visibility);
1021 AllIvarDecls.push_back(Field);
1022 return Field;
1023 }
1024 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1025
Chris Lattnera12405b2008-04-10 06:46:29 +00001026 // Parse all the comma separated declarators.
1027 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +00001028 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001029
Chris Lattner0ef13522007-10-09 17:51:17 +00001030 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001031 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001032 } else {
1033 Diag(Tok, diag::err_expected_semi_decl_list);
1034 // Skip to end of block or statement
1035 SkipUntil(tok::r_brace, true, true);
1036 }
1037 }
Steve Naroff33a1e802007-10-29 21:38:07 +00001038 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001039 // Call ActOnFields() even if we don't have any decls. This is useful
1040 // for code rewriting tools that need to be aware of the empty list.
1041 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +00001042 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001043 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001044 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001045}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001046
1047/// objc-protocol-declaration:
1048/// objc-protocol-definition
1049/// objc-protocol-forward-reference
1050///
1051/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001052/// @protocol identifier
1053/// objc-protocol-refs[opt]
1054/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001055/// @end
1056///
1057/// objc-protocol-forward-reference:
1058/// @protocol identifier-list ';'
1059///
1060/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001061/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001062/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattner83f095c2009-03-28 19:18:32 +00001063Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1064 AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +00001065 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001066 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1067 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001068
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001069 if (Tok.is(tok::code_completion)) {
1070 Actions.CodeCompleteObjCProtocolDecl(CurScope);
1071 ConsumeToken();
1072 }
1073
Chris Lattner0ef13522007-10-09 17:51:17 +00001074 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001075 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001076 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001077 }
1078 // Save the protocol name, then consume it.
1079 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1080 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001081
Chris Lattner0ef13522007-10-09 17:51:17 +00001082 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001083 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001084 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001085 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001086 attrList);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001087 }
Mike Stump11289f42009-09-09 15:08:12 +00001088
Chris Lattner0ef13522007-10-09 17:51:17 +00001089 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001090 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1091 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1092
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001093 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001094 while (1) {
1095 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001096 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001097 Diag(Tok, diag::err_expected_ident);
1098 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +00001099 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001100 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001101 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1102 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001103 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001104
Chris Lattner0ef13522007-10-09 17:51:17 +00001105 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001106 break;
1107 }
1108 // Consume the ';'.
1109 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattner83f095c2009-03-28 19:18:32 +00001110 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001111
Steve Naroff93eb5f12007-10-10 17:32:04 +00001112 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001113 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001114 ProtocolRefs.size(),
1115 attrList);
Chris Lattnerd7352d62008-07-21 22:17:28 +00001116 }
Mike Stump11289f42009-09-09 15:08:12 +00001117
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001118 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001119 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001120
Chris Lattner83f095c2009-03-28 19:18:32 +00001121 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001122 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001123 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001124 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1125 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +00001126 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001127
Chris Lattner83f095c2009-03-28 19:18:32 +00001128 DeclPtrTy ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001129 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001130 ProtocolRefs.data(),
1131 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001132 ProtocolLocs.data(),
Daniel Dunbar26e2ab42008-09-26 04:48:09 +00001133 EndProtoLoc, attrList);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001134 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001135 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001136}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001137
1138/// objc-implementation:
1139/// objc-class-implementation-prologue
1140/// objc-category-implementation-prologue
1141///
1142/// objc-class-implementation-prologue:
1143/// @implementation identifier objc-superclass[opt]
1144/// objc-class-instance-variables[opt]
1145///
1146/// objc-category-implementation-prologue:
1147/// @implementation identifier ( identifier )
Chris Lattner83f095c2009-03-28 19:18:32 +00001148Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001149 SourceLocation atLoc) {
1150 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1151 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1152 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001153
Douglas Gregor49c22a72009-11-18 16:26:39 +00001154 // Code completion after '@implementation'.
1155 if (Tok.is(tok::code_completion)) {
1156 Actions.CodeCompleteObjCImplementationDecl(CurScope);
1157 ConsumeToken();
1158 }
1159
Chris Lattner0ef13522007-10-09 17:51:17 +00001160 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001161 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001162 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001163 }
1164 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001165 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001166 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001167
1168 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001169 // we have a category implementation.
1170 SourceLocation lparenLoc = ConsumeParen();
1171 SourceLocation categoryLoc, rparenLoc;
1172 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001173
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001174 if (Tok.is(tok::code_completion)) {
1175 Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
1176 ConsumeToken();
1177 }
1178
Chris Lattner0ef13522007-10-09 17:51:17 +00001179 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001180 categoryId = Tok.getIdentifierInfo();
1181 categoryLoc = ConsumeToken();
1182 } else {
1183 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001184 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001185 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001186 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001187 Diag(Tok, diag::err_expected_rparen);
1188 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001189 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001190 }
1191 rparenLoc = ConsumeParen();
Chris Lattner83f095c2009-03-28 19:18:32 +00001192 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001193 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001194 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001195 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001196 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001197 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001198 }
1199 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001200 SourceLocation superClassLoc;
1201 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001202 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001203 // We have a super class
1204 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001205 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001206 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001207 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001208 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001209 superClassId = Tok.getIdentifierInfo();
1210 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001211 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001212 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001213 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001214 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001215
Steve Naroff33a1e802007-10-29 21:38:07 +00001216 if (Tok.is(tok::l_brace)) // we have ivars
1217 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001218 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001219 PendingObjCImpDecl.push_back(ObjCImpDecl);
1220
Chris Lattner83f095c2009-03-28 19:18:32 +00001221 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001222}
Steve Naroff33a1e802007-10-29 21:38:07 +00001223
Ted Kremenekc7c64312010-01-07 01:20:12 +00001224Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001225 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1226 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattner83f095c2009-03-28 19:18:32 +00001227 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001228 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001229 if (ObjCImpDecl) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001230 Actions.ActOnAtEnd(atEnd, ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001231 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001232 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001233 }
Ted Kremenekc7c64312010-01-07 01:20:12 +00001234 else {
1235 // missing @implementation
1236 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1237 }
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001238 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001239}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001240
Fariborz Jahanianbc02a102009-11-17 17:15:16 +00001241Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001242 if (PendingObjCImpDecl.empty())
1243 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1244 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
Ted Kremenekc7c64312010-01-07 01:20:12 +00001245 Actions.ActOnAtEnd(SourceRange(), ImpDecl);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001246 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1247}
1248
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001249/// compatibility-alias-decl:
1250/// @compatibility_alias alias-name class-name ';'
1251///
Chris Lattner83f095c2009-03-28 19:18:32 +00001252Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001253 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1254 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1255 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001256 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001257 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001258 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001259 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001260 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1261 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001262 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001263 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001264 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001265 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001266 IdentifierInfo *classId = Tok.getIdentifierInfo();
1267 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1268 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001269 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattner83f095c2009-03-28 19:18:32 +00001270 return DeclPtrTy();
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001271 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001272 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1273 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001274}
1275
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001276/// property-synthesis:
1277/// @synthesize property-ivar-list ';'
1278///
1279/// property-ivar-list:
1280/// property-ivar
1281/// property-ivar-list ',' property-ivar
1282///
1283/// property-ivar:
1284/// identifier
1285/// identifier '=' identifier
1286///
Chris Lattner83f095c2009-03-28 19:18:32 +00001287Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001288 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1289 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001290 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001291
Douglas Gregor88e72a02009-11-18 19:45:45 +00001292 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001293 if (Tok.is(tok::code_completion)) {
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001294 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
Douglas Gregor5d649882009-11-18 22:32:06 +00001295 ConsumeToken();
1296 }
1297
Douglas Gregor88e72a02009-11-18 19:45:45 +00001298 if (Tok.isNot(tok::identifier)) {
1299 Diag(Tok, diag::err_synthesized_property_name);
1300 SkipUntil(tok::semi);
1301 return DeclPtrTy();
1302 }
1303
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001304 IdentifierInfo *propertyIvar = 0;
1305 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1306 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattner0ef13522007-10-09 17:51:17 +00001307 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001308 // property '=' ivar-name
1309 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001310
1311 if (Tok.is(tok::code_completion)) {
1312 Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1313 ObjCImpDecl);
1314 ConsumeToken();
1315 }
1316
Chris Lattner0ef13522007-10-09 17:51:17 +00001317 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001318 Diag(Tok, diag::err_expected_ident);
1319 break;
1320 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001321 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001322 ConsumeToken(); // consume ivar-name
1323 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001324 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1325 propertyId, propertyIvar);
Chris Lattner0ef13522007-10-09 17:51:17 +00001326 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001327 break;
1328 ConsumeToken(); // consume ','
1329 }
Douglas Gregor88e72a02009-11-18 19:45:45 +00001330 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001331 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregor88e72a02009-11-18 19:45:45 +00001332 SkipUntil(tok::semi);
1333 }
Fariborz Jahanian95239112009-11-06 21:48:47 +00001334 else
1335 ConsumeToken(); // consume ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001336 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001337}
1338
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001339/// property-dynamic:
1340/// @dynamic property-list
1341///
1342/// property-list:
1343/// identifier
1344/// property-list ',' identifier
1345///
Chris Lattner83f095c2009-03-28 19:18:32 +00001346Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001347 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1348 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1349 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001350 while (true) {
1351 if (Tok.is(tok::code_completion)) {
1352 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1353 ConsumeToken();
1354 }
1355
1356 if (Tok.isNot(tok::identifier)) {
1357 Diag(Tok, diag::err_expected_ident);
1358 SkipUntil(tok::semi);
1359 return DeclPtrTy();
1360 }
1361
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001362 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1363 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1364 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1365 propertyId, 0);
1366
Chris Lattner0ef13522007-10-09 17:51:17 +00001367 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001368 break;
1369 ConsumeToken(); // consume ','
1370 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001371 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001372 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattner83f095c2009-03-28 19:18:32 +00001373 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001374}
Mike Stump11289f42009-09-09 15:08:12 +00001375
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001376/// objc-throw-statement:
1377/// throw expression[opt];
1378///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001379Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001380 OwningExprResult Res(Actions);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001381 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001382 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001383 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001384 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001385 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001386 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001387 }
1388 }
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001389 ConsumeToken(); // consume ';'
Steve Naroff5ee2c022009-02-11 20:05:44 +00001390 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001391}
1392
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001393/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001394/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001395///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001396Parser::OwningStmtResult
1397Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001398 ConsumeToken(); // consume synchronized
1399 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001400 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001401 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001402 }
1403 ConsumeParen(); // '('
Sebastian Redl59b5e512008-12-11 21:36:32 +00001404 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001405 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001406 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001407 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001408 }
1409 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001410 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001411 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001412 }
1413 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001414 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001415 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001416 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001417 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001418 // Enter a scope to hold everything within the compound stmt. Compound
1419 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001420 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001421
Sebastian Redl042ad952008-12-11 19:30:53 +00001422 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001423
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001424 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001425 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001426 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl726a0d92009-02-05 15:02:23 +00001427 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001428}
1429
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001430/// objc-try-catch-statement:
1431/// @try compound-statement objc-catch-list[opt]
1432/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1433///
1434/// objc-catch-list:
1435/// @catch ( parameter-declaration ) compound-statement
1436/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1437/// catch-parameter-declaration:
1438/// parameter-declaration
1439/// '...' [OBJC2]
1440///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001441Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001442 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001443
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001444 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001445 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001446 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001447 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001448 }
Sebastian Redlc13f2682008-12-09 20:22:58 +00001449 OwningStmtResult CatchStmts(Actions);
1450 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001451 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl042ad952008-12-11 19:30:53 +00001452 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001453 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001454 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001455 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001456
Chris Lattner0ef13522007-10-09 17:51:17 +00001457 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001458 // At this point, we need to lookahead to determine if this @ is the start
1459 // of an @catch or @finally. We don't want to consume the @ token if this
1460 // is an @try or @encode or something else.
1461 Token AfterAt = GetLookAheadToken(1);
1462 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1463 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1464 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001465
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001466 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001467 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001468 DeclPtrTy FirstPart;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001469 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001470 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001471 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001472 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001473 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001474 DeclSpec DS;
1475 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001476 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001477 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001478 // we must accept either 'declarator' or 'abstract-declarator' here.
1479 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1480 ParseDeclarator(ParmDecl);
1481
1482 // Inform the actions module about the parameter declarator, so it
1483 // gets added to the current scope.
Fariborz Jahaniane1651912010-02-03 00:32:51 +00001484 // FIXME. Probably can build a VarDecl and avoid setting DeclContext.
Steve Naroff371b8fb2009-03-03 19:52:17 +00001485 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Fariborz Jahanian08d614d2010-02-03 00:01:43 +00001486 Actions.ActOnObjCCatchParam(FirstPart);
Steve Naroffe6016792008-02-05 21:27:35 +00001487 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001488 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001489
Steve Naroff65a00892009-04-07 22:56:58 +00001490 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001491
Steve Naroff65a00892009-04-07 22:56:58 +00001492 if (Tok.is(tok::r_paren))
1493 RParenLoc = ConsumeParen();
1494 else // Skip over garbage, until we get to ')'. Eat the ')'.
1495 SkipUntil(tok::r_paren, true, false);
1496
Sebastian Redlc13f2682008-12-09 20:22:58 +00001497 OwningStmtResult CatchBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001498 if (Tok.is(tok::l_brace))
1499 CatchBody = ParseCompoundStatementBody();
1500 else
1501 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001502 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001503 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001504 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff371b8fb2009-03-03 19:52:17 +00001505 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001506 move(CatchStmts));
Steve Naroffe6016792008-02-05 21:27:35 +00001507 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001508 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1509 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001510 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001511 }
1512 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001513 } else {
1514 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001515 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001516 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001517
Sebastian Redlc13f2682008-12-09 20:22:58 +00001518 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001519 if (Tok.is(tok::l_brace))
1520 FinallyBody = ParseCompoundStatementBody();
1521 else
1522 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001523 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001524 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001525 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +00001526 move(FinallyBody));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001527 catch_or_finally_seen = true;
1528 break;
1529 }
1530 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001531 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001532 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001533 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001534 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001535 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1536 move(FinallyStmt));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001537}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001538
Steve Naroff09bf8152007-09-06 21:24:23 +00001539/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001540///
Chris Lattner83f095c2009-03-28 19:18:32 +00001541Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1542 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001543
Chris Lattnereae6cb62009-03-05 08:00:35 +00001544 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1545 PP.getSourceManager(),
1546 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001547
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001548 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001549 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001550 if (ObjCImpDecl) {
1551 Diag(Tok, diag::warn_semicolon_before_method_body)
Chris Lattner3c7b86f2009-12-06 17:36:05 +00001552 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001553 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001554 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001555 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001556
Steve Naroffbb875722007-11-11 19:54:21 +00001557 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001558 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001559 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001560
Steve Naroffbb875722007-11-11 19:54:21 +00001561 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1562 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001563
Steve Naroffbb875722007-11-11 19:54:21 +00001564 // If we didn't find the '{', bail out.
1565 if (Tok.isNot(tok::l_brace))
Chris Lattner83f095c2009-03-28 19:18:32 +00001566 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001567 }
Steve Naroffbb875722007-11-11 19:54:21 +00001568 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001569
Steve Naroffbb875722007-11-11 19:54:21 +00001570 // Enter a scope for the method body.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001571 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001572
Steve Naroffbb875722007-11-11 19:54:21 +00001573 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001574 // specified Declarator for the method.
Steve Naroff70f41d62009-02-28 16:59:13 +00001575 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001576
1577 OwningStmtResult FnBody(ParseCompoundStatementBody());
1578
Steve Naroffbb875722007-11-11 19:54:21 +00001579 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001580 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001581 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1582 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001583
Steve Naroffb94d7f62009-03-02 22:00:56 +00001584 // TODO: Pass argument information.
1585 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump11289f42009-09-09 15:08:12 +00001586
Steve Naroffbb875722007-11-11 19:54:21 +00001587 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001588 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001589
Steve Naroff7b8fa472007-11-13 23:01:27 +00001590 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001591}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001592
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001593Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001594 if (Tok.is(tok::code_completion)) {
1595 Actions.CodeCompleteObjCAtStatement(CurScope);
1596 ConsumeToken();
1597 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001598 }
1599
1600 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001601 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001602
1603 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001604 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001605
1606 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001607 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001608
Sebastian Redl90893182008-12-11 22:33:27 +00001609 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001610 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001611 // If the expression is invalid, skip ahead to the next semicolon. Not
1612 // doing this opens us up to the possibility of infinite loops if
1613 // ParseExpression does not consume any tokens.
1614 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001615 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001616 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001617
Steve Naroffe6016792008-02-05 21:27:35 +00001618 // Otherwise, eat the semicolon.
1619 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlssonafb2dad2009-12-16 02:09:40 +00001620 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Steve Naroffe6016792008-02-05 21:27:35 +00001621}
1622
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001623Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001624 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001625 case tok::code_completion:
1626 Actions.CodeCompleteObjCAtExpression(CurScope);
1627 ConsumeToken();
1628 return ExprError();
1629
Chris Lattnere002fbe2007-12-12 01:04:12 +00001630 case tok::string_literal: // primary-expression: string-literal
1631 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001632 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001633 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001634 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001635 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001636
Chris Lattner197a3012008-08-05 06:19:09 +00001637 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1638 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001639 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001640 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001641 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001642 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001643 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001644 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001645 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001646 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001647 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001648}
1649
Mike Stump11289f42009-09-09 15:08:12 +00001650/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001651/// '[' objc-receiver objc-message-args ']'
1652///
1653/// objc-receiver:
1654/// expression
1655/// class-name
1656/// type-name
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001657Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001658 assert(Tok.is(tok::l_square) && "'[' expected");
1659 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1660
1661 // Parse receiver
Chris Lattnere10c6da2008-01-25 19:25:00 +00001662 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner8f697062008-01-25 18:59:06 +00001663 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00001664 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1665 SourceLocation NameLoc = ConsumeToken();
1666 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1667 ExprArg(Actions));
1668 }
Chris Lattner8f697062008-01-25 18:59:06 +00001669 }
1670
Sebastian Redl59b5e512008-12-11 21:36:32 +00001671 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001672 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00001673 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001674 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00001675 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001676
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001677 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001678 0, move(Res));
Chris Lattner8f697062008-01-25 18:59:06 +00001679}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001680
Chris Lattner8f697062008-01-25 18:59:06 +00001681/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1682/// the rest of a message expression.
Mike Stump11289f42009-09-09 15:08:12 +00001683///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001684/// objc-message-args:
1685/// objc-selector
1686/// objc-keywordarg-list
1687///
1688/// objc-keywordarg-list:
1689/// objc-keywordarg
1690/// objc-keywordarg-list objc-keywordarg
1691///
Mike Stump11289f42009-09-09 15:08:12 +00001692/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001693/// selector-name[opt] ':' objc-keywordexpr
1694///
1695/// objc-keywordexpr:
1696/// nonempty-expr-list
1697///
1698/// nonempty-expr-list:
1699/// assignment-expression
1700/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001701///
1702Parser::OwningExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00001703Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff9e4ac112008-11-19 15:54:23 +00001704 SourceLocation NameLoc,
Chris Lattner8f697062008-01-25 18:59:06 +00001705 IdentifierInfo *ReceiverName,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001706 ExprArg ReceiverExpr) {
Steve Naroffeae65032009-11-07 02:08:14 +00001707 if (Tok.is(tok::code_completion)) {
1708 if (ReceiverName)
Douglas Gregor1b605f72009-11-19 01:08:35 +00001709 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1710 0, 0);
Steve Naroffeae65032009-11-07 02:08:14 +00001711 else
Douglas Gregor1b605f72009-11-19 01:08:35 +00001712 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1713 0, 0);
Steve Naroffeae65032009-11-07 02:08:14 +00001714 ConsumeToken();
1715 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00001716
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001717 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001718 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001719 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00001720
Anders Carlsson978f08d2009-02-14 18:21:46 +00001721 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00001722
Steve Narofff73590d2007-09-27 14:38:14 +00001723 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00001724 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00001725
Chris Lattner0ef13522007-10-09 17:51:17 +00001726 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001727 while (1) {
1728 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00001729 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00001730
Chris Lattner0ef13522007-10-09 17:51:17 +00001731 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001732 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00001733 // We must manually skip to a ']', otherwise the expression skipper will
1734 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1735 // the enclosing expression.
1736 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001737 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001738 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001739
Steve Narofff73590d2007-09-27 14:38:14 +00001740 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001741 /// Parse the expression after ':'
Sebastian Redl59b5e512008-12-11 21:36:32 +00001742 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001743 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001744 // We must manually skip to a ']', otherwise the expression skipper will
1745 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1746 // the enclosing expression.
1747 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001748 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00001749 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001750
Steve Naroff486760a2007-09-17 20:25:27 +00001751 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001752 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001753
Douglas Gregor1b605f72009-11-19 01:08:35 +00001754 // Code completion after each argument.
1755 if (Tok.is(tok::code_completion)) {
1756 if (ReceiverName)
1757 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1758 KeyIdents.data(),
1759 KeyIdents.size());
1760 else
1761 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1762 KeyIdents.data(),
1763 KeyIdents.size());
1764 ConsumeToken();
1765 }
1766
Steve Naroff486760a2007-09-17 20:25:27 +00001767 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00001768 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001769 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001770 break;
1771 // We have a selector or a colon, continue parsing.
1772 }
1773 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00001774 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001775 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00001776 /// Parse the expression after ','
Sebastian Redl59b5e512008-12-11 21:36:32 +00001777 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001778 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001779 // We must manually skip to a ']', otherwise the expression skipper will
1780 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1781 // the enclosing expression.
1782 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001783 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001784 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001785
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001786 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001787 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001788 }
1789 } else if (!selIdent) {
1790 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001791
Chris Lattner197a3012008-08-05 06:19:09 +00001792 // We must manually skip to a ']', otherwise the expression skipper will
1793 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1794 // the enclosing expression.
1795 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001796 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001797 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001798
Chris Lattner0ef13522007-10-09 17:51:17 +00001799 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001800 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00001801 // We must manually skip to a ']', otherwise the expression skipper will
1802 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1803 // the enclosing expression.
1804 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001805 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001806 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001807
Chris Lattner8f697062008-01-25 18:59:06 +00001808 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001809
Steve Naroffe61bfa82007-10-05 18:42:47 +00001810 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00001811 if (nKeys == 0)
1812 KeyIdents.push_back(selIdent);
1813 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001814
Chris Lattner5700fab2007-10-07 02:00:24 +00001815 // We've just parsed a keyword message.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001816 if (ReceiverName)
1817 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump11289f42009-09-09 15:08:12 +00001818 LBracLoc, NameLoc, SelectorLoc,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001819 RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001820 KeyExprs.take(), KeyExprs.size()));
1821 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001822 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001823 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001824}
1825
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001826Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redld65cea82008-12-11 22:51:44 +00001827 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001828 if (Res.isInvalid()) return move(Res);
1829
Chris Lattnere002fbe2007-12-12 01:04:12 +00001830 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1831 // expressions. At this point, we know that the only valid thing that starts
1832 // with '@' is an @"".
1833 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00001834 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00001835 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001836 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001837
Chris Lattnere002fbe2007-12-12 01:04:12 +00001838 while (Tok.is(tok::at)) {
1839 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00001840
Sebastian Redlc13f2682008-12-09 20:22:58 +00001841 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001842 if (!isTokenStringLiteral())
1843 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001844
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001845 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001846 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001847 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001848
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001849 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00001850 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001851
1852 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1853 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00001854}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001855
1856/// objc-encode-expression:
1857/// @encode ( type-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001858Parser::OwningExprResult
1859Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00001860 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001861
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001862 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001863
Chris Lattner197a3012008-08-05 06:19:09 +00001864 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001865 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1866
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001867 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001868
Douglas Gregor220cac52009-02-18 17:45:20 +00001869 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001870
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001871 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001872
Douglas Gregor220cac52009-02-18 17:45:20 +00001873 if (Ty.isInvalid())
1874 return ExprError();
1875
Mike Stump11289f42009-09-09 15:08:12 +00001876 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00001877 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001878}
Anders Carlssone01493d2007-08-23 15:25:28 +00001879
1880/// objc-protocol-expression
1881/// @protocol ( protocol-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001882Parser::OwningExprResult
1883Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00001884 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001885
Chris Lattner197a3012008-08-05 06:19:09 +00001886 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001887 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1888
Anders Carlssone01493d2007-08-23 15:25:28 +00001889 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001890
Chris Lattner197a3012008-08-05 06:19:09 +00001891 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001892 return ExprError(Diag(Tok, diag::err_expected_ident));
1893
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001894 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00001895 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001896
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001897 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00001898
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001899 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1900 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00001901}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001902
1903/// objc-selector-expression
1904/// @selector '(' objc-keyword-selector ')'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001905Parser::OwningExprResult
1906Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001907 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001908
Chris Lattner197a3012008-08-05 06:19:09 +00001909 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001910 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1911
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001912 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001913 SourceLocation LParenLoc = ConsumeParen();
1914 SourceLocation sLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001915 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001916 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1917 return ExprError(Diag(Tok, diag::err_expected_ident));
1918
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001919 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00001920 unsigned nColons = 0;
1921 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001922 while (1) {
Chris Lattner197a3012008-08-05 06:19:09 +00001923 if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001924 return ExprError(Diag(Tok, diag::err_expected_colon));
1925
Chris Lattner5e530bc2007-12-27 19:57:00 +00001926 nColons++;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001927 ConsumeToken(); // Eat the ':'.
1928 if (Tok.is(tok::r_paren))
1929 break;
1930 // Check for another keyword selector.
1931 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001932 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001933 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001934 if (!SelIdent && Tok.isNot(tok::colon))
1935 break;
1936 }
Steve Naroff152dd812007-12-05 22:21:29 +00001937 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001938 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00001939 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001940 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1941 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00001942 }