blob: 75ddfcb03ed4bb7bc7e28b70bcb5cd3ed1fecca3 [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);
Fariborz Jahaniana773d082014-03-26 22:02:43 +000086 Diag(AtLoc, diag::err_atimport);
87 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);
Douglas Gregore9d95f12015-07-07 03:57:35 +0000347 }
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000348 }
Douglas Gregor85f3f952015-07-07 03:57:15 +0000349
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000350 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000351 if (LAngleLoc.isValid()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000352 if (!ProtocolIdents.empty()) {
353 // We already parsed the protocols named when we thought we had a
354 // type parameter list. Translate them into actual protocol references.
355 for (const auto &pair : ProtocolIdents) {
356 protocolLocs.push_back(pair.second);
357 }
358 Actions.FindProtocolDeclaration(/*WarnOnDeclarations=*/true,
359 /*ForObjCContainer=*/true,
Craig Toppera9247eb2015-10-22 04:59:56 +0000360 ProtocolIdents, protocols);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000361 }
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000362 } else if (protocols.empty() && Tok.is(tok::less) &&
363 ParseObjCProtocolReferences(protocols, protocolLocs, true, true,
364 LAngleLoc, EndProtoLoc,
365 /*consumeLastToken=*/true)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000366 return nullptr;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000367 }
Mike Stump11289f42009-09-09 15:08:12 +0000368
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000369 if (Tok.isNot(tok::less))
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000370 Actions.ActOnTypedefedProtocols(protocols, superClassId, superClassLoc);
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000371
John McCall48871652010-08-21 09:40:31 +0000372 Decl *ClsType =
Douglas Gregore9d95f12015-07-07 03:57:35 +0000373 Actions.ActOnStartClassInterface(getCurScope(), AtLoc, nameId, nameLoc,
374 typeParameterList, superClassId,
375 superClassLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000376 typeArgs,
377 SourceRange(typeArgsLAngleLoc,
378 typeArgsRAngleLoc),
379 protocols.data(), protocols.size(),
380 protocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000381 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000382
Chris Lattner0ef13522007-10-09 17:51:17 +0000383 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000384 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000385
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000386 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000387
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000388 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000389}
390
Douglas Gregor813a0662015-06-19 18:14:38 +0000391/// Add an attribute for a context-sensitive type nullability to the given
392/// declarator.
393static void addContextSensitiveTypeNullability(Parser &P,
394 Declarator &D,
395 NullabilityKind nullability,
396 SourceLocation nullabilityLoc,
397 bool &addedToDeclSpec) {
398 // Create the attribute.
399 auto getNullabilityAttr = [&]() -> AttributeList * {
Douglas Gregorbec595a2015-06-19 18:27:45 +0000400 return D.getAttributePool().create(
401 P.getNullabilityKeyword(nullability),
402 SourceRange(nullabilityLoc),
403 nullptr, SourceLocation(),
404 nullptr, 0,
405 AttributeList::AS_ContextSensitiveKeyword);
Douglas Gregor813a0662015-06-19 18:14:38 +0000406 };
407
408 if (D.getNumTypeObjects() > 0) {
409 // Add the attribute to the declarator chunk nearest the declarator.
410 auto nullabilityAttr = getNullabilityAttr();
411 DeclaratorChunk &chunk = D.getTypeObject(0);
412 nullabilityAttr->setNext(chunk.getAttrListRef());
413 chunk.getAttrListRef() = nullabilityAttr;
414 } else if (!addedToDeclSpec) {
415 // Otherwise, just put it on the declaration specifiers (if one
416 // isn't there already).
417 D.getMutableDeclSpec().addAttributes(getNullabilityAttr());
418 addedToDeclSpec = true;
419 }
420}
421
Douglas Gregor85f3f952015-07-07 03:57:15 +0000422/// Parse an Objective-C type parameter list, if present, or capture
423/// the locations of the protocol identifiers for a list of protocol
424/// references.
425///
426/// objc-type-parameter-list:
427/// '<' objc-type-parameter (',' objc-type-parameter)* '>'
428///
429/// objc-type-parameter:
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000430/// objc-type-parameter-variance? identifier objc-type-parameter-bound[opt]
Douglas Gregor85f3f952015-07-07 03:57:15 +0000431///
432/// objc-type-parameter-bound:
433/// ':' type-name
434///
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000435/// objc-type-parameter-variance:
436/// '__covariant'
437/// '__contravariant'
438///
Douglas Gregor85f3f952015-07-07 03:57:15 +0000439/// \param lAngleLoc The location of the starting '<'.
440///
441/// \param protocolIdents Will capture the list of identifiers, if the
442/// angle brackets contain a list of protocol references rather than a
443/// type parameter list.
444///
445/// \param rAngleLoc The location of the ending '>'.
446ObjCTypeParamList *Parser::parseObjCTypeParamListOrProtocolRefs(
Richard Smith3df3f1d2015-11-03 01:19:56 +0000447 ObjCTypeParamListScope &Scope, SourceLocation &lAngleLoc,
448 SmallVectorImpl<IdentifierLocPair> &protocolIdents,
449 SourceLocation &rAngleLoc, bool mayBeProtocolList) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000450 assert(Tok.is(tok::less) && "Not at the beginning of a type parameter list");
451
452 // Within the type parameter list, don't treat '>' as an operator.
453 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
454
455 // Local function to "flush" the protocol identifiers, turning them into
456 // type parameters.
457 SmallVector<Decl *, 4> typeParams;
458 auto makeProtocolIdentsIntoTypeParameters = [&]() {
Douglas Gregore83b9562015-07-07 03:57:53 +0000459 unsigned index = 0;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000460 for (const auto &pair : protocolIdents) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000461 DeclResult typeParam = Actions.actOnObjCTypeParam(
David Blaikieefdccaa2016-01-15 23:43:34 +0000462 getCurScope(), ObjCTypeParamVariance::Invariant, SourceLocation(),
463 index++, pair.first, pair.second, SourceLocation(), nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000464 if (typeParam.isUsable())
465 typeParams.push_back(typeParam.get());
466 }
467
468 protocolIdents.clear();
469 mayBeProtocolList = false;
470 };
471
472 bool invalid = false;
473 lAngleLoc = ConsumeToken();
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000474
Douglas Gregor85f3f952015-07-07 03:57:15 +0000475 do {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000476 // Parse the variance, if any.
477 SourceLocation varianceLoc;
478 ObjCTypeParamVariance variance = ObjCTypeParamVariance::Invariant;
479 if (Tok.is(tok::kw___covariant) || Tok.is(tok::kw___contravariant)) {
480 variance = Tok.is(tok::kw___covariant)
481 ? ObjCTypeParamVariance::Covariant
482 : ObjCTypeParamVariance::Contravariant;
483 varianceLoc = ConsumeToken();
484
485 // Once we've seen a variance specific , we know this is not a
486 // list of protocol references.
487 if (mayBeProtocolList) {
488 // Up until now, we have been queuing up parameters because they
489 // might be protocol references. Turn them into parameters now.
490 makeProtocolIdentsIntoTypeParameters();
491 }
492 }
493
Douglas Gregor85f3f952015-07-07 03:57:15 +0000494 // Parse the identifier.
495 if (!Tok.is(tok::identifier)) {
496 // Code completion.
497 if (Tok.is(tok::code_completion)) {
498 // FIXME: If these aren't protocol references, we'll need different
499 // completions.
Craig Topper883dd332015-12-24 23:58:11 +0000500 Actions.CodeCompleteObjCProtocolReferences(protocolIdents);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000501 cutOffParsing();
502
503 // FIXME: Better recovery here?.
504 return nullptr;
505 }
506
507 Diag(Tok, diag::err_objc_expected_type_parameter);
508 invalid = true;
509 break;
510 }
511
512 IdentifierInfo *paramName = Tok.getIdentifierInfo();
513 SourceLocation paramLoc = ConsumeToken();
514
515 // If there is a bound, parse it.
516 SourceLocation colonLoc;
517 TypeResult boundType;
518 if (TryConsumeToken(tok::colon, colonLoc)) {
519 // Once we've seen a bound, we know this is not a list of protocol
520 // references.
521 if (mayBeProtocolList) {
522 // Up until now, we have been queuing up parameters because they
523 // might be protocol references. Turn them into parameters now.
524 makeProtocolIdentsIntoTypeParameters();
525 }
526
527 // type-name
528 boundType = ParseTypeName();
529 if (boundType.isInvalid())
530 invalid = true;
531 } else if (mayBeProtocolList) {
532 // If this could still be a protocol list, just capture the identifier.
533 // We don't want to turn it into a parameter.
534 protocolIdents.push_back(std::make_pair(paramName, paramLoc));
535 continue;
536 }
537
538 // Create the type parameter.
David Blaikieefdccaa2016-01-15 23:43:34 +0000539 DeclResult typeParam = Actions.actOnObjCTypeParam(
540 getCurScope(), variance, varianceLoc, typeParams.size(), paramName,
541 paramLoc, colonLoc, boundType.isUsable() ? boundType.get() : nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000542 if (typeParam.isUsable())
543 typeParams.push_back(typeParam.get());
544 } while (TryConsumeToken(tok::comma));
545
546 // Parse the '>'.
547 if (invalid) {
548 SkipUntil(tok::greater, tok::at, StopBeforeMatch);
549 if (Tok.is(tok::greater))
550 ConsumeToken();
551 } else if (ParseGreaterThanInTemplateList(rAngleLoc,
552 /*ConsumeLastToken=*/true,
553 /*ObjCGenericList=*/true)) {
554 Diag(lAngleLoc, diag::note_matching) << "'<'";
555 SkipUntil({tok::greater, tok::greaterequal, tok::at, tok::minus,
556 tok::minus, tok::plus, tok::colon, tok::l_paren, tok::l_brace,
557 tok::comma, tok::semi },
558 StopBeforeMatch);
559 if (Tok.is(tok::greater))
560 ConsumeToken();
561 }
562
563 if (mayBeProtocolList) {
564 // A type parameter list must be followed by either a ':' (indicating the
565 // presence of a superclass) or a '(' (indicating that this is a category
566 // or extension). This disambiguates between an objc-type-parameter-list
567 // and a objc-protocol-refs.
568 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_paren)) {
569 // Returning null indicates that we don't have a type parameter list.
570 // The results the caller needs to handle the protocol references are
571 // captured in the reference parameters already.
572 return nullptr;
573 }
574
575 // We have a type parameter list that looks like a list of protocol
576 // references. Turn that parameter list into type parameters.
577 makeProtocolIdentsIntoTypeParameters();
578 }
579
Richard Smith3df3f1d2015-11-03 01:19:56 +0000580 // Form the type parameter list and enter its scope.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000581 ObjCTypeParamList *list = Actions.actOnObjCTypeParamList(
582 getCurScope(),
583 lAngleLoc,
584 typeParams,
585 rAngleLoc);
Richard Smith3df3f1d2015-11-03 01:19:56 +0000586 Scope.enter(list);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000587
588 // Clear out the angle locations; they're used by the caller to indicate
589 // whether there are any protocol references.
590 lAngleLoc = SourceLocation();
591 rAngleLoc = SourceLocation();
Akira Hatanaka8ebd5802015-12-16 06:25:38 +0000592 return invalid ? nullptr : list;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000593}
594
595/// Parse an objc-type-parameter-list.
596ObjCTypeParamList *Parser::parseObjCTypeParamList() {
597 SourceLocation lAngleLoc;
598 SmallVector<IdentifierLocPair, 1> protocolIdents;
599 SourceLocation rAngleLoc;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000600
601 ObjCTypeParamListScope Scope(Actions, getCurScope());
602 return parseObjCTypeParamListOrProtocolRefs(Scope, lAngleLoc, protocolIdents,
603 rAngleLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000604 /*mayBeProtocolList=*/false);
605}
606
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000607/// objc-interface-decl-list:
608/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000609/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000610/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000611/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000612/// objc-interface-decl-list declaration
613/// objc-interface-decl-list ';'
614///
Steve Naroff99264b42007-08-22 16:35:03 +0000615/// objc-method-requirement: [OBJC2]
616/// @required
617/// @optional
618///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000619void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
620 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000621 SmallVector<Decl *, 32> allMethods;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000622 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000623 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000624
Ted Kremenekc7c64312010-01-07 01:20:12 +0000625 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000626
Steve Naroff99264b42007-08-22 16:35:03 +0000627 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000628 // If this is a method prototype, parse it.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000629 if (Tok.isOneOf(tok::minus, tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000630 if (Decl *methodPrototype =
631 ParseObjCMethodPrototype(MethodImplKind, false))
632 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000633 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
634 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000635 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
636 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000637 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000638 if (Tok.is(tok::semi))
639 ConsumeToken();
640 }
Steve Naroff99264b42007-08-22 16:35:03 +0000641 continue;
642 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000643 if (Tok.is(tok::l_paren)) {
644 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000645 ParseObjCMethodDecl(Tok.getLocation(),
646 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000647 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000648 continue;
649 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000650 // Ignore excess semicolons.
651 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000652 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000653 continue;
654 }
Mike Stump11289f42009-09-09 15:08:12 +0000655
Chris Lattnerda9fb152008-10-20 06:10:06 +0000656 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000657 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000658 break;
Mike Stump11289f42009-09-09 15:08:12 +0000659
Douglas Gregorf1934162010-01-13 21:24:21 +0000660 // Code completion within an Objective-C interface.
661 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000662 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000663 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000664 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000665 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000666 }
667
Chris Lattner038a3e32008-10-20 05:46:22 +0000668 // If we don't have an @ directive, parse it as a function definition.
669 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000670 // The code below does not consume '}'s because it is afraid of eating the
671 // end of a namespace. Because of the way this code is structured, an
672 // erroneous r_brace would cause an infinite loop if not handled here.
673 if (Tok.is(tok::r_brace))
674 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000675 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000676 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000677 continue;
678 }
Mike Stump11289f42009-09-09 15:08:12 +0000679
Chris Lattner038a3e32008-10-20 05:46:22 +0000680 // Otherwise, we have an @ directive, eat the @.
681 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000682 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000683 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000684 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000685 }
686
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000687 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000688
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000689 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000690 AtEnd.setBegin(AtLoc);
691 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000692 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000693 } else if (DirectiveKind == tok::objc_not_keyword) {
694 Diag(Tok, diag::err_objc_unknown_at);
695 SkipUntil(tok::semi);
696 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000697 }
Mike Stump11289f42009-09-09 15:08:12 +0000698
Chris Lattnerda9fb152008-10-20 06:10:06 +0000699 // Eat the identifier.
700 ConsumeToken();
701
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000702 switch (DirectiveKind) {
703 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000704 // FIXME: If someone forgets an @end on a protocol, this loop will
705 // continue to eat up tons of stuff and spew lots of nonsense errors. It
706 // would probably be better to bail out if we saw an @class or @interface
707 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000708 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000709 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000710 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000711 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000712
713 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000714 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000715 Diag(AtLoc, diag::err_objc_missing_end)
716 << FixItHint::CreateInsertion(AtLoc, "@end\n");
717 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
718 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000719 ConsumeToken();
720 break;
721
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000722 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000723 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000724 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000725 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000726 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000727 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000728 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000729 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000730 break;
Mike Stump11289f42009-09-09 15:08:12 +0000731
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000732 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000733 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000734 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000735
Chris Lattner038a3e32008-10-20 05:46:22 +0000736 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000737 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000738 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000739 if (Tok.is(tok::l_paren)) {
740 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000741 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000742 }
Mike Stump11289f42009-09-09 15:08:12 +0000743
Douglas Gregor813a0662015-06-19 18:14:38 +0000744 bool addedToDeclSpec = false;
Benjamin Kramera39beb92014-09-03 11:06:10 +0000745 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
746 if (FD.D.getIdentifier() == nullptr) {
747 Diag(AtLoc, diag::err_objc_property_requires_field_name)
748 << FD.D.getSourceRange();
749 return;
750 }
751 if (FD.BitfieldSize) {
752 Diag(AtLoc, diag::err_objc_property_bitfield)
753 << FD.D.getSourceRange();
754 return;
755 }
756
Douglas Gregor813a0662015-06-19 18:14:38 +0000757 // Map a nullability property attribute to a context-sensitive keyword
758 // attribute.
759 if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
760 addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
761 OCDS.getNullabilityLoc(),
762 addedToDeclSpec);
763
Benjamin Kramera39beb92014-09-03 11:06:10 +0000764 // Install the property declarator into interfaceDecl.
765 IdentifierInfo *SelName =
766 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
767
768 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
769 IdentifierInfo *SetterName = OCDS.getSetterName();
770 Selector SetterSel;
771 if (SetterName)
772 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
773 else
774 SetterSel = SelectorTable::constructSetterSelector(
775 PP.getIdentifierTable(), PP.getSelectorTable(),
776 FD.D.getIdentifier());
Benjamin Kramera39beb92014-09-03 11:06:10 +0000777 Decl *Property = Actions.ActOnProperty(
778 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000779 MethodImplKind);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000780
781 FD.complete(Property);
782 };
John McCallcfefb6d2009-11-03 02:38:08 +0000783
Chris Lattner038a3e32008-10-20 05:46:22 +0000784 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000785 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000786 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000787
John McCall405988b2011-03-26 01:53:26 +0000788 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000789 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000790 }
Steve Naroff99264b42007-08-22 16:35:03 +0000791 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000792
793 // We break out of the big loop in two cases: when we see @end or when we see
794 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000795 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000796 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000797 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000798 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000799 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000800 } else {
801 Diag(Tok, diag::err_objc_missing_end)
802 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
803 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
804 << (int) Actions.getObjCContainerKind();
805 AtEnd.setBegin(Tok.getLocation());
806 AtEnd.setEnd(Tok.getLocation());
807 }
Mike Stump11289f42009-09-09 15:08:12 +0000808
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000809 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000810 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000811 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000812}
813
Douglas Gregor813a0662015-06-19 18:14:38 +0000814/// Diagnose redundant or conflicting nullability information.
815static void diagnoseRedundantPropertyNullability(Parser &P,
816 ObjCDeclSpec &DS,
817 NullabilityKind nullability,
818 SourceLocation nullabilityLoc){
819 if (DS.getNullability() == nullability) {
820 P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000821 << DiagNullabilityKind(nullability, true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000822 << SourceRange(DS.getNullabilityLoc());
823 return;
824 }
825
826 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000827 << DiagNullabilityKind(nullability, true)
828 << DiagNullabilityKind(DS.getNullability(), true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000829 << SourceRange(DS.getNullabilityLoc());
830}
831
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000832/// Parse property attribute declarations.
833///
834/// property-attr-decl: '(' property-attrlist ')'
835/// property-attrlist:
836/// property-attribute
837/// property-attrlist ',' property-attribute
838/// property-attribute:
839/// getter '=' identifier
840/// setter '=' identifier ':'
841/// readonly
842/// readwrite
843/// assign
844/// retain
845/// copy
846/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000847/// atomic
848/// strong
849/// weak
850/// unsafe_unretained
Douglas Gregor813a0662015-06-19 18:14:38 +0000851/// nonnull
852/// nullable
853/// null_unspecified
Douglas Gregor849ebc22015-06-19 18:14:46 +0000854/// null_resettable
Manman Ren387ff7f2016-01-26 18:52:43 +0000855/// class
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000856///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000857void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000858 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000859 BalancedDelimiterTracker T(*this, tok::l_paren);
860 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000861
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000862 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000863 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000864 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000865 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000866 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000867 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000868
Chris Lattner76619232008-10-20 07:22:18 +0000869 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000870 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000871 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000872 return;
873 }
Mike Stump11289f42009-09-09 15:08:12 +0000874
Chris Lattner1db33542008-10-20 07:37:22 +0000875 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000876
Chris Lattner68e48682008-11-20 04:42:34 +0000877 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000878 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000879 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000880 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000881 else if (II->isStr("unsafe_unretained"))
882 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000883 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000884 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000885 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000886 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000887 else if (II->isStr("strong"))
888 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000889 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000890 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000891 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000892 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000893 else if (II->isStr("atomic"))
894 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000895 else if (II->isStr("weak"))
896 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000897 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000898 bool IsSetter = II->getNameStart()[0] == 's';
899
Chris Lattner825bca12008-10-20 07:39:53 +0000900 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000901 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
Craig Topper3110a5c2015-11-14 18:16:02 +0000902 diag::err_objc_expected_equal_for_getter;
Anders Carlssonfe15a782010-10-02 17:45:21 +0000903
Alp Toker383d2c42014-01-01 03:08:43 +0000904 if (ExpectAndConsume(tok::equal, DiagID)) {
905 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000906 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000907 }
Mike Stump11289f42009-09-09 15:08:12 +0000908
Douglas Gregorc8537c52009-11-19 07:41:15 +0000909 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000910 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000911 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000912 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000913 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000914 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000915 }
916
Anders Carlssonfe15a782010-10-02 17:45:21 +0000917 SourceLocation SelLoc;
918 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
919
920 if (!SelIdent) {
921 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
922 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000923 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000924 return;
925 }
Mike Stump11289f42009-09-09 15:08:12 +0000926
Anders Carlssonfe15a782010-10-02 17:45:21 +0000927 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000928 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000929 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000930
Alp Toker383d2c42014-01-01 03:08:43 +0000931 if (ExpectAndConsume(tok::colon,
932 diag::err_expected_colon_after_setter_name)) {
933 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000934 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000935 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000936 } else {
937 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000938 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000939 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000940 } else if (II->isStr("nonnull")) {
941 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
942 diagnoseRedundantPropertyNullability(*this, DS,
943 NullabilityKind::NonNull,
944 Tok.getLocation());
945 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
946 DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
947 } else if (II->isStr("nullable")) {
948 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
949 diagnoseRedundantPropertyNullability(*this, DS,
950 NullabilityKind::Nullable,
951 Tok.getLocation());
952 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
953 DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
954 } else if (II->isStr("null_unspecified")) {
955 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
956 diagnoseRedundantPropertyNullability(*this, DS,
957 NullabilityKind::Unspecified,
958 Tok.getLocation());
959 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
960 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000961 } else if (II->isStr("null_resettable")) {
962 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
963 diagnoseRedundantPropertyNullability(*this, DS,
964 NullabilityKind::Unspecified,
965 Tok.getLocation());
966 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
967 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
968
969 // Also set the null_resettable bit.
970 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
Manman Ren387ff7f2016-01-26 18:52:43 +0000971 } else if (II->isStr("class")) {
972 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_class);
Chris Lattner825bca12008-10-20 07:39:53 +0000973 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000974 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000975 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000976 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000977 }
Mike Stump11289f42009-09-09 15:08:12 +0000978
Chris Lattner1db33542008-10-20 07:37:22 +0000979 if (Tok.isNot(tok::comma))
980 break;
Mike Stump11289f42009-09-09 15:08:12 +0000981
Chris Lattner1db33542008-10-20 07:37:22 +0000982 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000983 }
Mike Stump11289f42009-09-09 15:08:12 +0000984
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000985 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000986}
987
Steve Naroff09bf8152007-09-06 21:24:23 +0000988/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000989/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000990/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000991///
992/// objc-instance-method: '-'
993/// objc-class-method: '+'
994///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000995/// objc-method-attributes: [OBJC2]
996/// __attribute__((deprecated))
997///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000998Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000999 bool MethodDefinition) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001000 assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +00001001
Mike Stump11289f42009-09-09 15:08:12 +00001002 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +00001003 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001004 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001005 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +00001006 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +00001007 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +00001008 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +00001009}
1010
1011/// objc-selector:
1012/// identifier
1013/// one of
1014/// enum struct union if else while do for switch case default
1015/// break continue return goto asm sizeof typeof __alignof
1016/// unsigned long const short volatile signed restrict _Complex
1017/// in out inout bycopy byref oneway int char float double void _Bool
1018///
Chris Lattner4f472a32009-04-11 18:13:45 +00001019IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +00001020
Chris Lattner5700fab2007-10-07 02:00:24 +00001021 switch (Tok.getKind()) {
1022 default:
Craig Topper161e4db2014-05-21 06:02:52 +00001023 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001024 case tok::ampamp:
1025 case tok::ampequal:
1026 case tok::amp:
1027 case tok::pipe:
1028 case tok::tilde:
1029 case tok::exclaim:
1030 case tok::exclaimequal:
1031 case tok::pipepipe:
1032 case tok::pipeequal:
1033 case tok::caret:
1034 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +00001035 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +00001036 if (isLetter(ThisTok[0])) {
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001037 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
1038 Tok.setKind(tok::identifier);
1039 SelectorLoc = ConsumeToken();
1040 return II;
1041 }
Craig Topper161e4db2014-05-21 06:02:52 +00001042 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001043 }
1044
Chris Lattner5700fab2007-10-07 02:00:24 +00001045 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001046 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +00001047 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001048 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001049 case tok::kw_break:
1050 case tok::kw_case:
1051 case tok::kw_catch:
1052 case tok::kw_char:
1053 case tok::kw_class:
1054 case tok::kw_const:
1055 case tok::kw_const_cast:
1056 case tok::kw_continue:
1057 case tok::kw_default:
1058 case tok::kw_delete:
1059 case tok::kw_do:
1060 case tok::kw_double:
1061 case tok::kw_dynamic_cast:
1062 case tok::kw_else:
1063 case tok::kw_enum:
1064 case tok::kw_explicit:
1065 case tok::kw_export:
1066 case tok::kw_extern:
1067 case tok::kw_false:
1068 case tok::kw_float:
1069 case tok::kw_for:
1070 case tok::kw_friend:
1071 case tok::kw_goto:
1072 case tok::kw_if:
1073 case tok::kw_inline:
1074 case tok::kw_int:
1075 case tok::kw_long:
1076 case tok::kw_mutable:
1077 case tok::kw_namespace:
1078 case tok::kw_new:
1079 case tok::kw_operator:
1080 case tok::kw_private:
1081 case tok::kw_protected:
1082 case tok::kw_public:
1083 case tok::kw_register:
1084 case tok::kw_reinterpret_cast:
1085 case tok::kw_restrict:
1086 case tok::kw_return:
1087 case tok::kw_short:
1088 case tok::kw_signed:
1089 case tok::kw_sizeof:
1090 case tok::kw_static:
1091 case tok::kw_static_cast:
1092 case tok::kw_struct:
1093 case tok::kw_switch:
1094 case tok::kw_template:
1095 case tok::kw_this:
1096 case tok::kw_throw:
1097 case tok::kw_true:
1098 case tok::kw_try:
1099 case tok::kw_typedef:
1100 case tok::kw_typeid:
1101 case tok::kw_typename:
1102 case tok::kw_typeof:
1103 case tok::kw_union:
1104 case tok::kw_unsigned:
1105 case tok::kw_using:
1106 case tok::kw_virtual:
1107 case tok::kw_void:
1108 case tok::kw_volatile:
1109 case tok::kw_wchar_t:
1110 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +00001111 case tok::kw__Bool:
1112 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001113 case tok::kw___alignof:
Richard Smithe301ba22015-11-11 02:02:15 +00001114 case tok::kw___auto_type:
Chris Lattner5700fab2007-10-07 02:00:24 +00001115 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001116 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +00001117 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +00001118 }
Steve Naroff99264b42007-08-22 16:35:03 +00001119}
1120
Fariborz Jahanian83615522008-01-02 22:54:34 +00001121/// objc-for-collection-in: 'in'
1122///
Fariborz Jahanian3622e592008-01-04 23:04:08 +00001123bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001124 // FIXME: May have to do additional look-ahead to only allow for
1125 // valid tokens following an 'in'; such as an identifier, unary operators,
1126 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001127 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +00001128 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +00001129}
1130
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001131/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +00001132/// qualifier list and builds their bitmask representation in the input
1133/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +00001134///
1135/// objc-type-qualifiers:
1136/// objc-type-qualifier
1137/// objc-type-qualifiers objc-type-qualifier
1138///
Douglas Gregor813a0662015-06-19 18:14:38 +00001139/// objc-type-qualifier:
1140/// 'in'
1141/// 'out'
1142/// 'inout'
1143/// 'oneway'
1144/// 'bycopy'
1145/// 'byref'
1146/// 'nonnull'
1147/// 'nullable'
1148/// 'null_unspecified'
1149///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001150void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001151 Declarator::TheContext Context) {
1152 assert(Context == Declarator::ObjCParameterContext ||
1153 Context == Declarator::ObjCResultContext);
1154
Chris Lattner61511e12007-12-12 06:56:32 +00001155 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +00001156 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +00001157 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +00001158 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001159 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +00001160 }
1161
Chris Lattner5e530bc2007-12-27 19:57:00 +00001162 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +00001163 return;
Mike Stump11289f42009-09-09 15:08:12 +00001164
Chris Lattner61511e12007-12-12 06:56:32 +00001165 const IdentifierInfo *II = Tok.getIdentifierInfo();
1166 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001167 if (II != ObjCTypeQuals[i] ||
1168 NextToken().is(tok::less) ||
1169 NextToken().is(tok::coloncolon))
Chris Lattner61511e12007-12-12 06:56:32 +00001170 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001171
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001172 ObjCDeclSpec::ObjCDeclQualifier Qual;
Douglas Gregor813a0662015-06-19 18:14:38 +00001173 NullabilityKind Nullability;
Chris Lattner61511e12007-12-12 06:56:32 +00001174 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +00001175 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001176 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
1177 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
1178 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
1179 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
1180 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
1181 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Douglas Gregor813a0662015-06-19 18:14:38 +00001182
1183 case objc_nonnull:
1184 Qual = ObjCDeclSpec::DQ_CSNullability;
1185 Nullability = NullabilityKind::NonNull;
1186 break;
1187
1188 case objc_nullable:
1189 Qual = ObjCDeclSpec::DQ_CSNullability;
1190 Nullability = NullabilityKind::Nullable;
1191 break;
1192
1193 case objc_null_unspecified:
1194 Qual = ObjCDeclSpec::DQ_CSNullability;
1195 Nullability = NullabilityKind::Unspecified;
1196 break;
Chris Lattner61511e12007-12-12 06:56:32 +00001197 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001198
1199 // FIXME: Diagnose redundant specifiers.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001200 DS.setObjCDeclQualifier(Qual);
Douglas Gregor813a0662015-06-19 18:14:38 +00001201 if (Qual == ObjCDeclSpec::DQ_CSNullability)
1202 DS.setNullability(Tok.getLocation(), Nullability);
1203
Chris Lattner61511e12007-12-12 06:56:32 +00001204 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001205 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +00001206 break;
1207 }
Mike Stump11289f42009-09-09 15:08:12 +00001208
Chris Lattner61511e12007-12-12 06:56:32 +00001209 // If this wasn't a recognized qualifier, bail out.
1210 if (II) return;
1211 }
1212}
1213
John McCalla55902b2011-10-01 09:56:14 +00001214/// Take all the decl attributes out of the given list and add
1215/// them to the given attribute set.
1216static void takeDeclAttributes(ParsedAttributes &attrs,
1217 AttributeList *list) {
1218 while (list) {
1219 AttributeList *cur = list;
1220 list = cur->getNext();
1221
1222 if (!cur->isUsedAsTypeAttr()) {
1223 // Clear out the next pointer. We're really completely
1224 // destroying the internal invariants of the declarator here,
1225 // but it doesn't matter because we're done with it.
Craig Topper161e4db2014-05-21 06:02:52 +00001226 cur->setNext(nullptr);
John McCalla55902b2011-10-01 09:56:14 +00001227 attrs.add(cur);
1228 }
1229 }
1230}
1231
1232/// takeDeclAttributes - Take all the decl attributes from the given
1233/// declarator and add them to the given list.
1234static void takeDeclAttributes(ParsedAttributes &attrs,
1235 Declarator &D) {
1236 // First, take ownership of all attributes.
1237 attrs.getPool().takeAllFrom(D.getAttributePool());
1238 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
1239
1240 // Now actually move the attributes over.
1241 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
1242 takeDeclAttributes(attrs, D.getAttributes());
1243 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
1244 takeDeclAttributes(attrs,
1245 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
1246}
1247
Chris Lattner61511e12007-12-12 06:56:32 +00001248/// objc-type-name:
1249/// '(' objc-type-qualifiers[opt] type-name ')'
1250/// '(' objc-type-qualifiers[opt] ')'
1251///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001252ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001253 Declarator::TheContext context,
1254 ParsedAttributes *paramAttrs) {
1255 assert(context == Declarator::ObjCParameterContext ||
1256 context == Declarator::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +00001257 assert((paramAttrs != nullptr) ==
1258 (context == Declarator::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +00001259
Chris Lattner0ef13522007-10-09 17:51:17 +00001260 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +00001261
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001262 BalancedDelimiterTracker T(*this, tok::l_paren);
1263 T.consumeOpen();
1264
Chris Lattner2ebb1782008-08-23 01:48:03 +00001265 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001266 ObjCDeclContextSwitch ObjCDC(*this);
1267
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001268 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001269 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +00001270
John McCallba7bf592010-08-24 05:47:05 +00001271 ParsedType Ty;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001272 if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
John McCalla55902b2011-10-01 09:56:14 +00001273 // Parse an abstract declarator.
1274 DeclSpec declSpec(AttrFactory);
1275 declSpec.setObjCQualifiers(&DS);
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001276 DeclSpecContext dsContext = DSC_normal;
1277 if (context == Declarator::ObjCResultContext)
1278 dsContext = DSC_objc_method_result;
1279 ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
John McCalla55902b2011-10-01 09:56:14 +00001280 Declarator declarator(declSpec, context);
1281 ParseDeclarator(declarator);
1282
1283 // If that's not invalid, extract a type.
1284 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001285 // Map a nullability specifier to a context-sensitive keyword attribute.
1286 bool addedToDeclSpec = false;
1287 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1288 addContextSensitiveTypeNullability(*this, declarator,
1289 DS.getNullability(),
1290 DS.getNullabilityLoc(),
1291 addedToDeclSpec);
1292
John McCalla55902b2011-10-01 09:56:14 +00001293 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1294 if (!type.isInvalid())
1295 Ty = type.get();
1296
1297 // If we're parsing a parameter, steal all the decl attributes
1298 // and add them to the decl spec.
1299 if (context == Declarator::ObjCParameterContext)
1300 takeDeclAttributes(*paramAttrs, declarator);
1301 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001302 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001303
Steve Naroff90255b42008-10-21 14:15:04 +00001304 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001305 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001306 else if (Tok.getLocation() == TypeStartLoc) {
1307 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001308 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001309 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001310 } else {
1311 // Otherwise, we found *something*, but didn't get a ')' in the right
1312 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001313 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001314 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001315 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001316}
1317
1318/// objc-method-decl:
1319/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001320/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001321/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001322/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001323///
1324/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001325/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001326/// objc-keyword-selector objc-keyword-decl
1327///
1328/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001329/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1330/// objc-selector ':' objc-keyword-attributes[opt] identifier
1331/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1332/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001333///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001334/// objc-parmlist:
1335/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001336///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001337/// objc-parms:
1338/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001339///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001340/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001341/// , ...
1342///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001343/// objc-keyword-attributes: [OBJC2]
1344/// __attribute__((unused))
1345///
John McCall48871652010-08-21 09:40:31 +00001346Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001347 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001348 tok::ObjCKeywordKind MethodImplKind,
1349 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001350 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001351
Douglas Gregor636a61e2010-04-07 00:21:17 +00001352 if (Tok.is(tok::code_completion)) {
David Blaikieefdccaa2016-01-15 23:43:34 +00001353 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
1354 /*ReturnType=*/nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001355 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001356 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001357 }
1358
Chris Lattner2ebb1782008-08-23 01:48:03 +00001359 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001360 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001361 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001362 if (Tok.is(tok::l_paren))
Craig Topper161e4db2014-05-21 06:02:52 +00001363 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext,
1364 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001365
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001366 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001367 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001368 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001369 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001370
Douglas Gregor636a61e2010-04-07 00:21:17 +00001371 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001372 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001373 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001374 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001375 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001376 }
1377
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001378 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001379 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001380 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001381
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001382 // An unnamed colon is valid.
1383 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001384 Diag(Tok, diag::err_expected_selector_for_method)
1385 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001386 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001387 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001388 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001389 }
Mike Stump11289f42009-09-09 15:08:12 +00001390
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001391 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001392 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001393 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001394 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001395 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001396
Chris Lattner5700fab2007-10-07 02:00:24 +00001397 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001398 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001399 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001400 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001401 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001402 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001403 methodAttrs.getList(), MethodImplKind,
1404 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001405 PD.complete(Result);
1406 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001407 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001408
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001409 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001410 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001411 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001412 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1413 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001414
1415 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001416 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001417 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001418 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001419
Chris Lattner5700fab2007-10-07 02:00:24 +00001420 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001421 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001422 break;
Mike Stump11289f42009-09-09 15:08:12 +00001423
David Blaikieefdccaa2016-01-15 23:43:34 +00001424 ArgInfo.Type = nullptr;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001425 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001426 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1427 Declarator::ObjCParameterContext,
1428 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001429
Chris Lattner5700fab2007-10-07 02:00:24 +00001430 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001431 // Regardless, collect all the attributes we've parsed so far.
Craig Topper161e4db2014-05-21 06:02:52 +00001432 ArgInfo.ArgAttrs = nullptr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001433 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001434 MaybeParseGNUAttributes(paramAttrs);
1435 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001436 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001437
Douglas Gregor45879692010-07-08 23:37:41 +00001438 // Code completion for the next piece of the selector.
1439 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001440 KeyIdents.push_back(SelIdent);
1441 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1442 mType == tok::minus,
1443 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001444 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001445 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001446 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001447 }
1448
Chris Lattner0ef13522007-10-09 17:51:17 +00001449 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001450 Diag(Tok, diag::err_expected)
1451 << tok::identifier; // missing argument name.
Chris Lattner5700fab2007-10-07 02:00:24 +00001452 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001453 }
Mike Stump11289f42009-09-09 15:08:12 +00001454
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001455 ArgInfo.Name = Tok.getIdentifierInfo();
1456 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001457 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001458
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001459 ArgInfos.push_back(ArgInfo);
1460 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001461 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001462
John McCall084e83d2011-03-24 11:26:52 +00001463 // Make sure the attributes persist.
1464 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1465
Douglas Gregor95887f92010-07-08 23:20:03 +00001466 // Code completion for the next piece of the selector.
1467 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001468 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1469 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001470 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001471 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001472 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001473 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001474 }
1475
Chris Lattner5700fab2007-10-07 02:00:24 +00001476 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001477 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001478 if (!SelIdent && Tok.isNot(tok::colon))
1479 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001480 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001481 SourceLocation ColonLoc = Tok.getLocation();
1482 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001483 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1484 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1485 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001486 }
1487 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001488 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001489 }
Mike Stump11289f42009-09-09 15:08:12 +00001490
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001491 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001492 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001493 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001494 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001495 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001496 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001497 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001498 ConsumeToken();
1499 break;
1500 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001501 if (!cStyleParamWarned) {
1502 Diag(Tok, diag::warn_cstyle_param);
1503 cStyleParamWarned = true;
1504 }
John McCall084e83d2011-03-24 11:26:52 +00001505 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001506 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001507 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001508 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1509 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001510 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001511 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001512 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1513 ParmDecl.getIdentifierLoc(),
1514 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001515 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001516 }
Mike Stump11289f42009-09-09 15:08:12 +00001517
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001518 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001519 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001520 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001521 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001522
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001523 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001524 return nullptr;
1525
Chris Lattner5700fab2007-10-07 02:00:24 +00001526 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1527 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001528 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001529 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001530 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001531 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001532 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001533 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001534 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001535
John McCall28a6aea2009-11-04 02:18:39 +00001536 PD.complete(Result);
1537 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001538}
1539
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001540/// objc-protocol-refs:
1541/// '<' identifier-list '>'
1542///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001543bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001544ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1545 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001546 bool WarnOnDeclarations, bool ForObjCContainer,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001547 SourceLocation &LAngleLoc, SourceLocation &EndLoc,
1548 bool consumeLastToken) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001549 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001550
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001551 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001552
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001553 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001554
Chris Lattner3bbae002008-07-26 04:03:38 +00001555 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001556 if (Tok.is(tok::code_completion)) {
Craig Topper883dd332015-12-24 23:58:11 +00001557 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001558 cutOffParsing();
1559 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001560 }
1561
Chris Lattner3bbae002008-07-26 04:03:38 +00001562 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001563 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001564 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001565 return true;
1566 }
1567 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1568 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001569 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001570 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001571
Alp Toker383d2c42014-01-01 03:08:43 +00001572 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001573 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001574 }
Mike Stump11289f42009-09-09 15:08:12 +00001575
Chris Lattner3bbae002008-07-26 04:03:38 +00001576 // Consume the '>'.
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001577 if (ParseGreaterThanInTemplateList(EndLoc, consumeLastToken,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001578 /*ObjCGenericList=*/false))
Chris Lattner3bbae002008-07-26 04:03:38 +00001579 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001580
Chris Lattner3bbae002008-07-26 04:03:38 +00001581 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001582 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Craig Toppera9247eb2015-10-22 04:59:56 +00001583 ProtocolIdents, Protocols);
Chris Lattner3bbae002008-07-26 04:03:38 +00001584 return false;
1585}
1586
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001587TypeResult Parser::parseObjCProtocolQualifierType(SourceLocation &rAngleLoc) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001588 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001589 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001590
1591 SourceLocation lAngleLoc;
1592 SmallVector<Decl *, 8> protocols;
1593 SmallVector<SourceLocation, 8> protocolLocs;
1594 (void)ParseObjCProtocolReferences(protocols, protocolLocs, false, false,
1595 lAngleLoc, rAngleLoc,
1596 /*consumeLastToken=*/true);
1597 TypeResult result = Actions.actOnObjCProtocolQualifierType(lAngleLoc,
1598 protocols,
1599 protocolLocs,
1600 rAngleLoc);
1601 if (result.isUsable()) {
1602 Diag(lAngleLoc, diag::warn_objc_protocol_qualifier_missing_id)
1603 << FixItHint::CreateInsertion(lAngleLoc, "id")
1604 << SourceRange(lAngleLoc, rAngleLoc);
1605 }
1606
1607 return result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001608}
1609
Douglas Gregore9d95f12015-07-07 03:57:35 +00001610/// Parse Objective-C type arguments or protocol qualifiers.
1611///
1612/// objc-type-arguments:
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001613/// '<' type-name '...'[opt] (',' type-name '...'[opt])* '>'
Douglas Gregore9d95f12015-07-07 03:57:35 +00001614///
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001615void Parser::parseObjCTypeArgsOrProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001616 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001617 SourceLocation &typeArgsLAngleLoc,
1618 SmallVectorImpl<ParsedType> &typeArgs,
1619 SourceLocation &typeArgsRAngleLoc,
1620 SourceLocation &protocolLAngleLoc,
1621 SmallVectorImpl<Decl *> &protocols,
1622 SmallVectorImpl<SourceLocation> &protocolLocs,
1623 SourceLocation &protocolRAngleLoc,
1624 bool consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001625 bool warnOnIncompleteProtocols) {
1626 assert(Tok.is(tok::less) && "Not at the start of type args or protocols");
1627 SourceLocation lAngleLoc = ConsumeToken();
1628
1629 // Whether all of the elements we've parsed thus far are single
1630 // identifiers, which might be types or might be protocols.
1631 bool allSingleIdentifiers = true;
1632 SmallVector<IdentifierInfo *, 4> identifiers;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001633 SmallVectorImpl<SourceLocation> &identifierLocs = protocolLocs;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001634
1635 // Parse a list of comma-separated identifiers, bailing out if we
1636 // see something different.
1637 do {
1638 // Parse a single identifier.
1639 if (Tok.is(tok::identifier) &&
1640 (NextToken().is(tok::comma) ||
1641 NextToken().is(tok::greater) ||
1642 NextToken().is(tok::greatergreater))) {
1643 identifiers.push_back(Tok.getIdentifierInfo());
1644 identifierLocs.push_back(ConsumeToken());
1645 continue;
1646 }
1647
1648 if (Tok.is(tok::code_completion)) {
1649 // FIXME: Also include types here.
1650 SmallVector<IdentifierLocPair, 4> identifierLocPairs;
1651 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1652 identifierLocPairs.push_back(IdentifierLocPair(identifiers[i],
1653 identifierLocs[i]));
1654 }
1655
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001656 QualType BaseT = Actions.GetTypeFromParser(baseType);
1657 if (!BaseT.isNull() && BaseT->acceptsObjCTypeParams()) {
1658 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
1659 } else {
Craig Topper883dd332015-12-24 23:58:11 +00001660 Actions.CodeCompleteObjCProtocolReferences(identifierLocPairs);
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001661 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001662 cutOffParsing();
1663 return;
1664 }
1665
1666 allSingleIdentifiers = false;
1667 break;
1668 } while (TryConsumeToken(tok::comma));
1669
1670 // If we parsed an identifier list, semantic analysis sorts out
1671 // whether it refers to protocols or to type arguments.
1672 if (allSingleIdentifiers) {
1673 // Parse the closing '>'.
1674 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001675 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001676 /*ObjCGenericList=*/true);
1677
1678 // Let Sema figure out what we parsed.
1679 Actions.actOnObjCTypeArgsOrProtocolQualifiers(getCurScope(),
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001680 baseType,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001681 lAngleLoc,
1682 identifiers,
1683 identifierLocs,
1684 rAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001685 typeArgsLAngleLoc,
1686 typeArgs,
1687 typeArgsRAngleLoc,
1688 protocolLAngleLoc,
1689 protocols,
1690 protocolRAngleLoc,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001691 warnOnIncompleteProtocols);
1692 return;
1693 }
1694
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001695 // We parsed an identifier list but stumbled into non single identifiers, this
1696 // means we might (a) check that what we already parsed is a legitimate type
1697 // (not a protocol or unknown type) and (b) parse the remaining ones, which
1698 // must all be type args.
Douglas Gregore9d95f12015-07-07 03:57:35 +00001699
1700 // Convert the identifiers into type arguments.
1701 bool invalid = false;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001702 IdentifierInfo *foundProtocolId = nullptr, *foundValidTypeId = nullptr;
1703 SourceLocation foundProtocolSrcLoc, foundValidTypeSrcLoc;
1704 SmallVector<IdentifierInfo *, 2> unknownTypeArgs;
1705 SmallVector<SourceLocation, 2> unknownTypeArgsLoc;
1706
Douglas Gregore9d95f12015-07-07 03:57:35 +00001707 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1708 ParsedType typeArg
1709 = Actions.getTypeName(*identifiers[i], identifierLocs[i], getCurScope());
1710 if (typeArg) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001711 DeclSpec DS(AttrFactory);
1712 const char *prevSpec = nullptr;
1713 unsigned diagID;
1714 DS.SetTypeSpecType(TST_typename, identifierLocs[i], prevSpec, diagID,
1715 typeArg, Actions.getASTContext().getPrintingPolicy());
1716
1717 // Form a declarator to turn this into a type.
1718 Declarator D(DS, Declarator::TypeNameContext);
1719 TypeResult fullTypeArg = Actions.ActOnTypeName(getCurScope(), D);
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001720 if (fullTypeArg.isUsable()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001721 typeArgs.push_back(fullTypeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001722 if (!foundValidTypeId) {
1723 foundValidTypeId = identifiers[i];
1724 foundValidTypeSrcLoc = identifierLocs[i];
1725 }
1726 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001727 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001728 unknownTypeArgs.push_back(identifiers[i]);
1729 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1730 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001731 } else {
1732 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001733 if (!Actions.LookupProtocol(identifiers[i], identifierLocs[i])) {
1734 unknownTypeArgs.push_back(identifiers[i]);
1735 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1736 } else if (!foundProtocolId) {
1737 foundProtocolId = identifiers[i];
1738 foundProtocolSrcLoc = identifierLocs[i];
1739 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001740 }
1741 }
1742
1743 // Continue parsing type-names.
1744 do {
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001745 Token CurTypeTok = Tok;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001746 TypeResult typeArg = ParseTypeName();
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001747
1748 // Consume the '...' for a pack expansion.
1749 SourceLocation ellipsisLoc;
1750 TryConsumeToken(tok::ellipsis, ellipsisLoc);
1751 if (typeArg.isUsable() && ellipsisLoc.isValid()) {
1752 typeArg = Actions.ActOnPackExpansion(typeArg.get(), ellipsisLoc);
1753 }
1754
Douglas Gregore9d95f12015-07-07 03:57:35 +00001755 if (typeArg.isUsable()) {
1756 typeArgs.push_back(typeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001757 if (!foundValidTypeId) {
1758 foundValidTypeId = CurTypeTok.getIdentifierInfo();
1759 foundValidTypeSrcLoc = CurTypeTok.getLocation();
1760 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001761 } else {
1762 invalid = true;
1763 }
1764 } while (TryConsumeToken(tok::comma));
1765
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001766 // Diagnose the mix between type args and protocols.
1767 if (foundProtocolId && foundValidTypeId)
1768 Actions.DiagnoseTypeArgsAndProtocols(foundProtocolId, foundProtocolSrcLoc,
1769 foundValidTypeId,
1770 foundValidTypeSrcLoc);
1771
1772 // Diagnose unknown arg types.
1773 ParsedType T;
1774 if (unknownTypeArgs.size())
1775 for (unsigned i = 0, e = unknownTypeArgsLoc.size(); i < e; ++i)
1776 Actions.DiagnoseUnknownTypeName(unknownTypeArgs[i], unknownTypeArgsLoc[i],
1777 getCurScope(), nullptr, T);
1778
Douglas Gregore9d95f12015-07-07 03:57:35 +00001779 // Parse the closing '>'.
1780 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001781 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001782 /*ObjCGenericList=*/true);
1783
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001784 if (invalid) {
1785 typeArgs.clear();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001786 return;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001787 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001788
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001789 // Record left/right angle locations.
1790 typeArgsLAngleLoc = lAngleLoc;
1791 typeArgsRAngleLoc = rAngleLoc;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001792}
1793
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001794void Parser::parseObjCTypeArgsAndProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001795 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001796 SourceLocation &typeArgsLAngleLoc,
1797 SmallVectorImpl<ParsedType> &typeArgs,
1798 SourceLocation &typeArgsRAngleLoc,
1799 SourceLocation &protocolLAngleLoc,
1800 SmallVectorImpl<Decl *> &protocols,
1801 SmallVectorImpl<SourceLocation> &protocolLocs,
1802 SourceLocation &protocolRAngleLoc,
1803 bool consumeLastToken) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001804 assert(Tok.is(tok::less));
1805
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001806 // Parse the first angle-bracket-delimited clause.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001807 parseObjCTypeArgsOrProtocolQualifiers(baseType,
1808 typeArgsLAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001809 typeArgs,
1810 typeArgsRAngleLoc,
1811 protocolLAngleLoc,
1812 protocols,
1813 protocolLocs,
1814 protocolRAngleLoc,
1815 consumeLastToken,
Douglas Gregore83b9562015-07-07 03:57:53 +00001816 /*warnOnIncompleteProtocols=*/false);
1817
1818 // An Objective-C object pointer followed by type arguments
1819 // can then be followed again by a set of protocol references, e.g.,
1820 // \c NSArray<NSView><NSTextDelegate>
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001821 if ((consumeLastToken && Tok.is(tok::less)) ||
1822 (!consumeLastToken && NextToken().is(tok::less))) {
1823 // If we aren't consuming the last token, the prior '>' is still hanging
1824 // there. Consume it before we parse the protocol qualifiers.
1825 if (!consumeLastToken)
1826 ConsumeToken();
1827
1828 if (!protocols.empty()) {
1829 SkipUntilFlags skipFlags = SkipUntilFlags();
1830 if (!consumeLastToken)
1831 skipFlags = skipFlags | StopBeforeMatch;
Douglas Gregore83b9562015-07-07 03:57:53 +00001832 Diag(Tok, diag::err_objc_type_args_after_protocols)
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001833 << SourceRange(protocolLAngleLoc, protocolRAngleLoc);
1834 SkipUntil(tok::greater, tok::greatergreater, skipFlags);
Douglas Gregore83b9562015-07-07 03:57:53 +00001835 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001836 ParseObjCProtocolReferences(protocols, protocolLocs,
1837 /*WarnOnDeclarations=*/false,
1838 /*ForObjCContainer=*/false,
1839 protocolLAngleLoc, protocolRAngleLoc,
1840 consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001841 }
1842 }
1843}
1844
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001845TypeResult Parser::parseObjCTypeArgsAndProtocolQualifiers(
1846 SourceLocation loc,
1847 ParsedType type,
1848 bool consumeLastToken,
1849 SourceLocation &endLoc) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001850 assert(Tok.is(tok::less));
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001851 SourceLocation typeArgsLAngleLoc;
1852 SmallVector<ParsedType, 4> typeArgs;
1853 SourceLocation typeArgsRAngleLoc;
1854 SourceLocation protocolLAngleLoc;
1855 SmallVector<Decl *, 4> protocols;
1856 SmallVector<SourceLocation, 4> protocolLocs;
1857 SourceLocation protocolRAngleLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00001858
1859 // Parse type arguments and protocol qualifiers.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001860 parseObjCTypeArgsAndProtocolQualifiers(type, typeArgsLAngleLoc, typeArgs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001861 typeArgsRAngleLoc, protocolLAngleLoc,
1862 protocols, protocolLocs,
1863 protocolRAngleLoc, consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001864
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001865 // Compute the location of the last token.
1866 if (consumeLastToken)
1867 endLoc = PrevTokLocation;
1868 else
1869 endLoc = Tok.getLocation();
1870
1871 return Actions.actOnObjCTypeArgsAndProtocolQualifiers(
1872 getCurScope(),
1873 loc,
1874 type,
1875 typeArgsLAngleLoc,
1876 typeArgs,
1877 typeArgsRAngleLoc,
1878 protocolLAngleLoc,
1879 protocols,
1880 protocolLocs,
1881 protocolRAngleLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00001882}
1883
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001884void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1885 BalancedDelimiterTracker &T,
1886 SmallVectorImpl<Decl *> &AllIvarDecls,
1887 bool RBraceMissing) {
1888 if (!RBraceMissing)
1889 T.consumeClose();
1890
1891 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1892 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1893 Actions.ActOnObjCContainerFinishDefinition();
1894 // Call ActOnFields() even if we don't have any decls. This is useful
1895 // for code rewriting tools that need to be aware of the empty list.
1896 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1897 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001898 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001899}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001900
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001901/// objc-class-instance-variables:
1902/// '{' objc-instance-variable-decl-list[opt] '}'
1903///
1904/// objc-instance-variable-decl-list:
1905/// objc-visibility-spec
1906/// objc-instance-variable-decl ';'
1907/// ';'
1908/// objc-instance-variable-decl-list objc-visibility-spec
1909/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1910/// objc-instance-variable-decl-list ';'
1911///
1912/// objc-visibility-spec:
1913/// @private
1914/// @protected
1915/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001916/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001917///
1918/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001919/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001920///
John McCall48871652010-08-21 09:40:31 +00001921void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001922 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001923 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001924 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001925 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001926
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001927 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001928 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001929
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001930 BalancedDelimiterTracker T(*this, tok::l_brace);
1931 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001932 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001933 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001934 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001935
Steve Naroff00433d32007-08-21 21:17:12 +00001936 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001937 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001938 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001939 continue;
1940 }
Mike Stump11289f42009-09-09 15:08:12 +00001941
Steve Naroff00433d32007-08-21 21:17:12 +00001942 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001943 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001944 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001945 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001946 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001947 }
1948
Steve Naroff7c348172007-08-23 18:16:40 +00001949 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001950 case tok::objc_private:
1951 case tok::objc_public:
1952 case tok::objc_protected:
1953 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001954 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001955 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001956 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001957
1958 case tok::objc_end:
1959 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001960 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1961 Tok.setKind(tok::at);
1962 Tok.setLength(1);
1963 PP.EnterToken(Tok);
1964 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1965 T, AllIvarDecls, true);
1966 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001967
1968 default:
1969 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1970 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001971 }
1972 }
Mike Stump11289f42009-09-09 15:08:12 +00001973
Douglas Gregor48d46252010-01-13 21:54:15 +00001974 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001975 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001976 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001977 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001978 }
John McCallcfefb6d2009-11-03 02:38:08 +00001979
Benjamin Kramera39beb92014-09-03 11:06:10 +00001980 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1981 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1982 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001983 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001984 Decl *Field = Actions.ActOnIvar(
1985 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1986 FD.BitfieldSize, visibility);
1987 Actions.ActOnObjCContainerFinishDefinition();
1988 if (Field)
1989 AllIvarDecls.push_back(Field);
1990 FD.complete(Field);
1991 };
John McCallcfefb6d2009-11-03 02:38:08 +00001992
Chris Lattnera12405b2008-04-10 06:46:29 +00001993 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001994 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001995 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001996
Chris Lattner0ef13522007-10-09 17:51:17 +00001997 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001998 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001999 } else {
2000 Diag(Tok, diag::err_expected_semi_decl_list);
2001 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00002002 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00002003 }
2004 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00002005 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
2006 T, AllIvarDecls, false);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002007}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002008
2009/// objc-protocol-declaration:
2010/// objc-protocol-definition
2011/// objc-protocol-forward-reference
2012///
2013/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00002014/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00002015/// objc-protocol-refs[opt]
2016/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00002017/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002018///
2019/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00002020/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002021///
James Dennett1355bd12012-06-11 06:19:40 +00002022/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00002023/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002024/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00002025Parser::DeclGroupPtrTy
2026Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
2027 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00002028 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002029 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
2030 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002031
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002032 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002033 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002034 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002035 return nullptr;
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002036 }
2037
Nico Weber69a79142013-04-04 00:15:10 +00002038 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00002039
Chris Lattner0ef13522007-10-09 17:51:17 +00002040 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002041 Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
David Blaikie0403cb12016-01-15 23:43:25 +00002042 return nullptr;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002043 }
2044 // Save the protocol name, then consume it.
2045 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
2046 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002047
Alp Toker383d2c42014-01-01 03:08:43 +00002048 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00002049 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Craig Topper0f723bb2015-10-22 05:00:01 +00002050 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtoInfo,
John McCall53fa7142010-12-24 02:08:15 +00002051 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002052 }
Mike Stump11289f42009-09-09 15:08:12 +00002053
Erik Verbruggenf9887852011-12-08 09:58:43 +00002054 CheckNestedObjCContexts(AtLoc);
2055
Chris Lattner0ef13522007-10-09 17:51:17 +00002056 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002057 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002058 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
2059
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002060 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002061 while (1) {
2062 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00002063 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002064 Diag(Tok, diag::err_expected) << tok::identifier;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002065 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002066 return nullptr;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002067 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00002068 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
2069 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002070 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00002071
Chris Lattner0ef13522007-10-09 17:51:17 +00002072 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002073 break;
2074 }
2075 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00002076 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
David Blaikie0403cb12016-01-15 23:43:25 +00002077 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002078
Craig Topper0f723bb2015-10-22 05:00:01 +00002079 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtocolRefs,
John McCall53fa7142010-12-24 02:08:15 +00002080 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00002081 }
Mike Stump11289f42009-09-09 15:08:12 +00002082
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002083 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00002084 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002085
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002086 SmallVector<Decl *, 8> ProtocolRefs;
2087 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002088 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00002089 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002090 LAngleLoc, EndProtoLoc,
2091 /*consumeLastToken=*/true))
David Blaikie0403cb12016-01-15 23:43:25 +00002092 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002093
John McCall48871652010-08-21 09:40:31 +00002094 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00002095 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002096 ProtocolRefs.data(),
2097 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00002098 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00002099 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002100
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00002101 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00002102 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002103}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002104
2105/// objc-implementation:
2106/// objc-class-implementation-prologue
2107/// objc-category-implementation-prologue
2108///
2109/// objc-class-implementation-prologue:
2110/// @implementation identifier objc-superclass[opt]
2111/// objc-class-instance-variables[opt]
2112///
2113/// objc-category-implementation-prologue:
2114/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002115Parser::DeclGroupPtrTy
2116Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002117 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
2118 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002119 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002120 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002121
Douglas Gregor49c22a72009-11-18 16:26:39 +00002122 // Code completion after '@implementation'.
2123 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002124 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002125 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002126 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +00002127 }
2128
Nico Weber69a79142013-04-04 00:15:10 +00002129 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00002130
Chris Lattner0ef13522007-10-09 17:51:17 +00002131 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002132 Diag(Tok, diag::err_expected)
2133 << tok::identifier; // missing class or category name.
David Blaikie0403cb12016-01-15 23:43:25 +00002134 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002135 }
2136 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00002137 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002138 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00002139 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002140
Douglas Gregor85f3f952015-07-07 03:57:15 +00002141 // Neither a type parameter list nor a list of protocol references is
2142 // permitted here. Parse and diagnose them.
2143 if (Tok.is(tok::less)) {
2144 SourceLocation lAngleLoc, rAngleLoc;
2145 SmallVector<IdentifierLocPair, 8> protocolIdents;
2146 SourceLocation diagLoc = Tok.getLocation();
Richard Smith3df3f1d2015-11-03 01:19:56 +00002147 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
2148 if (parseObjCTypeParamListOrProtocolRefs(typeParamScope, lAngleLoc,
2149 protocolIdents, rAngleLoc)) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00002150 Diag(diagLoc, diag::err_objc_parameterized_implementation)
2151 << SourceRange(diagLoc, PrevTokLocation);
2152 } else if (lAngleLoc.isValid()) {
2153 Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
2154 << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
2155 }
2156 }
2157
Mike Stump11289f42009-09-09 15:08:12 +00002158 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002159 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002160 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002161 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002162 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002163
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002164 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002165 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002166 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002167 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002168 }
2169
Chris Lattner0ef13522007-10-09 17:51:17 +00002170 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002171 categoryId = Tok.getIdentifierInfo();
2172 categoryLoc = ConsumeToken();
2173 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002174 Diag(Tok, diag::err_expected)
2175 << tok::identifier; // missing category name.
David Blaikie0403cb12016-01-15 23:43:25 +00002176 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002177 }
Chris Lattner0ef13522007-10-09 17:51:17 +00002178 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002179 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002180 SkipUntil(tok::r_paren); // don't stop at ';'
David Blaikie0403cb12016-01-15 23:43:25 +00002181 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002182 }
2183 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00002184 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2185 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002186 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2187 SmallVector<Decl *, 4> protocols;
2188 SmallVector<SourceLocation, 4> protocolLocs;
2189 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2190 /*warnOnIncompleteProtocols=*/false,
2191 /*ForObjCContainer=*/false,
2192 protocolLAngleLoc, protocolRAngleLoc,
2193 /*consumeLastToken=*/true);
Fariborz Jahanian85888552013-05-17 17:58:11 +00002194 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002195 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002196 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00002197 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002198
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002199 } else {
2200 // We have a class implementation
2201 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002202 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00002203 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002204 // We have a super class
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002205 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002206 Diag(Tok, diag::err_expected)
2207 << tok::identifier; // missing super class name.
David Blaikie0403cb12016-01-15 23:43:25 +00002208 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002209 }
2210 superClassId = Tok.getIdentifierInfo();
2211 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002212 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002213 ObjCImpDecl = Actions.ActOnStartClassImplementation(
2214 AtLoc, nameId, nameLoc,
2215 superClassId, superClassLoc);
2216
2217 if (Tok.is(tok::l_brace)) // we have ivars
2218 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002219 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2220 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002221
2222 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2223 SmallVector<Decl *, 4> protocols;
2224 SmallVector<SourceLocation, 4> protocolLocs;
2225 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2226 /*warnOnIncompleteProtocols=*/false,
2227 /*ForObjCContainer=*/false,
2228 protocolLAngleLoc, protocolRAngleLoc,
2229 /*consumeLastToken=*/true);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002230 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002231 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002232 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002233
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002234 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002235
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002236 {
2237 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00002238 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002239 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002240 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002241 MaybeParseMicrosoftAttributes(attrs);
2242 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
2243 DeclGroupRef DG = DGP.get();
2244 DeclsInGroup.append(DG.begin(), DG.end());
2245 }
2246 }
2247 }
2248
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00002249 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002250}
Steve Naroff33a1e802007-10-29 21:38:07 +00002251
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002252Parser::DeclGroupPtrTy
2253Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002254 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
2255 "ParseObjCAtEndDeclaration(): Expected @end");
2256 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002257 if (CurParsedObjCImpl)
2258 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002259 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00002260 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002261 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
David Blaikie0403cb12016-01-15 23:43:25 +00002262 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002263}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002264
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002265Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
2266 if (!Finished) {
2267 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00002268 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002269 P.Diag(P.Tok, diag::err_objc_missing_end)
2270 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
2271 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
2272 << Sema::OCK_Implementation;
2273 }
2274 }
Craig Topper161e4db2014-05-21 06:02:52 +00002275 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002276 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002277}
2278
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002279void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
2280 assert(!Finished);
2281 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
2282 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002283 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2284 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002285
2286 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
2287
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002288 if (HasCFunction)
2289 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
2290 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2291 false/*c-functions*/);
2292
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002293 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002294 for (LateParsedObjCMethodContainer::iterator
2295 I = LateParsedObjCMethods.begin(),
2296 E = LateParsedObjCMethods.end(); I != E; ++I)
2297 delete *I;
2298 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002299
2300 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002301}
2302
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002303/// compatibility-alias-decl:
2304/// @compatibility_alias alias-name class-name ';'
2305///
John McCall48871652010-08-21 09:40:31 +00002306Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002307 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
2308 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
2309 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00002310 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002311 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002312 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002313 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002314 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
2315 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00002316 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002317 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002318 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002319 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002320 IdentifierInfo *classId = Tok.getIdentifierInfo();
2321 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00002322 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00002323 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
2324 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002325}
2326
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002327/// property-synthesis:
2328/// @synthesize property-ivar-list ';'
2329///
2330/// property-ivar-list:
2331/// property-ivar
2332/// property-ivar-list ',' property-ivar
2333///
2334/// property-ivar:
2335/// identifier
2336/// identifier '=' identifier
2337///
John McCall48871652010-08-21 09:40:31 +00002338Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002339 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00002340 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002341 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00002342
Douglas Gregor88e72a02009-11-18 19:45:45 +00002343 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00002344 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002345 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002346 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002347 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002348 }
2349
Douglas Gregor88e72a02009-11-18 19:45:45 +00002350 if (Tok.isNot(tok::identifier)) {
2351 Diag(Tok, diag::err_synthesized_property_name);
2352 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002353 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00002354 }
Craig Topper161e4db2014-05-21 06:02:52 +00002355
2356 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002357 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2358 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002359 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00002360 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002361 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00002362 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002363 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002364 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002365 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002366 }
2367
Chris Lattner0ef13522007-10-09 17:51:17 +00002368 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002369 Diag(Tok, diag::err_expected) << tok::identifier;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002370 break;
2371 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002372 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002373 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002374 }
Manman Ren5b786402016-01-28 18:49:28 +00002375 Actions.ActOnPropertyImplDecl(
2376 getCurScope(), atLoc, propertyLoc, true,
2377 propertyId, propertyIvar, propertyIvarLoc,
2378 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Chris Lattner0ef13522007-10-09 17:51:17 +00002379 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002380 break;
2381 ConsumeToken(); // consume ','
2382 }
Alp Toker383d2c42014-01-01 03:08:43 +00002383 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00002384 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002385}
2386
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002387/// property-dynamic:
2388/// @dynamic property-list
2389///
2390/// property-list:
2391/// identifier
2392/// property-list ',' identifier
2393///
John McCall48871652010-08-21 09:40:31 +00002394Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002395 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2396 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002397 ConsumeToken(); // consume dynamic
Manman Ren0fe61f82016-01-29 19:05:57 +00002398
2399 bool isClassProperty = false;
2400 if (Tok.is(tok::l_paren)) {
2401 ConsumeParen();
2402 const IdentifierInfo *II = Tok.getIdentifierInfo();
2403
2404 if (!II) {
2405 Diag(Tok, diag::err_objc_expected_property_attr) << II;
2406 SkipUntil(tok::r_paren, StopAtSemi);
2407 } else {
2408 SourceLocation AttrName = ConsumeToken(); // consume attribute name
2409 if (II->isStr("class")) {
2410 isClassProperty = true;
2411 if (Tok.isNot(tok::r_paren)) {
2412 Diag(Tok, diag::err_expected) << tok::r_paren;
2413 SkipUntil(tok::r_paren, StopAtSemi);
2414 } else
2415 ConsumeParen();
2416 } else {
2417 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
2418 SkipUntil(tok::r_paren, StopAtSemi);
2419 }
2420 }
2421 }
2422
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002423 while (true) {
2424 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002425 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002426 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002427 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002428 }
2429
2430 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002431 Diag(Tok, diag::err_expected) << tok::identifier;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002432 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002433 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002434 }
2435
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002436 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2437 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Manman Ren5b786402016-01-28 18:49:28 +00002438 Actions.ActOnPropertyImplDecl(
2439 getCurScope(), atLoc, propertyLoc, false,
2440 propertyId, nullptr, SourceLocation(),
Manman Ren0fe61f82016-01-29 19:05:57 +00002441 isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
Manman Ren5b786402016-01-28 18:49:28 +00002442 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002443
Chris Lattner0ef13522007-10-09 17:51:17 +00002444 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002445 break;
2446 ConsumeToken(); // consume ','
2447 }
Alp Toker383d2c42014-01-01 03:08:43 +00002448 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00002449 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002450}
Mike Stump11289f42009-09-09 15:08:12 +00002451
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002452/// objc-throw-statement:
2453/// throw expression[opt];
2454///
John McCalldadc5752010-08-24 06:29:42 +00002455StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2456 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002457 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00002458 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00002459 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002460 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002461 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002462 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002463 }
2464 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00002465 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00002466 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002467 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002468}
2469
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002470/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002471/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002472///
John McCalldadc5752010-08-24 06:29:42 +00002473StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002474Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002475 ConsumeToken(); // consume synchronized
2476 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002477 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002478 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002479 }
John McCalld9bb7432011-07-27 21:50:02 +00002480
2481 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002482 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00002483 ExprResult operand(ParseExpression());
2484
2485 if (Tok.is(tok::r_paren)) {
2486 ConsumeParen(); // ')'
2487 } else {
2488 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002489 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00002490
2491 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002492 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002493 }
John McCalld9bb7432011-07-27 21:50:02 +00002494
2495 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002496 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00002497 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002498 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002499 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002500 }
Steve Naroffd9c26072008-06-04 20:36:13 +00002501
John McCalld9bb7432011-07-27 21:50:02 +00002502 // Check the @synchronized operand now.
2503 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002504 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002505
John McCalld9bb7432011-07-27 21:50:02 +00002506 // Parse the compound statement within a new scope.
2507 ParseScope bodyScope(this, Scope::DeclScope);
2508 StmtResult body(ParseCompoundStatementBody());
2509 bodyScope.Exit();
2510
2511 // If there was a semantic or parse error earlier with the
2512 // operand, fail now.
2513 if (operand.isInvalid())
2514 return StmtError();
2515
2516 if (body.isInvalid())
2517 body = Actions.ActOnNullStmt(Tok.getLocation());
2518
2519 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002520}
2521
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002522/// objc-try-catch-statement:
2523/// @try compound-statement objc-catch-list[opt]
2524/// @try compound-statement objc-catch-list[opt] @finally compound-statement
2525///
2526/// objc-catch-list:
2527/// @catch ( parameter-declaration ) compound-statement
2528/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2529/// catch-parameter-declaration:
2530/// parameter-declaration
2531/// '...' [OBJC2]
2532///
John McCalldadc5752010-08-24 06:29:42 +00002533StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002534 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002535
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002536 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00002537 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002538 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002539 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002540 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00002541 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00002542 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002543 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00002544 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002545 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002546 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002547 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002548
Chris Lattner0ef13522007-10-09 17:51:17 +00002549 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002550 // At this point, we need to lookahead to determine if this @ is the start
2551 // of an @catch or @finally. We don't want to consume the @ token if this
2552 // is an @try or @encode or something else.
2553 Token AfterAt = GetLookAheadToken(1);
2554 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2555 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2556 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002557
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002558 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002559 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002560 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002561 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002562 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002563 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00002564 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002565 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002566 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002567 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00002568 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002569 ParseDeclarator(ParmDecl);
2570
Douglas Gregore11ee112010-04-23 23:01:43 +00002571 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002572 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002573 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002574 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002575 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002576
Steve Naroff65a00892009-04-07 22:56:58 +00002577 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002578
Steve Naroff65a00892009-04-07 22:56:58 +00002579 if (Tok.is(tok::r_paren))
2580 RParenLoc = ConsumeParen();
2581 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002582 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002583
John McCalldadc5752010-08-24 06:29:42 +00002584 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002585 if (Tok.is(tok::l_brace))
2586 CatchBody = ParseCompoundStatementBody();
2587 else
Alp Tokerec543272013-12-24 09:48:30 +00002588 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002589 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002590 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00002591
John McCalldadc5752010-08-24 06:29:42 +00002592 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00002593 RParenLoc,
2594 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002595 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002596 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002597 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002598
Steve Naroffe6016792008-02-05 21:27:35 +00002599 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002600 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2601 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002602 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002603 }
2604 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002605 } else {
2606 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002607 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002608 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002609
John McCalldadc5752010-08-24 06:29:42 +00002610 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002611 if (Tok.is(tok::l_brace))
2612 FinallyBody = ParseCompoundStatementBody();
2613 else
Alp Tokerec543272013-12-24 09:48:30 +00002614 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002615 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002616 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002617 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002618 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002619 catch_or_finally_seen = true;
2620 break;
2621 }
2622 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002623 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002624 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002625 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002626 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002627
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002628 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002629 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002630 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002631}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002632
John McCall31168b02011-06-15 23:02:42 +00002633/// objc-autoreleasepool-statement:
2634/// @autoreleasepool compound-statement
2635///
2636StmtResult
2637Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2638 ConsumeToken(); // consume autoreleasepool
2639 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002640 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002641 return StmtError();
2642 }
2643 // Enter a scope to hold everything within the compound stmt. Compound
2644 // statements can always hold declarations.
2645 ParseScope BodyScope(this, Scope::DeclScope);
2646
2647 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2648
2649 BodyScope.Exit();
2650 if (AutoreleasePoolBody.isInvalid())
2651 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2652 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002653 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002654}
2655
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002656/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2657/// for later parsing.
2658void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002659 if (SkipFunctionBodies && (!MDecl || Actions.canSkipFunctionBody(MDecl)) &&
2660 trySkippingFunctionBody()) {
2661 Actions.ActOnSkippedFunctionBody(MDecl);
2662 return;
2663 }
2664
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002665 LexedMethod* LM = new LexedMethod(this, MDecl);
2666 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2667 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002668 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002669 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002670 if (Tok.is(tok::kw_try)) {
2671 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002672 if (Tok.is(tok::colon)) {
2673 Toks.push_back(Tok);
2674 ConsumeToken();
2675 while (Tok.isNot(tok::l_brace)) {
2676 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2677 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2678 }
2679 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002680 Toks.push_back(Tok); // also store '{'
2681 }
2682 else if (Tok.is(tok::colon)) {
2683 ConsumeToken();
Richard Smithb9fa9962015-08-21 03:04:33 +00002684 // FIXME: This is wrong, due to C++11 braced initialization.
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002685 while (Tok.isNot(tok::l_brace)) {
2686 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2687 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2688 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002689 Toks.push_back(Tok); // also store '{'
2690 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002691 ConsumeBrace();
2692 // Consume everything up to (and including) the matching right brace.
2693 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002694 while (Tok.is(tok::kw_catch)) {
2695 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2696 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2697 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002698}
2699
Steve Naroff09bf8152007-09-06 21:24:23 +00002700/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002701///
John McCall48871652010-08-21 09:40:31 +00002702Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002703 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002704
John McCallfaf5fb42010-08-26 23:41:50 +00002705 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2706 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002707
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002708 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002709 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002710 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002711 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002712 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002713 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002714 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002715 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002716
Steve Naroffbb875722007-11-11 19:54:21 +00002717 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002718 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002719 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002720
Steve Naroffbb875722007-11-11 19:54:21 +00002721 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002722 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002723
Steve Naroffbb875722007-11-11 19:54:21 +00002724 // If we didn't find the '{', bail out.
2725 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002726 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002727 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002728
2729 if (!MDecl) {
2730 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002731 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002732 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002733 }
2734
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002735 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002736 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002737 assert (CurParsedObjCImpl
2738 && "ParseObjCMethodDefinition - Method out of @implementation");
2739 // Consume the tokens and store them for later parsing.
2740 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002741 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002742}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002743
John McCalldadc5752010-08-24 06:29:42 +00002744StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002745 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002746 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002747 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002748 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002749 }
2750
2751 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002752 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002753
2754 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002755 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002756
2757 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002758 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002759
2760 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2761 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002762
2763 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2764 getLangOpts().DebuggerSupport) {
2765 SkipUntil(tok::semi);
2766 return Actions.ActOnNullStmt(Tok.getLocation());
2767 }
2768
John McCalldadc5752010-08-24 06:29:42 +00002769 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002770 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002771 // If the expression is invalid, skip ahead to the next semicolon. Not
2772 // doing this opens us up to the possibility of infinite loops if
2773 // ParseExpression does not consume any tokens.
2774 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002775 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002776 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002777
Steve Naroffe6016792008-02-05 21:27:35 +00002778 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002779 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002780 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002781}
2782
John McCalldadc5752010-08-24 06:29:42 +00002783ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002784 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002785 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002786 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002787 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002788 return ExprError();
2789
Ted Kremeneke65b0862012-03-06 20:05:56 +00002790 case tok::minus:
2791 case tok::plus: {
2792 tok::TokenKind Kind = Tok.getKind();
2793 SourceLocation OpLoc = ConsumeToken();
2794
2795 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002796 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002797 switch (Kind) {
2798 case tok::minus: Symbol = "-"; break;
2799 case tok::plus: Symbol = "+"; break;
2800 default: llvm_unreachable("missing unary operator case");
2801 }
2802 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2803 << Symbol;
2804 return ExprError();
2805 }
2806
2807 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2808 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002809 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002810 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002811 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002812
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002813 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002814 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002815 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002816
2817 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002818 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002819 }
2820
Chris Lattnere002fbe2007-12-12 01:04:12 +00002821 case tok::string_literal: // primary-expression: string-literal
2822 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002823 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002824
2825 case tok::char_constant:
2826 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2827
2828 case tok::numeric_constant:
2829 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2830
2831 case tok::kw_true: // Objective-C++, etc.
2832 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2833 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2834 case tok::kw_false: // Objective-C++, etc.
2835 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2836 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2837
2838 case tok::l_square:
2839 // Objective-C array literal
2840 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2841
2842 case tok::l_brace:
2843 // Objective-C dictionary literal
2844 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2845
Patrick Beard0caa3942012-04-19 00:25:12 +00002846 case tok::l_paren:
2847 // Objective-C boxed expression
2848 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2849
Chris Lattnere002fbe2007-12-12 01:04:12 +00002850 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002851 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002852 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002853
Chris Lattner197a3012008-08-05 06:19:09 +00002854 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2855 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002856 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002857 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002858 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002859 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002860 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Erik Pilkington29099de2016-07-16 00:35:23 +00002861 case tok::objc_available:
2862 return ParseAvailabilityCheckExpr(AtLoc);
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002863 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002864 const char *str = nullptr;
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002865 if (GetLookAheadToken(1).is(tok::l_brace)) {
2866 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2867 str =
2868 ch == 't' ? "try"
2869 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002870 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002871 }
2872 if (str) {
2873 SourceLocation kwLoc = Tok.getLocation();
2874 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2875 FixItHint::CreateReplacement(kwLoc, str));
2876 }
2877 else
2878 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2879 }
Chris Lattner197a3012008-08-05 06:19:09 +00002880 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002881 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002882}
2883
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002884/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002885///
2886/// This routine parses the receiver of a message send in
2887/// Objective-C++ either as a type or as an expression. Note that this
2888/// routine must not be called to parse a send to 'super', since it
2889/// has no way to return such a result.
2890///
2891/// \param IsExpr Whether the receiver was parsed as an expression.
2892///
2893/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2894/// IsExpr is true), the parsed expression. If the receiver was parsed
2895/// as a type (\c IsExpr is false), the parsed type.
2896///
2897/// \returns True if an error occurred during parsing or semantic
2898/// analysis, in which case the arguments do not have valid
2899/// values. Otherwise, returns false for a successful parse.
2900///
2901/// objc-receiver: [C++]
2902/// 'super' [not parsed here]
2903/// expression
2904/// simple-type-specifier
2905/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002906bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002907 InMessageExpressionRAIIObject InMessage(*this, true);
2908
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002909 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2910 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002911 TryAnnotateTypeOrScopeToken();
2912
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002913 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002914 // objc-receiver:
2915 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002916 // Make sure any typos in the receiver are corrected or diagnosed, so that
2917 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2918 // only the things that are valid ObjC receivers?
2919 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002920 if (Receiver.isInvalid())
2921 return true;
2922
2923 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002924 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002925 return false;
2926 }
2927
2928 // objc-receiver:
2929 // typename-specifier
2930 // simple-type-specifier
2931 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002932 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002933 ParseCXXSimpleTypeSpecifier(DS);
2934
2935 if (Tok.is(tok::l_paren)) {
2936 // If we see an opening parentheses at this point, we are
2937 // actually parsing an expression that starts with a
2938 // function-style cast, e.g.,
2939 //
2940 // postfix-expression:
2941 // simple-type-specifier ( expression-list [opt] )
2942 // typename-specifier ( expression-list [opt] )
2943 //
2944 // Parse the remainder of this case, then the (optional)
2945 // postfix-expression suffix, followed by the (optional)
2946 // right-hand side of the binary expression. We have an
2947 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002948 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002949 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002950 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002951 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002952 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002953 if (Receiver.isInvalid())
2954 return true;
2955
2956 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002957 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002958 return false;
2959 }
2960
2961 // We have a class message. Turn the simple-type-specifier or
2962 // typename-specifier we parsed into a type and parse the
2963 // remainder of the class message.
2964 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002965 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002966 if (Type.isInvalid())
2967 return true;
2968
2969 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002970 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002971 return false;
2972}
2973
Douglas Gregor990ccac2010-05-31 14:40:22 +00002974/// \brief Determine whether the parser is currently referring to a an
2975/// Objective-C message send, using a simplified heuristic to avoid overhead.
2976///
2977/// This routine will only return true for a subset of valid message-send
2978/// expressions.
2979bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002980 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002981 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002982 return GetLookAheadToken(1).is(tok::identifier) &&
2983 GetLookAheadToken(2).is(tok::identifier);
2984}
2985
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002986bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002987 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002988 InMessageExpression)
2989 return false;
2990
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002991 ParsedType Type;
2992
2993 if (Tok.is(tok::annot_typename))
2994 Type = getTypeAnnotation(Tok);
2995 else if (Tok.is(tok::identifier))
2996 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2997 getCurScope());
2998 else
2999 return false;
3000
3001 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
3002 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003003 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003004 if (Tok.is(tok::identifier))
3005 TryAnnotateTypeOrScopeToken();
3006
3007 return Tok.is(tok::annot_typename);
3008 }
3009 }
3010
3011 return false;
3012}
3013
Mike Stump11289f42009-09-09 15:08:12 +00003014/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003015/// '[' objc-receiver objc-message-args ']'
3016///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003017/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00003018/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003019/// expression
3020/// class-name
3021/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00003022///
John McCalldadc5752010-08-24 06:29:42 +00003023ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00003024 assert(Tok.is(tok::l_square) && "'[' expected");
3025 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
3026
Douglas Gregora817a192010-05-27 23:06:34 +00003027 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003028 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003029 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00003030 return ExprError();
3031 }
3032
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003033 InMessageExpressionRAIIObject InMessage(*this, true);
3034
David Blaikiebbafb8a2012-03-11 07:00:24 +00003035 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00003036 // We completely separate the C and C++ cases because C++ requires
3037 // more complicated (read: slower) parsing.
3038
3039 // Handle send to super.
3040 // FIXME: This doesn't benefit from the same typo-correction we
3041 // get in Objective-C.
3042 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00003043 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
David Blaikieefdccaa2016-01-15 23:43:34 +00003044 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3045 nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003046
3047 // Parse the receiver, which is either a type or an expression.
3048 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00003049 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00003050 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003051 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003052 return ExprError();
3053 }
3054
3055 if (IsExpr)
David Blaikieefdccaa2016-01-15 23:43:34 +00003056 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3057 static_cast<Expr *>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00003058
3059 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00003060 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00003061 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00003062 }
3063
3064 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00003065 IdentifierInfo *Name = Tok.getIdentifierInfo();
3066 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00003067 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003068 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00003069 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00003070 NextToken().is(tok::period),
3071 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00003072 case Sema::ObjCSuperMessage:
David Blaikieefdccaa2016-01-15 23:43:34 +00003073 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3074 nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003075
John McCallfaf5fb42010-08-26 23:41:50 +00003076 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00003077 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003078 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003079 return ExprError();
3080 }
3081
Douglas Gregore5798dc2010-04-21 20:38:13 +00003082 ConsumeToken(); // the type name
3083
Douglas Gregore83b9562015-07-07 03:57:53 +00003084 // Parse type arguments and protocol qualifiers.
3085 if (Tok.is(tok::less)) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003086 SourceLocation NewEndLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00003087 TypeResult NewReceiverType
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003088 = parseObjCTypeArgsAndProtocolQualifiers(NameLoc, ReceiverType,
3089 /*consumeLastToken=*/true,
3090 NewEndLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00003091 if (!NewReceiverType.isUsable()) {
3092 SkipUntil(tok::r_square, StopAtSemi);
3093 return ExprError();
3094 }
3095
3096 ReceiverType = NewReceiverType.get();
3097 }
3098
Douglas Gregore5798dc2010-04-21 20:38:13 +00003099 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00003100 ReceiverType, nullptr);
3101
John McCallfaf5fb42010-08-26 23:41:50 +00003102 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003103 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00003104 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00003105 }
Chris Lattner8f697062008-01-25 18:59:06 +00003106 }
Chris Lattnera36ec422010-04-11 08:28:14 +00003107
3108 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00003109 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003110 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003111 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003112 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00003113 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003114
David Blaikieefdccaa2016-01-15 23:43:34 +00003115 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3116 Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00003117}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003118
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003119/// \brief Parse the remainder of an Objective-C message following the
3120/// '[' objc-receiver.
3121///
3122/// This routine handles sends to super, class messages (sent to a
3123/// class name), and instance messages (sent to an object), and the
3124/// target is represented by \p SuperLoc, \p ReceiverType, or \p
3125/// ReceiverExpr, respectively. Only one of these parameters may have
3126/// a valid value.
3127///
3128/// \param LBracLoc The location of the opening '['.
3129///
3130/// \param SuperLoc If this is a send to 'super', the location of the
3131/// 'super' keyword that indicates a send to the superclass.
3132///
3133/// \param ReceiverType If this is a class message, the type of the
3134/// class we are sending a message to.
3135///
3136/// \param ReceiverExpr If this is an instance message, the expression
3137/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00003138///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003139/// objc-message-args:
3140/// objc-selector
3141/// objc-keywordarg-list
3142///
3143/// objc-keywordarg-list:
3144/// objc-keywordarg
3145/// objc-keywordarg-list objc-keywordarg
3146///
Mike Stump11289f42009-09-09 15:08:12 +00003147/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003148/// selector-name[opt] ':' objc-keywordexpr
3149///
3150/// objc-keywordexpr:
3151/// nonempty-expr-list
3152///
3153/// nonempty-expr-list:
3154/// assignment-expression
3155/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003156///
John McCalldadc5752010-08-24 06:29:42 +00003157ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00003158Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003159 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00003160 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00003161 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003162 InMessageExpressionRAIIObject InMessage(*this, true);
3163
Steve Naroffeae65032009-11-07 02:08:14 +00003164 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003165 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003166 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003167 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003168 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003169 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003170 false);
Steve Naroffeae65032009-11-07 02:08:14 +00003171 else
John McCallb268a282010-08-23 23:25:46 +00003172 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003173 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003174 cutOffParsing();
3175 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00003176 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00003177
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003178 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00003179 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003180 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003181
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003182 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003183 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003184 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00003185
Chris Lattner0ef13522007-10-09 17:51:17 +00003186 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003187 while (1) {
3188 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00003189 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003190 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00003191
Alp Toker383d2c42014-01-01 03:08:43 +00003192 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00003193 // We must manually skip to a ']', otherwise the expression skipper will
3194 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3195 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003196 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003197 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003198 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003199
Mike Stump11289f42009-09-09 15:08:12 +00003200 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003201
3202 if (Tok.is(tok::code_completion)) {
3203 if (SuperLoc.isValid())
3204 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003205 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003206 /*AtArgumentEpression=*/true);
3207 else if (ReceiverType)
3208 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003209 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003210 /*AtArgumentEpression=*/true);
3211 else
3212 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003213 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003214 /*AtArgumentEpression=*/true);
3215
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003216 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003217 return ExprError();
3218 }
3219
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00003220 ExprResult Expr;
3221 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3222 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3223 Expr = ParseBraceInitializer();
3224 } else
3225 Expr = ParseAssignmentExpression();
3226
3227 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003228 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00003229 // We must manually skip to a ']', otherwise the expression skipper will
3230 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3231 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003232 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003233 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00003234 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003235
Steve Naroff486760a2007-09-17 20:25:27 +00003236 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003237 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003238
Douglas Gregor1b605f72009-11-19 01:08:35 +00003239 // Code completion after each argument.
3240 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003241 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003242 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003243 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003244 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003245 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003246 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003247 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003248 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00003249 else
John McCallb268a282010-08-23 23:25:46 +00003250 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003251 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003252 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003253 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003254 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00003255 }
3256
Steve Naroff486760a2007-09-17 20:25:27 +00003257 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00003258 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00003259 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003260 break;
3261 // We have a selector or a colon, continue parsing.
3262 }
3263 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00003264 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003265 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00003266 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00003267 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00003268 if (Tok.is(tok::colon))
3269 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003270 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003271 if (Tok.is(tok::colon)) {
3272 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
3273 FixItHint::CreateRemoval(commaLoc);
3274 }
Chris Lattner197a3012008-08-05 06:19:09 +00003275 // We must manually skip to a ']', otherwise the expression skipper will
3276 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3277 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003278 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003279 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003280 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003281
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003282 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003283 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003284 }
3285 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00003286 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003287
Chris Lattner197a3012008-08-05 06:19:09 +00003288 // We must manually skip to a ']', otherwise the expression skipper will
3289 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3290 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003291 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003292 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003293 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00003294
Chris Lattner0ef13522007-10-09 17:51:17 +00003295 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00003296 Diag(Tok, diag::err_expected)
3297 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00003298 // We must manually skip to a ']', otherwise the expression skipper will
3299 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3300 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003301 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003302 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003303 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003304
Chris Lattner8f697062008-01-25 18:59:06 +00003305 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003306
Steve Naroffe61bfa82007-10-05 18:42:47 +00003307 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003308 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00003309 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003310 KeyLocs.push_back(Loc);
3311 }
Chris Lattner5700fab2007-10-07 02:00:24 +00003312 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003313
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003314 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003315 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003316 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003317 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003318 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003319 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00003320 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003321 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003322}
3323
John McCalldadc5752010-08-24 06:29:42 +00003324ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
3325 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003326 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003327
Chris Lattnere002fbe2007-12-12 01:04:12 +00003328 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
3329 // expressions. At this point, we know that the only valid thing that starts
3330 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003331 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003332 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00003333 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003334 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003335
Chris Lattnere002fbe2007-12-12 01:04:12 +00003336 while (Tok.is(tok::at)) {
3337 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00003338
Sebastian Redlc13f2682008-12-09 20:22:58 +00003339 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00003340 if (!isTokenStringLiteral())
3341 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00003342
John McCalldadc5752010-08-24 06:29:42 +00003343 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003344 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003345 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003346
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003347 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00003348 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003349
Craig Topper883dd332015-12-24 23:58:11 +00003350 return Actions.ParseObjCStringLiteral(AtLocs.data(), AtStrings);
Anders Carlsson76f4a902007-08-21 17:43:55 +00003351}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003352
Ted Kremeneke65b0862012-03-06 20:05:56 +00003353/// ParseObjCBooleanLiteral -
3354/// objc-scalar-literal : '@' boolean-keyword
3355/// ;
3356/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
3357/// ;
3358ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
3359 bool ArgValue) {
3360 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
3361 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
3362}
3363
3364/// ParseObjCCharacterLiteral -
3365/// objc-scalar-literal : '@' character-literal
3366/// ;
3367ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
3368 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
3369 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003370 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003371 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003372 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003373 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003374}
3375
3376/// ParseObjCNumericLiteral -
3377/// objc-scalar-literal : '@' scalar-literal
3378/// ;
3379/// scalar-literal : | numeric-constant /* any numeric constant. */
3380/// ;
3381ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3382 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3383 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003384 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003385 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003386 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003387 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003388}
3389
Patrick Beard0caa3942012-04-19 00:25:12 +00003390/// ParseObjCBoxedExpr -
3391/// objc-box-expression:
3392/// @( assignment-expression )
3393ExprResult
3394Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3395 if (Tok.isNot(tok::l_paren))
3396 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3397
3398 BalancedDelimiterTracker T(*this, tok::l_paren);
3399 T.consumeOpen();
3400 ExprResult ValueExpr(ParseAssignmentExpression());
3401 if (T.consumeClose())
3402 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00003403
3404 if (ValueExpr.isInvalid())
3405 return ExprError();
3406
Patrick Beard0caa3942012-04-19 00:25:12 +00003407 // Wrap the sub-expression in a parenthesized expression, to distinguish
3408 // a boxed expression from a literal.
3409 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003410 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00003411 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003412 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00003413}
3414
Ted Kremeneke65b0862012-03-06 20:05:56 +00003415ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00003416 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003417 ConsumeBracket(); // consume the l_square.
3418
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003419 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003420 while (Tok.isNot(tok::r_square)) {
3421 // Parse list of array element expressions (all must be id types).
3422 ExprResult Res(ParseAssignmentExpression());
3423 if (Res.isInvalid()) {
3424 // We must manually skip to a ']', otherwise the expression skipper will
3425 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3426 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003427 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003428 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003429 }
3430
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003431 Res = Actions.CorrectDelayedTyposInExpr(Res.get());
3432 if (Res.isInvalid())
3433 HasInvalidEltExpr = true;
3434
Ted Kremeneke65b0862012-03-06 20:05:56 +00003435 // Parse the ellipsis that indicates a pack expansion.
3436 if (Tok.is(tok::ellipsis))
3437 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
3438 if (Res.isInvalid())
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003439 HasInvalidEltExpr = true;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003440
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003441 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003442
3443 if (Tok.is(tok::comma))
3444 ConsumeToken(); // Eat the ','.
3445 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00003446 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3447 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003448 }
3449 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003450
3451 if (HasInvalidEltExpr)
3452 return ExprError();
3453
Benjamin Kramerf0623432012-08-23 22:51:59 +00003454 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00003455 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003456}
3457
3458ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3459 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3460 ConsumeBrace(); // consume the l_square.
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003461 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003462 while (Tok.isNot(tok::r_brace)) {
3463 // Parse the comma separated key : value expressions.
3464 ExprResult KeyExpr;
3465 {
3466 ColonProtectionRAIIObject X(*this);
3467 KeyExpr = ParseAssignmentExpression();
3468 if (KeyExpr.isInvalid()) {
3469 // We must manually skip to a '}', otherwise the expression skipper will
3470 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3471 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003472 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003473 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003474 }
3475 }
3476
Alp Toker383d2c42014-01-01 03:08:43 +00003477 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003478 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00003479 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003480 }
3481
3482 ExprResult ValueExpr(ParseAssignmentExpression());
3483 if (ValueExpr.isInvalid()) {
3484 // We must manually skip to a '}', otherwise the expression skipper will
3485 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3486 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003487 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003488 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003489 }
3490
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003491 // Check the key and value for possible typos
3492 KeyExpr = Actions.CorrectDelayedTyposInExpr(KeyExpr.get());
3493 ValueExpr = Actions.CorrectDelayedTyposInExpr(ValueExpr.get());
3494 if (KeyExpr.isInvalid() || ValueExpr.isInvalid())
3495 HasInvalidEltExpr = true;
3496
3497 // Parse the ellipsis that designates this as a pack expansion. Do not
3498 // ActOnPackExpansion here, leave it to template instantiation time where
3499 // we can get better diagnostics.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003500 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00003501 if (getLangOpts().CPlusPlus)
3502 TryConsumeToken(tok::ellipsis, EllipsisLoc);
3503
Ted Kremeneke65b0862012-03-06 20:05:56 +00003504 // We have a valid expression. Collect it in a vector so we can
3505 // build the argument list.
3506 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00003507 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00003508 };
3509 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00003510
3511 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00003512 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3513 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003514 }
3515 SourceLocation EndLoc = ConsumeBrace();
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003516
3517 if (HasInvalidEltExpr)
3518 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003519
3520 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00003521 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
Craig Topperd4336e02015-12-24 23:58:15 +00003522 Elements);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003523}
3524
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003525/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00003526/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00003527ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003528Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00003529 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003530
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003531 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003532
Chris Lattner197a3012008-08-05 06:19:09 +00003533 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003534 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3535
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003536 BalancedDelimiterTracker T(*this, tok::l_paren);
3537 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003538
Douglas Gregor220cac52009-02-18 17:45:20 +00003539 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003540
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003541 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003542
Douglas Gregor220cac52009-02-18 17:45:20 +00003543 if (Ty.isInvalid())
3544 return ExprError();
3545
Nico Webera7c7e602012-12-31 00:28:03 +00003546 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3547 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003548}
Anders Carlssone01493d2007-08-23 15:25:28 +00003549
3550/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00003551/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00003552ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003553Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00003554 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003555
Chris Lattner197a3012008-08-05 06:19:09 +00003556 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003557 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3558
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003559 BalancedDelimiterTracker T(*this, tok::l_paren);
3560 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003561
Chris Lattner197a3012008-08-05 06:19:09 +00003562 if (Tok.isNot(tok::identifier))
Alp Tokerec543272013-12-24 09:48:30 +00003563 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003564
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003565 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00003566 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003567
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003568 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00003569
Nico Webera7c7e602012-12-31 00:28:03 +00003570 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3571 T.getOpenLocation(), ProtoIdLoc,
3572 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00003573}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003574
3575/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003576/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003577ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003578 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003579
Chris Lattner197a3012008-08-05 06:19:09 +00003580 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003581 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3582
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003583 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003584 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003585
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003586 BalancedDelimiterTracker T(*this, tok::l_paren);
3587 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003588 bool HasOptionalParen = Tok.is(tok::l_paren);
3589 if (HasOptionalParen)
3590 ConsumeParen();
3591
Douglas Gregor67c692c2010-08-26 15:07:07 +00003592 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003593 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003594 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003595 return ExprError();
3596 }
3597
Chris Lattner4f472a32009-04-11 18:13:45 +00003598 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003599 if (!SelIdent && // missing selector name.
3600 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003601 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003602
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003603 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003604
Steve Naroff152dd812007-12-05 22:21:29 +00003605 unsigned nColons = 0;
3606 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003607 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003608 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003609 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003610 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003611 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3612 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003613 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003614
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003615 if (Tok.is(tok::r_paren))
3616 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003617
3618 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003619 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003620 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003621 return ExprError();
3622 }
3623
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003624 // Check for another keyword selector.
3625 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003626 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003627 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003628 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003629 break;
3630 }
Steve Naroff152dd812007-12-05 22:21:29 +00003631 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003632 if (HasOptionalParen && Tok.is(tok::r_paren))
3633 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003634 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003635 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003636 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3637 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003638 T.getCloseLocation(),
3639 !HasOptionalParen);
Eugene Zelenko1ced5092016-02-12 22:53:10 +00003640}
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003641
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003642void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3643 // MCDecl might be null due to error in method or c-function prototype, etc.
3644 Decl *MCDecl = LM.D;
3645 bool skip = MCDecl &&
3646 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3647 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3648 if (skip)
3649 return;
3650
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003651 // Save the current token position.
3652 SourceLocation OrigLoc = Tok.getLocation();
3653
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003654 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
3655 // Append the current token at the end of the new token stream so that it
3656 // doesn't get lost.
3657 LM.Toks.push_back(Tok);
David Blaikie2eabcc92016-02-09 18:52:09 +00003658 PP.EnterTokenStream(LM.Toks, true);
3659
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003660 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003661 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003662
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003663 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3664 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003665 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003666 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003667 parseMethod
3668 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
3669 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003670
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003671 // Tell the actions module that we have entered a method or c-function definition
3672 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003673 if (parseMethod)
3674 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3675 else
3676 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003677 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003678 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003679 else {
3680 if (Tok.is(tok::colon))
3681 ParseConstructorInitializer(MCDecl);
Akira Hatanakabd59b4892016-04-18 18:19:45 +00003682 else
3683 Actions.ActOnDefaultCtorInitializers(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003684 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003685 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00003686
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003687 if (Tok.getLocation() != OrigLoc) {
3688 // Due to parsing error, we either went over the cached tokens or
3689 // there are still cached tokens left. If it's the latter case skip the
3690 // leftover tokens.
3691 // Since this is an uncommon situation that should be avoided, use the
3692 // expensive isBeforeInTranslationUnit call.
3693 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3694 OrigLoc))
3695 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3696 ConsumeAnyToken();
3697 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003698}