blob: 980ea77d84a40c2c749dbbbdd0af5c7fb80c3315 [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);
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +0000347 if (Tok.is(tok::eof))
348 return nullptr;
Douglas Gregore9d95f12015-07-07 03:57:35 +0000349 }
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000350 }
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +0000351
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000352 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000353 if (LAngleLoc.isValid()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000354 if (!ProtocolIdents.empty()) {
355 // We already parsed the protocols named when we thought we had a
356 // type parameter list. Translate them into actual protocol references.
357 for (const auto &pair : ProtocolIdents) {
358 protocolLocs.push_back(pair.second);
359 }
360 Actions.FindProtocolDeclaration(/*WarnOnDeclarations=*/true,
361 /*ForObjCContainer=*/true,
Craig Toppera9247eb2015-10-22 04:59:56 +0000362 ProtocolIdents, protocols);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000363 }
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000364 } else if (protocols.empty() && Tok.is(tok::less) &&
365 ParseObjCProtocolReferences(protocols, protocolLocs, true, true,
366 LAngleLoc, EndProtoLoc,
367 /*consumeLastToken=*/true)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000368 return nullptr;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000369 }
Mike Stump11289f42009-09-09 15:08:12 +0000370
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000371 if (Tok.isNot(tok::less))
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000372 Actions.ActOnTypedefedProtocols(protocols, superClassId, superClassLoc);
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000373
John McCall48871652010-08-21 09:40:31 +0000374 Decl *ClsType =
Douglas Gregore9d95f12015-07-07 03:57:35 +0000375 Actions.ActOnStartClassInterface(getCurScope(), AtLoc, nameId, nameLoc,
376 typeParameterList, superClassId,
377 superClassLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000378 typeArgs,
379 SourceRange(typeArgsLAngleLoc,
380 typeArgsRAngleLoc),
381 protocols.data(), protocols.size(),
382 protocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000383 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000384
Chris Lattner0ef13522007-10-09 17:51:17 +0000385 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000386 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000387
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000388 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000389
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000390 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000391}
392
Douglas Gregor813a0662015-06-19 18:14:38 +0000393/// Add an attribute for a context-sensitive type nullability to the given
394/// declarator.
395static void addContextSensitiveTypeNullability(Parser &P,
396 Declarator &D,
397 NullabilityKind nullability,
398 SourceLocation nullabilityLoc,
399 bool &addedToDeclSpec) {
400 // Create the attribute.
401 auto getNullabilityAttr = [&]() -> AttributeList * {
Douglas Gregorbec595a2015-06-19 18:27:45 +0000402 return D.getAttributePool().create(
403 P.getNullabilityKeyword(nullability),
404 SourceRange(nullabilityLoc),
405 nullptr, SourceLocation(),
406 nullptr, 0,
407 AttributeList::AS_ContextSensitiveKeyword);
Douglas Gregor813a0662015-06-19 18:14:38 +0000408 };
409
410 if (D.getNumTypeObjects() > 0) {
411 // Add the attribute to the declarator chunk nearest the declarator.
412 auto nullabilityAttr = getNullabilityAttr();
413 DeclaratorChunk &chunk = D.getTypeObject(0);
414 nullabilityAttr->setNext(chunk.getAttrListRef());
415 chunk.getAttrListRef() = nullabilityAttr;
416 } else if (!addedToDeclSpec) {
417 // Otherwise, just put it on the declaration specifiers (if one
418 // isn't there already).
419 D.getMutableDeclSpec().addAttributes(getNullabilityAttr());
420 addedToDeclSpec = true;
421 }
422}
423
Douglas Gregor85f3f952015-07-07 03:57:15 +0000424/// Parse an Objective-C type parameter list, if present, or capture
425/// the locations of the protocol identifiers for a list of protocol
426/// references.
427///
428/// objc-type-parameter-list:
429/// '<' objc-type-parameter (',' objc-type-parameter)* '>'
430///
431/// objc-type-parameter:
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000432/// objc-type-parameter-variance? identifier objc-type-parameter-bound[opt]
Douglas Gregor85f3f952015-07-07 03:57:15 +0000433///
434/// objc-type-parameter-bound:
435/// ':' type-name
436///
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000437/// objc-type-parameter-variance:
438/// '__covariant'
439/// '__contravariant'
440///
Douglas Gregor85f3f952015-07-07 03:57:15 +0000441/// \param lAngleLoc The location of the starting '<'.
442///
443/// \param protocolIdents Will capture the list of identifiers, if the
444/// angle brackets contain a list of protocol references rather than a
445/// type parameter list.
446///
447/// \param rAngleLoc The location of the ending '>'.
448ObjCTypeParamList *Parser::parseObjCTypeParamListOrProtocolRefs(
Richard Smith3df3f1d2015-11-03 01:19:56 +0000449 ObjCTypeParamListScope &Scope, SourceLocation &lAngleLoc,
450 SmallVectorImpl<IdentifierLocPair> &protocolIdents,
451 SourceLocation &rAngleLoc, bool mayBeProtocolList) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000452 assert(Tok.is(tok::less) && "Not at the beginning of a type parameter list");
453
454 // Within the type parameter list, don't treat '>' as an operator.
455 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
456
457 // Local function to "flush" the protocol identifiers, turning them into
458 // type parameters.
459 SmallVector<Decl *, 4> typeParams;
460 auto makeProtocolIdentsIntoTypeParameters = [&]() {
Douglas Gregore83b9562015-07-07 03:57:53 +0000461 unsigned index = 0;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000462 for (const auto &pair : protocolIdents) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000463 DeclResult typeParam = Actions.actOnObjCTypeParam(
David Blaikieefdccaa2016-01-15 23:43:34 +0000464 getCurScope(), ObjCTypeParamVariance::Invariant, SourceLocation(),
465 index++, pair.first, pair.second, SourceLocation(), nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000466 if (typeParam.isUsable())
467 typeParams.push_back(typeParam.get());
468 }
469
470 protocolIdents.clear();
471 mayBeProtocolList = false;
472 };
473
474 bool invalid = false;
475 lAngleLoc = ConsumeToken();
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000476
Douglas Gregor85f3f952015-07-07 03:57:15 +0000477 do {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000478 // Parse the variance, if any.
479 SourceLocation varianceLoc;
480 ObjCTypeParamVariance variance = ObjCTypeParamVariance::Invariant;
481 if (Tok.is(tok::kw___covariant) || Tok.is(tok::kw___contravariant)) {
482 variance = Tok.is(tok::kw___covariant)
483 ? ObjCTypeParamVariance::Covariant
484 : ObjCTypeParamVariance::Contravariant;
485 varianceLoc = ConsumeToken();
486
487 // Once we've seen a variance specific , we know this is not a
488 // list of protocol references.
489 if (mayBeProtocolList) {
490 // Up until now, we have been queuing up parameters because they
491 // might be protocol references. Turn them into parameters now.
492 makeProtocolIdentsIntoTypeParameters();
493 }
494 }
495
Douglas Gregor85f3f952015-07-07 03:57:15 +0000496 // Parse the identifier.
497 if (!Tok.is(tok::identifier)) {
498 // Code completion.
499 if (Tok.is(tok::code_completion)) {
500 // FIXME: If these aren't protocol references, we'll need different
501 // completions.
Craig Topper883dd332015-12-24 23:58:11 +0000502 Actions.CodeCompleteObjCProtocolReferences(protocolIdents);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000503 cutOffParsing();
504
505 // FIXME: Better recovery here?.
506 return nullptr;
507 }
508
509 Diag(Tok, diag::err_objc_expected_type_parameter);
510 invalid = true;
511 break;
512 }
513
514 IdentifierInfo *paramName = Tok.getIdentifierInfo();
515 SourceLocation paramLoc = ConsumeToken();
516
517 // If there is a bound, parse it.
518 SourceLocation colonLoc;
519 TypeResult boundType;
520 if (TryConsumeToken(tok::colon, colonLoc)) {
521 // Once we've seen a bound, we know this is not a list of protocol
522 // references.
523 if (mayBeProtocolList) {
524 // Up until now, we have been queuing up parameters because they
525 // might be protocol references. Turn them into parameters now.
526 makeProtocolIdentsIntoTypeParameters();
527 }
528
529 // type-name
530 boundType = ParseTypeName();
531 if (boundType.isInvalid())
532 invalid = true;
533 } else if (mayBeProtocolList) {
534 // If this could still be a protocol list, just capture the identifier.
535 // We don't want to turn it into a parameter.
536 protocolIdents.push_back(std::make_pair(paramName, paramLoc));
537 continue;
538 }
539
540 // Create the type parameter.
David Blaikieefdccaa2016-01-15 23:43:34 +0000541 DeclResult typeParam = Actions.actOnObjCTypeParam(
542 getCurScope(), variance, varianceLoc, typeParams.size(), paramName,
543 paramLoc, colonLoc, boundType.isUsable() ? boundType.get() : nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000544 if (typeParam.isUsable())
545 typeParams.push_back(typeParam.get());
546 } while (TryConsumeToken(tok::comma));
547
548 // Parse the '>'.
549 if (invalid) {
550 SkipUntil(tok::greater, tok::at, StopBeforeMatch);
551 if (Tok.is(tok::greater))
552 ConsumeToken();
553 } else if (ParseGreaterThanInTemplateList(rAngleLoc,
554 /*ConsumeLastToken=*/true,
555 /*ObjCGenericList=*/true)) {
556 Diag(lAngleLoc, diag::note_matching) << "'<'";
557 SkipUntil({tok::greater, tok::greaterequal, tok::at, tok::minus,
558 tok::minus, tok::plus, tok::colon, tok::l_paren, tok::l_brace,
559 tok::comma, tok::semi },
560 StopBeforeMatch);
561 if (Tok.is(tok::greater))
562 ConsumeToken();
563 }
564
565 if (mayBeProtocolList) {
566 // A type parameter list must be followed by either a ':' (indicating the
567 // presence of a superclass) or a '(' (indicating that this is a category
568 // or extension). This disambiguates between an objc-type-parameter-list
569 // and a objc-protocol-refs.
570 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_paren)) {
571 // Returning null indicates that we don't have a type parameter list.
572 // The results the caller needs to handle the protocol references are
573 // captured in the reference parameters already.
574 return nullptr;
575 }
576
577 // We have a type parameter list that looks like a list of protocol
578 // references. Turn that parameter list into type parameters.
579 makeProtocolIdentsIntoTypeParameters();
580 }
581
Richard Smith3df3f1d2015-11-03 01:19:56 +0000582 // Form the type parameter list and enter its scope.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000583 ObjCTypeParamList *list = Actions.actOnObjCTypeParamList(
584 getCurScope(),
585 lAngleLoc,
586 typeParams,
587 rAngleLoc);
Richard Smith3df3f1d2015-11-03 01:19:56 +0000588 Scope.enter(list);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000589
590 // Clear out the angle locations; they're used by the caller to indicate
591 // whether there are any protocol references.
592 lAngleLoc = SourceLocation();
593 rAngleLoc = SourceLocation();
Akira Hatanaka8ebd5802015-12-16 06:25:38 +0000594 return invalid ? nullptr : list;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000595}
596
597/// Parse an objc-type-parameter-list.
598ObjCTypeParamList *Parser::parseObjCTypeParamList() {
599 SourceLocation lAngleLoc;
600 SmallVector<IdentifierLocPair, 1> protocolIdents;
601 SourceLocation rAngleLoc;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000602
603 ObjCTypeParamListScope Scope(Actions, getCurScope());
604 return parseObjCTypeParamListOrProtocolRefs(Scope, lAngleLoc, protocolIdents,
605 rAngleLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000606 /*mayBeProtocolList=*/false);
607}
608
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000609/// objc-interface-decl-list:
610/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000611/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000612/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000613/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000614/// objc-interface-decl-list declaration
615/// objc-interface-decl-list ';'
616///
Steve Naroff99264b42007-08-22 16:35:03 +0000617/// objc-method-requirement: [OBJC2]
618/// @required
619/// @optional
620///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000621void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
622 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000623 SmallVector<Decl *, 32> allMethods;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000624 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000625 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000626
Ted Kremenekc7c64312010-01-07 01:20:12 +0000627 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000628
Steve Naroff99264b42007-08-22 16:35:03 +0000629 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000630 // If this is a method prototype, parse it.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000631 if (Tok.isOneOf(tok::minus, tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000632 if (Decl *methodPrototype =
633 ParseObjCMethodPrototype(MethodImplKind, false))
634 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000635 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
636 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000637 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
638 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000639 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000640 if (Tok.is(tok::semi))
641 ConsumeToken();
642 }
Steve Naroff99264b42007-08-22 16:35:03 +0000643 continue;
644 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000645 if (Tok.is(tok::l_paren)) {
646 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000647 ParseObjCMethodDecl(Tok.getLocation(),
648 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000649 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000650 continue;
651 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000652 // Ignore excess semicolons.
653 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000654 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000655 continue;
656 }
Mike Stump11289f42009-09-09 15:08:12 +0000657
Chris Lattnerda9fb152008-10-20 06:10:06 +0000658 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000659 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000660 break;
Mike Stump11289f42009-09-09 15:08:12 +0000661
Douglas Gregorf1934162010-01-13 21:24:21 +0000662 // Code completion within an Objective-C interface.
663 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000664 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000665 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000666 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000667 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000668 }
669
Chris Lattner038a3e32008-10-20 05:46:22 +0000670 // If we don't have an @ directive, parse it as a function definition.
671 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000672 // The code below does not consume '}'s because it is afraid of eating the
673 // end of a namespace. Because of the way this code is structured, an
674 // erroneous r_brace would cause an infinite loop if not handled here.
675 if (Tok.is(tok::r_brace))
676 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000677 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000678 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000679 continue;
680 }
Mike Stump11289f42009-09-09 15:08:12 +0000681
Chris Lattner038a3e32008-10-20 05:46:22 +0000682 // Otherwise, we have an @ directive, eat the @.
683 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000684 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000685 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000686 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000687 }
688
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000689 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000690
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000691 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000692 AtEnd.setBegin(AtLoc);
693 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000694 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000695 } else if (DirectiveKind == tok::objc_not_keyword) {
696 Diag(Tok, diag::err_objc_unknown_at);
697 SkipUntil(tok::semi);
698 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000699 }
Mike Stump11289f42009-09-09 15:08:12 +0000700
Chris Lattnerda9fb152008-10-20 06:10:06 +0000701 // Eat the identifier.
702 ConsumeToken();
703
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000704 switch (DirectiveKind) {
705 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000706 // FIXME: If someone forgets an @end on a protocol, this loop will
707 // continue to eat up tons of stuff and spew lots of nonsense errors. It
708 // would probably be better to bail out if we saw an @class or @interface
709 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000710 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000711 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000712 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000713 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000714
715 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000716 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000717 Diag(AtLoc, diag::err_objc_missing_end)
718 << FixItHint::CreateInsertion(AtLoc, "@end\n");
719 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
720 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000721 ConsumeToken();
722 break;
723
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000724 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000725 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000726 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000727 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000728 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000729 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000730 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000731 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000732 break;
Mike Stump11289f42009-09-09 15:08:12 +0000733
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000734 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000735 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000736 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000737
Chris Lattner038a3e32008-10-20 05:46:22 +0000738 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000739 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000740 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000741 if (Tok.is(tok::l_paren)) {
742 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000743 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000744 }
Mike Stump11289f42009-09-09 15:08:12 +0000745
Douglas Gregor813a0662015-06-19 18:14:38 +0000746 bool addedToDeclSpec = false;
Benjamin Kramera39beb92014-09-03 11:06:10 +0000747 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
748 if (FD.D.getIdentifier() == nullptr) {
749 Diag(AtLoc, diag::err_objc_property_requires_field_name)
750 << FD.D.getSourceRange();
751 return;
752 }
753 if (FD.BitfieldSize) {
754 Diag(AtLoc, diag::err_objc_property_bitfield)
755 << FD.D.getSourceRange();
756 return;
757 }
758
Douglas Gregor813a0662015-06-19 18:14:38 +0000759 // Map a nullability property attribute to a context-sensitive keyword
760 // attribute.
761 if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
762 addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
763 OCDS.getNullabilityLoc(),
764 addedToDeclSpec);
765
Benjamin Kramera39beb92014-09-03 11:06:10 +0000766 // Install the property declarator into interfaceDecl.
767 IdentifierInfo *SelName =
768 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
769
770 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
771 IdentifierInfo *SetterName = OCDS.getSetterName();
772 Selector SetterSel;
773 if (SetterName)
774 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
775 else
776 SetterSel = SelectorTable::constructSetterSelector(
777 PP.getIdentifierTable(), PP.getSelectorTable(),
778 FD.D.getIdentifier());
Benjamin Kramera39beb92014-09-03 11:06:10 +0000779 Decl *Property = Actions.ActOnProperty(
780 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000781 MethodImplKind);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000782
783 FD.complete(Property);
784 };
John McCallcfefb6d2009-11-03 02:38:08 +0000785
Chris Lattner038a3e32008-10-20 05:46:22 +0000786 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000787 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000788 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000789
John McCall405988b2011-03-26 01:53:26 +0000790 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000791 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000792 }
Steve Naroff99264b42007-08-22 16:35:03 +0000793 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000794
795 // We break out of the big loop in two cases: when we see @end or when we see
796 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000797 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000798 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000799 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000800 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000801 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000802 } else {
803 Diag(Tok, diag::err_objc_missing_end)
804 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
805 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
806 << (int) Actions.getObjCContainerKind();
807 AtEnd.setBegin(Tok.getLocation());
808 AtEnd.setEnd(Tok.getLocation());
809 }
Mike Stump11289f42009-09-09 15:08:12 +0000810
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000811 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000812 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000813 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000814}
815
Douglas Gregor813a0662015-06-19 18:14:38 +0000816/// Diagnose redundant or conflicting nullability information.
817static void diagnoseRedundantPropertyNullability(Parser &P,
818 ObjCDeclSpec &DS,
819 NullabilityKind nullability,
820 SourceLocation nullabilityLoc){
821 if (DS.getNullability() == nullability) {
822 P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000823 << DiagNullabilityKind(nullability, true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000824 << SourceRange(DS.getNullabilityLoc());
825 return;
826 }
827
828 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000829 << DiagNullabilityKind(nullability, true)
830 << DiagNullabilityKind(DS.getNullability(), true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000831 << SourceRange(DS.getNullabilityLoc());
832}
833
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000834/// Parse property attribute declarations.
835///
836/// property-attr-decl: '(' property-attrlist ')'
837/// property-attrlist:
838/// property-attribute
839/// property-attrlist ',' property-attribute
840/// property-attribute:
841/// getter '=' identifier
842/// setter '=' identifier ':'
843/// readonly
844/// readwrite
845/// assign
846/// retain
847/// copy
848/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000849/// atomic
850/// strong
851/// weak
852/// unsafe_unretained
Douglas Gregor813a0662015-06-19 18:14:38 +0000853/// nonnull
854/// nullable
855/// null_unspecified
Douglas Gregor849ebc22015-06-19 18:14:46 +0000856/// null_resettable
Manman Ren387ff7f2016-01-26 18:52:43 +0000857/// class
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000858///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000859void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000860 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000861 BalancedDelimiterTracker T(*this, tok::l_paren);
862 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000863
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000864 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000865 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000866 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000867 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000868 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000869 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000870
Chris Lattner76619232008-10-20 07:22:18 +0000871 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000872 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000873 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000874 return;
875 }
Mike Stump11289f42009-09-09 15:08:12 +0000876
Chris Lattner1db33542008-10-20 07:37:22 +0000877 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000878
Chris Lattner68e48682008-11-20 04:42:34 +0000879 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000880 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000881 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000882 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000883 else if (II->isStr("unsafe_unretained"))
884 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000885 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000886 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000887 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000888 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000889 else if (II->isStr("strong"))
890 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000891 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000892 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000893 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000894 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000895 else if (II->isStr("atomic"))
896 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000897 else if (II->isStr("weak"))
898 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000899 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000900 bool IsSetter = II->getNameStart()[0] == 's';
901
Chris Lattner825bca12008-10-20 07:39:53 +0000902 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000903 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
Craig Topper3110a5c2015-11-14 18:16:02 +0000904 diag::err_objc_expected_equal_for_getter;
Anders Carlssonfe15a782010-10-02 17:45:21 +0000905
Alp Toker383d2c42014-01-01 03:08:43 +0000906 if (ExpectAndConsume(tok::equal, DiagID)) {
907 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000908 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000909 }
Mike Stump11289f42009-09-09 15:08:12 +0000910
Douglas Gregorc8537c52009-11-19 07:41:15 +0000911 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000912 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000913 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000914 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000915 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000916 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000917 }
918
Anders Carlssonfe15a782010-10-02 17:45:21 +0000919 SourceLocation SelLoc;
920 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
921
922 if (!SelIdent) {
923 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
924 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000925 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000926 return;
927 }
Mike Stump11289f42009-09-09 15:08:12 +0000928
Anders Carlssonfe15a782010-10-02 17:45:21 +0000929 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000930 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000931 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000932
Alp Toker383d2c42014-01-01 03:08:43 +0000933 if (ExpectAndConsume(tok::colon,
934 diag::err_expected_colon_after_setter_name)) {
935 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000936 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000937 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000938 } else {
939 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000940 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000941 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000942 } else if (II->isStr("nonnull")) {
943 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
944 diagnoseRedundantPropertyNullability(*this, DS,
945 NullabilityKind::NonNull,
946 Tok.getLocation());
947 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
948 DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
949 } else if (II->isStr("nullable")) {
950 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
951 diagnoseRedundantPropertyNullability(*this, DS,
952 NullabilityKind::Nullable,
953 Tok.getLocation());
954 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
955 DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
956 } else if (II->isStr("null_unspecified")) {
957 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
958 diagnoseRedundantPropertyNullability(*this, DS,
959 NullabilityKind::Unspecified,
960 Tok.getLocation());
961 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
962 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000963 } else if (II->isStr("null_resettable")) {
964 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
965 diagnoseRedundantPropertyNullability(*this, DS,
966 NullabilityKind::Unspecified,
967 Tok.getLocation());
968 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
969 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
970
971 // Also set the null_resettable bit.
972 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
Manman Ren387ff7f2016-01-26 18:52:43 +0000973 } else if (II->isStr("class")) {
974 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_class);
Chris Lattner825bca12008-10-20 07:39:53 +0000975 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000976 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000977 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000978 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000979 }
Mike Stump11289f42009-09-09 15:08:12 +0000980
Chris Lattner1db33542008-10-20 07:37:22 +0000981 if (Tok.isNot(tok::comma))
982 break;
Mike Stump11289f42009-09-09 15:08:12 +0000983
Chris Lattner1db33542008-10-20 07:37:22 +0000984 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000985 }
Mike Stump11289f42009-09-09 15:08:12 +0000986
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000987 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000988}
989
Steve Naroff09bf8152007-09-06 21:24:23 +0000990/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000991/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000992/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000993///
994/// objc-instance-method: '-'
995/// objc-class-method: '+'
996///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000997/// objc-method-attributes: [OBJC2]
998/// __attribute__((deprecated))
999///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001000Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001001 bool MethodDefinition) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001002 assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +00001003
Mike Stump11289f42009-09-09 15:08:12 +00001004 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +00001005 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001006 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001007 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +00001008 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +00001009 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +00001010 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +00001011}
1012
1013/// objc-selector:
1014/// identifier
1015/// one of
1016/// enum struct union if else while do for switch case default
1017/// break continue return goto asm sizeof typeof __alignof
1018/// unsigned long const short volatile signed restrict _Complex
1019/// in out inout bycopy byref oneway int char float double void _Bool
1020///
Chris Lattner4f472a32009-04-11 18:13:45 +00001021IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +00001022
Chris Lattner5700fab2007-10-07 02:00:24 +00001023 switch (Tok.getKind()) {
1024 default:
Craig Topper161e4db2014-05-21 06:02:52 +00001025 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001026 case tok::ampamp:
1027 case tok::ampequal:
1028 case tok::amp:
1029 case tok::pipe:
1030 case tok::tilde:
1031 case tok::exclaim:
1032 case tok::exclaimequal:
1033 case tok::pipepipe:
1034 case tok::pipeequal:
1035 case tok::caret:
1036 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +00001037 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +00001038 if (isLetter(ThisTok[0])) {
Malcolm Parsons731ca0e2016-11-03 12:25:51 +00001039 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok);
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001040 Tok.setKind(tok::identifier);
1041 SelectorLoc = ConsumeToken();
1042 return II;
1043 }
Craig Topper161e4db2014-05-21 06:02:52 +00001044 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001045 }
1046
Chris Lattner5700fab2007-10-07 02:00:24 +00001047 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001048 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +00001049 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001050 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001051 case tok::kw_break:
1052 case tok::kw_case:
1053 case tok::kw_catch:
1054 case tok::kw_char:
1055 case tok::kw_class:
1056 case tok::kw_const:
1057 case tok::kw_const_cast:
1058 case tok::kw_continue:
1059 case tok::kw_default:
1060 case tok::kw_delete:
1061 case tok::kw_do:
1062 case tok::kw_double:
1063 case tok::kw_dynamic_cast:
1064 case tok::kw_else:
1065 case tok::kw_enum:
1066 case tok::kw_explicit:
1067 case tok::kw_export:
1068 case tok::kw_extern:
1069 case tok::kw_false:
1070 case tok::kw_float:
1071 case tok::kw_for:
1072 case tok::kw_friend:
1073 case tok::kw_goto:
1074 case tok::kw_if:
1075 case tok::kw_inline:
1076 case tok::kw_int:
1077 case tok::kw_long:
1078 case tok::kw_mutable:
1079 case tok::kw_namespace:
1080 case tok::kw_new:
1081 case tok::kw_operator:
1082 case tok::kw_private:
1083 case tok::kw_protected:
1084 case tok::kw_public:
1085 case tok::kw_register:
1086 case tok::kw_reinterpret_cast:
1087 case tok::kw_restrict:
1088 case tok::kw_return:
1089 case tok::kw_short:
1090 case tok::kw_signed:
1091 case tok::kw_sizeof:
1092 case tok::kw_static:
1093 case tok::kw_static_cast:
1094 case tok::kw_struct:
1095 case tok::kw_switch:
1096 case tok::kw_template:
1097 case tok::kw_this:
1098 case tok::kw_throw:
1099 case tok::kw_true:
1100 case tok::kw_try:
1101 case tok::kw_typedef:
1102 case tok::kw_typeid:
1103 case tok::kw_typename:
1104 case tok::kw_typeof:
1105 case tok::kw_union:
1106 case tok::kw_unsigned:
1107 case tok::kw_using:
1108 case tok::kw_virtual:
1109 case tok::kw_void:
1110 case tok::kw_volatile:
1111 case tok::kw_wchar_t:
1112 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +00001113 case tok::kw__Bool:
1114 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001115 case tok::kw___alignof:
Richard Smithe301ba22015-11-11 02:02:15 +00001116 case tok::kw___auto_type:
Chris Lattner5700fab2007-10-07 02:00:24 +00001117 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001118 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +00001119 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +00001120 }
Steve Naroff99264b42007-08-22 16:35:03 +00001121}
1122
Fariborz Jahanian83615522008-01-02 22:54:34 +00001123/// objc-for-collection-in: 'in'
1124///
Fariborz Jahanian3622e592008-01-04 23:04:08 +00001125bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001126 // FIXME: May have to do additional look-ahead to only allow for
1127 // valid tokens following an 'in'; such as an identifier, unary operators,
1128 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001129 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +00001130 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +00001131}
1132
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001133/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +00001134/// qualifier list and builds their bitmask representation in the input
1135/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +00001136///
1137/// objc-type-qualifiers:
1138/// objc-type-qualifier
1139/// objc-type-qualifiers objc-type-qualifier
1140///
Douglas Gregor813a0662015-06-19 18:14:38 +00001141/// objc-type-qualifier:
1142/// 'in'
1143/// 'out'
1144/// 'inout'
1145/// 'oneway'
1146/// 'bycopy'
1147/// 'byref'
1148/// 'nonnull'
1149/// 'nullable'
1150/// 'null_unspecified'
1151///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001152void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001153 Declarator::TheContext Context) {
1154 assert(Context == Declarator::ObjCParameterContext ||
1155 Context == Declarator::ObjCResultContext);
1156
Chris Lattner61511e12007-12-12 06:56:32 +00001157 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +00001158 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +00001159 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +00001160 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001161 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +00001162 }
1163
Chris Lattner5e530bc2007-12-27 19:57:00 +00001164 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +00001165 return;
Mike Stump11289f42009-09-09 15:08:12 +00001166
Chris Lattner61511e12007-12-12 06:56:32 +00001167 const IdentifierInfo *II = Tok.getIdentifierInfo();
1168 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001169 if (II != ObjCTypeQuals[i] ||
1170 NextToken().is(tok::less) ||
1171 NextToken().is(tok::coloncolon))
Chris Lattner61511e12007-12-12 06:56:32 +00001172 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001173
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001174 ObjCDeclSpec::ObjCDeclQualifier Qual;
Douglas Gregor813a0662015-06-19 18:14:38 +00001175 NullabilityKind Nullability;
Chris Lattner61511e12007-12-12 06:56:32 +00001176 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +00001177 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001178 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
1179 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
1180 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
1181 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
1182 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
1183 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Douglas Gregor813a0662015-06-19 18:14:38 +00001184
1185 case objc_nonnull:
1186 Qual = ObjCDeclSpec::DQ_CSNullability;
1187 Nullability = NullabilityKind::NonNull;
1188 break;
1189
1190 case objc_nullable:
1191 Qual = ObjCDeclSpec::DQ_CSNullability;
1192 Nullability = NullabilityKind::Nullable;
1193 break;
1194
1195 case objc_null_unspecified:
1196 Qual = ObjCDeclSpec::DQ_CSNullability;
1197 Nullability = NullabilityKind::Unspecified;
1198 break;
Chris Lattner61511e12007-12-12 06:56:32 +00001199 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001200
1201 // FIXME: Diagnose redundant specifiers.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001202 DS.setObjCDeclQualifier(Qual);
Douglas Gregor813a0662015-06-19 18:14:38 +00001203 if (Qual == ObjCDeclSpec::DQ_CSNullability)
1204 DS.setNullability(Tok.getLocation(), Nullability);
1205
Chris Lattner61511e12007-12-12 06:56:32 +00001206 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001207 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +00001208 break;
1209 }
Mike Stump11289f42009-09-09 15:08:12 +00001210
Chris Lattner61511e12007-12-12 06:56:32 +00001211 // If this wasn't a recognized qualifier, bail out.
1212 if (II) return;
1213 }
1214}
1215
John McCalla55902b2011-10-01 09:56:14 +00001216/// Take all the decl attributes out of the given list and add
1217/// them to the given attribute set.
1218static void takeDeclAttributes(ParsedAttributes &attrs,
1219 AttributeList *list) {
1220 while (list) {
1221 AttributeList *cur = list;
1222 list = cur->getNext();
1223
1224 if (!cur->isUsedAsTypeAttr()) {
1225 // Clear out the next pointer. We're really completely
1226 // destroying the internal invariants of the declarator here,
1227 // but it doesn't matter because we're done with it.
Craig Topper161e4db2014-05-21 06:02:52 +00001228 cur->setNext(nullptr);
John McCalla55902b2011-10-01 09:56:14 +00001229 attrs.add(cur);
1230 }
1231 }
1232}
1233
1234/// takeDeclAttributes - Take all the decl attributes from the given
1235/// declarator and add them to the given list.
1236static void takeDeclAttributes(ParsedAttributes &attrs,
1237 Declarator &D) {
1238 // First, take ownership of all attributes.
1239 attrs.getPool().takeAllFrom(D.getAttributePool());
1240 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
1241
1242 // Now actually move the attributes over.
1243 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
1244 takeDeclAttributes(attrs, D.getAttributes());
1245 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
1246 takeDeclAttributes(attrs,
1247 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
1248}
1249
Chris Lattner61511e12007-12-12 06:56:32 +00001250/// objc-type-name:
1251/// '(' objc-type-qualifiers[opt] type-name ')'
1252/// '(' objc-type-qualifiers[opt] ')'
1253///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001254ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001255 Declarator::TheContext context,
1256 ParsedAttributes *paramAttrs) {
1257 assert(context == Declarator::ObjCParameterContext ||
1258 context == Declarator::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +00001259 assert((paramAttrs != nullptr) ==
1260 (context == Declarator::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +00001261
Chris Lattner0ef13522007-10-09 17:51:17 +00001262 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +00001263
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001264 BalancedDelimiterTracker T(*this, tok::l_paren);
1265 T.consumeOpen();
1266
Chris Lattner2ebb1782008-08-23 01:48:03 +00001267 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001268 ObjCDeclContextSwitch ObjCDC(*this);
1269
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001270 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001271 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +00001272
John McCallba7bf592010-08-24 05:47:05 +00001273 ParsedType Ty;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001274 if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
John McCalla55902b2011-10-01 09:56:14 +00001275 // Parse an abstract declarator.
1276 DeclSpec declSpec(AttrFactory);
1277 declSpec.setObjCQualifiers(&DS);
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001278 DeclSpecContext dsContext = DSC_normal;
1279 if (context == Declarator::ObjCResultContext)
1280 dsContext = DSC_objc_method_result;
1281 ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
John McCalla55902b2011-10-01 09:56:14 +00001282 Declarator declarator(declSpec, context);
1283 ParseDeclarator(declarator);
1284
1285 // If that's not invalid, extract a type.
1286 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001287 // Map a nullability specifier to a context-sensitive keyword attribute.
1288 bool addedToDeclSpec = false;
1289 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1290 addContextSensitiveTypeNullability(*this, declarator,
1291 DS.getNullability(),
1292 DS.getNullabilityLoc(),
1293 addedToDeclSpec);
1294
John McCalla55902b2011-10-01 09:56:14 +00001295 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1296 if (!type.isInvalid())
1297 Ty = type.get();
1298
1299 // If we're parsing a parameter, steal all the decl attributes
1300 // and add them to the decl spec.
1301 if (context == Declarator::ObjCParameterContext)
1302 takeDeclAttributes(*paramAttrs, declarator);
1303 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001304 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001305
Steve Naroff90255b42008-10-21 14:15:04 +00001306 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001307 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001308 else if (Tok.getLocation() == TypeStartLoc) {
1309 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001310 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001311 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001312 } else {
1313 // Otherwise, we found *something*, but didn't get a ')' in the right
1314 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001315 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001316 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001317 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001318}
1319
1320/// objc-method-decl:
1321/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001322/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001323/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001324/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001325///
1326/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001327/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001328/// objc-keyword-selector objc-keyword-decl
1329///
1330/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001331/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1332/// objc-selector ':' objc-keyword-attributes[opt] identifier
1333/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1334/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001335///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001336/// objc-parmlist:
1337/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001338///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001339/// objc-parms:
1340/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001341///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001342/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001343/// , ...
1344///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001345/// objc-keyword-attributes: [OBJC2]
1346/// __attribute__((unused))
1347///
John McCall48871652010-08-21 09:40:31 +00001348Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001349 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001350 tok::ObjCKeywordKind MethodImplKind,
1351 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001352 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001353
Douglas Gregor636a61e2010-04-07 00:21:17 +00001354 if (Tok.is(tok::code_completion)) {
David Blaikieefdccaa2016-01-15 23:43:34 +00001355 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
1356 /*ReturnType=*/nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001357 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001358 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001359 }
1360
Chris Lattner2ebb1782008-08-23 01:48:03 +00001361 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001362 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001363 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001364 if (Tok.is(tok::l_paren))
Craig Topper161e4db2014-05-21 06:02:52 +00001365 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext,
1366 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001367
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001368 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001369 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001370 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001371 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001372
Douglas Gregor636a61e2010-04-07 00:21:17 +00001373 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001374 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001375 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001376 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001377 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001378 }
1379
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001380 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001381 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001382 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001383
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001384 // An unnamed colon is valid.
1385 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001386 Diag(Tok, diag::err_expected_selector_for_method)
1387 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001388 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001389 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001390 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001391 }
Mike Stump11289f42009-09-09 15:08:12 +00001392
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001393 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001394 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001395 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001396 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001397 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001398
Chris Lattner5700fab2007-10-07 02:00:24 +00001399 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001400 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001401 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001402 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001403 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001404 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001405 methodAttrs.getList(), MethodImplKind,
1406 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001407 PD.complete(Result);
1408 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001409 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001410
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001411 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001412 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001413 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001414 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1415 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001416
1417 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001418 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001419 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001420 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001421
Chris Lattner5700fab2007-10-07 02:00:24 +00001422 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001423 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001424 break;
Mike Stump11289f42009-09-09 15:08:12 +00001425
David Blaikieefdccaa2016-01-15 23:43:34 +00001426 ArgInfo.Type = nullptr;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001427 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001428 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1429 Declarator::ObjCParameterContext,
1430 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001431
Chris Lattner5700fab2007-10-07 02:00:24 +00001432 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001433 // Regardless, collect all the attributes we've parsed so far.
Craig Topper161e4db2014-05-21 06:02:52 +00001434 ArgInfo.ArgAttrs = nullptr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001435 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001436 MaybeParseGNUAttributes(paramAttrs);
1437 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001438 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001439
Douglas Gregor45879692010-07-08 23:37:41 +00001440 // Code completion for the next piece of the selector.
1441 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001442 KeyIdents.push_back(SelIdent);
1443 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1444 mType == tok::minus,
1445 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001446 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001447 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001448 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001449 }
1450
Chris Lattner0ef13522007-10-09 17:51:17 +00001451 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001452 Diag(Tok, diag::err_expected)
1453 << tok::identifier; // missing argument name.
Chris Lattner5700fab2007-10-07 02:00:24 +00001454 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001455 }
Mike Stump11289f42009-09-09 15:08:12 +00001456
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001457 ArgInfo.Name = Tok.getIdentifierInfo();
1458 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001459 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001460
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001461 ArgInfos.push_back(ArgInfo);
1462 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001463 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001464
John McCall084e83d2011-03-24 11:26:52 +00001465 // Make sure the attributes persist.
1466 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1467
Douglas Gregor95887f92010-07-08 23:20:03 +00001468 // Code completion for the next piece of the selector.
1469 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001470 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1471 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001472 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001473 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001474 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001475 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001476 }
1477
Chris Lattner5700fab2007-10-07 02:00:24 +00001478 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001479 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001480 if (!SelIdent && Tok.isNot(tok::colon))
1481 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001482 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001483 SourceLocation ColonLoc = Tok.getLocation();
1484 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001485 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1486 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1487 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001488 }
1489 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001490 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001491 }
Mike Stump11289f42009-09-09 15:08:12 +00001492
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001493 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001494 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001495 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001496 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001497 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001498 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001499 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001500 ConsumeToken();
1501 break;
1502 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001503 if (!cStyleParamWarned) {
1504 Diag(Tok, diag::warn_cstyle_param);
1505 cStyleParamWarned = true;
1506 }
John McCall084e83d2011-03-24 11:26:52 +00001507 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001508 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001509 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001510 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1511 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001512 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001513 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001514 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1515 ParmDecl.getIdentifierLoc(),
1516 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001517 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001518 }
Mike Stump11289f42009-09-09 15:08:12 +00001519
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001520 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001521 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001522 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001523 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001524
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001525 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001526 return nullptr;
1527
Chris Lattner5700fab2007-10-07 02:00:24 +00001528 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1529 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001530 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001531 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001532 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001533 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001534 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001535 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001536 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001537
John McCall28a6aea2009-11-04 02:18:39 +00001538 PD.complete(Result);
1539 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001540}
1541
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001542/// objc-protocol-refs:
1543/// '<' identifier-list '>'
1544///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001545bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001546ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1547 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001548 bool WarnOnDeclarations, bool ForObjCContainer,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001549 SourceLocation &LAngleLoc, SourceLocation &EndLoc,
1550 bool consumeLastToken) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001551 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001552
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001553 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001554
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001555 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001556
Chris Lattner3bbae002008-07-26 04:03:38 +00001557 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001558 if (Tok.is(tok::code_completion)) {
Craig Topper883dd332015-12-24 23:58:11 +00001559 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001560 cutOffParsing();
1561 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001562 }
1563
Chris Lattner3bbae002008-07-26 04:03:38 +00001564 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001565 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001566 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001567 return true;
1568 }
1569 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1570 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001571 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001572 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001573
Alp Toker383d2c42014-01-01 03:08:43 +00001574 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001575 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001576 }
Mike Stump11289f42009-09-09 15:08:12 +00001577
Chris Lattner3bbae002008-07-26 04:03:38 +00001578 // Consume the '>'.
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001579 if (ParseGreaterThanInTemplateList(EndLoc, consumeLastToken,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001580 /*ObjCGenericList=*/false))
Chris Lattner3bbae002008-07-26 04:03:38 +00001581 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001582
Chris Lattner3bbae002008-07-26 04:03:38 +00001583 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001584 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Craig Toppera9247eb2015-10-22 04:59:56 +00001585 ProtocolIdents, Protocols);
Chris Lattner3bbae002008-07-26 04:03:38 +00001586 return false;
1587}
1588
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001589TypeResult Parser::parseObjCProtocolQualifierType(SourceLocation &rAngleLoc) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001590 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001591 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001592
1593 SourceLocation lAngleLoc;
1594 SmallVector<Decl *, 8> protocols;
1595 SmallVector<SourceLocation, 8> protocolLocs;
1596 (void)ParseObjCProtocolReferences(protocols, protocolLocs, false, false,
1597 lAngleLoc, rAngleLoc,
1598 /*consumeLastToken=*/true);
1599 TypeResult result = Actions.actOnObjCProtocolQualifierType(lAngleLoc,
1600 protocols,
1601 protocolLocs,
1602 rAngleLoc);
1603 if (result.isUsable()) {
1604 Diag(lAngleLoc, diag::warn_objc_protocol_qualifier_missing_id)
1605 << FixItHint::CreateInsertion(lAngleLoc, "id")
1606 << SourceRange(lAngleLoc, rAngleLoc);
1607 }
1608
1609 return result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001610}
1611
Douglas Gregore9d95f12015-07-07 03:57:35 +00001612/// Parse Objective-C type arguments or protocol qualifiers.
1613///
1614/// objc-type-arguments:
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001615/// '<' type-name '...'[opt] (',' type-name '...'[opt])* '>'
Douglas Gregore9d95f12015-07-07 03:57:35 +00001616///
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001617void Parser::parseObjCTypeArgsOrProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001618 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001619 SourceLocation &typeArgsLAngleLoc,
1620 SmallVectorImpl<ParsedType> &typeArgs,
1621 SourceLocation &typeArgsRAngleLoc,
1622 SourceLocation &protocolLAngleLoc,
1623 SmallVectorImpl<Decl *> &protocols,
1624 SmallVectorImpl<SourceLocation> &protocolLocs,
1625 SourceLocation &protocolRAngleLoc,
1626 bool consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001627 bool warnOnIncompleteProtocols) {
1628 assert(Tok.is(tok::less) && "Not at the start of type args or protocols");
1629 SourceLocation lAngleLoc = ConsumeToken();
1630
1631 // Whether all of the elements we've parsed thus far are single
1632 // identifiers, which might be types or might be protocols.
1633 bool allSingleIdentifiers = true;
1634 SmallVector<IdentifierInfo *, 4> identifiers;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001635 SmallVectorImpl<SourceLocation> &identifierLocs = protocolLocs;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001636
1637 // Parse a list of comma-separated identifiers, bailing out if we
1638 // see something different.
1639 do {
1640 // Parse a single identifier.
1641 if (Tok.is(tok::identifier) &&
1642 (NextToken().is(tok::comma) ||
1643 NextToken().is(tok::greater) ||
1644 NextToken().is(tok::greatergreater))) {
1645 identifiers.push_back(Tok.getIdentifierInfo());
1646 identifierLocs.push_back(ConsumeToken());
1647 continue;
1648 }
1649
1650 if (Tok.is(tok::code_completion)) {
1651 // FIXME: Also include types here.
1652 SmallVector<IdentifierLocPair, 4> identifierLocPairs;
1653 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1654 identifierLocPairs.push_back(IdentifierLocPair(identifiers[i],
1655 identifierLocs[i]));
1656 }
1657
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001658 QualType BaseT = Actions.GetTypeFromParser(baseType);
1659 if (!BaseT.isNull() && BaseT->acceptsObjCTypeParams()) {
1660 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
1661 } else {
Craig Topper883dd332015-12-24 23:58:11 +00001662 Actions.CodeCompleteObjCProtocolReferences(identifierLocPairs);
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001663 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001664 cutOffParsing();
1665 return;
1666 }
1667
1668 allSingleIdentifiers = false;
1669 break;
1670 } while (TryConsumeToken(tok::comma));
1671
1672 // If we parsed an identifier list, semantic analysis sorts out
1673 // whether it refers to protocols or to type arguments.
1674 if (allSingleIdentifiers) {
1675 // Parse the closing '>'.
1676 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001677 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001678 /*ObjCGenericList=*/true);
1679
1680 // Let Sema figure out what we parsed.
1681 Actions.actOnObjCTypeArgsOrProtocolQualifiers(getCurScope(),
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001682 baseType,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001683 lAngleLoc,
1684 identifiers,
1685 identifierLocs,
1686 rAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001687 typeArgsLAngleLoc,
1688 typeArgs,
1689 typeArgsRAngleLoc,
1690 protocolLAngleLoc,
1691 protocols,
1692 protocolRAngleLoc,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001693 warnOnIncompleteProtocols);
1694 return;
1695 }
1696
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001697 // We parsed an identifier list but stumbled into non single identifiers, this
1698 // means we might (a) check that what we already parsed is a legitimate type
1699 // (not a protocol or unknown type) and (b) parse the remaining ones, which
1700 // must all be type args.
Douglas Gregore9d95f12015-07-07 03:57:35 +00001701
1702 // Convert the identifiers into type arguments.
1703 bool invalid = false;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001704 IdentifierInfo *foundProtocolId = nullptr, *foundValidTypeId = nullptr;
1705 SourceLocation foundProtocolSrcLoc, foundValidTypeSrcLoc;
1706 SmallVector<IdentifierInfo *, 2> unknownTypeArgs;
1707 SmallVector<SourceLocation, 2> unknownTypeArgsLoc;
1708
Douglas Gregore9d95f12015-07-07 03:57:35 +00001709 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1710 ParsedType typeArg
1711 = Actions.getTypeName(*identifiers[i], identifierLocs[i], getCurScope());
1712 if (typeArg) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001713 DeclSpec DS(AttrFactory);
1714 const char *prevSpec = nullptr;
1715 unsigned diagID;
1716 DS.SetTypeSpecType(TST_typename, identifierLocs[i], prevSpec, diagID,
1717 typeArg, Actions.getASTContext().getPrintingPolicy());
1718
1719 // Form a declarator to turn this into a type.
1720 Declarator D(DS, Declarator::TypeNameContext);
1721 TypeResult fullTypeArg = Actions.ActOnTypeName(getCurScope(), D);
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001722 if (fullTypeArg.isUsable()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001723 typeArgs.push_back(fullTypeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001724 if (!foundValidTypeId) {
1725 foundValidTypeId = identifiers[i];
1726 foundValidTypeSrcLoc = identifierLocs[i];
1727 }
1728 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001729 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001730 unknownTypeArgs.push_back(identifiers[i]);
1731 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1732 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001733 } else {
1734 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001735 if (!Actions.LookupProtocol(identifiers[i], identifierLocs[i])) {
1736 unknownTypeArgs.push_back(identifiers[i]);
1737 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1738 } else if (!foundProtocolId) {
1739 foundProtocolId = identifiers[i];
1740 foundProtocolSrcLoc = identifierLocs[i];
1741 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001742 }
1743 }
1744
1745 // Continue parsing type-names.
1746 do {
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001747 Token CurTypeTok = Tok;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001748 TypeResult typeArg = ParseTypeName();
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001749
1750 // Consume the '...' for a pack expansion.
1751 SourceLocation ellipsisLoc;
1752 TryConsumeToken(tok::ellipsis, ellipsisLoc);
1753 if (typeArg.isUsable() && ellipsisLoc.isValid()) {
1754 typeArg = Actions.ActOnPackExpansion(typeArg.get(), ellipsisLoc);
1755 }
1756
Douglas Gregore9d95f12015-07-07 03:57:35 +00001757 if (typeArg.isUsable()) {
1758 typeArgs.push_back(typeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001759 if (!foundValidTypeId) {
1760 foundValidTypeId = CurTypeTok.getIdentifierInfo();
1761 foundValidTypeSrcLoc = CurTypeTok.getLocation();
1762 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001763 } else {
1764 invalid = true;
1765 }
1766 } while (TryConsumeToken(tok::comma));
1767
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001768 // Diagnose the mix between type args and protocols.
1769 if (foundProtocolId && foundValidTypeId)
1770 Actions.DiagnoseTypeArgsAndProtocols(foundProtocolId, foundProtocolSrcLoc,
1771 foundValidTypeId,
1772 foundValidTypeSrcLoc);
1773
1774 // Diagnose unknown arg types.
1775 ParsedType T;
1776 if (unknownTypeArgs.size())
1777 for (unsigned i = 0, e = unknownTypeArgsLoc.size(); i < e; ++i)
1778 Actions.DiagnoseUnknownTypeName(unknownTypeArgs[i], unknownTypeArgsLoc[i],
1779 getCurScope(), nullptr, T);
1780
Douglas Gregore9d95f12015-07-07 03:57:35 +00001781 // Parse the closing '>'.
1782 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001783 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001784 /*ObjCGenericList=*/true);
1785
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001786 if (invalid) {
1787 typeArgs.clear();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001788 return;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001789 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001790
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001791 // Record left/right angle locations.
1792 typeArgsLAngleLoc = lAngleLoc;
1793 typeArgsRAngleLoc = rAngleLoc;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001794}
1795
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001796void Parser::parseObjCTypeArgsAndProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001797 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001798 SourceLocation &typeArgsLAngleLoc,
1799 SmallVectorImpl<ParsedType> &typeArgs,
1800 SourceLocation &typeArgsRAngleLoc,
1801 SourceLocation &protocolLAngleLoc,
1802 SmallVectorImpl<Decl *> &protocols,
1803 SmallVectorImpl<SourceLocation> &protocolLocs,
1804 SourceLocation &protocolRAngleLoc,
1805 bool consumeLastToken) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001806 assert(Tok.is(tok::less));
1807
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001808 // Parse the first angle-bracket-delimited clause.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001809 parseObjCTypeArgsOrProtocolQualifiers(baseType,
1810 typeArgsLAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001811 typeArgs,
1812 typeArgsRAngleLoc,
1813 protocolLAngleLoc,
1814 protocols,
1815 protocolLocs,
1816 protocolRAngleLoc,
1817 consumeLastToken,
Douglas Gregore83b9562015-07-07 03:57:53 +00001818 /*warnOnIncompleteProtocols=*/false);
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00001819 if (Tok.is(tok::eof)) // Nothing else to do here...
1820 return;
Douglas Gregore83b9562015-07-07 03:57:53 +00001821
1822 // An Objective-C object pointer followed by type arguments
1823 // can then be followed again by a set of protocol references, e.g.,
1824 // \c NSArray<NSView><NSTextDelegate>
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001825 if ((consumeLastToken && Tok.is(tok::less)) ||
1826 (!consumeLastToken && NextToken().is(tok::less))) {
1827 // If we aren't consuming the last token, the prior '>' is still hanging
1828 // there. Consume it before we parse the protocol qualifiers.
1829 if (!consumeLastToken)
1830 ConsumeToken();
1831
1832 if (!protocols.empty()) {
1833 SkipUntilFlags skipFlags = SkipUntilFlags();
1834 if (!consumeLastToken)
1835 skipFlags = skipFlags | StopBeforeMatch;
Douglas Gregore83b9562015-07-07 03:57:53 +00001836 Diag(Tok, diag::err_objc_type_args_after_protocols)
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001837 << SourceRange(protocolLAngleLoc, protocolRAngleLoc);
1838 SkipUntil(tok::greater, tok::greatergreater, skipFlags);
Douglas Gregore83b9562015-07-07 03:57:53 +00001839 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001840 ParseObjCProtocolReferences(protocols, protocolLocs,
1841 /*WarnOnDeclarations=*/false,
1842 /*ForObjCContainer=*/false,
1843 protocolLAngleLoc, protocolRAngleLoc,
1844 consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001845 }
1846 }
1847}
1848
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001849TypeResult Parser::parseObjCTypeArgsAndProtocolQualifiers(
1850 SourceLocation loc,
1851 ParsedType type,
1852 bool consumeLastToken,
1853 SourceLocation &endLoc) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001854 assert(Tok.is(tok::less));
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001855 SourceLocation typeArgsLAngleLoc;
1856 SmallVector<ParsedType, 4> typeArgs;
1857 SourceLocation typeArgsRAngleLoc;
1858 SourceLocation protocolLAngleLoc;
1859 SmallVector<Decl *, 4> protocols;
1860 SmallVector<SourceLocation, 4> protocolLocs;
1861 SourceLocation protocolRAngleLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00001862
1863 // Parse type arguments and protocol qualifiers.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001864 parseObjCTypeArgsAndProtocolQualifiers(type, typeArgsLAngleLoc, typeArgs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001865 typeArgsRAngleLoc, protocolLAngleLoc,
1866 protocols, protocolLocs,
1867 protocolRAngleLoc, consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001868
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00001869 if (Tok.is(tok::eof))
1870 return true; // Invalid type result.
1871
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001872 // Compute the location of the last token.
1873 if (consumeLastToken)
1874 endLoc = PrevTokLocation;
1875 else
1876 endLoc = Tok.getLocation();
1877
1878 return Actions.actOnObjCTypeArgsAndProtocolQualifiers(
1879 getCurScope(),
1880 loc,
1881 type,
1882 typeArgsLAngleLoc,
1883 typeArgs,
1884 typeArgsRAngleLoc,
1885 protocolLAngleLoc,
1886 protocols,
1887 protocolLocs,
1888 protocolRAngleLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00001889}
1890
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001891void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1892 BalancedDelimiterTracker &T,
1893 SmallVectorImpl<Decl *> &AllIvarDecls,
1894 bool RBraceMissing) {
1895 if (!RBraceMissing)
1896 T.consumeClose();
1897
1898 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1899 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1900 Actions.ActOnObjCContainerFinishDefinition();
1901 // Call ActOnFields() even if we don't have any decls. This is useful
1902 // for code rewriting tools that need to be aware of the empty list.
1903 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1904 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001905 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001906}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001907
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001908/// objc-class-instance-variables:
1909/// '{' objc-instance-variable-decl-list[opt] '}'
1910///
1911/// objc-instance-variable-decl-list:
1912/// objc-visibility-spec
1913/// objc-instance-variable-decl ';'
1914/// ';'
1915/// objc-instance-variable-decl-list objc-visibility-spec
1916/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1917/// objc-instance-variable-decl-list ';'
1918///
1919/// objc-visibility-spec:
1920/// @private
1921/// @protected
1922/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001923/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001924///
1925/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001926/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001927///
John McCall48871652010-08-21 09:40:31 +00001928void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001929 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001930 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001931 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001932 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001933
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001934 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001935 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001936
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001937 BalancedDelimiterTracker T(*this, tok::l_brace);
1938 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001939 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001940 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001941 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001942
Steve Naroff00433d32007-08-21 21:17:12 +00001943 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001944 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001945 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001946 continue;
1947 }
Mike Stump11289f42009-09-09 15:08:12 +00001948
Steve Naroff00433d32007-08-21 21:17:12 +00001949 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001950 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001951 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001952 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001953 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001954 }
1955
Steve Naroff7c348172007-08-23 18:16:40 +00001956 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001957 case tok::objc_private:
1958 case tok::objc_public:
1959 case tok::objc_protected:
1960 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001961 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001962 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001963 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001964
1965 case tok::objc_end:
1966 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001967 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1968 Tok.setKind(tok::at);
1969 Tok.setLength(1);
1970 PP.EnterToken(Tok);
1971 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1972 T, AllIvarDecls, true);
1973 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001974
1975 default:
1976 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1977 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001978 }
1979 }
Mike Stump11289f42009-09-09 15:08:12 +00001980
Douglas Gregor48d46252010-01-13 21:54:15 +00001981 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001982 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001983 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001984 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001985 }
John McCallcfefb6d2009-11-03 02:38:08 +00001986
Benjamin Kramera39beb92014-09-03 11:06:10 +00001987 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1988 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1989 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001990 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001991 Decl *Field = Actions.ActOnIvar(
1992 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1993 FD.BitfieldSize, visibility);
1994 Actions.ActOnObjCContainerFinishDefinition();
1995 if (Field)
1996 AllIvarDecls.push_back(Field);
1997 FD.complete(Field);
1998 };
John McCallcfefb6d2009-11-03 02:38:08 +00001999
Chris Lattnera12405b2008-04-10 06:46:29 +00002000 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002001 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00002002 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00002003
Chris Lattner0ef13522007-10-09 17:51:17 +00002004 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00002005 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00002006 } else {
2007 Diag(Tok, diag::err_expected_semi_decl_list);
2008 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00002009 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00002010 }
2011 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00002012 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
2013 T, AllIvarDecls, false);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002014}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002015
2016/// objc-protocol-declaration:
2017/// objc-protocol-definition
2018/// objc-protocol-forward-reference
2019///
2020/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00002021/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00002022/// objc-protocol-refs[opt]
2023/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00002024/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002025///
2026/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00002027/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002028///
James Dennett1355bd12012-06-11 06:19:40 +00002029/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00002030/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002031/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00002032Parser::DeclGroupPtrTy
2033Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
2034 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00002035 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002036 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
2037 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002038
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002039 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002040 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002041 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002042 return nullptr;
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002043 }
2044
Nico Weber69a79142013-04-04 00:15:10 +00002045 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00002046
Chris Lattner0ef13522007-10-09 17:51:17 +00002047 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002048 Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
David Blaikie0403cb12016-01-15 23:43:25 +00002049 return nullptr;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002050 }
2051 // Save the protocol name, then consume it.
2052 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
2053 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002054
Alp Toker383d2c42014-01-01 03:08:43 +00002055 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00002056 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Craig Topper0f723bb2015-10-22 05:00:01 +00002057 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtoInfo,
John McCall53fa7142010-12-24 02:08:15 +00002058 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002059 }
Mike Stump11289f42009-09-09 15:08:12 +00002060
Erik Verbruggenf9887852011-12-08 09:58:43 +00002061 CheckNestedObjCContexts(AtLoc);
2062
Chris Lattner0ef13522007-10-09 17:51:17 +00002063 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002064 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002065 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
2066
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002067 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002068 while (1) {
2069 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00002070 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002071 Diag(Tok, diag::err_expected) << tok::identifier;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002072 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002073 return nullptr;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002074 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00002075 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
2076 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002077 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00002078
Chris Lattner0ef13522007-10-09 17:51:17 +00002079 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002080 break;
2081 }
2082 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00002083 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
David Blaikie0403cb12016-01-15 23:43:25 +00002084 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002085
Craig Topper0f723bb2015-10-22 05:00:01 +00002086 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtocolRefs,
John McCall53fa7142010-12-24 02:08:15 +00002087 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00002088 }
Mike Stump11289f42009-09-09 15:08:12 +00002089
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002090 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00002091 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002092
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002093 SmallVector<Decl *, 8> ProtocolRefs;
2094 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002095 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00002096 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002097 LAngleLoc, EndProtoLoc,
2098 /*consumeLastToken=*/true))
David Blaikie0403cb12016-01-15 23:43:25 +00002099 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002100
John McCall48871652010-08-21 09:40:31 +00002101 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00002102 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002103 ProtocolRefs.data(),
2104 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00002105 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00002106 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002107
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00002108 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00002109 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002110}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002111
2112/// objc-implementation:
2113/// objc-class-implementation-prologue
2114/// objc-category-implementation-prologue
2115///
2116/// objc-class-implementation-prologue:
2117/// @implementation identifier objc-superclass[opt]
2118/// objc-class-instance-variables[opt]
2119///
2120/// objc-category-implementation-prologue:
2121/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002122Parser::DeclGroupPtrTy
2123Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002124 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
2125 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002126 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002127 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002128
Douglas Gregor49c22a72009-11-18 16:26:39 +00002129 // Code completion after '@implementation'.
2130 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002131 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002132 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002133 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +00002134 }
2135
Nico Weber69a79142013-04-04 00:15:10 +00002136 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00002137
Chris Lattner0ef13522007-10-09 17:51:17 +00002138 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002139 Diag(Tok, diag::err_expected)
2140 << tok::identifier; // missing class or category name.
David Blaikie0403cb12016-01-15 23:43:25 +00002141 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002142 }
2143 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00002144 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002145 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00002146 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002147
Douglas Gregor85f3f952015-07-07 03:57:15 +00002148 // Neither a type parameter list nor a list of protocol references is
2149 // permitted here. Parse and diagnose them.
2150 if (Tok.is(tok::less)) {
2151 SourceLocation lAngleLoc, rAngleLoc;
2152 SmallVector<IdentifierLocPair, 8> protocolIdents;
2153 SourceLocation diagLoc = Tok.getLocation();
Richard Smith3df3f1d2015-11-03 01:19:56 +00002154 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
2155 if (parseObjCTypeParamListOrProtocolRefs(typeParamScope, lAngleLoc,
2156 protocolIdents, rAngleLoc)) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00002157 Diag(diagLoc, diag::err_objc_parameterized_implementation)
2158 << SourceRange(diagLoc, PrevTokLocation);
2159 } else if (lAngleLoc.isValid()) {
2160 Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
2161 << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
2162 }
2163 }
2164
Mike Stump11289f42009-09-09 15:08:12 +00002165 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002166 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002167 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002168 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002169 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002170
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002171 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002172 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002173 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002174 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002175 }
2176
Chris Lattner0ef13522007-10-09 17:51:17 +00002177 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002178 categoryId = Tok.getIdentifierInfo();
2179 categoryLoc = ConsumeToken();
2180 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002181 Diag(Tok, diag::err_expected)
2182 << tok::identifier; // missing category name.
David Blaikie0403cb12016-01-15 23:43:25 +00002183 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002184 }
Chris Lattner0ef13522007-10-09 17:51:17 +00002185 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002186 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002187 SkipUntil(tok::r_paren); // don't stop at ';'
David Blaikie0403cb12016-01-15 23:43:25 +00002188 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002189 }
2190 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00002191 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2192 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002193 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2194 SmallVector<Decl *, 4> protocols;
2195 SmallVector<SourceLocation, 4> protocolLocs;
2196 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2197 /*warnOnIncompleteProtocols=*/false,
2198 /*ForObjCContainer=*/false,
2199 protocolLAngleLoc, protocolRAngleLoc,
2200 /*consumeLastToken=*/true);
Fariborz Jahanian85888552013-05-17 17:58:11 +00002201 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002202 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002203 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00002204 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002205
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002206 } else {
2207 // We have a class implementation
2208 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002209 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00002210 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002211 // We have a super class
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002212 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002213 Diag(Tok, diag::err_expected)
2214 << tok::identifier; // missing super class name.
David Blaikie0403cb12016-01-15 23:43:25 +00002215 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002216 }
2217 superClassId = Tok.getIdentifierInfo();
2218 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002219 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002220 ObjCImpDecl = Actions.ActOnStartClassImplementation(
2221 AtLoc, nameId, nameLoc,
2222 superClassId, superClassLoc);
2223
2224 if (Tok.is(tok::l_brace)) // we have ivars
2225 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002226 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2227 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002228
2229 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2230 SmallVector<Decl *, 4> protocols;
2231 SmallVector<SourceLocation, 4> protocolLocs;
2232 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2233 /*warnOnIncompleteProtocols=*/false,
2234 /*ForObjCContainer=*/false,
2235 protocolLAngleLoc, protocolRAngleLoc,
2236 /*consumeLastToken=*/true);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002237 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002238 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002239 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002240
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002241 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002242
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002243 {
2244 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00002245 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002246 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002247 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002248 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
2249 DeclGroupRef DG = DGP.get();
2250 DeclsInGroup.append(DG.begin(), DG.end());
2251 }
2252 }
2253 }
2254
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00002255 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002256}
Steve Naroff33a1e802007-10-29 21:38:07 +00002257
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002258Parser::DeclGroupPtrTy
2259Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002260 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
2261 "ParseObjCAtEndDeclaration(): Expected @end");
2262 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002263 if (CurParsedObjCImpl)
2264 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002265 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00002266 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002267 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
David Blaikie0403cb12016-01-15 23:43:25 +00002268 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002269}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002270
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002271Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
2272 if (!Finished) {
2273 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00002274 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002275 P.Diag(P.Tok, diag::err_objc_missing_end)
2276 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
2277 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
2278 << Sema::OCK_Implementation;
2279 }
2280 }
Craig Topper161e4db2014-05-21 06:02:52 +00002281 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002282 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002283}
2284
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002285void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
2286 assert(!Finished);
2287 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
2288 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002289 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2290 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002291
2292 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
2293
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002294 if (HasCFunction)
2295 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
2296 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2297 false/*c-functions*/);
2298
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002299 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002300 for (LateParsedObjCMethodContainer::iterator
2301 I = LateParsedObjCMethods.begin(),
2302 E = LateParsedObjCMethods.end(); I != E; ++I)
2303 delete *I;
2304 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002305
2306 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002307}
2308
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002309/// compatibility-alias-decl:
2310/// @compatibility_alias alias-name class-name ';'
2311///
John McCall48871652010-08-21 09:40:31 +00002312Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002313 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
2314 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
2315 ConsumeToken(); // consume compatibility_alias
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 *aliasId = Tok.getIdentifierInfo();
2321 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00002322 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002323 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002324 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002325 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002326 IdentifierInfo *classId = Tok.getIdentifierInfo();
2327 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00002328 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00002329 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
2330 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002331}
2332
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002333/// property-synthesis:
2334/// @synthesize property-ivar-list ';'
2335///
2336/// property-ivar-list:
2337/// property-ivar
2338/// property-ivar-list ',' property-ivar
2339///
2340/// property-ivar:
2341/// identifier
2342/// identifier '=' identifier
2343///
John McCall48871652010-08-21 09:40:31 +00002344Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002345 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00002346 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002347 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00002348
Douglas Gregor88e72a02009-11-18 19:45:45 +00002349 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00002350 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002351 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002352 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002353 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002354 }
2355
Douglas Gregor88e72a02009-11-18 19:45:45 +00002356 if (Tok.isNot(tok::identifier)) {
2357 Diag(Tok, diag::err_synthesized_property_name);
2358 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002359 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00002360 }
Craig Topper161e4db2014-05-21 06:02:52 +00002361
2362 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002363 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2364 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002365 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00002366 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002367 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00002368 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002369 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002370 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002371 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002372 }
2373
Chris Lattner0ef13522007-10-09 17:51:17 +00002374 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002375 Diag(Tok, diag::err_expected) << tok::identifier;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002376 break;
2377 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002378 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002379 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002380 }
Manman Ren5b786402016-01-28 18:49:28 +00002381 Actions.ActOnPropertyImplDecl(
2382 getCurScope(), atLoc, propertyLoc, true,
2383 propertyId, propertyIvar, propertyIvarLoc,
2384 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Chris Lattner0ef13522007-10-09 17:51:17 +00002385 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002386 break;
2387 ConsumeToken(); // consume ','
2388 }
Alp Toker383d2c42014-01-01 03:08:43 +00002389 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00002390 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002391}
2392
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002393/// property-dynamic:
2394/// @dynamic property-list
2395///
2396/// property-list:
2397/// identifier
2398/// property-list ',' identifier
2399///
John McCall48871652010-08-21 09:40:31 +00002400Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002401 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2402 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002403 ConsumeToken(); // consume dynamic
Manman Ren0fe61f82016-01-29 19:05:57 +00002404
2405 bool isClassProperty = false;
2406 if (Tok.is(tok::l_paren)) {
2407 ConsumeParen();
2408 const IdentifierInfo *II = Tok.getIdentifierInfo();
2409
2410 if (!II) {
2411 Diag(Tok, diag::err_objc_expected_property_attr) << II;
2412 SkipUntil(tok::r_paren, StopAtSemi);
2413 } else {
2414 SourceLocation AttrName = ConsumeToken(); // consume attribute name
2415 if (II->isStr("class")) {
2416 isClassProperty = true;
2417 if (Tok.isNot(tok::r_paren)) {
2418 Diag(Tok, diag::err_expected) << tok::r_paren;
2419 SkipUntil(tok::r_paren, StopAtSemi);
2420 } else
2421 ConsumeParen();
2422 } else {
2423 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
2424 SkipUntil(tok::r_paren, StopAtSemi);
2425 }
2426 }
2427 }
2428
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002429 while (true) {
2430 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002431 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002432 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002433 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002434 }
2435
2436 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002437 Diag(Tok, diag::err_expected) << tok::identifier;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002438 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002439 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002440 }
2441
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002442 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2443 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Manman Ren5b786402016-01-28 18:49:28 +00002444 Actions.ActOnPropertyImplDecl(
2445 getCurScope(), atLoc, propertyLoc, false,
2446 propertyId, nullptr, SourceLocation(),
Manman Ren0fe61f82016-01-29 19:05:57 +00002447 isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
Manman Ren5b786402016-01-28 18:49:28 +00002448 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002449
Chris Lattner0ef13522007-10-09 17:51:17 +00002450 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002451 break;
2452 ConsumeToken(); // consume ','
2453 }
Alp Toker383d2c42014-01-01 03:08:43 +00002454 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00002455 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002456}
Mike Stump11289f42009-09-09 15:08:12 +00002457
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002458/// objc-throw-statement:
2459/// throw expression[opt];
2460///
John McCalldadc5752010-08-24 06:29:42 +00002461StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2462 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002463 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00002464 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00002465 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002466 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002467 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002468 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002469 }
2470 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00002471 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00002472 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002473 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002474}
2475
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002476/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002477/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002478///
John McCalldadc5752010-08-24 06:29:42 +00002479StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002480Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002481 ConsumeToken(); // consume synchronized
2482 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002483 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002484 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002485 }
John McCalld9bb7432011-07-27 21:50:02 +00002486
2487 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002488 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00002489 ExprResult operand(ParseExpression());
2490
2491 if (Tok.is(tok::r_paren)) {
2492 ConsumeParen(); // ')'
2493 } else {
2494 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002495 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00002496
2497 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002498 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002499 }
John McCalld9bb7432011-07-27 21:50:02 +00002500
2501 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002502 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00002503 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002504 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002505 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002506 }
Steve Naroffd9c26072008-06-04 20:36:13 +00002507
John McCalld9bb7432011-07-27 21:50:02 +00002508 // Check the @synchronized operand now.
2509 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002510 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002511
John McCalld9bb7432011-07-27 21:50:02 +00002512 // Parse the compound statement within a new scope.
2513 ParseScope bodyScope(this, Scope::DeclScope);
2514 StmtResult body(ParseCompoundStatementBody());
2515 bodyScope.Exit();
2516
2517 // If there was a semantic or parse error earlier with the
2518 // operand, fail now.
2519 if (operand.isInvalid())
2520 return StmtError();
2521
2522 if (body.isInvalid())
2523 body = Actions.ActOnNullStmt(Tok.getLocation());
2524
2525 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002526}
2527
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002528/// objc-try-catch-statement:
2529/// @try compound-statement objc-catch-list[opt]
2530/// @try compound-statement objc-catch-list[opt] @finally compound-statement
2531///
2532/// objc-catch-list:
2533/// @catch ( parameter-declaration ) compound-statement
2534/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2535/// catch-parameter-declaration:
2536/// parameter-declaration
2537/// '...' [OBJC2]
2538///
John McCalldadc5752010-08-24 06:29:42 +00002539StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002540 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002541
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002542 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00002543 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002544 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002545 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002546 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00002547 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00002548 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002549 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00002550 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002551 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002552 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002553 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002554
Chris Lattner0ef13522007-10-09 17:51:17 +00002555 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002556 // At this point, we need to lookahead to determine if this @ is the start
2557 // of an @catch or @finally. We don't want to consume the @ token if this
2558 // is an @try or @encode or something else.
2559 Token AfterAt = GetLookAheadToken(1);
2560 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2561 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2562 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002563
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002564 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002565 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002566 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002567 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002568 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002569 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00002570 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002571 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002572 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002573 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00002574 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002575 ParseDeclarator(ParmDecl);
2576
Douglas Gregore11ee112010-04-23 23:01:43 +00002577 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002578 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002579 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002580 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002581 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002582
Steve Naroff65a00892009-04-07 22:56:58 +00002583 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002584
Steve Naroff65a00892009-04-07 22:56:58 +00002585 if (Tok.is(tok::r_paren))
2586 RParenLoc = ConsumeParen();
2587 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002588 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002589
John McCalldadc5752010-08-24 06:29:42 +00002590 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002591 if (Tok.is(tok::l_brace))
2592 CatchBody = ParseCompoundStatementBody();
2593 else
Alp Tokerec543272013-12-24 09:48:30 +00002594 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002595 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002596 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00002597
John McCalldadc5752010-08-24 06:29:42 +00002598 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00002599 RParenLoc,
2600 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002601 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002602 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002603 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002604
Steve Naroffe6016792008-02-05 21:27:35 +00002605 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002606 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2607 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002608 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002609 }
2610 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002611 } else {
2612 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002613 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002614 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002615
John McCalldadc5752010-08-24 06:29:42 +00002616 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002617 if (Tok.is(tok::l_brace))
2618 FinallyBody = ParseCompoundStatementBody();
2619 else
Alp Tokerec543272013-12-24 09:48:30 +00002620 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002621 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002622 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002623 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002624 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002625 catch_or_finally_seen = true;
2626 break;
2627 }
2628 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002629 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002630 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002631 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002632 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002633
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002634 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002635 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002636 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002637}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002638
John McCall31168b02011-06-15 23:02:42 +00002639/// objc-autoreleasepool-statement:
2640/// @autoreleasepool compound-statement
2641///
2642StmtResult
2643Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2644 ConsumeToken(); // consume autoreleasepool
2645 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002646 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002647 return StmtError();
2648 }
2649 // Enter a scope to hold everything within the compound stmt. Compound
2650 // statements can always hold declarations.
2651 ParseScope BodyScope(this, Scope::DeclScope);
2652
2653 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2654
2655 BodyScope.Exit();
2656 if (AutoreleasePoolBody.isInvalid())
2657 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2658 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002659 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002660}
2661
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002662/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2663/// for later parsing.
2664void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002665 if (SkipFunctionBodies && (!MDecl || Actions.canSkipFunctionBody(MDecl)) &&
2666 trySkippingFunctionBody()) {
2667 Actions.ActOnSkippedFunctionBody(MDecl);
2668 return;
2669 }
2670
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002671 LexedMethod* LM = new LexedMethod(this, MDecl);
2672 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2673 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002674 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002675 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002676 if (Tok.is(tok::kw_try)) {
2677 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002678 if (Tok.is(tok::colon)) {
2679 Toks.push_back(Tok);
2680 ConsumeToken();
2681 while (Tok.isNot(tok::l_brace)) {
2682 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2683 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2684 }
2685 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002686 Toks.push_back(Tok); // also store '{'
2687 }
2688 else if (Tok.is(tok::colon)) {
2689 ConsumeToken();
Richard Smithb9fa9962015-08-21 03:04:33 +00002690 // FIXME: This is wrong, due to C++11 braced initialization.
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002691 while (Tok.isNot(tok::l_brace)) {
2692 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2693 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2694 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002695 Toks.push_back(Tok); // also store '{'
2696 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002697 ConsumeBrace();
2698 // Consume everything up to (and including) the matching right brace.
2699 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002700 while (Tok.is(tok::kw_catch)) {
2701 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2702 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2703 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002704}
2705
Steve Naroff09bf8152007-09-06 21:24:23 +00002706/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002707///
John McCall48871652010-08-21 09:40:31 +00002708Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002709 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002710
John McCallfaf5fb42010-08-26 23:41:50 +00002711 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2712 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002713
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002714 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002715 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002716 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002717 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002718 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002719 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002720 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002721 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002722
Steve Naroffbb875722007-11-11 19:54:21 +00002723 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002724 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002725 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002726
Steve Naroffbb875722007-11-11 19:54:21 +00002727 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002728 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002729
Steve Naroffbb875722007-11-11 19:54:21 +00002730 // If we didn't find the '{', bail out.
2731 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002732 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002733 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002734
2735 if (!MDecl) {
2736 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002737 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002738 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002739 }
2740
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002741 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002742 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002743 assert (CurParsedObjCImpl
2744 && "ParseObjCMethodDefinition - Method out of @implementation");
2745 // Consume the tokens and store them for later parsing.
2746 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002747 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002748}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002749
John McCalldadc5752010-08-24 06:29:42 +00002750StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002751 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002752 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002753 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002754 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002755 }
2756
2757 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002758 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002759
2760 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002761 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002762
2763 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002764 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002765
2766 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2767 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002768
2769 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2770 getLangOpts().DebuggerSupport) {
2771 SkipUntil(tok::semi);
2772 return Actions.ActOnNullStmt(Tok.getLocation());
2773 }
2774
John McCalldadc5752010-08-24 06:29:42 +00002775 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002776 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002777 // If the expression is invalid, skip ahead to the next semicolon. Not
2778 // doing this opens us up to the possibility of infinite loops if
2779 // ParseExpression does not consume any tokens.
2780 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002781 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002782 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002783
Steve Naroffe6016792008-02-05 21:27:35 +00002784 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002785 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002786 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002787}
2788
John McCalldadc5752010-08-24 06:29:42 +00002789ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002790 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002791 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002792 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002793 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002794 return ExprError();
2795
Ted Kremeneke65b0862012-03-06 20:05:56 +00002796 case tok::minus:
2797 case tok::plus: {
2798 tok::TokenKind Kind = Tok.getKind();
2799 SourceLocation OpLoc = ConsumeToken();
2800
2801 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002802 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002803 switch (Kind) {
2804 case tok::minus: Symbol = "-"; break;
2805 case tok::plus: Symbol = "+"; break;
2806 default: llvm_unreachable("missing unary operator case");
2807 }
2808 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2809 << Symbol;
2810 return ExprError();
2811 }
2812
2813 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2814 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002815 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002816 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002817 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002818
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002819 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002820 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002821 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002822
2823 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002824 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002825 }
2826
Chris Lattnere002fbe2007-12-12 01:04:12 +00002827 case tok::string_literal: // primary-expression: string-literal
2828 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002829 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002830
2831 case tok::char_constant:
2832 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2833
2834 case tok::numeric_constant:
2835 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2836
2837 case tok::kw_true: // Objective-C++, etc.
2838 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2839 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2840 case tok::kw_false: // Objective-C++, etc.
2841 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2842 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2843
2844 case tok::l_square:
2845 // Objective-C array literal
2846 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2847
2848 case tok::l_brace:
2849 // Objective-C dictionary literal
2850 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2851
Patrick Beard0caa3942012-04-19 00:25:12 +00002852 case tok::l_paren:
2853 // Objective-C boxed expression
2854 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2855
Chris Lattnere002fbe2007-12-12 01:04:12 +00002856 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002857 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002858 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002859
Chris Lattner197a3012008-08-05 06:19:09 +00002860 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2861 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002862 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002863 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002864 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002865 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002866 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Erik Pilkington29099de2016-07-16 00:35:23 +00002867 case tok::objc_available:
2868 return ParseAvailabilityCheckExpr(AtLoc);
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002869 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002870 const char *str = nullptr;
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002871 if (GetLookAheadToken(1).is(tok::l_brace)) {
2872 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2873 str =
2874 ch == 't' ? "try"
2875 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002876 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002877 }
2878 if (str) {
2879 SourceLocation kwLoc = Tok.getLocation();
2880 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2881 FixItHint::CreateReplacement(kwLoc, str));
2882 }
2883 else
2884 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2885 }
Chris Lattner197a3012008-08-05 06:19:09 +00002886 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002887 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002888}
2889
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002890/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002891///
2892/// This routine parses the receiver of a message send in
2893/// Objective-C++ either as a type or as an expression. Note that this
2894/// routine must not be called to parse a send to 'super', since it
2895/// has no way to return such a result.
2896///
2897/// \param IsExpr Whether the receiver was parsed as an expression.
2898///
2899/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2900/// IsExpr is true), the parsed expression. If the receiver was parsed
2901/// as a type (\c IsExpr is false), the parsed type.
2902///
2903/// \returns True if an error occurred during parsing or semantic
2904/// analysis, in which case the arguments do not have valid
2905/// values. Otherwise, returns false for a successful parse.
2906///
2907/// objc-receiver: [C++]
2908/// 'super' [not parsed here]
2909/// expression
2910/// simple-type-specifier
2911/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002912bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002913 InMessageExpressionRAIIObject InMessage(*this, true);
2914
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002915 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2916 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002917 TryAnnotateTypeOrScopeToken();
2918
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002919 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002920 // objc-receiver:
2921 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002922 // Make sure any typos in the receiver are corrected or diagnosed, so that
2923 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2924 // only the things that are valid ObjC receivers?
2925 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002926 if (Receiver.isInvalid())
2927 return true;
2928
2929 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002930 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002931 return false;
2932 }
2933
2934 // objc-receiver:
2935 // typename-specifier
2936 // simple-type-specifier
2937 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002938 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002939 ParseCXXSimpleTypeSpecifier(DS);
2940
2941 if (Tok.is(tok::l_paren)) {
2942 // If we see an opening parentheses at this point, we are
2943 // actually parsing an expression that starts with a
2944 // function-style cast, e.g.,
2945 //
2946 // postfix-expression:
2947 // simple-type-specifier ( expression-list [opt] )
2948 // typename-specifier ( expression-list [opt] )
2949 //
2950 // Parse the remainder of this case, then the (optional)
2951 // postfix-expression suffix, followed by the (optional)
2952 // right-hand side of the binary expression. We have an
2953 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002954 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002955 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002956 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002957 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002958 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002959 if (Receiver.isInvalid())
2960 return true;
2961
2962 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002963 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002964 return false;
2965 }
2966
2967 // We have a class message. Turn the simple-type-specifier or
2968 // typename-specifier we parsed into a type and parse the
2969 // remainder of the class message.
2970 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002971 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002972 if (Type.isInvalid())
2973 return true;
2974
2975 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002976 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002977 return false;
2978}
2979
Douglas Gregor990ccac2010-05-31 14:40:22 +00002980/// \brief Determine whether the parser is currently referring to a an
2981/// Objective-C message send, using a simplified heuristic to avoid overhead.
2982///
2983/// This routine will only return true for a subset of valid message-send
2984/// expressions.
2985bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002986 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002987 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002988 return GetLookAheadToken(1).is(tok::identifier) &&
2989 GetLookAheadToken(2).is(tok::identifier);
2990}
2991
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002992bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002993 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002994 InMessageExpression)
2995 return false;
2996
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002997 ParsedType Type;
2998
2999 if (Tok.is(tok::annot_typename))
3000 Type = getTypeAnnotation(Tok);
3001 else if (Tok.is(tok::identifier))
3002 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
3003 getCurScope());
3004 else
3005 return false;
3006
3007 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
3008 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003009 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003010 if (Tok.is(tok::identifier))
3011 TryAnnotateTypeOrScopeToken();
3012
3013 return Tok.is(tok::annot_typename);
3014 }
3015 }
3016
3017 return false;
3018}
3019
Mike Stump11289f42009-09-09 15:08:12 +00003020/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003021/// '[' objc-receiver objc-message-args ']'
3022///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003023/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00003024/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003025/// expression
3026/// class-name
3027/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00003028///
John McCalldadc5752010-08-24 06:29:42 +00003029ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00003030 assert(Tok.is(tok::l_square) && "'[' expected");
3031 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
3032
Douglas Gregora817a192010-05-27 23:06:34 +00003033 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003034 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003035 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00003036 return ExprError();
3037 }
3038
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003039 InMessageExpressionRAIIObject InMessage(*this, true);
3040
David Blaikiebbafb8a2012-03-11 07:00:24 +00003041 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00003042 // We completely separate the C and C++ cases because C++ requires
3043 // more complicated (read: slower) parsing.
3044
3045 // Handle send to super.
3046 // FIXME: This doesn't benefit from the same typo-correction we
3047 // get in Objective-C.
3048 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00003049 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
David Blaikieefdccaa2016-01-15 23:43:34 +00003050 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3051 nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003052
3053 // Parse the receiver, which is either a type or an expression.
3054 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00003055 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00003056 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003057 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003058 return ExprError();
3059 }
3060
3061 if (IsExpr)
David Blaikieefdccaa2016-01-15 23:43:34 +00003062 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3063 static_cast<Expr *>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00003064
3065 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00003066 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00003067 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00003068 }
3069
3070 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00003071 IdentifierInfo *Name = Tok.getIdentifierInfo();
3072 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00003073 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003074 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00003075 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00003076 NextToken().is(tok::period),
3077 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00003078 case Sema::ObjCSuperMessage:
David Blaikieefdccaa2016-01-15 23:43:34 +00003079 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3080 nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003081
John McCallfaf5fb42010-08-26 23:41:50 +00003082 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00003083 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003084 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003085 return ExprError();
3086 }
3087
Douglas Gregore5798dc2010-04-21 20:38:13 +00003088 ConsumeToken(); // the type name
3089
Douglas Gregore83b9562015-07-07 03:57:53 +00003090 // Parse type arguments and protocol qualifiers.
3091 if (Tok.is(tok::less)) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003092 SourceLocation NewEndLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00003093 TypeResult NewReceiverType
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003094 = parseObjCTypeArgsAndProtocolQualifiers(NameLoc, ReceiverType,
3095 /*consumeLastToken=*/true,
3096 NewEndLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00003097 if (!NewReceiverType.isUsable()) {
3098 SkipUntil(tok::r_square, StopAtSemi);
3099 return ExprError();
3100 }
3101
3102 ReceiverType = NewReceiverType.get();
3103 }
3104
Douglas Gregore5798dc2010-04-21 20:38:13 +00003105 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00003106 ReceiverType, nullptr);
3107
John McCallfaf5fb42010-08-26 23:41:50 +00003108 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003109 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00003110 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00003111 }
Chris Lattner8f697062008-01-25 18:59:06 +00003112 }
Chris Lattnera36ec422010-04-11 08:28:14 +00003113
3114 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00003115 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003116 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003117 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003118 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00003119 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003120
David Blaikieefdccaa2016-01-15 23:43:34 +00003121 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3122 Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00003123}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003124
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003125/// \brief Parse the remainder of an Objective-C message following the
3126/// '[' objc-receiver.
3127///
3128/// This routine handles sends to super, class messages (sent to a
3129/// class name), and instance messages (sent to an object), and the
3130/// target is represented by \p SuperLoc, \p ReceiverType, or \p
3131/// ReceiverExpr, respectively. Only one of these parameters may have
3132/// a valid value.
3133///
3134/// \param LBracLoc The location of the opening '['.
3135///
3136/// \param SuperLoc If this is a send to 'super', the location of the
3137/// 'super' keyword that indicates a send to the superclass.
3138///
3139/// \param ReceiverType If this is a class message, the type of the
3140/// class we are sending a message to.
3141///
3142/// \param ReceiverExpr If this is an instance message, the expression
3143/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00003144///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003145/// objc-message-args:
3146/// objc-selector
3147/// objc-keywordarg-list
3148///
3149/// objc-keywordarg-list:
3150/// objc-keywordarg
3151/// objc-keywordarg-list objc-keywordarg
3152///
Mike Stump11289f42009-09-09 15:08:12 +00003153/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003154/// selector-name[opt] ':' objc-keywordexpr
3155///
3156/// objc-keywordexpr:
3157/// nonempty-expr-list
3158///
3159/// nonempty-expr-list:
3160/// assignment-expression
3161/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003162///
John McCalldadc5752010-08-24 06:29:42 +00003163ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00003164Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003165 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00003166 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00003167 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003168 InMessageExpressionRAIIObject InMessage(*this, true);
3169
Steve Naroffeae65032009-11-07 02:08:14 +00003170 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003171 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003172 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003173 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003174 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003175 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003176 false);
Steve Naroffeae65032009-11-07 02:08:14 +00003177 else
John McCallb268a282010-08-23 23:25:46 +00003178 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003179 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003180 cutOffParsing();
3181 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00003182 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00003183
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003184 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00003185 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003186 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003187
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003188 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003189 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003190 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00003191
Chris Lattner0ef13522007-10-09 17:51:17 +00003192 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003193 while (1) {
3194 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00003195 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003196 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00003197
Alp Toker383d2c42014-01-01 03:08:43 +00003198 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00003199 // We must manually skip to a ']', otherwise the expression skipper will
3200 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3201 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003202 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003203 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003204 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003205
Mike Stump11289f42009-09-09 15:08:12 +00003206 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003207
3208 if (Tok.is(tok::code_completion)) {
3209 if (SuperLoc.isValid())
3210 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003211 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003212 /*AtArgumentEpression=*/true);
3213 else if (ReceiverType)
3214 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003215 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003216 /*AtArgumentEpression=*/true);
3217 else
3218 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003219 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003220 /*AtArgumentEpression=*/true);
3221
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003222 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003223 return ExprError();
3224 }
3225
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00003226 ExprResult Expr;
3227 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3228 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3229 Expr = ParseBraceInitializer();
3230 } else
3231 Expr = ParseAssignmentExpression();
3232
3233 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003234 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00003235 // We must manually skip to a ']', otherwise the expression skipper will
3236 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3237 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003238 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003239 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00003240 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003241
Steve Naroff486760a2007-09-17 20:25:27 +00003242 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003243 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003244
Douglas Gregor1b605f72009-11-19 01:08:35 +00003245 // Code completion after each argument.
3246 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003247 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003248 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003249 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003250 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003251 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003252 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003253 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003254 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00003255 else
John McCallb268a282010-08-23 23:25:46 +00003256 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003257 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003258 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003259 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003260 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00003261 }
3262
Steve Naroff486760a2007-09-17 20:25:27 +00003263 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00003264 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00003265 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003266 break;
3267 // We have a selector or a colon, continue parsing.
3268 }
3269 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00003270 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003271 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00003272 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00003273 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00003274 if (Tok.is(tok::colon))
3275 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003276 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003277 if (Tok.is(tok::colon)) {
3278 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
3279 FixItHint::CreateRemoval(commaLoc);
3280 }
Chris Lattner197a3012008-08-05 06:19:09 +00003281 // We must manually skip to a ']', otherwise the expression skipper will
3282 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3283 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003284 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003285 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003286 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003287
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003288 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003289 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003290 }
3291 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00003292 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003293
Chris Lattner197a3012008-08-05 06:19:09 +00003294 // We must manually skip to a ']', otherwise the expression skipper will
3295 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3296 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003297 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003298 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003299 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00003300
Chris Lattner0ef13522007-10-09 17:51:17 +00003301 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00003302 Diag(Tok, diag::err_expected)
3303 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00003304 // We must manually skip to a ']', otherwise the expression skipper will
3305 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3306 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003307 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003308 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003309 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003310
Chris Lattner8f697062008-01-25 18:59:06 +00003311 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003312
Steve Naroffe61bfa82007-10-05 18:42:47 +00003313 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003314 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00003315 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003316 KeyLocs.push_back(Loc);
3317 }
Chris Lattner5700fab2007-10-07 02:00:24 +00003318 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003319
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003320 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003321 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003322 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003323 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003324 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003325 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00003326 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003327 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003328}
3329
John McCalldadc5752010-08-24 06:29:42 +00003330ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
3331 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003332 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003333
Chris Lattnere002fbe2007-12-12 01:04:12 +00003334 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
3335 // expressions. At this point, we know that the only valid thing that starts
3336 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003337 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003338 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00003339 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003340 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003341
Chris Lattnere002fbe2007-12-12 01:04:12 +00003342 while (Tok.is(tok::at)) {
3343 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00003344
Sebastian Redlc13f2682008-12-09 20:22:58 +00003345 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00003346 if (!isTokenStringLiteral())
3347 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00003348
John McCalldadc5752010-08-24 06:29:42 +00003349 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003350 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003351 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003352
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003353 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00003354 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003355
Craig Topper883dd332015-12-24 23:58:11 +00003356 return Actions.ParseObjCStringLiteral(AtLocs.data(), AtStrings);
Anders Carlsson76f4a902007-08-21 17:43:55 +00003357}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003358
Ted Kremeneke65b0862012-03-06 20:05:56 +00003359/// ParseObjCBooleanLiteral -
3360/// objc-scalar-literal : '@' boolean-keyword
3361/// ;
3362/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
3363/// ;
3364ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
3365 bool ArgValue) {
3366 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
3367 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
3368}
3369
3370/// ParseObjCCharacterLiteral -
3371/// objc-scalar-literal : '@' character-literal
3372/// ;
3373ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
3374 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
3375 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003376 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003377 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003378 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003379 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003380}
3381
3382/// ParseObjCNumericLiteral -
3383/// objc-scalar-literal : '@' scalar-literal
3384/// ;
3385/// scalar-literal : | numeric-constant /* any numeric constant. */
3386/// ;
3387ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3388 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3389 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003390 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003391 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003392 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003393 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003394}
3395
Patrick Beard0caa3942012-04-19 00:25:12 +00003396/// ParseObjCBoxedExpr -
3397/// objc-box-expression:
3398/// @( assignment-expression )
3399ExprResult
3400Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3401 if (Tok.isNot(tok::l_paren))
3402 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3403
3404 BalancedDelimiterTracker T(*this, tok::l_paren);
3405 T.consumeOpen();
3406 ExprResult ValueExpr(ParseAssignmentExpression());
3407 if (T.consumeClose())
3408 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00003409
3410 if (ValueExpr.isInvalid())
3411 return ExprError();
3412
Patrick Beard0caa3942012-04-19 00:25:12 +00003413 // Wrap the sub-expression in a parenthesized expression, to distinguish
3414 // a boxed expression from a literal.
3415 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003416 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00003417 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003418 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00003419}
3420
Ted Kremeneke65b0862012-03-06 20:05:56 +00003421ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00003422 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003423 ConsumeBracket(); // consume the l_square.
3424
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003425 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003426 while (Tok.isNot(tok::r_square)) {
3427 // Parse list of array element expressions (all must be id types).
3428 ExprResult Res(ParseAssignmentExpression());
3429 if (Res.isInvalid()) {
3430 // We must manually skip to a ']', otherwise the expression skipper will
3431 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3432 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003433 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003434 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003435 }
3436
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003437 Res = Actions.CorrectDelayedTyposInExpr(Res.get());
3438 if (Res.isInvalid())
3439 HasInvalidEltExpr = true;
3440
Ted Kremeneke65b0862012-03-06 20:05:56 +00003441 // Parse the ellipsis that indicates a pack expansion.
3442 if (Tok.is(tok::ellipsis))
3443 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
3444 if (Res.isInvalid())
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003445 HasInvalidEltExpr = true;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003446
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003447 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003448
3449 if (Tok.is(tok::comma))
3450 ConsumeToken(); // Eat the ','.
3451 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00003452 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3453 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003454 }
3455 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003456
3457 if (HasInvalidEltExpr)
3458 return ExprError();
3459
Benjamin Kramerf0623432012-08-23 22:51:59 +00003460 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00003461 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003462}
3463
3464ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3465 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3466 ConsumeBrace(); // consume the l_square.
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003467 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003468 while (Tok.isNot(tok::r_brace)) {
3469 // Parse the comma separated key : value expressions.
3470 ExprResult KeyExpr;
3471 {
3472 ColonProtectionRAIIObject X(*this);
3473 KeyExpr = ParseAssignmentExpression();
3474 if (KeyExpr.isInvalid()) {
3475 // We must manually skip to a '}', otherwise the expression skipper will
3476 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3477 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003478 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003479 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003480 }
3481 }
3482
Alp Toker383d2c42014-01-01 03:08:43 +00003483 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003484 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00003485 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003486 }
3487
3488 ExprResult ValueExpr(ParseAssignmentExpression());
3489 if (ValueExpr.isInvalid()) {
3490 // We must manually skip to a '}', otherwise the expression skipper will
3491 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3492 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003493 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003494 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003495 }
3496
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003497 // Check the key and value for possible typos
3498 KeyExpr = Actions.CorrectDelayedTyposInExpr(KeyExpr.get());
3499 ValueExpr = Actions.CorrectDelayedTyposInExpr(ValueExpr.get());
3500 if (KeyExpr.isInvalid() || ValueExpr.isInvalid())
3501 HasInvalidEltExpr = true;
3502
3503 // Parse the ellipsis that designates this as a pack expansion. Do not
3504 // ActOnPackExpansion here, leave it to template instantiation time where
3505 // we can get better diagnostics.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003506 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00003507 if (getLangOpts().CPlusPlus)
3508 TryConsumeToken(tok::ellipsis, EllipsisLoc);
3509
Ted Kremeneke65b0862012-03-06 20:05:56 +00003510 // We have a valid expression. Collect it in a vector so we can
3511 // build the argument list.
3512 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00003513 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00003514 };
3515 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00003516
3517 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00003518 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3519 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003520 }
3521 SourceLocation EndLoc = ConsumeBrace();
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003522
3523 if (HasInvalidEltExpr)
3524 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003525
3526 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00003527 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
Craig Topperd4336e02015-12-24 23:58:15 +00003528 Elements);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003529}
3530
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003531/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00003532/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00003533ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003534Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00003535 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003536
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003537 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003538
Chris Lattner197a3012008-08-05 06:19:09 +00003539 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003540 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3541
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003542 BalancedDelimiterTracker T(*this, tok::l_paren);
3543 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003544
Douglas Gregor220cac52009-02-18 17:45:20 +00003545 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003546
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003547 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003548
Douglas Gregor220cac52009-02-18 17:45:20 +00003549 if (Ty.isInvalid())
3550 return ExprError();
3551
Nico Webera7c7e602012-12-31 00:28:03 +00003552 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3553 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003554}
Anders Carlssone01493d2007-08-23 15:25:28 +00003555
3556/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00003557/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00003558ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003559Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00003560 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003561
Chris Lattner197a3012008-08-05 06:19:09 +00003562 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003563 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3564
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003565 BalancedDelimiterTracker T(*this, tok::l_paren);
3566 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003567
Chris Lattner197a3012008-08-05 06:19:09 +00003568 if (Tok.isNot(tok::identifier))
Alp Tokerec543272013-12-24 09:48:30 +00003569 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003570
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003571 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00003572 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003573
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003574 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00003575
Nico Webera7c7e602012-12-31 00:28:03 +00003576 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3577 T.getOpenLocation(), ProtoIdLoc,
3578 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00003579}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003580
3581/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003582/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003583ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003584 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003585
Chris Lattner197a3012008-08-05 06:19:09 +00003586 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003587 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3588
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003589 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003590 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003591
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003592 BalancedDelimiterTracker T(*this, tok::l_paren);
3593 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003594 bool HasOptionalParen = Tok.is(tok::l_paren);
3595 if (HasOptionalParen)
3596 ConsumeParen();
3597
Douglas Gregor67c692c2010-08-26 15:07:07 +00003598 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003599 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003600 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003601 return ExprError();
3602 }
3603
Chris Lattner4f472a32009-04-11 18:13:45 +00003604 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003605 if (!SelIdent && // missing selector name.
3606 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003607 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003608
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003609 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003610
Steve Naroff152dd812007-12-05 22:21:29 +00003611 unsigned nColons = 0;
3612 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003613 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003614 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003615 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003616 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003617 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3618 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003619 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003620
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003621 if (Tok.is(tok::r_paren))
3622 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003623
3624 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003625 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003626 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003627 return ExprError();
3628 }
3629
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003630 // Check for another keyword selector.
3631 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003632 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003633 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003634 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003635 break;
3636 }
Steve Naroff152dd812007-12-05 22:21:29 +00003637 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003638 if (HasOptionalParen && Tok.is(tok::r_paren))
3639 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003640 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003641 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003642 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3643 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003644 T.getCloseLocation(),
3645 !HasOptionalParen);
Eugene Zelenko1ced5092016-02-12 22:53:10 +00003646}
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003647
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003648void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3649 // MCDecl might be null due to error in method or c-function prototype, etc.
3650 Decl *MCDecl = LM.D;
3651 bool skip = MCDecl &&
3652 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3653 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3654 if (skip)
3655 return;
3656
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003657 // Save the current token position.
3658 SourceLocation OrigLoc = Tok.getLocation();
3659
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003660 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
3661 // Append the current token at the end of the new token stream so that it
3662 // doesn't get lost.
3663 LM.Toks.push_back(Tok);
David Blaikie2eabcc92016-02-09 18:52:09 +00003664 PP.EnterTokenStream(LM.Toks, true);
3665
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003666 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003667 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003668
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003669 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3670 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003671 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003672 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003673 parseMethod
3674 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
3675 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003676
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003677 // Tell the actions module that we have entered a method or c-function definition
3678 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003679 if (parseMethod)
3680 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3681 else
3682 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003683 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003684 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003685 else {
3686 if (Tok.is(tok::colon))
3687 ParseConstructorInitializer(MCDecl);
Akira Hatanakabd59b4892016-04-18 18:19:45 +00003688 else
3689 Actions.ActOnDefaultCtorInitializers(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003690 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003691 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00003692
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003693 if (Tok.getLocation() != OrigLoc) {
3694 // Due to parsing error, we either went over the cached tokens or
3695 // there are still cached tokens left. If it's the latter case skip the
3696 // leftover tokens.
3697 // Since this is an uncommon situation that should be avoided, use the
3698 // expensive isBeforeInTranslationUnit call.
3699 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3700 OrigLoc))
3701 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3702 ConsumeAnyToken();
3703 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003704}