blob: 83236602df75667d35da85134924d35a71260c76 [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 +0000311IdentifierInfo *Parser::getNullabilityKeyword(NullabilityKind nullability) {
312 switch (nullability) {
313 case NullabilityKind::NonNull:
314 if (!Ident___nonnull)
315 Ident___nonnull = PP.getIdentifierInfo("__nonnull");
316 return Ident___nonnull;
317
318 case NullabilityKind::Nullable:
319 if (!Ident___nullable)
320 Ident___nullable = PP.getIdentifierInfo("__nullable");
321 return Ident___nullable;
322
323 case NullabilityKind::Unspecified:
324 if (!Ident___null_unspecified)
325 Ident___null_unspecified = PP.getIdentifierInfo("__null_unspecified");
326 return Ident___null_unspecified;
327 }
328}
329
330/// Add an attribute for a context-sensitive type nullability to the given
331/// declarator.
332static void addContextSensitiveTypeNullability(Parser &P,
333 Declarator &D,
334 NullabilityKind nullability,
335 SourceLocation nullabilityLoc,
336 bool &addedToDeclSpec) {
337 // Create the attribute.
338 auto getNullabilityAttr = [&]() -> AttributeList * {
339 auto attr = D.getAttributePool().create(
340 P.getNullabilityKeyword(nullability),
341 SourceRange(nullabilityLoc),
342 nullptr, SourceLocation(),
343 nullptr, 0,
344 AttributeList::AS_Keyword);
345 attr->setContextSensitiveKeywordAttribute();
346 return attr;
347 };
348
349 if (D.getNumTypeObjects() > 0) {
350 // Add the attribute to the declarator chunk nearest the declarator.
351 auto nullabilityAttr = getNullabilityAttr();
352 DeclaratorChunk &chunk = D.getTypeObject(0);
353 nullabilityAttr->setNext(chunk.getAttrListRef());
354 chunk.getAttrListRef() = nullabilityAttr;
355 } else if (!addedToDeclSpec) {
356 // Otherwise, just put it on the declaration specifiers (if one
357 // isn't there already).
358 D.getMutableDeclSpec().addAttributes(getNullabilityAttr());
359 addedToDeclSpec = true;
360 }
361}
362
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000363/// objc-interface-decl-list:
364/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000365/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000366/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000367/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000368/// objc-interface-decl-list declaration
369/// objc-interface-decl-list ';'
370///
Steve Naroff99264b42007-08-22 16:35:03 +0000371/// objc-method-requirement: [OBJC2]
372/// @required
373/// @optional
374///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000375void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
376 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000377 SmallVector<Decl *, 32> allMethods;
378 SmallVector<Decl *, 16> allProperties;
379 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000380 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000381
Ted Kremenekc7c64312010-01-07 01:20:12 +0000382 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000383
Steve Naroff99264b42007-08-22 16:35:03 +0000384 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000385 // If this is a method prototype, parse it.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000386 if (Tok.isOneOf(tok::minus, tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000387 if (Decl *methodPrototype =
388 ParseObjCMethodPrototype(MethodImplKind, false))
389 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000390 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
391 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000392 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
393 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000394 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000395 if (Tok.is(tok::semi))
396 ConsumeToken();
397 }
Steve Naroff99264b42007-08-22 16:35:03 +0000398 continue;
399 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000400 if (Tok.is(tok::l_paren)) {
401 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000402 ParseObjCMethodDecl(Tok.getLocation(),
403 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000404 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000405 continue;
406 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000407 // Ignore excess semicolons.
408 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000409 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000410 continue;
411 }
Mike Stump11289f42009-09-09 15:08:12 +0000412
Chris Lattnerda9fb152008-10-20 06:10:06 +0000413 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000414 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000415 break;
Mike Stump11289f42009-09-09 15:08:12 +0000416
Douglas Gregorf1934162010-01-13 21:24:21 +0000417 // Code completion within an Objective-C interface.
418 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000419 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000420 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000421 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000422 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000423 }
424
Chris Lattner038a3e32008-10-20 05:46:22 +0000425 // If we don't have an @ directive, parse it as a function definition.
426 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000427 // The code below does not consume '}'s because it is afraid of eating the
428 // end of a namespace. Because of the way this code is structured, an
429 // erroneous r_brace would cause an infinite loop if not handled here.
430 if (Tok.is(tok::r_brace))
431 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000432 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000433 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000434 continue;
435 }
Mike Stump11289f42009-09-09 15:08:12 +0000436
Chris Lattner038a3e32008-10-20 05:46:22 +0000437 // Otherwise, we have an @ directive, eat the @.
438 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000439 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000440 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000441 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000442 }
443
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000444 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000445
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000446 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000447 AtEnd.setBegin(AtLoc);
448 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000449 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000450 } else if (DirectiveKind == tok::objc_not_keyword) {
451 Diag(Tok, diag::err_objc_unknown_at);
452 SkipUntil(tok::semi);
453 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000454 }
Mike Stump11289f42009-09-09 15:08:12 +0000455
Chris Lattnerda9fb152008-10-20 06:10:06 +0000456 // Eat the identifier.
457 ConsumeToken();
458
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000459 switch (DirectiveKind) {
460 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000461 // FIXME: If someone forgets an @end on a protocol, this loop will
462 // continue to eat up tons of stuff and spew lots of nonsense errors. It
463 // would probably be better to bail out if we saw an @class or @interface
464 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000465 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000466 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000467 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000468 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000469
470 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000471 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000472 Diag(AtLoc, diag::err_objc_missing_end)
473 << FixItHint::CreateInsertion(AtLoc, "@end\n");
474 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
475 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000476 ConsumeToken();
477 break;
478
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000479 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000480 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000481 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000482 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000483 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000484 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000485 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000486 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000487 break;
Mike Stump11289f42009-09-09 15:08:12 +0000488
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000489 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000490 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000491 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000492
Chris Lattner038a3e32008-10-20 05:46:22 +0000493 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000494 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000495 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000496 if (Tok.is(tok::l_paren)) {
497 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000498 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000499 }
Mike Stump11289f42009-09-09 15:08:12 +0000500
Douglas Gregor813a0662015-06-19 18:14:38 +0000501 bool addedToDeclSpec = false;
Benjamin Kramera39beb92014-09-03 11:06:10 +0000502 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
503 if (FD.D.getIdentifier() == nullptr) {
504 Diag(AtLoc, diag::err_objc_property_requires_field_name)
505 << FD.D.getSourceRange();
506 return;
507 }
508 if (FD.BitfieldSize) {
509 Diag(AtLoc, diag::err_objc_property_bitfield)
510 << FD.D.getSourceRange();
511 return;
512 }
513
Douglas Gregor813a0662015-06-19 18:14:38 +0000514 // Map a nullability property attribute to a context-sensitive keyword
515 // attribute.
516 if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
517 addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
518 OCDS.getNullabilityLoc(),
519 addedToDeclSpec);
520
Benjamin Kramera39beb92014-09-03 11:06:10 +0000521 // Install the property declarator into interfaceDecl.
522 IdentifierInfo *SelName =
523 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
524
525 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
526 IdentifierInfo *SetterName = OCDS.getSetterName();
527 Selector SetterSel;
528 if (SetterName)
529 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
530 else
531 SetterSel = SelectorTable::constructSetterSelector(
532 PP.getIdentifierTable(), PP.getSelectorTable(),
533 FD.D.getIdentifier());
534 bool isOverridingProperty = false;
535 Decl *Property = Actions.ActOnProperty(
536 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
537 &isOverridingProperty, MethodImplKind);
538 if (!isOverridingProperty)
539 allProperties.push_back(Property);
540
541 FD.complete(Property);
542 };
John McCallcfefb6d2009-11-03 02:38:08 +0000543
Chris Lattner038a3e32008-10-20 05:46:22 +0000544 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000545 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000546 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000547
John McCall405988b2011-03-26 01:53:26 +0000548 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000549 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000550 }
Steve Naroff99264b42007-08-22 16:35:03 +0000551 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000552
553 // We break out of the big loop in two cases: when we see @end or when we see
554 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000555 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000556 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000557 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000558 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000559 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000560 } else {
561 Diag(Tok, diag::err_objc_missing_end)
562 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
563 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
564 << (int) Actions.getObjCContainerKind();
565 AtEnd.setBegin(Tok.getLocation());
566 AtEnd.setEnd(Tok.getLocation());
567 }
Mike Stump11289f42009-09-09 15:08:12 +0000568
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000569 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000570 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000571 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000572}
573
Douglas Gregor813a0662015-06-19 18:14:38 +0000574/// Diagnose redundant or conflicting nullability information.
575static void diagnoseRedundantPropertyNullability(Parser &P,
576 ObjCDeclSpec &DS,
577 NullabilityKind nullability,
578 SourceLocation nullabilityLoc){
579 if (DS.getNullability() == nullability) {
580 P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
581 << static_cast<unsigned>(nullability) << true
582 << SourceRange(DS.getNullabilityLoc());
583 return;
584 }
585
586 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
587 << static_cast<unsigned>(nullability) << true
588 << static_cast<unsigned>(DS.getNullability()) << true
589 << SourceRange(DS.getNullabilityLoc());
590}
591
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000592/// Parse property attribute declarations.
593///
594/// property-attr-decl: '(' property-attrlist ')'
595/// property-attrlist:
596/// property-attribute
597/// property-attrlist ',' property-attribute
598/// property-attribute:
599/// getter '=' identifier
600/// setter '=' identifier ':'
601/// readonly
602/// readwrite
603/// assign
604/// retain
605/// copy
606/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000607/// atomic
608/// strong
609/// weak
610/// unsafe_unretained
Douglas Gregor813a0662015-06-19 18:14:38 +0000611/// nonnull
612/// nullable
613/// null_unspecified
Douglas Gregor849ebc22015-06-19 18:14:46 +0000614/// null_resettable
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000615///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000616void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000617 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000618 BalancedDelimiterTracker T(*this, tok::l_paren);
619 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000620
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000621 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000622 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000623 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000624 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000625 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000626 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000627
Chris Lattner76619232008-10-20 07:22:18 +0000628 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000629 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000630 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000631 return;
632 }
Mike Stump11289f42009-09-09 15:08:12 +0000633
Chris Lattner1db33542008-10-20 07:37:22 +0000634 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000635
Chris Lattner68e48682008-11-20 04:42:34 +0000636 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000637 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000638 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000639 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000640 else if (II->isStr("unsafe_unretained"))
641 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000642 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000643 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000644 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000645 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000646 else if (II->isStr("strong"))
647 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000648 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000649 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000650 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000651 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000652 else if (II->isStr("atomic"))
653 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000654 else if (II->isStr("weak"))
655 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000656 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000657 bool IsSetter = II->getNameStart()[0] == 's';
658
Chris Lattner825bca12008-10-20 07:39:53 +0000659 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000660 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
661 diag::err_objc_expected_equal_for_getter;
662
Alp Toker383d2c42014-01-01 03:08:43 +0000663 if (ExpectAndConsume(tok::equal, DiagID)) {
664 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000665 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000666 }
Mike Stump11289f42009-09-09 15:08:12 +0000667
Douglas Gregorc8537c52009-11-19 07:41:15 +0000668 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000669 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000670 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000671 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000672 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000673 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000674 }
675
Anders Carlssonfe15a782010-10-02 17:45:21 +0000676
677 SourceLocation SelLoc;
678 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
679
680 if (!SelIdent) {
681 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
682 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000683 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000684 return;
685 }
Mike Stump11289f42009-09-09 15:08:12 +0000686
Anders Carlssonfe15a782010-10-02 17:45:21 +0000687 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000688 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000689 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000690
Alp Toker383d2c42014-01-01 03:08:43 +0000691 if (ExpectAndConsume(tok::colon,
692 diag::err_expected_colon_after_setter_name)) {
693 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000694 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000695 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000696 } else {
697 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000698 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000699 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000700 } else if (II->isStr("nonnull")) {
701 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
702 diagnoseRedundantPropertyNullability(*this, DS,
703 NullabilityKind::NonNull,
704 Tok.getLocation());
705 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
706 DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
707 } else if (II->isStr("nullable")) {
708 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
709 diagnoseRedundantPropertyNullability(*this, DS,
710 NullabilityKind::Nullable,
711 Tok.getLocation());
712 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
713 DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
714 } else if (II->isStr("null_unspecified")) {
715 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
716 diagnoseRedundantPropertyNullability(*this, DS,
717 NullabilityKind::Unspecified,
718 Tok.getLocation());
719 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
720 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000721 } else if (II->isStr("null_resettable")) {
722 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
723 diagnoseRedundantPropertyNullability(*this, DS,
724 NullabilityKind::Unspecified,
725 Tok.getLocation());
726 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
727 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
728
729 // Also set the null_resettable bit.
730 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
Chris Lattner825bca12008-10-20 07:39:53 +0000731 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000732 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000733 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000734 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000735 }
Mike Stump11289f42009-09-09 15:08:12 +0000736
Chris Lattner1db33542008-10-20 07:37:22 +0000737 if (Tok.isNot(tok::comma))
738 break;
Mike Stump11289f42009-09-09 15:08:12 +0000739
Chris Lattner1db33542008-10-20 07:37:22 +0000740 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000741 }
Mike Stump11289f42009-09-09 15:08:12 +0000742
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000743 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000744}
745
Steve Naroff09bf8152007-09-06 21:24:23 +0000746/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000747/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000748/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000749///
750/// objc-instance-method: '-'
751/// objc-class-method: '+'
752///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000753/// objc-method-attributes: [OBJC2]
754/// __attribute__((deprecated))
755///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000756Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000757 bool MethodDefinition) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000758 assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000759
Mike Stump11289f42009-09-09 15:08:12 +0000760 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000761 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000762 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000763 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000764 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000765 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000766 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000767}
768
769/// objc-selector:
770/// identifier
771/// one of
772/// enum struct union if else while do for switch case default
773/// break continue return goto asm sizeof typeof __alignof
774/// unsigned long const short volatile signed restrict _Complex
775/// in out inout bycopy byref oneway int char float double void _Bool
776///
Chris Lattner4f472a32009-04-11 18:13:45 +0000777IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000778
Chris Lattner5700fab2007-10-07 02:00:24 +0000779 switch (Tok.getKind()) {
780 default:
Craig Topper161e4db2014-05-21 06:02:52 +0000781 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000782 case tok::ampamp:
783 case tok::ampequal:
784 case tok::amp:
785 case tok::pipe:
786 case tok::tilde:
787 case tok::exclaim:
788 case tok::exclaimequal:
789 case tok::pipepipe:
790 case tok::pipeequal:
791 case tok::caret:
792 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000793 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +0000794 if (isLetter(ThisTok[0])) {
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000795 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
796 Tok.setKind(tok::identifier);
797 SelectorLoc = ConsumeToken();
798 return II;
799 }
Craig Topper161e4db2014-05-21 06:02:52 +0000800 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000801 }
802
Chris Lattner5700fab2007-10-07 02:00:24 +0000803 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000804 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000805 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000806 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000807 case tok::kw_break:
808 case tok::kw_case:
809 case tok::kw_catch:
810 case tok::kw_char:
811 case tok::kw_class:
812 case tok::kw_const:
813 case tok::kw_const_cast:
814 case tok::kw_continue:
815 case tok::kw_default:
816 case tok::kw_delete:
817 case tok::kw_do:
818 case tok::kw_double:
819 case tok::kw_dynamic_cast:
820 case tok::kw_else:
821 case tok::kw_enum:
822 case tok::kw_explicit:
823 case tok::kw_export:
824 case tok::kw_extern:
825 case tok::kw_false:
826 case tok::kw_float:
827 case tok::kw_for:
828 case tok::kw_friend:
829 case tok::kw_goto:
830 case tok::kw_if:
831 case tok::kw_inline:
832 case tok::kw_int:
833 case tok::kw_long:
834 case tok::kw_mutable:
835 case tok::kw_namespace:
836 case tok::kw_new:
837 case tok::kw_operator:
838 case tok::kw_private:
839 case tok::kw_protected:
840 case tok::kw_public:
841 case tok::kw_register:
842 case tok::kw_reinterpret_cast:
843 case tok::kw_restrict:
844 case tok::kw_return:
845 case tok::kw_short:
846 case tok::kw_signed:
847 case tok::kw_sizeof:
848 case tok::kw_static:
849 case tok::kw_static_cast:
850 case tok::kw_struct:
851 case tok::kw_switch:
852 case tok::kw_template:
853 case tok::kw_this:
854 case tok::kw_throw:
855 case tok::kw_true:
856 case tok::kw_try:
857 case tok::kw_typedef:
858 case tok::kw_typeid:
859 case tok::kw_typename:
860 case tok::kw_typeof:
861 case tok::kw_union:
862 case tok::kw_unsigned:
863 case tok::kw_using:
864 case tok::kw_virtual:
865 case tok::kw_void:
866 case tok::kw_volatile:
867 case tok::kw_wchar_t:
868 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000869 case tok::kw__Bool:
870 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000871 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000872 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000873 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000874 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000875 }
Steve Naroff99264b42007-08-22 16:35:03 +0000876}
877
Fariborz Jahanian83615522008-01-02 22:54:34 +0000878/// objc-for-collection-in: 'in'
879///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000880bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000881 // FIXME: May have to do additional look-ahead to only allow for
882 // valid tokens following an 'in'; such as an identifier, unary operators,
883 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000884 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000885 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000886}
887
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000888/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000889/// qualifier list and builds their bitmask representation in the input
890/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000891///
892/// objc-type-qualifiers:
893/// objc-type-qualifier
894/// objc-type-qualifiers objc-type-qualifier
895///
Douglas Gregor813a0662015-06-19 18:14:38 +0000896/// objc-type-qualifier:
897/// 'in'
898/// 'out'
899/// 'inout'
900/// 'oneway'
901/// 'bycopy'
902/// 'byref'
903/// 'nonnull'
904/// 'nullable'
905/// 'null_unspecified'
906///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000907void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +0000908 Declarator::TheContext Context) {
909 assert(Context == Declarator::ObjCParameterContext ||
910 Context == Declarator::ObjCResultContext);
911
Chris Lattner61511e12007-12-12 06:56:32 +0000912 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000913 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000914 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +0000915 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000916 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +0000917 }
918
Chris Lattner5e530bc2007-12-27 19:57:00 +0000919 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000920 return;
Mike Stump11289f42009-09-09 15:08:12 +0000921
Chris Lattner61511e12007-12-12 06:56:32 +0000922 const IdentifierInfo *II = Tok.getIdentifierInfo();
923 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Douglas Gregor813a0662015-06-19 18:14:38 +0000924 if (II != ObjCTypeQuals[i] ||
925 NextToken().is(tok::less) ||
926 NextToken().is(tok::coloncolon))
Chris Lattner61511e12007-12-12 06:56:32 +0000927 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000928
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000929 ObjCDeclSpec::ObjCDeclQualifier Qual;
Douglas Gregor813a0662015-06-19 18:14:38 +0000930 NullabilityKind Nullability;
Chris Lattner61511e12007-12-12 06:56:32 +0000931 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +0000932 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000933 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
934 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
935 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
936 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
937 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
938 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Douglas Gregor813a0662015-06-19 18:14:38 +0000939
940 case objc_nonnull:
941 Qual = ObjCDeclSpec::DQ_CSNullability;
942 Nullability = NullabilityKind::NonNull;
943 break;
944
945 case objc_nullable:
946 Qual = ObjCDeclSpec::DQ_CSNullability;
947 Nullability = NullabilityKind::Nullable;
948 break;
949
950 case objc_null_unspecified:
951 Qual = ObjCDeclSpec::DQ_CSNullability;
952 Nullability = NullabilityKind::Unspecified;
953 break;
Chris Lattner61511e12007-12-12 06:56:32 +0000954 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000955
956 // FIXME: Diagnose redundant specifiers.
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000957 DS.setObjCDeclQualifier(Qual);
Douglas Gregor813a0662015-06-19 18:14:38 +0000958 if (Qual == ObjCDeclSpec::DQ_CSNullability)
959 DS.setNullability(Tok.getLocation(), Nullability);
960
Chris Lattner61511e12007-12-12 06:56:32 +0000961 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +0000962 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +0000963 break;
964 }
Mike Stump11289f42009-09-09 15:08:12 +0000965
Chris Lattner61511e12007-12-12 06:56:32 +0000966 // If this wasn't a recognized qualifier, bail out.
967 if (II) return;
968 }
969}
970
John McCalla55902b2011-10-01 09:56:14 +0000971/// Take all the decl attributes out of the given list and add
972/// them to the given attribute set.
973static void takeDeclAttributes(ParsedAttributes &attrs,
974 AttributeList *list) {
975 while (list) {
976 AttributeList *cur = list;
977 list = cur->getNext();
978
979 if (!cur->isUsedAsTypeAttr()) {
980 // Clear out the next pointer. We're really completely
981 // destroying the internal invariants of the declarator here,
982 // but it doesn't matter because we're done with it.
Craig Topper161e4db2014-05-21 06:02:52 +0000983 cur->setNext(nullptr);
John McCalla55902b2011-10-01 09:56:14 +0000984 attrs.add(cur);
985 }
986 }
987}
988
989/// takeDeclAttributes - Take all the decl attributes from the given
990/// declarator and add them to the given list.
991static void takeDeclAttributes(ParsedAttributes &attrs,
992 Declarator &D) {
993 // First, take ownership of all attributes.
994 attrs.getPool().takeAllFrom(D.getAttributePool());
995 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
996
997 // Now actually move the attributes over.
998 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
999 takeDeclAttributes(attrs, D.getAttributes());
1000 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
1001 takeDeclAttributes(attrs,
1002 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
1003}
1004
Chris Lattner61511e12007-12-12 06:56:32 +00001005/// objc-type-name:
1006/// '(' objc-type-qualifiers[opt] type-name ')'
1007/// '(' objc-type-qualifiers[opt] ')'
1008///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001009ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001010 Declarator::TheContext context,
1011 ParsedAttributes *paramAttrs) {
1012 assert(context == Declarator::ObjCParameterContext ||
1013 context == Declarator::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +00001014 assert((paramAttrs != nullptr) ==
1015 (context == Declarator::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +00001016
Chris Lattner0ef13522007-10-09 17:51:17 +00001017 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +00001018
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001019 BalancedDelimiterTracker T(*this, tok::l_paren);
1020 T.consumeOpen();
1021
Chris Lattner2ebb1782008-08-23 01:48:03 +00001022 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001023 ObjCDeclContextSwitch ObjCDC(*this);
1024
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001025 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001026 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +00001027
John McCallba7bf592010-08-24 05:47:05 +00001028 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +00001029 if (isTypeSpecifierQualifier()) {
John McCalla55902b2011-10-01 09:56:14 +00001030 // Parse an abstract declarator.
1031 DeclSpec declSpec(AttrFactory);
1032 declSpec.setObjCQualifiers(&DS);
1033 ParseSpecifierQualifierList(declSpec);
Fariborz Jahanianb6499eb2012-05-29 21:52:45 +00001034 declSpec.SetRangeEnd(Tok.getLocation());
John McCalla55902b2011-10-01 09:56:14 +00001035 Declarator declarator(declSpec, context);
1036 ParseDeclarator(declarator);
1037
1038 // If that's not invalid, extract a type.
1039 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001040 // Map a nullability specifier to a context-sensitive keyword attribute.
1041 bool addedToDeclSpec = false;
1042 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1043 addContextSensitiveTypeNullability(*this, declarator,
1044 DS.getNullability(),
1045 DS.getNullabilityLoc(),
1046 addedToDeclSpec);
1047
John McCalla55902b2011-10-01 09:56:14 +00001048 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1049 if (!type.isInvalid())
1050 Ty = type.get();
1051
1052 // If we're parsing a parameter, steal all the decl attributes
1053 // and add them to the decl spec.
1054 if (context == Declarator::ObjCParameterContext)
1055 takeDeclAttributes(*paramAttrs, declarator);
1056 }
1057 } else if (context == Declarator::ObjCResultContext &&
1058 Tok.is(tok::identifier)) {
Douglas Gregorbab8a962011-09-08 01:46:34 +00001059 if (!Ident_instancetype)
1060 Ident_instancetype = PP.getIdentifierInfo("instancetype");
1061
1062 if (Tok.getIdentifierInfo() == Ident_instancetype) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001063 SourceLocation loc = ConsumeToken();
1064 Ty = Actions.ActOnObjCInstanceType(loc);
1065
1066 // Map a nullability specifier to a context-sensitive keyword attribute.
1067 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability) {
1068 // Synthesize an abstract declarator so we can use Sema::ActOnTypeName.
1069 bool addedToDeclSpec = false;
1070 const char *prevSpec;
1071 unsigned diagID;
1072 DeclSpec declSpec(AttrFactory);
1073 declSpec.setObjCQualifiers(&DS);
1074 declSpec.SetTypeSpecType(DeclSpec::TST_typename, loc, prevSpec, diagID,
1075 Ty,
1076 Actions.getASTContext().getPrintingPolicy());
1077 declSpec.SetRangeEnd(loc);
1078 Declarator declarator(declSpec, context);
1079
1080 // Add the context-sensitive keyword attribute.
1081 addContextSensitiveTypeNullability(*this, declarator,
1082 DS.getNullability(),
1083 DS.getNullabilityLoc(),
1084 addedToDeclSpec);
1085
1086
1087 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1088 if (!type.isInvalid())
1089 Ty = type.get();
1090 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001091 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001092 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001093
Steve Naroff90255b42008-10-21 14:15:04 +00001094 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001095 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001096 else if (Tok.getLocation() == TypeStartLoc) {
1097 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001098 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001099 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001100 } else {
1101 // Otherwise, we found *something*, but didn't get a ')' in the right
1102 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001103 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001104 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001105 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001106}
1107
1108/// objc-method-decl:
1109/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001110/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001111/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001112/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001113///
1114/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001115/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001116/// objc-keyword-selector objc-keyword-decl
1117///
1118/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001119/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1120/// objc-selector ':' objc-keyword-attributes[opt] identifier
1121/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1122/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001123///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001124/// objc-parmlist:
1125/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001126///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001127/// objc-parms:
1128/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001129///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001130/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001131/// , ...
1132///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001133/// objc-keyword-attributes: [OBJC2]
1134/// __attribute__((unused))
1135///
John McCall48871652010-08-21 09:40:31 +00001136Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001137 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001138 tok::ObjCKeywordKind MethodImplKind,
1139 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001140 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001141
Douglas Gregor636a61e2010-04-07 00:21:17 +00001142 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001143 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001144 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001145 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001146 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001147 }
1148
Chris Lattner2ebb1782008-08-23 01:48:03 +00001149 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001150 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001151 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001152 if (Tok.is(tok::l_paren))
Craig Topper161e4db2014-05-21 06:02:52 +00001153 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext,
1154 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001155
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001156 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001157 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001158 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001159 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001160
Douglas Gregor636a61e2010-04-07 00:21:17 +00001161 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001162 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001163 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001164 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001165 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001166 }
1167
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001168 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001169 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001170 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001171
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001172 // An unnamed colon is valid.
1173 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001174 Diag(Tok, diag::err_expected_selector_for_method)
1175 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001176 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001177 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001178 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001179 }
Mike Stump11289f42009-09-09 15:08:12 +00001180
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001181 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001182 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001183 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001184 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001185 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001186
Chris Lattner5700fab2007-10-07 02:00:24 +00001187 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001188 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001189 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001190 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001191 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001192 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001193 methodAttrs.getList(), MethodImplKind,
1194 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001195 PD.complete(Result);
1196 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001197 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001198
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001199 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001200 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001201 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001202 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1203 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001204
1205 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001206 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001207 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001208 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001209
Chris Lattner5700fab2007-10-07 02:00:24 +00001210 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001211 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001212 break;
Mike Stump11289f42009-09-09 15:08:12 +00001213
John McCallba7bf592010-08-24 05:47:05 +00001214 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001215 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001216 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1217 Declarator::ObjCParameterContext,
1218 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001219
Chris Lattner5700fab2007-10-07 02:00:24 +00001220 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001221 // Regardless, collect all the attributes we've parsed so far.
Craig Topper161e4db2014-05-21 06:02:52 +00001222 ArgInfo.ArgAttrs = nullptr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001223 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001224 MaybeParseGNUAttributes(paramAttrs);
1225 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001226 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001227
Douglas Gregor45879692010-07-08 23:37:41 +00001228 // Code completion for the next piece of the selector.
1229 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001230 KeyIdents.push_back(SelIdent);
1231 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1232 mType == tok::minus,
1233 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001234 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001235 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001236 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001237 }
1238
Chris Lattner0ef13522007-10-09 17:51:17 +00001239 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001240 Diag(Tok, diag::err_expected)
1241 << tok::identifier; // missing argument name.
Chris Lattner5700fab2007-10-07 02:00:24 +00001242 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001243 }
Mike Stump11289f42009-09-09 15:08:12 +00001244
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001245 ArgInfo.Name = Tok.getIdentifierInfo();
1246 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001247 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001248
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001249 ArgInfos.push_back(ArgInfo);
1250 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001251 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001252
John McCall084e83d2011-03-24 11:26:52 +00001253 // Make sure the attributes persist.
1254 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1255
Douglas Gregor95887f92010-07-08 23:20:03 +00001256 // Code completion for the next piece of the selector.
1257 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001258 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1259 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001260 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001261 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001262 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001263 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001264 }
1265
Chris Lattner5700fab2007-10-07 02:00:24 +00001266 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001267 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001268 if (!SelIdent && Tok.isNot(tok::colon))
1269 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001270 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001271 SourceLocation ColonLoc = Tok.getLocation();
1272 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001273 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1274 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1275 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001276 }
1277 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001278 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001279 }
Mike Stump11289f42009-09-09 15:08:12 +00001280
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001281 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001282 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001283 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001284 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001285 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001286 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001287 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001288 ConsumeToken();
1289 break;
1290 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001291 if (!cStyleParamWarned) {
1292 Diag(Tok, diag::warn_cstyle_param);
1293 cStyleParamWarned = true;
1294 }
John McCall084e83d2011-03-24 11:26:52 +00001295 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001296 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001297 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001298 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1299 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001300 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001301 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001302 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1303 ParmDecl.getIdentifierLoc(),
1304 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001305 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001306 }
Mike Stump11289f42009-09-09 15:08:12 +00001307
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001308 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001309 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001310 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001311 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001312
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001313 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001314 return nullptr;
1315
Chris Lattner5700fab2007-10-07 02:00:24 +00001316 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1317 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001318 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001319 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001320 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001321 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001322 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001323 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001324 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001325
John McCall28a6aea2009-11-04 02:18:39 +00001326 PD.complete(Result);
1327 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001328}
1329
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001330/// objc-protocol-refs:
1331/// '<' identifier-list '>'
1332///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001333bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001334ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1335 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001336 bool WarnOnDeclarations, bool ForObjCContainer,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001337 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001338 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001339
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001340 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001341
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001342 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001343
Chris Lattner3bbae002008-07-26 04:03:38 +00001344 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001345 if (Tok.is(tok::code_completion)) {
1346 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1347 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001348 cutOffParsing();
1349 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001350 }
1351
Chris Lattner3bbae002008-07-26 04:03:38 +00001352 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001353 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001354 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001355 return true;
1356 }
1357 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1358 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001359 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001360 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001361
Alp Toker383d2c42014-01-01 03:08:43 +00001362 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001363 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001364 }
Mike Stump11289f42009-09-09 15:08:12 +00001365
Chris Lattner3bbae002008-07-26 04:03:38 +00001366 // Consume the '>'.
Nico Weber7aa4a882012-12-14 18:22:38 +00001367 if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true))
Chris Lattner3bbae002008-07-26 04:03:38 +00001368 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001369
Chris Lattner3bbae002008-07-26 04:03:38 +00001370 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001371 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Chris Lattner3bbae002008-07-26 04:03:38 +00001372 &ProtocolIdents[0], ProtocolIdents.size(),
1373 Protocols);
1374 return false;
1375}
1376
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001377/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1378/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001379bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001380 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001381 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001382 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001383 SmallVector<Decl *, 8> ProtocolDecl;
1384 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001385 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001386 false,
Douglas Gregor3a001f42010-11-19 17:10:50 +00001387 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001388 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1389 ProtocolLocs.data(), LAngleLoc);
1390 if (EndProtoLoc.isValid())
1391 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001392 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001393}
1394
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001395void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1396 BalancedDelimiterTracker &T,
1397 SmallVectorImpl<Decl *> &AllIvarDecls,
1398 bool RBraceMissing) {
1399 if (!RBraceMissing)
1400 T.consumeClose();
1401
1402 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1403 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1404 Actions.ActOnObjCContainerFinishDefinition();
1405 // Call ActOnFields() even if we don't have any decls. This is useful
1406 // for code rewriting tools that need to be aware of the empty list.
1407 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1408 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001409 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001410}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001411
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001412/// objc-class-instance-variables:
1413/// '{' objc-instance-variable-decl-list[opt] '}'
1414///
1415/// objc-instance-variable-decl-list:
1416/// objc-visibility-spec
1417/// objc-instance-variable-decl ';'
1418/// ';'
1419/// objc-instance-variable-decl-list objc-visibility-spec
1420/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1421/// objc-instance-variable-decl-list ';'
1422///
1423/// objc-visibility-spec:
1424/// @private
1425/// @protected
1426/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001427/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001428///
1429/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001430/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001431///
John McCall48871652010-08-21 09:40:31 +00001432void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001433 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001434 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001435 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001436 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001437
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001438 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001439 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001440
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001441 BalancedDelimiterTracker T(*this, tok::l_brace);
1442 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001443 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001444 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001445 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001446
Steve Naroff00433d32007-08-21 21:17:12 +00001447 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001448 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001449 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001450 continue;
1451 }
Mike Stump11289f42009-09-09 15:08:12 +00001452
Steve Naroff00433d32007-08-21 21:17:12 +00001453 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001454 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001455 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001456 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001457 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001458 }
1459
Steve Naroff7c348172007-08-23 18:16:40 +00001460 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001461 case tok::objc_private:
1462 case tok::objc_public:
1463 case tok::objc_protected:
1464 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001465 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001466 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001467 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001468
1469 case tok::objc_end:
1470 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001471 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1472 Tok.setKind(tok::at);
1473 Tok.setLength(1);
1474 PP.EnterToken(Tok);
1475 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1476 T, AllIvarDecls, true);
1477 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001478
1479 default:
1480 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1481 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001482 }
1483 }
Mike Stump11289f42009-09-09 15:08:12 +00001484
Douglas Gregor48d46252010-01-13 21:54:15 +00001485 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001486 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001487 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001488 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001489 }
John McCallcfefb6d2009-11-03 02:38:08 +00001490
Benjamin Kramera39beb92014-09-03 11:06:10 +00001491 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1492 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1493 // Install the declarator into the interface decl.
1494 Decl *Field = Actions.ActOnIvar(
1495 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1496 FD.BitfieldSize, visibility);
1497 Actions.ActOnObjCContainerFinishDefinition();
1498 if (Field)
1499 AllIvarDecls.push_back(Field);
1500 FD.complete(Field);
1501 };
John McCallcfefb6d2009-11-03 02:38:08 +00001502
Chris Lattnera12405b2008-04-10 06:46:29 +00001503 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001504 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001505 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001506
Chris Lattner0ef13522007-10-09 17:51:17 +00001507 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001508 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001509 } else {
1510 Diag(Tok, diag::err_expected_semi_decl_list);
1511 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001512 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001513 }
1514 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001515 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1516 T, AllIvarDecls, false);
Steve Naroff00433d32007-08-21 21:17:12 +00001517 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001518}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001519
1520/// objc-protocol-declaration:
1521/// objc-protocol-definition
1522/// objc-protocol-forward-reference
1523///
1524/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00001525/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00001526/// objc-protocol-refs[opt]
1527/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00001528/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001529///
1530/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00001531/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001532///
James Dennett1355bd12012-06-11 06:19:40 +00001533/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001534/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001535/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001536Parser::DeclGroupPtrTy
1537Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1538 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001539 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001540 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1541 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001542
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001543 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001544 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001545 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00001546 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001547 }
1548
Nico Weber69a79142013-04-04 00:15:10 +00001549 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00001550
Chris Lattner0ef13522007-10-09 17:51:17 +00001551 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001552 Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00001553 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001554 }
1555 // Save the protocol name, then consume it.
1556 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1557 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001558
Alp Toker383d2c42014-01-01 03:08:43 +00001559 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001560 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001561 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001562 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001563 }
Mike Stump11289f42009-09-09 15:08:12 +00001564
Erik Verbruggenf9887852011-12-08 09:58:43 +00001565 CheckNestedObjCContexts(AtLoc);
1566
Chris Lattner0ef13522007-10-09 17:51:17 +00001567 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001568 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001569 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1570
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001571 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001572 while (1) {
1573 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001574 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001575 Diag(Tok, diag::err_expected) << tok::identifier;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001576 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00001577 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001578 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001579 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1580 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001581 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001582
Chris Lattner0ef13522007-10-09 17:51:17 +00001583 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001584 break;
1585 }
1586 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00001587 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00001588 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001589
Steve Naroff93eb5f12007-10-10 17:32:04 +00001590 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001591 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001592 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001593 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001594 }
Mike Stump11289f42009-09-09 15:08:12 +00001595
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001596 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001597 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001598
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001599 SmallVector<Decl *, 8> ProtocolRefs;
1600 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001601 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001602 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001603 LAngleLoc, EndProtoLoc))
Douglas Gregorf6102672012-01-01 21:23:57 +00001604 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001605
John McCall48871652010-08-21 09:40:31 +00001606 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001607 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001608 ProtocolRefs.data(),
1609 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001610 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001611 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001612
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001613 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00001614 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001615}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001616
1617/// objc-implementation:
1618/// objc-class-implementation-prologue
1619/// objc-category-implementation-prologue
1620///
1621/// objc-class-implementation-prologue:
1622/// @implementation identifier objc-superclass[opt]
1623/// objc-class-instance-variables[opt]
1624///
1625/// objc-category-implementation-prologue:
1626/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001627Parser::DeclGroupPtrTy
1628Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001629 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1630 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001631 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001632 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001633
Douglas Gregor49c22a72009-11-18 16:26:39 +00001634 // Code completion after '@implementation'.
1635 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001636 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001637 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001638 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001639 }
1640
Nico Weber69a79142013-04-04 00:15:10 +00001641 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00001642
Chris Lattner0ef13522007-10-09 17:51:17 +00001643 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001644 Diag(Tok, diag::err_expected)
1645 << tok::identifier; // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001646 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001647 }
1648 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001649 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001650 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00001651 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001652
1653 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001654 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001655 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001656 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001657 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001658
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001659 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001660 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001661 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001662 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001663 }
1664
Chris Lattner0ef13522007-10-09 17:51:17 +00001665 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001666 categoryId = Tok.getIdentifierInfo();
1667 categoryLoc = ConsumeToken();
1668 } else {
Alp Tokerec543272013-12-24 09:48:30 +00001669 Diag(Tok, diag::err_expected)
1670 << tok::identifier; // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001671 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001672 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001673 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001674 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001675 SkipUntil(tok::r_paren); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001676 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001677 }
1678 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00001679 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1680 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1681 AttributeFactory attr;
1682 DeclSpec DS(attr);
1683 (void)ParseObjCProtocolQualifiers(DS);
1684 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001685 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001686 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001687 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001688
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001689 } else {
1690 // We have a class implementation
1691 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001692 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00001693 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001694 // We have a super class
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001695 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001696 Diag(Tok, diag::err_expected)
1697 << tok::identifier; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001698 return DeclGroupPtrTy();
1699 }
1700 superClassId = Tok.getIdentifierInfo();
1701 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001702 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001703 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1704 AtLoc, nameId, nameLoc,
1705 superClassId, superClassLoc);
1706
1707 if (Tok.is(tok::l_brace)) // we have ivars
1708 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00001709 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1710 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1711 // try to recover.
1712 AttributeFactory attr;
1713 DeclSpec DS(attr);
1714 (void)ParseObjCProtocolQualifiers(DS);
1715 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001716 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001717 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001718
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001719 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001720
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001721 {
1722 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00001723 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001724 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001725 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001726 MaybeParseMicrosoftAttributes(attrs);
1727 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1728 DeclGroupRef DG = DGP.get();
1729 DeclsInGroup.append(DG.begin(), DG.end());
1730 }
1731 }
1732 }
1733
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001734 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001735}
Steve Naroff33a1e802007-10-29 21:38:07 +00001736
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001737Parser::DeclGroupPtrTy
1738Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001739 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1740 "ParseObjCAtEndDeclaration(): Expected @end");
1741 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001742 if (CurParsedObjCImpl)
1743 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001744 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00001745 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001746 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001747 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001748}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001749
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001750Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1751 if (!Finished) {
1752 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00001753 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001754 P.Diag(P.Tok, diag::err_objc_missing_end)
1755 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1756 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1757 << Sema::OCK_Implementation;
1758 }
1759 }
Craig Topper161e4db2014-05-21 06:02:52 +00001760 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001761 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001762}
1763
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001764void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1765 assert(!Finished);
1766 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1767 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001768 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1769 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001770
1771 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1772
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001773 if (HasCFunction)
1774 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1775 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1776 false/*c-functions*/);
1777
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001778 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001779 for (LateParsedObjCMethodContainer::iterator
1780 I = LateParsedObjCMethods.begin(),
1781 E = LateParsedObjCMethods.end(); I != E; ++I)
1782 delete *I;
1783 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001784
1785 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001786}
1787
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001788/// compatibility-alias-decl:
1789/// @compatibility_alias alias-name class-name ';'
1790///
John McCall48871652010-08-21 09:40:31 +00001791Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001792 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1793 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1794 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001795 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001796 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00001797 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001798 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001799 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1800 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
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;
Craig Topper161e4db2014-05-21 06:02:52 +00001803 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001804 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001805 IdentifierInfo *classId = Tok.getIdentifierInfo();
1806 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00001807 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00001808 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1809 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001810}
1811
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001812/// property-synthesis:
1813/// @synthesize property-ivar-list ';'
1814///
1815/// property-ivar-list:
1816/// property-ivar
1817/// property-ivar-list ',' property-ivar
1818///
1819/// property-ivar:
1820/// identifier
1821/// identifier '=' identifier
1822///
John McCall48871652010-08-21 09:40:31 +00001823Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001824 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00001825 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001826 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001827
Douglas Gregor88e72a02009-11-18 19:45:45 +00001828 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001829 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001830 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001831 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001832 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00001833 }
1834
Douglas Gregor88e72a02009-11-18 19:45:45 +00001835 if (Tok.isNot(tok::identifier)) {
1836 Diag(Tok, diag::err_synthesized_property_name);
1837 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00001838 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001839 }
Craig Topper161e4db2014-05-21 06:02:52 +00001840
1841 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001842 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1843 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001844 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00001845 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001846 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00001847 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001848 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001849 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001850 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00001851 }
1852
Chris Lattner0ef13522007-10-09 17:51:17 +00001853 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001854 Diag(Tok, diag::err_expected) << tok::identifier;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001855 break;
1856 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001857 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001858 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001859 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001860 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001861 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001862 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001863 break;
1864 ConsumeToken(); // consume ','
1865 }
Alp Toker383d2c42014-01-01 03:08:43 +00001866 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00001867 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001868}
1869
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001870/// property-dynamic:
1871/// @dynamic property-list
1872///
1873/// property-list:
1874/// identifier
1875/// property-list ',' identifier
1876///
John McCall48871652010-08-21 09:40:31 +00001877Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001878 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1879 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001880 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001881 while (true) {
1882 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001883 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001884 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001885 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001886 }
1887
1888 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001889 Diag(Tok, diag::err_expected) << tok::identifier;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001890 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00001891 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001892 }
1893
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001894 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1895 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001896 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Craig Topper161e4db2014-05-21 06:02:52 +00001897 propertyId, nullptr, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001898
Chris Lattner0ef13522007-10-09 17:51:17 +00001899 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001900 break;
1901 ConsumeToken(); // consume ','
1902 }
Alp Toker383d2c42014-01-01 03:08:43 +00001903 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00001904 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001905}
Mike Stump11289f42009-09-09 15:08:12 +00001906
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001907/// objc-throw-statement:
1908/// throw expression[opt];
1909///
John McCalldadc5752010-08-24 06:29:42 +00001910StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1911 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001912 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001913 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001914 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001915 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001916 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001917 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001918 }
1919 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001920 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00001921 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001922 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001923}
1924
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001925/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001926/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001927///
John McCalldadc5752010-08-24 06:29:42 +00001928StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001929Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001930 ConsumeToken(); // consume synchronized
1931 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001932 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001933 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001934 }
John McCalld9bb7432011-07-27 21:50:02 +00001935
1936 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001937 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00001938 ExprResult operand(ParseExpression());
1939
1940 if (Tok.is(tok::r_paren)) {
1941 ConsumeParen(); // ')'
1942 } else {
1943 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00001944 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00001945
1946 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001947 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001948 }
John McCalld9bb7432011-07-27 21:50:02 +00001949
1950 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001951 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00001952 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00001953 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001954 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001955 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001956
John McCalld9bb7432011-07-27 21:50:02 +00001957 // Check the @synchronized operand now.
1958 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001959 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001960
John McCalld9bb7432011-07-27 21:50:02 +00001961 // Parse the compound statement within a new scope.
1962 ParseScope bodyScope(this, Scope::DeclScope);
1963 StmtResult body(ParseCompoundStatementBody());
1964 bodyScope.Exit();
1965
1966 // If there was a semantic or parse error earlier with the
1967 // operand, fail now.
1968 if (operand.isInvalid())
1969 return StmtError();
1970
1971 if (body.isInvalid())
1972 body = Actions.ActOnNullStmt(Tok.getLocation());
1973
1974 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001975}
1976
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001977/// objc-try-catch-statement:
1978/// @try compound-statement objc-catch-list[opt]
1979/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1980///
1981/// objc-catch-list:
1982/// @catch ( parameter-declaration ) compound-statement
1983/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1984/// catch-parameter-declaration:
1985/// parameter-declaration
1986/// '...' [OBJC2]
1987///
John McCalldadc5752010-08-24 06:29:42 +00001988StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001989 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001990
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001991 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001992 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00001993 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001994 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001995 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00001996 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00001997 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001998 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001999 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002000 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002001 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002002 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002003
Chris Lattner0ef13522007-10-09 17:51:17 +00002004 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002005 // At this point, we need to lookahead to determine if this @ is the start
2006 // of an @catch or @finally. We don't want to consume the @ token if this
2007 // is an @try or @encode or something else.
2008 Token AfterAt = GetLookAheadToken(1);
2009 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2010 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2011 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002012
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002013 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002014 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002015 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002016 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002017 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002018 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00002019 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002020 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002021 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002022 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00002023 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002024 ParseDeclarator(ParmDecl);
2025
Douglas Gregore11ee112010-04-23 23:01:43 +00002026 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002027 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002028 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002029 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002030 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002031
Steve Naroff65a00892009-04-07 22:56:58 +00002032 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002033
Steve Naroff65a00892009-04-07 22:56:58 +00002034 if (Tok.is(tok::r_paren))
2035 RParenLoc = ConsumeParen();
2036 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002037 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002038
John McCalldadc5752010-08-24 06:29:42 +00002039 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002040 if (Tok.is(tok::l_brace))
2041 CatchBody = ParseCompoundStatementBody();
2042 else
Alp Tokerec543272013-12-24 09:48:30 +00002043 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002044 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002045 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00002046
John McCalldadc5752010-08-24 06:29:42 +00002047 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00002048 RParenLoc,
2049 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002050 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002051 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002052 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002053
Steve Naroffe6016792008-02-05 21:27:35 +00002054 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002055 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2056 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002057 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002058 }
2059 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002060 } else {
2061 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002062 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002063 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002064
John McCalldadc5752010-08-24 06:29:42 +00002065 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002066 if (Tok.is(tok::l_brace))
2067 FinallyBody = ParseCompoundStatementBody();
2068 else
Alp Tokerec543272013-12-24 09:48:30 +00002069 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002070 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002071 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002072 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002073 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002074 catch_or_finally_seen = true;
2075 break;
2076 }
2077 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002078 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002079 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002080 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002081 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002082
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002083 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002084 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002085 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002086}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002087
John McCall31168b02011-06-15 23:02:42 +00002088/// objc-autoreleasepool-statement:
2089/// @autoreleasepool compound-statement
2090///
2091StmtResult
2092Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2093 ConsumeToken(); // consume autoreleasepool
2094 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002095 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002096 return StmtError();
2097 }
2098 // Enter a scope to hold everything within the compound stmt. Compound
2099 // statements can always hold declarations.
2100 ParseScope BodyScope(this, Scope::DeclScope);
2101
2102 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2103
2104 BodyScope.Exit();
2105 if (AutoreleasePoolBody.isInvalid())
2106 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2107 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002108 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002109}
2110
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002111/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2112/// for later parsing.
2113void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
2114 LexedMethod* LM = new LexedMethod(this, MDecl);
2115 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2116 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002117 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002118 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002119 if (Tok.is(tok::kw_try)) {
2120 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002121 if (Tok.is(tok::colon)) {
2122 Toks.push_back(Tok);
2123 ConsumeToken();
2124 while (Tok.isNot(tok::l_brace)) {
2125 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2126 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2127 }
2128 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002129 Toks.push_back(Tok); // also store '{'
2130 }
2131 else if (Tok.is(tok::colon)) {
2132 ConsumeToken();
2133 while (Tok.isNot(tok::l_brace)) {
2134 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2135 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2136 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002137 Toks.push_back(Tok); // also store '{'
2138 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002139 ConsumeBrace();
2140 // Consume everything up to (and including) the matching right brace.
2141 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002142 while (Tok.is(tok::kw_catch)) {
2143 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2144 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2145 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002146}
2147
Steve Naroff09bf8152007-09-06 21:24:23 +00002148/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002149///
John McCall48871652010-08-21 09:40:31 +00002150Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002151 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002152
John McCallfaf5fb42010-08-26 23:41:50 +00002153 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2154 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002155
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002156 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002157 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002158 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002159 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002160 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002161 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002162 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002163 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002164
Steve Naroffbb875722007-11-11 19:54:21 +00002165 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002166 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002167 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002168
Steve Naroffbb875722007-11-11 19:54:21 +00002169 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002170 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002171
Steve Naroffbb875722007-11-11 19:54:21 +00002172 // If we didn't find the '{', bail out.
2173 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002174 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002175 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002176
2177 if (!MDecl) {
2178 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002179 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002180 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002181 }
2182
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002183 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002184 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002185 assert (CurParsedObjCImpl
2186 && "ParseObjCMethodDefinition - Method out of @implementation");
2187 // Consume the tokens and store them for later parsing.
2188 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002189 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002190}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002191
John McCalldadc5752010-08-24 06:29:42 +00002192StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002193 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002194 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002195 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002196 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002197 }
2198
2199 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002200 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002201
2202 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002203 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002204
2205 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002206 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002207
2208 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2209 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002210
2211 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2212 getLangOpts().DebuggerSupport) {
2213 SkipUntil(tok::semi);
2214 return Actions.ActOnNullStmt(Tok.getLocation());
2215 }
2216
John McCalldadc5752010-08-24 06:29:42 +00002217 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002218 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002219 // If the expression is invalid, skip ahead to the next semicolon. Not
2220 // doing this opens us up to the possibility of infinite loops if
2221 // ParseExpression does not consume any tokens.
2222 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002223 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002224 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002225
Steve Naroffe6016792008-02-05 21:27:35 +00002226 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002227 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002228 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002229}
2230
John McCalldadc5752010-08-24 06:29:42 +00002231ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002232 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002233 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002234 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002235 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002236 return ExprError();
2237
Ted Kremeneke65b0862012-03-06 20:05:56 +00002238 case tok::minus:
2239 case tok::plus: {
2240 tok::TokenKind Kind = Tok.getKind();
2241 SourceLocation OpLoc = ConsumeToken();
2242
2243 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002244 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002245 switch (Kind) {
2246 case tok::minus: Symbol = "-"; break;
2247 case tok::plus: Symbol = "+"; break;
2248 default: llvm_unreachable("missing unary operator case");
2249 }
2250 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2251 << Symbol;
2252 return ExprError();
2253 }
2254
2255 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2256 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002257 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002258 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002259 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002260
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002261 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002262 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002263 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002264
2265 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002266 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002267 }
2268
Chris Lattnere002fbe2007-12-12 01:04:12 +00002269 case tok::string_literal: // primary-expression: string-literal
2270 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002271 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002272
2273 case tok::char_constant:
2274 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2275
2276 case tok::numeric_constant:
2277 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2278
2279 case tok::kw_true: // Objective-C++, etc.
2280 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2281 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2282 case tok::kw_false: // Objective-C++, etc.
2283 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2284 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2285
2286 case tok::l_square:
2287 // Objective-C array literal
2288 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2289
2290 case tok::l_brace:
2291 // Objective-C dictionary literal
2292 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2293
Patrick Beard0caa3942012-04-19 00:25:12 +00002294 case tok::l_paren:
2295 // Objective-C boxed expression
2296 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2297
Chris Lattnere002fbe2007-12-12 01:04:12 +00002298 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002299 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002300 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002301
Chris Lattner197a3012008-08-05 06:19:09 +00002302 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2303 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002304 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002305 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002306 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002307 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002308 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002309 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002310 const char *str = nullptr;
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002311 if (GetLookAheadToken(1).is(tok::l_brace)) {
2312 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2313 str =
2314 ch == 't' ? "try"
2315 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002316 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002317 }
2318 if (str) {
2319 SourceLocation kwLoc = Tok.getLocation();
2320 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2321 FixItHint::CreateReplacement(kwLoc, str));
2322 }
2323 else
2324 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2325 }
Chris Lattner197a3012008-08-05 06:19:09 +00002326 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002327 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002328}
2329
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002330/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002331///
2332/// This routine parses the receiver of a message send in
2333/// Objective-C++ either as a type or as an expression. Note that this
2334/// routine must not be called to parse a send to 'super', since it
2335/// has no way to return such a result.
2336///
2337/// \param IsExpr Whether the receiver was parsed as an expression.
2338///
2339/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2340/// IsExpr is true), the parsed expression. If the receiver was parsed
2341/// as a type (\c IsExpr is false), the parsed type.
2342///
2343/// \returns True if an error occurred during parsing or semantic
2344/// analysis, in which case the arguments do not have valid
2345/// values. Otherwise, returns false for a successful parse.
2346///
2347/// objc-receiver: [C++]
2348/// 'super' [not parsed here]
2349/// expression
2350/// simple-type-specifier
2351/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002352bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002353 InMessageExpressionRAIIObject InMessage(*this, true);
2354
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002355 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2356 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002357 TryAnnotateTypeOrScopeToken();
2358
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002359 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002360 // objc-receiver:
2361 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002362 // Make sure any typos in the receiver are corrected or diagnosed, so that
2363 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2364 // only the things that are valid ObjC receivers?
2365 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002366 if (Receiver.isInvalid())
2367 return true;
2368
2369 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002370 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002371 return false;
2372 }
2373
2374 // objc-receiver:
2375 // typename-specifier
2376 // simple-type-specifier
2377 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002378 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002379 ParseCXXSimpleTypeSpecifier(DS);
2380
2381 if (Tok.is(tok::l_paren)) {
2382 // If we see an opening parentheses at this point, we are
2383 // actually parsing an expression that starts with a
2384 // function-style cast, e.g.,
2385 //
2386 // postfix-expression:
2387 // simple-type-specifier ( expression-list [opt] )
2388 // typename-specifier ( expression-list [opt] )
2389 //
2390 // Parse the remainder of this case, then the (optional)
2391 // postfix-expression suffix, followed by the (optional)
2392 // right-hand side of the binary expression. We have an
2393 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002394 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002395 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002396 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002397 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002398 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002399 if (Receiver.isInvalid())
2400 return true;
2401
2402 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002403 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002404 return false;
2405 }
2406
2407 // We have a class message. Turn the simple-type-specifier or
2408 // typename-specifier we parsed into a type and parse the
2409 // remainder of the class message.
2410 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002411 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002412 if (Type.isInvalid())
2413 return true;
2414
2415 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002416 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002417 return false;
2418}
2419
Douglas Gregor990ccac2010-05-31 14:40:22 +00002420/// \brief Determine whether the parser is currently referring to a an
2421/// Objective-C message send, using a simplified heuristic to avoid overhead.
2422///
2423/// This routine will only return true for a subset of valid message-send
2424/// expressions.
2425bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002426 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002427 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002428 return GetLookAheadToken(1).is(tok::identifier) &&
2429 GetLookAheadToken(2).is(tok::identifier);
2430}
2431
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002432bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002433 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002434 InMessageExpression)
2435 return false;
2436
2437
2438 ParsedType Type;
2439
2440 if (Tok.is(tok::annot_typename))
2441 Type = getTypeAnnotation(Tok);
2442 else if (Tok.is(tok::identifier))
2443 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2444 getCurScope());
2445 else
2446 return false;
2447
2448 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2449 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002450 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002451 if (Tok.is(tok::identifier))
2452 TryAnnotateTypeOrScopeToken();
2453
2454 return Tok.is(tok::annot_typename);
2455 }
2456 }
2457
2458 return false;
2459}
2460
Mike Stump11289f42009-09-09 15:08:12 +00002461/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002462/// '[' objc-receiver objc-message-args ']'
2463///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002464/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002465/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002466/// expression
2467/// class-name
2468/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002469///
John McCalldadc5752010-08-24 06:29:42 +00002470ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002471 assert(Tok.is(tok::l_square) && "'[' expected");
2472 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2473
Douglas Gregora817a192010-05-27 23:06:34 +00002474 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002475 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002476 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002477 return ExprError();
2478 }
2479
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002480 InMessageExpressionRAIIObject InMessage(*this, true);
2481
David Blaikiebbafb8a2012-03-11 07:00:24 +00002482 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002483 // We completely separate the C and C++ cases because C++ requires
2484 // more complicated (read: slower) parsing.
2485
2486 // Handle send to super.
2487 // FIXME: This doesn't benefit from the same typo-correction we
2488 // get in Objective-C.
2489 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002490 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002491 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002492 ParsedType(), nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002493
2494 // Parse the receiver, which is either a type or an expression.
2495 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00002496 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002497 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002498 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002499 return ExprError();
2500 }
2501
2502 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002503 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2504 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002505 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002506
2507 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002508 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00002509 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00002510 }
2511
2512 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002513 IdentifierInfo *Name = Tok.getIdentifierInfo();
2514 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002515 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002516 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002517 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002518 NextToken().is(tok::period),
2519 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002520 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002521 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002522 ParsedType(), nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002523
John McCallfaf5fb42010-08-26 23:41:50 +00002524 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002525 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002526 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002527 return ExprError();
2528 }
2529
Douglas Gregore5798dc2010-04-21 20:38:13 +00002530 ConsumeToken(); // the type name
2531
2532 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00002533 ReceiverType, nullptr);
2534
John McCallfaf5fb42010-08-26 23:41:50 +00002535 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002536 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002537 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002538 }
Chris Lattner8f697062008-01-25 18:59:06 +00002539 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002540
2541 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00002542 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002543 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002544 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002545 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00002546 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002547
John McCallba7bf592010-08-24 05:47:05 +00002548 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002549 ParsedType(), Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00002550}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002551
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002552/// \brief Parse the remainder of an Objective-C message following the
2553/// '[' objc-receiver.
2554///
2555/// This routine handles sends to super, class messages (sent to a
2556/// class name), and instance messages (sent to an object), and the
2557/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2558/// ReceiverExpr, respectively. Only one of these parameters may have
2559/// a valid value.
2560///
2561/// \param LBracLoc The location of the opening '['.
2562///
2563/// \param SuperLoc If this is a send to 'super', the location of the
2564/// 'super' keyword that indicates a send to the superclass.
2565///
2566/// \param ReceiverType If this is a class message, the type of the
2567/// class we are sending a message to.
2568///
2569/// \param ReceiverExpr If this is an instance message, the expression
2570/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002571///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002572/// objc-message-args:
2573/// objc-selector
2574/// objc-keywordarg-list
2575///
2576/// objc-keywordarg-list:
2577/// objc-keywordarg
2578/// objc-keywordarg-list objc-keywordarg
2579///
Mike Stump11289f42009-09-09 15:08:12 +00002580/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002581/// selector-name[opt] ':' objc-keywordexpr
2582///
2583/// objc-keywordexpr:
2584/// nonempty-expr-list
2585///
2586/// nonempty-expr-list:
2587/// assignment-expression
2588/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002589///
John McCalldadc5752010-08-24 06:29:42 +00002590ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002591Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002592 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002593 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00002594 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002595 InMessageExpressionRAIIObject InMessage(*this, true);
2596
Steve Naroffeae65032009-11-07 02:08:14 +00002597 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002598 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002599 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002600 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002601 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002602 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002603 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002604 else
John McCallb268a282010-08-23 23:25:46 +00002605 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002606 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002607 cutOffParsing();
2608 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002609 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002610
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002611 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002612 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002613 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002614
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002615 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002616 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002617 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00002618
Chris Lattner0ef13522007-10-09 17:51:17 +00002619 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002620 while (1) {
2621 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002622 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002623 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00002624
Alp Toker383d2c42014-01-01 03:08:43 +00002625 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00002626 // We must manually skip to a ']', otherwise the expression skipper will
2627 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2628 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002629 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002630 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002631 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002632
Mike Stump11289f42009-09-09 15:08:12 +00002633 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002634
2635 if (Tok.is(tok::code_completion)) {
2636 if (SuperLoc.isValid())
2637 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002638 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002639 /*AtArgumentEpression=*/true);
2640 else if (ReceiverType)
2641 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002642 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002643 /*AtArgumentEpression=*/true);
2644 else
2645 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002646 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002647 /*AtArgumentEpression=*/true);
2648
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002649 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002650 return ExprError();
2651 }
2652
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00002653 ExprResult Expr;
2654 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
2655 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2656 Expr = ParseBraceInitializer();
2657 } else
2658 Expr = ParseAssignmentExpression();
2659
2660 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002661 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002662 // We must manually skip to a ']', otherwise the expression skipper will
2663 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2664 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002665 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002666 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00002667 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002668
Steve Naroff486760a2007-09-17 20:25:27 +00002669 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002670 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002671
Douglas Gregor1b605f72009-11-19 01:08:35 +00002672 // Code completion after each argument.
2673 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002674 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002675 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002676 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002677 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002678 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002679 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002680 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002681 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002682 else
John McCallb268a282010-08-23 23:25:46 +00002683 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002684 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002685 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002686 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002687 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002688 }
2689
Steve Naroff486760a2007-09-17 20:25:27 +00002690 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002691 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002692 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002693 break;
2694 // We have a selector or a colon, continue parsing.
2695 }
2696 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002697 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002698 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002699 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002700 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00002701 if (Tok.is(tok::colon))
2702 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002703 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002704 if (Tok.is(tok::colon)) {
2705 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
2706 FixItHint::CreateRemoval(commaLoc);
2707 }
Chris Lattner197a3012008-08-05 06:19:09 +00002708 // We must manually skip to a ']', otherwise the expression skipper will
2709 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2710 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002711 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002712 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002713 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002714
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002715 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002716 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002717 }
2718 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00002719 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002720
Chris Lattner197a3012008-08-05 06:19:09 +00002721 // We must manually skip to a ']', otherwise the expression skipper will
2722 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2723 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002724 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002725 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002726 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002727
Chris Lattner0ef13522007-10-09 17:51:17 +00002728 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00002729 Diag(Tok, diag::err_expected)
2730 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00002731 // We must manually skip to a ']', otherwise the expression skipper will
2732 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2733 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002734 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002735 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002736 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002737
Chris Lattner8f697062008-01-25 18:59:06 +00002738 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002739
Steve Naroffe61bfa82007-10-05 18:42:47 +00002740 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002741 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00002742 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002743 KeyLocs.push_back(Loc);
2744 }
Chris Lattner5700fab2007-10-07 02:00:24 +00002745 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002746
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002747 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002748 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002749 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002750 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002751 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002752 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00002753 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002754 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002755}
2756
John McCalldadc5752010-08-24 06:29:42 +00002757ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2758 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002759 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002760
Chris Lattnere002fbe2007-12-12 01:04:12 +00002761 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2762 // expressions. At this point, we know that the only valid thing that starts
2763 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002764 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002765 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00002766 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002767 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002768
Chris Lattnere002fbe2007-12-12 01:04:12 +00002769 while (Tok.is(tok::at)) {
2770 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002771
Sebastian Redlc13f2682008-12-09 20:22:58 +00002772 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002773 if (!isTokenStringLiteral())
2774 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002775
John McCalldadc5752010-08-24 06:29:42 +00002776 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002777 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002778 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002779
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002780 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002781 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002782
Nico Webera7c7e602012-12-31 00:28:03 +00002783 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
2784 AtStrings.size());
Anders Carlsson76f4a902007-08-21 17:43:55 +00002785}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002786
Ted Kremeneke65b0862012-03-06 20:05:56 +00002787/// ParseObjCBooleanLiteral -
2788/// objc-scalar-literal : '@' boolean-keyword
2789/// ;
2790/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2791/// ;
2792ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2793 bool ArgValue) {
2794 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2795 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2796}
2797
2798/// ParseObjCCharacterLiteral -
2799/// objc-scalar-literal : '@' character-literal
2800/// ;
2801ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2802 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2803 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002804 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002805 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002806 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002807 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002808}
2809
2810/// ParseObjCNumericLiteral -
2811/// objc-scalar-literal : '@' scalar-literal
2812/// ;
2813/// scalar-literal : | numeric-constant /* any numeric constant. */
2814/// ;
2815ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2816 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2817 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002818 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002819 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002820 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002821 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002822}
2823
Patrick Beard0caa3942012-04-19 00:25:12 +00002824/// ParseObjCBoxedExpr -
2825/// objc-box-expression:
2826/// @( assignment-expression )
2827ExprResult
2828Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2829 if (Tok.isNot(tok::l_paren))
2830 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2831
2832 BalancedDelimiterTracker T(*this, tok::l_paren);
2833 T.consumeOpen();
2834 ExprResult ValueExpr(ParseAssignmentExpression());
2835 if (T.consumeClose())
2836 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00002837
2838 if (ValueExpr.isInvalid())
2839 return ExprError();
2840
Patrick Beard0caa3942012-04-19 00:25:12 +00002841 // Wrap the sub-expression in a parenthesized expression, to distinguish
2842 // a boxed expression from a literal.
2843 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002844 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00002845 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002846 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00002847}
2848
Ted Kremeneke65b0862012-03-06 20:05:56 +00002849ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002850 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002851 ConsumeBracket(); // consume the l_square.
2852
2853 while (Tok.isNot(tok::r_square)) {
2854 // Parse list of array element expressions (all must be id types).
2855 ExprResult Res(ParseAssignmentExpression());
2856 if (Res.isInvalid()) {
2857 // We must manually skip to a ']', otherwise the expression skipper will
2858 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2859 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002860 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002861 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002862 }
2863
2864 // Parse the ellipsis that indicates a pack expansion.
2865 if (Tok.is(tok::ellipsis))
2866 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2867 if (Res.isInvalid())
2868 return true;
2869
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002870 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002871
2872 if (Tok.is(tok::comma))
2873 ConsumeToken(); // Eat the ','.
2874 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00002875 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
2876 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002877 }
2878 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramerf0623432012-08-23 22:51:59 +00002879 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00002880 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002881}
2882
2883ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2884 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2885 ConsumeBrace(); // consume the l_square.
2886 while (Tok.isNot(tok::r_brace)) {
2887 // Parse the comma separated key : value expressions.
2888 ExprResult KeyExpr;
2889 {
2890 ColonProtectionRAIIObject X(*this);
2891 KeyExpr = ParseAssignmentExpression();
2892 if (KeyExpr.isInvalid()) {
2893 // We must manually skip to a '}', otherwise the expression skipper will
2894 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2895 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002896 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002897 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002898 }
2899 }
2900
Alp Toker383d2c42014-01-01 03:08:43 +00002901 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002902 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00002903 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00002904 }
2905
2906 ExprResult ValueExpr(ParseAssignmentExpression());
2907 if (ValueExpr.isInvalid()) {
2908 // We must manually skip to a '}', otherwise the expression skipper will
2909 // stop at the '}' when it skips to the ';'. We want it to skip beyond
2910 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002911 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002912 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002913 }
2914
2915 // Parse the ellipsis that designates this as a pack expansion.
2916 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00002917 if (getLangOpts().CPlusPlus)
2918 TryConsumeToken(tok::ellipsis, EllipsisLoc);
2919
Ted Kremeneke65b0862012-03-06 20:05:56 +00002920 // We have a valid expression. Collect it in a vector so we can
2921 // build the argument list.
2922 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00002923 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00002924 };
2925 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00002926
2927 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002928 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
2929 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00002930 }
2931 SourceLocation EndLoc = ConsumeBrace();
2932
2933 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00002934 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2935 Elements.data(), Elements.size());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002936}
2937
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002938/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002939/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002940ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002941Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002942 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002943
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002944 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002945
Chris Lattner197a3012008-08-05 06:19:09 +00002946 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002947 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2948
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002949 BalancedDelimiterTracker T(*this, tok::l_paren);
2950 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002951
Douglas Gregor220cac52009-02-18 17:45:20 +00002952 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002953
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002954 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002955
Douglas Gregor220cac52009-02-18 17:45:20 +00002956 if (Ty.isInvalid())
2957 return ExprError();
2958
Nico Webera7c7e602012-12-31 00:28:03 +00002959 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
2960 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002961}
Anders Carlssone01493d2007-08-23 15:25:28 +00002962
2963/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00002964/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002965ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002966Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002967 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002968
Chris Lattner197a3012008-08-05 06:19:09 +00002969 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002970 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2971
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002972 BalancedDelimiterTracker T(*this, tok::l_paren);
2973 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002974
Chris Lattner197a3012008-08-05 06:19:09 +00002975 if (Tok.isNot(tok::identifier))
Alp Tokerec543272013-12-24 09:48:30 +00002976 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002977
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002978 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00002979 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002980
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002981 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00002982
Nico Webera7c7e602012-12-31 00:28:03 +00002983 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2984 T.getOpenLocation(), ProtoIdLoc,
2985 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00002986}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002987
2988/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00002989/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002990ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002991 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002992
Chris Lattner197a3012008-08-05 06:19:09 +00002993 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002994 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2995
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002996 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002997 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002998
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002999 BalancedDelimiterTracker T(*this, tok::l_paren);
3000 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003001 bool HasOptionalParen = Tok.is(tok::l_paren);
3002 if (HasOptionalParen)
3003 ConsumeParen();
3004
Douglas Gregor67c692c2010-08-26 15:07:07 +00003005 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003006 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003007 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003008 return ExprError();
3009 }
3010
Chris Lattner4f472a32009-04-11 18:13:45 +00003011 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003012 if (!SelIdent && // missing selector name.
3013 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003014 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003015
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003016 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003017
Steve Naroff152dd812007-12-05 22:21:29 +00003018 unsigned nColons = 0;
3019 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003020 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003021 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003022 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003023 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003024 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3025 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003026 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003027
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003028 if (Tok.is(tok::r_paren))
3029 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003030
3031 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003032 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003033 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003034 return ExprError();
3035 }
3036
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003037 // Check for another keyword selector.
3038 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003039 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003040 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003041 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003042 break;
3043 }
Steve Naroff152dd812007-12-05 22:21:29 +00003044 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003045 if (HasOptionalParen && Tok.is(tok::r_paren))
3046 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003047 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003048 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003049 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3050 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003051 T.getCloseLocation(),
3052 !HasOptionalParen);
Gabor Greif24032f12007-10-19 15:38:32 +00003053 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003054
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003055void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3056 // MCDecl might be null due to error in method or c-function prototype, etc.
3057 Decl *MCDecl = LM.D;
3058 bool skip = MCDecl &&
3059 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3060 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3061 if (skip)
3062 return;
3063
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003064 // Save the current token position.
3065 SourceLocation OrigLoc = Tok.getLocation();
3066
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003067 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
3068 // Append the current token at the end of the new token stream so that it
3069 // doesn't get lost.
3070 LM.Toks.push_back(Tok);
3071 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
3072
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003073 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003074 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003075
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003076 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3077 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003078 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003079 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003080 parseMethod
3081 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
3082 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003083
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003084 // Tell the actions module that we have entered a method or c-function definition
3085 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003086 if (parseMethod)
3087 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3088 else
3089 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003090 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003091 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003092 else {
3093 if (Tok.is(tok::colon))
3094 ParseConstructorInitializer(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003095 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003096 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00003097
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003098 if (Tok.getLocation() != OrigLoc) {
3099 // Due to parsing error, we either went over the cached tokens or
3100 // there are still cached tokens left. If it's the latter case skip the
3101 // leftover tokens.
3102 // Since this is an uncommon situation that should be avoided, use the
3103 // expensive isBeforeInTranslationUnit call.
3104 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3105 OrigLoc))
3106 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3107 ConsumeAnyToken();
3108 }
3109
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003110 return;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003111}