blob: 4324e0824d9b600b5555f491a14f7ab0fed15db0 [file] [log] [blame]
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000015#include "clang/Parse/Parser.h"
Douglas Gregor0fbda682010-09-15 14:51:05 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/SmallVector.h"
21using namespace clang;
22
23
Chris Lattner891dca62008-12-08 21:53:24 +000024/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000025/// external-declaration: [C99 6.9]
26/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000027/// [OBJC] objc-class-declaration
28/// [OBJC] objc-alias-declaration
29/// [OBJC] objc-protocol-definition
30/// [OBJC] objc-method-definition
31/// [OBJC] '@' 'end'
John McCalld226f652010-08-21 09:40:31 +000032Decl *Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +000034
Douglas Gregorc464ae82009-12-07 09:27:33 +000035 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000036 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, false);
Douglas Gregordc845342010-05-25 05:58:43 +000037 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +000038 }
39
Steve Naroff861cf3e2007-08-23 18:16:40 +000040 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000041 case tok::objc_class:
42 return ParseObjCAtClassDeclaration(AtLoc);
43 case tok::objc_interface:
44 return ParseObjCAtInterfaceDeclaration(AtLoc);
45 case tok::objc_protocol:
46 return ParseObjCAtProtocolDeclaration(AtLoc);
47 case tok::objc_implementation:
48 return ParseObjCAtImplementationDeclaration(AtLoc);
49 case tok::objc_end:
50 return ParseObjCAtEndDeclaration(AtLoc);
51 case tok::objc_compatibility_alias:
52 return ParseObjCAtAliasDeclaration(AtLoc);
53 case tok::objc_synthesize:
54 return ParseObjCPropertySynthesize(AtLoc);
55 case tok::objc_dynamic:
56 return ParseObjCPropertyDynamic(AtLoc);
57 default:
58 Diag(AtLoc, diag::err_unexpected_at);
59 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +000060 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000061 }
62}
63
64///
Mike Stump1eb44332009-09-09 15:08:12 +000065/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000066/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000067///
John McCalld226f652010-08-21 09:40:31 +000068Decl *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000069 ConsumeToken(); // the identifier "class"
70 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremenekc09cba62009-11-17 23:12:20 +000071 llvm::SmallVector<SourceLocation, 8> ClassLocs;
72
Mike Stump1eb44332009-09-09 15:08:12 +000073
Reid Spencer5f016e22007-07-11 17:01:13 +000074 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000075 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000076 Diag(Tok, diag::err_expected_ident);
77 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +000078 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000079 }
Reid Spencer5f016e22007-07-11 17:01:13 +000080 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +000081 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000082 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +000083
Chris Lattnerdf195262007-10-09 17:51:17 +000084 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000085 break;
Mike Stump1eb44332009-09-09 15:08:12 +000086
Reid Spencer5f016e22007-07-11 17:01:13 +000087 ConsumeToken();
88 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Reid Spencer5f016e22007-07-11 17:01:13 +000090 // Consume the ';'.
91 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
John McCalld226f652010-08-21 09:40:31 +000092 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000093
Ted Kremenekc09cba62009-11-17 23:12:20 +000094 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
95 ClassLocs.data(),
96 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000097}
98
Steve Naroffdac269b2007-08-20 21:31:48 +000099///
100/// objc-interface:
101/// objc-class-interface-attributes[opt] objc-class-interface
102/// objc-category-interface
103///
104/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000105/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000106/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000107/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000108/// objc-interface-decl-list
109/// @end
110///
111/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000112/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000113/// objc-protocol-refs[opt]
114/// objc-interface-decl-list
115/// @end
116///
117/// objc-superclass:
118/// ':' identifier
119///
120/// objc-class-interface-attributes:
121/// __attribute__((visibility("default")))
122/// __attribute__((visibility("hidden")))
123/// __attribute__((deprecated))
124/// __attribute__((unavailable))
125/// __attribute__((objc_exception)) - used by NSException on 64-bit
126///
John McCalld226f652010-08-21 09:40:31 +0000127Decl *Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroffdac269b2007-08-20 21:31:48 +0000128 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000129 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000130 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
131 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000133 // Code completion after '@interface'.
134 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000135 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000136 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000137 }
138
Chris Lattnerdf195262007-10-09 17:51:17 +0000139 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000140 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000141 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000142 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000143
Steve Naroffdac269b2007-08-20 21:31:48 +0000144 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000145 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000146 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000147 if (Tok.is(tok::l_paren) &&
148 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000149 SourceLocation lparenLoc = ConsumeParen();
150 SourceLocation categoryLoc, rparenLoc;
151 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000152 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000153 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +0000154 ConsumeCodeCompletionToken();
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000155 }
156
Steve Naroff527fe232007-08-23 19:56:30 +0000157 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000158 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000159 categoryId = Tok.getIdentifierInfo();
160 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000161 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000162 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000163 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000164 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000165 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000166 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000167 Diag(Tok, diag::err_expected_rparen);
168 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +0000169 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000170 }
171 rparenLoc = ConsumeParen();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000172 // Next, we need to check for any protocol references.
173 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +0000174 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000175 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
176 if (Tok.is(tok::less) &&
177 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000178 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000179 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000181 if (attrList) // categories don't support attributes.
182 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000183
John McCalld226f652010-08-21 09:40:31 +0000184 Decl *CategoryType =
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000185 Actions.ActOnStartCategoryInterface(atLoc,
186 nameId, nameLoc,
187 categoryId, categoryLoc,
188 ProtocolRefs.data(),
189 ProtocolRefs.size(),
190 ProtocolLocs.data(),
191 EndProtoLoc);
192 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000193 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
194 atLoc);
195
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000196 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
197 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000198 }
199 // Parse a class interface.
200 IdentifierInfo *superClassId = 0;
201 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000202
Chris Lattnerdf195262007-10-09 17:51:17 +0000203 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000204 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000205
206 // Code completion of superclass names.
207 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000208 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +0000209 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000210 }
211
Chris Lattnerdf195262007-10-09 17:51:17 +0000212 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000213 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000214 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000215 }
216 superClassId = Tok.getIdentifierInfo();
217 superClassLoc = ConsumeToken();
218 }
219 // Next, we need to check for any protocol references.
John McCalld226f652010-08-21 09:40:31 +0000220 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000221 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
222 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000223 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000224 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
225 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000226 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000227
John McCalld226f652010-08-21 09:40:31 +0000228 Decl *ClsType =
Mike Stump1eb44332009-09-09 15:08:12 +0000229 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000230 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000231 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000232 ProtocolLocs.data(),
Chris Lattner06036d32008-07-26 04:13:19 +0000233 EndProtoLoc, attrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Chris Lattnerdf195262007-10-09 17:51:17 +0000235 if (Tok.is(tok::l_brace))
Fariborz Jahanian83c481a2010-02-22 23:04:20 +0000236 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000237
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000238 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000239 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000240}
241
John McCalld0014542009-12-03 22:31:13 +0000242/// The Objective-C property callback. This should be defined where
243/// it's used, but instead it's been lifted to here to support VS2005.
244struct Parser::ObjCPropertyCallback : FieldCallback {
245 Parser &P;
John McCalld226f652010-08-21 09:40:31 +0000246 Decl *IDecl;
247 llvm::SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000248 ObjCDeclSpec &OCDS;
249 SourceLocation AtLoc;
250 tok::ObjCKeywordKind MethodImplKind;
251
John McCalld226f652010-08-21 09:40:31 +0000252 ObjCPropertyCallback(Parser &P, Decl *IDecl,
253 llvm::SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000254 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
255 tok::ObjCKeywordKind MethodImplKind) :
256 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
257 MethodImplKind(MethodImplKind) {
258 }
259
John McCalld226f652010-08-21 09:40:31 +0000260 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000261 if (FD.D.getIdentifier() == 0) {
262 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
263 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000264 return 0;
John McCalld0014542009-12-03 22:31:13 +0000265 }
266 if (FD.BitfieldSize) {
267 P.Diag(AtLoc, diag::err_objc_property_bitfield)
268 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000269 return 0;
John McCalld0014542009-12-03 22:31:13 +0000270 }
271
272 // Install the property declarator into interfaceDecl.
273 IdentifierInfo *SelName =
274 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
275
276 Selector GetterSel =
277 P.PP.getSelectorTable().getNullarySelector(SelName);
278 IdentifierInfo *SetterName = OCDS.getSetterName();
279 Selector SetterSel;
280 if (SetterName)
281 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
282 else
283 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
284 P.PP.getSelectorTable(),
285 FD.D.getIdentifier());
286 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000287 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000288 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
John McCalld0014542009-12-03 22:31:13 +0000289 GetterSel, SetterSel, IDecl,
290 &isOverridingProperty,
291 MethodImplKind);
292 if (!isOverridingProperty)
293 Props.push_back(Property);
294
295 return Property;
296 }
297};
298
Steve Naroffdac269b2007-08-20 21:31:48 +0000299/// objc-interface-decl-list:
300/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000301/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000302/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000303/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000304/// objc-interface-decl-list declaration
305/// objc-interface-decl-list ';'
306///
Steve Naroff294494e2007-08-22 16:35:03 +0000307/// objc-method-requirement: [OBJC2]
308/// @required
309/// @optional
310///
John McCalld226f652010-08-21 09:40:31 +0000311void Parser::ParseObjCInterfaceDeclList(Decl *interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000312 tok::ObjCKeywordKind contextKey) {
John McCalld226f652010-08-21 09:40:31 +0000313 llvm::SmallVector<Decl *, 32> allMethods;
314 llvm::SmallVector<Decl *, 16> allProperties;
Chris Lattner682bf922009-03-29 16:50:03 +0000315 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000316 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremenek782f2f52010-01-07 01:20:12 +0000318 SourceRange AtEnd;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000319
Steve Naroff294494e2007-08-22 16:35:03 +0000320 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000321 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000322 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000323 Decl *methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000324 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000325 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000326 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
327 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000328 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
329 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000330 continue;
331 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000332 if (Tok.is(tok::l_paren)) {
333 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000334 ParseObjCMethodDecl(Tok.getLocation(),
335 tok::minus,
336 interfaceDecl,
337 MethodImplKind);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000338 continue;
339 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000340 // Ignore excess semicolons.
341 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000342 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000343 continue;
344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattnerbc662af2008-10-20 06:10:06 +0000346 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000347 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000348 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000350 // Code completion within an Objective-C interface.
351 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000352 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000353 ObjCImpDecl? Sema::PCC_ObjCImplementation
354 : Sema::PCC_ObjCInterface);
Douglas Gregordc845342010-05-25 05:58:43 +0000355 ConsumeCodeCompletionToken();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000356 }
357
Chris Lattnere82a10f2008-10-20 05:46:22 +0000358 // If we don't have an @ directive, parse it as a function definition.
359 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000360 // The code below does not consume '}'s because it is afraid of eating the
361 // end of a namespace. Because of the way this code is structured, an
362 // erroneous r_brace would cause an infinite loop if not handled here.
363 if (Tok.is(tok::r_brace))
364 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Steve Naroff4985ace2007-08-22 18:35:33 +0000366 // FIXME: as the name implies, this rule allows function definitions.
367 // We could pass a flag or check for functions during semantic analysis.
Sean Huntbbd37c62009-11-21 08:43:09 +0000368 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000369 continue;
370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Chris Lattnere82a10f2008-10-20 05:46:22 +0000372 // Otherwise, we have an @ directive, eat the @.
373 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000374 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000375 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregordc845342010-05-25 05:58:43 +0000376 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000377 break;
378 }
379
Chris Lattnera2449b22008-10-20 05:57:40 +0000380 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Chris Lattnera2449b22008-10-20 05:57:40 +0000382 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000383 AtEnd.setBegin(AtLoc);
384 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000385 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000386 } else if (DirectiveKind == tok::objc_not_keyword) {
387 Diag(Tok, diag::err_objc_unknown_at);
388 SkipUntil(tok::semi);
389 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Chris Lattnerbc662af2008-10-20 06:10:06 +0000392 // Eat the identifier.
393 ConsumeToken();
394
Chris Lattnera2449b22008-10-20 05:57:40 +0000395 switch (DirectiveKind) {
396 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000397 // FIXME: If someone forgets an @end on a protocol, this loop will
398 // continue to eat up tons of stuff and spew lots of nonsense errors. It
399 // would probably be better to bail out if we saw an @class or @interface
400 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000401 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000402 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000403 SkipUntil(tok::r_brace, tok::at);
404 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000405
406 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000407 case tok::objc_interface:
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000408 Diag(Tok, diag::err_objc_missing_end);
409 ConsumeToken();
410 break;
411
Chris Lattnera2449b22008-10-20 05:57:40 +0000412 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000413 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000414 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000415 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000416 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000417 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000418 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000419 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000420 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Chris Lattnera2449b22008-10-20 05:57:40 +0000422 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000423 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000424 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000425
Chris Lattnere82a10f2008-10-20 05:46:22 +0000426 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000427 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000428 if (Tok.is(tok::l_paren))
Douglas Gregor4ad96852009-11-19 07:41:15 +0000429 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
430 allMethods.data(), allMethods.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000431
John McCalld0014542009-12-03 22:31:13 +0000432 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
433 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000434
Chris Lattnere82a10f2008-10-20 05:46:22 +0000435 // Parse all the comma separated declarators.
436 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000437 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000439 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
440 tok::at);
Chris Lattnera2449b22008-10-20 05:57:40 +0000441 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000442 }
Steve Naroff294494e2007-08-22 16:35:03 +0000443 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000444
445 // We break out of the big loop in two cases: when we see @end or when we see
446 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000447 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000448 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregordc845342010-05-25 05:58:43 +0000449 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000450 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000451 ConsumeToken(); // the "end" identifier
452 else
453 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Chris Lattnera2449b22008-10-20 05:57:40 +0000455 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000456 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000457 Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000458 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000459 allProperties.data(), allProperties.size(),
460 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000461}
462
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000463/// Parse property attribute declarations.
464///
465/// property-attr-decl: '(' property-attrlist ')'
466/// property-attrlist:
467/// property-attribute
468/// property-attrlist ',' property-attribute
469/// property-attribute:
470/// getter '=' identifier
471/// setter '=' identifier ':'
472/// readonly
473/// readwrite
474/// assign
475/// retain
476/// copy
477/// nonatomic
478///
John McCalld226f652010-08-21 09:40:31 +0000479void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, Decl *ClassDecl,
480 Decl **Methods,
Douglas Gregor4ad96852009-11-19 07:41:15 +0000481 unsigned NumMethods) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000482 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000483 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000485 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000486 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000487 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Douglas Gregordc845342010-05-25 05:58:43 +0000488 ConsumeCodeCompletionToken();
Steve Naroffece8e712009-10-08 21:55:05 +0000489 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000490 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000492 // If this is not an identifier at all, bail out early.
493 if (II == 0) {
494 MatchRHSPunctuation(tok::r_paren, LHSLoc);
495 return;
496 }
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Chris Lattner156b0612008-10-20 07:37:22 +0000498 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Chris Lattner92e62b02008-11-20 04:42:34 +0000500 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000501 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000502 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000503 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000504 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000505 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000506 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000507 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000508 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000509 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000510 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000511 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000512 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000513 bool IsSetter = II->getNameStart()[0] == 's';
514
Chris Lattnere00da7c2008-10-20 07:39:53 +0000515 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000516 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
517 diag::err_objc_expected_equal_for_getter;
518
519 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000520 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Douglas Gregor4ad96852009-11-19 07:41:15 +0000522 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000523 if (IsSetter)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000524 Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl,
Douglas Gregor4ad96852009-11-19 07:41:15 +0000525 Methods, NumMethods);
526 else
Douglas Gregor23c94db2010-07-02 17:43:08 +0000527 Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl,
Douglas Gregor4ad96852009-11-19 07:41:15 +0000528 Methods, NumMethods);
Douglas Gregordc845342010-05-25 05:58:43 +0000529 ConsumeCodeCompletionToken();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000530 }
531
Anders Carlsson42499be2010-10-02 17:45:21 +0000532
533 SourceLocation SelLoc;
534 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
535
536 if (!SelIdent) {
537 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
538 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000539 SkipUntil(tok::r_paren);
540 return;
541 }
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Anders Carlsson42499be2010-10-02 17:45:21 +0000543 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000544 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000545 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000547 if (ExpectAndConsume(tok::colon,
548 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000549 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000550 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000551 } else {
552 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000553 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000554 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000555 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000556 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000557 SkipUntil(tok::r_paren);
558 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Chris Lattner156b0612008-10-20 07:37:22 +0000561 if (Tok.isNot(tok::comma))
562 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Chris Lattner156b0612008-10-20 07:37:22 +0000564 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000565 }
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Chris Lattner156b0612008-10-20 07:37:22 +0000567 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000568}
569
Steve Naroff3536b442007-09-06 21:24:23 +0000570/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000571/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000572/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000573///
574/// objc-instance-method: '-'
575/// objc-class-method: '+'
576///
Steve Naroff4985ace2007-08-22 18:35:33 +0000577/// objc-method-attributes: [OBJC2]
578/// __attribute__((deprecated))
579///
John McCalld226f652010-08-21 09:40:31 +0000580Decl *Parser::ParseObjCMethodPrototype(Decl *IDecl,
581 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000582 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000583
Mike Stump1eb44332009-09-09 15:08:12 +0000584 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000585 SourceLocation mLoc = ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000586 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000587 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000588 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000589 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000590}
591
592/// objc-selector:
593/// identifier
594/// one of
595/// enum struct union if else while do for switch case default
596/// break continue return goto asm sizeof typeof __alignof
597/// unsigned long const short volatile signed restrict _Complex
598/// in out inout bycopy byref oneway int char float double void _Bool
599///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000600IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000601
Chris Lattnerff384912007-10-07 02:00:24 +0000602 switch (Tok.getKind()) {
603 default:
604 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000605 case tok::ampamp:
606 case tok::ampequal:
607 case tok::amp:
608 case tok::pipe:
609 case tok::tilde:
610 case tok::exclaim:
611 case tok::exclaimequal:
612 case tok::pipepipe:
613 case tok::pipeequal:
614 case tok::caret:
615 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000616 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000617 if (isalpha(ThisTok[0])) {
618 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
619 Tok.setKind(tok::identifier);
620 SelectorLoc = ConsumeToken();
621 return II;
622 }
623 return 0;
624 }
625
Chris Lattnerff384912007-10-07 02:00:24 +0000626 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000627 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000628 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000629 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000630 case tok::kw_break:
631 case tok::kw_case:
632 case tok::kw_catch:
633 case tok::kw_char:
634 case tok::kw_class:
635 case tok::kw_const:
636 case tok::kw_const_cast:
637 case tok::kw_continue:
638 case tok::kw_default:
639 case tok::kw_delete:
640 case tok::kw_do:
641 case tok::kw_double:
642 case tok::kw_dynamic_cast:
643 case tok::kw_else:
644 case tok::kw_enum:
645 case tok::kw_explicit:
646 case tok::kw_export:
647 case tok::kw_extern:
648 case tok::kw_false:
649 case tok::kw_float:
650 case tok::kw_for:
651 case tok::kw_friend:
652 case tok::kw_goto:
653 case tok::kw_if:
654 case tok::kw_inline:
655 case tok::kw_int:
656 case tok::kw_long:
657 case tok::kw_mutable:
658 case tok::kw_namespace:
659 case tok::kw_new:
660 case tok::kw_operator:
661 case tok::kw_private:
662 case tok::kw_protected:
663 case tok::kw_public:
664 case tok::kw_register:
665 case tok::kw_reinterpret_cast:
666 case tok::kw_restrict:
667 case tok::kw_return:
668 case tok::kw_short:
669 case tok::kw_signed:
670 case tok::kw_sizeof:
671 case tok::kw_static:
672 case tok::kw_static_cast:
673 case tok::kw_struct:
674 case tok::kw_switch:
675 case tok::kw_template:
676 case tok::kw_this:
677 case tok::kw_throw:
678 case tok::kw_true:
679 case tok::kw_try:
680 case tok::kw_typedef:
681 case tok::kw_typeid:
682 case tok::kw_typename:
683 case tok::kw_typeof:
684 case tok::kw_union:
685 case tok::kw_unsigned:
686 case tok::kw_using:
687 case tok::kw_virtual:
688 case tok::kw_void:
689 case tok::kw_volatile:
690 case tok::kw_wchar_t:
691 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000692 case tok::kw__Bool:
693 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000694 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000695 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000696 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000697 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000698 }
Steve Naroff294494e2007-08-22 16:35:03 +0000699}
700
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000701/// objc-for-collection-in: 'in'
702///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000703bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000704 // FIXME: May have to do additional look-ahead to only allow for
705 // valid tokens following an 'in'; such as an identifier, unary operators,
706 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000707 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000708 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000709}
710
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000711/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000712/// qualifier list and builds their bitmask representation in the input
713/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000714///
715/// objc-type-qualifiers:
716/// objc-type-qualifier
717/// objc-type-qualifiers objc-type-qualifier
718///
Douglas Gregord32b0222010-08-24 01:06:58 +0000719void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS, bool IsParameter) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000720 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000721 if (Tok.is(tok::code_completion)) {
722 Actions.CodeCompleteObjCPassingType(getCurScope(), DS);
723 ConsumeCodeCompletionToken();
724 }
725
Chris Lattnercb53b362007-12-27 19:57:00 +0000726 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000727 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Chris Lattnere8b724d2007-12-12 06:56:32 +0000729 const IdentifierInfo *II = Tok.getIdentifierInfo();
730 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000731 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000732 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000734 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000735 switch (i) {
736 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000737 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
738 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
739 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
740 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
741 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
742 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000743 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000744 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000745 ConsumeToken();
746 II = 0;
747 break;
748 }
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Chris Lattnere8b724d2007-12-12 06:56:32 +0000750 // If this wasn't a recognized qualifier, bail out.
751 if (II) return;
752 }
753}
754
755/// objc-type-name:
756/// '(' objc-type-qualifiers[opt] type-name ')'
757/// '(' objc-type-qualifiers[opt] ')'
758///
John McCallb3d87482010-08-24 05:47:05 +0000759ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS, bool IsParameter) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000760 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Chris Lattner4a76b292008-10-22 03:52:06 +0000762 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000763 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000765 // Parse type qualifiers, in, inout, etc.
Douglas Gregord32b0222010-08-24 01:06:58 +0000766 ParseObjCTypeQualifierList(DS, IsParameter);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000767
John McCallb3d87482010-08-24 05:47:05 +0000768 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000769 if (isTypeSpecifierQualifier()) {
770 TypeResult TypeSpec = ParseTypeName();
771 if (!TypeSpec.isInvalid())
772 Ty = TypeSpec.get();
773 }
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Steve Naroffd7333c22008-10-21 14:15:04 +0000775 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000776 ConsumeParen();
777 else if (Tok.getLocation() == TypeStartLoc) {
778 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000779 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000780 SkipUntil(tok::r_paren);
781 } else {
782 // Otherwise, we found *something*, but didn't get a ')' in the right
783 // place. Emit an error then return what we have as the type.
784 MatchRHSPunctuation(tok::r_paren, LParenLoc);
785 }
Steve Narofff28b2642007-09-05 23:30:30 +0000786 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000787}
788
789/// objc-method-decl:
790/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000791/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000792/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000793/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000794///
795/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000796/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000797/// objc-keyword-selector objc-keyword-decl
798///
799/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000800/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
801/// objc-selector ':' objc-keyword-attributes[opt] identifier
802/// ':' objc-type-name objc-keyword-attributes[opt] identifier
803/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000804///
Steve Naroff4985ace2007-08-22 18:35:33 +0000805/// objc-parmlist:
806/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000807///
Steve Naroff4985ace2007-08-22 18:35:33 +0000808/// objc-parms:
809/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000810///
Steve Naroff4985ace2007-08-22 18:35:33 +0000811/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000812/// , ...
813///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000814/// objc-keyword-attributes: [OBJC2]
815/// __attribute__((unused))
816///
John McCalld226f652010-08-21 09:40:31 +0000817Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000818 tok::TokenKind mType,
819 Decl *IDecl,
820 tok::ObjCKeywordKind MethodImplKind) {
John McCall54abf7d2009-11-04 02:18:39 +0000821 ParsingDeclRAIIObject PD(*this);
822
Douglas Gregore8f5a172010-04-07 00:21:17 +0000823 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000824 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
John McCallb3d87482010-08-24 05:47:05 +0000825 /*ReturnType=*/ ParsedType(), IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000826 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000827 }
828
Chris Lattnere8904e92008-08-23 01:48:03 +0000829 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000830 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000831 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000832 if (Tok.is(tok::l_paren))
Douglas Gregord32b0222010-08-24 01:06:58 +0000833 ReturnType = ParseObjCTypeName(DSRet, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Ted Kremenek9e049352010-02-18 23:05:16 +0000835 // If attributes exist before the method, parse them.
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000836 AttributeList *MethodAttrs = 0;
Ted Kremenek9e049352010-02-18 23:05:16 +0000837 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000838 MethodAttrs = ParseGNUAttributes();
Ted Kremenek9e049352010-02-18 23:05:16 +0000839
Douglas Gregore8f5a172010-04-07 00:21:17 +0000840 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000841 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Douglas Gregore8f5a172010-04-07 00:21:17 +0000842 ReturnType, IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000843 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000844 }
845
Ted Kremenek9e049352010-02-18 23:05:16 +0000846 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000847 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000848 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000849
Steve Naroff84c43102009-02-11 20:43:13 +0000850 // An unnamed colon is valid.
851 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000852 Diag(Tok, diag::err_expected_selector_for_method)
853 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000854 // Skip until we get a ; or {}.
855 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000856 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000857 }
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000859 llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000860 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000861 // If attributes exist after the method, parse them.
Mike Stump1eb44332009-09-09 15:08:12 +0000862 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000863 MethodAttrs = addAttributeLists(MethodAttrs, ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Chris Lattnerff384912007-10-07 02:00:24 +0000865 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000866 Decl *Result
John McCall54abf7d2009-11-04 02:18:39 +0000867 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000868 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000869 0,
870 CParamInfo.data(), CParamInfo.size(),
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000871 MethodAttrs, MethodImplKind);
John McCall54abf7d2009-11-04 02:18:39 +0000872 PD.complete(Result);
873 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000874 }
Steve Narofff28b2642007-09-05 23:30:30 +0000875
Steve Naroff68d331a2007-09-27 14:38:14 +0000876 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
John McCallf312b1e2010-08-26 23:41:50 +0000877 llvm::SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Chris Lattnerff384912007-10-07 02:00:24 +0000879 while (1) {
John McCallf312b1e2010-08-26 23:41:50 +0000880 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Chris Lattnerff384912007-10-07 02:00:24 +0000882 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000883 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000884 Diag(Tok, diag::err_expected_colon);
885 break;
886 }
887 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000888
John McCallb3d87482010-08-24 05:47:05 +0000889 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +0000890 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregord32b0222010-08-24 01:06:58 +0000891 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, true);
Chris Lattnere294d3f2009-04-11 18:57:04 +0000892
Chris Lattnerff384912007-10-07 02:00:24 +0000893 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000894 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000895 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000896 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000897
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000898 // Code completion for the next piece of the selector.
899 if (Tok.is(tok::code_completion)) {
900 ConsumeCodeCompletionToken();
901 KeyIdents.push_back(SelIdent);
902 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
903 mType == tok::minus,
904 /*AtParameterName=*/true,
905 ReturnType,
906 KeyIdents.data(),
907 KeyIdents.size());
908 KeyIdents.pop_back();
909 break;
910 }
911
Chris Lattnerdf195262007-10-09 17:51:17 +0000912 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000913 Diag(Tok, diag::err_expected_ident); // missing argument name.
914 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000915 }
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Chris Lattnere294d3f2009-04-11 18:57:04 +0000917 ArgInfo.Name = Tok.getIdentifierInfo();
918 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000919 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Chris Lattnere294d3f2009-04-11 18:57:04 +0000921 ArgInfos.push_back(ArgInfo);
922 KeyIdents.push_back(SelIdent);
923
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000924 // Code completion for the next piece of the selector.
925 if (Tok.is(tok::code_completion)) {
926 ConsumeCodeCompletionToken();
927 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
928 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000929 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000930 ReturnType,
931 KeyIdents.data(),
932 KeyIdents.size());
933 break;
934 }
935
Chris Lattnerff384912007-10-07 02:00:24 +0000936 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000937 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000938 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000939 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000940 break;
941 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000942 }
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Steve Naroff335eafa2007-11-15 12:35:21 +0000944 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Chris Lattnerff384912007-10-07 02:00:24 +0000946 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000947 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000948 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000949 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000950 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000951 ConsumeToken();
952 break;
953 }
Chris Lattnerff384912007-10-07 02:00:24 +0000954 DeclSpec DS;
955 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000956 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000957 Declarator ParmDecl(DS, Declarator::PrototypeContext);
958 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000959 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +0000960 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000961 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
962 ParmDecl.getIdentifierLoc(),
963 Param,
964 0));
965
Chris Lattnerff384912007-10-07 02:00:24 +0000966 }
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +0000968 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000969 // If attributes exist after the method, parse them.
Mike Stump1eb44332009-09-09 15:08:12 +0000970 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000971 MethodAttrs = addAttributeLists(MethodAttrs, ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000973 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +0000974 return 0;
Chris Lattnerff384912007-10-07 02:00:24 +0000975 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
976 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +0000977 Decl *Result
John McCall54abf7d2009-11-04 02:18:39 +0000978 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000979 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000980 &ArgInfos[0],
981 CParamInfo.data(), CParamInfo.size(),
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000982 MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000983 MethodImplKind, isVariadic);
John McCall54abf7d2009-11-04 02:18:39 +0000984 PD.complete(Result);
985 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +0000986}
987
Steve Naroffdac269b2007-08-20 21:31:48 +0000988/// objc-protocol-refs:
989/// '<' identifier-list '>'
990///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000991bool Parser::
John McCalld226f652010-08-21 09:40:31 +0000992ParseObjCProtocolReferences(llvm::SmallVectorImpl<Decl *> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000993 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
994 bool WarnOnDeclarations,
995 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000996 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000998 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Chris Lattnere13b9592008-07-26 04:03:38 +00001000 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Chris Lattnere13b9592008-07-26 04:03:38 +00001002 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001003 if (Tok.is(tok::code_completion)) {
1004 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1005 ProtocolIdents.size());
Douglas Gregordc845342010-05-25 05:58:43 +00001006 ConsumeCodeCompletionToken();
Douglas Gregor55385fe2009-11-18 04:19:12 +00001007 }
1008
Chris Lattnere13b9592008-07-26 04:03:38 +00001009 if (Tok.isNot(tok::identifier)) {
1010 Diag(Tok, diag::err_expected_ident);
1011 SkipUntil(tok::greater);
1012 return true;
1013 }
1014 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1015 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001016 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001017 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Chris Lattnere13b9592008-07-26 04:03:38 +00001019 if (Tok.isNot(tok::comma))
1020 break;
1021 ConsumeToken();
1022 }
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Chris Lattnere13b9592008-07-26 04:03:38 +00001024 // Consume the '>'.
1025 if (Tok.isNot(tok::greater)) {
1026 Diag(Tok, diag::err_expected_greater);
1027 return true;
1028 }
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Chris Lattnere13b9592008-07-26 04:03:38 +00001030 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Chris Lattnere13b9592008-07-26 04:03:38 +00001032 // Convert the list of protocols identifiers into a list of protocol decls.
1033 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1034 &ProtocolIdents[0], ProtocolIdents.size(),
1035 Protocols);
1036 return false;
1037}
1038
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001039/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1040/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001041bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001042 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1043 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1044 SourceLocation LAngleLoc, EndProtoLoc;
1045 llvm::SmallVector<Decl *, 8> ProtocolDecl;
1046 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001047 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1048 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001049 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1050 ProtocolLocs.data(), LAngleLoc);
1051 if (EndProtoLoc.isValid())
1052 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001053 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001054}
1055
1056
Steve Naroffdac269b2007-08-20 21:31:48 +00001057/// objc-class-instance-variables:
1058/// '{' objc-instance-variable-decl-list[opt] '}'
1059///
1060/// objc-instance-variable-decl-list:
1061/// objc-visibility-spec
1062/// objc-instance-variable-decl ';'
1063/// ';'
1064/// objc-instance-variable-decl-list objc-visibility-spec
1065/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1066/// objc-instance-variable-decl-list ';'
1067///
1068/// objc-visibility-spec:
1069/// @private
1070/// @protected
1071/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001072/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001073///
1074/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001075/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001076///
John McCalld226f652010-08-21 09:40:31 +00001077void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001078 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001079 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001080 assert(Tok.is(tok::l_brace) && "expected {");
John McCalld226f652010-08-21 09:40:31 +00001081 llvm::SmallVector<Decl *, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001082
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001083 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001084
Steve Naroffddbff782007-08-21 21:17:12 +00001085 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Steve Naroffddbff782007-08-21 21:17:12 +00001087 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001088 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001089 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Steve Naroffddbff782007-08-21 21:17:12 +00001091 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001092 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001093 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001094 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001095 ConsumeToken();
1096 continue;
1097 }
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Steve Naroffddbff782007-08-21 21:17:12 +00001099 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001100 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001101 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001102
1103 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001104 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001105 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001106 }
1107
Steve Naroff861cf3e2007-08-23 18:16:40 +00001108 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001109 case tok::objc_private:
1110 case tok::objc_public:
1111 case tok::objc_protected:
1112 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001113 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001114 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001115 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001116 default:
1117 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001118 continue;
1119 }
1120 }
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001122 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001123 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001124 Sema::PCC_ObjCInstanceVariableList);
Douglas Gregordc845342010-05-25 05:58:43 +00001125 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001126 }
1127
John McCallbdd563e2009-11-03 02:38:08 +00001128 struct ObjCIvarCallback : FieldCallback {
1129 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001130 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001131 tok::ObjCKeywordKind visibility;
John McCalld226f652010-08-21 09:40:31 +00001132 llvm::SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001133
John McCalld226f652010-08-21 09:40:31 +00001134 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
1135 llvm::SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001136 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1137 }
1138
John McCalld226f652010-08-21 09:40:31 +00001139 Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001140 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001141 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001142 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001143 FD.D.getDeclSpec().getSourceRange().getBegin(),
1144 IDecl, FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001145 if (Field)
1146 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001147 return Field;
1148 }
1149 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001150
Chris Lattnere1359422008-04-10 06:46:29 +00001151 // Parse all the comma separated declarators.
1152 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +00001153 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Chris Lattnerdf195262007-10-09 17:51:17 +00001155 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001156 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001157 } else {
1158 Diag(Tok, diag::err_expected_semi_decl_list);
1159 // Skip to end of block or statement
1160 SkipUntil(tok::r_brace, true, true);
1161 }
1162 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001163 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001164 Actions.ActOnLastBitfield(RBraceLoc, interfaceDecl, AllIvarDecls);
Steve Naroff8749be52007-10-31 22:11:35 +00001165 // Call ActOnFields() even if we don't have any decls. This is useful
1166 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001167 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001168 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001169 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001170 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001171}
Steve Naroffdac269b2007-08-20 21:31:48 +00001172
1173/// objc-protocol-declaration:
1174/// objc-protocol-definition
1175/// objc-protocol-forward-reference
1176///
1177/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001178/// @protocol identifier
1179/// objc-protocol-refs[opt]
1180/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001181/// @end
1182///
1183/// objc-protocol-forward-reference:
1184/// @protocol identifier-list ';'
1185///
1186/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001187/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001188/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCalld226f652010-08-21 09:40:31 +00001189Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001190 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001191 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001192 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1193 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Douglas Gregor083128f2009-11-18 04:49:41 +00001195 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001196 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001197 ConsumeCodeCompletionToken();
Douglas Gregor083128f2009-11-18 04:49:41 +00001198 }
1199
Chris Lattnerdf195262007-10-09 17:51:17 +00001200 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001201 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCalld226f652010-08-21 09:40:31 +00001202 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001203 }
1204 // Save the protocol name, then consume it.
1205 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1206 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Chris Lattnerdf195262007-10-09 17:51:17 +00001208 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001209 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001210 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001211 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001212 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001213 }
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Chris Lattnerdf195262007-10-09 17:51:17 +00001215 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001216 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1217 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1218
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001219 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001220 while (1) {
1221 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001222 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001223 Diag(Tok, diag::err_expected_ident);
1224 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001225 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001226 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001227 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1228 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001229 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Chris Lattnerdf195262007-10-09 17:51:17 +00001231 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001232 break;
1233 }
1234 // Consume the ';'.
1235 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCalld226f652010-08-21 09:40:31 +00001236 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Steve Naroffe440eb82007-10-10 17:32:04 +00001238 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001239 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001240 ProtocolRefs.size(),
1241 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001242 }
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001244 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001245 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001246
John McCalld226f652010-08-21 09:40:31 +00001247 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001248 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001249 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001250 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1251 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +00001252 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001253
John McCalld226f652010-08-21 09:40:31 +00001254 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001255 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001256 ProtocolRefs.data(),
1257 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001258 ProtocolLocs.data(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001259 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001260 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001261 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001262}
Steve Naroffdac269b2007-08-20 21:31:48 +00001263
1264/// objc-implementation:
1265/// objc-class-implementation-prologue
1266/// objc-category-implementation-prologue
1267///
1268/// objc-class-implementation-prologue:
1269/// @implementation identifier objc-superclass[opt]
1270/// objc-class-instance-variables[opt]
1271///
1272/// objc-category-implementation-prologue:
1273/// @implementation identifier ( identifier )
John McCalld226f652010-08-21 09:40:31 +00001274Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001275 SourceLocation atLoc) {
1276 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1277 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1278 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001280 // Code completion after '@implementation'.
1281 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001282 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001283 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001284 }
1285
Chris Lattnerdf195262007-10-09 17:51:17 +00001286 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001287 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001288 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001289 }
1290 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001291 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001292 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001293
1294 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001295 // we have a category implementation.
1296 SourceLocation lparenLoc = ConsumeParen();
1297 SourceLocation categoryLoc, rparenLoc;
1298 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001300 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001301 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +00001302 ConsumeCodeCompletionToken();
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001303 }
1304
Chris Lattnerdf195262007-10-09 17:51:17 +00001305 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001306 categoryId = Tok.getIdentifierInfo();
1307 categoryLoc = ConsumeToken();
1308 } else {
1309 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001310 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001311 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001312 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001313 Diag(Tok, diag::err_expected_rparen);
1314 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001315 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001316 }
1317 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001318 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001319 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001320 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001321 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001322 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001323 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001324 }
1325 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001326 SourceLocation superClassLoc;
1327 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001328 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001329 // We have a super class
1330 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001331 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001332 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001333 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001334 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001335 superClassId = Tok.getIdentifierInfo();
1336 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001337 }
John McCalld226f652010-08-21 09:40:31 +00001338 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001339 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001340 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Steve Naroff60fccee2007-10-29 21:38:07 +00001342 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001343 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahanian01f1bfc2010-03-22 19:04:14 +00001344 tok::objc_private, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001345 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001346 PendingObjCImpDecl.push_back(ObjCImpDecl);
1347
John McCalld226f652010-08-21 09:40:31 +00001348 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001349}
Steve Naroff60fccee2007-10-29 21:38:07 +00001350
John McCalld226f652010-08-21 09:40:31 +00001351Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001352 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1353 "ParseObjCAtEndDeclaration(): Expected @end");
John McCalld226f652010-08-21 09:40:31 +00001354 Decl *Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001355 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001356 if (ObjCImpDecl) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001357 Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001358 ObjCImpDecl = 0;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001359 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001360 }
Ted Kremenek782f2f52010-01-07 01:20:12 +00001361 else {
1362 // missing @implementation
1363 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1364 }
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001365 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001366}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001367
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001368Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1369 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001370 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001371 return Actions.ConvertDeclToDeclGroup(0);
1372 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001373 Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001374 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1375}
1376
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001377/// compatibility-alias-decl:
1378/// @compatibility_alias alias-name class-name ';'
1379///
John McCalld226f652010-08-21 09:40:31 +00001380Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001381 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1382 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1383 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001384 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001385 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001386 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001387 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001388 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1389 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001390 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001391 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001392 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001393 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001394 IdentifierInfo *classId = Tok.getIdentifierInfo();
1395 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1396 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001397 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
John McCalld226f652010-08-21 09:40:31 +00001398 return 0;
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001399 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001400 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1401 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001402}
1403
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001404/// property-synthesis:
1405/// @synthesize property-ivar-list ';'
1406///
1407/// property-ivar-list:
1408/// property-ivar
1409/// property-ivar-list ',' property-ivar
1410///
1411/// property-ivar:
1412/// identifier
1413/// identifier '=' identifier
1414///
John McCalld226f652010-08-21 09:40:31 +00001415Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001416 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1417 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001418 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Douglas Gregorb328c422009-11-18 19:45:45 +00001420 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001421 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001422 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001423 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001424 }
1425
Douglas Gregorb328c422009-11-18 19:45:45 +00001426 if (Tok.isNot(tok::identifier)) {
1427 Diag(Tok, diag::err_synthesized_property_name);
1428 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001429 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001430 }
1431
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001432 IdentifierInfo *propertyIvar = 0;
1433 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1434 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001435 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001436 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001437 // property '=' ivar-name
1438 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001439
1440 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001441 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId,
Douglas Gregor322328b2009-11-18 22:32:06 +00001442 ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001443 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001444 }
1445
Chris Lattnerdf195262007-10-09 17:51:17 +00001446 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001447 Diag(Tok, diag::err_expected_ident);
1448 break;
1449 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001450 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001451 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001452 }
Douglas Gregor23c94db2010-07-02 17:43:08 +00001453 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001454 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001455 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001456 break;
1457 ConsumeToken(); // consume ','
1458 }
Douglas Gregorb328c422009-11-18 19:45:45 +00001459 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001460 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregorb328c422009-11-18 19:45:45 +00001461 SkipUntil(tok::semi);
1462 }
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001463 else
1464 ConsumeToken(); // consume ';'
John McCalld226f652010-08-21 09:40:31 +00001465 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001466}
1467
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001468/// property-dynamic:
1469/// @dynamic property-list
1470///
1471/// property-list:
1472/// identifier
1473/// property-list ',' identifier
1474///
John McCalld226f652010-08-21 09:40:31 +00001475Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001476 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1477 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1478 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001479 while (true) {
1480 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001481 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001482 ConsumeCodeCompletionToken();
Douglas Gregor424b2a52009-11-18 22:56:13 +00001483 }
1484
1485 if (Tok.isNot(tok::identifier)) {
1486 Diag(Tok, diag::err_expected_ident);
1487 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001488 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001489 }
1490
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001491 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1492 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregor23c94db2010-07-02 17:43:08 +00001493 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001494 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001495
Chris Lattnerdf195262007-10-09 17:51:17 +00001496 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001497 break;
1498 ConsumeToken(); // consume ','
1499 }
Fariborz Jahanian94b24db2010-04-14 20:52:42 +00001500 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001501 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanian94b24db2010-04-14 20:52:42 +00001502 SkipUntil(tok::semi);
1503 }
1504 else
1505 ConsumeToken(); // consume ';'
John McCalld226f652010-08-21 09:40:31 +00001506 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001507}
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001509/// objc-throw-statement:
1510/// throw expression[opt];
1511///
John McCall60d7b3a2010-08-24 06:29:42 +00001512StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1513 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001514 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001515 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001516 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001517 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001518 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001519 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001520 }
1521 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001522 // consume ';'
1523 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001524 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001525}
1526
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001527/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001528/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001529///
John McCall60d7b3a2010-08-24 06:29:42 +00001530StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001531Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001532 ConsumeToken(); // consume synchronized
1533 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001534 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001535 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001536 }
1537 ConsumeParen(); // '('
John McCall60d7b3a2010-08-24 06:29:42 +00001538 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001539 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001540 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001541 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001542 }
1543 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001544 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001545 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001546 }
1547 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001548 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001549 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001550 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001551 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001552 // Enter a scope to hold everything within the compound stmt. Compound
1553 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001554 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001555
John McCall60d7b3a2010-08-24 06:29:42 +00001556 StmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001557
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001558 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001559 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001560 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
John McCall9ae2f072010-08-23 23:25:46 +00001561 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.take(), SynchBody.take());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001562}
1563
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001564/// objc-try-catch-statement:
1565/// @try compound-statement objc-catch-list[opt]
1566/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1567///
1568/// objc-catch-list:
1569/// @catch ( parameter-declaration ) compound-statement
1570/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1571/// catch-parameter-declaration:
1572/// parameter-declaration
1573/// '...' [OBJC2]
1574///
John McCall60d7b3a2010-08-24 06:29:42 +00001575StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001576 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001577
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001578 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001579 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001580 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001581 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001582 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001583 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001584 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001585 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001586 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001587 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001588 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001589 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001590
Chris Lattnerdf195262007-10-09 17:51:17 +00001591 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001592 // At this point, we need to lookahead to determine if this @ is the start
1593 // of an @catch or @finally. We don't want to consume the @ token if this
1594 // is an @try or @encode or something else.
1595 Token AfterAt = GetLookAheadToken(1);
1596 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1597 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1598 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001599
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001600 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001601 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001602 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001603 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001604 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001605 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001606 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001607 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001608 DeclSpec DS;
1609 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001610 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001611 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001612 // we must accept either 'declarator' or 'abstract-declarator' here.
1613 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1614 ParseDeclarator(ParmDecl);
1615
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001616 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001617 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001618 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001619 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001620 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Steve Naroff93a25952009-04-07 22:56:58 +00001622 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Steve Naroff93a25952009-04-07 22:56:58 +00001624 if (Tok.is(tok::r_paren))
1625 RParenLoc = ConsumeParen();
1626 else // Skip over garbage, until we get to ')'. Eat the ')'.
1627 SkipUntil(tok::r_paren, true, false);
1628
John McCall60d7b3a2010-08-24 06:29:42 +00001629 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001630 if (Tok.is(tok::l_brace))
1631 CatchBody = ParseCompoundStatementBody();
1632 else
1633 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001634 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001635 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001636
John McCall60d7b3a2010-08-24 06:29:42 +00001637 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001638 RParenLoc,
1639 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001640 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001641 if (!Catch.isInvalid())
1642 CatchStmts.push_back(Catch.release());
1643
Steve Naroff64515f32008-02-05 21:27:35 +00001644 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001645 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1646 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001647 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001648 }
1649 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001650 } else {
1651 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001652 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001653 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001654
John McCall60d7b3a2010-08-24 06:29:42 +00001655 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001656 if (Tok.is(tok::l_brace))
1657 FinallyBody = ParseCompoundStatementBody();
1658 else
1659 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001660 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001661 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001662 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001663 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001664 catch_or_finally_seen = true;
1665 break;
1666 }
1667 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001668 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001669 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001670 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001671 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001672
John McCall9ae2f072010-08-23 23:25:46 +00001673 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001674 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001675 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001676}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001677
Steve Naroff3536b442007-09-06 21:24:23 +00001678/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001679///
John McCalld226f652010-08-21 09:40:31 +00001680Decl *Parser::ParseObjCMethodDefinition() {
1681 Decl *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001682
John McCallf312b1e2010-08-26 23:41:50 +00001683 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1684 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001686 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001687 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001688 if (ObjCImpDecl) {
1689 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001690 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001691 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001692 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001693 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001694
Steve Naroff409be832007-11-11 19:54:21 +00001695 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001696 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001697 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Steve Naroff409be832007-11-11 19:54:21 +00001699 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1700 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Steve Naroff409be832007-11-11 19:54:21 +00001702 // If we didn't find the '{', bail out.
1703 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001704 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001705 }
Steve Naroff409be832007-11-11 19:54:21 +00001706 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001707
Steve Naroff409be832007-11-11 19:54:21 +00001708 // Enter a scope for the method body.
Chris Lattner15faee12010-04-12 05:38:43 +00001709 ParseScope BodyScope(this,
1710 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Steve Naroff409be832007-11-11 19:54:21 +00001712 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001713 // specified Declarator for the method.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001714 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001715
John McCall60d7b3a2010-08-24 06:29:42 +00001716 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001717
Steve Naroff409be832007-11-11 19:54:21 +00001718 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001719 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001720 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1721 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001722
Steve Naroff32ce8372009-03-02 22:00:56 +00001723 // TODO: Pass argument information.
John McCall9ae2f072010-08-23 23:25:46 +00001724 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001725
Steve Naroff409be832007-11-11 19:54:21 +00001726 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001727 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001728
Steve Naroff71c0a952007-11-13 23:01:27 +00001729 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001730}
Anders Carlsson55085182007-08-21 17:43:55 +00001731
John McCall60d7b3a2010-08-24 06:29:42 +00001732StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001733 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001734 Actions.CodeCompleteObjCAtStatement(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001735 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001736 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001737 }
1738
1739 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001740 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001741
1742 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001743 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001744
1745 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001746 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001747
John McCall60d7b3a2010-08-24 06:29:42 +00001748 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001749 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001750 // If the expression is invalid, skip ahead to the next semicolon. Not
1751 // doing this opens us up to the possibility of infinite loops if
1752 // ParseExpression does not consume any tokens.
1753 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001754 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001755 }
Chris Lattner5d803162009-12-07 16:33:19 +00001756
Steve Naroff64515f32008-02-05 21:27:35 +00001757 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001758 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001759 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001760}
1761
John McCall60d7b3a2010-08-24 06:29:42 +00001762ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001763 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001764 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001765 Actions.CodeCompleteObjCAtExpression(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001766 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001767 return ExprError();
1768
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001769 case tok::string_literal: // primary-expression: string-literal
1770 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001771 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001772 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001773 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001774 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001775
Chris Lattner4fef81d2008-08-05 06:19:09 +00001776 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1777 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001778 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001779 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001780 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001781 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001782 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001783 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001784 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001785 }
Anders Carlsson55085182007-08-21 17:43:55 +00001786 }
Anders Carlsson55085182007-08-21 17:43:55 +00001787}
1788
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001789/// \brirg Parse the receiver of an Objective-C++ message send.
1790///
1791/// This routine parses the receiver of a message send in
1792/// Objective-C++ either as a type or as an expression. Note that this
1793/// routine must not be called to parse a send to 'super', since it
1794/// has no way to return such a result.
1795///
1796/// \param IsExpr Whether the receiver was parsed as an expression.
1797///
1798/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1799/// IsExpr is true), the parsed expression. If the receiver was parsed
1800/// as a type (\c IsExpr is false), the parsed type.
1801///
1802/// \returns True if an error occurred during parsing or semantic
1803/// analysis, in which case the arguments do not have valid
1804/// values. Otherwise, returns false for a successful parse.
1805///
1806/// objc-receiver: [C++]
1807/// 'super' [not parsed here]
1808/// expression
1809/// simple-type-specifier
1810/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001811bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001812 InMessageExpressionRAIIObject InMessage(*this, true);
1813
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001814 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1815 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1816 TryAnnotateTypeOrScopeToken();
1817
1818 if (!isCXXSimpleTypeSpecifier()) {
1819 // objc-receiver:
1820 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00001821 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001822 if (Receiver.isInvalid())
1823 return true;
1824
1825 IsExpr = true;
1826 TypeOrExpr = Receiver.take();
1827 return false;
1828 }
1829
1830 // objc-receiver:
1831 // typename-specifier
1832 // simple-type-specifier
1833 // expression (that starts with one of the above)
1834 DeclSpec DS;
1835 ParseCXXSimpleTypeSpecifier(DS);
1836
1837 if (Tok.is(tok::l_paren)) {
1838 // If we see an opening parentheses at this point, we are
1839 // actually parsing an expression that starts with a
1840 // function-style cast, e.g.,
1841 //
1842 // postfix-expression:
1843 // simple-type-specifier ( expression-list [opt] )
1844 // typename-specifier ( expression-list [opt] )
1845 //
1846 // Parse the remainder of this case, then the (optional)
1847 // postfix-expression suffix, followed by the (optional)
1848 // right-hand side of the binary expression. We have an
1849 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00001850 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001851 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001852 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001853 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001854 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001855 if (Receiver.isInvalid())
1856 return true;
1857
1858 IsExpr = true;
1859 TypeOrExpr = Receiver.take();
1860 return false;
1861 }
1862
1863 // We have a class message. Turn the simple-type-specifier or
1864 // typename-specifier we parsed into a type and parse the
1865 // remainder of the class message.
1866 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001867 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001868 if (Type.isInvalid())
1869 return true;
1870
1871 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00001872 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001873 return false;
1874}
1875
Douglas Gregor1b730e82010-05-31 14:40:22 +00001876/// \brief Determine whether the parser is currently referring to a an
1877/// Objective-C message send, using a simplified heuristic to avoid overhead.
1878///
1879/// This routine will only return true for a subset of valid message-send
1880/// expressions.
1881bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00001882 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00001883 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00001884 return GetLookAheadToken(1).is(tok::identifier) &&
1885 GetLookAheadToken(2).is(tok::identifier);
1886}
1887
Douglas Gregor9497a732010-09-16 01:51:54 +00001888bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
1889 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
1890 InMessageExpression)
1891 return false;
1892
1893
1894 ParsedType Type;
1895
1896 if (Tok.is(tok::annot_typename))
1897 Type = getTypeAnnotation(Tok);
1898 else if (Tok.is(tok::identifier))
1899 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
1900 getCurScope());
1901 else
1902 return false;
1903
1904 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
1905 const Token &AfterNext = GetLookAheadToken(2);
1906 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
1907 if (Tok.is(tok::identifier))
1908 TryAnnotateTypeOrScopeToken();
1909
1910 return Tok.is(tok::annot_typename);
1911 }
1912 }
1913
1914 return false;
1915}
1916
Mike Stump1eb44332009-09-09 15:08:12 +00001917/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001918/// '[' objc-receiver objc-message-args ']'
1919///
Douglas Gregor2725ca82010-04-21 19:57:20 +00001920/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00001921/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001922/// expression
1923/// class-name
1924/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001925///
John McCall60d7b3a2010-08-24 06:29:42 +00001926ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001927 assert(Tok.is(tok::l_square) && "'[' expected");
1928 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1929
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001930 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001931 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001932 ConsumeCodeCompletionToken();
1933 SkipUntil(tok::r_square);
1934 return ExprError();
1935 }
1936
Douglas Gregor0fbda682010-09-15 14:51:05 +00001937 InMessageExpressionRAIIObject InMessage(*this, true);
1938
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001939 if (getLang().CPlusPlus) {
1940 // We completely separate the C and C++ cases because C++ requires
1941 // more complicated (read: slower) parsing.
1942
1943 // Handle send to super.
1944 // FIXME: This doesn't benefit from the same typo-correction we
1945 // get in Objective-C.
1946 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001947 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00001948 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1949 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001950
1951 // Parse the receiver, which is either a type or an expression.
1952 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00001953 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001954 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
1955 SkipUntil(tok::r_square);
1956 return ExprError();
1957 }
1958
1959 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00001960 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1961 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00001962 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001963
1964 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00001965 ParsedType::getFromOpaquePtr(TypeOrExpr),
1966 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00001967 }
1968
1969 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001970 IdentifierInfo *Name = Tok.getIdentifierInfo();
1971 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00001972 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001973 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001974 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00001975 NextToken().is(tok::period),
1976 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00001977 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00001978 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1979 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00001980
John McCallf312b1e2010-08-26 23:41:50 +00001981 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00001982 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001983 SkipUntil(tok::r_square);
1984 return ExprError();
1985 }
1986
Douglas Gregor1569f952010-04-21 20:38:13 +00001987 ConsumeToken(); // the type name
1988
1989 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00001990 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001991
John McCallf312b1e2010-08-26 23:41:50 +00001992 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00001993 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001994 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001995 }
Chris Lattner699b6612008-01-25 18:59:06 +00001996 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00001997
1998 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00001999 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002000 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002001 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002002 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002003 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002004
John McCallb3d87482010-08-24 05:47:05 +00002005 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2006 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002007}
Sebastian Redl1d922962008-12-13 15:32:12 +00002008
Douglas Gregor2725ca82010-04-21 19:57:20 +00002009/// \brief Parse the remainder of an Objective-C message following the
2010/// '[' objc-receiver.
2011///
2012/// This routine handles sends to super, class messages (sent to a
2013/// class name), and instance messages (sent to an object), and the
2014/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2015/// ReceiverExpr, respectively. Only one of these parameters may have
2016/// a valid value.
2017///
2018/// \param LBracLoc The location of the opening '['.
2019///
2020/// \param SuperLoc If this is a send to 'super', the location of the
2021/// 'super' keyword that indicates a send to the superclass.
2022///
2023/// \param ReceiverType If this is a class message, the type of the
2024/// class we are sending a message to.
2025///
2026/// \param ReceiverExpr If this is an instance message, the expression
2027/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002028///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002029/// objc-message-args:
2030/// objc-selector
2031/// objc-keywordarg-list
2032///
2033/// objc-keywordarg-list:
2034/// objc-keywordarg
2035/// objc-keywordarg-list objc-keywordarg
2036///
Mike Stump1eb44332009-09-09 15:08:12 +00002037/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002038/// selector-name[opt] ':' objc-keywordexpr
2039///
2040/// objc-keywordexpr:
2041/// nonempty-expr-list
2042///
2043/// nonempty-expr-list:
2044/// assignment-expression
2045/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002046///
John McCall60d7b3a2010-08-24 06:29:42 +00002047ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002048Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002049 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002050 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002051 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002052 InMessageExpressionRAIIObject InMessage(*this, true);
2053
Steve Naroffc4df6d22009-11-07 02:08:14 +00002054 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002055 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002056 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2057 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002058 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002059 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2060 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002061 else
John McCall9ae2f072010-08-23 23:25:46 +00002062 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002063 0, 0, false);
Douglas Gregordc845342010-05-25 05:58:43 +00002064 ConsumeCodeCompletionToken();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002065 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002066
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002067 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002068 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002069 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002070
Anders Carlssonff975cf2009-02-14 18:21:46 +00002071 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00002072
Steve Naroff68d331a2007-09-27 14:38:14 +00002073 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002074 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002075
Chris Lattnerdf195262007-10-09 17:51:17 +00002076 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002077 while (1) {
2078 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002079 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00002080
Chris Lattnerdf195262007-10-09 17:51:17 +00002081 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002082 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002083 // We must manually skip to a ']', otherwise the expression skipper will
2084 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2085 // the enclosing expression.
2086 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002087 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002088 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002089
Steve Naroff68d331a2007-09-27 14:38:14 +00002090 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002091 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002092
2093 if (Tok.is(tok::code_completion)) {
2094 if (SuperLoc.isValid())
2095 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2096 KeyIdents.data(),
2097 KeyIdents.size(),
2098 /*AtArgumentEpression=*/true);
2099 else if (ReceiverType)
2100 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2101 KeyIdents.data(),
2102 KeyIdents.size(),
2103 /*AtArgumentEpression=*/true);
2104 else
2105 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2106 KeyIdents.data(),
2107 KeyIdents.size(),
2108 /*AtArgumentEpression=*/true);
2109
2110 ConsumeCodeCompletionToken();
2111 SkipUntil(tok::r_square);
2112 return ExprError();
2113 }
2114
John McCall60d7b3a2010-08-24 06:29:42 +00002115 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002116 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002117 // We must manually skip to a ']', otherwise the expression skipper will
2118 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2119 // the enclosing expression.
2120 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002121 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002122 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002123
Steve Naroff37387c92007-09-17 20:25:27 +00002124 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002125 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002126
Douglas Gregord3c68542009-11-19 01:08:35 +00002127 // Code completion after each argument.
2128 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002129 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002130 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002131 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002132 KeyIdents.size(),
2133 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002134 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002135 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002136 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002137 KeyIdents.size(),
2138 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002139 else
John McCall9ae2f072010-08-23 23:25:46 +00002140 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002141 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002142 KeyIdents.size(),
2143 /*AtArgumentEpression=*/false);
Douglas Gregordc845342010-05-25 05:58:43 +00002144 ConsumeCodeCompletionToken();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002145 SkipUntil(tok::r_square);
2146 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002147 }
2148
Steve Naroff37387c92007-09-17 20:25:27 +00002149 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002150 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002151 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002152 break;
2153 // We have a selector or a colon, continue parsing.
2154 }
2155 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002156 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002157 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002158 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002159 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002160 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002161 // We must manually skip to a ']', otherwise the expression skipper will
2162 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2163 // the enclosing expression.
2164 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002165 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002166 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002167
Steve Naroff49f109c2007-11-15 13:05:42 +00002168 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002169 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002170 }
2171 } else if (!selIdent) {
2172 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002173
Chris Lattner4fef81d2008-08-05 06:19:09 +00002174 // We must manually skip to a ']', otherwise the expression skipper will
2175 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2176 // the enclosing expression.
2177 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002178 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002179 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002180
Chris Lattnerdf195262007-10-09 17:51:17 +00002181 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002182 if (Tok.is(tok::identifier))
2183 Diag(Tok, diag::err_expected_colon);
2184 else
2185 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002186 // We must manually skip to a ']', otherwise the expression skipper will
2187 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2188 // the enclosing expression.
2189 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002190 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002191 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002192
Chris Lattner699b6612008-01-25 18:59:06 +00002193 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002194
Steve Naroff29238a02007-10-05 18:42:47 +00002195 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00002196 if (nKeys == 0)
2197 KeyIdents.push_back(selIdent);
2198 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002199
Douglas Gregor2725ca82010-04-21 19:57:20 +00002200 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002201 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002202 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002203 MultiExprArg(Actions,
2204 KeyExprs.take(),
2205 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002206 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002207 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002208 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002209 MultiExprArg(Actions,
2210 KeyExprs.take(),
2211 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002212 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002213 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002214 MultiExprArg(Actions,
2215 KeyExprs.take(),
2216 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002217}
2218
John McCall60d7b3a2010-08-24 06:29:42 +00002219ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2220 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002221 if (Res.isInvalid()) return move(Res);
2222
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002223 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2224 // expressions. At this point, we know that the only valid thing that starts
2225 // with '@' is an @"".
2226 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002227 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002228 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002229 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002230
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002231 while (Tok.is(tok::at)) {
2232 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002233
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002234 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002235 if (!isTokenStringLiteral())
2236 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002237
John McCall60d7b3a2010-08-24 06:29:42 +00002238 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002239 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002240 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002241
Sebastian Redleffa8d12008-12-10 00:02:53 +00002242 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002243 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002244
2245 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2246 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002247}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002248
2249/// objc-encode-expression:
2250/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002251ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002252Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002253 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002254
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002255 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002256
Chris Lattner4fef81d2008-08-05 06:19:09 +00002257 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002258 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2259
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002260 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002261
Douglas Gregor809070a2009-02-18 17:45:20 +00002262 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002263
Anders Carlsson4988ae32007-08-23 15:31:37 +00002264 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00002265
Douglas Gregor809070a2009-02-18 17:45:20 +00002266 if (Ty.isInvalid())
2267 return ExprError();
2268
Mike Stump1eb44332009-09-09 15:08:12 +00002269 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00002270 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002271}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002272
2273/// objc-protocol-expression
2274/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002275ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002276Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002277 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002278
Chris Lattner4fef81d2008-08-05 06:19:09 +00002279 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002280 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2281
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002282 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002283
Chris Lattner4fef81d2008-08-05 06:19:09 +00002284 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002285 return ExprError(Diag(Tok, diag::err_expected_ident));
2286
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002287 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002288 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002289
Anders Carlsson4988ae32007-08-23 15:31:37 +00002290 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002291
Sebastian Redl1d922962008-12-13 15:32:12 +00002292 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2293 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002294}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002295
2296/// objc-selector-expression
2297/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002298ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002299 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002300
Chris Lattner4fef81d2008-08-05 06:19:09 +00002301 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002302 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2303
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002304 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002305 SourceLocation LParenLoc = ConsumeParen();
2306 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002307
2308 if (Tok.is(tok::code_completion)) {
2309 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2310 KeyIdents.size());
2311 ConsumeCodeCompletionToken();
2312 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2313 return ExprError();
2314 }
2315
Chris Lattner2fc5c242009-04-11 18:13:45 +00002316 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002317 if (!SelIdent && // missing selector name.
2318 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002319 return ExprError(Diag(Tok, diag::err_expected_ident));
2320
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002321 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002322 unsigned nColons = 0;
2323 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002324 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002325 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2326 ++nColons;
2327 KeyIdents.push_back(0);
2328 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002329 return ExprError(Diag(Tok, diag::err_expected_colon));
2330
Chris Lattner5add7542010-08-27 22:32:41 +00002331 ++nColons;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002332 ConsumeToken(); // Eat the ':'.
2333 if (Tok.is(tok::r_paren))
2334 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002335
2336 if (Tok.is(tok::code_completion)) {
2337 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2338 KeyIdents.size());
2339 ConsumeCodeCompletionToken();
2340 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2341 return ExprError();
2342 }
2343
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002344 // Check for another keyword selector.
2345 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002346 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002347 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002348 if (!SelIdent && Tok.isNot(tok::colon))
2349 break;
2350 }
Steve Naroff887407e2007-12-05 22:21:29 +00002351 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002352 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00002353 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002354 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2355 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00002356 }