blob: c8460c0e808bb15188ea3ac5692750973b5c0327 [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
John McCall8b0666c2010-08-20 18:27:03 +000014#include "clang/Parse/Parser.h"
Douglas Gregore9bba4f2010-09-15 14:51:05 +000015#include "RAIIObjectsForParser.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000018#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/Scope.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000021#include "llvm/ADT/SmallVector.h"
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +000022#include "llvm/ADT/StringExtras.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000023using namespace clang;
24
Nico Weber04e213b2013-04-03 17:36:11 +000025/// Skips attributes after an Objective-C @ directive. Emits a diagnostic.
Nico Weber69a79142013-04-04 00:15:10 +000026void Parser::MaybeSkipAttributes(tok::ObjCKeywordKind Kind) {
Nico Weber04e213b2013-04-03 17:36:11 +000027 ParsedAttributes attrs(AttrFactory);
28 if (Tok.is(tok::kw___attribute)) {
Nico Weber69a79142013-04-04 00:15:10 +000029 if (Kind == tok::objc_interface || Kind == tok::objc_protocol)
30 Diag(Tok, diag::err_objc_postfix_attribute_hint)
31 << (Kind == tok::objc_protocol);
32 else
33 Diag(Tok, diag::err_objc_postfix_attribute);
Nico Weber04e213b2013-04-03 17:36:11 +000034 ParseGNUAttributes(attrs);
35 }
36}
Chris Lattnerda59c2f2006-11-05 02:08:13 +000037
Chris Lattner3a907162008-12-08 21:53:24 +000038/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000039/// external-declaration: [C99 6.9]
40/// [OBJC] objc-class-definition
Steve Naroffe0933392007-10-29 21:39:29 +000041/// [OBJC] objc-class-declaration
42/// [OBJC] objc-alias-declaration
43/// [OBJC] objc-protocol-definition
44/// [OBJC] objc-method-definition
45/// [OBJC] '@' 'end'
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000046Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000047 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000048
Douglas Gregorf48706c2009-12-07 09:27:33 +000049 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000050 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000051 cutOffParsing();
52 return DeclGroupPtrTy();
Douglas Gregorf48706c2009-12-07 09:27:33 +000053 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000054
55 Decl *SingleDecl = 0;
Steve Naroff7c348172007-08-23 18:16:40 +000056 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000057 case tok::objc_class:
58 return ParseObjCAtClassDeclaration(AtLoc);
John McCall53fa7142010-12-24 02:08:15 +000059 case tok::objc_interface: {
John McCall084e83d2011-03-24 11:26:52 +000060 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000061 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
62 break;
John McCall53fa7142010-12-24 02:08:15 +000063 }
64 case tok::objc_protocol: {
John McCall084e83d2011-03-24 11:26:52 +000065 ParsedAttributes attrs(AttrFactory);
Douglas Gregorf6102672012-01-01 21:23:57 +000066 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall53fa7142010-12-24 02:08:15 +000067 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000068 case tok::objc_implementation:
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +000069 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000070 case tok::objc_end:
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +000071 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000072 case tok::objc_compatibility_alias:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000073 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
74 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000075 case tok::objc_synthesize:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000076 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
77 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000078 case tok::objc_dynamic:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000079 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
80 break;
Douglas Gregorc50d4922012-12-11 22:11:52 +000081 case tok::objc_import:
David Blaikiebbafb8a2012-03-11 07:00:24 +000082 if (getLangOpts().Modules)
Douglas Gregor0bf886d2012-01-03 18:24:14 +000083 return ParseModuleImport(AtLoc);
84
85 // Fall through
86
Chris Lattnerce90ef52008-08-23 02:02:23 +000087 default:
88 Diag(AtLoc, diag::err_unexpected_at);
89 SkipUntil(tok::semi);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000090 SingleDecl = 0;
91 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000092 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000093 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000094}
95
96///
Mike Stump11289f42009-09-09 15:08:12 +000097/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000098/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000099///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000100Parser::DeclGroupPtrTy
101Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000102 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000103 SmallVector<IdentifierInfo *, 8> ClassNames;
104 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremeneka26da852009-11-17 23:12:20 +0000105
Mike Stump11289f42009-09-09 15:08:12 +0000106
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000107 while (1) {
Nico Weber69a79142013-04-04 00:15:10 +0000108 MaybeSkipAttributes(tok::objc_class);
Chris Lattner0ef13522007-10-09 17:51:17 +0000109 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000110 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000111 SkipUntil(tok::semi);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000112 return Actions.ConvertDeclToDeclGroup(0);
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000113 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000114 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +0000115 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000116 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000117
Alp Toker383d2c42014-01-01 03:08:43 +0000118 if (!TryConsumeToken(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000119 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000120 }
Mike Stump11289f42009-09-09 15:08:12 +0000121
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000122 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +0000123 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@class"))
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000124 return Actions.ConvertDeclToDeclGroup(0);
Mike Stump11289f42009-09-09 15:08:12 +0000125
Ted Kremeneka26da852009-11-17 23:12:20 +0000126 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
127 ClassLocs.data(),
128 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000129}
130
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000131void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
132{
133 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
134 if (ock == Sema::OCK_None)
135 return;
136
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000137 Decl *Decl = Actions.getObjCDeclContext();
138 if (CurParsedObjCImpl) {
139 CurParsedObjCImpl->finish(AtLoc);
140 } else {
141 Actions.ActOnAtEnd(getCurScope(), AtLoc);
142 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000143 Diag(AtLoc, diag::err_objc_missing_end)
144 << FixItHint::CreateInsertion(AtLoc, "@end\n");
145 if (Decl)
146 Diag(Decl->getLocStart(), diag::note_objc_container_start)
147 << (int) ock;
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000148}
149
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000150///
151/// objc-interface:
152/// objc-class-interface-attributes[opt] objc-class-interface
153/// objc-category-interface
154///
155/// objc-class-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000156/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000157/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000158/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000159/// objc-interface-decl-list
160/// @end
161///
162/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000163/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000164/// objc-protocol-refs[opt]
165/// objc-interface-decl-list
166/// @end
167///
168/// objc-superclass:
169/// ':' identifier
170///
171/// objc-class-interface-attributes:
172/// __attribute__((visibility("default")))
173/// __attribute__((visibility("hidden")))
174/// __attribute__((deprecated))
175/// __attribute__((unavailable))
176/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardacfbe9e2012-04-06 18:12:22 +0000177/// __attribute__((objc_root_class))
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000178///
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000179Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000180 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000181 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000182 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000183 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000184 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000185
Douglas Gregor49c22a72009-11-18 16:26:39 +0000186 // Code completion after '@interface'.
187 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000188 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000189 cutOffParsing();
190 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000191 }
192
Nico Weber69a79142013-04-04 00:15:10 +0000193 MaybeSkipAttributes(tok::objc_interface);
Nico Weber04e213b2013-04-03 17:36:11 +0000194
Chris Lattner0ef13522007-10-09 17:51:17 +0000195 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000196 Diag(Tok, diag::err_expected)
197 << tok::identifier; // missing class or category name.
John McCall48871652010-08-21 09:40:31 +0000198 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000199 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000200
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000201 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000202 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000203 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000204 if (Tok.is(tok::l_paren) &&
205 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000206
207 BalancedDelimiterTracker T(*this, tok::l_paren);
208 T.consumeOpen();
209
210 SourceLocation categoryLoc;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000211 IdentifierInfo *categoryId = 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000212 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000213 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000214 cutOffParsing();
215 return 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000216 }
217
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000218 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000219 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000220 categoryId = Tok.getIdentifierInfo();
221 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000222 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000223 else if (!getLangOpts().ObjC2) {
Alp Tokerec543272013-12-24 09:48:30 +0000224 Diag(Tok, diag::err_expected)
225 << tok::identifier; // missing category name.
John McCall48871652010-08-21 09:40:31 +0000226 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000227 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000228
229 T.consumeClose();
230 if (T.getCloseLocation().isInvalid())
John McCall48871652010-08-21 09:40:31 +0000231 return 0;
Douglas Gregor0c254a02011-09-23 19:19:41 +0000232
233 if (!attrs.empty()) { // categories don't support attributes.
234 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
235 attrs.clear();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000236 }
Douglas Gregor0c254a02011-09-23 19:19:41 +0000237
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000238 // Next, we need to check for any protocol references.
239 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000240 SmallVector<Decl *, 8> ProtocolRefs;
241 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000242 if (Tok.is(tok::less) &&
243 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000244 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000245 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000246
John McCall48871652010-08-21 09:40:31 +0000247 Decl *CategoryType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000248 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000249 nameId, nameLoc,
250 categoryId, categoryLoc,
251 ProtocolRefs.data(),
252 ProtocolRefs.size(),
253 ProtocolLocs.data(),
254 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000255
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000256 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000257 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000258
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000259 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000260 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000261 }
262 // Parse a class interface.
263 IdentifierInfo *superClassId = 0;
264 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000265
Chris Lattner0ef13522007-10-09 17:51:17 +0000266 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000267 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000268
269 // Code completion of superclass names.
270 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000271 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000272 cutOffParsing();
273 return 0;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000274 }
275
Chris Lattner0ef13522007-10-09 17:51:17 +0000276 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000277 Diag(Tok, diag::err_expected)
278 << tok::identifier; // missing super class name.
John McCall48871652010-08-21 09:40:31 +0000279 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000280 }
281 superClassId = Tok.getIdentifierInfo();
282 superClassLoc = ConsumeToken();
283 }
284 // Next, we need to check for any protocol references.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000285 SmallVector<Decl *, 8> ProtocolRefs;
286 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000287 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000288 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000289 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
290 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000291 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000292
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000293 if (Tok.isNot(tok::less))
294 Actions.ActOnTypedefedProtocols(ProtocolRefs, superClassId, superClassLoc);
295
John McCall48871652010-08-21 09:40:31 +0000296 Decl *ClsType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000297 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000298 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000299 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000300 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000301 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000302
Chris Lattner0ef13522007-10-09 17:51:17 +0000303 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000304 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000305
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000306 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000307 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000308}
309
John McCall064d77b2009-12-03 22:31:13 +0000310/// The Objective-C property callback. This should be defined where
311/// it's used, but instead it's been lifted to here to support VS2005.
312struct Parser::ObjCPropertyCallback : FieldCallback {
David Blaikie68e081d2011-12-20 02:48:34 +0000313private:
314 virtual void anchor();
315public:
John McCall064d77b2009-12-03 22:31:13 +0000316 Parser &P;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000317 SmallVectorImpl<Decl *> &Props;
John McCall064d77b2009-12-03 22:31:13 +0000318 ObjCDeclSpec &OCDS;
319 SourceLocation AtLoc;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000320 SourceLocation LParenLoc;
John McCall064d77b2009-12-03 22:31:13 +0000321 tok::ObjCKeywordKind MethodImplKind;
322
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000323 ObjCPropertyCallback(Parser &P,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000324 SmallVectorImpl<Decl *> &Props,
John McCall064d77b2009-12-03 22:31:13 +0000325 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000326 SourceLocation LParenLoc,
John McCall064d77b2009-12-03 22:31:13 +0000327 tok::ObjCKeywordKind MethodImplKind) :
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000328 P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc), LParenLoc(LParenLoc),
John McCall064d77b2009-12-03 22:31:13 +0000329 MethodImplKind(MethodImplKind) {
330 }
331
Eli Friedman934dbbf2012-08-08 23:53:27 +0000332 void invoke(ParsingFieldDeclarator &FD) {
John McCall064d77b2009-12-03 22:31:13 +0000333 if (FD.D.getIdentifier() == 0) {
334 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
335 << FD.D.getSourceRange();
Eli Friedman934dbbf2012-08-08 23:53:27 +0000336 return;
John McCall064d77b2009-12-03 22:31:13 +0000337 }
338 if (FD.BitfieldSize) {
339 P.Diag(AtLoc, diag::err_objc_property_bitfield)
340 << FD.D.getSourceRange();
Eli Friedman934dbbf2012-08-08 23:53:27 +0000341 return;
John McCall064d77b2009-12-03 22:31:13 +0000342 }
343
344 // Install the property declarator into interfaceDecl.
345 IdentifierInfo *SelName =
346 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
347
348 Selector GetterSel =
349 P.PP.getSelectorTable().getNullarySelector(SelName);
350 IdentifierInfo *SetterName = OCDS.getSetterName();
351 Selector SetterSel;
352 if (SetterName)
353 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
354 else
Adrian Prantla4ce9062013-06-07 22:29:12 +0000355 SetterSel =
356 SelectorTable::constructSetterSelector(P.PP.getIdentifierTable(),
357 P.PP.getSelectorTable(),
358 FD.D.getIdentifier());
John McCall064d77b2009-12-03 22:31:13 +0000359 bool isOverridingProperty = false;
John McCall48871652010-08-21 09:40:31 +0000360 Decl *Property =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000361 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, LParenLoc,
362 FD, OCDS,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000363 GetterSel, SetterSel,
John McCall064d77b2009-12-03 22:31:13 +0000364 &isOverridingProperty,
365 MethodImplKind);
366 if (!isOverridingProperty)
367 Props.push_back(Property);
368
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000369 FD.complete(Property);
John McCall064d77b2009-12-03 22:31:13 +0000370 }
371};
372
David Blaikie68e081d2011-12-20 02:48:34 +0000373void Parser::ObjCPropertyCallback::anchor() {
374}
375
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000376/// objc-interface-decl-list:
377/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000378/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000379/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000380/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000381/// objc-interface-decl-list declaration
382/// objc-interface-decl-list ';'
383///
Steve Naroff99264b42007-08-22 16:35:03 +0000384/// objc-method-requirement: [OBJC2]
385/// @required
386/// @optional
387///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000388void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
389 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000390 SmallVector<Decl *, 32> allMethods;
391 SmallVector<Decl *, 16> allProperties;
392 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000393 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000394
Ted Kremenekc7c64312010-01-07 01:20:12 +0000395 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000396
Steve Naroff99264b42007-08-22 16:35:03 +0000397 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000398 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000399 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000400 if (Decl *methodPrototype =
401 ParseObjCMethodPrototype(MethodImplKind, false))
402 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000403 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
404 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000405 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
406 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000407 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000408 if (Tok.is(tok::semi))
409 ConsumeToken();
410 }
Steve Naroff99264b42007-08-22 16:35:03 +0000411 continue;
412 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000413 if (Tok.is(tok::l_paren)) {
414 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000415 ParseObjCMethodDecl(Tok.getLocation(),
416 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000417 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000418 continue;
419 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000420 // Ignore excess semicolons.
421 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000422 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000423 continue;
424 }
Mike Stump11289f42009-09-09 15:08:12 +0000425
Chris Lattnerda9fb152008-10-20 06:10:06 +0000426 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000427 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000428 break;
Mike Stump11289f42009-09-09 15:08:12 +0000429
Douglas Gregorf1934162010-01-13 21:24:21 +0000430 // Code completion within an Objective-C interface.
431 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000432 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000433 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000434 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000435 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000436 }
437
Chris Lattner038a3e32008-10-20 05:46:22 +0000438 // If we don't have an @ directive, parse it as a function definition.
439 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000440 // The code below does not consume '}'s because it is afraid of eating the
441 // end of a namespace. Because of the way this code is structured, an
442 // erroneous r_brace would cause an infinite loop if not handled here.
443 if (Tok.is(tok::r_brace))
444 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000445 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000446 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000447 continue;
448 }
Mike Stump11289f42009-09-09 15:08:12 +0000449
Chris Lattner038a3e32008-10-20 05:46:22 +0000450 // Otherwise, we have an @ directive, eat the @.
451 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000452 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000453 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000454 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000455 }
456
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000457 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000458
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000459 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000460 AtEnd.setBegin(AtLoc);
461 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000462 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000463 } else if (DirectiveKind == tok::objc_not_keyword) {
464 Diag(Tok, diag::err_objc_unknown_at);
465 SkipUntil(tok::semi);
466 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000467 }
Mike Stump11289f42009-09-09 15:08:12 +0000468
Chris Lattnerda9fb152008-10-20 06:10:06 +0000469 // Eat the identifier.
470 ConsumeToken();
471
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000472 switch (DirectiveKind) {
473 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000474 // FIXME: If someone forgets an @end on a protocol, this loop will
475 // continue to eat up tons of stuff and spew lots of nonsense errors. It
476 // would probably be better to bail out if we saw an @class or @interface
477 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000478 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000479 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000480 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000481 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000482
483 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000484 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000485 Diag(AtLoc, diag::err_objc_missing_end)
486 << FixItHint::CreateInsertion(AtLoc, "@end\n");
487 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
488 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000489 ConsumeToken();
490 break;
491
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000492 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000493 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000494 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000495 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000496 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000497 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000498 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000499 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000500 break;
Mike Stump11289f42009-09-09 15:08:12 +0000501
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000502 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000503 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000504 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000505
Chris Lattner038a3e32008-10-20 05:46:22 +0000506 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000507 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000508 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000509 if (Tok.is(tok::l_paren)) {
510 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000511 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000512 }
Mike Stump11289f42009-09-09 15:08:12 +0000513
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000514 ObjCPropertyCallback Callback(*this, allProperties,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000515 OCDS, AtLoc, LParenLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000516
Chris Lattner038a3e32008-10-20 05:46:22 +0000517 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000518 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +0000519 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000520
John McCall405988b2011-03-26 01:53:26 +0000521 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000522 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000523 }
Steve Naroff99264b42007-08-22 16:35:03 +0000524 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000525
526 // We break out of the big loop in two cases: when we see @end or when we see
527 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000528 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000529 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000530 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000531 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000532 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000533 } else {
534 Diag(Tok, diag::err_objc_missing_end)
535 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
536 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
537 << (int) Actions.getObjCContainerKind();
538 AtEnd.setBegin(Tok.getLocation());
539 AtEnd.setEnd(Tok.getLocation());
540 }
Mike Stump11289f42009-09-09 15:08:12 +0000541
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000542 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000543 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000544 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000545}
546
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000547/// Parse property attribute declarations.
548///
549/// property-attr-decl: '(' property-attrlist ')'
550/// property-attrlist:
551/// property-attribute
552/// property-attrlist ',' property-attribute
553/// property-attribute:
554/// getter '=' identifier
555/// setter '=' identifier ':'
556/// readonly
557/// readwrite
558/// assign
559/// retain
560/// copy
561/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000562/// atomic
563/// strong
564/// weak
565/// unsafe_unretained
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000566///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000567void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000568 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000569 BalancedDelimiterTracker T(*this, tok::l_paren);
570 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000571
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000572 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000573 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000574 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000575 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000576 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000577 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000578
Chris Lattner76619232008-10-20 07:22:18 +0000579 // If this is not an identifier at all, bail out early.
580 if (II == 0) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000581 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000582 return;
583 }
Mike Stump11289f42009-09-09 15:08:12 +0000584
Chris Lattner1db33542008-10-20 07:37:22 +0000585 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000586
Chris Lattner68e48682008-11-20 04:42:34 +0000587 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000588 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000589 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000590 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000591 else if (II->isStr("unsafe_unretained"))
592 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000593 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000594 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000595 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000596 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000597 else if (II->isStr("strong"))
598 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000599 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000600 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000601 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000602 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000603 else if (II->isStr("atomic"))
604 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000605 else if (II->isStr("weak"))
606 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000607 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000608 bool IsSetter = II->getNameStart()[0] == 's';
609
Chris Lattner825bca12008-10-20 07:39:53 +0000610 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000611 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
612 diag::err_objc_expected_equal_for_getter;
613
Alp Toker383d2c42014-01-01 03:08:43 +0000614 if (ExpectAndConsume(tok::equal, DiagID)) {
615 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000616 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000617 }
Mike Stump11289f42009-09-09 15:08:12 +0000618
Douglas Gregorc8537c52009-11-19 07:41:15 +0000619 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000620 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000621 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000622 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000623 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000624 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000625 }
626
Anders Carlssonfe15a782010-10-02 17:45:21 +0000627
628 SourceLocation SelLoc;
629 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
630
631 if (!SelIdent) {
632 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
633 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000634 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000635 return;
636 }
Mike Stump11289f42009-09-09 15:08:12 +0000637
Anders Carlssonfe15a782010-10-02 17:45:21 +0000638 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000639 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000640 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000641
Alp Toker383d2c42014-01-01 03:08:43 +0000642 if (ExpectAndConsume(tok::colon,
643 diag::err_expected_colon_after_setter_name)) {
644 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000645 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000646 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000647 } else {
648 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000649 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000650 }
Chris Lattner825bca12008-10-20 07:39:53 +0000651 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000652 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000653 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000654 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000655 }
Mike Stump11289f42009-09-09 15:08:12 +0000656
Chris Lattner1db33542008-10-20 07:37:22 +0000657 if (Tok.isNot(tok::comma))
658 break;
Mike Stump11289f42009-09-09 15:08:12 +0000659
Chris Lattner1db33542008-10-20 07:37:22 +0000660 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000661 }
Mike Stump11289f42009-09-09 15:08:12 +0000662
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000663 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000664}
665
Steve Naroff09bf8152007-09-06 21:24:23 +0000666/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000667/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000668/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000669///
670/// objc-instance-method: '-'
671/// objc-class-method: '+'
672///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000673/// objc-method-attributes: [OBJC2]
674/// __attribute__((deprecated))
675///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000676Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000677 bool MethodDefinition) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000678 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000679
Mike Stump11289f42009-09-09 15:08:12 +0000680 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000681 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000682 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000683 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000684 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000685 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000686 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000687}
688
689/// objc-selector:
690/// identifier
691/// one of
692/// enum struct union if else while do for switch case default
693/// break continue return goto asm sizeof typeof __alignof
694/// unsigned long const short volatile signed restrict _Complex
695/// in out inout bycopy byref oneway int char float double void _Bool
696///
Chris Lattner4f472a32009-04-11 18:13:45 +0000697IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000698
Chris Lattner5700fab2007-10-07 02:00:24 +0000699 switch (Tok.getKind()) {
700 default:
701 return 0;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000702 case tok::ampamp:
703 case tok::ampequal:
704 case tok::amp:
705 case tok::pipe:
706 case tok::tilde:
707 case tok::exclaim:
708 case tok::exclaimequal:
709 case tok::pipepipe:
710 case tok::pipeequal:
711 case tok::caret:
712 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000713 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +0000714 if (isLetter(ThisTok[0])) {
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000715 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
716 Tok.setKind(tok::identifier);
717 SelectorLoc = ConsumeToken();
718 return II;
719 }
720 return 0;
721 }
722
Chris Lattner5700fab2007-10-07 02:00:24 +0000723 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000724 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000725 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000726 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000727 case tok::kw_break:
728 case tok::kw_case:
729 case tok::kw_catch:
730 case tok::kw_char:
731 case tok::kw_class:
732 case tok::kw_const:
733 case tok::kw_const_cast:
734 case tok::kw_continue:
735 case tok::kw_default:
736 case tok::kw_delete:
737 case tok::kw_do:
738 case tok::kw_double:
739 case tok::kw_dynamic_cast:
740 case tok::kw_else:
741 case tok::kw_enum:
742 case tok::kw_explicit:
743 case tok::kw_export:
744 case tok::kw_extern:
745 case tok::kw_false:
746 case tok::kw_float:
747 case tok::kw_for:
748 case tok::kw_friend:
749 case tok::kw_goto:
750 case tok::kw_if:
751 case tok::kw_inline:
752 case tok::kw_int:
753 case tok::kw_long:
754 case tok::kw_mutable:
755 case tok::kw_namespace:
756 case tok::kw_new:
757 case tok::kw_operator:
758 case tok::kw_private:
759 case tok::kw_protected:
760 case tok::kw_public:
761 case tok::kw_register:
762 case tok::kw_reinterpret_cast:
763 case tok::kw_restrict:
764 case tok::kw_return:
765 case tok::kw_short:
766 case tok::kw_signed:
767 case tok::kw_sizeof:
768 case tok::kw_static:
769 case tok::kw_static_cast:
770 case tok::kw_struct:
771 case tok::kw_switch:
772 case tok::kw_template:
773 case tok::kw_this:
774 case tok::kw_throw:
775 case tok::kw_true:
776 case tok::kw_try:
777 case tok::kw_typedef:
778 case tok::kw_typeid:
779 case tok::kw_typename:
780 case tok::kw_typeof:
781 case tok::kw_union:
782 case tok::kw_unsigned:
783 case tok::kw_using:
784 case tok::kw_virtual:
785 case tok::kw_void:
786 case tok::kw_volatile:
787 case tok::kw_wchar_t:
788 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000789 case tok::kw__Bool:
790 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000791 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000792 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000793 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000794 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000795 }
Steve Naroff99264b42007-08-22 16:35:03 +0000796}
797
Fariborz Jahanian83615522008-01-02 22:54:34 +0000798/// objc-for-collection-in: 'in'
799///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000800bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000801 // FIXME: May have to do additional look-ahead to only allow for
802 // valid tokens following an 'in'; such as an identifier, unary operators,
803 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000804 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000805 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000806}
807
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000808/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000809/// qualifier list and builds their bitmask representation in the input
810/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000811///
812/// objc-type-qualifiers:
813/// objc-type-qualifier
814/// objc-type-qualifiers objc-type-qualifier
815///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000816void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000817 Declarator::TheContext Context) {
818 assert(Context == Declarator::ObjCParameterContext ||
819 Context == Declarator::ObjCResultContext);
820
Chris Lattner61511e12007-12-12 06:56:32 +0000821 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000822 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000823 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +0000824 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000825 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +0000826 }
827
Chris Lattner5e530bc2007-12-27 19:57:00 +0000828 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000829 return;
Mike Stump11289f42009-09-09 15:08:12 +0000830
Chris Lattner61511e12007-12-12 06:56:32 +0000831 const IdentifierInfo *II = Tok.getIdentifierInfo();
832 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000833 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000834 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000835
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000836 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000837 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +0000838 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000839 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
840 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
841 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
842 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
843 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
844 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000845 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000846 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000847 ConsumeToken();
848 II = 0;
849 break;
850 }
Mike Stump11289f42009-09-09 15:08:12 +0000851
Chris Lattner61511e12007-12-12 06:56:32 +0000852 // If this wasn't a recognized qualifier, bail out.
853 if (II) return;
854 }
855}
856
John McCalla55902b2011-10-01 09:56:14 +0000857/// Take all the decl attributes out of the given list and add
858/// them to the given attribute set.
859static void takeDeclAttributes(ParsedAttributes &attrs,
860 AttributeList *list) {
861 while (list) {
862 AttributeList *cur = list;
863 list = cur->getNext();
864
865 if (!cur->isUsedAsTypeAttr()) {
866 // Clear out the next pointer. We're really completely
867 // destroying the internal invariants of the declarator here,
868 // but it doesn't matter because we're done with it.
869 cur->setNext(0);
870 attrs.add(cur);
871 }
872 }
873}
874
875/// takeDeclAttributes - Take all the decl attributes from the given
876/// declarator and add them to the given list.
877static void takeDeclAttributes(ParsedAttributes &attrs,
878 Declarator &D) {
879 // First, take ownership of all attributes.
880 attrs.getPool().takeAllFrom(D.getAttributePool());
881 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
882
883 // Now actually move the attributes over.
884 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
885 takeDeclAttributes(attrs, D.getAttributes());
886 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
887 takeDeclAttributes(attrs,
888 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
889}
890
Chris Lattner61511e12007-12-12 06:56:32 +0000891/// objc-type-name:
892/// '(' objc-type-qualifiers[opt] type-name ')'
893/// '(' objc-type-qualifiers[opt] ')'
894///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000895ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000896 Declarator::TheContext context,
897 ParsedAttributes *paramAttrs) {
898 assert(context == Declarator::ObjCParameterContext ||
899 context == Declarator::ObjCResultContext);
900 assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
901
Chris Lattner0ef13522007-10-09 17:51:17 +0000902 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000903
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000904 BalancedDelimiterTracker T(*this, tok::l_paren);
905 T.consumeOpen();
906
Chris Lattner2ebb1782008-08-23 01:48:03 +0000907 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000908 ObjCDeclContextSwitch ObjCDC(*this);
909
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000910 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +0000911 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000912
John McCallba7bf592010-08-24 05:47:05 +0000913 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000914 if (isTypeSpecifierQualifier()) {
John McCalla55902b2011-10-01 09:56:14 +0000915 // Parse an abstract declarator.
916 DeclSpec declSpec(AttrFactory);
917 declSpec.setObjCQualifiers(&DS);
918 ParseSpecifierQualifierList(declSpec);
Fariborz Jahanianb6499eb2012-05-29 21:52:45 +0000919 declSpec.SetRangeEnd(Tok.getLocation());
John McCalla55902b2011-10-01 09:56:14 +0000920 Declarator declarator(declSpec, context);
921 ParseDeclarator(declarator);
922
923 // If that's not invalid, extract a type.
924 if (!declarator.isInvalidType()) {
925 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
926 if (!type.isInvalid())
927 Ty = type.get();
928
929 // If we're parsing a parameter, steal all the decl attributes
930 // and add them to the decl spec.
931 if (context == Declarator::ObjCParameterContext)
932 takeDeclAttributes(*paramAttrs, declarator);
933 }
934 } else if (context == Declarator::ObjCResultContext &&
935 Tok.is(tok::identifier)) {
Douglas Gregorbab8a962011-09-08 01:46:34 +0000936 if (!Ident_instancetype)
937 Ident_instancetype = PP.getIdentifierInfo("instancetype");
938
939 if (Tok.getIdentifierInfo() == Ident_instancetype) {
940 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
941 ConsumeToken();
942 }
Douglas Gregor220cac52009-02-18 17:45:20 +0000943 }
Douglas Gregorbab8a962011-09-08 01:46:34 +0000944
Steve Naroff90255b42008-10-21 14:15:04 +0000945 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000946 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000947 else if (Tok.getLocation() == TypeStartLoc) {
948 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000949 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000950 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +0000951 } else {
952 // Otherwise, we found *something*, but didn't get a ')' in the right
953 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000954 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000955 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000956 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000957}
958
959/// objc-method-decl:
960/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000961/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000962/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000963/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000964///
965/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000966/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000967/// objc-keyword-selector objc-keyword-decl
968///
969/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000970/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
971/// objc-selector ':' objc-keyword-attributes[opt] identifier
972/// ':' objc-type-name objc-keyword-attributes[opt] identifier
973/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000974///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000975/// objc-parmlist:
976/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000977///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000978/// objc-parms:
979/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000980///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000981/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000982/// , ...
983///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000984/// objc-keyword-attributes: [OBJC2]
985/// __attribute__((unused))
986///
John McCall48871652010-08-21 09:40:31 +0000987Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000988 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000989 tok::ObjCKeywordKind MethodImplKind,
990 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +0000991 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +0000992
Douglas Gregor636a61e2010-04-07 00:21:17 +0000993 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000994 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000995 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000996 cutOffParsing();
997 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000998 }
999
Chris Lattner2ebb1782008-08-23 01:48:03 +00001000 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001001 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001002 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001003 if (Tok.is(tok::l_paren))
John McCalla55902b2011-10-01 09:56:14 +00001004 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001005
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001006 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001007 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001008 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001009 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001010
Douglas Gregor636a61e2010-04-07 00:21:17 +00001011 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001012 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001013 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001014 cutOffParsing();
1015 return 0;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001016 }
1017
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001018 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001019 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001020 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001021
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001022 // An unnamed colon is valid.
1023 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001024 Diag(Tok, diag::err_expected_selector_for_method)
1025 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001026 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001027 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
John McCall48871652010-08-21 09:40:31 +00001028 return 0;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001029 }
Mike Stump11289f42009-09-09 15:08:12 +00001030
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001031 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001032 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001033 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001034 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001035 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001036
Chris Lattner5700fab2007-10-07 02:00:24 +00001037 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001038 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001039 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001040 mType, DSRet, ReturnType,
Douglas Gregor33823722011-06-11 01:09:30 +00001041 selLoc, Sel, 0,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001042 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001043 methodAttrs.getList(), MethodImplKind,
1044 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001045 PD.complete(Result);
1046 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001047 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001048
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001049 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001050 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001051 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001052 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1053 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001054
1055 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001056 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001057 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001058 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001059
Chris Lattner5700fab2007-10-07 02:00:24 +00001060 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001061 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001062 break;
Mike Stump11289f42009-09-09 15:08:12 +00001063
John McCallba7bf592010-08-24 05:47:05 +00001064 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001065 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001066 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1067 Declarator::ObjCParameterContext,
1068 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001069
Chris Lattner5700fab2007-10-07 02:00:24 +00001070 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001071 // Regardless, collect all the attributes we've parsed so far.
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001072 ArgInfo.ArgAttrs = 0;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001073 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001074 MaybeParseGNUAttributes(paramAttrs);
1075 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001076 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001077
Douglas Gregor45879692010-07-08 23:37:41 +00001078 // Code completion for the next piece of the selector.
1079 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001080 KeyIdents.push_back(SelIdent);
1081 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1082 mType == tok::minus,
1083 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001084 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001085 cutOffParsing();
1086 return 0;
Douglas Gregor45879692010-07-08 23:37:41 +00001087 }
1088
Chris Lattner0ef13522007-10-09 17:51:17 +00001089 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001090 Diag(Tok, diag::err_expected)
1091 << tok::identifier; // missing argument name.
Chris Lattner5700fab2007-10-07 02:00:24 +00001092 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001093 }
Mike Stump11289f42009-09-09 15:08:12 +00001094
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001095 ArgInfo.Name = Tok.getIdentifierInfo();
1096 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001097 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001098
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001099 ArgInfos.push_back(ArgInfo);
1100 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001101 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001102
John McCall084e83d2011-03-24 11:26:52 +00001103 // Make sure the attributes persist.
1104 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1105
Douglas Gregor95887f92010-07-08 23:20:03 +00001106 // Code completion for the next piece of the selector.
1107 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001108 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1109 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001110 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001111 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001112 cutOffParsing();
1113 return 0;
Douglas Gregor95887f92010-07-08 23:20:03 +00001114 }
1115
Chris Lattner5700fab2007-10-07 02:00:24 +00001116 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001117 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001118 if (!SelIdent && Tok.isNot(tok::colon))
1119 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001120 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001121 SourceLocation ColonLoc = Tok.getLocation();
1122 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001123 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1124 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1125 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001126 }
1127 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001128 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001129 }
Mike Stump11289f42009-09-09 15:08:12 +00001130
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001131 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001132 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001133 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001134 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001135 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001136 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001137 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001138 ConsumeToken();
1139 break;
1140 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001141 if (!cStyleParamWarned) {
1142 Diag(Tok, diag::warn_cstyle_param);
1143 cStyleParamWarned = true;
1144 }
John McCall084e83d2011-03-24 11:26:52 +00001145 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001146 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001147 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001148 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1149 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001150 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001151 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001152 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1153 ParmDecl.getIdentifierLoc(),
1154 Param,
1155 0));
Chris Lattner5700fab2007-10-07 02:00:24 +00001156 }
Mike Stump11289f42009-09-09 15:08:12 +00001157
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001158 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001159 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001160 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001161 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001162
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001163 if (KeyIdents.size() == 0)
John McCall48871652010-08-21 09:40:31 +00001164 return 0;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001165
Chris Lattner5700fab2007-10-07 02:00:24 +00001166 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1167 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001168 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001169 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001170 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001171 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001172 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001173 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001174 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001175
John McCall28a6aea2009-11-04 02:18:39 +00001176 PD.complete(Result);
1177 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001178}
1179
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001180/// objc-protocol-refs:
1181/// '<' identifier-list '>'
1182///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001183bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001184ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1185 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001186 bool WarnOnDeclarations,
1187 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001188 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001189
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001190 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001191
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001192 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001193
Chris Lattner3bbae002008-07-26 04:03:38 +00001194 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001195 if (Tok.is(tok::code_completion)) {
1196 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1197 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001198 cutOffParsing();
1199 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001200 }
1201
Chris Lattner3bbae002008-07-26 04:03:38 +00001202 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001203 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001204 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001205 return true;
1206 }
1207 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1208 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001209 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001210 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001211
Alp Toker383d2c42014-01-01 03:08:43 +00001212 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001213 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001214 }
Mike Stump11289f42009-09-09 15:08:12 +00001215
Chris Lattner3bbae002008-07-26 04:03:38 +00001216 // Consume the '>'.
Nico Weber7aa4a882012-12-14 18:22:38 +00001217 if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true))
Chris Lattner3bbae002008-07-26 04:03:38 +00001218 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001219
Chris Lattner3bbae002008-07-26 04:03:38 +00001220 // Convert the list of protocols identifiers into a list of protocol decls.
1221 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1222 &ProtocolIdents[0], ProtocolIdents.size(),
1223 Protocols);
1224 return false;
1225}
1226
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001227/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1228/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001229bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001230 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001231 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001232 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001233 SmallVector<Decl *, 8> ProtocolDecl;
1234 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001235 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1236 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001237 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1238 ProtocolLocs.data(), LAngleLoc);
1239 if (EndProtoLoc.isValid())
1240 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001241 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001242}
1243
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001244void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1245 BalancedDelimiterTracker &T,
1246 SmallVectorImpl<Decl *> &AllIvarDecls,
1247 bool RBraceMissing) {
1248 if (!RBraceMissing)
1249 T.consumeClose();
1250
1251 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1252 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1253 Actions.ActOnObjCContainerFinishDefinition();
1254 // Call ActOnFields() even if we don't have any decls. This is useful
1255 // for code rewriting tools that need to be aware of the empty list.
1256 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1257 AllIvarDecls,
1258 T.getOpenLocation(), T.getCloseLocation(), 0);
1259}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001260
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001261/// objc-class-instance-variables:
1262/// '{' objc-instance-variable-decl-list[opt] '}'
1263///
1264/// objc-instance-variable-decl-list:
1265/// objc-visibility-spec
1266/// objc-instance-variable-decl ';'
1267/// ';'
1268/// objc-instance-variable-decl-list objc-visibility-spec
1269/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1270/// objc-instance-variable-decl-list ';'
1271///
1272/// objc-visibility-spec:
1273/// @private
1274/// @protected
1275/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001276/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001277///
1278/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001279/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001280///
John McCall48871652010-08-21 09:40:31 +00001281void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001282 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001283 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001284 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001285 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001286
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001287 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001288 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001289
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001290 BalancedDelimiterTracker T(*this, tok::l_brace);
1291 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001292 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001293 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001294 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001295
Steve Naroff00433d32007-08-21 21:17:12 +00001296 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001297 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001298 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001299 continue;
1300 }
Mike Stump11289f42009-09-09 15:08:12 +00001301
Steve Naroff00433d32007-08-21 21:17:12 +00001302 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001303 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001304 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001305 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001306 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001307 }
1308
Steve Naroff7c348172007-08-23 18:16:40 +00001309 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001310 case tok::objc_private:
1311 case tok::objc_public:
1312 case tok::objc_protected:
1313 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001314 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001315 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001316 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001317
1318 case tok::objc_end:
1319 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001320 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1321 Tok.setKind(tok::at);
1322 Tok.setLength(1);
1323 PP.EnterToken(Tok);
1324 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1325 T, AllIvarDecls, true);
1326 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001327
1328 default:
1329 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1330 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001331 }
1332 }
Mike Stump11289f42009-09-09 15:08:12 +00001333
Douglas Gregor48d46252010-01-13 21:54:15 +00001334 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001335 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001336 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001337 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001338 }
1339
John McCallcfefb6d2009-11-03 02:38:08 +00001340 struct ObjCIvarCallback : FieldCallback {
1341 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001342 Decl *IDecl;
John McCallcfefb6d2009-11-03 02:38:08 +00001343 tok::ObjCKeywordKind visibility;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001344 SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001345
John McCall48871652010-08-21 09:40:31 +00001346 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001347 SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001348 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1349 }
1350
Eli Friedman934dbbf2012-08-08 23:53:27 +00001351 void invoke(ParsingFieldDeclarator &FD) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001352 P.Actions.ActOnObjCContainerStartDefinition(IDecl);
John McCallcfefb6d2009-11-03 02:38:08 +00001353 // Install the declarator into the interface decl.
John McCall48871652010-08-21 09:40:31 +00001354 Decl *Field
Douglas Gregor0be31a22010-07-02 17:43:08 +00001355 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallcfefb6d2009-11-03 02:38:08 +00001356 FD.D.getDeclSpec().getSourceRange().getBegin(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001357 FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian4327b322011-08-29 17:33:12 +00001358 P.Actions.ActOnObjCContainerFinishDefinition();
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001359 if (Field)
1360 AllIvarDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001361 FD.complete(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001362 }
1363 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001364
Chris Lattnera12405b2008-04-10 06:46:29 +00001365 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001366 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001367 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001368
Chris Lattner0ef13522007-10-09 17:51:17 +00001369 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001370 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001371 } else {
1372 Diag(Tok, diag::err_expected_semi_decl_list);
1373 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001374 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001375 }
1376 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001377 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1378 T, AllIvarDecls, false);
Steve Naroff00433d32007-08-21 21:17:12 +00001379 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001380}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001381
1382/// objc-protocol-declaration:
1383/// objc-protocol-definition
1384/// objc-protocol-forward-reference
1385///
1386/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00001387/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00001388/// objc-protocol-refs[opt]
1389/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00001390/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001391///
1392/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00001393/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001394///
James Dennett1355bd12012-06-11 06:19:40 +00001395/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001396/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001397/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001398Parser::DeclGroupPtrTy
1399Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1400 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001401 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001402 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1403 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001404
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001405 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001406 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001407 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00001408 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001409 }
1410
Nico Weber69a79142013-04-04 00:15:10 +00001411 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00001412
Chris Lattner0ef13522007-10-09 17:51:17 +00001413 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001414 Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00001415 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001416 }
1417 // Save the protocol name, then consume it.
1418 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1419 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001420
Alp Toker383d2c42014-01-01 03:08:43 +00001421 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001422 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001423 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001424 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001425 }
Mike Stump11289f42009-09-09 15:08:12 +00001426
Erik Verbruggenf9887852011-12-08 09:58:43 +00001427 CheckNestedObjCContexts(AtLoc);
1428
Chris Lattner0ef13522007-10-09 17:51:17 +00001429 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001430 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001431 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1432
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001433 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001434 while (1) {
1435 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001436 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001437 Diag(Tok, diag::err_expected) << tok::identifier;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001438 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00001439 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001440 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001441 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1442 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001443 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001444
Chris Lattner0ef13522007-10-09 17:51:17 +00001445 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001446 break;
1447 }
1448 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00001449 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00001450 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001451
Steve Naroff93eb5f12007-10-10 17:32:04 +00001452 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001453 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001454 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001455 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001456 }
Mike Stump11289f42009-09-09 15:08:12 +00001457
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001458 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001459 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001460
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001461 SmallVector<Decl *, 8> ProtocolRefs;
1462 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001463 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001464 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1465 LAngleLoc, EndProtoLoc))
Douglas Gregorf6102672012-01-01 21:23:57 +00001466 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001467
John McCall48871652010-08-21 09:40:31 +00001468 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001469 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001470 ProtocolRefs.data(),
1471 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001472 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001473 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001474
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001475 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00001476 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001477}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001478
1479/// objc-implementation:
1480/// objc-class-implementation-prologue
1481/// objc-category-implementation-prologue
1482///
1483/// objc-class-implementation-prologue:
1484/// @implementation identifier objc-superclass[opt]
1485/// objc-class-instance-variables[opt]
1486///
1487/// objc-category-implementation-prologue:
1488/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001489Parser::DeclGroupPtrTy
1490Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001491 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1492 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001493 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001494 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001495
Douglas Gregor49c22a72009-11-18 16:26:39 +00001496 // Code completion after '@implementation'.
1497 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001498 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001499 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001500 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001501 }
1502
Nico Weber69a79142013-04-04 00:15:10 +00001503 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00001504
Chris Lattner0ef13522007-10-09 17:51:17 +00001505 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001506 Diag(Tok, diag::err_expected)
1507 << tok::identifier; // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001508 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001509 }
1510 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001511 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001512 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001513 Decl *ObjCImpDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001514
1515 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001516 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001517 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001518 SourceLocation categoryLoc, rparenLoc;
1519 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001520
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001521 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001522 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001523 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001524 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001525 }
1526
Chris Lattner0ef13522007-10-09 17:51:17 +00001527 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001528 categoryId = Tok.getIdentifierInfo();
1529 categoryLoc = ConsumeToken();
1530 } else {
Alp Tokerec543272013-12-24 09:48:30 +00001531 Diag(Tok, diag::err_expected)
1532 << tok::identifier; // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001533 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001534 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001535 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001536 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001537 SkipUntil(tok::r_paren); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001538 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001539 }
1540 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00001541 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1542 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1543 AttributeFactory attr;
1544 DeclSpec DS(attr);
1545 (void)ParseObjCProtocolQualifiers(DS);
1546 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001547 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001548 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001549 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001550
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001551 } else {
1552 // We have a class implementation
1553 SourceLocation superClassLoc;
1554 IdentifierInfo *superClassId = 0;
Alp Toker383d2c42014-01-01 03:08:43 +00001555 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001556 // We have a super class
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001557 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001558 Diag(Tok, diag::err_expected)
1559 << tok::identifier; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001560 return DeclGroupPtrTy();
1561 }
1562 superClassId = Tok.getIdentifierInfo();
1563 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001564 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001565 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1566 AtLoc, nameId, nameLoc,
1567 superClassId, superClassLoc);
1568
1569 if (Tok.is(tok::l_brace)) // we have ivars
1570 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00001571 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1572 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1573 // try to recover.
1574 AttributeFactory attr;
1575 DeclSpec DS(attr);
1576 (void)ParseObjCProtocolQualifiers(DS);
1577 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001578 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001579 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001580
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001581 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001582
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001583 {
1584 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00001585 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001586 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001587 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001588 MaybeParseMicrosoftAttributes(attrs);
1589 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1590 DeclGroupRef DG = DGP.get();
1591 DeclsInGroup.append(DG.begin(), DG.end());
1592 }
1593 }
1594 }
1595
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001596 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001597}
Steve Naroff33a1e802007-10-29 21:38:07 +00001598
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001599Parser::DeclGroupPtrTy
1600Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001601 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1602 "ParseObjCAtEndDeclaration(): Expected @end");
1603 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001604 if (CurParsedObjCImpl)
1605 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001606 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00001607 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001608 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001609 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001610}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001611
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001612Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1613 if (!Finished) {
1614 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00001615 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001616 P.Diag(P.Tok, diag::err_objc_missing_end)
1617 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1618 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1619 << Sema::OCK_Implementation;
1620 }
1621 }
1622 P.CurParsedObjCImpl = 0;
1623 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001624}
1625
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001626void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1627 assert(!Finished);
1628 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1629 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001630 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1631 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001632
1633 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1634
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001635 if (HasCFunction)
1636 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1637 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1638 false/*c-functions*/);
1639
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001640 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001641 for (LateParsedObjCMethodContainer::iterator
1642 I = LateParsedObjCMethods.begin(),
1643 E = LateParsedObjCMethods.end(); I != E; ++I)
1644 delete *I;
1645 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001646
1647 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001648}
1649
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001650/// compatibility-alias-decl:
1651/// @compatibility_alias alias-name class-name ';'
1652///
John McCall48871652010-08-21 09:40:31 +00001653Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001654 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1655 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1656 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001657 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001658 Diag(Tok, diag::err_expected) << tok::identifier;
John McCall48871652010-08-21 09:40:31 +00001659 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001660 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001661 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1662 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001663 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001664 Diag(Tok, diag::err_expected) << tok::identifier;
John McCall48871652010-08-21 09:40:31 +00001665 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001666 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001667 IdentifierInfo *classId = Tok.getIdentifierInfo();
1668 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00001669 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00001670 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1671 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001672}
1673
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001674/// property-synthesis:
1675/// @synthesize property-ivar-list ';'
1676///
1677/// property-ivar-list:
1678/// property-ivar
1679/// property-ivar-list ',' property-ivar
1680///
1681/// property-ivar:
1682/// identifier
1683/// identifier '=' identifier
1684///
John McCall48871652010-08-21 09:40:31 +00001685Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001686 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00001687 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001688 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001689
Douglas Gregor88e72a02009-11-18 19:45:45 +00001690 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001691 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001692 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001693 cutOffParsing();
1694 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001695 }
1696
Douglas Gregor88e72a02009-11-18 19:45:45 +00001697 if (Tok.isNot(tok::identifier)) {
1698 Diag(Tok, diag::err_synthesized_property_name);
1699 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001700 return 0;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001701 }
1702
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001703 IdentifierInfo *propertyIvar = 0;
1704 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1705 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001706 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00001707 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001708 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00001709 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001710 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001711 cutOffParsing();
1712 return 0;
Douglas Gregor5d649882009-11-18 22:32:06 +00001713 }
1714
Chris Lattner0ef13522007-10-09 17:51:17 +00001715 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001716 Diag(Tok, diag::err_expected) << tok::identifier;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001717 break;
1718 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001719 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001720 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001721 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001722 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001723 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001724 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001725 break;
1726 ConsumeToken(); // consume ','
1727 }
Alp Toker383d2c42014-01-01 03:08:43 +00001728 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
John McCall48871652010-08-21 09:40:31 +00001729 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001730}
1731
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001732/// property-dynamic:
1733/// @dynamic property-list
1734///
1735/// property-list:
1736/// identifier
1737/// property-list ',' identifier
1738///
John McCall48871652010-08-21 09:40:31 +00001739Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001740 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1741 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001742 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001743 while (true) {
1744 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001745 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001746 cutOffParsing();
1747 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001748 }
1749
1750 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001751 Diag(Tok, diag::err_expected) << tok::identifier;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001752 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001753 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001754 }
1755
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001756 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1757 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001758 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001759 propertyId, 0, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001760
Chris Lattner0ef13522007-10-09 17:51:17 +00001761 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001762 break;
1763 ConsumeToken(); // consume ','
1764 }
Alp Toker383d2c42014-01-01 03:08:43 +00001765 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
John McCall48871652010-08-21 09:40:31 +00001766 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001767}
Mike Stump11289f42009-09-09 15:08:12 +00001768
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001769/// objc-throw-statement:
1770/// throw expression[opt];
1771///
John McCalldadc5752010-08-24 06:29:42 +00001772StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1773 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001774 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001775 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001776 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001777 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001778 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001779 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001780 }
1781 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001782 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00001783 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
John McCallb268a282010-08-23 23:25:46 +00001784 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001785}
1786
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001787/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001788/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001789///
John McCalldadc5752010-08-24 06:29:42 +00001790StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001791Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001792 ConsumeToken(); // consume synchronized
1793 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001794 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001795 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001796 }
John McCalld9bb7432011-07-27 21:50:02 +00001797
1798 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001799 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00001800 ExprResult operand(ParseExpression());
1801
1802 if (Tok.is(tok::r_paren)) {
1803 ConsumeParen(); // ')'
1804 } else {
1805 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00001806 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00001807
1808 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001809 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001810 }
John McCalld9bb7432011-07-27 21:50:02 +00001811
1812 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001813 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00001814 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00001815 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001816 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001817 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001818
John McCalld9bb7432011-07-27 21:50:02 +00001819 // Check the @synchronized operand now.
1820 if (!operand.isInvalid())
1821 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001822
John McCalld9bb7432011-07-27 21:50:02 +00001823 // Parse the compound statement within a new scope.
1824 ParseScope bodyScope(this, Scope::DeclScope);
1825 StmtResult body(ParseCompoundStatementBody());
1826 bodyScope.Exit();
1827
1828 // If there was a semantic or parse error earlier with the
1829 // operand, fail now.
1830 if (operand.isInvalid())
1831 return StmtError();
1832
1833 if (body.isInvalid())
1834 body = Actions.ActOnNullStmt(Tok.getLocation());
1835
1836 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001837}
1838
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001839/// objc-try-catch-statement:
1840/// @try compound-statement objc-catch-list[opt]
1841/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1842///
1843/// objc-catch-list:
1844/// @catch ( parameter-declaration ) compound-statement
1845/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1846/// catch-parameter-declaration:
1847/// parameter-declaration
1848/// '...' [OBJC2]
1849///
John McCalldadc5752010-08-24 06:29:42 +00001850StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001851 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001852
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001853 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001854 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00001855 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001856 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001857 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00001858 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00001859 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001860 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001861 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001862 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001863 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001864 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001865
Chris Lattner0ef13522007-10-09 17:51:17 +00001866 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001867 // At this point, we need to lookahead to determine if this @ is the start
1868 // of an @catch or @finally. We don't want to consume the @ token if this
1869 // is an @try or @encode or something else.
1870 Token AfterAt = GetLookAheadToken(1);
1871 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1872 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1873 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001874
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001875 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001876 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCall48871652010-08-21 09:40:31 +00001877 Decl *FirstPart = 0;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001878 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001879 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001880 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001881 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001882 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001883 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001884 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00001885 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00001886 ParseDeclarator(ParmDecl);
1887
Douglas Gregore11ee112010-04-23 23:01:43 +00001888 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001889 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001890 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001891 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001892 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001893
Steve Naroff65a00892009-04-07 22:56:58 +00001894 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001895
Steve Naroff65a00892009-04-07 22:56:58 +00001896 if (Tok.is(tok::r_paren))
1897 RParenLoc = ConsumeParen();
1898 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001899 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00001900
John McCalldadc5752010-08-24 06:29:42 +00001901 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001902 if (Tok.is(tok::l_brace))
1903 CatchBody = ParseCompoundStatementBody();
1904 else
Alp Tokerec543272013-12-24 09:48:30 +00001905 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001906 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001907 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001908
John McCalldadc5752010-08-24 06:29:42 +00001909 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001910 RParenLoc,
1911 FirstPart,
John McCallb268a282010-08-23 23:25:46 +00001912 CatchBody.take());
Douglas Gregor96c79492010-04-23 22:50:49 +00001913 if (!Catch.isInvalid())
1914 CatchStmts.push_back(Catch.release());
1915
Steve Naroffe6016792008-02-05 21:27:35 +00001916 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001917 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1918 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001919 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001920 }
1921 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001922 } else {
1923 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001924 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001925 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001926
John McCalldadc5752010-08-24 06:29:42 +00001927 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001928 if (Tok.is(tok::l_brace))
1929 FinallyBody = ParseCompoundStatementBody();
1930 else
Alp Tokerec543272013-12-24 09:48:30 +00001931 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001932 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001933 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001934 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCallb268a282010-08-23 23:25:46 +00001935 FinallyBody.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001936 catch_or_finally_seen = true;
1937 break;
1938 }
1939 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001940 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001941 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001942 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001943 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001944
John McCallb268a282010-08-23 23:25:46 +00001945 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001946 CatchStmts,
John McCallb268a282010-08-23 23:25:46 +00001947 FinallyStmt.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001948}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001949
John McCall31168b02011-06-15 23:02:42 +00001950/// objc-autoreleasepool-statement:
1951/// @autoreleasepool compound-statement
1952///
1953StmtResult
1954Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1955 ConsumeToken(); // consume autoreleasepool
1956 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00001957 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00001958 return StmtError();
1959 }
1960 // Enter a scope to hold everything within the compound stmt. Compound
1961 // statements can always hold declarations.
1962 ParseScope BodyScope(this, Scope::DeclScope);
1963
1964 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1965
1966 BodyScope.Exit();
1967 if (AutoreleasePoolBody.isInvalid())
1968 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1969 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1970 AutoreleasePoolBody.take());
1971}
1972
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001973/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
1974/// for later parsing.
1975void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
1976 LexedMethod* LM = new LexedMethod(this, MDecl);
1977 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
1978 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00001979 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001980 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00001981 if (Tok.is(tok::kw_try)) {
1982 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00001983 if (Tok.is(tok::colon)) {
1984 Toks.push_back(Tok);
1985 ConsumeToken();
1986 while (Tok.isNot(tok::l_brace)) {
1987 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1988 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1989 }
1990 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00001991 Toks.push_back(Tok); // also store '{'
1992 }
1993 else if (Tok.is(tok::colon)) {
1994 ConsumeToken();
1995 while (Tok.isNot(tok::l_brace)) {
1996 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1997 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1998 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00001999 Toks.push_back(Tok); // also store '{'
2000 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002001 ConsumeBrace();
2002 // Consume everything up to (and including) the matching right brace.
2003 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002004 while (Tok.is(tok::kw_catch)) {
2005 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2006 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2007 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002008}
2009
Steve Naroff09bf8152007-09-06 21:24:23 +00002010/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002011///
John McCall48871652010-08-21 09:40:31 +00002012Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002013 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002014
John McCallfaf5fb42010-08-26 23:41:50 +00002015 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2016 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002017
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002018 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002019 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002020 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002021 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002022 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002023 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002024 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002025 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002026
Steve Naroffbb875722007-11-11 19:54:21 +00002027 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002028 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002029 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002030
Steve Naroffbb875722007-11-11 19:54:21 +00002031 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002032 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002033
Steve Naroffbb875722007-11-11 19:54:21 +00002034 // If we didn't find the '{', bail out.
2035 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +00002036 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002037 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002038
2039 if (!MDecl) {
2040 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002041 SkipUntil(tok::r_brace);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002042 return 0;
2043 }
2044
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002045 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002046 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002047 assert (CurParsedObjCImpl
2048 && "ParseObjCMethodDefinition - Method out of @implementation");
2049 // Consume the tokens and store them for later parsing.
2050 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002051 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002052}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002053
John McCalldadc5752010-08-24 06:29:42 +00002054StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002055 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002056 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002057 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002058 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002059 }
2060
2061 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002062 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002063
2064 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002065 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002066
2067 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002068 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002069
2070 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2071 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002072
John McCalldadc5752010-08-24 06:29:42 +00002073 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002074 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002075 // If the expression is invalid, skip ahead to the next semicolon. Not
2076 // doing this opens us up to the possibility of infinite loops if
2077 // ParseExpression does not consume any tokens.
2078 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002079 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002080 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002081
Steve Naroffe6016792008-02-05 21:27:35 +00002082 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002083 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002084 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002085}
2086
John McCalldadc5752010-08-24 06:29:42 +00002087ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002088 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002089 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002090 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002091 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002092 return ExprError();
2093
Ted Kremeneke65b0862012-03-06 20:05:56 +00002094 case tok::minus:
2095 case tok::plus: {
2096 tok::TokenKind Kind = Tok.getKind();
2097 SourceLocation OpLoc = ConsumeToken();
2098
2099 if (!Tok.is(tok::numeric_constant)) {
2100 const char *Symbol = 0;
2101 switch (Kind) {
2102 case tok::minus: Symbol = "-"; break;
2103 case tok::plus: Symbol = "+"; break;
2104 default: llvm_unreachable("missing unary operator case");
2105 }
2106 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2107 << Symbol;
2108 return ExprError();
2109 }
2110
2111 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2112 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002113 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002114 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002115 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002116
2117 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.take());
2118 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002119 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002120
2121 return ParsePostfixExpressionSuffix(
2122 Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2123 }
2124
Chris Lattnere002fbe2007-12-12 01:04:12 +00002125 case tok::string_literal: // primary-expression: string-literal
2126 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002127 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002128
2129 case tok::char_constant:
2130 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2131
2132 case tok::numeric_constant:
2133 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2134
2135 case tok::kw_true: // Objective-C++, etc.
2136 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2137 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2138 case tok::kw_false: // Objective-C++, etc.
2139 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2140 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2141
2142 case tok::l_square:
2143 // Objective-C array literal
2144 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2145
2146 case tok::l_brace:
2147 // Objective-C dictionary literal
2148 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2149
Patrick Beard0caa3942012-04-19 00:25:12 +00002150 case tok::l_paren:
2151 // Objective-C boxed expression
2152 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2153
Chris Lattnere002fbe2007-12-12 01:04:12 +00002154 default:
Chris Lattner197a3012008-08-05 06:19:09 +00002155 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002156 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002157
Chris Lattner197a3012008-08-05 06:19:09 +00002158 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2159 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002160 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002161 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002162 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002163 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002164 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002165 default: {
2166 const char *str = 0;
2167 if (GetLookAheadToken(1).is(tok::l_brace)) {
2168 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2169 str =
2170 ch == 't' ? "try"
2171 : (ch == 'f' ? "finally"
2172 : (ch == 'a' ? "autoreleasepool" : 0));
2173 }
2174 if (str) {
2175 SourceLocation kwLoc = Tok.getLocation();
2176 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2177 FixItHint::CreateReplacement(kwLoc, str));
2178 }
2179 else
2180 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2181 }
Chris Lattner197a3012008-08-05 06:19:09 +00002182 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002183 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002184}
2185
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002186/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002187///
2188/// This routine parses the receiver of a message send in
2189/// Objective-C++ either as a type or as an expression. Note that this
2190/// routine must not be called to parse a send to 'super', since it
2191/// has no way to return such a result.
2192///
2193/// \param IsExpr Whether the receiver was parsed as an expression.
2194///
2195/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2196/// IsExpr is true), the parsed expression. If the receiver was parsed
2197/// as a type (\c IsExpr is false), the parsed type.
2198///
2199/// \returns True if an error occurred during parsing or semantic
2200/// analysis, in which case the arguments do not have valid
2201/// values. Otherwise, returns false for a successful parse.
2202///
2203/// objc-receiver: [C++]
2204/// 'super' [not parsed here]
2205/// expression
2206/// simple-type-specifier
2207/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002208bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002209 InMessageExpressionRAIIObject InMessage(*this, true);
2210
Douglas Gregor8d4de672010-04-21 22:36:40 +00002211 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2212 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2213 TryAnnotateTypeOrScopeToken();
2214
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002215 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002216 // objc-receiver:
2217 // expression
John McCalldadc5752010-08-24 06:29:42 +00002218 ExprResult Receiver = ParseExpression();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002219 if (Receiver.isInvalid())
2220 return true;
2221
2222 IsExpr = true;
2223 TypeOrExpr = Receiver.take();
2224 return false;
2225 }
2226
2227 // objc-receiver:
2228 // typename-specifier
2229 // simple-type-specifier
2230 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002231 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002232 ParseCXXSimpleTypeSpecifier(DS);
2233
2234 if (Tok.is(tok::l_paren)) {
2235 // If we see an opening parentheses at this point, we are
2236 // actually parsing an expression that starts with a
2237 // function-style cast, e.g.,
2238 //
2239 // postfix-expression:
2240 // simple-type-specifier ( expression-list [opt] )
2241 // typename-specifier ( expression-list [opt] )
2242 //
2243 // Parse the remainder of this case, then the (optional)
2244 // postfix-expression suffix, followed by the (optional)
2245 // right-hand side of the binary expression. We have an
2246 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002247 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002248 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002249 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002250 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002251 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002252 if (Receiver.isInvalid())
2253 return true;
2254
2255 IsExpr = true;
2256 TypeOrExpr = Receiver.take();
2257 return false;
2258 }
2259
2260 // We have a class message. Turn the simple-type-specifier or
2261 // typename-specifier we parsed into a type and parse the
2262 // remainder of the class message.
2263 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002264 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002265 if (Type.isInvalid())
2266 return true;
2267
2268 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002269 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002270 return false;
2271}
2272
Douglas Gregor990ccac2010-05-31 14:40:22 +00002273/// \brief Determine whether the parser is currently referring to a an
2274/// Objective-C message send, using a simplified heuristic to avoid overhead.
2275///
2276/// This routine will only return true for a subset of valid message-send
2277/// expressions.
2278bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002279 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002280 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002281 return GetLookAheadToken(1).is(tok::identifier) &&
2282 GetLookAheadToken(2).is(tok::identifier);
2283}
2284
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002285bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002286 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002287 InMessageExpression)
2288 return false;
2289
2290
2291 ParsedType Type;
2292
2293 if (Tok.is(tok::annot_typename))
2294 Type = getTypeAnnotation(Tok);
2295 else if (Tok.is(tok::identifier))
2296 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2297 getCurScope());
2298 else
2299 return false;
2300
2301 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2302 const Token &AfterNext = GetLookAheadToken(2);
2303 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2304 if (Tok.is(tok::identifier))
2305 TryAnnotateTypeOrScopeToken();
2306
2307 return Tok.is(tok::annot_typename);
2308 }
2309 }
2310
2311 return false;
2312}
2313
Mike Stump11289f42009-09-09 15:08:12 +00002314/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002315/// '[' objc-receiver objc-message-args ']'
2316///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002317/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002318/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002319/// expression
2320/// class-name
2321/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002322///
John McCalldadc5752010-08-24 06:29:42 +00002323ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002324 assert(Tok.is(tok::l_square) && "'[' expected");
2325 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2326
Douglas Gregora817a192010-05-27 23:06:34 +00002327 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002328 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002329 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002330 return ExprError();
2331 }
2332
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002333 InMessageExpressionRAIIObject InMessage(*this, true);
2334
David Blaikiebbafb8a2012-03-11 07:00:24 +00002335 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002336 // We completely separate the C and C++ cases because C++ requires
2337 // more complicated (read: slower) parsing.
2338
2339 // Handle send to super.
2340 // FIXME: This doesn't benefit from the same typo-correction we
2341 // get in Objective-C.
2342 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002343 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002344 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2345 ParsedType(), 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002346
2347 // Parse the receiver, which is either a type or an expression.
2348 bool IsExpr;
Nick Lewycky970aed92010-09-15 18:35:19 +00002349 void *TypeOrExpr = NULL;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002350 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002351 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002352 return ExprError();
2353 }
2354
2355 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002356 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2357 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002358 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002359
2360 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002361 ParsedType::getFromOpaquePtr(TypeOrExpr),
2362 0);
Chris Lattner47054fb2010-05-31 18:18:22 +00002363 }
2364
2365 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002366 IdentifierInfo *Name = Tok.getIdentifierInfo();
2367 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002368 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002369 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002370 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002371 NextToken().is(tok::period),
2372 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002373 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002374 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2375 ParsedType(), 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002376
John McCallfaf5fb42010-08-26 23:41:50 +00002377 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002378 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002379 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002380 return ExprError();
2381 }
2382
Douglas Gregore5798dc2010-04-21 20:38:13 +00002383 ConsumeToken(); // the type name
2384
2385 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb268a282010-08-23 23:25:46 +00002386 ReceiverType, 0);
Douglas Gregora148a1d2010-04-14 02:22:16 +00002387
John McCallfaf5fb42010-08-26 23:41:50 +00002388 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002389 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002390 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002391 }
Chris Lattner8f697062008-01-25 18:59:06 +00002392 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002393
2394 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCalldadc5752010-08-24 06:29:42 +00002395 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002396 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002397 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002398 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00002399 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002400
John McCallba7bf592010-08-24 05:47:05 +00002401 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2402 ParsedType(), Res.take());
Chris Lattner8f697062008-01-25 18:59:06 +00002403}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002404
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002405/// \brief Parse the remainder of an Objective-C message following the
2406/// '[' objc-receiver.
2407///
2408/// This routine handles sends to super, class messages (sent to a
2409/// class name), and instance messages (sent to an object), and the
2410/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2411/// ReceiverExpr, respectively. Only one of these parameters may have
2412/// a valid value.
2413///
2414/// \param LBracLoc The location of the opening '['.
2415///
2416/// \param SuperLoc If this is a send to 'super', the location of the
2417/// 'super' keyword that indicates a send to the superclass.
2418///
2419/// \param ReceiverType If this is a class message, the type of the
2420/// class we are sending a message to.
2421///
2422/// \param ReceiverExpr If this is an instance message, the expression
2423/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002424///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002425/// objc-message-args:
2426/// objc-selector
2427/// objc-keywordarg-list
2428///
2429/// objc-keywordarg-list:
2430/// objc-keywordarg
2431/// objc-keywordarg-list objc-keywordarg
2432///
Mike Stump11289f42009-09-09 15:08:12 +00002433/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002434/// selector-name[opt] ':' objc-keywordexpr
2435///
2436/// objc-keywordexpr:
2437/// nonempty-expr-list
2438///
2439/// nonempty-expr-list:
2440/// assignment-expression
2441/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002442///
John McCalldadc5752010-08-24 06:29:42 +00002443ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002444Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002445 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002446 ParsedType ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002447 ExprArg ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002448 InMessageExpressionRAIIObject InMessage(*this, true);
2449
Steve Naroffeae65032009-11-07 02:08:14 +00002450 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002451 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002452 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002453 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002454 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002455 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002456 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002457 else
John McCallb268a282010-08-23 23:25:46 +00002458 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002459 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002460 cutOffParsing();
2461 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002462 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002463
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002464 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002465 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002466 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002467
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002468 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002469 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002470 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00002471
Chris Lattner0ef13522007-10-09 17:51:17 +00002472 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002473 while (1) {
2474 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002475 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002476 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00002477
Alp Toker383d2c42014-01-01 03:08:43 +00002478 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00002479 // We must manually skip to a ']', otherwise the expression skipper will
2480 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2481 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002482 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002483 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002484 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002485
Mike Stump11289f42009-09-09 15:08:12 +00002486 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002487
2488 if (Tok.is(tok::code_completion)) {
2489 if (SuperLoc.isValid())
2490 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002491 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002492 /*AtArgumentEpression=*/true);
2493 else if (ReceiverType)
2494 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002495 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002496 /*AtArgumentEpression=*/true);
2497 else
2498 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002499 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002500 /*AtArgumentEpression=*/true);
2501
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002502 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002503 return ExprError();
2504 }
2505
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00002506 ExprResult Expr;
2507 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
2508 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2509 Expr = ParseBraceInitializer();
2510 } else
2511 Expr = ParseAssignmentExpression();
2512
2513 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002514 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002515 // We must manually skip to a ']', otherwise the expression skipper will
2516 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2517 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002518 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002519 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00002520 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002521
Steve Naroff486760a2007-09-17 20:25:27 +00002522 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002523 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002524
Douglas Gregor1b605f72009-11-19 01:08:35 +00002525 // Code completion after each argument.
2526 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002527 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002528 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002529 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002530 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002531 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002532 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002533 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002534 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002535 else
John McCallb268a282010-08-23 23:25:46 +00002536 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002537 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002538 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002539 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002540 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002541 }
2542
Steve Naroff486760a2007-09-17 20:25:27 +00002543 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002544 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002545 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002546 break;
2547 // We have a selector or a colon, continue parsing.
2548 }
2549 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002550 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002551 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002552 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002553 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002554 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002555 if (Tok.is(tok::colon)) {
2556 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
2557 FixItHint::CreateRemoval(commaLoc);
2558 }
Chris Lattner197a3012008-08-05 06:19:09 +00002559 // We must manually skip to a ']', otherwise the expression skipper will
2560 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2561 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002562 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002563 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002564 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002565
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002566 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002567 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002568 }
2569 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00002570 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002571
Chris Lattner197a3012008-08-05 06:19:09 +00002572 // We must manually skip to a ']', otherwise the expression skipper will
2573 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2574 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002575 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002576 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002577 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002578
Chris Lattner0ef13522007-10-09 17:51:17 +00002579 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00002580 Diag(Tok, diag::err_expected)
2581 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00002582 // We must manually skip to a ']', otherwise the expression skipper will
2583 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2584 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002585 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002586 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002587 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002588
Chris Lattner8f697062008-01-25 18:59:06 +00002589 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002590
Steve Naroffe61bfa82007-10-05 18:42:47 +00002591 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002592 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00002593 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002594 KeyLocs.push_back(Loc);
2595 }
Chris Lattner5700fab2007-10-07 02:00:24 +00002596 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002597
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002598 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002599 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002600 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002601 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002602 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002603 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00002604 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002605 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002606}
2607
John McCalldadc5752010-08-24 06:29:42 +00002608ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2609 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002610 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002611
Chris Lattnere002fbe2007-12-12 01:04:12 +00002612 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2613 // expressions. At this point, we know that the only valid thing that starts
2614 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002615 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002616 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00002617 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002618 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002619
Chris Lattnere002fbe2007-12-12 01:04:12 +00002620 while (Tok.is(tok::at)) {
2621 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002622
Sebastian Redlc13f2682008-12-09 20:22:58 +00002623 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002624 if (!isTokenStringLiteral())
2625 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002626
John McCalldadc5752010-08-24 06:29:42 +00002627 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002628 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002629 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002630
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002631 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002632 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002633
Nico Webera7c7e602012-12-31 00:28:03 +00002634 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
2635 AtStrings.size());
Anders Carlsson76f4a902007-08-21 17:43:55 +00002636}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002637
Ted Kremeneke65b0862012-03-06 20:05:56 +00002638/// ParseObjCBooleanLiteral -
2639/// objc-scalar-literal : '@' boolean-keyword
2640/// ;
2641/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2642/// ;
2643ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2644 bool ArgValue) {
2645 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2646 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2647}
2648
2649/// ParseObjCCharacterLiteral -
2650/// objc-scalar-literal : '@' character-literal
2651/// ;
2652ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2653 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2654 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002655 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002656 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002657 ConsumeToken(); // Consume the literal token.
Nico Webera7c7e602012-12-31 00:28:03 +00002658 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002659}
2660
2661/// ParseObjCNumericLiteral -
2662/// objc-scalar-literal : '@' scalar-literal
2663/// ;
2664/// scalar-literal : | numeric-constant /* any numeric constant. */
2665/// ;
2666ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2667 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2668 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002669 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002670 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002671 ConsumeToken(); // Consume the literal token.
Nico Webera7c7e602012-12-31 00:28:03 +00002672 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002673}
2674
Patrick Beard0caa3942012-04-19 00:25:12 +00002675/// ParseObjCBoxedExpr -
2676/// objc-box-expression:
2677/// @( assignment-expression )
2678ExprResult
2679Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2680 if (Tok.isNot(tok::l_paren))
2681 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2682
2683 BalancedDelimiterTracker T(*this, tok::l_paren);
2684 T.consumeOpen();
2685 ExprResult ValueExpr(ParseAssignmentExpression());
2686 if (T.consumeClose())
2687 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00002688
2689 if (ValueExpr.isInvalid())
2690 return ExprError();
2691
Patrick Beard0caa3942012-04-19 00:25:12 +00002692 // Wrap the sub-expression in a parenthesized expression, to distinguish
2693 // a boxed expression from a literal.
2694 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
2695 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.take());
Nico Webera7c7e602012-12-31 00:28:03 +00002696 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
2697 ValueExpr.take());
Patrick Beard0caa3942012-04-19 00:25:12 +00002698}
2699
Ted Kremeneke65b0862012-03-06 20:05:56 +00002700ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002701 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002702 ConsumeBracket(); // consume the l_square.
2703
2704 while (Tok.isNot(tok::r_square)) {
2705 // Parse list of array element expressions (all must be id types).
2706 ExprResult Res(ParseAssignmentExpression());
2707 if (Res.isInvalid()) {
2708 // We must manually skip to a ']', otherwise the expression skipper will
2709 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2710 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002711 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002712 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002713 }
2714
2715 // Parse the ellipsis that indicates a pack expansion.
2716 if (Tok.is(tok::ellipsis))
2717 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2718 if (Res.isInvalid())
2719 return true;
2720
2721 ElementExprs.push_back(Res.release());
2722
2723 if (Tok.is(tok::comma))
2724 ConsumeToken(); // Eat the ','.
2725 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00002726 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
2727 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002728 }
2729 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramerf0623432012-08-23 22:51:59 +00002730 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00002731 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002732}
2733
2734ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2735 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2736 ConsumeBrace(); // consume the l_square.
2737 while (Tok.isNot(tok::r_brace)) {
2738 // Parse the comma separated key : value expressions.
2739 ExprResult KeyExpr;
2740 {
2741 ColonProtectionRAIIObject X(*this);
2742 KeyExpr = ParseAssignmentExpression();
2743 if (KeyExpr.isInvalid()) {
2744 // We must manually skip to a '}', otherwise the expression skipper will
2745 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2746 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002747 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002748 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002749 }
2750 }
2751
Alp Toker383d2c42014-01-01 03:08:43 +00002752 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002753 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00002754 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00002755 }
2756
2757 ExprResult ValueExpr(ParseAssignmentExpression());
2758 if (ValueExpr.isInvalid()) {
2759 // We must manually skip to a '}', otherwise the expression skipper will
2760 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2761 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002762 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002763 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002764 }
2765
2766 // Parse the ellipsis that designates this as a pack expansion.
2767 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00002768 if (getLangOpts().CPlusPlus)
2769 TryConsumeToken(tok::ellipsis, EllipsisLoc);
2770
Ted Kremeneke65b0862012-03-06 20:05:56 +00002771 // We have a valid expression. Collect it in a vector so we can
2772 // build the argument list.
2773 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00002774 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00002775 };
2776 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00002777
2778 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002779 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
2780 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002781 }
2782 SourceLocation EndLoc = ConsumeBrace();
2783
2784 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00002785 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2786 Elements.data(), Elements.size());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002787}
2788
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002789/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002790/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002791ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002792Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002793 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002794
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002795 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002796
Chris Lattner197a3012008-08-05 06:19:09 +00002797 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002798 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2799
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002800 BalancedDelimiterTracker T(*this, tok::l_paren);
2801 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002802
Douglas Gregor220cac52009-02-18 17:45:20 +00002803 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002804
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002805 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002806
Douglas Gregor220cac52009-02-18 17:45:20 +00002807 if (Ty.isInvalid())
2808 return ExprError();
2809
Nico Webera7c7e602012-12-31 00:28:03 +00002810 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
2811 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002812}
Anders Carlssone01493d2007-08-23 15:25:28 +00002813
2814/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00002815/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002816ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002817Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002818 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002819
Chris Lattner197a3012008-08-05 06:19:09 +00002820 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002821 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2822
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002823 BalancedDelimiterTracker T(*this, tok::l_paren);
2824 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002825
Chris Lattner197a3012008-08-05 06:19:09 +00002826 if (Tok.isNot(tok::identifier))
Alp Tokerec543272013-12-24 09:48:30 +00002827 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002828
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002829 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00002830 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002831
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002832 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00002833
Nico Webera7c7e602012-12-31 00:28:03 +00002834 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2835 T.getOpenLocation(), ProtoIdLoc,
2836 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00002837}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002838
2839/// objc-selector-expression
2840/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002841ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002842 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002843
Chris Lattner197a3012008-08-05 06:19:09 +00002844 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002845 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2846
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002847 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002848 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002849
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002850 BalancedDelimiterTracker T(*this, tok::l_paren);
2851 T.consumeOpen();
2852
Douglas Gregor67c692c2010-08-26 15:07:07 +00002853 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002854 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002855 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002856 return ExprError();
2857 }
2858
Chris Lattner4f472a32009-04-11 18:13:45 +00002859 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002860 if (!SelIdent && // missing selector name.
2861 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00002862 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002863
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002864 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002865 unsigned nColons = 0;
2866 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002867 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00002868 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00002869 ++nColons;
2870 KeyIdents.push_back(0);
Alp Toker383d2c42014-01-01 03:08:43 +00002871 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
2872 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00002873 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00002874
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002875 if (Tok.is(tok::r_paren))
2876 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002877
2878 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002879 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002880 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002881 return ExprError();
2882 }
2883
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002884 // Check for another keyword selector.
2885 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002886 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002887 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00002888 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002889 break;
2890 }
Steve Naroff152dd812007-12-05 22:21:29 +00002891 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002892 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00002893 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00002894 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2895 T.getOpenLocation(),
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002896 T.getCloseLocation());
Gabor Greif24032f12007-10-19 15:38:32 +00002897 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002898
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002899void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
2900 // MCDecl might be null due to error in method or c-function prototype, etc.
2901 Decl *MCDecl = LM.D;
2902 bool skip = MCDecl &&
2903 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
2904 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
2905 if (skip)
2906 return;
2907
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002908 // Save the current token position.
2909 SourceLocation OrigLoc = Tok.getLocation();
2910
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002911 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2912 // Append the current token at the end of the new token stream so that it
2913 // doesn't get lost.
2914 LM.Toks.push_back(Tok);
2915 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2916
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002917 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00002918 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002919
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002920 assert((Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
2921 Tok.is(tok::colon)) &&
2922 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00002923 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002924 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002925 parseMethod
2926 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
2927 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002928
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002929 // Tell the actions module that we have entered a method or c-function definition
2930 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00002931 if (parseMethod)
2932 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
2933 else
2934 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002935 if (Tok.is(tok::kw_try))
2936 MCDecl = ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002937 else {
2938 if (Tok.is(tok::colon))
2939 ParseConstructorInitializer(MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002940 MCDecl = ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002941 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00002942
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002943 if (Tok.getLocation() != OrigLoc) {
2944 // Due to parsing error, we either went over the cached tokens or
2945 // there are still cached tokens left. If it's the latter case skip the
2946 // leftover tokens.
2947 // Since this is an uncommon situation that should be avoided, use the
2948 // expensive isBeforeInTranslationUnit call.
2949 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2950 OrigLoc))
2951 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2952 ConsumeAnyToken();
2953 }
2954
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002955 return;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002956}