blob: 81761bf8d2d89535399793a61527db34d750e5c5 [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"
Eugene Zelenko1ced5092016-02-12 22:53:10 +000024
Chris Lattnerda59c2f2006-11-05 02:08:13 +000025using namespace clang;
26
Nico Weber04e213b2013-04-03 17:36:11 +000027/// Skips attributes after an Objective-C @ directive. Emits a diagnostic.
Nico Weber69a79142013-04-04 00:15:10 +000028void Parser::MaybeSkipAttributes(tok::ObjCKeywordKind Kind) {
Nico Weber04e213b2013-04-03 17:36:11 +000029 ParsedAttributes attrs(AttrFactory);
30 if (Tok.is(tok::kw___attribute)) {
Nico Weber69a79142013-04-04 00:15:10 +000031 if (Kind == tok::objc_interface || Kind == tok::objc_protocol)
32 Diag(Tok, diag::err_objc_postfix_attribute_hint)
33 << (Kind == tok::objc_protocol);
34 else
35 Diag(Tok, diag::err_objc_postfix_attribute);
Nico Weber04e213b2013-04-03 17:36:11 +000036 ParseGNUAttributes(attrs);
37 }
38}
Chris Lattnerda59c2f2006-11-05 02:08:13 +000039
Chris Lattner3a907162008-12-08 21:53:24 +000040/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000041/// external-declaration: [C99 6.9]
42/// [OBJC] objc-class-definition
Steve Naroffe0933392007-10-29 21:39:29 +000043/// [OBJC] objc-class-declaration
44/// [OBJC] objc-alias-declaration
45/// [OBJC] objc-protocol-definition
46/// [OBJC] objc-method-definition
47/// [OBJC] '@' 'end'
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000048Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000049 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000050
Douglas Gregorf48706c2009-12-07 09:27:33 +000051 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000052 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000053 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +000054 return nullptr;
Douglas Gregorf48706c2009-12-07 09:27:33 +000055 }
Craig Topper161e4db2014-05-21 06:02:52 +000056
57 Decl *SingleDecl = nullptr;
Steve Naroff7c348172007-08-23 18:16:40 +000058 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000059 case tok::objc_class:
60 return ParseObjCAtClassDeclaration(AtLoc);
John McCall53fa7142010-12-24 02:08:15 +000061 case tok::objc_interface: {
John McCall084e83d2011-03-24 11:26:52 +000062 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000063 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
64 break;
John McCall53fa7142010-12-24 02:08:15 +000065 }
66 case tok::objc_protocol: {
John McCall084e83d2011-03-24 11:26:52 +000067 ParsedAttributes attrs(AttrFactory);
Douglas Gregorf6102672012-01-01 21:23:57 +000068 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall53fa7142010-12-24 02:08:15 +000069 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000070 case tok::objc_implementation:
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +000071 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000072 case tok::objc_end:
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +000073 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000074 case tok::objc_compatibility_alias:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000075 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
76 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000077 case tok::objc_synthesize:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000078 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
79 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000080 case tok::objc_dynamic:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000081 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
82 break;
Douglas Gregorc50d4922012-12-11 22:11:52 +000083 case tok::objc_import:
Sean Callanan87596492014-12-09 23:47:56 +000084 if (getLangOpts().Modules || getLangOpts().DebuggerSupport)
Douglas Gregor0bf886d2012-01-03 18:24:14 +000085 return ParseModuleImport(AtLoc);
Manman Rendfcf1cb2017-01-20 20:03:00 +000086 Diag(AtLoc, diag::err_atimport);
Fariborz Jahaniana773d082014-03-26 22:02:43 +000087 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000088 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerce90ef52008-08-23 02:02:23 +000089 default:
90 Diag(AtLoc, diag::err_unexpected_at);
91 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000092 SingleDecl = nullptr;
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000093 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000094 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000095 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000096}
97
Richard Smith3df3f1d2015-11-03 01:19:56 +000098/// Class to handle popping type parameters when leaving the scope.
99class Parser::ObjCTypeParamListScope {
100 Sema &Actions;
101 Scope *S;
102 ObjCTypeParamList *Params;
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000103
Richard Smith3df3f1d2015-11-03 01:19:56 +0000104public:
105 ObjCTypeParamListScope(Sema &Actions, Scope *S)
106 : Actions(Actions), S(S), Params(nullptr) {}
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000107
Richard Smith3df3f1d2015-11-03 01:19:56 +0000108 ~ObjCTypeParamListScope() {
109 leave();
110 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000111
Richard Smith3df3f1d2015-11-03 01:19:56 +0000112 void enter(ObjCTypeParamList *P) {
113 assert(!Params);
114 Params = P;
115 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000116
Richard Smith3df3f1d2015-11-03 01:19:56 +0000117 void leave() {
118 if (Params)
119 Actions.popObjCTypeParamList(S, Params);
120 Params = nullptr;
121 }
122};
123
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000124///
Mike Stump11289f42009-09-09 15:08:12 +0000125/// objc-class-declaration:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000126/// '@' 'class' objc-class-forward-decl (',' objc-class-forward-decl)* ';'
127///
128/// objc-class-forward-decl:
129/// identifier objc-type-parameter-list[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000130///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000131Parser::DeclGroupPtrTy
132Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000133 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000134 SmallVector<IdentifierInfo *, 8> ClassNames;
135 SmallVector<SourceLocation, 8> ClassLocs;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000136 SmallVector<ObjCTypeParamList *, 8> ClassTypeParams;
Mike Stump11289f42009-09-09 15:08:12 +0000137
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000138 while (1) {
Nico Weber69a79142013-04-04 00:15:10 +0000139 MaybeSkipAttributes(tok::objc_class);
Chris Lattner0ef13522007-10-09 17:51:17 +0000140 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000141 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000142 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000143 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000144 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000145 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +0000146 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000147 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000148
Douglas Gregor85f3f952015-07-07 03:57:15 +0000149 // Parse the optional objc-type-parameter-list.
150 ObjCTypeParamList *TypeParams = nullptr;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000151 if (Tok.is(tok::less))
Douglas Gregor85f3f952015-07-07 03:57:15 +0000152 TypeParams = parseObjCTypeParamList();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000153 ClassTypeParams.push_back(TypeParams);
Alp Toker383d2c42014-01-01 03:08:43 +0000154 if (!TryConsumeToken(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000155 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000156 }
Mike Stump11289f42009-09-09 15:08:12 +0000157
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000158 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +0000159 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@class"))
Craig Topper161e4db2014-05-21 06:02:52 +0000160 return Actions.ConvertDeclToDeclGroup(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000161
Ted Kremeneka26da852009-11-17 23:12:20 +0000162 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
163 ClassLocs.data(),
Douglas Gregor85f3f952015-07-07 03:57:15 +0000164 ClassTypeParams,
Ted Kremeneka26da852009-11-17 23:12:20 +0000165 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000166}
167
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000168void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
169{
170 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
171 if (ock == Sema::OCK_None)
172 return;
173
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000174 Decl *Decl = Actions.getObjCDeclContext();
175 if (CurParsedObjCImpl) {
176 CurParsedObjCImpl->finish(AtLoc);
177 } else {
178 Actions.ActOnAtEnd(getCurScope(), AtLoc);
179 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000180 Diag(AtLoc, diag::err_objc_missing_end)
181 << FixItHint::CreateInsertion(AtLoc, "@end\n");
182 if (Decl)
183 Diag(Decl->getLocStart(), diag::note_objc_container_start)
184 << (int) ock;
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000185}
186
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000187///
188/// objc-interface:
189/// objc-class-interface-attributes[opt] objc-class-interface
190/// objc-category-interface
191///
192/// objc-class-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000193/// '@' 'interface' identifier objc-type-parameter-list[opt]
194/// objc-superclass[opt] objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000195/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000196/// objc-interface-decl-list
197/// @end
198///
199/// objc-category-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000200/// '@' 'interface' identifier objc-type-parameter-list[opt]
201/// '(' identifier[opt] ')' objc-protocol-refs[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000202/// objc-interface-decl-list
203/// @end
204///
205/// objc-superclass:
Douglas Gregore9d95f12015-07-07 03:57:35 +0000206/// ':' identifier objc-type-arguments[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000207///
208/// objc-class-interface-attributes:
209/// __attribute__((visibility("default")))
210/// __attribute__((visibility("hidden")))
211/// __attribute__((deprecated))
212/// __attribute__((unavailable))
213/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardacfbe9e2012-04-06 18:12:22 +0000214/// __attribute__((objc_root_class))
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000215///
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000216Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000217 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000218 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000219 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000220 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000221 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000222
Douglas Gregor49c22a72009-11-18 16:26:39 +0000223 // Code completion after '@interface'.
224 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000225 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000226 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000227 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000228 }
229
Nico Weber69a79142013-04-04 00:15:10 +0000230 MaybeSkipAttributes(tok::objc_interface);
Nico Weber04e213b2013-04-03 17:36:11 +0000231
Chris Lattner0ef13522007-10-09 17:51:17 +0000232 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000233 Diag(Tok, diag::err_expected)
234 << tok::identifier; // missing class or category name.
Craig Topper161e4db2014-05-21 06:02:52 +0000235 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000236 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000237
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000238 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000239 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000240 SourceLocation nameLoc = ConsumeToken();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000241
242 // Parse the objc-type-parameter-list or objc-protocol-refs. For the latter
243 // case, LAngleLoc will be valid and ProtocolIdents will capture the
244 // protocol references (that have not yet been resolved).
245 SourceLocation LAngleLoc, EndProtoLoc;
246 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
247 ObjCTypeParamList *typeParameterList = nullptr;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000248 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
249 if (Tok.is(tok::less))
250 typeParameterList = parseObjCTypeParamListOrProtocolRefs(
251 typeParamScope, LAngleLoc, ProtocolIdents, EndProtoLoc);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000252
253 if (Tok.is(tok::l_paren) &&
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000254 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000255
256 BalancedDelimiterTracker T(*this, tok::l_paren);
257 T.consumeOpen();
258
259 SourceLocation categoryLoc;
Craig Topper161e4db2014-05-21 06:02:52 +0000260 IdentifierInfo *categoryId = nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000261 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000262 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000263 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000264 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000265 }
266
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000267 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000268 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000269 categoryId = Tok.getIdentifierInfo();
270 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000271 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000272 else if (!getLangOpts().ObjC2) {
Alp Tokerec543272013-12-24 09:48:30 +0000273 Diag(Tok, diag::err_expected)
274 << tok::identifier; // missing category name.
Craig Topper161e4db2014-05-21 06:02:52 +0000275 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000276 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000277
278 T.consumeClose();
279 if (T.getCloseLocation().isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000280 return nullptr;
281
Douglas Gregor0c254a02011-09-23 19:19:41 +0000282 if (!attrs.empty()) { // categories don't support attributes.
283 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
284 attrs.clear();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000285 }
Douglas Gregor0c254a02011-09-23 19:19:41 +0000286
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000287 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000288 assert(LAngleLoc.isInvalid() && "Cannot have already parsed protocols");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000289 SmallVector<Decl *, 8> ProtocolRefs;
290 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000291 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000292 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000293 LAngleLoc, EndProtoLoc,
294 /*consumeLastToken=*/true))
Craig Topper161e4db2014-05-21 06:02:52 +0000295 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000296
John McCall48871652010-08-21 09:40:31 +0000297 Decl *CategoryType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000298 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000299 nameId, nameLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000300 typeParameterList,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000301 categoryId, categoryLoc,
302 ProtocolRefs.data(),
303 ProtocolRefs.size(),
304 ProtocolLocs.data(),
305 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000306
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000307 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000308 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000309
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000310 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000311
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000312 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000313 }
314 // Parse a class interface.
Craig Topper161e4db2014-05-21 06:02:52 +0000315 IdentifierInfo *superClassId = nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000316 SourceLocation superClassLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000317 SourceLocation typeArgsLAngleLoc;
318 SmallVector<ParsedType, 4> typeArgs;
319 SourceLocation typeArgsRAngleLoc;
320 SmallVector<Decl *, 4> protocols;
321 SmallVector<SourceLocation, 4> protocolLocs;
Chris Lattner0ef13522007-10-09 17:51:17 +0000322 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000323 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000324
325 // Code completion of superclass names.
326 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000327 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000328 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000329 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000330 }
331
Chris Lattner0ef13522007-10-09 17:51:17 +0000332 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000333 Diag(Tok, diag::err_expected)
334 << tok::identifier; // missing super class name.
Craig Topper161e4db2014-05-21 06:02:52 +0000335 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000336 }
337 superClassId = Tok.getIdentifierInfo();
338 superClassLoc = ConsumeToken();
Douglas Gregore9d95f12015-07-07 03:57:35 +0000339
340 // Type arguments for the superclass or protocol conformances.
341 if (Tok.is(tok::less)) {
David Blaikieefdccaa2016-01-15 23:43:34 +0000342 parseObjCTypeArgsOrProtocolQualifiers(
343 nullptr, typeArgsLAngleLoc, typeArgs, typeArgsRAngleLoc, LAngleLoc,
344 protocols, protocolLocs, EndProtoLoc,
345 /*consumeLastToken=*/true,
346 /*warnOnIncompleteProtocols=*/true);
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +0000347 if (Tok.is(tok::eof))
348 return nullptr;
Douglas Gregore9d95f12015-07-07 03:57:35 +0000349 }
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000350 }
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +0000351
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000352 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000353 if (LAngleLoc.isValid()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000354 if (!ProtocolIdents.empty()) {
355 // We already parsed the protocols named when we thought we had a
356 // type parameter list. Translate them into actual protocol references.
357 for (const auto &pair : ProtocolIdents) {
358 protocolLocs.push_back(pair.second);
359 }
360 Actions.FindProtocolDeclaration(/*WarnOnDeclarations=*/true,
361 /*ForObjCContainer=*/true,
Craig Toppera9247eb2015-10-22 04:59:56 +0000362 ProtocolIdents, protocols);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000363 }
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000364 } else if (protocols.empty() && Tok.is(tok::less) &&
365 ParseObjCProtocolReferences(protocols, protocolLocs, true, true,
366 LAngleLoc, EndProtoLoc,
367 /*consumeLastToken=*/true)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000368 return nullptr;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000369 }
Mike Stump11289f42009-09-09 15:08:12 +0000370
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000371 if (Tok.isNot(tok::less))
Argyrios Kyrtzidisf95a0002016-11-09 02:47:07 +0000372 Actions.ActOnTypedefedProtocols(protocols, protocolLocs,
373 superClassId, superClassLoc);
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000374
John McCall48871652010-08-21 09:40:31 +0000375 Decl *ClsType =
Douglas Gregore9d95f12015-07-07 03:57:35 +0000376 Actions.ActOnStartClassInterface(getCurScope(), AtLoc, nameId, nameLoc,
377 typeParameterList, superClassId,
378 superClassLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000379 typeArgs,
380 SourceRange(typeArgsLAngleLoc,
381 typeArgsRAngleLoc),
382 protocols.data(), protocols.size(),
383 protocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000384 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000385
Chris Lattner0ef13522007-10-09 17:51:17 +0000386 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000387 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000388
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000389 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000390
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000391 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000392}
393
Douglas Gregor813a0662015-06-19 18:14:38 +0000394/// Add an attribute for a context-sensitive type nullability to the given
395/// declarator.
396static void addContextSensitiveTypeNullability(Parser &P,
397 Declarator &D,
398 NullabilityKind nullability,
399 SourceLocation nullabilityLoc,
400 bool &addedToDeclSpec) {
401 // Create the attribute.
402 auto getNullabilityAttr = [&]() -> AttributeList * {
Douglas Gregorbec595a2015-06-19 18:27:45 +0000403 return D.getAttributePool().create(
404 P.getNullabilityKeyword(nullability),
405 SourceRange(nullabilityLoc),
406 nullptr, SourceLocation(),
407 nullptr, 0,
408 AttributeList::AS_ContextSensitiveKeyword);
Douglas Gregor813a0662015-06-19 18:14:38 +0000409 };
410
411 if (D.getNumTypeObjects() > 0) {
412 // Add the attribute to the declarator chunk nearest the declarator.
413 auto nullabilityAttr = getNullabilityAttr();
414 DeclaratorChunk &chunk = D.getTypeObject(0);
415 nullabilityAttr->setNext(chunk.getAttrListRef());
416 chunk.getAttrListRef() = nullabilityAttr;
417 } else if (!addedToDeclSpec) {
418 // Otherwise, just put it on the declaration specifiers (if one
419 // isn't there already).
420 D.getMutableDeclSpec().addAttributes(getNullabilityAttr());
421 addedToDeclSpec = true;
422 }
423}
424
Douglas Gregor85f3f952015-07-07 03:57:15 +0000425/// Parse an Objective-C type parameter list, if present, or capture
426/// the locations of the protocol identifiers for a list of protocol
427/// references.
428///
429/// objc-type-parameter-list:
430/// '<' objc-type-parameter (',' objc-type-parameter)* '>'
431///
432/// objc-type-parameter:
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000433/// objc-type-parameter-variance? identifier objc-type-parameter-bound[opt]
Douglas Gregor85f3f952015-07-07 03:57:15 +0000434///
435/// objc-type-parameter-bound:
436/// ':' type-name
437///
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000438/// objc-type-parameter-variance:
439/// '__covariant'
440/// '__contravariant'
441///
Douglas Gregor85f3f952015-07-07 03:57:15 +0000442/// \param lAngleLoc The location of the starting '<'.
443///
444/// \param protocolIdents Will capture the list of identifiers, if the
445/// angle brackets contain a list of protocol references rather than a
446/// type parameter list.
447///
448/// \param rAngleLoc The location of the ending '>'.
449ObjCTypeParamList *Parser::parseObjCTypeParamListOrProtocolRefs(
Richard Smith3df3f1d2015-11-03 01:19:56 +0000450 ObjCTypeParamListScope &Scope, SourceLocation &lAngleLoc,
451 SmallVectorImpl<IdentifierLocPair> &protocolIdents,
452 SourceLocation &rAngleLoc, bool mayBeProtocolList) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000453 assert(Tok.is(tok::less) && "Not at the beginning of a type parameter list");
454
455 // Within the type parameter list, don't treat '>' as an operator.
456 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
457
458 // Local function to "flush" the protocol identifiers, turning them into
459 // type parameters.
460 SmallVector<Decl *, 4> typeParams;
461 auto makeProtocolIdentsIntoTypeParameters = [&]() {
Douglas Gregore83b9562015-07-07 03:57:53 +0000462 unsigned index = 0;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000463 for (const auto &pair : protocolIdents) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000464 DeclResult typeParam = Actions.actOnObjCTypeParam(
David Blaikieefdccaa2016-01-15 23:43:34 +0000465 getCurScope(), ObjCTypeParamVariance::Invariant, SourceLocation(),
466 index++, pair.first, pair.second, SourceLocation(), nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000467 if (typeParam.isUsable())
468 typeParams.push_back(typeParam.get());
469 }
470
471 protocolIdents.clear();
472 mayBeProtocolList = false;
473 };
474
475 bool invalid = false;
476 lAngleLoc = ConsumeToken();
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000477
Douglas Gregor85f3f952015-07-07 03:57:15 +0000478 do {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000479 // Parse the variance, if any.
480 SourceLocation varianceLoc;
481 ObjCTypeParamVariance variance = ObjCTypeParamVariance::Invariant;
482 if (Tok.is(tok::kw___covariant) || Tok.is(tok::kw___contravariant)) {
483 variance = Tok.is(tok::kw___covariant)
484 ? ObjCTypeParamVariance::Covariant
485 : ObjCTypeParamVariance::Contravariant;
486 varianceLoc = ConsumeToken();
487
488 // Once we've seen a variance specific , we know this is not a
489 // list of protocol references.
490 if (mayBeProtocolList) {
491 // Up until now, we have been queuing up parameters because they
492 // might be protocol references. Turn them into parameters now.
493 makeProtocolIdentsIntoTypeParameters();
494 }
495 }
496
Douglas Gregor85f3f952015-07-07 03:57:15 +0000497 // Parse the identifier.
498 if (!Tok.is(tok::identifier)) {
499 // Code completion.
500 if (Tok.is(tok::code_completion)) {
501 // FIXME: If these aren't protocol references, we'll need different
502 // completions.
Craig Topper883dd332015-12-24 23:58:11 +0000503 Actions.CodeCompleteObjCProtocolReferences(protocolIdents);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000504 cutOffParsing();
505
506 // FIXME: Better recovery here?.
507 return nullptr;
508 }
509
510 Diag(Tok, diag::err_objc_expected_type_parameter);
511 invalid = true;
512 break;
513 }
514
515 IdentifierInfo *paramName = Tok.getIdentifierInfo();
516 SourceLocation paramLoc = ConsumeToken();
517
518 // If there is a bound, parse it.
519 SourceLocation colonLoc;
520 TypeResult boundType;
521 if (TryConsumeToken(tok::colon, colonLoc)) {
522 // Once we've seen a bound, we know this is not a list of protocol
523 // references.
524 if (mayBeProtocolList) {
525 // Up until now, we have been queuing up parameters because they
526 // might be protocol references. Turn them into parameters now.
527 makeProtocolIdentsIntoTypeParameters();
528 }
529
530 // type-name
531 boundType = ParseTypeName();
532 if (boundType.isInvalid())
533 invalid = true;
534 } else if (mayBeProtocolList) {
535 // If this could still be a protocol list, just capture the identifier.
536 // We don't want to turn it into a parameter.
537 protocolIdents.push_back(std::make_pair(paramName, paramLoc));
538 continue;
539 }
540
541 // Create the type parameter.
David Blaikieefdccaa2016-01-15 23:43:34 +0000542 DeclResult typeParam = Actions.actOnObjCTypeParam(
543 getCurScope(), variance, varianceLoc, typeParams.size(), paramName,
544 paramLoc, colonLoc, boundType.isUsable() ? boundType.get() : nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000545 if (typeParam.isUsable())
546 typeParams.push_back(typeParam.get());
547 } while (TryConsumeToken(tok::comma));
548
549 // Parse the '>'.
550 if (invalid) {
551 SkipUntil(tok::greater, tok::at, StopBeforeMatch);
552 if (Tok.is(tok::greater))
553 ConsumeToken();
554 } else if (ParseGreaterThanInTemplateList(rAngleLoc,
555 /*ConsumeLastToken=*/true,
556 /*ObjCGenericList=*/true)) {
557 Diag(lAngleLoc, diag::note_matching) << "'<'";
558 SkipUntil({tok::greater, tok::greaterequal, tok::at, tok::minus,
559 tok::minus, tok::plus, tok::colon, tok::l_paren, tok::l_brace,
560 tok::comma, tok::semi },
561 StopBeforeMatch);
562 if (Tok.is(tok::greater))
563 ConsumeToken();
564 }
565
566 if (mayBeProtocolList) {
567 // A type parameter list must be followed by either a ':' (indicating the
568 // presence of a superclass) or a '(' (indicating that this is a category
569 // or extension). This disambiguates between an objc-type-parameter-list
570 // and a objc-protocol-refs.
571 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_paren)) {
572 // Returning null indicates that we don't have a type parameter list.
573 // The results the caller needs to handle the protocol references are
574 // captured in the reference parameters already.
575 return nullptr;
576 }
577
578 // We have a type parameter list that looks like a list of protocol
579 // references. Turn that parameter list into type parameters.
580 makeProtocolIdentsIntoTypeParameters();
581 }
582
Richard Smith3df3f1d2015-11-03 01:19:56 +0000583 // Form the type parameter list and enter its scope.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000584 ObjCTypeParamList *list = Actions.actOnObjCTypeParamList(
585 getCurScope(),
586 lAngleLoc,
587 typeParams,
588 rAngleLoc);
Richard Smith3df3f1d2015-11-03 01:19:56 +0000589 Scope.enter(list);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000590
591 // Clear out the angle locations; they're used by the caller to indicate
592 // whether there are any protocol references.
593 lAngleLoc = SourceLocation();
594 rAngleLoc = SourceLocation();
Akira Hatanaka8ebd5802015-12-16 06:25:38 +0000595 return invalid ? nullptr : list;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000596}
597
598/// Parse an objc-type-parameter-list.
599ObjCTypeParamList *Parser::parseObjCTypeParamList() {
600 SourceLocation lAngleLoc;
601 SmallVector<IdentifierLocPair, 1> protocolIdents;
602 SourceLocation rAngleLoc;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000603
604 ObjCTypeParamListScope Scope(Actions, getCurScope());
605 return parseObjCTypeParamListOrProtocolRefs(Scope, lAngleLoc, protocolIdents,
606 rAngleLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000607 /*mayBeProtocolList=*/false);
608}
609
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000610/// objc-interface-decl-list:
611/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000612/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000613/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000614/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000615/// objc-interface-decl-list declaration
616/// objc-interface-decl-list ';'
617///
Steve Naroff99264b42007-08-22 16:35:03 +0000618/// objc-method-requirement: [OBJC2]
619/// @required
620/// @optional
621///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000622void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
623 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000624 SmallVector<Decl *, 32> allMethods;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000625 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000626 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000627
Ted Kremenekc7c64312010-01-07 01:20:12 +0000628 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000629
Steve Naroff99264b42007-08-22 16:35:03 +0000630 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000631 // If this is a method prototype, parse it.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000632 if (Tok.isOneOf(tok::minus, tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000633 if (Decl *methodPrototype =
634 ParseObjCMethodPrototype(MethodImplKind, false))
635 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000636 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
637 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000638 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
639 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000640 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000641 if (Tok.is(tok::semi))
642 ConsumeToken();
643 }
Steve Naroff99264b42007-08-22 16:35:03 +0000644 continue;
645 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000646 if (Tok.is(tok::l_paren)) {
647 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000648 ParseObjCMethodDecl(Tok.getLocation(),
649 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000650 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000651 continue;
652 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000653 // Ignore excess semicolons.
654 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000655 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000656 continue;
657 }
Mike Stump11289f42009-09-09 15:08:12 +0000658
Chris Lattnerda9fb152008-10-20 06:10:06 +0000659 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000660 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000661 break;
Mike Stump11289f42009-09-09 15:08:12 +0000662
Douglas Gregorf1934162010-01-13 21:24:21 +0000663 // Code completion within an Objective-C interface.
664 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000665 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000666 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000667 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000668 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000669 }
670
Chris Lattner038a3e32008-10-20 05:46:22 +0000671 // If we don't have an @ directive, parse it as a function definition.
672 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000673 // The code below does not consume '}'s because it is afraid of eating the
674 // end of a namespace. Because of the way this code is structured, an
675 // erroneous r_brace would cause an infinite loop if not handled here.
676 if (Tok.is(tok::r_brace))
677 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000678 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000679 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000680 continue;
681 }
Mike Stump11289f42009-09-09 15:08:12 +0000682
Chris Lattner038a3e32008-10-20 05:46:22 +0000683 // Otherwise, we have an @ directive, eat the @.
684 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000685 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000686 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000687 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000688 }
689
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000690 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000691
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000692 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000693 AtEnd.setBegin(AtLoc);
694 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000695 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000696 } else if (DirectiveKind == tok::objc_not_keyword) {
697 Diag(Tok, diag::err_objc_unknown_at);
698 SkipUntil(tok::semi);
699 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000700 }
Mike Stump11289f42009-09-09 15:08:12 +0000701
Chris Lattnerda9fb152008-10-20 06:10:06 +0000702 // Eat the identifier.
703 ConsumeToken();
704
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000705 switch (DirectiveKind) {
706 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000707 // FIXME: If someone forgets an @end on a protocol, this loop will
708 // continue to eat up tons of stuff and spew lots of nonsense errors. It
709 // would probably be better to bail out if we saw an @class or @interface
710 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000711 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000712 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000713 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000714 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000715
716 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000717 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000718 Diag(AtLoc, diag::err_objc_missing_end)
719 << FixItHint::CreateInsertion(AtLoc, "@end\n");
720 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
721 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000722 ConsumeToken();
723 break;
724
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000725 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000726 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000727 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000728 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000729 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000730 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000731 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000732 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000733 break;
Mike Stump11289f42009-09-09 15:08:12 +0000734
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000735 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000736 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000737 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000738
Chris Lattner038a3e32008-10-20 05:46:22 +0000739 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000740 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000741 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000742 if (Tok.is(tok::l_paren)) {
743 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000744 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000745 }
Mike Stump11289f42009-09-09 15:08:12 +0000746
Douglas Gregor813a0662015-06-19 18:14:38 +0000747 bool addedToDeclSpec = false;
Benjamin Kramera39beb92014-09-03 11:06:10 +0000748 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
749 if (FD.D.getIdentifier() == nullptr) {
750 Diag(AtLoc, diag::err_objc_property_requires_field_name)
751 << FD.D.getSourceRange();
752 return;
753 }
754 if (FD.BitfieldSize) {
755 Diag(AtLoc, diag::err_objc_property_bitfield)
756 << FD.D.getSourceRange();
757 return;
758 }
759
Douglas Gregor813a0662015-06-19 18:14:38 +0000760 // Map a nullability property attribute to a context-sensitive keyword
761 // attribute.
762 if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
763 addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
764 OCDS.getNullabilityLoc(),
765 addedToDeclSpec);
766
Benjamin Kramera39beb92014-09-03 11:06:10 +0000767 // Install the property declarator into interfaceDecl.
768 IdentifierInfo *SelName =
769 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
770
771 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
772 IdentifierInfo *SetterName = OCDS.getSetterName();
773 Selector SetterSel;
774 if (SetterName)
775 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
776 else
777 SetterSel = SelectorTable::constructSetterSelector(
778 PP.getIdentifierTable(), PP.getSelectorTable(),
779 FD.D.getIdentifier());
Benjamin Kramera39beb92014-09-03 11:06:10 +0000780 Decl *Property = Actions.ActOnProperty(
781 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000782 MethodImplKind);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000783
784 FD.complete(Property);
785 };
John McCallcfefb6d2009-11-03 02:38:08 +0000786
Chris Lattner038a3e32008-10-20 05:46:22 +0000787 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000788 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000789 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000790
John McCall405988b2011-03-26 01:53:26 +0000791 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000792 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000793 }
Steve Naroff99264b42007-08-22 16:35:03 +0000794 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000795
796 // We break out of the big loop in two cases: when we see @end or when we see
797 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000798 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000799 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000800 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000801 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000802 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000803 } else {
804 Diag(Tok, diag::err_objc_missing_end)
805 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
806 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
807 << (int) Actions.getObjCContainerKind();
808 AtEnd.setBegin(Tok.getLocation());
809 AtEnd.setEnd(Tok.getLocation());
810 }
Mike Stump11289f42009-09-09 15:08:12 +0000811
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000812 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000813 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000814 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000815}
816
Douglas Gregor813a0662015-06-19 18:14:38 +0000817/// Diagnose redundant or conflicting nullability information.
818static void diagnoseRedundantPropertyNullability(Parser &P,
819 ObjCDeclSpec &DS,
820 NullabilityKind nullability,
821 SourceLocation nullabilityLoc){
822 if (DS.getNullability() == nullability) {
823 P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000824 << DiagNullabilityKind(nullability, true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000825 << SourceRange(DS.getNullabilityLoc());
826 return;
827 }
828
829 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000830 << DiagNullabilityKind(nullability, true)
831 << DiagNullabilityKind(DS.getNullability(), true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000832 << SourceRange(DS.getNullabilityLoc());
833}
834
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000835/// Parse property attribute declarations.
836///
837/// property-attr-decl: '(' property-attrlist ')'
838/// property-attrlist:
839/// property-attribute
840/// property-attrlist ',' property-attribute
841/// property-attribute:
842/// getter '=' identifier
843/// setter '=' identifier ':'
844/// readonly
845/// readwrite
846/// assign
847/// retain
848/// copy
849/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000850/// atomic
851/// strong
852/// weak
853/// unsafe_unretained
Douglas Gregor813a0662015-06-19 18:14:38 +0000854/// nonnull
855/// nullable
856/// null_unspecified
Douglas Gregor849ebc22015-06-19 18:14:46 +0000857/// null_resettable
Manman Ren387ff7f2016-01-26 18:52:43 +0000858/// class
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000859///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000860void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000861 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000862 BalancedDelimiterTracker T(*this, tok::l_paren);
863 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000864
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000865 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000866 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000867 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000868 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000869 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000870 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000871
Chris Lattner76619232008-10-20 07:22:18 +0000872 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000873 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000874 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000875 return;
876 }
Mike Stump11289f42009-09-09 15:08:12 +0000877
Chris Lattner1db33542008-10-20 07:37:22 +0000878 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000879
Chris Lattner68e48682008-11-20 04:42:34 +0000880 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000881 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000882 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000883 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000884 else if (II->isStr("unsafe_unretained"))
885 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000886 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000887 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000888 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000889 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000890 else if (II->isStr("strong"))
891 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000892 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000893 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000894 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000895 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000896 else if (II->isStr("atomic"))
897 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000898 else if (II->isStr("weak"))
899 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000900 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000901 bool IsSetter = II->getNameStart()[0] == 's';
902
Chris Lattner825bca12008-10-20 07:39:53 +0000903 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000904 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
Craig Topper3110a5c2015-11-14 18:16:02 +0000905 diag::err_objc_expected_equal_for_getter;
Anders Carlssonfe15a782010-10-02 17:45:21 +0000906
Alp Toker383d2c42014-01-01 03:08:43 +0000907 if (ExpectAndConsume(tok::equal, DiagID)) {
908 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000909 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000910 }
Mike Stump11289f42009-09-09 15:08:12 +0000911
Douglas Gregorc8537c52009-11-19 07:41:15 +0000912 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000913 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000914 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000915 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000916 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000917 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000918 }
919
Anders Carlssonfe15a782010-10-02 17:45:21 +0000920 SourceLocation SelLoc;
921 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
922
923 if (!SelIdent) {
924 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
925 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000926 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000927 return;
928 }
Mike Stump11289f42009-09-09 15:08:12 +0000929
Anders Carlssonfe15a782010-10-02 17:45:21 +0000930 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000931 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000932 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000933
Alp Toker383d2c42014-01-01 03:08:43 +0000934 if (ExpectAndConsume(tok::colon,
935 diag::err_expected_colon_after_setter_name)) {
936 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000937 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000938 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000939 } else {
940 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000941 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000942 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000943 } else if (II->isStr("nonnull")) {
944 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
945 diagnoseRedundantPropertyNullability(*this, DS,
946 NullabilityKind::NonNull,
947 Tok.getLocation());
948 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
949 DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
950 } else if (II->isStr("nullable")) {
951 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
952 diagnoseRedundantPropertyNullability(*this, DS,
953 NullabilityKind::Nullable,
954 Tok.getLocation());
955 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
956 DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
957 } else if (II->isStr("null_unspecified")) {
958 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
959 diagnoseRedundantPropertyNullability(*this, DS,
960 NullabilityKind::Unspecified,
961 Tok.getLocation());
962 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
963 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000964 } else if (II->isStr("null_resettable")) {
965 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
966 diagnoseRedundantPropertyNullability(*this, DS,
967 NullabilityKind::Unspecified,
968 Tok.getLocation());
969 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
970 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
971
972 // Also set the null_resettable bit.
973 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
Manman Ren387ff7f2016-01-26 18:52:43 +0000974 } else if (II->isStr("class")) {
975 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_class);
Chris Lattner825bca12008-10-20 07:39:53 +0000976 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000977 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000978 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000979 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000980 }
Mike Stump11289f42009-09-09 15:08:12 +0000981
Chris Lattner1db33542008-10-20 07:37:22 +0000982 if (Tok.isNot(tok::comma))
983 break;
Mike Stump11289f42009-09-09 15:08:12 +0000984
Chris Lattner1db33542008-10-20 07:37:22 +0000985 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000986 }
Mike Stump11289f42009-09-09 15:08:12 +0000987
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000988 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000989}
990
Steve Naroff09bf8152007-09-06 21:24:23 +0000991/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000992/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000993/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000994///
995/// objc-instance-method: '-'
996/// objc-class-method: '+'
997///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000998/// objc-method-attributes: [OBJC2]
999/// __attribute__((deprecated))
1000///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001001Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001002 bool MethodDefinition) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001003 assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +00001004
Mike Stump11289f42009-09-09 15:08:12 +00001005 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +00001006 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001007 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001008 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +00001009 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +00001010 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +00001011 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +00001012}
1013
1014/// objc-selector:
1015/// identifier
1016/// one of
1017/// enum struct union if else while do for switch case default
1018/// break continue return goto asm sizeof typeof __alignof
1019/// unsigned long const short volatile signed restrict _Complex
1020/// in out inout bycopy byref oneway int char float double void _Bool
1021///
Chris Lattner4f472a32009-04-11 18:13:45 +00001022IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +00001023
Chris Lattner5700fab2007-10-07 02:00:24 +00001024 switch (Tok.getKind()) {
1025 default:
Craig Topper161e4db2014-05-21 06:02:52 +00001026 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001027 case tok::ampamp:
1028 case tok::ampequal:
1029 case tok::amp:
1030 case tok::pipe:
1031 case tok::tilde:
1032 case tok::exclaim:
1033 case tok::exclaimequal:
1034 case tok::pipepipe:
1035 case tok::pipeequal:
1036 case tok::caret:
1037 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +00001038 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +00001039 if (isLetter(ThisTok[0])) {
Malcolm Parsons731ca0e2016-11-03 12:25:51 +00001040 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok);
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001041 Tok.setKind(tok::identifier);
1042 SelectorLoc = ConsumeToken();
1043 return II;
1044 }
Craig Topper161e4db2014-05-21 06:02:52 +00001045 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001046 }
1047
Chris Lattner5700fab2007-10-07 02:00:24 +00001048 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001049 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +00001050 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001051 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001052 case tok::kw_break:
1053 case tok::kw_case:
1054 case tok::kw_catch:
1055 case tok::kw_char:
1056 case tok::kw_class:
1057 case tok::kw_const:
1058 case tok::kw_const_cast:
1059 case tok::kw_continue:
1060 case tok::kw_default:
1061 case tok::kw_delete:
1062 case tok::kw_do:
1063 case tok::kw_double:
1064 case tok::kw_dynamic_cast:
1065 case tok::kw_else:
1066 case tok::kw_enum:
1067 case tok::kw_explicit:
1068 case tok::kw_export:
1069 case tok::kw_extern:
1070 case tok::kw_false:
1071 case tok::kw_float:
1072 case tok::kw_for:
1073 case tok::kw_friend:
1074 case tok::kw_goto:
1075 case tok::kw_if:
1076 case tok::kw_inline:
1077 case tok::kw_int:
1078 case tok::kw_long:
1079 case tok::kw_mutable:
1080 case tok::kw_namespace:
1081 case tok::kw_new:
1082 case tok::kw_operator:
1083 case tok::kw_private:
1084 case tok::kw_protected:
1085 case tok::kw_public:
1086 case tok::kw_register:
1087 case tok::kw_reinterpret_cast:
1088 case tok::kw_restrict:
1089 case tok::kw_return:
1090 case tok::kw_short:
1091 case tok::kw_signed:
1092 case tok::kw_sizeof:
1093 case tok::kw_static:
1094 case tok::kw_static_cast:
1095 case tok::kw_struct:
1096 case tok::kw_switch:
1097 case tok::kw_template:
1098 case tok::kw_this:
1099 case tok::kw_throw:
1100 case tok::kw_true:
1101 case tok::kw_try:
1102 case tok::kw_typedef:
1103 case tok::kw_typeid:
1104 case tok::kw_typename:
1105 case tok::kw_typeof:
1106 case tok::kw_union:
1107 case tok::kw_unsigned:
1108 case tok::kw_using:
1109 case tok::kw_virtual:
1110 case tok::kw_void:
1111 case tok::kw_volatile:
1112 case tok::kw_wchar_t:
1113 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +00001114 case tok::kw__Bool:
1115 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001116 case tok::kw___alignof:
Richard Smithe301ba22015-11-11 02:02:15 +00001117 case tok::kw___auto_type:
Chris Lattner5700fab2007-10-07 02:00:24 +00001118 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001119 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +00001120 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +00001121 }
Steve Naroff99264b42007-08-22 16:35:03 +00001122}
1123
Fariborz Jahanian83615522008-01-02 22:54:34 +00001124/// objc-for-collection-in: 'in'
1125///
Fariborz Jahanian3622e592008-01-04 23:04:08 +00001126bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001127 // FIXME: May have to do additional look-ahead to only allow for
1128 // valid tokens following an 'in'; such as an identifier, unary operators,
1129 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001130 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +00001131 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +00001132}
1133
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001134/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +00001135/// qualifier list and builds their bitmask representation in the input
1136/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +00001137///
1138/// objc-type-qualifiers:
1139/// objc-type-qualifier
1140/// objc-type-qualifiers objc-type-qualifier
1141///
Douglas Gregor813a0662015-06-19 18:14:38 +00001142/// objc-type-qualifier:
1143/// 'in'
1144/// 'out'
1145/// 'inout'
1146/// 'oneway'
1147/// 'bycopy'
1148/// 'byref'
1149/// 'nonnull'
1150/// 'nullable'
1151/// 'null_unspecified'
1152///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001153void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001154 Declarator::TheContext Context) {
1155 assert(Context == Declarator::ObjCParameterContext ||
1156 Context == Declarator::ObjCResultContext);
1157
Chris Lattner61511e12007-12-12 06:56:32 +00001158 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +00001159 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +00001160 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +00001161 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001162 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +00001163 }
1164
Chris Lattner5e530bc2007-12-27 19:57:00 +00001165 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +00001166 return;
Mike Stump11289f42009-09-09 15:08:12 +00001167
Chris Lattner61511e12007-12-12 06:56:32 +00001168 const IdentifierInfo *II = Tok.getIdentifierInfo();
1169 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001170 if (II != ObjCTypeQuals[i] ||
1171 NextToken().is(tok::less) ||
1172 NextToken().is(tok::coloncolon))
Chris Lattner61511e12007-12-12 06:56:32 +00001173 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001174
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001175 ObjCDeclSpec::ObjCDeclQualifier Qual;
Douglas Gregor813a0662015-06-19 18:14:38 +00001176 NullabilityKind Nullability;
Chris Lattner61511e12007-12-12 06:56:32 +00001177 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +00001178 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001179 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
1180 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
1181 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
1182 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
1183 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
1184 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Douglas Gregor813a0662015-06-19 18:14:38 +00001185
1186 case objc_nonnull:
1187 Qual = ObjCDeclSpec::DQ_CSNullability;
1188 Nullability = NullabilityKind::NonNull;
1189 break;
1190
1191 case objc_nullable:
1192 Qual = ObjCDeclSpec::DQ_CSNullability;
1193 Nullability = NullabilityKind::Nullable;
1194 break;
1195
1196 case objc_null_unspecified:
1197 Qual = ObjCDeclSpec::DQ_CSNullability;
1198 Nullability = NullabilityKind::Unspecified;
1199 break;
Chris Lattner61511e12007-12-12 06:56:32 +00001200 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001201
1202 // FIXME: Diagnose redundant specifiers.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001203 DS.setObjCDeclQualifier(Qual);
Douglas Gregor813a0662015-06-19 18:14:38 +00001204 if (Qual == ObjCDeclSpec::DQ_CSNullability)
1205 DS.setNullability(Tok.getLocation(), Nullability);
1206
Chris Lattner61511e12007-12-12 06:56:32 +00001207 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001208 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +00001209 break;
1210 }
Mike Stump11289f42009-09-09 15:08:12 +00001211
Chris Lattner61511e12007-12-12 06:56:32 +00001212 // If this wasn't a recognized qualifier, bail out.
1213 if (II) return;
1214 }
1215}
1216
John McCalla55902b2011-10-01 09:56:14 +00001217/// Take all the decl attributes out of the given list and add
1218/// them to the given attribute set.
1219static void takeDeclAttributes(ParsedAttributes &attrs,
1220 AttributeList *list) {
1221 while (list) {
1222 AttributeList *cur = list;
1223 list = cur->getNext();
1224
1225 if (!cur->isUsedAsTypeAttr()) {
1226 // Clear out the next pointer. We're really completely
1227 // destroying the internal invariants of the declarator here,
1228 // but it doesn't matter because we're done with it.
Craig Topper161e4db2014-05-21 06:02:52 +00001229 cur->setNext(nullptr);
John McCalla55902b2011-10-01 09:56:14 +00001230 attrs.add(cur);
1231 }
1232 }
1233}
1234
1235/// takeDeclAttributes - Take all the decl attributes from the given
1236/// declarator and add them to the given list.
1237static void takeDeclAttributes(ParsedAttributes &attrs,
1238 Declarator &D) {
1239 // First, take ownership of all attributes.
1240 attrs.getPool().takeAllFrom(D.getAttributePool());
1241 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
1242
1243 // Now actually move the attributes over.
1244 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
1245 takeDeclAttributes(attrs, D.getAttributes());
1246 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
1247 takeDeclAttributes(attrs,
1248 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
1249}
1250
Chris Lattner61511e12007-12-12 06:56:32 +00001251/// objc-type-name:
1252/// '(' objc-type-qualifiers[opt] type-name ')'
1253/// '(' objc-type-qualifiers[opt] ')'
1254///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001255ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001256 Declarator::TheContext context,
1257 ParsedAttributes *paramAttrs) {
1258 assert(context == Declarator::ObjCParameterContext ||
1259 context == Declarator::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +00001260 assert((paramAttrs != nullptr) ==
1261 (context == Declarator::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +00001262
Chris Lattner0ef13522007-10-09 17:51:17 +00001263 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +00001264
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001265 BalancedDelimiterTracker T(*this, tok::l_paren);
1266 T.consumeOpen();
1267
Chris Lattner2ebb1782008-08-23 01:48:03 +00001268 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001269 ObjCDeclContextSwitch ObjCDC(*this);
1270
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001271 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001272 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +00001273
John McCallba7bf592010-08-24 05:47:05 +00001274 ParsedType Ty;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001275 if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
John McCalla55902b2011-10-01 09:56:14 +00001276 // Parse an abstract declarator.
1277 DeclSpec declSpec(AttrFactory);
1278 declSpec.setObjCQualifiers(&DS);
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001279 DeclSpecContext dsContext = DSC_normal;
1280 if (context == Declarator::ObjCResultContext)
1281 dsContext = DSC_objc_method_result;
1282 ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
John McCalla55902b2011-10-01 09:56:14 +00001283 Declarator declarator(declSpec, context);
1284 ParseDeclarator(declarator);
1285
1286 // If that's not invalid, extract a type.
1287 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001288 // Map a nullability specifier to a context-sensitive keyword attribute.
1289 bool addedToDeclSpec = false;
1290 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1291 addContextSensitiveTypeNullability(*this, declarator,
1292 DS.getNullability(),
1293 DS.getNullabilityLoc(),
1294 addedToDeclSpec);
1295
John McCalla55902b2011-10-01 09:56:14 +00001296 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1297 if (!type.isInvalid())
1298 Ty = type.get();
1299
1300 // If we're parsing a parameter, steal all the decl attributes
1301 // and add them to the decl spec.
1302 if (context == Declarator::ObjCParameterContext)
1303 takeDeclAttributes(*paramAttrs, declarator);
1304 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001305 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001306
Steve Naroff90255b42008-10-21 14:15:04 +00001307 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001308 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001309 else if (Tok.getLocation() == TypeStartLoc) {
1310 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001311 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001312 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001313 } else {
1314 // Otherwise, we found *something*, but didn't get a ')' in the right
1315 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001316 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001317 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001318 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001319}
1320
1321/// objc-method-decl:
1322/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001323/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001324/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001325/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001326///
1327/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001328/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001329/// objc-keyword-selector objc-keyword-decl
1330///
1331/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001332/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1333/// objc-selector ':' objc-keyword-attributes[opt] identifier
1334/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1335/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001336///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001337/// objc-parmlist:
1338/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001339///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001340/// objc-parms:
1341/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001342///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001343/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001344/// , ...
1345///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001346/// objc-keyword-attributes: [OBJC2]
1347/// __attribute__((unused))
1348///
John McCall48871652010-08-21 09:40:31 +00001349Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001350 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001351 tok::ObjCKeywordKind MethodImplKind,
1352 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001353 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001354
Douglas Gregor636a61e2010-04-07 00:21:17 +00001355 if (Tok.is(tok::code_completion)) {
David Blaikieefdccaa2016-01-15 23:43:34 +00001356 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
1357 /*ReturnType=*/nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001358 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001359 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001360 }
1361
Chris Lattner2ebb1782008-08-23 01:48:03 +00001362 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001363 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001364 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001365 if (Tok.is(tok::l_paren))
Craig Topper161e4db2014-05-21 06:02:52 +00001366 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext,
1367 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001368
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001369 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001370 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001371 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001372 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001373
Douglas Gregor636a61e2010-04-07 00:21:17 +00001374 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001375 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001376 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001377 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001378 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001379 }
1380
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001381 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001382 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001383 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001384
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001385 // An unnamed colon is valid.
1386 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001387 Diag(Tok, diag::err_expected_selector_for_method)
1388 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001389 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001390 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001391 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001392 }
Mike Stump11289f42009-09-09 15:08:12 +00001393
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001394 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001395 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001396 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001397 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001398 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001399
Chris Lattner5700fab2007-10-07 02:00:24 +00001400 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001401 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001402 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001403 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001404 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001405 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001406 methodAttrs.getList(), MethodImplKind,
1407 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001408 PD.complete(Result);
1409 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001410 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001411
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001412 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001413 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001414 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001415 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1416 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001417
1418 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001419 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001420 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001421 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001422
Chris Lattner5700fab2007-10-07 02:00:24 +00001423 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001424 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001425 break;
Mike Stump11289f42009-09-09 15:08:12 +00001426
David Blaikieefdccaa2016-01-15 23:43:34 +00001427 ArgInfo.Type = nullptr;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001428 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001429 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1430 Declarator::ObjCParameterContext,
1431 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001432
Chris Lattner5700fab2007-10-07 02:00:24 +00001433 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001434 // Regardless, collect all the attributes we've parsed so far.
Craig Topper161e4db2014-05-21 06:02:52 +00001435 ArgInfo.ArgAttrs = nullptr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001436 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001437 MaybeParseGNUAttributes(paramAttrs);
1438 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001439 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001440
Douglas Gregor45879692010-07-08 23:37:41 +00001441 // Code completion for the next piece of the selector.
1442 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001443 KeyIdents.push_back(SelIdent);
1444 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1445 mType == tok::minus,
1446 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001447 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001448 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001449 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001450 }
1451
Chris Lattner0ef13522007-10-09 17:51:17 +00001452 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001453 Diag(Tok, diag::err_expected)
1454 << tok::identifier; // missing argument name.
Chris Lattner5700fab2007-10-07 02:00:24 +00001455 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001456 }
Mike Stump11289f42009-09-09 15:08:12 +00001457
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001458 ArgInfo.Name = Tok.getIdentifierInfo();
1459 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001460 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001461
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001462 ArgInfos.push_back(ArgInfo);
1463 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001464 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001465
John McCall084e83d2011-03-24 11:26:52 +00001466 // Make sure the attributes persist.
1467 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1468
Douglas Gregor95887f92010-07-08 23:20:03 +00001469 // Code completion for the next piece of the selector.
1470 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001471 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1472 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001473 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001474 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001475 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001476 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001477 }
1478
Chris Lattner5700fab2007-10-07 02:00:24 +00001479 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001480 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001481 if (!SelIdent && Tok.isNot(tok::colon))
1482 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001483 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001484 SourceLocation ColonLoc = Tok.getLocation();
1485 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001486 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1487 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1488 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001489 }
1490 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001491 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001492 }
Mike Stump11289f42009-09-09 15:08:12 +00001493
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001494 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001495 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001496 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001497 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001498 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001499 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001500 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001501 ConsumeToken();
1502 break;
1503 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001504 if (!cStyleParamWarned) {
1505 Diag(Tok, diag::warn_cstyle_param);
1506 cStyleParamWarned = true;
1507 }
John McCall084e83d2011-03-24 11:26:52 +00001508 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001509 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001510 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001511 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1512 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001513 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001514 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001515 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1516 ParmDecl.getIdentifierLoc(),
1517 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001518 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001519 }
Mike Stump11289f42009-09-09 15:08:12 +00001520
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001521 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001522 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001523 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001524 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001525
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001526 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001527 return nullptr;
1528
Chris Lattner5700fab2007-10-07 02:00:24 +00001529 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1530 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001531 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001532 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001533 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001534 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001535 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001536 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001537 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001538
John McCall28a6aea2009-11-04 02:18:39 +00001539 PD.complete(Result);
1540 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001541}
1542
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001543/// objc-protocol-refs:
1544/// '<' identifier-list '>'
1545///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001546bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001547ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1548 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001549 bool WarnOnDeclarations, bool ForObjCContainer,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001550 SourceLocation &LAngleLoc, SourceLocation &EndLoc,
1551 bool consumeLastToken) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001552 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001553
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001554 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001555
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001556 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001557
Chris Lattner3bbae002008-07-26 04:03:38 +00001558 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001559 if (Tok.is(tok::code_completion)) {
Craig Topper883dd332015-12-24 23:58:11 +00001560 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001561 cutOffParsing();
1562 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001563 }
1564
Chris Lattner3bbae002008-07-26 04:03:38 +00001565 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001566 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001567 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001568 return true;
1569 }
1570 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1571 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001572 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001573 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001574
Alp Toker383d2c42014-01-01 03:08:43 +00001575 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001576 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001577 }
Mike Stump11289f42009-09-09 15:08:12 +00001578
Chris Lattner3bbae002008-07-26 04:03:38 +00001579 // Consume the '>'.
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001580 if (ParseGreaterThanInTemplateList(EndLoc, consumeLastToken,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001581 /*ObjCGenericList=*/false))
Chris Lattner3bbae002008-07-26 04:03:38 +00001582 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001583
Chris Lattner3bbae002008-07-26 04:03:38 +00001584 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001585 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Craig Toppera9247eb2015-10-22 04:59:56 +00001586 ProtocolIdents, Protocols);
Chris Lattner3bbae002008-07-26 04:03:38 +00001587 return false;
1588}
1589
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001590TypeResult Parser::parseObjCProtocolQualifierType(SourceLocation &rAngleLoc) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001591 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001592 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001593
1594 SourceLocation lAngleLoc;
1595 SmallVector<Decl *, 8> protocols;
1596 SmallVector<SourceLocation, 8> protocolLocs;
1597 (void)ParseObjCProtocolReferences(protocols, protocolLocs, false, false,
1598 lAngleLoc, rAngleLoc,
1599 /*consumeLastToken=*/true);
1600 TypeResult result = Actions.actOnObjCProtocolQualifierType(lAngleLoc,
1601 protocols,
1602 protocolLocs,
1603 rAngleLoc);
1604 if (result.isUsable()) {
1605 Diag(lAngleLoc, diag::warn_objc_protocol_qualifier_missing_id)
1606 << FixItHint::CreateInsertion(lAngleLoc, "id")
1607 << SourceRange(lAngleLoc, rAngleLoc);
1608 }
1609
1610 return result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001611}
1612
Douglas Gregore9d95f12015-07-07 03:57:35 +00001613/// Parse Objective-C type arguments or protocol qualifiers.
1614///
1615/// objc-type-arguments:
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001616/// '<' type-name '...'[opt] (',' type-name '...'[opt])* '>'
Douglas Gregore9d95f12015-07-07 03:57:35 +00001617///
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001618void Parser::parseObjCTypeArgsOrProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001619 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001620 SourceLocation &typeArgsLAngleLoc,
1621 SmallVectorImpl<ParsedType> &typeArgs,
1622 SourceLocation &typeArgsRAngleLoc,
1623 SourceLocation &protocolLAngleLoc,
1624 SmallVectorImpl<Decl *> &protocols,
1625 SmallVectorImpl<SourceLocation> &protocolLocs,
1626 SourceLocation &protocolRAngleLoc,
1627 bool consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001628 bool warnOnIncompleteProtocols) {
1629 assert(Tok.is(tok::less) && "Not at the start of type args or protocols");
1630 SourceLocation lAngleLoc = ConsumeToken();
1631
1632 // Whether all of the elements we've parsed thus far are single
1633 // identifiers, which might be types or might be protocols.
1634 bool allSingleIdentifiers = true;
1635 SmallVector<IdentifierInfo *, 4> identifiers;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001636 SmallVectorImpl<SourceLocation> &identifierLocs = protocolLocs;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001637
1638 // Parse a list of comma-separated identifiers, bailing out if we
1639 // see something different.
1640 do {
1641 // Parse a single identifier.
1642 if (Tok.is(tok::identifier) &&
1643 (NextToken().is(tok::comma) ||
1644 NextToken().is(tok::greater) ||
1645 NextToken().is(tok::greatergreater))) {
1646 identifiers.push_back(Tok.getIdentifierInfo());
1647 identifierLocs.push_back(ConsumeToken());
1648 continue;
1649 }
1650
1651 if (Tok.is(tok::code_completion)) {
1652 // FIXME: Also include types here.
1653 SmallVector<IdentifierLocPair, 4> identifierLocPairs;
1654 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1655 identifierLocPairs.push_back(IdentifierLocPair(identifiers[i],
1656 identifierLocs[i]));
1657 }
1658
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001659 QualType BaseT = Actions.GetTypeFromParser(baseType);
1660 if (!BaseT.isNull() && BaseT->acceptsObjCTypeParams()) {
1661 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
1662 } else {
Craig Topper883dd332015-12-24 23:58:11 +00001663 Actions.CodeCompleteObjCProtocolReferences(identifierLocPairs);
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001664 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001665 cutOffParsing();
1666 return;
1667 }
1668
1669 allSingleIdentifiers = false;
1670 break;
1671 } while (TryConsumeToken(tok::comma));
1672
1673 // If we parsed an identifier list, semantic analysis sorts out
1674 // whether it refers to protocols or to type arguments.
1675 if (allSingleIdentifiers) {
1676 // Parse the closing '>'.
1677 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001678 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001679 /*ObjCGenericList=*/true);
1680
1681 // Let Sema figure out what we parsed.
1682 Actions.actOnObjCTypeArgsOrProtocolQualifiers(getCurScope(),
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001683 baseType,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001684 lAngleLoc,
1685 identifiers,
1686 identifierLocs,
1687 rAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001688 typeArgsLAngleLoc,
1689 typeArgs,
1690 typeArgsRAngleLoc,
1691 protocolLAngleLoc,
1692 protocols,
1693 protocolRAngleLoc,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001694 warnOnIncompleteProtocols);
1695 return;
1696 }
1697
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001698 // We parsed an identifier list but stumbled into non single identifiers, this
1699 // means we might (a) check that what we already parsed is a legitimate type
1700 // (not a protocol or unknown type) and (b) parse the remaining ones, which
1701 // must all be type args.
Douglas Gregore9d95f12015-07-07 03:57:35 +00001702
1703 // Convert the identifiers into type arguments.
1704 bool invalid = false;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001705 IdentifierInfo *foundProtocolId = nullptr, *foundValidTypeId = nullptr;
1706 SourceLocation foundProtocolSrcLoc, foundValidTypeSrcLoc;
1707 SmallVector<IdentifierInfo *, 2> unknownTypeArgs;
1708 SmallVector<SourceLocation, 2> unknownTypeArgsLoc;
1709
Douglas Gregore9d95f12015-07-07 03:57:35 +00001710 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1711 ParsedType typeArg
1712 = Actions.getTypeName(*identifiers[i], identifierLocs[i], getCurScope());
1713 if (typeArg) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001714 DeclSpec DS(AttrFactory);
1715 const char *prevSpec = nullptr;
1716 unsigned diagID;
1717 DS.SetTypeSpecType(TST_typename, identifierLocs[i], prevSpec, diagID,
1718 typeArg, Actions.getASTContext().getPrintingPolicy());
1719
1720 // Form a declarator to turn this into a type.
1721 Declarator D(DS, Declarator::TypeNameContext);
1722 TypeResult fullTypeArg = Actions.ActOnTypeName(getCurScope(), D);
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001723 if (fullTypeArg.isUsable()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001724 typeArgs.push_back(fullTypeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001725 if (!foundValidTypeId) {
1726 foundValidTypeId = identifiers[i];
1727 foundValidTypeSrcLoc = identifierLocs[i];
1728 }
1729 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001730 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001731 unknownTypeArgs.push_back(identifiers[i]);
1732 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1733 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001734 } else {
1735 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001736 if (!Actions.LookupProtocol(identifiers[i], identifierLocs[i])) {
1737 unknownTypeArgs.push_back(identifiers[i]);
1738 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1739 } else if (!foundProtocolId) {
1740 foundProtocolId = identifiers[i];
1741 foundProtocolSrcLoc = identifierLocs[i];
1742 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001743 }
1744 }
1745
1746 // Continue parsing type-names.
1747 do {
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001748 Token CurTypeTok = Tok;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001749 TypeResult typeArg = ParseTypeName();
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001750
1751 // Consume the '...' for a pack expansion.
1752 SourceLocation ellipsisLoc;
1753 TryConsumeToken(tok::ellipsis, ellipsisLoc);
1754 if (typeArg.isUsable() && ellipsisLoc.isValid()) {
1755 typeArg = Actions.ActOnPackExpansion(typeArg.get(), ellipsisLoc);
1756 }
1757
Douglas Gregore9d95f12015-07-07 03:57:35 +00001758 if (typeArg.isUsable()) {
1759 typeArgs.push_back(typeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001760 if (!foundValidTypeId) {
1761 foundValidTypeId = CurTypeTok.getIdentifierInfo();
1762 foundValidTypeSrcLoc = CurTypeTok.getLocation();
1763 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001764 } else {
1765 invalid = true;
1766 }
1767 } while (TryConsumeToken(tok::comma));
1768
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001769 // Diagnose the mix between type args and protocols.
1770 if (foundProtocolId && foundValidTypeId)
1771 Actions.DiagnoseTypeArgsAndProtocols(foundProtocolId, foundProtocolSrcLoc,
1772 foundValidTypeId,
1773 foundValidTypeSrcLoc);
1774
1775 // Diagnose unknown arg types.
1776 ParsedType T;
1777 if (unknownTypeArgs.size())
1778 for (unsigned i = 0, e = unknownTypeArgsLoc.size(); i < e; ++i)
1779 Actions.DiagnoseUnknownTypeName(unknownTypeArgs[i], unknownTypeArgsLoc[i],
1780 getCurScope(), nullptr, T);
1781
Douglas Gregore9d95f12015-07-07 03:57:35 +00001782 // Parse the closing '>'.
1783 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001784 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001785 /*ObjCGenericList=*/true);
1786
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001787 if (invalid) {
1788 typeArgs.clear();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001789 return;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001790 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001791
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001792 // Record left/right angle locations.
1793 typeArgsLAngleLoc = lAngleLoc;
1794 typeArgsRAngleLoc = rAngleLoc;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001795}
1796
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001797void Parser::parseObjCTypeArgsAndProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001798 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001799 SourceLocation &typeArgsLAngleLoc,
1800 SmallVectorImpl<ParsedType> &typeArgs,
1801 SourceLocation &typeArgsRAngleLoc,
1802 SourceLocation &protocolLAngleLoc,
1803 SmallVectorImpl<Decl *> &protocols,
1804 SmallVectorImpl<SourceLocation> &protocolLocs,
1805 SourceLocation &protocolRAngleLoc,
1806 bool consumeLastToken) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001807 assert(Tok.is(tok::less));
1808
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001809 // Parse the first angle-bracket-delimited clause.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001810 parseObjCTypeArgsOrProtocolQualifiers(baseType,
1811 typeArgsLAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001812 typeArgs,
1813 typeArgsRAngleLoc,
1814 protocolLAngleLoc,
1815 protocols,
1816 protocolLocs,
1817 protocolRAngleLoc,
1818 consumeLastToken,
Douglas Gregore83b9562015-07-07 03:57:53 +00001819 /*warnOnIncompleteProtocols=*/false);
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00001820 if (Tok.is(tok::eof)) // Nothing else to do here...
1821 return;
Douglas Gregore83b9562015-07-07 03:57:53 +00001822
1823 // An Objective-C object pointer followed by type arguments
1824 // can then be followed again by a set of protocol references, e.g.,
1825 // \c NSArray<NSView><NSTextDelegate>
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001826 if ((consumeLastToken && Tok.is(tok::less)) ||
1827 (!consumeLastToken && NextToken().is(tok::less))) {
1828 // If we aren't consuming the last token, the prior '>' is still hanging
1829 // there. Consume it before we parse the protocol qualifiers.
1830 if (!consumeLastToken)
1831 ConsumeToken();
1832
1833 if (!protocols.empty()) {
1834 SkipUntilFlags skipFlags = SkipUntilFlags();
1835 if (!consumeLastToken)
1836 skipFlags = skipFlags | StopBeforeMatch;
Douglas Gregore83b9562015-07-07 03:57:53 +00001837 Diag(Tok, diag::err_objc_type_args_after_protocols)
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001838 << SourceRange(protocolLAngleLoc, protocolRAngleLoc);
1839 SkipUntil(tok::greater, tok::greatergreater, skipFlags);
Douglas Gregore83b9562015-07-07 03:57:53 +00001840 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001841 ParseObjCProtocolReferences(protocols, protocolLocs,
1842 /*WarnOnDeclarations=*/false,
1843 /*ForObjCContainer=*/false,
1844 protocolLAngleLoc, protocolRAngleLoc,
1845 consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001846 }
1847 }
1848}
1849
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001850TypeResult Parser::parseObjCTypeArgsAndProtocolQualifiers(
1851 SourceLocation loc,
1852 ParsedType type,
1853 bool consumeLastToken,
1854 SourceLocation &endLoc) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001855 assert(Tok.is(tok::less));
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001856 SourceLocation typeArgsLAngleLoc;
1857 SmallVector<ParsedType, 4> typeArgs;
1858 SourceLocation typeArgsRAngleLoc;
1859 SourceLocation protocolLAngleLoc;
1860 SmallVector<Decl *, 4> protocols;
1861 SmallVector<SourceLocation, 4> protocolLocs;
1862 SourceLocation protocolRAngleLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00001863
1864 // Parse type arguments and protocol qualifiers.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001865 parseObjCTypeArgsAndProtocolQualifiers(type, typeArgsLAngleLoc, typeArgs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001866 typeArgsRAngleLoc, protocolLAngleLoc,
1867 protocols, protocolLocs,
1868 protocolRAngleLoc, consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001869
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00001870 if (Tok.is(tok::eof))
1871 return true; // Invalid type result.
1872
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001873 // Compute the location of the last token.
1874 if (consumeLastToken)
1875 endLoc = PrevTokLocation;
1876 else
1877 endLoc = Tok.getLocation();
1878
1879 return Actions.actOnObjCTypeArgsAndProtocolQualifiers(
1880 getCurScope(),
1881 loc,
1882 type,
1883 typeArgsLAngleLoc,
1884 typeArgs,
1885 typeArgsRAngleLoc,
1886 protocolLAngleLoc,
1887 protocols,
1888 protocolLocs,
1889 protocolRAngleLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00001890}
1891
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001892void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1893 BalancedDelimiterTracker &T,
1894 SmallVectorImpl<Decl *> &AllIvarDecls,
1895 bool RBraceMissing) {
1896 if (!RBraceMissing)
1897 T.consumeClose();
1898
1899 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1900 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1901 Actions.ActOnObjCContainerFinishDefinition();
1902 // Call ActOnFields() even if we don't have any decls. This is useful
1903 // for code rewriting tools that need to be aware of the empty list.
1904 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1905 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001906 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001907}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001908
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001909/// objc-class-instance-variables:
1910/// '{' objc-instance-variable-decl-list[opt] '}'
1911///
1912/// objc-instance-variable-decl-list:
1913/// objc-visibility-spec
1914/// objc-instance-variable-decl ';'
1915/// ';'
1916/// objc-instance-variable-decl-list objc-visibility-spec
1917/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1918/// objc-instance-variable-decl-list ';'
1919///
1920/// objc-visibility-spec:
1921/// @private
1922/// @protected
1923/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001924/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001925///
1926/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001927/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001928///
John McCall48871652010-08-21 09:40:31 +00001929void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001930 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001931 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001932 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001933 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001934
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001935 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001936 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001937
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001938 BalancedDelimiterTracker T(*this, tok::l_brace);
1939 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001940 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001941 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001942 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001943
Steve Naroff00433d32007-08-21 21:17:12 +00001944 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001945 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001946 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001947 continue;
1948 }
Mike Stump11289f42009-09-09 15:08:12 +00001949
Steve Naroff00433d32007-08-21 21:17:12 +00001950 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001951 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001952 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001953 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001954 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001955 }
1956
Steve Naroff7c348172007-08-23 18:16:40 +00001957 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001958 case tok::objc_private:
1959 case tok::objc_public:
1960 case tok::objc_protected:
1961 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001962 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001963 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001964 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001965
1966 case tok::objc_end:
1967 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001968 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1969 Tok.setKind(tok::at);
1970 Tok.setLength(1);
1971 PP.EnterToken(Tok);
1972 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1973 T, AllIvarDecls, true);
1974 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001975
1976 default:
1977 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1978 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001979 }
1980 }
Mike Stump11289f42009-09-09 15:08:12 +00001981
Douglas Gregor48d46252010-01-13 21:54:15 +00001982 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001983 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001984 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001985 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001986 }
John McCallcfefb6d2009-11-03 02:38:08 +00001987
Benjamin Kramera39beb92014-09-03 11:06:10 +00001988 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1989 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1990 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001991 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001992 Decl *Field = Actions.ActOnIvar(
1993 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1994 FD.BitfieldSize, visibility);
1995 Actions.ActOnObjCContainerFinishDefinition();
1996 if (Field)
1997 AllIvarDecls.push_back(Field);
1998 FD.complete(Field);
1999 };
John McCallcfefb6d2009-11-03 02:38:08 +00002000
Chris Lattnera12405b2008-04-10 06:46:29 +00002001 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002002 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00002003 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00002004
Chris Lattner0ef13522007-10-09 17:51:17 +00002005 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00002006 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00002007 } else {
2008 Diag(Tok, diag::err_expected_semi_decl_list);
2009 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00002010 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00002011 }
2012 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00002013 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
2014 T, AllIvarDecls, false);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002015}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002016
2017/// objc-protocol-declaration:
2018/// objc-protocol-definition
2019/// objc-protocol-forward-reference
2020///
2021/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00002022/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00002023/// objc-protocol-refs[opt]
2024/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00002025/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002026///
2027/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00002028/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002029///
James Dennett1355bd12012-06-11 06:19:40 +00002030/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00002031/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002032/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00002033Parser::DeclGroupPtrTy
2034Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
2035 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00002036 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002037 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
2038 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002039
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002040 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002041 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002042 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002043 return nullptr;
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002044 }
2045
Nico Weber69a79142013-04-04 00:15:10 +00002046 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00002047
Chris Lattner0ef13522007-10-09 17:51:17 +00002048 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002049 Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
David Blaikie0403cb12016-01-15 23:43:25 +00002050 return nullptr;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002051 }
2052 // Save the protocol name, then consume it.
2053 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
2054 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002055
Alp Toker383d2c42014-01-01 03:08:43 +00002056 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00002057 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Craig Topper0f723bb2015-10-22 05:00:01 +00002058 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtoInfo,
John McCall53fa7142010-12-24 02:08:15 +00002059 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002060 }
Mike Stump11289f42009-09-09 15:08:12 +00002061
Erik Verbruggenf9887852011-12-08 09:58:43 +00002062 CheckNestedObjCContexts(AtLoc);
2063
Chris Lattner0ef13522007-10-09 17:51:17 +00002064 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002065 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002066 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
2067
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002068 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002069 while (1) {
2070 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00002071 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002072 Diag(Tok, diag::err_expected) << tok::identifier;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002073 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002074 return nullptr;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002075 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00002076 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
2077 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002078 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00002079
Chris Lattner0ef13522007-10-09 17:51:17 +00002080 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002081 break;
2082 }
2083 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00002084 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
David Blaikie0403cb12016-01-15 23:43:25 +00002085 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002086
Craig Topper0f723bb2015-10-22 05:00:01 +00002087 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtocolRefs,
John McCall53fa7142010-12-24 02:08:15 +00002088 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00002089 }
Mike Stump11289f42009-09-09 15:08:12 +00002090
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002091 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00002092 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002093
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002094 SmallVector<Decl *, 8> ProtocolRefs;
2095 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002096 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00002097 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002098 LAngleLoc, EndProtoLoc,
2099 /*consumeLastToken=*/true))
David Blaikie0403cb12016-01-15 23:43:25 +00002100 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002101
John McCall48871652010-08-21 09:40:31 +00002102 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00002103 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002104 ProtocolRefs.data(),
2105 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00002106 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00002107 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002108
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00002109 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00002110 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002111}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002112
2113/// objc-implementation:
2114/// objc-class-implementation-prologue
2115/// objc-category-implementation-prologue
2116///
2117/// objc-class-implementation-prologue:
2118/// @implementation identifier objc-superclass[opt]
2119/// objc-class-instance-variables[opt]
2120///
2121/// objc-category-implementation-prologue:
2122/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002123Parser::DeclGroupPtrTy
2124Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002125 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
2126 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002127 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002128 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002129
Douglas Gregor49c22a72009-11-18 16:26:39 +00002130 // Code completion after '@implementation'.
2131 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002132 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002133 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002134 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +00002135 }
2136
Nico Weber69a79142013-04-04 00:15:10 +00002137 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00002138
Chris Lattner0ef13522007-10-09 17:51:17 +00002139 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002140 Diag(Tok, diag::err_expected)
2141 << tok::identifier; // missing class or category name.
David Blaikie0403cb12016-01-15 23:43:25 +00002142 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002143 }
2144 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00002145 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002146 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00002147 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002148
Douglas Gregor85f3f952015-07-07 03:57:15 +00002149 // Neither a type parameter list nor a list of protocol references is
2150 // permitted here. Parse and diagnose them.
2151 if (Tok.is(tok::less)) {
2152 SourceLocation lAngleLoc, rAngleLoc;
2153 SmallVector<IdentifierLocPair, 8> protocolIdents;
2154 SourceLocation diagLoc = Tok.getLocation();
Richard Smith3df3f1d2015-11-03 01:19:56 +00002155 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
2156 if (parseObjCTypeParamListOrProtocolRefs(typeParamScope, lAngleLoc,
2157 protocolIdents, rAngleLoc)) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00002158 Diag(diagLoc, diag::err_objc_parameterized_implementation)
2159 << SourceRange(diagLoc, PrevTokLocation);
2160 } else if (lAngleLoc.isValid()) {
2161 Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
2162 << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
2163 }
2164 }
2165
Mike Stump11289f42009-09-09 15:08:12 +00002166 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002167 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002168 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002169 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002170 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002171
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002172 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002173 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002174 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002175 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002176 }
2177
Chris Lattner0ef13522007-10-09 17:51:17 +00002178 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002179 categoryId = Tok.getIdentifierInfo();
2180 categoryLoc = ConsumeToken();
2181 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002182 Diag(Tok, diag::err_expected)
2183 << tok::identifier; // missing category name.
David Blaikie0403cb12016-01-15 23:43:25 +00002184 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002185 }
Chris Lattner0ef13522007-10-09 17:51:17 +00002186 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002187 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002188 SkipUntil(tok::r_paren); // don't stop at ';'
David Blaikie0403cb12016-01-15 23:43:25 +00002189 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002190 }
2191 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00002192 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2193 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002194 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2195 SmallVector<Decl *, 4> protocols;
2196 SmallVector<SourceLocation, 4> protocolLocs;
2197 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2198 /*warnOnIncompleteProtocols=*/false,
2199 /*ForObjCContainer=*/false,
2200 protocolLAngleLoc, protocolRAngleLoc,
2201 /*consumeLastToken=*/true);
Fariborz Jahanian85888552013-05-17 17:58:11 +00002202 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002203 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002204 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00002205 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002206
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002207 } else {
2208 // We have a class implementation
2209 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002210 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00002211 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002212 // We have a super class
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002213 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002214 Diag(Tok, diag::err_expected)
2215 << tok::identifier; // missing super class name.
David Blaikie0403cb12016-01-15 23:43:25 +00002216 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002217 }
2218 superClassId = Tok.getIdentifierInfo();
2219 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002220 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002221 ObjCImpDecl = Actions.ActOnStartClassImplementation(
2222 AtLoc, nameId, nameLoc,
2223 superClassId, superClassLoc);
2224
2225 if (Tok.is(tok::l_brace)) // we have ivars
2226 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002227 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2228 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002229
2230 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2231 SmallVector<Decl *, 4> protocols;
2232 SmallVector<SourceLocation, 4> protocolLocs;
2233 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2234 /*warnOnIncompleteProtocols=*/false,
2235 /*ForObjCContainer=*/false,
2236 protocolLAngleLoc, protocolRAngleLoc,
2237 /*consumeLastToken=*/true);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002238 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002239 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002240 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002241
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002242 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002243
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002244 {
2245 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00002246 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002247 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002248 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002249 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
2250 DeclGroupRef DG = DGP.get();
2251 DeclsInGroup.append(DG.begin(), DG.end());
2252 }
2253 }
2254 }
2255
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00002256 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002257}
Steve Naroff33a1e802007-10-29 21:38:07 +00002258
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002259Parser::DeclGroupPtrTy
2260Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002261 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
2262 "ParseObjCAtEndDeclaration(): Expected @end");
2263 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002264 if (CurParsedObjCImpl)
2265 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002266 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00002267 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002268 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
David Blaikie0403cb12016-01-15 23:43:25 +00002269 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002270}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002271
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002272Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
2273 if (!Finished) {
2274 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00002275 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002276 P.Diag(P.Tok, diag::err_objc_missing_end)
2277 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
2278 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
2279 << Sema::OCK_Implementation;
2280 }
2281 }
Craig Topper161e4db2014-05-21 06:02:52 +00002282 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002283 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002284}
2285
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002286void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
2287 assert(!Finished);
2288 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
2289 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002290 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2291 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002292
2293 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
2294
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002295 if (HasCFunction)
2296 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
2297 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2298 false/*c-functions*/);
2299
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002300 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002301 for (LateParsedObjCMethodContainer::iterator
2302 I = LateParsedObjCMethods.begin(),
2303 E = LateParsedObjCMethods.end(); I != E; ++I)
2304 delete *I;
2305 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002306
2307 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002308}
2309
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002310/// compatibility-alias-decl:
2311/// @compatibility_alias alias-name class-name ';'
2312///
John McCall48871652010-08-21 09:40:31 +00002313Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002314 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
2315 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
2316 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00002317 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002318 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002319 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002320 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002321 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
2322 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00002323 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002324 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002325 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002326 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002327 IdentifierInfo *classId = Tok.getIdentifierInfo();
2328 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00002329 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00002330 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
2331 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002332}
2333
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002334/// property-synthesis:
2335/// @synthesize property-ivar-list ';'
2336///
2337/// property-ivar-list:
2338/// property-ivar
2339/// property-ivar-list ',' property-ivar
2340///
2341/// property-ivar:
2342/// identifier
2343/// identifier '=' identifier
2344///
John McCall48871652010-08-21 09:40:31 +00002345Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002346 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00002347 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002348 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00002349
Douglas Gregor88e72a02009-11-18 19:45:45 +00002350 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00002351 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002352 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002353 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002354 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002355 }
2356
Douglas Gregor88e72a02009-11-18 19:45:45 +00002357 if (Tok.isNot(tok::identifier)) {
2358 Diag(Tok, diag::err_synthesized_property_name);
2359 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002360 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00002361 }
Craig Topper161e4db2014-05-21 06:02:52 +00002362
2363 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002364 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2365 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002366 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00002367 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002368 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00002369 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002370 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002371 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002372 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002373 }
2374
Chris Lattner0ef13522007-10-09 17:51:17 +00002375 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002376 Diag(Tok, diag::err_expected) << tok::identifier;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002377 break;
2378 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002379 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002380 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002381 }
Manman Ren5b786402016-01-28 18:49:28 +00002382 Actions.ActOnPropertyImplDecl(
2383 getCurScope(), atLoc, propertyLoc, true,
2384 propertyId, propertyIvar, propertyIvarLoc,
2385 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Chris Lattner0ef13522007-10-09 17:51:17 +00002386 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002387 break;
2388 ConsumeToken(); // consume ','
2389 }
Alp Toker383d2c42014-01-01 03:08:43 +00002390 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00002391 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002392}
2393
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002394/// property-dynamic:
2395/// @dynamic property-list
2396///
2397/// property-list:
2398/// identifier
2399/// property-list ',' identifier
2400///
John McCall48871652010-08-21 09:40:31 +00002401Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002402 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2403 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002404 ConsumeToken(); // consume dynamic
Manman Ren0fe61f82016-01-29 19:05:57 +00002405
2406 bool isClassProperty = false;
2407 if (Tok.is(tok::l_paren)) {
2408 ConsumeParen();
2409 const IdentifierInfo *II = Tok.getIdentifierInfo();
2410
2411 if (!II) {
2412 Diag(Tok, diag::err_objc_expected_property_attr) << II;
2413 SkipUntil(tok::r_paren, StopAtSemi);
2414 } else {
2415 SourceLocation AttrName = ConsumeToken(); // consume attribute name
2416 if (II->isStr("class")) {
2417 isClassProperty = true;
2418 if (Tok.isNot(tok::r_paren)) {
2419 Diag(Tok, diag::err_expected) << tok::r_paren;
2420 SkipUntil(tok::r_paren, StopAtSemi);
2421 } else
2422 ConsumeParen();
2423 } else {
2424 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
2425 SkipUntil(tok::r_paren, StopAtSemi);
2426 }
2427 }
2428 }
2429
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002430 while (true) {
2431 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002432 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002433 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002434 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002435 }
2436
2437 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002438 Diag(Tok, diag::err_expected) << tok::identifier;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002439 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002440 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002441 }
2442
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002443 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2444 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Manman Ren5b786402016-01-28 18:49:28 +00002445 Actions.ActOnPropertyImplDecl(
2446 getCurScope(), atLoc, propertyLoc, false,
2447 propertyId, nullptr, SourceLocation(),
Manman Ren0fe61f82016-01-29 19:05:57 +00002448 isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
Manman Ren5b786402016-01-28 18:49:28 +00002449 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002450
Chris Lattner0ef13522007-10-09 17:51:17 +00002451 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002452 break;
2453 ConsumeToken(); // consume ','
2454 }
Alp Toker383d2c42014-01-01 03:08:43 +00002455 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00002456 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002457}
Mike Stump11289f42009-09-09 15:08:12 +00002458
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002459/// objc-throw-statement:
2460/// throw expression[opt];
2461///
John McCalldadc5752010-08-24 06:29:42 +00002462StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2463 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002464 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00002465 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00002466 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002467 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002468 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002469 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002470 }
2471 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00002472 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00002473 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002474 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002475}
2476
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002477/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002478/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002479///
John McCalldadc5752010-08-24 06:29:42 +00002480StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002481Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002482 ConsumeToken(); // consume synchronized
2483 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002484 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002485 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002486 }
John McCalld9bb7432011-07-27 21:50:02 +00002487
2488 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002489 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00002490 ExprResult operand(ParseExpression());
2491
2492 if (Tok.is(tok::r_paren)) {
2493 ConsumeParen(); // ')'
2494 } else {
2495 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002496 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00002497
2498 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002499 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002500 }
John McCalld9bb7432011-07-27 21:50:02 +00002501
2502 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002503 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00002504 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002505 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002506 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002507 }
Steve Naroffd9c26072008-06-04 20:36:13 +00002508
John McCalld9bb7432011-07-27 21:50:02 +00002509 // Check the @synchronized operand now.
2510 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002511 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002512
John McCalld9bb7432011-07-27 21:50:02 +00002513 // Parse the compound statement within a new scope.
2514 ParseScope bodyScope(this, Scope::DeclScope);
2515 StmtResult body(ParseCompoundStatementBody());
2516 bodyScope.Exit();
2517
2518 // If there was a semantic or parse error earlier with the
2519 // operand, fail now.
2520 if (operand.isInvalid())
2521 return StmtError();
2522
2523 if (body.isInvalid())
2524 body = Actions.ActOnNullStmt(Tok.getLocation());
2525
2526 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002527}
2528
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002529/// objc-try-catch-statement:
2530/// @try compound-statement objc-catch-list[opt]
2531/// @try compound-statement objc-catch-list[opt] @finally compound-statement
2532///
2533/// objc-catch-list:
2534/// @catch ( parameter-declaration ) compound-statement
2535/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2536/// catch-parameter-declaration:
2537/// parameter-declaration
2538/// '...' [OBJC2]
2539///
John McCalldadc5752010-08-24 06:29:42 +00002540StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002541 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002542
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002543 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00002544 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002545 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002546 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002547 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00002548 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00002549 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002550 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00002551 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002552 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002553 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002554 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002555
Chris Lattner0ef13522007-10-09 17:51:17 +00002556 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002557 // At this point, we need to lookahead to determine if this @ is the start
2558 // of an @catch or @finally. We don't want to consume the @ token if this
2559 // is an @try or @encode or something else.
2560 Token AfterAt = GetLookAheadToken(1);
2561 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2562 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2563 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002564
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002565 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002566 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002567 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002568 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002569 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002570 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00002571 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002572 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002573 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002574 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00002575 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002576 ParseDeclarator(ParmDecl);
2577
Douglas Gregore11ee112010-04-23 23:01:43 +00002578 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002579 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002580 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002581 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002582 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002583
Steve Naroff65a00892009-04-07 22:56:58 +00002584 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002585
Steve Naroff65a00892009-04-07 22:56:58 +00002586 if (Tok.is(tok::r_paren))
2587 RParenLoc = ConsumeParen();
2588 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002589 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002590
John McCalldadc5752010-08-24 06:29:42 +00002591 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002592 if (Tok.is(tok::l_brace))
2593 CatchBody = ParseCompoundStatementBody();
2594 else
Alp Tokerec543272013-12-24 09:48:30 +00002595 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002596 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002597 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00002598
John McCalldadc5752010-08-24 06:29:42 +00002599 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00002600 RParenLoc,
2601 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002602 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002603 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002604 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002605
Steve Naroffe6016792008-02-05 21:27:35 +00002606 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002607 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2608 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002609 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002610 }
2611 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002612 } else {
2613 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002614 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002615 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002616
John McCalldadc5752010-08-24 06:29:42 +00002617 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002618 if (Tok.is(tok::l_brace))
2619 FinallyBody = ParseCompoundStatementBody();
2620 else
Alp Tokerec543272013-12-24 09:48:30 +00002621 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002622 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002623 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002624 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002625 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002626 catch_or_finally_seen = true;
2627 break;
2628 }
2629 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002630 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002631 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002632 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002633 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002634
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002635 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002636 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002637 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002638}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002639
John McCall31168b02011-06-15 23:02:42 +00002640/// objc-autoreleasepool-statement:
2641/// @autoreleasepool compound-statement
2642///
2643StmtResult
2644Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2645 ConsumeToken(); // consume autoreleasepool
2646 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002647 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002648 return StmtError();
2649 }
2650 // Enter a scope to hold everything within the compound stmt. Compound
2651 // statements can always hold declarations.
2652 ParseScope BodyScope(this, Scope::DeclScope);
2653
2654 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2655
2656 BodyScope.Exit();
2657 if (AutoreleasePoolBody.isInvalid())
2658 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2659 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002660 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002661}
2662
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002663/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2664/// for later parsing.
2665void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002666 if (SkipFunctionBodies && (!MDecl || Actions.canSkipFunctionBody(MDecl)) &&
2667 trySkippingFunctionBody()) {
2668 Actions.ActOnSkippedFunctionBody(MDecl);
2669 return;
2670 }
2671
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002672 LexedMethod* LM = new LexedMethod(this, MDecl);
2673 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2674 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002675 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002676 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002677 if (Tok.is(tok::kw_try)) {
2678 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002679 if (Tok.is(tok::colon)) {
2680 Toks.push_back(Tok);
2681 ConsumeToken();
2682 while (Tok.isNot(tok::l_brace)) {
2683 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2684 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2685 }
2686 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002687 Toks.push_back(Tok); // also store '{'
2688 }
2689 else if (Tok.is(tok::colon)) {
2690 ConsumeToken();
Richard Smithb9fa9962015-08-21 03:04:33 +00002691 // FIXME: This is wrong, due to C++11 braced initialization.
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002692 while (Tok.isNot(tok::l_brace)) {
2693 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2694 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2695 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002696 Toks.push_back(Tok); // also store '{'
2697 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002698 ConsumeBrace();
2699 // Consume everything up to (and including) the matching right brace.
2700 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002701 while (Tok.is(tok::kw_catch)) {
2702 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2703 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2704 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002705}
2706
Steve Naroff09bf8152007-09-06 21:24:23 +00002707/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002708///
John McCall48871652010-08-21 09:40:31 +00002709Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002710 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002711
John McCallfaf5fb42010-08-26 23:41:50 +00002712 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2713 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002714
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002715 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002716 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002717 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002718 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002719 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002720 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002721 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002722 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002723
Steve Naroffbb875722007-11-11 19:54:21 +00002724 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002725 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002726 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002727
Steve Naroffbb875722007-11-11 19:54:21 +00002728 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002729 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002730
Steve Naroffbb875722007-11-11 19:54:21 +00002731 // If we didn't find the '{', bail out.
2732 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002733 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002734 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002735
2736 if (!MDecl) {
2737 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002738 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002739 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002740 }
2741
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002742 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002743 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002744 assert (CurParsedObjCImpl
2745 && "ParseObjCMethodDefinition - Method out of @implementation");
2746 // Consume the tokens and store them for later parsing.
2747 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002748 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002749}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002750
John McCalldadc5752010-08-24 06:29:42 +00002751StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002752 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002753 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002754 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002755 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002756 }
2757
2758 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002759 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002760
2761 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002762 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002763
2764 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002765 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002766
2767 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2768 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002769
2770 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2771 getLangOpts().DebuggerSupport) {
2772 SkipUntil(tok::semi);
2773 return Actions.ActOnNullStmt(Tok.getLocation());
2774 }
2775
Alex Lorenza589abc2016-12-01 12:14:38 +00002776 ExprStatementTokLoc = AtLoc;
John McCalldadc5752010-08-24 06:29:42 +00002777 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002778 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002779 // If the expression is invalid, skip ahead to the next semicolon. Not
2780 // doing this opens us up to the possibility of infinite loops if
2781 // ParseExpression does not consume any tokens.
2782 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002783 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002784 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002785
Steve Naroffe6016792008-02-05 21:27:35 +00002786 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002787 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002788 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002789}
2790
John McCalldadc5752010-08-24 06:29:42 +00002791ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002792 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002793 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002794 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002795 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002796 return ExprError();
2797
Ted Kremeneke65b0862012-03-06 20:05:56 +00002798 case tok::minus:
2799 case tok::plus: {
2800 tok::TokenKind Kind = Tok.getKind();
2801 SourceLocation OpLoc = ConsumeToken();
2802
2803 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002804 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002805 switch (Kind) {
2806 case tok::minus: Symbol = "-"; break;
2807 case tok::plus: Symbol = "+"; break;
2808 default: llvm_unreachable("missing unary operator case");
2809 }
2810 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2811 << Symbol;
2812 return ExprError();
2813 }
2814
2815 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2816 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002817 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002818 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002819 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002820
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002821 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002822 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002823 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002824
2825 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002826 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002827 }
2828
Chris Lattnere002fbe2007-12-12 01:04:12 +00002829 case tok::string_literal: // primary-expression: string-literal
2830 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002831 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002832
2833 case tok::char_constant:
2834 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2835
2836 case tok::numeric_constant:
2837 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2838
2839 case tok::kw_true: // Objective-C++, etc.
2840 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2841 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2842 case tok::kw_false: // Objective-C++, etc.
2843 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2844 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2845
2846 case tok::l_square:
2847 // Objective-C array literal
2848 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2849
2850 case tok::l_brace:
2851 // Objective-C dictionary literal
2852 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2853
Patrick Beard0caa3942012-04-19 00:25:12 +00002854 case tok::l_paren:
2855 // Objective-C boxed expression
2856 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2857
Chris Lattnere002fbe2007-12-12 01:04:12 +00002858 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002859 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002860 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002861
Chris Lattner197a3012008-08-05 06:19:09 +00002862 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2863 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002864 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002865 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002866 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002867 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002868 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Erik Pilkington29099de2016-07-16 00:35:23 +00002869 case tok::objc_available:
2870 return ParseAvailabilityCheckExpr(AtLoc);
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002871 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002872 const char *str = nullptr;
Alex Lorenza589abc2016-12-01 12:14:38 +00002873 // Only provide the @try/@finally/@autoreleasepool fixit when we're sure
2874 // that this is a proper statement where such directives could actually
2875 // occur.
2876 if (GetLookAheadToken(1).is(tok::l_brace) &&
2877 ExprStatementTokLoc == AtLoc) {
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002878 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2879 str =
2880 ch == 't' ? "try"
2881 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002882 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002883 }
2884 if (str) {
2885 SourceLocation kwLoc = Tok.getLocation();
2886 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2887 FixItHint::CreateReplacement(kwLoc, str));
2888 }
2889 else
2890 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2891 }
Chris Lattner197a3012008-08-05 06:19:09 +00002892 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002893 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002894}
2895
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002896/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002897///
2898/// This routine parses the receiver of a message send in
2899/// Objective-C++ either as a type or as an expression. Note that this
2900/// routine must not be called to parse a send to 'super', since it
2901/// has no way to return such a result.
2902///
2903/// \param IsExpr Whether the receiver was parsed as an expression.
2904///
2905/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2906/// IsExpr is true), the parsed expression. If the receiver was parsed
2907/// as a type (\c IsExpr is false), the parsed type.
2908///
2909/// \returns True if an error occurred during parsing or semantic
2910/// analysis, in which case the arguments do not have valid
2911/// values. Otherwise, returns false for a successful parse.
2912///
2913/// objc-receiver: [C++]
2914/// 'super' [not parsed here]
2915/// expression
2916/// simple-type-specifier
2917/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002918bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002919 InMessageExpressionRAIIObject InMessage(*this, true);
2920
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002921 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2922 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002923 TryAnnotateTypeOrScopeToken();
2924
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002925 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002926 // objc-receiver:
2927 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002928 // Make sure any typos in the receiver are corrected or diagnosed, so that
2929 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2930 // only the things that are valid ObjC receivers?
2931 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002932 if (Receiver.isInvalid())
2933 return true;
2934
2935 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002936 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002937 return false;
2938 }
2939
2940 // objc-receiver:
2941 // typename-specifier
2942 // simple-type-specifier
2943 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002944 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002945 ParseCXXSimpleTypeSpecifier(DS);
2946
2947 if (Tok.is(tok::l_paren)) {
2948 // If we see an opening parentheses at this point, we are
2949 // actually parsing an expression that starts with a
2950 // function-style cast, e.g.,
2951 //
2952 // postfix-expression:
2953 // simple-type-specifier ( expression-list [opt] )
2954 // typename-specifier ( expression-list [opt] )
2955 //
2956 // Parse the remainder of this case, then the (optional)
2957 // postfix-expression suffix, followed by the (optional)
2958 // right-hand side of the binary expression. We have an
2959 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002960 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002961 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002962 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002963 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002964 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002965 if (Receiver.isInvalid())
2966 return true;
2967
2968 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002969 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002970 return false;
2971 }
2972
2973 // We have a class message. Turn the simple-type-specifier or
2974 // typename-specifier we parsed into a type and parse the
2975 // remainder of the class message.
2976 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002977 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002978 if (Type.isInvalid())
2979 return true;
2980
2981 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002982 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002983 return false;
2984}
2985
Douglas Gregor990ccac2010-05-31 14:40:22 +00002986/// \brief Determine whether the parser is currently referring to a an
2987/// Objective-C message send, using a simplified heuristic to avoid overhead.
2988///
2989/// This routine will only return true for a subset of valid message-send
2990/// expressions.
2991bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002992 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002993 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002994 return GetLookAheadToken(1).is(tok::identifier) &&
2995 GetLookAheadToken(2).is(tok::identifier);
2996}
2997
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002998bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002999 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003000 InMessageExpression)
3001 return false;
3002
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003003 ParsedType Type;
3004
3005 if (Tok.is(tok::annot_typename))
3006 Type = getTypeAnnotation(Tok);
3007 else if (Tok.is(tok::identifier))
3008 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
3009 getCurScope());
3010 else
3011 return false;
3012
3013 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
3014 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003015 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003016 if (Tok.is(tok::identifier))
3017 TryAnnotateTypeOrScopeToken();
3018
3019 return Tok.is(tok::annot_typename);
3020 }
3021 }
3022
3023 return false;
3024}
3025
Mike Stump11289f42009-09-09 15:08:12 +00003026/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003027/// '[' objc-receiver objc-message-args ']'
3028///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003029/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00003030/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003031/// expression
3032/// class-name
3033/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00003034///
John McCalldadc5752010-08-24 06:29:42 +00003035ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00003036 assert(Tok.is(tok::l_square) && "'[' expected");
3037 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
3038
Douglas Gregora817a192010-05-27 23:06:34 +00003039 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003040 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003041 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00003042 return ExprError();
3043 }
3044
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003045 InMessageExpressionRAIIObject InMessage(*this, true);
3046
David Blaikiebbafb8a2012-03-11 07:00:24 +00003047 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00003048 // We completely separate the C and C++ cases because C++ requires
3049 // more complicated (read: slower) parsing.
3050
3051 // Handle send to super.
3052 // FIXME: This doesn't benefit from the same typo-correction we
3053 // get in Objective-C.
3054 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00003055 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
David Blaikieefdccaa2016-01-15 23:43:34 +00003056 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3057 nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003058
3059 // Parse the receiver, which is either a type or an expression.
3060 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00003061 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00003062 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003063 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003064 return ExprError();
3065 }
3066
3067 if (IsExpr)
David Blaikieefdccaa2016-01-15 23:43:34 +00003068 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3069 static_cast<Expr *>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00003070
3071 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00003072 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00003073 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00003074 }
3075
3076 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00003077 IdentifierInfo *Name = Tok.getIdentifierInfo();
3078 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00003079 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003080 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00003081 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00003082 NextToken().is(tok::period),
3083 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00003084 case Sema::ObjCSuperMessage:
David Blaikieefdccaa2016-01-15 23:43:34 +00003085 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3086 nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003087
John McCallfaf5fb42010-08-26 23:41:50 +00003088 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00003089 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003090 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003091 return ExprError();
3092 }
3093
Douglas Gregore5798dc2010-04-21 20:38:13 +00003094 ConsumeToken(); // the type name
3095
Douglas Gregore83b9562015-07-07 03:57:53 +00003096 // Parse type arguments and protocol qualifiers.
3097 if (Tok.is(tok::less)) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003098 SourceLocation NewEndLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00003099 TypeResult NewReceiverType
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003100 = parseObjCTypeArgsAndProtocolQualifiers(NameLoc, ReceiverType,
3101 /*consumeLastToken=*/true,
3102 NewEndLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00003103 if (!NewReceiverType.isUsable()) {
3104 SkipUntil(tok::r_square, StopAtSemi);
3105 return ExprError();
3106 }
3107
3108 ReceiverType = NewReceiverType.get();
3109 }
3110
Douglas Gregore5798dc2010-04-21 20:38:13 +00003111 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00003112 ReceiverType, nullptr);
3113
John McCallfaf5fb42010-08-26 23:41:50 +00003114 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003115 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00003116 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00003117 }
Chris Lattner8f697062008-01-25 18:59:06 +00003118 }
Chris Lattnera36ec422010-04-11 08:28:14 +00003119
3120 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00003121 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003122 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003123 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003124 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00003125 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003126
David Blaikieefdccaa2016-01-15 23:43:34 +00003127 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3128 Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00003129}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003130
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003131/// \brief Parse the remainder of an Objective-C message following the
3132/// '[' objc-receiver.
3133///
3134/// This routine handles sends to super, class messages (sent to a
3135/// class name), and instance messages (sent to an object), and the
3136/// target is represented by \p SuperLoc, \p ReceiverType, or \p
3137/// ReceiverExpr, respectively. Only one of these parameters may have
3138/// a valid value.
3139///
3140/// \param LBracLoc The location of the opening '['.
3141///
3142/// \param SuperLoc If this is a send to 'super', the location of the
3143/// 'super' keyword that indicates a send to the superclass.
3144///
3145/// \param ReceiverType If this is a class message, the type of the
3146/// class we are sending a message to.
3147///
3148/// \param ReceiverExpr If this is an instance message, the expression
3149/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00003150///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003151/// objc-message-args:
3152/// objc-selector
3153/// objc-keywordarg-list
3154///
3155/// objc-keywordarg-list:
3156/// objc-keywordarg
3157/// objc-keywordarg-list objc-keywordarg
3158///
Mike Stump11289f42009-09-09 15:08:12 +00003159/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003160/// selector-name[opt] ':' objc-keywordexpr
3161///
3162/// objc-keywordexpr:
3163/// nonempty-expr-list
3164///
3165/// nonempty-expr-list:
3166/// assignment-expression
3167/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003168///
John McCalldadc5752010-08-24 06:29:42 +00003169ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00003170Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003171 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00003172 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00003173 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003174 InMessageExpressionRAIIObject InMessage(*this, true);
3175
Steve Naroffeae65032009-11-07 02:08:14 +00003176 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003177 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003178 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003179 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003180 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003181 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003182 false);
Steve Naroffeae65032009-11-07 02:08:14 +00003183 else
John McCallb268a282010-08-23 23:25:46 +00003184 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003185 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003186 cutOffParsing();
3187 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00003188 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00003189
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003190 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00003191 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003192 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003193
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003194 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003195 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003196 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00003197
Chris Lattner0ef13522007-10-09 17:51:17 +00003198 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003199 while (1) {
3200 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00003201 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003202 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00003203
Alp Toker383d2c42014-01-01 03:08:43 +00003204 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00003205 // We must manually skip to a ']', otherwise the expression skipper will
3206 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3207 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003208 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003209 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003210 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003211
Mike Stump11289f42009-09-09 15:08:12 +00003212 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003213
3214 if (Tok.is(tok::code_completion)) {
3215 if (SuperLoc.isValid())
3216 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003217 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003218 /*AtArgumentEpression=*/true);
3219 else if (ReceiverType)
3220 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003221 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003222 /*AtArgumentEpression=*/true);
3223 else
3224 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003225 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003226 /*AtArgumentEpression=*/true);
3227
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003228 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003229 return ExprError();
3230 }
3231
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00003232 ExprResult Expr;
3233 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3234 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3235 Expr = ParseBraceInitializer();
3236 } else
3237 Expr = ParseAssignmentExpression();
3238
3239 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003240 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00003241 // We must manually skip to a ']', otherwise the expression skipper will
3242 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3243 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003244 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003245 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00003246 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003247
Steve Naroff486760a2007-09-17 20:25:27 +00003248 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003249 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003250
Douglas Gregor1b605f72009-11-19 01:08:35 +00003251 // Code completion after each argument.
3252 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003253 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003254 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003255 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003256 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003257 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003258 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003259 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003260 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00003261 else
John McCallb268a282010-08-23 23:25:46 +00003262 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003263 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003264 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003265 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003266 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00003267 }
3268
Steve Naroff486760a2007-09-17 20:25:27 +00003269 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00003270 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00003271 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003272 break;
3273 // We have a selector or a colon, continue parsing.
3274 }
3275 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00003276 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003277 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00003278 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00003279 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00003280 if (Tok.is(tok::colon))
3281 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003282 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003283 if (Tok.is(tok::colon)) {
3284 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
3285 FixItHint::CreateRemoval(commaLoc);
3286 }
Chris Lattner197a3012008-08-05 06:19:09 +00003287 // We must manually skip to a ']', otherwise the expression skipper will
3288 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3289 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003290 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003291 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003292 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003293
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003294 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003295 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003296 }
3297 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00003298 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003299
Chris Lattner197a3012008-08-05 06:19:09 +00003300 // We must manually skip to a ']', otherwise the expression skipper will
3301 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3302 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003303 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003304 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003305 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00003306
Chris Lattner0ef13522007-10-09 17:51:17 +00003307 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00003308 Diag(Tok, diag::err_expected)
3309 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00003310 // We must manually skip to a ']', otherwise the expression skipper will
3311 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3312 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003313 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003314 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003315 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003316
Chris Lattner8f697062008-01-25 18:59:06 +00003317 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003318
Steve Naroffe61bfa82007-10-05 18:42:47 +00003319 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003320 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00003321 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003322 KeyLocs.push_back(Loc);
3323 }
Chris Lattner5700fab2007-10-07 02:00:24 +00003324 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003325
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003326 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003327 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003328 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003329 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003330 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003331 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00003332 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003333 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003334}
3335
John McCalldadc5752010-08-24 06:29:42 +00003336ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
3337 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003338 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003339
Chris Lattnere002fbe2007-12-12 01:04:12 +00003340 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
3341 // expressions. At this point, we know that the only valid thing that starts
3342 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003343 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003344 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00003345 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003346 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003347
Chris Lattnere002fbe2007-12-12 01:04:12 +00003348 while (Tok.is(tok::at)) {
3349 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00003350
Sebastian Redlc13f2682008-12-09 20:22:58 +00003351 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00003352 if (!isTokenStringLiteral())
3353 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00003354
John McCalldadc5752010-08-24 06:29:42 +00003355 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003356 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003357 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003358
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003359 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00003360 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003361
Craig Topper883dd332015-12-24 23:58:11 +00003362 return Actions.ParseObjCStringLiteral(AtLocs.data(), AtStrings);
Anders Carlsson76f4a902007-08-21 17:43:55 +00003363}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003364
Ted Kremeneke65b0862012-03-06 20:05:56 +00003365/// ParseObjCBooleanLiteral -
3366/// objc-scalar-literal : '@' boolean-keyword
3367/// ;
3368/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
3369/// ;
3370ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
3371 bool ArgValue) {
3372 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
3373 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
3374}
3375
3376/// ParseObjCCharacterLiteral -
3377/// objc-scalar-literal : '@' character-literal
3378/// ;
3379ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
3380 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
3381 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003382 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003383 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003384 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003385 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003386}
3387
3388/// ParseObjCNumericLiteral -
3389/// objc-scalar-literal : '@' scalar-literal
3390/// ;
3391/// scalar-literal : | numeric-constant /* any numeric constant. */
3392/// ;
3393ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3394 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3395 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003396 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003397 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003398 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003399 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003400}
3401
Patrick Beard0caa3942012-04-19 00:25:12 +00003402/// ParseObjCBoxedExpr -
3403/// objc-box-expression:
3404/// @( assignment-expression )
3405ExprResult
3406Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3407 if (Tok.isNot(tok::l_paren))
3408 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3409
3410 BalancedDelimiterTracker T(*this, tok::l_paren);
3411 T.consumeOpen();
3412 ExprResult ValueExpr(ParseAssignmentExpression());
3413 if (T.consumeClose())
3414 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00003415
3416 if (ValueExpr.isInvalid())
3417 return ExprError();
3418
Patrick Beard0caa3942012-04-19 00:25:12 +00003419 // Wrap the sub-expression in a parenthesized expression, to distinguish
3420 // a boxed expression from a literal.
3421 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003422 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00003423 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003424 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00003425}
3426
Ted Kremeneke65b0862012-03-06 20:05:56 +00003427ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00003428 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003429 ConsumeBracket(); // consume the l_square.
3430
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003431 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003432 while (Tok.isNot(tok::r_square)) {
3433 // Parse list of array element expressions (all must be id types).
3434 ExprResult Res(ParseAssignmentExpression());
3435 if (Res.isInvalid()) {
3436 // We must manually skip to a ']', otherwise the expression skipper will
3437 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3438 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003439 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003440 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003441 }
3442
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003443 Res = Actions.CorrectDelayedTyposInExpr(Res.get());
3444 if (Res.isInvalid())
3445 HasInvalidEltExpr = true;
3446
Ted Kremeneke65b0862012-03-06 20:05:56 +00003447 // Parse the ellipsis that indicates a pack expansion.
3448 if (Tok.is(tok::ellipsis))
3449 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
3450 if (Res.isInvalid())
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003451 HasInvalidEltExpr = true;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003452
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003453 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003454
3455 if (Tok.is(tok::comma))
3456 ConsumeToken(); // Eat the ','.
3457 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00003458 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3459 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003460 }
3461 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003462
3463 if (HasInvalidEltExpr)
3464 return ExprError();
3465
Benjamin Kramerf0623432012-08-23 22:51:59 +00003466 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00003467 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003468}
3469
3470ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3471 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3472 ConsumeBrace(); // consume the l_square.
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003473 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003474 while (Tok.isNot(tok::r_brace)) {
3475 // Parse the comma separated key : value expressions.
3476 ExprResult KeyExpr;
3477 {
3478 ColonProtectionRAIIObject X(*this);
3479 KeyExpr = ParseAssignmentExpression();
3480 if (KeyExpr.isInvalid()) {
3481 // We must manually skip to a '}', otherwise the expression skipper will
3482 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3483 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003484 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003485 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003486 }
3487 }
3488
Alp Toker383d2c42014-01-01 03:08:43 +00003489 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003490 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00003491 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003492 }
3493
3494 ExprResult ValueExpr(ParseAssignmentExpression());
3495 if (ValueExpr.isInvalid()) {
3496 // We must manually skip to a '}', otherwise the expression skipper will
3497 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3498 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003499 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003500 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003501 }
3502
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003503 // Check the key and value for possible typos
3504 KeyExpr = Actions.CorrectDelayedTyposInExpr(KeyExpr.get());
3505 ValueExpr = Actions.CorrectDelayedTyposInExpr(ValueExpr.get());
3506 if (KeyExpr.isInvalid() || ValueExpr.isInvalid())
3507 HasInvalidEltExpr = true;
3508
3509 // Parse the ellipsis that designates this as a pack expansion. Do not
3510 // ActOnPackExpansion here, leave it to template instantiation time where
3511 // we can get better diagnostics.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003512 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00003513 if (getLangOpts().CPlusPlus)
3514 TryConsumeToken(tok::ellipsis, EllipsisLoc);
3515
Ted Kremeneke65b0862012-03-06 20:05:56 +00003516 // We have a valid expression. Collect it in a vector so we can
3517 // build the argument list.
3518 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00003519 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00003520 };
3521 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00003522
3523 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00003524 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3525 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003526 }
3527 SourceLocation EndLoc = ConsumeBrace();
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003528
3529 if (HasInvalidEltExpr)
3530 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003531
3532 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00003533 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
Craig Topperd4336e02015-12-24 23:58:15 +00003534 Elements);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003535}
3536
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003537/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00003538/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00003539ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003540Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00003541 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003542
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003543 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003544
Chris Lattner197a3012008-08-05 06:19:09 +00003545 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003546 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3547
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003548 BalancedDelimiterTracker T(*this, tok::l_paren);
3549 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003550
Douglas Gregor220cac52009-02-18 17:45:20 +00003551 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003552
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003553 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003554
Douglas Gregor220cac52009-02-18 17:45:20 +00003555 if (Ty.isInvalid())
3556 return ExprError();
3557
Nico Webera7c7e602012-12-31 00:28:03 +00003558 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3559 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003560}
Anders Carlssone01493d2007-08-23 15:25:28 +00003561
3562/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00003563/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00003564ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003565Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00003566 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003567
Chris Lattner197a3012008-08-05 06:19:09 +00003568 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003569 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3570
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003571 BalancedDelimiterTracker T(*this, tok::l_paren);
3572 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003573
Chris Lattner197a3012008-08-05 06:19:09 +00003574 if (Tok.isNot(tok::identifier))
Alp Tokerec543272013-12-24 09:48:30 +00003575 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003576
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003577 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00003578 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003579
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003580 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00003581
Nico Webera7c7e602012-12-31 00:28:03 +00003582 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3583 T.getOpenLocation(), ProtoIdLoc,
3584 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00003585}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003586
3587/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003588/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003589ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003590 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003591
Chris Lattner197a3012008-08-05 06:19:09 +00003592 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003593 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3594
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003595 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003596 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003597
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003598 BalancedDelimiterTracker T(*this, tok::l_paren);
3599 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003600 bool HasOptionalParen = Tok.is(tok::l_paren);
3601 if (HasOptionalParen)
3602 ConsumeParen();
3603
Douglas Gregor67c692c2010-08-26 15:07:07 +00003604 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003605 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003606 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003607 return ExprError();
3608 }
3609
Chris Lattner4f472a32009-04-11 18:13:45 +00003610 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003611 if (!SelIdent && // missing selector name.
3612 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003613 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003614
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003615 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003616
Steve Naroff152dd812007-12-05 22:21:29 +00003617 unsigned nColons = 0;
3618 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003619 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003620 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003621 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003622 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003623 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3624 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003625 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003626
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003627 if (Tok.is(tok::r_paren))
3628 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003629
3630 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003631 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003632 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003633 return ExprError();
3634 }
3635
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003636 // Check for another keyword selector.
3637 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003638 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003639 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003640 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003641 break;
3642 }
Steve Naroff152dd812007-12-05 22:21:29 +00003643 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003644 if (HasOptionalParen && Tok.is(tok::r_paren))
3645 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003646 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003647 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003648 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3649 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003650 T.getCloseLocation(),
3651 !HasOptionalParen);
Eugene Zelenko1ced5092016-02-12 22:53:10 +00003652}
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003653
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003654void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3655 // MCDecl might be null due to error in method or c-function prototype, etc.
3656 Decl *MCDecl = LM.D;
3657 bool skip = MCDecl &&
3658 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3659 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3660 if (skip)
3661 return;
3662
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003663 // Save the current token position.
3664 SourceLocation OrigLoc = Tok.getLocation();
3665
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003666 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
3667 // Append the current token at the end of the new token stream so that it
3668 // doesn't get lost.
3669 LM.Toks.push_back(Tok);
David Blaikie2eabcc92016-02-09 18:52:09 +00003670 PP.EnterTokenStream(LM.Toks, true);
3671
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003672 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003673 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003674
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003675 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3676 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003677 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003678 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003679 parseMethod
3680 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
3681 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003682
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003683 // Tell the actions module that we have entered a method or c-function definition
3684 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003685 if (parseMethod)
3686 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3687 else
3688 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003689 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003690 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003691 else {
3692 if (Tok.is(tok::colon))
3693 ParseConstructorInitializer(MCDecl);
Akira Hatanakabd59b4892016-04-18 18:19:45 +00003694 else
3695 Actions.ActOnDefaultCtorInitializers(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003696 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003697 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00003698
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003699 if (Tok.getLocation() != OrigLoc) {
3700 // Due to parsing error, we either went over the cached tokens or
3701 // there are still cached tokens left. If it's the latter case skip the
3702 // leftover tokens.
3703 // Since this is an uncommon situation that should be avoided, use the
3704 // expensive isBeforeInTranslationUnit call.
3705 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3706 OrigLoc))
3707 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3708 ConsumeAnyToken();
3709 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003710}