blob: 971c7d4887fb09a010937e9f3b75773236d5a4c7 [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:
Douglas Gregor0bf886d2012-01-03 18:24:14 +000068 if (getLang().Modules)
69 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
164///
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000165Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000166 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000167 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000168 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000169 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000170 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000171
Douglas Gregor49c22a72009-11-18 16:26:39 +0000172 // Code completion after '@interface'.
173 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000174 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000175 cutOffParsing();
176 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000177 }
178
Chris Lattner0ef13522007-10-09 17:51:17 +0000179 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000180 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +0000181 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000182 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000183
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000184 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000185 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000186 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000187 if (Tok.is(tok::l_paren) &&
188 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000189
190 BalancedDelimiterTracker T(*this, tok::l_paren);
191 T.consumeOpen();
192
193 SourceLocation categoryLoc;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000194 IdentifierInfo *categoryId = 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000195 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000196 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000197 cutOffParsing();
198 return 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000199 }
200
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000201 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000202 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000203 categoryId = Tok.getIdentifierInfo();
204 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000205 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000206 else if (!getLang().ObjC2) {
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000207 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +0000208 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000209 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000210
211 T.consumeClose();
212 if (T.getCloseLocation().isInvalid())
John McCall48871652010-08-21 09:40:31 +0000213 return 0;
Douglas Gregor0c254a02011-09-23 19:19:41 +0000214
215 if (!attrs.empty()) { // categories don't support attributes.
216 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
217 attrs.clear();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000218 }
Douglas Gregor0c254a02011-09-23 19:19:41 +0000219
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000220 // Next, we need to check for any protocol references.
221 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000222 SmallVector<Decl *, 8> ProtocolRefs;
223 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000224 if (Tok.is(tok::less) &&
225 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000226 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000227 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000228
John McCall48871652010-08-21 09:40:31 +0000229 Decl *CategoryType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000230 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000231 nameId, nameLoc,
232 categoryId, categoryLoc,
233 ProtocolRefs.data(),
234 ProtocolRefs.size(),
235 ProtocolLocs.data(),
236 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000237
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000238 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000239 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000240
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000241 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000242 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000243 }
244 // Parse a class interface.
245 IdentifierInfo *superClassId = 0;
246 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000247
Chris Lattner0ef13522007-10-09 17:51:17 +0000248 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000249 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000250
251 // Code completion of superclass names.
252 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000253 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000254 cutOffParsing();
255 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000256 }
257
Chris Lattner0ef13522007-10-09 17:51:17 +0000258 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000259 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +0000260 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000261 }
262 superClassId = Tok.getIdentifierInfo();
263 superClassLoc = ConsumeToken();
264 }
265 // Next, we need to check for any protocol references.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000266 SmallVector<Decl *, 8> ProtocolRefs;
267 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000268 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000269 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000270 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
271 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000272 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000273
John McCall48871652010-08-21 09:40:31 +0000274 Decl *ClsType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000275 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000276 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000277 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000278 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000279 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000280
Chris Lattner0ef13522007-10-09 17:51:17 +0000281 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000282 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000283
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000284 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000285 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000286}
287
John McCall064d77b2009-12-03 22:31:13 +0000288/// The Objective-C property callback. This should be defined where
289/// it's used, but instead it's been lifted to here to support VS2005.
290struct Parser::ObjCPropertyCallback : FieldCallback {
David Blaikie68e081d2011-12-20 02:48:34 +0000291private:
292 virtual void anchor();
293public:
John McCall064d77b2009-12-03 22:31:13 +0000294 Parser &P;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000295 SmallVectorImpl<Decl *> &Props;
John McCall064d77b2009-12-03 22:31:13 +0000296 ObjCDeclSpec &OCDS;
297 SourceLocation AtLoc;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000298 SourceLocation LParenLoc;
John McCall064d77b2009-12-03 22:31:13 +0000299 tok::ObjCKeywordKind MethodImplKind;
300
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000301 ObjCPropertyCallback(Parser &P,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000302 SmallVectorImpl<Decl *> &Props,
John McCall064d77b2009-12-03 22:31:13 +0000303 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000304 SourceLocation LParenLoc,
John McCall064d77b2009-12-03 22:31:13 +0000305 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000306 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc), LParenLoc(LParenLoc),
John McCall064d77b2009-12-03 22:31:13 +0000307 MethodImplKind(MethodImplKind) {
308 }
309
John McCall48871652010-08-21 09:40:31 +0000310 Decl *invoke(FieldDeclarator &FD) {
John McCall064d77b2009-12-03 22:31:13 +0000311 if (FD.D.getIdentifier() == 0) {
312 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
313 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000314 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000315 }
316 if (FD.BitfieldSize) {
317 P.Diag(AtLoc, diag::err_objc_property_bitfield)
318 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000319 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000320 }
321
322 // Install the property declarator into interfaceDecl.
323 IdentifierInfo *SelName =
324 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
325
326 Selector GetterSel =
327 P.PP.getSelectorTable().getNullarySelector(SelName);
328 IdentifierInfo *SetterName = OCDS.getSetterName();
329 Selector SetterSel;
330 if (SetterName)
331 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
332 else
333 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
334 P.PP.getSelectorTable(),
335 FD.D.getIdentifier());
336 bool isOverridingProperty = false;
John McCall48871652010-08-21 09:40:31 +0000337 Decl *Property =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000338 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, LParenLoc,
339 FD, OCDS,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000340 GetterSel, SetterSel,
John McCall064d77b2009-12-03 22:31:13 +0000341 &isOverridingProperty,
342 MethodImplKind);
343 if (!isOverridingProperty)
344 Props.push_back(Property);
345
346 return Property;
347 }
348};
349
David Blaikie68e081d2011-12-20 02:48:34 +0000350void Parser::ObjCPropertyCallback::anchor() {
351}
352
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000353/// objc-interface-decl-list:
354/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000355/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000356/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000357/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000358/// objc-interface-decl-list declaration
359/// objc-interface-decl-list ';'
360///
Steve Naroff99264b42007-08-22 16:35:03 +0000361/// objc-method-requirement: [OBJC2]
362/// @required
363/// @optional
364///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000365void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
366 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000367 SmallVector<Decl *, 32> allMethods;
368 SmallVector<Decl *, 16> allProperties;
369 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000370 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000371
Ted Kremenekc7c64312010-01-07 01:20:12 +0000372 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000373
Steve Naroff99264b42007-08-22 16:35:03 +0000374 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000375 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000376 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCall48871652010-08-21 09:40:31 +0000377 Decl *methodPrototype =
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000378 ParseObjCMethodPrototype(MethodImplKind, false);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000379 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000380 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
381 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000382 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
383 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
384 SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
385 if (Tok.is(tok::semi))
386 ConsumeToken();
387 }
Steve Naroff99264b42007-08-22 16:35:03 +0000388 continue;
389 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000390 if (Tok.is(tok::l_paren)) {
391 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000392 ParseObjCMethodDecl(Tok.getLocation(),
393 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000394 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000395 continue;
396 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000397 // Ignore excess semicolons.
398 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000399 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000400 continue;
401 }
Mike Stump11289f42009-09-09 15:08:12 +0000402
Chris Lattnerda9fb152008-10-20 06:10:06 +0000403 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000404 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000405 break;
Mike Stump11289f42009-09-09 15:08:12 +0000406
Douglas Gregorf1934162010-01-13 21:24:21 +0000407 // Code completion within an Objective-C interface.
408 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000409 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000410 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000411 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000412 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000413 }
414
Chris Lattner038a3e32008-10-20 05:46:22 +0000415 // If we don't have an @ directive, parse it as a function definition.
416 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000417 // The code below does not consume '}'s because it is afraid of eating the
418 // end of a namespace. Because of the way this code is structured, an
419 // erroneous r_brace would cause an infinite loop if not handled here.
420 if (Tok.is(tok::r_brace))
421 break;
John McCall084e83d2011-03-24 11:26:52 +0000422 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000423 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000424 continue;
425 }
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattner038a3e32008-10-20 05:46:22 +0000427 // Otherwise, we have an @ directive, eat the @.
428 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000429 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000430 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000431 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000432 }
433
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000434 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000435
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000436 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000437 AtEnd.setBegin(AtLoc);
438 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000439 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000440 } else if (DirectiveKind == tok::objc_not_keyword) {
441 Diag(Tok, diag::err_objc_unknown_at);
442 SkipUntil(tok::semi);
443 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000444 }
Mike Stump11289f42009-09-09 15:08:12 +0000445
Chris Lattnerda9fb152008-10-20 06:10:06 +0000446 // Eat the identifier.
447 ConsumeToken();
448
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000449 switch (DirectiveKind) {
450 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000451 // FIXME: If someone forgets an @end on a protocol, this loop will
452 // continue to eat up tons of stuff and spew lots of nonsense errors. It
453 // would probably be better to bail out if we saw an @class or @interface
454 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000455 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000456 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000457 SkipUntil(tok::r_brace, tok::at);
458 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000459
460 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000461 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000462 Diag(AtLoc, diag::err_objc_missing_end)
463 << FixItHint::CreateInsertion(AtLoc, "@end\n");
464 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
465 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000466 ConsumeToken();
467 break;
468
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000469 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000470 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000471 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000472 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000473 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000474 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000475 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000476 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000477 break;
Mike Stump11289f42009-09-09 15:08:12 +0000478
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000479 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000480 if (!getLang().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000481 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000482
Chris Lattner038a3e32008-10-20 05:46:22 +0000483 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000484 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000485 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000486 if (Tok.is(tok::l_paren)) {
487 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000488 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000489 }
Mike Stump11289f42009-09-09 15:08:12 +0000490
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000491 ObjCPropertyCallback Callback(*this, allProperties,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000492 OCDS, AtLoc, LParenLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000493
Chris Lattner038a3e32008-10-20 05:46:22 +0000494 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +0000495 DeclSpec DS(AttrFactory);
John McCallcfefb6d2009-11-03 02:38:08 +0000496 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000497
John McCall405988b2011-03-26 01:53:26 +0000498 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000499 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000500 }
Steve Naroff99264b42007-08-22 16:35:03 +0000501 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000502
503 // We break out of the big loop in two cases: when we see @end or when we see
504 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000505 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000506 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000507 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000508 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000509 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000510 } else {
511 Diag(Tok, diag::err_objc_missing_end)
512 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
513 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
514 << (int) Actions.getObjCContainerKind();
515 AtEnd.setBegin(Tok.getLocation());
516 AtEnd.setEnd(Tok.getLocation());
517 }
Mike Stump11289f42009-09-09 15:08:12 +0000518
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000519 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000520 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000521 Actions.ActOnAtEnd(getCurScope(), AtEnd,
Mike Stump11289f42009-09-09 15:08:12 +0000522 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000523 allProperties.data(), allProperties.size(),
524 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000525}
526
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000527/// Parse property attribute declarations.
528///
529/// property-attr-decl: '(' property-attrlist ')'
530/// property-attrlist:
531/// property-attribute
532/// property-attrlist ',' property-attribute
533/// property-attribute:
534/// getter '=' identifier
535/// setter '=' identifier ':'
536/// readonly
537/// readwrite
538/// assign
539/// retain
540/// copy
541/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000542/// atomic
543/// strong
544/// weak
545/// unsafe_unretained
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000546///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000547void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000548 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000549 BalancedDelimiterTracker T(*this, tok::l_paren);
550 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000551
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000552 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000553 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000554 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000555 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000556 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000557 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000558
Chris Lattner76619232008-10-20 07:22:18 +0000559 // If this is not an identifier at all, bail out early.
560 if (II == 0) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000561 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000562 return;
563 }
Mike Stump11289f42009-09-09 15:08:12 +0000564
Chris Lattner1db33542008-10-20 07:37:22 +0000565 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000566
Chris Lattner68e48682008-11-20 04:42:34 +0000567 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000568 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000569 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000570 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000571 else if (II->isStr("unsafe_unretained"))
572 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000573 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000574 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000575 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000576 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000577 else if (II->isStr("strong"))
578 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000579 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000580 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000581 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000582 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000583 else if (II->isStr("atomic"))
584 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000585 else if (II->isStr("weak"))
586 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000587 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000588 bool IsSetter = II->getNameStart()[0] == 's';
589
Chris Lattner825bca12008-10-20 07:39:53 +0000590 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000591 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
592 diag::err_objc_expected_equal_for_getter;
593
594 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000595 return;
Mike Stump11289f42009-09-09 15:08:12 +0000596
Douglas Gregorc8537c52009-11-19 07:41:15 +0000597 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000598 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000599 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000600 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000601 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000602 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000603 }
604
Anders Carlssonfe15a782010-10-02 17:45:21 +0000605
606 SourceLocation SelLoc;
607 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
608
609 if (!SelIdent) {
610 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
611 << IsSetter;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000612 SkipUntil(tok::r_paren);
613 return;
614 }
Mike Stump11289f42009-09-09 15:08:12 +0000615
Anders Carlssonfe15a782010-10-02 17:45:21 +0000616 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000617 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000618 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000619
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000620 if (ExpectAndConsume(tok::colon,
621 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000622 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000623 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000624 } else {
625 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000626 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000627 }
Chris Lattner825bca12008-10-20 07:39:53 +0000628 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000629 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000630 SkipUntil(tok::r_paren);
631 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000632 }
Mike Stump11289f42009-09-09 15:08:12 +0000633
Chris Lattner1db33542008-10-20 07:37:22 +0000634 if (Tok.isNot(tok::comma))
635 break;
Mike Stump11289f42009-09-09 15:08:12 +0000636
Chris Lattner1db33542008-10-20 07:37:22 +0000637 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000638 }
Mike Stump11289f42009-09-09 15:08:12 +0000639
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000640 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000641}
642
Steve Naroff09bf8152007-09-06 21:24:23 +0000643/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000644/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000645/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000646///
647/// objc-instance-method: '-'
648/// objc-class-method: '+'
649///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000650/// objc-method-attributes: [OBJC2]
651/// __attribute__((deprecated))
652///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000653Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000654 bool MethodDefinition) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000655 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000656
Mike Stump11289f42009-09-09 15:08:12 +0000657 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000658 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000659 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000660 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000661 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000662 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000663 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000664}
665
666/// objc-selector:
667/// identifier
668/// one of
669/// enum struct union if else while do for switch case default
670/// break continue return goto asm sizeof typeof __alignof
671/// unsigned long const short volatile signed restrict _Complex
672/// in out inout bycopy byref oneway int char float double void _Bool
673///
Chris Lattner4f472a32009-04-11 18:13:45 +0000674IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000675
Chris Lattner5700fab2007-10-07 02:00:24 +0000676 switch (Tok.getKind()) {
677 default:
678 return 0;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000679 case tok::ampamp:
680 case tok::ampequal:
681 case tok::amp:
682 case tok::pipe:
683 case tok::tilde:
684 case tok::exclaim:
685 case tok::exclaimequal:
686 case tok::pipepipe:
687 case tok::pipeequal:
688 case tok::caret:
689 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000690 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000691 if (isalpha(ThisTok[0])) {
692 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
693 Tok.setKind(tok::identifier);
694 SelectorLoc = ConsumeToken();
695 return II;
696 }
697 return 0;
698 }
699
Chris Lattner5700fab2007-10-07 02:00:24 +0000700 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000701 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000702 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000703 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000704 case tok::kw_break:
705 case tok::kw_case:
706 case tok::kw_catch:
707 case tok::kw_char:
708 case tok::kw_class:
709 case tok::kw_const:
710 case tok::kw_const_cast:
711 case tok::kw_continue:
712 case tok::kw_default:
713 case tok::kw_delete:
714 case tok::kw_do:
715 case tok::kw_double:
716 case tok::kw_dynamic_cast:
717 case tok::kw_else:
718 case tok::kw_enum:
719 case tok::kw_explicit:
720 case tok::kw_export:
721 case tok::kw_extern:
722 case tok::kw_false:
723 case tok::kw_float:
724 case tok::kw_for:
725 case tok::kw_friend:
726 case tok::kw_goto:
727 case tok::kw_if:
728 case tok::kw_inline:
729 case tok::kw_int:
730 case tok::kw_long:
731 case tok::kw_mutable:
732 case tok::kw_namespace:
733 case tok::kw_new:
734 case tok::kw_operator:
735 case tok::kw_private:
736 case tok::kw_protected:
737 case tok::kw_public:
738 case tok::kw_register:
739 case tok::kw_reinterpret_cast:
740 case tok::kw_restrict:
741 case tok::kw_return:
742 case tok::kw_short:
743 case tok::kw_signed:
744 case tok::kw_sizeof:
745 case tok::kw_static:
746 case tok::kw_static_cast:
747 case tok::kw_struct:
748 case tok::kw_switch:
749 case tok::kw_template:
750 case tok::kw_this:
751 case tok::kw_throw:
752 case tok::kw_true:
753 case tok::kw_try:
754 case tok::kw_typedef:
755 case tok::kw_typeid:
756 case tok::kw_typename:
757 case tok::kw_typeof:
758 case tok::kw_union:
759 case tok::kw_unsigned:
760 case tok::kw_using:
761 case tok::kw_virtual:
762 case tok::kw_void:
763 case tok::kw_volatile:
764 case tok::kw_wchar_t:
765 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000766 case tok::kw__Bool:
767 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000768 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000769 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000770 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000771 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000772 }
Steve Naroff99264b42007-08-22 16:35:03 +0000773}
774
Fariborz Jahanian83615522008-01-02 22:54:34 +0000775/// objc-for-collection-in: 'in'
776///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000777bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000778 // FIXME: May have to do additional look-ahead to only allow for
779 // valid tokens following an 'in'; such as an identifier, unary operators,
780 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000781 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000782 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000783}
784
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000785/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000786/// qualifier list and builds their bitmask representation in the input
787/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000788///
789/// objc-type-qualifiers:
790/// objc-type-qualifier
791/// objc-type-qualifiers objc-type-qualifier
792///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000793void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000794 Declarator::TheContext Context) {
795 assert(Context == Declarator::ObjCParameterContext ||
796 Context == Declarator::ObjCResultContext);
797
Chris Lattner61511e12007-12-12 06:56:32 +0000798 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000799 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000800 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +0000801 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000802 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +0000803 }
804
Chris Lattner5e530bc2007-12-27 19:57:00 +0000805 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000806 return;
Mike Stump11289f42009-09-09 15:08:12 +0000807
Chris Lattner61511e12007-12-12 06:56:32 +0000808 const IdentifierInfo *II = Tok.getIdentifierInfo();
809 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000810 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000811 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000812
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000813 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000814 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +0000815 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000816 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
817 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
818 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
819 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
820 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
821 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000822 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000823 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000824 ConsumeToken();
825 II = 0;
826 break;
827 }
Mike Stump11289f42009-09-09 15:08:12 +0000828
Chris Lattner61511e12007-12-12 06:56:32 +0000829 // If this wasn't a recognized qualifier, bail out.
830 if (II) return;
831 }
832}
833
John McCalla55902b2011-10-01 09:56:14 +0000834/// Take all the decl attributes out of the given list and add
835/// them to the given attribute set.
836static void takeDeclAttributes(ParsedAttributes &attrs,
837 AttributeList *list) {
838 while (list) {
839 AttributeList *cur = list;
840 list = cur->getNext();
841
842 if (!cur->isUsedAsTypeAttr()) {
843 // Clear out the next pointer. We're really completely
844 // destroying the internal invariants of the declarator here,
845 // but it doesn't matter because we're done with it.
846 cur->setNext(0);
847 attrs.add(cur);
848 }
849 }
850}
851
852/// takeDeclAttributes - Take all the decl attributes from the given
853/// declarator and add them to the given list.
854static void takeDeclAttributes(ParsedAttributes &attrs,
855 Declarator &D) {
856 // First, take ownership of all attributes.
857 attrs.getPool().takeAllFrom(D.getAttributePool());
858 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
859
860 // Now actually move the attributes over.
861 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
862 takeDeclAttributes(attrs, D.getAttributes());
863 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
864 takeDeclAttributes(attrs,
865 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
866}
867
Chris Lattner61511e12007-12-12 06:56:32 +0000868/// objc-type-name:
869/// '(' objc-type-qualifiers[opt] type-name ')'
870/// '(' objc-type-qualifiers[opt] ')'
871///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000872ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000873 Declarator::TheContext context,
874 ParsedAttributes *paramAttrs) {
875 assert(context == Declarator::ObjCParameterContext ||
876 context == Declarator::ObjCResultContext);
877 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
878
Chris Lattner0ef13522007-10-09 17:51:17 +0000879 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000880
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000881 BalancedDelimiterTracker T(*this, tok::l_paren);
882 T.consumeOpen();
883
Chris Lattner2ebb1782008-08-23 01:48:03 +0000884 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000885 ObjCDeclContextSwitch ObjCDC(*this);
886
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000887 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +0000888 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000889
John McCallba7bf592010-08-24 05:47:05 +0000890 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000891 if (isTypeSpecifierQualifier()) {
John McCalla55902b2011-10-01 09:56:14 +0000892 // Parse an abstract declarator.
893 DeclSpec declSpec(AttrFactory);
894 declSpec.setObjCQualifiers(&DS);
895 ParseSpecifierQualifierList(declSpec);
896 Declarator declarator(declSpec, context);
897 ParseDeclarator(declarator);
898
899 // If that's not invalid, extract a type.
900 if (!declarator.isInvalidType()) {
901 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
902 if (!type.isInvalid())
903 Ty = type.get();
904
905 // If we're parsing a parameter, steal all the decl attributes
906 // and add them to the decl spec.
907 if (context == Declarator::ObjCParameterContext)
908 takeDeclAttributes(*paramAttrs, declarator);
909 }
910 } else if (context == Declarator::ObjCResultContext &&
911 Tok.is(tok::identifier)) {
Douglas Gregorbab8a962011-09-08 01:46:34 +0000912 if (!Ident_instancetype)
913 Ident_instancetype = PP.getIdentifierInfo("instancetype");
914
915 if (Tok.getIdentifierInfo() == Ident_instancetype) {
916 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
917 ConsumeToken();
918 }
Douglas Gregor220cac52009-02-18 17:45:20 +0000919 }
Douglas Gregorbab8a962011-09-08 01:46:34 +0000920
Steve Naroff90255b42008-10-21 14:15:04 +0000921 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000922 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000923 else if (Tok.getLocation() == TypeStartLoc) {
924 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000925 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000926 SkipUntil(tok::r_paren);
927 } else {
928 // Otherwise, we found *something*, but didn't get a ')' in the right
929 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000930 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000931 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000932 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000933}
934
935/// objc-method-decl:
936/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000937/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000938/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000939/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000940///
941/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000942/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000943/// objc-keyword-selector objc-keyword-decl
944///
945/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000946/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
947/// objc-selector ':' objc-keyword-attributes[opt] identifier
948/// ':' objc-type-name objc-keyword-attributes[opt] identifier
949/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000950///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000951/// objc-parmlist:
952/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000953///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000954/// objc-parms:
955/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000956///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000957/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000958/// , ...
959///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000960/// objc-keyword-attributes: [OBJC2]
961/// __attribute__((unused))
962///
John McCall48871652010-08-21 09:40:31 +0000963Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000964 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000965 tok::ObjCKeywordKind MethodImplKind,
966 bool MethodDefinition) {
John McCall28a6aea2009-11-04 02:18:39 +0000967 ParsingDeclRAIIObject PD(*this);
968
Douglas Gregor636a61e2010-04-07 00:21:17 +0000969 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000970 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000971 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000972 cutOffParsing();
973 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000974 }
975
Chris Lattner2ebb1782008-08-23 01:48:03 +0000976 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +0000977 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000978 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000979 if (Tok.is(tok::l_paren))
John McCalla55902b2011-10-01 09:56:14 +0000980 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump11289f42009-09-09 15:08:12 +0000981
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000982 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +0000983 ParsedAttributes methodAttrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000984 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +0000985 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000986
Douglas Gregor636a61e2010-04-07 00:21:17 +0000987 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000988 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000989 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000990 cutOffParsing();
991 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000992 }
993
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000994 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +0000995 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000996 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000997
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000998 // An unnamed colon is valid.
999 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001000 Diag(Tok, diag::err_expected_selector_for_method)
1001 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +00001002 // Skip until we get a ; or {}.
1003 SkipUntil(tok::r_brace);
John McCall48871652010-08-21 09:40:31 +00001004 return 0;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001005 }
Mike Stump11289f42009-09-09 15:08:12 +00001006
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001007 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001008 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001009 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001010 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001011 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001012
Chris Lattner5700fab2007-10-07 02:00:24 +00001013 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001014 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001015 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001016 mType, DSRet, ReturnType,
Douglas Gregor33823722011-06-11 01:09:30 +00001017 selLoc, Sel, 0,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001018 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001019 methodAttrs.getList(), MethodImplKind,
1020 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001021 PD.complete(Result);
1022 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001023 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001024
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001025 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001026 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001027 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001028 ParseScope PrototypeScope(this,
1029 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001030
1031 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001032
Chris Lattner5700fab2007-10-07 02:00:24 +00001033 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001034 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001035 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001036
Chris Lattner5700fab2007-10-07 02:00:24 +00001037 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +00001038 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001039 Diag(Tok, diag::err_expected_colon);
1040 break;
1041 }
1042 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001043
John McCallba7bf592010-08-24 05:47:05 +00001044 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001045 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001046 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1047 Declarator::ObjCParameterContext,
1048 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001049
Chris Lattner5700fab2007-10-07 02:00:24 +00001050 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001051 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001052 ArgInfo.ArgAttrs = 0;
John McCall53fa7142010-12-24 02:08:15 +00001053 if (getLang().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001054 MaybeParseGNUAttributes(paramAttrs);
1055 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001056 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001057
Douglas Gregor45879692010-07-08 23:37:41 +00001058 // Code completion for the next piece of the selector.
1059 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001060 KeyIdents.push_back(SelIdent);
1061 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1062 mType == tok::minus,
1063 /*AtParameterName=*/true,
1064 ReturnType,
1065 KeyIdents.data(),
1066 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001067 cutOffParsing();
1068 return 0;
Douglas Gregor45879692010-07-08 23:37:41 +00001069 }
1070
Chris Lattner0ef13522007-10-09 17:51:17 +00001071 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001072 Diag(Tok, diag::err_expected_ident); // missing argument name.
1073 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001074 }
Mike Stump11289f42009-09-09 15:08:12 +00001075
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001076 ArgInfo.Name = Tok.getIdentifierInfo();
1077 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001078 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001079
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001080 ArgInfos.push_back(ArgInfo);
1081 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001082 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001083
John McCall084e83d2011-03-24 11:26:52 +00001084 // Make sure the attributes persist.
1085 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1086
Douglas Gregor95887f92010-07-08 23:20:03 +00001087 // Code completion for the next piece of the selector.
1088 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001089 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1090 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001091 /*AtParameterName=*/false,
Douglas Gregor95887f92010-07-08 23:20:03 +00001092 ReturnType,
1093 KeyIdents.data(),
1094 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001095 cutOffParsing();
1096 return 0;
Douglas Gregor95887f92010-07-08 23:20:03 +00001097 }
1098
Chris Lattner5700fab2007-10-07 02:00:24 +00001099 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001100 SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001101 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001102 break;
1103 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001104 }
Mike Stump11289f42009-09-09 15:08:12 +00001105
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001106 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +00001107
Chris Lattner5700fab2007-10-07 02:00:24 +00001108 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001109 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001110 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001111 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001112 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001113 ConsumeToken();
1114 break;
1115 }
John McCall084e83d2011-03-24 11:26:52 +00001116 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001117 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001118 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001119 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1120 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001121 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001122 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001123 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1124 ParmDecl.getIdentifierLoc(),
1125 Param,
1126 0));
1127
Chris Lattner5700fab2007-10-07 02:00:24 +00001128 }
Mike Stump11289f42009-09-09 15:08:12 +00001129
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001130 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001131 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001132 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001133 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001134
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001135 if (KeyIdents.size() == 0)
John McCall48871652010-08-21 09:40:31 +00001136 return 0;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001137
Chris Lattner5700fab2007-10-07 02:00:24 +00001138 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1139 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001140 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001141 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001142 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001143 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001144 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001145 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001146 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001147
John McCall28a6aea2009-11-04 02:18:39 +00001148 PD.complete(Result);
1149 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001150}
1151
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001152/// objc-protocol-refs:
1153/// '<' identifier-list '>'
1154///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001155bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001156ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1157 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001158 bool WarnOnDeclarations,
1159 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001160 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001161
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001162 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001163
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001164 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001165
Chris Lattner3bbae002008-07-26 04:03:38 +00001166 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001167 if (Tok.is(tok::code_completion)) {
1168 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1169 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001170 cutOffParsing();
1171 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001172 }
1173
Chris Lattner3bbae002008-07-26 04:03:38 +00001174 if (Tok.isNot(tok::identifier)) {
1175 Diag(Tok, diag::err_expected_ident);
1176 SkipUntil(tok::greater);
1177 return true;
1178 }
1179 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1180 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001181 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001182 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001183
Chris Lattner3bbae002008-07-26 04:03:38 +00001184 if (Tok.isNot(tok::comma))
1185 break;
1186 ConsumeToken();
1187 }
Mike Stump11289f42009-09-09 15:08:12 +00001188
Chris Lattner3bbae002008-07-26 04:03:38 +00001189 // Consume the '>'.
1190 if (Tok.isNot(tok::greater)) {
1191 Diag(Tok, diag::err_expected_greater);
1192 return true;
1193 }
Mike Stump11289f42009-09-09 15:08:12 +00001194
Chris Lattner3bbae002008-07-26 04:03:38 +00001195 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +00001196
Chris Lattner3bbae002008-07-26 04:03:38 +00001197 // Convert the list of protocols identifiers into a list of protocol decls.
1198 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1199 &ProtocolIdents[0], ProtocolIdents.size(),
1200 Protocols);
1201 return false;
1202}
1203
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001204/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1205/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001206bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001207 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1208 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1209 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001210 SmallVector<Decl *, 8> ProtocolDecl;
1211 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001212 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1213 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001214 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1215 ProtocolLocs.data(), LAngleLoc);
1216 if (EndProtoLoc.isValid())
1217 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001218 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001219}
1220
1221
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001222/// objc-class-instance-variables:
1223/// '{' objc-instance-variable-decl-list[opt] '}'
1224///
1225/// objc-instance-variable-decl-list:
1226/// objc-visibility-spec
1227/// objc-instance-variable-decl ';'
1228/// ';'
1229/// objc-instance-variable-decl-list objc-visibility-spec
1230/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1231/// objc-instance-variable-decl-list ';'
1232///
1233/// objc-visibility-spec:
1234/// @private
1235/// @protected
1236/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001237/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001238///
1239/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001240/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001241///
John McCall48871652010-08-21 09:40:31 +00001242void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001243 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001244 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001245 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001246 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001247
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001248 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001249 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001250
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001251 BalancedDelimiterTracker T(*this, tok::l_brace);
1252 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00001253
Steve Naroff00433d32007-08-21 21:17:12 +00001254 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +00001255 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001256 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001257
Steve Naroff00433d32007-08-21 21:17:12 +00001258 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001259 if (Tok.is(tok::semi)) {
Douglas Gregor13d05682010-06-16 23:08:59 +00001260 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregora771f462010-03-31 17:46:05 +00001261 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +00001262 ConsumeToken();
1263 continue;
1264 }
Mike Stump11289f42009-09-09 15:08:12 +00001265
Steve Naroff00433d32007-08-21 21:17:12 +00001266 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001267 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001268 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001269
1270 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001271 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001272 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001273 }
1274
Steve Naroff7c348172007-08-23 18:16:40 +00001275 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001276 case tok::objc_private:
1277 case tok::objc_public:
1278 case tok::objc_protected:
1279 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001280 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001281 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001282 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001283 default:
1284 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +00001285 continue;
1286 }
1287 }
Mike Stump11289f42009-09-09 15:08:12 +00001288
Douglas Gregor48d46252010-01-13 21:54:15 +00001289 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001290 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001291 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001292 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001293 }
1294
John McCallcfefb6d2009-11-03 02:38:08 +00001295 struct ObjCIvarCallback : FieldCallback {
1296 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001297 Decl *IDecl;
John McCallcfefb6d2009-11-03 02:38:08 +00001298 tok::ObjCKeywordKind visibility;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001299 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001300
John McCall48871652010-08-21 09:40:31 +00001301 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001302 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001303 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1304 }
1305
John McCall48871652010-08-21 09:40:31 +00001306 Decl *invoke(FieldDeclarator &FD) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001307 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallcfefb6d2009-11-03 02:38:08 +00001308 // Install the declarator into the interface decl.
John McCall48871652010-08-21 09:40:31 +00001309 Decl *Field
Douglas Gregor0be31a22010-07-02 17:43:08 +00001310 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallcfefb6d2009-11-03 02:38:08 +00001311 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001312 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian4327b322011-08-29 17:33:12 +00001313 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001314 if (Field)
1315 AllIvarDecls.push_back(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001316 return Field;
1317 }
1318 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001319
Chris Lattnera12405b2008-04-10 06:46:29 +00001320 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00001321 DeclSpec DS(AttrFactory);
John McCallcfefb6d2009-11-03 02:38:08 +00001322 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001323
Chris Lattner0ef13522007-10-09 17:51:17 +00001324 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001325 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001326 } else {
1327 Diag(Tok, diag::err_expected_semi_decl_list);
1328 // Skip to end of block or statement
1329 SkipUntil(tok::r_brace, true, true);
1330 }
1331 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001332 T.consumeClose();
1333
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001334 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001335 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
Fariborz Jahanian4327b322011-08-29 17:33:12 +00001336 Actions.ActOnObjCContainerFinishDefinition();
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001337 // Call ActOnFields() even if we don't have any decls. This is useful
1338 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001339 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
David Blaikie751c5582011-09-22 02:58:26 +00001340 AllIvarDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001341 T.getOpenLocation(), T.getCloseLocation(), 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001342 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001343}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001344
1345/// objc-protocol-declaration:
1346/// objc-protocol-definition
1347/// objc-protocol-forward-reference
1348///
1349/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001350/// @protocol identifier
1351/// objc-protocol-refs[opt]
1352/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001353/// @end
1354///
1355/// objc-protocol-forward-reference:
1356/// @protocol identifier-list ';'
1357///
1358/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001359/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001360/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001361Parser::DeclGroupPtrTy
1362Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1363 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001364 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001365 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1366 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001367
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001368 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001369 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001370 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00001371 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001372 }
1373
Chris Lattner0ef13522007-10-09 17:51:17 +00001374 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001375 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00001376 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001377 }
1378 // Save the protocol name, then consume it.
1379 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1380 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001381
Chris Lattner0ef13522007-10-09 17:51:17 +00001382 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001383 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001384 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001385 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001386 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001387 }
Mike Stump11289f42009-09-09 15:08:12 +00001388
Erik Verbruggenf9887852011-12-08 09:58:43 +00001389 CheckNestedObjCContexts(AtLoc);
1390
Chris Lattner0ef13522007-10-09 17:51:17 +00001391 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001392 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001393 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1394
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001395 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001396 while (1) {
1397 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001398 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001399 Diag(Tok, diag::err_expected_ident);
1400 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00001401 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001402 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001403 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1404 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001405 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001406
Chris Lattner0ef13522007-10-09 17:51:17 +00001407 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001408 break;
1409 }
1410 // Consume the ';'.
1411 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00001412 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001413
Steve Naroff93eb5f12007-10-10 17:32:04 +00001414 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001415 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001416 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001417 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001418 }
Mike Stump11289f42009-09-09 15:08:12 +00001419
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001420 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001421 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001422
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001423 SmallVector<Decl *, 8> ProtocolRefs;
1424 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001425 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001426 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1427 LAngleLoc, EndProtoLoc))
Douglas Gregorf6102672012-01-01 21:23:57 +00001428 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001429
John McCall48871652010-08-21 09:40:31 +00001430 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001431 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001432 ProtocolRefs.data(),
1433 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001434 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001435 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001436
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001437 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00001438 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001439}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001440
1441/// objc-implementation:
1442/// objc-class-implementation-prologue
1443/// objc-category-implementation-prologue
1444///
1445/// objc-class-implementation-prologue:
1446/// @implementation identifier objc-superclass[opt]
1447/// objc-class-instance-variables[opt]
1448///
1449/// objc-category-implementation-prologue:
1450/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001451Parser::DeclGroupPtrTy
1452Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001453 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1454 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001455 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001456 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001457
Douglas Gregor49c22a72009-11-18 16:26:39 +00001458 // Code completion after '@implementation'.
1459 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001460 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001461 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001462 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001463 }
1464
Chris Lattner0ef13522007-10-09 17:51:17 +00001465 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001466 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001467 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001468 }
1469 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001470 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001471 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001472 Decl *ObjCImpDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001473
1474 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001475 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001476 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001477 SourceLocation categoryLoc, rparenLoc;
1478 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001479
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001480 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001481 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001482 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001483 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001484 }
1485
Chris Lattner0ef13522007-10-09 17:51:17 +00001486 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001487 categoryId = Tok.getIdentifierInfo();
1488 categoryLoc = ConsumeToken();
1489 } else {
1490 Diag(Tok, diag::err_expected_ident); // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001491 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001492 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001493 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001494 Diag(Tok, diag::err_expected_rparen);
1495 SkipUntil(tok::r_paren, false); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001496 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001497 }
1498 rparenLoc = ConsumeParen();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001499 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001500 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001501 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001502
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001503 } else {
1504 // We have a class implementation
1505 SourceLocation superClassLoc;
1506 IdentifierInfo *superClassId = 0;
1507 if (Tok.is(tok::colon)) {
1508 // We have a super class
1509 ConsumeToken();
1510 if (Tok.isNot(tok::identifier)) {
1511 Diag(Tok, diag::err_expected_ident); // missing super class name.
1512 return DeclGroupPtrTy();
1513 }
1514 superClassId = Tok.getIdentifierInfo();
1515 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001516 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001517 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1518 AtLoc, nameId, nameLoc,
1519 superClassId, superClassLoc);
1520
1521 if (Tok.is(tok::l_brace)) // we have ivars
1522 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001523 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001524 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001525
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001526 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001527
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001528 {
1529 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
1530 while (!ObjCImplParsing.isFinished() && Tok.isNot(tok::eof)) {
1531 ParsedAttributesWithRange attrs(AttrFactory);
1532 MaybeParseCXX0XAttributes(attrs);
1533 MaybeParseMicrosoftAttributes(attrs);
1534 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1535 DeclGroupRef DG = DGP.get();
1536 DeclsInGroup.append(DG.begin(), DG.end());
1537 }
1538 }
1539 }
1540
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001541 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001542}
Steve Naroff33a1e802007-10-29 21:38:07 +00001543
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001544Parser::DeclGroupPtrTy
1545Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001546 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1547 "ParseObjCAtEndDeclaration(): Expected @end");
1548 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001549 if (CurParsedObjCImpl)
1550 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001551 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00001552 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001553 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001554 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001555}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001556
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001557Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1558 if (!Finished) {
1559 finish(P.Tok.getLocation());
1560 if (P.Tok.is(tok::eof)) {
1561 P.Diag(P.Tok, diag::err_objc_missing_end)
1562 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1563 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1564 << Sema::OCK_Implementation;
1565 }
1566 }
1567 P.CurParsedObjCImpl = 0;
1568 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001569}
1570
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001571void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1572 assert(!Finished);
1573 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1574 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1575 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
1576
1577 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1578
1579 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001580 for (LateParsedObjCMethodContainer::iterator
1581 I = LateParsedObjCMethods.begin(),
1582 E = LateParsedObjCMethods.end(); I != E; ++I)
1583 delete *I;
1584 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001585
1586 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001587}
1588
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001589/// compatibility-alias-decl:
1590/// @compatibility_alias alias-name class-name ';'
1591///
John McCall48871652010-08-21 09:40:31 +00001592Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001593 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1594 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1595 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001596 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001597 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001598 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001599 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001600 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1601 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001602 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001603 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001604 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001605 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001606 IdentifierInfo *classId = Tok.getIdentifierInfo();
1607 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregor78edf992011-01-05 01:10:06 +00001608 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1609 "@compatibility_alias");
Chris Lattner83f095c2009-03-28 19:18:32 +00001610 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1611 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001612}
1613
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001614/// property-synthesis:
1615/// @synthesize property-ivar-list ';'
1616///
1617/// property-ivar-list:
1618/// property-ivar
1619/// property-ivar-list ',' property-ivar
1620///
1621/// property-ivar:
1622/// identifier
1623/// identifier '=' identifier
1624///
John McCall48871652010-08-21 09:40:31 +00001625Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001626 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1627 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001628 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001629
Douglas Gregor88e72a02009-11-18 19:45:45 +00001630 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001631 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001632 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001633 cutOffParsing();
1634 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001635 }
1636
Douglas Gregor88e72a02009-11-18 19:45:45 +00001637 if (Tok.isNot(tok::identifier)) {
1638 Diag(Tok, diag::err_synthesized_property_name);
1639 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001640 return 0;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001641 }
1642
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001643 IdentifierInfo *propertyIvar = 0;
1644 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1645 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001646 SourceLocation propertyIvarLoc;
Chris Lattner0ef13522007-10-09 17:51:17 +00001647 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001648 // property '=' ivar-name
1649 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001650
1651 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001652 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001653 cutOffParsing();
1654 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001655 }
1656
Chris Lattner0ef13522007-10-09 17:51:17 +00001657 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001658 Diag(Tok, diag::err_expected_ident);
1659 break;
1660 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001661 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001662 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001663 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001664 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001665 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001666 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001667 break;
1668 ConsumeToken(); // consume ','
1669 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001670 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCall48871652010-08-21 09:40:31 +00001671 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001672}
1673
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001674/// property-dynamic:
1675/// @dynamic property-list
1676///
1677/// property-list:
1678/// identifier
1679/// property-list ',' identifier
1680///
John McCall48871652010-08-21 09:40:31 +00001681Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001682 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1683 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001684 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001685 while (true) {
1686 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001687 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001688 cutOffParsing();
1689 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001690 }
1691
1692 if (Tok.isNot(tok::identifier)) {
1693 Diag(Tok, diag::err_expected_ident);
1694 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001695 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001696 }
1697
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001698 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1699 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001700 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001701 propertyId, 0, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001702
Chris Lattner0ef13522007-10-09 17:51:17 +00001703 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001704 break;
1705 ConsumeToken(); // consume ','
1706 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001707 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCall48871652010-08-21 09:40:31 +00001708 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001709}
Mike Stump11289f42009-09-09 15:08:12 +00001710
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001711/// objc-throw-statement:
1712/// throw expression[opt];
1713///
John McCalldadc5752010-08-24 06:29:42 +00001714StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1715 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001716 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001717 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001718 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001719 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001720 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001721 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001722 }
1723 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001724 // consume ';'
1725 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCallb268a282010-08-23 23:25:46 +00001726 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001727}
1728
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001729/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001730/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001731///
John McCalldadc5752010-08-24 06:29:42 +00001732StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001733Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001734 ConsumeToken(); // consume synchronized
1735 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001736 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001737 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001738 }
John McCalld9bb7432011-07-27 21:50:02 +00001739
1740 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001741 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00001742 ExprResult operand(ParseExpression());
1743
1744 if (Tok.is(tok::r_paren)) {
1745 ConsumeParen(); // ')'
1746 } else {
1747 if (!operand.isInvalid())
1748 Diag(Tok, diag::err_expected_rparen);
1749
1750 // Skip forward until we see a left brace, but don't consume it.
1751 SkipUntil(tok::l_brace, true, true);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001752 }
John McCalld9bb7432011-07-27 21:50:02 +00001753
1754 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001755 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00001756 if (!operand.isInvalid())
1757 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001758 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001759 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001760
John McCalld9bb7432011-07-27 21:50:02 +00001761 // Check the @synchronized operand now.
1762 if (!operand.isInvalid())
1763 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001764
John McCalld9bb7432011-07-27 21:50:02 +00001765 // Parse the compound statement within a new scope.
1766 ParseScope bodyScope(this, Scope::DeclScope);
1767 StmtResult body(ParseCompoundStatementBody());
1768 bodyScope.Exit();
1769
1770 // If there was a semantic or parse error earlier with the
1771 // operand, fail now.
1772 if (operand.isInvalid())
1773 return StmtError();
1774
1775 if (body.isInvalid())
1776 body = Actions.ActOnNullStmt(Tok.getLocation());
1777
1778 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001779}
1780
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001781/// objc-try-catch-statement:
1782/// @try compound-statement objc-catch-list[opt]
1783/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1784///
1785/// objc-catch-list:
1786/// @catch ( parameter-declaration ) compound-statement
1787/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1788/// catch-parameter-declaration:
1789/// parameter-declaration
1790/// '...' [OBJC2]
1791///
John McCalldadc5752010-08-24 06:29:42 +00001792StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001793 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001794
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001795 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001796 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001797 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001798 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001799 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001800 StmtVector CatchStmts(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001801 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001802 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001803 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001804 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001805 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001806 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001807
Chris Lattner0ef13522007-10-09 17:51:17 +00001808 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001809 // At this point, we need to lookahead to determine if this @ is the start
1810 // of an @catch or @finally. We don't want to consume the @ token if this
1811 // is an @try or @encode or something else.
1812 Token AfterAt = GetLookAheadToken(1);
1813 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1814 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1815 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001816
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001817 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001818 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCall48871652010-08-21 09:40:31 +00001819 Decl *FirstPart = 0;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001820 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001821 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001822 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001823 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001824 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001825 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001826 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00001827 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00001828 ParseDeclarator(ParmDecl);
1829
Douglas Gregore11ee112010-04-23 23:01:43 +00001830 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001831 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001832 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001833 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001834 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001835
Steve Naroff65a00892009-04-07 22:56:58 +00001836 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001837
Steve Naroff65a00892009-04-07 22:56:58 +00001838 if (Tok.is(tok::r_paren))
1839 RParenLoc = ConsumeParen();
1840 else // Skip over garbage, until we get to ')'. Eat the ')'.
1841 SkipUntil(tok::r_paren, true, false);
1842
John McCalldadc5752010-08-24 06:29:42 +00001843 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001844 if (Tok.is(tok::l_brace))
1845 CatchBody = ParseCompoundStatementBody();
1846 else
1847 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001848 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001849 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001850
John McCalldadc5752010-08-24 06:29:42 +00001851 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001852 RParenLoc,
1853 FirstPart,
John McCallb268a282010-08-23 23:25:46 +00001854 CatchBody.take());
Douglas Gregor96c79492010-04-23 22:50:49 +00001855 if (!Catch.isInvalid())
1856 CatchStmts.push_back(Catch.release());
1857
Steve Naroffe6016792008-02-05 21:27:35 +00001858 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001859 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1860 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001861 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001862 }
1863 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001864 } else {
1865 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001866 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001867 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001868
John McCalldadc5752010-08-24 06:29:42 +00001869 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001870 if (Tok.is(tok::l_brace))
1871 FinallyBody = ParseCompoundStatementBody();
1872 else
1873 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001874 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001875 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001876 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCallb268a282010-08-23 23:25:46 +00001877 FinallyBody.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001878 catch_or_finally_seen = true;
1879 break;
1880 }
1881 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001882 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001883 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001884 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001885 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001886
John McCallb268a282010-08-23 23:25:46 +00001887 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor96c79492010-04-23 22:50:49 +00001888 move_arg(CatchStmts),
John McCallb268a282010-08-23 23:25:46 +00001889 FinallyStmt.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001890}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001891
John McCall31168b02011-06-15 23:02:42 +00001892/// objc-autoreleasepool-statement:
1893/// @autoreleasepool compound-statement
1894///
1895StmtResult
1896Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1897 ConsumeToken(); // consume autoreleasepool
1898 if (Tok.isNot(tok::l_brace)) {
1899 Diag(Tok, diag::err_expected_lbrace);
1900 return StmtError();
1901 }
1902 // Enter a scope to hold everything within the compound stmt. Compound
1903 // statements can always hold declarations.
1904 ParseScope BodyScope(this, Scope::DeclScope);
1905
1906 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1907
1908 BodyScope.Exit();
1909 if (AutoreleasePoolBody.isInvalid())
1910 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1911 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1912 AutoreleasePoolBody.take());
1913}
1914
Steve Naroff09bf8152007-09-06 21:24:23 +00001915/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001916///
John McCall48871652010-08-21 09:40:31 +00001917Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001918 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00001919
John McCallfaf5fb42010-08-26 23:41:50 +00001920 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1921 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001922
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001923 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001924 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001925 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001926 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00001927 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001928 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001929 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001930 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001931
Steve Naroffbb875722007-11-11 19:54:21 +00001932 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001933 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001934 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001935
Steve Naroffbb875722007-11-11 19:54:21 +00001936 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1937 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001938
Steve Naroffbb875722007-11-11 19:54:21 +00001939 // If we didn't find the '{', bail out.
1940 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +00001941 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001942 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001943
1944 if (!MDecl) {
1945 ConsumeBrace();
1946 SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
1947 return 0;
1948 }
1949
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001950 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001951 Actions.AddAnyMethodToGlobalPool(MDecl);
1952
1953 if (CurParsedObjCImpl) {
1954 // Consume the tokens and store them for later parsing.
1955 LexedMethod* LM = new LexedMethod(this, MDecl);
1956 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
1957 CachedTokens &Toks = LM->Toks;
1958 // Begin by storing the '{' token.
1959 Toks.push_back(Tok);
1960 ConsumeBrace();
1961 // Consume everything up to (and including) the matching right brace.
1962 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1963
1964 } else {
1965 ConsumeBrace();
1966 SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
1967 }
1968
Steve Naroff7b8fa472007-11-13 23:01:27 +00001969 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001970}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001971
John McCalldadc5752010-08-24 06:29:42 +00001972StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001973 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001974 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001975 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001976 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001977 }
1978
1979 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001980 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001981
1982 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001983 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001984
1985 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001986 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00001987
1988 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1989 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001990
John McCalldadc5752010-08-24 06:29:42 +00001991 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001992 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001993 // If the expression is invalid, skip ahead to the next semicolon. Not
1994 // doing this opens us up to the possibility of infinite loops if
1995 // ParseExpression does not consume any tokens.
1996 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001997 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001998 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001999
Steve Naroffe6016792008-02-05 21:27:35 +00002000 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002001 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +00002002 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroffe6016792008-02-05 21:27:35 +00002003}
2004
John McCalldadc5752010-08-24 06:29:42 +00002005ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002006 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002007 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002008 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002009 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002010 return ExprError();
2011
Ted Kremeneke65b0862012-03-06 20:05:56 +00002012 case tok::minus:
2013 case tok::plus: {
2014 tok::TokenKind Kind = Tok.getKind();
2015 SourceLocation OpLoc = ConsumeToken();
2016
2017 if (!Tok.is(tok::numeric_constant)) {
2018 const char *Symbol = 0;
2019 switch (Kind) {
2020 case tok::minus: Symbol = "-"; break;
2021 case tok::plus: Symbol = "+"; break;
2022 default: llvm_unreachable("missing unary operator case");
2023 }
2024 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2025 << Symbol;
2026 return ExprError();
2027 }
2028
2029 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2030 if (Lit.isInvalid()) {
2031 return move(Lit);
2032 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002033 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002034
2035 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.take());
2036 if (Lit.isInvalid())
2037 return move(Lit);
2038
2039 return ParsePostfixExpressionSuffix(
2040 Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2041 }
2042
Chris Lattnere002fbe2007-12-12 01:04:12 +00002043 case tok::string_literal: // primary-expression: string-literal
2044 case tok::wide_string_literal:
Richard Smithd67aea22012-03-06 03:21:47 +00002045 if (Tok.hasUDSuffix())
2046 return ExprError(Diag(Tok, diag::err_invalid_string_udl));
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
Chris Lattnere002fbe2007-12-12 01:04:12 +00002070 default:
Chris Lattner197a3012008-08-05 06:19:09 +00002071 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002072 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002073
Chris Lattner197a3012008-08-05 06:19:09 +00002074 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2075 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002076 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002077 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002078 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002079 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002080 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002081 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002082 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00002083 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002084 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002085}
2086
Douglas Gregor8d4de672010-04-21 22:36:40 +00002087/// \brirg Parse the receiver of an Objective-C++ message send.
2088///
2089/// This routine parses the receiver of a message send in
2090/// Objective-C++ either as a type or as an expression. Note that this
2091/// routine must not be called to parse a send to 'super', since it
2092/// has no way to return such a result.
2093///
2094/// \param IsExpr Whether the receiver was parsed as an expression.
2095///
2096/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2097/// IsExpr is true), the parsed expression. If the receiver was parsed
2098/// as a type (\c IsExpr is false), the parsed type.
2099///
2100/// \returns True if an error occurred during parsing or semantic
2101/// analysis, in which case the arguments do not have valid
2102/// values. Otherwise, returns false for a successful parse.
2103///
2104/// objc-receiver: [C++]
2105/// 'super' [not parsed here]
2106/// expression
2107/// simple-type-specifier
2108/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002109bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002110 InMessageExpressionRAIIObject InMessage(*this, true);
2111
Douglas Gregor8d4de672010-04-21 22:36:40 +00002112 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2113 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2114 TryAnnotateTypeOrScopeToken();
2115
2116 if (!isCXXSimpleTypeSpecifier()) {
2117 // objc-receiver:
2118 // expression
John McCalldadc5752010-08-24 06:29:42 +00002119 ExprResult Receiver = ParseExpression();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002120 if (Receiver.isInvalid())
2121 return true;
2122
2123 IsExpr = true;
2124 TypeOrExpr = Receiver.take();
2125 return false;
2126 }
2127
2128 // objc-receiver:
2129 // typename-specifier
2130 // simple-type-specifier
2131 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002132 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002133 ParseCXXSimpleTypeSpecifier(DS);
2134
2135 if (Tok.is(tok::l_paren)) {
2136 // If we see an opening parentheses at this point, we are
2137 // actually parsing an expression that starts with a
2138 // function-style cast, e.g.,
2139 //
2140 // postfix-expression:
2141 // simple-type-specifier ( expression-list [opt] )
2142 // typename-specifier ( expression-list [opt] )
2143 //
2144 // Parse the remainder of this case, then the (optional)
2145 // postfix-expression suffix, followed by the (optional)
2146 // right-hand side of the binary expression. We have an
2147 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002148 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002149 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002150 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002151 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002152 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002153 if (Receiver.isInvalid())
2154 return true;
2155
2156 IsExpr = true;
2157 TypeOrExpr = Receiver.take();
2158 return false;
2159 }
2160
2161 // We have a class message. Turn the simple-type-specifier or
2162 // typename-specifier we parsed into a type and parse the
2163 // remainder of the class message.
2164 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002165 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002166 if (Type.isInvalid())
2167 return true;
2168
2169 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002170 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002171 return false;
2172}
2173
Douglas Gregor990ccac2010-05-31 14:40:22 +00002174/// \brief Determine whether the parser is currently referring to a an
2175/// Objective-C message send, using a simplified heuristic to avoid overhead.
2176///
2177/// This routine will only return true for a subset of valid message-send
2178/// expressions.
2179bool Parser::isSimpleObjCMessageExpression() {
Chris Lattner47054fb2010-05-31 18:18:22 +00002180 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002181 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002182 return GetLookAheadToken(1).is(tok::identifier) &&
2183 GetLookAheadToken(2).is(tok::identifier);
2184}
2185
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002186bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
2187 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
2188 InMessageExpression)
2189 return false;
2190
2191
2192 ParsedType Type;
2193
2194 if (Tok.is(tok::annot_typename))
2195 Type = getTypeAnnotation(Tok);
2196 else if (Tok.is(tok::identifier))
2197 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2198 getCurScope());
2199 else
2200 return false;
2201
2202 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2203 const Token &AfterNext = GetLookAheadToken(2);
2204 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2205 if (Tok.is(tok::identifier))
2206 TryAnnotateTypeOrScopeToken();
2207
2208 return Tok.is(tok::annot_typename);
2209 }
2210 }
2211
2212 return false;
2213}
2214
Mike Stump11289f42009-09-09 15:08:12 +00002215/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002216/// '[' objc-receiver objc-message-args ']'
2217///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002218/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002219/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002220/// expression
2221/// class-name
2222/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002223///
John McCalldadc5752010-08-24 06:29:42 +00002224ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002225 assert(Tok.is(tok::l_square) && "'[' expected");
2226 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2227
Douglas Gregora817a192010-05-27 23:06:34 +00002228 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002229 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002230 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002231 return ExprError();
2232 }
2233
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002234 InMessageExpressionRAIIObject InMessage(*this, true);
2235
Douglas Gregor8d4de672010-04-21 22:36:40 +00002236 if (getLang().CPlusPlus) {
2237 // We completely separate the C and C++ cases because C++ requires
2238 // more complicated (read: slower) parsing.
2239
2240 // Handle send to super.
2241 // FIXME: This doesn't benefit from the same typo-correction we
2242 // get in Objective-C.
2243 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002244 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002245 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2246 ParsedType(), 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002247
2248 // Parse the receiver, which is either a type or an expression.
2249 bool IsExpr;
Nick Lewycky970aed92010-09-15 18:35:19 +00002250 void *TypeOrExpr = NULL;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002251 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2252 SkipUntil(tok::r_square);
2253 return ExprError();
2254 }
2255
2256 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002257 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2258 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002259 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002260
2261 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002262 ParsedType::getFromOpaquePtr(TypeOrExpr),
2263 0);
Chris Lattner47054fb2010-05-31 18:18:22 +00002264 }
2265
2266 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002267 IdentifierInfo *Name = Tok.getIdentifierInfo();
2268 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002269 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002270 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002271 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002272 NextToken().is(tok::period),
2273 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002274 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002275 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2276 ParsedType(), 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002277
John McCallfaf5fb42010-08-26 23:41:50 +00002278 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002279 if (!ReceiverType) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002280 SkipUntil(tok::r_square);
2281 return ExprError();
2282 }
2283
Douglas Gregore5798dc2010-04-21 20:38:13 +00002284 ConsumeToken(); // the type name
2285
2286 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb268a282010-08-23 23:25:46 +00002287 ReceiverType, 0);
Douglas Gregora148a1d2010-04-14 02:22:16 +00002288
John McCallfaf5fb42010-08-26 23:41:50 +00002289 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002290 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002291 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002292 }
Chris Lattner8f697062008-01-25 18:59:06 +00002293 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002294
2295 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCalldadc5752010-08-24 06:29:42 +00002296 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002297 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00002298 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002299 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00002300 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002301
John McCallba7bf592010-08-24 05:47:05 +00002302 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2303 ParsedType(), Res.take());
Chris Lattner8f697062008-01-25 18:59:06 +00002304}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002305
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002306/// \brief Parse the remainder of an Objective-C message following the
2307/// '[' objc-receiver.
2308///
2309/// This routine handles sends to super, class messages (sent to a
2310/// class name), and instance messages (sent to an object), and the
2311/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2312/// ReceiverExpr, respectively. Only one of these parameters may have
2313/// a valid value.
2314///
2315/// \param LBracLoc The location of the opening '['.
2316///
2317/// \param SuperLoc If this is a send to 'super', the location of the
2318/// 'super' keyword that indicates a send to the superclass.
2319///
2320/// \param ReceiverType If this is a class message, the type of the
2321/// class we are sending a message to.
2322///
2323/// \param ReceiverExpr If this is an instance message, the expression
2324/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002325///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002326/// objc-message-args:
2327/// objc-selector
2328/// objc-keywordarg-list
2329///
2330/// objc-keywordarg-list:
2331/// objc-keywordarg
2332/// objc-keywordarg-list objc-keywordarg
2333///
Mike Stump11289f42009-09-09 15:08:12 +00002334/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002335/// selector-name[opt] ':' objc-keywordexpr
2336///
2337/// objc-keywordexpr:
2338/// nonempty-expr-list
2339///
2340/// nonempty-expr-list:
2341/// assignment-expression
2342/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002343///
John McCalldadc5752010-08-24 06:29:42 +00002344ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002345Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002346 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002347 ParsedType ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002348 ExprArg ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002349 InMessageExpressionRAIIObject InMessage(*this, true);
2350
Steve Naroffeae65032009-11-07 02:08:14 +00002351 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002352 if (SuperLoc.isValid())
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002353 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2354 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002355 else if (ReceiverType)
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002356 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2357 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002358 else
John McCallb268a282010-08-23 23:25:46 +00002359 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002360 0, 0, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002361 cutOffParsing();
2362 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002363 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002364
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002365 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002366 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002367 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00002368
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002369 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002370 SmallVector<SourceLocation, 12> KeyLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00002371 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00002372
Chris Lattner0ef13522007-10-09 17:51:17 +00002373 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002374 while (1) {
2375 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002376 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002377 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00002378
Chris Lattner0ef13522007-10-09 17:51:17 +00002379 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002380 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00002381 // We must manually skip to a ']', otherwise the expression skipper will
2382 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2383 // the enclosing expression.
2384 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002385 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002386 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002387
Steve Narofff73590d2007-09-27 14:38:14 +00002388 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00002389 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002390
2391 if (Tok.is(tok::code_completion)) {
2392 if (SuperLoc.isValid())
2393 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2394 KeyIdents.data(),
2395 KeyIdents.size(),
2396 /*AtArgumentEpression=*/true);
2397 else if (ReceiverType)
2398 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2399 KeyIdents.data(),
2400 KeyIdents.size(),
2401 /*AtArgumentEpression=*/true);
2402 else
2403 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2404 KeyIdents.data(),
2405 KeyIdents.size(),
2406 /*AtArgumentEpression=*/true);
2407
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002408 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002409 return ExprError();
2410 }
2411
John McCalldadc5752010-08-24 06:29:42 +00002412 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002413 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002414 // We must manually skip to a ']', otherwise the expression skipper will
2415 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2416 // the enclosing expression.
2417 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002418 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00002419 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002420
Steve Naroff486760a2007-09-17 20:25:27 +00002421 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002422 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002423
Douglas Gregor1b605f72009-11-19 01:08:35 +00002424 // Code completion after each argument.
2425 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002426 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002427 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002428 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002429 KeyIdents.size(),
2430 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002431 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002432 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002433 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002434 KeyIdents.size(),
2435 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002436 else
John McCallb268a282010-08-23 23:25:46 +00002437 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002438 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002439 KeyIdents.size(),
2440 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002441 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002442 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002443 }
2444
Steve Naroff486760a2007-09-17 20:25:27 +00002445 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002446 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002447 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002448 break;
2449 // We have a selector or a colon, continue parsing.
2450 }
2451 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002452 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002453 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002454 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002455 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002456 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002457 // We must manually skip to a ']', otherwise the expression skipper will
2458 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2459 // the enclosing expression.
2460 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002461 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002462 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002463
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002464 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002465 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002466 }
2467 } else if (!selIdent) {
2468 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002469
Chris Lattner197a3012008-08-05 06:19:09 +00002470 // We must manually skip to a ']', otherwise the expression skipper will
2471 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2472 // the enclosing expression.
2473 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002474 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002475 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002476
Chris Lattner0ef13522007-10-09 17:51:17 +00002477 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002478 if (Tok.is(tok::identifier))
2479 Diag(Tok, diag::err_expected_colon);
2480 else
2481 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00002482 // We must manually skip to a ']', otherwise the expression skipper will
2483 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2484 // the enclosing expression.
2485 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002486 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002487 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002488
Chris Lattner8f697062008-01-25 18:59:06 +00002489 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002490
Steve Naroffe61bfa82007-10-05 18:42:47 +00002491 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002492 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00002493 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002494 KeyLocs.push_back(Loc);
2495 }
Chris Lattner5700fab2007-10-07 02:00:24 +00002496 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002497
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002498 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002499 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002500 LBracLoc, KeyLocs, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002501 MultiExprArg(Actions,
2502 KeyExprs.take(),
2503 KeyExprs.size()));
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002504 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002505 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002506 LBracLoc, KeyLocs, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002507 MultiExprArg(Actions,
2508 KeyExprs.take(),
2509 KeyExprs.size()));
John McCallb268a282010-08-23 23:25:46 +00002510 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002511 LBracLoc, KeyLocs, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002512 MultiExprArg(Actions,
2513 KeyExprs.take(),
2514 KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002515}
2516
John McCalldadc5752010-08-24 06:29:42 +00002517ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2518 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002519 if (Res.isInvalid()) return move(Res);
2520
Chris Lattnere002fbe2007-12-12 01:04:12 +00002521 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2522 // expressions. At this point, we know that the only valid thing that starts
2523 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002524 SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00002525 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00002526 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002527 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002528
Chris Lattnere002fbe2007-12-12 01:04:12 +00002529 while (Tok.is(tok::at)) {
2530 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002531
Sebastian Redlc13f2682008-12-09 20:22:58 +00002532 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002533 if (!isTokenStringLiteral())
2534 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002535
John McCalldadc5752010-08-24 06:29:42 +00002536 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002537 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002538 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002539
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002540 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002541 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002542
2543 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2544 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00002545}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002546
Ted Kremeneke65b0862012-03-06 20:05:56 +00002547/// ParseObjCBooleanLiteral -
2548/// objc-scalar-literal : '@' boolean-keyword
2549/// ;
2550/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2551/// ;
2552ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2553 bool ArgValue) {
2554 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2555 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2556}
2557
2558/// ParseObjCCharacterLiteral -
2559/// objc-scalar-literal : '@' character-literal
2560/// ;
2561ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2562 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2563 if (Lit.isInvalid()) {
2564 return move(Lit);
2565 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002566 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002567 return Owned(Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2568}
2569
2570/// ParseObjCNumericLiteral -
2571/// objc-scalar-literal : '@' scalar-literal
2572/// ;
2573/// scalar-literal : | numeric-constant /* any numeric constant. */
2574/// ;
2575ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2576 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2577 if (Lit.isInvalid()) {
2578 return move(Lit);
2579 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002580 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002581 return Owned(Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2582}
2583
2584ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
2585 ExprVector ElementExprs(Actions); // array elements.
2586 ConsumeBracket(); // consume the l_square.
2587
2588 while (Tok.isNot(tok::r_square)) {
2589 // Parse list of array element expressions (all must be id types).
2590 ExprResult Res(ParseAssignmentExpression());
2591 if (Res.isInvalid()) {
2592 // We must manually skip to a ']', otherwise the expression skipper will
2593 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2594 // the enclosing expression.
2595 SkipUntil(tok::r_square);
2596 return move(Res);
2597 }
2598
2599 // Parse the ellipsis that indicates a pack expansion.
2600 if (Tok.is(tok::ellipsis))
2601 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2602 if (Res.isInvalid())
2603 return true;
2604
2605 ElementExprs.push_back(Res.release());
2606
2607 if (Tok.is(tok::comma))
2608 ConsumeToken(); // Eat the ','.
2609 else if (Tok.isNot(tok::r_square))
2610 return ExprError(Diag(Tok, diag::err_expected_rsquare_or_comma));
2611 }
2612 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
2613 MultiExprArg Args(Actions, ElementExprs.take(), ElementExprs.size());
2614 return Owned(Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args));
2615}
2616
2617ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2618 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2619 ConsumeBrace(); // consume the l_square.
2620 while (Tok.isNot(tok::r_brace)) {
2621 // Parse the comma separated key : value expressions.
2622 ExprResult KeyExpr;
2623 {
2624 ColonProtectionRAIIObject X(*this);
2625 KeyExpr = ParseAssignmentExpression();
2626 if (KeyExpr.isInvalid()) {
2627 // We must manually skip to a '}', otherwise the expression skipper will
2628 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2629 // the enclosing expression.
2630 SkipUntil(tok::r_brace);
2631 return move(KeyExpr);
2632 }
2633 }
2634
2635 if (Tok.is(tok::colon)) {
2636 ConsumeToken();
2637 } else {
2638 return ExprError(Diag(Tok, diag::err_expected_colon));
2639 }
2640
2641 ExprResult ValueExpr(ParseAssignmentExpression());
2642 if (ValueExpr.isInvalid()) {
2643 // We must manually skip to a '}', otherwise the expression skipper will
2644 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2645 // the enclosing expression.
2646 SkipUntil(tok::r_brace);
2647 return move(ValueExpr);
2648 }
2649
2650 // Parse the ellipsis that designates this as a pack expansion.
2651 SourceLocation EllipsisLoc;
2652 if (Tok.is(tok::ellipsis) && getLang().CPlusPlus)
2653 EllipsisLoc = ConsumeToken();
2654
2655 // We have a valid expression. Collect it in a vector so we can
2656 // build the argument list.
2657 ObjCDictionaryElement Element = {
2658 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, llvm::Optional<unsigned>()
2659 };
2660 Elements.push_back(Element);
2661
2662 if (Tok.is(tok::comma))
2663 ConsumeToken(); // Eat the ','.
2664 else if (Tok.isNot(tok::r_brace))
2665 return ExprError(Diag(Tok, diag::err_expected_rbrace_or_comma));
2666 }
2667 SourceLocation EndLoc = ConsumeBrace();
2668
2669 // Create the ObjCDictionaryLiteral.
2670 return Owned(Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2671 Elements.data(),
2672 Elements.size()));
2673}
2674
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002675/// objc-encode-expression:
2676/// @encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002677ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002678Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002679 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002680
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002681 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002682
Chris Lattner197a3012008-08-05 06:19:09 +00002683 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002684 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2685
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002686 BalancedDelimiterTracker T(*this, tok::l_paren);
2687 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002688
Douglas Gregor220cac52009-02-18 17:45:20 +00002689 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002690
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002691 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002692
Douglas Gregor220cac52009-02-18 17:45:20 +00002693 if (Ty.isInvalid())
2694 return ExprError();
2695
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002696 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
2697 T.getOpenLocation(), Ty.get(),
2698 T.getCloseLocation()));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002699}
Anders Carlssone01493d2007-08-23 15:25:28 +00002700
2701/// objc-protocol-expression
2702/// @protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002703ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002704Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002705 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002706
Chris Lattner197a3012008-08-05 06:19:09 +00002707 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002708 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2709
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002710 BalancedDelimiterTracker T(*this, tok::l_paren);
2711 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002712
Chris Lattner197a3012008-08-05 06:19:09 +00002713 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002714 return ExprError(Diag(Tok, diag::err_expected_ident));
2715
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002716 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00002717 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002718
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002719 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00002720
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002721 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002722 T.getOpenLocation(),
2723 T.getCloseLocation()));
Anders Carlssone01493d2007-08-23 15:25:28 +00002724}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002725
2726/// objc-selector-expression
2727/// @selector '(' objc-keyword-selector ')'
John McCalldadc5752010-08-24 06:29:42 +00002728ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002729 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002730
Chris Lattner197a3012008-08-05 06:19:09 +00002731 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002732 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2733
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002734 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002735 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002736
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002737 BalancedDelimiterTracker T(*this, tok::l_paren);
2738 T.consumeOpen();
2739
Douglas Gregor67c692c2010-08-26 15:07:07 +00002740 if (Tok.is(tok::code_completion)) {
2741 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2742 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002743 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002744 return ExprError();
2745 }
2746
Chris Lattner4f472a32009-04-11 18:13:45 +00002747 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002748 if (!SelIdent && // missing selector name.
2749 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002750 return ExprError(Diag(Tok, diag::err_expected_ident));
2751
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002752 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002753 unsigned nColons = 0;
2754 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002755 while (1) {
Chris Lattner1ba64452010-08-27 22:32:41 +00002756 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2757 ++nColons;
2758 KeyIdents.push_back(0);
2759 } else if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002760 return ExprError(Diag(Tok, diag::err_expected_colon));
2761
Chris Lattner1ba64452010-08-27 22:32:41 +00002762 ++nColons;
Chris Lattner85222c62011-03-26 18:11:38 +00002763 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002764 if (Tok.is(tok::r_paren))
2765 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002766
2767 if (Tok.is(tok::code_completion)) {
2768 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2769 KeyIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002770 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002771 return ExprError();
2772 }
2773
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002774 // Check for another keyword selector.
2775 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002776 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002777 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00002778 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002779 break;
2780 }
Steve Naroff152dd812007-12-05 22:21:29 +00002781 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002782 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00002783 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002784 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002785 T.getOpenLocation(),
2786 T.getCloseLocation()));
Gabor Greif24032f12007-10-19 15:38:32 +00002787 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002788
2789Decl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002790
2791 // Save the current token position.
2792 SourceLocation OrigLoc = Tok.getLocation();
2793
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002794 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2795 // Append the current token at the end of the new token stream so that it
2796 // doesn't get lost.
2797 LM.Toks.push_back(Tok);
2798 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2799
2800 // MDecl might be null due to error in method prototype, etc.
2801 Decl *MDecl = LM.D;
2802 // Consume the previously pushed token.
2803 ConsumeAnyToken();
2804
2805 assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2806 SourceLocation BraceLoc = Tok.getLocation();
2807 // Enter a scope for the method body.
2808 ParseScope BodyScope(this,
2809 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2810
2811 // Tell the actions module that we have entered a method definition with the
2812 // specified Declarator for the method.
2813 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2814
2815 if (PP.isCodeCompletionEnabled()) {
2816 if (trySkippingFunctionBodyForCodeCompletion()) {
2817 BodyScope.Exit();
2818 return Actions.ActOnFinishFunctionBody(MDecl, 0);
2819 }
2820 }
2821
2822 StmtResult FnBody(ParseCompoundStatementBody());
2823
2824 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002825 if (FnBody.isInvalid()) {
2826 Sema::CompoundScopeRAII CompoundScope(Actions);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002827 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2828 MultiStmtArg(Actions), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002829 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002830
2831 // Leave the function body scope.
2832 BodyScope.Exit();
2833
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002834 MDecl = Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2835
2836 if (Tok.getLocation() != OrigLoc) {
2837 // Due to parsing error, we either went over the cached tokens or
2838 // there are still cached tokens left. If it's the latter case skip the
2839 // leftover tokens.
2840 // Since this is an uncommon situation that should be avoided, use the
2841 // expensive isBeforeInTranslationUnit call.
2842 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2843 OrigLoc))
2844 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2845 ConsumeAnyToken();
2846 }
2847
2848 return MDecl;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002849}