blob: 3ecdae2978d5d5fe434fb688b3fe184bb2f48663 [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);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000045 break;
John McCall7f040a92010-12-24 02:08:15 +000046 case tok::objc_interface: {
John McCall0b7e6782011-03-24 11:26:52 +000047 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000048 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
49 break;
John McCall7f040a92010-12-24 02:08:15 +000050 }
51 case tok::objc_protocol: {
John McCall0b7e6782011-03-24 11:26:52 +000052 ParsedAttributes attrs(AttrFactory);
Douglas Gregorbd9482d2012-01-01 21:23:57 +000053 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall7f040a92010-12-24 02:08:15 +000054 }
Chris Lattner5ffb14b2008-08-23 02:02:23 +000055 case tok::objc_implementation:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000056 SingleDecl = ParseObjCAtImplementationDeclaration(AtLoc);
57 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000058 case tok::objc_end:
Fariborz Jahanian140ab232011-08-31 17:37:55 +000059 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner5ffb14b2008-08-23 02:02:23 +000060 case tok::objc_compatibility_alias:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000061 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
62 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000063 case tok::objc_synthesize:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000064 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
65 break;
Chris Lattner5ffb14b2008-08-23 02:02:23 +000066 case tok::objc_dynamic:
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000067 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
68 break;
Douglas Gregor5948ae12012-01-03 18:04:46 +000069 case tok::objc_import:
Douglas Gregor94ad28b2012-01-03 18:24:14 +000070 if (getLang().Modules)
71 return ParseModuleImport(AtLoc);
72
73 // Fall through
74
Chris Lattner5ffb14b2008-08-23 02:02:23 +000075 default:
76 Diag(AtLoc, diag::err_unexpected_at);
77 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000078 SingleDecl = 0;
79 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000080 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000081 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +000082}
83
84///
Mike Stump1eb44332009-09-09 15:08:12 +000085/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000086/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000087///
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000088Parser::DeclGroupPtrTy
89Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000090 ConsumeToken(); // the identifier "class"
Chris Lattner5f9e2722011-07-23 10:55:15 +000091 SmallVector<IdentifierInfo *, 8> ClassNames;
92 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremenekc09cba62009-11-17 23:12:20 +000093
Mike Stump1eb44332009-09-09 15:08:12 +000094
Reid Spencer5f016e22007-07-11 17:01:13 +000095 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000096 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000097 Diag(Tok, diag::err_expected_ident);
98 SkipUntil(tok::semi);
Fariborz Jahanian95ed7782011-08-27 20:50:59 +000099 return Actions.ConvertDeclToDeclGroup(0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +0000102 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Chris Lattnerdf195262007-10-09 17:51:17 +0000105 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 ConsumeToken();
109 }
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 // Consume the ';'.
112 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian95ed7782011-08-27 20:50:59 +0000113 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremenekc09cba62009-11-17 23:12:20 +0000115 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
116 ClassLocs.data(),
117 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000118}
119
Erik Verbruggend64251f2011-12-06 09:25:23 +0000120void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
121{
122 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
123 if (ock == Sema::OCK_None)
124 return;
125
126 Decl *Decl = Actions.ActOnAtEnd(getCurScope(), AtLoc);
127 Diag(AtLoc, diag::err_objc_missing_end)
128 << FixItHint::CreateInsertion(AtLoc, "@end\n");
129 if (Decl)
130 Diag(Decl->getLocStart(), diag::note_objc_container_start)
131 << (int) ock;
132 if (!PendingObjCImpDecl.empty())
133 PendingObjCImpDecl.pop_back();
134 ObjCImpDecl = 0;
135}
136
Steve Naroffdac269b2007-08-20 21:31:48 +0000137///
138/// objc-interface:
139/// objc-class-interface-attributes[opt] objc-class-interface
140/// objc-category-interface
141///
142/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000143/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000144/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000145/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000146/// objc-interface-decl-list
147/// @end
148///
149/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000150/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000151/// objc-protocol-refs[opt]
152/// objc-interface-decl-list
153/// @end
154///
155/// objc-superclass:
156/// ':' identifier
157///
158/// objc-class-interface-attributes:
159/// __attribute__((visibility("default")))
160/// __attribute__((visibility("hidden")))
161/// __attribute__((deprecated))
162/// __attribute__((unavailable))
163/// __attribute__((objc_exception)) - used by NSException on 64-bit
164///
Erik Verbruggend64251f2011-12-06 09:25:23 +0000165Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall7f040a92010-12-24 02:08:15 +0000166 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000167 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000168 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggend64251f2011-12-06 09:25:23 +0000169 CheckNestedObjCContexts(AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000170 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000172 // Code completion after '@interface'.
173 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000174 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000175 cutOffParsing();
176 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000177 }
178
Chris Lattnerdf195262007-10-09 17:51:17 +0000179 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000180 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +0000181 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000182 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000183
Steve Naroffdac269b2007-08-20 21:31:48 +0000184 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000185 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000186 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000187 if (Tok.is(tok::l_paren) &&
188 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000189
190 BalancedDelimiterTracker T(*this, tok::l_paren);
191 T.consumeOpen();
192
193 SourceLocation categoryLoc;
Steve Naroffdac269b2007-08-20 21:31:48 +0000194 IdentifierInfo *categoryId = 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000195 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000196 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000197 cutOffParsing();
198 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000199 }
200
Steve Naroff527fe232007-08-23 19:56:30 +0000201 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000202 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000203 categoryId = Tok.getIdentifierInfo();
204 categoryLoc = ConsumeToken();
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000205 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000206 else if (!getLang().ObjC2) {
Steve Naroff527fe232007-08-23 19:56:30 +0000207 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +0000208 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000209 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000210
211 T.consumeClose();
212 if (T.getCloseLocation().isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000213 return 0;
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000214
215 if (!attrs.empty()) { // categories don't support attributes.
216 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
217 attrs.clear();
Steve Naroffdac269b2007-08-20 21:31:48 +0000218 }
Douglas Gregor13d05ac2011-09-23 19:19:41 +0000219
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000220 // Next, we need to check for any protocol references.
221 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000222 SmallVector<Decl *, 8> ProtocolRefs;
223 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000224 if (Tok.is(tok::less) &&
225 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000226 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000227 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000228
John McCalld226f652010-08-21 09:40:31 +0000229 Decl *CategoryType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000230 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000231 nameId, nameLoc,
232 categoryId, categoryLoc,
233 ProtocolRefs.data(),
234 ProtocolRefs.size(),
235 ProtocolLocs.data(),
236 EndProtoLoc);
Fariborz Jahaniane6f07f52011-08-19 18:02:47 +0000237
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000238 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000239 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000240
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000241 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000242 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000243 }
244 // Parse a class interface.
245 IdentifierInfo *superClassId = 0;
246 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000247
Chris Lattnerdf195262007-10-09 17:51:17 +0000248 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000249 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000250
251 // Code completion of superclass names.
252 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000253 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000254 cutOffParsing();
255 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000256 }
257
Chris Lattnerdf195262007-10-09 17:51:17 +0000258 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000259 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +0000260 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000261 }
262 superClassId = Tok.getIdentifierInfo();
263 superClassLoc = ConsumeToken();
264 }
265 // Next, we need to check for any protocol references.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000266 SmallVector<Decl *, 8> ProtocolRefs;
267 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000268 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000269 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000270 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
271 LAngleLoc, EndProtoLoc))
John McCalld226f652010-08-21 09:40:31 +0000272 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000273
John McCalld226f652010-08-21 09:40:31 +0000274 Decl *ClsType =
Erik Verbruggend64251f2011-12-06 09:25:23 +0000275 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000276 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000277 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +0000278 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +0000279 EndProtoLoc, attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Chris Lattnerdf195262007-10-09 17:51:17 +0000281 if (Tok.is(tok::l_brace))
Erik Verbruggend64251f2011-12-06 09:25:23 +0000282 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000283
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000284 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian5512ba52010-04-26 21:18:08 +0000285 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000286}
287
John McCalld0014542009-12-03 22:31:13 +0000288/// The Objective-C property callback. This should be defined where
289/// it's used, but instead it's been lifted to here to support VS2005.
290struct Parser::ObjCPropertyCallback : FieldCallback {
David Blaikie99ba9e32011-12-20 02:48:34 +0000291private:
292 virtual void anchor();
293public:
John McCalld0014542009-12-03 22:31:13 +0000294 Parser &P;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000295 SmallVectorImpl<Decl *> &Props;
John McCalld0014542009-12-03 22:31:13 +0000296 ObjCDeclSpec &OCDS;
297 SourceLocation AtLoc;
298 tok::ObjCKeywordKind MethodImplKind;
299
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000300 ObjCPropertyCallback(Parser &P,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000301 SmallVectorImpl<Decl *> &Props,
John McCalld0014542009-12-03 22:31:13 +0000302 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
303 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000304 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCalld0014542009-12-03 22:31:13 +0000305 MethodImplKind(MethodImplKind) {
306 }
307
John McCalld226f652010-08-21 09:40:31 +0000308 Decl *invoke(FieldDeclarator &FD) {
John McCalld0014542009-12-03 22:31:13 +0000309 if (FD.D.getIdentifier() == 0) {
310 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
311 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000312 return 0;
John McCalld0014542009-12-03 22:31:13 +0000313 }
314 if (FD.BitfieldSize) {
315 P.Diag(AtLoc, diag::err_objc_property_bitfield)
316 << FD.D.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000317 return 0;
John McCalld0014542009-12-03 22:31:13 +0000318 }
319
320 // Install the property declarator into interfaceDecl.
321 IdentifierInfo *SelName =
322 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
323
324 Selector GetterSel =
325 P.PP.getSelectorTable().getNullarySelector(SelName);
326 IdentifierInfo *SetterName = OCDS.getSetterName();
327 Selector SetterSel;
328 if (SetterName)
329 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
330 else
331 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
332 P.PP.getSelectorTable(),
333 FD.D.getIdentifier());
334 bool isOverridingProperty = false;
John McCalld226f652010-08-21 09:40:31 +0000335 Decl *Property =
Douglas Gregor23c94db2010-07-02 17:43:08 +0000336 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000337 GetterSel, SetterSel,
John McCalld0014542009-12-03 22:31:13 +0000338 &isOverridingProperty,
339 MethodImplKind);
340 if (!isOverridingProperty)
341 Props.push_back(Property);
342
343 return Property;
344 }
345};
346
David Blaikie99ba9e32011-12-20 02:48:34 +0000347void Parser::ObjCPropertyCallback::anchor() {
348}
349
Steve Naroffdac269b2007-08-20 21:31:48 +0000350/// objc-interface-decl-list:
351/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000352/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000353/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000354/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000355/// objc-interface-decl-list declaration
356/// objc-interface-decl-list ';'
357///
Steve Naroff294494e2007-08-22 16:35:03 +0000358/// objc-method-requirement: [OBJC2]
359/// @required
360/// @optional
361///
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000362void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
363 Decl *CDecl) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000364 SmallVector<Decl *, 32> allMethods;
365 SmallVector<Decl *, 16> allProperties;
366 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000367 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek782f2f52010-01-07 01:20:12 +0000369 SourceRange AtEnd;
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +0000370
Steve Naroff294494e2007-08-22 16:35:03 +0000371 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000372 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000373 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCalld226f652010-08-21 09:40:31 +0000374 Decl *methodPrototype =
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000375 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000376 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000377 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
378 // method definitions.
Argyrios Kyrtzidis0db9f4d2011-12-17 04:13:22 +0000379 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
380 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
381 SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
382 if (Tok.is(tok::semi))
383 ConsumeToken();
384 }
Steve Naroff294494e2007-08-22 16:35:03 +0000385 continue;
386 }
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000387 if (Tok.is(tok::l_paren)) {
388 Diag(Tok, diag::err_expected_minus_or_plus);
John McCalld226f652010-08-21 09:40:31 +0000389 ParseObjCMethodDecl(Tok.getLocation(),
390 tok::minus,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000391 MethodImplKind, false);
Fariborz Jahanian05511fa2010-04-02 23:15:40 +0000392 continue;
393 }
Chris Lattnere82a10f2008-10-20 05:46:22 +0000394 // Ignore excess semicolons.
395 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000396 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000397 continue;
398 }
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattnerbc662af2008-10-20 06:10:06 +0000400 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000401 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000402 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000404 // Code completion within an Objective-C interface.
405 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000406 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000407 ObjCImpDecl? Sema::PCC_ObjCImplementation
408 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000409 return cutOffParsing();
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000410 }
411
Chris Lattnere82a10f2008-10-20 05:46:22 +0000412 // If we don't have an @ directive, parse it as a function definition.
413 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000414 // The code below does not consume '}'s because it is afraid of eating the
415 // end of a namespace. Because of the way this code is structured, an
416 // erroneous r_brace would cause an infinite loop if not handled here.
417 if (Tok.is(tok::r_brace))
418 break;
John McCall0b7e6782011-03-24 11:26:52 +0000419 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000420 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattnere82a10f2008-10-20 05:46:22 +0000421 continue;
422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattnere82a10f2008-10-20 05:46:22 +0000424 // Otherwise, we have an @ directive, eat the @.
425 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorc464ae82009-12-07 09:27:33 +0000426 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000427 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000428 return cutOffParsing();
Douglas Gregorc464ae82009-12-07 09:27:33 +0000429 break;
430 }
431
Chris Lattnera2449b22008-10-20 05:57:40 +0000432 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Chris Lattnera2449b22008-10-20 05:57:40 +0000434 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenek782f2f52010-01-07 01:20:12 +0000435 AtEnd.setBegin(AtLoc);
436 AtEnd.setEnd(Tok.getLocation());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000437 break;
Douglas Gregorc3d43b72010-03-16 06:04:47 +0000438 } else if (DirectiveKind == tok::objc_not_keyword) {
439 Diag(Tok, diag::err_objc_unknown_at);
440 SkipUntil(tok::semi);
441 continue;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000442 }
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Chris Lattnerbc662af2008-10-20 06:10:06 +0000444 // Eat the identifier.
445 ConsumeToken();
446
Chris Lattnera2449b22008-10-20 05:57:40 +0000447 switch (DirectiveKind) {
448 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000449 // FIXME: If someone forgets an @end on a protocol, this loop will
450 // continue to eat up tons of stuff and spew lots of nonsense errors. It
451 // would probably be better to bail out if we saw an @class or @interface
452 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000453 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000454 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000455 SkipUntil(tok::r_brace, tok::at);
456 break;
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000457
458 case tok::objc_implementation:
Fariborz Jahaniandf81c2c2010-11-09 20:38:00 +0000459 case tok::objc_interface:
Erik Verbruggend64251f2011-12-06 09:25:23 +0000460 Diag(AtLoc, diag::err_objc_missing_end)
461 << FixItHint::CreateInsertion(AtLoc, "@end\n");
462 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
463 << (int) Actions.getObjCContainerKind();
Fariborz Jahanian46d545e2010-11-02 00:44:43 +0000464 ConsumeToken();
465 break;
466
Chris Lattnera2449b22008-10-20 05:57:40 +0000467 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000468 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000469 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000470 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000471 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000472 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000473 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000474 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000475 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Chris Lattnera2449b22008-10-20 05:57:40 +0000477 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000478 if (!getLang().ObjC2)
Chris Lattnerb321c0c2010-12-17 05:40:22 +0000479 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000480
Chris Lattnere82a10f2008-10-20 05:46:22 +0000481 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000482 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000483 if (Tok.is(tok::l_paren))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000484 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000486 ObjCPropertyCallback Callback(*this, allProperties,
John McCalld0014542009-12-03 22:31:13 +0000487 OCDS, AtLoc, MethodImplKind);
John McCallbdd563e2009-11-03 02:38:08 +0000488
Chris Lattnere82a10f2008-10-20 05:46:22 +0000489 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +0000490 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +0000491 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000492
John McCall7da19ea2011-03-26 01:53:26 +0000493 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnera2449b22008-10-20 05:57:40 +0000494 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000495 }
Steve Naroff294494e2007-08-22 16:35:03 +0000496 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000497
498 // We break out of the big loop in two cases: when we see @end or when we see
499 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorc464ae82009-12-07 09:27:33 +0000500 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000501 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000502 return cutOffParsing();
Erik Verbruggend64251f2011-12-06 09:25:23 +0000503 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerbc662af2008-10-20 06:10:06 +0000504 ConsumeToken(); // the "end" identifier
Erik Verbruggend64251f2011-12-06 09:25:23 +0000505 } else {
506 Diag(Tok, diag::err_objc_missing_end)
507 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
508 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
509 << (int) Actions.getObjCContainerKind();
510 AtEnd.setBegin(Tok.getLocation());
511 AtEnd.setEnd(Tok.getLocation());
512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Chris Lattnera2449b22008-10-20 05:57:40 +0000514 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000515 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000516 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump1eb44332009-09-09 15:08:12 +0000517 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000518 allProperties.data(), allProperties.size(),
519 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000520}
521
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000522/// Parse property attribute declarations.
523///
524/// property-attr-decl: '(' property-attrlist ')'
525/// property-attrlist:
526/// property-attribute
527/// property-attrlist ',' property-attribute
528/// property-attribute:
529/// getter '=' identifier
530/// setter '=' identifier ':'
531/// readonly
532/// readwrite
533/// assign
534/// retain
535/// copy
536/// nonatomic
John McCallf85e1932011-06-15 23:02:42 +0000537/// atomic
538/// strong
539/// weak
540/// unsafe_unretained
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000541///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000542void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000543 assert(Tok.getKind() == tok::l_paren);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000544 BalancedDelimiterTracker T(*this, tok::l_paren);
545 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000547 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000548 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000549 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000550 return cutOffParsing();
Steve Naroffece8e712009-10-08 21:55:05 +0000551 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000552 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000554 // If this is not an identifier at all, bail out early.
555 if (II == 0) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000556 T.consumeClose();
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000557 return;
558 }
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Chris Lattner156b0612008-10-20 07:37:22 +0000560 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Chris Lattner92e62b02008-11-20 04:42:34 +0000562 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000563 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000564 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000565 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCallf85e1932011-06-15 23:02:42 +0000566 else if (II->isStr("unsafe_unretained"))
567 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner92e62b02008-11-20 04:42:34 +0000568 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000569 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000570 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000571 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000572 else if (II->isStr("strong"))
573 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner92e62b02008-11-20 04:42:34 +0000574 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000575 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000576 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000577 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000578 else if (II->isStr("atomic"))
579 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCallf85e1932011-06-15 23:02:42 +0000580 else if (II->isStr("weak"))
581 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner92e62b02008-11-20 04:42:34 +0000582 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000583 bool IsSetter = II->getNameStart()[0] == 's';
584
Chris Lattnere00da7c2008-10-20 07:39:53 +0000585 // getter/setter require extra treatment.
Anders Carlsson42499be2010-10-02 17:45:21 +0000586 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
587 diag::err_objc_expected_equal_for_getter;
588
589 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000590 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Douglas Gregor4ad96852009-11-19 07:41:15 +0000592 if (Tok.is(tok::code_completion)) {
Anders Carlsson42499be2010-10-02 17:45:21 +0000593 if (IsSetter)
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000594 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregor4ad96852009-11-19 07:41:15 +0000595 else
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000596 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000597 return cutOffParsing();
Douglas Gregor4ad96852009-11-19 07:41:15 +0000598 }
599
Anders Carlsson42499be2010-10-02 17:45:21 +0000600
601 SourceLocation SelLoc;
602 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
603
604 if (!SelIdent) {
605 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
606 << IsSetter;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000607 SkipUntil(tok::r_paren);
608 return;
609 }
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Anders Carlsson42499be2010-10-02 17:45:21 +0000611 if (IsSetter) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000612 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000613 DS.setSetterName(SelIdent);
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Fariborz Jahaniane0097db2010-02-15 22:20:11 +0000615 if (ExpectAndConsume(tok::colon,
616 diag::err_expected_colon_after_setter_name, "",
Chris Lattner156b0612008-10-20 07:37:22 +0000617 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000618 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000619 } else {
620 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlsson42499be2010-10-02 17:45:21 +0000621 DS.setGetterName(SelIdent);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000622 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000623 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000624 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000625 SkipUntil(tok::r_paren);
626 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000627 }
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Chris Lattner156b0612008-10-20 07:37:22 +0000629 if (Tok.isNot(tok::comma))
630 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Chris Lattner156b0612008-10-20 07:37:22 +0000632 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000635 T.consumeClose();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000636}
637
Steve Naroff3536b442007-09-06 21:24:23 +0000638/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000639/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000640/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000641///
642/// objc-instance-method: '-'
643/// objc-class-method: '+'
644///
Steve Naroff4985ace2007-08-22 18:35:33 +0000645/// objc-method-attributes: [OBJC2]
646/// __attribute__((deprecated))
647///
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000648Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000649 bool MethodDefinition) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000650 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000651
Mike Stump1eb44332009-09-09 15:08:12 +0000652 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000653 SourceLocation mLoc = ConsumeToken();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000654 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000655 MethodDefinition);
Steve Naroff3536b442007-09-06 21:24:23 +0000656 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000657 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000658 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000659}
660
661/// objc-selector:
662/// identifier
663/// one of
664/// enum struct union if else while do for switch case default
665/// break continue return goto asm sizeof typeof __alignof
666/// unsigned long const short volatile signed restrict _Complex
667/// in out inout bycopy byref oneway int char float double void _Bool
668///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000669IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanianbe747402010-09-03 01:26:16 +0000670
Chris Lattnerff384912007-10-07 02:00:24 +0000671 switch (Tok.getKind()) {
672 default:
673 return 0;
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000674 case tok::ampamp:
675 case tok::ampequal:
676 case tok::amp:
677 case tok::pipe:
678 case tok::tilde:
679 case tok::exclaim:
680 case tok::exclaimequal:
681 case tok::pipepipe:
682 case tok::pipeequal:
683 case tok::caret:
684 case tok::caretequal: {
Fariborz Jahanian3846ca22010-09-03 18:01:09 +0000685 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanianafbc6812010-09-03 17:33:04 +0000686 if (isalpha(ThisTok[0])) {
687 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
688 Tok.setKind(tok::identifier);
689 SelectorLoc = ConsumeToken();
690 return II;
691 }
692 return 0;
693 }
694
Chris Lattnerff384912007-10-07 02:00:24 +0000695 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000696 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000697 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000698 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000699 case tok::kw_break:
700 case tok::kw_case:
701 case tok::kw_catch:
702 case tok::kw_char:
703 case tok::kw_class:
704 case tok::kw_const:
705 case tok::kw_const_cast:
706 case tok::kw_continue:
707 case tok::kw_default:
708 case tok::kw_delete:
709 case tok::kw_do:
710 case tok::kw_double:
711 case tok::kw_dynamic_cast:
712 case tok::kw_else:
713 case tok::kw_enum:
714 case tok::kw_explicit:
715 case tok::kw_export:
716 case tok::kw_extern:
717 case tok::kw_false:
718 case tok::kw_float:
719 case tok::kw_for:
720 case tok::kw_friend:
721 case tok::kw_goto:
722 case tok::kw_if:
723 case tok::kw_inline:
724 case tok::kw_int:
725 case tok::kw_long:
726 case tok::kw_mutable:
727 case tok::kw_namespace:
728 case tok::kw_new:
729 case tok::kw_operator:
730 case tok::kw_private:
731 case tok::kw_protected:
732 case tok::kw_public:
733 case tok::kw_register:
734 case tok::kw_reinterpret_cast:
735 case tok::kw_restrict:
736 case tok::kw_return:
737 case tok::kw_short:
738 case tok::kw_signed:
739 case tok::kw_sizeof:
740 case tok::kw_static:
741 case tok::kw_static_cast:
742 case tok::kw_struct:
743 case tok::kw_switch:
744 case tok::kw_template:
745 case tok::kw_this:
746 case tok::kw_throw:
747 case tok::kw_true:
748 case tok::kw_try:
749 case tok::kw_typedef:
750 case tok::kw_typeid:
751 case tok::kw_typename:
752 case tok::kw_typeof:
753 case tok::kw_union:
754 case tok::kw_unsigned:
755 case tok::kw_using:
756 case tok::kw_virtual:
757 case tok::kw_void:
758 case tok::kw_volatile:
759 case tok::kw_wchar_t:
760 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000761 case tok::kw__Bool:
762 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000763 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000764 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000765 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000766 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000767 }
Steve Naroff294494e2007-08-22 16:35:03 +0000768}
769
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000770/// objc-for-collection-in: 'in'
771///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000772bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000773 // FIXME: May have to do additional look-ahead to only allow for
774 // valid tokens following an 'in'; such as an identifier, unary operators,
775 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000776 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000777 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000778}
779
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000780/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000781/// qualifier list and builds their bitmask representation in the input
782/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000783///
784/// objc-type-qualifiers:
785/// objc-type-qualifier
786/// objc-type-qualifiers objc-type-qualifier
787///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000788void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000789 Declarator::TheContext Context) {
790 assert(Context == Declarator::ObjCParameterContext ||
791 Context == Declarator::ObjCResultContext);
792
Chris Lattnere8b724d2007-12-12 06:56:32 +0000793 while (1) {
Douglas Gregord32b0222010-08-24 01:06:58 +0000794 if (Tok.is(tok::code_completion)) {
Douglas Gregorb77cab92011-03-08 19:17:54 +0000795 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCallcdda47f2011-10-01 09:56:14 +0000796 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000797 return cutOffParsing();
Douglas Gregord32b0222010-08-24 01:06:58 +0000798 }
799
Chris Lattnercb53b362007-12-27 19:57:00 +0000800 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000801 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Chris Lattnere8b724d2007-12-12 06:56:32 +0000803 const IdentifierInfo *II = Tok.getIdentifierInfo();
804 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000805 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000806 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000808 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000809 switch (i) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000810 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000811 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
812 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
813 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
814 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
815 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
816 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000817 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000818 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000819 ConsumeToken();
820 II = 0;
821 break;
822 }
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Chris Lattnere8b724d2007-12-12 06:56:32 +0000824 // If this wasn't a recognized qualifier, bail out.
825 if (II) return;
826 }
827}
828
John McCallcdda47f2011-10-01 09:56:14 +0000829/// Take all the decl attributes out of the given list and add
830/// them to the given attribute set.
831static void takeDeclAttributes(ParsedAttributes &attrs,
832 AttributeList *list) {
833 while (list) {
834 AttributeList *cur = list;
835 list = cur->getNext();
836
837 if (!cur->isUsedAsTypeAttr()) {
838 // Clear out the next pointer. We're really completely
839 // destroying the internal invariants of the declarator here,
840 // but it doesn't matter because we're done with it.
841 cur->setNext(0);
842 attrs.add(cur);
843 }
844 }
845}
846
847/// takeDeclAttributes - Take all the decl attributes from the given
848/// declarator and add them to the given list.
849static void takeDeclAttributes(ParsedAttributes &attrs,
850 Declarator &D) {
851 // First, take ownership of all attributes.
852 attrs.getPool().takeAllFrom(D.getAttributePool());
853 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
854
855 // Now actually move the attributes over.
856 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
857 takeDeclAttributes(attrs, D.getAttributes());
858 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
859 takeDeclAttributes(attrs,
860 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
861}
862
Chris Lattnere8b724d2007-12-12 06:56:32 +0000863/// objc-type-name:
864/// '(' objc-type-qualifiers[opt] type-name ')'
865/// '(' objc-type-qualifiers[opt] ')'
866///
Douglas Gregorb77cab92011-03-08 19:17:54 +0000867ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCallcdda47f2011-10-01 09:56:14 +0000868 Declarator::TheContext context,
869 ParsedAttributes *paramAttrs) {
870 assert(context == Declarator::ObjCParameterContext ||
871 context == Declarator::ObjCResultContext);
872 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
873
Chris Lattnerdf195262007-10-09 17:51:17 +0000874 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000876 BalancedDelimiterTracker T(*this, tok::l_paren);
877 T.consumeOpen();
878
Chris Lattnere8904e92008-08-23 01:48:03 +0000879 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000880 ObjCDeclContextSwitch ObjCDC(*this);
881
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000882 // Parse type qualifiers, in, inout, etc.
John McCallcdda47f2011-10-01 09:56:14 +0000883 ParseObjCTypeQualifierList(DS, context);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000884
John McCallb3d87482010-08-24 05:47:05 +0000885 ParsedType Ty;
Douglas Gregor809070a2009-02-18 17:45:20 +0000886 if (isTypeSpecifierQualifier()) {
John McCallcdda47f2011-10-01 09:56:14 +0000887 // Parse an abstract declarator.
888 DeclSpec declSpec(AttrFactory);
889 declSpec.setObjCQualifiers(&DS);
890 ParseSpecifierQualifierList(declSpec);
891 Declarator declarator(declSpec, context);
892 ParseDeclarator(declarator);
893
894 // If that's not invalid, extract a type.
895 if (!declarator.isInvalidType()) {
896 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
897 if (!type.isInvalid())
898 Ty = type.get();
899
900 // If we're parsing a parameter, steal all the decl attributes
901 // and add them to the decl spec.
902 if (context == Declarator::ObjCParameterContext)
903 takeDeclAttributes(*paramAttrs, declarator);
904 }
905 } else if (context == Declarator::ObjCResultContext &&
906 Tok.is(tok::identifier)) {
Douglas Gregore97179c2011-09-08 01:46:34 +0000907 if (!Ident_instancetype)
908 Ident_instancetype = PP.getIdentifierInfo("instancetype");
909
910 if (Tok.getIdentifierInfo() == Ident_instancetype) {
911 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
912 ConsumeToken();
913 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000914 }
Douglas Gregore97179c2011-09-08 01:46:34 +0000915
Steve Naroffd7333c22008-10-21 14:15:04 +0000916 if (Tok.is(tok::r_paren))
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000917 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000918 else if (Tok.getLocation() == TypeStartLoc) {
919 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000920 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000921 SkipUntil(tok::r_paren);
922 } else {
923 // Otherwise, we found *something*, but didn't get a ')' in the right
924 // place. Emit an error then return what we have as the type.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000925 T.consumeClose();
Chris Lattner4a76b292008-10-22 03:52:06 +0000926 }
Steve Narofff28b2642007-09-05 23:30:30 +0000927 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000928}
929
930/// objc-method-decl:
931/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000932/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000933/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000934/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000935///
936/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000937/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000938/// objc-keyword-selector objc-keyword-decl
939///
940/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000941/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
942/// objc-selector ':' objc-keyword-attributes[opt] identifier
943/// ':' objc-type-name objc-keyword-attributes[opt] identifier
944/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000945///
Steve Naroff4985ace2007-08-22 18:35:33 +0000946/// objc-parmlist:
947/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000948///
Steve Naroff4985ace2007-08-22 18:35:33 +0000949/// objc-parms:
950/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000951///
Steve Naroff4985ace2007-08-22 18:35:33 +0000952/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000953/// , ...
954///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000955/// objc-keyword-attributes: [OBJC2]
956/// __attribute__((unused))
957///
John McCalld226f652010-08-21 09:40:31 +0000958Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000959 tok::TokenKind mType,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +0000960 tok::ObjCKeywordKind MethodImplKind,
961 bool MethodDefinition) {
John McCall54abf7d2009-11-04 02:18:39 +0000962 ParsingDeclRAIIObject PD(*this);
963
Douglas Gregore8f5a172010-04-07 00:21:17 +0000964 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000965 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000966 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000967 cutOffParsing();
968 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000969 }
970
Chris Lattnere8904e92008-08-23 01:48:03 +0000971 // Parse the return type if present.
John McCallb3d87482010-08-24 05:47:05 +0000972 ParsedType ReturnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000973 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000974 if (Tok.is(tok::l_paren))
John McCallcdda47f2011-10-01 09:56:14 +0000975 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Ted Kremenek9e049352010-02-18 23:05:16 +0000977 // If attributes exist before the method, parse them.
John McCall0b7e6782011-03-24 11:26:52 +0000978 ParsedAttributes methodAttrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000979 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +0000980 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek9e049352010-02-18 23:05:16 +0000981
Douglas Gregore8f5a172010-04-07 00:21:17 +0000982 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000983 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000984 ReturnType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000985 cutOffParsing();
986 return 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +0000987 }
988
Ted Kremenek9e049352010-02-18 23:05:16 +0000989 // Now parse the selector.
Steve Naroffbef11852007-10-26 20:53:56 +0000990 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000991 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000992
Steve Naroff84c43102009-02-11 20:43:13 +0000993 // An unnamed colon is valid.
994 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000995 Diag(Tok, diag::err_expected_selector_for_method)
996 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000997 // Skip until we get a ; or {}.
998 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000999 return 0;
Chris Lattnere8904e92008-08-23 01:48:03 +00001000 }
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Chris Lattner5f9e2722011-07-23 10:55:15 +00001002 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattnerdf195262007-10-09 17:51:17 +00001003 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001004 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001005 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001006 MaybeParseGNUAttributes(methodAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Chris Lattnerff384912007-10-07 02:00:24 +00001008 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCalld226f652010-08-21 09:40:31 +00001009 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001010 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001011 mType, DSRet, ReturnType,
Douglas Gregor926df6c2011-06-11 01:09:30 +00001012 selLoc, Sel, 0,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001013 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001014 methodAttrs.getList(), MethodImplKind,
1015 false, MethodDefinition);
John McCall54abf7d2009-11-04 02:18:39 +00001016 PD.complete(Result);
1017 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +00001018 }
Steve Narofff28b2642007-09-05 23:30:30 +00001019
Chris Lattner5f9e2722011-07-23 10:55:15 +00001020 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001021 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001022 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001023 ParseScope PrototypeScope(this,
1024 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall0b7e6782011-03-24 11:26:52 +00001025
1026 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001027
Chris Lattnerff384912007-10-07 02:00:24 +00001028 while (1) {
John McCall0b7e6782011-03-24 11:26:52 +00001029 ParsedAttributes paramAttrs(AttrFactory);
John McCallf312b1e2010-08-26 23:41:50 +00001030 Sema::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Chris Lattnerff384912007-10-07 02:00:24 +00001032 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +00001033 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001034 Diag(Tok, diag::err_expected_colon);
1035 break;
1036 }
1037 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001038
John McCallb3d87482010-08-24 05:47:05 +00001039 ArgInfo.Type = ParsedType();
Chris Lattnere294d3f2009-04-11 18:57:04 +00001040 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCallcdda47f2011-10-01 09:56:14 +00001041 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1042 Declarator::ObjCParameterContext,
1043 &paramAttrs);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001044
Chris Lattnerff384912007-10-07 02:00:24 +00001045 // If attributes exist before the argument name, parse them.
John McCallcdda47f2011-10-01 09:56:14 +00001046 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnere294d3f2009-04-11 18:57:04 +00001047 ArgInfo.ArgAttrs = 0;
John McCall7f040a92010-12-24 02:08:15 +00001048 if (getLang().ObjC2) {
John McCall0b7e6782011-03-24 11:26:52 +00001049 MaybeParseGNUAttributes(paramAttrs);
1050 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall7f040a92010-12-24 02:08:15 +00001051 }
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001052
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001053 // Code completion for the next piece of the selector.
1054 if (Tok.is(tok::code_completion)) {
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001055 KeyIdents.push_back(SelIdent);
1056 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1057 mType == tok::minus,
1058 /*AtParameterName=*/true,
1059 ReturnType,
1060 KeyIdents.data(),
1061 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001062 cutOffParsing();
1063 return 0;
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001064 }
1065
Chris Lattnerdf195262007-10-09 17:51:17 +00001066 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001067 Diag(Tok, diag::err_expected_ident); // missing argument name.
1068 break;
Steve Naroff4985ace2007-08-22 18:35:33 +00001069 }
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Chris Lattnere294d3f2009-04-11 18:57:04 +00001071 ArgInfo.Name = Tok.getIdentifierInfo();
1072 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +00001073 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Chris Lattnere294d3f2009-04-11 18:57:04 +00001075 ArgInfos.push_back(ArgInfo);
1076 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001077 KeyLocs.push_back(selLoc);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001078
John McCall0b7e6782011-03-24 11:26:52 +00001079 // Make sure the attributes persist.
1080 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1081
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001082 // Code completion for the next piece of the selector.
1083 if (Tok.is(tok::code_completion)) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001084 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1085 mType == tok::minus,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00001086 /*AtParameterName=*/false,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001087 ReturnType,
1088 KeyIdents.data(),
1089 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001090 cutOffParsing();
1091 return 0;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00001092 }
1093
Chris Lattnerff384912007-10-07 02:00:24 +00001094 // Check for another keyword selector.
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001095 SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001096 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +00001097 break;
1098 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +00001099 }
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Steve Naroff335eafa2007-11-15 12:35:21 +00001101 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Chris Lattnerff384912007-10-07 02:00:24 +00001103 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +00001104 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +00001105 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001106 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +00001107 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +00001108 ConsumeToken();
1109 break;
1110 }
John McCall0b7e6782011-03-24 11:26:52 +00001111 DeclSpec DS(AttrFactory);
Chris Lattnerff384912007-10-07 02:00:24 +00001112 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001113 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +00001114 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1115 ParseDeclarator(ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001116 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCalld226f652010-08-21 09:40:31 +00001117 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001118 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1119 ParmDecl.getIdentifierLoc(),
1120 Param,
1121 0));
1122
Chris Lattnerff384912007-10-07 02:00:24 +00001123 }
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Cameron Esfahani9c4bb2c2010-10-12 00:21:25 +00001125 // FIXME: Add support for optional parameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +00001126 // If attributes exist after the method, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001127 if (getLang().ObjC2)
John McCall0b7e6782011-03-24 11:26:52 +00001128 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001129
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001130 if (KeyIdents.size() == 0)
John McCalld226f652010-08-21 09:40:31 +00001131 return 0;
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001132
Chris Lattnerff384912007-10-07 02:00:24 +00001133 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1134 &KeyIdents[0]);
John McCalld226f652010-08-21 09:40:31 +00001135 Decl *Result
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001136 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001137 mType, DSRet, ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00001138 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001139 CParamInfo.data(), CParamInfo.size(),
John McCall0b7e6782011-03-24 11:26:52 +00001140 methodAttrs.getList(),
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00001141 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00001142
John McCall54abf7d2009-11-04 02:18:39 +00001143 PD.complete(Result);
1144 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +00001145}
1146
Steve Naroffdac269b2007-08-20 21:31:48 +00001147/// objc-protocol-refs:
1148/// '<' identifier-list '>'
1149///
Chris Lattner7caeabd2008-07-21 22:17:28 +00001150bool Parser::
Chris Lattner5f9e2722011-07-23 10:55:15 +00001151ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1152 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001153 bool WarnOnDeclarations,
1154 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +00001155 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001157 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Chris Lattner5f9e2722011-07-23 10:55:15 +00001159 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Chris Lattnere13b9592008-07-26 04:03:38 +00001161 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +00001162 if (Tok.is(tok::code_completion)) {
1163 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1164 ProtocolIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001165 cutOffParsing();
1166 return true;
Douglas Gregor55385fe2009-11-18 04:19:12 +00001167 }
1168
Chris Lattnere13b9592008-07-26 04:03:38 +00001169 if (Tok.isNot(tok::identifier)) {
1170 Diag(Tok, diag::err_expected_ident);
1171 SkipUntil(tok::greater);
1172 return true;
1173 }
1174 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1175 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001176 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +00001177 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Chris Lattnere13b9592008-07-26 04:03:38 +00001179 if (Tok.isNot(tok::comma))
1180 break;
1181 ConsumeToken();
1182 }
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Chris Lattnere13b9592008-07-26 04:03:38 +00001184 // Consume the '>'.
1185 if (Tok.isNot(tok::greater)) {
1186 Diag(Tok, diag::err_expected_greater);
1187 return true;
1188 }
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Chris Lattnere13b9592008-07-26 04:03:38 +00001190 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Chris Lattnere13b9592008-07-26 04:03:38 +00001192 // Convert the list of protocols identifiers into a list of protocol decls.
1193 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1194 &ProtocolIdents[0], ProtocolIdents.size(),
1195 Protocols);
1196 return false;
1197}
1198
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001199/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1200/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor46f936e2010-11-19 17:10:50 +00001201bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001202 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1203 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1204 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001205 SmallVector<Decl *, 8> ProtocolDecl;
1206 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor46f936e2010-11-19 17:10:50 +00001207 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1208 LAngleLoc, EndProtoLoc);
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001209 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1210 ProtocolLocs.data(), LAngleLoc);
1211 if (EndProtoLoc.isValid())
1212 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor46f936e2010-11-19 17:10:50 +00001213 return Result;
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001214}
1215
1216
Steve Naroffdac269b2007-08-20 21:31:48 +00001217/// objc-class-instance-variables:
1218/// '{' objc-instance-variable-decl-list[opt] '}'
1219///
1220/// objc-instance-variable-decl-list:
1221/// objc-visibility-spec
1222/// objc-instance-variable-decl ';'
1223/// ';'
1224/// objc-instance-variable-decl-list objc-visibility-spec
1225/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1226/// objc-instance-variable-decl-list ';'
1227///
1228/// objc-visibility-spec:
1229/// @private
1230/// @protected
1231/// @public
Steve Naroffddbff782007-08-21 21:17:12 +00001232/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +00001233///
1234/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +00001235/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +00001236///
John McCalld226f652010-08-21 09:40:31 +00001237void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian83c481a2010-02-22 23:04:20 +00001238 tok::ObjCKeywordKind visibility,
Steve Naroff60fccee2007-10-29 21:38:07 +00001239 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +00001240 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner5f9e2722011-07-23 10:55:15 +00001241 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001242
Douglas Gregor1a0d31a2009-01-12 18:45:55 +00001243 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001244 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor72de6672009-01-08 20:45:30 +00001245
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001246 BalancedDelimiterTracker T(*this, tok::l_brace);
1247 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Steve Naroffddbff782007-08-21 21:17:12 +00001249 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +00001250 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001251 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Steve Naroffddbff782007-08-21 21:17:12 +00001253 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +00001254 if (Tok.is(tok::semi)) {
Douglas Gregorf13ca062010-06-16 23:08:59 +00001255 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001256 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroffddbff782007-08-21 21:17:12 +00001257 ConsumeToken();
1258 continue;
1259 }
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Steve Naroffddbff782007-08-21 21:17:12 +00001261 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +00001262 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +00001263 ConsumeToken(); // eat the @ sign
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001264
1265 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001266 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001267 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001268 }
1269
Steve Naroff861cf3e2007-08-23 18:16:40 +00001270 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +00001271 case tok::objc_private:
1272 case tok::objc_public:
1273 case tok::objc_protected:
1274 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +00001275 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +00001276 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001277 continue;
Steve Naroffddbff782007-08-21 21:17:12 +00001278 default:
1279 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +00001280 continue;
1281 }
1282 }
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001284 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001285 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001286 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001287 return cutOffParsing();
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001288 }
1289
John McCallbdd563e2009-11-03 02:38:08 +00001290 struct ObjCIvarCallback : FieldCallback {
1291 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001292 Decl *IDecl;
John McCallbdd563e2009-11-03 02:38:08 +00001293 tok::ObjCKeywordKind visibility;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001294 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001295
John McCalld226f652010-08-21 09:40:31 +00001296 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001297 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001298 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1299 }
1300
John McCalld226f652010-08-21 09:40:31 +00001301 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001302 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallbdd563e2009-11-03 02:38:08 +00001303 // Install the declarator into the interface decl.
John McCalld226f652010-08-21 09:40:31 +00001304 Decl *Field
Douglas Gregor23c94db2010-07-02 17:43:08 +00001305 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallbdd563e2009-11-03 02:38:08 +00001306 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001307 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001308 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian0bd04592010-04-06 22:43:48 +00001309 if (Field)
1310 AllIvarDecls.push_back(Field);
John McCallbdd563e2009-11-03 02:38:08 +00001311 return Field;
1312 }
1313 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahaniand097be82010-08-23 22:46:52 +00001314
Chris Lattnere1359422008-04-10 06:46:29 +00001315 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00001316 DeclSpec DS(AttrFactory);
John McCallbdd563e2009-11-03 02:38:08 +00001317 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Chris Lattnerdf195262007-10-09 17:51:17 +00001319 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +00001320 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +00001321 } else {
1322 Diag(Tok, diag::err_expected_semi_decl_list);
1323 // Skip to end of block or statement
1324 SkipUntil(tok::r_brace, true, true);
1325 }
1326 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001327 T.consumeClose();
1328
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001329 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001330 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
Fariborz Jahanian10af8792011-08-29 17:33:12 +00001331 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff8749be52007-10-31 22:11:35 +00001332 // Call ActOnFields() even if we don't have any decls. This is useful
1333 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001334 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie77b6de02011-09-22 02:58:26 +00001335 AllIvarDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001336 T.getOpenLocation(), T.getCloseLocation(), 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001337 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001338}
Steve Naroffdac269b2007-08-20 21:31:48 +00001339
1340/// objc-protocol-declaration:
1341/// objc-protocol-definition
1342/// objc-protocol-forward-reference
1343///
1344/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001345/// @protocol identifier
1346/// objc-protocol-refs[opt]
1347/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001348/// @end
1349///
1350/// objc-protocol-forward-reference:
1351/// @protocol identifier-list ';'
1352///
1353/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001354/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001355/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001356Parser::DeclGroupPtrTy
1357Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1358 ParsedAttributes &attrs) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001359 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001360 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1361 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Douglas Gregor083128f2009-11-18 04:49:41 +00001363 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001364 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001365 cutOffParsing();
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001366 return DeclGroupPtrTy();
Douglas Gregor083128f2009-11-18 04:49:41 +00001367 }
1368
Chris Lattnerdf195262007-10-09 17:51:17 +00001369 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001370 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001371 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001372 }
1373 // Save the protocol name, then consume it.
1374 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1375 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Chris Lattnerdf195262007-10-09 17:51:17 +00001377 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001378 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001379 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001380 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall7f040a92010-12-24 02:08:15 +00001381 attrs.getList());
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001382 }
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Erik Verbruggen90ec96f2011-12-08 09:58:43 +00001384 CheckNestedObjCContexts(AtLoc);
1385
Chris Lattnerdf195262007-10-09 17:51:17 +00001386 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001387 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001388 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1389
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001390 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001391 while (1) {
1392 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001393 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001394 Diag(Tok, diag::err_expected_ident);
1395 SkipUntil(tok::semi);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001396 return DeclGroupPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001397 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001398 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1399 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001400 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Chris Lattnerdf195262007-10-09 17:51:17 +00001402 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001403 break;
1404 }
1405 // Consume the ';'.
1406 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001407 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Steve Naroffe440eb82007-10-10 17:32:04 +00001409 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001410 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001411 ProtocolRefs.size(),
John McCall7f040a92010-12-24 02:08:15 +00001412 attrs.getList());
Chris Lattner7caeabd2008-07-21 22:17:28 +00001413 }
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001415 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001416 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001417
Chris Lattner5f9e2722011-07-23 10:55:15 +00001418 SmallVector<Decl *, 8> ProtocolRefs;
1419 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001420 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001421 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1422 LAngleLoc, EndProtoLoc))
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001423 return DeclGroupPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001424
John McCalld226f652010-08-21 09:40:31 +00001425 Decl *ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001426 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001427 ProtocolRefs.data(),
1428 ProtocolRefs.size(),
Douglas Gregor18df52b2010-01-16 15:02:53 +00001429 ProtocolLocs.data(),
John McCall7f040a92010-12-24 02:08:15 +00001430 EndProtoLoc, attrs.getList());
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001431
Fariborz Jahanian2f64cfe2011-08-22 21:44:58 +00001432 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorbd9482d2012-01-01 21:23:57 +00001433 return Actions.ConvertDeclToDeclGroup(ProtoType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001434}
Steve Naroffdac269b2007-08-20 21:31:48 +00001435
1436/// objc-implementation:
1437/// objc-class-implementation-prologue
1438/// objc-category-implementation-prologue
1439///
1440/// objc-class-implementation-prologue:
1441/// @implementation identifier objc-superclass[opt]
1442/// objc-class-instance-variables[opt]
1443///
1444/// objc-category-implementation-prologue:
1445/// @implementation identifier ( identifier )
Erik Verbruggend64251f2011-12-06 09:25:23 +00001446Decl *Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001447 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1448 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggend64251f2011-12-06 09:25:23 +00001449 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001450 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001452 // Code completion after '@implementation'.
1453 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001454 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001455 cutOffParsing();
1456 return 0;
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001457 }
1458
Chris Lattnerdf195262007-10-09 17:51:17 +00001459 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001460 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCalld226f652010-08-21 09:40:31 +00001461 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001462 }
1463 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001464 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001465 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001466
1467 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001468 // we have a category implementation.
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001469 ConsumeParen();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001470 SourceLocation categoryLoc, rparenLoc;
1471 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001473 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001474 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001475 cutOffParsing();
1476 return 0;
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001477 }
1478
Chris Lattnerdf195262007-10-09 17:51:17 +00001479 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001480 categoryId = Tok.getIdentifierInfo();
1481 categoryLoc = ConsumeToken();
1482 } else {
1483 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCalld226f652010-08-21 09:40:31 +00001484 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001485 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001486 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001487 Diag(Tok, diag::err_expected_rparen);
1488 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCalld226f652010-08-21 09:40:31 +00001489 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001490 }
1491 rparenLoc = ConsumeParen();
John McCalld226f652010-08-21 09:40:31 +00001492 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001493 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001494 categoryLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001495
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001496 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001497 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001498 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001499 }
1500 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001501 SourceLocation superClassLoc;
1502 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001503 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001504 // We have a super class
1505 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001506 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001507 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCalld226f652010-08-21 09:40:31 +00001508 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001509 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001510 superClassId = Tok.getIdentifierInfo();
1511 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001512 }
John McCalld226f652010-08-21 09:40:31 +00001513 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Erik Verbruggend64251f2011-12-06 09:25:23 +00001514 AtLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001515 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Steve Naroff60fccee2007-10-29 21:38:07 +00001517 if (Tok.is(tok::l_brace)) // we have ivars
Erik Verbruggend64251f2011-12-06 09:25:23 +00001518 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, AtLoc);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001519
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001520 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001521 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCalld226f652010-08-21 09:40:31 +00001522 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001523}
Steve Naroff60fccee2007-10-29 21:38:07 +00001524
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001525Parser::DeclGroupPtrTy
1526Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001527 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1528 "ParseObjCAtEndDeclaration(): Expected @end");
1529 ConsumeToken(); // the "end" identifier
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001530 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001531 Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001532 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1533 Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
Argyrios Kyrtzidis35593a92011-11-16 02:35:10 +00001534 if (D)
1535 DeclsInGroup.push_back(D);
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001536 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001537 DeclsInGroup.push_back(ObjCImpDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001538
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001539 if (ObjCImpDecl) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001540 Actions.ActOnAtEnd(getCurScope(), atEnd);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001541 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001542 }
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001543 else
Ted Kremenek782f2f52010-01-07 01:20:12 +00001544 // missing @implementation
Erik Verbruggend64251f2011-12-06 09:25:23 +00001545 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001546
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001547 clearLateParsedObjCMethods();
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001548 ObjCImpDecl = 0;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001549 return Actions.BuildDeclaratorGroup(
1550 DeclsInGroup.data(), DeclsInGroup.size(), false);
Steve Naroffdac269b2007-08-20 21:31:48 +00001551}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001552
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001553Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1554 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001555 if (PendingObjCImpDecl.empty())
John McCalld226f652010-08-21 09:40:31 +00001556 return Actions.ConvertDeclToDeclGroup(0);
Erik Verbruggend64251f2011-12-06 09:25:23 +00001557
John McCalld226f652010-08-21 09:40:31 +00001558 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Erik Verbruggend64251f2011-12-06 09:25:23 +00001559 Actions.ActOnAtEnd(getCurScope(), SourceRange(Tok.getLocation()));
1560 Diag(Tok, diag::err_objc_missing_end)
1561 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
1562 if (ImpDecl)
1563 Diag(ImpDecl->getLocStart(), diag::note_objc_container_start)
1564 << Sema::OCK_Implementation;
1565
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001566 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1567}
1568
Argyrios Kyrtzidis2fea2242011-11-29 08:14:54 +00001569void Parser::clearLateParsedObjCMethods() {
1570 for (LateParsedObjCMethodContainer::iterator
1571 I = LateParsedObjCMethods.begin(),
1572 E = LateParsedObjCMethods.end(); I != E; ++I)
1573 delete *I;
1574 LateParsedObjCMethods.clear();
1575}
1576
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001577/// compatibility-alias-decl:
1578/// @compatibility_alias alias-name class-name ';'
1579///
John McCalld226f652010-08-21 09:40:31 +00001580Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001581 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1582 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1583 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001584 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001585 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001586 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001587 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001588 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1589 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001590 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001591 Diag(Tok, diag::err_expected_ident);
John McCalld226f652010-08-21 09:40:31 +00001592 return 0;
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001593 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001594 IdentifierInfo *classId = Tok.getIdentifierInfo();
1595 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001596 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1597 "@compatibility_alias");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001598 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1599 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001600}
1601
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001602/// property-synthesis:
1603/// @synthesize property-ivar-list ';'
1604///
1605/// property-ivar-list:
1606/// property-ivar
1607/// property-ivar-list ',' property-ivar
1608///
1609/// property-ivar:
1610/// identifier
1611/// identifier '=' identifier
1612///
John McCalld226f652010-08-21 09:40:31 +00001613Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001614 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1615 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001616 ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Douglas Gregorb328c422009-11-18 19:45:45 +00001618 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001619 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001620 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001621 cutOffParsing();
1622 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001623 }
1624
Douglas Gregorb328c422009-11-18 19:45:45 +00001625 if (Tok.isNot(tok::identifier)) {
1626 Diag(Tok, diag::err_synthesized_property_name);
1627 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001628 return 0;
Douglas Gregorb328c422009-11-18 19:45:45 +00001629 }
1630
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001631 IdentifierInfo *propertyIvar = 0;
1632 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1633 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregora4ffd852010-11-17 01:03:52 +00001634 SourceLocation propertyIvarLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +00001635 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001636 // property '=' ivar-name
1637 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001638
1639 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001640 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001641 cutOffParsing();
1642 return 0;
Douglas Gregor322328b2009-11-18 22:32:06 +00001643 }
1644
Chris Lattnerdf195262007-10-09 17:51:17 +00001645 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001646 Diag(Tok, diag::err_expected_ident);
1647 break;
1648 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001649 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregora4ffd852010-11-17 01:03:52 +00001650 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001651 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001652 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001653 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001654 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001655 break;
1656 ConsumeToken(); // consume ','
1657 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001658 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCalld226f652010-08-21 09:40:31 +00001659 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001660}
1661
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001662/// property-dynamic:
1663/// @dynamic property-list
1664///
1665/// property-list:
1666/// identifier
1667/// property-list ',' identifier
1668///
John McCalld226f652010-08-21 09:40:31 +00001669Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001670 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1671 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001672 ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001673 while (true) {
1674 if (Tok.is(tok::code_completion)) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001675 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001676 cutOffParsing();
1677 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001678 }
1679
1680 if (Tok.isNot(tok::identifier)) {
1681 Diag(Tok, diag::err_expected_ident);
1682 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +00001683 return 0;
Douglas Gregor424b2a52009-11-18 22:56:13 +00001684 }
1685
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001686 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1687 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001688 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001689 propertyId, 0, SourceLocation());
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001690
Chris Lattnerdf195262007-10-09 17:51:17 +00001691 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001692 break;
1693 ConsumeToken(); // consume ','
1694 }
Douglas Gregore6bf90a2011-01-05 01:10:06 +00001695 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCalld226f652010-08-21 09:40:31 +00001696 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001697}
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001699/// objc-throw-statement:
1700/// throw expression[opt];
1701///
John McCall60d7b3a2010-08-24 06:29:42 +00001702StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1703 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001704 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001705 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001706 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001707 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001708 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001709 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001710 }
1711 }
Ted Kremenek02418c72010-04-20 21:21:51 +00001712 // consume ';'
1713 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCall9ae2f072010-08-23 23:25:46 +00001714 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001715}
1716
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001717/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001718/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001719///
John McCall60d7b3a2010-08-24 06:29:42 +00001720StmtResult
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001721Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001722 ConsumeToken(); // consume synchronized
1723 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001724 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001725 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001726 }
John McCall07524032011-07-27 21:50:02 +00001727
1728 // The operand is surrounded with parentheses.
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001729 ConsumeParen(); // '('
John McCall07524032011-07-27 21:50:02 +00001730 ExprResult operand(ParseExpression());
1731
1732 if (Tok.is(tok::r_paren)) {
1733 ConsumeParen(); // ')'
1734 } else {
1735 if (!operand.isInvalid())
1736 Diag(Tok, diag::err_expected_rparen);
1737
1738 // Skip forward until we see a left brace, but don't consume it.
1739 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001740 }
John McCall07524032011-07-27 21:50:02 +00001741
1742 // Require a compound statement.
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001743 if (Tok.isNot(tok::l_brace)) {
John McCall07524032011-07-27 21:50:02 +00001744 if (!operand.isInvalid())
1745 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001746 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001747 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001748
John McCall07524032011-07-27 21:50:02 +00001749 // Check the @synchronized operand now.
1750 if (!operand.isInvalid())
1751 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001752
John McCall07524032011-07-27 21:50:02 +00001753 // Parse the compound statement within a new scope.
1754 ParseScope bodyScope(this, Scope::DeclScope);
1755 StmtResult body(ParseCompoundStatementBody());
1756 bodyScope.Exit();
1757
1758 // If there was a semantic or parse error earlier with the
1759 // operand, fail now.
1760 if (operand.isInvalid())
1761 return StmtError();
1762
1763 if (body.isInvalid())
1764 body = Actions.ActOnNullStmt(Tok.getLocation());
1765
1766 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001767}
1768
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001769/// objc-try-catch-statement:
1770/// @try compound-statement objc-catch-list[opt]
1771/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1772///
1773/// objc-catch-list:
1774/// @catch ( parameter-declaration ) compound-statement
1775/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1776/// catch-parameter-declaration:
1777/// parameter-declaration
1778/// '...' [OBJC2]
1779///
John McCall60d7b3a2010-08-24 06:29:42 +00001780StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001781 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001782
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001783 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001784 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001785 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001786 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001787 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001788 StmtVector CatchStmts(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001789 StmtResult FinallyStmt;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001790 ParseScope TryScope(this, Scope::DeclScope);
John McCall60d7b3a2010-08-24 06:29:42 +00001791 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001792 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001793 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001794 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001795
Chris Lattnerdf195262007-10-09 17:51:17 +00001796 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001797 // At this point, we need to lookahead to determine if this @ is the start
1798 // of an @catch or @finally. We don't want to consume the @ token if this
1799 // is an @try or @encode or something else.
1800 Token AfterAt = GetLookAheadToken(1);
1801 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1802 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1803 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001804
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001805 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001806 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCalld226f652010-08-21 09:40:31 +00001807 Decl *FirstPart = 0;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001808 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001809 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001810 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001811 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001812 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001813 DeclSpec DS(AttrFactory);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001814 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis17b63992011-07-01 22:22:40 +00001815 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff7ba138a2009-03-03 19:52:17 +00001816 ParseDeclarator(ParmDecl);
1817
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001818 // Inform the actions module about the declarator, so it
Steve Naroff7ba138a2009-03-03 19:52:17 +00001819 // gets added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001820 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001821 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001822 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Steve Naroff93a25952009-04-07 22:56:58 +00001824 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001825
Steve Naroff93a25952009-04-07 22:56:58 +00001826 if (Tok.is(tok::r_paren))
1827 RParenLoc = ConsumeParen();
1828 else // Skip over garbage, until we get to ')'. Eat the ')'.
1829 SkipUntil(tok::r_paren, true, false);
1830
John McCall60d7b3a2010-08-24 06:29:42 +00001831 StmtResult CatchBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001832 if (Tok.is(tok::l_brace))
1833 CatchBody = ParseCompoundStatementBody();
1834 else
1835 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001836 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001837 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001838
John McCall60d7b3a2010-08-24 06:29:42 +00001839 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001840 RParenLoc,
1841 FirstPart,
John McCall9ae2f072010-08-23 23:25:46 +00001842 CatchBody.take());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001843 if (!Catch.isInvalid())
1844 CatchStmts.push_back(Catch.release());
1845
Steve Naroff64515f32008-02-05 21:27:35 +00001846 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001847 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1848 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001849 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001850 }
1851 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001852 } else {
1853 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001854 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001855 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001856
John McCall60d7b3a2010-08-24 06:29:42 +00001857 StmtResult FinallyBody(true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001858 if (Tok.is(tok::l_brace))
1859 FinallyBody = ParseCompoundStatementBody();
1860 else
1861 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001862 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001863 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001864 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001865 FinallyBody.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001866 catch_or_finally_seen = true;
1867 break;
1868 }
1869 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001870 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001871 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001872 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001873 }
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001874
John McCall9ae2f072010-08-23 23:25:46 +00001875 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00001876 move_arg(CatchStmts),
John McCall9ae2f072010-08-23 23:25:46 +00001877 FinallyStmt.take());
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001878}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001879
John McCallf85e1932011-06-15 23:02:42 +00001880/// objc-autoreleasepool-statement:
1881/// @autoreleasepool compound-statement
1882///
1883StmtResult
1884Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1885 ConsumeToken(); // consume autoreleasepool
1886 if (Tok.isNot(tok::l_brace)) {
1887 Diag(Tok, diag::err_expected_lbrace);
1888 return StmtError();
1889 }
1890 // Enter a scope to hold everything within the compound stmt. Compound
1891 // statements can always hold declarations.
1892 ParseScope BodyScope(this, Scope::DeclScope);
1893
1894 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1895
1896 BodyScope.Exit();
1897 if (AutoreleasePoolBody.isInvalid())
1898 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1899 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1900 AutoreleasePoolBody.take());
1901}
1902
Steve Naroff3536b442007-09-06 21:24:23 +00001903/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001904///
John McCalld226f652010-08-21 09:40:31 +00001905Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001906 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump1eb44332009-09-09 15:08:12 +00001907
John McCallf312b1e2010-08-26 23:41:50 +00001908 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1909 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001910
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001911 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001912 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001913 if (ObjCImpDecl) {
1914 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregor849b2432010-03-31 17:46:05 +00001915 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek496e45e2009-11-10 22:55:49 +00001916 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001917 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001918 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001919
Steve Naroff409be832007-11-11 19:54:21 +00001920 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001921 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001922 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001923
Steve Naroff409be832007-11-11 19:54:21 +00001924 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1925 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001926
Steve Naroff409be832007-11-11 19:54:21 +00001927 // If we didn't find the '{', bail out.
1928 if (Tok.isNot(tok::l_brace))
John McCalld226f652010-08-21 09:40:31 +00001929 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001930 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00001931 // Allow the rest of sema to find private method decl implementations.
1932 if (MDecl)
1933 Actions.AddAnyMethodToGlobalPool(MDecl);
1934
1935 // Consume the tokens and store them for later parsing.
1936 LexedMethod* LM = new LexedMethod(this, MDecl);
1937 LateParsedObjCMethods.push_back(LM);
1938 CachedTokens &Toks = LM->Toks;
1939 // Begin by storing the '{' token.
1940 Toks.push_back(Tok);
1941 ConsumeBrace();
1942 // Consume everything up to (and including) the matching right brace.
1943 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Steve Naroff71c0a952007-11-13 23:01:27 +00001944 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001945}
Anders Carlsson55085182007-08-21 17:43:55 +00001946
John McCall60d7b3a2010-08-24 06:29:42 +00001947StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001948 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001949 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001950 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001951 return StmtError();
Chris Lattner5d803162009-12-07 16:33:19 +00001952 }
1953
1954 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner6b884502008-03-10 06:06:04 +00001955 return ParseObjCTryStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001956
1957 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroff64515f32008-02-05 21:27:35 +00001958 return ParseObjCThrowStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001959
1960 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroff64515f32008-02-05 21:27:35 +00001961 return ParseObjCSynchronizedStmt(AtLoc);
John McCallf85e1932011-06-15 23:02:42 +00001962
1963 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1964 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner5d803162009-12-07 16:33:19 +00001965
John McCall60d7b3a2010-08-24 06:29:42 +00001966 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001967 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001968 // If the expression is invalid, skip ahead to the next semicolon. Not
1969 // doing this opens us up to the possibility of infinite loops if
1970 // ParseExpression does not consume any tokens.
1971 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001972 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001973 }
Chris Lattner5d803162009-12-07 16:33:19 +00001974
Steve Naroff64515f32008-02-05 21:27:35 +00001975 // Otherwise, eat the semicolon.
Douglas Gregor9ba23b42010-09-07 15:23:11 +00001976 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +00001977 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroff64515f32008-02-05 21:27:35 +00001978}
1979
John McCall60d7b3a2010-08-24 06:29:42 +00001980ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001981 switch (Tok.getKind()) {
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001982 case tok::code_completion:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001983 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001984 cutOffParsing();
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00001985 return ExprError();
1986
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001987 case tok::string_literal: // primary-expression: string-literal
1988 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001989 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001990 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001991 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001992 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001993
Chris Lattner4fef81d2008-08-05 06:19:09 +00001994 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1995 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001996 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001997 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001998 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001999 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00002000 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002001 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00002002 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00002003 }
Anders Carlsson55085182007-08-21 17:43:55 +00002004 }
Anders Carlsson55085182007-08-21 17:43:55 +00002005}
2006
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002007/// \brirg Parse the receiver of an Objective-C++ message send.
2008///
2009/// This routine parses the receiver of a message send in
2010/// Objective-C++ either as a type or as an expression. Note that this
2011/// routine must not be called to parse a send to 'super', since it
2012/// has no way to return such a result.
2013///
2014/// \param IsExpr Whether the receiver was parsed as an expression.
2015///
2016/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2017/// IsExpr is true), the parsed expression. If the receiver was parsed
2018/// as a type (\c IsExpr is false), the parsed type.
2019///
2020/// \returns True if an error occurred during parsing or semantic
2021/// analysis, in which case the arguments do not have valid
2022/// values. Otherwise, returns false for a successful parse.
2023///
2024/// objc-receiver: [C++]
2025/// 'super' [not parsed here]
2026/// expression
2027/// simple-type-specifier
2028/// typename-specifier
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002029bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002030 InMessageExpressionRAIIObject InMessage(*this, true);
2031
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002032 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2033 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2034 TryAnnotateTypeOrScopeToken();
2035
2036 if (!isCXXSimpleTypeSpecifier()) {
2037 // objc-receiver:
2038 // expression
John McCall60d7b3a2010-08-24 06:29:42 +00002039 ExprResult Receiver = ParseExpression();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002040 if (Receiver.isInvalid())
2041 return true;
2042
2043 IsExpr = true;
2044 TypeOrExpr = Receiver.take();
2045 return false;
2046 }
2047
2048 // objc-receiver:
2049 // typename-specifier
2050 // simple-type-specifier
2051 // expression (that starts with one of the above)
John McCall0b7e6782011-03-24 11:26:52 +00002052 DeclSpec DS(AttrFactory);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002053 ParseCXXSimpleTypeSpecifier(DS);
2054
2055 if (Tok.is(tok::l_paren)) {
2056 // If we see an opening parentheses at this point, we are
2057 // actually parsing an expression that starts with a
2058 // function-style cast, e.g.,
2059 //
2060 // postfix-expression:
2061 // simple-type-specifier ( expression-list [opt] )
2062 // typename-specifier ( expression-list [opt] )
2063 //
2064 // Parse the remainder of this case, then the (optional)
2065 // postfix-expression suffix, followed by the (optional)
2066 // right-hand side of the binary expression. We have an
2067 // instance method.
John McCall60d7b3a2010-08-24 06:29:42 +00002068 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002069 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002070 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002071 if (!Receiver.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002072 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002073 if (Receiver.isInvalid())
2074 return true;
2075
2076 IsExpr = true;
2077 TypeOrExpr = Receiver.take();
2078 return false;
2079 }
2080
2081 // We have a class message. Turn the simple-type-specifier or
2082 // typename-specifier we parsed into a type and parse the
2083 // remainder of the class message.
2084 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002085 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002086 if (Type.isInvalid())
2087 return true;
2088
2089 IsExpr = false;
John McCallb3d87482010-08-24 05:47:05 +00002090 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002091 return false;
2092}
2093
Douglas Gregor1b730e82010-05-31 14:40:22 +00002094/// \brief Determine whether the parser is currently referring to a an
2095/// Objective-C message send, using a simplified heuristic to avoid overhead.
2096///
2097/// This routine will only return true for a subset of valid message-send
2098/// expressions.
2099bool Parser::isSimpleObjCMessageExpression() {
Chris Lattnerc59cb382010-05-31 18:18:22 +00002100 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor1b730e82010-05-31 14:40:22 +00002101 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor1b730e82010-05-31 14:40:22 +00002102 return GetLookAheadToken(1).is(tok::identifier) &&
2103 GetLookAheadToken(2).is(tok::identifier);
2104}
2105
Douglas Gregor9497a732010-09-16 01:51:54 +00002106bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
2107 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
2108 InMessageExpression)
2109 return false;
2110
2111
2112 ParsedType Type;
2113
2114 if (Tok.is(tok::annot_typename))
2115 Type = getTypeAnnotation(Tok);
2116 else if (Tok.is(tok::identifier))
2117 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2118 getCurScope());
2119 else
2120 return false;
2121
2122 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2123 const Token &AfterNext = GetLookAheadToken(2);
2124 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2125 if (Tok.is(tok::identifier))
2126 TryAnnotateTypeOrScopeToken();
2127
2128 return Tok.is(tok::annot_typename);
2129 }
2130 }
2131
2132 return false;
2133}
2134
Mike Stump1eb44332009-09-09 15:08:12 +00002135/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002136/// '[' objc-receiver objc-message-args ']'
2137///
Douglas Gregor2725ca82010-04-21 19:57:20 +00002138/// objc-receiver: [C]
Chris Lattnereb483eb2010-04-11 08:28:14 +00002139/// 'super'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002140/// expression
2141/// class-name
2142/// type-name
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002143///
John McCall60d7b3a2010-08-24 06:29:42 +00002144ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00002145 assert(Tok.is(tok::l_square) && "'[' expected");
2146 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2147
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002148 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002149 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002150 cutOffParsing();
Douglas Gregor8e254cf2010-05-27 23:06:34 +00002151 return ExprError();
2152 }
2153
Douglas Gregor0fbda682010-09-15 14:51:05 +00002154 InMessageExpressionRAIIObject InMessage(*this, true);
2155
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002156 if (getLang().CPlusPlus) {
2157 // We completely separate the C and C++ cases because C++ requires
2158 // more complicated (read: slower) parsing.
2159
2160 // Handle send to super.
2161 // FIXME: This doesn't benefit from the same typo-correction we
2162 // get in Objective-C.
2163 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002164 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallb3d87482010-08-24 05:47:05 +00002165 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2166 ParsedType(), 0);
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002167
2168 // Parse the receiver, which is either a type or an expression.
2169 bool IsExpr;
Nick Lewycky304b7522010-09-15 18:35:19 +00002170 void *TypeOrExpr = NULL;
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002171 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2172 SkipUntil(tok::r_square);
2173 return ExprError();
2174 }
2175
2176 if (IsExpr)
John McCallb3d87482010-08-24 05:47:05 +00002177 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2178 ParsedType(),
John McCall9ae2f072010-08-23 23:25:46 +00002179 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor6aa14d82010-04-21 22:36:40 +00002180
2181 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb3d87482010-08-24 05:47:05 +00002182 ParsedType::getFromOpaquePtr(TypeOrExpr),
2183 0);
Chris Lattnerc59cb382010-05-31 18:18:22 +00002184 }
2185
2186 if (Tok.is(tok::identifier)) {
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002187 IdentifierInfo *Name = Tok.getIdentifierInfo();
2188 SourceLocation NameLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002189 ParsedType ReceiverType;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002190 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002191 Name == Ident_super,
Douglas Gregor1569f952010-04-21 20:38:13 +00002192 NextToken().is(tok::period),
2193 ReceiverType)) {
John McCallf312b1e2010-08-26 23:41:50 +00002194 case Sema::ObjCSuperMessage:
John McCallb3d87482010-08-24 05:47:05 +00002195 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2196 ParsedType(), 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002197
John McCallf312b1e2010-08-26 23:41:50 +00002198 case Sema::ObjCClassMessage:
Douglas Gregor1569f952010-04-21 20:38:13 +00002199 if (!ReceiverType) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002200 SkipUntil(tok::r_square);
2201 return ExprError();
2202 }
2203
Douglas Gregor1569f952010-04-21 20:38:13 +00002204 ConsumeToken(); // the type name
2205
2206 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00002207 ReceiverType, 0);
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002208
John McCallf312b1e2010-08-26 23:41:50 +00002209 case Sema::ObjCInstanceMessage:
Douglas Gregor2725ca82010-04-21 19:57:20 +00002210 // Fall through to parse an expression.
Douglas Gregor1dbca6e2010-04-14 02:22:16 +00002211 break;
Fariborz Jahaniand2869922009-04-08 19:50:10 +00002212 }
Chris Lattner699b6612008-01-25 18:59:06 +00002213 }
Chris Lattnereb483eb2010-04-11 08:28:14 +00002214
2215 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCall60d7b3a2010-08-24 06:29:42 +00002216 ExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002217 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00002218 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002219 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00002220 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002221
John McCallb3d87482010-08-24 05:47:05 +00002222 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2223 ParsedType(), Res.take());
Chris Lattner699b6612008-01-25 18:59:06 +00002224}
Sebastian Redl1d922962008-12-13 15:32:12 +00002225
Douglas Gregor2725ca82010-04-21 19:57:20 +00002226/// \brief Parse the remainder of an Objective-C message following the
2227/// '[' objc-receiver.
2228///
2229/// This routine handles sends to super, class messages (sent to a
2230/// class name), and instance messages (sent to an object), and the
2231/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2232/// ReceiverExpr, respectively. Only one of these parameters may have
2233/// a valid value.
2234///
2235/// \param LBracLoc The location of the opening '['.
2236///
2237/// \param SuperLoc If this is a send to 'super', the location of the
2238/// 'super' keyword that indicates a send to the superclass.
2239///
2240/// \param ReceiverType If this is a class message, the type of the
2241/// class we are sending a message to.
2242///
2243/// \param ReceiverExpr If this is an instance message, the expression
2244/// used to compute the receiver object.
Mike Stump1eb44332009-09-09 15:08:12 +00002245///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002246/// objc-message-args:
2247/// objc-selector
2248/// objc-keywordarg-list
2249///
2250/// objc-keywordarg-list:
2251/// objc-keywordarg
2252/// objc-keywordarg-list objc-keywordarg
2253///
Mike Stump1eb44332009-09-09 15:08:12 +00002254/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002255/// selector-name[opt] ':' objc-keywordexpr
2256///
2257/// objc-keywordexpr:
2258/// nonempty-expr-list
2259///
2260/// nonempty-expr-list:
2261/// assignment-expression
2262/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00002263///
John McCall60d7b3a2010-08-24 06:29:42 +00002264ExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00002265Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002266 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +00002267 ParsedType ReceiverType,
Sebastian Redl1d922962008-12-13 15:32:12 +00002268 ExprArg ReceiverExpr) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002269 InMessageExpressionRAIIObject InMessage(*this, true);
2270
Steve Naroffc4df6d22009-11-07 02:08:14 +00002271 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002272 if (SuperLoc.isValid())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002273 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2274 false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002275 else if (ReceiverType)
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002276 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2277 false);
Steve Naroffc4df6d22009-11-07 02:08:14 +00002278 else
John McCall9ae2f072010-08-23 23:25:46 +00002279 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002280 0, 0, false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002281 cutOffParsing();
2282 return ExprError();
Steve Naroffc4df6d22009-11-07 02:08:14 +00002283 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002284
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002285 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002286 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002287 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00002288
Chris Lattner5f9e2722011-07-23 10:55:15 +00002289 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002290 SmallVector<SourceLocation, 12> KeyLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002291 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00002292
Chris Lattnerdf195262007-10-09 17:51:17 +00002293 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002294 while (1) {
2295 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00002296 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002297 KeyLocs.push_back(Loc);
Steve Naroff37387c92007-09-17 20:25:27 +00002298
Chris Lattnerdf195262007-10-09 17:51:17 +00002299 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002300 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002301 // We must manually skip to a ']', otherwise the expression skipper will
2302 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2303 // the enclosing expression.
2304 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002305 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002306 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002307
Steve Naroff68d331a2007-09-27 14:38:14 +00002308 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00002309 /// Parse the expression after ':'
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002310
2311 if (Tok.is(tok::code_completion)) {
2312 if (SuperLoc.isValid())
2313 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2314 KeyIdents.data(),
2315 KeyIdents.size(),
2316 /*AtArgumentEpression=*/true);
2317 else if (ReceiverType)
2318 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2319 KeyIdents.data(),
2320 KeyIdents.size(),
2321 /*AtArgumentEpression=*/true);
2322 else
2323 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2324 KeyIdents.data(),
2325 KeyIdents.size(),
2326 /*AtArgumentEpression=*/true);
2327
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002328 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002329 return ExprError();
2330 }
2331
John McCall60d7b3a2010-08-24 06:29:42 +00002332 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002333 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002334 // We must manually skip to a ']', otherwise the expression skipper will
2335 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2336 // the enclosing expression.
2337 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002338 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00002339 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002340
Steve Naroff37387c92007-09-17 20:25:27 +00002341 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002342 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00002343
Douglas Gregord3c68542009-11-19 01:08:35 +00002344 // Code completion after each argument.
2345 if (Tok.is(tok::code_completion)) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00002346 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002347 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +00002348 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002349 KeyIdents.size(),
2350 /*AtArgumentEpression=*/false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00002351 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002352 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregord3c68542009-11-19 01:08:35 +00002353 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002354 KeyIdents.size(),
2355 /*AtArgumentEpression=*/false);
Douglas Gregord3c68542009-11-19 01:08:35 +00002356 else
John McCall9ae2f072010-08-23 23:25:46 +00002357 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregord3c68542009-11-19 01:08:35 +00002358 KeyIdents.data(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002359 KeyIdents.size(),
2360 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002361 cutOffParsing();
Douglas Gregor70c5ac72010-09-20 23:34:21 +00002362 return ExprError();
Douglas Gregord3c68542009-11-19 01:08:35 +00002363 }
2364
Steve Naroff37387c92007-09-17 20:25:27 +00002365 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00002366 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00002367 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002368 break;
2369 // We have a selector or a colon, continue parsing.
2370 }
2371 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00002372 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002373 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00002374 /// Parse the expression after ','
John McCall60d7b3a2010-08-24 06:29:42 +00002375 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002376 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00002377 // We must manually skip to a ']', otherwise the expression skipper will
2378 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2379 // the enclosing expression.
2380 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002381 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00002382 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002383
Steve Naroff49f109c2007-11-15 13:05:42 +00002384 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00002385 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002386 }
2387 } else if (!selIdent) {
2388 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00002389
Chris Lattner4fef81d2008-08-05 06:19:09 +00002390 // We must manually skip to a ']', otherwise the expression skipper will
2391 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2392 // the enclosing expression.
2393 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002394 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002395 }
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002396
Chris Lattnerdf195262007-10-09 17:51:17 +00002397 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian809872e2010-03-31 20:22:35 +00002398 if (Tok.is(tok::identifier))
2399 Diag(Tok, diag::err_expected_colon);
2400 else
2401 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00002402 // We must manually skip to a ']', otherwise the expression skipper will
2403 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2404 // the enclosing expression.
2405 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00002406 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00002407 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002408
Chris Lattner699b6612008-01-25 18:59:06 +00002409 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00002410
Steve Naroff29238a02007-10-05 18:42:47 +00002411 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002412 if (nKeys == 0) {
Chris Lattnerff384912007-10-07 02:00:24 +00002413 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002414 KeyLocs.push_back(Loc);
2415 }
Chris Lattnerff384912007-10-07 02:00:24 +00002416 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002417
Douglas Gregor2725ca82010-04-21 19:57:20 +00002418 if (SuperLoc.isValid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00002419 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002420 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002421 MultiExprArg(Actions,
2422 KeyExprs.take(),
2423 KeyExprs.size()));
Douglas Gregor2725ca82010-04-21 19:57:20 +00002424 else if (ReceiverType)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002425 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002426 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002427 MultiExprArg(Actions,
2428 KeyExprs.take(),
2429 KeyExprs.size()));
John McCall9ae2f072010-08-23 23:25:46 +00002430 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002431 LBracLoc, KeyLocs, RBracLoc,
John McCallf312b1e2010-08-26 23:41:50 +00002432 MultiExprArg(Actions,
2433 KeyExprs.take(),
2434 KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00002435}
2436
John McCall60d7b3a2010-08-24 06:29:42 +00002437ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2438 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00002439 if (Res.isInvalid()) return move(Res);
2440
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002441 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2442 // expressions. At this point, we know that the only valid thing that starts
2443 // with '@' is an @"".
Chris Lattner5f9e2722011-07-23 10:55:15 +00002444 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00002445 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002446 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00002447 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002448
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002449 while (Tok.is(tok::at)) {
2450 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00002451
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002452 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00002453 if (!isTokenStringLiteral())
2454 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002455
John McCall60d7b3a2010-08-24 06:29:42 +00002456 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002457 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00002458 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002459
Sebastian Redleffa8d12008-12-10 00:02:53 +00002460 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00002461 }
Sebastian Redl1d922962008-12-13 15:32:12 +00002462
2463 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2464 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00002465}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002466
2467/// objc-encode-expression:
2468/// @encode ( type-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002469ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002470Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00002471 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00002472
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002473 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002474
Chris Lattner4fef81d2008-08-05 06:19:09 +00002475 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002476 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2477
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002478 BalancedDelimiterTracker T(*this, tok::l_paren);
2479 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002480
Douglas Gregor809070a2009-02-18 17:45:20 +00002481 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00002482
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002483 T.consumeClose();
Sebastian Redl1d922962008-12-13 15:32:12 +00002484
Douglas Gregor809070a2009-02-18 17:45:20 +00002485 if (Ty.isInvalid())
2486 return ExprError();
2487
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002488 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
2489 T.getOpenLocation(), Ty.get(),
2490 T.getCloseLocation()));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00002491}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002492
2493/// objc-protocol-expression
2494/// @protocol ( protocol-name )
John McCall60d7b3a2010-08-24 06:29:42 +00002495ExprResult
Sebastian Redl1d922962008-12-13 15:32:12 +00002496Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002497 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002498
Chris Lattner4fef81d2008-08-05 06:19:09 +00002499 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002500 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2501
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002502 BalancedDelimiterTracker T(*this, tok::l_paren);
2503 T.consumeOpen();
Sebastian Redl1d922962008-12-13 15:32:12 +00002504
Chris Lattner4fef81d2008-08-05 06:19:09 +00002505 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00002506 return ExprError(Diag(Tok, diag::err_expected_ident));
2507
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002508 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002509 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002510
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002511 T.consumeClose();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002512
Sebastian Redl1d922962008-12-13 15:32:12 +00002513 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002514 T.getOpenLocation(),
2515 T.getCloseLocation()));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00002516}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002517
2518/// objc-selector-expression
2519/// @selector '(' objc-keyword-selector ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002520ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002521 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002522
Chris Lattner4fef81d2008-08-05 06:19:09 +00002523 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00002524 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2525
Chris Lattner5f9e2722011-07-23 10:55:15 +00002526 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002527 SourceLocation sLoc;
Douglas Gregor458433d2010-08-26 15:07:07 +00002528
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002529 BalancedDelimiterTracker T(*this, tok::l_paren);
2530 T.consumeOpen();
2531
Douglas Gregor458433d2010-08-26 15:07:07 +00002532 if (Tok.is(tok::code_completion)) {
2533 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2534 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002535 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002536 return ExprError();
2537 }
2538
Chris Lattner2fc5c242009-04-11 18:13:45 +00002539 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner5add7542010-08-27 22:32:41 +00002540 if (!SelIdent && // missing selector name.
2541 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002542 return ExprError(Diag(Tok, diag::err_expected_ident));
2543
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002544 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00002545 unsigned nColons = 0;
2546 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002547 while (1) {
Chris Lattner5add7542010-08-27 22:32:41 +00002548 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2549 ++nColons;
2550 KeyIdents.push_back(0);
2551 } else if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00002552 return ExprError(Diag(Tok, diag::err_expected_colon));
2553
Chris Lattner5add7542010-08-27 22:32:41 +00002554 ++nColons;
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002555 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002556 if (Tok.is(tok::r_paren))
2557 break;
Douglas Gregor458433d2010-08-26 15:07:07 +00002558
2559 if (Tok.is(tok::code_completion)) {
2560 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2561 KeyIdents.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002562 cutOffParsing();
Douglas Gregor458433d2010-08-26 15:07:07 +00002563 return ExprError();
2564 }
2565
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002566 // Check for another keyword selector.
2567 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00002568 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002569 KeyIdents.push_back(SelIdent);
Chris Lattner3b3e1a92011-03-26 18:11:38 +00002570 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00002571 break;
2572 }
Steve Naroff887407e2007-12-05 22:21:29 +00002573 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002574 T.consumeClose();
Steve Naroff887407e2007-12-05 22:21:29 +00002575 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00002576 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002577 T.getOpenLocation(),
2578 T.getCloseLocation()));
Gabor Greif58065b22007-10-19 15:38:32 +00002579 }
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002580
2581Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002582
2583 // Save the current token position.
2584 SourceLocation OrigLoc = Tok.getLocation();
2585
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002586 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2587 // Append the current token at the end of the new token stream so that it
2588 // doesn't get lost.
2589 LM.Toks.push_back(Tok);
2590 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2591
2592 // MDecl might be null due to error in method prototype, etc.
2593 Decl *MDecl = LM.D;
2594 // Consume the previously pushed token.
2595 ConsumeAnyToken();
2596
2597 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2598 SourceLocation BraceLoc = Tok.getLocation();
2599 // Enter a scope for the method body.
2600 ParseScope BodyScope(this,
2601 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2602
2603 // Tell the actions module that we have entered a method definition with the
2604 // specified Declarator for the method.
2605 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2606
2607 if (PP.isCodeCompletionEnabled()) {
2608 if (trySkippingFunctionBodyForCodeCompletion()) {
2609 BodyScope.Exit();
2610 return Actions.ActOnFinishFunctionBody(MDecl, 0);
2611 }
2612 }
2613
2614 StmtResult FnBody(ParseCompoundStatementBody());
2615
2616 // If the function body could not be parsed, make a bogus compoundstmt.
2617 if (FnBody.isInvalid())
2618 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2619 MultiStmtArg(Actions), false);
2620
2621 // Leave the function body scope.
2622 BodyScope.Exit();
2623
Argyrios Kyrtzidisa24195a2011-12-17 04:13:18 +00002624 MDecl = Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2625
2626 if (Tok.getLocation() != OrigLoc) {
2627 // Due to parsing error, we either went over the cached tokens or
2628 // there are still cached tokens left. If it's the latter case skip the
2629 // leftover tokens.
2630 // Since this is an uncommon situation that should be avoided, use the
2631 // expensive isBeforeInTranslationUnit call.
2632 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2633 OrigLoc))
2634 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2635 ConsumeAnyToken();
2636 }
2637
2638 return MDecl;
Fariborz Jahanian140ab232011-08-31 17:37:55 +00002639}