blob: f975f8715ed90e96ffab25946871b46f283c80c5 [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)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000560 << DiagNullabilityKind(nullability, true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000561 << SourceRange(DS.getNullabilityLoc());
562 return;
563 }
564
565 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000566 << DiagNullabilityKind(nullability, true)
567 << DiagNullabilityKind(DS.getNullability(), true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000568 << 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 Gregor5c0870a2015-06-19 23:18:00 +00001008 if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
John McCalla55902b2011-10-01 09:56:14 +00001009 // Parse an abstract declarator.
1010 DeclSpec declSpec(AttrFactory);
1011 declSpec.setObjCQualifiers(&DS);
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001012 DeclSpecContext dsContext = DSC_normal;
1013 if (context == Declarator::ObjCResultContext)
1014 dsContext = DSC_objc_method_result;
1015 ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
Fariborz Jahanianb6499eb2012-05-29 21:52:45 +00001016 declSpec.SetRangeEnd(Tok.getLocation());
John McCalla55902b2011-10-01 09:56:14 +00001017 Declarator declarator(declSpec, context);
1018 ParseDeclarator(declarator);
1019
1020 // If that's not invalid, extract a type.
1021 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001022 // Map a nullability specifier to a context-sensitive keyword attribute.
1023 bool addedToDeclSpec = false;
1024 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1025 addContextSensitiveTypeNullability(*this, declarator,
1026 DS.getNullability(),
1027 DS.getNullabilityLoc(),
1028 addedToDeclSpec);
1029
John McCalla55902b2011-10-01 09:56:14 +00001030 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1031 if (!type.isInvalid())
1032 Ty = type.get();
1033
1034 // If we're parsing a parameter, steal all the decl attributes
1035 // and add them to the decl spec.
1036 if (context == Declarator::ObjCParameterContext)
1037 takeDeclAttributes(*paramAttrs, declarator);
1038 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001039 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001040
Steve Naroff90255b42008-10-21 14:15:04 +00001041 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001042 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001043 else if (Tok.getLocation() == TypeStartLoc) {
1044 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001045 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001046 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001047 } else {
1048 // Otherwise, we found *something*, but didn't get a ')' in the right
1049 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001050 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001051 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001052 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001053}
1054
1055/// objc-method-decl:
1056/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001057/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001058/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001059/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001060///
1061/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001062/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001063/// objc-keyword-selector objc-keyword-decl
1064///
1065/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001066/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1067/// objc-selector ':' objc-keyword-attributes[opt] identifier
1068/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1069/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001070///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001071/// objc-parmlist:
1072/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001073///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001074/// objc-parms:
1075/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001076///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001077/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001078/// , ...
1079///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001080/// objc-keyword-attributes: [OBJC2]
1081/// __attribute__((unused))
1082///
John McCall48871652010-08-21 09:40:31 +00001083Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001084 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001085 tok::ObjCKeywordKind MethodImplKind,
1086 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001087 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001088
Douglas Gregor636a61e2010-04-07 00:21:17 +00001089 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001090 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001091 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001092 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001093 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001094 }
1095
Chris Lattner2ebb1782008-08-23 01:48:03 +00001096 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001097 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001098 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001099 if (Tok.is(tok::l_paren))
Craig Topper161e4db2014-05-21 06:02:52 +00001100 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext,
1101 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001102
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001103 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001104 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001105 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001106 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001107
Douglas Gregor636a61e2010-04-07 00:21:17 +00001108 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001109 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001110 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001111 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001112 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001113 }
1114
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001115 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001116 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001117 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001118
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001119 // An unnamed colon is valid.
1120 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001121 Diag(Tok, diag::err_expected_selector_for_method)
1122 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001123 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001124 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001125 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001126 }
Mike Stump11289f42009-09-09 15:08:12 +00001127
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001128 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001129 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001130 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001131 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001132 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001133
Chris Lattner5700fab2007-10-07 02:00:24 +00001134 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001135 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001136 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001137 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001138 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001139 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001140 methodAttrs.getList(), MethodImplKind,
1141 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001142 PD.complete(Result);
1143 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001144 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001145
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001146 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001147 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001148 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001149 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1150 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001151
1152 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001153 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001154 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001155 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001156
Chris Lattner5700fab2007-10-07 02:00:24 +00001157 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001158 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001159 break;
Mike Stump11289f42009-09-09 15:08:12 +00001160
John McCallba7bf592010-08-24 05:47:05 +00001161 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001162 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001163 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1164 Declarator::ObjCParameterContext,
1165 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001166
Chris Lattner5700fab2007-10-07 02:00:24 +00001167 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001168 // Regardless, collect all the attributes we've parsed so far.
Craig Topper161e4db2014-05-21 06:02:52 +00001169 ArgInfo.ArgAttrs = nullptr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001170 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001171 MaybeParseGNUAttributes(paramAttrs);
1172 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001173 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001174
Douglas Gregor45879692010-07-08 23:37:41 +00001175 // Code completion for the next piece of the selector.
1176 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001177 KeyIdents.push_back(SelIdent);
1178 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1179 mType == tok::minus,
1180 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001181 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001182 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001183 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001184 }
1185
Chris Lattner0ef13522007-10-09 17:51:17 +00001186 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001187 Diag(Tok, diag::err_expected)
1188 << tok::identifier; // missing argument name.
Chris Lattner5700fab2007-10-07 02:00:24 +00001189 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001190 }
Mike Stump11289f42009-09-09 15:08:12 +00001191
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001192 ArgInfo.Name = Tok.getIdentifierInfo();
1193 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001194 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001195
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001196 ArgInfos.push_back(ArgInfo);
1197 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001198 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001199
John McCall084e83d2011-03-24 11:26:52 +00001200 // Make sure the attributes persist.
1201 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1202
Douglas Gregor95887f92010-07-08 23:20:03 +00001203 // Code completion for the next piece of the selector.
1204 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001205 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1206 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001207 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001208 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001209 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001210 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001211 }
1212
Chris Lattner5700fab2007-10-07 02:00:24 +00001213 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001214 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001215 if (!SelIdent && Tok.isNot(tok::colon))
1216 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001217 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001218 SourceLocation ColonLoc = Tok.getLocation();
1219 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001220 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1221 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1222 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001223 }
1224 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001225 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001226 }
Mike Stump11289f42009-09-09 15:08:12 +00001227
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001228 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001229 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001230 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001231 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001232 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001233 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001234 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001235 ConsumeToken();
1236 break;
1237 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001238 if (!cStyleParamWarned) {
1239 Diag(Tok, diag::warn_cstyle_param);
1240 cStyleParamWarned = true;
1241 }
John McCall084e83d2011-03-24 11:26:52 +00001242 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001243 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001244 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001245 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1246 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001247 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001248 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001249 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1250 ParmDecl.getIdentifierLoc(),
1251 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001252 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001253 }
Mike Stump11289f42009-09-09 15:08:12 +00001254
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001255 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001256 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001257 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001258 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001259
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001260 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001261 return nullptr;
1262
Chris Lattner5700fab2007-10-07 02:00:24 +00001263 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1264 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001265 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001266 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001267 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001268 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001269 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001270 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001271 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001272
John McCall28a6aea2009-11-04 02:18:39 +00001273 PD.complete(Result);
1274 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001275}
1276
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001277/// objc-protocol-refs:
1278/// '<' identifier-list '>'
1279///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001280bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001281ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1282 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001283 bool WarnOnDeclarations, bool ForObjCContainer,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001284 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001285 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001286
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001287 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001288
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001289 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001290
Chris Lattner3bbae002008-07-26 04:03:38 +00001291 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001292 if (Tok.is(tok::code_completion)) {
1293 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1294 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001295 cutOffParsing();
1296 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001297 }
1298
Chris Lattner3bbae002008-07-26 04:03:38 +00001299 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001300 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001301 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001302 return true;
1303 }
1304 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1305 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001306 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001307 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001308
Alp Toker383d2c42014-01-01 03:08:43 +00001309 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001310 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001311 }
Mike Stump11289f42009-09-09 15:08:12 +00001312
Chris Lattner3bbae002008-07-26 04:03:38 +00001313 // Consume the '>'.
Nico Weber7aa4a882012-12-14 18:22:38 +00001314 if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true))
Chris Lattner3bbae002008-07-26 04:03:38 +00001315 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001316
Chris Lattner3bbae002008-07-26 04:03:38 +00001317 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001318 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Chris Lattner3bbae002008-07-26 04:03:38 +00001319 &ProtocolIdents[0], ProtocolIdents.size(),
1320 Protocols);
1321 return false;
1322}
1323
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001324/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1325/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001326bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001327 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001328 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001329 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001330 SmallVector<Decl *, 8> ProtocolDecl;
1331 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001332 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001333 false,
Douglas Gregor3a001f42010-11-19 17:10:50 +00001334 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001335 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1336 ProtocolLocs.data(), LAngleLoc);
1337 if (EndProtoLoc.isValid())
1338 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001339 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001340}
1341
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001342void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1343 BalancedDelimiterTracker &T,
1344 SmallVectorImpl<Decl *> &AllIvarDecls,
1345 bool RBraceMissing) {
1346 if (!RBraceMissing)
1347 T.consumeClose();
1348
1349 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1350 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1351 Actions.ActOnObjCContainerFinishDefinition();
1352 // Call ActOnFields() even if we don't have any decls. This is useful
1353 // for code rewriting tools that need to be aware of the empty list.
1354 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1355 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001356 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001357}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001358
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001359/// objc-class-instance-variables:
1360/// '{' objc-instance-variable-decl-list[opt] '}'
1361///
1362/// objc-instance-variable-decl-list:
1363/// objc-visibility-spec
1364/// objc-instance-variable-decl ';'
1365/// ';'
1366/// objc-instance-variable-decl-list objc-visibility-spec
1367/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1368/// objc-instance-variable-decl-list ';'
1369///
1370/// objc-visibility-spec:
1371/// @private
1372/// @protected
1373/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001374/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001375///
1376/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001377/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001378///
John McCall48871652010-08-21 09:40:31 +00001379void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001380 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001381 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001382 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001383 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001384
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001385 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001386 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001387
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001388 BalancedDelimiterTracker T(*this, tok::l_brace);
1389 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001390 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001391 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001392 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001393
Steve Naroff00433d32007-08-21 21:17:12 +00001394 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001395 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001396 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001397 continue;
1398 }
Mike Stump11289f42009-09-09 15:08:12 +00001399
Steve Naroff00433d32007-08-21 21:17:12 +00001400 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001401 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001402 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001403 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001404 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001405 }
1406
Steve Naroff7c348172007-08-23 18:16:40 +00001407 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001408 case tok::objc_private:
1409 case tok::objc_public:
1410 case tok::objc_protected:
1411 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001412 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001413 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001414 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001415
1416 case tok::objc_end:
1417 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001418 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1419 Tok.setKind(tok::at);
1420 Tok.setLength(1);
1421 PP.EnterToken(Tok);
1422 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1423 T, AllIvarDecls, true);
1424 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001425
1426 default:
1427 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1428 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001429 }
1430 }
Mike Stump11289f42009-09-09 15:08:12 +00001431
Douglas Gregor48d46252010-01-13 21:54:15 +00001432 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001433 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001434 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001435 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001436 }
John McCallcfefb6d2009-11-03 02:38:08 +00001437
Benjamin Kramera39beb92014-09-03 11:06:10 +00001438 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1439 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1440 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001441 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001442 Decl *Field = Actions.ActOnIvar(
1443 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1444 FD.BitfieldSize, visibility);
1445 Actions.ActOnObjCContainerFinishDefinition();
1446 if (Field)
1447 AllIvarDecls.push_back(Field);
1448 FD.complete(Field);
1449 };
John McCallcfefb6d2009-11-03 02:38:08 +00001450
Chris Lattnera12405b2008-04-10 06:46:29 +00001451 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001452 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001453 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001454
Chris Lattner0ef13522007-10-09 17:51:17 +00001455 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001456 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001457 } else {
1458 Diag(Tok, diag::err_expected_semi_decl_list);
1459 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001460 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001461 }
1462 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001463 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1464 T, AllIvarDecls, false);
Steve Naroff00433d32007-08-21 21:17:12 +00001465 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001466}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001467
1468/// objc-protocol-declaration:
1469/// objc-protocol-definition
1470/// objc-protocol-forward-reference
1471///
1472/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00001473/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00001474/// objc-protocol-refs[opt]
1475/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00001476/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001477///
1478/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00001479/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001480///
James Dennett1355bd12012-06-11 06:19:40 +00001481/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001482/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001483/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001484Parser::DeclGroupPtrTy
1485Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1486 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001487 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001488 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1489 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001490
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001491 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001492 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001493 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00001494 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001495 }
1496
Nico Weber69a79142013-04-04 00:15:10 +00001497 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00001498
Chris Lattner0ef13522007-10-09 17:51:17 +00001499 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001500 Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00001501 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001502 }
1503 // Save the protocol name, then consume it.
1504 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1505 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001506
Alp Toker383d2c42014-01-01 03:08:43 +00001507 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001508 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001509 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001510 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001511 }
Mike Stump11289f42009-09-09 15:08:12 +00001512
Erik Verbruggenf9887852011-12-08 09:58:43 +00001513 CheckNestedObjCContexts(AtLoc);
1514
Chris Lattner0ef13522007-10-09 17:51:17 +00001515 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001516 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001517 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1518
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001519 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001520 while (1) {
1521 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001522 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001523 Diag(Tok, diag::err_expected) << tok::identifier;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001524 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00001525 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001526 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001527 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1528 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001529 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001530
Chris Lattner0ef13522007-10-09 17:51:17 +00001531 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001532 break;
1533 }
1534 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00001535 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00001536 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001537
Steve Naroff93eb5f12007-10-10 17:32:04 +00001538 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001539 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001540 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001541 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001542 }
Mike Stump11289f42009-09-09 15:08:12 +00001543
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001544 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001545 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001546
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001547 SmallVector<Decl *, 8> ProtocolRefs;
1548 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001549 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001550 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001551 LAngleLoc, EndProtoLoc))
Douglas Gregorf6102672012-01-01 21:23:57 +00001552 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001553
John McCall48871652010-08-21 09:40:31 +00001554 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001555 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001556 ProtocolRefs.data(),
1557 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001558 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001559 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001560
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001561 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00001562 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001563}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001564
1565/// objc-implementation:
1566/// objc-class-implementation-prologue
1567/// objc-category-implementation-prologue
1568///
1569/// objc-class-implementation-prologue:
1570/// @implementation identifier objc-superclass[opt]
1571/// objc-class-instance-variables[opt]
1572///
1573/// objc-category-implementation-prologue:
1574/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001575Parser::DeclGroupPtrTy
1576Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001577 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1578 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001579 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001580 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001581
Douglas Gregor49c22a72009-11-18 16:26:39 +00001582 // Code completion after '@implementation'.
1583 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001584 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001585 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001586 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001587 }
1588
Nico Weber69a79142013-04-04 00:15:10 +00001589 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00001590
Chris Lattner0ef13522007-10-09 17:51:17 +00001591 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001592 Diag(Tok, diag::err_expected)
1593 << tok::identifier; // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001594 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001595 }
1596 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001597 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001598 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00001599 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001600
1601 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001602 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001603 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001604 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001605 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001606
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001607 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001608 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001609 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001610 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001611 }
1612
Chris Lattner0ef13522007-10-09 17:51:17 +00001613 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001614 categoryId = Tok.getIdentifierInfo();
1615 categoryLoc = ConsumeToken();
1616 } else {
Alp Tokerec543272013-12-24 09:48:30 +00001617 Diag(Tok, diag::err_expected)
1618 << tok::identifier; // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001619 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001620 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001621 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001622 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001623 SkipUntil(tok::r_paren); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001624 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001625 }
1626 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00001627 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1628 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1629 AttributeFactory attr;
1630 DeclSpec DS(attr);
1631 (void)ParseObjCProtocolQualifiers(DS);
1632 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001633 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001634 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001635 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001636
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001637 } else {
1638 // We have a class implementation
1639 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001640 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00001641 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001642 // We have a super class
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001643 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001644 Diag(Tok, diag::err_expected)
1645 << tok::identifier; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001646 return DeclGroupPtrTy();
1647 }
1648 superClassId = Tok.getIdentifierInfo();
1649 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001650 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001651 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1652 AtLoc, nameId, nameLoc,
1653 superClassId, superClassLoc);
1654
1655 if (Tok.is(tok::l_brace)) // we have ivars
1656 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00001657 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1658 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1659 // try to recover.
1660 AttributeFactory attr;
1661 DeclSpec DS(attr);
1662 (void)ParseObjCProtocolQualifiers(DS);
1663 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001664 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001665 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001666
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001667 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001668
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001669 {
1670 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00001671 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001672 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001673 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001674 MaybeParseMicrosoftAttributes(attrs);
1675 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1676 DeclGroupRef DG = DGP.get();
1677 DeclsInGroup.append(DG.begin(), DG.end());
1678 }
1679 }
1680 }
1681
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001682 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001683}
Steve Naroff33a1e802007-10-29 21:38:07 +00001684
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001685Parser::DeclGroupPtrTy
1686Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001687 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1688 "ParseObjCAtEndDeclaration(): Expected @end");
1689 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001690 if (CurParsedObjCImpl)
1691 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001692 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00001693 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001694 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001695 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001696}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001697
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001698Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1699 if (!Finished) {
1700 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00001701 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001702 P.Diag(P.Tok, diag::err_objc_missing_end)
1703 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1704 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1705 << Sema::OCK_Implementation;
1706 }
1707 }
Craig Topper161e4db2014-05-21 06:02:52 +00001708 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001709 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001710}
1711
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001712void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1713 assert(!Finished);
1714 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1715 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001716 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1717 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001718
1719 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1720
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001721 if (HasCFunction)
1722 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1723 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1724 false/*c-functions*/);
1725
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001726 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001727 for (LateParsedObjCMethodContainer::iterator
1728 I = LateParsedObjCMethods.begin(),
1729 E = LateParsedObjCMethods.end(); I != E; ++I)
1730 delete *I;
1731 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001732
1733 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001734}
1735
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001736/// compatibility-alias-decl:
1737/// @compatibility_alias alias-name class-name ';'
1738///
John McCall48871652010-08-21 09:40:31 +00001739Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001740 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1741 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1742 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001743 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001744 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00001745 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001746 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001747 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1748 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001749 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001750 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00001751 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001752 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001753 IdentifierInfo *classId = Tok.getIdentifierInfo();
1754 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00001755 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00001756 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1757 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001758}
1759
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001760/// property-synthesis:
1761/// @synthesize property-ivar-list ';'
1762///
1763/// property-ivar-list:
1764/// property-ivar
1765/// property-ivar-list ',' property-ivar
1766///
1767/// property-ivar:
1768/// identifier
1769/// identifier '=' identifier
1770///
John McCall48871652010-08-21 09:40:31 +00001771Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001772 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00001773 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001774 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001775
Douglas Gregor88e72a02009-11-18 19:45:45 +00001776 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001777 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001778 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001779 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001780 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00001781 }
1782
Douglas Gregor88e72a02009-11-18 19:45:45 +00001783 if (Tok.isNot(tok::identifier)) {
1784 Diag(Tok, diag::err_synthesized_property_name);
1785 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00001786 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001787 }
Craig Topper161e4db2014-05-21 06:02:52 +00001788
1789 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001790 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1791 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001792 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00001793 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001794 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00001795 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001796 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001797 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001798 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00001799 }
1800
Chris Lattner0ef13522007-10-09 17:51:17 +00001801 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001802 Diag(Tok, diag::err_expected) << tok::identifier;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001803 break;
1804 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001805 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001806 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001807 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001808 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001809 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001810 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001811 break;
1812 ConsumeToken(); // consume ','
1813 }
Alp Toker383d2c42014-01-01 03:08:43 +00001814 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00001815 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001816}
1817
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001818/// property-dynamic:
1819/// @dynamic property-list
1820///
1821/// property-list:
1822/// identifier
1823/// property-list ',' identifier
1824///
John McCall48871652010-08-21 09:40:31 +00001825Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001826 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1827 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001828 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001829 while (true) {
1830 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001831 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001832 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001833 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001834 }
1835
1836 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001837 Diag(Tok, diag::err_expected) << tok::identifier;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001838 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00001839 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001840 }
1841
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001842 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1843 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001844 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Craig Topper161e4db2014-05-21 06:02:52 +00001845 propertyId, nullptr, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001846
Chris Lattner0ef13522007-10-09 17:51:17 +00001847 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001848 break;
1849 ConsumeToken(); // consume ','
1850 }
Alp Toker383d2c42014-01-01 03:08:43 +00001851 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00001852 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001853}
Mike Stump11289f42009-09-09 15:08:12 +00001854
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001855/// objc-throw-statement:
1856/// throw expression[opt];
1857///
John McCalldadc5752010-08-24 06:29:42 +00001858StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1859 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001860 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001861 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001862 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001863 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001864 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001865 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001866 }
1867 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001868 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00001869 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001870 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001871}
1872
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001873/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001874/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001875///
John McCalldadc5752010-08-24 06:29:42 +00001876StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001877Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001878 ConsumeToken(); // consume synchronized
1879 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001880 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001881 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001882 }
John McCalld9bb7432011-07-27 21:50:02 +00001883
1884 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001885 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00001886 ExprResult operand(ParseExpression());
1887
1888 if (Tok.is(tok::r_paren)) {
1889 ConsumeParen(); // ')'
1890 } else {
1891 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00001892 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00001893
1894 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001895 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001896 }
John McCalld9bb7432011-07-27 21:50:02 +00001897
1898 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001899 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00001900 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00001901 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001902 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001903 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001904
John McCalld9bb7432011-07-27 21:50:02 +00001905 // Check the @synchronized operand now.
1906 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001907 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001908
John McCalld9bb7432011-07-27 21:50:02 +00001909 // Parse the compound statement within a new scope.
1910 ParseScope bodyScope(this, Scope::DeclScope);
1911 StmtResult body(ParseCompoundStatementBody());
1912 bodyScope.Exit();
1913
1914 // If there was a semantic or parse error earlier with the
1915 // operand, fail now.
1916 if (operand.isInvalid())
1917 return StmtError();
1918
1919 if (body.isInvalid())
1920 body = Actions.ActOnNullStmt(Tok.getLocation());
1921
1922 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001923}
1924
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001925/// objc-try-catch-statement:
1926/// @try compound-statement objc-catch-list[opt]
1927/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1928///
1929/// objc-catch-list:
1930/// @catch ( parameter-declaration ) compound-statement
1931/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1932/// catch-parameter-declaration:
1933/// parameter-declaration
1934/// '...' [OBJC2]
1935///
John McCalldadc5752010-08-24 06:29:42 +00001936StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001937 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001938
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001939 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001940 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00001941 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001942 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001943 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00001944 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00001945 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001946 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001947 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001948 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001949 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001950 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001951
Chris Lattner0ef13522007-10-09 17:51:17 +00001952 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001953 // At this point, we need to lookahead to determine if this @ is the start
1954 // of an @catch or @finally. We don't want to consume the @ token if this
1955 // is an @try or @encode or something else.
1956 Token AfterAt = GetLookAheadToken(1);
1957 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1958 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1959 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001960
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001961 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001962 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00001963 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001964 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001965 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001966 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001967 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001968 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001969 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001970 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00001971 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00001972 ParseDeclarator(ParmDecl);
1973
Douglas Gregore11ee112010-04-23 23:01:43 +00001974 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001975 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001976 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001977 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001978 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001979
Steve Naroff65a00892009-04-07 22:56:58 +00001980 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001981
Steve Naroff65a00892009-04-07 22:56:58 +00001982 if (Tok.is(tok::r_paren))
1983 RParenLoc = ConsumeParen();
1984 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001985 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00001986
John McCalldadc5752010-08-24 06:29:42 +00001987 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001988 if (Tok.is(tok::l_brace))
1989 CatchBody = ParseCompoundStatementBody();
1990 else
Alp Tokerec543272013-12-24 09:48:30 +00001991 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001992 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001993 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001994
John McCalldadc5752010-08-24 06:29:42 +00001995 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001996 RParenLoc,
1997 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001998 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00001999 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002000 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002001
Steve Naroffe6016792008-02-05 21:27:35 +00002002 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002003 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2004 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002005 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002006 }
2007 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002008 } else {
2009 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002010 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002011 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002012
John McCalldadc5752010-08-24 06:29:42 +00002013 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002014 if (Tok.is(tok::l_brace))
2015 FinallyBody = ParseCompoundStatementBody();
2016 else
Alp Tokerec543272013-12-24 09:48:30 +00002017 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002018 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002019 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002020 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002021 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002022 catch_or_finally_seen = true;
2023 break;
2024 }
2025 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002026 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002027 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002028 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002029 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002030
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002031 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002032 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002033 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002034}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002035
John McCall31168b02011-06-15 23:02:42 +00002036/// objc-autoreleasepool-statement:
2037/// @autoreleasepool compound-statement
2038///
2039StmtResult
2040Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2041 ConsumeToken(); // consume autoreleasepool
2042 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002043 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002044 return StmtError();
2045 }
2046 // Enter a scope to hold everything within the compound stmt. Compound
2047 // statements can always hold declarations.
2048 ParseScope BodyScope(this, Scope::DeclScope);
2049
2050 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2051
2052 BodyScope.Exit();
2053 if (AutoreleasePoolBody.isInvalid())
2054 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2055 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002056 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002057}
2058
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002059/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2060/// for later parsing.
2061void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
2062 LexedMethod* LM = new LexedMethod(this, MDecl);
2063 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2064 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002065 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002066 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002067 if (Tok.is(tok::kw_try)) {
2068 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002069 if (Tok.is(tok::colon)) {
2070 Toks.push_back(Tok);
2071 ConsumeToken();
2072 while (Tok.isNot(tok::l_brace)) {
2073 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2074 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2075 }
2076 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002077 Toks.push_back(Tok); // also store '{'
2078 }
2079 else if (Tok.is(tok::colon)) {
2080 ConsumeToken();
2081 while (Tok.isNot(tok::l_brace)) {
2082 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2083 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2084 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002085 Toks.push_back(Tok); // also store '{'
2086 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002087 ConsumeBrace();
2088 // Consume everything up to (and including) the matching right brace.
2089 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002090 while (Tok.is(tok::kw_catch)) {
2091 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2092 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2093 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002094}
2095
Steve Naroff09bf8152007-09-06 21:24:23 +00002096/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002097///
John McCall48871652010-08-21 09:40:31 +00002098Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002099 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002100
John McCallfaf5fb42010-08-26 23:41:50 +00002101 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2102 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002103
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002104 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002105 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002106 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002107 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002108 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002109 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002110 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002111 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002112
Steve Naroffbb875722007-11-11 19:54:21 +00002113 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002114 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002115 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002116
Steve Naroffbb875722007-11-11 19:54:21 +00002117 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002118 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002119
Steve Naroffbb875722007-11-11 19:54:21 +00002120 // If we didn't find the '{', bail out.
2121 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002122 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002123 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002124
2125 if (!MDecl) {
2126 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002127 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002128 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002129 }
2130
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002131 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002132 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002133 assert (CurParsedObjCImpl
2134 && "ParseObjCMethodDefinition - Method out of @implementation");
2135 // Consume the tokens and store them for later parsing.
2136 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002137 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002138}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002139
John McCalldadc5752010-08-24 06:29:42 +00002140StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002141 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002142 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002143 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002144 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002145 }
2146
2147 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002148 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002149
2150 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002151 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002152
2153 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002154 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002155
2156 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2157 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002158
2159 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2160 getLangOpts().DebuggerSupport) {
2161 SkipUntil(tok::semi);
2162 return Actions.ActOnNullStmt(Tok.getLocation());
2163 }
2164
John McCalldadc5752010-08-24 06:29:42 +00002165 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002166 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002167 // If the expression is invalid, skip ahead to the next semicolon. Not
2168 // doing this opens us up to the possibility of infinite loops if
2169 // ParseExpression does not consume any tokens.
2170 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002171 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002172 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002173
Steve Naroffe6016792008-02-05 21:27:35 +00002174 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002175 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002176 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002177}
2178
John McCalldadc5752010-08-24 06:29:42 +00002179ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002180 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002181 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002182 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002183 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002184 return ExprError();
2185
Ted Kremeneke65b0862012-03-06 20:05:56 +00002186 case tok::minus:
2187 case tok::plus: {
2188 tok::TokenKind Kind = Tok.getKind();
2189 SourceLocation OpLoc = ConsumeToken();
2190
2191 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002192 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002193 switch (Kind) {
2194 case tok::minus: Symbol = "-"; break;
2195 case tok::plus: Symbol = "+"; break;
2196 default: llvm_unreachable("missing unary operator case");
2197 }
2198 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2199 << Symbol;
2200 return ExprError();
2201 }
2202
2203 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2204 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002205 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002206 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002207 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002208
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002209 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002210 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002211 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002212
2213 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002214 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002215 }
2216
Chris Lattnere002fbe2007-12-12 01:04:12 +00002217 case tok::string_literal: // primary-expression: string-literal
2218 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002219 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002220
2221 case tok::char_constant:
2222 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2223
2224 case tok::numeric_constant:
2225 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2226
2227 case tok::kw_true: // Objective-C++, etc.
2228 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2229 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2230 case tok::kw_false: // Objective-C++, etc.
2231 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2232 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2233
2234 case tok::l_square:
2235 // Objective-C array literal
2236 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2237
2238 case tok::l_brace:
2239 // Objective-C dictionary literal
2240 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2241
Patrick Beard0caa3942012-04-19 00:25:12 +00002242 case tok::l_paren:
2243 // Objective-C boxed expression
2244 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2245
Chris Lattnere002fbe2007-12-12 01:04:12 +00002246 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002247 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002248 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002249
Chris Lattner197a3012008-08-05 06:19:09 +00002250 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2251 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002252 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002253 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002254 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002255 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002256 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002257 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002258 const char *str = nullptr;
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002259 if (GetLookAheadToken(1).is(tok::l_brace)) {
2260 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2261 str =
2262 ch == 't' ? "try"
2263 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002264 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002265 }
2266 if (str) {
2267 SourceLocation kwLoc = Tok.getLocation();
2268 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2269 FixItHint::CreateReplacement(kwLoc, str));
2270 }
2271 else
2272 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2273 }
Chris Lattner197a3012008-08-05 06:19:09 +00002274 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002275 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002276}
2277
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002278/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002279///
2280/// This routine parses the receiver of a message send in
2281/// Objective-C++ either as a type or as an expression. Note that this
2282/// routine must not be called to parse a send to 'super', since it
2283/// has no way to return such a result.
2284///
2285/// \param IsExpr Whether the receiver was parsed as an expression.
2286///
2287/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2288/// IsExpr is true), the parsed expression. If the receiver was parsed
2289/// as a type (\c IsExpr is false), the parsed type.
2290///
2291/// \returns True if an error occurred during parsing or semantic
2292/// analysis, in which case the arguments do not have valid
2293/// values. Otherwise, returns false for a successful parse.
2294///
2295/// objc-receiver: [C++]
2296/// 'super' [not parsed here]
2297/// expression
2298/// simple-type-specifier
2299/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002300bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002301 InMessageExpressionRAIIObject InMessage(*this, true);
2302
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002303 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2304 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002305 TryAnnotateTypeOrScopeToken();
2306
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002307 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002308 // objc-receiver:
2309 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002310 // Make sure any typos in the receiver are corrected or diagnosed, so that
2311 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2312 // only the things that are valid ObjC receivers?
2313 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002314 if (Receiver.isInvalid())
2315 return true;
2316
2317 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002318 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002319 return false;
2320 }
2321
2322 // objc-receiver:
2323 // typename-specifier
2324 // simple-type-specifier
2325 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002326 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002327 ParseCXXSimpleTypeSpecifier(DS);
2328
2329 if (Tok.is(tok::l_paren)) {
2330 // If we see an opening parentheses at this point, we are
2331 // actually parsing an expression that starts with a
2332 // function-style cast, e.g.,
2333 //
2334 // postfix-expression:
2335 // simple-type-specifier ( expression-list [opt] )
2336 // typename-specifier ( expression-list [opt] )
2337 //
2338 // Parse the remainder of this case, then the (optional)
2339 // postfix-expression suffix, followed by the (optional)
2340 // right-hand side of the binary expression. We have an
2341 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002342 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002343 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002344 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002345 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002346 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002347 if (Receiver.isInvalid())
2348 return true;
2349
2350 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002351 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002352 return false;
2353 }
2354
2355 // We have a class message. Turn the simple-type-specifier or
2356 // typename-specifier we parsed into a type and parse the
2357 // remainder of the class message.
2358 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002359 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002360 if (Type.isInvalid())
2361 return true;
2362
2363 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002364 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002365 return false;
2366}
2367
Douglas Gregor990ccac2010-05-31 14:40:22 +00002368/// \brief Determine whether the parser is currently referring to a an
2369/// Objective-C message send, using a simplified heuristic to avoid overhead.
2370///
2371/// This routine will only return true for a subset of valid message-send
2372/// expressions.
2373bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002374 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002375 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002376 return GetLookAheadToken(1).is(tok::identifier) &&
2377 GetLookAheadToken(2).is(tok::identifier);
2378}
2379
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002380bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002381 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002382 InMessageExpression)
2383 return false;
2384
2385
2386 ParsedType Type;
2387
2388 if (Tok.is(tok::annot_typename))
2389 Type = getTypeAnnotation(Tok);
2390 else if (Tok.is(tok::identifier))
2391 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2392 getCurScope());
2393 else
2394 return false;
2395
2396 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2397 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002398 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002399 if (Tok.is(tok::identifier))
2400 TryAnnotateTypeOrScopeToken();
2401
2402 return Tok.is(tok::annot_typename);
2403 }
2404 }
2405
2406 return false;
2407}
2408
Mike Stump11289f42009-09-09 15:08:12 +00002409/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002410/// '[' objc-receiver objc-message-args ']'
2411///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002412/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002413/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002414/// expression
2415/// class-name
2416/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002417///
John McCalldadc5752010-08-24 06:29:42 +00002418ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002419 assert(Tok.is(tok::l_square) && "'[' expected");
2420 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2421
Douglas Gregora817a192010-05-27 23:06:34 +00002422 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002423 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002424 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002425 return ExprError();
2426 }
2427
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002428 InMessageExpressionRAIIObject InMessage(*this, true);
2429
David Blaikiebbafb8a2012-03-11 07:00:24 +00002430 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002431 // We completely separate the C and C++ cases because C++ requires
2432 // more complicated (read: slower) parsing.
2433
2434 // Handle send to super.
2435 // FIXME: This doesn't benefit from the same typo-correction we
2436 // get in Objective-C.
2437 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002438 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002439 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002440 ParsedType(), nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002441
2442 // Parse the receiver, which is either a type or an expression.
2443 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00002444 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002445 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002446 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002447 return ExprError();
2448 }
2449
2450 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002451 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2452 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002453 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002454
2455 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002456 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00002457 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00002458 }
2459
2460 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002461 IdentifierInfo *Name = Tok.getIdentifierInfo();
2462 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002463 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002464 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002465 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002466 NextToken().is(tok::period),
2467 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002468 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002469 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002470 ParsedType(), nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002471
John McCallfaf5fb42010-08-26 23:41:50 +00002472 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002473 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002474 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002475 return ExprError();
2476 }
2477
Douglas Gregore5798dc2010-04-21 20:38:13 +00002478 ConsumeToken(); // the type name
2479
2480 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00002481 ReceiverType, nullptr);
2482
John McCallfaf5fb42010-08-26 23:41:50 +00002483 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002484 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002485 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002486 }
Chris Lattner8f697062008-01-25 18:59:06 +00002487 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002488
2489 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00002490 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002491 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002492 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002493 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00002494 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002495
John McCallba7bf592010-08-24 05:47:05 +00002496 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002497 ParsedType(), Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00002498}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002499
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002500/// \brief Parse the remainder of an Objective-C message following the
2501/// '[' objc-receiver.
2502///
2503/// This routine handles sends to super, class messages (sent to a
2504/// class name), and instance messages (sent to an object), and the
2505/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2506/// ReceiverExpr, respectively. Only one of these parameters may have
2507/// a valid value.
2508///
2509/// \param LBracLoc The location of the opening '['.
2510///
2511/// \param SuperLoc If this is a send to 'super', the location of the
2512/// 'super' keyword that indicates a send to the superclass.
2513///
2514/// \param ReceiverType If this is a class message, the type of the
2515/// class we are sending a message to.
2516///
2517/// \param ReceiverExpr If this is an instance message, the expression
2518/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002519///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002520/// objc-message-args:
2521/// objc-selector
2522/// objc-keywordarg-list
2523///
2524/// objc-keywordarg-list:
2525/// objc-keywordarg
2526/// objc-keywordarg-list objc-keywordarg
2527///
Mike Stump11289f42009-09-09 15:08:12 +00002528/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002529/// selector-name[opt] ':' objc-keywordexpr
2530///
2531/// objc-keywordexpr:
2532/// nonempty-expr-list
2533///
2534/// nonempty-expr-list:
2535/// assignment-expression
2536/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002537///
John McCalldadc5752010-08-24 06:29:42 +00002538ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002539Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002540 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002541 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00002542 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002543 InMessageExpressionRAIIObject InMessage(*this, true);
2544
Steve Naroffeae65032009-11-07 02:08:14 +00002545 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002546 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002547 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002548 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002549 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002550 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002551 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002552 else
John McCallb268a282010-08-23 23:25:46 +00002553 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002554 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002555 cutOffParsing();
2556 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002557 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002558
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002559 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002560 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002561 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002562
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002563 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002564 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002565 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00002566
Chris Lattner0ef13522007-10-09 17:51:17 +00002567 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002568 while (1) {
2569 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002570 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002571 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00002572
Alp Toker383d2c42014-01-01 03:08:43 +00002573 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00002574 // We must manually skip to a ']', otherwise the expression skipper will
2575 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2576 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002577 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002578 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002579 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002580
Mike Stump11289f42009-09-09 15:08:12 +00002581 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002582
2583 if (Tok.is(tok::code_completion)) {
2584 if (SuperLoc.isValid())
2585 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002586 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002587 /*AtArgumentEpression=*/true);
2588 else if (ReceiverType)
2589 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002590 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002591 /*AtArgumentEpression=*/true);
2592 else
2593 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002594 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002595 /*AtArgumentEpression=*/true);
2596
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002597 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002598 return ExprError();
2599 }
2600
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00002601 ExprResult Expr;
2602 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
2603 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2604 Expr = ParseBraceInitializer();
2605 } else
2606 Expr = ParseAssignmentExpression();
2607
2608 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002609 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002610 // We must manually skip to a ']', otherwise the expression skipper will
2611 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2612 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002613 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002614 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00002615 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002616
Steve Naroff486760a2007-09-17 20:25:27 +00002617 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002618 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002619
Douglas Gregor1b605f72009-11-19 01:08:35 +00002620 // Code completion after each argument.
2621 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002622 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002623 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002624 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002625 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002626 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002627 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002628 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002629 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002630 else
John McCallb268a282010-08-23 23:25:46 +00002631 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002632 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002633 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002634 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002635 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002636 }
2637
Steve Naroff486760a2007-09-17 20:25:27 +00002638 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002639 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002640 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002641 break;
2642 // We have a selector or a colon, continue parsing.
2643 }
2644 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002645 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002646 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002647 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002648 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00002649 if (Tok.is(tok::colon))
2650 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002651 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002652 if (Tok.is(tok::colon)) {
2653 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
2654 FixItHint::CreateRemoval(commaLoc);
2655 }
Chris Lattner197a3012008-08-05 06:19:09 +00002656 // We must manually skip to a ']', otherwise the expression skipper will
2657 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2658 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002659 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002660 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002661 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002662
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002663 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002664 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002665 }
2666 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00002667 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002668
Chris Lattner197a3012008-08-05 06:19:09 +00002669 // We must manually skip to a ']', otherwise the expression skipper will
2670 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2671 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002672 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002673 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002674 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002675
Chris Lattner0ef13522007-10-09 17:51:17 +00002676 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00002677 Diag(Tok, diag::err_expected)
2678 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00002679 // We must manually skip to a ']', otherwise the expression skipper will
2680 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2681 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002682 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002683 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002684 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002685
Chris Lattner8f697062008-01-25 18:59:06 +00002686 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002687
Steve Naroffe61bfa82007-10-05 18:42:47 +00002688 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002689 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00002690 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002691 KeyLocs.push_back(Loc);
2692 }
Chris Lattner5700fab2007-10-07 02:00:24 +00002693 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002694
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002695 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002696 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002697 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002698 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002699 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002700 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00002701 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002702 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002703}
2704
John McCalldadc5752010-08-24 06:29:42 +00002705ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2706 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002707 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002708
Chris Lattnere002fbe2007-12-12 01:04:12 +00002709 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2710 // expressions. At this point, we know that the only valid thing that starts
2711 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002712 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002713 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00002714 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002715 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002716
Chris Lattnere002fbe2007-12-12 01:04:12 +00002717 while (Tok.is(tok::at)) {
2718 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002719
Sebastian Redlc13f2682008-12-09 20:22:58 +00002720 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002721 if (!isTokenStringLiteral())
2722 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002723
John McCalldadc5752010-08-24 06:29:42 +00002724 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002725 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002726 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002727
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002728 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002729 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002730
Nico Webera7c7e602012-12-31 00:28:03 +00002731 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
2732 AtStrings.size());
Anders Carlsson76f4a902007-08-21 17:43:55 +00002733}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002734
Ted Kremeneke65b0862012-03-06 20:05:56 +00002735/// ParseObjCBooleanLiteral -
2736/// objc-scalar-literal : '@' boolean-keyword
2737/// ;
2738/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2739/// ;
2740ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2741 bool ArgValue) {
2742 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2743 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2744}
2745
2746/// ParseObjCCharacterLiteral -
2747/// objc-scalar-literal : '@' character-literal
2748/// ;
2749ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2750 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2751 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002752 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002753 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002754 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002755 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002756}
2757
2758/// ParseObjCNumericLiteral -
2759/// objc-scalar-literal : '@' scalar-literal
2760/// ;
2761/// scalar-literal : | numeric-constant /* any numeric constant. */
2762/// ;
2763ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2764 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2765 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002766 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002767 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002768 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002769 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002770}
2771
Patrick Beard0caa3942012-04-19 00:25:12 +00002772/// ParseObjCBoxedExpr -
2773/// objc-box-expression:
2774/// @( assignment-expression )
2775ExprResult
2776Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2777 if (Tok.isNot(tok::l_paren))
2778 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2779
2780 BalancedDelimiterTracker T(*this, tok::l_paren);
2781 T.consumeOpen();
2782 ExprResult ValueExpr(ParseAssignmentExpression());
2783 if (T.consumeClose())
2784 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00002785
2786 if (ValueExpr.isInvalid())
2787 return ExprError();
2788
Patrick Beard0caa3942012-04-19 00:25:12 +00002789 // Wrap the sub-expression in a parenthesized expression, to distinguish
2790 // a boxed expression from a literal.
2791 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002792 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00002793 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002794 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00002795}
2796
Ted Kremeneke65b0862012-03-06 20:05:56 +00002797ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002798 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002799 ConsumeBracket(); // consume the l_square.
2800
2801 while (Tok.isNot(tok::r_square)) {
2802 // Parse list of array element expressions (all must be id types).
2803 ExprResult Res(ParseAssignmentExpression());
2804 if (Res.isInvalid()) {
2805 // We must manually skip to a ']', otherwise the expression skipper will
2806 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2807 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002808 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002809 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002810 }
2811
2812 // Parse the ellipsis that indicates a pack expansion.
2813 if (Tok.is(tok::ellipsis))
2814 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2815 if (Res.isInvalid())
2816 return true;
2817
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002818 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002819
2820 if (Tok.is(tok::comma))
2821 ConsumeToken(); // Eat the ','.
2822 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00002823 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
2824 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002825 }
2826 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramerf0623432012-08-23 22:51:59 +00002827 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00002828 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002829}
2830
2831ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2832 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2833 ConsumeBrace(); // consume the l_square.
2834 while (Tok.isNot(tok::r_brace)) {
2835 // Parse the comma separated key : value expressions.
2836 ExprResult KeyExpr;
2837 {
2838 ColonProtectionRAIIObject X(*this);
2839 KeyExpr = ParseAssignmentExpression();
2840 if (KeyExpr.isInvalid()) {
2841 // We must manually skip to a '}', otherwise the expression skipper will
2842 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2843 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002844 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002845 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002846 }
2847 }
2848
Alp Toker383d2c42014-01-01 03:08:43 +00002849 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002850 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00002851 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00002852 }
2853
2854 ExprResult ValueExpr(ParseAssignmentExpression());
2855 if (ValueExpr.isInvalid()) {
2856 // We must manually skip to a '}', otherwise the expression skipper will
2857 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2858 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002859 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002860 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002861 }
2862
2863 // Parse the ellipsis that designates this as a pack expansion.
2864 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00002865 if (getLangOpts().CPlusPlus)
2866 TryConsumeToken(tok::ellipsis, EllipsisLoc);
2867
Ted Kremeneke65b0862012-03-06 20:05:56 +00002868 // We have a valid expression. Collect it in a vector so we can
2869 // build the argument list.
2870 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00002871 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00002872 };
2873 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00002874
2875 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002876 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
2877 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002878 }
2879 SourceLocation EndLoc = ConsumeBrace();
2880
2881 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00002882 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2883 Elements.data(), Elements.size());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002884}
2885
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002886/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002887/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002888ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002889Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002890 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002891
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002892 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002893
Chris Lattner197a3012008-08-05 06:19:09 +00002894 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002895 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2896
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002897 BalancedDelimiterTracker T(*this, tok::l_paren);
2898 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002899
Douglas Gregor220cac52009-02-18 17:45:20 +00002900 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002901
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002902 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002903
Douglas Gregor220cac52009-02-18 17:45:20 +00002904 if (Ty.isInvalid())
2905 return ExprError();
2906
Nico Webera7c7e602012-12-31 00:28:03 +00002907 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
2908 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002909}
Anders Carlssone01493d2007-08-23 15:25:28 +00002910
2911/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00002912/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002913ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002914Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002915 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002916
Chris Lattner197a3012008-08-05 06:19:09 +00002917 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002918 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2919
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002920 BalancedDelimiterTracker T(*this, tok::l_paren);
2921 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002922
Chris Lattner197a3012008-08-05 06:19:09 +00002923 if (Tok.isNot(tok::identifier))
Alp Tokerec543272013-12-24 09:48:30 +00002924 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002925
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002926 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00002927 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002928
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002929 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00002930
Nico Webera7c7e602012-12-31 00:28:03 +00002931 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2932 T.getOpenLocation(), ProtoIdLoc,
2933 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00002934}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002935
2936/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00002937/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002938ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002939 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002940
Chris Lattner197a3012008-08-05 06:19:09 +00002941 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002942 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2943
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002944 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002945 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002946
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002947 BalancedDelimiterTracker T(*this, tok::l_paren);
2948 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00002949 bool HasOptionalParen = Tok.is(tok::l_paren);
2950 if (HasOptionalParen)
2951 ConsumeParen();
2952
Douglas Gregor67c692c2010-08-26 15:07:07 +00002953 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002954 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002955 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002956 return ExprError();
2957 }
2958
Chris Lattner4f472a32009-04-11 18:13:45 +00002959 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002960 if (!SelIdent && // missing selector name.
2961 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00002962 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002963
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002964 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00002965
Steve Naroff152dd812007-12-05 22:21:29 +00002966 unsigned nColons = 0;
2967 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002968 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00002969 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00002970 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00002971 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00002972 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
2973 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00002974 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00002975
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002976 if (Tok.is(tok::r_paren))
2977 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002978
2979 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002980 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002981 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00002982 return ExprError();
2983 }
2984
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002985 // Check for another keyword selector.
2986 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002987 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002988 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00002989 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002990 break;
2991 }
Steve Naroff152dd812007-12-05 22:21:29 +00002992 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00002993 if (HasOptionalParen && Tok.is(tok::r_paren))
2994 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002995 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00002996 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00002997 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2998 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00002999 T.getCloseLocation(),
3000 !HasOptionalParen);
Gabor Greif24032f12007-10-19 15:38:32 +00003001 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003002
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003003void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3004 // MCDecl might be null due to error in method or c-function prototype, etc.
3005 Decl *MCDecl = LM.D;
3006 bool skip = MCDecl &&
3007 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3008 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3009 if (skip)
3010 return;
3011
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003012 // Save the current token position.
3013 SourceLocation OrigLoc = Tok.getLocation();
3014
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003015 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
3016 // Append the current token at the end of the new token stream so that it
3017 // doesn't get lost.
3018 LM.Toks.push_back(Tok);
3019 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
3020
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003021 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003022 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003023
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003024 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3025 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003026 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003027 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003028 parseMethod
3029 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
3030 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003031
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003032 // Tell the actions module that we have entered a method or c-function definition
3033 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003034 if (parseMethod)
3035 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3036 else
3037 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003038 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003039 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003040 else {
3041 if (Tok.is(tok::colon))
3042 ParseConstructorInitializer(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003043 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003044 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00003045
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003046 if (Tok.getLocation() != OrigLoc) {
3047 // Due to parsing error, we either went over the cached tokens or
3048 // there are still cached tokens left. If it's the latter case skip the
3049 // leftover tokens.
3050 // Since this is an uncommon situation that should be avoided, use the
3051 // expensive isBeforeInTranslationUnit call.
3052 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3053 OrigLoc))
3054 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3055 ConsumeAnyToken();
3056 }
3057
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003058 return;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003059}