blob: 6861ce940f7657b88134bd92418faccfcd8792d4 [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"
16#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000017#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000018#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/SmallVector.h"
20using namespace clang;
21
22
Chris Lattner891dca62008-12-08 21:53:24 +000023/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000024/// external-declaration: [C99 6.9]
25/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000026/// [OBJC] objc-class-declaration
27/// [OBJC] objc-alias-declaration
28/// [OBJC] objc-protocol-definition
29/// [OBJC] objc-method-definition
30/// [OBJC] '@' 'end'
John McCalld226f652010-08-21 09:40:31 +000031Decl *Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +000033
Douglas Gregorc464ae82009-12-07 09:27:33 +000034 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000035 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, false);
Douglas Gregordc845342010-05-25 05:58:43 +000036 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +000037 }
38
Steve Naroff861cf3e2007-08-23 18:16:40 +000039 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000040 case tok::objc_class:
41 return ParseObjCAtClassDeclaration(AtLoc);
42 case tok::objc_interface:
43 return ParseObjCAtInterfaceDeclaration(AtLoc);
44 case tok::objc_protocol:
45 return ParseObjCAtProtocolDeclaration(AtLoc);
46 case tok::objc_implementation:
47 return ParseObjCAtImplementationDeclaration(AtLoc);
48 case tok::objc_end:
49 return ParseObjCAtEndDeclaration(AtLoc);
50 case tok::objc_compatibility_alias:
51 return ParseObjCAtAliasDeclaration(AtLoc);
52 case tok::objc_synthesize:
53 return ParseObjCPropertySynthesize(AtLoc);
54 case tok::objc_dynamic:
55 return ParseObjCPropertyDynamic(AtLoc);
56 default:
57 Diag(AtLoc, diag::err_unexpected_at);
58 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +000059 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000060 }
61}
62
63///
Mike Stump1eb44332009-09-09 15:08:12 +000064/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000065/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000066///
John McCalld226f652010-08-21 09:40:31 +000067Decl *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000068 ConsumeToken(); // the identifier "class"
69 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremenekc09cba62009-11-17 23:12:20 +000070 llvm::SmallVector<SourceLocation, 8> ClassLocs;
71
Mike Stump1eb44332009-09-09 15:08:12 +000072
Reid Spencer5f016e22007-07-11 17:01:13 +000073 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000074 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000075 Diag(Tok, diag::err_expected_ident);
76 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +000077 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000078 }
Reid Spencer5f016e22007-07-11 17:01:13 +000079 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +000080 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000081 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +000082
Chris Lattnerdf195262007-10-09 17:51:17 +000083 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000084 break;
Mike Stump1eb44332009-09-09 15:08:12 +000085
Reid Spencer5f016e22007-07-11 17:01:13 +000086 ConsumeToken();
87 }
Mike Stump1eb44332009-09-09 15:08:12 +000088
Reid Spencer5f016e22007-07-11 17:01:13 +000089 // Consume the ';'.
90 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
John McCalld226f652010-08-21 09:40:31 +000091 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000092
Ted Kremenekc09cba62009-11-17 23:12:20 +000093 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
94 ClassLocs.data(),
95 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000096}
97
Steve Naroffdac269b2007-08-20 21:31:48 +000098///
99/// objc-interface:
100/// objc-class-interface-attributes[opt] objc-class-interface
101/// objc-category-interface
102///
103/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000104/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000105/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000106/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000107/// objc-interface-decl-list
108/// @end
109///
110/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000111/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000112/// objc-protocol-refs[opt]
113/// objc-interface-decl-list
114/// @end
115///
116/// objc-superclass:
117/// ':' identifier
118///
119/// objc-class-interface-attributes:
120/// __attribute__((visibility("default")))
121/// __attribute__((visibility("hidden")))
122/// __attribute__((deprecated))
123/// __attribute__((unavailable))
124/// __attribute__((objc_exception)) - used by NSException on 64-bit
125///
John McCalld226f652010-08-21 09:40:31 +0000126Decl *Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroffdac269b2007-08-20 21:31:48 +0000127 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000128 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000129 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
130 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000132 // Code completion after '@interface'.
133 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000134 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000135 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000136 }
137
Chris Lattnerdf195262007-10-09 17:51:17 +0000138 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000139 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000140 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000141 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000142
Steve Naroffdac269b2007-08-20 21:31:48 +0000143 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000144 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000145 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000146 if (Tok.is(tok::l_paren) &&
147 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000148 SourceLocation lparenLoc = ConsumeParen();
149 SourceLocation categoryLoc, rparenLoc;
150 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000151 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000152 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +0000153 ConsumeCodeCompletionToken();
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000154 }
155
Steve Naroff527fe232007-08-23 19:56:30 +0000156 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000157 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000158 categoryId = Tok.getIdentifierInfo();
159 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000160 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000161 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000162 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000163 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000164 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000165 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000166 Diag(Tok, diag::err_expected_rparen);
167 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +0000168 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000169 }
170 rparenLoc = ConsumeParen();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000171 // Next, we need to check for any protocol references.
172 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +0000173 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000174 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
175 if (Tok.is(tok::less) &&
176 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000177 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000178 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000180 if (attrList) // categories don't support attributes.
181 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000182
John McCalld226f652010-08-21 09:40:31 +0000183 Decl *CategoryType =
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000184 Actions.ActOnStartCategoryInterface(atLoc,
185 nameId, nameLoc,
186 categoryId, categoryLoc,
187 ProtocolRefs.data(),
188 ProtocolRefs.size(),
189 ProtocolLocs.data(),
190 EndProtoLoc);
191 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000192 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
193 atLoc);
194
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000195 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
196 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000197 }
198 // Parse a class interface.
199 IdentifierInfo *superClassId = 0;
200 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000201
Chris Lattnerdf195262007-10-09 17:51:17 +0000202 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000203 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000204
205 // Code completion of superclass names.
206 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000207 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +0000208 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000209 }
210
Chris Lattnerdf195262007-10-09 17:51:17 +0000211 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000212 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000213 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000214 }
215 superClassId = Tok.getIdentifierInfo();
216 superClassLoc = ConsumeToken();
217 }
218 // Next, we need to check for any protocol references.
John McCalld226f652010-08-21 09:40:31 +0000219 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000220 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
221 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000222 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000223 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
224 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000225 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000226
John McCalld226f652010-08-21 09:40:31 +0000227 Decl *ClsType =
Mike Stump1eb44332009-09-09 15:08:12 +0000228 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000229 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000230 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000231 ProtocolLocs.data(),
Chris Lattner06036d32008-07-26 04:13:19 +0000232 EndProtoLoc, attrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Chris Lattnerdf195262007-10-09 17:51:17 +0000234 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000235 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000236
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000237 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000238 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000239}
240
John McCalld0014542009-12-03 22:31:13 +0000241/// The Objective-C property callback. This should be defined where
242/// it's used, but instead it's been lifted to here to support VS2005.
243struct Parser::ObjCPropertyCallback : FieldCallback {
244 Parser &P;
John McCalld226f652010-08-21 09:40:31 +0000245 Decl *IDecl;
246 llvm::SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000247 ObjCDeclSpec &OCDS;
248 SourceLocation AtLoc;
249 tok::ObjCKeywordKind MethodImplKind;
250
John McCalld226f652010-08-21 09:40:31 +0000251 ObjCPropertyCallback(Parser &P, Decl *IDecl,
252 llvm::SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000253 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
254 tok::ObjCKeywordKind MethodImplKind) :
255 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
256 MethodImplKind(MethodImplKind) {
257 }
258
John McCalld226f652010-08-21 09:40:31 +0000259 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000260 if (FD.D.getIdentifier() == 0) {
261 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
262 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000263 return 0;
John McCalld0014542009-12-03 22:31:13 +0000264 }
265 if (FD.BitfieldSize) {
266 P.Diag(AtLoc, diag::err_objc_property_bitfield)
267 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000268 return 0;
John McCalld0014542009-12-03 22:31:13 +0000269 }
270
271 // Install the property declarator into interfaceDecl.
272 IdentifierInfo *SelName =
273 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
274
275 Selector GetterSel =
276 P.PP.getSelectorTable().getNullarySelector(SelName);
277 IdentifierInfo *SetterName = OCDS.getSetterName();
278 Selector SetterSel;
279 if (SetterName)
280 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
281 else
282 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
283 P.PP.getSelectorTable(),
284 FD.D.getIdentifier());
285 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000286 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000287 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
John McCalld0014542009-12-03 22:31:13 +0000288 GetterSel, SetterSel, IDecl,
289 &isOverridingProperty,
290 MethodImplKind);
291 if (!isOverridingProperty)
292 Props.push_back(Property);
293
294 return Property;
295 }
296};
297
Steve Naroffdac269b2007-08-20 21:31:48 +0000298/// objc-interface-decl-list:
299/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000300/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000301/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000302/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000303/// objc-interface-decl-list declaration
304/// objc-interface-decl-list ';'
305///
Steve Naroff294494e2007-08-22 16:35:03 +0000306/// objc-method-requirement: [OBJC2]
307/// @required
308/// @optional
309///
John McCalld226f652010-08-21 09:40:31 +0000310void Parser::ParseObjCInterfaceDeclList(Decl *interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000311 tok::ObjCKeywordKind contextKey) {
John McCalld226f652010-08-21 09:40:31 +0000312 llvm::SmallVector<Decl *, 32> allMethods;
313 llvm::SmallVector<Decl *, 16> allProperties;
Chris Lattner682bf922009-03-29 16:50:03 +0000314 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000315 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Ted Kremenek782f2f52010-01-07 01:20:12 +0000317 SourceRange AtEnd;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000318
Steve Naroff294494e2007-08-22 16:35:03 +0000319 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000320 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000321 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000322 Decl *methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000323 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000324 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000325 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
326 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000327 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
328 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000329 continue;
330 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000331 if (Tok.is(tok::l_paren)) {
332 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000333 ParseObjCMethodDecl(Tok.getLocation(),
334 tok::minus,
335 interfaceDecl,
336 MethodImplKind);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000337 continue;
338 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000339 // Ignore excess semicolons.
340 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000341 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000342 continue;
343 }
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Chris Lattnerbc662af2008-10-20 06:10:06 +0000345 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000346 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000347 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000349 // Code completion within an Objective-C interface.
350 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000351 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000352 ObjCImpDecl? Sema::PCC_ObjCImplementation
353 : Sema::PCC_ObjCInterface);
Douglas Gregordc845342010-05-25 05:58:43 +0000354 ConsumeCodeCompletionToken();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000355 }
356
Chris Lattnere82a10f2008-10-20 05:46:22 +0000357 // If we don't have an @ directive, parse it as a function definition.
358 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000359 // The code below does not consume '}'s because it is afraid of eating the
360 // end of a namespace. Because of the way this code is structured, an
361 // erroneous r_brace would cause an infinite loop if not handled here.
362 if (Tok.is(tok::r_brace))
363 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Steve Naroff4985ace2007-08-22 18:35:33 +0000365 // FIXME: as the name implies, this rule allows function definitions.
366 // We could pass a flag or check for functions during semantic analysis.
Sean Huntbbd37c62009-11-21 08:43:09 +0000367 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000368 continue;
369 }
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Chris Lattnere82a10f2008-10-20 05:46:22 +0000371 // Otherwise, we have an @ directive, eat the @.
372 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000373 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000374 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregordc845342010-05-25 05:58:43 +0000375 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000376 break;
377 }
378
Chris Lattnera2449b22008-10-20 05:57:40 +0000379 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Chris Lattnera2449b22008-10-20 05:57:40 +0000381 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000382 AtEnd.setBegin(AtLoc);
383 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000384 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000385 } else if (DirectiveKind == tok::objc_not_keyword) {
386 Diag(Tok, diag::err_objc_unknown_at);
387 SkipUntil(tok::semi);
388 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Chris Lattnerbc662af2008-10-20 06:10:06 +0000391 // Eat the identifier.
392 ConsumeToken();
393
Chris Lattnera2449b22008-10-20 05:57:40 +0000394 switch (DirectiveKind) {
395 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000396 // FIXME: If someone forgets an @end on a protocol, this loop will
397 // continue to eat up tons of stuff and spew lots of nonsense errors. It
398 // would probably be better to bail out if we saw an @class or @interface
399 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000400 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000401 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000402 SkipUntil(tok::r_brace, tok::at);
403 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattnera2449b22008-10-20 05:57:40 +0000405 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000406 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000407 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000408 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000409 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000410 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000411 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000412 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000413 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Chris Lattnera2449b22008-10-20 05:57:40 +0000415 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000416 if (!getLang().ObjC2)
417 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
418
Chris Lattnere82a10f2008-10-20 05:46:22 +0000419 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000420 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000421 if (Tok.is(tok::l_paren))
Douglas Gregor4ad96852009-11-19 07:41:15 +0000422 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
423 allMethods.data(), allMethods.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000424
John McCalld0014542009-12-03 22:31:13 +0000425 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
426 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000427
Chris Lattnere82a10f2008-10-20 05:46:22 +0000428 // Parse all the comma separated declarators.
429 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000430 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000432 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
433 tok::at);
Chris Lattnera2449b22008-10-20 05:57:40 +0000434 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000435 }
Steve Naroff294494e2007-08-22 16:35:03 +0000436 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000437
438 // We break out of the big loop in two cases: when we see @end or when we see
439 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000440 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000441 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregordc845342010-05-25 05:58:43 +0000442 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000443 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000444 ConsumeToken(); // the "end" identifier
445 else
446 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Chris Lattnera2449b22008-10-20 05:57:40 +0000448 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000449 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000450 Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000451 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000452 allProperties.data(), allProperties.size(),
453 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000454}
455
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000456/// Parse property attribute declarations.
457///
458/// property-attr-decl: '(' property-attrlist ')'
459/// property-attrlist:
460/// property-attribute
461/// property-attrlist ',' property-attribute
462/// property-attribute:
463/// getter '=' identifier
464/// setter '=' identifier ':'
465/// readonly
466/// readwrite
467/// assign
468/// retain
469/// copy
470/// nonatomic
471///
John McCalld226f652010-08-21 09:40:31 +0000472void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, Decl *ClassDecl,
473 Decl **Methods,
Douglas Gregor4ad96852009-11-19 07:41:15 +0000474 unsigned NumMethods) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000475 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000476 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000478 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000479 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000480 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Douglas Gregordc845342010-05-25 05:58:43 +0000481 ConsumeCodeCompletionToken();
Steve Naroffece8e712009-10-08 21:55:05 +0000482 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000483 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000485 // If this is not an identifier at all, bail out early.
486 if (II == 0) {
487 MatchRHSPunctuation(tok::r_paren, LHSLoc);
488 return;
489 }
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Chris Lattner156b0612008-10-20 07:37:22 +0000491 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Chris Lattner92e62b02008-11-20 04:42:34 +0000493 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000494 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000495 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000496 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000497 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000498 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000499 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000500 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000501 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000502 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000503 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000504 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000505 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000506 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000507 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
508 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000509 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Douglas Gregor4ad96852009-11-19 07:41:15 +0000511 if (Tok.is(tok::code_completion)) {
512 if (II->getNameStart()[0] == 's')
Douglas Gregor23c94db2010-07-02 17:43:08 +0000513 Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl,
Douglas Gregor4ad96852009-11-19 07:41:15 +0000514 Methods, NumMethods);
515 else
Douglas Gregor23c94db2010-07-02 17:43:08 +0000516 Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl,
Douglas Gregor4ad96852009-11-19 07:41:15 +0000517 Methods, NumMethods);
Douglas Gregordc845342010-05-25 05:58:43 +0000518 ConsumeCodeCompletionToken();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000519 }
520
Chris Lattner8ca329c2008-10-20 07:24:39 +0000521 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000522 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000523 SkipUntil(tok::r_paren);
524 return;
525 }
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Daniel Dunbare013d682009-10-18 20:26:12 +0000527 if (II->getNameStart()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000528 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
529 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000530 ConsumeToken(); // consume method name
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000532 if (ExpectAndConsume(tok::colon,
533 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000534 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000535 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000536 } else {
537 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
538 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000539 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000540 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000541 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000542 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000543 SkipUntil(tok::r_paren);
544 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000545 }
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Chris Lattner156b0612008-10-20 07:37:22 +0000547 if (Tok.isNot(tok::comma))
548 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Chris Lattner156b0612008-10-20 07:37:22 +0000550 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000551 }
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Chris Lattner156b0612008-10-20 07:37:22 +0000553 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000554}
555
Steve Naroff3536b442007-09-06 21:24:23 +0000556/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000557/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000558/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000559///
560/// objc-instance-method: '-'
561/// objc-class-method: '+'
562///
Steve Naroff4985ace2007-08-22 18:35:33 +0000563/// objc-method-attributes: [OBJC2]
564/// __attribute__((deprecated))
565///
John McCalld226f652010-08-21 09:40:31 +0000566Decl *Parser::ParseObjCMethodPrototype(Decl *IDecl,
567 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000568 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000569
Mike Stump1eb44332009-09-09 15:08:12 +0000570 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000571 SourceLocation mLoc = ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000572 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000573 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000574 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000575 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000576}
577
578/// objc-selector:
579/// identifier
580/// one of
581/// enum struct union if else while do for switch case default
582/// break continue return goto asm sizeof typeof __alignof
583/// unsigned long const short volatile signed restrict _Complex
584/// in out inout bycopy byref oneway int char float double void _Bool
585///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000586IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000587
Chris Lattnerff384912007-10-07 02:00:24 +0000588 switch (Tok.getKind()) {
589 default:
590 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000591 case tok::ampamp:
592 case tok::ampequal:
593 case tok::amp:
594 case tok::pipe:
595 case tok::tilde:
596 case tok::exclaim:
597 case tok::exclaimequal:
598 case tok::pipepipe:
599 case tok::pipeequal:
600 case tok::caret:
601 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000602 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000603 if (isalpha(ThisTok[0])) {
604 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
605 Tok.setKind(tok::identifier);
606 SelectorLoc = ConsumeToken();
607 return II;
608 }
609 return 0;
610 }
611
Chris Lattnerff384912007-10-07 02:00:24 +0000612 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000613 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000614 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000615 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000616 case tok::kw_break:
617 case tok::kw_case:
618 case tok::kw_catch:
619 case tok::kw_char:
620 case tok::kw_class:
621 case tok::kw_const:
622 case tok::kw_const_cast:
623 case tok::kw_continue:
624 case tok::kw_default:
625 case tok::kw_delete:
626 case tok::kw_do:
627 case tok::kw_double:
628 case tok::kw_dynamic_cast:
629 case tok::kw_else:
630 case tok::kw_enum:
631 case tok::kw_explicit:
632 case tok::kw_export:
633 case tok::kw_extern:
634 case tok::kw_false:
635 case tok::kw_float:
636 case tok::kw_for:
637 case tok::kw_friend:
638 case tok::kw_goto:
639 case tok::kw_if:
640 case tok::kw_inline:
641 case tok::kw_int:
642 case tok::kw_long:
643 case tok::kw_mutable:
644 case tok::kw_namespace:
645 case tok::kw_new:
646 case tok::kw_operator:
647 case tok::kw_private:
648 case tok::kw_protected:
649 case tok::kw_public:
650 case tok::kw_register:
651 case tok::kw_reinterpret_cast:
652 case tok::kw_restrict:
653 case tok::kw_return:
654 case tok::kw_short:
655 case tok::kw_signed:
656 case tok::kw_sizeof:
657 case tok::kw_static:
658 case tok::kw_static_cast:
659 case tok::kw_struct:
660 case tok::kw_switch:
661 case tok::kw_template:
662 case tok::kw_this:
663 case tok::kw_throw:
664 case tok::kw_true:
665 case tok::kw_try:
666 case tok::kw_typedef:
667 case tok::kw_typeid:
668 case tok::kw_typename:
669 case tok::kw_typeof:
670 case tok::kw_union:
671 case tok::kw_unsigned:
672 case tok::kw_using:
673 case tok::kw_virtual:
674 case tok::kw_void:
675 case tok::kw_volatile:
676 case tok::kw_wchar_t:
677 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000678 case tok::kw__Bool:
679 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000680 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000681 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000682 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000683 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000684 }
Steve Naroff294494e2007-08-22 16:35:03 +0000685}
686
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000687/// objc-for-collection-in: 'in'
688///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000689bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000690 // FIXME: May have to do additional look-ahead to only allow for
691 // valid tokens following an 'in'; such as an identifier, unary operators,
692 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000693 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000694 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000695}
696
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000697/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000698/// qualifier list and builds their bitmask representation in the input
699/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000700///
701/// objc-type-qualifiers:
702/// objc-type-qualifier
703/// objc-type-qualifiers objc-type-qualifier
704///
Douglas Gregord32b0222010-08-24 01:06:58 +0000705void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS, bool IsParameter) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000706 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000707 if (Tok.is(tok::code_completion)) {
708 Actions.CodeCompleteObjCPassingType(getCurScope(), DS);
709 ConsumeCodeCompletionToken();
710 }
711
Chris Lattnercb53b362007-12-27 19:57:00 +0000712 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000713 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Chris Lattnere8b724d2007-12-12 06:56:32 +0000715 const IdentifierInfo *II = Tok.getIdentifierInfo();
716 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000717 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000718 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000720 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000721 switch (i) {
722 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000723 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
724 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
725 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
726 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
727 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
728 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000729 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000730 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000731 ConsumeToken();
732 II = 0;
733 break;
734 }
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Chris Lattnere8b724d2007-12-12 06:56:32 +0000736 // If this wasn't a recognized qualifier, bail out.
737 if (II) return;
738 }
739}
740
741/// objc-type-name:
742/// '(' objc-type-qualifiers[opt] type-name ')'
743/// '(' objc-type-qualifiers[opt] ')'
744///
John McCallb3d87482010-08-24 05:47:05 +0000745ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS, bool IsParameter) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000746 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Chris Lattner4a76b292008-10-22 03:52:06 +0000748 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000749 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000751 // Parse type qualifiers, in, inout, etc.
Douglas Gregord32b0222010-08-24 01:06:58 +0000752 ParseObjCTypeQualifierList(DS, IsParameter);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000753
John McCallb3d87482010-08-24 05:47:05 +0000754 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000755 if (isTypeSpecifierQualifier()) {
756 TypeResult TypeSpec = ParseTypeName();
757 if (!TypeSpec.isInvalid())
758 Ty = TypeSpec.get();
759 }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Steve Naroffd7333c22008-10-21 14:15:04 +0000761 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000762 ConsumeParen();
763 else if (Tok.getLocation() == TypeStartLoc) {
764 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000765 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000766 SkipUntil(tok::r_paren);
767 } else {
768 // Otherwise, we found *something*, but didn't get a ')' in the right
769 // place. Emit an error then return what we have as the type.
770 MatchRHSPunctuation(tok::r_paren, LParenLoc);
771 }
Steve Narofff28b2642007-09-05 23:30:30 +0000772 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000773}
774
775/// objc-method-decl:
776/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000777/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000778/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000779/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000780///
781/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000782/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000783/// objc-keyword-selector objc-keyword-decl
784///
785/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000786/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
787/// objc-selector ':' objc-keyword-attributes[opt] identifier
788/// ':' objc-type-name objc-keyword-attributes[opt] identifier
789/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000790///
Steve Naroff4985ace2007-08-22 18:35:33 +0000791/// objc-parmlist:
792/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000793///
Steve Naroff4985ace2007-08-22 18:35:33 +0000794/// objc-parms:
795/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000796///
Steve Naroff4985ace2007-08-22 18:35:33 +0000797/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000798/// , ...
799///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000800/// objc-keyword-attributes: [OBJC2]
801/// __attribute__((unused))
802///
John McCalld226f652010-08-21 09:40:31 +0000803Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000804 tok::TokenKind mType,
805 Decl *IDecl,
806 tok::ObjCKeywordKind MethodImplKind) {
John McCall54abf7d2009-11-04 02:18:39 +0000807 ParsingDeclRAIIObject PD(*this);
808
Douglas Gregore8f5a172010-04-07 00:21:17 +0000809 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000810 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
John McCallb3d87482010-08-24 05:47:05 +0000811 /*ReturnType=*/ ParsedType(), IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000812 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000813 }
814
Chris Lattnere8904e92008-08-23 01:48:03 +0000815 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000816 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000817 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000818 if (Tok.is(tok::l_paren))
Douglas Gregord32b0222010-08-24 01:06:58 +0000819 ReturnType = ParseObjCTypeName(DSRet, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Ted Kremenek9e049352010-02-18 23:05:16 +0000821 // If attributes exist before the method, parse them.
822 llvm::OwningPtr<AttributeList> MethodAttrs;
823 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
824 MethodAttrs.reset(ParseGNUAttributes());
825
Douglas Gregore8f5a172010-04-07 00:21:17 +0000826 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000827 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Douglas Gregore8f5a172010-04-07 00:21:17 +0000828 ReturnType, IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000829 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000830 }
831
Ted Kremenek9e049352010-02-18 23:05:16 +0000832 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000833 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000834 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000835
Steve Naroff84c43102009-02-11 20:43:13 +0000836 // An unnamed colon is valid.
837 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000838 Diag(Tok, diag::err_expected_selector_for_method)
839 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000840 // Skip until we get a ; or {}.
841 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000842 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000843 }
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000845 llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000846 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000847 // If attributes exist after the method, parse them.
Mike Stump1eb44332009-09-09 15:08:12 +0000848 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek9e049352010-02-18 23:05:16 +0000849 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
850 ParseGNUAttributes()));
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Chris Lattnerff384912007-10-07 02:00:24 +0000852 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000853 Decl *Result
John McCall54abf7d2009-11-04 02:18:39 +0000854 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000855 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000856 0,
857 CParamInfo.data(), CParamInfo.size(),
858 MethodAttrs.get(),
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000859 MethodImplKind);
John McCall54abf7d2009-11-04 02:18:39 +0000860 PD.complete(Result);
861 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000862 }
Steve Narofff28b2642007-09-05 23:30:30 +0000863
Steve Naroff68d331a2007-09-27 14:38:14 +0000864 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
John McCallf312b1e2010-08-26 23:41:50 +0000865 llvm::SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Chris Lattnerff384912007-10-07 02:00:24 +0000867 while (1) {
John McCallf312b1e2010-08-26 23:41:50 +0000868 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Chris Lattnerff384912007-10-07 02:00:24 +0000870 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000871 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000872 Diag(Tok, diag::err_expected_colon);
873 break;
874 }
875 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000876
John McCallb3d87482010-08-24 05:47:05 +0000877 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +0000878 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregord32b0222010-08-24 01:06:58 +0000879 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, true);
Chris Lattnere294d3f2009-04-11 18:57:04 +0000880
Chris Lattnerff384912007-10-07 02:00:24 +0000881 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000882 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000883 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000884 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000885
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000886 // Code completion for the next piece of the selector.
887 if (Tok.is(tok::code_completion)) {
888 ConsumeCodeCompletionToken();
889 KeyIdents.push_back(SelIdent);
890 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
891 mType == tok::minus,
892 /*AtParameterName=*/true,
893 ReturnType,
894 KeyIdents.data(),
895 KeyIdents.size());
896 KeyIdents.pop_back();
897 break;
898 }
899
Chris Lattnerdf195262007-10-09 17:51:17 +0000900 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000901 Diag(Tok, diag::err_expected_ident); // missing argument name.
902 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000903 }
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Chris Lattnere294d3f2009-04-11 18:57:04 +0000905 ArgInfo.Name = Tok.getIdentifierInfo();
906 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000907 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Chris Lattnere294d3f2009-04-11 18:57:04 +0000909 ArgInfos.push_back(ArgInfo);
910 KeyIdents.push_back(SelIdent);
911
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000912 // Code completion for the next piece of the selector.
913 if (Tok.is(tok::code_completion)) {
914 ConsumeCodeCompletionToken();
915 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
916 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000917 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000918 ReturnType,
919 KeyIdents.data(),
920 KeyIdents.size());
921 break;
922 }
923
Chris Lattnerff384912007-10-07 02:00:24 +0000924 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000925 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000926 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000927 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000928 break;
929 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000930 }
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Steve Naroff335eafa2007-11-15 12:35:21 +0000932 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Chris Lattnerff384912007-10-07 02:00:24 +0000934 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000935 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000936 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000937 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000938 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000939 ConsumeToken();
940 break;
941 }
Chris Lattnerff384912007-10-07 02:00:24 +0000942 DeclSpec DS;
943 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000944 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000945 Declarator ParmDecl(DS, Declarator::PrototypeContext);
946 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000947 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +0000948 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000949 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
950 ParmDecl.getIdentifierLoc(),
951 Param,
952 0));
953
Chris Lattnerff384912007-10-07 02:00:24 +0000954 }
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Chris Lattnerff384912007-10-07 02:00:24 +0000956 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000957 // If attributes exist after the method, parse them.
Mike Stump1eb44332009-09-09 15:08:12 +0000958 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek9e049352010-02-18 23:05:16 +0000959 MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
960 ParseGNUAttributes()));
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000962 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +0000963 return 0;
Chris Lattnerff384912007-10-07 02:00:24 +0000964 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
965 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +0000966 Decl *Result
John McCall54abf7d2009-11-04 02:18:39 +0000967 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000968 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000969 &ArgInfos[0],
970 CParamInfo.data(), CParamInfo.size(),
Ted Kremenek1e377652010-02-11 02:19:13 +0000971 MethodAttrs.get(),
Steve Naroff335eafa2007-11-15 12:35:21 +0000972 MethodImplKind, isVariadic);
John McCall54abf7d2009-11-04 02:18:39 +0000973 PD.complete(Result);
Ted Kremenek1e377652010-02-11 02:19:13 +0000974
975 // Delete referenced AttributeList objects.
John McCallf312b1e2010-08-26 23:41:50 +0000976 for (llvm::SmallVectorImpl<Sema::ObjCArgInfo>::iterator
Ted Kremenek1e377652010-02-11 02:19:13 +0000977 I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I)
978 delete I->ArgAttrs;
979
John McCall54abf7d2009-11-04 02:18:39 +0000980 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +0000981}
982
Steve Naroffdac269b2007-08-20 21:31:48 +0000983/// objc-protocol-refs:
984/// '<' identifier-list '>'
985///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000986bool Parser::
John McCalld226f652010-08-21 09:40:31 +0000987ParseObjCProtocolReferences(llvm::SmallVectorImpl<Decl *> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000988 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
989 bool WarnOnDeclarations,
990 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000991 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000993 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Chris Lattnere13b9592008-07-26 04:03:38 +0000995 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Chris Lattnere13b9592008-07-26 04:03:38 +0000997 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +0000998 if (Tok.is(tok::code_completion)) {
999 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1000 ProtocolIdents.size());
Douglas Gregordc845342010-05-25 05:58:43 +00001001 ConsumeCodeCompletionToken();
Douglas Gregor55385fe2009-11-18 04:19:12 +00001002 }
1003
Chris Lattnere13b9592008-07-26 04:03:38 +00001004 if (Tok.isNot(tok::identifier)) {
1005 Diag(Tok, diag::err_expected_ident);
1006 SkipUntil(tok::greater);
1007 return true;
1008 }
1009 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1010 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001011 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001012 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Chris Lattnere13b9592008-07-26 04:03:38 +00001014 if (Tok.isNot(tok::comma))
1015 break;
1016 ConsumeToken();
1017 }
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Chris Lattnere13b9592008-07-26 04:03:38 +00001019 // Consume the '>'.
1020 if (Tok.isNot(tok::greater)) {
1021 Diag(Tok, diag::err_expected_greater);
1022 return true;
1023 }
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Chris Lattnere13b9592008-07-26 04:03:38 +00001025 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Chris Lattnere13b9592008-07-26 04:03:38 +00001027 // Convert the list of protocols identifiers into a list of protocol decls.
1028 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1029 &ProtocolIdents[0], ProtocolIdents.size(),
1030 Protocols);
1031 return false;
1032}
1033
Steve Naroffdac269b2007-08-20 21:31:48 +00001034/// objc-class-instance-variables:
1035/// '{' objc-instance-variable-decl-list[opt] '}'
1036///
1037/// objc-instance-variable-decl-list:
1038/// objc-visibility-spec
1039/// objc-instance-variable-decl ';'
1040/// ';'
1041/// objc-instance-variable-decl-list objc-visibility-spec
1042/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1043/// objc-instance-variable-decl-list ';'
1044///
1045/// objc-visibility-spec:
1046/// @private
1047/// @protected
1048/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001049/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001050///
1051/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001052/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001053///
John McCalld226f652010-08-21 09:40:31 +00001054void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001055 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001056 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001057 assert(Tok.is(tok::l_brace) && "expected {");
John McCalld226f652010-08-21 09:40:31 +00001058 llvm::SmallVector<Decl *, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001059
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001060 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001061
Steve Naroffddbff782007-08-21 21:17:12 +00001062 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Steve Naroffddbff782007-08-21 21:17:12 +00001064 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001065 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001066 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Steve Naroffddbff782007-08-21 21:17:12 +00001068 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001069 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001070 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001071 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001072 ConsumeToken();
1073 continue;
1074 }
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Steve Naroffddbff782007-08-21 21:17:12 +00001076 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001077 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001078 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001079
1080 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001081 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001082 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001083 }
1084
Steve Naroff861cf3e2007-08-23 18:16:40 +00001085 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001086 case tok::objc_private:
1087 case tok::objc_public:
1088 case tok::objc_protected:
1089 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001090 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001091 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001092 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001093 default:
1094 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001095 continue;
1096 }
1097 }
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001099 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001100 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001101 Sema::PCC_ObjCInstanceVariableList);
Douglas Gregordc845342010-05-25 05:58:43 +00001102 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001103 }
1104
John McCallbdd563e2009-11-03 02:38:08 +00001105 struct ObjCIvarCallback : FieldCallback {
1106 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001107 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001108 tok::ObjCKeywordKind visibility;
John McCalld226f652010-08-21 09:40:31 +00001109 llvm::SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001110
John McCalld226f652010-08-21 09:40:31 +00001111 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
1112 llvm::SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001113 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1114 }
1115
John McCalld226f652010-08-21 09:40:31 +00001116 Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001117 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001118 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001119 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001120 FD.D.getDeclSpec().getSourceRange().getBegin(),
1121 IDecl, FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001122 if (Field)
1123 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001124 return Field;
1125 }
1126 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001127
Chris Lattnere1359422008-04-10 06:46:29 +00001128 // Parse all the comma separated declarators.
1129 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +00001130 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Chris Lattnerdf195262007-10-09 17:51:17 +00001132 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001133 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001134 } else {
1135 Diag(Tok, diag::err_expected_semi_decl_list);
1136 // Skip to end of block or statement
1137 SkipUntil(tok::r_brace, true, true);
1138 }
1139 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001140 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001141 Actions.ActOnLastBitfield(RBraceLoc, interfaceDecl, AllIvarDecls);
Steve Naroff8749be52007-10-31 22:11:35 +00001142 // Call ActOnFields() even if we don't have any decls. This is useful
1143 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001144 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001145 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001146 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001147 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001148}
Steve Naroffdac269b2007-08-20 21:31:48 +00001149
1150/// objc-protocol-declaration:
1151/// objc-protocol-definition
1152/// objc-protocol-forward-reference
1153///
1154/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001155/// @protocol identifier
1156/// objc-protocol-refs[opt]
1157/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001158/// @end
1159///
1160/// objc-protocol-forward-reference:
1161/// @protocol identifier-list ';'
1162///
1163/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001164/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001165/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCalld226f652010-08-21 09:40:31 +00001166Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001167 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001168 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001169 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1170 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Douglas Gregor083128f2009-11-18 04:49:41 +00001172 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001173 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001174 ConsumeCodeCompletionToken();
Douglas Gregor083128f2009-11-18 04:49:41 +00001175 }
1176
Chris Lattnerdf195262007-10-09 17:51:17 +00001177 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001178 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCalld226f652010-08-21 09:40:31 +00001179 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001180 }
1181 // Save the protocol name, then consume it.
1182 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1183 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Chris Lattnerdf195262007-10-09 17:51:17 +00001185 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001186 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001187 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001188 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001189 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001190 }
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Chris Lattnerdf195262007-10-09 17:51:17 +00001192 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001193 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1194 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1195
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001196 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001197 while (1) {
1198 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001199 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001200 Diag(Tok, diag::err_expected_ident);
1201 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001202 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001203 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001204 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1205 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001206 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Chris Lattnerdf195262007-10-09 17:51:17 +00001208 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001209 break;
1210 }
1211 // Consume the ';'.
1212 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCalld226f652010-08-21 09:40:31 +00001213 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Steve Naroffe440eb82007-10-10 17:32:04 +00001215 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001216 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001217 ProtocolRefs.size(),
1218 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001219 }
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001221 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001222 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001223
John McCalld226f652010-08-21 09:40:31 +00001224 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001225 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001226 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001227 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1228 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +00001229 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001230
John McCalld226f652010-08-21 09:40:31 +00001231 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001232 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001233 ProtocolRefs.data(),
1234 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001235 ProtocolLocs.data(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001236 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001237 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001238 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001239}
Steve Naroffdac269b2007-08-20 21:31:48 +00001240
1241/// objc-implementation:
1242/// objc-class-implementation-prologue
1243/// objc-category-implementation-prologue
1244///
1245/// objc-class-implementation-prologue:
1246/// @implementation identifier objc-superclass[opt]
1247/// objc-class-instance-variables[opt]
1248///
1249/// objc-category-implementation-prologue:
1250/// @implementation identifier ( identifier )
John McCalld226f652010-08-21 09:40:31 +00001251Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001252 SourceLocation atLoc) {
1253 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1254 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1255 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001257 // Code completion after '@implementation'.
1258 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001259 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001260 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001261 }
1262
Chris Lattnerdf195262007-10-09 17:51:17 +00001263 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001264 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001265 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001266 }
1267 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001268 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001269 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001270
1271 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001272 // we have a category implementation.
1273 SourceLocation lparenLoc = ConsumeParen();
1274 SourceLocation categoryLoc, rparenLoc;
1275 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001277 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001278 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +00001279 ConsumeCodeCompletionToken();
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001280 }
1281
Chris Lattnerdf195262007-10-09 17:51:17 +00001282 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001283 categoryId = Tok.getIdentifierInfo();
1284 categoryLoc = ConsumeToken();
1285 } else {
1286 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001287 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001288 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001289 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001290 Diag(Tok, diag::err_expected_rparen);
1291 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001292 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001293 }
1294 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001295 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001296 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001297 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001298 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001299 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001300 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001301 }
1302 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001303 SourceLocation superClassLoc;
1304 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001305 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001306 // We have a super class
1307 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001308 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001309 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001310 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001311 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001312 superClassId = Tok.getIdentifierInfo();
1313 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001314 }
John McCalld226f652010-08-21 09:40:31 +00001315 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001316 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001317 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Steve Naroff60fccee2007-10-29 21:38:07 +00001319 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001320 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahanian01f1bfc2010-03-22 19:04:14 +00001321 tok::objc_private, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001322 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001323 PendingObjCImpDecl.push_back(ObjCImpDecl);
1324
John McCalld226f652010-08-21 09:40:31 +00001325 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001326}
Steve Naroff60fccee2007-10-29 21:38:07 +00001327
John McCalld226f652010-08-21 09:40:31 +00001328Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001329 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1330 "ParseObjCAtEndDeclaration(): Expected @end");
John McCalld226f652010-08-21 09:40:31 +00001331 Decl *Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001332 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001333 if (ObjCImpDecl) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001334 Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001335 ObjCImpDecl = 0;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001336 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001337 }
Ted Kremenek782f2f52010-01-07 01:20:12 +00001338 else {
1339 // missing @implementation
1340 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1341 }
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001342 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001343}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001344
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001345Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1346 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001347 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001348 return Actions.ConvertDeclToDeclGroup(0);
1349 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001350 Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001351 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1352}
1353
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001354/// compatibility-alias-decl:
1355/// @compatibility_alias alias-name class-name ';'
1356///
John McCalld226f652010-08-21 09:40:31 +00001357Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001358 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1359 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1360 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001361 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001362 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001363 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001364 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001365 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1366 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001367 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001368 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001369 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001370 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001371 IdentifierInfo *classId = Tok.getIdentifierInfo();
1372 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1373 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001374 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
John McCalld226f652010-08-21 09:40:31 +00001375 return 0;
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001376 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001377 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1378 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001379}
1380
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001381/// property-synthesis:
1382/// @synthesize property-ivar-list ';'
1383///
1384/// property-ivar-list:
1385/// property-ivar
1386/// property-ivar-list ',' property-ivar
1387///
1388/// property-ivar:
1389/// identifier
1390/// identifier '=' identifier
1391///
John McCalld226f652010-08-21 09:40:31 +00001392Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001393 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1394 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001395 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Douglas Gregorb328c422009-11-18 19:45:45 +00001397 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001398 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001399 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001400 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001401 }
1402
Douglas Gregorb328c422009-11-18 19:45:45 +00001403 if (Tok.isNot(tok::identifier)) {
1404 Diag(Tok, diag::err_synthesized_property_name);
1405 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001406 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001407 }
1408
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001409 IdentifierInfo *propertyIvar = 0;
1410 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1411 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001412 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001413 // property '=' ivar-name
1414 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001415
1416 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001417 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId,
Douglas Gregor322328b2009-11-18 22:32:06 +00001418 ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001419 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001420 }
1421
Chris Lattnerdf195262007-10-09 17:51:17 +00001422 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001423 Diag(Tok, diag::err_expected_ident);
1424 break;
1425 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001426 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001427 ConsumeToken(); // consume ivar-name
1428 }
Douglas Gregor23c94db2010-07-02 17:43:08 +00001429 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl,
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001430 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001431 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001432 break;
1433 ConsumeToken(); // consume ','
1434 }
Douglas Gregorb328c422009-11-18 19:45:45 +00001435 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001436 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregorb328c422009-11-18 19:45:45 +00001437 SkipUntil(tok::semi);
1438 }
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001439 else
1440 ConsumeToken(); // consume ';'
John McCalld226f652010-08-21 09:40:31 +00001441 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001442}
1443
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001444/// property-dynamic:
1445/// @dynamic property-list
1446///
1447/// property-list:
1448/// identifier
1449/// property-list ',' identifier
1450///
John McCalld226f652010-08-21 09:40:31 +00001451Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001452 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1453 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1454 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001455 while (true) {
1456 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001457 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001458 ConsumeCodeCompletionToken();
Douglas Gregor424b2a52009-11-18 22:56:13 +00001459 }
1460
1461 if (Tok.isNot(tok::identifier)) {
1462 Diag(Tok, diag::err_expected_ident);
1463 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001464 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001465 }
1466
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001467 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1468 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregor23c94db2010-07-02 17:43:08 +00001469 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl,
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001470 propertyId, 0);
1471
Chris Lattnerdf195262007-10-09 17:51:17 +00001472 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001473 break;
1474 ConsumeToken(); // consume ','
1475 }
Fariborz Jahanian94b24db2010-04-14 20:52:42 +00001476 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001477 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanian94b24db2010-04-14 20:52:42 +00001478 SkipUntil(tok::semi);
1479 }
1480 else
1481 ConsumeToken(); // consume ';'
John McCalld226f652010-08-21 09:40:31 +00001482 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001483}
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001485/// objc-throw-statement:
1486/// throw expression[opt];
1487///
John McCall60d7b3a2010-08-24 06:29:42 +00001488StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1489 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001490 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001491 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001492 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001493 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001494 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001495 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001496 }
1497 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001498 // consume ';'
1499 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001500 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001501}
1502
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001503/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001504/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001505///
John McCall60d7b3a2010-08-24 06:29:42 +00001506StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001507Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001508 ConsumeToken(); // consume synchronized
1509 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001510 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001511 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001512 }
1513 ConsumeParen(); // '('
John McCall60d7b3a2010-08-24 06:29:42 +00001514 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001515 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001516 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001517 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001518 }
1519 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001520 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001521 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001522 }
1523 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001524 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001525 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001526 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001527 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001528 // Enter a scope to hold everything within the compound stmt. Compound
1529 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001530 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001531
John McCall60d7b3a2010-08-24 06:29:42 +00001532 StmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001533
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001534 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001535 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001536 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
John McCall9ae2f072010-08-23 23:25:46 +00001537 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.take(), SynchBody.take());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001538}
1539
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001540/// objc-try-catch-statement:
1541/// @try compound-statement objc-catch-list[opt]
1542/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1543///
1544/// objc-catch-list:
1545/// @catch ( parameter-declaration ) compound-statement
1546/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1547/// catch-parameter-declaration:
1548/// parameter-declaration
1549/// '...' [OBJC2]
1550///
John McCall60d7b3a2010-08-24 06:29:42 +00001551StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001552 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001553
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001554 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001555 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001556 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001557 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001558 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001559 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001560 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001561 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001562 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001563 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001564 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001565 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001566
Chris Lattnerdf195262007-10-09 17:51:17 +00001567 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001568 // At this point, we need to lookahead to determine if this @ is the start
1569 // of an @catch or @finally. We don't want to consume the @ token if this
1570 // is an @try or @encode or something else.
1571 Token AfterAt = GetLookAheadToken(1);
1572 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1573 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1574 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001575
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001576 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001577 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001578 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001579 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001580 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001581 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001582 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001583 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001584 DeclSpec DS;
1585 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001586 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001587 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001588 // we must accept either 'declarator' or 'abstract-declarator' here.
1589 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1590 ParseDeclarator(ParmDecl);
1591
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001592 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001593 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001594 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001595 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001596 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Steve Naroff93a25952009-04-07 22:56:58 +00001598 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Steve Naroff93a25952009-04-07 22:56:58 +00001600 if (Tok.is(tok::r_paren))
1601 RParenLoc = ConsumeParen();
1602 else // Skip over garbage, until we get to ')'. Eat the ')'.
1603 SkipUntil(tok::r_paren, true, false);
1604
John McCall60d7b3a2010-08-24 06:29:42 +00001605 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001606 if (Tok.is(tok::l_brace))
1607 CatchBody = ParseCompoundStatementBody();
1608 else
1609 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001610 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001611 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001612
John McCall60d7b3a2010-08-24 06:29:42 +00001613 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001614 RParenLoc,
1615 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001616 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001617 if (!Catch.isInvalid())
1618 CatchStmts.push_back(Catch.release());
1619
Steve Naroff64515f32008-02-05 21:27:35 +00001620 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001621 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1622 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001623 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001624 }
1625 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001626 } else {
1627 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001628 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001629 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001630
John McCall60d7b3a2010-08-24 06:29:42 +00001631 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001632 if (Tok.is(tok::l_brace))
1633 FinallyBody = ParseCompoundStatementBody();
1634 else
1635 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001636 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001637 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001638 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001639 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001640 catch_or_finally_seen = true;
1641 break;
1642 }
1643 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001644 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001645 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001646 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001647 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001648
John McCall9ae2f072010-08-23 23:25:46 +00001649 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001650 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001651 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001652}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001653
Steve Naroff3536b442007-09-06 21:24:23 +00001654/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001655///
John McCalld226f652010-08-21 09:40:31 +00001656Decl *Parser::ParseObjCMethodDefinition() {
1657 Decl *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001658
John McCallf312b1e2010-08-26 23:41:50 +00001659 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1660 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001662 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001663 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001664 if (ObjCImpDecl) {
1665 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001666 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001667 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001668 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001669 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001670
Steve Naroff409be832007-11-11 19:54:21 +00001671 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001672 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001673 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Steve Naroff409be832007-11-11 19:54:21 +00001675 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1676 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001677
Steve Naroff409be832007-11-11 19:54:21 +00001678 // If we didn't find the '{', bail out.
1679 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001680 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001681 }
Steve Naroff409be832007-11-11 19:54:21 +00001682 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Steve Naroff409be832007-11-11 19:54:21 +00001684 // Enter a scope for the method body.
Chris Lattner15faee12010-04-12 05:38:43 +00001685 ParseScope BodyScope(this,
1686 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Steve Naroff409be832007-11-11 19:54:21 +00001688 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001689 // specified Declarator for the method.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001690 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001691
John McCall60d7b3a2010-08-24 06:29:42 +00001692 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001693
Steve Naroff409be832007-11-11 19:54:21 +00001694 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001695 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001696 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1697 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001698
Steve Naroff32ce8372009-03-02 22:00:56 +00001699 // TODO: Pass argument information.
John McCall9ae2f072010-08-23 23:25:46 +00001700 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Steve Naroff409be832007-11-11 19:54:21 +00001702 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001703 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001704
Steve Naroff71c0a952007-11-13 23:01:27 +00001705 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001706}
Anders Carlsson55085182007-08-21 17:43:55 +00001707
John McCall60d7b3a2010-08-24 06:29:42 +00001708StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001709 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001710 Actions.CodeCompleteObjCAtStatement(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001711 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001712 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001713 }
1714
1715 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001716 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001717
1718 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001719 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001720
1721 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001722 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001723
John McCall60d7b3a2010-08-24 06:29:42 +00001724 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001725 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001726 // If the expression is invalid, skip ahead to the next semicolon. Not
1727 // doing this opens us up to the possibility of infinite loops if
1728 // ParseExpression does not consume any tokens.
1729 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001730 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001731 }
Chris Lattner5d803162009-12-07 16:33:19 +00001732
Steve Naroff64515f32008-02-05 21:27:35 +00001733 // Otherwise, eat the semicolon.
1734 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001735 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001736}
1737
John McCall60d7b3a2010-08-24 06:29:42 +00001738ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001739 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001740 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001741 Actions.CodeCompleteObjCAtExpression(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001742 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001743 return ExprError();
1744
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001745 case tok::string_literal: // primary-expression: string-literal
1746 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001747 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001748 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001749 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001750 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001751
Chris Lattner4fef81d2008-08-05 06:19:09 +00001752 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1753 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001754 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001755 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001756 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001757 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001758 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001759 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001760 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001761 }
Anders Carlsson55085182007-08-21 17:43:55 +00001762 }
Anders Carlsson55085182007-08-21 17:43:55 +00001763}
1764
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001765/// \brirg Parse the receiver of an Objective-C++ message send.
1766///
1767/// This routine parses the receiver of a message send in
1768/// Objective-C++ either as a type or as an expression. Note that this
1769/// routine must not be called to parse a send to 'super', since it
1770/// has no way to return such a result.
1771///
1772/// \param IsExpr Whether the receiver was parsed as an expression.
1773///
1774/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1775/// IsExpr is true), the parsed expression. If the receiver was parsed
1776/// as a type (\c IsExpr is false), the parsed type.
1777///
1778/// \returns True if an error occurred during parsing or semantic
1779/// analysis, in which case the arguments do not have valid
1780/// values. Otherwise, returns false for a successful parse.
1781///
1782/// objc-receiver: [C++]
1783/// 'super' [not parsed here]
1784/// expression
1785/// simple-type-specifier
1786/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001787bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
1788 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1789 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1790 TryAnnotateTypeOrScopeToken();
1791
1792 if (!isCXXSimpleTypeSpecifier()) {
1793 // objc-receiver:
1794 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00001795 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001796 if (Receiver.isInvalid())
1797 return true;
1798
1799 IsExpr = true;
1800 TypeOrExpr = Receiver.take();
1801 return false;
1802 }
1803
1804 // objc-receiver:
1805 // typename-specifier
1806 // simple-type-specifier
1807 // expression (that starts with one of the above)
1808 DeclSpec DS;
1809 ParseCXXSimpleTypeSpecifier(DS);
1810
1811 if (Tok.is(tok::l_paren)) {
1812 // If we see an opening parentheses at this point, we are
1813 // actually parsing an expression that starts with a
1814 // function-style cast, e.g.,
1815 //
1816 // postfix-expression:
1817 // simple-type-specifier ( expression-list [opt] )
1818 // typename-specifier ( expression-list [opt] )
1819 //
1820 // Parse the remainder of this case, then the (optional)
1821 // postfix-expression suffix, followed by the (optional)
1822 // right-hand side of the binary expression. We have an
1823 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00001824 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001825 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001826 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001827 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001828 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001829 if (Receiver.isInvalid())
1830 return true;
1831
1832 IsExpr = true;
1833 TypeOrExpr = Receiver.take();
1834 return false;
1835 }
1836
1837 // We have a class message. Turn the simple-type-specifier or
1838 // typename-specifier we parsed into a type and parse the
1839 // remainder of the class message.
1840 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001841 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001842 if (Type.isInvalid())
1843 return true;
1844
1845 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00001846 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001847 return false;
1848}
1849
Douglas Gregor1b730e82010-05-31 14:40:22 +00001850/// \brief Determine whether the parser is currently referring to a an
1851/// Objective-C message send, using a simplified heuristic to avoid overhead.
1852///
1853/// This routine will only return true for a subset of valid message-send
1854/// expressions.
1855bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00001856 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00001857 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00001858 return GetLookAheadToken(1).is(tok::identifier) &&
1859 GetLookAheadToken(2).is(tok::identifier);
1860}
1861
Mike Stump1eb44332009-09-09 15:08:12 +00001862/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001863/// '[' objc-receiver objc-message-args ']'
1864///
Douglas Gregor2725ca82010-04-21 19:57:20 +00001865/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00001866/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001867/// expression
1868/// class-name
1869/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001870///
John McCall60d7b3a2010-08-24 06:29:42 +00001871ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001872 assert(Tok.is(tok::l_square) && "'[' expected");
1873 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1874
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001875 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001876 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001877 ConsumeCodeCompletionToken();
1878 SkipUntil(tok::r_square);
1879 return ExprError();
1880 }
1881
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001882 if (getLang().CPlusPlus) {
1883 // We completely separate the C and C++ cases because C++ requires
1884 // more complicated (read: slower) parsing.
1885
1886 // Handle send to super.
1887 // FIXME: This doesn't benefit from the same typo-correction we
1888 // get in Objective-C.
1889 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001890 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00001891 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1892 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001893
1894 // Parse the receiver, which is either a type or an expression.
1895 bool IsExpr;
1896 void *TypeOrExpr;
1897 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
1898 SkipUntil(tok::r_square);
1899 return ExprError();
1900 }
1901
1902 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00001903 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1904 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00001905 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001906
1907 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00001908 ParsedType::getFromOpaquePtr(TypeOrExpr),
1909 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00001910 }
1911
1912 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001913 IdentifierInfo *Name = Tok.getIdentifierInfo();
1914 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00001915 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001916 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001917 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00001918 NextToken().is(tok::period),
1919 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00001920 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00001921 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1922 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00001923
John McCallf312b1e2010-08-26 23:41:50 +00001924 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00001925 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001926 SkipUntil(tok::r_square);
1927 return ExprError();
1928 }
1929
Douglas Gregor1569f952010-04-21 20:38:13 +00001930 ConsumeToken(); // the type name
1931
1932 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00001933 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001934
John McCallf312b1e2010-08-26 23:41:50 +00001935 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00001936 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001937 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001938 }
Chris Lattner699b6612008-01-25 18:59:06 +00001939 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00001940
1941 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00001942 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001943 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001944 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001945 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001946 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001947
John McCallb3d87482010-08-24 05:47:05 +00001948 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1949 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00001950}
Sebastian Redl1d922962008-12-13 15:32:12 +00001951
Douglas Gregor2725ca82010-04-21 19:57:20 +00001952/// \brief Parse the remainder of an Objective-C message following the
1953/// '[' objc-receiver.
1954///
1955/// This routine handles sends to super, class messages (sent to a
1956/// class name), and instance messages (sent to an object), and the
1957/// target is represented by \p SuperLoc, \p ReceiverType, or \p
1958/// ReceiverExpr, respectively. Only one of these parameters may have
1959/// a valid value.
1960///
1961/// \param LBracLoc The location of the opening '['.
1962///
1963/// \param SuperLoc If this is a send to 'super', the location of the
1964/// 'super' keyword that indicates a send to the superclass.
1965///
1966/// \param ReceiverType If this is a class message, the type of the
1967/// class we are sending a message to.
1968///
1969/// \param ReceiverExpr If this is an instance message, the expression
1970/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00001971///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001972/// objc-message-args:
1973/// objc-selector
1974/// objc-keywordarg-list
1975///
1976/// objc-keywordarg-list:
1977/// objc-keywordarg
1978/// objc-keywordarg-list objc-keywordarg
1979///
Mike Stump1eb44332009-09-09 15:08:12 +00001980/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001981/// selector-name[opt] ':' objc-keywordexpr
1982///
1983/// objc-keywordexpr:
1984/// nonempty-expr-list
1985///
1986/// nonempty-expr-list:
1987/// assignment-expression
1988/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001989///
John McCall60d7b3a2010-08-24 06:29:42 +00001990ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001991Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00001992 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00001993 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00001994 ExprArg ReceiverExpr) {
Steve Naroffc4df6d22009-11-07 02:08:14 +00001995 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001996 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00001997 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00001998 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001999 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002000 else
John McCall9ae2f072010-08-23 23:25:46 +00002001 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002002 0, 0);
Douglas Gregordc845342010-05-25 05:58:43 +00002003 ConsumeCodeCompletionToken();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002004 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002005
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002006 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002007 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002008 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002009
Anders Carlssonff975cf2009-02-14 18:21:46 +00002010 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Steve Naroff68d331a2007-09-27 14:38:14 +00002012 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002013 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002014
Chris Lattnerdf195262007-10-09 17:51:17 +00002015 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002016 while (1) {
2017 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002018 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00002019
Chris Lattnerdf195262007-10-09 17:51:17 +00002020 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002021 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002022 // We must manually skip to a ']', otherwise the expression skipper will
2023 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2024 // the enclosing expression.
2025 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002026 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002027 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002028
Steve Naroff68d331a2007-09-27 14:38:14 +00002029 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002030 /// Parse the expression after ':'
John McCall60d7b3a2010-08-24 06:29:42 +00002031 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002032 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002033 // We must manually skip to a ']', otherwise the expression skipper will
2034 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2035 // the enclosing expression.
2036 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002037 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002038 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002039
Steve Naroff37387c92007-09-17 20:25:27 +00002040 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002041 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002042
Douglas Gregord3c68542009-11-19 01:08:35 +00002043 // Code completion after each argument.
2044 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002045 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002046 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002047 KeyIdents.data(),
2048 KeyIdents.size());
2049 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002050 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002051 KeyIdents.data(),
2052 KeyIdents.size());
2053 else
John McCall9ae2f072010-08-23 23:25:46 +00002054 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002055 KeyIdents.data(),
2056 KeyIdents.size());
Douglas Gregordc845342010-05-25 05:58:43 +00002057 ConsumeCodeCompletionToken();
Douglas Gregord3c68542009-11-19 01:08:35 +00002058 }
2059
Steve Naroff37387c92007-09-17 20:25:27 +00002060 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002061 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002062 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002063 break;
2064 // We have a selector or a colon, continue parsing.
2065 }
2066 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002067 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002068 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002069 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002070 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002071 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002072 // We must manually skip to a ']', otherwise the expression skipper will
2073 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2074 // the enclosing expression.
2075 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002076 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002077 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002078
Steve Naroff49f109c2007-11-15 13:05:42 +00002079 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002080 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002081 }
2082 } else if (!selIdent) {
2083 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002084
Chris Lattner4fef81d2008-08-05 06:19:09 +00002085 // We must manually skip to a ']', otherwise the expression skipper will
2086 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2087 // the enclosing expression.
2088 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002089 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002090 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002091
Chris Lattnerdf195262007-10-09 17:51:17 +00002092 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002093 if (Tok.is(tok::identifier))
2094 Diag(Tok, diag::err_expected_colon);
2095 else
2096 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002097 // We must manually skip to a ']', otherwise the expression skipper will
2098 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2099 // the enclosing expression.
2100 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002101 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002102 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002103
Chris Lattner699b6612008-01-25 18:59:06 +00002104 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002105
Steve Naroff29238a02007-10-05 18:42:47 +00002106 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00002107 if (nKeys == 0)
2108 KeyIdents.push_back(selIdent);
2109 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002110
Douglas Gregor2725ca82010-04-21 19:57:20 +00002111 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002112 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002113 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002114 MultiExprArg(Actions,
2115 KeyExprs.take(),
2116 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002117 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002118 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002119 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002120 MultiExprArg(Actions,
2121 KeyExprs.take(),
2122 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002123 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002124 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002125 MultiExprArg(Actions,
2126 KeyExprs.take(),
2127 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002128}
2129
John McCall60d7b3a2010-08-24 06:29:42 +00002130ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2131 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002132 if (Res.isInvalid()) return move(Res);
2133
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002134 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2135 // expressions. At this point, we know that the only valid thing that starts
2136 // with '@' is an @"".
2137 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002138 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002139 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002140 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002141
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002142 while (Tok.is(tok::at)) {
2143 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002144
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002145 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002146 if (!isTokenStringLiteral())
2147 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002148
John McCall60d7b3a2010-08-24 06:29:42 +00002149 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002150 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002151 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002152
Sebastian Redleffa8d12008-12-10 00:02:53 +00002153 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002154 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002155
2156 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2157 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002158}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002159
2160/// objc-encode-expression:
2161/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002162ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002163Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002164 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002165
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002166 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002167
Chris Lattner4fef81d2008-08-05 06:19:09 +00002168 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002169 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2170
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002171 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002172
Douglas Gregor809070a2009-02-18 17:45:20 +00002173 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002174
Anders Carlsson4988ae32007-08-23 15:31:37 +00002175 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00002176
Douglas Gregor809070a2009-02-18 17:45:20 +00002177 if (Ty.isInvalid())
2178 return ExprError();
2179
Mike Stump1eb44332009-09-09 15:08:12 +00002180 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00002181 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002182}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002183
2184/// objc-protocol-expression
2185/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002186ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002187Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002188 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002189
Chris Lattner4fef81d2008-08-05 06:19:09 +00002190 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002191 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2192
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002193 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002194
Chris Lattner4fef81d2008-08-05 06:19:09 +00002195 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002196 return ExprError(Diag(Tok, diag::err_expected_ident));
2197
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002198 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002199 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002200
Anders Carlsson4988ae32007-08-23 15:31:37 +00002201 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002202
Sebastian Redl1d922962008-12-13 15:32:12 +00002203 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2204 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002205}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002206
2207/// objc-selector-expression
2208/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002209ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002210 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002211
Chris Lattner4fef81d2008-08-05 06:19:09 +00002212 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002213 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2214
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002215 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002216 SourceLocation LParenLoc = ConsumeParen();
2217 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002218
2219 if (Tok.is(tok::code_completion)) {
2220 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2221 KeyIdents.size());
2222 ConsumeCodeCompletionToken();
2223 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2224 return ExprError();
2225 }
2226
Chris Lattner2fc5c242009-04-11 18:13:45 +00002227 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002228 if (!SelIdent && // missing selector name.
2229 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002230 return ExprError(Diag(Tok, diag::err_expected_ident));
2231
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002232 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002233 unsigned nColons = 0;
2234 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002235 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002236 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2237 ++nColons;
2238 KeyIdents.push_back(0);
2239 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002240 return ExprError(Diag(Tok, diag::err_expected_colon));
2241
Chris Lattner5add7542010-08-27 22:32:41 +00002242 ++nColons;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002243 ConsumeToken(); // Eat the ':'.
2244 if (Tok.is(tok::r_paren))
2245 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002246
2247 if (Tok.is(tok::code_completion)) {
2248 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2249 KeyIdents.size());
2250 ConsumeCodeCompletionToken();
2251 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2252 return ExprError();
2253 }
2254
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002255 // Check for another keyword selector.
2256 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002257 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002258 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002259 if (!SelIdent && Tok.isNot(tok::colon))
2260 break;
2261 }
Steve Naroff887407e2007-12-05 22:21:29 +00002262 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002263 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00002264 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002265 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2266 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00002267 }