blob: 3ecdae2978d5d5fe434fb688b3fe184bb2f48663 [file] [log] [blame]
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerda59c2f2006-11-05 02:08:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner60f36222009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000015#include "clang/Parse/Parser.h"
Douglas Gregore9bba4f2010-09-15 14:51:05 +000016#include "RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000020#include "llvm/ADT/SmallVector.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000021using namespace clang;
22
23
Chris Lattner3a907162008-12-08 21:53:24 +000024/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000025/// external-declaration: [C99 6.9]
26/// [OBJC] objc-class-definition
Steve Naroffe0933392007-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 Jahanian3a039e32011-08-27 20:50:59 +000032Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000033 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000034
Douglas Gregorf48706c2009-12-07 09:27:33 +000035 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000036 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000037 cutOffParsing();
38 return DeclGroupPtrTy();
Douglas Gregorf48706c2009-12-07 09:27:33 +000039 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000040
41 Decl *SingleDecl = 0;
Steve Naroff7c348172007-08-23 18:16:40 +000042 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000043 case tok::objc_class:
44 return ParseObjCAtClassDeclaration(AtLoc);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000045 break;
John McCall53fa7142010-12-24 02:08:15 +000046 case tok::objc_interface: {
John McCall084e83d2011-03-24 11:26:52 +000047 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000048 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
49 break;
John McCall53fa7142010-12-24 02:08:15 +000050 }
51 case tok::objc_protocol: {
John McCall084e83d2011-03-24 11:26:52 +000052 ParsedAttributes attrs(AttrFactory);
Douglas Gregorf6102672012-01-01 21:23:57 +000053 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall53fa7142010-12-24 02:08:15 +000054 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000055 case tok::objc_implementation:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000056 SingleDecl = ParseObjCAtImplementationDeclaration(AtLoc);
57 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000058 case tok::objc_end:
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +000059 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000060 case tok::objc_compatibility_alias:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000061 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
62 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000063 case tok::objc_synthesize:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000064 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
65 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000066 case tok::objc_dynamic:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000067 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
68 break;
Douglas Gregor22d09742012-01-03 18:04:46 +000069 case tok::objc_import:
Douglas Gregor0bf886d2012-01-03 18:24:14 +000070 if (getLang().Modules)
71 return ParseModuleImport(AtLoc);
72
73 // Fall through
74
Chris Lattnerce90ef52008-08-23 02:02:23 +000075 default:
76 Diag(AtLoc, diag::err_unexpected_at);
77 SkipUntil(tok::semi);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000078 SingleDecl = 0;
79 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000080 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000081 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000082}
83
84///
Mike Stump11289f42009-09-09 15:08:12 +000085/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000086/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000087///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000088Parser::DeclGroupPtrTy
89Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000090 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +000091 SmallVector<IdentifierInfo *, 8> ClassNames;
92 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremeneka26da852009-11-17 23:12:20 +000093
Mike Stump11289f42009-09-09 15:08:12 +000094
Chris Lattnerda59c2f2006-11-05 02:08:13 +000095 while (1) {
Chris Lattner0ef13522007-10-09 17:51:17 +000096 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +000097 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000098 SkipUntil(tok::semi);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000099 return Actions.ConvertDeclToDeclGroup(0);
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000100 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000101 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +0000102 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000103 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000104
Chris Lattner0ef13522007-10-09 17:51:17 +0000105 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000106 break;
Mike Stump11289f42009-09-09 15:08:12 +0000107
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000108 ConsumeToken();
109 }
Mike Stump11289f42009-09-09 15:08:12 +0000110
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000111 // Consume the ';'.
112 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000113 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump11289f42009-09-09 15:08:12 +0000114
Ted Kremeneka26da852009-11-17 23:12:20 +0000115 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
116 ClassLocs.data(),
117 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000118}
119
Erik Verbruggenc6c8d932011-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 Naroff1eb1ad62007-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 Stump11289f42009-09-09 15:08:12 +0000143/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000144/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000145/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000146/// objc-interface-decl-list
147/// @end
148///
149/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000150/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-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 Verbruggenc6c8d932011-12-06 09:25:23 +0000165Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000166 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000167 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000168 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000169 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000170 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000171
Douglas Gregor49c22a72009-11-18 16:26:39 +0000172 // Code completion after '@interface'.
173 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000174 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000175 cutOffParsing();
176 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000177 }
178
Chris Lattner0ef13522007-10-09 17:51:17 +0000179 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000180 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +0000181 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000182 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000183
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000184 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000185 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000186 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000187 if (Tok.is(tok::l_paren) &&
188 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000189
190 BalancedDelimiterTracker T(*this, tok::l_paren);
191 T.consumeOpen();
192
193 SourceLocation categoryLoc;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000194 IdentifierInfo *categoryId = 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000195 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000196 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000197 cutOffParsing();
198 return 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000199 }
200
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000201 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000202 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000203 categoryId = Tok.getIdentifierInfo();
204 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000205 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000206 else if (!getLang().ObjC2) {
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000207 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +0000208 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000209 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000210
211 T.consumeClose();
212 if (T.getCloseLocation().isInvalid())
John McCall48871652010-08-21 09:40:31 +0000213 return 0;
Douglas Gregor0c254a02011-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 Naroff1eb1ad62007-08-20 21:31:48 +0000218 }
Douglas Gregor0c254a02011-09-23 19:19:41 +0000219
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000220 // Next, we need to check for any protocol references.
221 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000222 SmallVector<Decl *, 8> ProtocolRefs;
223 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000224 if (Tok.is(tok::less) &&
225 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000226 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000227 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000228
John McCall48871652010-08-21 09:40:31 +0000229 Decl *CategoryType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000230 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000231 nameId, nameLoc,
232 categoryId, categoryLoc,
233 ProtocolRefs.data(),
234 ProtocolRefs.size(),
235 ProtocolLocs.data(),
236 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000237
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000238 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000239 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000240
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000241 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000242 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000243 }
244 // Parse a class interface.
245 IdentifierInfo *superClassId = 0;
246 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000247
Chris Lattner0ef13522007-10-09 17:51:17 +0000248 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000249 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000250
251 // Code completion of superclass names.
252 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000253 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000254 cutOffParsing();
255 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000256 }
257
Chris Lattner0ef13522007-10-09 17:51:17 +0000258 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000259 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +0000260 return 0;
Steve Naroff1eb1ad62007-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 Lattner0e62c1c2011-07-23 10:55:15 +0000266 SmallVector<Decl *, 8> ProtocolRefs;
267 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000268 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000269 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000270 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
271 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000272 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000273
John McCall48871652010-08-21 09:40:31 +0000274 Decl *ClsType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000275 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000276 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000277 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000278 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000279 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000280
Chris Lattner0ef13522007-10-09 17:51:17 +0000281 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000282 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000283
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000284 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000285 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000286}
287
John McCall064d77b2009-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 Blaikie68e081d2011-12-20 02:48:34 +0000291private:
292 virtual void anchor();
293public:
John McCall064d77b2009-12-03 22:31:13 +0000294 Parser &P;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000295 SmallVectorImpl<Decl *> &Props;
John McCall064d77b2009-12-03 22:31:13 +0000296 ObjCDeclSpec &OCDS;
297 SourceLocation AtLoc;
298 tok::ObjCKeywordKind MethodImplKind;
299
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000300 ObjCPropertyCallback(Parser &P,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000301 SmallVectorImpl<Decl *> &Props,
John McCall064d77b2009-12-03 22:31:13 +0000302 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
303 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000304 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
John McCall064d77b2009-12-03 22:31:13 +0000305 MethodImplKind(MethodImplKind) {
306 }
307
John McCall48871652010-08-21 09:40:31 +0000308 Decl *invoke(FieldDeclarator &FD) {
John McCall064d77b2009-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 McCall48871652010-08-21 09:40:31 +0000312 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000313 }
314 if (FD.BitfieldSize) {
315 P.Diag(AtLoc, diag::err_objc_property_bitfield)
316 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000317 return 0;
John McCall064d77b2009-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 McCall48871652010-08-21 09:40:31 +0000335 Decl *Property =
Douglas Gregor0be31a22010-07-02 17:43:08 +0000336 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000337 GetterSel, SetterSel,
John McCall064d77b2009-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 Blaikie68e081d2011-12-20 02:48:34 +0000347void Parser::ObjCPropertyCallback::anchor() {
348}
349
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000350/// objc-interface-decl-list:
351/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000352/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000353/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000354/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000355/// objc-interface-decl-list declaration
356/// objc-interface-decl-list ';'
357///
Steve Naroff99264b42007-08-22 16:35:03 +0000358/// objc-method-requirement: [OBJC2]
359/// @required
360/// @optional
361///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000362void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
363 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000364 SmallVector<Decl *, 32> allMethods;
365 SmallVector<Decl *, 16> allProperties;
366 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000367 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000368
Ted Kremenekc7c64312010-01-07 01:20:12 +0000369 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000370
Steve Naroff99264b42007-08-22 16:35:03 +0000371 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000372 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000373 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCall48871652010-08-21 09:40:31 +0000374 Decl *methodPrototype =
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000375 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000376 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000377 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
378 // method definitions.
Argyrios Kyrtzidise1ee6232011-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 Naroff99264b42007-08-22 16:35:03 +0000385 continue;
386 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000387 if (Tok.is(tok::l_paren)) {
388 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000389 ParseObjCMethodDecl(Tok.getLocation(),
390 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000391 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000392 continue;
393 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000394 // Ignore excess semicolons.
395 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000396 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000397 continue;
398 }
Mike Stump11289f42009-09-09 15:08:12 +0000399
Chris Lattnerda9fb152008-10-20 06:10:06 +0000400 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000401 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000402 break;
Mike Stump11289f42009-09-09 15:08:12 +0000403
Douglas Gregorf1934162010-01-13 21:24:21 +0000404 // Code completion within an Objective-C interface.
405 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000406 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000407 ObjCImpDecl? Sema::PCC_ObjCImplementation
408 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000409 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000410 }
411
Chris Lattner038a3e32008-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 Lattnerc7c9ab72009-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 McCall084e83d2011-03-24 11:26:52 +0000419 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000420 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000421 continue;
422 }
Mike Stump11289f42009-09-09 15:08:12 +0000423
Chris Lattner038a3e32008-10-20 05:46:22 +0000424 // Otherwise, we have an @ directive, eat the @.
425 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000426 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000427 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000428 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000429 break;
430 }
431
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000432 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000433
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000434 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000435 AtEnd.setBegin(AtLoc);
436 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000437 break;
Douglas Gregor00a0cf72010-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 Lattnerda9fb152008-10-20 06:10:06 +0000442 }
Mike Stump11289f42009-09-09 15:08:12 +0000443
Chris Lattnerda9fb152008-10-20 06:10:06 +0000444 // Eat the identifier.
445 ConsumeToken();
446
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000447 switch (DirectiveKind) {
448 default:
Chris Lattnerda9fb152008-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 Lattner76619232008-10-20 07:22:18 +0000453 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000454 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000455 SkipUntil(tok::r_brace, tok::at);
456 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000457
458 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000459 case tok::objc_interface:
Erik Verbruggenc6c8d932011-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 Jahaniand4c53482010-11-02 00:44:43 +0000464 ConsumeToken();
465 break;
466
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000467 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000468 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000469 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000470 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000471 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000472 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000473 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000474 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000475 break;
Mike Stump11289f42009-09-09 15:08:12 +0000476
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000477 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000478 if (!getLang().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000479 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000480
Chris Lattner038a3e32008-10-20 05:46:22 +0000481 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000482 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000483 if (Tok.is(tok::l_paren))
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000484 ParseObjCPropertyAttribute(OCDS);
Mike Stump11289f42009-09-09 15:08:12 +0000485
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000486 ObjCPropertyCallback Callback(*this, allProperties,
John McCall064d77b2009-12-03 22:31:13 +0000487 OCDS, AtLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000488
Chris Lattner038a3e32008-10-20 05:46:22 +0000489 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +0000490 DeclSpec DS(AttrFactory);
John McCallcfefb6d2009-11-03 02:38:08 +0000491 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000492
John McCall405988b2011-03-26 01:53:26 +0000493 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000494 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000495 }
Steve Naroff99264b42007-08-22 16:35:03 +0000496 }
Chris Lattnerda9fb152008-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 Gregorf48706c2009-12-07 09:27:33 +0000500 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000501 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000502 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000503 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000504 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-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 Stump11289f42009-09-09 15:08:12 +0000513
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000514 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000515 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000516 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump11289f42009-09-09 15:08:12 +0000517 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000518 allProperties.data(), allProperties.size(),
519 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000520}
521
Fariborz Jahanian9fca6df2007-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 McCall31168b02011-06-15 23:02:42 +0000537/// atomic
538/// strong
539/// weak
540/// unsafe_unretained
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000541///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000542void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000543 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000544 BalancedDelimiterTracker T(*this, tok::l_paren);
545 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000546
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000547 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000548 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000549 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000550 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000551 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000552 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000553
Chris Lattner76619232008-10-20 07:22:18 +0000554 // If this is not an identifier at all, bail out early.
555 if (II == 0) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000556 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000557 return;
558 }
Mike Stump11289f42009-09-09 15:08:12 +0000559
Chris Lattner1db33542008-10-20 07:37:22 +0000560 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000561
Chris Lattner68e48682008-11-20 04:42:34 +0000562 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000563 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000564 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000565 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000566 else if (II->isStr("unsafe_unretained"))
567 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000568 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000569 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000570 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000571 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000572 else if (II->isStr("strong"))
573 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000574 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000575 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000576 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000577 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000578 else if (II->isStr("atomic"))
579 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000580 else if (II->isStr("weak"))
581 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000582 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000583 bool IsSetter = II->getNameStart()[0] == 's';
584
Chris Lattner825bca12008-10-20 07:39:53 +0000585 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-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 Lattner43c76c32008-10-20 07:00:43 +0000590 return;
Mike Stump11289f42009-09-09 15:08:12 +0000591
Douglas Gregorc8537c52009-11-19 07:41:15 +0000592 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000593 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000594 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000595 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000596 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000597 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000598 }
599
Anders Carlssonfe15a782010-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 Lattnerbeca7702008-10-20 07:24:39 +0000607 SkipUntil(tok::r_paren);
608 return;
609 }
Mike Stump11289f42009-09-09 15:08:12 +0000610
Anders Carlssonfe15a782010-10-02 17:45:21 +0000611 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000612 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000613 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000614
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000615 if (ExpectAndConsume(tok::colon,
616 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000617 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000618 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000619 } else {
620 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000621 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000622 }
Chris Lattner825bca12008-10-20 07:39:53 +0000623 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000624 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000625 SkipUntil(tok::r_paren);
626 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000627 }
Mike Stump11289f42009-09-09 15:08:12 +0000628
Chris Lattner1db33542008-10-20 07:37:22 +0000629 if (Tok.isNot(tok::comma))
630 break;
Mike Stump11289f42009-09-09 15:08:12 +0000631
Chris Lattner1db33542008-10-20 07:37:22 +0000632 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000633 }
Mike Stump11289f42009-09-09 15:08:12 +0000634
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000635 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000636}
637
Steve Naroff09bf8152007-09-06 21:24:23 +0000638/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000639/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000640/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000641///
642/// objc-instance-method: '-'
643/// objc-class-method: '+'
644///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000645/// objc-method-attributes: [OBJC2]
646/// __attribute__((deprecated))
647///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000648Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000649 bool MethodDefinition) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000650 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000651
Mike Stump11289f42009-09-09 15:08:12 +0000652 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000653 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000654 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000655 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000656 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000657 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000658 return MDecl;
Steve Naroff99264b42007-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 Lattner4f472a32009-04-11 18:13:45 +0000669IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000670
Chris Lattner5700fab2007-10-07 02:00:24 +0000671 switch (Tok.getKind()) {
672 default:
673 return 0;
Fariborz Jahanian9e42a952010-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 Jahaniandadfc1c2010-09-03 18:01:09 +0000685 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanian9e42a952010-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 Lattner5700fab2007-10-07 02:00:24 +0000695 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000696 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000697 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000698 case tok::kw_bool:
Anders Carlssonf93f56a2008-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 Lattner5700fab2007-10-07 02:00:24 +0000761 case tok::kw__Bool:
762 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000763 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000764 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000765 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000766 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000767 }
Steve Naroff99264b42007-08-22 16:35:03 +0000768}
769
Fariborz Jahanian83615522008-01-02 22:54:34 +0000770/// objc-for-collection-in: 'in'
771///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000772bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-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 Stump11289f42009-09-09 15:08:12 +0000776 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000777 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000778}
779
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000780/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000781/// qualifier list and builds their bitmask representation in the input
782/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000783///
784/// objc-type-qualifiers:
785/// objc-type-qualifier
786/// objc-type-qualifiers objc-type-qualifier
787///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000788void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000789 Declarator::TheContext Context) {
790 assert(Context == Declarator::ObjCParameterContext ||
791 Context == Declarator::ObjCResultContext);
792
Chris Lattner61511e12007-12-12 06:56:32 +0000793 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000794 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000795 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +0000796 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000797 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +0000798 }
799
Chris Lattner5e530bc2007-12-27 19:57:00 +0000800 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000801 return;
Mike Stump11289f42009-09-09 15:08:12 +0000802
Chris Lattner61511e12007-12-12 06:56:32 +0000803 const IdentifierInfo *II = Tok.getIdentifierInfo();
804 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000805 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000806 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000807
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000808 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000809 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +0000810 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-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 Lattner61511e12007-12-12 06:56:32 +0000817 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000818 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000819 ConsumeToken();
820 II = 0;
821 break;
822 }
Mike Stump11289f42009-09-09 15:08:12 +0000823
Chris Lattner61511e12007-12-12 06:56:32 +0000824 // If this wasn't a recognized qualifier, bail out.
825 if (II) return;
826 }
827}
828
John McCalla55902b2011-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 Lattner61511e12007-12-12 06:56:32 +0000863/// objc-type-name:
864/// '(' objc-type-qualifiers[opt] type-name ')'
865/// '(' objc-type-qualifiers[opt] ')'
866///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000867ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-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 Lattner0ef13522007-10-09 17:51:17 +0000874 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000875
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000876 BalancedDelimiterTracker T(*this, tok::l_paren);
877 T.consumeOpen();
878
Chris Lattner2ebb1782008-08-23 01:48:03 +0000879 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000880 ObjCDeclContextSwitch ObjCDC(*this);
881
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000882 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +0000883 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000884
John McCallba7bf592010-08-24 05:47:05 +0000885 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000886 if (isTypeSpecifierQualifier()) {
John McCalla55902b2011-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 Gregorbab8a962011-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 Gregor220cac52009-02-18 17:45:20 +0000914 }
Douglas Gregorbab8a962011-09-08 01:46:34 +0000915
Steve Naroff90255b42008-10-21 14:15:04 +0000916 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000917 T.consumeClose();
Chris Lattnerb7954432008-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 Lattner6d29c102008-11-18 07:48:38 +0000920 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-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 Gregore7a8e3b2011-10-12 16:37:45 +0000925 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000926 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000927 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000928}
929
930/// objc-method-decl:
931/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000932/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000933/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000934/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000935///
936/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000937/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000938/// objc-keyword-selector objc-keyword-decl
939///
940/// objc-keyword-decl:
Steve Naroff0b6a01a2007-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 Naroff99264b42007-08-22 16:35:03 +0000945///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000946/// objc-parmlist:
947/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000948///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000949/// objc-parms:
950/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000951///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000952/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000953/// , ...
954///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000955/// objc-keyword-attributes: [OBJC2]
956/// __attribute__((unused))
957///
John McCall48871652010-08-21 09:40:31 +0000958Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000959 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000960 tok::ObjCKeywordKind MethodImplKind,
961 bool MethodDefinition) {
John McCall28a6aea2009-11-04 02:18:39 +0000962 ParsingDeclRAIIObject PD(*this);
963
Douglas Gregor636a61e2010-04-07 00:21:17 +0000964 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000965 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000966 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000967 cutOffParsing();
968 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000969 }
970
Chris Lattner2ebb1782008-08-23 01:48:03 +0000971 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +0000972 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000973 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000974 if (Tok.is(tok::l_paren))
John McCalla55902b2011-10-01 09:56:14 +0000975 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump11289f42009-09-09 15:08:12 +0000976
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000977 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +0000978 ParsedAttributes methodAttrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000979 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +0000980 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000981
Douglas Gregor636a61e2010-04-07 00:21:17 +0000982 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000983 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000984 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000985 cutOffParsing();
986 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000987 }
988
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000989 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +0000990 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000991 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000992
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000993 // An unnamed colon is valid.
994 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000995 Diag(Tok, diag::err_expected_selector_for_method)
996 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000997 // Skip until we get a ; or {}.
998 SkipUntil(tok::r_brace);
John McCall48871652010-08-21 09:40:31 +0000999 return 0;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001000 }
Mike Stump11289f42009-09-09 15:08:12 +00001001
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001002 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001003 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001004 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001005 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001006 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001007
Chris Lattner5700fab2007-10-07 02:00:24 +00001008 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001009 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001010 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001011 mType, DSRet, ReturnType,
Douglas Gregor33823722011-06-11 01:09:30 +00001012 selLoc, Sel, 0,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001013 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001014 methodAttrs.getList(), MethodImplKind,
1015 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001016 PD.complete(Result);
1017 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001018 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001019
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001020 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001021 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001022 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001023 ParseScope PrototypeScope(this,
1024 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001025
1026 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001027
Chris Lattner5700fab2007-10-07 02:00:24 +00001028 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001029 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001030 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001031
Chris Lattner5700fab2007-10-07 02:00:24 +00001032 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +00001033 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001034 Diag(Tok, diag::err_expected_colon);
1035 break;
1036 }
1037 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001038
John McCallba7bf592010-08-24 05:47:05 +00001039 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001040 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001041 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1042 Declarator::ObjCParameterContext,
1043 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001044
Chris Lattner5700fab2007-10-07 02:00:24 +00001045 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001046 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001047 ArgInfo.ArgAttrs = 0;
John McCall53fa7142010-12-24 02:08:15 +00001048 if (getLang().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001049 MaybeParseGNUAttributes(paramAttrs);
1050 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001051 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001052
Douglas Gregor45879692010-07-08 23:37:41 +00001053 // Code completion for the next piece of the selector.
1054 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-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 Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001062 cutOffParsing();
1063 return 0;
Douglas Gregor45879692010-07-08 23:37:41 +00001064 }
1065
Chris Lattner0ef13522007-10-09 17:51:17 +00001066 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001067 Diag(Tok, diag::err_expected_ident); // missing argument name.
1068 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001069 }
Mike Stump11289f42009-09-09 15:08:12 +00001070
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001071 ArgInfo.Name = Tok.getIdentifierInfo();
1072 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001073 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001074
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001075 ArgInfos.push_back(ArgInfo);
1076 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001077 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001078
John McCall084e83d2011-03-24 11:26:52 +00001079 // Make sure the attributes persist.
1080 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1081
Douglas Gregor95887f92010-07-08 23:20:03 +00001082 // Code completion for the next piece of the selector.
1083 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001084 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1085 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001086 /*AtParameterName=*/false,
Douglas Gregor95887f92010-07-08 23:20:03 +00001087 ReturnType,
1088 KeyIdents.data(),
1089 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001090 cutOffParsing();
1091 return 0;
Douglas Gregor95887f92010-07-08 23:20:03 +00001092 }
1093
Chris Lattner5700fab2007-10-07 02:00:24 +00001094 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001095 SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001096 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001097 break;
1098 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001099 }
Mike Stump11289f42009-09-09 15:08:12 +00001100
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001101 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +00001102
Chris Lattner5700fab2007-10-07 02:00:24 +00001103 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001104 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001105 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001106 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001107 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001108 ConsumeToken();
1109 break;
1110 }
John McCall084e83d2011-03-24 11:26:52 +00001111 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001112 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001113 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001114 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1115 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001116 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001117 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001118 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1119 ParmDecl.getIdentifierLoc(),
1120 Param,
1121 0));
1122
Chris Lattner5700fab2007-10-07 02:00:24 +00001123 }
Mike Stump11289f42009-09-09 15:08:12 +00001124
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001125 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001126 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001127 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001128 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001129
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001130 if (KeyIdents.size() == 0)
John McCall48871652010-08-21 09:40:31 +00001131 return 0;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001132
Chris Lattner5700fab2007-10-07 02:00:24 +00001133 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1134 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001135 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001136 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001137 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001138 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001139 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001140 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001141 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001142
John McCall28a6aea2009-11-04 02:18:39 +00001143 PD.complete(Result);
1144 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001145}
1146
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001147/// objc-protocol-refs:
1148/// '<' identifier-list '>'
1149///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001150bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001151ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1152 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001153 bool WarnOnDeclarations,
1154 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001155 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001156
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001157 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001158
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001159 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001160
Chris Lattner3bbae002008-07-26 04:03:38 +00001161 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001162 if (Tok.is(tok::code_completion)) {
1163 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1164 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001165 cutOffParsing();
1166 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001167 }
1168
Chris Lattner3bbae002008-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 Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001176 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001177 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001178
Chris Lattner3bbae002008-07-26 04:03:38 +00001179 if (Tok.isNot(tok::comma))
1180 break;
1181 ConsumeToken();
1182 }
Mike Stump11289f42009-09-09 15:08:12 +00001183
Chris Lattner3bbae002008-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 Stump11289f42009-09-09 15:08:12 +00001189
Chris Lattner3bbae002008-07-26 04:03:38 +00001190 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +00001191
Chris Lattner3bbae002008-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 Gregor06e41ae2010-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 Gregor3a001f42010-11-19 17:10:50 +00001201bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-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 Lattner0e62c1c2011-07-23 10:55:15 +00001205 SmallVector<Decl *, 8> ProtocolDecl;
1206 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001207 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1208 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-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 Gregor3a001f42010-11-19 17:10:50 +00001213 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001214}
1215
1216
Steve Naroff1eb1ad62007-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 Naroff00433d32007-08-21 21:17:12 +00001232/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001233///
1234/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001235/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001236///
John McCall48871652010-08-21 09:40:31 +00001237void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001238 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001239 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001240 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001241 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001242
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001243 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001244 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001245
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001246 BalancedDelimiterTracker T(*this, tok::l_brace);
1247 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00001248
Steve Naroff00433d32007-08-21 21:17:12 +00001249 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +00001250 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001251 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001252
Steve Naroff00433d32007-08-21 21:17:12 +00001253 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001254 if (Tok.is(tok::semi)) {
Douglas Gregor13d05682010-06-16 23:08:59 +00001255 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregora771f462010-03-31 17:46:05 +00001256 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +00001257 ConsumeToken();
1258 continue;
1259 }
Mike Stump11289f42009-09-09 15:08:12 +00001260
Steve Naroff00433d32007-08-21 21:17:12 +00001261 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001262 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001263 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001264
1265 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001266 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001267 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001268 }
1269
Steve Naroff7c348172007-08-23 18:16:40 +00001270 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-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 Naroff7c348172007-08-23 18:16:40 +00001275 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001276 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001277 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001278 default:
1279 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +00001280 continue;
1281 }
1282 }
Mike Stump11289f42009-09-09 15:08:12 +00001283
Douglas Gregor48d46252010-01-13 21:54:15 +00001284 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001285 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001286 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001287 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001288 }
1289
John McCallcfefb6d2009-11-03 02:38:08 +00001290 struct ObjCIvarCallback : FieldCallback {
1291 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001292 Decl *IDecl;
John McCallcfefb6d2009-11-03 02:38:08 +00001293 tok::ObjCKeywordKind visibility;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001294 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001295
John McCall48871652010-08-21 09:40:31 +00001296 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001297 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001298 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1299 }
1300
John McCall48871652010-08-21 09:40:31 +00001301 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001302 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallcfefb6d2009-11-03 02:38:08 +00001303 // Install the declarator into the interface decl.
John McCall48871652010-08-21 09:40:31 +00001304 Decl *Field
Douglas Gregor0be31a22010-07-02 17:43:08 +00001305 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallcfefb6d2009-11-03 02:38:08 +00001306 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001307 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian4327b322011-08-29 17:33:12 +00001308 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001309 if (Field)
1310 AllIvarDecls.push_back(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001311 return Field;
1312 }
1313 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001314
Chris Lattnera12405b2008-04-10 06:46:29 +00001315 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00001316 DeclSpec DS(AttrFactory);
John McCallcfefb6d2009-11-03 02:38:08 +00001317 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001318
Chris Lattner0ef13522007-10-09 17:51:17 +00001319 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001320 ConsumeToken();
Steve Naroff00433d32007-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 Gregore7a8e3b2011-10-12 16:37:45 +00001327 T.consumeClose();
1328
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001329 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001330 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
Fariborz Jahanian4327b322011-08-29 17:33:12 +00001331 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff9c4fddd2007-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 Gregor0be31a22010-07-02 17:43:08 +00001334 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie751c5582011-09-22 02:58:26 +00001335 AllIvarDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001336 T.getOpenLocation(), T.getCloseLocation(), 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001337 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001338}
Steve Naroff1eb1ad62007-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 Stump11289f42009-09-09 15:08:12 +00001345/// @protocol identifier
1346/// objc-protocol-refs[opt]
1347/// objc-interface-decl-list
Steve Naroff1eb1ad62007-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 Naroff09bf8152007-09-06 21:24:23 +00001354/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001355/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001356Parser::DeclGroupPtrTy
1357Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1358 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001359 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001360 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1361 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001362
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001363 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001364 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001365 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00001366 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001367 }
1368
Chris Lattner0ef13522007-10-09 17:51:17 +00001369 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001370 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00001371 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001372 }
1373 // Save the protocol name, then consume it.
1374 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1375 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001376
Chris Lattner0ef13522007-10-09 17:51:17 +00001377 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001378 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001379 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001380 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001381 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001382 }
Mike Stump11289f42009-09-09 15:08:12 +00001383
Erik Verbruggenf9887852011-12-08 09:58:43 +00001384 CheckNestedObjCContexts(AtLoc);
1385
Chris Lattner0ef13522007-10-09 17:51:17 +00001386 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001387 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001388 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1389
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001390 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001391 while (1) {
1392 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001393 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001394 Diag(Tok, diag::err_expected_ident);
1395 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00001396 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001397 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001398 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1399 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001400 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001401
Chris Lattner0ef13522007-10-09 17:51:17 +00001402 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001403 break;
1404 }
1405 // Consume the ';'.
1406 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00001407 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001408
Steve Naroff93eb5f12007-10-10 17:32:04 +00001409 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001410 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001411 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001412 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001413 }
Mike Stump11289f42009-09-09 15:08:12 +00001414
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001415 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001416 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001417
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001418 SmallVector<Decl *, 8> ProtocolRefs;
1419 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001420 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001421 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1422 LAngleLoc, EndProtoLoc))
Douglas Gregorf6102672012-01-01 21:23:57 +00001423 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001424
John McCall48871652010-08-21 09:40:31 +00001425 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001426 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001427 ProtocolRefs.data(),
1428 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001429 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001430 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001431
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001432 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00001433 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001434}
Steve Naroff1eb1ad62007-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 Verbruggenc6c8d932011-12-06 09:25:23 +00001446Decl *Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001447 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1448 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001449 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001450 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001451
Douglas Gregor49c22a72009-11-18 16:26:39 +00001452 // Code completion after '@implementation'.
1453 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001454 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001455 cutOffParsing();
1456 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +00001457 }
1458
Chris Lattner0ef13522007-10-09 17:51:17 +00001459 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001460 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +00001461 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001462 }
1463 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001464 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001465 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001466
1467 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001468 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001469 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001470 SourceLocation categoryLoc, rparenLoc;
1471 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001472
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001473 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001474 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001475 cutOffParsing();
1476 return 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001477 }
1478
Chris Lattner0ef13522007-10-09 17:51:17 +00001479 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-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 McCall48871652010-08-21 09:40:31 +00001484 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001485 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001486 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001487 Diag(Tok, diag::err_expected_rparen);
1488 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCall48871652010-08-21 09:40:31 +00001489 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001490 }
1491 rparenLoc = ConsumeParen();
John McCall48871652010-08-21 09:40:31 +00001492 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001493 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001494 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001495
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001496 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001497 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001498 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001499 }
1500 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001501 SourceLocation superClassLoc;
1502 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001503 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001504 // We have a super class
1505 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001506 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001507 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +00001508 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001509 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001510 superClassId = Tok.getIdentifierInfo();
1511 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001512 }
John McCall48871652010-08-21 09:40:31 +00001513 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001514 AtLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001515 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001516
Steve Naroff33a1e802007-10-29 21:38:07 +00001517 if (Tok.is(tok::l_brace)) // we have ivars
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001518 ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001519
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001520 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001521 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001522 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001523}
Steve Naroff33a1e802007-10-29 21:38:07 +00001524
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001525Parser::DeclGroupPtrTy
1526Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001527 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1528 "ParseObjCAtEndDeclaration(): Expected @end");
1529 ConsumeToken(); // the "end" identifier
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001530 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001531 Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001532 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1533 Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
Argyrios Kyrtzidis516eec22011-11-16 02:35:10 +00001534 if (D)
1535 DeclsInGroup.push_back(D);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001536 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001537 DeclsInGroup.push_back(ObjCImpDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001538
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001539 if (ObjCImpDecl) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001540 Actions.ActOnAtEnd(getCurScope(), atEnd);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001541 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001542 }
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001543 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00001544 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001545 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001546
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001547 clearLateParsedObjCMethods();
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001548 ObjCImpDecl = 0;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001549 return Actions.BuildDeclaratorGroup(
1550 DeclsInGroup.data(), DeclsInGroup.size(), false);
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001551}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001552
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001553Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1554 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001555 if (PendingObjCImpDecl.empty())
John McCall48871652010-08-21 09:40:31 +00001556 return Actions.ConvertDeclToDeclGroup(0);
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001557
John McCall48871652010-08-21 09:40:31 +00001558 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Erik Verbruggenc6c8d932011-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 Jahanian9290ede2009-11-16 18:57:01 +00001566 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1567}
1568
Argyrios Kyrtzidis004685b2011-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 Jahaniand8e12d32007-09-04 19:26:51 +00001577/// compatibility-alias-decl:
1578/// @compatibility_alias alias-name class-name ';'
1579///
John McCall48871652010-08-21 09:40:31 +00001580Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001581 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1582 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1583 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001584 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001585 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001586 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001587 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001588 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1589 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001590 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001591 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001592 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001593 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001594 IdentifierInfo *classId = Tok.getIdentifierInfo();
1595 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregor78edf992011-01-05 01:10:06 +00001596 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1597 "@compatibility_alias");
Chris Lattner83f095c2009-03-28 19:18:32 +00001598 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1599 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001600}
1601
Fariborz Jahanian53cacae2007-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 McCall48871652010-08-21 09:40:31 +00001613Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001614 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1615 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001616 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001617
Douglas Gregor88e72a02009-11-18 19:45:45 +00001618 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001619 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001620 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001621 cutOffParsing();
1622 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001623 }
1624
Douglas Gregor88e72a02009-11-18 19:45:45 +00001625 if (Tok.isNot(tok::identifier)) {
1626 Diag(Tok, diag::err_synthesized_property_name);
1627 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001628 return 0;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001629 }
1630
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001631 IdentifierInfo *propertyIvar = 0;
1632 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1633 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001634 SourceLocation propertyIvarLoc;
Chris Lattner0ef13522007-10-09 17:51:17 +00001635 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001636 // property '=' ivar-name
1637 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001638
1639 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001640 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001641 cutOffParsing();
1642 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001643 }
1644
Chris Lattner0ef13522007-10-09 17:51:17 +00001645 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001646 Diag(Tok, diag::err_expected_ident);
1647 break;
1648 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001649 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001650 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001651 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001652 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001653 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001654 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001655 break;
1656 ConsumeToken(); // consume ','
1657 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001658 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCall48871652010-08-21 09:40:31 +00001659 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001660}
1661
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001662/// property-dynamic:
1663/// @dynamic property-list
1664///
1665/// property-list:
1666/// identifier
1667/// property-list ',' identifier
1668///
John McCall48871652010-08-21 09:40:31 +00001669Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001670 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1671 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001672 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001673 while (true) {
1674 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001675 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001676 cutOffParsing();
1677 return 0;
Douglas Gregor52e78bd2009-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 McCall48871652010-08-21 09:40:31 +00001683 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001684 }
1685
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001686 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1687 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001688 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001689 propertyId, 0, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001690
Chris Lattner0ef13522007-10-09 17:51:17 +00001691 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001692 break;
1693 ConsumeToken(); // consume ','
1694 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001695 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCall48871652010-08-21 09:40:31 +00001696 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001697}
Mike Stump11289f42009-09-09 15:08:12 +00001698
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001699/// objc-throw-statement:
1700/// throw expression[opt];
1701///
John McCalldadc5752010-08-24 06:29:42 +00001702StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1703 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001704 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001705 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001706 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001707 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001708 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001709 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001710 }
1711 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001712 // consume ';'
1713 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCallb268a282010-08-23 23:25:46 +00001714 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001715}
1716
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001717/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001718/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001719///
John McCalldadc5752010-08-24 06:29:42 +00001720StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001721Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001722 ConsumeToken(); // consume synchronized
1723 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001724 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001725 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001726 }
John McCalld9bb7432011-07-27 21:50:02 +00001727
1728 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001729 ConsumeParen(); // '('
John McCalld9bb7432011-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 Jahanian48085b82008-01-29 19:14:59 +00001740 }
John McCalld9bb7432011-07-27 21:50:02 +00001741
1742 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001743 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00001744 if (!operand.isInvalid())
1745 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001746 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001747 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001748
John McCalld9bb7432011-07-27 21:50:02 +00001749 // Check the @synchronized operand now.
1750 if (!operand.isInvalid())
1751 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001752
John McCalld9bb7432011-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 Jahanianf89ca382008-01-29 18:21:32 +00001767}
1768
Fariborz Jahanian62fd2b42007-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 McCalldadc5752010-08-24 06:29:42 +00001780StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001781 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001782
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001783 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001784 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001785 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001786 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001787 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001788 StmtVector CatchStmts(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001789 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001790 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001791 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001792 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001793 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001794 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001795
Chris Lattner0ef13522007-10-09 17:51:17 +00001796 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-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 Redl17f2c7d2008-12-09 13:15:23 +00001804
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001805 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001806 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCall48871652010-08-21 09:40:31 +00001807 Decl *FirstPart = 0;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001808 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001809 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001810 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001811 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001812 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001813 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001814 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00001815 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00001816 ParseDeclarator(ParmDecl);
1817
Douglas Gregore11ee112010-04-23 23:01:43 +00001818 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001819 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001820 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001821 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001822 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001823
Steve Naroff65a00892009-04-07 22:56:58 +00001824 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001825
Steve Naroff65a00892009-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 McCalldadc5752010-08-24 06:29:42 +00001831 StmtResult CatchBody(true);
Chris Lattner99a59b62008-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 Redl17f2c7d2008-12-09 13:15:23 +00001836 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001837 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001838
John McCalldadc5752010-08-24 06:29:42 +00001839 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001840 RParenLoc,
1841 FirstPart,
John McCallb268a282010-08-23 23:25:46 +00001842 CatchBody.take());
Douglas Gregor96c79492010-04-23 22:50:49 +00001843 if (!Catch.isInvalid())
1844 CatchStmts.push_back(Catch.release());
1845
Steve Naroffe6016792008-02-05 21:27:35 +00001846 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001847 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1848 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001849 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001850 }
1851 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001852 } else {
1853 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001854 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001855 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001856
John McCalldadc5752010-08-24 06:29:42 +00001857 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-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 Redl17f2c7d2008-12-09 13:15:23 +00001862 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001863 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001864 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCallb268a282010-08-23 23:25:46 +00001865 FinallyBody.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001866 catch_or_finally_seen = true;
1867 break;
1868 }
1869 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001870 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001871 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001872 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001873 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001874
John McCallb268a282010-08-23 23:25:46 +00001875 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor96c79492010-04-23 22:50:49 +00001876 move_arg(CatchStmts),
John McCallb268a282010-08-23 23:25:46 +00001877 FinallyStmt.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001878}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001879
John McCall31168b02011-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 Naroff09bf8152007-09-06 21:24:23 +00001903/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001904///
John McCall48871652010-08-21 09:40:31 +00001905Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001906 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00001907
John McCallfaf5fb42010-08-26 23:41:50 +00001908 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1909 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001910
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001911 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001912 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001913 if (ObjCImpDecl) {
1914 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00001915 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001916 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001917 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001918 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001919
Steve Naroffbb875722007-11-11 19:54:21 +00001920 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001921 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001922 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001923
Steve Naroffbb875722007-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 Stump11289f42009-09-09 15:08:12 +00001926
Steve Naroffbb875722007-11-11 19:54:21 +00001927 // If we didn't find the '{', bail out.
1928 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +00001929 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001930 }
Fariborz Jahanianbd0642f2011-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 Naroff7b8fa472007-11-13 23:01:27 +00001944 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001945}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001946
John McCalldadc5752010-08-24 06:29:42 +00001947StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001948 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001949 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001950 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001951 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001952 }
1953
1954 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001955 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001956
1957 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001958 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001959
1960 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001961 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00001962
1963 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1964 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001965
John McCalldadc5752010-08-24 06:29:42 +00001966 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001967 if (Res.isInvalid()) {
Steve Naroffe6016792008-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 Redlbab9a4b2008-12-11 20:12:42 +00001972 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001973 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001974
Steve Naroffe6016792008-02-05 21:27:35 +00001975 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00001976 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +00001977 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroffe6016792008-02-05 21:27:35 +00001978}
1979
John McCalldadc5752010-08-24 06:29:42 +00001980ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001981 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001982 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001983 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001984 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001985 return ExprError();
1986
Chris Lattnere002fbe2007-12-12 01:04:12 +00001987 case tok::string_literal: // primary-expression: string-literal
1988 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001989 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001990 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001991 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001992 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001993
Chris Lattner197a3012008-08-05 06:19:09 +00001994 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1995 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001996 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001997 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001998 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001999 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002000 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002001 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002002 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00002003 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002004 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002005}
2006
Douglas Gregor8d4de672010-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 Gregor8d4de672010-04-21 22:36:40 +00002029bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002030 InMessageExpressionRAIIObject InMessage(*this, true);
2031
Douglas Gregor8d4de672010-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 McCalldadc5752010-08-24 06:29:42 +00002039 ExprResult Receiver = ParseExpression();
Douglas Gregor8d4de672010-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 McCall084e83d2011-03-24 11:26:52 +00002052 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-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 McCalldadc5752010-08-24 06:29:42 +00002068 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002069 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002070 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002071 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002072 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor8d4de672010-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 Gregor0be31a22010-07-02 17:43:08 +00002085 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002086 if (Type.isInvalid())
2087 return true;
2088
2089 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002090 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002091 return false;
2092}
2093
Douglas Gregor990ccac2010-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 Lattner47054fb2010-05-31 18:18:22 +00002100 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002101 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002102 return GetLookAheadToken(1).is(tok::identifier) &&
2103 GetLookAheadToken(2).is(tok::identifier);
2104}
2105
Douglas Gregorabf4a3e2010-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 Stump11289f42009-09-09 15:08:12 +00002135/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002136/// '[' objc-receiver objc-message-args ']'
2137///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002138/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002139/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002140/// expression
2141/// class-name
2142/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002143///
John McCalldadc5752010-08-24 06:29:42 +00002144ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002145 assert(Tok.is(tok::l_square) && "'[' expected");
2146 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2147
Douglas Gregora817a192010-05-27 23:06:34 +00002148 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002149 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002150 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002151 return ExprError();
2152 }
2153
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002154 InMessageExpressionRAIIObject InMessage(*this, true);
2155
Douglas Gregor8d4de672010-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 Gregor0be31a22010-07-02 17:43:08 +00002164 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002165 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2166 ParsedType(), 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002167
2168 // Parse the receiver, which is either a type or an expression.
2169 bool IsExpr;
Nick Lewycky970aed92010-09-15 18:35:19 +00002170 void *TypeOrExpr = NULL;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002171 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2172 SkipUntil(tok::r_square);
2173 return ExprError();
2174 }
2175
2176 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002177 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2178 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002179 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002180
2181 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002182 ParsedType::getFromOpaquePtr(TypeOrExpr),
2183 0);
Chris Lattner47054fb2010-05-31 18:18:22 +00002184 }
2185
2186 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002187 IdentifierInfo *Name = Tok.getIdentifierInfo();
2188 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002189 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002190 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002191 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002192 NextToken().is(tok::period),
2193 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002194 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002195 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2196 ParsedType(), 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002197
John McCallfaf5fb42010-08-26 23:41:50 +00002198 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002199 if (!ReceiverType) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002200 SkipUntil(tok::r_square);
2201 return ExprError();
2202 }
2203
Douglas Gregore5798dc2010-04-21 20:38:13 +00002204 ConsumeToken(); // the type name
2205
2206 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb268a282010-08-23 23:25:46 +00002207 ReceiverType, 0);
Douglas Gregora148a1d2010-04-14 02:22:16 +00002208
John McCallfaf5fb42010-08-26 23:41:50 +00002209 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002210 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002211 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002212 }
Chris Lattner8f697062008-01-25 18:59:06 +00002213 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002214
2215 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCalldadc5752010-08-24 06:29:42 +00002216 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002217 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00002218 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002219 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00002220 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002221
John McCallba7bf592010-08-24 05:47:05 +00002222 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2223 ParsedType(), Res.take());
Chris Lattner8f697062008-01-25 18:59:06 +00002224}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002225
Douglas Gregor0c78ad92010-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 Stump11289f42009-09-09 15:08:12 +00002245///
Fariborz Jahanian7db004d2007-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 Stump11289f42009-09-09 15:08:12 +00002254/// objc-keywordarg:
Fariborz Jahanian7db004d2007-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 Redlcb6e2c62008-12-13 15:32:12 +00002263///
John McCalldadc5752010-08-24 06:29:42 +00002264ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002265Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002266 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002267 ParsedType ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002268 ExprArg ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002269 InMessageExpressionRAIIObject InMessage(*this, true);
2270
Steve Naroffeae65032009-11-07 02:08:14 +00002271 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002272 if (SuperLoc.isValid())
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002273 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2274 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002275 else if (ReceiverType)
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002276 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2277 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002278 else
John McCallb268a282010-08-23 23:25:46 +00002279 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002280 0, 0, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002281 cutOffParsing();
2282 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002283 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002284
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002285 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002286 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002287 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00002288
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002289 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002290 SmallVector<SourceLocation, 12> KeyLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00002291 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00002292
Chris Lattner0ef13522007-10-09 17:51:17 +00002293 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002294 while (1) {
2295 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002296 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002297 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00002298
Chris Lattner0ef13522007-10-09 17:51:17 +00002299 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002300 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-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 Redlcb6e2c62008-12-13 15:32:12 +00002305 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002306 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002307
Steve Narofff73590d2007-09-27 14:38:14 +00002308 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00002309 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-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 Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002328 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002329 return ExprError();
2330 }
2331
John McCalldadc5752010-08-24 06:29:42 +00002332 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002333 if (Res.isInvalid()) {
Chris Lattner197a3012008-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 Redlcb6e2c62008-12-13 15:32:12 +00002338 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00002339 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002340
Steve Naroff486760a2007-09-17 20:25:27 +00002341 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002342 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002343
Douglas Gregor1b605f72009-11-19 01:08:35 +00002344 // Code completion after each argument.
2345 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002346 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002347 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002348 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002349 KeyIdents.size(),
2350 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002351 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002352 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002353 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002354 KeyIdents.size(),
2355 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002356 else
John McCallb268a282010-08-23 23:25:46 +00002357 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002358 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002359 KeyIdents.size(),
2360 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002361 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002362 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002363 }
2364
Steve Naroff486760a2007-09-17 20:25:27 +00002365 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002366 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002367 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-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 Lattner0ef13522007-10-09 17:51:17 +00002372 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002373 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002374 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002375 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002376 if (Res.isInvalid()) {
Chris Lattner197a3012008-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 Redlcb6e2c62008-12-13 15:32:12 +00002381 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002382 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002383
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002384 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002385 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002386 }
2387 } else if (!selIdent) {
2388 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002389
Chris Lattner197a3012008-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 Redlcb6e2c62008-12-13 15:32:12 +00002394 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002395 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002396
Chris Lattner0ef13522007-10-09 17:51:17 +00002397 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian083712f2010-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 Lattner197a3012008-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 Redlcb6e2c62008-12-13 15:32:12 +00002406 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002407 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002408
Chris Lattner8f697062008-01-25 18:59:06 +00002409 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002410
Steve Naroffe61bfa82007-10-05 18:42:47 +00002411 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002412 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00002413 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002414 KeyLocs.push_back(Loc);
2415 }
Chris Lattner5700fab2007-10-07 02:00:24 +00002416 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002417
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002418 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002419 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002420 LBracLoc, KeyLocs, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002421 MultiExprArg(Actions,
2422 KeyExprs.take(),
2423 KeyExprs.size()));
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002424 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002425 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002426 LBracLoc, KeyLocs, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002427 MultiExprArg(Actions,
2428 KeyExprs.take(),
2429 KeyExprs.size()));
John McCallb268a282010-08-23 23:25:46 +00002430 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002431 LBracLoc, KeyLocs, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002432 MultiExprArg(Actions,
2433 KeyExprs.take(),
2434 KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002435}
2436
John McCalldadc5752010-08-24 06:29:42 +00002437ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2438 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002439 if (Res.isInvalid()) return move(Res);
2440
Chris Lattnere002fbe2007-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 Lattner0e62c1c2011-07-23 10:55:15 +00002444 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00002445 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00002446 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002447 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002448
Chris Lattnere002fbe2007-12-12 01:04:12 +00002449 while (Tok.is(tok::at)) {
2450 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002451
Sebastian Redlc13f2682008-12-09 20:22:58 +00002452 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002453 if (!isTokenStringLiteral())
2454 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002455
John McCalldadc5752010-08-24 06:29:42 +00002456 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002457 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002458 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002459
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002460 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002461 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002462
2463 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2464 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00002465}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002466
2467/// objc-encode-expression:
2468/// @encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002469ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002470Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002471 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002472
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002473 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002474
Chris Lattner197a3012008-08-05 06:19:09 +00002475 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002476 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2477
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002478 BalancedDelimiterTracker T(*this, tok::l_paren);
2479 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002480
Douglas Gregor220cac52009-02-18 17:45:20 +00002481 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002482
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002483 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002484
Douglas Gregor220cac52009-02-18 17:45:20 +00002485 if (Ty.isInvalid())
2486 return ExprError();
2487
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002488 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
2489 T.getOpenLocation(), Ty.get(),
2490 T.getCloseLocation()));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002491}
Anders Carlssone01493d2007-08-23 15:25:28 +00002492
2493/// objc-protocol-expression
2494/// @protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002495ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002496Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002497 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002498
Chris Lattner197a3012008-08-05 06:19:09 +00002499 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002500 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2501
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002502 BalancedDelimiterTracker T(*this, tok::l_paren);
2503 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002504
Chris Lattner197a3012008-08-05 06:19:09 +00002505 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002506 return ExprError(Diag(Tok, diag::err_expected_ident));
2507
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002508 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00002509 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002510
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002511 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00002512
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002513 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002514 T.getOpenLocation(),
2515 T.getCloseLocation()));
Anders Carlssone01493d2007-08-23 15:25:28 +00002516}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002517
2518/// objc-selector-expression
2519/// @selector '(' objc-keyword-selector ')'
John McCalldadc5752010-08-24 06:29:42 +00002520ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002521 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002522
Chris Lattner197a3012008-08-05 06:19:09 +00002523 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002524 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2525
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002526 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002527 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002528
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002529 BalancedDelimiterTracker T(*this, tok::l_paren);
2530 T.consumeOpen();
2531
Douglas Gregor67c692c2010-08-26 15:07:07 +00002532 if (Tok.is(tok::code_completion)) {
2533 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2534 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002535 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002536 return ExprError();
2537 }
2538
Chris Lattner4f472a32009-04-11 18:13:45 +00002539 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002540 if (!SelIdent && // missing selector name.
2541 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002542 return ExprError(Diag(Tok, diag::err_expected_ident));
2543
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002544 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002545 unsigned nColons = 0;
2546 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002547 while (1) {
Chris Lattner1ba64452010-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 Redlcb6e2c62008-12-13 15:32:12 +00002552 return ExprError(Diag(Tok, diag::err_expected_colon));
2553
Chris Lattner1ba64452010-08-27 22:32:41 +00002554 ++nColons;
Chris Lattner85222c62011-03-26 18:11:38 +00002555 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002556 if (Tok.is(tok::r_paren))
2557 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002558
2559 if (Tok.is(tok::code_completion)) {
2560 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2561 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002562 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002563 return ExprError();
2564 }
2565
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002566 // Check for another keyword selector.
2567 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002568 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002569 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00002570 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002571 break;
2572 }
Steve Naroff152dd812007-12-05 22:21:29 +00002573 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002574 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00002575 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002576 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002577 T.getOpenLocation(),
2578 T.getCloseLocation()));
Gabor Greif24032f12007-10-19 15:38:32 +00002579 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002580
2581Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002582
2583 // Save the current token position.
2584 SourceLocation OrigLoc = Tok.getLocation();
2585
Fariborz Jahanianbd0642f2011-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 Kyrtzidis9a174fb2011-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 Jahanianbd0642f2011-08-31 17:37:55 +00002639}