blob: 2bcb7a786ab4cfa98183f05b63e19ef5da712e1b [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'
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000032Parser::DeclGroupPtrTy 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)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000036 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000037 cutOffParsing();
38 return DeclGroupPtrTy();
Douglas Gregorc464ae82009-12-07 09:27:33 +000039 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000040
41 Decl *SingleDecl = 0;
Steve Naroff861cf3e2007-08-23 18:16:40 +000042 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000043 case tok::objc_class:
44 return ParseObjCAtClassDeclaration(AtLoc);
John McCall7f040a92010-12-24 02:08:15 +000045 case tok::objc_interface: {
John McCall0b7e6782011-03-24 11:26:52 +000046 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000047 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
48 break;
John McCall7f040a92010-12-24 02:08:15 +000049 }
50 case tok::objc_protocol: {
John McCall0b7e6782011-03-24 11:26:52 +000051 ParsedAttributes attrs(AttrFactory);
Douglas Gregorbd9482d2012-01-01 21:23:57 +000052 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall7f040a92010-12-24 02:08:15 +000053 }
Chris Lattner5ffb14b2008-08-23 02:02:23 +000054 case tok::objc_implementation:
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +000055 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattner5ffb14b2008-08-23 02:02:23 +000056 case tok::objc_end:
Fariborz Jahanian140ab232011-08-31 17:37:55 +000057 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner5ffb14b2008-08-23 02:02:23 +000058 case tok::objc_compatibility_alias:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000059 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
60 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000061 case tok::objc_synthesize:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000062 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
63 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000064 case tok::objc_dynamic:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000065 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
66 break;
Ted Kremenek32ad2ee2012-03-01 22:07:04 +000067 case tok::objc___experimental_modules_import:
David Blaikie4e4d0842012-03-11 07:00:24 +000068 if (getLangOpts().Modules)
Douglas Gregor94ad28b2012-01-03 18:24:14 +000069 return ParseModuleImport(AtLoc);
70
71 // Fall through
72
Chris Lattner5ffb14b2008-08-23 02:02:23 +000073 default:
74 Diag(AtLoc, diag::err_unexpected_at);
75 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000076 SingleDecl = 0;
77 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000078 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000079 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +000080}
81
82///
Mike Stump1eb44332009-09-09 15:08:12 +000083/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000084/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000085///
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000086Parser::DeclGroupPtrTy
87Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000088 ConsumeToken(); // the identifier "class"
Chris Lattner5f9e2722011-07-23 10:55:15 +000089 SmallVector<IdentifierInfo *, 8> ClassNames;
90 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremenekc09cba62009-11-17 23:12:20 +000091
Mike Stump1eb44332009-09-09 15:08:12 +000092
Reid Spencer5f016e22007-07-11 17:01:13 +000093 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000094 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000095 Diag(Tok, diag::err_expected_ident);
96 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000097 return Actions.ConvertDeclToDeclGroup(0);
Reid Spencer5f016e22007-07-11 17:01:13 +000098 }
Reid Spencer5f016e22007-07-11 17:01:13 +000099 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +0000100 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattnerdf195262007-10-09 17:51:17 +0000103 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 ConsumeToken();
107 }
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 // Consume the ';'.
110 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000111 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenekc09cba62009-11-17 23:12:20 +0000113 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
114 ClassLocs.data(),
115 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000116}
117
Erik Verbruggend64251f2011-12-06 09:25:23 +0000118void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
119{
120 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
121 if (ock == Sema::OCK_None)
122 return;
123
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +0000124 Decl *Decl = Actions.getObjCDeclContext();
125 if (CurParsedObjCImpl) {
126 CurParsedObjCImpl->finish(AtLoc);
127 } else {
128 Actions.ActOnAtEnd(getCurScope(), AtLoc);
129 }
Erik Verbruggend64251f2011-12-06 09:25:23 +0000130 Diag(AtLoc, diag::err_objc_missing_end)
131 << FixItHint::CreateInsertion(AtLoc, "@end\n");
132 if (Decl)
133 Diag(Decl->getLocStart(), diag::note_objc_container_start)
134 << (int) ock;
Erik Verbruggend64251f2011-12-06 09:25:23 +0000135}
136
Steve Naroffdac269b2007-08-20 21:31:48 +0000137///
138/// objc-interface:
139/// objc-class-interface-attributes[opt] objc-class-interface
140/// objc-category-interface
141///
142/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000143/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000144/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000145/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000146/// objc-interface-decl-list
147/// @end
148///
149/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000150/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000151/// objc-protocol-refs[opt]
152/// objc-interface-decl-list
153/// @end
154///
155/// objc-superclass:
156/// ':' identifier
157///
158/// objc-class-interface-attributes:
159/// __attribute__((visibility("default")))
160/// __attribute__((visibility("hidden")))
161/// __attribute__((deprecated))
162/// __attribute__((unavailable))
163/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardb2f68202012-04-06 18:12:22 +0000164/// __attribute__((objc_root_class))
Steve Naroffdac269b2007-08-20 21:31:48 +0000165///
Erik Verbruggend64251f2011-12-06 09:25:23 +0000166Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +0000167 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000168 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000169 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggend64251f2011-12-06 09:25:23 +0000170 CheckNestedObjCContexts(AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000171 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000173 // Code completion after '@interface'.
174 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000175 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000176 cutOffParsing();
177 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000178 }
179
Chris Lattnerdf195262007-10-09 17:51:17 +0000180 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000181 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000182 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000183 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000184
Steve Naroffdac269b2007-08-20 21:31:48 +0000185 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000186 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000187 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000188 if (Tok.is(tok::l_paren) &&
189 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000190
191 BalancedDelimiterTracker T(*this, tok::l_paren);
192 T.consumeOpen();
193
194 SourceLocation categoryLoc;
Steve Naroffdac269b2007-08-20 21:31:48 +0000195 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000196 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000197 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000198 cutOffParsing();
199 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000200 }
201
Steve Naroff527fe232007-08-23 19:56:30 +0000202 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000203 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000204 categoryId = Tok.getIdentifierInfo();
205 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000206 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000207 else if (!getLangOpts().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000208 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000209 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000210 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000211
212 T.consumeClose();
213 if (T.getCloseLocation().isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000214 return 0;
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000215
216 if (!attrs.empty()) { // categories don't support attributes.
217 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
218 attrs.clear();
Steve Naroffdac269b2007-08-20 21:31:48 +0000219 }
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000220
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000221 // Next, we need to check for any protocol references.
222 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000223 SmallVector<Decl *, 8> ProtocolRefs;
224 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000225 if (Tok.is(tok::less) &&
226 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000227 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000228 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000229
John McCalld226f652010-08-21 09:40:31 +0000230 Decl *CategoryType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000231 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000232 nameId, nameLoc,
233 categoryId, categoryLoc,
234 ProtocolRefs.data(),
235 ProtocolRefs.size(),
236 ProtocolLocs.data(),
237 EndProtoLoc);
Fariborz Jahaniane6f07f52011-08-19 18:02:47 +0000238
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000239 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000240 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000241
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000242 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000243 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000244 }
245 // Parse a class interface.
246 IdentifierInfo *superClassId = 0;
247 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000248
Chris Lattnerdf195262007-10-09 17:51:17 +0000249 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000250 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000251
252 // Code completion of superclass names.
253 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000254 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000255 cutOffParsing();
256 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000257 }
258
Chris Lattnerdf195262007-10-09 17:51:17 +0000259 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000260 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000261 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000262 }
263 superClassId = Tok.getIdentifierInfo();
264 superClassLoc = ConsumeToken();
265 }
266 // Next, we need to check for any protocol references.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000267 SmallVector<Decl *, 8> ProtocolRefs;
268 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000269 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000270 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000271 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
272 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000273 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000274
John McCalld226f652010-08-21 09:40:31 +0000275 Decl *ClsType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000276 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000277 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000278 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000279 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +0000280 EndProtoLoc, attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Chris Lattnerdf195262007-10-09 17:51:17 +0000282 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000283 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000284
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000285 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000286 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000287}
288
John McCalld0014542009-12-03 22:31:13 +0000289/// The Objective-C property callback. This should be defined where
290/// it's used, but instead it's been lifted to here to support VS2005.
291struct Parser::ObjCPropertyCallback : FieldCallback {
David Blaikie99ba9e32011-12-20 02:48:34 +0000292private:
293 virtual void anchor();
294public:
John McCalld0014542009-12-03 22:31:13 +0000295 Parser &P;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000296 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000297 ObjCDeclSpec &OCDS;
298 SourceLocation AtLoc;
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000299 SourceLocation LParenLoc;
John McCalld0014542009-12-03 22:31:13 +0000300 tok::ObjCKeywordKind MethodImplKind;
301
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000302 ObjCPropertyCallback(Parser &P,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000303 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000304 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000305 SourceLocation LParenLoc,
John McCalld0014542009-12-03 22:31:13 +0000306 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000307 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc), LParenLoc(LParenLoc),
John McCalld0014542009-12-03 22:31:13 +0000308 MethodImplKind(MethodImplKind) {
309 }
310
Eli Friedmandcdff462012-08-08 23:53:27 +0000311 void invoke(ParsingFieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000312 if (FD.D.getIdentifier() == 0) {
313 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
314 << FD.D.getSourceRange();
Eli Friedmandcdff462012-08-08 23:53:27 +0000315 return;
John McCalld0014542009-12-03 22:31:13 +0000316 }
317 if (FD.BitfieldSize) {
318 P.Diag(AtLoc, diag::err_objc_property_bitfield)
319 << FD.D.getSourceRange();
Eli Friedmandcdff462012-08-08 23:53:27 +0000320 return;
John McCalld0014542009-12-03 22:31:13 +0000321 }
322
323 // Install the property declarator into interfaceDecl.
324 IdentifierInfo *SelName =
325 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
326
327 Selector GetterSel =
328 P.PP.getSelectorTable().getNullarySelector(SelName);
329 IdentifierInfo *SetterName = OCDS.getSetterName();
330 Selector SetterSel;
331 if (SetterName)
332 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
333 else
334 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
335 P.PP.getSelectorTable(),
336 FD.D.getIdentifier());
337 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000338 Decl *Property =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000339 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, LParenLoc,
340 FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000341 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000342 &isOverridingProperty,
343 MethodImplKind);
344 if (!isOverridingProperty)
345 Props.push_back(Property);
346
Eli Friedmanf66a0dd2012-08-08 23:04:35 +0000347 FD.complete(Property);
John McCalld0014542009-12-03 22:31:13 +0000348 }
349};
350
David Blaikie99ba9e32011-12-20 02:48:34 +0000351void Parser::ObjCPropertyCallback::anchor() {
352}
353
Steve Naroffdac269b2007-08-20 21:31:48 +0000354/// objc-interface-decl-list:
355/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000356/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000357/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000358/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000359/// objc-interface-decl-list declaration
360/// objc-interface-decl-list ';'
361///
Steve Naroff294494e2007-08-22 16:35:03 +0000362/// objc-method-requirement: [OBJC2]
363/// @required
364/// @optional
365///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000366void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
367 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000368 SmallVector<Decl *, 32> allMethods;
369 SmallVector<Decl *, 16> allProperties;
370 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000371 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Ted Kremenek782f2f52010-01-07 01:20:12 +0000373 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000374
Steve Naroff294494e2007-08-22 16:35:03 +0000375 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000376 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000377 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Fariborz Jahaniand30ec702012-07-26 17:32:28 +0000378 if (Decl *methodPrototype =
379 ParseObjCMethodPrototype(MethodImplKind, false))
380 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000381 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
382 // method definitions.
Argyrios Kyrtzidis0db9f4d2011-12-17 04:13:22 +0000383 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
384 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
385 SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
386 if (Tok.is(tok::semi))
387 ConsumeToken();
388 }
Steve Naroff294494e2007-08-22 16:35:03 +0000389 continue;
390 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000391 if (Tok.is(tok::l_paren)) {
392 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000393 ParseObjCMethodDecl(Tok.getLocation(),
394 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000395 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000396 continue;
397 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000398 // Ignore excess semicolons.
399 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000400 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000401 continue;
402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattnerbc662af2008-10-20 06:10:06 +0000404 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000405 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000406 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000408 // Code completion within an Objective-C interface.
409 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000410 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +0000411 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallf312b1e2010-08-26 23:41:50 +0000412 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000413 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000414 }
415
Chris Lattnere82a10f2008-10-20 05:46:22 +0000416 // If we don't have an @ directive, parse it as a function definition.
417 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000418 // The code below does not consume '}'s because it is afraid of eating the
419 // end of a namespace. Because of the way this code is structured, an
420 // erroneous r_brace would cause an infinite loop if not handled here.
421 if (Tok.is(tok::r_brace))
422 break;
Sean Hunt2edf0a22012-06-23 05:07:58 +0000423 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000424 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000425 continue;
426 }
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Chris Lattnere82a10f2008-10-20 05:46:22 +0000428 // Otherwise, we have an @ directive, eat the @.
429 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000430 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000431 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000432 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000433 }
434
Chris Lattnera2449b22008-10-20 05:57:40 +0000435 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Chris Lattnera2449b22008-10-20 05:57:40 +0000437 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000438 AtEnd.setBegin(AtLoc);
439 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000440 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000441 } else if (DirectiveKind == tok::objc_not_keyword) {
442 Diag(Tok, diag::err_objc_unknown_at);
443 SkipUntil(tok::semi);
444 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000445 }
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattnerbc662af2008-10-20 06:10:06 +0000447 // Eat the identifier.
448 ConsumeToken();
449
Chris Lattnera2449b22008-10-20 05:57:40 +0000450 switch (DirectiveKind) {
451 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000452 // FIXME: If someone forgets an @end on a protocol, this loop will
453 // continue to eat up tons of stuff and spew lots of nonsense errors. It
454 // would probably be better to bail out if we saw an @class or @interface
455 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000456 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000457 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000458 SkipUntil(tok::r_brace, tok::at);
459 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000460
461 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000462 case tok::objc_interface:
Erik Verbruggend64251f2011-12-06 09:25:23 +0000463 Diag(AtLoc, diag::err_objc_missing_end)
464 << FixItHint::CreateInsertion(AtLoc, "@end\n");
465 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
466 << (int) Actions.getObjCContainerKind();
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000467 ConsumeToken();
468 break;
469
Chris Lattnera2449b22008-10-20 05:57:40 +0000470 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000471 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000472 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000473 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000474 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000475 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000476 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000477 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000478 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Chris Lattnera2449b22008-10-20 05:57:40 +0000480 case tok::objc_property:
David Blaikie4e4d0842012-03-11 07:00:24 +0000481 if (!getLangOpts().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000482 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000483
Chris Lattnere82a10f2008-10-20 05:46:22 +0000484 ObjCDeclSpec OCDS;
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000485 SourceLocation LParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000486 // Parse property attribute list, if any.
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000487 if (Tok.is(tok::l_paren)) {
488 LParenLoc = Tok.getLocation();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000489 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000490 }
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000492 ObjCPropertyCallback Callback(*this, allProperties,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000493 OCDS, AtLoc, LParenLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000494
Chris Lattnere82a10f2008-10-20 05:46:22 +0000495 // Parse all the comma separated declarators.
Eli Friedmanf66a0dd2012-08-08 23:04:35 +0000496 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +0000497 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000498
John McCall7da19ea2011-03-26 01:53:26 +0000499 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000500 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000501 }
Steve Naroff294494e2007-08-22 16:35:03 +0000502 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000503
504 // We break out of the big loop in two cases: when we see @end or when we see
505 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000506 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000507 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000508 return cutOffParsing();
Erik Verbruggend64251f2011-12-06 09:25:23 +0000509 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerbc662af2008-10-20 06:10:06 +0000510 ConsumeToken(); // the "end" identifier
Erik Verbruggend64251f2011-12-06 09:25:23 +0000511 } else {
512 Diag(Tok, diag::err_objc_missing_end)
513 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
514 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
515 << (int) Actions.getObjCContainerKind();
516 AtEnd.setBegin(Tok.getLocation());
517 AtEnd.setEnd(Tok.getLocation());
518 }
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Chris Lattnera2449b22008-10-20 05:57:40 +0000520 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000521 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000522 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000523 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000524 allProperties.data(), allProperties.size(),
525 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000526}
527
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000528/// Parse property attribute declarations.
529///
530/// property-attr-decl: '(' property-attrlist ')'
531/// property-attrlist:
532/// property-attribute
533/// property-attrlist ',' property-attribute
534/// property-attribute:
535/// getter '=' identifier
536/// setter '=' identifier ':'
537/// readonly
538/// readwrite
539/// assign
540/// retain
541/// copy
542/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000543/// atomic
544/// strong
545/// weak
546/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000547///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000548void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000549 assert(Tok.getKind() == tok::l_paren);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000550 BalancedDelimiterTracker T(*this, tok::l_paren);
551 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000553 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000554 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000555 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000556 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000557 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000558 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000560 // If this is not an identifier at all, bail out early.
561 if (II == 0) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000562 T.consumeClose();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000563 return;
564 }
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Chris Lattner156b0612008-10-20 07:37:22 +0000566 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Chris Lattner92e62b02008-11-20 04:42:34 +0000568 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000569 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000570 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000571 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000572 else if (II->isStr("unsafe_unretained"))
573 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000574 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000575 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000576 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000577 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000578 else if (II->isStr("strong"))
579 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000580 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000581 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000582 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000583 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000584 else if (II->isStr("atomic"))
585 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000586 else if (II->isStr("weak"))
587 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000588 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000589 bool IsSetter = II->getNameStart()[0] == 's';
590
Chris Lattnere00da7c2008-10-20 07:39:53 +0000591 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000592 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
593 diag::err_objc_expected_equal_for_getter;
594
595 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000596 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Douglas Gregor4ad96852009-11-19 07:41:15 +0000598 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000599 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000600 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000601 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000602 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000603 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000604 }
605
Anders Carlsson42499be2010-10-02 17:45:21 +0000606
607 SourceLocation SelLoc;
608 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
609
610 if (!SelIdent) {
611 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
612 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000613 SkipUntil(tok::r_paren);
614 return;
615 }
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Anders Carlsson42499be2010-10-02 17:45:21 +0000617 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000618 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000619 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000621 if (ExpectAndConsume(tok::colon,
622 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000623 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000624 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000625 } else {
626 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000627 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000628 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000629 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000630 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000631 SkipUntil(tok::r_paren);
632 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Chris Lattner156b0612008-10-20 07:37:22 +0000635 if (Tok.isNot(tok::comma))
636 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Chris Lattner156b0612008-10-20 07:37:22 +0000638 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000639 }
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000641 T.consumeClose();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000642}
643
Steve Naroff3536b442007-09-06 21:24:23 +0000644/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000645/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000646/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000647///
648/// objc-instance-method: '-'
649/// objc-class-method: '+'
650///
Steve Naroff4985ace2007-08-22 18:35:33 +0000651/// objc-method-attributes: [OBJC2]
652/// __attribute__((deprecated))
653///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000654Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000655 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000656 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000657
Mike Stump1eb44332009-09-09 15:08:12 +0000658 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000659 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000660 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000661 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000662 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000663 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000664 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000665}
666
667/// objc-selector:
668/// identifier
669/// one of
670/// enum struct union if else while do for switch case default
671/// break continue return goto asm sizeof typeof __alignof
672/// unsigned long const short volatile signed restrict _Complex
673/// in out inout bycopy byref oneway int char float double void _Bool
674///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000675IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000676
Chris Lattnerff384912007-10-07 02:00:24 +0000677 switch (Tok.getKind()) {
678 default:
679 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000680 case tok::ampamp:
681 case tok::ampequal:
682 case tok::amp:
683 case tok::pipe:
684 case tok::tilde:
685 case tok::exclaim:
686 case tok::exclaimequal:
687 case tok::pipepipe:
688 case tok::pipeequal:
689 case tok::caret:
690 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000691 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000692 if (isalpha(ThisTok[0])) {
693 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
694 Tok.setKind(tok::identifier);
695 SelectorLoc = ConsumeToken();
696 return II;
697 }
698 return 0;
699 }
700
Chris Lattnerff384912007-10-07 02:00:24 +0000701 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000702 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000703 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000704 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000705 case tok::kw_break:
706 case tok::kw_case:
707 case tok::kw_catch:
708 case tok::kw_char:
709 case tok::kw_class:
710 case tok::kw_const:
711 case tok::kw_const_cast:
712 case tok::kw_continue:
713 case tok::kw_default:
714 case tok::kw_delete:
715 case tok::kw_do:
716 case tok::kw_double:
717 case tok::kw_dynamic_cast:
718 case tok::kw_else:
719 case tok::kw_enum:
720 case tok::kw_explicit:
721 case tok::kw_export:
722 case tok::kw_extern:
723 case tok::kw_false:
724 case tok::kw_float:
725 case tok::kw_for:
726 case tok::kw_friend:
727 case tok::kw_goto:
728 case tok::kw_if:
729 case tok::kw_inline:
730 case tok::kw_int:
731 case tok::kw_long:
732 case tok::kw_mutable:
733 case tok::kw_namespace:
734 case tok::kw_new:
735 case tok::kw_operator:
736 case tok::kw_private:
737 case tok::kw_protected:
738 case tok::kw_public:
739 case tok::kw_register:
740 case tok::kw_reinterpret_cast:
741 case tok::kw_restrict:
742 case tok::kw_return:
743 case tok::kw_short:
744 case tok::kw_signed:
745 case tok::kw_sizeof:
746 case tok::kw_static:
747 case tok::kw_static_cast:
748 case tok::kw_struct:
749 case tok::kw_switch:
750 case tok::kw_template:
751 case tok::kw_this:
752 case tok::kw_throw:
753 case tok::kw_true:
754 case tok::kw_try:
755 case tok::kw_typedef:
756 case tok::kw_typeid:
757 case tok::kw_typename:
758 case tok::kw_typeof:
759 case tok::kw_union:
760 case tok::kw_unsigned:
761 case tok::kw_using:
762 case tok::kw_virtual:
763 case tok::kw_void:
764 case tok::kw_volatile:
765 case tok::kw_wchar_t:
766 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000767 case tok::kw__Bool:
768 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000769 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000770 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000771 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000772 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000773 }
Steve Naroff294494e2007-08-22 16:35:03 +0000774}
775
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000776/// objc-for-collection-in: 'in'
777///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000778bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000779 // FIXME: May have to do additional look-ahead to only allow for
780 // valid tokens following an 'in'; such as an identifier, unary operators,
781 // '[' etc.
David Blaikie4e4d0842012-03-11 07:00:24 +0000782 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000783 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000784}
785
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000786/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000787/// qualifier list and builds their bitmask representation in the input
788/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000789///
790/// objc-type-qualifiers:
791/// objc-type-qualifier
792/// objc-type-qualifiers objc-type-qualifier
793///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000794void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000795 Declarator::TheContext Context) {
796 assert(Context == Declarator::ObjCParameterContext ||
797 Context == Declarator::ObjCResultContext);
798
Chris Lattnere8b724d2007-12-12 06:56:32 +0000799 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000800 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000801 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCallcdda47f2011-10-01 09:56:14 +0000802 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000803 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000804 }
805
Chris Lattnercb53b362007-12-27 19:57:00 +0000806 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000807 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Chris Lattnere8b724d2007-12-12 06:56:32 +0000809 const IdentifierInfo *II = Tok.getIdentifierInfo();
810 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000811 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000812 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000814 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000815 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000816 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000817 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
818 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
819 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
820 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
821 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
822 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000823 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000824 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000825 ConsumeToken();
826 II = 0;
827 break;
828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Chris Lattnere8b724d2007-12-12 06:56:32 +0000830 // If this wasn't a recognized qualifier, bail out.
831 if (II) return;
832 }
833}
834
John McCallcdda47f2011-10-01 09:56:14 +0000835/// Take all the decl attributes out of the given list and add
836/// them to the given attribute set.
837static void takeDeclAttributes(ParsedAttributes &attrs,
838 AttributeList *list) {
839 while (list) {
840 AttributeList *cur = list;
841 list = cur->getNext();
842
843 if (!cur->isUsedAsTypeAttr()) {
844 // Clear out the next pointer. We're really completely
845 // destroying the internal invariants of the declarator here,
846 // but it doesn't matter because we're done with it.
847 cur->setNext(0);
848 attrs.add(cur);
849 }
850 }
851}
852
853/// takeDeclAttributes - Take all the decl attributes from the given
854/// declarator and add them to the given list.
855static void takeDeclAttributes(ParsedAttributes &attrs,
856 Declarator &D) {
857 // First, take ownership of all attributes.
858 attrs.getPool().takeAllFrom(D.getAttributePool());
859 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
860
861 // Now actually move the attributes over.
862 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
863 takeDeclAttributes(attrs, D.getAttributes());
864 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
865 takeDeclAttributes(attrs,
866 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
867}
868
Chris Lattnere8b724d2007-12-12 06:56:32 +0000869/// objc-type-name:
870/// '(' objc-type-qualifiers[opt] type-name ')'
871/// '(' objc-type-qualifiers[opt] ')'
872///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000873ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000874 Declarator::TheContext context,
875 ParsedAttributes *paramAttrs) {
876 assert(context == Declarator::ObjCParameterContext ||
877 context == Declarator::ObjCResultContext);
878 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
879
Chris Lattnerdf195262007-10-09 17:51:17 +0000880 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000882 BalancedDelimiterTracker T(*this, tok::l_paren);
883 T.consumeOpen();
884
Chris Lattnere8904e92008-08-23 01:48:03 +0000885 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000886 ObjCDeclContextSwitch ObjCDC(*this);
887
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000888 // Parse type qualifiers, in, inout, etc.
John McCallcdda47f2011-10-01 09:56:14 +0000889 ParseObjCTypeQualifierList(DS, context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000890
John McCallb3d87482010-08-24 05:47:05 +0000891 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000892 if (isTypeSpecifierQualifier()) {
John McCallcdda47f2011-10-01 09:56:14 +0000893 // Parse an abstract declarator.
894 DeclSpec declSpec(AttrFactory);
895 declSpec.setObjCQualifiers(&DS);
896 ParseSpecifierQualifierList(declSpec);
Fariborz Jahanian6d1de1b2012-05-29 21:52:45 +0000897 declSpec.SetRangeEnd(Tok.getLocation());
John McCallcdda47f2011-10-01 09:56:14 +0000898 Declarator declarator(declSpec, context);
899 ParseDeclarator(declarator);
900
901 // If that's not invalid, extract a type.
902 if (!declarator.isInvalidType()) {
903 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
904 if (!type.isInvalid())
905 Ty = type.get();
906
907 // If we're parsing a parameter, steal all the decl attributes
908 // and add them to the decl spec.
909 if (context == Declarator::ObjCParameterContext)
910 takeDeclAttributes(*paramAttrs, declarator);
911 }
912 } else if (context == Declarator::ObjCResultContext &&
913 Tok.is(tok::identifier)) {
Douglas Gregore97179c2011-09-08 01:46:34 +0000914 if (!Ident_instancetype)
915 Ident_instancetype = PP.getIdentifierInfo("instancetype");
916
917 if (Tok.getIdentifierInfo() == Ident_instancetype) {
918 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
919 ConsumeToken();
920 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000921 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000922
Steve Naroffd7333c22008-10-21 14:15:04 +0000923 if (Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000924 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000925 else if (Tok.getLocation() == TypeStartLoc) {
926 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000927 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000928 SkipUntil(tok::r_paren);
929 } else {
930 // Otherwise, we found *something*, but didn't get a ')' in the right
931 // place. Emit an error then return what we have as the type.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000932 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000933 }
Steve Narofff28b2642007-09-05 23:30:30 +0000934 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000935}
936
937/// objc-method-decl:
938/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000939/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000940/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000941/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000942///
943/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000944/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000945/// objc-keyword-selector objc-keyword-decl
946///
947/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000948/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
949/// objc-selector ':' objc-keyword-attributes[opt] identifier
950/// ':' objc-type-name objc-keyword-attributes[opt] identifier
951/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000952///
Steve Naroff4985ace2007-08-22 18:35:33 +0000953/// objc-parmlist:
954/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000955///
Steve Naroff4985ace2007-08-22 18:35:33 +0000956/// objc-parms:
957/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000958///
Steve Naroff4985ace2007-08-22 18:35:33 +0000959/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000960/// , ...
961///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000962/// objc-keyword-attributes: [OBJC2]
963/// __attribute__((unused))
964///
John McCalld226f652010-08-21 09:40:31 +0000965Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000966 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000967 tok::ObjCKeywordKind MethodImplKind,
968 bool MethodDefinition) {
John McCall92576642012-05-07 06:16:41 +0000969 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall54abf7d2009-11-04 02:18:39 +0000970
Douglas Gregore8f5a172010-04-07 00:21:17 +0000971 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000972 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000973 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000974 cutOffParsing();
975 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000976 }
977
Chris Lattnere8904e92008-08-23 01:48:03 +0000978 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000979 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000980 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000981 if (Tok.is(tok::l_paren))
John McCallcdda47f2011-10-01 09:56:14 +0000982 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Ted Kremenek9e049352010-02-18 23:05:16 +0000984 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000985 ParsedAttributes methodAttrs(AttrFactory);
David Blaikie4e4d0842012-03-11 07:00:24 +0000986 if (getLangOpts().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000987 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000988
Douglas Gregore8f5a172010-04-07 00:21:17 +0000989 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000990 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000991 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000992 cutOffParsing();
993 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000994 }
995
Ted Kremenek9e049352010-02-18 23:05:16 +0000996 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000997 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000998 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000999
Steve Naroff84c43102009-02-11 20:43:13 +00001000 // An unnamed colon is valid.
1001 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001002 Diag(Tok, diag::err_expected_selector_for_method)
1003 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniand30ec702012-07-26 17:32:28 +00001004 // Skip until we get a ; or @.
1005 SkipUntil(tok::at, true /*StopAtSemi*/, true /*don't consume*/);
John McCalld226f652010-08-21 09:40:31 +00001006 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +00001007 }
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Chris Lattner5f9e2722011-07-23 10:55:15 +00001009 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +00001010 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001011 // If attributes exist after the method, parse them.
David Blaikie4e4d0842012-03-11 07:00:24 +00001012 if (getLangOpts().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001013 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Chris Lattnerff384912007-10-07 02:00:24 +00001015 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +00001016 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001017 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001018 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001019 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001020 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001021 methodAttrs.getList(), MethodImplKind,
1022 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +00001023 PD.complete(Result);
1024 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +00001025 }
Steve Narofff28b2642007-09-05 23:30:30 +00001026
Chris Lattner5f9e2722011-07-23 10:55:15 +00001027 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001028 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001029 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001030 ParseScope PrototypeScope(this,
1031 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +00001032
1033 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian92faee72012-09-11 17:24:26 +00001034 bool warnSelectorName = false;
Fariborz Jahanian10d65cd2012-09-11 21:27:45 +00001035 bool warnHasNoName = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001036 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +00001037 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +00001038 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Chris Lattnerff384912007-10-07 02:00:24 +00001040 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +00001041 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001042 Diag(Tok, diag::err_expected_colon);
1043 break;
1044 }
1045 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001046
John McCallb3d87482010-08-24 05:47:05 +00001047 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001048 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCallcdda47f2011-10-01 09:56:14 +00001049 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1050 Declarator::ObjCParameterContext,
1051 &paramAttrs);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001052
Chris Lattnerff384912007-10-07 02:00:24 +00001053 // If attributes exist before the argument name, parse them.
John McCallcdda47f2011-10-01 09:56:14 +00001054 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnere294d3f2009-04-11 18:57:04 +00001055 ArgInfo.ArgAttrs = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001056 if (getLangOpts().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +00001057 MaybeParseGNUAttributes(paramAttrs);
1058 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +00001059 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001060
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001061 // Code completion for the next piece of the selector.
1062 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001063 KeyIdents.push_back(SelIdent);
1064 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1065 mType == tok::minus,
1066 /*AtParameterName=*/true,
1067 ReturnType,
1068 KeyIdents.data(),
1069 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001070 cutOffParsing();
1071 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001072 }
1073
Chris Lattnerdf195262007-10-09 17:51:17 +00001074 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001075 Diag(Tok, diag::err_expected_ident); // missing argument name.
1076 break;
Steve Naroff4985ace2007-08-22 18:35:33 +00001077 }
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Chris Lattnere294d3f2009-04-11 18:57:04 +00001079 ArgInfo.Name = Tok.getIdentifierInfo();
1080 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +00001081 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Chris Lattnere294d3f2009-04-11 18:57:04 +00001083 ArgInfos.push_back(ArgInfo);
1084 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001085 KeyLocs.push_back(selLoc);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001086
John McCall0b7e6782011-03-24 11:26:52 +00001087 // Make sure the attributes persist.
1088 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1089
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001090 // Code completion for the next piece of the selector.
1091 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001092 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1093 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001094 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001095 ReturnType,
1096 KeyIdents.data(),
1097 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001098 cutOffParsing();
1099 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001100 }
1101
Chris Lattnerff384912007-10-07 02:00:24 +00001102 // Check for another keyword selector.
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001103 SelIdent = ParseObjCSelectorPiece(selLoc);
Fariborz Jahanian92faee72012-09-11 17:24:26 +00001104 if (!SelIdent) {
1105 if (Tok.isNot(tok::colon))
1106 break;
1107 // parameter name was not followed with selector name; as in:
1108 // - (void) Meth: (id) Name:(id)Arg2; Issue a warning as user
1109 // might have meant: - (void) Meth: (id)Arg1 Name:(id)Arg2;
1110 Diag(Tok, diag::warn_missing_argument_name); // missing argument name.
1111 warnSelectorName = true;
1112 }
Fariborz Jahanian10d65cd2012-09-11 21:27:45 +00001113 else
1114 warnHasNoName = false;
Chris Lattnerff384912007-10-07 02:00:24 +00001115 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +00001116 }
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Steve Naroff335eafa2007-11-15 12:35:21 +00001118 bool isVariadic = false;
Fariborz Jahanian56242ba2012-06-21 18:43:08 +00001119 bool cStyleParamWarned = false;
Chris Lattnerff384912007-10-07 02:00:24 +00001120 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +00001121 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001122 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001123 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001124 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001125 ConsumeToken();
1126 break;
1127 }
Fariborz Jahanian56242ba2012-06-21 18:43:08 +00001128 if (!cStyleParamWarned) {
1129 Diag(Tok, diag::warn_cstyle_param);
1130 cStyleParamWarned = true;
1131 }
John McCall0b7e6782011-03-24 11:26:52 +00001132 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001133 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001134 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001135 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1136 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001137 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001138 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001139 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1140 ParmDecl.getIdentifierLoc(),
1141 Param,
1142 0));
Chris Lattnerff384912007-10-07 02:00:24 +00001143 }
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001145 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001146 // If attributes exist after the method, parse them.
David Blaikie4e4d0842012-03-11 07:00:24 +00001147 if (getLangOpts().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001148 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001149
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001150 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001151 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001152
Chris Lattnerff384912007-10-07 02:00:24 +00001153 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1154 &KeyIdents[0]);
Fariborz Jahanian10d65cd2012-09-11 21:27:45 +00001155 if (warnSelectorName) {
1156 if (warnHasNoName)
1157 Diag(mLoc, diag::warn_selector_with_bare_colon);
Fariborz Jahanian92faee72012-09-11 17:24:26 +00001158 Diag(mLoc, diag::note_missing_argument_name) << Sel.getAsString();
Fariborz Jahanian10d65cd2012-09-11 21:27:45 +00001159 }
Fariborz Jahanian92faee72012-09-11 17:24:26 +00001160
John McCalld226f652010-08-21 09:40:31 +00001161 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001162 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001163 mType, DSRet, ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001164 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001165 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001166 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001167 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001168
John McCall54abf7d2009-11-04 02:18:39 +00001169 PD.complete(Result);
1170 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001171}
1172
Steve Naroffdac269b2007-08-20 21:31:48 +00001173/// objc-protocol-refs:
1174/// '<' identifier-list '>'
1175///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001176bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001177ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1178 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001179 bool WarnOnDeclarations,
1180 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001181 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001183 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Chris Lattner5f9e2722011-07-23 10:55:15 +00001185 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Chris Lattnere13b9592008-07-26 04:03:38 +00001187 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001188 if (Tok.is(tok::code_completion)) {
1189 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1190 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001191 cutOffParsing();
1192 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001193 }
1194
Chris Lattnere13b9592008-07-26 04:03:38 +00001195 if (Tok.isNot(tok::identifier)) {
1196 Diag(Tok, diag::err_expected_ident);
1197 SkipUntil(tok::greater);
1198 return true;
1199 }
1200 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1201 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001202 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001203 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Chris Lattnere13b9592008-07-26 04:03:38 +00001205 if (Tok.isNot(tok::comma))
1206 break;
1207 ConsumeToken();
1208 }
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Chris Lattnere13b9592008-07-26 04:03:38 +00001210 // Consume the '>'.
1211 if (Tok.isNot(tok::greater)) {
1212 Diag(Tok, diag::err_expected_greater);
1213 return true;
1214 }
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001216 EndLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Chris Lattnere13b9592008-07-26 04:03:38 +00001218 // Convert the list of protocols identifiers into a list of protocol decls.
1219 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1220 &ProtocolIdents[0], ProtocolIdents.size(),
1221 Protocols);
1222 return false;
1223}
1224
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001225/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1226/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001227bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001228 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikie4e4d0842012-03-11 07:00:24 +00001229 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001230 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001231 SmallVector<Decl *, 8> ProtocolDecl;
1232 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001233 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1234 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001235 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1236 ProtocolLocs.data(), LAngleLoc);
1237 if (EndProtoLoc.isValid())
1238 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001239 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001240}
1241
1242
Steve Naroffdac269b2007-08-20 21:31:48 +00001243/// objc-class-instance-variables:
1244/// '{' objc-instance-variable-decl-list[opt] '}'
1245///
1246/// objc-instance-variable-decl-list:
1247/// objc-visibility-spec
1248/// objc-instance-variable-decl ';'
1249/// ';'
1250/// objc-instance-variable-decl-list objc-visibility-spec
1251/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1252/// objc-instance-variable-decl-list ';'
1253///
1254/// objc-visibility-spec:
1255/// @private
1256/// @protected
1257/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001258/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001259///
1260/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001261/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001262///
John McCalld226f652010-08-21 09:40:31 +00001263void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001264 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001265 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001266 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001267 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001268
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001269 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001270 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor72de6672009-01-08 20:45:30 +00001271
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001272 BalancedDelimiterTracker T(*this, tok::l_brace);
1273 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Steve Naroffddbff782007-08-21 21:17:12 +00001275 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001276 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001277 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Steve Naroffddbff782007-08-21 21:17:12 +00001279 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001280 if (Tok.is(tok::semi)) {
Richard Trieu4b0e6f12012-05-16 19:04:59 +00001281 ConsumeExtraSemi(InstanceVariableList);
Steve Naroffddbff782007-08-21 21:17:12 +00001282 continue;
1283 }
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Steve Naroffddbff782007-08-21 21:17:12 +00001285 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001286 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001287 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001288
1289 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001290 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001291 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001292 }
1293
Steve Naroff861cf3e2007-08-23 18:16:40 +00001294 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001295 case tok::objc_private:
1296 case tok::objc_public:
1297 case tok::objc_protected:
1298 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001299 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001300 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001301 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001302 default:
1303 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001304 continue;
1305 }
1306 }
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001308 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001309 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001310 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001311 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001312 }
1313
John McCallbdd563e2009-11-03 02:38:08 +00001314 struct ObjCIvarCallback : FieldCallback {
1315 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001316 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001317 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001318 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001319
John McCalld226f652010-08-21 09:40:31 +00001320 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001321 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001322 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1323 }
1324
Eli Friedmandcdff462012-08-08 23:53:27 +00001325 void invoke(ParsingFieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001326 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001327 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001328 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001329 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001330 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001331 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001332 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001333 if (Field)
1334 AllIvarDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00001335 FD.complete(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001336 }
1337 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001338
Chris Lattnere1359422008-04-10 06:46:29 +00001339 // Parse all the comma separated declarators.
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00001340 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001341 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001342
Chris Lattnerdf195262007-10-09 17:51:17 +00001343 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001344 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001345 } else {
1346 Diag(Tok, diag::err_expected_semi_decl_list);
1347 // Skip to end of block or statement
1348 SkipUntil(tok::r_brace, true, true);
1349 }
1350 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001351 T.consumeClose();
1352
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001353 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001354 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001355 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff8749be52007-10-31 22:11:35 +00001356 // Call ActOnFields() even if we don't have any decls. This is useful
1357 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001358 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie77b6de02011-09-22 02:58:26 +00001359 AllIvarDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001360 T.getOpenLocation(), T.getCloseLocation(), 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001361 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001362}
Steve Naroffdac269b2007-08-20 21:31:48 +00001363
1364/// objc-protocol-declaration:
1365/// objc-protocol-definition
1366/// objc-protocol-forward-reference
1367///
1368/// objc-protocol-definition:
James Dennett17d26a62012-06-11 06:19:40 +00001369/// \@protocol identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001370/// objc-protocol-refs[opt]
1371/// objc-interface-decl-list
James Dennett17d26a62012-06-11 06:19:40 +00001372/// \@end
Steve Naroffdac269b2007-08-20 21:31:48 +00001373///
1374/// objc-protocol-forward-reference:
James Dennett17d26a62012-06-11 06:19:40 +00001375/// \@protocol identifier-list ';'
Steve Naroffdac269b2007-08-20 21:31:48 +00001376///
James Dennett17d26a62012-06-11 06:19:40 +00001377/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001378/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001379/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001380Parser::DeclGroupPtrTy
1381Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1382 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001383 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001384 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1385 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Douglas Gregor083128f2009-11-18 04:49:41 +00001387 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001388 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001389 cutOffParsing();
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001390 return DeclGroupPtrTy();
Douglas Gregor083128f2009-11-18 04:49:41 +00001391 }
1392
Chris Lattnerdf195262007-10-09 17:51:17 +00001393 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001394 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001395 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001396 }
1397 // Save the protocol name, then consume it.
1398 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1399 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Chris Lattnerdf195262007-10-09 17:51:17 +00001401 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001402 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001403 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001404 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001405 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001406 }
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Erik Verbruggen90ec96f2011-12-08 09:58:43 +00001408 CheckNestedObjCContexts(AtLoc);
1409
Chris Lattnerdf195262007-10-09 17:51:17 +00001410 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001411 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001412 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1413
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001414 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001415 while (1) {
1416 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001417 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001418 Diag(Tok, diag::err_expected_ident);
1419 SkipUntil(tok::semi);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001420 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001421 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001422 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1423 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001424 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Chris Lattnerdf195262007-10-09 17:51:17 +00001426 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001427 break;
1428 }
1429 // Consume the ';'.
1430 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001431 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001432
Steve Naroffe440eb82007-10-10 17:32:04 +00001433 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001434 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001435 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001436 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001437 }
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001439 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001440 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001441
Chris Lattner5f9e2722011-07-23 10:55:15 +00001442 SmallVector<Decl *, 8> ProtocolRefs;
1443 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001444 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001445 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1446 LAngleLoc, EndProtoLoc))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001447 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001448
John McCalld226f652010-08-21 09:40:31 +00001449 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001450 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001451 ProtocolRefs.data(),
1452 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001453 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001454 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001455
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001456 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001457 return Actions.ConvertDeclToDeclGroup(ProtoType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001458}
Steve Naroffdac269b2007-08-20 21:31:48 +00001459
1460/// objc-implementation:
1461/// objc-class-implementation-prologue
1462/// objc-category-implementation-prologue
1463///
1464/// objc-class-implementation-prologue:
1465/// @implementation identifier objc-superclass[opt]
1466/// objc-class-instance-variables[opt]
1467///
1468/// objc-category-implementation-prologue:
1469/// @implementation identifier ( identifier )
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001470Parser::DeclGroupPtrTy
1471Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001472 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1473 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggend64251f2011-12-06 09:25:23 +00001474 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001475 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001477 // Code completion after '@implementation'.
1478 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001479 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001480 cutOffParsing();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001481 return DeclGroupPtrTy();
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001482 }
1483
Chris Lattnerdf195262007-10-09 17:51:17 +00001484 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001485 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001486 return DeclGroupPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001487 }
1488 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001489 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001490 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001491 Decl *ObjCImpDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001492
1493 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001494 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001495 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001496 SourceLocation categoryLoc, rparenLoc;
1497 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001499 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001500 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001501 cutOffParsing();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001502 return DeclGroupPtrTy();
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001503 }
1504
Chris Lattnerdf195262007-10-09 17:51:17 +00001505 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001506 categoryId = Tok.getIdentifierInfo();
1507 categoryLoc = ConsumeToken();
1508 } else {
1509 Diag(Tok, diag::err_expected_ident); // missing category name.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001510 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001511 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001512 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001513 Diag(Tok, diag::err_expected_rparen);
1514 SkipUntil(tok::r_paren, false); // don't stop at ';'
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001515 return DeclGroupPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001516 }
1517 rparenLoc = ConsumeParen();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001518 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001519 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001520 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001521
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001522 } else {
1523 // We have a class implementation
1524 SourceLocation superClassLoc;
1525 IdentifierInfo *superClassId = 0;
1526 if (Tok.is(tok::colon)) {
1527 // We have a super class
1528 ConsumeToken();
1529 if (Tok.isNot(tok::identifier)) {
1530 Diag(Tok, diag::err_expected_ident); // missing super class name.
1531 return DeclGroupPtrTy();
1532 }
1533 superClassId = Tok.getIdentifierInfo();
1534 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001535 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001536 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1537 AtLoc, nameId, nameLoc,
1538 superClassId, superClassLoc);
1539
1540 if (Tok.is(tok::l_brace)) // we have ivars
1541 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001542 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001543 assert(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001545 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001546
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001547 {
1548 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
1549 while (!ObjCImplParsing.isFinished() && Tok.isNot(tok::eof)) {
1550 ParsedAttributesWithRange attrs(AttrFactory);
1551 MaybeParseCXX0XAttributes(attrs);
1552 MaybeParseMicrosoftAttributes(attrs);
1553 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1554 DeclGroupRef DG = DGP.get();
1555 DeclsInGroup.append(DG.begin(), DG.end());
1556 }
1557 }
1558 }
1559
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001560 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +00001561}
Steve Naroff60fccee2007-10-29 21:38:07 +00001562
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001563Parser::DeclGroupPtrTy
1564Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001565 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1566 "ParseObjCAtEndDeclaration(): Expected @end");
1567 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001568 if (CurParsedObjCImpl)
1569 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001570 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001571 // missing @implementation
Erik Verbruggend64251f2011-12-06 09:25:23 +00001572 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001573 return DeclGroupPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +00001574}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001575
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001576Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1577 if (!Finished) {
1578 finish(P.Tok.getLocation());
1579 if (P.Tok.is(tok::eof)) {
1580 P.Diag(P.Tok, diag::err_objc_missing_end)
1581 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1582 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1583 << Sema::OCK_Implementation;
1584 }
1585 }
1586 P.CurParsedObjCImpl = 0;
1587 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001588}
1589
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001590void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1591 assert(!Finished);
1592 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1593 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001594 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1595 true/*Methods*/);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001596
1597 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1598
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001599 if (HasCFunction)
1600 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1601 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1602 false/*c-functions*/);
1603
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001604 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001605 for (LateParsedObjCMethodContainer::iterator
1606 I = LateParsedObjCMethods.begin(),
1607 E = LateParsedObjCMethods.end(); I != E; ++I)
1608 delete *I;
1609 LateParsedObjCMethods.clear();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001610
1611 Finished = true;
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001612}
1613
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001614/// compatibility-alias-decl:
1615/// @compatibility_alias alias-name class-name ';'
1616///
John McCalld226f652010-08-21 09:40:31 +00001617Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001618 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1619 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1620 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001621 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001622 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001623 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001624 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001625 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1626 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001627 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001628 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001629 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001630 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001631 IdentifierInfo *classId = Tok.getIdentifierInfo();
1632 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001633 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1634 "@compatibility_alias");
Richard Smithde01b7a2012-08-08 23:32:13 +00001635 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1636 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001637}
1638
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001639/// property-synthesis:
1640/// @synthesize property-ivar-list ';'
1641///
1642/// property-ivar-list:
1643/// property-ivar
1644/// property-ivar-list ',' property-ivar
1645///
1646/// property-ivar:
1647/// identifier
1648/// identifier '=' identifier
1649///
John McCalld226f652010-08-21 09:40:31 +00001650Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001651 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1652 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001653 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Douglas Gregorb328c422009-11-18 19:45:45 +00001655 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001656 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001657 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001658 cutOffParsing();
1659 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001660 }
1661
Douglas Gregorb328c422009-11-18 19:45:45 +00001662 if (Tok.isNot(tok::identifier)) {
1663 Diag(Tok, diag::err_synthesized_property_name);
1664 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001665 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001666 }
1667
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001668 IdentifierInfo *propertyIvar = 0;
1669 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1670 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001671 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001672 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001673 // property '=' ivar-name
1674 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001675
1676 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001677 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001678 cutOffParsing();
1679 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001680 }
1681
Chris Lattnerdf195262007-10-09 17:51:17 +00001682 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001683 Diag(Tok, diag::err_expected_ident);
1684 break;
1685 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001686 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001687 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001688 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001689 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001690 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001691 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001692 break;
1693 ConsumeToken(); // consume ','
1694 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001695 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001696 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001697}
1698
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001699/// property-dynamic:
1700/// @dynamic property-list
1701///
1702/// property-list:
1703/// identifier
1704/// property-list ',' identifier
1705///
John McCalld226f652010-08-21 09:40:31 +00001706Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001707 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1708 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001709 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001710 while (true) {
1711 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001712 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001713 cutOffParsing();
1714 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001715 }
1716
1717 if (Tok.isNot(tok::identifier)) {
1718 Diag(Tok, diag::err_expected_ident);
1719 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001720 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001721 }
1722
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001723 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1724 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001725 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001726 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001727
Chris Lattnerdf195262007-10-09 17:51:17 +00001728 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001729 break;
1730 ConsumeToken(); // consume ','
1731 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001732 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001733 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001734}
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001736/// objc-throw-statement:
1737/// throw expression[opt];
1738///
John McCall60d7b3a2010-08-24 06:29:42 +00001739StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1740 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001741 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001742 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001743 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001744 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001745 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001746 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001747 }
1748 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001749 // consume ';'
1750 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001751 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001752}
1753
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001754/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001755/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001756///
John McCall60d7b3a2010-08-24 06:29:42 +00001757StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001758Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001759 ConsumeToken(); // consume synchronized
1760 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001761 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001762 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001763 }
John McCall07524032011-07-27 21:50:02 +00001764
1765 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001766 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001767 ExprResult operand(ParseExpression());
1768
1769 if (Tok.is(tok::r_paren)) {
1770 ConsumeParen(); // ')'
1771 } else {
1772 if (!operand.isInvalid())
1773 Diag(Tok, diag::err_expected_rparen);
1774
1775 // Skip forward until we see a left brace, but don't consume it.
1776 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001777 }
John McCall07524032011-07-27 21:50:02 +00001778
1779 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001780 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001781 if (!operand.isInvalid())
1782 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001783 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001784 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001785
John McCall07524032011-07-27 21:50:02 +00001786 // Check the @synchronized operand now.
1787 if (!operand.isInvalid())
1788 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001789
John McCall07524032011-07-27 21:50:02 +00001790 // Parse the compound statement within a new scope.
1791 ParseScope bodyScope(this, Scope::DeclScope);
1792 StmtResult body(ParseCompoundStatementBody());
1793 bodyScope.Exit();
1794
1795 // If there was a semantic or parse error earlier with the
1796 // operand, fail now.
1797 if (operand.isInvalid())
1798 return StmtError();
1799
1800 if (body.isInvalid())
1801 body = Actions.ActOnNullStmt(Tok.getLocation());
1802
1803 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001804}
1805
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001806/// objc-try-catch-statement:
1807/// @try compound-statement objc-catch-list[opt]
1808/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1809///
1810/// objc-catch-list:
1811/// @catch ( parameter-declaration ) compound-statement
1812/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1813/// catch-parameter-declaration:
1814/// parameter-declaration
1815/// '...' [OBJC2]
1816///
John McCall60d7b3a2010-08-24 06:29:42 +00001817StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001818 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001819
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001820 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001821 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001822 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001823 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001824 }
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001825 StmtVector CatchStmts;
John McCall60d7b3a2010-08-24 06:29:42 +00001826 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001827 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001828 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001829 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001830 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001831 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001832
Chris Lattnerdf195262007-10-09 17:51:17 +00001833 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001834 // At this point, we need to lookahead to determine if this @ is the start
1835 // of an @catch or @finally. We don't want to consume the @ token if this
1836 // is an @try or @encode or something else.
1837 Token AfterAt = GetLookAheadToken(1);
1838 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1839 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1840 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001841
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001842 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001843 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001844 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001845 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001846 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001847 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001848 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001849 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001850 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001851 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001852 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001853 ParseDeclarator(ParmDecl);
1854
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001855 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001856 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001857 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001858 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001859 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Steve Naroff93a25952009-04-07 22:56:58 +00001861 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Steve Naroff93a25952009-04-07 22:56:58 +00001863 if (Tok.is(tok::r_paren))
1864 RParenLoc = ConsumeParen();
1865 else // Skip over garbage, until we get to ')'. Eat the ')'.
1866 SkipUntil(tok::r_paren, true, false);
1867
John McCall60d7b3a2010-08-24 06:29:42 +00001868 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001869 if (Tok.is(tok::l_brace))
1870 CatchBody = ParseCompoundStatementBody();
1871 else
1872 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001873 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001874 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001875
John McCall60d7b3a2010-08-24 06:29:42 +00001876 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001877 RParenLoc,
1878 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001879 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001880 if (!Catch.isInvalid())
1881 CatchStmts.push_back(Catch.release());
1882
Steve Naroff64515f32008-02-05 21:27:35 +00001883 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001884 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1885 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001886 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001887 }
1888 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001889 } else {
1890 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001891 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001892 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001893
John McCall60d7b3a2010-08-24 06:29:42 +00001894 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001895 if (Tok.is(tok::l_brace))
1896 FinallyBody = ParseCompoundStatementBody();
1897 else
1898 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001899 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001900 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001901 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001902 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001903 catch_or_finally_seen = true;
1904 break;
1905 }
1906 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001907 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001908 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001909 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001910 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001911
John McCall9ae2f072010-08-23 23:25:46 +00001912 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001913 CatchStmts,
John McCall9ae2f072010-08-23 23:25:46 +00001914 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001915}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001916
John McCallf85e1932011-06-15 23:02:42 +00001917/// objc-autoreleasepool-statement:
1918/// @autoreleasepool compound-statement
1919///
1920StmtResult
1921Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1922 ConsumeToken(); // consume autoreleasepool
1923 if (Tok.isNot(tok::l_brace)) {
1924 Diag(Tok, diag::err_expected_lbrace);
1925 return StmtError();
1926 }
1927 // Enter a scope to hold everything within the compound stmt. Compound
1928 // statements can always hold declarations.
1929 ParseScope BodyScope(this, Scope::DeclScope);
1930
1931 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1932
1933 BodyScope.Exit();
1934 if (AutoreleasePoolBody.isInvalid())
1935 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1936 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1937 AutoreleasePoolBody.take());
1938}
1939
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001940/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
1941/// for later parsing.
1942void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
1943 LexedMethod* LM = new LexedMethod(this, MDecl);
1944 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
1945 CachedTokens &Toks = LM->Toks;
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00001946 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001947 Toks.push_back(Tok);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00001948 if (Tok.is(tok::kw_try)) {
1949 ConsumeToken();
Fariborz Jahaniandbd69452012-08-10 20:34:17 +00001950 if (Tok.is(tok::colon)) {
1951 Toks.push_back(Tok);
1952 ConsumeToken();
1953 while (Tok.isNot(tok::l_brace)) {
1954 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1955 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1956 }
1957 }
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00001958 Toks.push_back(Tok); // also store '{'
1959 }
1960 else if (Tok.is(tok::colon)) {
1961 ConsumeToken();
1962 while (Tok.isNot(tok::l_brace)) {
1963 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1964 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1965 }
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00001966 Toks.push_back(Tok); // also store '{'
1967 }
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001968 ConsumeBrace();
1969 // Consume everything up to (and including) the matching right brace.
1970 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00001971 while (Tok.is(tok::kw_catch)) {
1972 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
1973 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1974 }
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001975}
1976
Steve Naroff3536b442007-09-06 21:24:23 +00001977/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001978///
John McCalld226f652010-08-21 09:40:31 +00001979Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001980 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001981
John McCallf312b1e2010-08-26 23:41:50 +00001982 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1983 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001984
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001985 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001986 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001987 if (CurParsedObjCImpl) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001988 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001989 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001990 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001991 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001992 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001993
Steve Naroff409be832007-11-11 19:54:21 +00001994 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001995 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001996 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001997
Steve Naroff409be832007-11-11 19:54:21 +00001998 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1999 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00002000
Steve Naroff409be832007-11-11 19:54:21 +00002001 // If we didn't find the '{', bail out.
2002 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00002003 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00002004 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002005
2006 if (!MDecl) {
2007 ConsumeBrace();
2008 SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
2009 return 0;
2010 }
2011
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002012 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002013 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahanianc9b97092012-08-09 17:15:00 +00002014 assert (CurParsedObjCImpl
2015 && "ParseObjCMethodDefinition - Method out of @implementation");
2016 // Consume the tokens and store them for later parsing.
2017 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff71c0a952007-11-13 23:01:27 +00002018 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00002019}
Anders Carlsson55085182007-08-21 17:43:55 +00002020
John McCall60d7b3a2010-08-24 06:29:42 +00002021StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002022 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002023 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002024 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002025 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00002026 }
2027
2028 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00002029 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002030
2031 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00002032 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002033
2034 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00002035 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00002036
2037 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2038 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00002039
John McCall60d7b3a2010-08-24 06:29:42 +00002040 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002041 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00002042 // If the expression is invalid, skip ahead to the next semicolon. Not
2043 // doing this opens us up to the possibility of infinite loops if
2044 // ParseExpression does not consume any tokens.
2045 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00002046 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00002047 }
Chris Lattner5d803162009-12-07 16:33:19 +00002048
Steve Naroff64515f32008-02-05 21:27:35 +00002049 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00002050 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00002051 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00002052}
2053
John McCall60d7b3a2010-08-24 06:29:42 +00002054ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00002055 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002056 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00002057 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002058 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002059 return ExprError();
2060
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002061 case tok::minus:
2062 case tok::plus: {
2063 tok::TokenKind Kind = Tok.getKind();
2064 SourceLocation OpLoc = ConsumeToken();
2065
2066 if (!Tok.is(tok::numeric_constant)) {
2067 const char *Symbol = 0;
2068 switch (Kind) {
2069 case tok::minus: Symbol = "-"; break;
2070 case tok::plus: Symbol = "+"; break;
2071 default: llvm_unreachable("missing unary operator case");
2072 }
2073 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2074 << Symbol;
2075 return ExprError();
2076 }
2077
2078 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2079 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002080 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002081 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002082 ConsumeToken(); // Consume the literal token.
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002083
2084 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.take());
2085 if (Lit.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002086 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002087
2088 return ParsePostfixExpressionSuffix(
2089 Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2090 }
2091
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002092 case tok::string_literal: // primary-expression: string-literal
2093 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00002094 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002095
2096 case tok::char_constant:
2097 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2098
2099 case tok::numeric_constant:
2100 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2101
2102 case tok::kw_true: // Objective-C++, etc.
2103 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2104 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2105 case tok::kw_false: // Objective-C++, etc.
2106 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2107 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2108
2109 case tok::l_square:
2110 // Objective-C array literal
2111 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2112
2113 case tok::l_brace:
2114 // Objective-C dictionary literal
2115 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2116
Patrick Beardeb382ec2012-04-19 00:25:12 +00002117 case tok::l_paren:
2118 // Objective-C boxed expression
2119 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2120
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002121 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00002122 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00002123 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002124
Chris Lattner4fef81d2008-08-05 06:19:09 +00002125 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2126 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00002127 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002128 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00002129 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002130 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00002131 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian6749ae12012-07-09 20:00:35 +00002132 default: {
2133 const char *str = 0;
2134 if (GetLookAheadToken(1).is(tok::l_brace)) {
2135 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2136 str =
2137 ch == 't' ? "try"
2138 : (ch == 'f' ? "finally"
2139 : (ch == 'a' ? "autoreleasepool" : 0));
2140 }
2141 if (str) {
2142 SourceLocation kwLoc = Tok.getLocation();
2143 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2144 FixItHint::CreateReplacement(kwLoc, str));
2145 }
2146 else
2147 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2148 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00002149 }
Anders Carlsson55085182007-08-21 17:43:55 +00002150 }
Anders Carlsson55085182007-08-21 17:43:55 +00002151}
2152
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002153/// \brirg Parse the receiver of an Objective-C++ message send.
2154///
2155/// This routine parses the receiver of a message send in
2156/// Objective-C++ either as a type or as an expression. Note that this
2157/// routine must not be called to parse a send to 'super', since it
2158/// has no way to return such a result.
2159///
2160/// \param IsExpr Whether the receiver was parsed as an expression.
2161///
2162/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2163/// IsExpr is true), the parsed expression. If the receiver was parsed
2164/// as a type (\c IsExpr is false), the parsed type.
2165///
2166/// \returns True if an error occurred during parsing or semantic
2167/// analysis, in which case the arguments do not have valid
2168/// values. Otherwise, returns false for a successful parse.
2169///
2170/// objc-receiver: [C++]
2171/// 'super' [not parsed here]
2172/// expression
2173/// simple-type-specifier
2174/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002175bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002176 InMessageExpressionRAIIObject InMessage(*this, true);
2177
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002178 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2179 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2180 TryAnnotateTypeOrScopeToken();
2181
Kaelyn Uhrain7bf33402012-06-15 23:45:51 +00002182 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002183 // objc-receiver:
2184 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00002185 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002186 if (Receiver.isInvalid())
2187 return true;
2188
2189 IsExpr = true;
2190 TypeOrExpr = Receiver.take();
2191 return false;
2192 }
2193
2194 // objc-receiver:
2195 // typename-specifier
2196 // simple-type-specifier
2197 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00002198 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002199 ParseCXXSimpleTypeSpecifier(DS);
2200
2201 if (Tok.is(tok::l_paren)) {
2202 // If we see an opening parentheses at this point, we are
2203 // actually parsing an expression that starts with a
2204 // function-style cast, e.g.,
2205 //
2206 // postfix-expression:
2207 // simple-type-specifier ( expression-list [opt] )
2208 // typename-specifier ( expression-list [opt] )
2209 //
2210 // Parse the remainder of this case, then the (optional)
2211 // postfix-expression suffix, followed by the (optional)
2212 // right-hand side of the binary expression. We have an
2213 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002214 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002215 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002216 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002217 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002218 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002219 if (Receiver.isInvalid())
2220 return true;
2221
2222 IsExpr = true;
2223 TypeOrExpr = Receiver.take();
2224 return false;
2225 }
2226
2227 // We have a class message. Turn the simple-type-specifier or
2228 // typename-specifier we parsed into a type and parse the
2229 // remainder of the class message.
2230 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002231 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002232 if (Type.isInvalid())
2233 return true;
2234
2235 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002236 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002237 return false;
2238}
2239
Douglas Gregor1b730e82010-05-31 14:40:22 +00002240/// \brief Determine whether the parser is currently referring to a an
2241/// Objective-C message send, using a simplified heuristic to avoid overhead.
2242///
2243/// This routine will only return true for a subset of valid message-send
2244/// expressions.
2245bool Parser::isSimpleObjCMessageExpression() {
David Blaikie4e4d0842012-03-11 07:00:24 +00002246 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002247 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002248 return GetLookAheadToken(1).is(tok::identifier) &&
2249 GetLookAheadToken(2).is(tok::identifier);
2250}
2251
Douglas Gregor9497a732010-09-16 01:51:54 +00002252bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikie4e4d0842012-03-11 07:00:24 +00002253 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregor9497a732010-09-16 01:51:54 +00002254 InMessageExpression)
2255 return false;
2256
2257
2258 ParsedType Type;
2259
2260 if (Tok.is(tok::annot_typename))
2261 Type = getTypeAnnotation(Tok);
2262 else if (Tok.is(tok::identifier))
2263 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2264 getCurScope());
2265 else
2266 return false;
2267
2268 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2269 const Token &AfterNext = GetLookAheadToken(2);
2270 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2271 if (Tok.is(tok::identifier))
2272 TryAnnotateTypeOrScopeToken();
2273
2274 return Tok.is(tok::annot_typename);
2275 }
2276 }
2277
2278 return false;
2279}
2280
Mike Stump1eb44332009-09-09 15:08:12 +00002281/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002282/// '[' objc-receiver objc-message-args ']'
2283///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002284/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002285/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002286/// expression
2287/// class-name
2288/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002289///
John McCall60d7b3a2010-08-24 06:29:42 +00002290ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002291 assert(Tok.is(tok::l_square) && "'[' expected");
2292 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2293
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002294 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002295 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002296 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002297 return ExprError();
2298 }
2299
Douglas Gregor0fbda682010-09-15 14:51:05 +00002300 InMessageExpressionRAIIObject InMessage(*this, true);
2301
David Blaikie4e4d0842012-03-11 07:00:24 +00002302 if (getLangOpts().CPlusPlus) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002303 // We completely separate the C and C++ cases because C++ requires
2304 // more complicated (read: slower) parsing.
2305
2306 // Handle send to super.
2307 // FIXME: This doesn't benefit from the same typo-correction we
2308 // get in Objective-C.
2309 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002310 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002311 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2312 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002313
2314 // Parse the receiver, which is either a type or an expression.
2315 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002316 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002317 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2318 SkipUntil(tok::r_square);
2319 return ExprError();
2320 }
2321
2322 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002323 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2324 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002325 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002326
2327 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002328 ParsedType::getFromOpaquePtr(TypeOrExpr),
2329 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002330 }
2331
2332 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002333 IdentifierInfo *Name = Tok.getIdentifierInfo();
2334 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002335 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002336 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002337 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002338 NextToken().is(tok::period),
2339 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002340 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002341 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2342 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002343
John McCallf312b1e2010-08-26 23:41:50 +00002344 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002345 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002346 SkipUntil(tok::r_square);
2347 return ExprError();
2348 }
2349
Douglas Gregor1569f952010-04-21 20:38:13 +00002350 ConsumeToken(); // the type name
2351
2352 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002353 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002354
John McCallf312b1e2010-08-26 23:41:50 +00002355 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002356 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002357 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002358 }
Chris Lattner699b6612008-01-25 18:59:06 +00002359 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002360
2361 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002362 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002363 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002364 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002365 return Res;
Chris Lattner699b6612008-01-25 18:59:06 +00002366 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002367
John McCallb3d87482010-08-24 05:47:05 +00002368 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2369 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002370}
Sebastian Redl1d922962008-12-13 15:32:12 +00002371
Douglas Gregor2725ca82010-04-21 19:57:20 +00002372/// \brief Parse the remainder of an Objective-C message following the
2373/// '[' objc-receiver.
2374///
2375/// This routine handles sends to super, class messages (sent to a
2376/// class name), and instance messages (sent to an object), and the
2377/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2378/// ReceiverExpr, respectively. Only one of these parameters may have
2379/// a valid value.
2380///
2381/// \param LBracLoc The location of the opening '['.
2382///
2383/// \param SuperLoc If this is a send to 'super', the location of the
2384/// 'super' keyword that indicates a send to the superclass.
2385///
2386/// \param ReceiverType If this is a class message, the type of the
2387/// class we are sending a message to.
2388///
2389/// \param ReceiverExpr If this is an instance message, the expression
2390/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002391///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002392/// objc-message-args:
2393/// objc-selector
2394/// objc-keywordarg-list
2395///
2396/// objc-keywordarg-list:
2397/// objc-keywordarg
2398/// objc-keywordarg-list objc-keywordarg
2399///
Mike Stump1eb44332009-09-09 15:08:12 +00002400/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002401/// selector-name[opt] ':' objc-keywordexpr
2402///
2403/// objc-keywordexpr:
2404/// nonempty-expr-list
2405///
2406/// nonempty-expr-list:
2407/// assignment-expression
2408/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002409///
John McCall60d7b3a2010-08-24 06:29:42 +00002410ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002411Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002412 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002413 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002414 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002415 InMessageExpressionRAIIObject InMessage(*this, true);
2416
Steve Naroffc4df6d22009-11-07 02:08:14 +00002417 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002418 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002419 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2420 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002421 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002422 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2423 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002424 else
John McCall9ae2f072010-08-23 23:25:46 +00002425 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002426 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002427 cutOffParsing();
2428 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002429 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002430
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002431 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002432 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002433 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002434
Chris Lattner5f9e2722011-07-23 10:55:15 +00002435 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002436 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002437 ExprVector KeyExprs;
Steve Naroff68d331a2007-09-27 14:38:14 +00002438
Chris Lattnerdf195262007-10-09 17:51:17 +00002439 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002440 while (1) {
2441 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002442 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002443 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002444
Chris Lattnerdf195262007-10-09 17:51:17 +00002445 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002446 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002447 // We must manually skip to a ']', otherwise the expression skipper will
2448 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2449 // the enclosing expression.
2450 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002451 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002452 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002453
Steve Naroff68d331a2007-09-27 14:38:14 +00002454 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002455 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002456
2457 if (Tok.is(tok::code_completion)) {
2458 if (SuperLoc.isValid())
2459 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2460 KeyIdents.data(),
2461 KeyIdents.size(),
2462 /*AtArgumentEpression=*/true);
2463 else if (ReceiverType)
2464 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2465 KeyIdents.data(),
2466 KeyIdents.size(),
2467 /*AtArgumentEpression=*/true);
2468 else
2469 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2470 KeyIdents.data(),
2471 KeyIdents.size(),
2472 /*AtArgumentEpression=*/true);
2473
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002474 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002475 return ExprError();
2476 }
2477
John McCall60d7b3a2010-08-24 06:29:42 +00002478 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002479 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002480 // We must manually skip to a ']', otherwise the expression skipper will
2481 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2482 // the enclosing expression.
2483 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002484 return Res;
Steve Naroff37387c92007-09-17 20:25:27 +00002485 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002486
Steve Naroff37387c92007-09-17 20:25:27 +00002487 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002488 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002489
Douglas Gregord3c68542009-11-19 01:08:35 +00002490 // Code completion after each argument.
2491 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002492 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002493 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002494 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002495 KeyIdents.size(),
2496 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002497 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002498 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002499 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002500 KeyIdents.size(),
2501 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002502 else
John McCall9ae2f072010-08-23 23:25:46 +00002503 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002504 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002505 KeyIdents.size(),
2506 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002507 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002508 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002509 }
2510
Steve Naroff37387c92007-09-17 20:25:27 +00002511 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002512 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002513 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002514 break;
2515 // We have a selector or a colon, continue parsing.
2516 }
2517 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002518 while (Tok.is(tok::comma)) {
Fariborz Jahanian18df0eb2012-05-21 22:43:44 +00002519 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002520 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002521 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002522 if (Res.isInvalid()) {
Fariborz Jahanian18df0eb2012-05-21 22:43:44 +00002523 if (Tok.is(tok::colon)) {
2524 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
2525 FixItHint::CreateRemoval(commaLoc);
2526 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00002527 // We must manually skip to a ']', otherwise the expression skipper will
2528 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2529 // the enclosing expression.
2530 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002531 return Res;
Steve Naroff49f109c2007-11-15 13:05:42 +00002532 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002533
Steve Naroff49f109c2007-11-15 13:05:42 +00002534 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002535 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002536 }
2537 } else if (!selIdent) {
2538 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002539
Chris Lattner4fef81d2008-08-05 06:19:09 +00002540 // We must manually skip to a ']', otherwise the expression skipper will
2541 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2542 // the enclosing expression.
2543 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002544 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002545 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002546
Chris Lattnerdf195262007-10-09 17:51:17 +00002547 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002548 if (Tok.is(tok::identifier))
2549 Diag(Tok, diag::err_expected_colon);
2550 else
2551 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002552 // We must manually skip to a ']', otherwise the expression skipper will
2553 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2554 // the enclosing expression.
2555 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002556 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002557 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002558
Chris Lattner699b6612008-01-25 18:59:06 +00002559 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002560
Steve Naroff29238a02007-10-05 18:42:47 +00002561 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002562 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002563 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002564 KeyLocs.push_back(Loc);
2565 }
Chris Lattnerff384912007-10-07 02:00:24 +00002566 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002567
Douglas Gregor2725ca82010-04-21 19:57:20 +00002568 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002569 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002570 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002571 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002572 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002573 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCall9ae2f072010-08-23 23:25:46 +00002574 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002575 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002576}
2577
John McCall60d7b3a2010-08-24 06:29:42 +00002578ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2579 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002580 if (Res.isInvalid()) return Res;
Sebastian Redl1d922962008-12-13 15:32:12 +00002581
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002582 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2583 // expressions. At this point, we know that the only valid thing that starts
2584 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002585 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002586 ExprVector AtStrings;
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002587 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002588 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002589
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002590 while (Tok.is(tok::at)) {
2591 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002592
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002593 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002594 if (!isTokenStringLiteral())
2595 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002596
John McCall60d7b3a2010-08-24 06:29:42 +00002597 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002598 if (Lit.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002599 return Lit;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002600
Sebastian Redleffa8d12008-12-10 00:02:53 +00002601 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002602 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002603
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002604 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
Sebastian Redl1d922962008-12-13 15:32:12 +00002605 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002606}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002607
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002608/// ParseObjCBooleanLiteral -
2609/// objc-scalar-literal : '@' boolean-keyword
2610/// ;
2611/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2612/// ;
2613ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2614 bool ArgValue) {
2615 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2616 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2617}
2618
2619/// ParseObjCCharacterLiteral -
2620/// objc-scalar-literal : '@' character-literal
2621/// ;
2622ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2623 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2624 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002625 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002626 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002627 ConsumeToken(); // Consume the literal token.
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002628 return Owned(Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2629}
2630
2631/// ParseObjCNumericLiteral -
2632/// objc-scalar-literal : '@' scalar-literal
2633/// ;
2634/// scalar-literal : | numeric-constant /* any numeric constant. */
2635/// ;
2636ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2637 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2638 if (Lit.isInvalid()) {
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002639 return Lit;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002640 }
Benjamin Kramer8b8d9532012-03-07 00:14:40 +00002641 ConsumeToken(); // Consume the literal token.
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002642 return Owned(Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2643}
2644
Patrick Beardeb382ec2012-04-19 00:25:12 +00002645/// ParseObjCBoxedExpr -
2646/// objc-box-expression:
2647/// @( assignment-expression )
2648ExprResult
2649Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2650 if (Tok.isNot(tok::l_paren))
2651 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2652
2653 BalancedDelimiterTracker T(*this, tok::l_paren);
2654 T.consumeOpen();
2655 ExprResult ValueExpr(ParseAssignmentExpression());
2656 if (T.consumeClose())
2657 return ExprError();
Argyrios Kyrtzidisedd27602012-05-10 20:02:36 +00002658
2659 if (ValueExpr.isInvalid())
2660 return ExprError();
2661
Patrick Beardeb382ec2012-04-19 00:25:12 +00002662 // Wrap the sub-expression in a parenthesized expression, to distinguish
2663 // a boxed expression from a literal.
2664 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
2665 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.take());
2666 return Owned(Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
2667 ValueExpr.take()));
2668}
2669
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002670ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002671 ExprVector ElementExprs; // array elements.
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002672 ConsumeBracket(); // consume the l_square.
2673
2674 while (Tok.isNot(tok::r_square)) {
2675 // Parse list of array element expressions (all must be id types).
2676 ExprResult Res(ParseAssignmentExpression());
2677 if (Res.isInvalid()) {
2678 // We must manually skip to a ']', otherwise the expression skipper will
2679 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2680 // the enclosing expression.
2681 SkipUntil(tok::r_square);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002682 return Res;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002683 }
2684
2685 // Parse the ellipsis that indicates a pack expansion.
2686 if (Tok.is(tok::ellipsis))
2687 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2688 if (Res.isInvalid())
2689 return true;
2690
2691 ElementExprs.push_back(Res.release());
2692
2693 if (Tok.is(tok::comma))
2694 ConsumeToken(); // Eat the ','.
2695 else if (Tok.isNot(tok::r_square))
2696 return ExprError(Diag(Tok, diag::err_expected_rsquare_or_comma));
2697 }
2698 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002699 MultiExprArg Args(ElementExprs);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002700 return Owned(Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args));
2701}
2702
2703ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2704 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2705 ConsumeBrace(); // consume the l_square.
2706 while (Tok.isNot(tok::r_brace)) {
2707 // Parse the comma separated key : value expressions.
2708 ExprResult KeyExpr;
2709 {
2710 ColonProtectionRAIIObject X(*this);
2711 KeyExpr = ParseAssignmentExpression();
2712 if (KeyExpr.isInvalid()) {
2713 // We must manually skip to a '}', otherwise the expression skipper will
2714 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2715 // the enclosing expression.
2716 SkipUntil(tok::r_brace);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002717 return KeyExpr;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002718 }
2719 }
2720
2721 if (Tok.is(tok::colon)) {
2722 ConsumeToken();
2723 } else {
2724 return ExprError(Diag(Tok, diag::err_expected_colon));
2725 }
2726
2727 ExprResult ValueExpr(ParseAssignmentExpression());
2728 if (ValueExpr.isInvalid()) {
2729 // We must manually skip to a '}', otherwise the expression skipper will
2730 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2731 // the enclosing expression.
2732 SkipUntil(tok::r_brace);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002733 return ValueExpr;
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002734 }
2735
2736 // Parse the ellipsis that designates this as a pack expansion.
2737 SourceLocation EllipsisLoc;
David Blaikie4e4d0842012-03-11 07:00:24 +00002738 if (Tok.is(tok::ellipsis) && getLangOpts().CPlusPlus)
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002739 EllipsisLoc = ConsumeToken();
2740
2741 // We have a valid expression. Collect it in a vector so we can
2742 // build the argument list.
2743 ObjCDictionaryElement Element = {
2744 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, llvm::Optional<unsigned>()
2745 };
2746 Elements.push_back(Element);
2747
2748 if (Tok.is(tok::comma))
2749 ConsumeToken(); // Eat the ','.
2750 else if (Tok.isNot(tok::r_brace))
2751 return ExprError(Diag(Tok, diag::err_expected_rbrace_or_comma));
2752 }
2753 SourceLocation EndLoc = ConsumeBrace();
2754
2755 // Create the ObjCDictionaryLiteral.
2756 return Owned(Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2757 Elements.data(),
2758 Elements.size()));
2759}
2760
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002761/// objc-encode-expression:
2762/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002763ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002764Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002765 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002766
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002767 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002768
Chris Lattner4fef81d2008-08-05 06:19:09 +00002769 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002770 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2771
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002772 BalancedDelimiterTracker T(*this, tok::l_paren);
2773 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002774
Douglas Gregor809070a2009-02-18 17:45:20 +00002775 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002776
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002777 T.consumeClose();
Sebastian Redl1d922962008-12-13 15:32:12 +00002778
Douglas Gregor809070a2009-02-18 17:45:20 +00002779 if (Ty.isInvalid())
2780 return ExprError();
2781
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002782 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
2783 T.getOpenLocation(), Ty.get(),
2784 T.getCloseLocation()));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002785}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002786
2787/// objc-protocol-expression
James Dennett17d26a62012-06-11 06:19:40 +00002788/// \@protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002789ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002790Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002791 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002792
Chris Lattner4fef81d2008-08-05 06:19:09 +00002793 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002794 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2795
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002796 BalancedDelimiterTracker T(*this, tok::l_paren);
2797 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002798
Chris Lattner4fef81d2008-08-05 06:19:09 +00002799 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002800 return ExprError(Diag(Tok, diag::err_expected_ident));
2801
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002802 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidis7d24e282012-05-16 00:50:02 +00002803 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002804
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002805 T.consumeClose();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002806
Sebastian Redl1d922962008-12-13 15:32:12 +00002807 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002808 T.getOpenLocation(),
Argyrios Kyrtzidis7d24e282012-05-16 00:50:02 +00002809 ProtoIdLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002810 T.getCloseLocation()));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002811}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002812
2813/// objc-selector-expression
2814/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002815ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002816 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002817
Chris Lattner4fef81d2008-08-05 06:19:09 +00002818 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002819 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2820
Chris Lattner5f9e2722011-07-23 10:55:15 +00002821 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002822 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002823
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002824 BalancedDelimiterTracker T(*this, tok::l_paren);
2825 T.consumeOpen();
2826
Douglas Gregor458433d2010-08-26 15:07:07 +00002827 if (Tok.is(tok::code_completion)) {
2828 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2829 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002830 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002831 return ExprError();
2832 }
2833
Chris Lattner2fc5c242009-04-11 18:13:45 +00002834 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002835 if (!SelIdent && // missing selector name.
2836 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002837 return ExprError(Diag(Tok, diag::err_expected_ident));
2838
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002839 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002840 unsigned nColons = 0;
2841 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002842 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002843 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2844 ++nColons;
2845 KeyIdents.push_back(0);
2846 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002847 return ExprError(Diag(Tok, diag::err_expected_colon));
2848
Chris Lattner5add7542010-08-27 22:32:41 +00002849 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002850 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002851 if (Tok.is(tok::r_paren))
2852 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002853
2854 if (Tok.is(tok::code_completion)) {
2855 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2856 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002857 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002858 return ExprError();
2859 }
2860
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002861 // Check for another keyword selector.
2862 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002863 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002864 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002865 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002866 break;
2867 }
Steve Naroff887407e2007-12-05 22:21:29 +00002868 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002869 T.consumeClose();
Steve Naroff887407e2007-12-05 22:21:29 +00002870 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002871 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002872 T.getOpenLocation(),
2873 T.getCloseLocation()));
Gabor Greif58065b22007-10-19 15:38:32 +00002874 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002875
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002876void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
2877 // MCDecl might be null due to error in method or c-function prototype, etc.
2878 Decl *MCDecl = LM.D;
2879 bool skip = MCDecl &&
2880 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
2881 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
2882 if (skip)
2883 return;
2884
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002885 // Save the current token position.
2886 SourceLocation OrigLoc = Tok.getLocation();
2887
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002888 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2889 // Append the current token at the end of the new token stream so that it
2890 // doesn't get lost.
2891 LM.Toks.push_back(Tok);
2892 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2893
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002894 // Consume the previously pushed token.
2895 ConsumeAnyToken();
2896
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002897 assert((Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
2898 Tok.is(tok::colon)) &&
2899 "Inline objective-c method not starting with '{' or 'try' or ':'");
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002900 // Enter a scope for the method or c-fucntion body.
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002901 ParseScope BodyScope(this,
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002902 parseMethod
2903 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
2904 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002905
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002906 // Tell the actions module that we have entered a method or c-function definition
2907 // with the specified Declarator for the method/function.
Fariborz Jahanian8c6cb462012-08-08 23:41:08 +00002908 if (parseMethod)
2909 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
2910 else
2911 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00002912 if (Tok.is(tok::kw_try))
2913 MCDecl = ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002914 else {
2915 if (Tok.is(tok::colon))
2916 ParseConstructorInitializer(MCDecl);
Fariborz Jahanian2eb362b2012-08-10 18:10:56 +00002917 MCDecl = ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanian9e5df312012-08-10 21:15:06 +00002918 }
Fariborz Jahanian69409722012-08-09 21:12:39 +00002919
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002920 if (Tok.getLocation() != OrigLoc) {
2921 // Due to parsing error, we either went over the cached tokens or
2922 // there are still cached tokens left. If it's the latter case skip the
2923 // leftover tokens.
2924 // Since this is an uncommon situation that should be avoided, use the
2925 // expensive isBeforeInTranslationUnit call.
2926 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2927 OrigLoc))
2928 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2929 ConsumeAnyToken();
2930 }
2931
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00002932 return;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002933}