blob: 9aca5cd5399a0e284cdc3a40f853a18780c3ce82 [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);
John McCall53fa7142010-12-24 02:08:15 +000045 case tok::objc_interface: {
John McCall084e83d2011-03-24 11:26:52 +000046 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000047 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
48 break;
John McCall53fa7142010-12-24 02:08:15 +000049 }
50 case tok::objc_protocol: {
John McCall084e83d2011-03-24 11:26:52 +000051 ParsedAttributes attrs(AttrFactory);
Douglas Gregorf6102672012-01-01 21:23:57 +000052 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall53fa7142010-12-24 02:08:15 +000053 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000054 case tok::objc_implementation:
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +000055 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000056 case tok::objc_end:
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +000057 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000058 case tok::objc_compatibility_alias:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000059 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
60 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000061 case tok::objc_synthesize:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000062 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
63 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000064 case tok::objc_dynamic:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000065 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
66 break;
Ted Kremenekc1e4dd02012-03-01 22:07:04 +000067 case tok::objc___experimental_modules_import:
David Blaikiebbafb8a2012-03-11 07:00:24 +000068 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +000069 return ParseModuleImport(AtLoc);
70
71 // Fall through
72
Chris Lattnerce90ef52008-08-23 02:02:23 +000073 default:
74 Diag(AtLoc, diag::err_unexpected_at);
75 SkipUntil(tok::semi);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000076 SingleDecl = 0;
77 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000078 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000079 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000080}
81
82///
Mike Stump11289f42009-09-09 15:08:12 +000083/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000084/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000085///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000086Parser::DeclGroupPtrTy
87Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000088 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +000089 SmallVector<IdentifierInfo *, 8> ClassNames;
90 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremeneka26da852009-11-17 23:12:20 +000091
Mike Stump11289f42009-09-09 15:08:12 +000092
Chris Lattnerda59c2f2006-11-05 02:08:13 +000093 while (1) {
Chris Lattner0ef13522007-10-09 17:51:17 +000094 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +000095 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000096 SkipUntil(tok::semi);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000097 return Actions.ConvertDeclToDeclGroup(0);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000098 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +000099 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +0000100 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000101 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000102
Chris Lattner0ef13522007-10-09 17:51:17 +0000103 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000104 break;
Mike Stump11289f42009-09-09 15:08:12 +0000105
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000106 ConsumeToken();
107 }
Mike Stump11289f42009-09-09 15:08:12 +0000108
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000109 // Consume the ';'.
110 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000111 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump11289f42009-09-09 15:08:12 +0000112
Ted Kremeneka26da852009-11-17 23:12:20 +0000113 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
114 ClassLocs.data(),
115 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000116}
117
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000118void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
119{
120 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
121 if (ock == Sema::OCK_None)
122 return;
123
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000124 Decl *Decl = Actions.getObjCDeclContext();
125 if (CurParsedObjCImpl) {
126 CurParsedObjCImpl->finish(AtLoc);
127 } else {
128 Actions.ActOnAtEnd(getCurScope(), AtLoc);
129 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000130 Diag(AtLoc, diag::err_objc_missing_end)
131 << FixItHint::CreateInsertion(AtLoc, "@end\n");
132 if (Decl)
133 Diag(Decl->getLocStart(), diag::note_objc_container_start)
134 << (int) ock;
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000135}
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
Patrick Beardacfbe9e2012-04-06 18:12:22 +0000164/// __attribute__((objc_root_class))
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000165///
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000166Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000167 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000168 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000169 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000170 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000171 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000172
Douglas Gregor49c22a72009-11-18 16:26:39 +0000173 // Code completion after '@interface'.
174 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000175 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000176 cutOffParsing();
177 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000178 }
179
Chris Lattner0ef13522007-10-09 17:51:17 +0000180 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000181 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +0000182 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000183 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000184
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000185 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000186 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000187 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000188 if (Tok.is(tok::l_paren) &&
189 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000190
191 BalancedDelimiterTracker T(*this, tok::l_paren);
192 T.consumeOpen();
193
194 SourceLocation categoryLoc;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000195 IdentifierInfo *categoryId = 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000196 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000197 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000198 cutOffParsing();
199 return 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000200 }
201
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000202 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000203 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000204 categoryId = Tok.getIdentifierInfo();
205 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000206 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000207 else if (!getLangOpts().ObjC2) {
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000208 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +0000209 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000210 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000211
212 T.consumeClose();
213 if (T.getCloseLocation().isInvalid())
John McCall48871652010-08-21 09:40:31 +0000214 return 0;
Douglas Gregor0c254a02011-09-23 19:19:41 +0000215
216 if (!attrs.empty()) { // categories don't support attributes.
217 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
218 attrs.clear();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000219 }
Douglas Gregor0c254a02011-09-23 19:19:41 +0000220
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000221 // Next, we need to check for any protocol references.
222 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000223 SmallVector<Decl *, 8> ProtocolRefs;
224 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000225 if (Tok.is(tok::less) &&
226 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000227 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000228 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000229
John McCall48871652010-08-21 09:40:31 +0000230 Decl *CategoryType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000231 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000232 nameId, nameLoc,
233 categoryId, categoryLoc,
234 ProtocolRefs.data(),
235 ProtocolRefs.size(),
236 ProtocolLocs.data(),
237 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000238
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000239 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000240 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000241
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000242 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000243 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000244 }
245 // Parse a class interface.
246 IdentifierInfo *superClassId = 0;
247 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000248
Chris Lattner0ef13522007-10-09 17:51:17 +0000249 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000250 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000251
252 // Code completion of superclass names.
253 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000254 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000255 cutOffParsing();
256 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000257 }
258
Chris Lattner0ef13522007-10-09 17:51:17 +0000259 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000260 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +0000261 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000262 }
263 superClassId = Tok.getIdentifierInfo();
264 superClassLoc = ConsumeToken();
265 }
266 // Next, we need to check for any protocol references.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000267 SmallVector<Decl *, 8> ProtocolRefs;
268 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000269 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000270 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000271 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
272 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000273 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000274
John McCall48871652010-08-21 09:40:31 +0000275 Decl *ClsType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000276 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000277 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000278 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000279 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000280 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000281
Chris Lattner0ef13522007-10-09 17:51:17 +0000282 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000283 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000284
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000285 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000286 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000287}
288
John McCall064d77b2009-12-03 22:31:13 +0000289/// The Objective-C property callback. This should be defined where
290/// it's used, but instead it's been lifted to here to support VS2005.
291struct Parser::ObjCPropertyCallback : FieldCallback {
David Blaikie68e081d2011-12-20 02:48:34 +0000292private:
293 virtual void anchor();
294public:
John McCall064d77b2009-12-03 22:31:13 +0000295 Parser &P;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000296 SmallVectorImpl<Decl *> &Props;
John McCall064d77b2009-12-03 22:31:13 +0000297 ObjCDeclSpec &OCDS;
298 SourceLocation AtLoc;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000299 SourceLocation LParenLoc;
John McCall064d77b2009-12-03 22:31:13 +0000300 tok::ObjCKeywordKind MethodImplKind;
301
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000302 ObjCPropertyCallback(Parser &P,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000303 SmallVectorImpl<Decl *> &Props,
John McCall064d77b2009-12-03 22:31:13 +0000304 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000305 SourceLocation LParenLoc,
John McCall064d77b2009-12-03 22:31:13 +0000306 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000307 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc), LParenLoc(LParenLoc),
John McCall064d77b2009-12-03 22:31:13 +0000308 MethodImplKind(MethodImplKind) {
309 }
310
John McCall48871652010-08-21 09:40:31 +0000311 Decl *invoke(FieldDeclarator &FD) {
John McCall064d77b2009-12-03 22:31:13 +0000312 if (FD.D.getIdentifier() == 0) {
313 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
314 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000315 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000316 }
317 if (FD.BitfieldSize) {
318 P.Diag(AtLoc, diag::err_objc_property_bitfield)
319 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000320 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000321 }
322
323 // Install the property declarator into interfaceDecl.
324 IdentifierInfo *SelName =
325 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
326
327 Selector GetterSel =
328 P.PP.getSelectorTable().getNullarySelector(SelName);
329 IdentifierInfo *SetterName = OCDS.getSetterName();
330 Selector SetterSel;
331 if (SetterName)
332 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
333 else
334 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
335 P.PP.getSelectorTable(),
336 FD.D.getIdentifier());
337 bool isOverridingProperty = false;
John McCall48871652010-08-21 09:40:31 +0000338 Decl *Property =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000339 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, LParenLoc,
340 FD, OCDS,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000341 GetterSel, SetterSel,
John McCall064d77b2009-12-03 22:31:13 +0000342 &isOverridingProperty,
343 MethodImplKind);
344 if (!isOverridingProperty)
345 Props.push_back(Property);
346
347 return Property;
348 }
349};
350
David Blaikie68e081d2011-12-20 02:48:34 +0000351void Parser::ObjCPropertyCallback::anchor() {
352}
353
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000354/// objc-interface-decl-list:
355/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000356/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000357/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000358/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000359/// objc-interface-decl-list declaration
360/// objc-interface-decl-list ';'
361///
Steve Naroff99264b42007-08-22 16:35:03 +0000362/// objc-method-requirement: [OBJC2]
363/// @required
364/// @optional
365///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000366void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
367 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000368 SmallVector<Decl *, 32> allMethods;
369 SmallVector<Decl *, 16> allProperties;
370 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000371 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000372
Ted Kremenekc7c64312010-01-07 01:20:12 +0000373 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000374
Steve Naroff99264b42007-08-22 16:35:03 +0000375 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000376 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000377 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCall48871652010-08-21 09:40:31 +0000378 Decl *methodPrototype =
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000379 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000380 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000381 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
382 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000383 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
384 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
385 SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
386 if (Tok.is(tok::semi))
387 ConsumeToken();
388 }
Steve Naroff99264b42007-08-22 16:35:03 +0000389 continue;
390 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000391 if (Tok.is(tok::l_paren)) {
392 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000393 ParseObjCMethodDecl(Tok.getLocation(),
394 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000395 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000396 continue;
397 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000398 // Ignore excess semicolons.
399 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000400 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000401 continue;
402 }
Mike Stump11289f42009-09-09 15:08:12 +0000403
Chris Lattnerda9fb152008-10-20 06:10:06 +0000404 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000405 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000406 break;
Mike Stump11289f42009-09-09 15:08:12 +0000407
Douglas Gregorf1934162010-01-13 21:24:21 +0000408 // Code completion within an Objective-C interface.
409 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000410 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000411 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000412 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000413 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000414 }
415
Chris Lattner038a3e32008-10-20 05:46:22 +0000416 // If we don't have an @ directive, parse it as a function definition.
417 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000418 // The code below does not consume '}'s because it is afraid of eating the
419 // end of a namespace. Because of the way this code is structured, an
420 // erroneous r_brace would cause an infinite loop if not handled here.
421 if (Tok.is(tok::r_brace))
422 break;
John McCall084e83d2011-03-24 11:26:52 +0000423 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000424 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000425 continue;
426 }
Mike Stump11289f42009-09-09 15:08:12 +0000427
Chris Lattner038a3e32008-10-20 05:46:22 +0000428 // Otherwise, we have an @ directive, eat the @.
429 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000430 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000431 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000432 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000433 }
434
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000435 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000436
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000437 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000438 AtEnd.setBegin(AtLoc);
439 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000440 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000441 } else if (DirectiveKind == tok::objc_not_keyword) {
442 Diag(Tok, diag::err_objc_unknown_at);
443 SkipUntil(tok::semi);
444 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000445 }
Mike Stump11289f42009-09-09 15:08:12 +0000446
Chris Lattnerda9fb152008-10-20 06:10:06 +0000447 // Eat the identifier.
448 ConsumeToken();
449
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000450 switch (DirectiveKind) {
451 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000452 // FIXME: If someone forgets an @end on a protocol, this loop will
453 // continue to eat up tons of stuff and spew lots of nonsense errors. It
454 // would probably be better to bail out if we saw an @class or @interface
455 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000456 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000457 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000458 SkipUntil(tok::r_brace, tok::at);
459 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000460
461 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000462 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000463 Diag(AtLoc, diag::err_objc_missing_end)
464 << FixItHint::CreateInsertion(AtLoc, "@end\n");
465 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
466 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000467 ConsumeToken();
468 break;
469
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000470 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000471 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000472 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000473 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000474 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000475 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000476 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000477 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000478 break;
Mike Stump11289f42009-09-09 15:08:12 +0000479
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000480 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000481 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000482 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000483
Chris Lattner038a3e32008-10-20 05:46:22 +0000484 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000485 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000486 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000487 if (Tok.is(tok::l_paren)) {
488 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000489 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000490 }
Mike Stump11289f42009-09-09 15:08:12 +0000491
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000492 ObjCPropertyCallback Callback(*this, allProperties,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000493 OCDS, AtLoc, LParenLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000494
Chris Lattner038a3e32008-10-20 05:46:22 +0000495 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +0000496 DeclSpec DS(AttrFactory);
John McCallcfefb6d2009-11-03 02:38:08 +0000497 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000498
John McCall405988b2011-03-26 01:53:26 +0000499 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000500 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000501 }
Steve Naroff99264b42007-08-22 16:35:03 +0000502 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000503
504 // We break out of the big loop in two cases: when we see @end or when we see
505 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000506 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000507 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000508 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000509 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000510 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000511 } else {
512 Diag(Tok, diag::err_objc_missing_end)
513 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
514 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
515 << (int) Actions.getObjCContainerKind();
516 AtEnd.setBegin(Tok.getLocation());
517 AtEnd.setEnd(Tok.getLocation());
518 }
Mike Stump11289f42009-09-09 15:08:12 +0000519
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000520 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000521 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000522 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump11289f42009-09-09 15:08:12 +0000523 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000524 allProperties.data(), allProperties.size(),
525 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000526}
527
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000528/// Parse property attribute declarations.
529///
530/// property-attr-decl: '(' property-attrlist ')'
531/// property-attrlist:
532/// property-attribute
533/// property-attrlist ',' property-attribute
534/// property-attribute:
535/// getter '=' identifier
536/// setter '=' identifier ':'
537/// readonly
538/// readwrite
539/// assign
540/// retain
541/// copy
542/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000543/// atomic
544/// strong
545/// weak
546/// unsafe_unretained
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000547///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000548void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000549 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000550 BalancedDelimiterTracker T(*this, tok::l_paren);
551 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000552
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000553 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000554 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000555 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000556 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000557 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000558 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000559
Chris Lattner76619232008-10-20 07:22:18 +0000560 // If this is not an identifier at all, bail out early.
561 if (II == 0) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000562 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000563 return;
564 }
Mike Stump11289f42009-09-09 15:08:12 +0000565
Chris Lattner1db33542008-10-20 07:37:22 +0000566 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000567
Chris Lattner68e48682008-11-20 04:42:34 +0000568 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000569 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000570 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000571 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000572 else if (II->isStr("unsafe_unretained"))
573 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000574 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000575 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000576 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000577 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000578 else if (II->isStr("strong"))
579 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000580 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000581 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000582 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000583 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000584 else if (II->isStr("atomic"))
585 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000586 else if (II->isStr("weak"))
587 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000588 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000589 bool IsSetter = II->getNameStart()[0] == 's';
590
Chris Lattner825bca12008-10-20 07:39:53 +0000591 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000592 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
593 diag::err_objc_expected_equal_for_getter;
594
595 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000596 return;
Mike Stump11289f42009-09-09 15:08:12 +0000597
Douglas Gregorc8537c52009-11-19 07:41:15 +0000598 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000599 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000600 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000601 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000602 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000603 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000604 }
605
Anders Carlssonfe15a782010-10-02 17:45:21 +0000606
607 SourceLocation SelLoc;
608 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
609
610 if (!SelIdent) {
611 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
612 << IsSetter;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000613 SkipUntil(tok::r_paren);
614 return;
615 }
Mike Stump11289f42009-09-09 15:08:12 +0000616
Anders Carlssonfe15a782010-10-02 17:45:21 +0000617 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000618 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000619 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000620
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000621 if (ExpectAndConsume(tok::colon,
622 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000623 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000624 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000625 } else {
626 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000627 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000628 }
Chris Lattner825bca12008-10-20 07:39:53 +0000629 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000630 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000631 SkipUntil(tok::r_paren);
632 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000633 }
Mike Stump11289f42009-09-09 15:08:12 +0000634
Chris Lattner1db33542008-10-20 07:37:22 +0000635 if (Tok.isNot(tok::comma))
636 break;
Mike Stump11289f42009-09-09 15:08:12 +0000637
Chris Lattner1db33542008-10-20 07:37:22 +0000638 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000639 }
Mike Stump11289f42009-09-09 15:08:12 +0000640
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000641 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000642}
643
Steve Naroff09bf8152007-09-06 21:24:23 +0000644/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000645/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000646/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000647///
648/// objc-instance-method: '-'
649/// objc-class-method: '+'
650///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000651/// objc-method-attributes: [OBJC2]
652/// __attribute__((deprecated))
653///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000654Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000655 bool MethodDefinition) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000656 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000657
Mike Stump11289f42009-09-09 15:08:12 +0000658 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000659 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000660 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000661 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000662 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000663 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000664 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000665}
666
667/// objc-selector:
668/// identifier
669/// one of
670/// enum struct union if else while do for switch case default
671/// break continue return goto asm sizeof typeof __alignof
672/// unsigned long const short volatile signed restrict _Complex
673/// in out inout bycopy byref oneway int char float double void _Bool
674///
Chris Lattner4f472a32009-04-11 18:13:45 +0000675IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000676
Chris Lattner5700fab2007-10-07 02:00:24 +0000677 switch (Tok.getKind()) {
678 default:
679 return 0;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000680 case tok::ampamp:
681 case tok::ampequal:
682 case tok::amp:
683 case tok::pipe:
684 case tok::tilde:
685 case tok::exclaim:
686 case tok::exclaimequal:
687 case tok::pipepipe:
688 case tok::pipeequal:
689 case tok::caret:
690 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000691 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000692 if (isalpha(ThisTok[0])) {
693 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
694 Tok.setKind(tok::identifier);
695 SelectorLoc = ConsumeToken();
696 return II;
697 }
698 return 0;
699 }
700
Chris Lattner5700fab2007-10-07 02:00:24 +0000701 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000702 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000703 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000704 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000705 case tok::kw_break:
706 case tok::kw_case:
707 case tok::kw_catch:
708 case tok::kw_char:
709 case tok::kw_class:
710 case tok::kw_const:
711 case tok::kw_const_cast:
712 case tok::kw_continue:
713 case tok::kw_default:
714 case tok::kw_delete:
715 case tok::kw_do:
716 case tok::kw_double:
717 case tok::kw_dynamic_cast:
718 case tok::kw_else:
719 case tok::kw_enum:
720 case tok::kw_explicit:
721 case tok::kw_export:
722 case tok::kw_extern:
723 case tok::kw_false:
724 case tok::kw_float:
725 case tok::kw_for:
726 case tok::kw_friend:
727 case tok::kw_goto:
728 case tok::kw_if:
729 case tok::kw_inline:
730 case tok::kw_int:
731 case tok::kw_long:
732 case tok::kw_mutable:
733 case tok::kw_namespace:
734 case tok::kw_new:
735 case tok::kw_operator:
736 case tok::kw_private:
737 case tok::kw_protected:
738 case tok::kw_public:
739 case tok::kw_register:
740 case tok::kw_reinterpret_cast:
741 case tok::kw_restrict:
742 case tok::kw_return:
743 case tok::kw_short:
744 case tok::kw_signed:
745 case tok::kw_sizeof:
746 case tok::kw_static:
747 case tok::kw_static_cast:
748 case tok::kw_struct:
749 case tok::kw_switch:
750 case tok::kw_template:
751 case tok::kw_this:
752 case tok::kw_throw:
753 case tok::kw_true:
754 case tok::kw_try:
755 case tok::kw_typedef:
756 case tok::kw_typeid:
757 case tok::kw_typename:
758 case tok::kw_typeof:
759 case tok::kw_union:
760 case tok::kw_unsigned:
761 case tok::kw_using:
762 case tok::kw_virtual:
763 case tok::kw_void:
764 case tok::kw_volatile:
765 case tok::kw_wchar_t:
766 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000767 case tok::kw__Bool:
768 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000769 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000770 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000771 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000772 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000773 }
Steve Naroff99264b42007-08-22 16:35:03 +0000774}
775
Fariborz Jahanian83615522008-01-02 22:54:34 +0000776/// objc-for-collection-in: 'in'
777///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000778bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000779 // FIXME: May have to do additional look-ahead to only allow for
780 // valid tokens following an 'in'; such as an identifier, unary operators,
781 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000782 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000783 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000784}
785
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000786/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000787/// qualifier list and builds their bitmask representation in the input
788/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000789///
790/// objc-type-qualifiers:
791/// objc-type-qualifier
792/// objc-type-qualifiers objc-type-qualifier
793///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000794void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000795 Declarator::TheContext Context) {
796 assert(Context == Declarator::ObjCParameterContext ||
797 Context == Declarator::ObjCResultContext);
798
Chris Lattner61511e12007-12-12 06:56:32 +0000799 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000800 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000801 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +0000802 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000803 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +0000804 }
805
Chris Lattner5e530bc2007-12-27 19:57:00 +0000806 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000807 return;
Mike Stump11289f42009-09-09 15:08:12 +0000808
Chris Lattner61511e12007-12-12 06:56:32 +0000809 const IdentifierInfo *II = Tok.getIdentifierInfo();
810 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000811 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000812 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000813
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000814 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000815 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +0000816 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000817 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
818 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
819 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
820 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
821 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
822 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000823 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000824 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000825 ConsumeToken();
826 II = 0;
827 break;
828 }
Mike Stump11289f42009-09-09 15:08:12 +0000829
Chris Lattner61511e12007-12-12 06:56:32 +0000830 // If this wasn't a recognized qualifier, bail out.
831 if (II) return;
832 }
833}
834
John McCalla55902b2011-10-01 09:56:14 +0000835/// Take all the decl attributes out of the given list and add
836/// them to the given attribute set.
837static void takeDeclAttributes(ParsedAttributes &attrs,
838 AttributeList *list) {
839 while (list) {
840 AttributeList *cur = list;
841 list = cur->getNext();
842
843 if (!cur->isUsedAsTypeAttr()) {
844 // Clear out the next pointer. We're really completely
845 // destroying the internal invariants of the declarator here,
846 // but it doesn't matter because we're done with it.
847 cur->setNext(0);
848 attrs.add(cur);
849 }
850 }
851}
852
853/// takeDeclAttributes - Take all the decl attributes from the given
854/// declarator and add them to the given list.
855static void takeDeclAttributes(ParsedAttributes &attrs,
856 Declarator &D) {
857 // First, take ownership of all attributes.
858 attrs.getPool().takeAllFrom(D.getAttributePool());
859 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
860
861 // Now actually move the attributes over.
862 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
863 takeDeclAttributes(attrs, D.getAttributes());
864 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
865 takeDeclAttributes(attrs,
866 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
867}
868
Chris Lattner61511e12007-12-12 06:56:32 +0000869/// objc-type-name:
870/// '(' objc-type-qualifiers[opt] type-name ')'
871/// '(' objc-type-qualifiers[opt] ')'
872///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000873ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000874 Declarator::TheContext context,
875 ParsedAttributes *paramAttrs) {
876 assert(context == Declarator::ObjCParameterContext ||
877 context == Declarator::ObjCResultContext);
878 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
879
Chris Lattner0ef13522007-10-09 17:51:17 +0000880 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000881
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000882 BalancedDelimiterTracker T(*this, tok::l_paren);
883 T.consumeOpen();
884
Chris Lattner2ebb1782008-08-23 01:48:03 +0000885 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000886 ObjCDeclContextSwitch ObjCDC(*this);
887
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000888 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +0000889 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000890
John McCallba7bf592010-08-24 05:47:05 +0000891 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000892 if (isTypeSpecifierQualifier()) {
John McCalla55902b2011-10-01 09:56:14 +0000893 // Parse an abstract declarator.
894 DeclSpec declSpec(AttrFactory);
895 declSpec.setObjCQualifiers(&DS);
896 ParseSpecifierQualifierList(declSpec);
Fariborz Jahanian7a055362012-05-09 21:49:29 +0000897 declSpec.SetRangeEnd(Tok.getLocation().getLocWithOffset(-1));
John McCalla55902b2011-10-01 09:56:14 +0000898 Declarator declarator(declSpec, context);
899 ParseDeclarator(declarator);
900
901 // If that's not invalid, extract a type.
902 if (!declarator.isInvalidType()) {
903 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
904 if (!type.isInvalid())
905 Ty = type.get();
906
907 // If we're parsing a parameter, steal all the decl attributes
908 // and add them to the decl spec.
909 if (context == Declarator::ObjCParameterContext)
910 takeDeclAttributes(*paramAttrs, declarator);
911 }
912 } else if (context == Declarator::ObjCResultContext &&
913 Tok.is(tok::identifier)) {
Douglas Gregorbab8a962011-09-08 01:46:34 +0000914 if (!Ident_instancetype)
915 Ident_instancetype = PP.getIdentifierInfo("instancetype");
916
917 if (Tok.getIdentifierInfo() == Ident_instancetype) {
918 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
919 ConsumeToken();
920 }
Douglas Gregor220cac52009-02-18 17:45:20 +0000921 }
Douglas Gregorbab8a962011-09-08 01:46:34 +0000922
Steve Naroff90255b42008-10-21 14:15:04 +0000923 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000924 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000925 else if (Tok.getLocation() == TypeStartLoc) {
926 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000927 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000928 SkipUntil(tok::r_paren);
929 } else {
930 // Otherwise, we found *something*, but didn't get a ')' in the right
931 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000932 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000933 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000934 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000935}
936
937/// objc-method-decl:
938/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000939/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000940/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000941/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000942///
943/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000944/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000945/// objc-keyword-selector objc-keyword-decl
946///
947/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000948/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
949/// objc-selector ':' objc-keyword-attributes[opt] identifier
950/// ':' objc-type-name objc-keyword-attributes[opt] identifier
951/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000952///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000953/// objc-parmlist:
954/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000955///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000956/// objc-parms:
957/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000958///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000959/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000960/// , ...
961///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000962/// objc-keyword-attributes: [OBJC2]
963/// __attribute__((unused))
964///
John McCall48871652010-08-21 09:40:31 +0000965Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000966 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000967 tok::ObjCKeywordKind MethodImplKind,
968 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +0000969 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +0000970
Douglas Gregor636a61e2010-04-07 00:21:17 +0000971 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000972 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000973 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000974 cutOffParsing();
975 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000976 }
977
Chris Lattner2ebb1782008-08-23 01:48:03 +0000978 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +0000979 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000980 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000981 if (Tok.is(tok::l_paren))
John McCalla55902b2011-10-01 09:56:14 +0000982 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump11289f42009-09-09 15:08:12 +0000983
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000984 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +0000985 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +0000986 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +0000987 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000988
Douglas Gregor636a61e2010-04-07 00:21:17 +0000989 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000990 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000991 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000992 cutOffParsing();
993 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000994 }
995
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000996 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +0000997 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000998 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000999
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001000 // An unnamed colon is valid.
1001 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001002 Diag(Tok, diag::err_expected_selector_for_method)
1003 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +00001004 // Skip until we get a ; or {}.
1005 SkipUntil(tok::r_brace);
John McCall48871652010-08-21 09:40:31 +00001006 return 0;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001007 }
Mike Stump11289f42009-09-09 15:08:12 +00001008
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001009 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001010 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001011 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001012 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001013 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001014
Chris Lattner5700fab2007-10-07 02:00:24 +00001015 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001016 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001017 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001018 mType, DSRet, ReturnType,
Douglas Gregor33823722011-06-11 01:09:30 +00001019 selLoc, Sel, 0,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001020 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001021 methodAttrs.getList(), MethodImplKind,
1022 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001023 PD.complete(Result);
1024 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001025 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001026
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001027 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001028 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001029 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001030 ParseScope PrototypeScope(this,
1031 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001032
1033 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001034
Chris Lattner5700fab2007-10-07 02:00:24 +00001035 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001036 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001037 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001038
Chris Lattner5700fab2007-10-07 02:00:24 +00001039 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +00001040 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001041 Diag(Tok, diag::err_expected_colon);
1042 break;
1043 }
1044 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001045
John McCallba7bf592010-08-24 05:47:05 +00001046 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001047 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001048 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1049 Declarator::ObjCParameterContext,
1050 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001051
Chris Lattner5700fab2007-10-07 02:00:24 +00001052 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001053 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001054 ArgInfo.ArgAttrs = 0;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001055 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001056 MaybeParseGNUAttributes(paramAttrs);
1057 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001058 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001059
Douglas Gregor45879692010-07-08 23:37:41 +00001060 // Code completion for the next piece of the selector.
1061 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001062 KeyIdents.push_back(SelIdent);
1063 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1064 mType == tok::minus,
1065 /*AtParameterName=*/true,
1066 ReturnType,
1067 KeyIdents.data(),
1068 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001069 cutOffParsing();
1070 return 0;
Douglas Gregor45879692010-07-08 23:37:41 +00001071 }
1072
Chris Lattner0ef13522007-10-09 17:51:17 +00001073 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001074 Diag(Tok, diag::err_expected_ident); // missing argument name.
1075 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001076 }
Mike Stump11289f42009-09-09 15:08:12 +00001077
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001078 ArgInfo.Name = Tok.getIdentifierInfo();
1079 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001080 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001081
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001082 ArgInfos.push_back(ArgInfo);
1083 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001084 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001085
John McCall084e83d2011-03-24 11:26:52 +00001086 // Make sure the attributes persist.
1087 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1088
Douglas Gregor95887f92010-07-08 23:20:03 +00001089 // Code completion for the next piece of the selector.
1090 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001091 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1092 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001093 /*AtParameterName=*/false,
Douglas Gregor95887f92010-07-08 23:20:03 +00001094 ReturnType,
1095 KeyIdents.data(),
1096 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001097 cutOffParsing();
1098 return 0;
Douglas Gregor95887f92010-07-08 23:20:03 +00001099 }
1100
Chris Lattner5700fab2007-10-07 02:00:24 +00001101 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001102 SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001103 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001104 break;
1105 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001106 }
Mike Stump11289f42009-09-09 15:08:12 +00001107
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001108 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +00001109
Chris Lattner5700fab2007-10-07 02:00:24 +00001110 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001111 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001112 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001113 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001114 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001115 ConsumeToken();
1116 break;
1117 }
John McCall084e83d2011-03-24 11:26:52 +00001118 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001119 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001120 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001121 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1122 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001123 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001124 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001125 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1126 ParmDecl.getIdentifierLoc(),
1127 Param,
1128 0));
1129
Chris Lattner5700fab2007-10-07 02:00:24 +00001130 }
Mike Stump11289f42009-09-09 15:08:12 +00001131
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001132 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001133 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001134 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001135 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001136
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001137 if (KeyIdents.size() == 0)
John McCall48871652010-08-21 09:40:31 +00001138 return 0;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001139
Chris Lattner5700fab2007-10-07 02:00:24 +00001140 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1141 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001142 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001143 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001144 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001145 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001146 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001147 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001148 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001149
John McCall28a6aea2009-11-04 02:18:39 +00001150 PD.complete(Result);
1151 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001152}
1153
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001154/// objc-protocol-refs:
1155/// '<' identifier-list '>'
1156///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001157bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001158ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1159 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001160 bool WarnOnDeclarations,
1161 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001162 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001163
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001164 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001165
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001166 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001167
Chris Lattner3bbae002008-07-26 04:03:38 +00001168 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001169 if (Tok.is(tok::code_completion)) {
1170 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1171 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001172 cutOffParsing();
1173 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001174 }
1175
Chris Lattner3bbae002008-07-26 04:03:38 +00001176 if (Tok.isNot(tok::identifier)) {
1177 Diag(Tok, diag::err_expected_ident);
1178 SkipUntil(tok::greater);
1179 return true;
1180 }
1181 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1182 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001183 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001184 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001185
Chris Lattner3bbae002008-07-26 04:03:38 +00001186 if (Tok.isNot(tok::comma))
1187 break;
1188 ConsumeToken();
1189 }
Mike Stump11289f42009-09-09 15:08:12 +00001190
Chris Lattner3bbae002008-07-26 04:03:38 +00001191 // Consume the '>'.
1192 if (Tok.isNot(tok::greater)) {
1193 Diag(Tok, diag::err_expected_greater);
1194 return true;
1195 }
Mike Stump11289f42009-09-09 15:08:12 +00001196
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001197 EndLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001198
Chris Lattner3bbae002008-07-26 04:03:38 +00001199 // Convert the list of protocols identifiers into a list of protocol decls.
1200 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1201 &ProtocolIdents[0], ProtocolIdents.size(),
1202 Protocols);
1203 return false;
1204}
1205
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001206/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1207/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001208bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001209 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001210 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001211 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001212 SmallVector<Decl *, 8> ProtocolDecl;
1213 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001214 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1215 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001216 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1217 ProtocolLocs.data(), LAngleLoc);
1218 if (EndProtoLoc.isValid())
1219 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001220 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001221}
1222
1223
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001224/// objc-class-instance-variables:
1225/// '{' objc-instance-variable-decl-list[opt] '}'
1226///
1227/// objc-instance-variable-decl-list:
1228/// objc-visibility-spec
1229/// objc-instance-variable-decl ';'
1230/// ';'
1231/// objc-instance-variable-decl-list objc-visibility-spec
1232/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1233/// objc-instance-variable-decl-list ';'
1234///
1235/// objc-visibility-spec:
1236/// @private
1237/// @protected
1238/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001239/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001240///
1241/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001242/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001243///
John McCall48871652010-08-21 09:40:31 +00001244void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001245 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001246 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001247 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001248 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001249
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001250 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001251 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001252
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001253 BalancedDelimiterTracker T(*this, tok::l_brace);
1254 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00001255
Steve Naroff00433d32007-08-21 21:17:12 +00001256 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +00001257 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001258 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001259
Steve Naroff00433d32007-08-21 21:17:12 +00001260 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001261 if (Tok.is(tok::semi)) {
Douglas Gregor13d05682010-06-16 23:08:59 +00001262 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregora771f462010-03-31 17:46:05 +00001263 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +00001264 ConsumeToken();
1265 continue;
1266 }
Mike Stump11289f42009-09-09 15:08:12 +00001267
Steve Naroff00433d32007-08-21 21:17:12 +00001268 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001269 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001270 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001271
1272 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001273 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001274 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001275 }
1276
Steve Naroff7c348172007-08-23 18:16:40 +00001277 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001278 case tok::objc_private:
1279 case tok::objc_public:
1280 case tok::objc_protected:
1281 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001282 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001283 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001284 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001285 default:
1286 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +00001287 continue;
1288 }
1289 }
Mike Stump11289f42009-09-09 15:08:12 +00001290
Douglas Gregor48d46252010-01-13 21:54:15 +00001291 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001292 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001293 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001294 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001295 }
1296
John McCallcfefb6d2009-11-03 02:38:08 +00001297 struct ObjCIvarCallback : FieldCallback {
1298 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001299 Decl *IDecl;
John McCallcfefb6d2009-11-03 02:38:08 +00001300 tok::ObjCKeywordKind visibility;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001301 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001302
John McCall48871652010-08-21 09:40:31 +00001303 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001304 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001305 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1306 }
1307
John McCall48871652010-08-21 09:40:31 +00001308 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001309 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallcfefb6d2009-11-03 02:38:08 +00001310 // Install the declarator into the interface decl.
John McCall48871652010-08-21 09:40:31 +00001311 Decl *Field
Douglas Gregor0be31a22010-07-02 17:43:08 +00001312 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallcfefb6d2009-11-03 02:38:08 +00001313 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001314 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian4327b322011-08-29 17:33:12 +00001315 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001316 if (Field)
1317 AllIvarDecls.push_back(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001318 return Field;
1319 }
1320 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001321
Chris Lattnera12405b2008-04-10 06:46:29 +00001322 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00001323 DeclSpec DS(AttrFactory);
John McCallcfefb6d2009-11-03 02:38:08 +00001324 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001325
Chris Lattner0ef13522007-10-09 17:51:17 +00001326 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001327 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001328 } else {
1329 Diag(Tok, diag::err_expected_semi_decl_list);
1330 // Skip to end of block or statement
1331 SkipUntil(tok::r_brace, true, true);
1332 }
1333 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001334 T.consumeClose();
1335
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001336 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001337 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
Fariborz Jahanian4327b322011-08-29 17:33:12 +00001338 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001339 // Call ActOnFields() even if we don't have any decls. This is useful
1340 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001341 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie751c5582011-09-22 02:58:26 +00001342 AllIvarDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001343 T.getOpenLocation(), T.getCloseLocation(), 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001344 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001345}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001346
1347/// objc-protocol-declaration:
1348/// objc-protocol-definition
1349/// objc-protocol-forward-reference
1350///
1351/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001352/// @protocol identifier
1353/// objc-protocol-refs[opt]
1354/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001355/// @end
1356///
1357/// objc-protocol-forward-reference:
1358/// @protocol identifier-list ';'
1359///
1360/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001361/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001362/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001363Parser::DeclGroupPtrTy
1364Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1365 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001366 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001367 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1368 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001369
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001370 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001371 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001372 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00001373 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001374 }
1375
Chris Lattner0ef13522007-10-09 17:51:17 +00001376 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001377 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00001378 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001379 }
1380 // Save the protocol name, then consume it.
1381 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1382 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001383
Chris Lattner0ef13522007-10-09 17:51:17 +00001384 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001385 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001386 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001387 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001388 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001389 }
Mike Stump11289f42009-09-09 15:08:12 +00001390
Erik Verbruggenf9887852011-12-08 09:58:43 +00001391 CheckNestedObjCContexts(AtLoc);
1392
Chris Lattner0ef13522007-10-09 17:51:17 +00001393 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001394 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001395 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1396
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001397 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001398 while (1) {
1399 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001400 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001401 Diag(Tok, diag::err_expected_ident);
1402 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00001403 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001404 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001405 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1406 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001407 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001408
Chris Lattner0ef13522007-10-09 17:51:17 +00001409 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001410 break;
1411 }
1412 // Consume the ';'.
1413 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00001414 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001415
Steve Naroff93eb5f12007-10-10 17:32:04 +00001416 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001417 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001418 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001419 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001420 }
Mike Stump11289f42009-09-09 15:08:12 +00001421
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001422 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001423 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001424
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001425 SmallVector<Decl *, 8> ProtocolRefs;
1426 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001427 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001428 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1429 LAngleLoc, EndProtoLoc))
Douglas Gregorf6102672012-01-01 21:23:57 +00001430 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001431
John McCall48871652010-08-21 09:40:31 +00001432 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001433 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001434 ProtocolRefs.data(),
1435 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001436 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001437 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001438
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001439 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00001440 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001441}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001442
1443/// objc-implementation:
1444/// objc-class-implementation-prologue
1445/// objc-category-implementation-prologue
1446///
1447/// objc-class-implementation-prologue:
1448/// @implementation identifier objc-superclass[opt]
1449/// objc-class-instance-variables[opt]
1450///
1451/// objc-category-implementation-prologue:
1452/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001453Parser::DeclGroupPtrTy
1454Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001455 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1456 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001457 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001458 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001459
Douglas Gregor49c22a72009-11-18 16:26:39 +00001460 // Code completion after '@implementation'.
1461 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001462 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001463 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001464 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001465 }
1466
Chris Lattner0ef13522007-10-09 17:51:17 +00001467 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001468 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001469 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001470 }
1471 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001472 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001473 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001474 Decl *ObjCImpDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001475
1476 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001477 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001478 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001479 SourceLocation categoryLoc, rparenLoc;
1480 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001481
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001482 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001483 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001484 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001485 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001486 }
1487
Chris Lattner0ef13522007-10-09 17:51:17 +00001488 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001489 categoryId = Tok.getIdentifierInfo();
1490 categoryLoc = ConsumeToken();
1491 } else {
1492 Diag(Tok, diag::err_expected_ident); // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001493 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001494 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001495 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001496 Diag(Tok, diag::err_expected_rparen);
1497 SkipUntil(tok::r_paren, false); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001498 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001499 }
1500 rparenLoc = ConsumeParen();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001501 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001502 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001503 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001504
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001505 } else {
1506 // We have a class implementation
1507 SourceLocation superClassLoc;
1508 IdentifierInfo *superClassId = 0;
1509 if (Tok.is(tok::colon)) {
1510 // We have a super class
1511 ConsumeToken();
1512 if (Tok.isNot(tok::identifier)) {
1513 Diag(Tok, diag::err_expected_ident); // missing super class name.
1514 return DeclGroupPtrTy();
1515 }
1516 superClassId = Tok.getIdentifierInfo();
1517 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001518 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001519 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1520 AtLoc, nameId, nameLoc,
1521 superClassId, superClassLoc);
1522
1523 if (Tok.is(tok::l_brace)) // we have ivars
1524 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001525 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001526 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001527
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001528 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001529
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001530 {
1531 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
1532 while (!ObjCImplParsing.isFinished() && Tok.isNot(tok::eof)) {
1533 ParsedAttributesWithRange attrs(AttrFactory);
1534 MaybeParseCXX0XAttributes(attrs);
1535 MaybeParseMicrosoftAttributes(attrs);
1536 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1537 DeclGroupRef DG = DGP.get();
1538 DeclsInGroup.append(DG.begin(), DG.end());
1539 }
1540 }
1541 }
1542
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001543 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001544}
Steve Naroff33a1e802007-10-29 21:38:07 +00001545
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001546Parser::DeclGroupPtrTy
1547Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001548 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1549 "ParseObjCAtEndDeclaration(): Expected @end");
1550 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001551 if (CurParsedObjCImpl)
1552 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001553 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00001554 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001555 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001556 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001557}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001558
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001559Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1560 if (!Finished) {
1561 finish(P.Tok.getLocation());
1562 if (P.Tok.is(tok::eof)) {
1563 P.Diag(P.Tok, diag::err_objc_missing_end)
1564 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1565 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1566 << Sema::OCK_Implementation;
1567 }
1568 }
1569 P.CurParsedObjCImpl = 0;
1570 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001571}
1572
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001573void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1574 assert(!Finished);
1575 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1576 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1577 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
1578
1579 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1580
1581 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001582 for (LateParsedObjCMethodContainer::iterator
1583 I = LateParsedObjCMethods.begin(),
1584 E = LateParsedObjCMethods.end(); I != E; ++I)
1585 delete *I;
1586 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001587
1588 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001589}
1590
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001591/// compatibility-alias-decl:
1592/// @compatibility_alias alias-name class-name ';'
1593///
John McCall48871652010-08-21 09:40:31 +00001594Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001595 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1596 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1597 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001598 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001599 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001600 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001601 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001602 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1603 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001604 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001605 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001606 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001607 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001608 IdentifierInfo *classId = Tok.getIdentifierInfo();
1609 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregor78edf992011-01-05 01:10:06 +00001610 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1611 "@compatibility_alias");
Chris Lattner83f095c2009-03-28 19:18:32 +00001612 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1613 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001614}
1615
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001616/// property-synthesis:
1617/// @synthesize property-ivar-list ';'
1618///
1619/// property-ivar-list:
1620/// property-ivar
1621/// property-ivar-list ',' property-ivar
1622///
1623/// property-ivar:
1624/// identifier
1625/// identifier '=' identifier
1626///
John McCall48871652010-08-21 09:40:31 +00001627Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001628 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1629 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001630 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001631
Douglas Gregor88e72a02009-11-18 19:45:45 +00001632 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001633 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001634 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001635 cutOffParsing();
1636 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001637 }
1638
Douglas Gregor88e72a02009-11-18 19:45:45 +00001639 if (Tok.isNot(tok::identifier)) {
1640 Diag(Tok, diag::err_synthesized_property_name);
1641 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001642 return 0;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001643 }
1644
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001645 IdentifierInfo *propertyIvar = 0;
1646 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1647 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001648 SourceLocation propertyIvarLoc;
Chris Lattner0ef13522007-10-09 17:51:17 +00001649 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001650 // property '=' ivar-name
1651 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001652
1653 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001654 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001655 cutOffParsing();
1656 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001657 }
1658
Chris Lattner0ef13522007-10-09 17:51:17 +00001659 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001660 Diag(Tok, diag::err_expected_ident);
1661 break;
1662 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001663 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001664 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001665 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001666 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001667 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001668 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001669 break;
1670 ConsumeToken(); // consume ','
1671 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001672 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCall48871652010-08-21 09:40:31 +00001673 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001674}
1675
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001676/// property-dynamic:
1677/// @dynamic property-list
1678///
1679/// property-list:
1680/// identifier
1681/// property-list ',' identifier
1682///
John McCall48871652010-08-21 09:40:31 +00001683Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001684 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1685 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001686 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001687 while (true) {
1688 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001689 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001690 cutOffParsing();
1691 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001692 }
1693
1694 if (Tok.isNot(tok::identifier)) {
1695 Diag(Tok, diag::err_expected_ident);
1696 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001697 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001698 }
1699
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001700 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1701 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001702 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001703 propertyId, 0, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001704
Chris Lattner0ef13522007-10-09 17:51:17 +00001705 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001706 break;
1707 ConsumeToken(); // consume ','
1708 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001709 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCall48871652010-08-21 09:40:31 +00001710 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001711}
Mike Stump11289f42009-09-09 15:08:12 +00001712
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001713/// objc-throw-statement:
1714/// throw expression[opt];
1715///
John McCalldadc5752010-08-24 06:29:42 +00001716StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1717 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001718 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001719 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001720 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001721 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001722 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001723 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001724 }
1725 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001726 // consume ';'
1727 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCallb268a282010-08-23 23:25:46 +00001728 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001729}
1730
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001731/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001732/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001733///
John McCalldadc5752010-08-24 06:29:42 +00001734StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001735Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001736 ConsumeToken(); // consume synchronized
1737 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001738 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001739 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001740 }
John McCalld9bb7432011-07-27 21:50:02 +00001741
1742 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001743 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00001744 ExprResult operand(ParseExpression());
1745
1746 if (Tok.is(tok::r_paren)) {
1747 ConsumeParen(); // ')'
1748 } else {
1749 if (!operand.isInvalid())
1750 Diag(Tok, diag::err_expected_rparen);
1751
1752 // Skip forward until we see a left brace, but don't consume it.
1753 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001754 }
John McCalld9bb7432011-07-27 21:50:02 +00001755
1756 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001757 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00001758 if (!operand.isInvalid())
1759 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001760 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001761 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001762
John McCalld9bb7432011-07-27 21:50:02 +00001763 // Check the @synchronized operand now.
1764 if (!operand.isInvalid())
1765 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001766
John McCalld9bb7432011-07-27 21:50:02 +00001767 // Parse the compound statement within a new scope.
1768 ParseScope bodyScope(this, Scope::DeclScope);
1769 StmtResult body(ParseCompoundStatementBody());
1770 bodyScope.Exit();
1771
1772 // If there was a semantic or parse error earlier with the
1773 // operand, fail now.
1774 if (operand.isInvalid())
1775 return StmtError();
1776
1777 if (body.isInvalid())
1778 body = Actions.ActOnNullStmt(Tok.getLocation());
1779
1780 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001781}
1782
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001783/// objc-try-catch-statement:
1784/// @try compound-statement objc-catch-list[opt]
1785/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1786///
1787/// objc-catch-list:
1788/// @catch ( parameter-declaration ) compound-statement
1789/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1790/// catch-parameter-declaration:
1791/// parameter-declaration
1792/// '...' [OBJC2]
1793///
John McCalldadc5752010-08-24 06:29:42 +00001794StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001795 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001796
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001797 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001798 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001799 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001800 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001801 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001802 StmtVector CatchStmts(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001803 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001804 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001805 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001806 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001807 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001808 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001809
Chris Lattner0ef13522007-10-09 17:51:17 +00001810 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001811 // At this point, we need to lookahead to determine if this @ is the start
1812 // of an @catch or @finally. We don't want to consume the @ token if this
1813 // is an @try or @encode or something else.
1814 Token AfterAt = GetLookAheadToken(1);
1815 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1816 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1817 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001818
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001819 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001820 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCall48871652010-08-21 09:40:31 +00001821 Decl *FirstPart = 0;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001822 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001823 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001824 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001825 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001826 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001827 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001828 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00001829 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00001830 ParseDeclarator(ParmDecl);
1831
Douglas Gregore11ee112010-04-23 23:01:43 +00001832 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001833 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001834 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001835 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001836 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001837
Steve Naroff65a00892009-04-07 22:56:58 +00001838 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001839
Steve Naroff65a00892009-04-07 22:56:58 +00001840 if (Tok.is(tok::r_paren))
1841 RParenLoc = ConsumeParen();
1842 else // Skip over garbage, until we get to ')'. Eat the ')'.
1843 SkipUntil(tok::r_paren, true, false);
1844
John McCalldadc5752010-08-24 06:29:42 +00001845 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001846 if (Tok.is(tok::l_brace))
1847 CatchBody = ParseCompoundStatementBody();
1848 else
1849 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001850 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001851 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001852
John McCalldadc5752010-08-24 06:29:42 +00001853 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001854 RParenLoc,
1855 FirstPart,
John McCallb268a282010-08-23 23:25:46 +00001856 CatchBody.take());
Douglas Gregor96c79492010-04-23 22:50:49 +00001857 if (!Catch.isInvalid())
1858 CatchStmts.push_back(Catch.release());
1859
Steve Naroffe6016792008-02-05 21:27:35 +00001860 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001861 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1862 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001863 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001864 }
1865 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001866 } else {
1867 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001868 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001869 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001870
John McCalldadc5752010-08-24 06:29:42 +00001871 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001872 if (Tok.is(tok::l_brace))
1873 FinallyBody = ParseCompoundStatementBody();
1874 else
1875 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001876 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001877 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001878 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCallb268a282010-08-23 23:25:46 +00001879 FinallyBody.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001880 catch_or_finally_seen = true;
1881 break;
1882 }
1883 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001884 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001885 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001886 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001887 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001888
John McCallb268a282010-08-23 23:25:46 +00001889 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor96c79492010-04-23 22:50:49 +00001890 move_arg(CatchStmts),
John McCallb268a282010-08-23 23:25:46 +00001891 FinallyStmt.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001892}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001893
John McCall31168b02011-06-15 23:02:42 +00001894/// objc-autoreleasepool-statement:
1895/// @autoreleasepool compound-statement
1896///
1897StmtResult
1898Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1899 ConsumeToken(); // consume autoreleasepool
1900 if (Tok.isNot(tok::l_brace)) {
1901 Diag(Tok, diag::err_expected_lbrace);
1902 return StmtError();
1903 }
1904 // Enter a scope to hold everything within the compound stmt. Compound
1905 // statements can always hold declarations.
1906 ParseScope BodyScope(this, Scope::DeclScope);
1907
1908 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1909
1910 BodyScope.Exit();
1911 if (AutoreleasePoolBody.isInvalid())
1912 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1913 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1914 AutoreleasePoolBody.take());
1915}
1916
Steve Naroff09bf8152007-09-06 21:24:23 +00001917/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001918///
John McCall48871652010-08-21 09:40:31 +00001919Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001920 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00001921
John McCallfaf5fb42010-08-26 23:41:50 +00001922 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1923 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001924
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001925 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001926 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001927 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001928 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00001929 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001930 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001931 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001932 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001933
Steve Naroffbb875722007-11-11 19:54:21 +00001934 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001935 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001936 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001937
Steve Naroffbb875722007-11-11 19:54:21 +00001938 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1939 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001940
Steve Naroffbb875722007-11-11 19:54:21 +00001941 // If we didn't find the '{', bail out.
1942 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +00001943 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001944 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001945
1946 if (!MDecl) {
1947 ConsumeBrace();
1948 SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
1949 return 0;
1950 }
1951
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001952 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001953 Actions.AddAnyMethodToGlobalPool(MDecl);
1954
1955 if (CurParsedObjCImpl) {
1956 // Consume the tokens and store them for later parsing.
1957 LexedMethod* LM = new LexedMethod(this, MDecl);
1958 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
1959 CachedTokens &Toks = LM->Toks;
1960 // Begin by storing the '{' token.
1961 Toks.push_back(Tok);
1962 ConsumeBrace();
1963 // Consume everything up to (and including) the matching right brace.
1964 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1965
1966 } else {
1967 ConsumeBrace();
1968 SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
1969 }
1970
Steve Naroff7b8fa472007-11-13 23:01:27 +00001971 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001972}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001973
John McCalldadc5752010-08-24 06:29:42 +00001974StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001975 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001976 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001977 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001978 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001979 }
1980
1981 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001982 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001983
1984 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001985 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001986
1987 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001988 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00001989
1990 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1991 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001992
John McCalldadc5752010-08-24 06:29:42 +00001993 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001994 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001995 // If the expression is invalid, skip ahead to the next semicolon. Not
1996 // doing this opens us up to the possibility of infinite loops if
1997 // ParseExpression does not consume any tokens.
1998 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001999 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002000 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002001
Steve Naroffe6016792008-02-05 21:27:35 +00002002 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002003 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +00002004 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroffe6016792008-02-05 21:27:35 +00002005}
2006
John McCalldadc5752010-08-24 06:29:42 +00002007ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002008 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002009 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002010 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002011 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002012 return ExprError();
2013
Ted Kremeneke65b0862012-03-06 20:05:56 +00002014 case tok::minus:
2015 case tok::plus: {
2016 tok::TokenKind Kind = Tok.getKind();
2017 SourceLocation OpLoc = ConsumeToken();
2018
2019 if (!Tok.is(tok::numeric_constant)) {
2020 const char *Symbol = 0;
2021 switch (Kind) {
2022 case tok::minus: Symbol = "-"; break;
2023 case tok::plus: Symbol = "+"; break;
2024 default: llvm_unreachable("missing unary operator case");
2025 }
2026 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2027 << Symbol;
2028 return ExprError();
2029 }
2030
2031 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2032 if (Lit.isInvalid()) {
2033 return move(Lit);
2034 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002035 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002036
2037 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.take());
2038 if (Lit.isInvalid())
2039 return move(Lit);
2040
2041 return ParsePostfixExpressionSuffix(
2042 Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2043 }
2044
Chris Lattnere002fbe2007-12-12 01:04:12 +00002045 case tok::string_literal: // primary-expression: string-literal
2046 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002047 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002048
2049 case tok::char_constant:
2050 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2051
2052 case tok::numeric_constant:
2053 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2054
2055 case tok::kw_true: // Objective-C++, etc.
2056 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2057 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2058 case tok::kw_false: // Objective-C++, etc.
2059 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2060 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2061
2062 case tok::l_square:
2063 // Objective-C array literal
2064 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2065
2066 case tok::l_brace:
2067 // Objective-C dictionary literal
2068 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2069
Patrick Beard0caa3942012-04-19 00:25:12 +00002070 case tok::l_paren:
2071 // Objective-C boxed expression
2072 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2073
Chris Lattnere002fbe2007-12-12 01:04:12 +00002074 default:
Chris Lattner197a3012008-08-05 06:19:09 +00002075 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002076 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002077
Chris Lattner197a3012008-08-05 06:19:09 +00002078 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2079 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002080 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002081 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002082 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002083 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002084 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002085 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002086 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00002087 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002088 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002089}
2090
Douglas Gregor8d4de672010-04-21 22:36:40 +00002091/// \brirg Parse the receiver of an Objective-C++ message send.
2092///
2093/// This routine parses the receiver of a message send in
2094/// Objective-C++ either as a type or as an expression. Note that this
2095/// routine must not be called to parse a send to 'super', since it
2096/// has no way to return such a result.
2097///
2098/// \param IsExpr Whether the receiver was parsed as an expression.
2099///
2100/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2101/// IsExpr is true), the parsed expression. If the receiver was parsed
2102/// as a type (\c IsExpr is false), the parsed type.
2103///
2104/// \returns True if an error occurred during parsing or semantic
2105/// analysis, in which case the arguments do not have valid
2106/// values. Otherwise, returns false for a successful parse.
2107///
2108/// objc-receiver: [C++]
2109/// 'super' [not parsed here]
2110/// expression
2111/// simple-type-specifier
2112/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002113bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002114 InMessageExpressionRAIIObject InMessage(*this, true);
2115
Douglas Gregor8d4de672010-04-21 22:36:40 +00002116 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2117 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2118 TryAnnotateTypeOrScopeToken();
2119
2120 if (!isCXXSimpleTypeSpecifier()) {
2121 // objc-receiver:
2122 // expression
John McCalldadc5752010-08-24 06:29:42 +00002123 ExprResult Receiver = ParseExpression();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002124 if (Receiver.isInvalid())
2125 return true;
2126
2127 IsExpr = true;
2128 TypeOrExpr = Receiver.take();
2129 return false;
2130 }
2131
2132 // objc-receiver:
2133 // typename-specifier
2134 // simple-type-specifier
2135 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002136 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002137 ParseCXXSimpleTypeSpecifier(DS);
2138
2139 if (Tok.is(tok::l_paren)) {
2140 // If we see an opening parentheses at this point, we are
2141 // actually parsing an expression that starts with a
2142 // function-style cast, e.g.,
2143 //
2144 // postfix-expression:
2145 // simple-type-specifier ( expression-list [opt] )
2146 // typename-specifier ( expression-list [opt] )
2147 //
2148 // Parse the remainder of this case, then the (optional)
2149 // postfix-expression suffix, followed by the (optional)
2150 // right-hand side of the binary expression. We have an
2151 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002152 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002153 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002154 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002155 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002156 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002157 if (Receiver.isInvalid())
2158 return true;
2159
2160 IsExpr = true;
2161 TypeOrExpr = Receiver.take();
2162 return false;
2163 }
2164
2165 // We have a class message. Turn the simple-type-specifier or
2166 // typename-specifier we parsed into a type and parse the
2167 // remainder of the class message.
2168 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002169 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002170 if (Type.isInvalid())
2171 return true;
2172
2173 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002174 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002175 return false;
2176}
2177
Douglas Gregor990ccac2010-05-31 14:40:22 +00002178/// \brief Determine whether the parser is currently referring to a an
2179/// Objective-C message send, using a simplified heuristic to avoid overhead.
2180///
2181/// This routine will only return true for a subset of valid message-send
2182/// expressions.
2183bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002184 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002185 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002186 return GetLookAheadToken(1).is(tok::identifier) &&
2187 GetLookAheadToken(2).is(tok::identifier);
2188}
2189
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002190bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002191 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002192 InMessageExpression)
2193 return false;
2194
2195
2196 ParsedType Type;
2197
2198 if (Tok.is(tok::annot_typename))
2199 Type = getTypeAnnotation(Tok);
2200 else if (Tok.is(tok::identifier))
2201 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2202 getCurScope());
2203 else
2204 return false;
2205
2206 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2207 const Token &AfterNext = GetLookAheadToken(2);
2208 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2209 if (Tok.is(tok::identifier))
2210 TryAnnotateTypeOrScopeToken();
2211
2212 return Tok.is(tok::annot_typename);
2213 }
2214 }
2215
2216 return false;
2217}
2218
Mike Stump11289f42009-09-09 15:08:12 +00002219/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002220/// '[' objc-receiver objc-message-args ']'
2221///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002222/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002223/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002224/// expression
2225/// class-name
2226/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002227///
John McCalldadc5752010-08-24 06:29:42 +00002228ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002229 assert(Tok.is(tok::l_square) && "'[' expected");
2230 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2231
Douglas Gregora817a192010-05-27 23:06:34 +00002232 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002233 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002234 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002235 return ExprError();
2236 }
2237
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002238 InMessageExpressionRAIIObject InMessage(*this, true);
2239
David Blaikiebbafb8a2012-03-11 07:00:24 +00002240 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002241 // We completely separate the C and C++ cases because C++ requires
2242 // more complicated (read: slower) parsing.
2243
2244 // Handle send to super.
2245 // FIXME: This doesn't benefit from the same typo-correction we
2246 // get in Objective-C.
2247 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002248 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002249 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2250 ParsedType(), 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002251
2252 // Parse the receiver, which is either a type or an expression.
2253 bool IsExpr;
Nick Lewycky970aed92010-09-15 18:35:19 +00002254 void *TypeOrExpr = NULL;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002255 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2256 SkipUntil(tok::r_square);
2257 return ExprError();
2258 }
2259
2260 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002261 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2262 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002263 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002264
2265 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002266 ParsedType::getFromOpaquePtr(TypeOrExpr),
2267 0);
Chris Lattner47054fb2010-05-31 18:18:22 +00002268 }
2269
2270 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002271 IdentifierInfo *Name = Tok.getIdentifierInfo();
2272 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002273 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002274 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002275 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002276 NextToken().is(tok::period),
2277 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002278 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002279 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2280 ParsedType(), 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002281
John McCallfaf5fb42010-08-26 23:41:50 +00002282 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002283 if (!ReceiverType) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002284 SkipUntil(tok::r_square);
2285 return ExprError();
2286 }
2287
Douglas Gregore5798dc2010-04-21 20:38:13 +00002288 ConsumeToken(); // the type name
2289
2290 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb268a282010-08-23 23:25:46 +00002291 ReceiverType, 0);
Douglas Gregora148a1d2010-04-14 02:22:16 +00002292
John McCallfaf5fb42010-08-26 23:41:50 +00002293 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002294 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002295 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002296 }
Chris Lattner8f697062008-01-25 18:59:06 +00002297 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002298
2299 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCalldadc5752010-08-24 06:29:42 +00002300 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002301 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00002302 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002303 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00002304 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002305
John McCallba7bf592010-08-24 05:47:05 +00002306 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2307 ParsedType(), Res.take());
Chris Lattner8f697062008-01-25 18:59:06 +00002308}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002309
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002310/// \brief Parse the remainder of an Objective-C message following the
2311/// '[' objc-receiver.
2312///
2313/// This routine handles sends to super, class messages (sent to a
2314/// class name), and instance messages (sent to an object), and the
2315/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2316/// ReceiverExpr, respectively. Only one of these parameters may have
2317/// a valid value.
2318///
2319/// \param LBracLoc The location of the opening '['.
2320///
2321/// \param SuperLoc If this is a send to 'super', the location of the
2322/// 'super' keyword that indicates a send to the superclass.
2323///
2324/// \param ReceiverType If this is a class message, the type of the
2325/// class we are sending a message to.
2326///
2327/// \param ReceiverExpr If this is an instance message, the expression
2328/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002329///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002330/// objc-message-args:
2331/// objc-selector
2332/// objc-keywordarg-list
2333///
2334/// objc-keywordarg-list:
2335/// objc-keywordarg
2336/// objc-keywordarg-list objc-keywordarg
2337///
Mike Stump11289f42009-09-09 15:08:12 +00002338/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002339/// selector-name[opt] ':' objc-keywordexpr
2340///
2341/// objc-keywordexpr:
2342/// nonempty-expr-list
2343///
2344/// nonempty-expr-list:
2345/// assignment-expression
2346/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002347///
John McCalldadc5752010-08-24 06:29:42 +00002348ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002349Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002350 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002351 ParsedType ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002352 ExprArg ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002353 InMessageExpressionRAIIObject InMessage(*this, true);
2354
Steve Naroffeae65032009-11-07 02:08:14 +00002355 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002356 if (SuperLoc.isValid())
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002357 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2358 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002359 else if (ReceiverType)
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002360 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2361 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002362 else
John McCallb268a282010-08-23 23:25:46 +00002363 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002364 0, 0, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002365 cutOffParsing();
2366 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002367 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002368
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002369 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002370 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002371 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00002372
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002373 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002374 SmallVector<SourceLocation, 12> KeyLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00002375 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00002376
Chris Lattner0ef13522007-10-09 17:51:17 +00002377 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002378 while (1) {
2379 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002380 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002381 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00002382
Chris Lattner0ef13522007-10-09 17:51:17 +00002383 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002384 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00002385 // We must manually skip to a ']', otherwise the expression skipper will
2386 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2387 // the enclosing expression.
2388 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002389 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002390 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002391
Steve Narofff73590d2007-09-27 14:38:14 +00002392 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00002393 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002394
2395 if (Tok.is(tok::code_completion)) {
2396 if (SuperLoc.isValid())
2397 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2398 KeyIdents.data(),
2399 KeyIdents.size(),
2400 /*AtArgumentEpression=*/true);
2401 else if (ReceiverType)
2402 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2403 KeyIdents.data(),
2404 KeyIdents.size(),
2405 /*AtArgumentEpression=*/true);
2406 else
2407 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2408 KeyIdents.data(),
2409 KeyIdents.size(),
2410 /*AtArgumentEpression=*/true);
2411
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002412 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002413 return ExprError();
2414 }
2415
John McCalldadc5752010-08-24 06:29:42 +00002416 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002417 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002418 // We must manually skip to a ']', otherwise the expression skipper will
2419 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2420 // the enclosing expression.
2421 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002422 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00002423 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002424
Steve Naroff486760a2007-09-17 20:25:27 +00002425 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002426 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002427
Douglas Gregor1b605f72009-11-19 01:08:35 +00002428 // Code completion after each argument.
2429 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002430 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002431 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002432 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002433 KeyIdents.size(),
2434 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002435 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002436 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002437 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002438 KeyIdents.size(),
2439 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002440 else
John McCallb268a282010-08-23 23:25:46 +00002441 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002442 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002443 KeyIdents.size(),
2444 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002445 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002446 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002447 }
2448
Steve Naroff486760a2007-09-17 20:25:27 +00002449 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002450 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002451 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002452 break;
2453 // We have a selector or a colon, continue parsing.
2454 }
2455 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002456 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002457 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002458 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002459 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002460 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002461 // We must manually skip to a ']', otherwise the expression skipper will
2462 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2463 // the enclosing expression.
2464 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002465 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002466 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002467
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002468 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002469 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002470 }
2471 } else if (!selIdent) {
2472 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002473
Chris Lattner197a3012008-08-05 06:19:09 +00002474 // We must manually skip to a ']', otherwise the expression skipper will
2475 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2476 // the enclosing expression.
2477 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002478 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002479 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002480
Chris Lattner0ef13522007-10-09 17:51:17 +00002481 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002482 if (Tok.is(tok::identifier))
2483 Diag(Tok, diag::err_expected_colon);
2484 else
2485 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00002486 // We must manually skip to a ']', otherwise the expression skipper will
2487 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2488 // the enclosing expression.
2489 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002490 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002491 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002492
Chris Lattner8f697062008-01-25 18:59:06 +00002493 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002494
Steve Naroffe61bfa82007-10-05 18:42:47 +00002495 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002496 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00002497 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002498 KeyLocs.push_back(Loc);
2499 }
Chris Lattner5700fab2007-10-07 02:00:24 +00002500 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002501
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002502 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002503 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002504 LBracLoc, KeyLocs, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002505 MultiExprArg(Actions,
2506 KeyExprs.take(),
2507 KeyExprs.size()));
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002508 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002509 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002510 LBracLoc, KeyLocs, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002511 MultiExprArg(Actions,
2512 KeyExprs.take(),
2513 KeyExprs.size()));
John McCallb268a282010-08-23 23:25:46 +00002514 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002515 LBracLoc, KeyLocs, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002516 MultiExprArg(Actions,
2517 KeyExprs.take(),
2518 KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002519}
2520
John McCalldadc5752010-08-24 06:29:42 +00002521ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2522 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002523 if (Res.isInvalid()) return move(Res);
2524
Chris Lattnere002fbe2007-12-12 01:04:12 +00002525 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2526 // expressions. At this point, we know that the only valid thing that starts
2527 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002528 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00002529 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00002530 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002531 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002532
Chris Lattnere002fbe2007-12-12 01:04:12 +00002533 while (Tok.is(tok::at)) {
2534 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002535
Sebastian Redlc13f2682008-12-09 20:22:58 +00002536 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002537 if (!isTokenStringLiteral())
2538 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002539
John McCalldadc5752010-08-24 06:29:42 +00002540 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002541 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002542 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002543
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002544 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002545 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002546
2547 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2548 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00002549}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002550
Ted Kremeneke65b0862012-03-06 20:05:56 +00002551/// ParseObjCBooleanLiteral -
2552/// objc-scalar-literal : '@' boolean-keyword
2553/// ;
2554/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2555/// ;
2556ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2557 bool ArgValue) {
2558 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2559 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2560}
2561
2562/// ParseObjCCharacterLiteral -
2563/// objc-scalar-literal : '@' character-literal
2564/// ;
2565ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2566 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2567 if (Lit.isInvalid()) {
2568 return move(Lit);
2569 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002570 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002571 return Owned(Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2572}
2573
2574/// ParseObjCNumericLiteral -
2575/// objc-scalar-literal : '@' scalar-literal
2576/// ;
2577/// scalar-literal : | numeric-constant /* any numeric constant. */
2578/// ;
2579ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2580 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2581 if (Lit.isInvalid()) {
2582 return move(Lit);
2583 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002584 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002585 return Owned(Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2586}
2587
Patrick Beard0caa3942012-04-19 00:25:12 +00002588/// ParseObjCBoxedExpr -
2589/// objc-box-expression:
2590/// @( assignment-expression )
2591ExprResult
2592Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2593 if (Tok.isNot(tok::l_paren))
2594 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2595
2596 BalancedDelimiterTracker T(*this, tok::l_paren);
2597 T.consumeOpen();
2598 ExprResult ValueExpr(ParseAssignmentExpression());
2599 if (T.consumeClose())
2600 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00002601
2602 if (ValueExpr.isInvalid())
2603 return ExprError();
2604
Patrick Beard0caa3942012-04-19 00:25:12 +00002605 // Wrap the sub-expression in a parenthesized expression, to distinguish
2606 // a boxed expression from a literal.
2607 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
2608 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.take());
2609 return Owned(Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
2610 ValueExpr.take()));
2611}
2612
Ted Kremeneke65b0862012-03-06 20:05:56 +00002613ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
2614 ExprVector ElementExprs(Actions); // array elements.
2615 ConsumeBracket(); // consume the l_square.
2616
2617 while (Tok.isNot(tok::r_square)) {
2618 // Parse list of array element expressions (all must be id types).
2619 ExprResult Res(ParseAssignmentExpression());
2620 if (Res.isInvalid()) {
2621 // We must manually skip to a ']', otherwise the expression skipper will
2622 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2623 // the enclosing expression.
2624 SkipUntil(tok::r_square);
2625 return move(Res);
2626 }
2627
2628 // Parse the ellipsis that indicates a pack expansion.
2629 if (Tok.is(tok::ellipsis))
2630 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2631 if (Res.isInvalid())
2632 return true;
2633
2634 ElementExprs.push_back(Res.release());
2635
2636 if (Tok.is(tok::comma))
2637 ConsumeToken(); // Eat the ','.
2638 else if (Tok.isNot(tok::r_square))
2639 return ExprError(Diag(Tok, diag::err_expected_rsquare_or_comma));
2640 }
2641 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
2642 MultiExprArg Args(Actions, ElementExprs.take(), ElementExprs.size());
2643 return Owned(Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args));
2644}
2645
2646ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2647 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2648 ConsumeBrace(); // consume the l_square.
2649 while (Tok.isNot(tok::r_brace)) {
2650 // Parse the comma separated key : value expressions.
2651 ExprResult KeyExpr;
2652 {
2653 ColonProtectionRAIIObject X(*this);
2654 KeyExpr = ParseAssignmentExpression();
2655 if (KeyExpr.isInvalid()) {
2656 // We must manually skip to a '}', otherwise the expression skipper will
2657 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2658 // the enclosing expression.
2659 SkipUntil(tok::r_brace);
2660 return move(KeyExpr);
2661 }
2662 }
2663
2664 if (Tok.is(tok::colon)) {
2665 ConsumeToken();
2666 } else {
2667 return ExprError(Diag(Tok, diag::err_expected_colon));
2668 }
2669
2670 ExprResult ValueExpr(ParseAssignmentExpression());
2671 if (ValueExpr.isInvalid()) {
2672 // We must manually skip to a '}', otherwise the expression skipper will
2673 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2674 // the enclosing expression.
2675 SkipUntil(tok::r_brace);
2676 return move(ValueExpr);
2677 }
2678
2679 // Parse the ellipsis that designates this as a pack expansion.
2680 SourceLocation EllipsisLoc;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002681 if (Tok.is(tok::ellipsis) && getLangOpts().CPlusPlus)
Ted Kremeneke65b0862012-03-06 20:05:56 +00002682 EllipsisLoc = ConsumeToken();
2683
2684 // We have a valid expression. Collect it in a vector so we can
2685 // build the argument list.
2686 ObjCDictionaryElement Element = {
2687 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, llvm::Optional<unsigned>()
2688 };
2689 Elements.push_back(Element);
2690
2691 if (Tok.is(tok::comma))
2692 ConsumeToken(); // Eat the ','.
2693 else if (Tok.isNot(tok::r_brace))
2694 return ExprError(Diag(Tok, diag::err_expected_rbrace_or_comma));
2695 }
2696 SourceLocation EndLoc = ConsumeBrace();
2697
2698 // Create the ObjCDictionaryLiteral.
2699 return Owned(Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2700 Elements.data(),
2701 Elements.size()));
2702}
2703
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002704/// objc-encode-expression:
2705/// @encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002706ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002707Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002708 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002709
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002710 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002711
Chris Lattner197a3012008-08-05 06:19:09 +00002712 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002713 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2714
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002715 BalancedDelimiterTracker T(*this, tok::l_paren);
2716 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002717
Douglas Gregor220cac52009-02-18 17:45:20 +00002718 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002719
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002720 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002721
Douglas Gregor220cac52009-02-18 17:45:20 +00002722 if (Ty.isInvalid())
2723 return ExprError();
2724
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002725 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
2726 T.getOpenLocation(), Ty.get(),
2727 T.getCloseLocation()));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002728}
Anders Carlssone01493d2007-08-23 15:25:28 +00002729
2730/// objc-protocol-expression
2731/// @protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002732ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002733Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002734 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002735
Chris Lattner197a3012008-08-05 06:19:09 +00002736 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002737 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2738
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002739 BalancedDelimiterTracker T(*this, tok::l_paren);
2740 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002741
Chris Lattner197a3012008-08-05 06:19:09 +00002742 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002743 return ExprError(Diag(Tok, diag::err_expected_ident));
2744
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002745 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00002746 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002747
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002748 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00002749
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002750 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002751 T.getOpenLocation(),
2752 T.getCloseLocation()));
Anders Carlssone01493d2007-08-23 15:25:28 +00002753}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002754
2755/// objc-selector-expression
2756/// @selector '(' objc-keyword-selector ')'
John McCalldadc5752010-08-24 06:29:42 +00002757ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002758 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002759
Chris Lattner197a3012008-08-05 06:19:09 +00002760 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002761 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2762
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002763 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002764 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002765
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002766 BalancedDelimiterTracker T(*this, tok::l_paren);
2767 T.consumeOpen();
2768
Douglas Gregor67c692c2010-08-26 15:07:07 +00002769 if (Tok.is(tok::code_completion)) {
2770 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2771 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002772 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002773 return ExprError();
2774 }
2775
Chris Lattner4f472a32009-04-11 18:13:45 +00002776 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002777 if (!SelIdent && // missing selector name.
2778 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002779 return ExprError(Diag(Tok, diag::err_expected_ident));
2780
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002781 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002782 unsigned nColons = 0;
2783 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002784 while (1) {
Chris Lattner1ba64452010-08-27 22:32:41 +00002785 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2786 ++nColons;
2787 KeyIdents.push_back(0);
2788 } else if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002789 return ExprError(Diag(Tok, diag::err_expected_colon));
2790
Chris Lattner1ba64452010-08-27 22:32:41 +00002791 ++nColons;
Chris Lattner85222c62011-03-26 18:11:38 +00002792 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002793 if (Tok.is(tok::r_paren))
2794 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002795
2796 if (Tok.is(tok::code_completion)) {
2797 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2798 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002799 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002800 return ExprError();
2801 }
2802
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002803 // Check for another keyword selector.
2804 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002805 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002806 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00002807 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002808 break;
2809 }
Steve Naroff152dd812007-12-05 22:21:29 +00002810 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002811 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00002812 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002813 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002814 T.getOpenLocation(),
2815 T.getCloseLocation()));
Gabor Greif24032f12007-10-19 15:38:32 +00002816 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002817
2818Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002819
2820 // Save the current token position.
2821 SourceLocation OrigLoc = Tok.getLocation();
2822
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002823 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2824 // Append the current token at the end of the new token stream so that it
2825 // doesn't get lost.
2826 LM.Toks.push_back(Tok);
2827 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2828
2829 // MDecl might be null due to error in method prototype, etc.
2830 Decl *MDecl = LM.D;
2831 // Consume the previously pushed token.
2832 ConsumeAnyToken();
2833
2834 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2835 SourceLocation BraceLoc = Tok.getLocation();
2836 // Enter a scope for the method body.
2837 ParseScope BodyScope(this,
2838 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2839
2840 // Tell the actions module that we have entered a method definition with the
2841 // specified Declarator for the method.
2842 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2843
Erik Verbruggen6e922512012-04-12 10:11:59 +00002844 if (SkipFunctionBodies && trySkippingFunctionBody()) {
2845 BodyScope.Exit();
2846 return Actions.ActOnFinishFunctionBody(MDecl, 0);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002847 }
2848
2849 StmtResult FnBody(ParseCompoundStatementBody());
2850
2851 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002852 if (FnBody.isInvalid()) {
2853 Sema::CompoundScopeRAII CompoundScope(Actions);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002854 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2855 MultiStmtArg(Actions), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002856 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002857
2858 // Leave the function body scope.
2859 BodyScope.Exit();
2860
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002861 MDecl = Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2862
2863 if (Tok.getLocation() != OrigLoc) {
2864 // Due to parsing error, we either went over the cached tokens or
2865 // there are still cached tokens left. If it's the latter case skip the
2866 // leftover tokens.
2867 // Since this is an uncommon situation that should be avoided, use the
2868 // expensive isBeforeInTranslationUnit call.
2869 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2870 OrigLoc))
2871 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2872 ConsumeAnyToken();
2873 }
2874
2875 return MDecl;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002876}