blob: 3f4f4611fd3c89b77e07b6bab56eda685be89058 [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:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000055 SingleDecl = ParseObjCAtImplementationDeclaration(AtLoc);
56 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000057 case tok::objc_end:
Fariborz Jahanian140ab232011-08-31 17:37:55 +000058 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner5ffb14b2008-08-23 02:02:23 +000059 case tok::objc_compatibility_alias:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000060 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
61 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000062 case tok::objc_synthesize:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000063 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
64 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000065 case tok::objc_dynamic:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000066 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
67 break;
Douglas Gregor5948ae12012-01-03 18:04:46 +000068 case tok::objc_import:
Douglas Gregor94ad28b2012-01-03 18:24:14 +000069 if (getLang().Modules)
70 return ParseModuleImport(AtLoc);
71
72 // Fall through
73
Chris Lattner5ffb14b2008-08-23 02:02:23 +000074 default:
75 Diag(AtLoc, diag::err_unexpected_at);
76 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000077 SingleDecl = 0;
78 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000079 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000080 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +000081}
82
83///
Mike Stump1eb44332009-09-09 15:08:12 +000084/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000085/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000086///
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000087Parser::DeclGroupPtrTy
88Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000089 ConsumeToken(); // the identifier "class"
Chris Lattner5f9e2722011-07-23 10:55:15 +000090 SmallVector<IdentifierInfo *, 8> ClassNames;
91 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremenekc09cba62009-11-17 23:12:20 +000092
Mike Stump1eb44332009-09-09 15:08:12 +000093
Reid Spencer5f016e22007-07-11 17:01:13 +000094 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000095 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000096 Diag(Tok, diag::err_expected_ident);
97 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000098 return Actions.ConvertDeclToDeclGroup(0);
Reid Spencer5f016e22007-07-11 17:01:13 +000099 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +0000101 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Chris Lattnerdf195262007-10-09 17:51:17 +0000104 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 ConsumeToken();
108 }
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 // Consume the ';'.
111 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000112 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremenekc09cba62009-11-17 23:12:20 +0000114 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
115 ClassLocs.data(),
116 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000117}
118
Erik Verbruggend64251f2011-12-06 09:25:23 +0000119void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
120{
121 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
122 if (ock == Sema::OCK_None)
123 return;
124
125 Decl *Decl = Actions.ActOnAtEnd(getCurScope(), AtLoc);
126 Diag(AtLoc, diag::err_objc_missing_end)
127 << FixItHint::CreateInsertion(AtLoc, "@end\n");
128 if (Decl)
129 Diag(Decl->getLocStart(), diag::note_objc_container_start)
130 << (int) ock;
131 if (!PendingObjCImpDecl.empty())
132 PendingObjCImpDecl.pop_back();
133 ObjCImpDecl = 0;
134}
135
Steve Naroffdac269b2007-08-20 21:31:48 +0000136///
137/// objc-interface:
138/// objc-class-interface-attributes[opt] objc-class-interface
139/// objc-category-interface
140///
141/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000142/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000143/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000144/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000145/// objc-interface-decl-list
146/// @end
147///
148/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000149/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000150/// objc-protocol-refs[opt]
151/// objc-interface-decl-list
152/// @end
153///
154/// objc-superclass:
155/// ':' identifier
156///
157/// objc-class-interface-attributes:
158/// __attribute__((visibility("default")))
159/// __attribute__((visibility("hidden")))
160/// __attribute__((deprecated))
161/// __attribute__((unavailable))
162/// __attribute__((objc_exception)) - used by NSException on 64-bit
163///
Erik Verbruggend64251f2011-12-06 09:25:23 +0000164Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +0000165 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000166 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000167 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggend64251f2011-12-06 09:25:23 +0000168 CheckNestedObjCContexts(AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000169 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000171 // Code completion after '@interface'.
172 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000173 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000174 cutOffParsing();
175 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000176 }
177
Chris Lattnerdf195262007-10-09 17:51:17 +0000178 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000179 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000180 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000181 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000182
Steve Naroffdac269b2007-08-20 21:31:48 +0000183 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000184 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000185 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000186 if (Tok.is(tok::l_paren) &&
187 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000188
189 BalancedDelimiterTracker T(*this, tok::l_paren);
190 T.consumeOpen();
191
192 SourceLocation categoryLoc;
Steve Naroffdac269b2007-08-20 21:31:48 +0000193 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000194 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000195 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000196 cutOffParsing();
197 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000198 }
199
Steve Naroff527fe232007-08-23 19:56:30 +0000200 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000201 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000202 categoryId = Tok.getIdentifierInfo();
203 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000204 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000205 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000206 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000207 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000208 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000209
210 T.consumeClose();
211 if (T.getCloseLocation().isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000212 return 0;
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000213
214 if (!attrs.empty()) { // categories don't support attributes.
215 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
216 attrs.clear();
Steve Naroffdac269b2007-08-20 21:31:48 +0000217 }
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000218
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000219 // Next, we need to check for any protocol references.
220 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000221 SmallVector<Decl *, 8> ProtocolRefs;
222 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000223 if (Tok.is(tok::less) &&
224 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000225 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000226 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000227
John McCalld226f652010-08-21 09:40:31 +0000228 Decl *CategoryType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000229 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000230 nameId, nameLoc,
231 categoryId, categoryLoc,
232 ProtocolRefs.data(),
233 ProtocolRefs.size(),
234 ProtocolLocs.data(),
235 EndProtoLoc);
Fariborz Jahaniane6f07f52011-08-19 18:02:47 +0000236
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000237 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000238 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000239
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000240 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000241 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000242 }
243 // Parse a class interface.
244 IdentifierInfo *superClassId = 0;
245 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000246
Chris Lattnerdf195262007-10-09 17:51:17 +0000247 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000248 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000249
250 // Code completion of superclass names.
251 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000252 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000253 cutOffParsing();
254 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000255 }
256
Chris Lattnerdf195262007-10-09 17:51:17 +0000257 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000258 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000259 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000260 }
261 superClassId = Tok.getIdentifierInfo();
262 superClassLoc = ConsumeToken();
263 }
264 // Next, we need to check for any protocol references.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000265 SmallVector<Decl *, 8> ProtocolRefs;
266 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000267 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000268 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000269 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
270 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000271 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
John McCalld226f652010-08-21 09:40:31 +0000273 Decl *ClsType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000274 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000275 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000276 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000277 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +0000278 EndProtoLoc, attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Chris Lattnerdf195262007-10-09 17:51:17 +0000280 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000281 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000282
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000283 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000284 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000285}
286
John McCalld0014542009-12-03 22:31:13 +0000287/// The Objective-C property callback. This should be defined where
288/// it's used, but instead it's been lifted to here to support VS2005.
289struct Parser::ObjCPropertyCallback : FieldCallback {
David Blaikie99ba9e32011-12-20 02:48:34 +0000290private:
291 virtual void anchor();
292public:
John McCalld0014542009-12-03 22:31:13 +0000293 Parser &P;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000294 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000295 ObjCDeclSpec &OCDS;
296 SourceLocation AtLoc;
297 tok::ObjCKeywordKind MethodImplKind;
298
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000299 ObjCPropertyCallback(Parser &P,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000300 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000301 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
302 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000303 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCalld0014542009-12-03 22:31:13 +0000304 MethodImplKind(MethodImplKind) {
305 }
306
John McCalld226f652010-08-21 09:40:31 +0000307 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000308 if (FD.D.getIdentifier() == 0) {
309 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
310 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000311 return 0;
John McCalld0014542009-12-03 22:31:13 +0000312 }
313 if (FD.BitfieldSize) {
314 P.Diag(AtLoc, diag::err_objc_property_bitfield)
315 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000316 return 0;
John McCalld0014542009-12-03 22:31:13 +0000317 }
318
319 // Install the property declarator into interfaceDecl.
320 IdentifierInfo *SelName =
321 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
322
323 Selector GetterSel =
324 P.PP.getSelectorTable().getNullarySelector(SelName);
325 IdentifierInfo *SetterName = OCDS.getSetterName();
326 Selector SetterSel;
327 if (SetterName)
328 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
329 else
330 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
331 P.PP.getSelectorTable(),
332 FD.D.getIdentifier());
333 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000334 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000335 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000336 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000337 &isOverridingProperty,
338 MethodImplKind);
339 if (!isOverridingProperty)
340 Props.push_back(Property);
341
342 return Property;
343 }
344};
345
David Blaikie99ba9e32011-12-20 02:48:34 +0000346void Parser::ObjCPropertyCallback::anchor() {
347}
348
Steve Naroffdac269b2007-08-20 21:31:48 +0000349/// objc-interface-decl-list:
350/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000351/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000352/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000353/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000354/// objc-interface-decl-list declaration
355/// objc-interface-decl-list ';'
356///
Steve Naroff294494e2007-08-22 16:35:03 +0000357/// objc-method-requirement: [OBJC2]
358/// @required
359/// @optional
360///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000361void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
362 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000363 SmallVector<Decl *, 32> allMethods;
364 SmallVector<Decl *, 16> allProperties;
365 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000366 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek782f2f52010-01-07 01:20:12 +0000368 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000369
Steve Naroff294494e2007-08-22 16:35:03 +0000370 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000371 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000372 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000373 Decl *methodPrototype =
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000374 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000375 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000376 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
377 // method definitions.
Argyrios Kyrtzidis0db9f4d2011-12-17 04:13:22 +0000378 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
379 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
380 SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
381 if (Tok.is(tok::semi))
382 ConsumeToken();
383 }
Steve Naroff294494e2007-08-22 16:35:03 +0000384 continue;
385 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000386 if (Tok.is(tok::l_paren)) {
387 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000388 ParseObjCMethodDecl(Tok.getLocation(),
389 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000390 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000391 continue;
392 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000393 // Ignore excess semicolons.
394 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000395 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000396 continue;
397 }
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Chris Lattnerbc662af2008-10-20 06:10:06 +0000399 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000400 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000401 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000403 // Code completion within an Objective-C interface.
404 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000405 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000406 ObjCImpDecl? Sema::PCC_ObjCImplementation
407 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000408 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000409 }
410
Chris Lattnere82a10f2008-10-20 05:46:22 +0000411 // If we don't have an @ directive, parse it as a function definition.
412 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000413 // The code below does not consume '}'s because it is afraid of eating the
414 // end of a namespace. Because of the way this code is structured, an
415 // erroneous r_brace would cause an infinite loop if not handled here.
416 if (Tok.is(tok::r_brace))
417 break;
John McCall0b7e6782011-03-24 11:26:52 +0000418 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000419 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000420 continue;
421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Chris Lattnere82a10f2008-10-20 05:46:22 +0000423 // Otherwise, we have an @ directive, eat the @.
424 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000425 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000426 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000427 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000428 }
429
Chris Lattnera2449b22008-10-20 05:57:40 +0000430 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Chris Lattnera2449b22008-10-20 05:57:40 +0000432 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000433 AtEnd.setBegin(AtLoc);
434 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000435 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000436 } else if (DirectiveKind == tok::objc_not_keyword) {
437 Diag(Tok, diag::err_objc_unknown_at);
438 SkipUntil(tok::semi);
439 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000440 }
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Chris Lattnerbc662af2008-10-20 06:10:06 +0000442 // Eat the identifier.
443 ConsumeToken();
444
Chris Lattnera2449b22008-10-20 05:57:40 +0000445 switch (DirectiveKind) {
446 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000447 // FIXME: If someone forgets an @end on a protocol, this loop will
448 // continue to eat up tons of stuff and spew lots of nonsense errors. It
449 // would probably be better to bail out if we saw an @class or @interface
450 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000451 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000452 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000453 SkipUntil(tok::r_brace, tok::at);
454 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000455
456 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000457 case tok::objc_interface:
Erik Verbruggend64251f2011-12-06 09:25:23 +0000458 Diag(AtLoc, diag::err_objc_missing_end)
459 << FixItHint::CreateInsertion(AtLoc, "@end\n");
460 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
461 << (int) Actions.getObjCContainerKind();
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000462 ConsumeToken();
463 break;
464
Chris Lattnera2449b22008-10-20 05:57:40 +0000465 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000466 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000467 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000468 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000469 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000470 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000471 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000472 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000473 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Chris Lattnera2449b22008-10-20 05:57:40 +0000475 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000476 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000477 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000478
Chris Lattnere82a10f2008-10-20 05:46:22 +0000479 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000480 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000481 if (Tok.is(tok::l_paren))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000482 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000484 ObjCPropertyCallback Callback(*this, allProperties,
John McCalld0014542009-12-03 22:31:13 +0000485 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000486
Chris Lattnere82a10f2008-10-20 05:46:22 +0000487 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +0000488 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +0000489 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000490
John McCall7da19ea2011-03-26 01:53:26 +0000491 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000492 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000493 }
Steve Naroff294494e2007-08-22 16:35:03 +0000494 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000495
496 // We break out of the big loop in two cases: when we see @end or when we see
497 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000498 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000499 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000500 return cutOffParsing();
Erik Verbruggend64251f2011-12-06 09:25:23 +0000501 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerbc662af2008-10-20 06:10:06 +0000502 ConsumeToken(); // the "end" identifier
Erik Verbruggend64251f2011-12-06 09:25:23 +0000503 } else {
504 Diag(Tok, diag::err_objc_missing_end)
505 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
506 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
507 << (int) Actions.getObjCContainerKind();
508 AtEnd.setBegin(Tok.getLocation());
509 AtEnd.setEnd(Tok.getLocation());
510 }
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Chris Lattnera2449b22008-10-20 05:57:40 +0000512 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000513 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000514 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000515 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000516 allProperties.data(), allProperties.size(),
517 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000518}
519
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000520/// Parse property attribute declarations.
521///
522/// property-attr-decl: '(' property-attrlist ')'
523/// property-attrlist:
524/// property-attribute
525/// property-attrlist ',' property-attribute
526/// property-attribute:
527/// getter '=' identifier
528/// setter '=' identifier ':'
529/// readonly
530/// readwrite
531/// assign
532/// retain
533/// copy
534/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000535/// atomic
536/// strong
537/// weak
538/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000539///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000540void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000541 assert(Tok.getKind() == tok::l_paren);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000542 BalancedDelimiterTracker T(*this, tok::l_paren);
543 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000545 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000546 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000547 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000548 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000549 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000550 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000552 // If this is not an identifier at all, bail out early.
553 if (II == 0) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000554 T.consumeClose();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000555 return;
556 }
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattner156b0612008-10-20 07:37:22 +0000558 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Chris Lattner92e62b02008-11-20 04:42:34 +0000560 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000561 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000562 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000563 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000564 else if (II->isStr("unsafe_unretained"))
565 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000566 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000567 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000568 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000569 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000570 else if (II->isStr("strong"))
571 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000572 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000573 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000574 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000575 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000576 else if (II->isStr("atomic"))
577 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000578 else if (II->isStr("weak"))
579 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000580 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000581 bool IsSetter = II->getNameStart()[0] == 's';
582
Chris Lattnere00da7c2008-10-20 07:39:53 +0000583 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000584 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
585 diag::err_objc_expected_equal_for_getter;
586
587 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000588 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Douglas Gregor4ad96852009-11-19 07:41:15 +0000590 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000591 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000592 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000593 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000594 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000595 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000596 }
597
Anders Carlsson42499be2010-10-02 17:45:21 +0000598
599 SourceLocation SelLoc;
600 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
601
602 if (!SelIdent) {
603 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
604 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000605 SkipUntil(tok::r_paren);
606 return;
607 }
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Anders Carlsson42499be2010-10-02 17:45:21 +0000609 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000610 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000611 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000613 if (ExpectAndConsume(tok::colon,
614 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000615 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000616 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000617 } else {
618 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000619 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000620 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000621 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000622 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000623 SkipUntil(tok::r_paren);
624 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000625 }
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Chris Lattner156b0612008-10-20 07:37:22 +0000627 if (Tok.isNot(tok::comma))
628 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Chris Lattner156b0612008-10-20 07:37:22 +0000630 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000631 }
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000633 T.consumeClose();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000634}
635
Steve Naroff3536b442007-09-06 21:24:23 +0000636/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000637/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000638/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000639///
640/// objc-instance-method: '-'
641/// objc-class-method: '+'
642///
Steve Naroff4985ace2007-08-22 18:35:33 +0000643/// objc-method-attributes: [OBJC2]
644/// __attribute__((deprecated))
645///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000646Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000647 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000648 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000649
Mike Stump1eb44332009-09-09 15:08:12 +0000650 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000651 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000652 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000653 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000654 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000655 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000656 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000657}
658
659/// objc-selector:
660/// identifier
661/// one of
662/// enum struct union if else while do for switch case default
663/// break continue return goto asm sizeof typeof __alignof
664/// unsigned long const short volatile signed restrict _Complex
665/// in out inout bycopy byref oneway int char float double void _Bool
666///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000667IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000668
Chris Lattnerff384912007-10-07 02:00:24 +0000669 switch (Tok.getKind()) {
670 default:
671 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000672 case tok::ampamp:
673 case tok::ampequal:
674 case tok::amp:
675 case tok::pipe:
676 case tok::tilde:
677 case tok::exclaim:
678 case tok::exclaimequal:
679 case tok::pipepipe:
680 case tok::pipeequal:
681 case tok::caret:
682 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000683 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000684 if (isalpha(ThisTok[0])) {
685 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
686 Tok.setKind(tok::identifier);
687 SelectorLoc = ConsumeToken();
688 return II;
689 }
690 return 0;
691 }
692
Chris Lattnerff384912007-10-07 02:00:24 +0000693 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000694 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000695 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000696 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000697 case tok::kw_break:
698 case tok::kw_case:
699 case tok::kw_catch:
700 case tok::kw_char:
701 case tok::kw_class:
702 case tok::kw_const:
703 case tok::kw_const_cast:
704 case tok::kw_continue:
705 case tok::kw_default:
706 case tok::kw_delete:
707 case tok::kw_do:
708 case tok::kw_double:
709 case tok::kw_dynamic_cast:
710 case tok::kw_else:
711 case tok::kw_enum:
712 case tok::kw_explicit:
713 case tok::kw_export:
714 case tok::kw_extern:
715 case tok::kw_false:
716 case tok::kw_float:
717 case tok::kw_for:
718 case tok::kw_friend:
719 case tok::kw_goto:
720 case tok::kw_if:
721 case tok::kw_inline:
722 case tok::kw_int:
723 case tok::kw_long:
724 case tok::kw_mutable:
725 case tok::kw_namespace:
726 case tok::kw_new:
727 case tok::kw_operator:
728 case tok::kw_private:
729 case tok::kw_protected:
730 case tok::kw_public:
731 case tok::kw_register:
732 case tok::kw_reinterpret_cast:
733 case tok::kw_restrict:
734 case tok::kw_return:
735 case tok::kw_short:
736 case tok::kw_signed:
737 case tok::kw_sizeof:
738 case tok::kw_static:
739 case tok::kw_static_cast:
740 case tok::kw_struct:
741 case tok::kw_switch:
742 case tok::kw_template:
743 case tok::kw_this:
744 case tok::kw_throw:
745 case tok::kw_true:
746 case tok::kw_try:
747 case tok::kw_typedef:
748 case tok::kw_typeid:
749 case tok::kw_typename:
750 case tok::kw_typeof:
751 case tok::kw_union:
752 case tok::kw_unsigned:
753 case tok::kw_using:
754 case tok::kw_virtual:
755 case tok::kw_void:
756 case tok::kw_volatile:
757 case tok::kw_wchar_t:
758 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000759 case tok::kw__Bool:
760 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000761 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000762 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000763 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000764 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000765 }
Steve Naroff294494e2007-08-22 16:35:03 +0000766}
767
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000768/// objc-for-collection-in: 'in'
769///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000770bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000771 // FIXME: May have to do additional look-ahead to only allow for
772 // valid tokens following an 'in'; such as an identifier, unary operators,
773 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000774 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000775 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000776}
777
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000778/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000779/// qualifier list and builds their bitmask representation in the input
780/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000781///
782/// objc-type-qualifiers:
783/// objc-type-qualifier
784/// objc-type-qualifiers objc-type-qualifier
785///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000786void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000787 Declarator::TheContext Context) {
788 assert(Context == Declarator::ObjCParameterContext ||
789 Context == Declarator::ObjCResultContext);
790
Chris Lattnere8b724d2007-12-12 06:56:32 +0000791 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000792 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000793 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCallcdda47f2011-10-01 09:56:14 +0000794 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000795 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000796 }
797
Chris Lattnercb53b362007-12-27 19:57:00 +0000798 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000799 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Chris Lattnere8b724d2007-12-12 06:56:32 +0000801 const IdentifierInfo *II = Tok.getIdentifierInfo();
802 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000803 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000804 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000806 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000807 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000808 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000809 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
810 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
811 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
812 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
813 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
814 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000815 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000816 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000817 ConsumeToken();
818 II = 0;
819 break;
820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Chris Lattnere8b724d2007-12-12 06:56:32 +0000822 // If this wasn't a recognized qualifier, bail out.
823 if (II) return;
824 }
825}
826
John McCallcdda47f2011-10-01 09:56:14 +0000827/// Take all the decl attributes out of the given list and add
828/// them to the given attribute set.
829static void takeDeclAttributes(ParsedAttributes &attrs,
830 AttributeList *list) {
831 while (list) {
832 AttributeList *cur = list;
833 list = cur->getNext();
834
835 if (!cur->isUsedAsTypeAttr()) {
836 // Clear out the next pointer. We're really completely
837 // destroying the internal invariants of the declarator here,
838 // but it doesn't matter because we're done with it.
839 cur->setNext(0);
840 attrs.add(cur);
841 }
842 }
843}
844
845/// takeDeclAttributes - Take all the decl attributes from the given
846/// declarator and add them to the given list.
847static void takeDeclAttributes(ParsedAttributes &attrs,
848 Declarator &D) {
849 // First, take ownership of all attributes.
850 attrs.getPool().takeAllFrom(D.getAttributePool());
851 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
852
853 // Now actually move the attributes over.
854 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
855 takeDeclAttributes(attrs, D.getAttributes());
856 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
857 takeDeclAttributes(attrs,
858 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
859}
860
Chris Lattnere8b724d2007-12-12 06:56:32 +0000861/// objc-type-name:
862/// '(' objc-type-qualifiers[opt] type-name ')'
863/// '(' objc-type-qualifiers[opt] ')'
864///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000865ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000866 Declarator::TheContext context,
867 ParsedAttributes *paramAttrs) {
868 assert(context == Declarator::ObjCParameterContext ||
869 context == Declarator::ObjCResultContext);
870 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
871
Chris Lattnerdf195262007-10-09 17:51:17 +0000872 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000874 BalancedDelimiterTracker T(*this, tok::l_paren);
875 T.consumeOpen();
876
Chris Lattnere8904e92008-08-23 01:48:03 +0000877 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000878 ObjCDeclContextSwitch ObjCDC(*this);
879
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000880 // Parse type qualifiers, in, inout, etc.
John McCallcdda47f2011-10-01 09:56:14 +0000881 ParseObjCTypeQualifierList(DS, context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000882
John McCallb3d87482010-08-24 05:47:05 +0000883 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000884 if (isTypeSpecifierQualifier()) {
John McCallcdda47f2011-10-01 09:56:14 +0000885 // Parse an abstract declarator.
886 DeclSpec declSpec(AttrFactory);
887 declSpec.setObjCQualifiers(&DS);
888 ParseSpecifierQualifierList(declSpec);
889 Declarator declarator(declSpec, context);
890 ParseDeclarator(declarator);
891
892 // If that's not invalid, extract a type.
893 if (!declarator.isInvalidType()) {
894 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
895 if (!type.isInvalid())
896 Ty = type.get();
897
898 // If we're parsing a parameter, steal all the decl attributes
899 // and add them to the decl spec.
900 if (context == Declarator::ObjCParameterContext)
901 takeDeclAttributes(*paramAttrs, declarator);
902 }
903 } else if (context == Declarator::ObjCResultContext &&
904 Tok.is(tok::identifier)) {
Douglas Gregore97179c2011-09-08 01:46:34 +0000905 if (!Ident_instancetype)
906 Ident_instancetype = PP.getIdentifierInfo("instancetype");
907
908 if (Tok.getIdentifierInfo() == Ident_instancetype) {
909 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
910 ConsumeToken();
911 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000912 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000913
Steve Naroffd7333c22008-10-21 14:15:04 +0000914 if (Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000915 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000916 else if (Tok.getLocation() == TypeStartLoc) {
917 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000918 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000919 SkipUntil(tok::r_paren);
920 } else {
921 // Otherwise, we found *something*, but didn't get a ')' in the right
922 // place. Emit an error then return what we have as the type.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000923 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000924 }
Steve Narofff28b2642007-09-05 23:30:30 +0000925 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000926}
927
928/// objc-method-decl:
929/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000930/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000931/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000932/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000933///
934/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000935/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000936/// objc-keyword-selector objc-keyword-decl
937///
938/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000939/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
940/// objc-selector ':' objc-keyword-attributes[opt] identifier
941/// ':' objc-type-name objc-keyword-attributes[opt] identifier
942/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000943///
Steve Naroff4985ace2007-08-22 18:35:33 +0000944/// objc-parmlist:
945/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000946///
Steve Naroff4985ace2007-08-22 18:35:33 +0000947/// objc-parms:
948/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000949///
Steve Naroff4985ace2007-08-22 18:35:33 +0000950/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000951/// , ...
952///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000953/// objc-keyword-attributes: [OBJC2]
954/// __attribute__((unused))
955///
John McCalld226f652010-08-21 09:40:31 +0000956Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000957 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000958 tok::ObjCKeywordKind MethodImplKind,
959 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000960 ParsingDeclRAIIObject PD(*this);
961
Douglas Gregore8f5a172010-04-07 00:21:17 +0000962 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000963 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000964 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000965 cutOffParsing();
966 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000967 }
968
Chris Lattnere8904e92008-08-23 01:48:03 +0000969 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000970 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000971 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000972 if (Tok.is(tok::l_paren))
John McCallcdda47f2011-10-01 09:56:14 +0000973 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Ted Kremenek9e049352010-02-18 23:05:16 +0000975 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000976 ParsedAttributes methodAttrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000977 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000978 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000979
Douglas Gregore8f5a172010-04-07 00:21:17 +0000980 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000981 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000982 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000983 cutOffParsing();
984 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000985 }
986
Ted Kremenek9e049352010-02-18 23:05:16 +0000987 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000988 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000989 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000990
Steve Naroff84c43102009-02-11 20:43:13 +0000991 // An unnamed colon is valid.
992 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000993 Diag(Tok, diag::err_expected_selector_for_method)
994 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000995 // Skip until we get a ; or {}.
996 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000997 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +0000998 }
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Chris Lattner5f9e2722011-07-23 10:55:15 +00001000 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +00001001 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001002 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001003 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001004 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Chris Lattnerff384912007-10-07 02:00:24 +00001006 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +00001007 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001008 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001009 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001010 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001011 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001012 methodAttrs.getList(), MethodImplKind,
1013 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +00001014 PD.complete(Result);
1015 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +00001016 }
Steve Narofff28b2642007-09-05 23:30:30 +00001017
Chris Lattner5f9e2722011-07-23 10:55:15 +00001018 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001019 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001020 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001021 ParseScope PrototypeScope(this,
1022 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +00001023
1024 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001025
Chris Lattnerff384912007-10-07 02:00:24 +00001026 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +00001027 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +00001028 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Chris Lattnerff384912007-10-07 02:00:24 +00001030 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +00001031 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001032 Diag(Tok, diag::err_expected_colon);
1033 break;
1034 }
1035 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001036
John McCallb3d87482010-08-24 05:47:05 +00001037 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001038 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCallcdda47f2011-10-01 09:56:14 +00001039 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1040 Declarator::ObjCParameterContext,
1041 &paramAttrs);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001042
Chris Lattnerff384912007-10-07 02:00:24 +00001043 // If attributes exist before the argument name, parse them.
John McCallcdda47f2011-10-01 09:56:14 +00001044 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnere294d3f2009-04-11 18:57:04 +00001045 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +00001046 if (getLang().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +00001047 MaybeParseGNUAttributes(paramAttrs);
1048 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +00001049 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001050
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001051 // Code completion for the next piece of the selector.
1052 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001053 KeyIdents.push_back(SelIdent);
1054 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1055 mType == tok::minus,
1056 /*AtParameterName=*/true,
1057 ReturnType,
1058 KeyIdents.data(),
1059 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001060 cutOffParsing();
1061 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001062 }
1063
Chris Lattnerdf195262007-10-09 17:51:17 +00001064 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001065 Diag(Tok, diag::err_expected_ident); // missing argument name.
1066 break;
Steve Naroff4985ace2007-08-22 18:35:33 +00001067 }
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Chris Lattnere294d3f2009-04-11 18:57:04 +00001069 ArgInfo.Name = Tok.getIdentifierInfo();
1070 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +00001071 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Chris Lattnere294d3f2009-04-11 18:57:04 +00001073 ArgInfos.push_back(ArgInfo);
1074 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001075 KeyLocs.push_back(selLoc);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001076
John McCall0b7e6782011-03-24 11:26:52 +00001077 // Make sure the attributes persist.
1078 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1079
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001080 // Code completion for the next piece of the selector.
1081 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001082 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1083 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001084 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001085 ReturnType,
1086 KeyIdents.data(),
1087 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001088 cutOffParsing();
1089 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001090 }
1091
Chris Lattnerff384912007-10-07 02:00:24 +00001092 // Check for another keyword selector.
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001093 SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001094 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +00001095 break;
1096 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +00001097 }
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Steve Naroff335eafa2007-11-15 12:35:21 +00001099 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Chris Lattnerff384912007-10-07 02:00:24 +00001101 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +00001102 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001103 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001104 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001105 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001106 ConsumeToken();
1107 break;
1108 }
John McCall0b7e6782011-03-24 11:26:52 +00001109 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001110 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001111 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001112 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1113 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001114 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001115 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001116 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1117 ParmDecl.getIdentifierLoc(),
1118 Param,
1119 0));
1120
Chris Lattnerff384912007-10-07 02:00:24 +00001121 }
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001123 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001124 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001125 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001126 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001127
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001128 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001129 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001130
Chris Lattnerff384912007-10-07 02:00:24 +00001131 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1132 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001133 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001134 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001135 mType, DSRet, ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001136 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001137 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001138 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001139 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001140
John McCall54abf7d2009-11-04 02:18:39 +00001141 PD.complete(Result);
1142 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001143}
1144
Steve Naroffdac269b2007-08-20 21:31:48 +00001145/// objc-protocol-refs:
1146/// '<' identifier-list '>'
1147///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001148bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001149ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1150 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001151 bool WarnOnDeclarations,
1152 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001153 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001155 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Chris Lattner5f9e2722011-07-23 10:55:15 +00001157 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Chris Lattnere13b9592008-07-26 04:03:38 +00001159 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001160 if (Tok.is(tok::code_completion)) {
1161 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1162 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001163 cutOffParsing();
1164 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001165 }
1166
Chris Lattnere13b9592008-07-26 04:03:38 +00001167 if (Tok.isNot(tok::identifier)) {
1168 Diag(Tok, diag::err_expected_ident);
1169 SkipUntil(tok::greater);
1170 return true;
1171 }
1172 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1173 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001174 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001175 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Chris Lattnere13b9592008-07-26 04:03:38 +00001177 if (Tok.isNot(tok::comma))
1178 break;
1179 ConsumeToken();
1180 }
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Chris Lattnere13b9592008-07-26 04:03:38 +00001182 // Consume the '>'.
1183 if (Tok.isNot(tok::greater)) {
1184 Diag(Tok, diag::err_expected_greater);
1185 return true;
1186 }
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Chris Lattnere13b9592008-07-26 04:03:38 +00001188 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Chris Lattnere13b9592008-07-26 04:03:38 +00001190 // Convert the list of protocols identifiers into a list of protocol decls.
1191 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1192 &ProtocolIdents[0], ProtocolIdents.size(),
1193 Protocols);
1194 return false;
1195}
1196
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001197/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1198/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001199bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001200 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1201 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1202 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001203 SmallVector<Decl *, 8> ProtocolDecl;
1204 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001205 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1206 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001207 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1208 ProtocolLocs.data(), LAngleLoc);
1209 if (EndProtoLoc.isValid())
1210 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001211 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001212}
1213
1214
Steve Naroffdac269b2007-08-20 21:31:48 +00001215/// objc-class-instance-variables:
1216/// '{' objc-instance-variable-decl-list[opt] '}'
1217///
1218/// objc-instance-variable-decl-list:
1219/// objc-visibility-spec
1220/// objc-instance-variable-decl ';'
1221/// ';'
1222/// objc-instance-variable-decl-list objc-visibility-spec
1223/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1224/// objc-instance-variable-decl-list ';'
1225///
1226/// objc-visibility-spec:
1227/// @private
1228/// @protected
1229/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001230/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001231///
1232/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001233/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001234///
John McCalld226f652010-08-21 09:40:31 +00001235void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001236 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001237 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001238 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001239 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001240
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001241 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001242 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor72de6672009-01-08 20:45:30 +00001243
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001244 BalancedDelimiterTracker T(*this, tok::l_brace);
1245 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Steve Naroffddbff782007-08-21 21:17:12 +00001247 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001248 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001249 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Steve Naroffddbff782007-08-21 21:17:12 +00001251 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001252 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001253 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001254 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001255 ConsumeToken();
1256 continue;
1257 }
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Steve Naroffddbff782007-08-21 21:17:12 +00001259 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001260 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001261 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001262
1263 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001264 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001265 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001266 }
1267
Steve Naroff861cf3e2007-08-23 18:16:40 +00001268 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001269 case tok::objc_private:
1270 case tok::objc_public:
1271 case tok::objc_protected:
1272 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001273 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001274 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001275 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001276 default:
1277 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001278 continue;
1279 }
1280 }
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001282 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001283 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001284 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001285 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001286 }
1287
John McCallbdd563e2009-11-03 02:38:08 +00001288 struct ObjCIvarCallback : FieldCallback {
1289 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001290 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001291 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001292 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001293
John McCalld226f652010-08-21 09:40:31 +00001294 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001295 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001296 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1297 }
1298
John McCalld226f652010-08-21 09:40:31 +00001299 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001300 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001301 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001302 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001303 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001304 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001305 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001306 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001307 if (Field)
1308 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001309 return Field;
1310 }
1311 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001312
Chris Lattnere1359422008-04-10 06:46:29 +00001313 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00001314 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +00001315 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Chris Lattnerdf195262007-10-09 17:51:17 +00001317 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001318 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001319 } else {
1320 Diag(Tok, diag::err_expected_semi_decl_list);
1321 // Skip to end of block or statement
1322 SkipUntil(tok::r_brace, true, true);
1323 }
1324 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001325 T.consumeClose();
1326
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001327 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001328 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001329 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff8749be52007-10-31 22:11:35 +00001330 // Call ActOnFields() even if we don't have any decls. This is useful
1331 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001332 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie77b6de02011-09-22 02:58:26 +00001333 AllIvarDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001334 T.getOpenLocation(), T.getCloseLocation(), 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001335 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001336}
Steve Naroffdac269b2007-08-20 21:31:48 +00001337
1338/// objc-protocol-declaration:
1339/// objc-protocol-definition
1340/// objc-protocol-forward-reference
1341///
1342/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001343/// @protocol identifier
1344/// objc-protocol-refs[opt]
1345/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001346/// @end
1347///
1348/// objc-protocol-forward-reference:
1349/// @protocol identifier-list ';'
1350///
1351/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001352/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001353/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001354Parser::DeclGroupPtrTy
1355Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1356 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001357 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001358 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1359 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001360
Douglas Gregor083128f2009-11-18 04:49:41 +00001361 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001362 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001363 cutOffParsing();
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001364 return DeclGroupPtrTy();
Douglas Gregor083128f2009-11-18 04:49:41 +00001365 }
1366
Chris Lattnerdf195262007-10-09 17:51:17 +00001367 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001368 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001369 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001370 }
1371 // Save the protocol name, then consume it.
1372 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1373 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Chris Lattnerdf195262007-10-09 17:51:17 +00001375 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001376 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001377 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001378 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001379 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001380 }
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Erik Verbruggen90ec96f2011-12-08 09:58:43 +00001382 CheckNestedObjCContexts(AtLoc);
1383
Chris Lattnerdf195262007-10-09 17:51:17 +00001384 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001385 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001386 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1387
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001388 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001389 while (1) {
1390 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001391 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001392 Diag(Tok, diag::err_expected_ident);
1393 SkipUntil(tok::semi);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001394 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001395 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001396 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1397 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001398 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Chris Lattnerdf195262007-10-09 17:51:17 +00001400 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001401 break;
1402 }
1403 // Consume the ';'.
1404 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001405 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001406
Steve Naroffe440eb82007-10-10 17:32:04 +00001407 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001408 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001409 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001410 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001411 }
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001413 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001414 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001415
Chris Lattner5f9e2722011-07-23 10:55:15 +00001416 SmallVector<Decl *, 8> ProtocolRefs;
1417 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001418 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001419 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1420 LAngleLoc, EndProtoLoc))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001421 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001422
John McCalld226f652010-08-21 09:40:31 +00001423 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001424 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001425 ProtocolRefs.data(),
1426 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001427 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001428 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001429
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001430 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001431 return Actions.ConvertDeclToDeclGroup(ProtoType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001432}
Steve Naroffdac269b2007-08-20 21:31:48 +00001433
1434/// objc-implementation:
1435/// objc-class-implementation-prologue
1436/// objc-category-implementation-prologue
1437///
1438/// objc-class-implementation-prologue:
1439/// @implementation identifier objc-superclass[opt]
1440/// objc-class-instance-variables[opt]
1441///
1442/// objc-category-implementation-prologue:
1443/// @implementation identifier ( identifier )
Erik Verbruggend64251f2011-12-06 09:25:23 +00001444Decl *Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001445 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1446 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggend64251f2011-12-06 09:25:23 +00001447 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001448 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001450 // Code completion after '@implementation'.
1451 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001452 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001453 cutOffParsing();
1454 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001455 }
1456
Chris Lattnerdf195262007-10-09 17:51:17 +00001457 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001458 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001459 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001460 }
1461 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001462 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001463 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001464
1465 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001466 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001467 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001468 SourceLocation categoryLoc, rparenLoc;
1469 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001471 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001472 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001473 cutOffParsing();
1474 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001475 }
1476
Chris Lattnerdf195262007-10-09 17:51:17 +00001477 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001478 categoryId = Tok.getIdentifierInfo();
1479 categoryLoc = ConsumeToken();
1480 } else {
1481 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001482 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001483 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001484 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001485 Diag(Tok, diag::err_expected_rparen);
1486 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001487 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001488 }
1489 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001490 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001491 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001492 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001493
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001494 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001495 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001496 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001497 }
1498 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001499 SourceLocation superClassLoc;
1500 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001501 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001502 // We have a super class
1503 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001504 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001505 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001506 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001507 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001508 superClassId = Tok.getIdentifierInfo();
1509 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001510 }
John McCalld226f652010-08-21 09:40:31 +00001511 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001512 AtLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001513 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Steve Naroff60fccee2007-10-29 21:38:07 +00001515 if (Tok.is(tok::l_brace)) // we have ivars
Erik Verbruggend64251f2011-12-06 09:25:23 +00001516 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, AtLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001517
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001518 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001519 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001520 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001521}
Steve Naroff60fccee2007-10-29 21:38:07 +00001522
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001523Parser::DeclGroupPtrTy
1524Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001525 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1526 "ParseObjCAtEndDeclaration(): Expected @end");
1527 ConsumeToken(); // the "end" identifier
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001528 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001529 Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001530 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1531 Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
Argyrios Kyrtzidis35593a92011-11-16 02:35:10 +00001532 if (D)
1533 DeclsInGroup.push_back(D);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001534 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001535 DeclsInGroup.push_back(ObjCImpDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001536
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001537 if (ObjCImpDecl) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001538 Actions.ActOnAtEnd(getCurScope(), atEnd);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001539 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001540 }
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001541 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001542 // missing @implementation
Erik Verbruggend64251f2011-12-06 09:25:23 +00001543 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001544
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001545 clearLateParsedObjCMethods();
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001546 ObjCImpDecl = 0;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001547 return Actions.BuildDeclaratorGroup(
1548 DeclsInGroup.data(), DeclsInGroup.size(), false);
Steve Naroffdac269b2007-08-20 21:31:48 +00001549}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001550
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001551Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1552 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001553 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001554 return Actions.ConvertDeclToDeclGroup(0);
Erik Verbruggend64251f2011-12-06 09:25:23 +00001555
John McCalld226f652010-08-21 09:40:31 +00001556 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Erik Verbruggend64251f2011-12-06 09:25:23 +00001557 Actions.ActOnAtEnd(getCurScope(), SourceRange(Tok.getLocation()));
1558 Diag(Tok, diag::err_objc_missing_end)
1559 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
1560 if (ImpDecl)
1561 Diag(ImpDecl->getLocStart(), diag::note_objc_container_start)
1562 << Sema::OCK_Implementation;
1563
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001564 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1565}
1566
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001567void Parser::clearLateParsedObjCMethods() {
1568 for (LateParsedObjCMethodContainer::iterator
1569 I = LateParsedObjCMethods.begin(),
1570 E = LateParsedObjCMethods.end(); I != E; ++I)
1571 delete *I;
1572 LateParsedObjCMethods.clear();
1573}
1574
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001575/// compatibility-alias-decl:
1576/// @compatibility_alias alias-name class-name ';'
1577///
John McCalld226f652010-08-21 09:40:31 +00001578Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001579 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1580 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1581 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001582 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001583 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001584 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001585 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001586 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1587 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001588 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001589 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001590 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001591 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001592 IdentifierInfo *classId = Tok.getIdentifierInfo();
1593 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001594 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1595 "@compatibility_alias");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001596 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1597 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001598}
1599
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001600/// property-synthesis:
1601/// @synthesize property-ivar-list ';'
1602///
1603/// property-ivar-list:
1604/// property-ivar
1605/// property-ivar-list ',' property-ivar
1606///
1607/// property-ivar:
1608/// identifier
1609/// identifier '=' identifier
1610///
John McCalld226f652010-08-21 09:40:31 +00001611Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001612 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1613 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001614 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Douglas Gregorb328c422009-11-18 19:45:45 +00001616 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001617 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001618 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001619 cutOffParsing();
1620 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001621 }
1622
Douglas Gregorb328c422009-11-18 19:45:45 +00001623 if (Tok.isNot(tok::identifier)) {
1624 Diag(Tok, diag::err_synthesized_property_name);
1625 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001626 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001627 }
1628
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001629 IdentifierInfo *propertyIvar = 0;
1630 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1631 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001632 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001633 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001634 // property '=' ivar-name
1635 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001636
1637 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001638 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001639 cutOffParsing();
1640 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001641 }
1642
Chris Lattnerdf195262007-10-09 17:51:17 +00001643 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001644 Diag(Tok, diag::err_expected_ident);
1645 break;
1646 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001647 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001648 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001649 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001650 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001651 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001652 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001653 break;
1654 ConsumeToken(); // consume ','
1655 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001656 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001657 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001658}
1659
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001660/// property-dynamic:
1661/// @dynamic property-list
1662///
1663/// property-list:
1664/// identifier
1665/// property-list ',' identifier
1666///
John McCalld226f652010-08-21 09:40:31 +00001667Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001668 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1669 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001670 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001671 while (true) {
1672 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001673 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001674 cutOffParsing();
1675 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001676 }
1677
1678 if (Tok.isNot(tok::identifier)) {
1679 Diag(Tok, diag::err_expected_ident);
1680 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001681 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001682 }
1683
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001684 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1685 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001686 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001687 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001688
Chris Lattnerdf195262007-10-09 17:51:17 +00001689 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001690 break;
1691 ConsumeToken(); // consume ','
1692 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001693 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001694 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001695}
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001697/// objc-throw-statement:
1698/// throw expression[opt];
1699///
John McCall60d7b3a2010-08-24 06:29:42 +00001700StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1701 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001702 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001703 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001704 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001705 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001706 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001707 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001708 }
1709 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001710 // consume ';'
1711 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001712 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001713}
1714
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001715/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001716/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001717///
John McCall60d7b3a2010-08-24 06:29:42 +00001718StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001719Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001720 ConsumeToken(); // consume synchronized
1721 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001722 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001723 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001724 }
John McCall07524032011-07-27 21:50:02 +00001725
1726 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001727 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001728 ExprResult operand(ParseExpression());
1729
1730 if (Tok.is(tok::r_paren)) {
1731 ConsumeParen(); // ')'
1732 } else {
1733 if (!operand.isInvalid())
1734 Diag(Tok, diag::err_expected_rparen);
1735
1736 // Skip forward until we see a left brace, but don't consume it.
1737 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001738 }
John McCall07524032011-07-27 21:50:02 +00001739
1740 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001741 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001742 if (!operand.isInvalid())
1743 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001744 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001745 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001746
John McCall07524032011-07-27 21:50:02 +00001747 // Check the @synchronized operand now.
1748 if (!operand.isInvalid())
1749 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001750
John McCall07524032011-07-27 21:50:02 +00001751 // Parse the compound statement within a new scope.
1752 ParseScope bodyScope(this, Scope::DeclScope);
1753 StmtResult body(ParseCompoundStatementBody());
1754 bodyScope.Exit();
1755
1756 // If there was a semantic or parse error earlier with the
1757 // operand, fail now.
1758 if (operand.isInvalid())
1759 return StmtError();
1760
1761 if (body.isInvalid())
1762 body = Actions.ActOnNullStmt(Tok.getLocation());
1763
1764 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001765}
1766
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001767/// objc-try-catch-statement:
1768/// @try compound-statement objc-catch-list[opt]
1769/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1770///
1771/// objc-catch-list:
1772/// @catch ( parameter-declaration ) compound-statement
1773/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1774/// catch-parameter-declaration:
1775/// parameter-declaration
1776/// '...' [OBJC2]
1777///
John McCall60d7b3a2010-08-24 06:29:42 +00001778StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001779 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001780
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001781 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001782 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001783 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001784 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001785 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001786 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001787 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001788 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001789 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001790 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001791 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001792 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001793
Chris Lattnerdf195262007-10-09 17:51:17 +00001794 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001795 // At this point, we need to lookahead to determine if this @ is the start
1796 // of an @catch or @finally. We don't want to consume the @ token if this
1797 // is an @try or @encode or something else.
1798 Token AfterAt = GetLookAheadToken(1);
1799 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1800 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1801 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001802
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001803 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001804 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001805 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001806 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001807 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001808 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001809 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001810 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001811 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001812 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001813 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001814 ParseDeclarator(ParmDecl);
1815
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001816 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001817 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001818 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001819 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001820 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Steve Naroff93a25952009-04-07 22:56:58 +00001822 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Steve Naroff93a25952009-04-07 22:56:58 +00001824 if (Tok.is(tok::r_paren))
1825 RParenLoc = ConsumeParen();
1826 else // Skip over garbage, until we get to ')'. Eat the ')'.
1827 SkipUntil(tok::r_paren, true, false);
1828
John McCall60d7b3a2010-08-24 06:29:42 +00001829 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001830 if (Tok.is(tok::l_brace))
1831 CatchBody = ParseCompoundStatementBody();
1832 else
1833 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001834 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001835 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001836
John McCall60d7b3a2010-08-24 06:29:42 +00001837 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001838 RParenLoc,
1839 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001840 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001841 if (!Catch.isInvalid())
1842 CatchStmts.push_back(Catch.release());
1843
Steve Naroff64515f32008-02-05 21:27:35 +00001844 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001845 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1846 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001847 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001848 }
1849 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001850 } else {
1851 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001852 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001853 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001854
John McCall60d7b3a2010-08-24 06:29:42 +00001855 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001856 if (Tok.is(tok::l_brace))
1857 FinallyBody = ParseCompoundStatementBody();
1858 else
1859 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001860 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001861 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001862 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001863 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001864 catch_or_finally_seen = true;
1865 break;
1866 }
1867 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001868 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001869 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001870 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001871 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001872
John McCall9ae2f072010-08-23 23:25:46 +00001873 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001874 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001875 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001876}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001877
John McCallf85e1932011-06-15 23:02:42 +00001878/// objc-autoreleasepool-statement:
1879/// @autoreleasepool compound-statement
1880///
1881StmtResult
1882Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1883 ConsumeToken(); // consume autoreleasepool
1884 if (Tok.isNot(tok::l_brace)) {
1885 Diag(Tok, diag::err_expected_lbrace);
1886 return StmtError();
1887 }
1888 // Enter a scope to hold everything within the compound stmt. Compound
1889 // statements can always hold declarations.
1890 ParseScope BodyScope(this, Scope::DeclScope);
1891
1892 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1893
1894 BodyScope.Exit();
1895 if (AutoreleasePoolBody.isInvalid())
1896 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1897 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1898 AutoreleasePoolBody.take());
1899}
1900
Steve Naroff3536b442007-09-06 21:24:23 +00001901/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001902///
John McCalld226f652010-08-21 09:40:31 +00001903Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001904 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001905
John McCallf312b1e2010-08-26 23:41:50 +00001906 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1907 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001909 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001910 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001911 if (ObjCImpDecl) {
1912 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001913 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001914 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001915 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001916 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001917
Steve Naroff409be832007-11-11 19:54:21 +00001918 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001919 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001920 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001921
Steve Naroff409be832007-11-11 19:54:21 +00001922 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1923 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001924
Steve Naroff409be832007-11-11 19:54:21 +00001925 // If we didn't find the '{', bail out.
1926 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001927 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001928 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001929 // Allow the rest of sema to find private method decl implementations.
1930 if (MDecl)
1931 Actions.AddAnyMethodToGlobalPool(MDecl);
1932
1933 // Consume the tokens and store them for later parsing.
1934 LexedMethod* LM = new LexedMethod(this, MDecl);
1935 LateParsedObjCMethods.push_back(LM);
1936 CachedTokens &Toks = LM->Toks;
1937 // Begin by storing the '{' token.
1938 Toks.push_back(Tok);
1939 ConsumeBrace();
1940 // Consume everything up to (and including) the matching right brace.
1941 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Steve Naroff71c0a952007-11-13 23:01:27 +00001942 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001943}
Anders Carlsson55085182007-08-21 17:43:55 +00001944
John McCall60d7b3a2010-08-24 06:29:42 +00001945StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001946 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001947 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001948 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001949 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001950 }
1951
1952 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001953 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001954
1955 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001956 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001957
1958 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001959 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00001960
1961 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1962 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001963
John McCall60d7b3a2010-08-24 06:29:42 +00001964 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001965 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001966 // If the expression is invalid, skip ahead to the next semicolon. Not
1967 // doing this opens us up to the possibility of infinite loops if
1968 // ParseExpression does not consume any tokens.
1969 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001970 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001971 }
Chris Lattner5d803162009-12-07 16:33:19 +00001972
Steve Naroff64515f32008-02-05 21:27:35 +00001973 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001974 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001975 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001976}
1977
John McCall60d7b3a2010-08-24 06:29:42 +00001978ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001979 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001980 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001981 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001982 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001983 return ExprError();
1984
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001985 case tok::string_literal: // primary-expression: string-literal
1986 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001987 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001988 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001989 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001990 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001991
Chris Lattner4fef81d2008-08-05 06:19:09 +00001992 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1993 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001994 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001995 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001996 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001997 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001998 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001999 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00002000 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002001 }
Anders Carlsson55085182007-08-21 17:43:55 +00002002 }
Anders Carlsson55085182007-08-21 17:43:55 +00002003}
2004
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002005/// \brirg Parse the receiver of an Objective-C++ message send.
2006///
2007/// This routine parses the receiver of a message send in
2008/// Objective-C++ either as a type or as an expression. Note that this
2009/// routine must not be called to parse a send to 'super', since it
2010/// has no way to return such a result.
2011///
2012/// \param IsExpr Whether the receiver was parsed as an expression.
2013///
2014/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2015/// IsExpr is true), the parsed expression. If the receiver was parsed
2016/// as a type (\c IsExpr is false), the parsed type.
2017///
2018/// \returns True if an error occurred during parsing or semantic
2019/// analysis, in which case the arguments do not have valid
2020/// values. Otherwise, returns false for a successful parse.
2021///
2022/// objc-receiver: [C++]
2023/// 'super' [not parsed here]
2024/// expression
2025/// simple-type-specifier
2026/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002027bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002028 InMessageExpressionRAIIObject InMessage(*this, true);
2029
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002030 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2031 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2032 TryAnnotateTypeOrScopeToken();
2033
2034 if (!isCXXSimpleTypeSpecifier()) {
2035 // objc-receiver:
2036 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00002037 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002038 if (Receiver.isInvalid())
2039 return true;
2040
2041 IsExpr = true;
2042 TypeOrExpr = Receiver.take();
2043 return false;
2044 }
2045
2046 // objc-receiver:
2047 // typename-specifier
2048 // simple-type-specifier
2049 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00002050 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002051 ParseCXXSimpleTypeSpecifier(DS);
2052
2053 if (Tok.is(tok::l_paren)) {
2054 // If we see an opening parentheses at this point, we are
2055 // actually parsing an expression that starts with a
2056 // function-style cast, e.g.,
2057 //
2058 // postfix-expression:
2059 // simple-type-specifier ( expression-list [opt] )
2060 // typename-specifier ( expression-list [opt] )
2061 //
2062 // Parse the remainder of this case, then the (optional)
2063 // postfix-expression suffix, followed by the (optional)
2064 // right-hand side of the binary expression. We have an
2065 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002066 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002067 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002068 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002069 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002070 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002071 if (Receiver.isInvalid())
2072 return true;
2073
2074 IsExpr = true;
2075 TypeOrExpr = Receiver.take();
2076 return false;
2077 }
2078
2079 // We have a class message. Turn the simple-type-specifier or
2080 // typename-specifier we parsed into a type and parse the
2081 // remainder of the class message.
2082 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002083 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002084 if (Type.isInvalid())
2085 return true;
2086
2087 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002088 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002089 return false;
2090}
2091
Douglas Gregor1b730e82010-05-31 14:40:22 +00002092/// \brief Determine whether the parser is currently referring to a an
2093/// Objective-C message send, using a simplified heuristic to avoid overhead.
2094///
2095/// This routine will only return true for a subset of valid message-send
2096/// expressions.
2097bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00002098 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002099 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002100 return GetLookAheadToken(1).is(tok::identifier) &&
2101 GetLookAheadToken(2).is(tok::identifier);
2102}
2103
Douglas Gregor9497a732010-09-16 01:51:54 +00002104bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
2105 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
2106 InMessageExpression)
2107 return false;
2108
2109
2110 ParsedType Type;
2111
2112 if (Tok.is(tok::annot_typename))
2113 Type = getTypeAnnotation(Tok);
2114 else if (Tok.is(tok::identifier))
2115 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2116 getCurScope());
2117 else
2118 return false;
2119
2120 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2121 const Token &AfterNext = GetLookAheadToken(2);
2122 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2123 if (Tok.is(tok::identifier))
2124 TryAnnotateTypeOrScopeToken();
2125
2126 return Tok.is(tok::annot_typename);
2127 }
2128 }
2129
2130 return false;
2131}
2132
Mike Stump1eb44332009-09-09 15:08:12 +00002133/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002134/// '[' objc-receiver objc-message-args ']'
2135///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002136/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002137/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002138/// expression
2139/// class-name
2140/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002141///
John McCall60d7b3a2010-08-24 06:29:42 +00002142ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002143 assert(Tok.is(tok::l_square) && "'[' expected");
2144 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2145
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002146 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002147 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002148 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002149 return ExprError();
2150 }
2151
Douglas Gregor0fbda682010-09-15 14:51:05 +00002152 InMessageExpressionRAIIObject InMessage(*this, true);
2153
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002154 if (getLang().CPlusPlus) {
2155 // We completely separate the C and C++ cases because C++ requires
2156 // more complicated (read: slower) parsing.
2157
2158 // Handle send to super.
2159 // FIXME: This doesn't benefit from the same typo-correction we
2160 // get in Objective-C.
2161 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002162 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002163 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2164 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002165
2166 // Parse the receiver, which is either a type or an expression.
2167 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002168 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002169 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2170 SkipUntil(tok::r_square);
2171 return ExprError();
2172 }
2173
2174 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002175 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2176 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002177 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002178
2179 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002180 ParsedType::getFromOpaquePtr(TypeOrExpr),
2181 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002182 }
2183
2184 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002185 IdentifierInfo *Name = Tok.getIdentifierInfo();
2186 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002187 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002188 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002189 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002190 NextToken().is(tok::period),
2191 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002192 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002193 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2194 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002195
John McCallf312b1e2010-08-26 23:41:50 +00002196 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002197 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002198 SkipUntil(tok::r_square);
2199 return ExprError();
2200 }
2201
Douglas Gregor1569f952010-04-21 20:38:13 +00002202 ConsumeToken(); // the type name
2203
2204 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002205 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002206
John McCallf312b1e2010-08-26 23:41:50 +00002207 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002208 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002209 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002210 }
Chris Lattner699b6612008-01-25 18:59:06 +00002211 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002212
2213 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002214 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002215 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002216 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002217 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002218 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002219
John McCallb3d87482010-08-24 05:47:05 +00002220 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2221 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002222}
Sebastian Redl1d922962008-12-13 15:32:12 +00002223
Douglas Gregor2725ca82010-04-21 19:57:20 +00002224/// \brief Parse the remainder of an Objective-C message following the
2225/// '[' objc-receiver.
2226///
2227/// This routine handles sends to super, class messages (sent to a
2228/// class name), and instance messages (sent to an object), and the
2229/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2230/// ReceiverExpr, respectively. Only one of these parameters may have
2231/// a valid value.
2232///
2233/// \param LBracLoc The location of the opening '['.
2234///
2235/// \param SuperLoc If this is a send to 'super', the location of the
2236/// 'super' keyword that indicates a send to the superclass.
2237///
2238/// \param ReceiverType If this is a class message, the type of the
2239/// class we are sending a message to.
2240///
2241/// \param ReceiverExpr If this is an instance message, the expression
2242/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002243///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002244/// objc-message-args:
2245/// objc-selector
2246/// objc-keywordarg-list
2247///
2248/// objc-keywordarg-list:
2249/// objc-keywordarg
2250/// objc-keywordarg-list objc-keywordarg
2251///
Mike Stump1eb44332009-09-09 15:08:12 +00002252/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002253/// selector-name[opt] ':' objc-keywordexpr
2254///
2255/// objc-keywordexpr:
2256/// nonempty-expr-list
2257///
2258/// nonempty-expr-list:
2259/// assignment-expression
2260/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002261///
John McCall60d7b3a2010-08-24 06:29:42 +00002262ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002263Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002264 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002265 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002266 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002267 InMessageExpressionRAIIObject InMessage(*this, true);
2268
Steve Naroffc4df6d22009-11-07 02:08:14 +00002269 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002270 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002271 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2272 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002273 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002274 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2275 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002276 else
John McCall9ae2f072010-08-23 23:25:46 +00002277 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002278 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002279 cutOffParsing();
2280 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002281 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002282
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002283 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002284 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002285 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002286
Chris Lattner5f9e2722011-07-23 10:55:15 +00002287 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002288 SmallVector<SourceLocation, 12> KeyLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002289 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002290
Chris Lattnerdf195262007-10-09 17:51:17 +00002291 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002292 while (1) {
2293 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002294 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002295 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002296
Chris Lattnerdf195262007-10-09 17:51:17 +00002297 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002298 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002299 // We must manually skip to a ']', otherwise the expression skipper will
2300 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2301 // the enclosing expression.
2302 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002303 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002304 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002305
Steve Naroff68d331a2007-09-27 14:38:14 +00002306 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002307 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002308
2309 if (Tok.is(tok::code_completion)) {
2310 if (SuperLoc.isValid())
2311 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2312 KeyIdents.data(),
2313 KeyIdents.size(),
2314 /*AtArgumentEpression=*/true);
2315 else if (ReceiverType)
2316 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2317 KeyIdents.data(),
2318 KeyIdents.size(),
2319 /*AtArgumentEpression=*/true);
2320 else
2321 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2322 KeyIdents.data(),
2323 KeyIdents.size(),
2324 /*AtArgumentEpression=*/true);
2325
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002326 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002327 return ExprError();
2328 }
2329
John McCall60d7b3a2010-08-24 06:29:42 +00002330 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002331 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002332 // We must manually skip to a ']', otherwise the expression skipper will
2333 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2334 // the enclosing expression.
2335 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002336 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002337 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002338
Steve Naroff37387c92007-09-17 20:25:27 +00002339 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002340 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002341
Douglas Gregord3c68542009-11-19 01:08:35 +00002342 // Code completion after each argument.
2343 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002344 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002345 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002346 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002347 KeyIdents.size(),
2348 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002349 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002350 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002351 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002352 KeyIdents.size(),
2353 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002354 else
John McCall9ae2f072010-08-23 23:25:46 +00002355 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002356 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002357 KeyIdents.size(),
2358 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002359 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002360 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002361 }
2362
Steve Naroff37387c92007-09-17 20:25:27 +00002363 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002364 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002365 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002366 break;
2367 // We have a selector or a colon, continue parsing.
2368 }
2369 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002370 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002371 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002372 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002373 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002374 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002375 // We must manually skip to a ']', otherwise the expression skipper will
2376 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2377 // the enclosing expression.
2378 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002379 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002380 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002381
Steve Naroff49f109c2007-11-15 13:05:42 +00002382 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002383 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002384 }
2385 } else if (!selIdent) {
2386 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002387
Chris Lattner4fef81d2008-08-05 06:19:09 +00002388 // We must manually skip to a ']', otherwise the expression skipper will
2389 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2390 // the enclosing expression.
2391 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002392 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002393 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002394
Chris Lattnerdf195262007-10-09 17:51:17 +00002395 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002396 if (Tok.is(tok::identifier))
2397 Diag(Tok, diag::err_expected_colon);
2398 else
2399 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002400 // We must manually skip to a ']', otherwise the expression skipper will
2401 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2402 // the enclosing expression.
2403 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002404 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002405 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002406
Chris Lattner699b6612008-01-25 18:59:06 +00002407 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002408
Steve Naroff29238a02007-10-05 18:42:47 +00002409 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002410 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002411 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002412 KeyLocs.push_back(Loc);
2413 }
Chris Lattnerff384912007-10-07 02:00:24 +00002414 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002415
Douglas Gregor2725ca82010-04-21 19:57:20 +00002416 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002417 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002418 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002419 MultiExprArg(Actions,
2420 KeyExprs.take(),
2421 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002422 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002423 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002424 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002425 MultiExprArg(Actions,
2426 KeyExprs.take(),
2427 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002428 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002429 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002430 MultiExprArg(Actions,
2431 KeyExprs.take(),
2432 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002433}
2434
John McCall60d7b3a2010-08-24 06:29:42 +00002435ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2436 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002437 if (Res.isInvalid()) return move(Res);
2438
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002439 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2440 // expressions. At this point, we know that the only valid thing that starts
2441 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002442 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002443 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002444 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002445 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002446
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002447 while (Tok.is(tok::at)) {
2448 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002449
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002450 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002451 if (!isTokenStringLiteral())
2452 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002453
John McCall60d7b3a2010-08-24 06:29:42 +00002454 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002455 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002456 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002457
Sebastian Redleffa8d12008-12-10 00:02:53 +00002458 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002459 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002460
2461 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2462 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002463}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002464
2465/// objc-encode-expression:
2466/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002467ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002468Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002469 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002470
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002471 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002472
Chris Lattner4fef81d2008-08-05 06:19:09 +00002473 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002474 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2475
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002476 BalancedDelimiterTracker T(*this, tok::l_paren);
2477 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002478
Douglas Gregor809070a2009-02-18 17:45:20 +00002479 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002480
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002481 T.consumeClose();
Sebastian Redl1d922962008-12-13 15:32:12 +00002482
Douglas Gregor809070a2009-02-18 17:45:20 +00002483 if (Ty.isInvalid())
2484 return ExprError();
2485
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002486 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
2487 T.getOpenLocation(), Ty.get(),
2488 T.getCloseLocation()));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002489}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002490
2491/// objc-protocol-expression
2492/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002493ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002494Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002495 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002496
Chris Lattner4fef81d2008-08-05 06:19:09 +00002497 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002498 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2499
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002500 BalancedDelimiterTracker T(*this, tok::l_paren);
2501 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002502
Chris Lattner4fef81d2008-08-05 06:19:09 +00002503 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002504 return ExprError(Diag(Tok, diag::err_expected_ident));
2505
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002506 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002507 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002508
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002509 T.consumeClose();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002510
Sebastian Redl1d922962008-12-13 15:32:12 +00002511 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002512 T.getOpenLocation(),
2513 T.getCloseLocation()));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002514}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002515
2516/// objc-selector-expression
2517/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002518ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002519 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002520
Chris Lattner4fef81d2008-08-05 06:19:09 +00002521 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002522 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2523
Chris Lattner5f9e2722011-07-23 10:55:15 +00002524 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002525 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002526
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002527 BalancedDelimiterTracker T(*this, tok::l_paren);
2528 T.consumeOpen();
2529
Douglas Gregor458433d2010-08-26 15:07:07 +00002530 if (Tok.is(tok::code_completion)) {
2531 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2532 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002533 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002534 return ExprError();
2535 }
2536
Chris Lattner2fc5c242009-04-11 18:13:45 +00002537 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002538 if (!SelIdent && // missing selector name.
2539 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002540 return ExprError(Diag(Tok, diag::err_expected_ident));
2541
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002542 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002543 unsigned nColons = 0;
2544 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002545 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002546 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2547 ++nColons;
2548 KeyIdents.push_back(0);
2549 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002550 return ExprError(Diag(Tok, diag::err_expected_colon));
2551
Chris Lattner5add7542010-08-27 22:32:41 +00002552 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002553 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002554 if (Tok.is(tok::r_paren))
2555 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002556
2557 if (Tok.is(tok::code_completion)) {
2558 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2559 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002560 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002561 return ExprError();
2562 }
2563
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002564 // Check for another keyword selector.
2565 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002566 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002567 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002568 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002569 break;
2570 }
Steve Naroff887407e2007-12-05 22:21:29 +00002571 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002572 T.consumeClose();
Steve Naroff887407e2007-12-05 22:21:29 +00002573 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002574 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002575 T.getOpenLocation(),
2576 T.getCloseLocation()));
Gabor Greif58065b22007-10-19 15:38:32 +00002577 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002578
2579Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002580
2581 // Save the current token position.
2582 SourceLocation OrigLoc = Tok.getLocation();
2583
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002584 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2585 // Append the current token at the end of the new token stream so that it
2586 // doesn't get lost.
2587 LM.Toks.push_back(Tok);
2588 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2589
2590 // MDecl might be null due to error in method prototype, etc.
2591 Decl *MDecl = LM.D;
2592 // Consume the previously pushed token.
2593 ConsumeAnyToken();
2594
2595 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2596 SourceLocation BraceLoc = Tok.getLocation();
2597 // Enter a scope for the method body.
2598 ParseScope BodyScope(this,
2599 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2600
2601 // Tell the actions module that we have entered a method definition with the
2602 // specified Declarator for the method.
2603 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2604
2605 if (PP.isCodeCompletionEnabled()) {
2606 if (trySkippingFunctionBodyForCodeCompletion()) {
2607 BodyScope.Exit();
2608 return Actions.ActOnFinishFunctionBody(MDecl, 0);
2609 }
2610 }
2611
2612 StmtResult FnBody(ParseCompoundStatementBody());
2613
2614 // If the function body could not be parsed, make a bogus compoundstmt.
2615 if (FnBody.isInvalid())
2616 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2617 MultiStmtArg(Actions), false);
2618
2619 // Leave the function body scope.
2620 BodyScope.Exit();
2621
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002622 MDecl = Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2623
2624 if (Tok.getLocation() != OrigLoc) {
2625 // Due to parsing error, we either went over the cached tokens or
2626 // there are still cached tokens left. If it's the latter case skip the
2627 // leftover tokens.
2628 // Since this is an uncommon situation that should be avoided, use the
2629 // expensive isBeforeInTranslationUnit call.
2630 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2631 OrigLoc))
2632 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2633 ConsumeAnyToken();
2634 }
2635
2636 return MDecl;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002637}