blob: a597a1658cf6b87f35a519a11f4645edad3a5336 [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 }
Craig Topper161e4db2014-05-21 06:02:52 +000054
55 Decl *SingleDecl = nullptr;
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:
Sean Callanan87596492014-12-09 23:47:56 +000082 if (getLangOpts().Modules || getLangOpts().DebuggerSupport)
Douglas Gregor0bf886d2012-01-03 18:24:14 +000083 return ParseModuleImport(AtLoc);
Fariborz Jahaniana773d082014-03-26 22:02:43 +000084 Diag(AtLoc, diag::err_atimport);
85 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000086 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerce90ef52008-08-23 02:02:23 +000087 default:
88 Diag(AtLoc, diag::err_unexpected_at);
89 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000090 SingleDecl = nullptr;
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000091 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);
Craig Topper161e4db2014-05-21 06:02:52 +0000112 return Actions.ConvertDeclToDeclGroup(nullptr);
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"))
Craig Topper161e4db2014-05-21 06:02:52 +0000124 return Actions.ConvertDeclToDeclGroup(nullptr);
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();
Craig Topper161e4db2014-05-21 06:02:52 +0000190 return nullptr;
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.
Craig Topper161e4db2014-05-21 06:02:52 +0000198 return nullptr;
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;
Craig Topper161e4db2014-05-21 06:02:52 +0000211 IdentifierInfo *categoryId = nullptr;
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();
Craig Topper161e4db2014-05-21 06:02:52 +0000215 return nullptr;
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.
Craig Topper161e4db2014-05-21 06:02:52 +0000226 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000227 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000228
229 T.consumeClose();
230 if (T.getCloseLocation().isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000231 return nullptr;
232
Douglas Gregor0c254a02011-09-23 19:19:41 +0000233 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))
Craig Topper161e4db2014-05-21 06:02:52 +0000245 return nullptr;
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.
Craig Topper161e4db2014-05-21 06:02:52 +0000263 IdentifierInfo *superClassId = nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000264 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();
Craig Topper161e4db2014-05-21 06:02:52 +0000273 return nullptr;
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.
Craig Topper161e4db2014-05-21 06:02:52 +0000279 return nullptr;
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))
Craig Topper161e4db2014-05-21 06:02:52 +0000291 return nullptr;
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
310/// objc-interface-decl-list:
311/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000312/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000313/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000314/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000315/// objc-interface-decl-list declaration
316/// objc-interface-decl-list ';'
317///
Steve Naroff99264b42007-08-22 16:35:03 +0000318/// objc-method-requirement: [OBJC2]
319/// @required
320/// @optional
321///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000322void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
323 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000324 SmallVector<Decl *, 32> allMethods;
325 SmallVector<Decl *, 16> allProperties;
326 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000327 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000328
Ted Kremenekc7c64312010-01-07 01:20:12 +0000329 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000330
Steve Naroff99264b42007-08-22 16:35:03 +0000331 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000332 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000333 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000334 if (Decl *methodPrototype =
335 ParseObjCMethodPrototype(MethodImplKind, false))
336 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000337 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
338 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000339 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
340 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000341 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000342 if (Tok.is(tok::semi))
343 ConsumeToken();
344 }
Steve Naroff99264b42007-08-22 16:35:03 +0000345 continue;
346 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000347 if (Tok.is(tok::l_paren)) {
348 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000349 ParseObjCMethodDecl(Tok.getLocation(),
350 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000351 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000352 continue;
353 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000354 // Ignore excess semicolons.
355 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000356 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000357 continue;
358 }
Mike Stump11289f42009-09-09 15:08:12 +0000359
Chris Lattnerda9fb152008-10-20 06:10:06 +0000360 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000361 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000362 break;
Mike Stump11289f42009-09-09 15:08:12 +0000363
Douglas Gregorf1934162010-01-13 21:24:21 +0000364 // Code completion within an Objective-C interface.
365 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000366 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000367 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000368 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000369 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000370 }
371
Chris Lattner038a3e32008-10-20 05:46:22 +0000372 // If we don't have an @ directive, parse it as a function definition.
373 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000374 // The code below does not consume '}'s because it is afraid of eating the
375 // end of a namespace. Because of the way this code is structured, an
376 // erroneous r_brace would cause an infinite loop if not handled here.
377 if (Tok.is(tok::r_brace))
378 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000379 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000380 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000381 continue;
382 }
Mike Stump11289f42009-09-09 15:08:12 +0000383
Chris Lattner038a3e32008-10-20 05:46:22 +0000384 // Otherwise, we have an @ directive, eat the @.
385 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000386 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000387 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000388 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000389 }
390
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000391 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000392
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000393 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000394 AtEnd.setBegin(AtLoc);
395 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000396 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000397 } else if (DirectiveKind == tok::objc_not_keyword) {
398 Diag(Tok, diag::err_objc_unknown_at);
399 SkipUntil(tok::semi);
400 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000401 }
Mike Stump11289f42009-09-09 15:08:12 +0000402
Chris Lattnerda9fb152008-10-20 06:10:06 +0000403 // Eat the identifier.
404 ConsumeToken();
405
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000406 switch (DirectiveKind) {
407 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000408 // FIXME: If someone forgets an @end on a protocol, this loop will
409 // continue to eat up tons of stuff and spew lots of nonsense errors. It
410 // would probably be better to bail out if we saw an @class or @interface
411 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000412 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000413 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000414 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000415 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000416
417 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000418 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000419 Diag(AtLoc, diag::err_objc_missing_end)
420 << FixItHint::CreateInsertion(AtLoc, "@end\n");
421 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
422 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000423 ConsumeToken();
424 break;
425
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000426 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000427 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000428 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000429 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000430 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000431 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000432 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000433 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000434 break;
Mike Stump11289f42009-09-09 15:08:12 +0000435
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000436 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000437 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000438 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000439
Chris Lattner038a3e32008-10-20 05:46:22 +0000440 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000441 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000442 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000443 if (Tok.is(tok::l_paren)) {
444 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000445 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000446 }
Mike Stump11289f42009-09-09 15:08:12 +0000447
Benjamin Kramera39beb92014-09-03 11:06:10 +0000448 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
449 if (FD.D.getIdentifier() == nullptr) {
450 Diag(AtLoc, diag::err_objc_property_requires_field_name)
451 << FD.D.getSourceRange();
452 return;
453 }
454 if (FD.BitfieldSize) {
455 Diag(AtLoc, diag::err_objc_property_bitfield)
456 << FD.D.getSourceRange();
457 return;
458 }
459
460 // Install the property declarator into interfaceDecl.
461 IdentifierInfo *SelName =
462 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
463
464 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
465 IdentifierInfo *SetterName = OCDS.getSetterName();
466 Selector SetterSel;
467 if (SetterName)
468 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
469 else
470 SetterSel = SelectorTable::constructSetterSelector(
471 PP.getIdentifierTable(), PP.getSelectorTable(),
472 FD.D.getIdentifier());
473 bool isOverridingProperty = false;
474 Decl *Property = Actions.ActOnProperty(
475 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
476 &isOverridingProperty, MethodImplKind);
477 if (!isOverridingProperty)
478 allProperties.push_back(Property);
479
480 FD.complete(Property);
481 };
John McCallcfefb6d2009-11-03 02:38:08 +0000482
Chris Lattner038a3e32008-10-20 05:46:22 +0000483 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000484 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000485 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000486
John McCall405988b2011-03-26 01:53:26 +0000487 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000488 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000489 }
Steve Naroff99264b42007-08-22 16:35:03 +0000490 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000491
492 // We break out of the big loop in two cases: when we see @end or when we see
493 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000494 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000495 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000496 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000497 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000498 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000499 } else {
500 Diag(Tok, diag::err_objc_missing_end)
501 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
502 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
503 << (int) Actions.getObjCContainerKind();
504 AtEnd.setBegin(Tok.getLocation());
505 AtEnd.setEnd(Tok.getLocation());
506 }
Mike Stump11289f42009-09-09 15:08:12 +0000507
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000508 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000509 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000510 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000511}
512
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000513/// Parse property attribute declarations.
514///
515/// property-attr-decl: '(' property-attrlist ')'
516/// property-attrlist:
517/// property-attribute
518/// property-attrlist ',' property-attribute
519/// property-attribute:
520/// getter '=' identifier
521/// setter '=' identifier ':'
522/// readonly
523/// readwrite
524/// assign
525/// retain
526/// copy
527/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000528/// atomic
529/// strong
530/// weak
531/// unsafe_unretained
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000532///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000533void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000534 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000535 BalancedDelimiterTracker T(*this, tok::l_paren);
536 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000537
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000538 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000539 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000540 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000541 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000542 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000543 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000544
Chris Lattner76619232008-10-20 07:22:18 +0000545 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000546 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000547 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000548 return;
549 }
Mike Stump11289f42009-09-09 15:08:12 +0000550
Chris Lattner1db33542008-10-20 07:37:22 +0000551 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000552
Chris Lattner68e48682008-11-20 04:42:34 +0000553 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000554 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000555 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000556 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000557 else if (II->isStr("unsafe_unretained"))
558 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000559 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000560 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000561 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000562 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000563 else if (II->isStr("strong"))
564 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000565 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000566 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000567 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000568 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000569 else if (II->isStr("atomic"))
570 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000571 else if (II->isStr("weak"))
572 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000573 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000574 bool IsSetter = II->getNameStart()[0] == 's';
575
Chris Lattner825bca12008-10-20 07:39:53 +0000576 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000577 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
578 diag::err_objc_expected_equal_for_getter;
579
Alp Toker383d2c42014-01-01 03:08:43 +0000580 if (ExpectAndConsume(tok::equal, DiagID)) {
581 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000582 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000583 }
Mike Stump11289f42009-09-09 15:08:12 +0000584
Douglas Gregorc8537c52009-11-19 07:41:15 +0000585 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000586 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000587 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000588 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000589 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000590 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000591 }
592
Anders Carlssonfe15a782010-10-02 17:45:21 +0000593
594 SourceLocation SelLoc;
595 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
596
597 if (!SelIdent) {
598 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
599 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000600 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000601 return;
602 }
Mike Stump11289f42009-09-09 15:08:12 +0000603
Anders Carlssonfe15a782010-10-02 17:45:21 +0000604 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000605 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000606 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000607
Alp Toker383d2c42014-01-01 03:08:43 +0000608 if (ExpectAndConsume(tok::colon,
609 diag::err_expected_colon_after_setter_name)) {
610 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000611 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000612 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000613 } else {
614 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000615 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000616 }
Chris Lattner825bca12008-10-20 07:39:53 +0000617 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000618 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000619 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000620 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000621 }
Mike Stump11289f42009-09-09 15:08:12 +0000622
Chris Lattner1db33542008-10-20 07:37:22 +0000623 if (Tok.isNot(tok::comma))
624 break;
Mike Stump11289f42009-09-09 15:08:12 +0000625
Chris Lattner1db33542008-10-20 07:37:22 +0000626 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000627 }
Mike Stump11289f42009-09-09 15:08:12 +0000628
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000629 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000630}
631
Steve Naroff09bf8152007-09-06 21:24:23 +0000632/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000633/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000634/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000635///
636/// objc-instance-method: '-'
637/// objc-class-method: '+'
638///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000639/// objc-method-attributes: [OBJC2]
640/// __attribute__((deprecated))
641///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000642Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000643 bool MethodDefinition) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000644 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000645
Mike Stump11289f42009-09-09 15:08:12 +0000646 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000647 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000648 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000649 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000650 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000651 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000652 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000653}
654
655/// objc-selector:
656/// identifier
657/// one of
658/// enum struct union if else while do for switch case default
659/// break continue return goto asm sizeof typeof __alignof
660/// unsigned long const short volatile signed restrict _Complex
661/// in out inout bycopy byref oneway int char float double void _Bool
662///
Chris Lattner4f472a32009-04-11 18:13:45 +0000663IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000664
Chris Lattner5700fab2007-10-07 02:00:24 +0000665 switch (Tok.getKind()) {
666 default:
Craig Topper161e4db2014-05-21 06:02:52 +0000667 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000668 case tok::ampamp:
669 case tok::ampequal:
670 case tok::amp:
671 case tok::pipe:
672 case tok::tilde:
673 case tok::exclaim:
674 case tok::exclaimequal:
675 case tok::pipepipe:
676 case tok::pipeequal:
677 case tok::caret:
678 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000679 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +0000680 if (isLetter(ThisTok[0])) {
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000681 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
682 Tok.setKind(tok::identifier);
683 SelectorLoc = ConsumeToken();
684 return II;
685 }
Craig Topper161e4db2014-05-21 06:02:52 +0000686 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000687 }
688
Chris Lattner5700fab2007-10-07 02:00:24 +0000689 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000690 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000691 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000692 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000693 case tok::kw_break:
694 case tok::kw_case:
695 case tok::kw_catch:
696 case tok::kw_char:
697 case tok::kw_class:
698 case tok::kw_const:
699 case tok::kw_const_cast:
700 case tok::kw_continue:
701 case tok::kw_default:
702 case tok::kw_delete:
703 case tok::kw_do:
704 case tok::kw_double:
705 case tok::kw_dynamic_cast:
706 case tok::kw_else:
707 case tok::kw_enum:
708 case tok::kw_explicit:
709 case tok::kw_export:
710 case tok::kw_extern:
711 case tok::kw_false:
712 case tok::kw_float:
713 case tok::kw_for:
714 case tok::kw_friend:
715 case tok::kw_goto:
716 case tok::kw_if:
717 case tok::kw_inline:
718 case tok::kw_int:
719 case tok::kw_long:
720 case tok::kw_mutable:
721 case tok::kw_namespace:
722 case tok::kw_new:
723 case tok::kw_operator:
724 case tok::kw_private:
725 case tok::kw_protected:
726 case tok::kw_public:
727 case tok::kw_register:
728 case tok::kw_reinterpret_cast:
729 case tok::kw_restrict:
730 case tok::kw_return:
731 case tok::kw_short:
732 case tok::kw_signed:
733 case tok::kw_sizeof:
734 case tok::kw_static:
735 case tok::kw_static_cast:
736 case tok::kw_struct:
737 case tok::kw_switch:
738 case tok::kw_template:
739 case tok::kw_this:
740 case tok::kw_throw:
741 case tok::kw_true:
742 case tok::kw_try:
743 case tok::kw_typedef:
744 case tok::kw_typeid:
745 case tok::kw_typename:
746 case tok::kw_typeof:
747 case tok::kw_union:
748 case tok::kw_unsigned:
749 case tok::kw_using:
750 case tok::kw_virtual:
751 case tok::kw_void:
752 case tok::kw_volatile:
753 case tok::kw_wchar_t:
754 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000755 case tok::kw__Bool:
756 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000757 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000758 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000759 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000760 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000761 }
Steve Naroff99264b42007-08-22 16:35:03 +0000762}
763
Fariborz Jahanian83615522008-01-02 22:54:34 +0000764/// objc-for-collection-in: 'in'
765///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000766bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000767 // FIXME: May have to do additional look-ahead to only allow for
768 // valid tokens following an 'in'; such as an identifier, unary operators,
769 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000770 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000771 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000772}
773
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000774/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000775/// qualifier list and builds their bitmask representation in the input
776/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000777///
778/// objc-type-qualifiers:
779/// objc-type-qualifier
780/// objc-type-qualifiers objc-type-qualifier
781///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000782void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000783 Declarator::TheContext Context) {
784 assert(Context == Declarator::ObjCParameterContext ||
785 Context == Declarator::ObjCResultContext);
786
Chris Lattner61511e12007-12-12 06:56:32 +0000787 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000788 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000789 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +0000790 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000791 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +0000792 }
793
Chris Lattner5e530bc2007-12-27 19:57:00 +0000794 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000795 return;
Mike Stump11289f42009-09-09 15:08:12 +0000796
Chris Lattner61511e12007-12-12 06:56:32 +0000797 const IdentifierInfo *II = Tok.getIdentifierInfo();
798 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000799 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000800 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000801
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000802 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000803 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +0000804 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000805 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
806 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
807 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
808 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
809 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
810 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000811 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000812 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000813 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +0000814 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +0000815 break;
816 }
Mike Stump11289f42009-09-09 15:08:12 +0000817
Chris Lattner61511e12007-12-12 06:56:32 +0000818 // If this wasn't a recognized qualifier, bail out.
819 if (II) return;
820 }
821}
822
John McCalla55902b2011-10-01 09:56:14 +0000823/// Take all the decl attributes out of the given list and add
824/// them to the given attribute set.
825static void takeDeclAttributes(ParsedAttributes &attrs,
826 AttributeList *list) {
827 while (list) {
828 AttributeList *cur = list;
829 list = cur->getNext();
830
831 if (!cur->isUsedAsTypeAttr()) {
832 // Clear out the next pointer. We're really completely
833 // destroying the internal invariants of the declarator here,
834 // but it doesn't matter because we're done with it.
Craig Topper161e4db2014-05-21 06:02:52 +0000835 cur->setNext(nullptr);
John McCalla55902b2011-10-01 09:56:14 +0000836 attrs.add(cur);
837 }
838 }
839}
840
841/// takeDeclAttributes - Take all the decl attributes from the given
842/// declarator and add them to the given list.
843static void takeDeclAttributes(ParsedAttributes &attrs,
844 Declarator &D) {
845 // First, take ownership of all attributes.
846 attrs.getPool().takeAllFrom(D.getAttributePool());
847 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
848
849 // Now actually move the attributes over.
850 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
851 takeDeclAttributes(attrs, D.getAttributes());
852 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
853 takeDeclAttributes(attrs,
854 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
855}
856
Chris Lattner61511e12007-12-12 06:56:32 +0000857/// objc-type-name:
858/// '(' objc-type-qualifiers[opt] type-name ')'
859/// '(' objc-type-qualifiers[opt] ')'
860///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000861ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000862 Declarator::TheContext context,
863 ParsedAttributes *paramAttrs) {
864 assert(context == Declarator::ObjCParameterContext ||
865 context == Declarator::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +0000866 assert((paramAttrs != nullptr) ==
867 (context == Declarator::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +0000868
Chris Lattner0ef13522007-10-09 17:51:17 +0000869 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000870
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000871 BalancedDelimiterTracker T(*this, tok::l_paren);
872 T.consumeOpen();
873
Chris Lattner2ebb1782008-08-23 01:48:03 +0000874 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000875 ObjCDeclContextSwitch ObjCDC(*this);
876
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000877 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +0000878 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000879
John McCallba7bf592010-08-24 05:47:05 +0000880 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000881 if (isTypeSpecifierQualifier()) {
John McCalla55902b2011-10-01 09:56:14 +0000882 // Parse an abstract declarator.
883 DeclSpec declSpec(AttrFactory);
884 declSpec.setObjCQualifiers(&DS);
885 ParseSpecifierQualifierList(declSpec);
Fariborz Jahanianb6499eb2012-05-29 21:52:45 +0000886 declSpec.SetRangeEnd(Tok.getLocation());
John McCalla55902b2011-10-01 09:56:14 +0000887 Declarator declarator(declSpec, context);
888 ParseDeclarator(declarator);
889
890 // If that's not invalid, extract a type.
891 if (!declarator.isInvalidType()) {
892 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
893 if (!type.isInvalid())
894 Ty = type.get();
895
896 // If we're parsing a parameter, steal all the decl attributes
897 // and add them to the decl spec.
898 if (context == Declarator::ObjCParameterContext)
899 takeDeclAttributes(*paramAttrs, declarator);
900 }
901 } else if (context == Declarator::ObjCResultContext &&
902 Tok.is(tok::identifier)) {
Douglas Gregorbab8a962011-09-08 01:46:34 +0000903 if (!Ident_instancetype)
904 Ident_instancetype = PP.getIdentifierInfo("instancetype");
905
906 if (Tok.getIdentifierInfo() == Ident_instancetype) {
907 Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
908 ConsumeToken();
909 }
Douglas Gregor220cac52009-02-18 17:45:20 +0000910 }
Douglas Gregorbab8a962011-09-08 01:46:34 +0000911
Steve Naroff90255b42008-10-21 14:15:04 +0000912 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000913 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000914 else if (Tok.getLocation() == TypeStartLoc) {
915 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000916 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000917 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +0000918 } else {
919 // Otherwise, we found *something*, but didn't get a ')' in the right
920 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000921 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +0000922 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000923 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000924}
925
926/// objc-method-decl:
927/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000928/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000929/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000930/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000931///
932/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000933/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000934/// objc-keyword-selector objc-keyword-decl
935///
936/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000937/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
938/// objc-selector ':' objc-keyword-attributes[opt] identifier
939/// ':' objc-type-name objc-keyword-attributes[opt] identifier
940/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000941///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000942/// objc-parmlist:
943/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000944///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000945/// objc-parms:
946/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000947///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000948/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000949/// , ...
950///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000951/// objc-keyword-attributes: [OBJC2]
952/// __attribute__((unused))
953///
John McCall48871652010-08-21 09:40:31 +0000954Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000955 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000956 tok::ObjCKeywordKind MethodImplKind,
957 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +0000958 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +0000959
Douglas Gregor636a61e2010-04-07 00:21:17 +0000960 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000961 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000962 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000963 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000964 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000965 }
966
Chris Lattner2ebb1782008-08-23 01:48:03 +0000967 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +0000968 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000969 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000970 if (Tok.is(tok::l_paren))
Craig Topper161e4db2014-05-21 06:02:52 +0000971 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext,
972 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000973
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000974 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +0000975 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +0000976 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +0000977 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000978
Douglas Gregor636a61e2010-04-07 00:21:17 +0000979 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000980 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000981 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000982 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000983 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +0000984 }
985
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000986 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +0000987 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000988 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000989
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000990 // An unnamed colon is valid.
991 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000992 Diag(Tok, diag::err_expected_selector_for_method)
993 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000994 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000995 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +0000996 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +0000997 }
Mike Stump11289f42009-09-09 15:08:12 +0000998
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000999 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001000 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001001 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001002 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001003 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001004
Chris Lattner5700fab2007-10-07 02:00:24 +00001005 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001006 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001007 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001008 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001009 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001010 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001011 methodAttrs.getList(), MethodImplKind,
1012 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001013 PD.complete(Result);
1014 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001015 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001016
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001017 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001018 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001019 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001020 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1021 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001022
1023 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001024 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001025 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001026 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001027
Chris Lattner5700fab2007-10-07 02:00:24 +00001028 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001029 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001030 break;
Mike Stump11289f42009-09-09 15:08:12 +00001031
John McCallba7bf592010-08-24 05:47:05 +00001032 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001033 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001034 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1035 Declarator::ObjCParameterContext,
1036 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001037
Chris Lattner5700fab2007-10-07 02:00:24 +00001038 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001039 // Regardless, collect all the attributes we've parsed so far.
Craig Topper161e4db2014-05-21 06:02:52 +00001040 ArgInfo.ArgAttrs = nullptr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001041 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001042 MaybeParseGNUAttributes(paramAttrs);
1043 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001044 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001045
Douglas Gregor45879692010-07-08 23:37:41 +00001046 // Code completion for the next piece of the selector.
1047 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001048 KeyIdents.push_back(SelIdent);
1049 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1050 mType == tok::minus,
1051 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001052 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001053 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001054 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001055 }
1056
Chris Lattner0ef13522007-10-09 17:51:17 +00001057 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001058 Diag(Tok, diag::err_expected)
1059 << tok::identifier; // missing argument name.
Chris Lattner5700fab2007-10-07 02:00:24 +00001060 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001061 }
Mike Stump11289f42009-09-09 15:08:12 +00001062
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001063 ArgInfo.Name = Tok.getIdentifierInfo();
1064 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001065 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001066
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001067 ArgInfos.push_back(ArgInfo);
1068 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001069 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001070
John McCall084e83d2011-03-24 11:26:52 +00001071 // Make sure the attributes persist.
1072 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1073
Douglas Gregor95887f92010-07-08 23:20:03 +00001074 // Code completion for the next piece of the selector.
1075 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001076 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1077 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001078 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001079 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001080 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001081 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001082 }
1083
Chris Lattner5700fab2007-10-07 02:00:24 +00001084 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001085 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001086 if (!SelIdent && Tok.isNot(tok::colon))
1087 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001088 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001089 SourceLocation ColonLoc = Tok.getLocation();
1090 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001091 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1092 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1093 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001094 }
1095 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001096 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001097 }
Mike Stump11289f42009-09-09 15:08:12 +00001098
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001099 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001100 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001101 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001102 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001103 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001104 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001105 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001106 ConsumeToken();
1107 break;
1108 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001109 if (!cStyleParamWarned) {
1110 Diag(Tok, diag::warn_cstyle_param);
1111 cStyleParamWarned = true;
1112 }
John McCall084e83d2011-03-24 11:26:52 +00001113 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001114 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001115 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001116 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1117 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001118 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001119 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001120 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1121 ParmDecl.getIdentifierLoc(),
1122 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001123 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001124 }
Mike Stump11289f42009-09-09 15:08:12 +00001125
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001126 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001127 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001128 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001129 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001130
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001131 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001132 return nullptr;
1133
Chris Lattner5700fab2007-10-07 02:00:24 +00001134 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1135 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001136 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001137 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001138 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001139 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001140 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001141 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001142 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001143
John McCall28a6aea2009-11-04 02:18:39 +00001144 PD.complete(Result);
1145 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001146}
1147
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001148/// objc-protocol-refs:
1149/// '<' identifier-list '>'
1150///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001151bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001152ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1153 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001154 bool WarnOnDeclarations,
1155 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001156 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001157
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001158 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001159
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001160 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001161
Chris Lattner3bbae002008-07-26 04:03:38 +00001162 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001163 if (Tok.is(tok::code_completion)) {
1164 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1165 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001166 cutOffParsing();
1167 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001168 }
1169
Chris Lattner3bbae002008-07-26 04:03:38 +00001170 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001171 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001172 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001173 return true;
1174 }
1175 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1176 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001177 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001178 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001179
Alp Toker383d2c42014-01-01 03:08:43 +00001180 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001181 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001182 }
Mike Stump11289f42009-09-09 15:08:12 +00001183
Chris Lattner3bbae002008-07-26 04:03:38 +00001184 // Consume the '>'.
Nico Weber7aa4a882012-12-14 18:22:38 +00001185 if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true))
Chris Lattner3bbae002008-07-26 04:03:38 +00001186 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001187
Chris Lattner3bbae002008-07-26 04:03:38 +00001188 // Convert the list of protocols identifiers into a list of protocol decls.
1189 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1190 &ProtocolIdents[0], ProtocolIdents.size(),
1191 Protocols);
1192 return false;
1193}
1194
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001195/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1196/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001197bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001198 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001199 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001200 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001201 SmallVector<Decl *, 8> ProtocolDecl;
1202 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001203 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1204 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001205 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1206 ProtocolLocs.data(), LAngleLoc);
1207 if (EndProtoLoc.isValid())
1208 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001209 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001210}
1211
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001212void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1213 BalancedDelimiterTracker &T,
1214 SmallVectorImpl<Decl *> &AllIvarDecls,
1215 bool RBraceMissing) {
1216 if (!RBraceMissing)
1217 T.consumeClose();
1218
1219 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1220 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1221 Actions.ActOnObjCContainerFinishDefinition();
1222 // Call ActOnFields() even if we don't have any decls. This is useful
1223 // for code rewriting tools that need to be aware of the empty list.
1224 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1225 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001226 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001227}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001228
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001229/// objc-class-instance-variables:
1230/// '{' objc-instance-variable-decl-list[opt] '}'
1231///
1232/// objc-instance-variable-decl-list:
1233/// objc-visibility-spec
1234/// objc-instance-variable-decl ';'
1235/// ';'
1236/// objc-instance-variable-decl-list objc-visibility-spec
1237/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1238/// objc-instance-variable-decl-list ';'
1239///
1240/// objc-visibility-spec:
1241/// @private
1242/// @protected
1243/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001244/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001245///
1246/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001247/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001248///
John McCall48871652010-08-21 09:40:31 +00001249void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001250 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001251 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001252 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001253 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001254
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001255 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001256 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001257
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001258 BalancedDelimiterTracker T(*this, tok::l_brace);
1259 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001260 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001261 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001262 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001263
Steve Naroff00433d32007-08-21 21:17:12 +00001264 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001265 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001266 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001267 continue;
1268 }
Mike Stump11289f42009-09-09 15:08:12 +00001269
Steve Naroff00433d32007-08-21 21:17:12 +00001270 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001271 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001272 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001273 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001274 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001275 }
1276
Steve Naroff7c348172007-08-23 18:16:40 +00001277 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001278 case tok::objc_private:
1279 case tok::objc_public:
1280 case tok::objc_protected:
1281 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001282 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001283 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001284 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001285
1286 case tok::objc_end:
1287 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001288 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1289 Tok.setKind(tok::at);
1290 Tok.setLength(1);
1291 PP.EnterToken(Tok);
1292 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1293 T, AllIvarDecls, true);
1294 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001295
1296 default:
1297 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1298 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001299 }
1300 }
Mike Stump11289f42009-09-09 15:08:12 +00001301
Douglas Gregor48d46252010-01-13 21:54:15 +00001302 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001303 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001304 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001305 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001306 }
John McCallcfefb6d2009-11-03 02:38:08 +00001307
Benjamin Kramera39beb92014-09-03 11:06:10 +00001308 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1309 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1310 // Install the declarator into the interface decl.
1311 Decl *Field = Actions.ActOnIvar(
1312 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1313 FD.BitfieldSize, visibility);
1314 Actions.ActOnObjCContainerFinishDefinition();
1315 if (Field)
1316 AllIvarDecls.push_back(Field);
1317 FD.complete(Field);
1318 };
John McCallcfefb6d2009-11-03 02:38:08 +00001319
Chris Lattnera12405b2008-04-10 06:46:29 +00001320 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001321 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001322 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001323
Chris Lattner0ef13522007-10-09 17:51:17 +00001324 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001325 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001326 } else {
1327 Diag(Tok, diag::err_expected_semi_decl_list);
1328 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001329 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001330 }
1331 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001332 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1333 T, AllIvarDecls, false);
Steve Naroff00433d32007-08-21 21:17:12 +00001334 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001335}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001336
1337/// objc-protocol-declaration:
1338/// objc-protocol-definition
1339/// objc-protocol-forward-reference
1340///
1341/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00001342/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00001343/// objc-protocol-refs[opt]
1344/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00001345/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001346///
1347/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00001348/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001349///
James Dennett1355bd12012-06-11 06:19:40 +00001350/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001351/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001352/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001353Parser::DeclGroupPtrTy
1354Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1355 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001356 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001357 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1358 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001359
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001360 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001361 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001362 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00001363 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001364 }
1365
Nico Weber69a79142013-04-04 00:15:10 +00001366 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00001367
Chris Lattner0ef13522007-10-09 17:51:17 +00001368 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001369 Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00001370 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001371 }
1372 // Save the protocol name, then consume it.
1373 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1374 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001375
Alp Toker383d2c42014-01-01 03:08:43 +00001376 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001377 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001378 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001379 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001380 }
Mike Stump11289f42009-09-09 15:08:12 +00001381
Erik Verbruggenf9887852011-12-08 09:58:43 +00001382 CheckNestedObjCContexts(AtLoc);
1383
Chris Lattner0ef13522007-10-09 17:51:17 +00001384 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001385 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001386 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1387
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001388 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001389 while (1) {
1390 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001391 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001392 Diag(Tok, diag::err_expected) << tok::identifier;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001393 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00001394 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001395 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001396 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1397 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001398 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001399
Chris Lattner0ef13522007-10-09 17:51:17 +00001400 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001401 break;
1402 }
1403 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00001404 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00001405 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001406
Steve Naroff93eb5f12007-10-10 17:32:04 +00001407 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001408 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001409 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001410 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001411 }
Mike Stump11289f42009-09-09 15:08:12 +00001412
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001413 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001414 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001415
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001416 SmallVector<Decl *, 8> ProtocolRefs;
1417 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001418 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001419 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1420 LAngleLoc, EndProtoLoc))
Douglas Gregorf6102672012-01-01 21:23:57 +00001421 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001422
John McCall48871652010-08-21 09:40:31 +00001423 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001424 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001425 ProtocolRefs.data(),
1426 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001427 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001428 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001429
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001430 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00001431 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001432}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001433
1434/// objc-implementation:
1435/// objc-class-implementation-prologue
1436/// objc-category-implementation-prologue
1437///
1438/// objc-class-implementation-prologue:
1439/// @implementation identifier objc-superclass[opt]
1440/// objc-class-instance-variables[opt]
1441///
1442/// objc-category-implementation-prologue:
1443/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001444Parser::DeclGroupPtrTy
1445Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001446 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1447 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001448 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001449 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001450
Douglas Gregor49c22a72009-11-18 16:26:39 +00001451 // Code completion after '@implementation'.
1452 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001453 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001454 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001455 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001456 }
1457
Nico Weber69a79142013-04-04 00:15:10 +00001458 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00001459
Chris Lattner0ef13522007-10-09 17:51:17 +00001460 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001461 Diag(Tok, diag::err_expected)
1462 << tok::identifier; // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001463 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001464 }
1465 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001466 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001467 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00001468 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001469
1470 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001471 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001472 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001473 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001474 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001475
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001476 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001477 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001478 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001479 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001480 }
1481
Chris Lattner0ef13522007-10-09 17:51:17 +00001482 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001483 categoryId = Tok.getIdentifierInfo();
1484 categoryLoc = ConsumeToken();
1485 } else {
Alp Tokerec543272013-12-24 09:48:30 +00001486 Diag(Tok, diag::err_expected)
1487 << tok::identifier; // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001488 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001489 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001490 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001491 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001492 SkipUntil(tok::r_paren); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001493 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001494 }
1495 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00001496 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1497 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1498 AttributeFactory attr;
1499 DeclSpec DS(attr);
1500 (void)ParseObjCProtocolQualifiers(DS);
1501 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001502 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001503 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001504 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001505
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001506 } else {
1507 // We have a class implementation
1508 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001509 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00001510 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001511 // We have a super class
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001512 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001513 Diag(Tok, diag::err_expected)
1514 << tok::identifier; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001515 return DeclGroupPtrTy();
1516 }
1517 superClassId = Tok.getIdentifierInfo();
1518 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001519 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001520 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1521 AtLoc, nameId, nameLoc,
1522 superClassId, superClassLoc);
1523
1524 if (Tok.is(tok::l_brace)) // we have ivars
1525 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00001526 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1527 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1528 // try to recover.
1529 AttributeFactory attr;
1530 DeclSpec DS(attr);
1531 (void)ParseObjCProtocolQualifiers(DS);
1532 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001533 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001534 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001535
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001536 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001537
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001538 {
1539 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00001540 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001541 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001542 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001543 MaybeParseMicrosoftAttributes(attrs);
1544 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1545 DeclGroupRef DG = DGP.get();
1546 DeclsInGroup.append(DG.begin(), DG.end());
1547 }
1548 }
1549 }
1550
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001551 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001552}
Steve Naroff33a1e802007-10-29 21:38:07 +00001553
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001554Parser::DeclGroupPtrTy
1555Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001556 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1557 "ParseObjCAtEndDeclaration(): Expected @end");
1558 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001559 if (CurParsedObjCImpl)
1560 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001561 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00001562 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001563 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001564 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001565}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001566
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001567Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1568 if (!Finished) {
1569 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00001570 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001571 P.Diag(P.Tok, diag::err_objc_missing_end)
1572 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1573 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1574 << Sema::OCK_Implementation;
1575 }
1576 }
Craig Topper161e4db2014-05-21 06:02:52 +00001577 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001578 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001579}
1580
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001581void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1582 assert(!Finished);
1583 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1584 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001585 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1586 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001587
1588 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1589
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001590 if (HasCFunction)
1591 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1592 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1593 false/*c-functions*/);
1594
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001595 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001596 for (LateParsedObjCMethodContainer::iterator
1597 I = LateParsedObjCMethods.begin(),
1598 E = LateParsedObjCMethods.end(); I != E; ++I)
1599 delete *I;
1600 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001601
1602 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001603}
1604
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001605/// compatibility-alias-decl:
1606/// @compatibility_alias alias-name class-name ';'
1607///
John McCall48871652010-08-21 09:40:31 +00001608Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001609 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1610 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1611 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001612 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001613 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00001614 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001615 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001616 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1617 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001618 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001619 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00001620 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001621 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001622 IdentifierInfo *classId = Tok.getIdentifierInfo();
1623 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00001624 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00001625 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1626 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001627}
1628
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001629/// property-synthesis:
1630/// @synthesize property-ivar-list ';'
1631///
1632/// property-ivar-list:
1633/// property-ivar
1634/// property-ivar-list ',' property-ivar
1635///
1636/// property-ivar:
1637/// identifier
1638/// identifier '=' identifier
1639///
John McCall48871652010-08-21 09:40:31 +00001640Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001641 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00001642 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001643 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001644
Douglas Gregor88e72a02009-11-18 19:45:45 +00001645 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001646 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001647 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001648 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001649 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00001650 }
1651
Douglas Gregor88e72a02009-11-18 19:45:45 +00001652 if (Tok.isNot(tok::identifier)) {
1653 Diag(Tok, diag::err_synthesized_property_name);
1654 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00001655 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001656 }
Craig Topper161e4db2014-05-21 06:02:52 +00001657
1658 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001659 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1660 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001661 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00001662 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001663 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00001664 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001665 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001666 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001667 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00001668 }
1669
Chris Lattner0ef13522007-10-09 17:51:17 +00001670 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001671 Diag(Tok, diag::err_expected) << tok::identifier;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001672 break;
1673 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001674 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001675 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001676 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001677 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001678 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001679 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001680 break;
1681 ConsumeToken(); // consume ','
1682 }
Alp Toker383d2c42014-01-01 03:08:43 +00001683 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00001684 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001685}
1686
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001687/// property-dynamic:
1688/// @dynamic property-list
1689///
1690/// property-list:
1691/// identifier
1692/// property-list ',' identifier
1693///
John McCall48871652010-08-21 09:40:31 +00001694Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001695 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1696 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001697 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001698 while (true) {
1699 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001700 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001701 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001702 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001703 }
1704
1705 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001706 Diag(Tok, diag::err_expected) << tok::identifier;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001707 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00001708 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001709 }
1710
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001711 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1712 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001713 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Craig Topper161e4db2014-05-21 06:02:52 +00001714 propertyId, nullptr, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001715
Chris Lattner0ef13522007-10-09 17:51:17 +00001716 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001717 break;
1718 ConsumeToken(); // consume ','
1719 }
Alp Toker383d2c42014-01-01 03:08:43 +00001720 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00001721 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001722}
Mike Stump11289f42009-09-09 15:08:12 +00001723
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001724/// objc-throw-statement:
1725/// throw expression[opt];
1726///
John McCalldadc5752010-08-24 06:29:42 +00001727StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1728 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001729 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001730 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001731 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001732 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001733 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001734 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001735 }
1736 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001737 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00001738 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001739 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001740}
1741
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001742/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001743/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001744///
John McCalldadc5752010-08-24 06:29:42 +00001745StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001746Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001747 ConsumeToken(); // consume synchronized
1748 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001749 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001750 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001751 }
John McCalld9bb7432011-07-27 21:50:02 +00001752
1753 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001754 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00001755 ExprResult operand(ParseExpression());
1756
1757 if (Tok.is(tok::r_paren)) {
1758 ConsumeParen(); // ')'
1759 } else {
1760 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00001761 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00001762
1763 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001764 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001765 }
John McCalld9bb7432011-07-27 21:50:02 +00001766
1767 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001768 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00001769 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00001770 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001771 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001772 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001773
John McCalld9bb7432011-07-27 21:50:02 +00001774 // Check the @synchronized operand now.
1775 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001776 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001777
John McCalld9bb7432011-07-27 21:50:02 +00001778 // Parse the compound statement within a new scope.
1779 ParseScope bodyScope(this, Scope::DeclScope);
1780 StmtResult body(ParseCompoundStatementBody());
1781 bodyScope.Exit();
1782
1783 // If there was a semantic or parse error earlier with the
1784 // operand, fail now.
1785 if (operand.isInvalid())
1786 return StmtError();
1787
1788 if (body.isInvalid())
1789 body = Actions.ActOnNullStmt(Tok.getLocation());
1790
1791 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001792}
1793
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001794/// objc-try-catch-statement:
1795/// @try compound-statement objc-catch-list[opt]
1796/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1797///
1798/// objc-catch-list:
1799/// @catch ( parameter-declaration ) compound-statement
1800/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1801/// catch-parameter-declaration:
1802/// parameter-declaration
1803/// '...' [OBJC2]
1804///
John McCalldadc5752010-08-24 06:29:42 +00001805StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001806 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001807
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001808 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001809 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00001810 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001811 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001812 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00001813 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00001814 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001815 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001816 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001817 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001818 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001819 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001820
Chris Lattner0ef13522007-10-09 17:51:17 +00001821 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001822 // At this point, we need to lookahead to determine if this @ is the start
1823 // of an @catch or @finally. We don't want to consume the @ token if this
1824 // is an @try or @encode or something else.
1825 Token AfterAt = GetLookAheadToken(1);
1826 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1827 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1828 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001829
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001830 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001831 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00001832 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001833 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001834 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001835 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001836 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001837 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001838 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001839 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00001840 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00001841 ParseDeclarator(ParmDecl);
1842
Douglas Gregore11ee112010-04-23 23:01:43 +00001843 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001844 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001845 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001846 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001847 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001848
Steve Naroff65a00892009-04-07 22:56:58 +00001849 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001850
Steve Naroff65a00892009-04-07 22:56:58 +00001851 if (Tok.is(tok::r_paren))
1852 RParenLoc = ConsumeParen();
1853 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001854 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00001855
John McCalldadc5752010-08-24 06:29:42 +00001856 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001857 if (Tok.is(tok::l_brace))
1858 CatchBody = ParseCompoundStatementBody();
1859 else
Alp Tokerec543272013-12-24 09:48:30 +00001860 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001861 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001862 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001863
John McCalldadc5752010-08-24 06:29:42 +00001864 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001865 RParenLoc,
1866 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001867 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00001868 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001869 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00001870
Steve Naroffe6016792008-02-05 21:27:35 +00001871 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001872 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1873 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001874 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001875 }
1876 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001877 } else {
1878 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001879 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001880 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001881
John McCalldadc5752010-08-24 06:29:42 +00001882 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001883 if (Tok.is(tok::l_brace))
1884 FinallyBody = ParseCompoundStatementBody();
1885 else
Alp Tokerec543272013-12-24 09:48:30 +00001886 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001887 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001888 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001889 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001890 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001891 catch_or_finally_seen = true;
1892 break;
1893 }
1894 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001895 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001896 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001897 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001898 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001899
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001900 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001901 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001902 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001903}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001904
John McCall31168b02011-06-15 23:02:42 +00001905/// objc-autoreleasepool-statement:
1906/// @autoreleasepool compound-statement
1907///
1908StmtResult
1909Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1910 ConsumeToken(); // consume autoreleasepool
1911 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00001912 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00001913 return StmtError();
1914 }
1915 // Enter a scope to hold everything within the compound stmt. Compound
1916 // statements can always hold declarations.
1917 ParseScope BodyScope(this, Scope::DeclScope);
1918
1919 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1920
1921 BodyScope.Exit();
1922 if (AutoreleasePoolBody.isInvalid())
1923 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1924 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001925 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00001926}
1927
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001928/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
1929/// for later parsing.
1930void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
1931 LexedMethod* LM = new LexedMethod(this, MDecl);
1932 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
1933 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00001934 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001935 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00001936 if (Tok.is(tok::kw_try)) {
1937 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00001938 if (Tok.is(tok::colon)) {
1939 Toks.push_back(Tok);
1940 ConsumeToken();
1941 while (Tok.isNot(tok::l_brace)) {
1942 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1943 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1944 }
1945 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00001946 Toks.push_back(Tok); // also store '{'
1947 }
1948 else if (Tok.is(tok::colon)) {
1949 ConsumeToken();
1950 while (Tok.isNot(tok::l_brace)) {
1951 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1952 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1953 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00001954 Toks.push_back(Tok); // also store '{'
1955 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001956 ConsumeBrace();
1957 // Consume everything up to (and including) the matching right brace.
1958 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00001959 while (Tok.is(tok::kw_catch)) {
1960 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
1961 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1962 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001963}
1964
Steve Naroff09bf8152007-09-06 21:24:23 +00001965/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001966///
John McCall48871652010-08-21 09:40:31 +00001967Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001968 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00001969
John McCallfaf5fb42010-08-26 23:41:50 +00001970 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1971 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001972
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001973 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001974 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001975 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001976 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00001977 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001978 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001979 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001980 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001981
Steve Naroffbb875722007-11-11 19:54:21 +00001982 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001983 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001984 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001985
Steve Naroffbb875722007-11-11 19:54:21 +00001986 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001987 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00001988
Steve Naroffbb875722007-11-11 19:54:21 +00001989 // If we didn't find the '{', bail out.
1990 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00001991 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001992 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001993
1994 if (!MDecl) {
1995 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001996 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00001997 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001998 }
1999
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002000 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002001 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002002 assert (CurParsedObjCImpl
2003 && "ParseObjCMethodDefinition - Method out of @implementation");
2004 // Consume the tokens and store them for later parsing.
2005 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002006 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002007}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002008
John McCalldadc5752010-08-24 06:29:42 +00002009StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002010 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002011 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002012 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002013 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002014 }
2015
2016 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002017 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002018
2019 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002020 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002021
2022 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002023 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002024
2025 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2026 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002027
2028 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2029 getLangOpts().DebuggerSupport) {
2030 SkipUntil(tok::semi);
2031 return Actions.ActOnNullStmt(Tok.getLocation());
2032 }
2033
John McCalldadc5752010-08-24 06:29:42 +00002034 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002035 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002036 // If the expression is invalid, skip ahead to the next semicolon. Not
2037 // doing this opens us up to the possibility of infinite loops if
2038 // ParseExpression does not consume any tokens.
2039 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002040 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002041 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002042
Steve Naroffe6016792008-02-05 21:27:35 +00002043 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002044 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002045 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002046}
2047
John McCalldadc5752010-08-24 06:29:42 +00002048ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002049 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002050 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002051 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002052 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002053 return ExprError();
2054
Ted Kremeneke65b0862012-03-06 20:05:56 +00002055 case tok::minus:
2056 case tok::plus: {
2057 tok::TokenKind Kind = Tok.getKind();
2058 SourceLocation OpLoc = ConsumeToken();
2059
2060 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002061 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002062 switch (Kind) {
2063 case tok::minus: Symbol = "-"; break;
2064 case tok::plus: Symbol = "+"; break;
2065 default: llvm_unreachable("missing unary operator case");
2066 }
2067 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2068 << Symbol;
2069 return ExprError();
2070 }
2071
2072 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2073 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002074 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002075 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002076 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002077
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002078 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002079 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002080 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002081
2082 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002083 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002084 }
2085
Chris Lattnere002fbe2007-12-12 01:04:12 +00002086 case tok::string_literal: // primary-expression: string-literal
2087 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002088 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002089
2090 case tok::char_constant:
2091 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2092
2093 case tok::numeric_constant:
2094 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2095
2096 case tok::kw_true: // Objective-C++, etc.
2097 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2098 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2099 case tok::kw_false: // Objective-C++, etc.
2100 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2101 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2102
2103 case tok::l_square:
2104 // Objective-C array literal
2105 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2106
2107 case tok::l_brace:
2108 // Objective-C dictionary literal
2109 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2110
Patrick Beard0caa3942012-04-19 00:25:12 +00002111 case tok::l_paren:
2112 // Objective-C boxed expression
2113 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2114
Chris Lattnere002fbe2007-12-12 01:04:12 +00002115 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002116 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002117 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002118
Chris Lattner197a3012008-08-05 06:19:09 +00002119 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2120 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002121 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002122 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002123 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002124 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002125 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002126 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002127 const char *str = nullptr;
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002128 if (GetLookAheadToken(1).is(tok::l_brace)) {
2129 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2130 str =
2131 ch == 't' ? "try"
2132 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002133 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002134 }
2135 if (str) {
2136 SourceLocation kwLoc = Tok.getLocation();
2137 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2138 FixItHint::CreateReplacement(kwLoc, str));
2139 }
2140 else
2141 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2142 }
Chris Lattner197a3012008-08-05 06:19:09 +00002143 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002144 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002145}
2146
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002147/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002148///
2149/// This routine parses the receiver of a message send in
2150/// Objective-C++ either as a type or as an expression. Note that this
2151/// routine must not be called to parse a send to 'super', since it
2152/// has no way to return such a result.
2153///
2154/// \param IsExpr Whether the receiver was parsed as an expression.
2155///
2156/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2157/// IsExpr is true), the parsed expression. If the receiver was parsed
2158/// as a type (\c IsExpr is false), the parsed type.
2159///
2160/// \returns True if an error occurred during parsing or semantic
2161/// analysis, in which case the arguments do not have valid
2162/// values. Otherwise, returns false for a successful parse.
2163///
2164/// objc-receiver: [C++]
2165/// 'super' [not parsed here]
2166/// expression
2167/// simple-type-specifier
2168/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002169bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002170 InMessageExpressionRAIIObject InMessage(*this, true);
2171
Douglas Gregor8d4de672010-04-21 22:36:40 +00002172 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2173 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
2174 TryAnnotateTypeOrScopeToken();
2175
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002176 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002177 // objc-receiver:
2178 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002179 // Make sure any typos in the receiver are corrected or diagnosed, so that
2180 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2181 // only the things that are valid ObjC receivers?
2182 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002183 if (Receiver.isInvalid())
2184 return true;
2185
2186 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002187 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002188 return false;
2189 }
2190
2191 // objc-receiver:
2192 // typename-specifier
2193 // simple-type-specifier
2194 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002195 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002196 ParseCXXSimpleTypeSpecifier(DS);
2197
2198 if (Tok.is(tok::l_paren)) {
2199 // If we see an opening parentheses at this point, we are
2200 // actually parsing an expression that starts with a
2201 // function-style cast, e.g.,
2202 //
2203 // postfix-expression:
2204 // simple-type-specifier ( expression-list [opt] )
2205 // typename-specifier ( expression-list [opt] )
2206 //
2207 // Parse the remainder of this case, then the (optional)
2208 // postfix-expression suffix, followed by the (optional)
2209 // right-hand side of the binary expression. We have an
2210 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002211 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002212 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002213 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002214 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002215 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002216 if (Receiver.isInvalid())
2217 return true;
2218
2219 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002220 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002221 return false;
2222 }
2223
2224 // We have a class message. Turn the simple-type-specifier or
2225 // typename-specifier we parsed into a type and parse the
2226 // remainder of the class message.
2227 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002228 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002229 if (Type.isInvalid())
2230 return true;
2231
2232 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002233 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002234 return false;
2235}
2236
Douglas Gregor990ccac2010-05-31 14:40:22 +00002237/// \brief Determine whether the parser is currently referring to a an
2238/// Objective-C message send, using a simplified heuristic to avoid overhead.
2239///
2240/// This routine will only return true for a subset of valid message-send
2241/// expressions.
2242bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002243 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002244 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002245 return GetLookAheadToken(1).is(tok::identifier) &&
2246 GetLookAheadToken(2).is(tok::identifier);
2247}
2248
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002249bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002250 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002251 InMessageExpression)
2252 return false;
2253
2254
2255 ParsedType Type;
2256
2257 if (Tok.is(tok::annot_typename))
2258 Type = getTypeAnnotation(Tok);
2259 else if (Tok.is(tok::identifier))
2260 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2261 getCurScope());
2262 else
2263 return false;
2264
2265 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2266 const Token &AfterNext = GetLookAheadToken(2);
2267 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
2268 if (Tok.is(tok::identifier))
2269 TryAnnotateTypeOrScopeToken();
2270
2271 return Tok.is(tok::annot_typename);
2272 }
2273 }
2274
2275 return false;
2276}
2277
Mike Stump11289f42009-09-09 15:08:12 +00002278/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002279/// '[' objc-receiver objc-message-args ']'
2280///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002281/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002282/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002283/// expression
2284/// class-name
2285/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002286///
John McCalldadc5752010-08-24 06:29:42 +00002287ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002288 assert(Tok.is(tok::l_square) && "'[' expected");
2289 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2290
Douglas Gregora817a192010-05-27 23:06:34 +00002291 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002292 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002293 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002294 return ExprError();
2295 }
2296
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002297 InMessageExpressionRAIIObject InMessage(*this, true);
2298
David Blaikiebbafb8a2012-03-11 07:00:24 +00002299 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002300 // We completely separate the C and C++ cases because C++ requires
2301 // more complicated (read: slower) parsing.
2302
2303 // Handle send to super.
2304 // FIXME: This doesn't benefit from the same typo-correction we
2305 // get in Objective-C.
2306 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002307 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002308 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002309 ParsedType(), nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002310
2311 // Parse the receiver, which is either a type or an expression.
2312 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00002313 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002314 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002315 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002316 return ExprError();
2317 }
2318
2319 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002320 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2321 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002322 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002323
2324 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002325 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00002326 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00002327 }
2328
2329 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002330 IdentifierInfo *Name = Tok.getIdentifierInfo();
2331 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002332 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002333 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002334 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002335 NextToken().is(tok::period),
2336 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002337 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002338 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002339 ParsedType(), nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002340
John McCallfaf5fb42010-08-26 23:41:50 +00002341 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002342 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002343 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002344 return ExprError();
2345 }
2346
Douglas Gregore5798dc2010-04-21 20:38:13 +00002347 ConsumeToken(); // the type name
2348
2349 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00002350 ReceiverType, nullptr);
2351
John McCallfaf5fb42010-08-26 23:41:50 +00002352 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002353 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002354 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002355 }
Chris Lattner8f697062008-01-25 18:59:06 +00002356 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002357
2358 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00002359 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002360 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002361 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002362 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00002363 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002364
John McCallba7bf592010-08-24 05:47:05 +00002365 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002366 ParsedType(), Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00002367}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002368
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002369/// \brief Parse the remainder of an Objective-C message following the
2370/// '[' objc-receiver.
2371///
2372/// This routine handles sends to super, class messages (sent to a
2373/// class name), and instance messages (sent to an object), and the
2374/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2375/// ReceiverExpr, respectively. Only one of these parameters may have
2376/// a valid value.
2377///
2378/// \param LBracLoc The location of the opening '['.
2379///
2380/// \param SuperLoc If this is a send to 'super', the location of the
2381/// 'super' keyword that indicates a send to the superclass.
2382///
2383/// \param ReceiverType If this is a class message, the type of the
2384/// class we are sending a message to.
2385///
2386/// \param ReceiverExpr If this is an instance message, the expression
2387/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002388///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002389/// objc-message-args:
2390/// objc-selector
2391/// objc-keywordarg-list
2392///
2393/// objc-keywordarg-list:
2394/// objc-keywordarg
2395/// objc-keywordarg-list objc-keywordarg
2396///
Mike Stump11289f42009-09-09 15:08:12 +00002397/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002398/// selector-name[opt] ':' objc-keywordexpr
2399///
2400/// objc-keywordexpr:
2401/// nonempty-expr-list
2402///
2403/// nonempty-expr-list:
2404/// assignment-expression
2405/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002406///
John McCalldadc5752010-08-24 06:29:42 +00002407ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002408Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002409 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002410 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00002411 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002412 InMessageExpressionRAIIObject InMessage(*this, true);
2413
Steve Naroffeae65032009-11-07 02:08:14 +00002414 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002415 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002416 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002417 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002418 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002419 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002420 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002421 else
John McCallb268a282010-08-23 23:25:46 +00002422 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002423 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002424 cutOffParsing();
2425 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002426 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002427
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002428 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002429 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002430 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002431
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002432 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002433 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002434 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00002435
Chris Lattner0ef13522007-10-09 17:51:17 +00002436 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002437 while (1) {
2438 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002439 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002440 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00002441
Alp Toker383d2c42014-01-01 03:08:43 +00002442 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00002443 // We must manually skip to a ']', otherwise the expression skipper will
2444 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2445 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002446 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002447 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002448 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002449
Mike Stump11289f42009-09-09 15:08:12 +00002450 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002451
2452 if (Tok.is(tok::code_completion)) {
2453 if (SuperLoc.isValid())
2454 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002455 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002456 /*AtArgumentEpression=*/true);
2457 else if (ReceiverType)
2458 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002459 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002460 /*AtArgumentEpression=*/true);
2461 else
2462 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002463 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002464 /*AtArgumentEpression=*/true);
2465
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002466 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002467 return ExprError();
2468 }
2469
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00002470 ExprResult Expr;
2471 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
2472 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2473 Expr = ParseBraceInitializer();
2474 } else
2475 Expr = ParseAssignmentExpression();
2476
2477 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002478 if (Res.isInvalid()) {
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);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002483 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00002484 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002485
Steve Naroff486760a2007-09-17 20:25:27 +00002486 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002487 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002488
Douglas Gregor1b605f72009-11-19 01:08:35 +00002489 // Code completion after each argument.
2490 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002491 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002492 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002493 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002494 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002495 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002496 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002497 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002498 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002499 else
John McCallb268a282010-08-23 23:25:46 +00002500 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002501 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002502 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002503 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002504 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002505 }
2506
Steve Naroff486760a2007-09-17 20:25:27 +00002507 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002508 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002509 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002510 break;
2511 // We have a selector or a colon, continue parsing.
2512 }
2513 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002514 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002515 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002516 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002517 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00002518 if (Tok.is(tok::colon))
2519 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002520 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002521 if (Tok.is(tok::colon)) {
2522 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
2523 FixItHint::CreateRemoval(commaLoc);
2524 }
Chris Lattner197a3012008-08-05 06:19:09 +00002525 // We must manually skip to a ']', otherwise the expression skipper will
2526 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2527 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002528 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002529 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002530 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002531
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002532 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002533 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002534 }
2535 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00002536 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002537
Chris Lattner197a3012008-08-05 06:19:09 +00002538 // We must manually skip to a ']', otherwise the expression skipper will
2539 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2540 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002541 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002542 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002543 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002544
Chris Lattner0ef13522007-10-09 17:51:17 +00002545 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00002546 Diag(Tok, diag::err_expected)
2547 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00002548 // We must manually skip to a ']', otherwise the expression skipper will
2549 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2550 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002551 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002552 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002553 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002554
Chris Lattner8f697062008-01-25 18:59:06 +00002555 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002556
Steve Naroffe61bfa82007-10-05 18:42:47 +00002557 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002558 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00002559 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002560 KeyLocs.push_back(Loc);
2561 }
Chris Lattner5700fab2007-10-07 02:00:24 +00002562 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002563
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002564 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002565 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002566 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002567 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002568 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002569 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00002570 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002571 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002572}
2573
John McCalldadc5752010-08-24 06:29:42 +00002574ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2575 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002576 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002577
Chris Lattnere002fbe2007-12-12 01:04:12 +00002578 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2579 // expressions. At this point, we know that the only valid thing that starts
2580 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002581 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002582 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00002583 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002584 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002585
Chris Lattnere002fbe2007-12-12 01:04:12 +00002586 while (Tok.is(tok::at)) {
2587 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002588
Sebastian Redlc13f2682008-12-09 20:22:58 +00002589 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002590 if (!isTokenStringLiteral())
2591 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002592
John McCalldadc5752010-08-24 06:29:42 +00002593 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002594 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002595 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002596
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002597 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002598 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002599
Nico Webera7c7e602012-12-31 00:28:03 +00002600 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
2601 AtStrings.size());
Anders Carlsson76f4a902007-08-21 17:43:55 +00002602}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002603
Ted Kremeneke65b0862012-03-06 20:05:56 +00002604/// ParseObjCBooleanLiteral -
2605/// objc-scalar-literal : '@' boolean-keyword
2606/// ;
2607/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2608/// ;
2609ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2610 bool ArgValue) {
2611 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2612 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2613}
2614
2615/// ParseObjCCharacterLiteral -
2616/// objc-scalar-literal : '@' character-literal
2617/// ;
2618ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2619 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2620 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002621 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002622 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002623 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002624 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002625}
2626
2627/// ParseObjCNumericLiteral -
2628/// objc-scalar-literal : '@' scalar-literal
2629/// ;
2630/// scalar-literal : | numeric-constant /* any numeric constant. */
2631/// ;
2632ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2633 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2634 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002635 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002636 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002637 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002638 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002639}
2640
Patrick Beard0caa3942012-04-19 00:25:12 +00002641/// ParseObjCBoxedExpr -
2642/// objc-box-expression:
2643/// @( assignment-expression )
2644ExprResult
2645Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2646 if (Tok.isNot(tok::l_paren))
2647 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2648
2649 BalancedDelimiterTracker T(*this, tok::l_paren);
2650 T.consumeOpen();
2651 ExprResult ValueExpr(ParseAssignmentExpression());
2652 if (T.consumeClose())
2653 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00002654
2655 if (ValueExpr.isInvalid())
2656 return ExprError();
2657
Patrick Beard0caa3942012-04-19 00:25:12 +00002658 // Wrap the sub-expression in a parenthesized expression, to distinguish
2659 // a boxed expression from a literal.
2660 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002661 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00002662 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002663 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00002664}
2665
Ted Kremeneke65b0862012-03-06 20:05:56 +00002666ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002667 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002668 ConsumeBracket(); // consume the l_square.
2669
2670 while (Tok.isNot(tok::r_square)) {
2671 // Parse list of array element expressions (all must be id types).
2672 ExprResult Res(ParseAssignmentExpression());
2673 if (Res.isInvalid()) {
2674 // We must manually skip to a ']', otherwise the expression skipper will
2675 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2676 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002677 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002678 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002679 }
2680
2681 // Parse the ellipsis that indicates a pack expansion.
2682 if (Tok.is(tok::ellipsis))
2683 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2684 if (Res.isInvalid())
2685 return true;
2686
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002687 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002688
2689 if (Tok.is(tok::comma))
2690 ConsumeToken(); // Eat the ','.
2691 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00002692 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
2693 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002694 }
2695 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramerf0623432012-08-23 22:51:59 +00002696 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00002697 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002698}
2699
2700ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2701 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2702 ConsumeBrace(); // consume the l_square.
2703 while (Tok.isNot(tok::r_brace)) {
2704 // Parse the comma separated key : value expressions.
2705 ExprResult KeyExpr;
2706 {
2707 ColonProtectionRAIIObject X(*this);
2708 KeyExpr = ParseAssignmentExpression();
2709 if (KeyExpr.isInvalid()) {
2710 // We must manually skip to a '}', otherwise the expression skipper will
2711 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2712 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002713 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002714 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002715 }
2716 }
2717
Alp Toker383d2c42014-01-01 03:08:43 +00002718 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002719 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00002720 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00002721 }
2722
2723 ExprResult ValueExpr(ParseAssignmentExpression());
2724 if (ValueExpr.isInvalid()) {
2725 // We must manually skip to a '}', otherwise the expression skipper will
2726 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2727 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002728 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002729 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002730 }
2731
2732 // Parse the ellipsis that designates this as a pack expansion.
2733 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00002734 if (getLangOpts().CPlusPlus)
2735 TryConsumeToken(tok::ellipsis, EllipsisLoc);
2736
Ted Kremeneke65b0862012-03-06 20:05:56 +00002737 // We have a valid expression. Collect it in a vector so we can
2738 // build the argument list.
2739 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00002740 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00002741 };
2742 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00002743
2744 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002745 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
2746 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002747 }
2748 SourceLocation EndLoc = ConsumeBrace();
2749
2750 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00002751 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2752 Elements.data(), Elements.size());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002753}
2754
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002755/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002756/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002757ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002758Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002759 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002760
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002761 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002762
Chris Lattner197a3012008-08-05 06:19:09 +00002763 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002764 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2765
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002766 BalancedDelimiterTracker T(*this, tok::l_paren);
2767 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002768
Douglas Gregor220cac52009-02-18 17:45:20 +00002769 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002770
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002771 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002772
Douglas Gregor220cac52009-02-18 17:45:20 +00002773 if (Ty.isInvalid())
2774 return ExprError();
2775
Nico Webera7c7e602012-12-31 00:28:03 +00002776 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
2777 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002778}
Anders Carlssone01493d2007-08-23 15:25:28 +00002779
2780/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00002781/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002782ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002783Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002784 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002785
Chris Lattner197a3012008-08-05 06:19:09 +00002786 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002787 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2788
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002789 BalancedDelimiterTracker T(*this, tok::l_paren);
2790 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002791
Chris Lattner197a3012008-08-05 06:19:09 +00002792 if (Tok.isNot(tok::identifier))
Alp Tokerec543272013-12-24 09:48:30 +00002793 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002794
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002795 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00002796 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002797
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002798 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00002799
Nico Webera7c7e602012-12-31 00:28:03 +00002800 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2801 T.getOpenLocation(), ProtoIdLoc,
2802 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00002803}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002804
2805/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00002806/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002807ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002808 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002809
Chris Lattner197a3012008-08-05 06:19:09 +00002810 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002811 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2812
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002813 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002814 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002815
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002816 BalancedDelimiterTracker T(*this, tok::l_paren);
2817 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00002818 bool HasOptionalParen = Tok.is(tok::l_paren);
2819 if (HasOptionalParen)
2820 ConsumeParen();
2821
Douglas Gregor67c692c2010-08-26 15:07:07 +00002822 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002823 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002824 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002825 return ExprError();
2826 }
2827
Chris Lattner4f472a32009-04-11 18:13:45 +00002828 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002829 if (!SelIdent && // missing selector name.
2830 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00002831 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002832
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002833 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00002834
Steve Naroff152dd812007-12-05 22:21:29 +00002835 unsigned nColons = 0;
2836 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002837 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00002838 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00002839 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00002840 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00002841 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
2842 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00002843 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00002844
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002845 if (Tok.is(tok::r_paren))
2846 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002847
2848 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002849 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002850 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002851 return ExprError();
2852 }
2853
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002854 // Check for another keyword selector.
2855 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002856 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002857 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00002858 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002859 break;
2860 }
Steve Naroff152dd812007-12-05 22:21:29 +00002861 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00002862 if (HasOptionalParen && Tok.is(tok::r_paren))
2863 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002864 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00002865 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00002866 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2867 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00002868 T.getCloseLocation(),
2869 !HasOptionalParen);
Gabor Greif24032f12007-10-19 15:38:32 +00002870 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002871
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002872void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
2873 // MCDecl might be null due to error in method or c-function prototype, etc.
2874 Decl *MCDecl = LM.D;
2875 bool skip = MCDecl &&
2876 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
2877 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
2878 if (skip)
2879 return;
2880
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002881 // Save the current token position.
2882 SourceLocation OrigLoc = Tok.getLocation();
2883
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002884 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2885 // Append the current token at the end of the new token stream so that it
2886 // doesn't get lost.
2887 LM.Toks.push_back(Tok);
2888 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2889
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002890 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00002891 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002892
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002893 assert((Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
2894 Tok.is(tok::colon)) &&
2895 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00002896 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002897 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002898 parseMethod
2899 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
2900 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002901
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002902 // Tell the actions module that we have entered a method or c-function definition
2903 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00002904 if (parseMethod)
2905 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
2906 else
2907 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002908 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00002909 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002910 else {
2911 if (Tok.is(tok::colon))
2912 ParseConstructorInitializer(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00002913 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002914 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00002915
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00002916 if (Tok.getLocation() != OrigLoc) {
2917 // Due to parsing error, we either went over the cached tokens or
2918 // there are still cached tokens left. If it's the latter case skip the
2919 // leftover tokens.
2920 // Since this is an uncommon situation that should be avoided, use the
2921 // expensive isBeforeInTranslationUnit call.
2922 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2923 OrigLoc))
2924 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2925 ConsumeAnyToken();
2926 }
2927
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002928 return;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002929}