blob: 3757ee2f6d2342b6f98b46b13be16fc2e85c2190 [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.
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000789 llvm::OwningPtr<AttributeList> MethodAttrs;
Mike Stump11289f42009-09-09 15:08:12 +0000790 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000791 MethodAttrs.reset(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 Kremenekc162e8e2010-02-11 02:19:13 +0000797 0, CargNames, MethodAttrs.get(),
Ted Kremenekbc76c722009-05-04 17:04:30 +0000798 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.
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000865 llvm::OwningPtr<AttributeList> MethodAttrs;
Mike Stump11289f42009-09-09 15:08:12 +0000866 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000867 MethodAttrs.reset(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 Kremenekc162e8e2010-02-11 02:19:13 +0000876 &ArgInfos[0], CargNames,
877 MethodAttrs.get(),
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000878 MethodImplKind, isVariadic);
John McCall28a6aea2009-11-04 02:18:39 +0000879 PD.complete(Result);
Ted Kremenekc162e8e2010-02-11 02:19:13 +0000880
881 // Delete referenced AttributeList objects.
882 for (llvm::SmallVectorImpl<Action::ObjCArgInfo>::iterator
883 I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I)
884 delete I->ArgAttrs;
885
John McCall28a6aea2009-11-04 02:18:39 +0000886 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +0000887}
888
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000889/// objc-protocol-refs:
890/// '<' identifier-list '>'
891///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000892bool Parser::
Chris Lattner83f095c2009-03-28 19:18:32 +0000893ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000894 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
895 bool WarnOnDeclarations,
896 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +0000897 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +0000898
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000899 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +0000900
Chris Lattner3bbae002008-07-26 04:03:38 +0000901 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +0000902
Chris Lattner3bbae002008-07-26 04:03:38 +0000903 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +0000904 if (Tok.is(tok::code_completion)) {
905 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
906 ProtocolIdents.size());
907 ConsumeToken();
908 }
909
Chris Lattner3bbae002008-07-26 04:03:38 +0000910 if (Tok.isNot(tok::identifier)) {
911 Diag(Tok, diag::err_expected_ident);
912 SkipUntil(tok::greater);
913 return true;
914 }
915 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
916 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000917 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +0000918 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000919
Chris Lattner3bbae002008-07-26 04:03:38 +0000920 if (Tok.isNot(tok::comma))
921 break;
922 ConsumeToken();
923 }
Mike Stump11289f42009-09-09 15:08:12 +0000924
Chris Lattner3bbae002008-07-26 04:03:38 +0000925 // Consume the '>'.
926 if (Tok.isNot(tok::greater)) {
927 Diag(Tok, diag::err_expected_greater);
928 return true;
929 }
Mike Stump11289f42009-09-09 15:08:12 +0000930
Chris Lattner3bbae002008-07-26 04:03:38 +0000931 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +0000932
Chris Lattner3bbae002008-07-26 04:03:38 +0000933 // Convert the list of protocols identifiers into a list of protocol decls.
934 Actions.FindProtocolDeclaration(WarnOnDeclarations,
935 &ProtocolIdents[0], ProtocolIdents.size(),
936 Protocols);
937 return false;
938}
939
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000940/// objc-class-instance-variables:
941/// '{' objc-instance-variable-decl-list[opt] '}'
942///
943/// objc-instance-variable-decl-list:
944/// objc-visibility-spec
945/// objc-instance-variable-decl ';'
946/// ';'
947/// objc-instance-variable-decl-list objc-visibility-spec
948/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
949/// objc-instance-variable-decl-list ';'
950///
951/// objc-visibility-spec:
952/// @private
953/// @protected
954/// @public
Steve Naroff00433d32007-08-21 21:17:12 +0000955/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000956///
957/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +0000958/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000959///
Chris Lattner83f095c2009-03-28 19:18:32 +0000960void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff33a1e802007-10-29 21:38:07 +0000961 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000962 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner83f095c2009-03-28 19:18:32 +0000963 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000964
Douglas Gregor45a33ec2009-01-12 18:45:55 +0000965 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000966
Steve Naroff00433d32007-08-21 21:17:12 +0000967 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +0000968
Fariborz Jahanian2dfcec12008-04-29 23:03:51 +0000969 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroff00433d32007-08-21 21:17:12 +0000970 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +0000971 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000972 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +0000973
Steve Naroff00433d32007-08-21 21:17:12 +0000974 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +0000975 if (Tok.is(tok::semi)) {
Chris Lattner3e4fac72009-11-06 06:40:12 +0000976 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner3c7b86f2009-12-06 17:36:05 +0000977 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +0000978 ConsumeToken();
979 continue;
980 }
Mike Stump11289f42009-09-09 15:08:12 +0000981
Steve Naroff00433d32007-08-21 21:17:12 +0000982 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +0000983 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +0000984 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +0000985
986 if (Tok.is(tok::code_completion)) {
987 Actions.CodeCompleteObjCAtVisibility(CurScope);
988 ConsumeToken();
989 }
990
Steve Naroff7c348172007-08-23 18:16:40 +0000991 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +0000992 case tok::objc_private:
993 case tok::objc_public:
994 case tok::objc_protected:
995 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +0000996 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +0000997 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000998 continue;
Steve Naroff00433d32007-08-21 21:17:12 +0000999 default:
1000 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +00001001 continue;
1002 }
1003 }
Mike Stump11289f42009-09-09 15:08:12 +00001004
Douglas Gregor48d46252010-01-13 21:54:15 +00001005 if (Tok.is(tok::code_completion)) {
1006 Actions.CodeCompleteOrdinaryName(CurScope,
1007 Action::CCC_ObjCInstanceVariableList);
1008 ConsumeToken();
1009 }
1010
John McCallcfefb6d2009-11-03 02:38:08 +00001011 struct ObjCIvarCallback : FieldCallback {
1012 Parser &P;
1013 DeclPtrTy IDecl;
1014 tok::ObjCKeywordKind visibility;
1015 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
1016
1017 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
1018 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
1019 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1020 }
1021
1022 DeclPtrTy invoke(FieldDeclarator &FD) {
1023 // Install the declarator into the interface decl.
1024 DeclPtrTy Field
1025 = P.Actions.ActOnIvar(P.CurScope,
1026 FD.D.getDeclSpec().getSourceRange().getBegin(),
1027 IDecl, FD.D, FD.BitfieldSize, visibility);
1028 AllIvarDecls.push_back(Field);
1029 return Field;
1030 }
1031 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1032
Chris Lattnera12405b2008-04-10 06:46:29 +00001033 // Parse all the comma separated declarators.
1034 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +00001035 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001036
Chris Lattner0ef13522007-10-09 17:51:17 +00001037 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001038 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001039 } else {
1040 Diag(Tok, diag::err_expected_semi_decl_list);
1041 // Skip to end of block or statement
1042 SkipUntil(tok::r_brace, true, true);
1043 }
1044 }
Steve Naroff33a1e802007-10-29 21:38:07 +00001045 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001046 // Call ActOnFields() even if we don't have any decls. This is useful
1047 // for code rewriting tools that need to be aware of the empty list.
1048 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +00001049 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001050 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001051 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001052}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001053
1054/// objc-protocol-declaration:
1055/// objc-protocol-definition
1056/// objc-protocol-forward-reference
1057///
1058/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001059/// @protocol identifier
1060/// objc-protocol-refs[opt]
1061/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001062/// @end
1063///
1064/// objc-protocol-forward-reference:
1065/// @protocol identifier-list ';'
1066///
1067/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001068/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001069/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattner83f095c2009-03-28 19:18:32 +00001070Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1071 AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +00001072 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001073 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1074 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001075
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001076 if (Tok.is(tok::code_completion)) {
1077 Actions.CodeCompleteObjCProtocolDecl(CurScope);
1078 ConsumeToken();
1079 }
1080
Chris Lattner0ef13522007-10-09 17:51:17 +00001081 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001082 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001083 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001084 }
1085 // Save the protocol name, then consume it.
1086 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1087 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001088
Chris Lattner0ef13522007-10-09 17:51:17 +00001089 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001090 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001091 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001092 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001093 attrList);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001094 }
Mike Stump11289f42009-09-09 15:08:12 +00001095
Chris Lattner0ef13522007-10-09 17:51:17 +00001096 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001097 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1098 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1099
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001100 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001101 while (1) {
1102 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001103 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001104 Diag(Tok, diag::err_expected_ident);
1105 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +00001106 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001107 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001108 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1109 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001110 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001111
Chris Lattner0ef13522007-10-09 17:51:17 +00001112 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001113 break;
1114 }
1115 // Consume the ';'.
1116 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattner83f095c2009-03-28 19:18:32 +00001117 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001118
Steve Naroff93eb5f12007-10-10 17:32:04 +00001119 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001120 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001121 ProtocolRefs.size(),
1122 attrList);
Chris Lattnerd7352d62008-07-21 22:17:28 +00001123 }
Mike Stump11289f42009-09-09 15:08:12 +00001124
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001125 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001126 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001127
Chris Lattner83f095c2009-03-28 19:18:32 +00001128 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001129 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001130 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001131 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1132 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +00001133 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001134
Chris Lattner83f095c2009-03-28 19:18:32 +00001135 DeclPtrTy ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001136 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001137 ProtocolRefs.data(),
1138 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001139 ProtocolLocs.data(),
Daniel Dunbar26e2ab42008-09-26 04:48:09 +00001140 EndProtoLoc, attrList);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001141 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001142 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001143}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001144
1145/// objc-implementation:
1146/// objc-class-implementation-prologue
1147/// objc-category-implementation-prologue
1148///
1149/// objc-class-implementation-prologue:
1150/// @implementation identifier objc-superclass[opt]
1151/// objc-class-instance-variables[opt]
1152///
1153/// objc-category-implementation-prologue:
1154/// @implementation identifier ( identifier )
Chris Lattner83f095c2009-03-28 19:18:32 +00001155Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001156 SourceLocation atLoc) {
1157 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1158 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1159 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001160
Douglas Gregor49c22a72009-11-18 16:26:39 +00001161 // Code completion after '@implementation'.
1162 if (Tok.is(tok::code_completion)) {
1163 Actions.CodeCompleteObjCImplementationDecl(CurScope);
1164 ConsumeToken();
1165 }
1166
Chris Lattner0ef13522007-10-09 17:51:17 +00001167 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001168 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001169 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001170 }
1171 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001172 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001173 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001174
1175 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001176 // we have a category implementation.
1177 SourceLocation lparenLoc = ConsumeParen();
1178 SourceLocation categoryLoc, rparenLoc;
1179 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001180
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001181 if (Tok.is(tok::code_completion)) {
1182 Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
1183 ConsumeToken();
1184 }
1185
Chris Lattner0ef13522007-10-09 17:51:17 +00001186 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001187 categoryId = Tok.getIdentifierInfo();
1188 categoryLoc = ConsumeToken();
1189 } else {
1190 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001191 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001192 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001193 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001194 Diag(Tok, diag::err_expected_rparen);
1195 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001196 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001197 }
1198 rparenLoc = ConsumeParen();
Chris Lattner83f095c2009-03-28 19:18:32 +00001199 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001200 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001201 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001202 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001203 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001204 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001205 }
1206 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001207 SourceLocation superClassLoc;
1208 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001209 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001210 // We have a super class
1211 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001212 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001213 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001214 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001215 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001216 superClassId = Tok.getIdentifierInfo();
1217 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001218 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001219 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001220 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001221 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001222
Steve Naroff33a1e802007-10-29 21:38:07 +00001223 if (Tok.is(tok::l_brace)) // we have ivars
1224 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001225 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001226 PendingObjCImpDecl.push_back(ObjCImpDecl);
1227
Chris Lattner83f095c2009-03-28 19:18:32 +00001228 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001229}
Steve Naroff33a1e802007-10-29 21:38:07 +00001230
Ted Kremenekc7c64312010-01-07 01:20:12 +00001231Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001232 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1233 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattner83f095c2009-03-28 19:18:32 +00001234 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001235 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001236 if (ObjCImpDecl) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00001237 Actions.ActOnAtEnd(atEnd, ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001238 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001239 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001240 }
Ted Kremenekc7c64312010-01-07 01:20:12 +00001241 else {
1242 // missing @implementation
1243 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1244 }
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001245 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001246}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001247
Fariborz Jahanianbc02a102009-11-17 17:15:16 +00001248Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001249 if (PendingObjCImpDecl.empty())
1250 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1251 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
Ted Kremenekc7c64312010-01-07 01:20:12 +00001252 Actions.ActOnAtEnd(SourceRange(), ImpDecl);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001253 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1254}
1255
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001256/// compatibility-alias-decl:
1257/// @compatibility_alias alias-name class-name ';'
1258///
Chris Lattner83f095c2009-03-28 19:18:32 +00001259Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001260 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1261 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1262 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001263 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001264 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001265 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001266 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001267 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1268 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001269 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001270 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001271 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001272 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001273 IdentifierInfo *classId = Tok.getIdentifierInfo();
1274 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1275 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001276 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattner83f095c2009-03-28 19:18:32 +00001277 return DeclPtrTy();
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001278 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001279 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1280 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001281}
1282
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001283/// property-synthesis:
1284/// @synthesize property-ivar-list ';'
1285///
1286/// property-ivar-list:
1287/// property-ivar
1288/// property-ivar-list ',' property-ivar
1289///
1290/// property-ivar:
1291/// identifier
1292/// identifier '=' identifier
1293///
Chris Lattner83f095c2009-03-28 19:18:32 +00001294Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001295 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1296 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001297 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001298
Douglas Gregor88e72a02009-11-18 19:45:45 +00001299 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001300 if (Tok.is(tok::code_completion)) {
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001301 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
Douglas Gregor5d649882009-11-18 22:32:06 +00001302 ConsumeToken();
1303 }
1304
Douglas Gregor88e72a02009-11-18 19:45:45 +00001305 if (Tok.isNot(tok::identifier)) {
1306 Diag(Tok, diag::err_synthesized_property_name);
1307 SkipUntil(tok::semi);
1308 return DeclPtrTy();
1309 }
1310
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001311 IdentifierInfo *propertyIvar = 0;
1312 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1313 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattner0ef13522007-10-09 17:51:17 +00001314 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001315 // property '=' ivar-name
1316 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001317
1318 if (Tok.is(tok::code_completion)) {
1319 Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1320 ObjCImpDecl);
1321 ConsumeToken();
1322 }
1323
Chris Lattner0ef13522007-10-09 17:51:17 +00001324 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001325 Diag(Tok, diag::err_expected_ident);
1326 break;
1327 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001328 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001329 ConsumeToken(); // consume ivar-name
1330 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001331 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1332 propertyId, propertyIvar);
Chris Lattner0ef13522007-10-09 17:51:17 +00001333 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001334 break;
1335 ConsumeToken(); // consume ','
1336 }
Douglas Gregor88e72a02009-11-18 19:45:45 +00001337 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001338 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregor88e72a02009-11-18 19:45:45 +00001339 SkipUntil(tok::semi);
1340 }
Fariborz Jahanian95239112009-11-06 21:48:47 +00001341 else
1342 ConsumeToken(); // consume ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001343 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001344}
1345
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001346/// property-dynamic:
1347/// @dynamic property-list
1348///
1349/// property-list:
1350/// identifier
1351/// property-list ',' identifier
1352///
Chris Lattner83f095c2009-03-28 19:18:32 +00001353Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001354 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1355 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1356 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001357 while (true) {
1358 if (Tok.is(tok::code_completion)) {
1359 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1360 ConsumeToken();
1361 }
1362
1363 if (Tok.isNot(tok::identifier)) {
1364 Diag(Tok, diag::err_expected_ident);
1365 SkipUntil(tok::semi);
1366 return DeclPtrTy();
1367 }
1368
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001369 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1370 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1371 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1372 propertyId, 0);
1373
Chris Lattner0ef13522007-10-09 17:51:17 +00001374 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001375 break;
1376 ConsumeToken(); // consume ','
1377 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001378 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001379 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattner83f095c2009-03-28 19:18:32 +00001380 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001381}
Mike Stump11289f42009-09-09 15:08:12 +00001382
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001383/// objc-throw-statement:
1384/// throw expression[opt];
1385///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001386Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001387 OwningExprResult Res(Actions);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001388 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001389 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001390 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001391 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001392 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001393 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001394 }
1395 }
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001396 ConsumeToken(); // consume ';'
Steve Naroff5ee2c022009-02-11 20:05:44 +00001397 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001398}
1399
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001400/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001401/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001402///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001403Parser::OwningStmtResult
1404Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001405 ConsumeToken(); // consume synchronized
1406 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001407 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001408 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001409 }
1410 ConsumeParen(); // '('
Sebastian Redl59b5e512008-12-11 21:36:32 +00001411 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001412 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001413 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001414 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001415 }
1416 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001417 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001418 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001419 }
1420 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001421 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001422 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001423 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001424 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001425 // Enter a scope to hold everything within the compound stmt. Compound
1426 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001427 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001428
Sebastian Redl042ad952008-12-11 19:30:53 +00001429 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001430
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001431 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001432 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001433 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl726a0d92009-02-05 15:02:23 +00001434 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001435}
1436
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001437/// objc-try-catch-statement:
1438/// @try compound-statement objc-catch-list[opt]
1439/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1440///
1441/// objc-catch-list:
1442/// @catch ( parameter-declaration ) compound-statement
1443/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1444/// catch-parameter-declaration:
1445/// parameter-declaration
1446/// '...' [OBJC2]
1447///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001448Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001449 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001450
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001451 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001452 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001453 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001454 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001455 }
Sebastian Redlc13f2682008-12-09 20:22:58 +00001456 OwningStmtResult CatchStmts(Actions);
1457 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001458 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl042ad952008-12-11 19:30:53 +00001459 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001460 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001461 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001462 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001463
Chris Lattner0ef13522007-10-09 17:51:17 +00001464 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001465 // At this point, we need to lookahead to determine if this @ is the start
1466 // of an @catch or @finally. We don't want to consume the @ token if this
1467 // is an @try or @encode or something else.
1468 Token AfterAt = GetLookAheadToken(1);
1469 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1470 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1471 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001472
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001473 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001474 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001475 DeclPtrTy FirstPart;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001476 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001477 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001478 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001479 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001480 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001481 DeclSpec DS;
1482 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001483 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001484 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001485 // we must accept either 'declarator' or 'abstract-declarator' here.
1486 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1487 ParseDeclarator(ParmDecl);
1488
1489 // Inform the actions module about the parameter declarator, so it
1490 // gets added to the current scope.
Fariborz Jahaniane1651912010-02-03 00:32:51 +00001491 // FIXME. Probably can build a VarDecl and avoid setting DeclContext.
Steve Naroff371b8fb2009-03-03 19:52:17 +00001492 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Fariborz Jahanian08d614d2010-02-03 00:01:43 +00001493 Actions.ActOnObjCCatchParam(FirstPart);
Steve Naroffe6016792008-02-05 21:27:35 +00001494 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001495 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001496
Steve Naroff65a00892009-04-07 22:56:58 +00001497 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001498
Steve Naroff65a00892009-04-07 22:56:58 +00001499 if (Tok.is(tok::r_paren))
1500 RParenLoc = ConsumeParen();
1501 else // Skip over garbage, until we get to ')'. Eat the ')'.
1502 SkipUntil(tok::r_paren, true, false);
1503
Sebastian Redlc13f2682008-12-09 20:22:58 +00001504 OwningStmtResult CatchBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001505 if (Tok.is(tok::l_brace))
1506 CatchBody = ParseCompoundStatementBody();
1507 else
1508 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001509 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001510 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001511 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff371b8fb2009-03-03 19:52:17 +00001512 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001513 move(CatchStmts));
Steve Naroffe6016792008-02-05 21:27:35 +00001514 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001515 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1516 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001517 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001518 }
1519 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001520 } else {
1521 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001522 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001523 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001524
Sebastian Redlc13f2682008-12-09 20:22:58 +00001525 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001526 if (Tok.is(tok::l_brace))
1527 FinallyBody = ParseCompoundStatementBody();
1528 else
1529 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001530 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001531 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001532 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +00001533 move(FinallyBody));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001534 catch_or_finally_seen = true;
1535 break;
1536 }
1537 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001538 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001539 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001540 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001541 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001542 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1543 move(FinallyStmt));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001544}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001545
Steve Naroff09bf8152007-09-06 21:24:23 +00001546/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001547///
Chris Lattner83f095c2009-03-28 19:18:32 +00001548Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1549 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001550
Chris Lattnereae6cb62009-03-05 08:00:35 +00001551 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1552 PP.getSourceManager(),
1553 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001554
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001555 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001556 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001557 if (ObjCImpDecl) {
1558 Diag(Tok, diag::warn_semicolon_before_method_body)
Chris Lattner3c7b86f2009-12-06 17:36:05 +00001559 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001560 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001561 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001562 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001563
Steve Naroffbb875722007-11-11 19:54:21 +00001564 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001565 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001566 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001567
Steve Naroffbb875722007-11-11 19:54:21 +00001568 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1569 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001570
Steve Naroffbb875722007-11-11 19:54:21 +00001571 // If we didn't find the '{', bail out.
1572 if (Tok.isNot(tok::l_brace))
Chris Lattner83f095c2009-03-28 19:18:32 +00001573 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001574 }
Steve Naroffbb875722007-11-11 19:54:21 +00001575 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001576
Steve Naroffbb875722007-11-11 19:54:21 +00001577 // Enter a scope for the method body.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001578 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001579
Steve Naroffbb875722007-11-11 19:54:21 +00001580 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001581 // specified Declarator for the method.
Steve Naroff70f41d62009-02-28 16:59:13 +00001582 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001583
1584 OwningStmtResult FnBody(ParseCompoundStatementBody());
1585
Steve Naroffbb875722007-11-11 19:54:21 +00001586 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001587 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001588 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1589 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001590
Steve Naroffb94d7f62009-03-02 22:00:56 +00001591 // TODO: Pass argument information.
1592 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump11289f42009-09-09 15:08:12 +00001593
Steve Naroffbb875722007-11-11 19:54:21 +00001594 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001595 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001596
Steve Naroff7b8fa472007-11-13 23:01:27 +00001597 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001598}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001599
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001600Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001601 if (Tok.is(tok::code_completion)) {
1602 Actions.CodeCompleteObjCAtStatement(CurScope);
1603 ConsumeToken();
1604 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001605 }
1606
1607 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001608 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001609
1610 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001611 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001612
1613 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001614 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001615
Sebastian Redl90893182008-12-11 22:33:27 +00001616 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001617 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001618 // If the expression is invalid, skip ahead to the next semicolon. Not
1619 // doing this opens us up to the possibility of infinite loops if
1620 // ParseExpression does not consume any tokens.
1621 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001622 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001623 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001624
Steve Naroffe6016792008-02-05 21:27:35 +00001625 // Otherwise, eat the semicolon.
1626 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlssonafb2dad2009-12-16 02:09:40 +00001627 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
Steve Naroffe6016792008-02-05 21:27:35 +00001628}
1629
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001630Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001631 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001632 case tok::code_completion:
1633 Actions.CodeCompleteObjCAtExpression(CurScope);
1634 ConsumeToken();
1635 return ExprError();
1636
Chris Lattnere002fbe2007-12-12 01:04:12 +00001637 case tok::string_literal: // primary-expression: string-literal
1638 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001639 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001640 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001641 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001642 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001643
Chris Lattner197a3012008-08-05 06:19:09 +00001644 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1645 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001646 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001647 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001648 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001649 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001650 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001651 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001652 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001653 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001654 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001655}
1656
Mike Stump11289f42009-09-09 15:08:12 +00001657/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001658/// '[' objc-receiver objc-message-args ']'
1659///
1660/// objc-receiver:
1661/// expression
1662/// class-name
1663/// type-name
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001664Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001665 assert(Tok.is(tok::l_square) && "'[' expected");
1666 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1667
1668 // Parse receiver
Chris Lattnere10c6da2008-01-25 19:25:00 +00001669 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner8f697062008-01-25 18:59:06 +00001670 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00001671 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1672 SourceLocation NameLoc = ConsumeToken();
1673 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1674 ExprArg(Actions));
1675 }
Chris Lattner8f697062008-01-25 18:59:06 +00001676 }
1677
Sebastian Redl59b5e512008-12-11 21:36:32 +00001678 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001679 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00001680 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001681 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00001682 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001683
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001684 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001685 0, move(Res));
Chris Lattner8f697062008-01-25 18:59:06 +00001686}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001687
Chris Lattner8f697062008-01-25 18:59:06 +00001688/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1689/// the rest of a message expression.
Mike Stump11289f42009-09-09 15:08:12 +00001690///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001691/// objc-message-args:
1692/// objc-selector
1693/// objc-keywordarg-list
1694///
1695/// objc-keywordarg-list:
1696/// objc-keywordarg
1697/// objc-keywordarg-list objc-keywordarg
1698///
Mike Stump11289f42009-09-09 15:08:12 +00001699/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001700/// selector-name[opt] ':' objc-keywordexpr
1701///
1702/// objc-keywordexpr:
1703/// nonempty-expr-list
1704///
1705/// nonempty-expr-list:
1706/// assignment-expression
1707/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001708///
1709Parser::OwningExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00001710Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff9e4ac112008-11-19 15:54:23 +00001711 SourceLocation NameLoc,
Chris Lattner8f697062008-01-25 18:59:06 +00001712 IdentifierInfo *ReceiverName,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001713 ExprArg ReceiverExpr) {
Steve Naroffeae65032009-11-07 02:08:14 +00001714 if (Tok.is(tok::code_completion)) {
1715 if (ReceiverName)
Douglas Gregor1b605f72009-11-19 01:08:35 +00001716 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1717 0, 0);
Steve Naroffeae65032009-11-07 02:08:14 +00001718 else
Douglas Gregor1b605f72009-11-19 01:08:35 +00001719 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1720 0, 0);
Steve Naroffeae65032009-11-07 02:08:14 +00001721 ConsumeToken();
1722 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00001723
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001724 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001725 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001726 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00001727
Anders Carlsson978f08d2009-02-14 18:21:46 +00001728 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00001729
Steve Narofff73590d2007-09-27 14:38:14 +00001730 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00001731 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00001732
Chris Lattner0ef13522007-10-09 17:51:17 +00001733 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001734 while (1) {
1735 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00001736 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00001737
Chris Lattner0ef13522007-10-09 17:51:17 +00001738 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001739 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00001740 // We must manually skip to a ']', otherwise the expression skipper will
1741 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1742 // the enclosing expression.
1743 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001744 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001745 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001746
Steve Narofff73590d2007-09-27 14:38:14 +00001747 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001748 /// Parse the expression after ':'
Sebastian Redl59b5e512008-12-11 21:36:32 +00001749 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001750 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001751 // We must manually skip to a ']', otherwise the expression skipper will
1752 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1753 // the enclosing expression.
1754 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001755 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00001756 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001757
Steve Naroff486760a2007-09-17 20:25:27 +00001758 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001759 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001760
Douglas Gregor1b605f72009-11-19 01:08:35 +00001761 // Code completion after each argument.
1762 if (Tok.is(tok::code_completion)) {
1763 if (ReceiverName)
1764 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1765 KeyIdents.data(),
1766 KeyIdents.size());
1767 else
1768 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1769 KeyIdents.data(),
1770 KeyIdents.size());
1771 ConsumeToken();
1772 }
1773
Steve Naroff486760a2007-09-17 20:25:27 +00001774 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00001775 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001776 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001777 break;
1778 // We have a selector or a colon, continue parsing.
1779 }
1780 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00001781 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001782 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00001783 /// Parse the expression after ','
Sebastian Redl59b5e512008-12-11 21:36:32 +00001784 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001785 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001786 // We must manually skip to a ']', otherwise the expression skipper will
1787 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1788 // the enclosing expression.
1789 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001790 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001791 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001792
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001793 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001794 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001795 }
1796 } else if (!selIdent) {
1797 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001798
Chris Lattner197a3012008-08-05 06:19:09 +00001799 // We must manually skip to a ']', otherwise the expression skipper will
1800 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1801 // the enclosing expression.
1802 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001803 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001804 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001805
Chris Lattner0ef13522007-10-09 17:51:17 +00001806 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001807 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00001808 // We must manually skip to a ']', otherwise the expression skipper will
1809 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1810 // the enclosing expression.
1811 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001812 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001813 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001814
Chris Lattner8f697062008-01-25 18:59:06 +00001815 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001816
Steve Naroffe61bfa82007-10-05 18:42:47 +00001817 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00001818 if (nKeys == 0)
1819 KeyIdents.push_back(selIdent);
1820 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001821
Chris Lattner5700fab2007-10-07 02:00:24 +00001822 // We've just parsed a keyword message.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001823 if (ReceiverName)
1824 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump11289f42009-09-09 15:08:12 +00001825 LBracLoc, NameLoc, SelectorLoc,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001826 RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001827 KeyExprs.take(), KeyExprs.size()));
1828 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001829 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001830 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001831}
1832
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001833Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redld65cea82008-12-11 22:51:44 +00001834 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001835 if (Res.isInvalid()) return move(Res);
1836
Chris Lattnere002fbe2007-12-12 01:04:12 +00001837 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1838 // expressions. At this point, we know that the only valid thing that starts
1839 // with '@' is an @"".
1840 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00001841 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00001842 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001843 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001844
Chris Lattnere002fbe2007-12-12 01:04:12 +00001845 while (Tok.is(tok::at)) {
1846 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00001847
Sebastian Redlc13f2682008-12-09 20:22:58 +00001848 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001849 if (!isTokenStringLiteral())
1850 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001851
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001852 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001853 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001854 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001855
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001856 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00001857 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001858
1859 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1860 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00001861}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001862
1863/// objc-encode-expression:
1864/// @encode ( type-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001865Parser::OwningExprResult
1866Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00001867 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001868
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001869 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001870
Chris Lattner197a3012008-08-05 06:19:09 +00001871 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001872 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1873
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001874 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001875
Douglas Gregor220cac52009-02-18 17:45:20 +00001876 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001877
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001878 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001879
Douglas Gregor220cac52009-02-18 17:45:20 +00001880 if (Ty.isInvalid())
1881 return ExprError();
1882
Mike Stump11289f42009-09-09 15:08:12 +00001883 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00001884 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001885}
Anders Carlssone01493d2007-08-23 15:25:28 +00001886
1887/// objc-protocol-expression
1888/// @protocol ( protocol-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001889Parser::OwningExprResult
1890Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00001891 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001892
Chris Lattner197a3012008-08-05 06:19:09 +00001893 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001894 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1895
Anders Carlssone01493d2007-08-23 15:25:28 +00001896 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001897
Chris Lattner197a3012008-08-05 06:19:09 +00001898 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001899 return ExprError(Diag(Tok, diag::err_expected_ident));
1900
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001901 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00001902 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001903
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001904 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00001905
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001906 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1907 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00001908}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001909
1910/// objc-selector-expression
1911/// @selector '(' objc-keyword-selector ')'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001912Parser::OwningExprResult
1913Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001914 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001915
Chris Lattner197a3012008-08-05 06:19:09 +00001916 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001917 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1918
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001919 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001920 SourceLocation LParenLoc = ConsumeParen();
1921 SourceLocation sLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001922 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001923 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1924 return ExprError(Diag(Tok, diag::err_expected_ident));
1925
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001926 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00001927 unsigned nColons = 0;
1928 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001929 while (1) {
Chris Lattner197a3012008-08-05 06:19:09 +00001930 if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001931 return ExprError(Diag(Tok, diag::err_expected_colon));
1932
Chris Lattner5e530bc2007-12-27 19:57:00 +00001933 nColons++;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001934 ConsumeToken(); // Eat the ':'.
1935 if (Tok.is(tok::r_paren))
1936 break;
1937 // Check for another keyword selector.
1938 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001939 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001940 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001941 if (!SelIdent && Tok.isNot(tok::colon))
1942 break;
1943 }
Steve Naroff152dd812007-12-05 22:21:29 +00001944 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001945 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00001946 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001947 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1948 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00001949 }