blob: de347181bf3913616eb666853eff103c29bbd348 [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"
Douglas Gregor813a0662015-06-19 18:14:38 +000016#include "clang/AST/ASTContext.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000017#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/Scope.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000022#include "llvm/ADT/SmallVector.h"
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000024using namespace clang;
25
Nico Weber04e213b2013-04-03 17:36:11 +000026/// Skips attributes after an Objective-C @ directive. Emits a diagnostic.
Nico Weber69a79142013-04-04 00:15:10 +000027void Parser::MaybeSkipAttributes(tok::ObjCKeywordKind Kind) {
Nico Weber04e213b2013-04-03 17:36:11 +000028 ParsedAttributes attrs(AttrFactory);
29 if (Tok.is(tok::kw___attribute)) {
Nico Weber69a79142013-04-04 00:15:10 +000030 if (Kind == tok::objc_interface || Kind == tok::objc_protocol)
31 Diag(Tok, diag::err_objc_postfix_attribute_hint)
32 << (Kind == tok::objc_protocol);
33 else
34 Diag(Tok, diag::err_objc_postfix_attribute);
Nico Weber04e213b2013-04-03 17:36:11 +000035 ParseGNUAttributes(attrs);
36 }
37}
Chris Lattnerda59c2f2006-11-05 02:08:13 +000038
Chris Lattner3a907162008-12-08 21:53:24 +000039/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000040/// external-declaration: [C99 6.9]
41/// [OBJC] objc-class-definition
Steve Naroffe0933392007-10-29 21:39:29 +000042/// [OBJC] objc-class-declaration
43/// [OBJC] objc-alias-declaration
44/// [OBJC] objc-protocol-definition
45/// [OBJC] objc-method-definition
46/// [OBJC] '@' 'end'
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000047Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000048 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000049
Douglas Gregorf48706c2009-12-07 09:27:33 +000050 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000051 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000052 cutOffParsing();
53 return DeclGroupPtrTy();
Douglas Gregorf48706c2009-12-07 09:27:33 +000054 }
Craig Topper161e4db2014-05-21 06:02:52 +000055
56 Decl *SingleDecl = nullptr;
Steve Naroff7c348172007-08-23 18:16:40 +000057 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000058 case tok::objc_class:
59 return ParseObjCAtClassDeclaration(AtLoc);
John McCall53fa7142010-12-24 02:08:15 +000060 case tok::objc_interface: {
John McCall084e83d2011-03-24 11:26:52 +000061 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000062 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
63 break;
John McCall53fa7142010-12-24 02:08:15 +000064 }
65 case tok::objc_protocol: {
John McCall084e83d2011-03-24 11:26:52 +000066 ParsedAttributes attrs(AttrFactory);
Douglas Gregorf6102672012-01-01 21:23:57 +000067 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall53fa7142010-12-24 02:08:15 +000068 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000069 case tok::objc_implementation:
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +000070 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000071 case tok::objc_end:
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +000072 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000073 case tok::objc_compatibility_alias:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000074 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
75 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000076 case tok::objc_synthesize:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000077 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
78 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000079 case tok::objc_dynamic:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000080 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
81 break;
Douglas Gregorc50d4922012-12-11 22:11:52 +000082 case tok::objc_import:
Sean Callanan87596492014-12-09 23:47:56 +000083 if (getLangOpts().Modules || getLangOpts().DebuggerSupport)
Douglas Gregor0bf886d2012-01-03 18:24:14 +000084 return ParseModuleImport(AtLoc);
Fariborz Jahaniana773d082014-03-26 22:02:43 +000085 Diag(AtLoc, diag::err_atimport);
86 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000087 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerce90ef52008-08-23 02:02:23 +000088 default:
89 Diag(AtLoc, diag::err_unexpected_at);
90 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000091 SingleDecl = nullptr;
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000092 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000093 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000094 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000095}
96
97///
Mike Stump11289f42009-09-09 15:08:12 +000098/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000099/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +0000100///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000101Parser::DeclGroupPtrTy
102Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000103 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000104 SmallVector<IdentifierInfo *, 8> ClassNames;
105 SmallVector<SourceLocation, 8> ClassLocs;
Ted Kremeneka26da852009-11-17 23:12:20 +0000106
Mike Stump11289f42009-09-09 15:08:12 +0000107
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000108 while (1) {
Nico Weber69a79142013-04-04 00:15:10 +0000109 MaybeSkipAttributes(tok::objc_class);
Chris Lattner0ef13522007-10-09 17:51:17 +0000110 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000111 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000112 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000113 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000114 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000115 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +0000116 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000117 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000118
Alp Toker383d2c42014-01-01 03:08:43 +0000119 if (!TryConsumeToken(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000120 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000121 }
Mike Stump11289f42009-09-09 15:08:12 +0000122
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000123 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +0000124 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@class"))
Craig Topper161e4db2014-05-21 06:02:52 +0000125 return Actions.ConvertDeclToDeclGroup(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000126
Ted Kremeneka26da852009-11-17 23:12:20 +0000127 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
128 ClassLocs.data(),
129 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000130}
131
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000132void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
133{
134 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
135 if (ock == Sema::OCK_None)
136 return;
137
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000138 Decl *Decl = Actions.getObjCDeclContext();
139 if (CurParsedObjCImpl) {
140 CurParsedObjCImpl->finish(AtLoc);
141 } else {
142 Actions.ActOnAtEnd(getCurScope(), AtLoc);
143 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000144 Diag(AtLoc, diag::err_objc_missing_end)
145 << FixItHint::CreateInsertion(AtLoc, "@end\n");
146 if (Decl)
147 Diag(Decl->getLocStart(), diag::note_objc_container_start)
148 << (int) ock;
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000149}
150
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000151///
152/// objc-interface:
153/// objc-class-interface-attributes[opt] objc-class-interface
154/// objc-category-interface
155///
156/// objc-class-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000157/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000158/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000159/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000160/// objc-interface-decl-list
161/// @end
162///
163/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000164/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000165/// objc-protocol-refs[opt]
166/// objc-interface-decl-list
167/// @end
168///
169/// objc-superclass:
170/// ':' identifier
171///
172/// objc-class-interface-attributes:
173/// __attribute__((visibility("default")))
174/// __attribute__((visibility("hidden")))
175/// __attribute__((deprecated))
176/// __attribute__((unavailable))
177/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardacfbe9e2012-04-06 18:12:22 +0000178/// __attribute__((objc_root_class))
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000179///
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000180Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000181 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000182 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000183 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000184 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000185 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000186
Douglas Gregor49c22a72009-11-18 16:26:39 +0000187 // Code completion after '@interface'.
188 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000189 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000190 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000191 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000192 }
193
Nico Weber69a79142013-04-04 00:15:10 +0000194 MaybeSkipAttributes(tok::objc_interface);
Nico Weber04e213b2013-04-03 17:36:11 +0000195
Chris Lattner0ef13522007-10-09 17:51:17 +0000196 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000197 Diag(Tok, diag::err_expected)
198 << tok::identifier; // missing class or category name.
Craig Topper161e4db2014-05-21 06:02:52 +0000199 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000200 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000201
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000202 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000203 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000204 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000205 if (Tok.is(tok::l_paren) &&
206 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000207
208 BalancedDelimiterTracker T(*this, tok::l_paren);
209 T.consumeOpen();
210
211 SourceLocation categoryLoc;
Craig Topper161e4db2014-05-21 06:02:52 +0000212 IdentifierInfo *categoryId = nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000213 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000214 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000215 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000216 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000217 }
218
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000219 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000220 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000221 categoryId = Tok.getIdentifierInfo();
222 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000223 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000224 else if (!getLangOpts().ObjC2) {
Alp Tokerec543272013-12-24 09:48:30 +0000225 Diag(Tok, diag::err_expected)
226 << tok::identifier; // missing category name.
Craig Topper161e4db2014-05-21 06:02:52 +0000227 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000228 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000229
230 T.consumeClose();
231 if (T.getCloseLocation().isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000232 return nullptr;
233
Douglas Gregor0c254a02011-09-23 19:19:41 +0000234 if (!attrs.empty()) { // categories don't support attributes.
235 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
236 attrs.clear();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000237 }
Douglas Gregor0c254a02011-09-23 19:19:41 +0000238
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000239 // Next, we need to check for any protocol references.
240 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000241 SmallVector<Decl *, 8> ProtocolRefs;
242 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000243 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000244 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000245 LAngleLoc, EndProtoLoc))
Craig Topper161e4db2014-05-21 06:02:52 +0000246 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000247
John McCall48871652010-08-21 09:40:31 +0000248 Decl *CategoryType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000249 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000250 nameId, nameLoc,
251 categoryId, categoryLoc,
252 ProtocolRefs.data(),
253 ProtocolRefs.size(),
254 ProtocolLocs.data(),
255 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000256
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000257 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000258 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000259
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000260 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000261 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000262 }
263 // Parse a class interface.
Craig Topper161e4db2014-05-21 06:02:52 +0000264 IdentifierInfo *superClassId = nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000265 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000266
Chris Lattner0ef13522007-10-09 17:51:17 +0000267 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000268 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000269
270 // Code completion of superclass names.
271 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000272 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000273 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000274 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000275 }
276
Chris Lattner0ef13522007-10-09 17:51:17 +0000277 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000278 Diag(Tok, diag::err_expected)
279 << tok::identifier; // missing super class name.
Craig Topper161e4db2014-05-21 06:02:52 +0000280 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000281 }
282 superClassId = Tok.getIdentifierInfo();
283 superClassLoc = ConsumeToken();
284 }
285 // Next, we need to check for any protocol references.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000286 SmallVector<Decl *, 8> ProtocolRefs;
287 SmallVector<SourceLocation, 8> ProtocolLocs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000288 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000289 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000290 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000291 LAngleLoc, EndProtoLoc))
Craig Topper161e4db2014-05-21 06:02:52 +0000292 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000293
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000294 if (Tok.isNot(tok::less))
295 Actions.ActOnTypedefedProtocols(ProtocolRefs, superClassId, superClassLoc);
296
John McCall48871652010-08-21 09:40:31 +0000297 Decl *ClsType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000298 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000299 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000300 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000301 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000302 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000303
Chris Lattner0ef13522007-10-09 17:51:17 +0000304 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000305 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000306
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000307 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000308 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000309}
310
Douglas Gregor813a0662015-06-19 18:14:38 +0000311/// Add an attribute for a context-sensitive type nullability to the given
312/// declarator.
313static void addContextSensitiveTypeNullability(Parser &P,
314 Declarator &D,
315 NullabilityKind nullability,
316 SourceLocation nullabilityLoc,
317 bool &addedToDeclSpec) {
318 // Create the attribute.
319 auto getNullabilityAttr = [&]() -> AttributeList * {
Douglas Gregorbec595a2015-06-19 18:27:45 +0000320 return D.getAttributePool().create(
321 P.getNullabilityKeyword(nullability),
322 SourceRange(nullabilityLoc),
323 nullptr, SourceLocation(),
324 nullptr, 0,
325 AttributeList::AS_ContextSensitiveKeyword);
Douglas Gregor813a0662015-06-19 18:14:38 +0000326 };
327
328 if (D.getNumTypeObjects() > 0) {
329 // Add the attribute to the declarator chunk nearest the declarator.
330 auto nullabilityAttr = getNullabilityAttr();
331 DeclaratorChunk &chunk = D.getTypeObject(0);
332 nullabilityAttr->setNext(chunk.getAttrListRef());
333 chunk.getAttrListRef() = nullabilityAttr;
334 } else if (!addedToDeclSpec) {
335 // Otherwise, just put it on the declaration specifiers (if one
336 // isn't there already).
337 D.getMutableDeclSpec().addAttributes(getNullabilityAttr());
338 addedToDeclSpec = true;
339 }
340}
341
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000342/// objc-interface-decl-list:
343/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000344/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000345/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000346/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000347/// objc-interface-decl-list declaration
348/// objc-interface-decl-list ';'
349///
Steve Naroff99264b42007-08-22 16:35:03 +0000350/// objc-method-requirement: [OBJC2]
351/// @required
352/// @optional
353///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000354void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
355 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000356 SmallVector<Decl *, 32> allMethods;
357 SmallVector<Decl *, 16> allProperties;
358 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000359 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000360
Ted Kremenekc7c64312010-01-07 01:20:12 +0000361 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000362
Steve Naroff99264b42007-08-22 16:35:03 +0000363 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000364 // If this is a method prototype, parse it.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000365 if (Tok.isOneOf(tok::minus, tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000366 if (Decl *methodPrototype =
367 ParseObjCMethodPrototype(MethodImplKind, false))
368 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000369 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
370 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000371 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
372 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000373 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000374 if (Tok.is(tok::semi))
375 ConsumeToken();
376 }
Steve Naroff99264b42007-08-22 16:35:03 +0000377 continue;
378 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000379 if (Tok.is(tok::l_paren)) {
380 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000381 ParseObjCMethodDecl(Tok.getLocation(),
382 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000383 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000384 continue;
385 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000386 // Ignore excess semicolons.
387 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000388 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000389 continue;
390 }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Chris Lattnerda9fb152008-10-20 06:10:06 +0000392 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000393 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000394 break;
Mike Stump11289f42009-09-09 15:08:12 +0000395
Douglas Gregorf1934162010-01-13 21:24:21 +0000396 // Code completion within an Objective-C interface.
397 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000398 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000399 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000400 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000401 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000402 }
403
Chris Lattner038a3e32008-10-20 05:46:22 +0000404 // If we don't have an @ directive, parse it as a function definition.
405 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000406 // The code below does not consume '}'s because it is afraid of eating the
407 // end of a namespace. Because of the way this code is structured, an
408 // erroneous r_brace would cause an infinite loop if not handled here.
409 if (Tok.is(tok::r_brace))
410 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000411 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000412 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000413 continue;
414 }
Mike Stump11289f42009-09-09 15:08:12 +0000415
Chris Lattner038a3e32008-10-20 05:46:22 +0000416 // Otherwise, we have an @ directive, eat the @.
417 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000418 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000419 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000420 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000421 }
422
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000423 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000424
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000425 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000426 AtEnd.setBegin(AtLoc);
427 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000428 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000429 } else if (DirectiveKind == tok::objc_not_keyword) {
430 Diag(Tok, diag::err_objc_unknown_at);
431 SkipUntil(tok::semi);
432 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000433 }
Mike Stump11289f42009-09-09 15:08:12 +0000434
Chris Lattnerda9fb152008-10-20 06:10:06 +0000435 // Eat the identifier.
436 ConsumeToken();
437
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000438 switch (DirectiveKind) {
439 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000440 // FIXME: If someone forgets an @end on a protocol, this loop will
441 // continue to eat up tons of stuff and spew lots of nonsense errors. It
442 // would probably be better to bail out if we saw an @class or @interface
443 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000444 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000445 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000446 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000447 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000448
449 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000450 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000451 Diag(AtLoc, diag::err_objc_missing_end)
452 << FixItHint::CreateInsertion(AtLoc, "@end\n");
453 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
454 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000455 ConsumeToken();
456 break;
457
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000458 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000459 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000460 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000461 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000462 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000463 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000464 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000465 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000466 break;
Mike Stump11289f42009-09-09 15:08:12 +0000467
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000468 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000469 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000470 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000471
Chris Lattner038a3e32008-10-20 05:46:22 +0000472 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000473 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000474 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000475 if (Tok.is(tok::l_paren)) {
476 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000477 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000478 }
Mike Stump11289f42009-09-09 15:08:12 +0000479
Douglas Gregor813a0662015-06-19 18:14:38 +0000480 bool addedToDeclSpec = false;
Benjamin Kramera39beb92014-09-03 11:06:10 +0000481 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
482 if (FD.D.getIdentifier() == nullptr) {
483 Diag(AtLoc, diag::err_objc_property_requires_field_name)
484 << FD.D.getSourceRange();
485 return;
486 }
487 if (FD.BitfieldSize) {
488 Diag(AtLoc, diag::err_objc_property_bitfield)
489 << FD.D.getSourceRange();
490 return;
491 }
492
Douglas Gregor813a0662015-06-19 18:14:38 +0000493 // Map a nullability property attribute to a context-sensitive keyword
494 // attribute.
495 if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
496 addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
497 OCDS.getNullabilityLoc(),
498 addedToDeclSpec);
499
Benjamin Kramera39beb92014-09-03 11:06:10 +0000500 // Install the property declarator into interfaceDecl.
501 IdentifierInfo *SelName =
502 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
503
504 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
505 IdentifierInfo *SetterName = OCDS.getSetterName();
506 Selector SetterSel;
507 if (SetterName)
508 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
509 else
510 SetterSel = SelectorTable::constructSetterSelector(
511 PP.getIdentifierTable(), PP.getSelectorTable(),
512 FD.D.getIdentifier());
513 bool isOverridingProperty = false;
514 Decl *Property = Actions.ActOnProperty(
515 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
516 &isOverridingProperty, MethodImplKind);
517 if (!isOverridingProperty)
518 allProperties.push_back(Property);
519
520 FD.complete(Property);
521 };
John McCallcfefb6d2009-11-03 02:38:08 +0000522
Chris Lattner038a3e32008-10-20 05:46:22 +0000523 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000524 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000525 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000526
John McCall405988b2011-03-26 01:53:26 +0000527 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000528 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000529 }
Steve Naroff99264b42007-08-22 16:35:03 +0000530 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000531
532 // We break out of the big loop in two cases: when we see @end or when we see
533 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000534 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000535 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000536 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000537 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000538 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000539 } else {
540 Diag(Tok, diag::err_objc_missing_end)
541 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
542 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
543 << (int) Actions.getObjCContainerKind();
544 AtEnd.setBegin(Tok.getLocation());
545 AtEnd.setEnd(Tok.getLocation());
546 }
Mike Stump11289f42009-09-09 15:08:12 +0000547
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000548 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000549 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000550 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000551}
552
Douglas Gregor813a0662015-06-19 18:14:38 +0000553/// Diagnose redundant or conflicting nullability information.
554static void diagnoseRedundantPropertyNullability(Parser &P,
555 ObjCDeclSpec &DS,
556 NullabilityKind nullability,
557 SourceLocation nullabilityLoc){
558 if (DS.getNullability() == nullability) {
559 P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
560 << static_cast<unsigned>(nullability) << true
561 << SourceRange(DS.getNullabilityLoc());
562 return;
563 }
564
565 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
566 << static_cast<unsigned>(nullability) << true
567 << static_cast<unsigned>(DS.getNullability()) << true
568 << SourceRange(DS.getNullabilityLoc());
569}
570
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000571/// Parse property attribute declarations.
572///
573/// property-attr-decl: '(' property-attrlist ')'
574/// property-attrlist:
575/// property-attribute
576/// property-attrlist ',' property-attribute
577/// property-attribute:
578/// getter '=' identifier
579/// setter '=' identifier ':'
580/// readonly
581/// readwrite
582/// assign
583/// retain
584/// copy
585/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000586/// atomic
587/// strong
588/// weak
589/// unsafe_unretained
Douglas Gregor813a0662015-06-19 18:14:38 +0000590/// nonnull
591/// nullable
592/// null_unspecified
Douglas Gregor849ebc22015-06-19 18:14:46 +0000593/// null_resettable
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000594///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000595void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000596 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000597 BalancedDelimiterTracker T(*this, tok::l_paren);
598 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000599
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000600 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000601 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000602 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000603 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000604 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000605 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000606
Chris Lattner76619232008-10-20 07:22:18 +0000607 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000608 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000609 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000610 return;
611 }
Mike Stump11289f42009-09-09 15:08:12 +0000612
Chris Lattner1db33542008-10-20 07:37:22 +0000613 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000614
Chris Lattner68e48682008-11-20 04:42:34 +0000615 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000616 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000617 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000618 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000619 else if (II->isStr("unsafe_unretained"))
620 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000621 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000622 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000623 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000624 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000625 else if (II->isStr("strong"))
626 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000627 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000628 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000629 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000630 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000631 else if (II->isStr("atomic"))
632 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000633 else if (II->isStr("weak"))
634 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000635 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000636 bool IsSetter = II->getNameStart()[0] == 's';
637
Chris Lattner825bca12008-10-20 07:39:53 +0000638 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000639 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
640 diag::err_objc_expected_equal_for_getter;
641
Alp Toker383d2c42014-01-01 03:08:43 +0000642 if (ExpectAndConsume(tok::equal, DiagID)) {
643 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000644 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000645 }
Mike Stump11289f42009-09-09 15:08:12 +0000646
Douglas Gregorc8537c52009-11-19 07:41:15 +0000647 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000648 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000649 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000650 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000651 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000652 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000653 }
654
Anders Carlssonfe15a782010-10-02 17:45:21 +0000655
656 SourceLocation SelLoc;
657 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
658
659 if (!SelIdent) {
660 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
661 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000662 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000663 return;
664 }
Mike Stump11289f42009-09-09 15:08:12 +0000665
Anders Carlssonfe15a782010-10-02 17:45:21 +0000666 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000667 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000668 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000669
Alp Toker383d2c42014-01-01 03:08:43 +0000670 if (ExpectAndConsume(tok::colon,
671 diag::err_expected_colon_after_setter_name)) {
672 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000673 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000674 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000675 } else {
676 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000677 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000678 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000679 } else if (II->isStr("nonnull")) {
680 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
681 diagnoseRedundantPropertyNullability(*this, DS,
682 NullabilityKind::NonNull,
683 Tok.getLocation());
684 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
685 DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
686 } else if (II->isStr("nullable")) {
687 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
688 diagnoseRedundantPropertyNullability(*this, DS,
689 NullabilityKind::Nullable,
690 Tok.getLocation());
691 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
692 DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
693 } else if (II->isStr("null_unspecified")) {
694 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
695 diagnoseRedundantPropertyNullability(*this, DS,
696 NullabilityKind::Unspecified,
697 Tok.getLocation());
698 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
699 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000700 } else if (II->isStr("null_resettable")) {
701 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
702 diagnoseRedundantPropertyNullability(*this, DS,
703 NullabilityKind::Unspecified,
704 Tok.getLocation());
705 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
706 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
707
708 // Also set the null_resettable bit.
709 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
Chris Lattner825bca12008-10-20 07:39:53 +0000710 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000711 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000712 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000713 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000714 }
Mike Stump11289f42009-09-09 15:08:12 +0000715
Chris Lattner1db33542008-10-20 07:37:22 +0000716 if (Tok.isNot(tok::comma))
717 break;
Mike Stump11289f42009-09-09 15:08:12 +0000718
Chris Lattner1db33542008-10-20 07:37:22 +0000719 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000720 }
Mike Stump11289f42009-09-09 15:08:12 +0000721
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000722 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000723}
724
Steve Naroff09bf8152007-09-06 21:24:23 +0000725/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000726/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000727/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000728///
729/// objc-instance-method: '-'
730/// objc-class-method: '+'
731///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000732/// objc-method-attributes: [OBJC2]
733/// __attribute__((deprecated))
734///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000735Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000736 bool MethodDefinition) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000737 assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000738
Mike Stump11289f42009-09-09 15:08:12 +0000739 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000740 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000741 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000742 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000743 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000744 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000745 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000746}
747
748/// objc-selector:
749/// identifier
750/// one of
751/// enum struct union if else while do for switch case default
752/// break continue return goto asm sizeof typeof __alignof
753/// unsigned long const short volatile signed restrict _Complex
754/// in out inout bycopy byref oneway int char float double void _Bool
755///
Chris Lattner4f472a32009-04-11 18:13:45 +0000756IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000757
Chris Lattner5700fab2007-10-07 02:00:24 +0000758 switch (Tok.getKind()) {
759 default:
Craig Topper161e4db2014-05-21 06:02:52 +0000760 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000761 case tok::ampamp:
762 case tok::ampequal:
763 case tok::amp:
764 case tok::pipe:
765 case tok::tilde:
766 case tok::exclaim:
767 case tok::exclaimequal:
768 case tok::pipepipe:
769 case tok::pipeequal:
770 case tok::caret:
771 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000772 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +0000773 if (isLetter(ThisTok[0])) {
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000774 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
775 Tok.setKind(tok::identifier);
776 SelectorLoc = ConsumeToken();
777 return II;
778 }
Craig Topper161e4db2014-05-21 06:02:52 +0000779 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000780 }
781
Chris Lattner5700fab2007-10-07 02:00:24 +0000782 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000783 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000784 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000785 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000786 case tok::kw_break:
787 case tok::kw_case:
788 case tok::kw_catch:
789 case tok::kw_char:
790 case tok::kw_class:
791 case tok::kw_const:
792 case tok::kw_const_cast:
793 case tok::kw_continue:
794 case tok::kw_default:
795 case tok::kw_delete:
796 case tok::kw_do:
797 case tok::kw_double:
798 case tok::kw_dynamic_cast:
799 case tok::kw_else:
800 case tok::kw_enum:
801 case tok::kw_explicit:
802 case tok::kw_export:
803 case tok::kw_extern:
804 case tok::kw_false:
805 case tok::kw_float:
806 case tok::kw_for:
807 case tok::kw_friend:
808 case tok::kw_goto:
809 case tok::kw_if:
810 case tok::kw_inline:
811 case tok::kw_int:
812 case tok::kw_long:
813 case tok::kw_mutable:
814 case tok::kw_namespace:
815 case tok::kw_new:
816 case tok::kw_operator:
817 case tok::kw_private:
818 case tok::kw_protected:
819 case tok::kw_public:
820 case tok::kw_register:
821 case tok::kw_reinterpret_cast:
822 case tok::kw_restrict:
823 case tok::kw_return:
824 case tok::kw_short:
825 case tok::kw_signed:
826 case tok::kw_sizeof:
827 case tok::kw_static:
828 case tok::kw_static_cast:
829 case tok::kw_struct:
830 case tok::kw_switch:
831 case tok::kw_template:
832 case tok::kw_this:
833 case tok::kw_throw:
834 case tok::kw_true:
835 case tok::kw_try:
836 case tok::kw_typedef:
837 case tok::kw_typeid:
838 case tok::kw_typename:
839 case tok::kw_typeof:
840 case tok::kw_union:
841 case tok::kw_unsigned:
842 case tok::kw_using:
843 case tok::kw_virtual:
844 case tok::kw_void:
845 case tok::kw_volatile:
846 case tok::kw_wchar_t:
847 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000848 case tok::kw__Bool:
849 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000850 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000851 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000852 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000853 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000854 }
Steve Naroff99264b42007-08-22 16:35:03 +0000855}
856
Fariborz Jahanian83615522008-01-02 22:54:34 +0000857/// objc-for-collection-in: 'in'
858///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000859bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000860 // FIXME: May have to do additional look-ahead to only allow for
861 // valid tokens following an 'in'; such as an identifier, unary operators,
862 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000863 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000864 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000865}
866
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000867/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000868/// qualifier list and builds their bitmask representation in the input
869/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000870///
871/// objc-type-qualifiers:
872/// objc-type-qualifier
873/// objc-type-qualifiers objc-type-qualifier
874///
Douglas Gregor813a0662015-06-19 18:14:38 +0000875/// objc-type-qualifier:
876/// 'in'
877/// 'out'
878/// 'inout'
879/// 'oneway'
880/// 'bycopy'
881/// 'byref'
882/// 'nonnull'
883/// 'nullable'
884/// 'null_unspecified'
885///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000886void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000887 Declarator::TheContext Context) {
888 assert(Context == Declarator::ObjCParameterContext ||
889 Context == Declarator::ObjCResultContext);
890
Chris Lattner61511e12007-12-12 06:56:32 +0000891 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000892 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000893 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +0000894 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000895 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +0000896 }
897
Chris Lattner5e530bc2007-12-27 19:57:00 +0000898 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000899 return;
Mike Stump11289f42009-09-09 15:08:12 +0000900
Chris Lattner61511e12007-12-12 06:56:32 +0000901 const IdentifierInfo *II = Tok.getIdentifierInfo();
902 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Douglas Gregor813a0662015-06-19 18:14:38 +0000903 if (II != ObjCTypeQuals[i] ||
904 NextToken().is(tok::less) ||
905 NextToken().is(tok::coloncolon))
Chris Lattner61511e12007-12-12 06:56:32 +0000906 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000907
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000908 ObjCDeclSpec::ObjCDeclQualifier Qual;
Douglas Gregor813a0662015-06-19 18:14:38 +0000909 NullabilityKind Nullability;
Chris Lattner61511e12007-12-12 06:56:32 +0000910 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +0000911 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000912 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
913 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
914 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
915 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
916 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
917 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Douglas Gregor813a0662015-06-19 18:14:38 +0000918
919 case objc_nonnull:
920 Qual = ObjCDeclSpec::DQ_CSNullability;
921 Nullability = NullabilityKind::NonNull;
922 break;
923
924 case objc_nullable:
925 Qual = ObjCDeclSpec::DQ_CSNullability;
926 Nullability = NullabilityKind::Nullable;
927 break;
928
929 case objc_null_unspecified:
930 Qual = ObjCDeclSpec::DQ_CSNullability;
931 Nullability = NullabilityKind::Unspecified;
932 break;
Chris Lattner61511e12007-12-12 06:56:32 +0000933 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000934
935 // FIXME: Diagnose redundant specifiers.
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000936 DS.setObjCDeclQualifier(Qual);
Douglas Gregor813a0662015-06-19 18:14:38 +0000937 if (Qual == ObjCDeclSpec::DQ_CSNullability)
938 DS.setNullability(Tok.getLocation(), Nullability);
939
Chris Lattner61511e12007-12-12 06:56:32 +0000940 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +0000941 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +0000942 break;
943 }
Mike Stump11289f42009-09-09 15:08:12 +0000944
Chris Lattner61511e12007-12-12 06:56:32 +0000945 // If this wasn't a recognized qualifier, bail out.
946 if (II) return;
947 }
948}
949
John McCalla55902b2011-10-01 09:56:14 +0000950/// Take all the decl attributes out of the given list and add
951/// them to the given attribute set.
952static void takeDeclAttributes(ParsedAttributes &attrs,
953 AttributeList *list) {
954 while (list) {
955 AttributeList *cur = list;
956 list = cur->getNext();
957
958 if (!cur->isUsedAsTypeAttr()) {
959 // Clear out the next pointer. We're really completely
960 // destroying the internal invariants of the declarator here,
961 // but it doesn't matter because we're done with it.
Craig Topper161e4db2014-05-21 06:02:52 +0000962 cur->setNext(nullptr);
John McCalla55902b2011-10-01 09:56:14 +0000963 attrs.add(cur);
964 }
965 }
966}
967
968/// takeDeclAttributes - Take all the decl attributes from the given
969/// declarator and add them to the given list.
970static void takeDeclAttributes(ParsedAttributes &attrs,
971 Declarator &D) {
972 // First, take ownership of all attributes.
973 attrs.getPool().takeAllFrom(D.getAttributePool());
974 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
975
976 // Now actually move the attributes over.
977 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
978 takeDeclAttributes(attrs, D.getAttributes());
979 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
980 takeDeclAttributes(attrs,
981 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
982}
983
Chris Lattner61511e12007-12-12 06:56:32 +0000984/// objc-type-name:
985/// '(' objc-type-qualifiers[opt] type-name ')'
986/// '(' objc-type-qualifiers[opt] ')'
987///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000988ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000989 Declarator::TheContext context,
990 ParsedAttributes *paramAttrs) {
991 assert(context == Declarator::ObjCParameterContext ||
992 context == Declarator::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +0000993 assert((paramAttrs != nullptr) ==
994 (context == Declarator::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +0000995
Chris Lattner0ef13522007-10-09 17:51:17 +0000996 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000997
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000998 BalancedDelimiterTracker T(*this, tok::l_paren);
999 T.consumeOpen();
1000
Chris Lattner2ebb1782008-08-23 01:48:03 +00001001 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001002 ObjCDeclContextSwitch ObjCDC(*this);
1003
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001004 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001005 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +00001006
John McCallba7bf592010-08-24 05:47:05 +00001007 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +00001008 if (isTypeSpecifierQualifier()) {
John McCalla55902b2011-10-01 09:56:14 +00001009 // Parse an abstract declarator.
1010 DeclSpec declSpec(AttrFactory);
1011 declSpec.setObjCQualifiers(&DS);
1012 ParseSpecifierQualifierList(declSpec);
Fariborz Jahanianb6499eb2012-05-29 21:52:45 +00001013 declSpec.SetRangeEnd(Tok.getLocation());
John McCalla55902b2011-10-01 09:56:14 +00001014 Declarator declarator(declSpec, context);
1015 ParseDeclarator(declarator);
1016
1017 // If that's not invalid, extract a type.
1018 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001019 // Map a nullability specifier to a context-sensitive keyword attribute.
1020 bool addedToDeclSpec = false;
1021 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1022 addContextSensitiveTypeNullability(*this, declarator,
1023 DS.getNullability(),
1024 DS.getNullabilityLoc(),
1025 addedToDeclSpec);
1026
John McCalla55902b2011-10-01 09:56:14 +00001027 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1028 if (!type.isInvalid())
1029 Ty = type.get();
1030
1031 // If we're parsing a parameter, steal all the decl attributes
1032 // and add them to the decl spec.
1033 if (context == Declarator::ObjCParameterContext)
1034 takeDeclAttributes(*paramAttrs, declarator);
1035 }
1036 } else if (context == Declarator::ObjCResultContext &&
1037 Tok.is(tok::identifier)) {
Douglas Gregorbab8a962011-09-08 01:46:34 +00001038 if (!Ident_instancetype)
1039 Ident_instancetype = PP.getIdentifierInfo("instancetype");
1040
1041 if (Tok.getIdentifierInfo() == Ident_instancetype) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001042 SourceLocation loc = ConsumeToken();
1043 Ty = Actions.ActOnObjCInstanceType(loc);
1044
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001045 // Synthesize an abstract declarator so we can use Sema::ActOnTypeName.
1046 bool addedToDeclSpec = false;
1047 const char *prevSpec;
1048 unsigned diagID;
1049 DeclSpec declSpec(AttrFactory);
1050 declSpec.setObjCQualifiers(&DS);
1051 declSpec.SetTypeSpecType(DeclSpec::TST_typename, loc, prevSpec, diagID,
1052 Ty,
1053 Actions.getASTContext().getPrintingPolicy());
1054 declSpec.SetRangeEnd(loc);
1055 Declarator declarator(declSpec, context);
Douglas Gregor813a0662015-06-19 18:14:38 +00001056
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001057 // Map a nullability specifier to a context-sensitive keyword attribute.
1058 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
Douglas Gregor813a0662015-06-19 18:14:38 +00001059 addContextSensitiveTypeNullability(*this, declarator,
1060 DS.getNullability(),
1061 DS.getNullabilityLoc(),
1062 addedToDeclSpec);
1063
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001064 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1065 if (!type.isInvalid())
1066 Ty = type.get();
Douglas Gregorbab8a962011-09-08 01:46:34 +00001067 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001068 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001069
Steve Naroff90255b42008-10-21 14:15:04 +00001070 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001071 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001072 else if (Tok.getLocation() == TypeStartLoc) {
1073 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001074 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001075 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001076 } else {
1077 // Otherwise, we found *something*, but didn't get a ')' in the right
1078 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001079 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001080 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001081 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001082}
1083
1084/// objc-method-decl:
1085/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001086/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001087/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001088/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001089///
1090/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001091/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001092/// objc-keyword-selector objc-keyword-decl
1093///
1094/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001095/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1096/// objc-selector ':' objc-keyword-attributes[opt] identifier
1097/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1098/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001099///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001100/// objc-parmlist:
1101/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001102///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001103/// objc-parms:
1104/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001105///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001106/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001107/// , ...
1108///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001109/// objc-keyword-attributes: [OBJC2]
1110/// __attribute__((unused))
1111///
John McCall48871652010-08-21 09:40:31 +00001112Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001113 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001114 tok::ObjCKeywordKind MethodImplKind,
1115 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001116 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001117
Douglas Gregor636a61e2010-04-07 00:21:17 +00001118 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001119 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001120 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001121 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001122 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001123 }
1124
Chris Lattner2ebb1782008-08-23 01:48:03 +00001125 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001126 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001127 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001128 if (Tok.is(tok::l_paren))
Craig Topper161e4db2014-05-21 06:02:52 +00001129 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext,
1130 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001131
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001132 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001133 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001134 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001135 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001136
Douglas Gregor636a61e2010-04-07 00:21:17 +00001137 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001138 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001139 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001140 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001141 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001142 }
1143
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001144 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001145 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001146 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001147
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001148 // An unnamed colon is valid.
1149 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001150 Diag(Tok, diag::err_expected_selector_for_method)
1151 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001152 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001153 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001154 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001155 }
Mike Stump11289f42009-09-09 15:08:12 +00001156
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001157 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001158 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001159 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001160 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001161 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001162
Chris Lattner5700fab2007-10-07 02:00:24 +00001163 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001164 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001165 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001166 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001167 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001168 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001169 methodAttrs.getList(), MethodImplKind,
1170 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001171 PD.complete(Result);
1172 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001173 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001174
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001175 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001176 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001177 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001178 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1179 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001180
1181 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001182 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001183 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001184 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001185
Chris Lattner5700fab2007-10-07 02:00:24 +00001186 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001187 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001188 break;
Mike Stump11289f42009-09-09 15:08:12 +00001189
John McCallba7bf592010-08-24 05:47:05 +00001190 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001191 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001192 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1193 Declarator::ObjCParameterContext,
1194 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001195
Chris Lattner5700fab2007-10-07 02:00:24 +00001196 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001197 // Regardless, collect all the attributes we've parsed so far.
Craig Topper161e4db2014-05-21 06:02:52 +00001198 ArgInfo.ArgAttrs = nullptr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001199 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001200 MaybeParseGNUAttributes(paramAttrs);
1201 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001202 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001203
Douglas Gregor45879692010-07-08 23:37:41 +00001204 // Code completion for the next piece of the selector.
1205 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001206 KeyIdents.push_back(SelIdent);
1207 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1208 mType == tok::minus,
1209 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001210 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001211 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001212 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001213 }
1214
Chris Lattner0ef13522007-10-09 17:51:17 +00001215 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001216 Diag(Tok, diag::err_expected)
1217 << tok::identifier; // missing argument name.
Chris Lattner5700fab2007-10-07 02:00:24 +00001218 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001219 }
Mike Stump11289f42009-09-09 15:08:12 +00001220
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001221 ArgInfo.Name = Tok.getIdentifierInfo();
1222 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001223 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001224
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001225 ArgInfos.push_back(ArgInfo);
1226 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001227 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001228
John McCall084e83d2011-03-24 11:26:52 +00001229 // Make sure the attributes persist.
1230 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1231
Douglas Gregor95887f92010-07-08 23:20:03 +00001232 // Code completion for the next piece of the selector.
1233 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001234 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1235 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001236 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001237 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001238 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001239 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001240 }
1241
Chris Lattner5700fab2007-10-07 02:00:24 +00001242 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001243 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001244 if (!SelIdent && Tok.isNot(tok::colon))
1245 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001246 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001247 SourceLocation ColonLoc = Tok.getLocation();
1248 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001249 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1250 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1251 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001252 }
1253 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001254 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001255 }
Mike Stump11289f42009-09-09 15:08:12 +00001256
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001257 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001258 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001259 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001260 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001261 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001262 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001263 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001264 ConsumeToken();
1265 break;
1266 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001267 if (!cStyleParamWarned) {
1268 Diag(Tok, diag::warn_cstyle_param);
1269 cStyleParamWarned = true;
1270 }
John McCall084e83d2011-03-24 11:26:52 +00001271 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001272 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001273 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001274 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1275 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001276 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001277 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001278 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1279 ParmDecl.getIdentifierLoc(),
1280 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001281 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001282 }
Mike Stump11289f42009-09-09 15:08:12 +00001283
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001284 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001285 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001286 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001287 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001288
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001289 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001290 return nullptr;
1291
Chris Lattner5700fab2007-10-07 02:00:24 +00001292 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1293 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001294 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001295 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001296 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001297 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001298 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001299 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001300 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001301
John McCall28a6aea2009-11-04 02:18:39 +00001302 PD.complete(Result);
1303 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001304}
1305
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001306/// objc-protocol-refs:
1307/// '<' identifier-list '>'
1308///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001309bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001310ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1311 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001312 bool WarnOnDeclarations, bool ForObjCContainer,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001313 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001314 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001315
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001316 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001317
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001318 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001319
Chris Lattner3bbae002008-07-26 04:03:38 +00001320 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001321 if (Tok.is(tok::code_completion)) {
1322 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1323 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001324 cutOffParsing();
1325 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001326 }
1327
Chris Lattner3bbae002008-07-26 04:03:38 +00001328 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001329 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001330 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001331 return true;
1332 }
1333 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1334 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001335 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001336 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001337
Alp Toker383d2c42014-01-01 03:08:43 +00001338 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001339 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001340 }
Mike Stump11289f42009-09-09 15:08:12 +00001341
Chris Lattner3bbae002008-07-26 04:03:38 +00001342 // Consume the '>'.
Nico Weber7aa4a882012-12-14 18:22:38 +00001343 if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true))
Chris Lattner3bbae002008-07-26 04:03:38 +00001344 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001345
Chris Lattner3bbae002008-07-26 04:03:38 +00001346 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001347 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Chris Lattner3bbae002008-07-26 04:03:38 +00001348 &ProtocolIdents[0], ProtocolIdents.size(),
1349 Protocols);
1350 return false;
1351}
1352
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001353/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1354/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001355bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001356 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001357 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001358 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001359 SmallVector<Decl *, 8> ProtocolDecl;
1360 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001361 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001362 false,
Douglas Gregor3a001f42010-11-19 17:10:50 +00001363 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001364 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1365 ProtocolLocs.data(), LAngleLoc);
1366 if (EndProtoLoc.isValid())
1367 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001368 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001369}
1370
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001371void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1372 BalancedDelimiterTracker &T,
1373 SmallVectorImpl<Decl *> &AllIvarDecls,
1374 bool RBraceMissing) {
1375 if (!RBraceMissing)
1376 T.consumeClose();
1377
1378 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1379 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1380 Actions.ActOnObjCContainerFinishDefinition();
1381 // Call ActOnFields() even if we don't have any decls. This is useful
1382 // for code rewriting tools that need to be aware of the empty list.
1383 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1384 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001385 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001386}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001387
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001388/// objc-class-instance-variables:
1389/// '{' objc-instance-variable-decl-list[opt] '}'
1390///
1391/// objc-instance-variable-decl-list:
1392/// objc-visibility-spec
1393/// objc-instance-variable-decl ';'
1394/// ';'
1395/// objc-instance-variable-decl-list objc-visibility-spec
1396/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1397/// objc-instance-variable-decl-list ';'
1398///
1399/// objc-visibility-spec:
1400/// @private
1401/// @protected
1402/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001403/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001404///
1405/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001406/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001407///
John McCall48871652010-08-21 09:40:31 +00001408void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001409 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001410 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001411 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001412 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001413
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001414 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001415 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001416
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001417 BalancedDelimiterTracker T(*this, tok::l_brace);
1418 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001419 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001420 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001421 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001422
Steve Naroff00433d32007-08-21 21:17:12 +00001423 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001424 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001425 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001426 continue;
1427 }
Mike Stump11289f42009-09-09 15:08:12 +00001428
Steve Naroff00433d32007-08-21 21:17:12 +00001429 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001430 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001431 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001432 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001433 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001434 }
1435
Steve Naroff7c348172007-08-23 18:16:40 +00001436 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001437 case tok::objc_private:
1438 case tok::objc_public:
1439 case tok::objc_protected:
1440 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001441 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001442 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001443 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001444
1445 case tok::objc_end:
1446 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001447 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1448 Tok.setKind(tok::at);
1449 Tok.setLength(1);
1450 PP.EnterToken(Tok);
1451 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1452 T, AllIvarDecls, true);
1453 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001454
1455 default:
1456 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1457 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001458 }
1459 }
Mike Stump11289f42009-09-09 15:08:12 +00001460
Douglas Gregor48d46252010-01-13 21:54:15 +00001461 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001462 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001463 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001464 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001465 }
John McCallcfefb6d2009-11-03 02:38:08 +00001466
Benjamin Kramera39beb92014-09-03 11:06:10 +00001467 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1468 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1469 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001470 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001471 Decl *Field = Actions.ActOnIvar(
1472 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1473 FD.BitfieldSize, visibility);
1474 Actions.ActOnObjCContainerFinishDefinition();
1475 if (Field)
1476 AllIvarDecls.push_back(Field);
1477 FD.complete(Field);
1478 };
John McCallcfefb6d2009-11-03 02:38:08 +00001479
Chris Lattnera12405b2008-04-10 06:46:29 +00001480 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001481 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001482 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001483
Chris Lattner0ef13522007-10-09 17:51:17 +00001484 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001485 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001486 } else {
1487 Diag(Tok, diag::err_expected_semi_decl_list);
1488 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001489 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001490 }
1491 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001492 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1493 T, AllIvarDecls, false);
Steve Naroff00433d32007-08-21 21:17:12 +00001494 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001495}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001496
1497/// objc-protocol-declaration:
1498/// objc-protocol-definition
1499/// objc-protocol-forward-reference
1500///
1501/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00001502/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00001503/// objc-protocol-refs[opt]
1504/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00001505/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001506///
1507/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00001508/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001509///
James Dennett1355bd12012-06-11 06:19:40 +00001510/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001511/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001512/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001513Parser::DeclGroupPtrTy
1514Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1515 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001516 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001517 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1518 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001519
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001520 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001521 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001522 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00001523 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001524 }
1525
Nico Weber69a79142013-04-04 00:15:10 +00001526 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00001527
Chris Lattner0ef13522007-10-09 17:51:17 +00001528 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001529 Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00001530 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001531 }
1532 // Save the protocol name, then consume it.
1533 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1534 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001535
Alp Toker383d2c42014-01-01 03:08:43 +00001536 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001537 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001538 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001539 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001540 }
Mike Stump11289f42009-09-09 15:08:12 +00001541
Erik Verbruggenf9887852011-12-08 09:58:43 +00001542 CheckNestedObjCContexts(AtLoc);
1543
Chris Lattner0ef13522007-10-09 17:51:17 +00001544 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001545 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001546 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1547
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001548 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001549 while (1) {
1550 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001551 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001552 Diag(Tok, diag::err_expected) << tok::identifier;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001553 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00001554 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001555 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001556 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1557 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001558 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001559
Chris Lattner0ef13522007-10-09 17:51:17 +00001560 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001561 break;
1562 }
1563 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00001564 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00001565 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001566
Steve Naroff93eb5f12007-10-10 17:32:04 +00001567 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001568 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001569 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001570 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001571 }
Mike Stump11289f42009-09-09 15:08:12 +00001572
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001573 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001574 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001575
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001576 SmallVector<Decl *, 8> ProtocolRefs;
1577 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001578 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001579 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001580 LAngleLoc, EndProtoLoc))
Douglas Gregorf6102672012-01-01 21:23:57 +00001581 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001582
John McCall48871652010-08-21 09:40:31 +00001583 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001584 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001585 ProtocolRefs.data(),
1586 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001587 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001588 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001589
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001590 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00001591 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001592}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001593
1594/// objc-implementation:
1595/// objc-class-implementation-prologue
1596/// objc-category-implementation-prologue
1597///
1598/// objc-class-implementation-prologue:
1599/// @implementation identifier objc-superclass[opt]
1600/// objc-class-instance-variables[opt]
1601///
1602/// objc-category-implementation-prologue:
1603/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001604Parser::DeclGroupPtrTy
1605Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001606 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1607 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001608 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001609 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001610
Douglas Gregor49c22a72009-11-18 16:26:39 +00001611 // Code completion after '@implementation'.
1612 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001613 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001614 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001615 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001616 }
1617
Nico Weber69a79142013-04-04 00:15:10 +00001618 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00001619
Chris Lattner0ef13522007-10-09 17:51:17 +00001620 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001621 Diag(Tok, diag::err_expected)
1622 << tok::identifier; // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001623 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001624 }
1625 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001626 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001627 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00001628 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001629
1630 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001631 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001632 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001633 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001634 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001635
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001636 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001637 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001638 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001639 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001640 }
1641
Chris Lattner0ef13522007-10-09 17:51:17 +00001642 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001643 categoryId = Tok.getIdentifierInfo();
1644 categoryLoc = ConsumeToken();
1645 } else {
Alp Tokerec543272013-12-24 09:48:30 +00001646 Diag(Tok, diag::err_expected)
1647 << tok::identifier; // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001648 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001649 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001650 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001651 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001652 SkipUntil(tok::r_paren); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001653 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001654 }
1655 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00001656 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1657 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1658 AttributeFactory attr;
1659 DeclSpec DS(attr);
1660 (void)ParseObjCProtocolQualifiers(DS);
1661 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001662 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001663 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001664 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001665
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001666 } else {
1667 // We have a class implementation
1668 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001669 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00001670 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001671 // We have a super class
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001672 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001673 Diag(Tok, diag::err_expected)
1674 << tok::identifier; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001675 return DeclGroupPtrTy();
1676 }
1677 superClassId = Tok.getIdentifierInfo();
1678 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001679 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001680 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1681 AtLoc, nameId, nameLoc,
1682 superClassId, superClassLoc);
1683
1684 if (Tok.is(tok::l_brace)) // we have ivars
1685 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00001686 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1687 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1688 // try to recover.
1689 AttributeFactory attr;
1690 DeclSpec DS(attr);
1691 (void)ParseObjCProtocolQualifiers(DS);
1692 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001693 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001694 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001695
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001696 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001697
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001698 {
1699 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00001700 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001701 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001702 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001703 MaybeParseMicrosoftAttributes(attrs);
1704 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1705 DeclGroupRef DG = DGP.get();
1706 DeclsInGroup.append(DG.begin(), DG.end());
1707 }
1708 }
1709 }
1710
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001711 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001712}
Steve Naroff33a1e802007-10-29 21:38:07 +00001713
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001714Parser::DeclGroupPtrTy
1715Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001716 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1717 "ParseObjCAtEndDeclaration(): Expected @end");
1718 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001719 if (CurParsedObjCImpl)
1720 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001721 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00001722 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001723 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001724 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001725}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001726
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001727Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1728 if (!Finished) {
1729 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00001730 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001731 P.Diag(P.Tok, diag::err_objc_missing_end)
1732 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1733 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1734 << Sema::OCK_Implementation;
1735 }
1736 }
Craig Topper161e4db2014-05-21 06:02:52 +00001737 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001738 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001739}
1740
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001741void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1742 assert(!Finished);
1743 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1744 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001745 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1746 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001747
1748 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1749
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001750 if (HasCFunction)
1751 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1752 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1753 false/*c-functions*/);
1754
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001755 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001756 for (LateParsedObjCMethodContainer::iterator
1757 I = LateParsedObjCMethods.begin(),
1758 E = LateParsedObjCMethods.end(); I != E; ++I)
1759 delete *I;
1760 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001761
1762 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001763}
1764
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001765/// compatibility-alias-decl:
1766/// @compatibility_alias alias-name class-name ';'
1767///
John McCall48871652010-08-21 09:40:31 +00001768Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001769 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1770 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1771 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001772 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001773 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00001774 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001775 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001776 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1777 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001778 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001779 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00001780 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001781 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001782 IdentifierInfo *classId = Tok.getIdentifierInfo();
1783 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00001784 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00001785 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1786 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001787}
1788
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001789/// property-synthesis:
1790/// @synthesize property-ivar-list ';'
1791///
1792/// property-ivar-list:
1793/// property-ivar
1794/// property-ivar-list ',' property-ivar
1795///
1796/// property-ivar:
1797/// identifier
1798/// identifier '=' identifier
1799///
John McCall48871652010-08-21 09:40:31 +00001800Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001801 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00001802 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001803 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001804
Douglas Gregor88e72a02009-11-18 19:45:45 +00001805 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001806 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001807 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001808 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001809 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00001810 }
1811
Douglas Gregor88e72a02009-11-18 19:45:45 +00001812 if (Tok.isNot(tok::identifier)) {
1813 Diag(Tok, diag::err_synthesized_property_name);
1814 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00001815 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001816 }
Craig Topper161e4db2014-05-21 06:02:52 +00001817
1818 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001819 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1820 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001821 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00001822 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001823 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00001824 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001825 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001826 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001827 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00001828 }
1829
Chris Lattner0ef13522007-10-09 17:51:17 +00001830 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001831 Diag(Tok, diag::err_expected) << tok::identifier;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001832 break;
1833 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001834 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001835 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001836 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001837 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001838 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001839 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001840 break;
1841 ConsumeToken(); // consume ','
1842 }
Alp Toker383d2c42014-01-01 03:08:43 +00001843 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00001844 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001845}
1846
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001847/// property-dynamic:
1848/// @dynamic property-list
1849///
1850/// property-list:
1851/// identifier
1852/// property-list ',' identifier
1853///
John McCall48871652010-08-21 09:40:31 +00001854Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001855 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1856 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001857 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001858 while (true) {
1859 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001860 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001861 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001862 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001863 }
1864
1865 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001866 Diag(Tok, diag::err_expected) << tok::identifier;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001867 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00001868 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001869 }
1870
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001871 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1872 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001873 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Craig Topper161e4db2014-05-21 06:02:52 +00001874 propertyId, nullptr, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001875
Chris Lattner0ef13522007-10-09 17:51:17 +00001876 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001877 break;
1878 ConsumeToken(); // consume ','
1879 }
Alp Toker383d2c42014-01-01 03:08:43 +00001880 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00001881 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001882}
Mike Stump11289f42009-09-09 15:08:12 +00001883
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001884/// objc-throw-statement:
1885/// throw expression[opt];
1886///
John McCalldadc5752010-08-24 06:29:42 +00001887StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1888 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001889 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001890 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001891 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001892 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001893 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001894 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001895 }
1896 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001897 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00001898 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001899 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001900}
1901
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001902/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001903/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001904///
John McCalldadc5752010-08-24 06:29:42 +00001905StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001906Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001907 ConsumeToken(); // consume synchronized
1908 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001909 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001910 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001911 }
John McCalld9bb7432011-07-27 21:50:02 +00001912
1913 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001914 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00001915 ExprResult operand(ParseExpression());
1916
1917 if (Tok.is(tok::r_paren)) {
1918 ConsumeParen(); // ')'
1919 } else {
1920 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00001921 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00001922
1923 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001924 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001925 }
John McCalld9bb7432011-07-27 21:50:02 +00001926
1927 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001928 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00001929 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00001930 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001931 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001932 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001933
John McCalld9bb7432011-07-27 21:50:02 +00001934 // Check the @synchronized operand now.
1935 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001936 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001937
John McCalld9bb7432011-07-27 21:50:02 +00001938 // Parse the compound statement within a new scope.
1939 ParseScope bodyScope(this, Scope::DeclScope);
1940 StmtResult body(ParseCompoundStatementBody());
1941 bodyScope.Exit();
1942
1943 // If there was a semantic or parse error earlier with the
1944 // operand, fail now.
1945 if (operand.isInvalid())
1946 return StmtError();
1947
1948 if (body.isInvalid())
1949 body = Actions.ActOnNullStmt(Tok.getLocation());
1950
1951 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001952}
1953
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001954/// objc-try-catch-statement:
1955/// @try compound-statement objc-catch-list[opt]
1956/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1957///
1958/// objc-catch-list:
1959/// @catch ( parameter-declaration ) compound-statement
1960/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1961/// catch-parameter-declaration:
1962/// parameter-declaration
1963/// '...' [OBJC2]
1964///
John McCalldadc5752010-08-24 06:29:42 +00001965StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001966 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001967
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001968 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001969 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00001970 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001971 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001972 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00001973 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00001974 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001975 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001976 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001977 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001978 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001979 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001980
Chris Lattner0ef13522007-10-09 17:51:17 +00001981 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001982 // At this point, we need to lookahead to determine if this @ is the start
1983 // of an @catch or @finally. We don't want to consume the @ token if this
1984 // is an @try or @encode or something else.
1985 Token AfterAt = GetLookAheadToken(1);
1986 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1987 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1988 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001989
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001990 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001991 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00001992 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001993 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001994 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001995 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001996 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001997 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001998 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001999 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00002000 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002001 ParseDeclarator(ParmDecl);
2002
Douglas Gregore11ee112010-04-23 23:01:43 +00002003 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002004 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002005 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002006 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002007 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002008
Steve Naroff65a00892009-04-07 22:56:58 +00002009 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002010
Steve Naroff65a00892009-04-07 22:56:58 +00002011 if (Tok.is(tok::r_paren))
2012 RParenLoc = ConsumeParen();
2013 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002014 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002015
John McCalldadc5752010-08-24 06:29:42 +00002016 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002017 if (Tok.is(tok::l_brace))
2018 CatchBody = ParseCompoundStatementBody();
2019 else
Alp Tokerec543272013-12-24 09:48:30 +00002020 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002021 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002022 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00002023
John McCalldadc5752010-08-24 06:29:42 +00002024 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00002025 RParenLoc,
2026 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002027 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002028 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002029 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002030
Steve Naroffe6016792008-02-05 21:27:35 +00002031 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002032 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2033 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002034 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002035 }
2036 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002037 } else {
2038 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002039 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002040 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002041
John McCalldadc5752010-08-24 06:29:42 +00002042 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002043 if (Tok.is(tok::l_brace))
2044 FinallyBody = ParseCompoundStatementBody();
2045 else
Alp Tokerec543272013-12-24 09:48:30 +00002046 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002047 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002048 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002049 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002050 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002051 catch_or_finally_seen = true;
2052 break;
2053 }
2054 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002055 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002056 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002057 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002058 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002059
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002060 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002061 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002062 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002063}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002064
John McCall31168b02011-06-15 23:02:42 +00002065/// objc-autoreleasepool-statement:
2066/// @autoreleasepool compound-statement
2067///
2068StmtResult
2069Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2070 ConsumeToken(); // consume autoreleasepool
2071 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002072 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002073 return StmtError();
2074 }
2075 // Enter a scope to hold everything within the compound stmt. Compound
2076 // statements can always hold declarations.
2077 ParseScope BodyScope(this, Scope::DeclScope);
2078
2079 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2080
2081 BodyScope.Exit();
2082 if (AutoreleasePoolBody.isInvalid())
2083 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2084 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002085 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002086}
2087
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002088/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2089/// for later parsing.
2090void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
2091 LexedMethod* LM = new LexedMethod(this, MDecl);
2092 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2093 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002094 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002095 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002096 if (Tok.is(tok::kw_try)) {
2097 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002098 if (Tok.is(tok::colon)) {
2099 Toks.push_back(Tok);
2100 ConsumeToken();
2101 while (Tok.isNot(tok::l_brace)) {
2102 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2103 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2104 }
2105 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002106 Toks.push_back(Tok); // also store '{'
2107 }
2108 else if (Tok.is(tok::colon)) {
2109 ConsumeToken();
2110 while (Tok.isNot(tok::l_brace)) {
2111 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2112 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2113 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002114 Toks.push_back(Tok); // also store '{'
2115 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002116 ConsumeBrace();
2117 // Consume everything up to (and including) the matching right brace.
2118 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002119 while (Tok.is(tok::kw_catch)) {
2120 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2121 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2122 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002123}
2124
Steve Naroff09bf8152007-09-06 21:24:23 +00002125/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002126///
John McCall48871652010-08-21 09:40:31 +00002127Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002128 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002129
John McCallfaf5fb42010-08-26 23:41:50 +00002130 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2131 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002132
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002133 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002134 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002135 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002136 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002137 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002138 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002139 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002140 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002141
Steve Naroffbb875722007-11-11 19:54:21 +00002142 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002143 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002144 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002145
Steve Naroffbb875722007-11-11 19:54:21 +00002146 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002147 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002148
Steve Naroffbb875722007-11-11 19:54:21 +00002149 // If we didn't find the '{', bail out.
2150 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002151 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002152 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002153
2154 if (!MDecl) {
2155 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002156 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002157 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002158 }
2159
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002160 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002161 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002162 assert (CurParsedObjCImpl
2163 && "ParseObjCMethodDefinition - Method out of @implementation");
2164 // Consume the tokens and store them for later parsing.
2165 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002166 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002167}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002168
John McCalldadc5752010-08-24 06:29:42 +00002169StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002170 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002171 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002172 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002173 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002174 }
2175
2176 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002177 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002178
2179 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002180 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002181
2182 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002183 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002184
2185 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2186 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002187
2188 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2189 getLangOpts().DebuggerSupport) {
2190 SkipUntil(tok::semi);
2191 return Actions.ActOnNullStmt(Tok.getLocation());
2192 }
2193
John McCalldadc5752010-08-24 06:29:42 +00002194 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002195 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002196 // If the expression is invalid, skip ahead to the next semicolon. Not
2197 // doing this opens us up to the possibility of infinite loops if
2198 // ParseExpression does not consume any tokens.
2199 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002200 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002201 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002202
Steve Naroffe6016792008-02-05 21:27:35 +00002203 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002204 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002205 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002206}
2207
John McCalldadc5752010-08-24 06:29:42 +00002208ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002209 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002210 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002211 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002212 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002213 return ExprError();
2214
Ted Kremeneke65b0862012-03-06 20:05:56 +00002215 case tok::minus:
2216 case tok::plus: {
2217 tok::TokenKind Kind = Tok.getKind();
2218 SourceLocation OpLoc = ConsumeToken();
2219
2220 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002221 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002222 switch (Kind) {
2223 case tok::minus: Symbol = "-"; break;
2224 case tok::plus: Symbol = "+"; break;
2225 default: llvm_unreachable("missing unary operator case");
2226 }
2227 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2228 << Symbol;
2229 return ExprError();
2230 }
2231
2232 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2233 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002234 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002235 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002236 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002237
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002238 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002239 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002240 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002241
2242 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002243 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002244 }
2245
Chris Lattnere002fbe2007-12-12 01:04:12 +00002246 case tok::string_literal: // primary-expression: string-literal
2247 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002248 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002249
2250 case tok::char_constant:
2251 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2252
2253 case tok::numeric_constant:
2254 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2255
2256 case tok::kw_true: // Objective-C++, etc.
2257 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2258 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2259 case tok::kw_false: // Objective-C++, etc.
2260 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2261 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2262
2263 case tok::l_square:
2264 // Objective-C array literal
2265 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2266
2267 case tok::l_brace:
2268 // Objective-C dictionary literal
2269 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2270
Patrick Beard0caa3942012-04-19 00:25:12 +00002271 case tok::l_paren:
2272 // Objective-C boxed expression
2273 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2274
Chris Lattnere002fbe2007-12-12 01:04:12 +00002275 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002276 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002277 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002278
Chris Lattner197a3012008-08-05 06:19:09 +00002279 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2280 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002281 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002282 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002283 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002284 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002285 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002286 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002287 const char *str = nullptr;
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002288 if (GetLookAheadToken(1).is(tok::l_brace)) {
2289 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2290 str =
2291 ch == 't' ? "try"
2292 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002293 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002294 }
2295 if (str) {
2296 SourceLocation kwLoc = Tok.getLocation();
2297 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2298 FixItHint::CreateReplacement(kwLoc, str));
2299 }
2300 else
2301 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2302 }
Chris Lattner197a3012008-08-05 06:19:09 +00002303 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002304 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002305}
2306
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002307/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002308///
2309/// This routine parses the receiver of a message send in
2310/// Objective-C++ either as a type or as an expression. Note that this
2311/// routine must not be called to parse a send to 'super', since it
2312/// has no way to return such a result.
2313///
2314/// \param IsExpr Whether the receiver was parsed as an expression.
2315///
2316/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2317/// IsExpr is true), the parsed expression. If the receiver was parsed
2318/// as a type (\c IsExpr is false), the parsed type.
2319///
2320/// \returns True if an error occurred during parsing or semantic
2321/// analysis, in which case the arguments do not have valid
2322/// values. Otherwise, returns false for a successful parse.
2323///
2324/// objc-receiver: [C++]
2325/// 'super' [not parsed here]
2326/// expression
2327/// simple-type-specifier
2328/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002329bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002330 InMessageExpressionRAIIObject InMessage(*this, true);
2331
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002332 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2333 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002334 TryAnnotateTypeOrScopeToken();
2335
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002336 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002337 // objc-receiver:
2338 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002339 // Make sure any typos in the receiver are corrected or diagnosed, so that
2340 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2341 // only the things that are valid ObjC receivers?
2342 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002343 if (Receiver.isInvalid())
2344 return true;
2345
2346 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002347 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002348 return false;
2349 }
2350
2351 // objc-receiver:
2352 // typename-specifier
2353 // simple-type-specifier
2354 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002355 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002356 ParseCXXSimpleTypeSpecifier(DS);
2357
2358 if (Tok.is(tok::l_paren)) {
2359 // If we see an opening parentheses at this point, we are
2360 // actually parsing an expression that starts with a
2361 // function-style cast, e.g.,
2362 //
2363 // postfix-expression:
2364 // simple-type-specifier ( expression-list [opt] )
2365 // typename-specifier ( expression-list [opt] )
2366 //
2367 // Parse the remainder of this case, then the (optional)
2368 // postfix-expression suffix, followed by the (optional)
2369 // right-hand side of the binary expression. We have an
2370 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002371 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002372 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002373 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002374 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002375 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002376 if (Receiver.isInvalid())
2377 return true;
2378
2379 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002380 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002381 return false;
2382 }
2383
2384 // We have a class message. Turn the simple-type-specifier or
2385 // typename-specifier we parsed into a type and parse the
2386 // remainder of the class message.
2387 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002388 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002389 if (Type.isInvalid())
2390 return true;
2391
2392 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002393 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002394 return false;
2395}
2396
Douglas Gregor990ccac2010-05-31 14:40:22 +00002397/// \brief Determine whether the parser is currently referring to a an
2398/// Objective-C message send, using a simplified heuristic to avoid overhead.
2399///
2400/// This routine will only return true for a subset of valid message-send
2401/// expressions.
2402bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002403 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002404 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002405 return GetLookAheadToken(1).is(tok::identifier) &&
2406 GetLookAheadToken(2).is(tok::identifier);
2407}
2408
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002409bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002410 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002411 InMessageExpression)
2412 return false;
2413
2414
2415 ParsedType Type;
2416
2417 if (Tok.is(tok::annot_typename))
2418 Type = getTypeAnnotation(Tok);
2419 else if (Tok.is(tok::identifier))
2420 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2421 getCurScope());
2422 else
2423 return false;
2424
2425 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2426 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002427 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002428 if (Tok.is(tok::identifier))
2429 TryAnnotateTypeOrScopeToken();
2430
2431 return Tok.is(tok::annot_typename);
2432 }
2433 }
2434
2435 return false;
2436}
2437
Mike Stump11289f42009-09-09 15:08:12 +00002438/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002439/// '[' objc-receiver objc-message-args ']'
2440///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002441/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002442/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002443/// expression
2444/// class-name
2445/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002446///
John McCalldadc5752010-08-24 06:29:42 +00002447ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002448 assert(Tok.is(tok::l_square) && "'[' expected");
2449 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2450
Douglas Gregora817a192010-05-27 23:06:34 +00002451 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002452 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002453 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002454 return ExprError();
2455 }
2456
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002457 InMessageExpressionRAIIObject InMessage(*this, true);
2458
David Blaikiebbafb8a2012-03-11 07:00:24 +00002459 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002460 // We completely separate the C and C++ cases because C++ requires
2461 // more complicated (read: slower) parsing.
2462
2463 // Handle send to super.
2464 // FIXME: This doesn't benefit from the same typo-correction we
2465 // get in Objective-C.
2466 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002467 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002468 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002469 ParsedType(), nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002470
2471 // Parse the receiver, which is either a type or an expression.
2472 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00002473 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002474 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002475 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002476 return ExprError();
2477 }
2478
2479 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002480 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2481 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002482 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002483
2484 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002485 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00002486 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00002487 }
2488
2489 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002490 IdentifierInfo *Name = Tok.getIdentifierInfo();
2491 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002492 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002493 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002494 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002495 NextToken().is(tok::period),
2496 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002497 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002498 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002499 ParsedType(), nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002500
John McCallfaf5fb42010-08-26 23:41:50 +00002501 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002502 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002503 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002504 return ExprError();
2505 }
2506
Douglas Gregore5798dc2010-04-21 20:38:13 +00002507 ConsumeToken(); // the type name
2508
2509 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00002510 ReceiverType, nullptr);
2511
John McCallfaf5fb42010-08-26 23:41:50 +00002512 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002513 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002514 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002515 }
Chris Lattner8f697062008-01-25 18:59:06 +00002516 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002517
2518 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00002519 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002520 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002521 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002522 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00002523 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002524
John McCallba7bf592010-08-24 05:47:05 +00002525 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002526 ParsedType(), Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00002527}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002528
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002529/// \brief Parse the remainder of an Objective-C message following the
2530/// '[' objc-receiver.
2531///
2532/// This routine handles sends to super, class messages (sent to a
2533/// class name), and instance messages (sent to an object), and the
2534/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2535/// ReceiverExpr, respectively. Only one of these parameters may have
2536/// a valid value.
2537///
2538/// \param LBracLoc The location of the opening '['.
2539///
2540/// \param SuperLoc If this is a send to 'super', the location of the
2541/// 'super' keyword that indicates a send to the superclass.
2542///
2543/// \param ReceiverType If this is a class message, the type of the
2544/// class we are sending a message to.
2545///
2546/// \param ReceiverExpr If this is an instance message, the expression
2547/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002548///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002549/// objc-message-args:
2550/// objc-selector
2551/// objc-keywordarg-list
2552///
2553/// objc-keywordarg-list:
2554/// objc-keywordarg
2555/// objc-keywordarg-list objc-keywordarg
2556///
Mike Stump11289f42009-09-09 15:08:12 +00002557/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002558/// selector-name[opt] ':' objc-keywordexpr
2559///
2560/// objc-keywordexpr:
2561/// nonempty-expr-list
2562///
2563/// nonempty-expr-list:
2564/// assignment-expression
2565/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002566///
John McCalldadc5752010-08-24 06:29:42 +00002567ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002568Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002569 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002570 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00002571 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002572 InMessageExpressionRAIIObject InMessage(*this, true);
2573
Steve Naroffeae65032009-11-07 02:08:14 +00002574 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002575 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002576 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002577 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002578 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002579 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002580 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002581 else
John McCallb268a282010-08-23 23:25:46 +00002582 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002583 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002584 cutOffParsing();
2585 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002586 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002587
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002588 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002589 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002590 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002591
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002592 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002593 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002594 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00002595
Chris Lattner0ef13522007-10-09 17:51:17 +00002596 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002597 while (1) {
2598 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002599 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002600 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00002601
Alp Toker383d2c42014-01-01 03:08:43 +00002602 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00002603 // We must manually skip to a ']', otherwise the expression skipper will
2604 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2605 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002606 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002607 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002608 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002609
Mike Stump11289f42009-09-09 15:08:12 +00002610 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002611
2612 if (Tok.is(tok::code_completion)) {
2613 if (SuperLoc.isValid())
2614 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002615 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002616 /*AtArgumentEpression=*/true);
2617 else if (ReceiverType)
2618 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002619 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002620 /*AtArgumentEpression=*/true);
2621 else
2622 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002623 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002624 /*AtArgumentEpression=*/true);
2625
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002626 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002627 return ExprError();
2628 }
2629
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00002630 ExprResult Expr;
2631 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
2632 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2633 Expr = ParseBraceInitializer();
2634 } else
2635 Expr = ParseAssignmentExpression();
2636
2637 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002638 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002639 // We must manually skip to a ']', otherwise the expression skipper will
2640 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2641 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002642 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002643 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00002644 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002645
Steve Naroff486760a2007-09-17 20:25:27 +00002646 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002647 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002648
Douglas Gregor1b605f72009-11-19 01:08:35 +00002649 // Code completion after each argument.
2650 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002651 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002652 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002653 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002654 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002655 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002656 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002657 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002658 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002659 else
John McCallb268a282010-08-23 23:25:46 +00002660 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002661 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002662 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002663 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002664 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002665 }
2666
Steve Naroff486760a2007-09-17 20:25:27 +00002667 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002668 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002669 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002670 break;
2671 // We have a selector or a colon, continue parsing.
2672 }
2673 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002674 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002675 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002676 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002677 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00002678 if (Tok.is(tok::colon))
2679 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002680 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002681 if (Tok.is(tok::colon)) {
2682 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
2683 FixItHint::CreateRemoval(commaLoc);
2684 }
Chris Lattner197a3012008-08-05 06:19:09 +00002685 // We must manually skip to a ']', otherwise the expression skipper will
2686 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2687 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002688 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002689 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002690 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002691
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002692 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002693 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002694 }
2695 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00002696 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002697
Chris Lattner197a3012008-08-05 06:19:09 +00002698 // We must manually skip to a ']', otherwise the expression skipper will
2699 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2700 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002701 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002702 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002703 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002704
Chris Lattner0ef13522007-10-09 17:51:17 +00002705 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00002706 Diag(Tok, diag::err_expected)
2707 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00002708 // We must manually skip to a ']', otherwise the expression skipper will
2709 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2710 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002711 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002712 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002713 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002714
Chris Lattner8f697062008-01-25 18:59:06 +00002715 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002716
Steve Naroffe61bfa82007-10-05 18:42:47 +00002717 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002718 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00002719 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002720 KeyLocs.push_back(Loc);
2721 }
Chris Lattner5700fab2007-10-07 02:00:24 +00002722 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002723
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002724 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002725 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002726 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002727 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002728 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002729 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00002730 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002731 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002732}
2733
John McCalldadc5752010-08-24 06:29:42 +00002734ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2735 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002736 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002737
Chris Lattnere002fbe2007-12-12 01:04:12 +00002738 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2739 // expressions. At this point, we know that the only valid thing that starts
2740 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002741 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002742 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00002743 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002744 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002745
Chris Lattnere002fbe2007-12-12 01:04:12 +00002746 while (Tok.is(tok::at)) {
2747 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002748
Sebastian Redlc13f2682008-12-09 20:22:58 +00002749 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002750 if (!isTokenStringLiteral())
2751 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002752
John McCalldadc5752010-08-24 06:29:42 +00002753 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002754 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002755 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002756
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002757 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002758 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002759
Nico Webera7c7e602012-12-31 00:28:03 +00002760 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
2761 AtStrings.size());
Anders Carlsson76f4a902007-08-21 17:43:55 +00002762}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002763
Ted Kremeneke65b0862012-03-06 20:05:56 +00002764/// ParseObjCBooleanLiteral -
2765/// objc-scalar-literal : '@' boolean-keyword
2766/// ;
2767/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2768/// ;
2769ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2770 bool ArgValue) {
2771 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2772 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2773}
2774
2775/// ParseObjCCharacterLiteral -
2776/// objc-scalar-literal : '@' character-literal
2777/// ;
2778ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2779 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2780 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002781 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002782 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002783 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002784 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002785}
2786
2787/// ParseObjCNumericLiteral -
2788/// objc-scalar-literal : '@' scalar-literal
2789/// ;
2790/// scalar-literal : | numeric-constant /* any numeric constant. */
2791/// ;
2792ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2793 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2794 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002795 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002796 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002797 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002798 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002799}
2800
Patrick Beard0caa3942012-04-19 00:25:12 +00002801/// ParseObjCBoxedExpr -
2802/// objc-box-expression:
2803/// @( assignment-expression )
2804ExprResult
2805Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2806 if (Tok.isNot(tok::l_paren))
2807 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2808
2809 BalancedDelimiterTracker T(*this, tok::l_paren);
2810 T.consumeOpen();
2811 ExprResult ValueExpr(ParseAssignmentExpression());
2812 if (T.consumeClose())
2813 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00002814
2815 if (ValueExpr.isInvalid())
2816 return ExprError();
2817
Patrick Beard0caa3942012-04-19 00:25:12 +00002818 // Wrap the sub-expression in a parenthesized expression, to distinguish
2819 // a boxed expression from a literal.
2820 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002821 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00002822 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002823 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00002824}
2825
Ted Kremeneke65b0862012-03-06 20:05:56 +00002826ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002827 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002828 ConsumeBracket(); // consume the l_square.
2829
2830 while (Tok.isNot(tok::r_square)) {
2831 // Parse list of array element expressions (all must be id types).
2832 ExprResult Res(ParseAssignmentExpression());
2833 if (Res.isInvalid()) {
2834 // We must manually skip to a ']', otherwise the expression skipper will
2835 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2836 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002837 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002838 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002839 }
2840
2841 // Parse the ellipsis that indicates a pack expansion.
2842 if (Tok.is(tok::ellipsis))
2843 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2844 if (Res.isInvalid())
2845 return true;
2846
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002847 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002848
2849 if (Tok.is(tok::comma))
2850 ConsumeToken(); // Eat the ','.
2851 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00002852 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
2853 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002854 }
2855 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramerf0623432012-08-23 22:51:59 +00002856 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00002857 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002858}
2859
2860ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2861 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2862 ConsumeBrace(); // consume the l_square.
2863 while (Tok.isNot(tok::r_brace)) {
2864 // Parse the comma separated key : value expressions.
2865 ExprResult KeyExpr;
2866 {
2867 ColonProtectionRAIIObject X(*this);
2868 KeyExpr = ParseAssignmentExpression();
2869 if (KeyExpr.isInvalid()) {
2870 // We must manually skip to a '}', otherwise the expression skipper will
2871 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2872 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002873 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002874 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002875 }
2876 }
2877
Alp Toker383d2c42014-01-01 03:08:43 +00002878 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002879 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00002880 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00002881 }
2882
2883 ExprResult ValueExpr(ParseAssignmentExpression());
2884 if (ValueExpr.isInvalid()) {
2885 // We must manually skip to a '}', otherwise the expression skipper will
2886 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2887 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002888 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002889 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002890 }
2891
2892 // Parse the ellipsis that designates this as a pack expansion.
2893 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00002894 if (getLangOpts().CPlusPlus)
2895 TryConsumeToken(tok::ellipsis, EllipsisLoc);
2896
Ted Kremeneke65b0862012-03-06 20:05:56 +00002897 // We have a valid expression. Collect it in a vector so we can
2898 // build the argument list.
2899 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00002900 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00002901 };
2902 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00002903
2904 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002905 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
2906 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002907 }
2908 SourceLocation EndLoc = ConsumeBrace();
2909
2910 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00002911 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2912 Elements.data(), Elements.size());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002913}
2914
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002915/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002916/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002917ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002918Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002919 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002920
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002921 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002922
Chris Lattner197a3012008-08-05 06:19:09 +00002923 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002924 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2925
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002926 BalancedDelimiterTracker T(*this, tok::l_paren);
2927 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002928
Douglas Gregor220cac52009-02-18 17:45:20 +00002929 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002930
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002931 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002932
Douglas Gregor220cac52009-02-18 17:45:20 +00002933 if (Ty.isInvalid())
2934 return ExprError();
2935
Nico Webera7c7e602012-12-31 00:28:03 +00002936 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
2937 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002938}
Anders Carlssone01493d2007-08-23 15:25:28 +00002939
2940/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00002941/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002942ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002943Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002944 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002945
Chris Lattner197a3012008-08-05 06:19:09 +00002946 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002947 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2948
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002949 BalancedDelimiterTracker T(*this, tok::l_paren);
2950 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002951
Chris Lattner197a3012008-08-05 06:19:09 +00002952 if (Tok.isNot(tok::identifier))
Alp Tokerec543272013-12-24 09:48:30 +00002953 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002954
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002955 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00002956 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002957
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002958 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00002959
Nico Webera7c7e602012-12-31 00:28:03 +00002960 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2961 T.getOpenLocation(), ProtoIdLoc,
2962 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00002963}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002964
2965/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00002966/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002967ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002968 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002969
Chris Lattner197a3012008-08-05 06:19:09 +00002970 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002971 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2972
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002973 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002974 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002975
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002976 BalancedDelimiterTracker T(*this, tok::l_paren);
2977 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00002978 bool HasOptionalParen = Tok.is(tok::l_paren);
2979 if (HasOptionalParen)
2980 ConsumeParen();
2981
Douglas Gregor67c692c2010-08-26 15:07:07 +00002982 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002983 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002984 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002985 return ExprError();
2986 }
2987
Chris Lattner4f472a32009-04-11 18:13:45 +00002988 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002989 if (!SelIdent && // missing selector name.
2990 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00002991 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002992
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002993 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00002994
Steve Naroff152dd812007-12-05 22:21:29 +00002995 unsigned nColons = 0;
2996 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002997 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00002998 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00002999 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003000 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003001 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3002 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003003 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003004
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003005 if (Tok.is(tok::r_paren))
3006 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003007
3008 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003009 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003010 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003011 return ExprError();
3012 }
3013
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003014 // Check for another keyword selector.
3015 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003016 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003017 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003018 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003019 break;
3020 }
Steve Naroff152dd812007-12-05 22:21:29 +00003021 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003022 if (HasOptionalParen && Tok.is(tok::r_paren))
3023 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003024 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003025 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003026 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3027 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003028 T.getCloseLocation(),
3029 !HasOptionalParen);
Gabor Greif24032f12007-10-19 15:38:32 +00003030 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003031
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003032void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3033 // MCDecl might be null due to error in method or c-function prototype, etc.
3034 Decl *MCDecl = LM.D;
3035 bool skip = MCDecl &&
3036 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3037 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3038 if (skip)
3039 return;
3040
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003041 // Save the current token position.
3042 SourceLocation OrigLoc = Tok.getLocation();
3043
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003044 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
3045 // Append the current token at the end of the new token stream so that it
3046 // doesn't get lost.
3047 LM.Toks.push_back(Tok);
3048 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
3049
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003050 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003051 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003052
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003053 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3054 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003055 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003056 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003057 parseMethod
3058 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
3059 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003060
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003061 // Tell the actions module that we have entered a method or c-function definition
3062 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003063 if (parseMethod)
3064 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3065 else
3066 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003067 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003068 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003069 else {
3070 if (Tok.is(tok::colon))
3071 ParseConstructorInitializer(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003072 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003073 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00003074
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003075 if (Tok.getLocation() != OrigLoc) {
3076 // Due to parsing error, we either went over the cached tokens or
3077 // there are still cached tokens left. If it's the latter case skip the
3078 // leftover tokens.
3079 // Since this is an uncommon situation that should be avoided, use the
3080 // expensive isBeforeInTranslationUnit call.
3081 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3082 OrigLoc))
3083 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3084 ConsumeAnyToken();
3085 }
3086
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003087 return;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003088}