blob: c29c618b0144ac304c552c6c56ab2c953150394a [file] [log] [blame]
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000015#include "clang/Parse/Parser.h"
Douglas Gregor0fbda682010-09-15 14:51:05 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/SmallVector.h"
21using namespace clang;
22
23
Chris Lattner891dca62008-12-08 21:53:24 +000024/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000025/// external-declaration: [C99 6.9]
26/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000027/// [OBJC] objc-class-declaration
28/// [OBJC] objc-alias-declaration
29/// [OBJC] objc-protocol-definition
30/// [OBJC] objc-method-definition
31/// [OBJC] '@' 'end'
John McCalld226f652010-08-21 09:40:31 +000032Decl *Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +000034
Douglas Gregorc464ae82009-12-07 09:27:33 +000035 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000036 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, false);
Douglas Gregordc845342010-05-25 05:58:43 +000037 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +000038 }
39
Steve Naroff861cf3e2007-08-23 18:16:40 +000040 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000041 case tok::objc_class:
42 return ParseObjCAtClassDeclaration(AtLoc);
43 case tok::objc_interface:
44 return ParseObjCAtInterfaceDeclaration(AtLoc);
45 case tok::objc_protocol:
46 return ParseObjCAtProtocolDeclaration(AtLoc);
47 case tok::objc_implementation:
48 return ParseObjCAtImplementationDeclaration(AtLoc);
49 case tok::objc_end:
50 return ParseObjCAtEndDeclaration(AtLoc);
51 case tok::objc_compatibility_alias:
52 return ParseObjCAtAliasDeclaration(AtLoc);
53 case tok::objc_synthesize:
54 return ParseObjCPropertySynthesize(AtLoc);
55 case tok::objc_dynamic:
56 return ParseObjCPropertyDynamic(AtLoc);
57 default:
58 Diag(AtLoc, diag::err_unexpected_at);
59 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +000060 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000061 }
62}
63
64///
Mike Stump1eb44332009-09-09 15:08:12 +000065/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000066/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000067///
John McCalld226f652010-08-21 09:40:31 +000068Decl *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000069 ConsumeToken(); // the identifier "class"
70 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremenekc09cba62009-11-17 23:12:20 +000071 llvm::SmallVector<SourceLocation, 8> ClassLocs;
72
Mike Stump1eb44332009-09-09 15:08:12 +000073
Reid Spencer5f016e22007-07-11 17:01:13 +000074 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000075 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000076 Diag(Tok, diag::err_expected_ident);
77 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +000078 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000079 }
Reid Spencer5f016e22007-07-11 17:01:13 +000080 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +000081 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000082 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +000083
Chris Lattnerdf195262007-10-09 17:51:17 +000084 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000085 break;
Mike Stump1eb44332009-09-09 15:08:12 +000086
Reid Spencer5f016e22007-07-11 17:01:13 +000087 ConsumeToken();
88 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Reid Spencer5f016e22007-07-11 17:01:13 +000090 // Consume the ';'.
91 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
John McCalld226f652010-08-21 09:40:31 +000092 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000093
Ted Kremenekc09cba62009-11-17 23:12:20 +000094 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
95 ClassLocs.data(),
96 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000097}
98
Steve Naroffdac269b2007-08-20 21:31:48 +000099///
100/// objc-interface:
101/// objc-class-interface-attributes[opt] objc-class-interface
102/// objc-category-interface
103///
104/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000105/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000106/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000107/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000108/// objc-interface-decl-list
109/// @end
110///
111/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000112/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000113/// objc-protocol-refs[opt]
114/// objc-interface-decl-list
115/// @end
116///
117/// objc-superclass:
118/// ':' identifier
119///
120/// objc-class-interface-attributes:
121/// __attribute__((visibility("default")))
122/// __attribute__((visibility("hidden")))
123/// __attribute__((deprecated))
124/// __attribute__((unavailable))
125/// __attribute__((objc_exception)) - used by NSException on 64-bit
126///
John McCalld226f652010-08-21 09:40:31 +0000127Decl *Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroffdac269b2007-08-20 21:31:48 +0000128 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000129 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000130 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
131 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000133 // Code completion after '@interface'.
134 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000135 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000136 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000137 }
138
Chris Lattnerdf195262007-10-09 17:51:17 +0000139 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000140 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000141 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000142 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000143
Steve Naroffdac269b2007-08-20 21:31:48 +0000144 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000145 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000146 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000147 if (Tok.is(tok::l_paren) &&
148 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000149 SourceLocation lparenLoc = ConsumeParen();
150 SourceLocation categoryLoc, rparenLoc;
151 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000152 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000153 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +0000154 ConsumeCodeCompletionToken();
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000155 }
156
Steve Naroff527fe232007-08-23 19:56:30 +0000157 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000158 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000159 categoryId = Tok.getIdentifierInfo();
160 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000161 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000162 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000163 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000164 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000165 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000166 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000167 Diag(Tok, diag::err_expected_rparen);
168 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +0000169 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000170 }
171 rparenLoc = ConsumeParen();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000172 // Next, we need to check for any protocol references.
173 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +0000174 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000175 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
176 if (Tok.is(tok::less) &&
177 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000178 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000179 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000181 if (attrList) // categories don't support attributes.
182 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000183
John McCalld226f652010-08-21 09:40:31 +0000184 Decl *CategoryType =
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000185 Actions.ActOnStartCategoryInterface(atLoc,
186 nameId, nameLoc,
187 categoryId, categoryLoc,
188 ProtocolRefs.data(),
189 ProtocolRefs.size(),
190 ProtocolLocs.data(),
191 EndProtoLoc);
192 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000193 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
194 atLoc);
195
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000196 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
197 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000198 }
199 // Parse a class interface.
200 IdentifierInfo *superClassId = 0;
201 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000202
Chris Lattnerdf195262007-10-09 17:51:17 +0000203 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000204 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000205
206 // Code completion of superclass names.
207 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000208 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +0000209 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000210 }
211
Chris Lattnerdf195262007-10-09 17:51:17 +0000212 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000213 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000214 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000215 }
216 superClassId = Tok.getIdentifierInfo();
217 superClassLoc = ConsumeToken();
218 }
219 // Next, we need to check for any protocol references.
John McCalld226f652010-08-21 09:40:31 +0000220 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000221 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
222 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000223 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000224 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
225 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000226 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000227
John McCalld226f652010-08-21 09:40:31 +0000228 Decl *ClsType =
Mike Stump1eb44332009-09-09 15:08:12 +0000229 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000230 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000231 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000232 ProtocolLocs.data(),
Chris Lattner06036d32008-07-26 04:13:19 +0000233 EndProtoLoc, attrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Chris Lattnerdf195262007-10-09 17:51:17 +0000235 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000236 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000237
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000238 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000239 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000240}
241
John McCalld0014542009-12-03 22:31:13 +0000242/// The Objective-C property callback. This should be defined where
243/// it's used, but instead it's been lifted to here to support VS2005.
244struct Parser::ObjCPropertyCallback : FieldCallback {
245 Parser &P;
John McCalld226f652010-08-21 09:40:31 +0000246 Decl *IDecl;
247 llvm::SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000248 ObjCDeclSpec &OCDS;
249 SourceLocation AtLoc;
250 tok::ObjCKeywordKind MethodImplKind;
251
John McCalld226f652010-08-21 09:40:31 +0000252 ObjCPropertyCallback(Parser &P, Decl *IDecl,
253 llvm::SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000254 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
255 tok::ObjCKeywordKind MethodImplKind) :
256 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
257 MethodImplKind(MethodImplKind) {
258 }
259
John McCalld226f652010-08-21 09:40:31 +0000260 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000261 if (FD.D.getIdentifier() == 0) {
262 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
263 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000264 return 0;
John McCalld0014542009-12-03 22:31:13 +0000265 }
266 if (FD.BitfieldSize) {
267 P.Diag(AtLoc, diag::err_objc_property_bitfield)
268 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000269 return 0;
John McCalld0014542009-12-03 22:31:13 +0000270 }
271
272 // Install the property declarator into interfaceDecl.
273 IdentifierInfo *SelName =
274 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
275
276 Selector GetterSel =
277 P.PP.getSelectorTable().getNullarySelector(SelName);
278 IdentifierInfo *SetterName = OCDS.getSetterName();
279 Selector SetterSel;
280 if (SetterName)
281 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
282 else
283 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
284 P.PP.getSelectorTable(),
285 FD.D.getIdentifier());
286 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000287 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000288 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
John McCalld0014542009-12-03 22:31:13 +0000289 GetterSel, SetterSel, IDecl,
290 &isOverridingProperty,
291 MethodImplKind);
292 if (!isOverridingProperty)
293 Props.push_back(Property);
294
295 return Property;
296 }
297};
298
Steve Naroffdac269b2007-08-20 21:31:48 +0000299/// objc-interface-decl-list:
300/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000301/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000302/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000303/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000304/// objc-interface-decl-list declaration
305/// objc-interface-decl-list ';'
306///
Steve Naroff294494e2007-08-22 16:35:03 +0000307/// objc-method-requirement: [OBJC2]
308/// @required
309/// @optional
310///
John McCalld226f652010-08-21 09:40:31 +0000311void Parser::ParseObjCInterfaceDeclList(Decl *interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000312 tok::ObjCKeywordKind contextKey) {
John McCalld226f652010-08-21 09:40:31 +0000313 llvm::SmallVector<Decl *, 32> allMethods;
314 llvm::SmallVector<Decl *, 16> allProperties;
Chris Lattner682bf922009-03-29 16:50:03 +0000315 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000316 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremenek782f2f52010-01-07 01:20:12 +0000318 SourceRange AtEnd;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000319
Steve Naroff294494e2007-08-22 16:35:03 +0000320 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000321 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000322 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000323 Decl *methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000324 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000325 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000326 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
327 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000328 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
329 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000330 continue;
331 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000332 if (Tok.is(tok::l_paren)) {
333 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000334 ParseObjCMethodDecl(Tok.getLocation(),
335 tok::minus,
336 interfaceDecl,
337 MethodImplKind);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000338 continue;
339 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000340 // Ignore excess semicolons.
341 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000342 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000343 continue;
344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattnerbc662af2008-10-20 06:10:06 +0000346 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000347 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000348 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000350 // Code completion within an Objective-C interface.
351 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000352 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000353 ObjCImpDecl? Sema::PCC_ObjCImplementation
354 : Sema::PCC_ObjCInterface);
Douglas Gregordc845342010-05-25 05:58:43 +0000355 ConsumeCodeCompletionToken();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000356 }
357
Chris Lattnere82a10f2008-10-20 05:46:22 +0000358 // If we don't have an @ directive, parse it as a function definition.
359 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000360 // The code below does not consume '}'s because it is afraid of eating the
361 // end of a namespace. Because of the way this code is structured, an
362 // erroneous r_brace would cause an infinite loop if not handled here.
363 if (Tok.is(tok::r_brace))
364 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Steve Naroff4985ace2007-08-22 18:35:33 +0000366 // FIXME: as the name implies, this rule allows function definitions.
367 // We could pass a flag or check for functions during semantic analysis.
Sean Huntbbd37c62009-11-21 08:43:09 +0000368 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000369 continue;
370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Chris Lattnere82a10f2008-10-20 05:46:22 +0000372 // Otherwise, we have an @ directive, eat the @.
373 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000374 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000375 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregordc845342010-05-25 05:58:43 +0000376 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000377 break;
378 }
379
Chris Lattnera2449b22008-10-20 05:57:40 +0000380 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Chris Lattnera2449b22008-10-20 05:57:40 +0000382 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000383 AtEnd.setBegin(AtLoc);
384 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000385 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000386 } else if (DirectiveKind == tok::objc_not_keyword) {
387 Diag(Tok, diag::err_objc_unknown_at);
388 SkipUntil(tok::semi);
389 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Chris Lattnerbc662af2008-10-20 06:10:06 +0000392 // Eat the identifier.
393 ConsumeToken();
394
Chris Lattnera2449b22008-10-20 05:57:40 +0000395 switch (DirectiveKind) {
396 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000397 // FIXME: If someone forgets an @end on a protocol, this loop will
398 // continue to eat up tons of stuff and spew lots of nonsense errors. It
399 // would probably be better to bail out if we saw an @class or @interface
400 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000401 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000402 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000403 SkipUntil(tok::r_brace, tok::at);
404 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Chris Lattnera2449b22008-10-20 05:57:40 +0000406 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000407 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000408 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000409 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000410 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000411 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000412 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000413 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000414 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Chris Lattnera2449b22008-10-20 05:57:40 +0000416 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000417 if (!getLang().ObjC2)
418 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
419
Chris Lattnere82a10f2008-10-20 05:46:22 +0000420 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000421 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000422 if (Tok.is(tok::l_paren))
Douglas Gregor4ad96852009-11-19 07:41:15 +0000423 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
424 allMethods.data(), allMethods.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000425
John McCalld0014542009-12-03 22:31:13 +0000426 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
427 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000428
Chris Lattnere82a10f2008-10-20 05:46:22 +0000429 // Parse all the comma separated declarators.
430 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000431 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000433 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
434 tok::at);
Chris Lattnera2449b22008-10-20 05:57:40 +0000435 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000436 }
Steve Naroff294494e2007-08-22 16:35:03 +0000437 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000438
439 // We break out of the big loop in two cases: when we see @end or when we see
440 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000441 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000442 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregordc845342010-05-25 05:58:43 +0000443 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000444 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000445 ConsumeToken(); // the "end" identifier
446 else
447 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Chris Lattnera2449b22008-10-20 05:57:40 +0000449 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000450 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000451 Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000452 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000453 allProperties.data(), allProperties.size(),
454 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000455}
456
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000457/// Parse property attribute declarations.
458///
459/// property-attr-decl: '(' property-attrlist ')'
460/// property-attrlist:
461/// property-attribute
462/// property-attrlist ',' property-attribute
463/// property-attribute:
464/// getter '=' identifier
465/// setter '=' identifier ':'
466/// readonly
467/// readwrite
468/// assign
469/// retain
470/// copy
471/// nonatomic
472///
John McCalld226f652010-08-21 09:40:31 +0000473void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, Decl *ClassDecl,
474 Decl **Methods,
Douglas Gregor4ad96852009-11-19 07:41:15 +0000475 unsigned NumMethods) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000476 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000477 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000479 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000480 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000481 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Douglas Gregordc845342010-05-25 05:58:43 +0000482 ConsumeCodeCompletionToken();
Steve Naroffece8e712009-10-08 21:55:05 +0000483 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000484 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000486 // If this is not an identifier at all, bail out early.
487 if (II == 0) {
488 MatchRHSPunctuation(tok::r_paren, LHSLoc);
489 return;
490 }
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Chris Lattner156b0612008-10-20 07:37:22 +0000492 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Chris Lattner92e62b02008-11-20 04:42:34 +0000494 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000495 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000496 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000497 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000498 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000499 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000500 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000501 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000502 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000503 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000504 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000505 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000506 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000507 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000508 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
509 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000510 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Douglas Gregor4ad96852009-11-19 07:41:15 +0000512 if (Tok.is(tok::code_completion)) {
513 if (II->getNameStart()[0] == 's')
Douglas Gregor23c94db2010-07-02 17:43:08 +0000514 Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl,
Douglas Gregor4ad96852009-11-19 07:41:15 +0000515 Methods, NumMethods);
516 else
Douglas Gregor23c94db2010-07-02 17:43:08 +0000517 Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl,
Douglas Gregor4ad96852009-11-19 07:41:15 +0000518 Methods, NumMethods);
Douglas Gregordc845342010-05-25 05:58:43 +0000519 ConsumeCodeCompletionToken();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000520 }
521
Chris Lattner8ca329c2008-10-20 07:24:39 +0000522 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000523 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000524 SkipUntil(tok::r_paren);
525 return;
526 }
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Daniel Dunbare013d682009-10-18 20:26:12 +0000528 if (II->getNameStart()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000529 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
530 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000531 ConsumeToken(); // consume method name
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000533 if (ExpectAndConsume(tok::colon,
534 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000535 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000536 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000537 } else {
538 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
539 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000540 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000541 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000542 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000543 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000544 SkipUntil(tok::r_paren);
545 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000546 }
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chris Lattner156b0612008-10-20 07:37:22 +0000548 if (Tok.isNot(tok::comma))
549 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Chris Lattner156b0612008-10-20 07:37:22 +0000551 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000552 }
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Chris Lattner156b0612008-10-20 07:37:22 +0000554 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000555}
556
Steve Naroff3536b442007-09-06 21:24:23 +0000557/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000558/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000559/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000560///
561/// objc-instance-method: '-'
562/// objc-class-method: '+'
563///
Steve Naroff4985ace2007-08-22 18:35:33 +0000564/// objc-method-attributes: [OBJC2]
565/// __attribute__((deprecated))
566///
John McCalld226f652010-08-21 09:40:31 +0000567Decl *Parser::ParseObjCMethodPrototype(Decl *IDecl,
568 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000569 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000570
Mike Stump1eb44332009-09-09 15:08:12 +0000571 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000572 SourceLocation mLoc = ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000573 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000574 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000575 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000576 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000577}
578
579/// objc-selector:
580/// identifier
581/// one of
582/// enum struct union if else while do for switch case default
583/// break continue return goto asm sizeof typeof __alignof
584/// unsigned long const short volatile signed restrict _Complex
585/// in out inout bycopy byref oneway int char float double void _Bool
586///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000587IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000588
Chris Lattnerff384912007-10-07 02:00:24 +0000589 switch (Tok.getKind()) {
590 default:
591 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000592 case tok::ampamp:
593 case tok::ampequal:
594 case tok::amp:
595 case tok::pipe:
596 case tok::tilde:
597 case tok::exclaim:
598 case tok::exclaimequal:
599 case tok::pipepipe:
600 case tok::pipeequal:
601 case tok::caret:
602 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000603 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000604 if (isalpha(ThisTok[0])) {
605 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
606 Tok.setKind(tok::identifier);
607 SelectorLoc = ConsumeToken();
608 return II;
609 }
610 return 0;
611 }
612
Chris Lattnerff384912007-10-07 02:00:24 +0000613 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000614 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000615 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000616 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000617 case tok::kw_break:
618 case tok::kw_case:
619 case tok::kw_catch:
620 case tok::kw_char:
621 case tok::kw_class:
622 case tok::kw_const:
623 case tok::kw_const_cast:
624 case tok::kw_continue:
625 case tok::kw_default:
626 case tok::kw_delete:
627 case tok::kw_do:
628 case tok::kw_double:
629 case tok::kw_dynamic_cast:
630 case tok::kw_else:
631 case tok::kw_enum:
632 case tok::kw_explicit:
633 case tok::kw_export:
634 case tok::kw_extern:
635 case tok::kw_false:
636 case tok::kw_float:
637 case tok::kw_for:
638 case tok::kw_friend:
639 case tok::kw_goto:
640 case tok::kw_if:
641 case tok::kw_inline:
642 case tok::kw_int:
643 case tok::kw_long:
644 case tok::kw_mutable:
645 case tok::kw_namespace:
646 case tok::kw_new:
647 case tok::kw_operator:
648 case tok::kw_private:
649 case tok::kw_protected:
650 case tok::kw_public:
651 case tok::kw_register:
652 case tok::kw_reinterpret_cast:
653 case tok::kw_restrict:
654 case tok::kw_return:
655 case tok::kw_short:
656 case tok::kw_signed:
657 case tok::kw_sizeof:
658 case tok::kw_static:
659 case tok::kw_static_cast:
660 case tok::kw_struct:
661 case tok::kw_switch:
662 case tok::kw_template:
663 case tok::kw_this:
664 case tok::kw_throw:
665 case tok::kw_true:
666 case tok::kw_try:
667 case tok::kw_typedef:
668 case tok::kw_typeid:
669 case tok::kw_typename:
670 case tok::kw_typeof:
671 case tok::kw_union:
672 case tok::kw_unsigned:
673 case tok::kw_using:
674 case tok::kw_virtual:
675 case tok::kw_void:
676 case tok::kw_volatile:
677 case tok::kw_wchar_t:
678 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000679 case tok::kw__Bool:
680 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000681 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000682 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000683 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000684 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000685 }
Steve Naroff294494e2007-08-22 16:35:03 +0000686}
687
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000688/// objc-for-collection-in: 'in'
689///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000690bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000691 // FIXME: May have to do additional look-ahead to only allow for
692 // valid tokens following an 'in'; such as an identifier, unary operators,
693 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000694 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000695 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000696}
697
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000698/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000699/// qualifier list and builds their bitmask representation in the input
700/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000701///
702/// objc-type-qualifiers:
703/// objc-type-qualifier
704/// objc-type-qualifiers objc-type-qualifier
705///
Douglas Gregord32b0222010-08-24 01:06:58 +0000706void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS, bool IsParameter) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000707 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000708 if (Tok.is(tok::code_completion)) {
709 Actions.CodeCompleteObjCPassingType(getCurScope(), DS);
710 ConsumeCodeCompletionToken();
711 }
712
Chris Lattnercb53b362007-12-27 19:57:00 +0000713 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000714 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Chris Lattnere8b724d2007-12-12 06:56:32 +0000716 const IdentifierInfo *II = Tok.getIdentifierInfo();
717 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000718 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000719 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000721 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000722 switch (i) {
723 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000724 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
725 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
726 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
727 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
728 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
729 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000730 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000731 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000732 ConsumeToken();
733 II = 0;
734 break;
735 }
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Chris Lattnere8b724d2007-12-12 06:56:32 +0000737 // If this wasn't a recognized qualifier, bail out.
738 if (II) return;
739 }
740}
741
742/// objc-type-name:
743/// '(' objc-type-qualifiers[opt] type-name ')'
744/// '(' objc-type-qualifiers[opt] ')'
745///
John McCallb3d87482010-08-24 05:47:05 +0000746ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS, bool IsParameter) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000747 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Chris Lattner4a76b292008-10-22 03:52:06 +0000749 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000750 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000752 // Parse type qualifiers, in, inout, etc.
Douglas Gregord32b0222010-08-24 01:06:58 +0000753 ParseObjCTypeQualifierList(DS, IsParameter);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000754
John McCallb3d87482010-08-24 05:47:05 +0000755 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000756 if (isTypeSpecifierQualifier()) {
757 TypeResult TypeSpec = ParseTypeName();
758 if (!TypeSpec.isInvalid())
759 Ty = TypeSpec.get();
760 }
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Steve Naroffd7333c22008-10-21 14:15:04 +0000762 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000763 ConsumeParen();
764 else if (Tok.getLocation() == TypeStartLoc) {
765 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000766 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000767 SkipUntil(tok::r_paren);
768 } else {
769 // Otherwise, we found *something*, but didn't get a ')' in the right
770 // place. Emit an error then return what we have as the type.
771 MatchRHSPunctuation(tok::r_paren, LParenLoc);
772 }
Steve Narofff28b2642007-09-05 23:30:30 +0000773 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000774}
775
776/// objc-method-decl:
777/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000778/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000779/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000780/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000781///
782/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000783/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000784/// objc-keyword-selector objc-keyword-decl
785///
786/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000787/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
788/// objc-selector ':' objc-keyword-attributes[opt] identifier
789/// ':' objc-type-name objc-keyword-attributes[opt] identifier
790/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000791///
Steve Naroff4985ace2007-08-22 18:35:33 +0000792/// objc-parmlist:
793/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000794///
Steve Naroff4985ace2007-08-22 18:35:33 +0000795/// objc-parms:
796/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000797///
Steve Naroff4985ace2007-08-22 18:35:33 +0000798/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000799/// , ...
800///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000801/// objc-keyword-attributes: [OBJC2]
802/// __attribute__((unused))
803///
John McCalld226f652010-08-21 09:40:31 +0000804Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000805 tok::TokenKind mType,
806 Decl *IDecl,
807 tok::ObjCKeywordKind MethodImplKind) {
John McCall54abf7d2009-11-04 02:18:39 +0000808 ParsingDeclRAIIObject PD(*this);
809
Douglas Gregore8f5a172010-04-07 00:21:17 +0000810 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000811 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
John McCallb3d87482010-08-24 05:47:05 +0000812 /*ReturnType=*/ ParsedType(), IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000813 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000814 }
815
Chris Lattnere8904e92008-08-23 01:48:03 +0000816 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000817 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000818 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000819 if (Tok.is(tok::l_paren))
Douglas Gregord32b0222010-08-24 01:06:58 +0000820 ReturnType = ParseObjCTypeName(DSRet, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Ted Kremenek9e049352010-02-18 23:05:16 +0000822 // If attributes exist before the method, parse them.
823 llvm::OwningPtr<AttributeList> MethodAttrs;
824 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
825 MethodAttrs.reset(ParseGNUAttributes());
826
Douglas Gregore8f5a172010-04-07 00:21:17 +0000827 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000828 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Douglas Gregore8f5a172010-04-07 00:21:17 +0000829 ReturnType, IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000830 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000831 }
832
Ted Kremenek9e049352010-02-18 23:05:16 +0000833 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000834 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000835 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000836
Steve Naroff84c43102009-02-11 20:43:13 +0000837 // An unnamed colon is valid.
838 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000839 Diag(Tok, diag::err_expected_selector_for_method)
840 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000841 // Skip until we get a ; or {}.
842 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000843 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000844 }
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000846 llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000847 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000848 // If attributes exist after the method, parse them.
Mike Stump1eb44332009-09-09 15:08:12 +0000849 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek9e049352010-02-18 23:05:16 +0000850 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
851 ParseGNUAttributes()));
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Chris Lattnerff384912007-10-07 02:00:24 +0000853 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000854 Decl *Result
John McCall54abf7d2009-11-04 02:18:39 +0000855 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000856 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000857 0,
858 CParamInfo.data(), CParamInfo.size(),
859 MethodAttrs.get(),
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000860 MethodImplKind);
John McCall54abf7d2009-11-04 02:18:39 +0000861 PD.complete(Result);
862 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000863 }
Steve Narofff28b2642007-09-05 23:30:30 +0000864
Steve Naroff68d331a2007-09-27 14:38:14 +0000865 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
John McCallf312b1e2010-08-26 23:41:50 +0000866 llvm::SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Chris Lattnerff384912007-10-07 02:00:24 +0000868 while (1) {
John McCallf312b1e2010-08-26 23:41:50 +0000869 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Chris Lattnerff384912007-10-07 02:00:24 +0000871 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000872 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000873 Diag(Tok, diag::err_expected_colon);
874 break;
875 }
876 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000877
John McCallb3d87482010-08-24 05:47:05 +0000878 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +0000879 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregord32b0222010-08-24 01:06:58 +0000880 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, true);
Chris Lattnere294d3f2009-04-11 18:57:04 +0000881
Chris Lattnerff384912007-10-07 02:00:24 +0000882 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000883 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000884 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000885 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000886
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000887 // Code completion for the next piece of the selector.
888 if (Tok.is(tok::code_completion)) {
889 ConsumeCodeCompletionToken();
890 KeyIdents.push_back(SelIdent);
891 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
892 mType == tok::minus,
893 /*AtParameterName=*/true,
894 ReturnType,
895 KeyIdents.data(),
896 KeyIdents.size());
897 KeyIdents.pop_back();
898 break;
899 }
900
Chris Lattnerdf195262007-10-09 17:51:17 +0000901 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000902 Diag(Tok, diag::err_expected_ident); // missing argument name.
903 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000904 }
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Chris Lattnere294d3f2009-04-11 18:57:04 +0000906 ArgInfo.Name = Tok.getIdentifierInfo();
907 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000908 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Chris Lattnere294d3f2009-04-11 18:57:04 +0000910 ArgInfos.push_back(ArgInfo);
911 KeyIdents.push_back(SelIdent);
912
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000913 // Code completion for the next piece of the selector.
914 if (Tok.is(tok::code_completion)) {
915 ConsumeCodeCompletionToken();
916 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
917 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000918 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000919 ReturnType,
920 KeyIdents.data(),
921 KeyIdents.size());
922 break;
923 }
924
Chris Lattnerff384912007-10-07 02:00:24 +0000925 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000926 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000927 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000928 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000929 break;
930 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000931 }
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Steve Naroff335eafa2007-11-15 12:35:21 +0000933 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Chris Lattnerff384912007-10-07 02:00:24 +0000935 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000936 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000937 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000938 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000939 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000940 ConsumeToken();
941 break;
942 }
Chris Lattnerff384912007-10-07 02:00:24 +0000943 DeclSpec DS;
944 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000945 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000946 Declarator ParmDecl(DS, Declarator::PrototypeContext);
947 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000948 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +0000949 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000950 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
951 ParmDecl.getIdentifierLoc(),
952 Param,
953 0));
954
Chris Lattnerff384912007-10-07 02:00:24 +0000955 }
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Chris Lattnerff384912007-10-07 02:00:24 +0000957 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000958 // If attributes exist after the method, parse them.
Mike Stump1eb44332009-09-09 15:08:12 +0000959 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek9e049352010-02-18 23:05:16 +0000960 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
961 ParseGNUAttributes()));
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000963 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +0000964 return 0;
Chris Lattnerff384912007-10-07 02:00:24 +0000965 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
966 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +0000967 Decl *Result
John McCall54abf7d2009-11-04 02:18:39 +0000968 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000969 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000970 &ArgInfos[0],
971 CParamInfo.data(), CParamInfo.size(),
Ted Kremenek1e377652010-02-11 02:19:13 +0000972 MethodAttrs.get(),
Steve Naroff335eafa2007-11-15 12:35:21 +0000973 MethodImplKind, isVariadic);
John McCall54abf7d2009-11-04 02:18:39 +0000974 PD.complete(Result);
Ted Kremenek1e377652010-02-11 02:19:13 +0000975
976 // Delete referenced AttributeList objects.
John McCallf312b1e2010-08-26 23:41:50 +0000977 for (llvm::SmallVectorImpl<Sema::ObjCArgInfo>::iterator
Ted Kremenek1e377652010-02-11 02:19:13 +0000978 I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I)
979 delete I->ArgAttrs;
980
John McCall54abf7d2009-11-04 02:18:39 +0000981 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +0000982}
983
Steve Naroffdac269b2007-08-20 21:31:48 +0000984/// objc-protocol-refs:
985/// '<' identifier-list '>'
986///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000987bool Parser::
John McCalld226f652010-08-21 09:40:31 +0000988ParseObjCProtocolReferences(llvm::SmallVectorImpl<Decl *> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000989 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
990 bool WarnOnDeclarations,
991 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000992 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000994 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Chris Lattnere13b9592008-07-26 04:03:38 +0000996 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Chris Lattnere13b9592008-07-26 04:03:38 +0000998 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +0000999 if (Tok.is(tok::code_completion)) {
1000 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1001 ProtocolIdents.size());
Douglas Gregordc845342010-05-25 05:58:43 +00001002 ConsumeCodeCompletionToken();
Douglas Gregor55385fe2009-11-18 04:19:12 +00001003 }
1004
Chris Lattnere13b9592008-07-26 04:03:38 +00001005 if (Tok.isNot(tok::identifier)) {
1006 Diag(Tok, diag::err_expected_ident);
1007 SkipUntil(tok::greater);
1008 return true;
1009 }
1010 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1011 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001012 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001013 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Chris Lattnere13b9592008-07-26 04:03:38 +00001015 if (Tok.isNot(tok::comma))
1016 break;
1017 ConsumeToken();
1018 }
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Chris Lattnere13b9592008-07-26 04:03:38 +00001020 // Consume the '>'.
1021 if (Tok.isNot(tok::greater)) {
1022 Diag(Tok, diag::err_expected_greater);
1023 return true;
1024 }
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Chris Lattnere13b9592008-07-26 04:03:38 +00001026 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Chris Lattnere13b9592008-07-26 04:03:38 +00001028 // Convert the list of protocols identifiers into a list of protocol decls.
1029 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1030 &ProtocolIdents[0], ProtocolIdents.size(),
1031 Protocols);
1032 return false;
1033}
1034
Steve Naroffdac269b2007-08-20 21:31:48 +00001035/// objc-class-instance-variables:
1036/// '{' objc-instance-variable-decl-list[opt] '}'
1037///
1038/// objc-instance-variable-decl-list:
1039/// objc-visibility-spec
1040/// objc-instance-variable-decl ';'
1041/// ';'
1042/// objc-instance-variable-decl-list objc-visibility-spec
1043/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1044/// objc-instance-variable-decl-list ';'
1045///
1046/// objc-visibility-spec:
1047/// @private
1048/// @protected
1049/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001050/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001051///
1052/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001053/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001054///
John McCalld226f652010-08-21 09:40:31 +00001055void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001056 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001057 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001058 assert(Tok.is(tok::l_brace) && "expected {");
John McCalld226f652010-08-21 09:40:31 +00001059 llvm::SmallVector<Decl *, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001060
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001061 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001062
Steve Naroffddbff782007-08-21 21:17:12 +00001063 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Steve Naroffddbff782007-08-21 21:17:12 +00001065 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001066 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001067 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Steve Naroffddbff782007-08-21 21:17:12 +00001069 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001070 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001071 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001072 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001073 ConsumeToken();
1074 continue;
1075 }
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Steve Naroffddbff782007-08-21 21:17:12 +00001077 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001078 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001079 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001080
1081 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001082 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001083 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001084 }
1085
Steve Naroff861cf3e2007-08-23 18:16:40 +00001086 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001087 case tok::objc_private:
1088 case tok::objc_public:
1089 case tok::objc_protected:
1090 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001091 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001092 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001093 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001094 default:
1095 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001096 continue;
1097 }
1098 }
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001100 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001101 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001102 Sema::PCC_ObjCInstanceVariableList);
Douglas Gregordc845342010-05-25 05:58:43 +00001103 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001104 }
1105
John McCallbdd563e2009-11-03 02:38:08 +00001106 struct ObjCIvarCallback : FieldCallback {
1107 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001108 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001109 tok::ObjCKeywordKind visibility;
John McCalld226f652010-08-21 09:40:31 +00001110 llvm::SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001111
John McCalld226f652010-08-21 09:40:31 +00001112 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
1113 llvm::SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001114 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1115 }
1116
John McCalld226f652010-08-21 09:40:31 +00001117 Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001118 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001119 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001120 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001121 FD.D.getDeclSpec().getSourceRange().getBegin(),
1122 IDecl, FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001123 if (Field)
1124 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001125 return Field;
1126 }
1127 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001128
Chris Lattnere1359422008-04-10 06:46:29 +00001129 // Parse all the comma separated declarators.
1130 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +00001131 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Chris Lattnerdf195262007-10-09 17:51:17 +00001133 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001134 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001135 } else {
1136 Diag(Tok, diag::err_expected_semi_decl_list);
1137 // Skip to end of block or statement
1138 SkipUntil(tok::r_brace, true, true);
1139 }
1140 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001141 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001142 Actions.ActOnLastBitfield(RBraceLoc, interfaceDecl, AllIvarDecls);
Steve Naroff8749be52007-10-31 22:11:35 +00001143 // Call ActOnFields() even if we don't have any decls. This is useful
1144 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001145 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001146 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001147 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001148 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001149}
Steve Naroffdac269b2007-08-20 21:31:48 +00001150
1151/// objc-protocol-declaration:
1152/// objc-protocol-definition
1153/// objc-protocol-forward-reference
1154///
1155/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001156/// @protocol identifier
1157/// objc-protocol-refs[opt]
1158/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001159/// @end
1160///
1161/// objc-protocol-forward-reference:
1162/// @protocol identifier-list ';'
1163///
1164/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001165/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001166/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCalld226f652010-08-21 09:40:31 +00001167Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001168 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001169 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001170 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1171 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Douglas Gregor083128f2009-11-18 04:49:41 +00001173 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001174 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001175 ConsumeCodeCompletionToken();
Douglas Gregor083128f2009-11-18 04:49:41 +00001176 }
1177
Chris Lattnerdf195262007-10-09 17:51:17 +00001178 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001179 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCalld226f652010-08-21 09:40:31 +00001180 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001181 }
1182 // Save the protocol name, then consume it.
1183 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1184 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Chris Lattnerdf195262007-10-09 17:51:17 +00001186 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001187 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001188 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001189 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001190 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001191 }
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Chris Lattnerdf195262007-10-09 17:51:17 +00001193 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001194 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1195 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1196
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001197 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001198 while (1) {
1199 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001200 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001201 Diag(Tok, diag::err_expected_ident);
1202 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001203 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001204 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001205 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1206 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001207 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Chris Lattnerdf195262007-10-09 17:51:17 +00001209 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001210 break;
1211 }
1212 // Consume the ';'.
1213 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCalld226f652010-08-21 09:40:31 +00001214 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Steve Naroffe440eb82007-10-10 17:32:04 +00001216 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001217 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001218 ProtocolRefs.size(),
1219 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001220 }
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001222 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001223 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001224
John McCalld226f652010-08-21 09:40:31 +00001225 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001226 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001227 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001228 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1229 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +00001230 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001231
John McCalld226f652010-08-21 09:40:31 +00001232 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001233 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001234 ProtocolRefs.data(),
1235 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001236 ProtocolLocs.data(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001237 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001238 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001239 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001240}
Steve Naroffdac269b2007-08-20 21:31:48 +00001241
1242/// objc-implementation:
1243/// objc-class-implementation-prologue
1244/// objc-category-implementation-prologue
1245///
1246/// objc-class-implementation-prologue:
1247/// @implementation identifier objc-superclass[opt]
1248/// objc-class-instance-variables[opt]
1249///
1250/// objc-category-implementation-prologue:
1251/// @implementation identifier ( identifier )
John McCalld226f652010-08-21 09:40:31 +00001252Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001253 SourceLocation atLoc) {
1254 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1255 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1256 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001258 // Code completion after '@implementation'.
1259 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001260 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001261 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001262 }
1263
Chris Lattnerdf195262007-10-09 17:51:17 +00001264 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001265 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001266 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001267 }
1268 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001269 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001270 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001271
1272 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001273 // we have a category implementation.
1274 SourceLocation lparenLoc = ConsumeParen();
1275 SourceLocation categoryLoc, rparenLoc;
1276 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001278 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001279 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +00001280 ConsumeCodeCompletionToken();
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001281 }
1282
Chris Lattnerdf195262007-10-09 17:51:17 +00001283 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001284 categoryId = Tok.getIdentifierInfo();
1285 categoryLoc = ConsumeToken();
1286 } else {
1287 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001288 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001289 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001290 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001291 Diag(Tok, diag::err_expected_rparen);
1292 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001293 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001294 }
1295 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001296 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001297 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001298 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001299 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001300 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001301 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001302 }
1303 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001304 SourceLocation superClassLoc;
1305 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001306 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001307 // We have a super class
1308 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001309 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001310 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001311 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001312 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001313 superClassId = Tok.getIdentifierInfo();
1314 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001315 }
John McCalld226f652010-08-21 09:40:31 +00001316 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001317 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001318 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Steve Naroff60fccee2007-10-29 21:38:07 +00001320 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001321 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahanian01f1bfc2010-03-22 19:04:14 +00001322 tok::objc_private, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001323 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001324 PendingObjCImpDecl.push_back(ObjCImpDecl);
1325
John McCalld226f652010-08-21 09:40:31 +00001326 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001327}
Steve Naroff60fccee2007-10-29 21:38:07 +00001328
John McCalld226f652010-08-21 09:40:31 +00001329Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001330 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1331 "ParseObjCAtEndDeclaration(): Expected @end");
John McCalld226f652010-08-21 09:40:31 +00001332 Decl *Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001333 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001334 if (ObjCImpDecl) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001335 Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001336 ObjCImpDecl = 0;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001337 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001338 }
Ted Kremenek782f2f52010-01-07 01:20:12 +00001339 else {
1340 // missing @implementation
1341 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1342 }
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001343 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001344}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001345
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001346Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1347 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001348 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001349 return Actions.ConvertDeclToDeclGroup(0);
1350 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001351 Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001352 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1353}
1354
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001355/// compatibility-alias-decl:
1356/// @compatibility_alias alias-name class-name ';'
1357///
John McCalld226f652010-08-21 09:40:31 +00001358Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001359 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1360 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1361 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001362 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001363 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001364 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001365 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001366 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1367 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001368 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001369 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001370 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001371 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001372 IdentifierInfo *classId = Tok.getIdentifierInfo();
1373 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1374 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001375 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
John McCalld226f652010-08-21 09:40:31 +00001376 return 0;
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001377 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001378 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1379 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001380}
1381
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001382/// property-synthesis:
1383/// @synthesize property-ivar-list ';'
1384///
1385/// property-ivar-list:
1386/// property-ivar
1387/// property-ivar-list ',' property-ivar
1388///
1389/// property-ivar:
1390/// identifier
1391/// identifier '=' identifier
1392///
John McCalld226f652010-08-21 09:40:31 +00001393Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001394 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1395 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001396 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Douglas Gregorb328c422009-11-18 19:45:45 +00001398 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001399 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001400 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001401 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001402 }
1403
Douglas Gregorb328c422009-11-18 19:45:45 +00001404 if (Tok.isNot(tok::identifier)) {
1405 Diag(Tok, diag::err_synthesized_property_name);
1406 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001407 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001408 }
1409
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001410 IdentifierInfo *propertyIvar = 0;
1411 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1412 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001413 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001414 // property '=' ivar-name
1415 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001416
1417 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001418 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId,
Douglas Gregor322328b2009-11-18 22:32:06 +00001419 ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001420 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001421 }
1422
Chris Lattnerdf195262007-10-09 17:51:17 +00001423 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001424 Diag(Tok, diag::err_expected_ident);
1425 break;
1426 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001427 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001428 ConsumeToken(); // consume ivar-name
1429 }
Douglas Gregor23c94db2010-07-02 17:43:08 +00001430 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl,
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001431 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001432 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001433 break;
1434 ConsumeToken(); // consume ','
1435 }
Douglas Gregorb328c422009-11-18 19:45:45 +00001436 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001437 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregorb328c422009-11-18 19:45:45 +00001438 SkipUntil(tok::semi);
1439 }
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001440 else
1441 ConsumeToken(); // consume ';'
John McCalld226f652010-08-21 09:40:31 +00001442 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001443}
1444
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001445/// property-dynamic:
1446/// @dynamic property-list
1447///
1448/// property-list:
1449/// identifier
1450/// property-list ',' identifier
1451///
John McCalld226f652010-08-21 09:40:31 +00001452Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001453 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1454 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1455 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001456 while (true) {
1457 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001458 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001459 ConsumeCodeCompletionToken();
Douglas Gregor424b2a52009-11-18 22:56:13 +00001460 }
1461
1462 if (Tok.isNot(tok::identifier)) {
1463 Diag(Tok, diag::err_expected_ident);
1464 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001465 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001466 }
1467
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001468 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1469 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregor23c94db2010-07-02 17:43:08 +00001470 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl,
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001471 propertyId, 0);
1472
Chris Lattnerdf195262007-10-09 17:51:17 +00001473 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001474 break;
1475 ConsumeToken(); // consume ','
1476 }
Fariborz Jahanian94b24db2010-04-14 20:52:42 +00001477 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001478 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanian94b24db2010-04-14 20:52:42 +00001479 SkipUntil(tok::semi);
1480 }
1481 else
1482 ConsumeToken(); // consume ';'
John McCalld226f652010-08-21 09:40:31 +00001483 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001484}
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001486/// objc-throw-statement:
1487/// throw expression[opt];
1488///
John McCall60d7b3a2010-08-24 06:29:42 +00001489StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1490 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001491 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001492 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001493 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001494 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001495 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001496 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001497 }
1498 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001499 // consume ';'
1500 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001501 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001502}
1503
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001504/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001505/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001506///
John McCall60d7b3a2010-08-24 06:29:42 +00001507StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001508Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001509 ConsumeToken(); // consume synchronized
1510 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001511 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001512 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001513 }
1514 ConsumeParen(); // '('
John McCall60d7b3a2010-08-24 06:29:42 +00001515 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001516 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001517 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001518 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001519 }
1520 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001521 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001522 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001523 }
1524 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001525 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001526 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001527 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001528 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001529 // Enter a scope to hold everything within the compound stmt. Compound
1530 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001531 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001532
John McCall60d7b3a2010-08-24 06:29:42 +00001533 StmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001534
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001535 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001536 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001537 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
John McCall9ae2f072010-08-23 23:25:46 +00001538 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.take(), SynchBody.take());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001539}
1540
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001541/// objc-try-catch-statement:
1542/// @try compound-statement objc-catch-list[opt]
1543/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1544///
1545/// objc-catch-list:
1546/// @catch ( parameter-declaration ) compound-statement
1547/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1548/// catch-parameter-declaration:
1549/// parameter-declaration
1550/// '...' [OBJC2]
1551///
John McCall60d7b3a2010-08-24 06:29:42 +00001552StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001553 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001554
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001555 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001556 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001557 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001558 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001559 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001560 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001561 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001562 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001563 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001564 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001565 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001566 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001567
Chris Lattnerdf195262007-10-09 17:51:17 +00001568 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001569 // At this point, we need to lookahead to determine if this @ is the start
1570 // of an @catch or @finally. We don't want to consume the @ token if this
1571 // is an @try or @encode or something else.
1572 Token AfterAt = GetLookAheadToken(1);
1573 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1574 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1575 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001576
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001577 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001578 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001579 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001580 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001581 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001582 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001583 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001584 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001585 DeclSpec DS;
1586 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001587 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001588 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001589 // we must accept either 'declarator' or 'abstract-declarator' here.
1590 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1591 ParseDeclarator(ParmDecl);
1592
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001593 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001594 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001595 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001596 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001597 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Steve Naroff93a25952009-04-07 22:56:58 +00001599 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Steve Naroff93a25952009-04-07 22:56:58 +00001601 if (Tok.is(tok::r_paren))
1602 RParenLoc = ConsumeParen();
1603 else // Skip over garbage, until we get to ')'. Eat the ')'.
1604 SkipUntil(tok::r_paren, true, false);
1605
John McCall60d7b3a2010-08-24 06:29:42 +00001606 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001607 if (Tok.is(tok::l_brace))
1608 CatchBody = ParseCompoundStatementBody();
1609 else
1610 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001611 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001612 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001613
John McCall60d7b3a2010-08-24 06:29:42 +00001614 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001615 RParenLoc,
1616 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001617 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001618 if (!Catch.isInvalid())
1619 CatchStmts.push_back(Catch.release());
1620
Steve Naroff64515f32008-02-05 21:27:35 +00001621 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001622 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1623 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001624 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001625 }
1626 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001627 } else {
1628 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001629 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001630 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001631
John McCall60d7b3a2010-08-24 06:29:42 +00001632 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001633 if (Tok.is(tok::l_brace))
1634 FinallyBody = ParseCompoundStatementBody();
1635 else
1636 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001637 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001638 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001639 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001640 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001641 catch_or_finally_seen = true;
1642 break;
1643 }
1644 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001645 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001646 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001647 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001648 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001649
John McCall9ae2f072010-08-23 23:25:46 +00001650 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001651 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001652 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001653}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001654
Steve Naroff3536b442007-09-06 21:24:23 +00001655/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001656///
John McCalld226f652010-08-21 09:40:31 +00001657Decl *Parser::ParseObjCMethodDefinition() {
1658 Decl *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001659
John McCallf312b1e2010-08-26 23:41:50 +00001660 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1661 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001663 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001664 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001665 if (ObjCImpDecl) {
1666 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001667 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001668 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001669 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001670 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001671
Steve Naroff409be832007-11-11 19:54:21 +00001672 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001673 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001674 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001675
Steve Naroff409be832007-11-11 19:54:21 +00001676 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1677 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Steve Naroff409be832007-11-11 19:54:21 +00001679 // If we didn't find the '{', bail out.
1680 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001681 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001682 }
Steve Naroff409be832007-11-11 19:54:21 +00001683 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Steve Naroff409be832007-11-11 19:54:21 +00001685 // Enter a scope for the method body.
Chris Lattner15faee12010-04-12 05:38:43 +00001686 ParseScope BodyScope(this,
1687 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Steve Naroff409be832007-11-11 19:54:21 +00001689 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001690 // specified Declarator for the method.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001691 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001692
John McCall60d7b3a2010-08-24 06:29:42 +00001693 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001694
Steve Naroff409be832007-11-11 19:54:21 +00001695 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001696 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001697 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1698 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001699
Steve Naroff32ce8372009-03-02 22:00:56 +00001700 // TODO: Pass argument information.
John McCall9ae2f072010-08-23 23:25:46 +00001701 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Steve Naroff409be832007-11-11 19:54:21 +00001703 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001704 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001705
Steve Naroff71c0a952007-11-13 23:01:27 +00001706 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001707}
Anders Carlsson55085182007-08-21 17:43:55 +00001708
John McCall60d7b3a2010-08-24 06:29:42 +00001709StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001710 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001711 Actions.CodeCompleteObjCAtStatement(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001712 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001713 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001714 }
1715
1716 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001717 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001718
1719 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001720 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001721
1722 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001723 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001724
John McCall60d7b3a2010-08-24 06:29:42 +00001725 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001726 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001727 // If the expression is invalid, skip ahead to the next semicolon. Not
1728 // doing this opens us up to the possibility of infinite loops if
1729 // ParseExpression does not consume any tokens.
1730 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001731 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001732 }
Chris Lattner5d803162009-12-07 16:33:19 +00001733
Steve Naroff64515f32008-02-05 21:27:35 +00001734 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001735 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001736 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001737}
1738
John McCall60d7b3a2010-08-24 06:29:42 +00001739ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001740 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001741 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001742 Actions.CodeCompleteObjCAtExpression(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001743 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001744 return ExprError();
1745
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001746 case tok::string_literal: // primary-expression: string-literal
1747 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001748 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001749 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001750 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001751 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001752
Chris Lattner4fef81d2008-08-05 06:19:09 +00001753 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1754 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001755 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001756 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001757 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001758 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001759 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001760 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001761 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001762 }
Anders Carlsson55085182007-08-21 17:43:55 +00001763 }
Anders Carlsson55085182007-08-21 17:43:55 +00001764}
1765
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001766/// \brirg Parse the receiver of an Objective-C++ message send.
1767///
1768/// This routine parses the receiver of a message send in
1769/// Objective-C++ either as a type or as an expression. Note that this
1770/// routine must not be called to parse a send to 'super', since it
1771/// has no way to return such a result.
1772///
1773/// \param IsExpr Whether the receiver was parsed as an expression.
1774///
1775/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1776/// IsExpr is true), the parsed expression. If the receiver was parsed
1777/// as a type (\c IsExpr is false), the parsed type.
1778///
1779/// \returns True if an error occurred during parsing or semantic
1780/// analysis, in which case the arguments do not have valid
1781/// values. Otherwise, returns false for a successful parse.
1782///
1783/// objc-receiver: [C++]
1784/// 'super' [not parsed here]
1785/// expression
1786/// simple-type-specifier
1787/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001788bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001789 InMessageExpressionRAIIObject InMessage(*this, true);
1790
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001791 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1792 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1793 TryAnnotateTypeOrScopeToken();
1794
1795 if (!isCXXSimpleTypeSpecifier()) {
1796 // objc-receiver:
1797 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00001798 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001799 if (Receiver.isInvalid())
1800 return true;
1801
1802 IsExpr = true;
1803 TypeOrExpr = Receiver.take();
1804 return false;
1805 }
1806
1807 // objc-receiver:
1808 // typename-specifier
1809 // simple-type-specifier
1810 // expression (that starts with one of the above)
1811 DeclSpec DS;
1812 ParseCXXSimpleTypeSpecifier(DS);
1813
1814 if (Tok.is(tok::l_paren)) {
1815 // If we see an opening parentheses at this point, we are
1816 // actually parsing an expression that starts with a
1817 // function-style cast, e.g.,
1818 //
1819 // postfix-expression:
1820 // simple-type-specifier ( expression-list [opt] )
1821 // typename-specifier ( expression-list [opt] )
1822 //
1823 // Parse the remainder of this case, then the (optional)
1824 // postfix-expression suffix, followed by the (optional)
1825 // right-hand side of the binary expression. We have an
1826 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00001827 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001828 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001829 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001830 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001831 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001832 if (Receiver.isInvalid())
1833 return true;
1834
1835 IsExpr = true;
1836 TypeOrExpr = Receiver.take();
1837 return false;
1838 }
1839
1840 // We have a class message. Turn the simple-type-specifier or
1841 // typename-specifier we parsed into a type and parse the
1842 // remainder of the class message.
1843 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001844 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001845 if (Type.isInvalid())
1846 return true;
1847
1848 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00001849 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001850 return false;
1851}
1852
Douglas Gregor1b730e82010-05-31 14:40:22 +00001853/// \brief Determine whether the parser is currently referring to a an
1854/// Objective-C message send, using a simplified heuristic to avoid overhead.
1855///
1856/// This routine will only return true for a subset of valid message-send
1857/// expressions.
1858bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00001859 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00001860 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00001861 return GetLookAheadToken(1).is(tok::identifier) &&
1862 GetLookAheadToken(2).is(tok::identifier);
1863}
1864
Mike Stump1eb44332009-09-09 15:08:12 +00001865/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001866/// '[' objc-receiver objc-message-args ']'
1867///
Douglas Gregor2725ca82010-04-21 19:57:20 +00001868/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00001869/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001870/// expression
1871/// class-name
1872/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001873///
John McCall60d7b3a2010-08-24 06:29:42 +00001874ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001875 assert(Tok.is(tok::l_square) && "'[' expected");
1876 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1877
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001878 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001879 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001880 ConsumeCodeCompletionToken();
1881 SkipUntil(tok::r_square);
1882 return ExprError();
1883 }
1884
Douglas Gregor0fbda682010-09-15 14:51:05 +00001885 InMessageExpressionRAIIObject InMessage(*this, true);
1886
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001887 if (getLang().CPlusPlus) {
1888 // We completely separate the C and C++ cases because C++ requires
1889 // more complicated (read: slower) parsing.
1890
1891 // Handle send to super.
1892 // FIXME: This doesn't benefit from the same typo-correction we
1893 // get in Objective-C.
1894 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001895 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00001896 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1897 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001898
1899 // Parse the receiver, which is either a type or an expression.
1900 bool IsExpr;
1901 void *TypeOrExpr;
1902 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
1903 SkipUntil(tok::r_square);
1904 return ExprError();
1905 }
1906
1907 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00001908 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1909 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00001910 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001911
1912 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00001913 ParsedType::getFromOpaquePtr(TypeOrExpr),
1914 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00001915 }
1916
1917 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001918 IdentifierInfo *Name = Tok.getIdentifierInfo();
1919 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00001920 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001921 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001922 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00001923 NextToken().is(tok::period),
1924 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00001925 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00001926 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1927 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00001928
John McCallf312b1e2010-08-26 23:41:50 +00001929 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00001930 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001931 SkipUntil(tok::r_square);
1932 return ExprError();
1933 }
1934
Douglas Gregor1569f952010-04-21 20:38:13 +00001935 ConsumeToken(); // the type name
1936
1937 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00001938 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001939
John McCallf312b1e2010-08-26 23:41:50 +00001940 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00001941 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001942 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001943 }
Chris Lattner699b6612008-01-25 18:59:06 +00001944 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00001945
1946 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00001947 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001948 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001949 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001950 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001951 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001952
John McCallb3d87482010-08-24 05:47:05 +00001953 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1954 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00001955}
Sebastian Redl1d922962008-12-13 15:32:12 +00001956
Douglas Gregor2725ca82010-04-21 19:57:20 +00001957/// \brief Parse the remainder of an Objective-C message following the
1958/// '[' objc-receiver.
1959///
1960/// This routine handles sends to super, class messages (sent to a
1961/// class name), and instance messages (sent to an object), and the
1962/// target is represented by \p SuperLoc, \p ReceiverType, or \p
1963/// ReceiverExpr, respectively. Only one of these parameters may have
1964/// a valid value.
1965///
1966/// \param LBracLoc The location of the opening '['.
1967///
1968/// \param SuperLoc If this is a send to 'super', the location of the
1969/// 'super' keyword that indicates a send to the superclass.
1970///
1971/// \param ReceiverType If this is a class message, the type of the
1972/// class we are sending a message to.
1973///
1974/// \param ReceiverExpr If this is an instance message, the expression
1975/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00001976///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001977/// objc-message-args:
1978/// objc-selector
1979/// objc-keywordarg-list
1980///
1981/// objc-keywordarg-list:
1982/// objc-keywordarg
1983/// objc-keywordarg-list objc-keywordarg
1984///
Mike Stump1eb44332009-09-09 15:08:12 +00001985/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001986/// selector-name[opt] ':' objc-keywordexpr
1987///
1988/// objc-keywordexpr:
1989/// nonempty-expr-list
1990///
1991/// nonempty-expr-list:
1992/// assignment-expression
1993/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001994///
John McCall60d7b3a2010-08-24 06:29:42 +00001995ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001996Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00001997 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00001998 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00001999 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002000 InMessageExpressionRAIIObject InMessage(*this, true);
2001
Steve Naroffc4df6d22009-11-07 02:08:14 +00002002 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002003 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002004 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002005 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002006 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002007 else
John McCall9ae2f072010-08-23 23:25:46 +00002008 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002009 0, 0);
Douglas Gregordc845342010-05-25 05:58:43 +00002010 ConsumeCodeCompletionToken();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002011 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002012
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002013 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002014 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002015 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002016
Anders Carlssonff975cf2009-02-14 18:21:46 +00002017 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Steve Naroff68d331a2007-09-27 14:38:14 +00002019 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002020 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002021
Chris Lattnerdf195262007-10-09 17:51:17 +00002022 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002023 while (1) {
2024 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002025 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00002026
Chris Lattnerdf195262007-10-09 17:51:17 +00002027 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002028 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002029 // We must manually skip to a ']', otherwise the expression skipper will
2030 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2031 // the enclosing expression.
2032 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002033 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002034 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002035
Steve Naroff68d331a2007-09-27 14:38:14 +00002036 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002037 /// Parse the expression after ':'
John McCall60d7b3a2010-08-24 06:29:42 +00002038 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002039 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002040 // We must manually skip to a ']', otherwise the expression skipper will
2041 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2042 // the enclosing expression.
2043 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002044 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002045 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002046
Steve Naroff37387c92007-09-17 20:25:27 +00002047 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002048 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002049
Douglas Gregord3c68542009-11-19 01:08:35 +00002050 // Code completion after each argument.
2051 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002052 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002053 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002054 KeyIdents.data(),
2055 KeyIdents.size());
2056 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002057 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002058 KeyIdents.data(),
2059 KeyIdents.size());
2060 else
John McCall9ae2f072010-08-23 23:25:46 +00002061 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002062 KeyIdents.data(),
2063 KeyIdents.size());
Douglas Gregordc845342010-05-25 05:58:43 +00002064 ConsumeCodeCompletionToken();
Douglas Gregord3c68542009-11-19 01:08:35 +00002065 }
2066
Steve Naroff37387c92007-09-17 20:25:27 +00002067 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002068 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002069 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002070 break;
2071 // We have a selector or a colon, continue parsing.
2072 }
2073 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002074 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002075 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002076 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002077 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002078 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002079 // We must manually skip to a ']', otherwise the expression skipper will
2080 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2081 // the enclosing expression.
2082 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002083 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002084 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002085
Steve Naroff49f109c2007-11-15 13:05:42 +00002086 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002087 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002088 }
2089 } else if (!selIdent) {
2090 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002091
Chris Lattner4fef81d2008-08-05 06:19:09 +00002092 // We must manually skip to a ']', otherwise the expression skipper will
2093 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2094 // the enclosing expression.
2095 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002096 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002097 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002098
Chris Lattnerdf195262007-10-09 17:51:17 +00002099 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002100 if (Tok.is(tok::identifier))
2101 Diag(Tok, diag::err_expected_colon);
2102 else
2103 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002104 // We must manually skip to a ']', otherwise the expression skipper will
2105 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2106 // the enclosing expression.
2107 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002108 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002109 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002110
Chris Lattner699b6612008-01-25 18:59:06 +00002111 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002112
Steve Naroff29238a02007-10-05 18:42:47 +00002113 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00002114 if (nKeys == 0)
2115 KeyIdents.push_back(selIdent);
2116 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002117
Douglas Gregor2725ca82010-04-21 19:57:20 +00002118 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002119 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002120 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002121 MultiExprArg(Actions,
2122 KeyExprs.take(),
2123 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002124 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002125 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002126 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002127 MultiExprArg(Actions,
2128 KeyExprs.take(),
2129 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002130 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002131 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002132 MultiExprArg(Actions,
2133 KeyExprs.take(),
2134 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002135}
2136
John McCall60d7b3a2010-08-24 06:29:42 +00002137ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2138 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002139 if (Res.isInvalid()) return move(Res);
2140
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002141 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2142 // expressions. At this point, we know that the only valid thing that starts
2143 // with '@' is an @"".
2144 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002145 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002146 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002147 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002148
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002149 while (Tok.is(tok::at)) {
2150 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002151
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002152 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002153 if (!isTokenStringLiteral())
2154 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002155
John McCall60d7b3a2010-08-24 06:29:42 +00002156 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002157 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002158 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002159
Sebastian Redleffa8d12008-12-10 00:02:53 +00002160 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002161 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002162
2163 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2164 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002165}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002166
2167/// objc-encode-expression:
2168/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002169ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002170Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002171 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002172
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002173 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002174
Chris Lattner4fef81d2008-08-05 06:19:09 +00002175 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002176 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2177
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002178 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002179
Douglas Gregor809070a2009-02-18 17:45:20 +00002180 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002181
Anders Carlsson4988ae32007-08-23 15:31:37 +00002182 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00002183
Douglas Gregor809070a2009-02-18 17:45:20 +00002184 if (Ty.isInvalid())
2185 return ExprError();
2186
Mike Stump1eb44332009-09-09 15:08:12 +00002187 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00002188 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002189}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002190
2191/// objc-protocol-expression
2192/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002193ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002194Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002195 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002196
Chris Lattner4fef81d2008-08-05 06:19:09 +00002197 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002198 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2199
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002200 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002201
Chris Lattner4fef81d2008-08-05 06:19:09 +00002202 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002203 return ExprError(Diag(Tok, diag::err_expected_ident));
2204
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002205 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002206 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002207
Anders Carlsson4988ae32007-08-23 15:31:37 +00002208 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002209
Sebastian Redl1d922962008-12-13 15:32:12 +00002210 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2211 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002212}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002213
2214/// objc-selector-expression
2215/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002216ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002217 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002218
Chris Lattner4fef81d2008-08-05 06:19:09 +00002219 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002220 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2221
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002222 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002223 SourceLocation LParenLoc = ConsumeParen();
2224 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002225
2226 if (Tok.is(tok::code_completion)) {
2227 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2228 KeyIdents.size());
2229 ConsumeCodeCompletionToken();
2230 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2231 return ExprError();
2232 }
2233
Chris Lattner2fc5c242009-04-11 18:13:45 +00002234 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002235 if (!SelIdent && // missing selector name.
2236 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002237 return ExprError(Diag(Tok, diag::err_expected_ident));
2238
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002239 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002240 unsigned nColons = 0;
2241 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002242 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002243 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2244 ++nColons;
2245 KeyIdents.push_back(0);
2246 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002247 return ExprError(Diag(Tok, diag::err_expected_colon));
2248
Chris Lattner5add7542010-08-27 22:32:41 +00002249 ++nColons;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002250 ConsumeToken(); // Eat the ':'.
2251 if (Tok.is(tok::r_paren))
2252 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002253
2254 if (Tok.is(tok::code_completion)) {
2255 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2256 KeyIdents.size());
2257 ConsumeCodeCompletionToken();
2258 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2259 return ExprError();
2260 }
2261
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002262 // Check for another keyword selector.
2263 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002264 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002265 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002266 if (!SelIdent && Tok.isNot(tok::colon))
2267 break;
2268 }
Steve Naroff887407e2007-12-05 22:21:29 +00002269 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002270 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00002271 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002272 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2273 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00002274 }