blob: cd1321ed83248fe2d3a7d17668dc5a979902a18f [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 Gregorbdb2d502010-12-21 17:34:17 +0000429 ParseObjCPropertyAttribute(OCDS, interfaceDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000430
John McCalld0014542009-12-03 22:31:13 +0000431 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
432 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000433
Chris Lattnere82a10f2008-10-20 05:46:22 +0000434 // Parse all the comma separated declarators.
435 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000436 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000438 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
439 tok::at);
Chris Lattnera2449b22008-10-20 05:57:40 +0000440 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000441 }
Steve Naroff294494e2007-08-22 16:35:03 +0000442 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000443
444 // We break out of the big loop in two cases: when we see @end or when we see
445 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000446 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000447 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregordc845342010-05-25 05:58:43 +0000448 ConsumeCodeCompletionToken();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000449 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerbc662af2008-10-20 06:10:06 +0000450 ConsumeToken(); // the "end" identifier
451 else
452 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Chris Lattnera2449b22008-10-20 05:57:40 +0000454 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000455 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000456 Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000457 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000458 allProperties.data(), allProperties.size(),
459 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000460}
461
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000462/// Parse property attribute declarations.
463///
464/// property-attr-decl: '(' property-attrlist ')'
465/// property-attrlist:
466/// property-attribute
467/// property-attrlist ',' property-attribute
468/// property-attribute:
469/// getter '=' identifier
470/// setter '=' identifier ':'
471/// readonly
472/// readwrite
473/// assign
474/// retain
475/// copy
476/// nonatomic
477///
Douglas Gregorbdb2d502010-12-21 17:34:17 +0000478void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, Decl *ClassDecl) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000479 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000480 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000482 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000483 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000484 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Douglas Gregordc845342010-05-25 05:58:43 +0000485 ConsumeCodeCompletionToken();
Steve Naroffece8e712009-10-08 21:55:05 +0000486 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000487 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000489 // If this is not an identifier at all, bail out early.
490 if (II == 0) {
491 MatchRHSPunctuation(tok::r_paren, LHSLoc);
492 return;
493 }
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Chris Lattner156b0612008-10-20 07:37:22 +0000495 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Chris Lattner92e62b02008-11-20 04:42:34 +0000497 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000498 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000499 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000500 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000501 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000502 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000503 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000504 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000505 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000506 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000507 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000508 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000509 else if (II->isStr("atomic"))
510 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000511 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000512 bool IsSetter = II->getNameStart()[0] == 's';
513
Chris Lattnere00da7c2008-10-20 07:39:53 +0000514 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000515 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
516 diag::err_objc_expected_equal_for_getter;
517
518 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000519 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Douglas Gregor4ad96852009-11-19 07:41:15 +0000521 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000522 if (IsSetter)
Douglas Gregorbdb2d502010-12-21 17:34:17 +0000523 Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl);
Douglas Gregor4ad96852009-11-19 07:41:15 +0000524 else
Douglas Gregorbdb2d502010-12-21 17:34:17 +0000525 Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000526 ConsumeCodeCompletionToken();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000527 }
528
Anders Carlsson42499be2010-10-02 17:45:21 +0000529
530 SourceLocation SelLoc;
531 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
532
533 if (!SelIdent) {
534 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
535 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000536 SkipUntil(tok::r_paren);
537 return;
538 }
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Anders Carlsson42499be2010-10-02 17:45:21 +0000540 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000541 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000542 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000544 if (ExpectAndConsume(tok::colon,
545 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000546 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000547 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000548 } else {
549 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000550 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000551 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000552 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000553 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000554 SkipUntil(tok::r_paren);
555 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000556 }
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattner156b0612008-10-20 07:37:22 +0000558 if (Tok.isNot(tok::comma))
559 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Chris Lattner156b0612008-10-20 07:37:22 +0000561 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000562 }
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Chris Lattner156b0612008-10-20 07:37:22 +0000564 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000565}
566
Steve Naroff3536b442007-09-06 21:24:23 +0000567/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000568/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000569/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000570///
571/// objc-instance-method: '-'
572/// objc-class-method: '+'
573///
Steve Naroff4985ace2007-08-22 18:35:33 +0000574/// objc-method-attributes: [OBJC2]
575/// __attribute__((deprecated))
576///
John McCalld226f652010-08-21 09:40:31 +0000577Decl *Parser::ParseObjCMethodPrototype(Decl *IDecl,
578 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000579 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000580
Mike Stump1eb44332009-09-09 15:08:12 +0000581 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000582 SourceLocation mLoc = ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000583 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000584 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000585 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000586 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000587}
588
589/// objc-selector:
590/// identifier
591/// one of
592/// enum struct union if else while do for switch case default
593/// break continue return goto asm sizeof typeof __alignof
594/// unsigned long const short volatile signed restrict _Complex
595/// in out inout bycopy byref oneway int char float double void _Bool
596///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000597IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000598
Chris Lattnerff384912007-10-07 02:00:24 +0000599 switch (Tok.getKind()) {
600 default:
601 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000602 case tok::ampamp:
603 case tok::ampequal:
604 case tok::amp:
605 case tok::pipe:
606 case tok::tilde:
607 case tok::exclaim:
608 case tok::exclaimequal:
609 case tok::pipepipe:
610 case tok::pipeequal:
611 case tok::caret:
612 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000613 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000614 if (isalpha(ThisTok[0])) {
615 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
616 Tok.setKind(tok::identifier);
617 SelectorLoc = ConsumeToken();
618 return II;
619 }
620 return 0;
621 }
622
Chris Lattnerff384912007-10-07 02:00:24 +0000623 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000624 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000625 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000626 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000627 case tok::kw_break:
628 case tok::kw_case:
629 case tok::kw_catch:
630 case tok::kw_char:
631 case tok::kw_class:
632 case tok::kw_const:
633 case tok::kw_const_cast:
634 case tok::kw_continue:
635 case tok::kw_default:
636 case tok::kw_delete:
637 case tok::kw_do:
638 case tok::kw_double:
639 case tok::kw_dynamic_cast:
640 case tok::kw_else:
641 case tok::kw_enum:
642 case tok::kw_explicit:
643 case tok::kw_export:
644 case tok::kw_extern:
645 case tok::kw_false:
646 case tok::kw_float:
647 case tok::kw_for:
648 case tok::kw_friend:
649 case tok::kw_goto:
650 case tok::kw_if:
651 case tok::kw_inline:
652 case tok::kw_int:
653 case tok::kw_long:
654 case tok::kw_mutable:
655 case tok::kw_namespace:
656 case tok::kw_new:
657 case tok::kw_operator:
658 case tok::kw_private:
659 case tok::kw_protected:
660 case tok::kw_public:
661 case tok::kw_register:
662 case tok::kw_reinterpret_cast:
663 case tok::kw_restrict:
664 case tok::kw_return:
665 case tok::kw_short:
666 case tok::kw_signed:
667 case tok::kw_sizeof:
668 case tok::kw_static:
669 case tok::kw_static_cast:
670 case tok::kw_struct:
671 case tok::kw_switch:
672 case tok::kw_template:
673 case tok::kw_this:
674 case tok::kw_throw:
675 case tok::kw_true:
676 case tok::kw_try:
677 case tok::kw_typedef:
678 case tok::kw_typeid:
679 case tok::kw_typename:
680 case tok::kw_typeof:
681 case tok::kw_union:
682 case tok::kw_unsigned:
683 case tok::kw_using:
684 case tok::kw_virtual:
685 case tok::kw_void:
686 case tok::kw_volatile:
687 case tok::kw_wchar_t:
688 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000689 case tok::kw__Bool:
690 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000691 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000692 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000693 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000694 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000695 }
Steve Naroff294494e2007-08-22 16:35:03 +0000696}
697
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000698/// objc-for-collection-in: 'in'
699///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000700bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000701 // FIXME: May have to do additional look-ahead to only allow for
702 // valid tokens following an 'in'; such as an identifier, unary operators,
703 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000704 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000705 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000706}
707
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000708/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000709/// qualifier list and builds their bitmask representation in the input
710/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000711///
712/// objc-type-qualifiers:
713/// objc-type-qualifier
714/// objc-type-qualifiers objc-type-qualifier
715///
Douglas Gregord32b0222010-08-24 01:06:58 +0000716void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS, bool IsParameter) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000717 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000718 if (Tok.is(tok::code_completion)) {
719 Actions.CodeCompleteObjCPassingType(getCurScope(), DS);
720 ConsumeCodeCompletionToken();
721 }
722
Chris Lattnercb53b362007-12-27 19:57:00 +0000723 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000724 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Chris Lattnere8b724d2007-12-12 06:56:32 +0000726 const IdentifierInfo *II = Tok.getIdentifierInfo();
727 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000728 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000729 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000731 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000732 switch (i) {
733 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000734 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
735 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
736 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
737 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
738 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
739 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000740 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000741 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000742 ConsumeToken();
743 II = 0;
744 break;
745 }
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Chris Lattnere8b724d2007-12-12 06:56:32 +0000747 // If this wasn't a recognized qualifier, bail out.
748 if (II) return;
749 }
750}
751
752/// objc-type-name:
753/// '(' objc-type-qualifiers[opt] type-name ')'
754/// '(' objc-type-qualifiers[opt] ')'
755///
John McCallb3d87482010-08-24 05:47:05 +0000756ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS, bool IsParameter) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000757 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Chris Lattner4a76b292008-10-22 03:52:06 +0000759 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000760 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000762 // Parse type qualifiers, in, inout, etc.
Douglas Gregord32b0222010-08-24 01:06:58 +0000763 ParseObjCTypeQualifierList(DS, IsParameter);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000764
John McCallb3d87482010-08-24 05:47:05 +0000765 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000766 if (isTypeSpecifierQualifier()) {
767 TypeResult TypeSpec = ParseTypeName();
768 if (!TypeSpec.isInvalid())
769 Ty = TypeSpec.get();
770 }
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Steve Naroffd7333c22008-10-21 14:15:04 +0000772 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000773 ConsumeParen();
774 else if (Tok.getLocation() == TypeStartLoc) {
775 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000776 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000777 SkipUntil(tok::r_paren);
778 } else {
779 // Otherwise, we found *something*, but didn't get a ')' in the right
780 // place. Emit an error then return what we have as the type.
781 MatchRHSPunctuation(tok::r_paren, LParenLoc);
782 }
Steve Narofff28b2642007-09-05 23:30:30 +0000783 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000784}
785
786/// objc-method-decl:
787/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000788/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000789/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000790/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000791///
792/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000793/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000794/// objc-keyword-selector objc-keyword-decl
795///
796/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000797/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
798/// objc-selector ':' objc-keyword-attributes[opt] identifier
799/// ':' objc-type-name objc-keyword-attributes[opt] identifier
800/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000801///
Steve Naroff4985ace2007-08-22 18:35:33 +0000802/// objc-parmlist:
803/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000804///
Steve Naroff4985ace2007-08-22 18:35:33 +0000805/// objc-parms:
806/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000807///
Steve Naroff4985ace2007-08-22 18:35:33 +0000808/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000809/// , ...
810///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000811/// objc-keyword-attributes: [OBJC2]
812/// __attribute__((unused))
813///
John McCalld226f652010-08-21 09:40:31 +0000814Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000815 tok::TokenKind mType,
816 Decl *IDecl,
817 tok::ObjCKeywordKind MethodImplKind) {
John McCall54abf7d2009-11-04 02:18:39 +0000818 ParsingDeclRAIIObject PD(*this);
819
Douglas Gregore8f5a172010-04-07 00:21:17 +0000820 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000821 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
John McCallb3d87482010-08-24 05:47:05 +0000822 /*ReturnType=*/ ParsedType(), IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000823 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000824 }
825
Chris Lattnere8904e92008-08-23 01:48:03 +0000826 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000827 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000828 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000829 if (Tok.is(tok::l_paren))
Douglas Gregord32b0222010-08-24 01:06:58 +0000830 ReturnType = ParseObjCTypeName(DSRet, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Ted Kremenek9e049352010-02-18 23:05:16 +0000832 // If attributes exist before the method, parse them.
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000833 AttributeList *MethodAttrs = 0;
Ted Kremenek9e049352010-02-18 23:05:16 +0000834 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000835 MethodAttrs = ParseGNUAttributes();
Ted Kremenek9e049352010-02-18 23:05:16 +0000836
Douglas Gregore8f5a172010-04-07 00:21:17 +0000837 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000838 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Douglas Gregore8f5a172010-04-07 00:21:17 +0000839 ReturnType, IDecl);
Douglas Gregordc845342010-05-25 05:58:43 +0000840 ConsumeCodeCompletionToken();
Douglas Gregore8f5a172010-04-07 00:21:17 +0000841 }
842
Ted Kremenek9e049352010-02-18 23:05:16 +0000843 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000844 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000845 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000846
Steve Naroff84c43102009-02-11 20:43:13 +0000847 // An unnamed colon is valid.
848 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000849 Diag(Tok, diag::err_expected_selector_for_method)
850 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000851 // Skip until we get a ; or {}.
852 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000853 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000854 }
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000856 llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +0000857 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000858 // If attributes exist after the method, parse them.
Mike Stump1eb44332009-09-09 15:08:12 +0000859 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000860 MethodAttrs = addAttributeLists(MethodAttrs, ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Chris Lattnerff384912007-10-07 02:00:24 +0000862 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +0000863 Decl *Result
John McCall54abf7d2009-11-04 02:18:39 +0000864 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000865 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000866 0,
867 CParamInfo.data(), CParamInfo.size(),
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000868 MethodAttrs, MethodImplKind);
John McCall54abf7d2009-11-04 02:18:39 +0000869 PD.complete(Result);
870 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000871 }
Steve Narofff28b2642007-09-05 23:30:30 +0000872
Steve Naroff68d331a2007-09-27 14:38:14 +0000873 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
John McCallf312b1e2010-08-26 23:41:50 +0000874 llvm::SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Chris Lattnerff384912007-10-07 02:00:24 +0000876 while (1) {
John McCallf312b1e2010-08-26 23:41:50 +0000877 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Chris Lattnerff384912007-10-07 02:00:24 +0000879 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000880 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000881 Diag(Tok, diag::err_expected_colon);
882 break;
883 }
884 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000885
John McCallb3d87482010-08-24 05:47:05 +0000886 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +0000887 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregord32b0222010-08-24 01:06:58 +0000888 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, true);
Chris Lattnere294d3f2009-04-11 18:57:04 +0000889
Chris Lattnerff384912007-10-07 02:00:24 +0000890 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000891 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000892 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000893 ArgInfo.ArgAttrs = ParseGNUAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000894
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000895 // Code completion for the next piece of the selector.
896 if (Tok.is(tok::code_completion)) {
897 ConsumeCodeCompletionToken();
898 KeyIdents.push_back(SelIdent);
899 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
900 mType == tok::minus,
901 /*AtParameterName=*/true,
902 ReturnType,
903 KeyIdents.data(),
904 KeyIdents.size());
905 KeyIdents.pop_back();
906 break;
907 }
908
Chris Lattnerdf195262007-10-09 17:51:17 +0000909 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000910 Diag(Tok, diag::err_expected_ident); // missing argument name.
911 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000912 }
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Chris Lattnere294d3f2009-04-11 18:57:04 +0000914 ArgInfo.Name = Tok.getIdentifierInfo();
915 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000916 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Chris Lattnere294d3f2009-04-11 18:57:04 +0000918 ArgInfos.push_back(ArgInfo);
919 KeyIdents.push_back(SelIdent);
920
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000921 // Code completion for the next piece of the selector.
922 if (Tok.is(tok::code_completion)) {
923 ConsumeCodeCompletionToken();
924 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
925 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +0000926 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000927 ReturnType,
928 KeyIdents.data(),
929 KeyIdents.size());
930 break;
931 }
932
Chris Lattnerff384912007-10-07 02:00:24 +0000933 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000934 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000935 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000936 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000937 break;
938 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000939 }
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Steve Naroff335eafa2007-11-15 12:35:21 +0000941 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Chris Lattnerff384912007-10-07 02:00:24 +0000943 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000944 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000945 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000946 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000947 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000948 ConsumeToken();
949 break;
950 }
Chris Lattnerff384912007-10-07 02:00:24 +0000951 DeclSpec DS;
952 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000953 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000954 Declarator ParmDecl(DS, Declarator::PrototypeContext);
955 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000956 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +0000957 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000958 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
959 ParmDecl.getIdentifierLoc(),
960 Param,
961 0));
962
Chris Lattnerff384912007-10-07 02:00:24 +0000963 }
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +0000965 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000966 // If attributes exist after the method, parse them.
Mike Stump1eb44332009-09-09 15:08:12 +0000967 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000968 MethodAttrs = addAttributeLists(MethodAttrs, ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000970 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +0000971 return 0;
Chris Lattnerff384912007-10-07 02:00:24 +0000972 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
973 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +0000974 Decl *Result
John McCall54abf7d2009-11-04 02:18:39 +0000975 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000976 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +0000977 &ArgInfos[0],
978 CParamInfo.data(), CParamInfo.size(),
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000979 MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000980 MethodImplKind, isVariadic);
John McCall54abf7d2009-11-04 02:18:39 +0000981 PD.complete(Result);
982 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +0000983}
984
Steve Naroffdac269b2007-08-20 21:31:48 +0000985/// objc-protocol-refs:
986/// '<' identifier-list '>'
987///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000988bool Parser::
John McCalld226f652010-08-21 09:40:31 +0000989ParseObjCProtocolReferences(llvm::SmallVectorImpl<Decl *> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000990 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
991 bool WarnOnDeclarations,
992 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000993 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000995 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Chris Lattnere13b9592008-07-26 04:03:38 +0000997 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Chris Lattnere13b9592008-07-26 04:03:38 +0000999 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001000 if (Tok.is(tok::code_completion)) {
1001 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1002 ProtocolIdents.size());
Douglas Gregordc845342010-05-25 05:58:43 +00001003 ConsumeCodeCompletionToken();
Douglas Gregor55385fe2009-11-18 04:19:12 +00001004 }
1005
Chris Lattnere13b9592008-07-26 04:03:38 +00001006 if (Tok.isNot(tok::identifier)) {
1007 Diag(Tok, diag::err_expected_ident);
1008 SkipUntil(tok::greater);
1009 return true;
1010 }
1011 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1012 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001013 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001014 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Chris Lattnere13b9592008-07-26 04:03:38 +00001016 if (Tok.isNot(tok::comma))
1017 break;
1018 ConsumeToken();
1019 }
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Chris Lattnere13b9592008-07-26 04:03:38 +00001021 // Consume the '>'.
1022 if (Tok.isNot(tok::greater)) {
1023 Diag(Tok, diag::err_expected_greater);
1024 return true;
1025 }
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Chris Lattnere13b9592008-07-26 04:03:38 +00001027 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Chris Lattnere13b9592008-07-26 04:03:38 +00001029 // Convert the list of protocols identifiers into a list of protocol decls.
1030 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1031 &ProtocolIdents[0], ProtocolIdents.size(),
1032 Protocols);
1033 return false;
1034}
1035
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001036/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1037/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001038bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001039 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1040 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1041 SourceLocation LAngleLoc, EndProtoLoc;
1042 llvm::SmallVector<Decl *, 8> ProtocolDecl;
1043 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001044 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1045 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001046 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1047 ProtocolLocs.data(), LAngleLoc);
1048 if (EndProtoLoc.isValid())
1049 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001050 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001051}
1052
1053
Steve Naroffdac269b2007-08-20 21:31:48 +00001054/// objc-class-instance-variables:
1055/// '{' objc-instance-variable-decl-list[opt] '}'
1056///
1057/// objc-instance-variable-decl-list:
1058/// objc-visibility-spec
1059/// objc-instance-variable-decl ';'
1060/// ';'
1061/// objc-instance-variable-decl-list objc-visibility-spec
1062/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1063/// objc-instance-variable-decl-list ';'
1064///
1065/// objc-visibility-spec:
1066/// @private
1067/// @protected
1068/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001069/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001070///
1071/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001072/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001073///
John McCalld226f652010-08-21 09:40:31 +00001074void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001075 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001076 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001077 assert(Tok.is(tok::l_brace) && "expected {");
John McCalld226f652010-08-21 09:40:31 +00001078 llvm::SmallVector<Decl *, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001079
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001080 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001081
Steve Naroffddbff782007-08-21 21:17:12 +00001082 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Steve Naroffddbff782007-08-21 21:17:12 +00001084 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001085 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001086 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Steve Naroffddbff782007-08-21 21:17:12 +00001088 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001089 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001090 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001091 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001092 ConsumeToken();
1093 continue;
1094 }
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Steve Naroffddbff782007-08-21 21:17:12 +00001096 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001097 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001098 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001099
1100 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001101 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001102 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001103 }
1104
Steve Naroff861cf3e2007-08-23 18:16:40 +00001105 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001106 case tok::objc_private:
1107 case tok::objc_public:
1108 case tok::objc_protected:
1109 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001110 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001111 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001112 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001113 default:
1114 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001115 continue;
1116 }
1117 }
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001119 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001120 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001121 Sema::PCC_ObjCInstanceVariableList);
Douglas Gregordc845342010-05-25 05:58:43 +00001122 ConsumeCodeCompletionToken();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001123 }
1124
John McCallbdd563e2009-11-03 02:38:08 +00001125 struct ObjCIvarCallback : FieldCallback {
1126 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001127 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001128 tok::ObjCKeywordKind visibility;
John McCalld226f652010-08-21 09:40:31 +00001129 llvm::SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001130
John McCalld226f652010-08-21 09:40:31 +00001131 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
1132 llvm::SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001133 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1134 }
1135
John McCalld226f652010-08-21 09:40:31 +00001136 Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001137 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001138 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001139 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001140 FD.D.getDeclSpec().getSourceRange().getBegin(),
1141 IDecl, FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001142 if (Field)
1143 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001144 return Field;
1145 }
1146 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001147
Chris Lattnere1359422008-04-10 06:46:29 +00001148 // Parse all the comma separated declarators.
1149 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +00001150 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Chris Lattnerdf195262007-10-09 17:51:17 +00001152 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001153 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001154 } else {
1155 Diag(Tok, diag::err_expected_semi_decl_list);
1156 // Skip to end of block or statement
1157 SkipUntil(tok::r_brace, true, true);
1158 }
1159 }
Steve Naroff60fccee2007-10-29 21:38:07 +00001160 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001161 Actions.ActOnLastBitfield(RBraceLoc, interfaceDecl, AllIvarDecls);
Steve Naroff8749be52007-10-31 22:11:35 +00001162 // Call ActOnFields() even if we don't have any decls. This is useful
1163 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001164 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001165 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001166 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001167 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001168}
Steve Naroffdac269b2007-08-20 21:31:48 +00001169
1170/// objc-protocol-declaration:
1171/// objc-protocol-definition
1172/// objc-protocol-forward-reference
1173///
1174/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001175/// @protocol identifier
1176/// objc-protocol-refs[opt]
1177/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001178/// @end
1179///
1180/// objc-protocol-forward-reference:
1181/// @protocol identifier-list ';'
1182///
1183/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001184/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001185/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCalld226f652010-08-21 09:40:31 +00001186Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001187 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001188 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001189 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1190 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Douglas Gregor083128f2009-11-18 04:49:41 +00001192 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001193 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001194 ConsumeCodeCompletionToken();
Douglas Gregor083128f2009-11-18 04:49:41 +00001195 }
1196
Chris Lattnerdf195262007-10-09 17:51:17 +00001197 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001198 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCalld226f652010-08-21 09:40:31 +00001199 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001200 }
1201 // Save the protocol name, then consume it.
1202 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1203 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Chris Lattnerdf195262007-10-09 17:51:17 +00001205 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001206 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001207 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001208 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001209 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001210 }
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Chris Lattnerdf195262007-10-09 17:51:17 +00001212 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001213 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1214 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1215
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001216 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001217 while (1) {
1218 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001219 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001220 Diag(Tok, diag::err_expected_ident);
1221 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001222 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001223 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001224 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1225 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001226 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Chris Lattnerdf195262007-10-09 17:51:17 +00001228 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001229 break;
1230 }
1231 // Consume the ';'.
1232 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCalld226f652010-08-21 09:40:31 +00001233 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Steve Naroffe440eb82007-10-10 17:32:04 +00001235 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001236 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001237 ProtocolRefs.size(),
1238 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001239 }
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001241 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001242 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001243
John McCalld226f652010-08-21 09:40:31 +00001244 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001245 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001246 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001247 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1248 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +00001249 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001250
John McCalld226f652010-08-21 09:40:31 +00001251 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001252 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001253 ProtocolRefs.data(),
1254 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001255 ProtocolLocs.data(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001256 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001257 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001258 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001259}
Steve Naroffdac269b2007-08-20 21:31:48 +00001260
1261/// objc-implementation:
1262/// objc-class-implementation-prologue
1263/// objc-category-implementation-prologue
1264///
1265/// objc-class-implementation-prologue:
1266/// @implementation identifier objc-superclass[opt]
1267/// objc-class-instance-variables[opt]
1268///
1269/// objc-category-implementation-prologue:
1270/// @implementation identifier ( identifier )
John McCalld226f652010-08-21 09:40:31 +00001271Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001272 SourceLocation atLoc) {
1273 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1274 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1275 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001277 // Code completion after '@implementation'.
1278 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001279 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001280 ConsumeCodeCompletionToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001281 }
1282
Chris Lattnerdf195262007-10-09 17:51:17 +00001283 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001284 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001285 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001286 }
1287 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001288 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001289 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001290
1291 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001292 // we have a category implementation.
1293 SourceLocation lparenLoc = ConsumeParen();
1294 SourceLocation categoryLoc, rparenLoc;
1295 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001297 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001298 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Douglas Gregordc845342010-05-25 05:58:43 +00001299 ConsumeCodeCompletionToken();
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001300 }
1301
Chris Lattnerdf195262007-10-09 17:51:17 +00001302 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001303 categoryId = Tok.getIdentifierInfo();
1304 categoryLoc = ConsumeToken();
1305 } else {
1306 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001307 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001308 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001309 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001310 Diag(Tok, diag::err_expected_rparen);
1311 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001312 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001313 }
1314 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001315 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001316 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001317 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001318 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001319 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001320 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001321 }
1322 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001323 SourceLocation superClassLoc;
1324 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001325 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001326 // We have a super class
1327 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001328 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001329 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001330 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001331 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001332 superClassId = Tok.getIdentifierInfo();
1333 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001334 }
John McCalld226f652010-08-21 09:40:31 +00001335 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001336 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001337 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001338
Steve Naroff60fccee2007-10-29 21:38:07 +00001339 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001340 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahanian01f1bfc2010-03-22 19:04:14 +00001341 tok::objc_private, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001342 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001343 PendingObjCImpDecl.push_back(ObjCImpDecl);
1344
John McCalld226f652010-08-21 09:40:31 +00001345 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001346}
Steve Naroff60fccee2007-10-29 21:38:07 +00001347
John McCalld226f652010-08-21 09:40:31 +00001348Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001349 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1350 "ParseObjCAtEndDeclaration(): Expected @end");
John McCalld226f652010-08-21 09:40:31 +00001351 Decl *Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001352 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001353 if (ObjCImpDecl) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001354 Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001355 ObjCImpDecl = 0;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001356 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001357 }
Ted Kremenek782f2f52010-01-07 01:20:12 +00001358 else {
1359 // missing @implementation
1360 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1361 }
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001362 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001363}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001364
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001365Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1366 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001367 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001368 return Actions.ConvertDeclToDeclGroup(0);
1369 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001370 Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001371 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1372}
1373
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001374/// compatibility-alias-decl:
1375/// @compatibility_alias alias-name class-name ';'
1376///
John McCalld226f652010-08-21 09:40:31 +00001377Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001378 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1379 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1380 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001381 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001382 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001383 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001384 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001385 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1386 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001387 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001388 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001389 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001390 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001391 IdentifierInfo *classId = Tok.getIdentifierInfo();
1392 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1393 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001394 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
John McCalld226f652010-08-21 09:40:31 +00001395 return 0;
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001396 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001397 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1398 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001399}
1400
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001401/// property-synthesis:
1402/// @synthesize property-ivar-list ';'
1403///
1404/// property-ivar-list:
1405/// property-ivar
1406/// property-ivar-list ',' property-ivar
1407///
1408/// property-ivar:
1409/// identifier
1410/// identifier '=' identifier
1411///
John McCalld226f652010-08-21 09:40:31 +00001412Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001413 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1414 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001415 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Douglas Gregorb328c422009-11-18 19:45:45 +00001417 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001418 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001419 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001420 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001421 }
1422
Douglas Gregorb328c422009-11-18 19:45:45 +00001423 if (Tok.isNot(tok::identifier)) {
1424 Diag(Tok, diag::err_synthesized_property_name);
1425 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001426 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001427 }
1428
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001429 IdentifierInfo *propertyIvar = 0;
1430 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1431 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001432 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001433 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001434 // property '=' ivar-name
1435 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001436
1437 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001438 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId,
Douglas Gregor322328b2009-11-18 22:32:06 +00001439 ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001440 ConsumeCodeCompletionToken();
Douglas Gregor322328b2009-11-18 22:32:06 +00001441 }
1442
Chris Lattnerdf195262007-10-09 17:51:17 +00001443 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001444 Diag(Tok, diag::err_expected_ident);
1445 break;
1446 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001447 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001448 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001449 }
Douglas Gregor23c94db2010-07-02 17:43:08 +00001450 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001451 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001452 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001453 break;
1454 ConsumeToken(); // consume ','
1455 }
Douglas Gregorb328c422009-11-18 19:45:45 +00001456 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001457 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregorb328c422009-11-18 19:45:45 +00001458 SkipUntil(tok::semi);
1459 }
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001460 else
1461 ConsumeToken(); // consume ';'
John McCalld226f652010-08-21 09:40:31 +00001462 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001463}
1464
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001465/// property-dynamic:
1466/// @dynamic property-list
1467///
1468/// property-list:
1469/// identifier
1470/// property-list ',' identifier
1471///
John McCalld226f652010-08-21 09:40:31 +00001472Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001473 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1474 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1475 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001476 while (true) {
1477 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001478 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregordc845342010-05-25 05:58:43 +00001479 ConsumeCodeCompletionToken();
Douglas Gregor424b2a52009-11-18 22:56:13 +00001480 }
1481
1482 if (Tok.isNot(tok::identifier)) {
1483 Diag(Tok, diag::err_expected_ident);
1484 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001485 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001486 }
1487
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001488 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1489 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregor23c94db2010-07-02 17:43:08 +00001490 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001491 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001492
Chris Lattnerdf195262007-10-09 17:51:17 +00001493 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001494 break;
1495 ConsumeToken(); // consume ','
1496 }
Fariborz Jahanian94b24db2010-04-14 20:52:42 +00001497 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001498 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanian94b24db2010-04-14 20:52:42 +00001499 SkipUntil(tok::semi);
1500 }
1501 else
1502 ConsumeToken(); // consume ';'
John McCalld226f652010-08-21 09:40:31 +00001503 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001504}
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001506/// objc-throw-statement:
1507/// throw expression[opt];
1508///
John McCall60d7b3a2010-08-24 06:29:42 +00001509StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1510 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001511 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001512 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001513 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001514 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001515 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001516 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001517 }
1518 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001519 // consume ';'
1520 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001521 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001522}
1523
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001524/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001525/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001526///
John McCall60d7b3a2010-08-24 06:29:42 +00001527StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001528Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001529 ConsumeToken(); // consume synchronized
1530 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001531 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001532 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001533 }
1534 ConsumeParen(); // '('
John McCall60d7b3a2010-08-24 06:29:42 +00001535 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001536 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001537 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001538 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001539 }
1540 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001541 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001542 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001543 }
1544 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001545 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001546 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001547 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001548 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001549 // Enter a scope to hold everything within the compound stmt. Compound
1550 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001551 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001552
John McCall60d7b3a2010-08-24 06:29:42 +00001553 StmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001554
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001555 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001556 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001557 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
John McCall9ae2f072010-08-23 23:25:46 +00001558 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.take(), SynchBody.take());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001559}
1560
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001561/// objc-try-catch-statement:
1562/// @try compound-statement objc-catch-list[opt]
1563/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1564///
1565/// objc-catch-list:
1566/// @catch ( parameter-declaration ) compound-statement
1567/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1568/// catch-parameter-declaration:
1569/// parameter-declaration
1570/// '...' [OBJC2]
1571///
John McCall60d7b3a2010-08-24 06:29:42 +00001572StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001573 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001574
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001575 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001576 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001577 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001578 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001579 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001580 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001581 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001582 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001583 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001584 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001585 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001586 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001587
Chris Lattnerdf195262007-10-09 17:51:17 +00001588 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001589 // At this point, we need to lookahead to determine if this @ is the start
1590 // of an @catch or @finally. We don't want to consume the @ token if this
1591 // is an @try or @encode or something else.
1592 Token AfterAt = GetLookAheadToken(1);
1593 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1594 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1595 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001596
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001597 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001598 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001599 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001600 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001601 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001602 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001603 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001604 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001605 DeclSpec DS;
1606 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001607 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001608 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001609 // we must accept either 'declarator' or 'abstract-declarator' here.
1610 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1611 ParseDeclarator(ParmDecl);
1612
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001613 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001614 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001615 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001616 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001617 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Steve Naroff93a25952009-04-07 22:56:58 +00001619 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Steve Naroff93a25952009-04-07 22:56:58 +00001621 if (Tok.is(tok::r_paren))
1622 RParenLoc = ConsumeParen();
1623 else // Skip over garbage, until we get to ')'. Eat the ')'.
1624 SkipUntil(tok::r_paren, true, false);
1625
John McCall60d7b3a2010-08-24 06:29:42 +00001626 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001627 if (Tok.is(tok::l_brace))
1628 CatchBody = ParseCompoundStatementBody();
1629 else
1630 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001631 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001632 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001633
John McCall60d7b3a2010-08-24 06:29:42 +00001634 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001635 RParenLoc,
1636 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001637 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001638 if (!Catch.isInvalid())
1639 CatchStmts.push_back(Catch.release());
1640
Steve Naroff64515f32008-02-05 21:27:35 +00001641 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001642 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1643 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001644 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001645 }
1646 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001647 } else {
1648 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001649 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001650 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001651
John McCall60d7b3a2010-08-24 06:29:42 +00001652 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001653 if (Tok.is(tok::l_brace))
1654 FinallyBody = ParseCompoundStatementBody();
1655 else
1656 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001657 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001658 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001659 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001660 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001661 catch_or_finally_seen = true;
1662 break;
1663 }
1664 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001665 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001666 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001667 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001668 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001669
John McCall9ae2f072010-08-23 23:25:46 +00001670 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001671 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001672 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001673}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001674
Steve Naroff3536b442007-09-06 21:24:23 +00001675/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001676///
John McCalld226f652010-08-21 09:40:31 +00001677Decl *Parser::ParseObjCMethodDefinition() {
1678 Decl *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001679
John McCallf312b1e2010-08-26 23:41:50 +00001680 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1681 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001682
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001683 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001684 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001685 if (ObjCImpDecl) {
1686 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001687 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001688 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001689 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001690 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001691
Steve Naroff409be832007-11-11 19:54:21 +00001692 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001693 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001694 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001695
Steve Naroff409be832007-11-11 19:54:21 +00001696 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1697 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Steve Naroff409be832007-11-11 19:54:21 +00001699 // If we didn't find the '{', bail out.
1700 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001701 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001702 }
Steve Naroff409be832007-11-11 19:54:21 +00001703 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Steve Naroff409be832007-11-11 19:54:21 +00001705 // Enter a scope for the method body.
Chris Lattner15faee12010-04-12 05:38:43 +00001706 ParseScope BodyScope(this,
1707 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Steve Naroff409be832007-11-11 19:54:21 +00001709 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001710 // specified Declarator for the method.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001711 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001712
John McCall60d7b3a2010-08-24 06:29:42 +00001713 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001714
Steve Naroff409be832007-11-11 19:54:21 +00001715 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001716 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001717 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1718 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001719
Steve Naroff32ce8372009-03-02 22:00:56 +00001720 // TODO: Pass argument information.
John McCall9ae2f072010-08-23 23:25:46 +00001721 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001722
Steve Naroff409be832007-11-11 19:54:21 +00001723 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001724 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001725
Steve Naroff71c0a952007-11-13 23:01:27 +00001726 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001727}
Anders Carlsson55085182007-08-21 17:43:55 +00001728
John McCall60d7b3a2010-08-24 06:29:42 +00001729StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001730 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001731 Actions.CodeCompleteObjCAtStatement(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001732 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001733 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001734 }
1735
1736 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001737 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001738
1739 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001740 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001741
1742 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001743 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001744
John McCall60d7b3a2010-08-24 06:29:42 +00001745 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001746 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001747 // If the expression is invalid, skip ahead to the next semicolon. Not
1748 // doing this opens us up to the possibility of infinite loops if
1749 // ParseExpression does not consume any tokens.
1750 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001751 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001752 }
Chris Lattner5d803162009-12-07 16:33:19 +00001753
Steve Naroff64515f32008-02-05 21:27:35 +00001754 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001755 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001756 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001757}
1758
John McCall60d7b3a2010-08-24 06:29:42 +00001759ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001760 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001761 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001762 Actions.CodeCompleteObjCAtExpression(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +00001763 ConsumeCodeCompletionToken();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001764 return ExprError();
1765
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001766 case tok::string_literal: // primary-expression: string-literal
1767 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001768 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001769 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001770 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001771 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001772
Chris Lattner4fef81d2008-08-05 06:19:09 +00001773 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1774 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001775 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001776 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001777 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001778 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001779 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001780 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001781 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001782 }
Anders Carlsson55085182007-08-21 17:43:55 +00001783 }
Anders Carlsson55085182007-08-21 17:43:55 +00001784}
1785
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001786/// \brirg Parse the receiver of an Objective-C++ message send.
1787///
1788/// This routine parses the receiver of a message send in
1789/// Objective-C++ either as a type or as an expression. Note that this
1790/// routine must not be called to parse a send to 'super', since it
1791/// has no way to return such a result.
1792///
1793/// \param IsExpr Whether the receiver was parsed as an expression.
1794///
1795/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1796/// IsExpr is true), the parsed expression. If the receiver was parsed
1797/// as a type (\c IsExpr is false), the parsed type.
1798///
1799/// \returns True if an error occurred during parsing or semantic
1800/// analysis, in which case the arguments do not have valid
1801/// values. Otherwise, returns false for a successful parse.
1802///
1803/// objc-receiver: [C++]
1804/// 'super' [not parsed here]
1805/// expression
1806/// simple-type-specifier
1807/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001808bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001809 InMessageExpressionRAIIObject InMessage(*this, true);
1810
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001811 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1812 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1813 TryAnnotateTypeOrScopeToken();
1814
1815 if (!isCXXSimpleTypeSpecifier()) {
1816 // objc-receiver:
1817 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00001818 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001819 if (Receiver.isInvalid())
1820 return true;
1821
1822 IsExpr = true;
1823 TypeOrExpr = Receiver.take();
1824 return false;
1825 }
1826
1827 // objc-receiver:
1828 // typename-specifier
1829 // simple-type-specifier
1830 // expression (that starts with one of the above)
1831 DeclSpec DS;
1832 ParseCXXSimpleTypeSpecifier(DS);
1833
1834 if (Tok.is(tok::l_paren)) {
1835 // If we see an opening parentheses at this point, we are
1836 // actually parsing an expression that starts with a
1837 // function-style cast, e.g.,
1838 //
1839 // postfix-expression:
1840 // simple-type-specifier ( expression-list [opt] )
1841 // typename-specifier ( expression-list [opt] )
1842 //
1843 // Parse the remainder of this case, then the (optional)
1844 // postfix-expression suffix, followed by the (optional)
1845 // right-hand side of the binary expression. We have an
1846 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00001847 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001848 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001849 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001850 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001851 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001852 if (Receiver.isInvalid())
1853 return true;
1854
1855 IsExpr = true;
1856 TypeOrExpr = Receiver.take();
1857 return false;
1858 }
1859
1860 // We have a class message. Turn the simple-type-specifier or
1861 // typename-specifier we parsed into a type and parse the
1862 // remainder of the class message.
1863 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001864 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001865 if (Type.isInvalid())
1866 return true;
1867
1868 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00001869 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001870 return false;
1871}
1872
Douglas Gregor1b730e82010-05-31 14:40:22 +00001873/// \brief Determine whether the parser is currently referring to a an
1874/// Objective-C message send, using a simplified heuristic to avoid overhead.
1875///
1876/// This routine will only return true for a subset of valid message-send
1877/// expressions.
1878bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00001879 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00001880 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00001881 return GetLookAheadToken(1).is(tok::identifier) &&
1882 GetLookAheadToken(2).is(tok::identifier);
1883}
1884
Douglas Gregor9497a732010-09-16 01:51:54 +00001885bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
1886 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
1887 InMessageExpression)
1888 return false;
1889
1890
1891 ParsedType Type;
1892
1893 if (Tok.is(tok::annot_typename))
1894 Type = getTypeAnnotation(Tok);
1895 else if (Tok.is(tok::identifier))
1896 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
1897 getCurScope());
1898 else
1899 return false;
1900
1901 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
1902 const Token &AfterNext = GetLookAheadToken(2);
1903 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
1904 if (Tok.is(tok::identifier))
1905 TryAnnotateTypeOrScopeToken();
1906
1907 return Tok.is(tok::annot_typename);
1908 }
1909 }
1910
1911 return false;
1912}
1913
Mike Stump1eb44332009-09-09 15:08:12 +00001914/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001915/// '[' objc-receiver objc-message-args ']'
1916///
Douglas Gregor2725ca82010-04-21 19:57:20 +00001917/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00001918/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001919/// expression
1920/// class-name
1921/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001922///
John McCall60d7b3a2010-08-24 06:29:42 +00001923ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001924 assert(Tok.is(tok::l_square) && "'[' expected");
1925 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1926
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001927 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001928 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001929 ConsumeCodeCompletionToken();
1930 SkipUntil(tok::r_square);
1931 return ExprError();
1932 }
1933
Douglas Gregor0fbda682010-09-15 14:51:05 +00001934 InMessageExpressionRAIIObject InMessage(*this, true);
1935
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001936 if (getLang().CPlusPlus) {
1937 // We completely separate the C and C++ cases because C++ requires
1938 // more complicated (read: slower) parsing.
1939
1940 // Handle send to super.
1941 // FIXME: This doesn't benefit from the same typo-correction we
1942 // get in Objective-C.
1943 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001944 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00001945 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1946 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001947
1948 // Parse the receiver, which is either a type or an expression.
1949 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00001950 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001951 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
1952 SkipUntil(tok::r_square);
1953 return ExprError();
1954 }
1955
1956 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00001957 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1958 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00001959 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00001960
1961 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00001962 ParsedType::getFromOpaquePtr(TypeOrExpr),
1963 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00001964 }
1965
1966 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001967 IdentifierInfo *Name = Tok.getIdentifierInfo();
1968 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00001969 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001970 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001971 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00001972 NextToken().is(tok::period),
1973 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00001974 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00001975 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1976 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00001977
John McCallf312b1e2010-08-26 23:41:50 +00001978 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00001979 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00001980 SkipUntil(tok::r_square);
1981 return ExprError();
1982 }
1983
Douglas Gregor1569f952010-04-21 20:38:13 +00001984 ConsumeToken(); // the type name
1985
1986 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00001987 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001988
John McCallf312b1e2010-08-26 23:41:50 +00001989 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00001990 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00001991 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001992 }
Chris Lattner699b6612008-01-25 18:59:06 +00001993 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00001994
1995 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00001996 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001997 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001998 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001999 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002000 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002001
John McCallb3d87482010-08-24 05:47:05 +00002002 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2003 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002004}
Sebastian Redl1d922962008-12-13 15:32:12 +00002005
Douglas Gregor2725ca82010-04-21 19:57:20 +00002006/// \brief Parse the remainder of an Objective-C message following the
2007/// '[' objc-receiver.
2008///
2009/// This routine handles sends to super, class messages (sent to a
2010/// class name), and instance messages (sent to an object), and the
2011/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2012/// ReceiverExpr, respectively. Only one of these parameters may have
2013/// a valid value.
2014///
2015/// \param LBracLoc The location of the opening '['.
2016///
2017/// \param SuperLoc If this is a send to 'super', the location of the
2018/// 'super' keyword that indicates a send to the superclass.
2019///
2020/// \param ReceiverType If this is a class message, the type of the
2021/// class we are sending a message to.
2022///
2023/// \param ReceiverExpr If this is an instance message, the expression
2024/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002025///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002026/// objc-message-args:
2027/// objc-selector
2028/// objc-keywordarg-list
2029///
2030/// objc-keywordarg-list:
2031/// objc-keywordarg
2032/// objc-keywordarg-list objc-keywordarg
2033///
Mike Stump1eb44332009-09-09 15:08:12 +00002034/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002035/// selector-name[opt] ':' objc-keywordexpr
2036///
2037/// objc-keywordexpr:
2038/// nonempty-expr-list
2039///
2040/// nonempty-expr-list:
2041/// assignment-expression
2042/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002043///
John McCall60d7b3a2010-08-24 06:29:42 +00002044ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002045Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002046 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002047 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002048 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002049 InMessageExpressionRAIIObject InMessage(*this, true);
2050
Steve Naroffc4df6d22009-11-07 02:08:14 +00002051 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002052 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002053 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2054 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002055 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002056 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2057 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002058 else
John McCall9ae2f072010-08-23 23:25:46 +00002059 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002060 0, 0, false);
Douglas Gregordc845342010-05-25 05:58:43 +00002061 ConsumeCodeCompletionToken();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002062 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002063
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002064 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002065 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002066 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002067
Anders Carlssonff975cf2009-02-14 18:21:46 +00002068 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Steve Naroff68d331a2007-09-27 14:38:14 +00002070 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002071 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002072
Chris Lattnerdf195262007-10-09 17:51:17 +00002073 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002074 while (1) {
2075 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002076 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00002077
Chris Lattnerdf195262007-10-09 17:51:17 +00002078 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002079 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002080 // We must manually skip to a ']', otherwise the expression skipper will
2081 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2082 // the enclosing expression.
2083 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002084 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002085 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002086
Steve Naroff68d331a2007-09-27 14:38:14 +00002087 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002088 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002089
2090 if (Tok.is(tok::code_completion)) {
2091 if (SuperLoc.isValid())
2092 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2093 KeyIdents.data(),
2094 KeyIdents.size(),
2095 /*AtArgumentEpression=*/true);
2096 else if (ReceiverType)
2097 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2098 KeyIdents.data(),
2099 KeyIdents.size(),
2100 /*AtArgumentEpression=*/true);
2101 else
2102 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2103 KeyIdents.data(),
2104 KeyIdents.size(),
2105 /*AtArgumentEpression=*/true);
2106
2107 ConsumeCodeCompletionToken();
2108 SkipUntil(tok::r_square);
2109 return ExprError();
2110 }
2111
John McCall60d7b3a2010-08-24 06:29:42 +00002112 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002113 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002114 // We must manually skip to a ']', otherwise the expression skipper will
2115 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2116 // the enclosing expression.
2117 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002118 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002119 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002120
Steve Naroff37387c92007-09-17 20:25:27 +00002121 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002122 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002123
Douglas Gregord3c68542009-11-19 01:08:35 +00002124 // Code completion after each argument.
2125 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002126 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002127 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002128 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002129 KeyIdents.size(),
2130 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002131 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002132 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002133 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002134 KeyIdents.size(),
2135 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002136 else
John McCall9ae2f072010-08-23 23:25:46 +00002137 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002138 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002139 KeyIdents.size(),
2140 /*AtArgumentEpression=*/false);
Douglas Gregordc845342010-05-25 05:58:43 +00002141 ConsumeCodeCompletionToken();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002142 SkipUntil(tok::r_square);
2143 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002144 }
2145
Steve Naroff37387c92007-09-17 20:25:27 +00002146 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002147 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002148 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002149 break;
2150 // We have a selector or a colon, continue parsing.
2151 }
2152 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002153 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002154 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002155 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002156 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002157 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002158 // We must manually skip to a ']', otherwise the expression skipper will
2159 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2160 // the enclosing expression.
2161 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002162 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002163 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002164
Steve Naroff49f109c2007-11-15 13:05:42 +00002165 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002166 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002167 }
2168 } else if (!selIdent) {
2169 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002170
Chris Lattner4fef81d2008-08-05 06:19:09 +00002171 // We must manually skip to a ']', otherwise the expression skipper will
2172 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2173 // the enclosing expression.
2174 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002175 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002176 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002177
Chris Lattnerdf195262007-10-09 17:51:17 +00002178 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002179 if (Tok.is(tok::identifier))
2180 Diag(Tok, diag::err_expected_colon);
2181 else
2182 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002183 // We must manually skip to a ']', otherwise the expression skipper will
2184 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2185 // the enclosing expression.
2186 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002187 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002188 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002189
Chris Lattner699b6612008-01-25 18:59:06 +00002190 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002191
Steve Naroff29238a02007-10-05 18:42:47 +00002192 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00002193 if (nKeys == 0)
2194 KeyIdents.push_back(selIdent);
2195 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002196
Douglas Gregor2725ca82010-04-21 19:57:20 +00002197 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002198 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002199 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002200 MultiExprArg(Actions,
2201 KeyExprs.take(),
2202 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002203 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002204 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002205 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002206 MultiExprArg(Actions,
2207 KeyExprs.take(),
2208 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002209 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002210 LBracLoc, SelectorLoc, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002211 MultiExprArg(Actions,
2212 KeyExprs.take(),
2213 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002214}
2215
John McCall60d7b3a2010-08-24 06:29:42 +00002216ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2217 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002218 if (Res.isInvalid()) return move(Res);
2219
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002220 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2221 // expressions. At this point, we know that the only valid thing that starts
2222 // with '@' is an @"".
2223 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002224 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002225 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002226 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002227
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002228 while (Tok.is(tok::at)) {
2229 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002230
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002231 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002232 if (!isTokenStringLiteral())
2233 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002234
John McCall60d7b3a2010-08-24 06:29:42 +00002235 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002236 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002237 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002238
Sebastian Redleffa8d12008-12-10 00:02:53 +00002239 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002240 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002241
2242 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2243 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002244}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002245
2246/// objc-encode-expression:
2247/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002248ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002249Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002250 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002251
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002252 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002253
Chris Lattner4fef81d2008-08-05 06:19:09 +00002254 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002255 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2256
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002257 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002258
Douglas Gregor809070a2009-02-18 17:45:20 +00002259 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002260
Anders Carlsson4988ae32007-08-23 15:31:37 +00002261 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00002262
Douglas Gregor809070a2009-02-18 17:45:20 +00002263 if (Ty.isInvalid())
2264 return ExprError();
2265
Mike Stump1eb44332009-09-09 15:08:12 +00002266 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00002267 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002268}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002269
2270/// objc-protocol-expression
2271/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002272ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002273Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002274 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002275
Chris Lattner4fef81d2008-08-05 06:19:09 +00002276 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002277 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2278
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002279 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002280
Chris Lattner4fef81d2008-08-05 06:19:09 +00002281 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002282 return ExprError(Diag(Tok, diag::err_expected_ident));
2283
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002284 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002285 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002286
Anders Carlsson4988ae32007-08-23 15:31:37 +00002287 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002288
Sebastian Redl1d922962008-12-13 15:32:12 +00002289 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2290 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002291}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002292
2293/// objc-selector-expression
2294/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002295ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002296 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002297
Chris Lattner4fef81d2008-08-05 06:19:09 +00002298 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002299 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2300
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002301 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002302 SourceLocation LParenLoc = ConsumeParen();
2303 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002304
2305 if (Tok.is(tok::code_completion)) {
2306 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2307 KeyIdents.size());
2308 ConsumeCodeCompletionToken();
2309 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2310 return ExprError();
2311 }
2312
Chris Lattner2fc5c242009-04-11 18:13:45 +00002313 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002314 if (!SelIdent && // missing selector name.
2315 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002316 return ExprError(Diag(Tok, diag::err_expected_ident));
2317
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002318 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002319 unsigned nColons = 0;
2320 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002321 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002322 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2323 ++nColons;
2324 KeyIdents.push_back(0);
2325 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002326 return ExprError(Diag(Tok, diag::err_expected_colon));
2327
Chris Lattner5add7542010-08-27 22:32:41 +00002328 ++nColons;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002329 ConsumeToken(); // Eat the ':'.
2330 if (Tok.is(tok::r_paren))
2331 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002332
2333 if (Tok.is(tok::code_completion)) {
2334 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2335 KeyIdents.size());
2336 ConsumeCodeCompletionToken();
2337 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2338 return ExprError();
2339 }
2340
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002341 // Check for another keyword selector.
2342 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002343 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002344 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002345 if (!SelIdent && Tok.isNot(tok::colon))
2346 break;
2347 }
Steve Naroff887407e2007-12-05 22:21:29 +00002348 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002349 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00002350 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002351 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2352 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00002353 }