blob: 669c0653df818d8c30c8aa90337c8ec0c5df053b [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:
Douglas Gregor85f3f952015-07-07 03:57:15 +000099/// '@' 'class' objc-class-forward-decl (',' objc-class-forward-decl)* ';'
100///
101/// objc-class-forward-decl:
102/// identifier objc-type-parameter-list[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000103///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000104Parser::DeclGroupPtrTy
105Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000106 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000107 SmallVector<IdentifierInfo *, 8> ClassNames;
108 SmallVector<SourceLocation, 8> ClassLocs;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000109 SmallVector<ObjCTypeParamList *, 8> ClassTypeParams;
Mike Stump11289f42009-09-09 15:08:12 +0000110
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000111 while (1) {
Nico Weber69a79142013-04-04 00:15:10 +0000112 MaybeSkipAttributes(tok::objc_class);
Chris Lattner0ef13522007-10-09 17:51:17 +0000113 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000114 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000115 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000116 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000117 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000118 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +0000119 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000120 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000121
Douglas Gregor85f3f952015-07-07 03:57:15 +0000122 // Parse the optional objc-type-parameter-list.
123 ObjCTypeParamList *TypeParams = nullptr;
124 if (Tok.is(tok::less)) {
125 TypeParams = parseObjCTypeParamList();
126 if (TypeParams)
127 Actions.popObjCTypeParamList(getCurScope(), TypeParams);
128 }
129 ClassTypeParams.push_back(TypeParams);
Alp Toker383d2c42014-01-01 03:08:43 +0000130 if (!TryConsumeToken(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000131 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000132 }
Mike Stump11289f42009-09-09 15:08:12 +0000133
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000134 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +0000135 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@class"))
Craig Topper161e4db2014-05-21 06:02:52 +0000136 return Actions.ConvertDeclToDeclGroup(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000137
Ted Kremeneka26da852009-11-17 23:12:20 +0000138 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
139 ClassLocs.data(),
Douglas Gregor85f3f952015-07-07 03:57:15 +0000140 ClassTypeParams,
Ted Kremeneka26da852009-11-17 23:12:20 +0000141 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000142}
143
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000144void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
145{
146 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
147 if (ock == Sema::OCK_None)
148 return;
149
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000150 Decl *Decl = Actions.getObjCDeclContext();
151 if (CurParsedObjCImpl) {
152 CurParsedObjCImpl->finish(AtLoc);
153 } else {
154 Actions.ActOnAtEnd(getCurScope(), AtLoc);
155 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000156 Diag(AtLoc, diag::err_objc_missing_end)
157 << FixItHint::CreateInsertion(AtLoc, "@end\n");
158 if (Decl)
159 Diag(Decl->getLocStart(), diag::note_objc_container_start)
160 << (int) ock;
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000161}
162
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000163///
164/// objc-interface:
165/// objc-class-interface-attributes[opt] objc-class-interface
166/// objc-category-interface
167///
168/// objc-class-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000169/// '@' 'interface' identifier objc-type-parameter-list[opt]
170/// objc-superclass[opt] objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000171/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000172/// objc-interface-decl-list
173/// @end
174///
175/// objc-category-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000176/// '@' 'interface' identifier objc-type-parameter-list[opt]
177/// '(' identifier[opt] ')' objc-protocol-refs[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000178/// objc-interface-decl-list
179/// @end
180///
181/// objc-superclass:
Douglas Gregore9d95f12015-07-07 03:57:35 +0000182/// ':' identifier objc-type-arguments[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000183///
184/// objc-class-interface-attributes:
185/// __attribute__((visibility("default")))
186/// __attribute__((visibility("hidden")))
187/// __attribute__((deprecated))
188/// __attribute__((unavailable))
189/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardacfbe9e2012-04-06 18:12:22 +0000190/// __attribute__((objc_root_class))
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000191///
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000192Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000193 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000194 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000195 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000196 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000197 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000198
Douglas Gregor49c22a72009-11-18 16:26:39 +0000199 // Code completion after '@interface'.
200 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000201 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000202 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000203 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000204 }
205
Nico Weber69a79142013-04-04 00:15:10 +0000206 MaybeSkipAttributes(tok::objc_interface);
Nico Weber04e213b2013-04-03 17:36:11 +0000207
Chris Lattner0ef13522007-10-09 17:51:17 +0000208 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000209 Diag(Tok, diag::err_expected)
210 << tok::identifier; // missing class or category name.
Craig Topper161e4db2014-05-21 06:02:52 +0000211 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000212 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000213
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000214 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000215 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000216 SourceLocation nameLoc = ConsumeToken();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000217
218 // Parse the objc-type-parameter-list or objc-protocol-refs. For the latter
219 // case, LAngleLoc will be valid and ProtocolIdents will capture the
220 // protocol references (that have not yet been resolved).
221 SourceLocation LAngleLoc, EndProtoLoc;
222 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
223 ObjCTypeParamList *typeParameterList = nullptr;
224 if (Tok.is(tok::less)) {
225 typeParameterList = parseObjCTypeParamListOrProtocolRefs(LAngleLoc,
226 ProtocolIdents,
227 EndProtoLoc);
228 }
229
230 if (Tok.is(tok::l_paren) &&
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000231 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000232
233 BalancedDelimiterTracker T(*this, tok::l_paren);
234 T.consumeOpen();
235
236 SourceLocation categoryLoc;
Craig Topper161e4db2014-05-21 06:02:52 +0000237 IdentifierInfo *categoryId = nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000238 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000239 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000240 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000241 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000242 }
243
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000244 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000245 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000246 categoryId = Tok.getIdentifierInfo();
247 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000248 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000249 else if (!getLangOpts().ObjC2) {
Alp Tokerec543272013-12-24 09:48:30 +0000250 Diag(Tok, diag::err_expected)
251 << tok::identifier; // missing category name.
Craig Topper161e4db2014-05-21 06:02:52 +0000252 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000253 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000254
255 T.consumeClose();
256 if (T.getCloseLocation().isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000257 return nullptr;
258
Douglas Gregor0c254a02011-09-23 19:19:41 +0000259 if (!attrs.empty()) { // categories don't support attributes.
260 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
261 attrs.clear();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000262 }
Douglas Gregor0c254a02011-09-23 19:19:41 +0000263
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000264 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000265 assert(LAngleLoc.isInvalid() && "Cannot have already parsed protocols");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000266 SmallVector<Decl *, 8> ProtocolRefs;
267 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000268 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000269 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000270 LAngleLoc, EndProtoLoc))
Craig Topper161e4db2014-05-21 06:02:52 +0000271 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000272
John McCall48871652010-08-21 09:40:31 +0000273 Decl *CategoryType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000274 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000275 nameId, nameLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000276 typeParameterList,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000277 categoryId, categoryLoc,
278 ProtocolRefs.data(),
279 ProtocolRefs.size(),
280 ProtocolLocs.data(),
281 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000282
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000283 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000284 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000285
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000286 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000287
288 if (typeParameterList)
289 Actions.popObjCTypeParamList(getCurScope(), typeParameterList);
290
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000291 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000292 }
293 // Parse a class interface.
Craig Topper161e4db2014-05-21 06:02:52 +0000294 IdentifierInfo *superClassId = nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000295 SourceLocation superClassLoc;
Douglas Gregore9d95f12015-07-07 03:57:35 +0000296 DeclSpec superClassDS(AttrFactory);
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000297
Chris Lattner0ef13522007-10-09 17:51:17 +0000298 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000299 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000300
301 // Code completion of superclass names.
302 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000303 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000304 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000305 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000306 }
307
Chris Lattner0ef13522007-10-09 17:51:17 +0000308 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000309 Diag(Tok, diag::err_expected)
310 << tok::identifier; // missing super class name.
Craig Topper161e4db2014-05-21 06:02:52 +0000311 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000312 }
313 superClassId = Tok.getIdentifierInfo();
314 superClassLoc = ConsumeToken();
Douglas Gregore9d95f12015-07-07 03:57:35 +0000315
316 // Type arguments for the superclass or protocol conformances.
317 if (Tok.is(tok::less)) {
318 ParseObjCTypeArgsOrProtocolQualifiers(superClassDS,
319 /*warnOnIncompleteProtocols=*/true);
320 }
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000321 }
Douglas Gregor85f3f952015-07-07 03:57:15 +0000322
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000323 // Next, we need to check for any protocol references.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000324 SmallVector<Decl *, 8> ProtocolRefs;
325 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000326 if (LAngleLoc.isValid()) {
327 // We already parsed the protocols named when we thought we had a
328 // type parameter list. Translate them into actual protocol references.
329 for (const auto &pair : ProtocolIdents) {
330 ProtocolLocs.push_back(pair.second);
331 }
332 Actions.FindProtocolDeclaration(/*WarnOnDeclarations=*/true,
333 /*ForObjCContainer=*/true,
334 &ProtocolIdents[0], ProtocolIdents.size(),
335 ProtocolRefs);
Douglas Gregore9d95f12015-07-07 03:57:35 +0000336 } else if (auto protocols = superClassDS.getProtocolQualifiers()) {
337 // We already parsed the protocols named when we thought we had a
338 // type argument list (for a specialized superclass). Treat them
339 // as actual protocol references.
340 unsigned numProtocols = superClassDS.getNumProtocolQualifiers();
341 ProtocolRefs.append(protocols, protocols + numProtocols);
342 ProtocolLocs.append(superClassDS.getProtocolLocs(),
343 superClassDS.getProtocolLocs() + numProtocols);
344 LAngleLoc = superClassDS.getProtocolLAngleLoc();
345 EndProtoLoc = superClassDS.getLocEnd();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000346 } else if (Tok.is(tok::less) &&
347 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, true,
348 LAngleLoc, EndProtoLoc)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000349 return nullptr;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000350 }
Mike Stump11289f42009-09-09 15:08:12 +0000351
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000352 if (Tok.isNot(tok::less))
353 Actions.ActOnTypedefedProtocols(ProtocolRefs, superClassId, superClassLoc);
354
John McCall48871652010-08-21 09:40:31 +0000355 Decl *ClsType =
Douglas Gregore9d95f12015-07-07 03:57:35 +0000356 Actions.ActOnStartClassInterface(getCurScope(), AtLoc, nameId, nameLoc,
357 typeParameterList, superClassId,
358 superClassLoc,
359 superClassDS.getObjCTypeArgs(),
360 superClassDS.getObjCTypeArgsRange(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000361 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000362 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000363 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000364
Chris Lattner0ef13522007-10-09 17:51:17 +0000365 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000366 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000367
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000368 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000369
370 if (typeParameterList)
371 Actions.popObjCTypeParamList(getCurScope(), typeParameterList);
372
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000373 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000374}
375
Douglas Gregor813a0662015-06-19 18:14:38 +0000376/// Add an attribute for a context-sensitive type nullability to the given
377/// declarator.
378static void addContextSensitiveTypeNullability(Parser &P,
379 Declarator &D,
380 NullabilityKind nullability,
381 SourceLocation nullabilityLoc,
382 bool &addedToDeclSpec) {
383 // Create the attribute.
384 auto getNullabilityAttr = [&]() -> AttributeList * {
Douglas Gregorbec595a2015-06-19 18:27:45 +0000385 return D.getAttributePool().create(
386 P.getNullabilityKeyword(nullability),
387 SourceRange(nullabilityLoc),
388 nullptr, SourceLocation(),
389 nullptr, 0,
390 AttributeList::AS_ContextSensitiveKeyword);
Douglas Gregor813a0662015-06-19 18:14:38 +0000391 };
392
393 if (D.getNumTypeObjects() > 0) {
394 // Add the attribute to the declarator chunk nearest the declarator.
395 auto nullabilityAttr = getNullabilityAttr();
396 DeclaratorChunk &chunk = D.getTypeObject(0);
397 nullabilityAttr->setNext(chunk.getAttrListRef());
398 chunk.getAttrListRef() = nullabilityAttr;
399 } else if (!addedToDeclSpec) {
400 // Otherwise, just put it on the declaration specifiers (if one
401 // isn't there already).
402 D.getMutableDeclSpec().addAttributes(getNullabilityAttr());
403 addedToDeclSpec = true;
404 }
405}
406
Douglas Gregor85f3f952015-07-07 03:57:15 +0000407/// Parse an Objective-C type parameter list, if present, or capture
408/// the locations of the protocol identifiers for a list of protocol
409/// references.
410///
411/// objc-type-parameter-list:
412/// '<' objc-type-parameter (',' objc-type-parameter)* '>'
413///
414/// objc-type-parameter:
415/// identifier objc-type-parameter-bound[opt]
416///
417/// objc-type-parameter-bound:
418/// ':' type-name
419///
420/// \param lAngleLoc The location of the starting '<'.
421///
422/// \param protocolIdents Will capture the list of identifiers, if the
423/// angle brackets contain a list of protocol references rather than a
424/// type parameter list.
425///
426/// \param rAngleLoc The location of the ending '>'.
427ObjCTypeParamList *Parser::parseObjCTypeParamListOrProtocolRefs(
428 SourceLocation &lAngleLoc,
429 SmallVectorImpl<IdentifierLocPair> &protocolIdents,
430 SourceLocation &rAngleLoc,
431 bool mayBeProtocolList) {
432 assert(Tok.is(tok::less) && "Not at the beginning of a type parameter list");
433
434 // Within the type parameter list, don't treat '>' as an operator.
435 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
436
437 // Local function to "flush" the protocol identifiers, turning them into
438 // type parameters.
439 SmallVector<Decl *, 4> typeParams;
440 auto makeProtocolIdentsIntoTypeParameters = [&]() {
Douglas Gregore83b9562015-07-07 03:57:53 +0000441 unsigned index = 0;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000442 for (const auto &pair : protocolIdents) {
443 DeclResult typeParam = Actions.actOnObjCTypeParam(getCurScope(),
Douglas Gregore83b9562015-07-07 03:57:53 +0000444 index++,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000445 pair.first,
446 pair.second,
447 SourceLocation(),
448 ParsedType());
449 if (typeParam.isUsable())
450 typeParams.push_back(typeParam.get());
451 }
452
453 protocolIdents.clear();
454 mayBeProtocolList = false;
455 };
456
457 bool invalid = false;
458 lAngleLoc = ConsumeToken();
459 do {
460 // Parse the identifier.
461 if (!Tok.is(tok::identifier)) {
462 // Code completion.
463 if (Tok.is(tok::code_completion)) {
464 // FIXME: If these aren't protocol references, we'll need different
465 // completions.
466 Actions.CodeCompleteObjCProtocolReferences(protocolIdents.data(),
467 protocolIdents.size());
468 cutOffParsing();
469
470 // FIXME: Better recovery here?.
471 return nullptr;
472 }
473
474 Diag(Tok, diag::err_objc_expected_type_parameter);
475 invalid = true;
476 break;
477 }
478
479 IdentifierInfo *paramName = Tok.getIdentifierInfo();
480 SourceLocation paramLoc = ConsumeToken();
481
482 // If there is a bound, parse it.
483 SourceLocation colonLoc;
484 TypeResult boundType;
485 if (TryConsumeToken(tok::colon, colonLoc)) {
486 // Once we've seen a bound, we know this is not a list of protocol
487 // references.
488 if (mayBeProtocolList) {
489 // Up until now, we have been queuing up parameters because they
490 // might be protocol references. Turn them into parameters now.
491 makeProtocolIdentsIntoTypeParameters();
492 }
493
494 // type-name
495 boundType = ParseTypeName();
496 if (boundType.isInvalid())
497 invalid = true;
498 } else if (mayBeProtocolList) {
499 // If this could still be a protocol list, just capture the identifier.
500 // We don't want to turn it into a parameter.
501 protocolIdents.push_back(std::make_pair(paramName, paramLoc));
502 continue;
503 }
504
505 // Create the type parameter.
506 DeclResult typeParam = Actions.actOnObjCTypeParam(getCurScope(),
Douglas Gregore83b9562015-07-07 03:57:53 +0000507 typeParams.size(),
Douglas Gregor85f3f952015-07-07 03:57:15 +0000508 paramName,
509 paramLoc,
510 colonLoc,
511 boundType.isUsable()
512 ? boundType.get()
513 : ParsedType());
514 if (typeParam.isUsable())
515 typeParams.push_back(typeParam.get());
516 } while (TryConsumeToken(tok::comma));
517
518 // Parse the '>'.
519 if (invalid) {
520 SkipUntil(tok::greater, tok::at, StopBeforeMatch);
521 if (Tok.is(tok::greater))
522 ConsumeToken();
523 } else if (ParseGreaterThanInTemplateList(rAngleLoc,
524 /*ConsumeLastToken=*/true,
525 /*ObjCGenericList=*/true)) {
526 Diag(lAngleLoc, diag::note_matching) << "'<'";
527 SkipUntil({tok::greater, tok::greaterequal, tok::at, tok::minus,
528 tok::minus, tok::plus, tok::colon, tok::l_paren, tok::l_brace,
529 tok::comma, tok::semi },
530 StopBeforeMatch);
531 if (Tok.is(tok::greater))
532 ConsumeToken();
533 }
534
535 if (mayBeProtocolList) {
536 // A type parameter list must be followed by either a ':' (indicating the
537 // presence of a superclass) or a '(' (indicating that this is a category
538 // or extension). This disambiguates between an objc-type-parameter-list
539 // and a objc-protocol-refs.
540 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_paren)) {
541 // Returning null indicates that we don't have a type parameter list.
542 // The results the caller needs to handle the protocol references are
543 // captured in the reference parameters already.
544 return nullptr;
545 }
546
547 // We have a type parameter list that looks like a list of protocol
548 // references. Turn that parameter list into type parameters.
549 makeProtocolIdentsIntoTypeParameters();
550 }
551
552 // Form the type parameter list.
553 ObjCTypeParamList *list = Actions.actOnObjCTypeParamList(
554 getCurScope(),
555 lAngleLoc,
556 typeParams,
557 rAngleLoc);
558
559 // Clear out the angle locations; they're used by the caller to indicate
560 // whether there are any protocol references.
561 lAngleLoc = SourceLocation();
562 rAngleLoc = SourceLocation();
563 return list;
564}
565
566/// Parse an objc-type-parameter-list.
567ObjCTypeParamList *Parser::parseObjCTypeParamList() {
568 SourceLocation lAngleLoc;
569 SmallVector<IdentifierLocPair, 1> protocolIdents;
570 SourceLocation rAngleLoc;
571 return parseObjCTypeParamListOrProtocolRefs(lAngleLoc, protocolIdents,
572 rAngleLoc,
573 /*mayBeProtocolList=*/false);
574}
575
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000576/// objc-interface-decl-list:
577/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000578/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000579/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000580/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000581/// objc-interface-decl-list declaration
582/// objc-interface-decl-list ';'
583///
Steve Naroff99264b42007-08-22 16:35:03 +0000584/// objc-method-requirement: [OBJC2]
585/// @required
586/// @optional
587///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000588void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
589 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000590 SmallVector<Decl *, 32> allMethods;
591 SmallVector<Decl *, 16> allProperties;
592 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000593 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000594
Ted Kremenekc7c64312010-01-07 01:20:12 +0000595 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000596
Steve Naroff99264b42007-08-22 16:35:03 +0000597 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000598 // If this is a method prototype, parse it.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000599 if (Tok.isOneOf(tok::minus, tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000600 if (Decl *methodPrototype =
601 ParseObjCMethodPrototype(MethodImplKind, false))
602 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000603 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
604 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000605 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
606 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000607 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000608 if (Tok.is(tok::semi))
609 ConsumeToken();
610 }
Steve Naroff99264b42007-08-22 16:35:03 +0000611 continue;
612 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000613 if (Tok.is(tok::l_paren)) {
614 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000615 ParseObjCMethodDecl(Tok.getLocation(),
616 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000617 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000618 continue;
619 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000620 // Ignore excess semicolons.
621 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000622 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000623 continue;
624 }
Mike Stump11289f42009-09-09 15:08:12 +0000625
Chris Lattnerda9fb152008-10-20 06:10:06 +0000626 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000627 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000628 break;
Mike Stump11289f42009-09-09 15:08:12 +0000629
Douglas Gregorf1934162010-01-13 21:24:21 +0000630 // Code completion within an Objective-C interface.
631 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000632 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000633 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000634 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000635 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000636 }
637
Chris Lattner038a3e32008-10-20 05:46:22 +0000638 // If we don't have an @ directive, parse it as a function definition.
639 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000640 // The code below does not consume '}'s because it is afraid of eating the
641 // end of a namespace. Because of the way this code is structured, an
642 // erroneous r_brace would cause an infinite loop if not handled here.
643 if (Tok.is(tok::r_brace))
644 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000645 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000646 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000647 continue;
648 }
Mike Stump11289f42009-09-09 15:08:12 +0000649
Chris Lattner038a3e32008-10-20 05:46:22 +0000650 // Otherwise, we have an @ directive, eat the @.
651 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000652 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000653 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000654 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000655 }
656
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000657 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000658
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000659 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000660 AtEnd.setBegin(AtLoc);
661 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000662 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000663 } else if (DirectiveKind == tok::objc_not_keyword) {
664 Diag(Tok, diag::err_objc_unknown_at);
665 SkipUntil(tok::semi);
666 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000667 }
Mike Stump11289f42009-09-09 15:08:12 +0000668
Chris Lattnerda9fb152008-10-20 06:10:06 +0000669 // Eat the identifier.
670 ConsumeToken();
671
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000672 switch (DirectiveKind) {
673 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000674 // FIXME: If someone forgets an @end on a protocol, this loop will
675 // continue to eat up tons of stuff and spew lots of nonsense errors. It
676 // would probably be better to bail out if we saw an @class or @interface
677 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000678 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000679 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000680 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000681 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000682
683 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000684 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000685 Diag(AtLoc, diag::err_objc_missing_end)
686 << FixItHint::CreateInsertion(AtLoc, "@end\n");
687 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
688 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000689 ConsumeToken();
690 break;
691
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000692 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000693 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000694 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000695 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000696 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000697 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000698 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000699 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000700 break;
Mike Stump11289f42009-09-09 15:08:12 +0000701
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000702 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000703 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000704 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000705
Chris Lattner038a3e32008-10-20 05:46:22 +0000706 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000707 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000708 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000709 if (Tok.is(tok::l_paren)) {
710 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000711 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000712 }
Mike Stump11289f42009-09-09 15:08:12 +0000713
Douglas Gregor813a0662015-06-19 18:14:38 +0000714 bool addedToDeclSpec = false;
Benjamin Kramera39beb92014-09-03 11:06:10 +0000715 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
716 if (FD.D.getIdentifier() == nullptr) {
717 Diag(AtLoc, diag::err_objc_property_requires_field_name)
718 << FD.D.getSourceRange();
719 return;
720 }
721 if (FD.BitfieldSize) {
722 Diag(AtLoc, diag::err_objc_property_bitfield)
723 << FD.D.getSourceRange();
724 return;
725 }
726
Douglas Gregor813a0662015-06-19 18:14:38 +0000727 // Map a nullability property attribute to a context-sensitive keyword
728 // attribute.
729 if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
730 addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
731 OCDS.getNullabilityLoc(),
732 addedToDeclSpec);
733
Benjamin Kramera39beb92014-09-03 11:06:10 +0000734 // Install the property declarator into interfaceDecl.
735 IdentifierInfo *SelName =
736 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
737
738 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
739 IdentifierInfo *SetterName = OCDS.getSetterName();
740 Selector SetterSel;
741 if (SetterName)
742 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
743 else
744 SetterSel = SelectorTable::constructSetterSelector(
745 PP.getIdentifierTable(), PP.getSelectorTable(),
746 FD.D.getIdentifier());
747 bool isOverridingProperty = false;
748 Decl *Property = Actions.ActOnProperty(
749 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
750 &isOverridingProperty, MethodImplKind);
751 if (!isOverridingProperty)
752 allProperties.push_back(Property);
753
754 FD.complete(Property);
755 };
John McCallcfefb6d2009-11-03 02:38:08 +0000756
Chris Lattner038a3e32008-10-20 05:46:22 +0000757 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000758 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000759 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000760
John McCall405988b2011-03-26 01:53:26 +0000761 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000762 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000763 }
Steve Naroff99264b42007-08-22 16:35:03 +0000764 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000765
766 // We break out of the big loop in two cases: when we see @end or when we see
767 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000768 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000769 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000770 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000771 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000772 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000773 } else {
774 Diag(Tok, diag::err_objc_missing_end)
775 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
776 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
777 << (int) Actions.getObjCContainerKind();
778 AtEnd.setBegin(Tok.getLocation());
779 AtEnd.setEnd(Tok.getLocation());
780 }
Mike Stump11289f42009-09-09 15:08:12 +0000781
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000782 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000783 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000784 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000785}
786
Douglas Gregor813a0662015-06-19 18:14:38 +0000787/// Diagnose redundant or conflicting nullability information.
788static void diagnoseRedundantPropertyNullability(Parser &P,
789 ObjCDeclSpec &DS,
790 NullabilityKind nullability,
791 SourceLocation nullabilityLoc){
792 if (DS.getNullability() == nullability) {
793 P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000794 << DiagNullabilityKind(nullability, true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000795 << SourceRange(DS.getNullabilityLoc());
796 return;
797 }
798
799 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000800 << DiagNullabilityKind(nullability, true)
801 << DiagNullabilityKind(DS.getNullability(), true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000802 << SourceRange(DS.getNullabilityLoc());
803}
804
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000805/// Parse property attribute declarations.
806///
807/// property-attr-decl: '(' property-attrlist ')'
808/// property-attrlist:
809/// property-attribute
810/// property-attrlist ',' property-attribute
811/// property-attribute:
812/// getter '=' identifier
813/// setter '=' identifier ':'
814/// readonly
815/// readwrite
816/// assign
817/// retain
818/// copy
819/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000820/// atomic
821/// strong
822/// weak
823/// unsafe_unretained
Douglas Gregor813a0662015-06-19 18:14:38 +0000824/// nonnull
825/// nullable
826/// null_unspecified
Douglas Gregor849ebc22015-06-19 18:14:46 +0000827/// null_resettable
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000828///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000829void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000830 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000831 BalancedDelimiterTracker T(*this, tok::l_paren);
832 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000833
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000834 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000835 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000836 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000837 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000838 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000839 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000840
Chris Lattner76619232008-10-20 07:22:18 +0000841 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000842 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000843 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000844 return;
845 }
Mike Stump11289f42009-09-09 15:08:12 +0000846
Chris Lattner1db33542008-10-20 07:37:22 +0000847 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000848
Chris Lattner68e48682008-11-20 04:42:34 +0000849 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000850 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000851 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000852 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000853 else if (II->isStr("unsafe_unretained"))
854 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000855 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000856 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000857 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000858 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000859 else if (II->isStr("strong"))
860 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000861 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000862 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000863 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000864 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000865 else if (II->isStr("atomic"))
866 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000867 else if (II->isStr("weak"))
868 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000869 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000870 bool IsSetter = II->getNameStart()[0] == 's';
871
Chris Lattner825bca12008-10-20 07:39:53 +0000872 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000873 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
874 diag::err_objc_expected_equal_for_getter;
875
Alp Toker383d2c42014-01-01 03:08:43 +0000876 if (ExpectAndConsume(tok::equal, DiagID)) {
877 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000878 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000879 }
Mike Stump11289f42009-09-09 15:08:12 +0000880
Douglas Gregorc8537c52009-11-19 07:41:15 +0000881 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000882 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000883 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000884 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000885 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000886 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000887 }
888
Anders Carlssonfe15a782010-10-02 17:45:21 +0000889
890 SourceLocation SelLoc;
891 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
892
893 if (!SelIdent) {
894 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
895 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000896 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000897 return;
898 }
Mike Stump11289f42009-09-09 15:08:12 +0000899
Anders Carlssonfe15a782010-10-02 17:45:21 +0000900 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000901 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000902 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000903
Alp Toker383d2c42014-01-01 03:08:43 +0000904 if (ExpectAndConsume(tok::colon,
905 diag::err_expected_colon_after_setter_name)) {
906 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000907 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000908 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000909 } else {
910 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000911 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000912 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000913 } else if (II->isStr("nonnull")) {
914 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
915 diagnoseRedundantPropertyNullability(*this, DS,
916 NullabilityKind::NonNull,
917 Tok.getLocation());
918 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
919 DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
920 } else if (II->isStr("nullable")) {
921 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
922 diagnoseRedundantPropertyNullability(*this, DS,
923 NullabilityKind::Nullable,
924 Tok.getLocation());
925 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
926 DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
927 } else if (II->isStr("null_unspecified")) {
928 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
929 diagnoseRedundantPropertyNullability(*this, DS,
930 NullabilityKind::Unspecified,
931 Tok.getLocation());
932 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
933 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000934 } else if (II->isStr("null_resettable")) {
935 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
936 diagnoseRedundantPropertyNullability(*this, DS,
937 NullabilityKind::Unspecified,
938 Tok.getLocation());
939 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
940 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
941
942 // Also set the null_resettable bit.
943 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
Chris Lattner825bca12008-10-20 07:39:53 +0000944 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000945 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000946 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000947 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000948 }
Mike Stump11289f42009-09-09 15:08:12 +0000949
Chris Lattner1db33542008-10-20 07:37:22 +0000950 if (Tok.isNot(tok::comma))
951 break;
Mike Stump11289f42009-09-09 15:08:12 +0000952
Chris Lattner1db33542008-10-20 07:37:22 +0000953 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000954 }
Mike Stump11289f42009-09-09 15:08:12 +0000955
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000956 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000957}
958
Steve Naroff09bf8152007-09-06 21:24:23 +0000959/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000960/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000961/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000962///
963/// objc-instance-method: '-'
964/// objc-class-method: '+'
965///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000966/// objc-method-attributes: [OBJC2]
967/// __attribute__((deprecated))
968///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000969Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000970 bool MethodDefinition) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000971 assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000972
Mike Stump11289f42009-09-09 15:08:12 +0000973 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000974 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000975 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000976 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000977 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000978 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000979 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000980}
981
982/// objc-selector:
983/// identifier
984/// one of
985/// enum struct union if else while do for switch case default
986/// break continue return goto asm sizeof typeof __alignof
987/// unsigned long const short volatile signed restrict _Complex
988/// in out inout bycopy byref oneway int char float double void _Bool
989///
Chris Lattner4f472a32009-04-11 18:13:45 +0000990IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000991
Chris Lattner5700fab2007-10-07 02:00:24 +0000992 switch (Tok.getKind()) {
993 default:
Craig Topper161e4db2014-05-21 06:02:52 +0000994 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000995 case tok::ampamp:
996 case tok::ampequal:
997 case tok::amp:
998 case tok::pipe:
999 case tok::tilde:
1000 case tok::exclaim:
1001 case tok::exclaimequal:
1002 case tok::pipepipe:
1003 case tok::pipeequal:
1004 case tok::caret:
1005 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +00001006 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +00001007 if (isLetter(ThisTok[0])) {
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001008 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
1009 Tok.setKind(tok::identifier);
1010 SelectorLoc = ConsumeToken();
1011 return II;
1012 }
Craig Topper161e4db2014-05-21 06:02:52 +00001013 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001014 }
1015
Chris Lattner5700fab2007-10-07 02:00:24 +00001016 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001017 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +00001018 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001019 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001020 case tok::kw_break:
1021 case tok::kw_case:
1022 case tok::kw_catch:
1023 case tok::kw_char:
1024 case tok::kw_class:
1025 case tok::kw_const:
1026 case tok::kw_const_cast:
1027 case tok::kw_continue:
1028 case tok::kw_default:
1029 case tok::kw_delete:
1030 case tok::kw_do:
1031 case tok::kw_double:
1032 case tok::kw_dynamic_cast:
1033 case tok::kw_else:
1034 case tok::kw_enum:
1035 case tok::kw_explicit:
1036 case tok::kw_export:
1037 case tok::kw_extern:
1038 case tok::kw_false:
1039 case tok::kw_float:
1040 case tok::kw_for:
1041 case tok::kw_friend:
1042 case tok::kw_goto:
1043 case tok::kw_if:
1044 case tok::kw_inline:
1045 case tok::kw_int:
1046 case tok::kw_long:
1047 case tok::kw_mutable:
1048 case tok::kw_namespace:
1049 case tok::kw_new:
1050 case tok::kw_operator:
1051 case tok::kw_private:
1052 case tok::kw_protected:
1053 case tok::kw_public:
1054 case tok::kw_register:
1055 case tok::kw_reinterpret_cast:
1056 case tok::kw_restrict:
1057 case tok::kw_return:
1058 case tok::kw_short:
1059 case tok::kw_signed:
1060 case tok::kw_sizeof:
1061 case tok::kw_static:
1062 case tok::kw_static_cast:
1063 case tok::kw_struct:
1064 case tok::kw_switch:
1065 case tok::kw_template:
1066 case tok::kw_this:
1067 case tok::kw_throw:
1068 case tok::kw_true:
1069 case tok::kw_try:
1070 case tok::kw_typedef:
1071 case tok::kw_typeid:
1072 case tok::kw_typename:
1073 case tok::kw_typeof:
1074 case tok::kw_union:
1075 case tok::kw_unsigned:
1076 case tok::kw_using:
1077 case tok::kw_virtual:
1078 case tok::kw_void:
1079 case tok::kw_volatile:
1080 case tok::kw_wchar_t:
1081 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +00001082 case tok::kw__Bool:
1083 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001084 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +00001085 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001086 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +00001087 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +00001088 }
Steve Naroff99264b42007-08-22 16:35:03 +00001089}
1090
Fariborz Jahanian83615522008-01-02 22:54:34 +00001091/// objc-for-collection-in: 'in'
1092///
Fariborz Jahanian3622e592008-01-04 23:04:08 +00001093bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001094 // FIXME: May have to do additional look-ahead to only allow for
1095 // valid tokens following an 'in'; such as an identifier, unary operators,
1096 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001097 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +00001098 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +00001099}
1100
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001101/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +00001102/// qualifier list and builds their bitmask representation in the input
1103/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +00001104///
1105/// objc-type-qualifiers:
1106/// objc-type-qualifier
1107/// objc-type-qualifiers objc-type-qualifier
1108///
Douglas Gregor813a0662015-06-19 18:14:38 +00001109/// objc-type-qualifier:
1110/// 'in'
1111/// 'out'
1112/// 'inout'
1113/// 'oneway'
1114/// 'bycopy'
1115/// 'byref'
1116/// 'nonnull'
1117/// 'nullable'
1118/// 'null_unspecified'
1119///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001120void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001121 Declarator::TheContext Context) {
1122 assert(Context == Declarator::ObjCParameterContext ||
1123 Context == Declarator::ObjCResultContext);
1124
Chris Lattner61511e12007-12-12 06:56:32 +00001125 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +00001126 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +00001127 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +00001128 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001129 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +00001130 }
1131
Chris Lattner5e530bc2007-12-27 19:57:00 +00001132 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +00001133 return;
Mike Stump11289f42009-09-09 15:08:12 +00001134
Chris Lattner61511e12007-12-12 06:56:32 +00001135 const IdentifierInfo *II = Tok.getIdentifierInfo();
1136 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001137 if (II != ObjCTypeQuals[i] ||
1138 NextToken().is(tok::less) ||
1139 NextToken().is(tok::coloncolon))
Chris Lattner61511e12007-12-12 06:56:32 +00001140 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001141
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001142 ObjCDeclSpec::ObjCDeclQualifier Qual;
Douglas Gregor813a0662015-06-19 18:14:38 +00001143 NullabilityKind Nullability;
Chris Lattner61511e12007-12-12 06:56:32 +00001144 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +00001145 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001146 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
1147 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
1148 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
1149 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
1150 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
1151 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Douglas Gregor813a0662015-06-19 18:14:38 +00001152
1153 case objc_nonnull:
1154 Qual = ObjCDeclSpec::DQ_CSNullability;
1155 Nullability = NullabilityKind::NonNull;
1156 break;
1157
1158 case objc_nullable:
1159 Qual = ObjCDeclSpec::DQ_CSNullability;
1160 Nullability = NullabilityKind::Nullable;
1161 break;
1162
1163 case objc_null_unspecified:
1164 Qual = ObjCDeclSpec::DQ_CSNullability;
1165 Nullability = NullabilityKind::Unspecified;
1166 break;
Chris Lattner61511e12007-12-12 06:56:32 +00001167 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001168
1169 // FIXME: Diagnose redundant specifiers.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001170 DS.setObjCDeclQualifier(Qual);
Douglas Gregor813a0662015-06-19 18:14:38 +00001171 if (Qual == ObjCDeclSpec::DQ_CSNullability)
1172 DS.setNullability(Tok.getLocation(), Nullability);
1173
Chris Lattner61511e12007-12-12 06:56:32 +00001174 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001175 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +00001176 break;
1177 }
Mike Stump11289f42009-09-09 15:08:12 +00001178
Chris Lattner61511e12007-12-12 06:56:32 +00001179 // If this wasn't a recognized qualifier, bail out.
1180 if (II) return;
1181 }
1182}
1183
John McCalla55902b2011-10-01 09:56:14 +00001184/// Take all the decl attributes out of the given list and add
1185/// them to the given attribute set.
1186static void takeDeclAttributes(ParsedAttributes &attrs,
1187 AttributeList *list) {
1188 while (list) {
1189 AttributeList *cur = list;
1190 list = cur->getNext();
1191
1192 if (!cur->isUsedAsTypeAttr()) {
1193 // Clear out the next pointer. We're really completely
1194 // destroying the internal invariants of the declarator here,
1195 // but it doesn't matter because we're done with it.
Craig Topper161e4db2014-05-21 06:02:52 +00001196 cur->setNext(nullptr);
John McCalla55902b2011-10-01 09:56:14 +00001197 attrs.add(cur);
1198 }
1199 }
1200}
1201
1202/// takeDeclAttributes - Take all the decl attributes from the given
1203/// declarator and add them to the given list.
1204static void takeDeclAttributes(ParsedAttributes &attrs,
1205 Declarator &D) {
1206 // First, take ownership of all attributes.
1207 attrs.getPool().takeAllFrom(D.getAttributePool());
1208 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
1209
1210 // Now actually move the attributes over.
1211 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
1212 takeDeclAttributes(attrs, D.getAttributes());
1213 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
1214 takeDeclAttributes(attrs,
1215 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
1216}
1217
Chris Lattner61511e12007-12-12 06:56:32 +00001218/// objc-type-name:
1219/// '(' objc-type-qualifiers[opt] type-name ')'
1220/// '(' objc-type-qualifiers[opt] ')'
1221///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001222ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001223 Declarator::TheContext context,
1224 ParsedAttributes *paramAttrs) {
1225 assert(context == Declarator::ObjCParameterContext ||
1226 context == Declarator::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +00001227 assert((paramAttrs != nullptr) ==
1228 (context == Declarator::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +00001229
Chris Lattner0ef13522007-10-09 17:51:17 +00001230 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +00001231
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001232 BalancedDelimiterTracker T(*this, tok::l_paren);
1233 T.consumeOpen();
1234
Chris Lattner2ebb1782008-08-23 01:48:03 +00001235 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001236 ObjCDeclContextSwitch ObjCDC(*this);
1237
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001238 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001239 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +00001240
John McCallba7bf592010-08-24 05:47:05 +00001241 ParsedType Ty;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001242 if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
John McCalla55902b2011-10-01 09:56:14 +00001243 // Parse an abstract declarator.
1244 DeclSpec declSpec(AttrFactory);
1245 declSpec.setObjCQualifiers(&DS);
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001246 DeclSpecContext dsContext = DSC_normal;
1247 if (context == Declarator::ObjCResultContext)
1248 dsContext = DSC_objc_method_result;
1249 ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
Fariborz Jahanianb6499eb2012-05-29 21:52:45 +00001250 declSpec.SetRangeEnd(Tok.getLocation());
John McCalla55902b2011-10-01 09:56:14 +00001251 Declarator declarator(declSpec, context);
1252 ParseDeclarator(declarator);
1253
1254 // If that's not invalid, extract a type.
1255 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001256 // Map a nullability specifier to a context-sensitive keyword attribute.
1257 bool addedToDeclSpec = false;
1258 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1259 addContextSensitiveTypeNullability(*this, declarator,
1260 DS.getNullability(),
1261 DS.getNullabilityLoc(),
1262 addedToDeclSpec);
1263
John McCalla55902b2011-10-01 09:56:14 +00001264 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1265 if (!type.isInvalid())
1266 Ty = type.get();
1267
1268 // If we're parsing a parameter, steal all the decl attributes
1269 // and add them to the decl spec.
1270 if (context == Declarator::ObjCParameterContext)
1271 takeDeclAttributes(*paramAttrs, declarator);
1272 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001273 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001274
Steve Naroff90255b42008-10-21 14:15:04 +00001275 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001276 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001277 else if (Tok.getLocation() == TypeStartLoc) {
1278 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001279 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001280 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001281 } else {
1282 // Otherwise, we found *something*, but didn't get a ')' in the right
1283 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001284 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001285 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001286 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001287}
1288
1289/// objc-method-decl:
1290/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001291/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001292/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001293/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001294///
1295/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001296/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001297/// objc-keyword-selector objc-keyword-decl
1298///
1299/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001300/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1301/// objc-selector ':' objc-keyword-attributes[opt] identifier
1302/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1303/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001304///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001305/// objc-parmlist:
1306/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001307///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001308/// objc-parms:
1309/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001310///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001311/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001312/// , ...
1313///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001314/// objc-keyword-attributes: [OBJC2]
1315/// __attribute__((unused))
1316///
John McCall48871652010-08-21 09:40:31 +00001317Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001318 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001319 tok::ObjCKeywordKind MethodImplKind,
1320 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001321 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001322
Douglas Gregor636a61e2010-04-07 00:21:17 +00001323 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001324 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001325 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001326 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001327 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001328 }
1329
Chris Lattner2ebb1782008-08-23 01:48:03 +00001330 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001331 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001332 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001333 if (Tok.is(tok::l_paren))
Craig Topper161e4db2014-05-21 06:02:52 +00001334 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext,
1335 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001336
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001337 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001338 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001339 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001340 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001341
Douglas Gregor636a61e2010-04-07 00:21:17 +00001342 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001343 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001344 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001345 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001346 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001347 }
1348
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001349 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001350 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001351 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001352
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001353 // An unnamed colon is valid.
1354 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001355 Diag(Tok, diag::err_expected_selector_for_method)
1356 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001357 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001358 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001359 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001360 }
Mike Stump11289f42009-09-09 15:08:12 +00001361
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001362 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001363 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001364 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001365 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001366 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001367
Chris Lattner5700fab2007-10-07 02:00:24 +00001368 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001369 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001370 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001371 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001372 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001373 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001374 methodAttrs.getList(), MethodImplKind,
1375 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001376 PD.complete(Result);
1377 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001378 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001379
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001380 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001381 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001382 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001383 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1384 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001385
1386 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001387 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001388 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001389 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001390
Chris Lattner5700fab2007-10-07 02:00:24 +00001391 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001392 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001393 break;
Mike Stump11289f42009-09-09 15:08:12 +00001394
John McCallba7bf592010-08-24 05:47:05 +00001395 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001396 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001397 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1398 Declarator::ObjCParameterContext,
1399 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001400
Chris Lattner5700fab2007-10-07 02:00:24 +00001401 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001402 // Regardless, collect all the attributes we've parsed so far.
Craig Topper161e4db2014-05-21 06:02:52 +00001403 ArgInfo.ArgAttrs = nullptr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001404 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001405 MaybeParseGNUAttributes(paramAttrs);
1406 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001407 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001408
Douglas Gregor45879692010-07-08 23:37:41 +00001409 // Code completion for the next piece of the selector.
1410 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001411 KeyIdents.push_back(SelIdent);
1412 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1413 mType == tok::minus,
1414 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001415 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001416 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001417 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001418 }
1419
Chris Lattner0ef13522007-10-09 17:51:17 +00001420 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001421 Diag(Tok, diag::err_expected)
1422 << tok::identifier; // missing argument name.
Chris Lattner5700fab2007-10-07 02:00:24 +00001423 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001424 }
Mike Stump11289f42009-09-09 15:08:12 +00001425
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001426 ArgInfo.Name = Tok.getIdentifierInfo();
1427 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001428 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001429
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001430 ArgInfos.push_back(ArgInfo);
1431 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001432 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001433
John McCall084e83d2011-03-24 11:26:52 +00001434 // Make sure the attributes persist.
1435 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1436
Douglas Gregor95887f92010-07-08 23:20:03 +00001437 // Code completion for the next piece of the selector.
1438 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001439 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1440 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001441 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001442 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001443 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001444 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001445 }
1446
Chris Lattner5700fab2007-10-07 02:00:24 +00001447 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001448 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001449 if (!SelIdent && Tok.isNot(tok::colon))
1450 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001451 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001452 SourceLocation ColonLoc = Tok.getLocation();
1453 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001454 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1455 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1456 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001457 }
1458 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001459 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001460 }
Mike Stump11289f42009-09-09 15:08:12 +00001461
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001462 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001463 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001464 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001465 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001466 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001467 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001468 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001469 ConsumeToken();
1470 break;
1471 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001472 if (!cStyleParamWarned) {
1473 Diag(Tok, diag::warn_cstyle_param);
1474 cStyleParamWarned = true;
1475 }
John McCall084e83d2011-03-24 11:26:52 +00001476 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001477 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001478 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001479 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1480 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001481 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001482 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001483 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1484 ParmDecl.getIdentifierLoc(),
1485 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001486 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001487 }
Mike Stump11289f42009-09-09 15:08:12 +00001488
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001489 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001490 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001491 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001492 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001493
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001494 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001495 return nullptr;
1496
Chris Lattner5700fab2007-10-07 02:00:24 +00001497 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1498 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001499 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001500 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001501 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001502 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001503 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001504 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001505 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001506
John McCall28a6aea2009-11-04 02:18:39 +00001507 PD.complete(Result);
1508 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001509}
1510
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001511/// objc-protocol-refs:
1512/// '<' identifier-list '>'
1513///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001514bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001515ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1516 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001517 bool WarnOnDeclarations, bool ForObjCContainer,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001518 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001519 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001520
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001521 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001522
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001523 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001524
Chris Lattner3bbae002008-07-26 04:03:38 +00001525 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001526 if (Tok.is(tok::code_completion)) {
1527 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1528 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001529 cutOffParsing();
1530 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001531 }
1532
Chris Lattner3bbae002008-07-26 04:03:38 +00001533 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001534 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001535 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001536 return true;
1537 }
1538 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1539 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001540 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001541 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001542
Alp Toker383d2c42014-01-01 03:08:43 +00001543 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001544 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001545 }
Mike Stump11289f42009-09-09 15:08:12 +00001546
Chris Lattner3bbae002008-07-26 04:03:38 +00001547 // Consume the '>'.
Douglas Gregor85f3f952015-07-07 03:57:15 +00001548 if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true,
1549 /*ObjCGenericList=*/false))
Chris Lattner3bbae002008-07-26 04:03:38 +00001550 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001551
Chris Lattner3bbae002008-07-26 04:03:38 +00001552 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001553 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Chris Lattner3bbae002008-07-26 04:03:38 +00001554 &ProtocolIdents[0], ProtocolIdents.size(),
1555 Protocols);
1556 return false;
1557}
1558
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001559/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1560/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001561bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001562 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001563 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001564 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001565 SmallVector<Decl *, 8> ProtocolDecl;
1566 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001567 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001568 false, LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001569 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1570 ProtocolLocs.data(), LAngleLoc);
1571 if (EndProtoLoc.isValid())
1572 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001573 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001574}
1575
Douglas Gregore9d95f12015-07-07 03:57:35 +00001576/// Parse Objective-C type arguments or protocol qualifiers.
1577///
1578/// objc-type-arguments:
1579/// '<' type-name (',' type-name)* '>'
1580///
1581void Parser::ParseObjCTypeArgsOrProtocolQualifiers(
1582 DeclSpec &DS,
1583 bool warnOnIncompleteProtocols) {
1584 assert(Tok.is(tok::less) && "Not at the start of type args or protocols");
1585 SourceLocation lAngleLoc = ConsumeToken();
1586
1587 // Whether all of the elements we've parsed thus far are single
1588 // identifiers, which might be types or might be protocols.
1589 bool allSingleIdentifiers = true;
1590 SmallVector<IdentifierInfo *, 4> identifiers;
1591 SmallVector<SourceLocation, 4> identifierLocs;
1592
1593 // Parse a list of comma-separated identifiers, bailing out if we
1594 // see something different.
1595 do {
1596 // Parse a single identifier.
1597 if (Tok.is(tok::identifier) &&
1598 (NextToken().is(tok::comma) ||
1599 NextToken().is(tok::greater) ||
1600 NextToken().is(tok::greatergreater))) {
1601 identifiers.push_back(Tok.getIdentifierInfo());
1602 identifierLocs.push_back(ConsumeToken());
1603 continue;
1604 }
1605
1606 if (Tok.is(tok::code_completion)) {
1607 // FIXME: Also include types here.
1608 SmallVector<IdentifierLocPair, 4> identifierLocPairs;
1609 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1610 identifierLocPairs.push_back(IdentifierLocPair(identifiers[i],
1611 identifierLocs[i]));
1612 }
1613
1614 Actions.CodeCompleteObjCProtocolReferences(identifierLocPairs.data(),
1615 identifierLocPairs.size());
1616 cutOffParsing();
1617 return;
1618 }
1619
1620 allSingleIdentifiers = false;
1621 break;
1622 } while (TryConsumeToken(tok::comma));
1623
1624 // If we parsed an identifier list, semantic analysis sorts out
1625 // whether it refers to protocols or to type arguments.
1626 if (allSingleIdentifiers) {
1627 // Parse the closing '>'.
1628 SourceLocation rAngleLoc;
1629 (void)ParseGreaterThanInTemplateList(rAngleLoc, /*ConsumeLastToken=*/true,
1630 /*ObjCGenericList=*/true);
1631
1632 // Let Sema figure out what we parsed.
1633 Actions.actOnObjCTypeArgsOrProtocolQualifiers(getCurScope(),
1634 DS,
1635 lAngleLoc,
1636 identifiers,
1637 identifierLocs,
1638 rAngleLoc,
1639 warnOnIncompleteProtocols);
1640 return;
1641 }
1642
1643 // We syntactically matched a type argument, so commit to parsing
1644 // type arguments.
1645 SmallVector<ParsedType, 4> typeArgs;
1646
1647 // Convert the identifiers into type arguments.
1648 bool invalid = false;
1649 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1650 ParsedType typeArg
1651 = Actions.getTypeName(*identifiers[i], identifierLocs[i], getCurScope());
1652 if (typeArg) {
1653 typeArgs.push_back(typeArg);
1654 } else {
1655 invalid = true;
1656 }
1657 }
1658
1659 // Continue parsing type-names.
1660 do {
1661 TypeResult typeArg = ParseTypeName();
1662 if (typeArg.isUsable()) {
1663 typeArgs.push_back(typeArg.get());
1664 } else {
1665 invalid = true;
1666 }
1667 } while (TryConsumeToken(tok::comma));
1668
1669 // Parse the closing '>'.
1670 SourceLocation rAngleLoc;
1671 (void)ParseGreaterThanInTemplateList(rAngleLoc, /*ConsumeLastToken=*/true,
1672 /*ObjCGenericList=*/true);
1673
1674 if (invalid)
1675 return;
1676
1677 // Update the DeclSpec appropriately.
1678 DS.setObjCTypeArgs(lAngleLoc, typeArgs, rAngleLoc);
1679}
1680
Douglas Gregore83b9562015-07-07 03:57:53 +00001681void Parser::ParseObjCTypeArgsAndProtocolQualifiers(DeclSpec &DS) {
1682 assert(Tok.is(tok::less));
1683
1684 ParseObjCTypeArgsOrProtocolQualifiers(DS,
1685 /*warnOnIncompleteProtocols=*/false);
1686
1687 // An Objective-C object pointer followed by type arguments
1688 // can then be followed again by a set of protocol references, e.g.,
1689 // \c NSArray<NSView><NSTextDelegate>
1690 if (Tok.is(tok::less)) {
1691 if (DS.getProtocolQualifiers()) {
1692 Diag(Tok, diag::err_objc_type_args_after_protocols)
1693 << SourceRange(DS.getProtocolLAngleLoc(), DS.getLocEnd());
1694 SkipUntil(tok::greater, tok::greatergreater);
1695 } else {
1696 ParseObjCProtocolQualifiers(DS);
1697 }
1698 }
1699}
1700
1701TypeResult Parser::ParseObjCTypeArgsAndProtocolQualifiers(SourceLocation loc,
1702 ParsedType type) {
1703 assert(Tok.is(tok::less));
1704
1705 // Create declaration specifiers and set the type as the type specifier.
1706 DeclSpec DS(AttrFactory);
1707 const char *prevSpec = nullptr;
1708 unsigned diagID;
1709 DS.SetTypeSpecType(TST_typename, loc, prevSpec, diagID, type,
1710 Actions.getASTContext().getPrintingPolicy());
1711
1712 // Parse type arguments and protocol qualifiers.
1713 ParseObjCTypeArgsAndProtocolQualifiers(DS);
1714
1715 // Form a declarator to turn this into a type.
1716 Declarator D(DS, Declarator::TypeNameContext);
1717 return Actions.ActOnTypeName(getCurScope(), D);
1718}
1719
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001720void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1721 BalancedDelimiterTracker &T,
1722 SmallVectorImpl<Decl *> &AllIvarDecls,
1723 bool RBraceMissing) {
1724 if (!RBraceMissing)
1725 T.consumeClose();
1726
1727 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1728 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1729 Actions.ActOnObjCContainerFinishDefinition();
1730 // Call ActOnFields() even if we don't have any decls. This is useful
1731 // for code rewriting tools that need to be aware of the empty list.
1732 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1733 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001734 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001735}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001736
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001737/// objc-class-instance-variables:
1738/// '{' objc-instance-variable-decl-list[opt] '}'
1739///
1740/// objc-instance-variable-decl-list:
1741/// objc-visibility-spec
1742/// objc-instance-variable-decl ';'
1743/// ';'
1744/// objc-instance-variable-decl-list objc-visibility-spec
1745/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1746/// objc-instance-variable-decl-list ';'
1747///
1748/// objc-visibility-spec:
1749/// @private
1750/// @protected
1751/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001752/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001753///
1754/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001755/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001756///
John McCall48871652010-08-21 09:40:31 +00001757void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001758 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001759 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001760 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001761 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001762
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001763 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001764 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001765
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001766 BalancedDelimiterTracker T(*this, tok::l_brace);
1767 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001768 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001769 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001770 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001771
Steve Naroff00433d32007-08-21 21:17:12 +00001772 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001773 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001774 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001775 continue;
1776 }
Mike Stump11289f42009-09-09 15:08:12 +00001777
Steve Naroff00433d32007-08-21 21:17:12 +00001778 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001779 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001780 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001781 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001782 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001783 }
1784
Steve Naroff7c348172007-08-23 18:16:40 +00001785 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001786 case tok::objc_private:
1787 case tok::objc_public:
1788 case tok::objc_protected:
1789 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001790 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001791 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001792 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001793
1794 case tok::objc_end:
1795 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001796 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1797 Tok.setKind(tok::at);
1798 Tok.setLength(1);
1799 PP.EnterToken(Tok);
1800 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1801 T, AllIvarDecls, true);
1802 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001803
1804 default:
1805 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1806 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001807 }
1808 }
Mike Stump11289f42009-09-09 15:08:12 +00001809
Douglas Gregor48d46252010-01-13 21:54:15 +00001810 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001811 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001812 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001813 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001814 }
John McCallcfefb6d2009-11-03 02:38:08 +00001815
Benjamin Kramera39beb92014-09-03 11:06:10 +00001816 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1817 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1818 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001819 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001820 Decl *Field = Actions.ActOnIvar(
1821 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1822 FD.BitfieldSize, visibility);
1823 Actions.ActOnObjCContainerFinishDefinition();
1824 if (Field)
1825 AllIvarDecls.push_back(Field);
1826 FD.complete(Field);
1827 };
John McCallcfefb6d2009-11-03 02:38:08 +00001828
Chris Lattnera12405b2008-04-10 06:46:29 +00001829 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001830 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001831 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001832
Chris Lattner0ef13522007-10-09 17:51:17 +00001833 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001834 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001835 } else {
1836 Diag(Tok, diag::err_expected_semi_decl_list);
1837 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001838 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001839 }
1840 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001841 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1842 T, AllIvarDecls, false);
Steve Naroff00433d32007-08-21 21:17:12 +00001843 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001844}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001845
1846/// objc-protocol-declaration:
1847/// objc-protocol-definition
1848/// objc-protocol-forward-reference
1849///
1850/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00001851/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00001852/// objc-protocol-refs[opt]
1853/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00001854/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001855///
1856/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00001857/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001858///
James Dennett1355bd12012-06-11 06:19:40 +00001859/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001860/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001861/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001862Parser::DeclGroupPtrTy
1863Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1864 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001865 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001866 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1867 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001868
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001869 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001870 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001871 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00001872 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001873 }
1874
Nico Weber69a79142013-04-04 00:15:10 +00001875 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00001876
Chris Lattner0ef13522007-10-09 17:51:17 +00001877 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001878 Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00001879 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001880 }
1881 // Save the protocol name, then consume it.
1882 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1883 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001884
Alp Toker383d2c42014-01-01 03:08:43 +00001885 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001886 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001887 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001888 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001889 }
Mike Stump11289f42009-09-09 15:08:12 +00001890
Erik Verbruggenf9887852011-12-08 09:58:43 +00001891 CheckNestedObjCContexts(AtLoc);
1892
Chris Lattner0ef13522007-10-09 17:51:17 +00001893 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001894 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001895 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1896
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001897 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001898 while (1) {
1899 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001900 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001901 Diag(Tok, diag::err_expected) << tok::identifier;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001902 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00001903 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001904 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001905 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1906 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001907 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001908
Chris Lattner0ef13522007-10-09 17:51:17 +00001909 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001910 break;
1911 }
1912 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00001913 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00001914 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001915
Steve Naroff93eb5f12007-10-10 17:32:04 +00001916 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001917 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001918 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001919 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001920 }
Mike Stump11289f42009-09-09 15:08:12 +00001921
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001922 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001923 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001924
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001925 SmallVector<Decl *, 8> ProtocolRefs;
1926 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001927 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001928 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001929 LAngleLoc, EndProtoLoc))
Douglas Gregorf6102672012-01-01 21:23:57 +00001930 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001931
John McCall48871652010-08-21 09:40:31 +00001932 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001933 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001934 ProtocolRefs.data(),
1935 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001936 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001937 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001938
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001939 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00001940 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001941}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001942
1943/// objc-implementation:
1944/// objc-class-implementation-prologue
1945/// objc-category-implementation-prologue
1946///
1947/// objc-class-implementation-prologue:
1948/// @implementation identifier objc-superclass[opt]
1949/// objc-class-instance-variables[opt]
1950///
1951/// objc-category-implementation-prologue:
1952/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001953Parser::DeclGroupPtrTy
1954Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001955 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1956 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001957 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001958 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001959
Douglas Gregor49c22a72009-11-18 16:26:39 +00001960 // Code completion after '@implementation'.
1961 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001962 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001963 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001964 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001965 }
1966
Nico Weber69a79142013-04-04 00:15:10 +00001967 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00001968
Chris Lattner0ef13522007-10-09 17:51:17 +00001969 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001970 Diag(Tok, diag::err_expected)
1971 << tok::identifier; // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001972 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001973 }
1974 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001975 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001976 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00001977 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001978
Douglas Gregor85f3f952015-07-07 03:57:15 +00001979 // Neither a type parameter list nor a list of protocol references is
1980 // permitted here. Parse and diagnose them.
1981 if (Tok.is(tok::less)) {
1982 SourceLocation lAngleLoc, rAngleLoc;
1983 SmallVector<IdentifierLocPair, 8> protocolIdents;
1984 SourceLocation diagLoc = Tok.getLocation();
1985 if (parseObjCTypeParamListOrProtocolRefs(lAngleLoc, protocolIdents,
1986 rAngleLoc)) {
1987 Diag(diagLoc, diag::err_objc_parameterized_implementation)
1988 << SourceRange(diagLoc, PrevTokLocation);
1989 } else if (lAngleLoc.isValid()) {
1990 Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
1991 << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
1992 }
1993 }
1994
Mike Stump11289f42009-09-09 15:08:12 +00001995 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001996 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001997 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001998 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001999 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002000
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002001 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002002 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002003 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002004 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002005 }
2006
Chris Lattner0ef13522007-10-09 17:51:17 +00002007 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002008 categoryId = Tok.getIdentifierInfo();
2009 categoryLoc = ConsumeToken();
2010 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002011 Diag(Tok, diag::err_expected)
2012 << tok::identifier; // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002013 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002014 }
Chris Lattner0ef13522007-10-09 17:51:17 +00002015 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002016 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002017 SkipUntil(tok::r_paren); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002018 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002019 }
2020 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00002021 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2022 Diag(Tok, diag::err_unexpected_protocol_qualifier);
2023 AttributeFactory attr;
2024 DeclSpec DS(attr);
2025 (void)ParseObjCProtocolQualifiers(DS);
2026 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002027 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002028 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00002029 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002030
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002031 } else {
2032 // We have a class implementation
2033 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002034 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00002035 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002036 // We have a super class
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002037 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002038 Diag(Tok, diag::err_expected)
2039 << tok::identifier; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002040 return DeclGroupPtrTy();
2041 }
2042 superClassId = Tok.getIdentifierInfo();
2043 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002044 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002045 ObjCImpDecl = Actions.ActOnStartClassImplementation(
2046 AtLoc, nameId, nameLoc,
2047 superClassId, superClassLoc);
2048
2049 if (Tok.is(tok::l_brace)) // we have ivars
2050 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002051 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2052 Diag(Tok, diag::err_unexpected_protocol_qualifier);
2053 // try to recover.
2054 AttributeFactory attr;
2055 DeclSpec DS(attr);
2056 (void)ParseObjCProtocolQualifiers(DS);
2057 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002058 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002059 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002060
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002061 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002062
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002063 {
2064 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00002065 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002066 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002067 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002068 MaybeParseMicrosoftAttributes(attrs);
2069 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
2070 DeclGroupRef DG = DGP.get();
2071 DeclsInGroup.append(DG.begin(), DG.end());
2072 }
2073 }
2074 }
2075
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00002076 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002077}
Steve Naroff33a1e802007-10-29 21:38:07 +00002078
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002079Parser::DeclGroupPtrTy
2080Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002081 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
2082 "ParseObjCAtEndDeclaration(): Expected @end");
2083 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002084 if (CurParsedObjCImpl)
2085 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002086 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00002087 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002088 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002089 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002090}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002091
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002092Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
2093 if (!Finished) {
2094 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00002095 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002096 P.Diag(P.Tok, diag::err_objc_missing_end)
2097 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
2098 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
2099 << Sema::OCK_Implementation;
2100 }
2101 }
Craig Topper161e4db2014-05-21 06:02:52 +00002102 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002103 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002104}
2105
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002106void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
2107 assert(!Finished);
2108 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
2109 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002110 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2111 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002112
2113 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
2114
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002115 if (HasCFunction)
2116 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
2117 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2118 false/*c-functions*/);
2119
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002120 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002121 for (LateParsedObjCMethodContainer::iterator
2122 I = LateParsedObjCMethods.begin(),
2123 E = LateParsedObjCMethods.end(); I != E; ++I)
2124 delete *I;
2125 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002126
2127 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002128}
2129
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002130/// compatibility-alias-decl:
2131/// @compatibility_alias alias-name class-name ';'
2132///
John McCall48871652010-08-21 09:40:31 +00002133Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002134 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
2135 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
2136 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00002137 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002138 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002139 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002140 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002141 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
2142 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00002143 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002144 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002145 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002146 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002147 IdentifierInfo *classId = Tok.getIdentifierInfo();
2148 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00002149 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00002150 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
2151 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002152}
2153
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002154/// property-synthesis:
2155/// @synthesize property-ivar-list ';'
2156///
2157/// property-ivar-list:
2158/// property-ivar
2159/// property-ivar-list ',' property-ivar
2160///
2161/// property-ivar:
2162/// identifier
2163/// identifier '=' identifier
2164///
John McCall48871652010-08-21 09:40:31 +00002165Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002166 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00002167 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002168 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00002169
Douglas Gregor88e72a02009-11-18 19:45:45 +00002170 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00002171 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002172 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002173 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002174 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002175 }
2176
Douglas Gregor88e72a02009-11-18 19:45:45 +00002177 if (Tok.isNot(tok::identifier)) {
2178 Diag(Tok, diag::err_synthesized_property_name);
2179 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002180 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00002181 }
Craig Topper161e4db2014-05-21 06:02:52 +00002182
2183 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002184 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2185 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002186 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00002187 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002188 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00002189 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002190 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002191 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002192 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002193 }
2194
Chris Lattner0ef13522007-10-09 17:51:17 +00002195 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002196 Diag(Tok, diag::err_expected) << tok::identifier;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002197 break;
2198 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002199 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002200 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002201 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002202 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002203 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002204 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002205 break;
2206 ConsumeToken(); // consume ','
2207 }
Alp Toker383d2c42014-01-01 03:08:43 +00002208 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00002209 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002210}
2211
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002212/// property-dynamic:
2213/// @dynamic property-list
2214///
2215/// property-list:
2216/// identifier
2217/// property-list ',' identifier
2218///
John McCall48871652010-08-21 09:40:31 +00002219Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002220 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2221 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002222 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002223 while (true) {
2224 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002225 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002226 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002227 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002228 }
2229
2230 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002231 Diag(Tok, diag::err_expected) << tok::identifier;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002232 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002233 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002234 }
2235
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002236 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2237 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002238 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Craig Topper161e4db2014-05-21 06:02:52 +00002239 propertyId, nullptr, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002240
Chris Lattner0ef13522007-10-09 17:51:17 +00002241 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002242 break;
2243 ConsumeToken(); // consume ','
2244 }
Alp Toker383d2c42014-01-01 03:08:43 +00002245 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00002246 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002247}
Mike Stump11289f42009-09-09 15:08:12 +00002248
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002249/// objc-throw-statement:
2250/// throw expression[opt];
2251///
John McCalldadc5752010-08-24 06:29:42 +00002252StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2253 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002254 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00002255 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00002256 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002257 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002258 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002259 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002260 }
2261 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00002262 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00002263 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002264 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002265}
2266
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002267/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002268/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002269///
John McCalldadc5752010-08-24 06:29:42 +00002270StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002271Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002272 ConsumeToken(); // consume synchronized
2273 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002274 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002275 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002276 }
John McCalld9bb7432011-07-27 21:50:02 +00002277
2278 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002279 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00002280 ExprResult operand(ParseExpression());
2281
2282 if (Tok.is(tok::r_paren)) {
2283 ConsumeParen(); // ')'
2284 } else {
2285 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002286 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00002287
2288 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002289 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002290 }
John McCalld9bb7432011-07-27 21:50:02 +00002291
2292 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002293 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00002294 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002295 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002296 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002297 }
Steve Naroffd9c26072008-06-04 20:36:13 +00002298
John McCalld9bb7432011-07-27 21:50:02 +00002299 // Check the @synchronized operand now.
2300 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002301 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002302
John McCalld9bb7432011-07-27 21:50:02 +00002303 // Parse the compound statement within a new scope.
2304 ParseScope bodyScope(this, Scope::DeclScope);
2305 StmtResult body(ParseCompoundStatementBody());
2306 bodyScope.Exit();
2307
2308 // If there was a semantic or parse error earlier with the
2309 // operand, fail now.
2310 if (operand.isInvalid())
2311 return StmtError();
2312
2313 if (body.isInvalid())
2314 body = Actions.ActOnNullStmt(Tok.getLocation());
2315
2316 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002317}
2318
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002319/// objc-try-catch-statement:
2320/// @try compound-statement objc-catch-list[opt]
2321/// @try compound-statement objc-catch-list[opt] @finally compound-statement
2322///
2323/// objc-catch-list:
2324/// @catch ( parameter-declaration ) compound-statement
2325/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2326/// catch-parameter-declaration:
2327/// parameter-declaration
2328/// '...' [OBJC2]
2329///
John McCalldadc5752010-08-24 06:29:42 +00002330StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002331 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002332
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002333 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00002334 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002335 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002336 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002337 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00002338 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00002339 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002340 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00002341 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002342 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002343 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002344 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002345
Chris Lattner0ef13522007-10-09 17:51:17 +00002346 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002347 // At this point, we need to lookahead to determine if this @ is the start
2348 // of an @catch or @finally. We don't want to consume the @ token if this
2349 // is an @try or @encode or something else.
2350 Token AfterAt = GetLookAheadToken(1);
2351 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2352 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2353 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002354
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002355 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002356 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002357 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002358 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002359 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002360 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00002361 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002362 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002363 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002364 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00002365 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002366 ParseDeclarator(ParmDecl);
2367
Douglas Gregore11ee112010-04-23 23:01:43 +00002368 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002369 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002370 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002371 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002372 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002373
Steve Naroff65a00892009-04-07 22:56:58 +00002374 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002375
Steve Naroff65a00892009-04-07 22:56:58 +00002376 if (Tok.is(tok::r_paren))
2377 RParenLoc = ConsumeParen();
2378 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002379 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002380
John McCalldadc5752010-08-24 06:29:42 +00002381 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002382 if (Tok.is(tok::l_brace))
2383 CatchBody = ParseCompoundStatementBody();
2384 else
Alp Tokerec543272013-12-24 09:48:30 +00002385 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002386 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002387 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00002388
John McCalldadc5752010-08-24 06:29:42 +00002389 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00002390 RParenLoc,
2391 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002392 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002393 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002394 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002395
Steve Naroffe6016792008-02-05 21:27:35 +00002396 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002397 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2398 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002399 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002400 }
2401 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002402 } else {
2403 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002404 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002405 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002406
John McCalldadc5752010-08-24 06:29:42 +00002407 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002408 if (Tok.is(tok::l_brace))
2409 FinallyBody = ParseCompoundStatementBody();
2410 else
Alp Tokerec543272013-12-24 09:48:30 +00002411 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002412 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002413 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002414 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002415 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002416 catch_or_finally_seen = true;
2417 break;
2418 }
2419 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002420 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002421 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002422 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002423 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002424
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002425 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002426 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002427 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002428}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002429
John McCall31168b02011-06-15 23:02:42 +00002430/// objc-autoreleasepool-statement:
2431/// @autoreleasepool compound-statement
2432///
2433StmtResult
2434Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2435 ConsumeToken(); // consume autoreleasepool
2436 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002437 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002438 return StmtError();
2439 }
2440 // Enter a scope to hold everything within the compound stmt. Compound
2441 // statements can always hold declarations.
2442 ParseScope BodyScope(this, Scope::DeclScope);
2443
2444 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2445
2446 BodyScope.Exit();
2447 if (AutoreleasePoolBody.isInvalid())
2448 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2449 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002450 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002451}
2452
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002453/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2454/// for later parsing.
2455void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
2456 LexedMethod* LM = new LexedMethod(this, MDecl);
2457 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2458 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002459 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002460 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002461 if (Tok.is(tok::kw_try)) {
2462 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002463 if (Tok.is(tok::colon)) {
2464 Toks.push_back(Tok);
2465 ConsumeToken();
2466 while (Tok.isNot(tok::l_brace)) {
2467 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2468 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2469 }
2470 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002471 Toks.push_back(Tok); // also store '{'
2472 }
2473 else if (Tok.is(tok::colon)) {
2474 ConsumeToken();
2475 while (Tok.isNot(tok::l_brace)) {
2476 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2477 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2478 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002479 Toks.push_back(Tok); // also store '{'
2480 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002481 ConsumeBrace();
2482 // Consume everything up to (and including) the matching right brace.
2483 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002484 while (Tok.is(tok::kw_catch)) {
2485 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2486 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2487 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002488}
2489
Steve Naroff09bf8152007-09-06 21:24:23 +00002490/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002491///
John McCall48871652010-08-21 09:40:31 +00002492Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002493 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002494
John McCallfaf5fb42010-08-26 23:41:50 +00002495 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2496 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002497
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002498 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002499 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002500 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002501 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002502 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002503 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002504 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002505 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002506
Steve Naroffbb875722007-11-11 19:54:21 +00002507 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002508 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002509 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002510
Steve Naroffbb875722007-11-11 19:54:21 +00002511 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002512 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002513
Steve Naroffbb875722007-11-11 19:54:21 +00002514 // If we didn't find the '{', bail out.
2515 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002516 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002517 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002518
2519 if (!MDecl) {
2520 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002521 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002522 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002523 }
2524
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002525 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002526 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002527 assert (CurParsedObjCImpl
2528 && "ParseObjCMethodDefinition - Method out of @implementation");
2529 // Consume the tokens and store them for later parsing.
2530 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002531 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002532}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002533
John McCalldadc5752010-08-24 06:29:42 +00002534StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002535 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002536 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002537 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002538 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002539 }
2540
2541 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002542 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002543
2544 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002545 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002546
2547 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002548 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002549
2550 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2551 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002552
2553 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2554 getLangOpts().DebuggerSupport) {
2555 SkipUntil(tok::semi);
2556 return Actions.ActOnNullStmt(Tok.getLocation());
2557 }
2558
John McCalldadc5752010-08-24 06:29:42 +00002559 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002560 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002561 // If the expression is invalid, skip ahead to the next semicolon. Not
2562 // doing this opens us up to the possibility of infinite loops if
2563 // ParseExpression does not consume any tokens.
2564 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002565 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002566 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002567
Steve Naroffe6016792008-02-05 21:27:35 +00002568 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002569 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002570 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002571}
2572
John McCalldadc5752010-08-24 06:29:42 +00002573ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002574 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002575 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002576 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002577 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002578 return ExprError();
2579
Ted Kremeneke65b0862012-03-06 20:05:56 +00002580 case tok::minus:
2581 case tok::plus: {
2582 tok::TokenKind Kind = Tok.getKind();
2583 SourceLocation OpLoc = ConsumeToken();
2584
2585 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002586 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002587 switch (Kind) {
2588 case tok::minus: Symbol = "-"; break;
2589 case tok::plus: Symbol = "+"; break;
2590 default: llvm_unreachable("missing unary operator case");
2591 }
2592 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2593 << Symbol;
2594 return ExprError();
2595 }
2596
2597 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2598 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002599 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002600 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002601 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002602
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002603 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002604 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002605 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002606
2607 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002608 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002609 }
2610
Chris Lattnere002fbe2007-12-12 01:04:12 +00002611 case tok::string_literal: // primary-expression: string-literal
2612 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002613 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002614
2615 case tok::char_constant:
2616 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2617
2618 case tok::numeric_constant:
2619 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2620
2621 case tok::kw_true: // Objective-C++, etc.
2622 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2623 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2624 case tok::kw_false: // Objective-C++, etc.
2625 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2626 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2627
2628 case tok::l_square:
2629 // Objective-C array literal
2630 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2631
2632 case tok::l_brace:
2633 // Objective-C dictionary literal
2634 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2635
Patrick Beard0caa3942012-04-19 00:25:12 +00002636 case tok::l_paren:
2637 // Objective-C boxed expression
2638 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2639
Chris Lattnere002fbe2007-12-12 01:04:12 +00002640 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002641 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002642 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002643
Chris Lattner197a3012008-08-05 06:19:09 +00002644 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2645 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002646 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002647 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002648 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002649 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002650 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002651 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002652 const char *str = nullptr;
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002653 if (GetLookAheadToken(1).is(tok::l_brace)) {
2654 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2655 str =
2656 ch == 't' ? "try"
2657 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002658 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002659 }
2660 if (str) {
2661 SourceLocation kwLoc = Tok.getLocation();
2662 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2663 FixItHint::CreateReplacement(kwLoc, str));
2664 }
2665 else
2666 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2667 }
Chris Lattner197a3012008-08-05 06:19:09 +00002668 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002669 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002670}
2671
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002672/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002673///
2674/// This routine parses the receiver of a message send in
2675/// Objective-C++ either as a type or as an expression. Note that this
2676/// routine must not be called to parse a send to 'super', since it
2677/// has no way to return such a result.
2678///
2679/// \param IsExpr Whether the receiver was parsed as an expression.
2680///
2681/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2682/// IsExpr is true), the parsed expression. If the receiver was parsed
2683/// as a type (\c IsExpr is false), the parsed type.
2684///
2685/// \returns True if an error occurred during parsing or semantic
2686/// analysis, in which case the arguments do not have valid
2687/// values. Otherwise, returns false for a successful parse.
2688///
2689/// objc-receiver: [C++]
2690/// 'super' [not parsed here]
2691/// expression
2692/// simple-type-specifier
2693/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002694bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002695 InMessageExpressionRAIIObject InMessage(*this, true);
2696
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002697 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2698 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002699 TryAnnotateTypeOrScopeToken();
2700
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002701 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002702 // objc-receiver:
2703 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002704 // Make sure any typos in the receiver are corrected or diagnosed, so that
2705 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2706 // only the things that are valid ObjC receivers?
2707 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002708 if (Receiver.isInvalid())
2709 return true;
2710
2711 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002712 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002713 return false;
2714 }
2715
2716 // objc-receiver:
2717 // typename-specifier
2718 // simple-type-specifier
2719 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002720 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002721 ParseCXXSimpleTypeSpecifier(DS);
2722
2723 if (Tok.is(tok::l_paren)) {
2724 // If we see an opening parentheses at this point, we are
2725 // actually parsing an expression that starts with a
2726 // function-style cast, e.g.,
2727 //
2728 // postfix-expression:
2729 // simple-type-specifier ( expression-list [opt] )
2730 // typename-specifier ( expression-list [opt] )
2731 //
2732 // Parse the remainder of this case, then the (optional)
2733 // postfix-expression suffix, followed by the (optional)
2734 // right-hand side of the binary expression. We have an
2735 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002736 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002737 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002738 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002739 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002740 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002741 if (Receiver.isInvalid())
2742 return true;
2743
2744 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002745 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002746 return false;
2747 }
2748
2749 // We have a class message. Turn the simple-type-specifier or
2750 // typename-specifier we parsed into a type and parse the
2751 // remainder of the class message.
2752 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002753 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002754 if (Type.isInvalid())
2755 return true;
2756
2757 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002758 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002759 return false;
2760}
2761
Douglas Gregor990ccac2010-05-31 14:40:22 +00002762/// \brief Determine whether the parser is currently referring to a an
2763/// Objective-C message send, using a simplified heuristic to avoid overhead.
2764///
2765/// This routine will only return true for a subset of valid message-send
2766/// expressions.
2767bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002768 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002769 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002770 return GetLookAheadToken(1).is(tok::identifier) &&
2771 GetLookAheadToken(2).is(tok::identifier);
2772}
2773
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002774bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002775 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002776 InMessageExpression)
2777 return false;
2778
2779
2780 ParsedType Type;
2781
2782 if (Tok.is(tok::annot_typename))
2783 Type = getTypeAnnotation(Tok);
2784 else if (Tok.is(tok::identifier))
2785 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2786 getCurScope());
2787 else
2788 return false;
2789
2790 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2791 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002792 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002793 if (Tok.is(tok::identifier))
2794 TryAnnotateTypeOrScopeToken();
2795
2796 return Tok.is(tok::annot_typename);
2797 }
2798 }
2799
2800 return false;
2801}
2802
Mike Stump11289f42009-09-09 15:08:12 +00002803/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002804/// '[' objc-receiver objc-message-args ']'
2805///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002806/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002807/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002808/// expression
2809/// class-name
2810/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002811///
John McCalldadc5752010-08-24 06:29:42 +00002812ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002813 assert(Tok.is(tok::l_square) && "'[' expected");
2814 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2815
Douglas Gregora817a192010-05-27 23:06:34 +00002816 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002817 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002818 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002819 return ExprError();
2820 }
2821
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002822 InMessageExpressionRAIIObject InMessage(*this, true);
2823
David Blaikiebbafb8a2012-03-11 07:00:24 +00002824 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002825 // We completely separate the C and C++ cases because C++ requires
2826 // more complicated (read: slower) parsing.
2827
2828 // Handle send to super.
2829 // FIXME: This doesn't benefit from the same typo-correction we
2830 // get in Objective-C.
2831 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002832 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002833 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002834 ParsedType(), nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002835
2836 // Parse the receiver, which is either a type or an expression.
2837 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00002838 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002839 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002840 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002841 return ExprError();
2842 }
2843
2844 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002845 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2846 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002847 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002848
2849 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002850 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00002851 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00002852 }
2853
2854 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002855 IdentifierInfo *Name = Tok.getIdentifierInfo();
2856 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002857 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002858 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002859 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002860 NextToken().is(tok::period),
2861 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002862 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002863 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002864 ParsedType(), nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002865
John McCallfaf5fb42010-08-26 23:41:50 +00002866 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002867 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002868 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002869 return ExprError();
2870 }
2871
Douglas Gregore5798dc2010-04-21 20:38:13 +00002872 ConsumeToken(); // the type name
2873
Douglas Gregore83b9562015-07-07 03:57:53 +00002874 // Parse type arguments and protocol qualifiers.
2875 if (Tok.is(tok::less)) {
2876 TypeResult NewReceiverType
2877 = ParseObjCTypeArgsAndProtocolQualifiers(NameLoc, ReceiverType);
2878 if (!NewReceiverType.isUsable()) {
2879 SkipUntil(tok::r_square, StopAtSemi);
2880 return ExprError();
2881 }
2882
2883 ReceiverType = NewReceiverType.get();
2884 }
2885
Douglas Gregore5798dc2010-04-21 20:38:13 +00002886 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00002887 ReceiverType, nullptr);
2888
John McCallfaf5fb42010-08-26 23:41:50 +00002889 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002890 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002891 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002892 }
Chris Lattner8f697062008-01-25 18:59:06 +00002893 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002894
2895 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00002896 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002897 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002898 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002899 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00002900 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002901
John McCallba7bf592010-08-24 05:47:05 +00002902 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002903 ParsedType(), Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00002904}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002905
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002906/// \brief Parse the remainder of an Objective-C message following the
2907/// '[' objc-receiver.
2908///
2909/// This routine handles sends to super, class messages (sent to a
2910/// class name), and instance messages (sent to an object), and the
2911/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2912/// ReceiverExpr, respectively. Only one of these parameters may have
2913/// a valid value.
2914///
2915/// \param LBracLoc The location of the opening '['.
2916///
2917/// \param SuperLoc If this is a send to 'super', the location of the
2918/// 'super' keyword that indicates a send to the superclass.
2919///
2920/// \param ReceiverType If this is a class message, the type of the
2921/// class we are sending a message to.
2922///
2923/// \param ReceiverExpr If this is an instance message, the expression
2924/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002925///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002926/// objc-message-args:
2927/// objc-selector
2928/// objc-keywordarg-list
2929///
2930/// objc-keywordarg-list:
2931/// objc-keywordarg
2932/// objc-keywordarg-list objc-keywordarg
2933///
Mike Stump11289f42009-09-09 15:08:12 +00002934/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002935/// selector-name[opt] ':' objc-keywordexpr
2936///
2937/// objc-keywordexpr:
2938/// nonempty-expr-list
2939///
2940/// nonempty-expr-list:
2941/// assignment-expression
2942/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002943///
John McCalldadc5752010-08-24 06:29:42 +00002944ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002945Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002946 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002947 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00002948 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002949 InMessageExpressionRAIIObject InMessage(*this, true);
2950
Steve Naroffeae65032009-11-07 02:08:14 +00002951 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002952 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002953 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002954 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002955 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002956 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002957 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002958 else
John McCallb268a282010-08-23 23:25:46 +00002959 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002960 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002961 cutOffParsing();
2962 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002963 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002964
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002965 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002966 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002967 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002968
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002969 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002970 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002971 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00002972
Chris Lattner0ef13522007-10-09 17:51:17 +00002973 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002974 while (1) {
2975 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002976 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002977 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00002978
Alp Toker383d2c42014-01-01 03:08:43 +00002979 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00002980 // We must manually skip to a ']', otherwise the expression skipper will
2981 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2982 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002983 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002984 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002985 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002986
Mike Stump11289f42009-09-09 15:08:12 +00002987 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002988
2989 if (Tok.is(tok::code_completion)) {
2990 if (SuperLoc.isValid())
2991 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002992 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002993 /*AtArgumentEpression=*/true);
2994 else if (ReceiverType)
2995 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002996 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002997 /*AtArgumentEpression=*/true);
2998 else
2999 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003000 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003001 /*AtArgumentEpression=*/true);
3002
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003003 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003004 return ExprError();
3005 }
3006
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00003007 ExprResult Expr;
3008 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3009 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3010 Expr = ParseBraceInitializer();
3011 } else
3012 Expr = ParseAssignmentExpression();
3013
3014 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003015 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00003016 // We must manually skip to a ']', otherwise the expression skipper will
3017 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3018 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003019 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003020 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00003021 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003022
Steve Naroff486760a2007-09-17 20:25:27 +00003023 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003024 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003025
Douglas Gregor1b605f72009-11-19 01:08:35 +00003026 // Code completion after each argument.
3027 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003028 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003029 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003030 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003031 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003032 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003033 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003034 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003035 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00003036 else
John McCallb268a282010-08-23 23:25:46 +00003037 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003038 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003039 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003040 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003041 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00003042 }
3043
Steve Naroff486760a2007-09-17 20:25:27 +00003044 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00003045 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00003046 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003047 break;
3048 // We have a selector or a colon, continue parsing.
3049 }
3050 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00003051 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003052 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00003053 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00003054 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00003055 if (Tok.is(tok::colon))
3056 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003057 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003058 if (Tok.is(tok::colon)) {
3059 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
3060 FixItHint::CreateRemoval(commaLoc);
3061 }
Chris Lattner197a3012008-08-05 06:19:09 +00003062 // We must manually skip to a ']', otherwise the expression skipper will
3063 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3064 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003065 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003066 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003067 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003068
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003069 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003070 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003071 }
3072 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00003073 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003074
Chris Lattner197a3012008-08-05 06:19:09 +00003075 // We must manually skip to a ']', otherwise the expression skipper will
3076 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3077 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003078 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003079 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003080 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00003081
Chris Lattner0ef13522007-10-09 17:51:17 +00003082 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00003083 Diag(Tok, diag::err_expected)
3084 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00003085 // We must manually skip to a ']', otherwise the expression skipper will
3086 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3087 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003088 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003089 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003090 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003091
Chris Lattner8f697062008-01-25 18:59:06 +00003092 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003093
Steve Naroffe61bfa82007-10-05 18:42:47 +00003094 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003095 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00003096 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003097 KeyLocs.push_back(Loc);
3098 }
Chris Lattner5700fab2007-10-07 02:00:24 +00003099 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003100
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003101 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003102 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003103 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003104 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003105 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003106 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00003107 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003108 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003109}
3110
John McCalldadc5752010-08-24 06:29:42 +00003111ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
3112 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003113 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003114
Chris Lattnere002fbe2007-12-12 01:04:12 +00003115 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
3116 // expressions. At this point, we know that the only valid thing that starts
3117 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003118 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003119 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00003120 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003121 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003122
Chris Lattnere002fbe2007-12-12 01:04:12 +00003123 while (Tok.is(tok::at)) {
3124 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00003125
Sebastian Redlc13f2682008-12-09 20:22:58 +00003126 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00003127 if (!isTokenStringLiteral())
3128 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00003129
John McCalldadc5752010-08-24 06:29:42 +00003130 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003131 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003132 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003133
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003134 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00003135 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003136
Nico Webera7c7e602012-12-31 00:28:03 +00003137 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
3138 AtStrings.size());
Anders Carlsson76f4a902007-08-21 17:43:55 +00003139}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003140
Ted Kremeneke65b0862012-03-06 20:05:56 +00003141/// ParseObjCBooleanLiteral -
3142/// objc-scalar-literal : '@' boolean-keyword
3143/// ;
3144/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
3145/// ;
3146ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
3147 bool ArgValue) {
3148 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
3149 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
3150}
3151
3152/// ParseObjCCharacterLiteral -
3153/// objc-scalar-literal : '@' character-literal
3154/// ;
3155ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
3156 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
3157 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003158 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003159 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003160 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003161 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003162}
3163
3164/// ParseObjCNumericLiteral -
3165/// objc-scalar-literal : '@' scalar-literal
3166/// ;
3167/// scalar-literal : | numeric-constant /* any numeric constant. */
3168/// ;
3169ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3170 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3171 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003172 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003173 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003174 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003175 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003176}
3177
Patrick Beard0caa3942012-04-19 00:25:12 +00003178/// ParseObjCBoxedExpr -
3179/// objc-box-expression:
3180/// @( assignment-expression )
3181ExprResult
3182Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3183 if (Tok.isNot(tok::l_paren))
3184 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3185
3186 BalancedDelimiterTracker T(*this, tok::l_paren);
3187 T.consumeOpen();
3188 ExprResult ValueExpr(ParseAssignmentExpression());
3189 if (T.consumeClose())
3190 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00003191
3192 if (ValueExpr.isInvalid())
3193 return ExprError();
3194
Patrick Beard0caa3942012-04-19 00:25:12 +00003195 // Wrap the sub-expression in a parenthesized expression, to distinguish
3196 // a boxed expression from a literal.
3197 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003198 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00003199 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003200 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00003201}
3202
Ted Kremeneke65b0862012-03-06 20:05:56 +00003203ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00003204 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003205 ConsumeBracket(); // consume the l_square.
3206
3207 while (Tok.isNot(tok::r_square)) {
3208 // Parse list of array element expressions (all must be id types).
3209 ExprResult Res(ParseAssignmentExpression());
3210 if (Res.isInvalid()) {
3211 // We must manually skip to a ']', otherwise the expression skipper will
3212 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3213 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003214 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003215 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003216 }
3217
3218 // Parse the ellipsis that indicates a pack expansion.
3219 if (Tok.is(tok::ellipsis))
3220 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
3221 if (Res.isInvalid())
3222 return true;
3223
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003224 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003225
3226 if (Tok.is(tok::comma))
3227 ConsumeToken(); // Eat the ','.
3228 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00003229 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3230 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003231 }
3232 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramerf0623432012-08-23 22:51:59 +00003233 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00003234 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003235}
3236
3237ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3238 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3239 ConsumeBrace(); // consume the l_square.
3240 while (Tok.isNot(tok::r_brace)) {
3241 // Parse the comma separated key : value expressions.
3242 ExprResult KeyExpr;
3243 {
3244 ColonProtectionRAIIObject X(*this);
3245 KeyExpr = ParseAssignmentExpression();
3246 if (KeyExpr.isInvalid()) {
3247 // We must manually skip to a '}', otherwise the expression skipper will
3248 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3249 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003250 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003251 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003252 }
3253 }
3254
Alp Toker383d2c42014-01-01 03:08:43 +00003255 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003256 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00003257 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003258 }
3259
3260 ExprResult ValueExpr(ParseAssignmentExpression());
3261 if (ValueExpr.isInvalid()) {
3262 // We must manually skip to a '}', otherwise the expression skipper will
3263 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3264 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003265 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003266 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003267 }
3268
3269 // Parse the ellipsis that designates this as a pack expansion.
3270 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00003271 if (getLangOpts().CPlusPlus)
3272 TryConsumeToken(tok::ellipsis, EllipsisLoc);
3273
Ted Kremeneke65b0862012-03-06 20:05:56 +00003274 // We have a valid expression. Collect it in a vector so we can
3275 // build the argument list.
3276 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00003277 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00003278 };
3279 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00003280
3281 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00003282 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3283 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003284 }
3285 SourceLocation EndLoc = ConsumeBrace();
3286
3287 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00003288 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
3289 Elements.data(), Elements.size());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003290}
3291
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003292/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00003293/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00003294ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003295Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00003296 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003297
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003298 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003299
Chris Lattner197a3012008-08-05 06:19:09 +00003300 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003301 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3302
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003303 BalancedDelimiterTracker T(*this, tok::l_paren);
3304 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003305
Douglas Gregor220cac52009-02-18 17:45:20 +00003306 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003307
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003308 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003309
Douglas Gregor220cac52009-02-18 17:45:20 +00003310 if (Ty.isInvalid())
3311 return ExprError();
3312
Nico Webera7c7e602012-12-31 00:28:03 +00003313 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3314 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003315}
Anders Carlssone01493d2007-08-23 15:25:28 +00003316
3317/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00003318/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00003319ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003320Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00003321 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003322
Chris Lattner197a3012008-08-05 06:19:09 +00003323 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003324 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3325
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003326 BalancedDelimiterTracker T(*this, tok::l_paren);
3327 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003328
Chris Lattner197a3012008-08-05 06:19:09 +00003329 if (Tok.isNot(tok::identifier))
Alp Tokerec543272013-12-24 09:48:30 +00003330 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003331
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003332 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00003333 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003334
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003335 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00003336
Nico Webera7c7e602012-12-31 00:28:03 +00003337 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3338 T.getOpenLocation(), ProtoIdLoc,
3339 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00003340}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003341
3342/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003343/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003344ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003345 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003346
Chris Lattner197a3012008-08-05 06:19:09 +00003347 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003348 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3349
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003350 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003351 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003352
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003353 BalancedDelimiterTracker T(*this, tok::l_paren);
3354 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003355 bool HasOptionalParen = Tok.is(tok::l_paren);
3356 if (HasOptionalParen)
3357 ConsumeParen();
3358
Douglas Gregor67c692c2010-08-26 15:07:07 +00003359 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003360 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003361 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003362 return ExprError();
3363 }
3364
Chris Lattner4f472a32009-04-11 18:13:45 +00003365 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003366 if (!SelIdent && // missing selector name.
3367 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003368 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003369
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003370 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003371
Steve Naroff152dd812007-12-05 22:21:29 +00003372 unsigned nColons = 0;
3373 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003374 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003375 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003376 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003377 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003378 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3379 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003380 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003381
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003382 if (Tok.is(tok::r_paren))
3383 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003384
3385 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003386 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003387 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003388 return ExprError();
3389 }
3390
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003391 // Check for another keyword selector.
3392 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003393 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003394 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003395 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003396 break;
3397 }
Steve Naroff152dd812007-12-05 22:21:29 +00003398 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003399 if (HasOptionalParen && Tok.is(tok::r_paren))
3400 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003401 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003402 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003403 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3404 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003405 T.getCloseLocation(),
3406 !HasOptionalParen);
Gabor Greif24032f12007-10-19 15:38:32 +00003407 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003408
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003409void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3410 // MCDecl might be null due to error in method or c-function prototype, etc.
3411 Decl *MCDecl = LM.D;
3412 bool skip = MCDecl &&
3413 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3414 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3415 if (skip)
3416 return;
3417
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003418 // Save the current token position.
3419 SourceLocation OrigLoc = Tok.getLocation();
3420
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003421 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
3422 // Append the current token at the end of the new token stream so that it
3423 // doesn't get lost.
3424 LM.Toks.push_back(Tok);
3425 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
3426
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003427 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003428 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003429
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003430 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3431 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003432 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003433 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003434 parseMethod
3435 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
3436 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003437
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003438 // Tell the actions module that we have entered a method or c-function definition
3439 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003440 if (parseMethod)
3441 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3442 else
3443 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003444 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003445 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003446 else {
3447 if (Tok.is(tok::colon))
3448 ParseConstructorInitializer(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003449 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003450 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00003451
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003452 if (Tok.getLocation() != OrigLoc) {
3453 // Due to parsing error, we either went over the cached tokens or
3454 // there are still cached tokens left. If it's the latter case skip the
3455 // leftover tokens.
3456 // Since this is an uncommon situation that should be avoided, use the
3457 // expensive isBeforeInTranslationUnit call.
3458 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3459 OrigLoc))
3460 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3461 ConsumeAnyToken();
3462 }
3463
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003464 return;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003465}