blob: 416cb39a6396d5793a39d20fce16ddea597950d5 [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;
Douglas Gregor5948ae12012-01-03 18:04:46 +000067 case tok::objc_import:
Douglas Gregor94ad28b2012-01-03 18:24:14 +000068 if (getLang().Modules)
69 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
164///
Erik Verbruggend64251f2011-12-06 09:25:23 +0000165Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +0000166 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000167 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000168 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggend64251f2011-12-06 09:25:23 +0000169 CheckNestedObjCContexts(AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000170 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000172 // Code completion after '@interface'.
173 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000174 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000175 cutOffParsing();
176 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000177 }
178
Chris Lattnerdf195262007-10-09 17:51:17 +0000179 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000180 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000181 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000182 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000183
Steve Naroffdac269b2007-08-20 21:31:48 +0000184 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000185 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000186 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000187 if (Tok.is(tok::l_paren) &&
188 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000189
190 BalancedDelimiterTracker T(*this, tok::l_paren);
191 T.consumeOpen();
192
193 SourceLocation categoryLoc;
Steve Naroffdac269b2007-08-20 21:31:48 +0000194 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000195 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000196 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000197 cutOffParsing();
198 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000199 }
200
Steve Naroff527fe232007-08-23 19:56:30 +0000201 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000202 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000203 categoryId = Tok.getIdentifierInfo();
204 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000205 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000206 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000207 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000208 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000209 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000210
211 T.consumeClose();
212 if (T.getCloseLocation().isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000213 return 0;
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000214
215 if (!attrs.empty()) { // categories don't support attributes.
216 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
217 attrs.clear();
Steve Naroffdac269b2007-08-20 21:31:48 +0000218 }
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000219
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000220 // Next, we need to check for any protocol references.
221 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000222 SmallVector<Decl *, 8> ProtocolRefs;
223 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000224 if (Tok.is(tok::less) &&
225 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000226 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000227 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000228
John McCalld226f652010-08-21 09:40:31 +0000229 Decl *CategoryType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000230 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000231 nameId, nameLoc,
232 categoryId, categoryLoc,
233 ProtocolRefs.data(),
234 ProtocolRefs.size(),
235 ProtocolLocs.data(),
236 EndProtoLoc);
Fariborz Jahaniane6f07f52011-08-19 18:02:47 +0000237
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000238 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000239 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000240
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000241 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000242 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000243 }
244 // Parse a class interface.
245 IdentifierInfo *superClassId = 0;
246 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000247
Chris Lattnerdf195262007-10-09 17:51:17 +0000248 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000249 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000250
251 // Code completion of superclass names.
252 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000253 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000254 cutOffParsing();
255 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000256 }
257
Chris Lattnerdf195262007-10-09 17:51:17 +0000258 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000259 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000260 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000261 }
262 superClassId = Tok.getIdentifierInfo();
263 superClassLoc = ConsumeToken();
264 }
265 // Next, we need to check for any protocol references.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000266 SmallVector<Decl *, 8> ProtocolRefs;
267 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000268 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000269 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000270 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
271 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000272 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000273
John McCalld226f652010-08-21 09:40:31 +0000274 Decl *ClsType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000275 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000276 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000277 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000278 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +0000279 EndProtoLoc, attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Chris Lattnerdf195262007-10-09 17:51:17 +0000281 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000282 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000283
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000284 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000285 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000286}
287
John McCalld0014542009-12-03 22:31:13 +0000288/// The Objective-C property callback. This should be defined where
289/// it's used, but instead it's been lifted to here to support VS2005.
290struct Parser::ObjCPropertyCallback : FieldCallback {
David Blaikie99ba9e32011-12-20 02:48:34 +0000291private:
292 virtual void anchor();
293public:
John McCalld0014542009-12-03 22:31:13 +0000294 Parser &P;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000295 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000296 ObjCDeclSpec &OCDS;
297 SourceLocation AtLoc;
298 tok::ObjCKeywordKind MethodImplKind;
299
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000300 ObjCPropertyCallback(Parser &P,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000301 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000302 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
303 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000304 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCalld0014542009-12-03 22:31:13 +0000305 MethodImplKind(MethodImplKind) {
306 }
307
John McCalld226f652010-08-21 09:40:31 +0000308 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000309 if (FD.D.getIdentifier() == 0) {
310 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
311 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000312 return 0;
John McCalld0014542009-12-03 22:31:13 +0000313 }
314 if (FD.BitfieldSize) {
315 P.Diag(AtLoc, diag::err_objc_property_bitfield)
316 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000317 return 0;
John McCalld0014542009-12-03 22:31:13 +0000318 }
319
320 // Install the property declarator into interfaceDecl.
321 IdentifierInfo *SelName =
322 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
323
324 Selector GetterSel =
325 P.PP.getSelectorTable().getNullarySelector(SelName);
326 IdentifierInfo *SetterName = OCDS.getSetterName();
327 Selector SetterSel;
328 if (SetterName)
329 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
330 else
331 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
332 P.PP.getSelectorTable(),
333 FD.D.getIdentifier());
334 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000335 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000336 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000337 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000338 &isOverridingProperty,
339 MethodImplKind);
340 if (!isOverridingProperty)
341 Props.push_back(Property);
342
343 return Property;
344 }
345};
346
David Blaikie99ba9e32011-12-20 02:48:34 +0000347void Parser::ObjCPropertyCallback::anchor() {
348}
349
Steve Naroffdac269b2007-08-20 21:31:48 +0000350/// objc-interface-decl-list:
351/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000352/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000353/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000354/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000355/// objc-interface-decl-list declaration
356/// objc-interface-decl-list ';'
357///
Steve Naroff294494e2007-08-22 16:35:03 +0000358/// objc-method-requirement: [OBJC2]
359/// @required
360/// @optional
361///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000362void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
363 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000364 SmallVector<Decl *, 32> allMethods;
365 SmallVector<Decl *, 16> allProperties;
366 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000367 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek782f2f52010-01-07 01:20:12 +0000369 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000370
Steve Naroff294494e2007-08-22 16:35:03 +0000371 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000372 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000373 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000374 Decl *methodPrototype =
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000375 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000376 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000377 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
378 // method definitions.
Argyrios Kyrtzidis0db9f4d2011-12-17 04:13:22 +0000379 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
380 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
381 SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
382 if (Tok.is(tok::semi))
383 ConsumeToken();
384 }
Steve Naroff294494e2007-08-22 16:35:03 +0000385 continue;
386 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000387 if (Tok.is(tok::l_paren)) {
388 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000389 ParseObjCMethodDecl(Tok.getLocation(),
390 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000391 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000392 continue;
393 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000394 // Ignore excess semicolons.
395 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000396 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000397 continue;
398 }
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattnerbc662af2008-10-20 06:10:06 +0000400 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000401 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000402 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000404 // Code completion within an Objective-C interface.
405 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000406 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +0000407 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallf312b1e2010-08-26 23:41:50 +0000408 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000409 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000410 }
411
Chris Lattnere82a10f2008-10-20 05:46:22 +0000412 // If we don't have an @ directive, parse it as a function definition.
413 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000414 // The code below does not consume '}'s because it is afraid of eating the
415 // end of a namespace. Because of the way this code is structured, an
416 // erroneous r_brace would cause an infinite loop if not handled here.
417 if (Tok.is(tok::r_brace))
418 break;
John McCall0b7e6782011-03-24 11:26:52 +0000419 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000420 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000421 continue;
422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattnere82a10f2008-10-20 05:46:22 +0000424 // Otherwise, we have an @ directive, eat the @.
425 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000426 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000427 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000428 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000429 }
430
Chris Lattnera2449b22008-10-20 05:57:40 +0000431 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Chris Lattnera2449b22008-10-20 05:57:40 +0000433 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000434 AtEnd.setBegin(AtLoc);
435 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000436 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000437 } else if (DirectiveKind == tok::objc_not_keyword) {
438 Diag(Tok, diag::err_objc_unknown_at);
439 SkipUntil(tok::semi);
440 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000441 }
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Chris Lattnerbc662af2008-10-20 06:10:06 +0000443 // Eat the identifier.
444 ConsumeToken();
445
Chris Lattnera2449b22008-10-20 05:57:40 +0000446 switch (DirectiveKind) {
447 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000448 // FIXME: If someone forgets an @end on a protocol, this loop will
449 // continue to eat up tons of stuff and spew lots of nonsense errors. It
450 // would probably be better to bail out if we saw an @class or @interface
451 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000452 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000453 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000454 SkipUntil(tok::r_brace, tok::at);
455 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000456
457 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000458 case tok::objc_interface:
Erik Verbruggend64251f2011-12-06 09:25:23 +0000459 Diag(AtLoc, diag::err_objc_missing_end)
460 << FixItHint::CreateInsertion(AtLoc, "@end\n");
461 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
462 << (int) Actions.getObjCContainerKind();
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000463 ConsumeToken();
464 break;
465
Chris Lattnera2449b22008-10-20 05:57:40 +0000466 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000467 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000468 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000469 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000470 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000471 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000472 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000473 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000474 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Chris Lattnera2449b22008-10-20 05:57:40 +0000476 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000477 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000478 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000479
Chris Lattnere82a10f2008-10-20 05:46:22 +0000480 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000481 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000482 if (Tok.is(tok::l_paren))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000483 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000485 ObjCPropertyCallback Callback(*this, allProperties,
John McCalld0014542009-12-03 22:31:13 +0000486 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000487
Chris Lattnere82a10f2008-10-20 05:46:22 +0000488 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +0000489 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +0000490 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000491
John McCall7da19ea2011-03-26 01:53:26 +0000492 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000493 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000494 }
Steve Naroff294494e2007-08-22 16:35:03 +0000495 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000496
497 // We break out of the big loop in two cases: when we see @end or when we see
498 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000499 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000500 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000501 return cutOffParsing();
Erik Verbruggend64251f2011-12-06 09:25:23 +0000502 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerbc662af2008-10-20 06:10:06 +0000503 ConsumeToken(); // the "end" identifier
Erik Verbruggend64251f2011-12-06 09:25:23 +0000504 } else {
505 Diag(Tok, diag::err_objc_missing_end)
506 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
507 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
508 << (int) Actions.getObjCContainerKind();
509 AtEnd.setBegin(Tok.getLocation());
510 AtEnd.setEnd(Tok.getLocation());
511 }
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Chris Lattnera2449b22008-10-20 05:57:40 +0000513 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000514 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000515 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000516 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000517 allProperties.data(), allProperties.size(),
518 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000519}
520
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000521/// Parse property attribute declarations.
522///
523/// property-attr-decl: '(' property-attrlist ')'
524/// property-attrlist:
525/// property-attribute
526/// property-attrlist ',' property-attribute
527/// property-attribute:
528/// getter '=' identifier
529/// setter '=' identifier ':'
530/// readonly
531/// readwrite
532/// assign
533/// retain
534/// copy
535/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000536/// atomic
537/// strong
538/// weak
539/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000540///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000541void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000542 assert(Tok.getKind() == tok::l_paren);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000543 BalancedDelimiterTracker T(*this, tok::l_paren);
544 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000546 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000547 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000548 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000549 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000550 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000551 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000553 // If this is not an identifier at all, bail out early.
554 if (II == 0) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000555 T.consumeClose();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000556 return;
557 }
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattner156b0612008-10-20 07:37:22 +0000559 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Chris Lattner92e62b02008-11-20 04:42:34 +0000561 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000562 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000563 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000564 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000565 else if (II->isStr("unsafe_unretained"))
566 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000567 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000568 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000569 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000570 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000571 else if (II->isStr("strong"))
572 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000573 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000574 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000575 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000576 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000577 else if (II->isStr("atomic"))
578 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000579 else if (II->isStr("weak"))
580 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000581 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000582 bool IsSetter = II->getNameStart()[0] == 's';
583
Chris Lattnere00da7c2008-10-20 07:39:53 +0000584 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000585 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
586 diag::err_objc_expected_equal_for_getter;
587
588 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000589 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Douglas Gregor4ad96852009-11-19 07:41:15 +0000591 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000592 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000593 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000594 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000595 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000596 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000597 }
598
Anders Carlsson42499be2010-10-02 17:45:21 +0000599
600 SourceLocation SelLoc;
601 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
602
603 if (!SelIdent) {
604 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
605 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000606 SkipUntil(tok::r_paren);
607 return;
608 }
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Anders Carlsson42499be2010-10-02 17:45:21 +0000610 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000611 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000612 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000614 if (ExpectAndConsume(tok::colon,
615 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000616 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000617 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000618 } else {
619 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000620 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000621 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000622 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000623 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000624 SkipUntil(tok::r_paren);
625 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000626 }
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Chris Lattner156b0612008-10-20 07:37:22 +0000628 if (Tok.isNot(tok::comma))
629 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Chris Lattner156b0612008-10-20 07:37:22 +0000631 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000634 T.consumeClose();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000635}
636
Steve Naroff3536b442007-09-06 21:24:23 +0000637/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000638/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000639/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000640///
641/// objc-instance-method: '-'
642/// objc-class-method: '+'
643///
Steve Naroff4985ace2007-08-22 18:35:33 +0000644/// objc-method-attributes: [OBJC2]
645/// __attribute__((deprecated))
646///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000647Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000648 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000649 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000650
Mike Stump1eb44332009-09-09 15:08:12 +0000651 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000652 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000653 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000654 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000655 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000656 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000657 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000658}
659
660/// objc-selector:
661/// identifier
662/// one of
663/// enum struct union if else while do for switch case default
664/// break continue return goto asm sizeof typeof __alignof
665/// unsigned long const short volatile signed restrict _Complex
666/// in out inout bycopy byref oneway int char float double void _Bool
667///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000668IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000669
Chris Lattnerff384912007-10-07 02:00:24 +0000670 switch (Tok.getKind()) {
671 default:
672 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000673 case tok::ampamp:
674 case tok::ampequal:
675 case tok::amp:
676 case tok::pipe:
677 case tok::tilde:
678 case tok::exclaim:
679 case tok::exclaimequal:
680 case tok::pipepipe:
681 case tok::pipeequal:
682 case tok::caret:
683 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000684 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000685 if (isalpha(ThisTok[0])) {
686 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
687 Tok.setKind(tok::identifier);
688 SelectorLoc = ConsumeToken();
689 return II;
690 }
691 return 0;
692 }
693
Chris Lattnerff384912007-10-07 02:00:24 +0000694 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000695 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000696 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000697 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000698 case tok::kw_break:
699 case tok::kw_case:
700 case tok::kw_catch:
701 case tok::kw_char:
702 case tok::kw_class:
703 case tok::kw_const:
704 case tok::kw_const_cast:
705 case tok::kw_continue:
706 case tok::kw_default:
707 case tok::kw_delete:
708 case tok::kw_do:
709 case tok::kw_double:
710 case tok::kw_dynamic_cast:
711 case tok::kw_else:
712 case tok::kw_enum:
713 case tok::kw_explicit:
714 case tok::kw_export:
715 case tok::kw_extern:
716 case tok::kw_false:
717 case tok::kw_float:
718 case tok::kw_for:
719 case tok::kw_friend:
720 case tok::kw_goto:
721 case tok::kw_if:
722 case tok::kw_inline:
723 case tok::kw_int:
724 case tok::kw_long:
725 case tok::kw_mutable:
726 case tok::kw_namespace:
727 case tok::kw_new:
728 case tok::kw_operator:
729 case tok::kw_private:
730 case tok::kw_protected:
731 case tok::kw_public:
732 case tok::kw_register:
733 case tok::kw_reinterpret_cast:
734 case tok::kw_restrict:
735 case tok::kw_return:
736 case tok::kw_short:
737 case tok::kw_signed:
738 case tok::kw_sizeof:
739 case tok::kw_static:
740 case tok::kw_static_cast:
741 case tok::kw_struct:
742 case tok::kw_switch:
743 case tok::kw_template:
744 case tok::kw_this:
745 case tok::kw_throw:
746 case tok::kw_true:
747 case tok::kw_try:
748 case tok::kw_typedef:
749 case tok::kw_typeid:
750 case tok::kw_typename:
751 case tok::kw_typeof:
752 case tok::kw_union:
753 case tok::kw_unsigned:
754 case tok::kw_using:
755 case tok::kw_virtual:
756 case tok::kw_void:
757 case tok::kw_volatile:
758 case tok::kw_wchar_t:
759 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000760 case tok::kw__Bool:
761 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000762 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000763 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000764 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000765 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000766 }
Steve Naroff294494e2007-08-22 16:35:03 +0000767}
768
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000769/// objc-for-collection-in: 'in'
770///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000771bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000772 // FIXME: May have to do additional look-ahead to only allow for
773 // valid tokens following an 'in'; such as an identifier, unary operators,
774 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000775 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000776 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000777}
778
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000779/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000780/// qualifier list and builds their bitmask representation in the input
781/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000782///
783/// objc-type-qualifiers:
784/// objc-type-qualifier
785/// objc-type-qualifiers objc-type-qualifier
786///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000787void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000788 Declarator::TheContext Context) {
789 assert(Context == Declarator::ObjCParameterContext ||
790 Context == Declarator::ObjCResultContext);
791
Chris Lattnere8b724d2007-12-12 06:56:32 +0000792 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000793 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000794 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCallcdda47f2011-10-01 09:56:14 +0000795 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000796 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000797 }
798
Chris Lattnercb53b362007-12-27 19:57:00 +0000799 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000800 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Chris Lattnere8b724d2007-12-12 06:56:32 +0000802 const IdentifierInfo *II = Tok.getIdentifierInfo();
803 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000804 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000805 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000807 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000808 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000809 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000810 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
811 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
812 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
813 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
814 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
815 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000816 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000817 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000818 ConsumeToken();
819 II = 0;
820 break;
821 }
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Chris Lattnere8b724d2007-12-12 06:56:32 +0000823 // If this wasn't a recognized qualifier, bail out.
824 if (II) return;
825 }
826}
827
John McCallcdda47f2011-10-01 09:56:14 +0000828/// Take all the decl attributes out of the given list and add
829/// them to the given attribute set.
830static void takeDeclAttributes(ParsedAttributes &attrs,
831 AttributeList *list) {
832 while (list) {
833 AttributeList *cur = list;
834 list = cur->getNext();
835
836 if (!cur->isUsedAsTypeAttr()) {
837 // Clear out the next pointer. We're really completely
838 // destroying the internal invariants of the declarator here,
839 // but it doesn't matter because we're done with it.
840 cur->setNext(0);
841 attrs.add(cur);
842 }
843 }
844}
845
846/// takeDeclAttributes - Take all the decl attributes from the given
847/// declarator and add them to the given list.
848static void takeDeclAttributes(ParsedAttributes &attrs,
849 Declarator &D) {
850 // First, take ownership of all attributes.
851 attrs.getPool().takeAllFrom(D.getAttributePool());
852 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
853
854 // Now actually move the attributes over.
855 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
856 takeDeclAttributes(attrs, D.getAttributes());
857 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
858 takeDeclAttributes(attrs,
859 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
860}
861
Chris Lattnere8b724d2007-12-12 06:56:32 +0000862/// objc-type-name:
863/// '(' objc-type-qualifiers[opt] type-name ')'
864/// '(' objc-type-qualifiers[opt] ')'
865///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000866ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000867 Declarator::TheContext context,
868 ParsedAttributes *paramAttrs) {
869 assert(context == Declarator::ObjCParameterContext ||
870 context == Declarator::ObjCResultContext);
871 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
872
Chris Lattnerdf195262007-10-09 17:51:17 +0000873 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000875 BalancedDelimiterTracker T(*this, tok::l_paren);
876 T.consumeOpen();
877
Chris Lattnere8904e92008-08-23 01:48:03 +0000878 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000879 ObjCDeclContextSwitch ObjCDC(*this);
880
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000881 // Parse type qualifiers, in, inout, etc.
John McCallcdda47f2011-10-01 09:56:14 +0000882 ParseObjCTypeQualifierList(DS, context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000883
John McCallb3d87482010-08-24 05:47:05 +0000884 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000885 if (isTypeSpecifierQualifier()) {
John McCallcdda47f2011-10-01 09:56:14 +0000886 // Parse an abstract declarator.
887 DeclSpec declSpec(AttrFactory);
888 declSpec.setObjCQualifiers(&DS);
889 ParseSpecifierQualifierList(declSpec);
890 Declarator declarator(declSpec, context);
891 ParseDeclarator(declarator);
892
893 // If that's not invalid, extract a type.
894 if (!declarator.isInvalidType()) {
895 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
896 if (!type.isInvalid())
897 Ty = type.get();
898
899 // If we're parsing a parameter, steal all the decl attributes
900 // and add them to the decl spec.
901 if (context == Declarator::ObjCParameterContext)
902 takeDeclAttributes(*paramAttrs, declarator);
903 }
904 } else if (context == Declarator::ObjCResultContext &&
905 Tok.is(tok::identifier)) {
Douglas Gregore97179c2011-09-08 01:46:34 +0000906 if (!Ident_instancetype)
907 Ident_instancetype = PP.getIdentifierInfo("instancetype");
908
909 if (Tok.getIdentifierInfo() == Ident_instancetype) {
910 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
911 ConsumeToken();
912 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000913 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000914
Steve Naroffd7333c22008-10-21 14:15:04 +0000915 if (Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000916 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000917 else if (Tok.getLocation() == TypeStartLoc) {
918 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000919 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000920 SkipUntil(tok::r_paren);
921 } else {
922 // Otherwise, we found *something*, but didn't get a ')' in the right
923 // place. Emit an error then return what we have as the type.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000924 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000925 }
Steve Narofff28b2642007-09-05 23:30:30 +0000926 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000927}
928
929/// objc-method-decl:
930/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000931/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000932/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000933/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000934///
935/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000936/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000937/// objc-keyword-selector objc-keyword-decl
938///
939/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000940/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
941/// objc-selector ':' objc-keyword-attributes[opt] identifier
942/// ':' objc-type-name objc-keyword-attributes[opt] identifier
943/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000944///
Steve Naroff4985ace2007-08-22 18:35:33 +0000945/// objc-parmlist:
946/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000947///
Steve Naroff4985ace2007-08-22 18:35:33 +0000948/// objc-parms:
949/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000950///
Steve Naroff4985ace2007-08-22 18:35:33 +0000951/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000952/// , ...
953///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000954/// objc-keyword-attributes: [OBJC2]
955/// __attribute__((unused))
956///
John McCalld226f652010-08-21 09:40:31 +0000957Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000958 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000959 tok::ObjCKeywordKind MethodImplKind,
960 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000961 ParsingDeclRAIIObject PD(*this);
962
Douglas Gregore8f5a172010-04-07 00:21:17 +0000963 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000964 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000965 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000966 cutOffParsing();
967 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000968 }
969
Chris Lattnere8904e92008-08-23 01:48:03 +0000970 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000971 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000972 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000973 if (Tok.is(tok::l_paren))
John McCallcdda47f2011-10-01 09:56:14 +0000974 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Ted Kremenek9e049352010-02-18 23:05:16 +0000976 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000977 ParsedAttributes methodAttrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000978 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000979 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000980
Douglas Gregore8f5a172010-04-07 00:21:17 +0000981 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000982 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000983 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000984 cutOffParsing();
985 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000986 }
987
Ted Kremenek9e049352010-02-18 23:05:16 +0000988 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000989 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000990 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000991
Steve Naroff84c43102009-02-11 20:43:13 +0000992 // An unnamed colon is valid.
993 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000994 Diag(Tok, diag::err_expected_selector_for_method)
995 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000996 // Skip until we get a ; or {}.
997 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000998 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000999 }
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Chris Lattner5f9e2722011-07-23 10:55:15 +00001001 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +00001002 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001003 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001004 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001005 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Chris Lattnerff384912007-10-07 02:00:24 +00001007 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +00001008 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001009 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001010 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001011 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001012 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001013 methodAttrs.getList(), MethodImplKind,
1014 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +00001015 PD.complete(Result);
1016 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +00001017 }
Steve Narofff28b2642007-09-05 23:30:30 +00001018
Chris Lattner5f9e2722011-07-23 10:55:15 +00001019 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001020 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001021 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001022 ParseScope PrototypeScope(this,
1023 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +00001024
1025 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001026
Chris Lattnerff384912007-10-07 02:00:24 +00001027 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +00001028 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +00001029 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Chris Lattnerff384912007-10-07 02:00:24 +00001031 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +00001032 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001033 Diag(Tok, diag::err_expected_colon);
1034 break;
1035 }
1036 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001037
John McCallb3d87482010-08-24 05:47:05 +00001038 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001039 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCallcdda47f2011-10-01 09:56:14 +00001040 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1041 Declarator::ObjCParameterContext,
1042 &paramAttrs);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001043
Chris Lattnerff384912007-10-07 02:00:24 +00001044 // If attributes exist before the argument name, parse them.
John McCallcdda47f2011-10-01 09:56:14 +00001045 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnere294d3f2009-04-11 18:57:04 +00001046 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +00001047 if (getLang().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +00001048 MaybeParseGNUAttributes(paramAttrs);
1049 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +00001050 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001051
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001052 // Code completion for the next piece of the selector.
1053 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001054 KeyIdents.push_back(SelIdent);
1055 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1056 mType == tok::minus,
1057 /*AtParameterName=*/true,
1058 ReturnType,
1059 KeyIdents.data(),
1060 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001061 cutOffParsing();
1062 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001063 }
1064
Chris Lattnerdf195262007-10-09 17:51:17 +00001065 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001066 Diag(Tok, diag::err_expected_ident); // missing argument name.
1067 break;
Steve Naroff4985ace2007-08-22 18:35:33 +00001068 }
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Chris Lattnere294d3f2009-04-11 18:57:04 +00001070 ArgInfo.Name = Tok.getIdentifierInfo();
1071 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +00001072 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Chris Lattnere294d3f2009-04-11 18:57:04 +00001074 ArgInfos.push_back(ArgInfo);
1075 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001076 KeyLocs.push_back(selLoc);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001077
John McCall0b7e6782011-03-24 11:26:52 +00001078 // Make sure the attributes persist.
1079 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1080
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001081 // Code completion for the next piece of the selector.
1082 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001083 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1084 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001085 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001086 ReturnType,
1087 KeyIdents.data(),
1088 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001089 cutOffParsing();
1090 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001091 }
1092
Chris Lattnerff384912007-10-07 02:00:24 +00001093 // Check for another keyword selector.
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001094 SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001095 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +00001096 break;
1097 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +00001098 }
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Steve Naroff335eafa2007-11-15 12:35:21 +00001100 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Chris Lattnerff384912007-10-07 02:00:24 +00001102 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +00001103 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001104 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001105 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001106 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001107 ConsumeToken();
1108 break;
1109 }
John McCall0b7e6782011-03-24 11:26:52 +00001110 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001111 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001112 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001113 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1114 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001115 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001116 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001117 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1118 ParmDecl.getIdentifierLoc(),
1119 Param,
1120 0));
1121
Chris Lattnerff384912007-10-07 02:00:24 +00001122 }
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001124 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001125 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001126 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001127 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001128
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001129 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001130 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001131
Chris Lattnerff384912007-10-07 02:00:24 +00001132 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1133 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001134 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001135 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001136 mType, DSRet, ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001137 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001138 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001139 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001140 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001141
John McCall54abf7d2009-11-04 02:18:39 +00001142 PD.complete(Result);
1143 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001144}
1145
Steve Naroffdac269b2007-08-20 21:31:48 +00001146/// objc-protocol-refs:
1147/// '<' identifier-list '>'
1148///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001149bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001150ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1151 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001152 bool WarnOnDeclarations,
1153 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001154 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001156 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Chris Lattner5f9e2722011-07-23 10:55:15 +00001158 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Chris Lattnere13b9592008-07-26 04:03:38 +00001160 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001161 if (Tok.is(tok::code_completion)) {
1162 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1163 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001164 cutOffParsing();
1165 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001166 }
1167
Chris Lattnere13b9592008-07-26 04:03:38 +00001168 if (Tok.isNot(tok::identifier)) {
1169 Diag(Tok, diag::err_expected_ident);
1170 SkipUntil(tok::greater);
1171 return true;
1172 }
1173 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1174 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001175 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001176 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Chris Lattnere13b9592008-07-26 04:03:38 +00001178 if (Tok.isNot(tok::comma))
1179 break;
1180 ConsumeToken();
1181 }
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Chris Lattnere13b9592008-07-26 04:03:38 +00001183 // Consume the '>'.
1184 if (Tok.isNot(tok::greater)) {
1185 Diag(Tok, diag::err_expected_greater);
1186 return true;
1187 }
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Chris Lattnere13b9592008-07-26 04:03:38 +00001189 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Chris Lattnere13b9592008-07-26 04:03:38 +00001191 // Convert the list of protocols identifiers into a list of protocol decls.
1192 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1193 &ProtocolIdents[0], ProtocolIdents.size(),
1194 Protocols);
1195 return false;
1196}
1197
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001198/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1199/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001200bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001201 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1202 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1203 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001204 SmallVector<Decl *, 8> ProtocolDecl;
1205 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001206 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1207 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001208 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1209 ProtocolLocs.data(), LAngleLoc);
1210 if (EndProtoLoc.isValid())
1211 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001212 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001213}
1214
1215
Steve Naroffdac269b2007-08-20 21:31:48 +00001216/// objc-class-instance-variables:
1217/// '{' objc-instance-variable-decl-list[opt] '}'
1218///
1219/// objc-instance-variable-decl-list:
1220/// objc-visibility-spec
1221/// objc-instance-variable-decl ';'
1222/// ';'
1223/// objc-instance-variable-decl-list objc-visibility-spec
1224/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1225/// objc-instance-variable-decl-list ';'
1226///
1227/// objc-visibility-spec:
1228/// @private
1229/// @protected
1230/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001231/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001232///
1233/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001234/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001235///
John McCalld226f652010-08-21 09:40:31 +00001236void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001237 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001238 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001239 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001240 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001241
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001242 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001243 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor72de6672009-01-08 20:45:30 +00001244
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001245 BalancedDelimiterTracker T(*this, tok::l_brace);
1246 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Steve Naroffddbff782007-08-21 21:17:12 +00001248 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001249 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001250 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Steve Naroffddbff782007-08-21 21:17:12 +00001252 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001253 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001254 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001255 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001256 ConsumeToken();
1257 continue;
1258 }
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Steve Naroffddbff782007-08-21 21:17:12 +00001260 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001261 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001262 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001263
1264 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001265 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001266 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001267 }
1268
Steve Naroff861cf3e2007-08-23 18:16:40 +00001269 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001270 case tok::objc_private:
1271 case tok::objc_public:
1272 case tok::objc_protected:
1273 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001274 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001275 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001276 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001277 default:
1278 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001279 continue;
1280 }
1281 }
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001283 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001284 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001285 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001286 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001287 }
1288
John McCallbdd563e2009-11-03 02:38:08 +00001289 struct ObjCIvarCallback : FieldCallback {
1290 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001291 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001292 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001293 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001294
John McCalld226f652010-08-21 09:40:31 +00001295 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001296 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001297 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1298 }
1299
John McCalld226f652010-08-21 09:40:31 +00001300 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001301 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001302 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001303 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001304 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001305 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001306 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001307 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001308 if (Field)
1309 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001310 return Field;
1311 }
1312 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001313
Chris Lattnere1359422008-04-10 06:46:29 +00001314 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00001315 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +00001316 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Chris Lattnerdf195262007-10-09 17:51:17 +00001318 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001319 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001320 } else {
1321 Diag(Tok, diag::err_expected_semi_decl_list);
1322 // Skip to end of block or statement
1323 SkipUntil(tok::r_brace, true, true);
1324 }
1325 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001326 T.consumeClose();
1327
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001328 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001329 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001330 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff8749be52007-10-31 22:11:35 +00001331 // Call ActOnFields() even if we don't have any decls. This is useful
1332 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001333 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie77b6de02011-09-22 02:58:26 +00001334 AllIvarDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001335 T.getOpenLocation(), T.getCloseLocation(), 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001336 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001337}
Steve Naroffdac269b2007-08-20 21:31:48 +00001338
1339/// objc-protocol-declaration:
1340/// objc-protocol-definition
1341/// objc-protocol-forward-reference
1342///
1343/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001344/// @protocol identifier
1345/// objc-protocol-refs[opt]
1346/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001347/// @end
1348///
1349/// objc-protocol-forward-reference:
1350/// @protocol identifier-list ';'
1351///
1352/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001353/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001354/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001355Parser::DeclGroupPtrTy
1356Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1357 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001358 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001359 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1360 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001361
Douglas Gregor083128f2009-11-18 04:49:41 +00001362 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001363 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001364 cutOffParsing();
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001365 return DeclGroupPtrTy();
Douglas Gregor083128f2009-11-18 04:49:41 +00001366 }
1367
Chris Lattnerdf195262007-10-09 17:51:17 +00001368 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001369 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001370 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001371 }
1372 // Save the protocol name, then consume it.
1373 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1374 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001375
Chris Lattnerdf195262007-10-09 17:51:17 +00001376 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001377 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001378 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001379 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001380 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001381 }
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Erik Verbruggen90ec96f2011-12-08 09:58:43 +00001383 CheckNestedObjCContexts(AtLoc);
1384
Chris Lattnerdf195262007-10-09 17:51:17 +00001385 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001386 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001387 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1388
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001389 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001390 while (1) {
1391 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001392 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001393 Diag(Tok, diag::err_expected_ident);
1394 SkipUntil(tok::semi);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001395 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001396 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001397 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1398 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001399 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Chris Lattnerdf195262007-10-09 17:51:17 +00001401 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001402 break;
1403 }
1404 // Consume the ';'.
1405 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001406 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Steve Naroffe440eb82007-10-10 17:32:04 +00001408 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001409 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001410 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001411 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001412 }
Mike Stump1eb44332009-09-09 15:08:12 +00001413
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001414 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001415 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001416
Chris Lattner5f9e2722011-07-23 10:55:15 +00001417 SmallVector<Decl *, 8> ProtocolRefs;
1418 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001419 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001420 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1421 LAngleLoc, EndProtoLoc))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001422 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001423
John McCalld226f652010-08-21 09:40:31 +00001424 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001425 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001426 ProtocolRefs.data(),
1427 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001428 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001429 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001430
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001431 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001432 return Actions.ConvertDeclToDeclGroup(ProtoType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001433}
Steve Naroffdac269b2007-08-20 21:31:48 +00001434
1435/// objc-implementation:
1436/// objc-class-implementation-prologue
1437/// objc-category-implementation-prologue
1438///
1439/// objc-class-implementation-prologue:
1440/// @implementation identifier objc-superclass[opt]
1441/// objc-class-instance-variables[opt]
1442///
1443/// objc-category-implementation-prologue:
1444/// @implementation identifier ( identifier )
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001445Parser::DeclGroupPtrTy
1446Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001447 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1448 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggend64251f2011-12-06 09:25:23 +00001449 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001450 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001452 // Code completion after '@implementation'.
1453 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001454 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001455 cutOffParsing();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001456 return DeclGroupPtrTy();
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001457 }
1458
Chris Lattnerdf195262007-10-09 17:51:17 +00001459 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001460 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001461 return DeclGroupPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001462 }
1463 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001464 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001465 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001466 Decl *ObjCImpDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001467
1468 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001469 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001470 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001471 SourceLocation categoryLoc, rparenLoc;
1472 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001474 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001475 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001476 cutOffParsing();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001477 return DeclGroupPtrTy();
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001478 }
1479
Chris Lattnerdf195262007-10-09 17:51:17 +00001480 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001481 categoryId = Tok.getIdentifierInfo();
1482 categoryLoc = ConsumeToken();
1483 } else {
1484 Diag(Tok, diag::err_expected_ident); // missing category name.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001485 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001486 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001487 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001488 Diag(Tok, diag::err_expected_rparen);
1489 SkipUntil(tok::r_paren, false); // don't stop at ';'
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001490 return DeclGroupPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001491 }
1492 rparenLoc = ConsumeParen();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001493 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001494 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001495 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001496
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001497 } else {
1498 // We have a class implementation
1499 SourceLocation superClassLoc;
1500 IdentifierInfo *superClassId = 0;
1501 if (Tok.is(tok::colon)) {
1502 // We have a super class
1503 ConsumeToken();
1504 if (Tok.isNot(tok::identifier)) {
1505 Diag(Tok, diag::err_expected_ident); // missing super class name.
1506 return DeclGroupPtrTy();
1507 }
1508 superClassId = Tok.getIdentifierInfo();
1509 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001510 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001511 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1512 AtLoc, nameId, nameLoc,
1513 superClassId, superClassLoc);
1514
1515 if (Tok.is(tok::l_brace)) // we have ivars
1516 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001517 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001518 assert(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001520 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001521
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001522 {
1523 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
1524 while (!ObjCImplParsing.isFinished() && Tok.isNot(tok::eof)) {
1525 ParsedAttributesWithRange attrs(AttrFactory);
1526 MaybeParseCXX0XAttributes(attrs);
1527 MaybeParseMicrosoftAttributes(attrs);
1528 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1529 DeclGroupRef DG = DGP.get();
1530 DeclsInGroup.append(DG.begin(), DG.end());
1531 }
1532 }
1533 }
1534
1535 DeclsInGroup.push_back(ObjCImpDecl);
1536 return Actions.BuildDeclaratorGroup(
1537 DeclsInGroup.data(), DeclsInGroup.size(), false);
Reid Spencer5f016e22007-07-11 17:01:13 +00001538}
Steve Naroff60fccee2007-10-29 21:38:07 +00001539
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001540Parser::DeclGroupPtrTy
1541Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001542 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1543 "ParseObjCAtEndDeclaration(): Expected @end");
1544 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001545 if (CurParsedObjCImpl)
1546 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001547 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001548 // missing @implementation
Erik Verbruggend64251f2011-12-06 09:25:23 +00001549 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001550 return DeclGroupPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +00001551}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001552
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001553Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1554 if (!Finished) {
1555 finish(P.Tok.getLocation());
1556 if (P.Tok.is(tok::eof)) {
1557 P.Diag(P.Tok, diag::err_objc_missing_end)
1558 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1559 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1560 << Sema::OCK_Implementation;
1561 }
1562 }
1563 P.CurParsedObjCImpl = 0;
1564 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001565}
1566
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001567void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1568 assert(!Finished);
1569 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1570 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1571 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
1572
1573 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1574
1575 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001576 for (LateParsedObjCMethodContainer::iterator
1577 I = LateParsedObjCMethods.begin(),
1578 E = LateParsedObjCMethods.end(); I != E; ++I)
1579 delete *I;
1580 LateParsedObjCMethods.clear();
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001581
1582 Finished = true;
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001583}
1584
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001585/// compatibility-alias-decl:
1586/// @compatibility_alias alias-name class-name ';'
1587///
John McCalld226f652010-08-21 09:40:31 +00001588Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001589 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1590 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1591 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001592 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001593 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001594 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001595 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001596 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1597 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001598 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001599 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001600 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001601 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001602 IdentifierInfo *classId = Tok.getIdentifierInfo();
1603 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001604 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1605 "@compatibility_alias");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001606 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1607 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001608}
1609
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001610/// property-synthesis:
1611/// @synthesize property-ivar-list ';'
1612///
1613/// property-ivar-list:
1614/// property-ivar
1615/// property-ivar-list ',' property-ivar
1616///
1617/// property-ivar:
1618/// identifier
1619/// identifier '=' identifier
1620///
John McCalld226f652010-08-21 09:40:31 +00001621Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001622 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1623 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001624 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Douglas Gregorb328c422009-11-18 19:45:45 +00001626 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001627 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001628 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001629 cutOffParsing();
1630 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001631 }
1632
Douglas Gregorb328c422009-11-18 19:45:45 +00001633 if (Tok.isNot(tok::identifier)) {
1634 Diag(Tok, diag::err_synthesized_property_name);
1635 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001636 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001637 }
1638
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001639 IdentifierInfo *propertyIvar = 0;
1640 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1641 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001642 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001643 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001644 // property '=' ivar-name
1645 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001646
1647 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001648 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001649 cutOffParsing();
1650 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001651 }
1652
Chris Lattnerdf195262007-10-09 17:51:17 +00001653 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001654 Diag(Tok, diag::err_expected_ident);
1655 break;
1656 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001657 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001658 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001659 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001660 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001661 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001662 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001663 break;
1664 ConsumeToken(); // consume ','
1665 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001666 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001667 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001668}
1669
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001670/// property-dynamic:
1671/// @dynamic property-list
1672///
1673/// property-list:
1674/// identifier
1675/// property-list ',' identifier
1676///
John McCalld226f652010-08-21 09:40:31 +00001677Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001678 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1679 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001680 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001681 while (true) {
1682 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001683 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001684 cutOffParsing();
1685 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001686 }
1687
1688 if (Tok.isNot(tok::identifier)) {
1689 Diag(Tok, diag::err_expected_ident);
1690 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001691 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001692 }
1693
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001694 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1695 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001696 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001697 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001698
Chris Lattnerdf195262007-10-09 17:51:17 +00001699 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001700 break;
1701 ConsumeToken(); // consume ','
1702 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001703 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001704 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001705}
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001707/// objc-throw-statement:
1708/// throw expression[opt];
1709///
John McCall60d7b3a2010-08-24 06:29:42 +00001710StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1711 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001712 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001713 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001714 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001715 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001716 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001717 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001718 }
1719 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001720 // consume ';'
1721 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001722 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001723}
1724
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001725/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001726/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001727///
John McCall60d7b3a2010-08-24 06:29:42 +00001728StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001729Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001730 ConsumeToken(); // consume synchronized
1731 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001732 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001733 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001734 }
John McCall07524032011-07-27 21:50:02 +00001735
1736 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001737 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001738 ExprResult operand(ParseExpression());
1739
1740 if (Tok.is(tok::r_paren)) {
1741 ConsumeParen(); // ')'
1742 } else {
1743 if (!operand.isInvalid())
1744 Diag(Tok, diag::err_expected_rparen);
1745
1746 // Skip forward until we see a left brace, but don't consume it.
1747 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001748 }
John McCall07524032011-07-27 21:50:02 +00001749
1750 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001751 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001752 if (!operand.isInvalid())
1753 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001754 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001755 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001756
John McCall07524032011-07-27 21:50:02 +00001757 // Check the @synchronized operand now.
1758 if (!operand.isInvalid())
1759 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001760
John McCall07524032011-07-27 21:50:02 +00001761 // Parse the compound statement within a new scope.
1762 ParseScope bodyScope(this, Scope::DeclScope);
1763 StmtResult body(ParseCompoundStatementBody());
1764 bodyScope.Exit();
1765
1766 // If there was a semantic or parse error earlier with the
1767 // operand, fail now.
1768 if (operand.isInvalid())
1769 return StmtError();
1770
1771 if (body.isInvalid())
1772 body = Actions.ActOnNullStmt(Tok.getLocation());
1773
1774 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001775}
1776
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001777/// objc-try-catch-statement:
1778/// @try compound-statement objc-catch-list[opt]
1779/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1780///
1781/// objc-catch-list:
1782/// @catch ( parameter-declaration ) compound-statement
1783/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1784/// catch-parameter-declaration:
1785/// parameter-declaration
1786/// '...' [OBJC2]
1787///
John McCall60d7b3a2010-08-24 06:29:42 +00001788StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001789 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001790
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001791 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001792 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001793 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001794 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001795 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001796 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001797 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001798 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001799 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001800 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001801 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001802 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001803
Chris Lattnerdf195262007-10-09 17:51:17 +00001804 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001805 // At this point, we need to lookahead to determine if this @ is the start
1806 // of an @catch or @finally. We don't want to consume the @ token if this
1807 // is an @try or @encode or something else.
1808 Token AfterAt = GetLookAheadToken(1);
1809 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1810 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1811 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001812
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001813 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001814 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001815 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001816 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001817 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001818 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001819 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001820 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001821 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001822 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001823 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001824 ParseDeclarator(ParmDecl);
1825
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001826 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001827 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001828 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001829 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001830 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Steve Naroff93a25952009-04-07 22:56:58 +00001832 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001833
Steve Naroff93a25952009-04-07 22:56:58 +00001834 if (Tok.is(tok::r_paren))
1835 RParenLoc = ConsumeParen();
1836 else // Skip over garbage, until we get to ')'. Eat the ')'.
1837 SkipUntil(tok::r_paren, true, false);
1838
John McCall60d7b3a2010-08-24 06:29:42 +00001839 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001840 if (Tok.is(tok::l_brace))
1841 CatchBody = ParseCompoundStatementBody();
1842 else
1843 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001844 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001845 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001846
John McCall60d7b3a2010-08-24 06:29:42 +00001847 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001848 RParenLoc,
1849 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001850 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001851 if (!Catch.isInvalid())
1852 CatchStmts.push_back(Catch.release());
1853
Steve Naroff64515f32008-02-05 21:27:35 +00001854 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001855 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1856 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001857 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001858 }
1859 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001860 } else {
1861 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001862 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001863 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001864
John McCall60d7b3a2010-08-24 06:29:42 +00001865 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001866 if (Tok.is(tok::l_brace))
1867 FinallyBody = ParseCompoundStatementBody();
1868 else
1869 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001870 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001871 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001872 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001873 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001874 catch_or_finally_seen = true;
1875 break;
1876 }
1877 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001878 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001879 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001880 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001881 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001882
John McCall9ae2f072010-08-23 23:25:46 +00001883 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001884 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001885 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001886}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001887
John McCallf85e1932011-06-15 23:02:42 +00001888/// objc-autoreleasepool-statement:
1889/// @autoreleasepool compound-statement
1890///
1891StmtResult
1892Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1893 ConsumeToken(); // consume autoreleasepool
1894 if (Tok.isNot(tok::l_brace)) {
1895 Diag(Tok, diag::err_expected_lbrace);
1896 return StmtError();
1897 }
1898 // Enter a scope to hold everything within the compound stmt. Compound
1899 // statements can always hold declarations.
1900 ParseScope BodyScope(this, Scope::DeclScope);
1901
1902 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1903
1904 BodyScope.Exit();
1905 if (AutoreleasePoolBody.isInvalid())
1906 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1907 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1908 AutoreleasePoolBody.take());
1909}
1910
Steve Naroff3536b442007-09-06 21:24:23 +00001911/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001912///
John McCalld226f652010-08-21 09:40:31 +00001913Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001914 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001915
John McCallf312b1e2010-08-26 23:41:50 +00001916 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1917 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001918
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001919 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001920 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001921 if (CurParsedObjCImpl) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001922 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001923 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001924 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001925 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001926 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001927
Steve Naroff409be832007-11-11 19:54:21 +00001928 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001929 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001930 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Steve Naroff409be832007-11-11 19:54:21 +00001932 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1933 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001934
Steve Naroff409be832007-11-11 19:54:21 +00001935 // If we didn't find the '{', bail out.
1936 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001937 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001938 }
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001939
1940 if (!MDecl) {
1941 ConsumeBrace();
1942 SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
1943 return 0;
1944 }
1945
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001946 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001947 Actions.AddAnyMethodToGlobalPool(MDecl);
1948
1949 if (CurParsedObjCImpl) {
1950 // Consume the tokens and store them for later parsing.
1951 LexedMethod* LM = new LexedMethod(this, MDecl);
1952 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
1953 CachedTokens &Toks = LM->Toks;
1954 // Begin by storing the '{' token.
1955 Toks.push_back(Tok);
1956 ConsumeBrace();
1957 // Consume everything up to (and including) the matching right brace.
1958 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1959
1960 } else {
1961 ConsumeBrace();
1962 SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
1963 }
1964
Steve Naroff71c0a952007-11-13 23:01:27 +00001965 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001966}
Anders Carlsson55085182007-08-21 17:43:55 +00001967
John McCall60d7b3a2010-08-24 06:29:42 +00001968StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001969 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001970 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001971 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001972 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001973 }
1974
1975 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001976 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001977
1978 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001979 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001980
1981 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001982 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00001983
1984 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1985 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001986
John McCall60d7b3a2010-08-24 06:29:42 +00001987 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001988 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001989 // If the expression is invalid, skip ahead to the next semicolon. Not
1990 // doing this opens us up to the possibility of infinite loops if
1991 // ParseExpression does not consume any tokens.
1992 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001993 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001994 }
Chris Lattner5d803162009-12-07 16:33:19 +00001995
Steve Naroff64515f32008-02-05 21:27:35 +00001996 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001997 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001998 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001999}
2000
John McCall60d7b3a2010-08-24 06:29:42 +00002001ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00002002 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002003 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00002004 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002005 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00002006 return ExprError();
2007
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002008 case tok::string_literal: // primary-expression: string-literal
2009 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00002010 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002011 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00002012 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00002013 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002014
Chris Lattner4fef81d2008-08-05 06:19:09 +00002015 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2016 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00002017 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002018 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00002019 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002020 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00002021 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002022 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00002023 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002024 }
Anders Carlsson55085182007-08-21 17:43:55 +00002025 }
Anders Carlsson55085182007-08-21 17:43:55 +00002026}
2027
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002028/// \brirg Parse the receiver of an Objective-C++ message send.
2029///
2030/// This routine parses the receiver of a message send in
2031/// Objective-C++ either as a type or as an expression. Note that this
2032/// routine must not be called to parse a send to 'super', since it
2033/// has no way to return such a result.
2034///
2035/// \param IsExpr Whether the receiver was parsed as an expression.
2036///
2037/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2038/// IsExpr is true), the parsed expression. If the receiver was parsed
2039/// as a type (\c IsExpr is false), the parsed type.
2040///
2041/// \returns True if an error occurred during parsing or semantic
2042/// analysis, in which case the arguments do not have valid
2043/// values. Otherwise, returns false for a successful parse.
2044///
2045/// objc-receiver: [C++]
2046/// 'super' [not parsed here]
2047/// expression
2048/// simple-type-specifier
2049/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002050bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002051 InMessageExpressionRAIIObject InMessage(*this, true);
2052
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002053 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2054 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2055 TryAnnotateTypeOrScopeToken();
2056
2057 if (!isCXXSimpleTypeSpecifier()) {
2058 // objc-receiver:
2059 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00002060 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002061 if (Receiver.isInvalid())
2062 return true;
2063
2064 IsExpr = true;
2065 TypeOrExpr = Receiver.take();
2066 return false;
2067 }
2068
2069 // objc-receiver:
2070 // typename-specifier
2071 // simple-type-specifier
2072 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00002073 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002074 ParseCXXSimpleTypeSpecifier(DS);
2075
2076 if (Tok.is(tok::l_paren)) {
2077 // If we see an opening parentheses at this point, we are
2078 // actually parsing an expression that starts with a
2079 // function-style cast, e.g.,
2080 //
2081 // postfix-expression:
2082 // simple-type-specifier ( expression-list [opt] )
2083 // typename-specifier ( expression-list [opt] )
2084 //
2085 // Parse the remainder of this case, then the (optional)
2086 // postfix-expression suffix, followed by the (optional)
2087 // right-hand side of the binary expression. We have an
2088 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002089 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002090 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002091 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002092 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002093 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002094 if (Receiver.isInvalid())
2095 return true;
2096
2097 IsExpr = true;
2098 TypeOrExpr = Receiver.take();
2099 return false;
2100 }
2101
2102 // We have a class message. Turn the simple-type-specifier or
2103 // typename-specifier we parsed into a type and parse the
2104 // remainder of the class message.
2105 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002106 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002107 if (Type.isInvalid())
2108 return true;
2109
2110 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002111 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002112 return false;
2113}
2114
Douglas Gregor1b730e82010-05-31 14:40:22 +00002115/// \brief Determine whether the parser is currently referring to a an
2116/// Objective-C message send, using a simplified heuristic to avoid overhead.
2117///
2118/// This routine will only return true for a subset of valid message-send
2119/// expressions.
2120bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00002121 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002122 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002123 return GetLookAheadToken(1).is(tok::identifier) &&
2124 GetLookAheadToken(2).is(tok::identifier);
2125}
2126
Douglas Gregor9497a732010-09-16 01:51:54 +00002127bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
2128 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
2129 InMessageExpression)
2130 return false;
2131
2132
2133 ParsedType Type;
2134
2135 if (Tok.is(tok::annot_typename))
2136 Type = getTypeAnnotation(Tok);
2137 else if (Tok.is(tok::identifier))
2138 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2139 getCurScope());
2140 else
2141 return false;
2142
2143 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2144 const Token &AfterNext = GetLookAheadToken(2);
2145 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2146 if (Tok.is(tok::identifier))
2147 TryAnnotateTypeOrScopeToken();
2148
2149 return Tok.is(tok::annot_typename);
2150 }
2151 }
2152
2153 return false;
2154}
2155
Mike Stump1eb44332009-09-09 15:08:12 +00002156/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002157/// '[' objc-receiver objc-message-args ']'
2158///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002159/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002160/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002161/// expression
2162/// class-name
2163/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002164///
John McCall60d7b3a2010-08-24 06:29:42 +00002165ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002166 assert(Tok.is(tok::l_square) && "'[' expected");
2167 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2168
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002169 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002170 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002171 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002172 return ExprError();
2173 }
2174
Douglas Gregor0fbda682010-09-15 14:51:05 +00002175 InMessageExpressionRAIIObject InMessage(*this, true);
2176
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002177 if (getLang().CPlusPlus) {
2178 // We completely separate the C and C++ cases because C++ requires
2179 // more complicated (read: slower) parsing.
2180
2181 // Handle send to super.
2182 // FIXME: This doesn't benefit from the same typo-correction we
2183 // get in Objective-C.
2184 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002185 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002186 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2187 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002188
2189 // Parse the receiver, which is either a type or an expression.
2190 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002191 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002192 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2193 SkipUntil(tok::r_square);
2194 return ExprError();
2195 }
2196
2197 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002198 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2199 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002200 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002201
2202 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002203 ParsedType::getFromOpaquePtr(TypeOrExpr),
2204 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002205 }
2206
2207 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002208 IdentifierInfo *Name = Tok.getIdentifierInfo();
2209 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002210 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002211 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002212 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002213 NextToken().is(tok::period),
2214 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002215 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002216 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2217 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002218
John McCallf312b1e2010-08-26 23:41:50 +00002219 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002220 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002221 SkipUntil(tok::r_square);
2222 return ExprError();
2223 }
2224
Douglas Gregor1569f952010-04-21 20:38:13 +00002225 ConsumeToken(); // the type name
2226
2227 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002228 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002229
John McCallf312b1e2010-08-26 23:41:50 +00002230 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002231 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002232 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002233 }
Chris Lattner699b6612008-01-25 18:59:06 +00002234 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002235
2236 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002237 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002238 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002239 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002240 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002241 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002242
John McCallb3d87482010-08-24 05:47:05 +00002243 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2244 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002245}
Sebastian Redl1d922962008-12-13 15:32:12 +00002246
Douglas Gregor2725ca82010-04-21 19:57:20 +00002247/// \brief Parse the remainder of an Objective-C message following the
2248/// '[' objc-receiver.
2249///
2250/// This routine handles sends to super, class messages (sent to a
2251/// class name), and instance messages (sent to an object), and the
2252/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2253/// ReceiverExpr, respectively. Only one of these parameters may have
2254/// a valid value.
2255///
2256/// \param LBracLoc The location of the opening '['.
2257///
2258/// \param SuperLoc If this is a send to 'super', the location of the
2259/// 'super' keyword that indicates a send to the superclass.
2260///
2261/// \param ReceiverType If this is a class message, the type of the
2262/// class we are sending a message to.
2263///
2264/// \param ReceiverExpr If this is an instance message, the expression
2265/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002266///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002267/// objc-message-args:
2268/// objc-selector
2269/// objc-keywordarg-list
2270///
2271/// objc-keywordarg-list:
2272/// objc-keywordarg
2273/// objc-keywordarg-list objc-keywordarg
2274///
Mike Stump1eb44332009-09-09 15:08:12 +00002275/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002276/// selector-name[opt] ':' objc-keywordexpr
2277///
2278/// objc-keywordexpr:
2279/// nonempty-expr-list
2280///
2281/// nonempty-expr-list:
2282/// assignment-expression
2283/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002284///
John McCall60d7b3a2010-08-24 06:29:42 +00002285ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002286Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002287 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002288 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002289 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002290 InMessageExpressionRAIIObject InMessage(*this, true);
2291
Steve Naroffc4df6d22009-11-07 02:08:14 +00002292 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002293 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002294 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2295 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002296 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002297 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2298 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002299 else
John McCall9ae2f072010-08-23 23:25:46 +00002300 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002301 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002302 cutOffParsing();
2303 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002304 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002305
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002306 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002307 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002308 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002309
Chris Lattner5f9e2722011-07-23 10:55:15 +00002310 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002311 SmallVector<SourceLocation, 12> KeyLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002312 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002313
Chris Lattnerdf195262007-10-09 17:51:17 +00002314 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002315 while (1) {
2316 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002317 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002318 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002319
Chris Lattnerdf195262007-10-09 17:51:17 +00002320 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002321 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002322 // We must manually skip to a ']', otherwise the expression skipper will
2323 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2324 // the enclosing expression.
2325 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002326 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002327 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002328
Steve Naroff68d331a2007-09-27 14:38:14 +00002329 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002330 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002331
2332 if (Tok.is(tok::code_completion)) {
2333 if (SuperLoc.isValid())
2334 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2335 KeyIdents.data(),
2336 KeyIdents.size(),
2337 /*AtArgumentEpression=*/true);
2338 else if (ReceiverType)
2339 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2340 KeyIdents.data(),
2341 KeyIdents.size(),
2342 /*AtArgumentEpression=*/true);
2343 else
2344 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2345 KeyIdents.data(),
2346 KeyIdents.size(),
2347 /*AtArgumentEpression=*/true);
2348
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002349 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002350 return ExprError();
2351 }
2352
John McCall60d7b3a2010-08-24 06:29:42 +00002353 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002354 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002355 // We must manually skip to a ']', otherwise the expression skipper will
2356 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2357 // the enclosing expression.
2358 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002359 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002360 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002361
Steve Naroff37387c92007-09-17 20:25:27 +00002362 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002363 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002364
Douglas Gregord3c68542009-11-19 01:08:35 +00002365 // Code completion after each argument.
2366 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002367 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002368 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002369 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002370 KeyIdents.size(),
2371 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002372 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002373 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002374 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002375 KeyIdents.size(),
2376 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002377 else
John McCall9ae2f072010-08-23 23:25:46 +00002378 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002379 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002380 KeyIdents.size(),
2381 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002382 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002383 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002384 }
2385
Steve Naroff37387c92007-09-17 20:25:27 +00002386 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002387 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002388 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002389 break;
2390 // We have a selector or a colon, continue parsing.
2391 }
2392 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002393 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002394 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002395 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002396 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002397 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002398 // We must manually skip to a ']', otherwise the expression skipper will
2399 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2400 // the enclosing expression.
2401 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002402 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002403 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002404
Steve Naroff49f109c2007-11-15 13:05:42 +00002405 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002406 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002407 }
2408 } else if (!selIdent) {
2409 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002410
Chris Lattner4fef81d2008-08-05 06:19:09 +00002411 // We must manually skip to a ']', otherwise the expression skipper will
2412 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2413 // the enclosing expression.
2414 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002415 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002416 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002417
Chris Lattnerdf195262007-10-09 17:51:17 +00002418 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002419 if (Tok.is(tok::identifier))
2420 Diag(Tok, diag::err_expected_colon);
2421 else
2422 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002423 // We must manually skip to a ']', otherwise the expression skipper will
2424 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2425 // the enclosing expression.
2426 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002427 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002428 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002429
Chris Lattner699b6612008-01-25 18:59:06 +00002430 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002431
Steve Naroff29238a02007-10-05 18:42:47 +00002432 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002433 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002434 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002435 KeyLocs.push_back(Loc);
2436 }
Chris Lattnerff384912007-10-07 02:00:24 +00002437 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002438
Douglas Gregor2725ca82010-04-21 19:57:20 +00002439 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002440 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002441 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002442 MultiExprArg(Actions,
2443 KeyExprs.take(),
2444 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002445 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002446 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002447 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002448 MultiExprArg(Actions,
2449 KeyExprs.take(),
2450 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002451 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002452 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002453 MultiExprArg(Actions,
2454 KeyExprs.take(),
2455 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002456}
2457
John McCall60d7b3a2010-08-24 06:29:42 +00002458ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2459 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002460 if (Res.isInvalid()) return move(Res);
2461
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002462 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2463 // expressions. At this point, we know that the only valid thing that starts
2464 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002465 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002466 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002467 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002468 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002469
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002470 while (Tok.is(tok::at)) {
2471 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002472
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002473 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002474 if (!isTokenStringLiteral())
2475 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002476
John McCall60d7b3a2010-08-24 06:29:42 +00002477 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002478 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002479 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002480
Sebastian Redleffa8d12008-12-10 00:02:53 +00002481 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002482 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002483
2484 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2485 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002486}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002487
2488/// objc-encode-expression:
2489/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002490ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002491Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002492 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002493
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002494 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002495
Chris Lattner4fef81d2008-08-05 06:19:09 +00002496 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002497 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2498
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002499 BalancedDelimiterTracker T(*this, tok::l_paren);
2500 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002501
Douglas Gregor809070a2009-02-18 17:45:20 +00002502 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002503
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002504 T.consumeClose();
Sebastian Redl1d922962008-12-13 15:32:12 +00002505
Douglas Gregor809070a2009-02-18 17:45:20 +00002506 if (Ty.isInvalid())
2507 return ExprError();
2508
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002509 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
2510 T.getOpenLocation(), Ty.get(),
2511 T.getCloseLocation()));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002512}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002513
2514/// objc-protocol-expression
2515/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002516ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002517Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002518 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002519
Chris Lattner4fef81d2008-08-05 06:19:09 +00002520 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002521 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2522
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002523 BalancedDelimiterTracker T(*this, tok::l_paren);
2524 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002525
Chris Lattner4fef81d2008-08-05 06:19:09 +00002526 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002527 return ExprError(Diag(Tok, diag::err_expected_ident));
2528
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002529 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002530 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002531
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002532 T.consumeClose();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002533
Sebastian Redl1d922962008-12-13 15:32:12 +00002534 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002535 T.getOpenLocation(),
2536 T.getCloseLocation()));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002537}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002538
2539/// objc-selector-expression
2540/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002541ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002542 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002543
Chris Lattner4fef81d2008-08-05 06:19:09 +00002544 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002545 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2546
Chris Lattner5f9e2722011-07-23 10:55:15 +00002547 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002548 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002549
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002550 BalancedDelimiterTracker T(*this, tok::l_paren);
2551 T.consumeOpen();
2552
Douglas Gregor458433d2010-08-26 15:07:07 +00002553 if (Tok.is(tok::code_completion)) {
2554 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2555 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002556 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002557 return ExprError();
2558 }
2559
Chris Lattner2fc5c242009-04-11 18:13:45 +00002560 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002561 if (!SelIdent && // missing selector name.
2562 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002563 return ExprError(Diag(Tok, diag::err_expected_ident));
2564
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002565 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002566 unsigned nColons = 0;
2567 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002568 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002569 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2570 ++nColons;
2571 KeyIdents.push_back(0);
2572 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002573 return ExprError(Diag(Tok, diag::err_expected_colon));
2574
Chris Lattner5add7542010-08-27 22:32:41 +00002575 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002576 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002577 if (Tok.is(tok::r_paren))
2578 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002579
2580 if (Tok.is(tok::code_completion)) {
2581 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2582 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002583 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002584 return ExprError();
2585 }
2586
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002587 // Check for another keyword selector.
2588 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002589 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002590 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002591 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002592 break;
2593 }
Steve Naroff887407e2007-12-05 22:21:29 +00002594 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002595 T.consumeClose();
Steve Naroff887407e2007-12-05 22:21:29 +00002596 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002597 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002598 T.getOpenLocation(),
2599 T.getCloseLocation()));
Gabor Greif58065b22007-10-19 15:38:32 +00002600 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002601
2602Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002603
2604 // Save the current token position.
2605 SourceLocation OrigLoc = Tok.getLocation();
2606
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002607 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2608 // Append the current token at the end of the new token stream so that it
2609 // doesn't get lost.
2610 LM.Toks.push_back(Tok);
2611 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2612
2613 // MDecl might be null due to error in method prototype, etc.
2614 Decl *MDecl = LM.D;
2615 // Consume the previously pushed token.
2616 ConsumeAnyToken();
2617
2618 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2619 SourceLocation BraceLoc = Tok.getLocation();
2620 // Enter a scope for the method body.
2621 ParseScope BodyScope(this,
2622 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2623
2624 // Tell the actions module that we have entered a method definition with the
2625 // specified Declarator for the method.
2626 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2627
2628 if (PP.isCodeCompletionEnabled()) {
2629 if (trySkippingFunctionBodyForCodeCompletion()) {
2630 BodyScope.Exit();
2631 return Actions.ActOnFinishFunctionBody(MDecl, 0);
2632 }
2633 }
2634
2635 StmtResult FnBody(ParseCompoundStatementBody());
2636
2637 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko625bb562012-02-14 22:14:32 +00002638 if (FnBody.isInvalid()) {
2639 Sema::CompoundScopeRAII CompoundScope(Actions);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002640 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2641 MultiStmtArg(Actions), false);
Dmitri Gribenko625bb562012-02-14 22:14:32 +00002642 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002643
2644 // Leave the function body scope.
2645 BodyScope.Exit();
2646
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002647 MDecl = Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2648
2649 if (Tok.getLocation() != OrigLoc) {
2650 // Due to parsing error, we either went over the cached tokens or
2651 // there are still cached tokens left. If it's the latter case skip the
2652 // leftover tokens.
2653 // Since this is an uncommon situation that should be avoided, use the
2654 // expensive isBeforeInTranslationUnit call.
2655 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2656 OrigLoc))
2657 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2658 ConsumeAnyToken();
2659 }
2660
2661 return MDecl;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002662}