blob: 503c551200dc38888f86f3d6bb095c6c4f674d2b [file] [log] [blame]
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerda59c2f2006-11-05 02:08:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
John McCall8b0666c2010-08-20 18:27:03 +000014#include "clang/Parse/Parser.h"
Douglas Gregore9bba4f2010-09-15 14:51:05 +000015#include "RAIIObjectsForParser.h"
Douglas Gregor813a0662015-06-19 18:14:38 +000016#include "clang/AST/ASTContext.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000017#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/Scope.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000022#include "llvm/ADT/SmallVector.h"
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000024using namespace clang;
25
Nico Weber04e213b2013-04-03 17:36:11 +000026/// Skips attributes after an Objective-C @ directive. Emits a diagnostic.
Nico Weber69a79142013-04-04 00:15:10 +000027void Parser::MaybeSkipAttributes(tok::ObjCKeywordKind Kind) {
Nico Weber04e213b2013-04-03 17:36:11 +000028 ParsedAttributes attrs(AttrFactory);
29 if (Tok.is(tok::kw___attribute)) {
Nico Weber69a79142013-04-04 00:15:10 +000030 if (Kind == tok::objc_interface || Kind == tok::objc_protocol)
31 Diag(Tok, diag::err_objc_postfix_attribute_hint)
32 << (Kind == tok::objc_protocol);
33 else
34 Diag(Tok, diag::err_objc_postfix_attribute);
Nico Weber04e213b2013-04-03 17:36:11 +000035 ParseGNUAttributes(attrs);
36 }
37}
Chris Lattnerda59c2f2006-11-05 02:08:13 +000038
Chris Lattner3a907162008-12-08 21:53:24 +000039/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000040/// external-declaration: [C99 6.9]
41/// [OBJC] objc-class-definition
Steve Naroffe0933392007-10-29 21:39:29 +000042/// [OBJC] objc-class-declaration
43/// [OBJC] objc-alias-declaration
44/// [OBJC] objc-protocol-definition
45/// [OBJC] objc-method-definition
46/// [OBJC] '@' 'end'
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000047Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000048 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000049
Douglas Gregorf48706c2009-12-07 09:27:33 +000050 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000051 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000052 cutOffParsing();
53 return DeclGroupPtrTy();
Douglas Gregorf48706c2009-12-07 09:27:33 +000054 }
Craig Topper161e4db2014-05-21 06:02:52 +000055
56 Decl *SingleDecl = nullptr;
Steve Naroff7c348172007-08-23 18:16:40 +000057 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000058 case tok::objc_class:
59 return ParseObjCAtClassDeclaration(AtLoc);
John McCall53fa7142010-12-24 02:08:15 +000060 case tok::objc_interface: {
John McCall084e83d2011-03-24 11:26:52 +000061 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000062 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
63 break;
John McCall53fa7142010-12-24 02:08:15 +000064 }
65 case tok::objc_protocol: {
John McCall084e83d2011-03-24 11:26:52 +000066 ParsedAttributes attrs(AttrFactory);
Douglas Gregorf6102672012-01-01 21:23:57 +000067 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall53fa7142010-12-24 02:08:15 +000068 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000069 case tok::objc_implementation:
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +000070 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000071 case tok::objc_end:
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +000072 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000073 case tok::objc_compatibility_alias:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000074 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
75 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000076 case tok::objc_synthesize:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000077 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
78 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000079 case tok::objc_dynamic:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000080 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
81 break;
Douglas Gregorc50d4922012-12-11 22:11:52 +000082 case tok::objc_import:
Sean Callanan87596492014-12-09 23:47:56 +000083 if (getLangOpts().Modules || getLangOpts().DebuggerSupport)
Douglas Gregor0bf886d2012-01-03 18:24:14 +000084 return ParseModuleImport(AtLoc);
Fariborz Jahaniana773d082014-03-26 22:02:43 +000085 Diag(AtLoc, diag::err_atimport);
86 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000087 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerce90ef52008-08-23 02:02:23 +000088 default:
89 Diag(AtLoc, diag::err_unexpected_at);
90 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000091 SingleDecl = nullptr;
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000092 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000093 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000094 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000095}
96
Richard Smith3df3f1d2015-11-03 01:19:56 +000097/// Class to handle popping type parameters when leaving the scope.
98class Parser::ObjCTypeParamListScope {
99 Sema &Actions;
100 Scope *S;
101 ObjCTypeParamList *Params;
102public:
103 ObjCTypeParamListScope(Sema &Actions, Scope *S)
104 : Actions(Actions), S(S), Params(nullptr) {}
105 ~ObjCTypeParamListScope() {
106 leave();
107 }
108 void enter(ObjCTypeParamList *P) {
109 assert(!Params);
110 Params = P;
111 }
112 void leave() {
113 if (Params)
114 Actions.popObjCTypeParamList(S, Params);
115 Params = nullptr;
116 }
117};
118
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000119///
Mike Stump11289f42009-09-09 15:08:12 +0000120/// objc-class-declaration:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000121/// '@' 'class' objc-class-forward-decl (',' objc-class-forward-decl)* ';'
122///
123/// objc-class-forward-decl:
124/// identifier objc-type-parameter-list[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000125///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000126Parser::DeclGroupPtrTy
127Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000128 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000129 SmallVector<IdentifierInfo *, 8> ClassNames;
130 SmallVector<SourceLocation, 8> ClassLocs;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000131 SmallVector<ObjCTypeParamList *, 8> ClassTypeParams;
Mike Stump11289f42009-09-09 15:08:12 +0000132
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000133 while (1) {
Nico Weber69a79142013-04-04 00:15:10 +0000134 MaybeSkipAttributes(tok::objc_class);
Chris Lattner0ef13522007-10-09 17:51:17 +0000135 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000136 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000137 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000138 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000139 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000140 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +0000141 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000142 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000143
Douglas Gregor85f3f952015-07-07 03:57:15 +0000144 // Parse the optional objc-type-parameter-list.
145 ObjCTypeParamList *TypeParams = nullptr;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000146 if (Tok.is(tok::less))
Douglas Gregor85f3f952015-07-07 03:57:15 +0000147 TypeParams = parseObjCTypeParamList();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000148 ClassTypeParams.push_back(TypeParams);
Alp Toker383d2c42014-01-01 03:08:43 +0000149 if (!TryConsumeToken(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000150 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000151 }
Mike Stump11289f42009-09-09 15:08:12 +0000152
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000153 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +0000154 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@class"))
Craig Topper161e4db2014-05-21 06:02:52 +0000155 return Actions.ConvertDeclToDeclGroup(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000156
Ted Kremeneka26da852009-11-17 23:12:20 +0000157 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
158 ClassLocs.data(),
Douglas Gregor85f3f952015-07-07 03:57:15 +0000159 ClassTypeParams,
Ted Kremeneka26da852009-11-17 23:12:20 +0000160 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000161}
162
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000163void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
164{
165 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
166 if (ock == Sema::OCK_None)
167 return;
168
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000169 Decl *Decl = Actions.getObjCDeclContext();
170 if (CurParsedObjCImpl) {
171 CurParsedObjCImpl->finish(AtLoc);
172 } else {
173 Actions.ActOnAtEnd(getCurScope(), AtLoc);
174 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000175 Diag(AtLoc, diag::err_objc_missing_end)
176 << FixItHint::CreateInsertion(AtLoc, "@end\n");
177 if (Decl)
178 Diag(Decl->getLocStart(), diag::note_objc_container_start)
179 << (int) ock;
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000180}
181
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000182///
183/// objc-interface:
184/// objc-class-interface-attributes[opt] objc-class-interface
185/// objc-category-interface
186///
187/// objc-class-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000188/// '@' 'interface' identifier objc-type-parameter-list[opt]
189/// objc-superclass[opt] objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000190/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000191/// objc-interface-decl-list
192/// @end
193///
194/// objc-category-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000195/// '@' 'interface' identifier objc-type-parameter-list[opt]
196/// '(' identifier[opt] ')' objc-protocol-refs[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000197/// objc-interface-decl-list
198/// @end
199///
200/// objc-superclass:
Douglas Gregore9d95f12015-07-07 03:57:35 +0000201/// ':' identifier objc-type-arguments[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000202///
203/// objc-class-interface-attributes:
204/// __attribute__((visibility("default")))
205/// __attribute__((visibility("hidden")))
206/// __attribute__((deprecated))
207/// __attribute__((unavailable))
208/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardacfbe9e2012-04-06 18:12:22 +0000209/// __attribute__((objc_root_class))
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000210///
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000211Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000212 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000213 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000214 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000215 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000216 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000217
Douglas Gregor49c22a72009-11-18 16:26:39 +0000218 // Code completion after '@interface'.
219 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000220 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000221 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000222 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000223 }
224
Nico Weber69a79142013-04-04 00:15:10 +0000225 MaybeSkipAttributes(tok::objc_interface);
Nico Weber04e213b2013-04-03 17:36:11 +0000226
Chris Lattner0ef13522007-10-09 17:51:17 +0000227 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000228 Diag(Tok, diag::err_expected)
229 << tok::identifier; // missing class or category name.
Craig Topper161e4db2014-05-21 06:02:52 +0000230 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000231 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000232
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000233 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000234 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000235 SourceLocation nameLoc = ConsumeToken();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000236
237 // Parse the objc-type-parameter-list or objc-protocol-refs. For the latter
238 // case, LAngleLoc will be valid and ProtocolIdents will capture the
239 // protocol references (that have not yet been resolved).
240 SourceLocation LAngleLoc, EndProtoLoc;
241 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
242 ObjCTypeParamList *typeParameterList = nullptr;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000243 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
244 if (Tok.is(tok::less))
245 typeParameterList = parseObjCTypeParamListOrProtocolRefs(
246 typeParamScope, LAngleLoc, ProtocolIdents, EndProtoLoc);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000247
248 if (Tok.is(tok::l_paren) &&
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000249 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000250
251 BalancedDelimiterTracker T(*this, tok::l_paren);
252 T.consumeOpen();
253
254 SourceLocation categoryLoc;
Craig Topper161e4db2014-05-21 06:02:52 +0000255 IdentifierInfo *categoryId = nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000256 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000257 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000258 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000259 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000260 }
261
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000262 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000263 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000264 categoryId = Tok.getIdentifierInfo();
265 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000266 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000267 else if (!getLangOpts().ObjC2) {
Alp Tokerec543272013-12-24 09:48:30 +0000268 Diag(Tok, diag::err_expected)
269 << tok::identifier; // missing category name.
Craig Topper161e4db2014-05-21 06:02:52 +0000270 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000271 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000272
273 T.consumeClose();
274 if (T.getCloseLocation().isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000275 return nullptr;
276
Douglas Gregor0c254a02011-09-23 19:19:41 +0000277 if (!attrs.empty()) { // categories don't support attributes.
278 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
279 attrs.clear();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000280 }
Douglas Gregor0c254a02011-09-23 19:19:41 +0000281
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000282 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000283 assert(LAngleLoc.isInvalid() && "Cannot have already parsed protocols");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000284 SmallVector<Decl *, 8> ProtocolRefs;
285 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000286 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000287 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000288 LAngleLoc, EndProtoLoc,
289 /*consumeLastToken=*/true))
Craig Topper161e4db2014-05-21 06:02:52 +0000290 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000291
John McCall48871652010-08-21 09:40:31 +0000292 Decl *CategoryType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000293 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000294 nameId, nameLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000295 typeParameterList,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000296 categoryId, categoryLoc,
297 ProtocolRefs.data(),
298 ProtocolRefs.size(),
299 ProtocolLocs.data(),
300 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000301
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000302 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000303 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000304
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000305 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000306
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000307 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000308 }
309 // Parse a class interface.
Craig Topper161e4db2014-05-21 06:02:52 +0000310 IdentifierInfo *superClassId = nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000311 SourceLocation superClassLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000312 SourceLocation typeArgsLAngleLoc;
313 SmallVector<ParsedType, 4> typeArgs;
314 SourceLocation typeArgsRAngleLoc;
315 SmallVector<Decl *, 4> protocols;
316 SmallVector<SourceLocation, 4> protocolLocs;
Chris Lattner0ef13522007-10-09 17:51:17 +0000317 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000318 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000319
320 // Code completion of superclass names.
321 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000322 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000323 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000324 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000325 }
326
Chris Lattner0ef13522007-10-09 17:51:17 +0000327 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000328 Diag(Tok, diag::err_expected)
329 << tok::identifier; // missing super class name.
Craig Topper161e4db2014-05-21 06:02:52 +0000330 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000331 }
332 superClassId = Tok.getIdentifierInfo();
333 superClassLoc = ConsumeToken();
Douglas Gregore9d95f12015-07-07 03:57:35 +0000334
335 // Type arguments for the superclass or protocol conformances.
336 if (Tok.is(tok::less)) {
Douglas Gregor10dc9d82015-07-07 03:58:28 +0000337 parseObjCTypeArgsOrProtocolQualifiers(ParsedType(),
338 typeArgsLAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000339 typeArgs,
340 typeArgsRAngleLoc,
341 LAngleLoc,
342 protocols,
343 protocolLocs,
344 EndProtoLoc,
345 /*consumeLastToken=*/true,
Douglas Gregore9d95f12015-07-07 03:57:35 +0000346 /*warnOnIncompleteProtocols=*/true);
347 }
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000348 }
Douglas Gregor85f3f952015-07-07 03:57:15 +0000349
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000350 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000351 if (LAngleLoc.isValid()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000352 if (!ProtocolIdents.empty()) {
353 // We already parsed the protocols named when we thought we had a
354 // type parameter list. Translate them into actual protocol references.
355 for (const auto &pair : ProtocolIdents) {
356 protocolLocs.push_back(pair.second);
357 }
358 Actions.FindProtocolDeclaration(/*WarnOnDeclarations=*/true,
359 /*ForObjCContainer=*/true,
Craig Toppera9247eb2015-10-22 04:59:56 +0000360 ProtocolIdents, protocols);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000361 }
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000362 } else if (protocols.empty() && Tok.is(tok::less) &&
363 ParseObjCProtocolReferences(protocols, protocolLocs, true, true,
364 LAngleLoc, EndProtoLoc,
365 /*consumeLastToken=*/true)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000366 return nullptr;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000367 }
Mike Stump11289f42009-09-09 15:08:12 +0000368
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000369 if (Tok.isNot(tok::less))
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000370 Actions.ActOnTypedefedProtocols(protocols, superClassId, superClassLoc);
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000371
John McCall48871652010-08-21 09:40:31 +0000372 Decl *ClsType =
Douglas Gregore9d95f12015-07-07 03:57:35 +0000373 Actions.ActOnStartClassInterface(getCurScope(), AtLoc, nameId, nameLoc,
374 typeParameterList, superClassId,
375 superClassLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000376 typeArgs,
377 SourceRange(typeArgsLAngleLoc,
378 typeArgsRAngleLoc),
379 protocols.data(), protocols.size(),
380 protocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000381 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000382
Chris Lattner0ef13522007-10-09 17:51:17 +0000383 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000384 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000385
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000386 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000387
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000388 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000389}
390
Douglas Gregor813a0662015-06-19 18:14:38 +0000391/// Add an attribute for a context-sensitive type nullability to the given
392/// declarator.
393static void addContextSensitiveTypeNullability(Parser &P,
394 Declarator &D,
395 NullabilityKind nullability,
396 SourceLocation nullabilityLoc,
397 bool &addedToDeclSpec) {
398 // Create the attribute.
399 auto getNullabilityAttr = [&]() -> AttributeList * {
Douglas Gregorbec595a2015-06-19 18:27:45 +0000400 return D.getAttributePool().create(
401 P.getNullabilityKeyword(nullability),
402 SourceRange(nullabilityLoc),
403 nullptr, SourceLocation(),
404 nullptr, 0,
405 AttributeList::AS_ContextSensitiveKeyword);
Douglas Gregor813a0662015-06-19 18:14:38 +0000406 };
407
408 if (D.getNumTypeObjects() > 0) {
409 // Add the attribute to the declarator chunk nearest the declarator.
410 auto nullabilityAttr = getNullabilityAttr();
411 DeclaratorChunk &chunk = D.getTypeObject(0);
412 nullabilityAttr->setNext(chunk.getAttrListRef());
413 chunk.getAttrListRef() = nullabilityAttr;
414 } else if (!addedToDeclSpec) {
415 // Otherwise, just put it on the declaration specifiers (if one
416 // isn't there already).
417 D.getMutableDeclSpec().addAttributes(getNullabilityAttr());
418 addedToDeclSpec = true;
419 }
420}
421
Douglas Gregor85f3f952015-07-07 03:57:15 +0000422/// Parse an Objective-C type parameter list, if present, or capture
423/// the locations of the protocol identifiers for a list of protocol
424/// references.
425///
426/// objc-type-parameter-list:
427/// '<' objc-type-parameter (',' objc-type-parameter)* '>'
428///
429/// objc-type-parameter:
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000430/// objc-type-parameter-variance? identifier objc-type-parameter-bound[opt]
Douglas Gregor85f3f952015-07-07 03:57:15 +0000431///
432/// objc-type-parameter-bound:
433/// ':' type-name
434///
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000435/// objc-type-parameter-variance:
436/// '__covariant'
437/// '__contravariant'
438///
Douglas Gregor85f3f952015-07-07 03:57:15 +0000439/// \param lAngleLoc The location of the starting '<'.
440///
441/// \param protocolIdents Will capture the list of identifiers, if the
442/// angle brackets contain a list of protocol references rather than a
443/// type parameter list.
444///
445/// \param rAngleLoc The location of the ending '>'.
446ObjCTypeParamList *Parser::parseObjCTypeParamListOrProtocolRefs(
Richard Smith3df3f1d2015-11-03 01:19:56 +0000447 ObjCTypeParamListScope &Scope, SourceLocation &lAngleLoc,
448 SmallVectorImpl<IdentifierLocPair> &protocolIdents,
449 SourceLocation &rAngleLoc, bool mayBeProtocolList) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000450 assert(Tok.is(tok::less) && "Not at the beginning of a type parameter list");
451
452 // Within the type parameter list, don't treat '>' as an operator.
453 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
454
455 // Local function to "flush" the protocol identifiers, turning them into
456 // type parameters.
457 SmallVector<Decl *, 4> typeParams;
458 auto makeProtocolIdentsIntoTypeParameters = [&]() {
Douglas Gregore83b9562015-07-07 03:57:53 +0000459 unsigned index = 0;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000460 for (const auto &pair : protocolIdents) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000461 DeclResult typeParam = Actions.actOnObjCTypeParam(
462 getCurScope(),
463 ObjCTypeParamVariance::Invariant,
464 SourceLocation(),
465 index++,
466 pair.first,
467 pair.second,
468 SourceLocation(),
469 ParsedType());
Douglas Gregor85f3f952015-07-07 03:57:15 +0000470 if (typeParam.isUsable())
471 typeParams.push_back(typeParam.get());
472 }
473
474 protocolIdents.clear();
475 mayBeProtocolList = false;
476 };
477
478 bool invalid = false;
479 lAngleLoc = ConsumeToken();
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000480
Douglas Gregor85f3f952015-07-07 03:57:15 +0000481 do {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000482 // Parse the variance, if any.
483 SourceLocation varianceLoc;
484 ObjCTypeParamVariance variance = ObjCTypeParamVariance::Invariant;
485 if (Tok.is(tok::kw___covariant) || Tok.is(tok::kw___contravariant)) {
486 variance = Tok.is(tok::kw___covariant)
487 ? ObjCTypeParamVariance::Covariant
488 : ObjCTypeParamVariance::Contravariant;
489 varianceLoc = ConsumeToken();
490
491 // Once we've seen a variance specific , we know this is not a
492 // list of protocol references.
493 if (mayBeProtocolList) {
494 // Up until now, we have been queuing up parameters because they
495 // might be protocol references. Turn them into parameters now.
496 makeProtocolIdentsIntoTypeParameters();
497 }
498 }
499
Douglas Gregor85f3f952015-07-07 03:57:15 +0000500 // Parse the identifier.
501 if (!Tok.is(tok::identifier)) {
502 // Code completion.
503 if (Tok.is(tok::code_completion)) {
504 // FIXME: If these aren't protocol references, we'll need different
505 // completions.
Craig Topper883dd332015-12-24 23:58:11 +0000506 Actions.CodeCompleteObjCProtocolReferences(protocolIdents);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000507 cutOffParsing();
508
509 // FIXME: Better recovery here?.
510 return nullptr;
511 }
512
513 Diag(Tok, diag::err_objc_expected_type_parameter);
514 invalid = true;
515 break;
516 }
517
518 IdentifierInfo *paramName = Tok.getIdentifierInfo();
519 SourceLocation paramLoc = ConsumeToken();
520
521 // If there is a bound, parse it.
522 SourceLocation colonLoc;
523 TypeResult boundType;
524 if (TryConsumeToken(tok::colon, colonLoc)) {
525 // Once we've seen a bound, we know this is not a list of protocol
526 // references.
527 if (mayBeProtocolList) {
528 // Up until now, we have been queuing up parameters because they
529 // might be protocol references. Turn them into parameters now.
530 makeProtocolIdentsIntoTypeParameters();
531 }
532
533 // type-name
534 boundType = ParseTypeName();
535 if (boundType.isInvalid())
536 invalid = true;
537 } else if (mayBeProtocolList) {
538 // If this could still be a protocol list, just capture the identifier.
539 // We don't want to turn it into a parameter.
540 protocolIdents.push_back(std::make_pair(paramName, paramLoc));
541 continue;
542 }
543
544 // Create the type parameter.
545 DeclResult typeParam = Actions.actOnObjCTypeParam(getCurScope(),
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000546 variance,
547 varianceLoc,
Douglas Gregore83b9562015-07-07 03:57:53 +0000548 typeParams.size(),
Douglas Gregor85f3f952015-07-07 03:57:15 +0000549 paramName,
550 paramLoc,
551 colonLoc,
552 boundType.isUsable()
553 ? boundType.get()
554 : ParsedType());
555 if (typeParam.isUsable())
556 typeParams.push_back(typeParam.get());
557 } while (TryConsumeToken(tok::comma));
558
559 // Parse the '>'.
560 if (invalid) {
561 SkipUntil(tok::greater, tok::at, StopBeforeMatch);
562 if (Tok.is(tok::greater))
563 ConsumeToken();
564 } else if (ParseGreaterThanInTemplateList(rAngleLoc,
565 /*ConsumeLastToken=*/true,
566 /*ObjCGenericList=*/true)) {
567 Diag(lAngleLoc, diag::note_matching) << "'<'";
568 SkipUntil({tok::greater, tok::greaterequal, tok::at, tok::minus,
569 tok::minus, tok::plus, tok::colon, tok::l_paren, tok::l_brace,
570 tok::comma, tok::semi },
571 StopBeforeMatch);
572 if (Tok.is(tok::greater))
573 ConsumeToken();
574 }
575
576 if (mayBeProtocolList) {
577 // A type parameter list must be followed by either a ':' (indicating the
578 // presence of a superclass) or a '(' (indicating that this is a category
579 // or extension). This disambiguates between an objc-type-parameter-list
580 // and a objc-protocol-refs.
581 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_paren)) {
582 // Returning null indicates that we don't have a type parameter list.
583 // The results the caller needs to handle the protocol references are
584 // captured in the reference parameters already.
585 return nullptr;
586 }
587
588 // We have a type parameter list that looks like a list of protocol
589 // references. Turn that parameter list into type parameters.
590 makeProtocolIdentsIntoTypeParameters();
591 }
592
Richard Smith3df3f1d2015-11-03 01:19:56 +0000593 // Form the type parameter list and enter its scope.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000594 ObjCTypeParamList *list = Actions.actOnObjCTypeParamList(
595 getCurScope(),
596 lAngleLoc,
597 typeParams,
598 rAngleLoc);
Richard Smith3df3f1d2015-11-03 01:19:56 +0000599 Scope.enter(list);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000600
601 // Clear out the angle locations; they're used by the caller to indicate
602 // whether there are any protocol references.
603 lAngleLoc = SourceLocation();
604 rAngleLoc = SourceLocation();
Akira Hatanaka8ebd5802015-12-16 06:25:38 +0000605 return invalid ? nullptr : list;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000606}
607
608/// Parse an objc-type-parameter-list.
609ObjCTypeParamList *Parser::parseObjCTypeParamList() {
610 SourceLocation lAngleLoc;
611 SmallVector<IdentifierLocPair, 1> protocolIdents;
612 SourceLocation rAngleLoc;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000613
614 ObjCTypeParamListScope Scope(Actions, getCurScope());
615 return parseObjCTypeParamListOrProtocolRefs(Scope, lAngleLoc, protocolIdents,
616 rAngleLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000617 /*mayBeProtocolList=*/false);
618}
619
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000620/// objc-interface-decl-list:
621/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000622/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000623/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000624/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000625/// objc-interface-decl-list declaration
626/// objc-interface-decl-list ';'
627///
Steve Naroff99264b42007-08-22 16:35:03 +0000628/// objc-method-requirement: [OBJC2]
629/// @required
630/// @optional
631///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000632void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
633 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000634 SmallVector<Decl *, 32> allMethods;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000635 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000636 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000637
Ted Kremenekc7c64312010-01-07 01:20:12 +0000638 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000639
Steve Naroff99264b42007-08-22 16:35:03 +0000640 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000641 // If this is a method prototype, parse it.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000642 if (Tok.isOneOf(tok::minus, tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000643 if (Decl *methodPrototype =
644 ParseObjCMethodPrototype(MethodImplKind, false))
645 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000646 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
647 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000648 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
649 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000650 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000651 if (Tok.is(tok::semi))
652 ConsumeToken();
653 }
Steve Naroff99264b42007-08-22 16:35:03 +0000654 continue;
655 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000656 if (Tok.is(tok::l_paren)) {
657 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000658 ParseObjCMethodDecl(Tok.getLocation(),
659 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000660 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000661 continue;
662 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000663 // Ignore excess semicolons.
664 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000665 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000666 continue;
667 }
Mike Stump11289f42009-09-09 15:08:12 +0000668
Chris Lattnerda9fb152008-10-20 06:10:06 +0000669 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000670 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000671 break;
Mike Stump11289f42009-09-09 15:08:12 +0000672
Douglas Gregorf1934162010-01-13 21:24:21 +0000673 // Code completion within an Objective-C interface.
674 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000675 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000676 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000677 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000678 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000679 }
680
Chris Lattner038a3e32008-10-20 05:46:22 +0000681 // If we don't have an @ directive, parse it as a function definition.
682 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000683 // The code below does not consume '}'s because it is afraid of eating the
684 // end of a namespace. Because of the way this code is structured, an
685 // erroneous r_brace would cause an infinite loop if not handled here.
686 if (Tok.is(tok::r_brace))
687 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000688 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000689 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000690 continue;
691 }
Mike Stump11289f42009-09-09 15:08:12 +0000692
Chris Lattner038a3e32008-10-20 05:46:22 +0000693 // Otherwise, we have an @ directive, eat the @.
694 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000695 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000696 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000697 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000698 }
699
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000700 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000701
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000702 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000703 AtEnd.setBegin(AtLoc);
704 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000705 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000706 } else if (DirectiveKind == tok::objc_not_keyword) {
707 Diag(Tok, diag::err_objc_unknown_at);
708 SkipUntil(tok::semi);
709 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000710 }
Mike Stump11289f42009-09-09 15:08:12 +0000711
Chris Lattnerda9fb152008-10-20 06:10:06 +0000712 // Eat the identifier.
713 ConsumeToken();
714
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000715 switch (DirectiveKind) {
716 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000717 // FIXME: If someone forgets an @end on a protocol, this loop will
718 // continue to eat up tons of stuff and spew lots of nonsense errors. It
719 // would probably be better to bail out if we saw an @class or @interface
720 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000721 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000722 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000723 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000724 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000725
726 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000727 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000728 Diag(AtLoc, diag::err_objc_missing_end)
729 << FixItHint::CreateInsertion(AtLoc, "@end\n");
730 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
731 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000732 ConsumeToken();
733 break;
734
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000735 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000736 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000737 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000738 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000739 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000740 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000741 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000742 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000743 break;
Mike Stump11289f42009-09-09 15:08:12 +0000744
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000745 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000746 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000747 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000748
Chris Lattner038a3e32008-10-20 05:46:22 +0000749 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000750 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000751 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000752 if (Tok.is(tok::l_paren)) {
753 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000754 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000755 }
Mike Stump11289f42009-09-09 15:08:12 +0000756
Douglas Gregor813a0662015-06-19 18:14:38 +0000757 bool addedToDeclSpec = false;
Benjamin Kramera39beb92014-09-03 11:06:10 +0000758 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
759 if (FD.D.getIdentifier() == nullptr) {
760 Diag(AtLoc, diag::err_objc_property_requires_field_name)
761 << FD.D.getSourceRange();
762 return;
763 }
764 if (FD.BitfieldSize) {
765 Diag(AtLoc, diag::err_objc_property_bitfield)
766 << FD.D.getSourceRange();
767 return;
768 }
769
Douglas Gregor813a0662015-06-19 18:14:38 +0000770 // Map a nullability property attribute to a context-sensitive keyword
771 // attribute.
772 if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
773 addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
774 OCDS.getNullabilityLoc(),
775 addedToDeclSpec);
776
Benjamin Kramera39beb92014-09-03 11:06:10 +0000777 // Install the property declarator into interfaceDecl.
778 IdentifierInfo *SelName =
779 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
780
781 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
782 IdentifierInfo *SetterName = OCDS.getSetterName();
783 Selector SetterSel;
784 if (SetterName)
785 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
786 else
787 SetterSel = SelectorTable::constructSetterSelector(
788 PP.getIdentifierTable(), PP.getSelectorTable(),
789 FD.D.getIdentifier());
Benjamin Kramera39beb92014-09-03 11:06:10 +0000790 Decl *Property = Actions.ActOnProperty(
791 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000792 MethodImplKind);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000793
794 FD.complete(Property);
795 };
John McCallcfefb6d2009-11-03 02:38:08 +0000796
Chris Lattner038a3e32008-10-20 05:46:22 +0000797 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000798 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000799 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000800
John McCall405988b2011-03-26 01:53:26 +0000801 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000802 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000803 }
Steve Naroff99264b42007-08-22 16:35:03 +0000804 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000805
806 // We break out of the big loop in two cases: when we see @end or when we see
807 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000808 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000809 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000810 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000811 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000812 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000813 } else {
814 Diag(Tok, diag::err_objc_missing_end)
815 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
816 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
817 << (int) Actions.getObjCContainerKind();
818 AtEnd.setBegin(Tok.getLocation());
819 AtEnd.setEnd(Tok.getLocation());
820 }
Mike Stump11289f42009-09-09 15:08:12 +0000821
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000822 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000823 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000824 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000825}
826
Douglas Gregor813a0662015-06-19 18:14:38 +0000827/// Diagnose redundant or conflicting nullability information.
828static void diagnoseRedundantPropertyNullability(Parser &P,
829 ObjCDeclSpec &DS,
830 NullabilityKind nullability,
831 SourceLocation nullabilityLoc){
832 if (DS.getNullability() == nullability) {
833 P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000834 << DiagNullabilityKind(nullability, true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000835 << SourceRange(DS.getNullabilityLoc());
836 return;
837 }
838
839 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000840 << DiagNullabilityKind(nullability, true)
841 << DiagNullabilityKind(DS.getNullability(), true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000842 << SourceRange(DS.getNullabilityLoc());
843}
844
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000845/// Parse property attribute declarations.
846///
847/// property-attr-decl: '(' property-attrlist ')'
848/// property-attrlist:
849/// property-attribute
850/// property-attrlist ',' property-attribute
851/// property-attribute:
852/// getter '=' identifier
853/// setter '=' identifier ':'
854/// readonly
855/// readwrite
856/// assign
857/// retain
858/// copy
859/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000860/// atomic
861/// strong
862/// weak
863/// unsafe_unretained
Douglas Gregor813a0662015-06-19 18:14:38 +0000864/// nonnull
865/// nullable
866/// null_unspecified
Douglas Gregor849ebc22015-06-19 18:14:46 +0000867/// null_resettable
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000868///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000869void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000870 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000871 BalancedDelimiterTracker T(*this, tok::l_paren);
872 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000873
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000874 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000875 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000876 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000877 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000878 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000879 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000880
Chris Lattner76619232008-10-20 07:22:18 +0000881 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000882 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000883 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000884 return;
885 }
Mike Stump11289f42009-09-09 15:08:12 +0000886
Chris Lattner1db33542008-10-20 07:37:22 +0000887 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000888
Chris Lattner68e48682008-11-20 04:42:34 +0000889 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000890 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000891 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000892 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000893 else if (II->isStr("unsafe_unretained"))
894 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000895 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000896 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000897 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000898 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000899 else if (II->isStr("strong"))
900 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000901 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000902 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000903 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000904 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000905 else if (II->isStr("atomic"))
906 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000907 else if (II->isStr("weak"))
908 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000909 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000910 bool IsSetter = II->getNameStart()[0] == 's';
911
Chris Lattner825bca12008-10-20 07:39:53 +0000912 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000913 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
Craig Topper3110a5c2015-11-14 18:16:02 +0000914 diag::err_objc_expected_equal_for_getter;
Anders Carlssonfe15a782010-10-02 17:45:21 +0000915
Alp Toker383d2c42014-01-01 03:08:43 +0000916 if (ExpectAndConsume(tok::equal, DiagID)) {
917 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000918 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000919 }
Mike Stump11289f42009-09-09 15:08:12 +0000920
Douglas Gregorc8537c52009-11-19 07:41:15 +0000921 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000922 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000923 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000924 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000925 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000926 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000927 }
928
Anders Carlssonfe15a782010-10-02 17:45:21 +0000929 SourceLocation SelLoc;
930 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
931
932 if (!SelIdent) {
933 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
934 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000935 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000936 return;
937 }
Mike Stump11289f42009-09-09 15:08:12 +0000938
Anders Carlssonfe15a782010-10-02 17:45:21 +0000939 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000940 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000941 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000942
Alp Toker383d2c42014-01-01 03:08:43 +0000943 if (ExpectAndConsume(tok::colon,
944 diag::err_expected_colon_after_setter_name)) {
945 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000946 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000947 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000948 } else {
949 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000950 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000951 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000952 } else if (II->isStr("nonnull")) {
953 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
954 diagnoseRedundantPropertyNullability(*this, DS,
955 NullabilityKind::NonNull,
956 Tok.getLocation());
957 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
958 DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
959 } else if (II->isStr("nullable")) {
960 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
961 diagnoseRedundantPropertyNullability(*this, DS,
962 NullabilityKind::Nullable,
963 Tok.getLocation());
964 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
965 DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
966 } else if (II->isStr("null_unspecified")) {
967 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
968 diagnoseRedundantPropertyNullability(*this, DS,
969 NullabilityKind::Unspecified,
970 Tok.getLocation());
971 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
972 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000973 } else if (II->isStr("null_resettable")) {
974 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
975 diagnoseRedundantPropertyNullability(*this, DS,
976 NullabilityKind::Unspecified,
977 Tok.getLocation());
978 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
979 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
980
981 // Also set the null_resettable bit.
982 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
Chris Lattner825bca12008-10-20 07:39:53 +0000983 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000984 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000985 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000986 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000987 }
Mike Stump11289f42009-09-09 15:08:12 +0000988
Chris Lattner1db33542008-10-20 07:37:22 +0000989 if (Tok.isNot(tok::comma))
990 break;
Mike Stump11289f42009-09-09 15:08:12 +0000991
Chris Lattner1db33542008-10-20 07:37:22 +0000992 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000993 }
Mike Stump11289f42009-09-09 15:08:12 +0000994
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000995 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000996}
997
Steve Naroff09bf8152007-09-06 21:24:23 +0000998/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000999/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +00001000/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001001///
1002/// objc-instance-method: '-'
1003/// objc-class-method: '+'
1004///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001005/// objc-method-attributes: [OBJC2]
1006/// __attribute__((deprecated))
1007///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001008Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001009 bool MethodDefinition) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001010 assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +00001011
Mike Stump11289f42009-09-09 15:08:12 +00001012 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +00001013 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001014 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001015 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +00001016 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +00001017 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +00001018 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +00001019}
1020
1021/// objc-selector:
1022/// identifier
1023/// one of
1024/// enum struct union if else while do for switch case default
1025/// break continue return goto asm sizeof typeof __alignof
1026/// unsigned long const short volatile signed restrict _Complex
1027/// in out inout bycopy byref oneway int char float double void _Bool
1028///
Chris Lattner4f472a32009-04-11 18:13:45 +00001029IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +00001030
Chris Lattner5700fab2007-10-07 02:00:24 +00001031 switch (Tok.getKind()) {
1032 default:
Craig Topper161e4db2014-05-21 06:02:52 +00001033 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001034 case tok::ampamp:
1035 case tok::ampequal:
1036 case tok::amp:
1037 case tok::pipe:
1038 case tok::tilde:
1039 case tok::exclaim:
1040 case tok::exclaimequal:
1041 case tok::pipepipe:
1042 case tok::pipeequal:
1043 case tok::caret:
1044 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +00001045 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +00001046 if (isLetter(ThisTok[0])) {
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001047 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
1048 Tok.setKind(tok::identifier);
1049 SelectorLoc = ConsumeToken();
1050 return II;
1051 }
Craig Topper161e4db2014-05-21 06:02:52 +00001052 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001053 }
1054
Chris Lattner5700fab2007-10-07 02:00:24 +00001055 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001056 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +00001057 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001058 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001059 case tok::kw_break:
1060 case tok::kw_case:
1061 case tok::kw_catch:
1062 case tok::kw_char:
1063 case tok::kw_class:
1064 case tok::kw_const:
1065 case tok::kw_const_cast:
1066 case tok::kw_continue:
1067 case tok::kw_default:
1068 case tok::kw_delete:
1069 case tok::kw_do:
1070 case tok::kw_double:
1071 case tok::kw_dynamic_cast:
1072 case tok::kw_else:
1073 case tok::kw_enum:
1074 case tok::kw_explicit:
1075 case tok::kw_export:
1076 case tok::kw_extern:
1077 case tok::kw_false:
1078 case tok::kw_float:
1079 case tok::kw_for:
1080 case tok::kw_friend:
1081 case tok::kw_goto:
1082 case tok::kw_if:
1083 case tok::kw_inline:
1084 case tok::kw_int:
1085 case tok::kw_long:
1086 case tok::kw_mutable:
1087 case tok::kw_namespace:
1088 case tok::kw_new:
1089 case tok::kw_operator:
1090 case tok::kw_private:
1091 case tok::kw_protected:
1092 case tok::kw_public:
1093 case tok::kw_register:
1094 case tok::kw_reinterpret_cast:
1095 case tok::kw_restrict:
1096 case tok::kw_return:
1097 case tok::kw_short:
1098 case tok::kw_signed:
1099 case tok::kw_sizeof:
1100 case tok::kw_static:
1101 case tok::kw_static_cast:
1102 case tok::kw_struct:
1103 case tok::kw_switch:
1104 case tok::kw_template:
1105 case tok::kw_this:
1106 case tok::kw_throw:
1107 case tok::kw_true:
1108 case tok::kw_try:
1109 case tok::kw_typedef:
1110 case tok::kw_typeid:
1111 case tok::kw_typename:
1112 case tok::kw_typeof:
1113 case tok::kw_union:
1114 case tok::kw_unsigned:
1115 case tok::kw_using:
1116 case tok::kw_virtual:
1117 case tok::kw_void:
1118 case tok::kw_volatile:
1119 case tok::kw_wchar_t:
1120 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +00001121 case tok::kw__Bool:
1122 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001123 case tok::kw___alignof:
Richard Smithe301ba22015-11-11 02:02:15 +00001124 case tok::kw___auto_type:
Chris Lattner5700fab2007-10-07 02:00:24 +00001125 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001126 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +00001127 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +00001128 }
Steve Naroff99264b42007-08-22 16:35:03 +00001129}
1130
Fariborz Jahanian83615522008-01-02 22:54:34 +00001131/// objc-for-collection-in: 'in'
1132///
Fariborz Jahanian3622e592008-01-04 23:04:08 +00001133bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001134 // FIXME: May have to do additional look-ahead to only allow for
1135 // valid tokens following an 'in'; such as an identifier, unary operators,
1136 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001137 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +00001138 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +00001139}
1140
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001141/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +00001142/// qualifier list and builds their bitmask representation in the input
1143/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +00001144///
1145/// objc-type-qualifiers:
1146/// objc-type-qualifier
1147/// objc-type-qualifiers objc-type-qualifier
1148///
Douglas Gregor813a0662015-06-19 18:14:38 +00001149/// objc-type-qualifier:
1150/// 'in'
1151/// 'out'
1152/// 'inout'
1153/// 'oneway'
1154/// 'bycopy'
1155/// 'byref'
1156/// 'nonnull'
1157/// 'nullable'
1158/// 'null_unspecified'
1159///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001160void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001161 Declarator::TheContext Context) {
1162 assert(Context == Declarator::ObjCParameterContext ||
1163 Context == Declarator::ObjCResultContext);
1164
Chris Lattner61511e12007-12-12 06:56:32 +00001165 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +00001166 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +00001167 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +00001168 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001169 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +00001170 }
1171
Chris Lattner5e530bc2007-12-27 19:57:00 +00001172 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +00001173 return;
Mike Stump11289f42009-09-09 15:08:12 +00001174
Chris Lattner61511e12007-12-12 06:56:32 +00001175 const IdentifierInfo *II = Tok.getIdentifierInfo();
1176 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001177 if (II != ObjCTypeQuals[i] ||
1178 NextToken().is(tok::less) ||
1179 NextToken().is(tok::coloncolon))
Chris Lattner61511e12007-12-12 06:56:32 +00001180 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001181
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001182 ObjCDeclSpec::ObjCDeclQualifier Qual;
Douglas Gregor813a0662015-06-19 18:14:38 +00001183 NullabilityKind Nullability;
Chris Lattner61511e12007-12-12 06:56:32 +00001184 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +00001185 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001186 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
1187 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
1188 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
1189 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
1190 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
1191 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Douglas Gregor813a0662015-06-19 18:14:38 +00001192
1193 case objc_nonnull:
1194 Qual = ObjCDeclSpec::DQ_CSNullability;
1195 Nullability = NullabilityKind::NonNull;
1196 break;
1197
1198 case objc_nullable:
1199 Qual = ObjCDeclSpec::DQ_CSNullability;
1200 Nullability = NullabilityKind::Nullable;
1201 break;
1202
1203 case objc_null_unspecified:
1204 Qual = ObjCDeclSpec::DQ_CSNullability;
1205 Nullability = NullabilityKind::Unspecified;
1206 break;
Chris Lattner61511e12007-12-12 06:56:32 +00001207 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001208
1209 // FIXME: Diagnose redundant specifiers.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001210 DS.setObjCDeclQualifier(Qual);
Douglas Gregor813a0662015-06-19 18:14:38 +00001211 if (Qual == ObjCDeclSpec::DQ_CSNullability)
1212 DS.setNullability(Tok.getLocation(), Nullability);
1213
Chris Lattner61511e12007-12-12 06:56:32 +00001214 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001215 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +00001216 break;
1217 }
Mike Stump11289f42009-09-09 15:08:12 +00001218
Chris Lattner61511e12007-12-12 06:56:32 +00001219 // If this wasn't a recognized qualifier, bail out.
1220 if (II) return;
1221 }
1222}
1223
John McCalla55902b2011-10-01 09:56:14 +00001224/// Take all the decl attributes out of the given list and add
1225/// them to the given attribute set.
1226static void takeDeclAttributes(ParsedAttributes &attrs,
1227 AttributeList *list) {
1228 while (list) {
1229 AttributeList *cur = list;
1230 list = cur->getNext();
1231
1232 if (!cur->isUsedAsTypeAttr()) {
1233 // Clear out the next pointer. We're really completely
1234 // destroying the internal invariants of the declarator here,
1235 // but it doesn't matter because we're done with it.
Craig Topper161e4db2014-05-21 06:02:52 +00001236 cur->setNext(nullptr);
John McCalla55902b2011-10-01 09:56:14 +00001237 attrs.add(cur);
1238 }
1239 }
1240}
1241
1242/// takeDeclAttributes - Take all the decl attributes from the given
1243/// declarator and add them to the given list.
1244static void takeDeclAttributes(ParsedAttributes &attrs,
1245 Declarator &D) {
1246 // First, take ownership of all attributes.
1247 attrs.getPool().takeAllFrom(D.getAttributePool());
1248 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
1249
1250 // Now actually move the attributes over.
1251 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
1252 takeDeclAttributes(attrs, D.getAttributes());
1253 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
1254 takeDeclAttributes(attrs,
1255 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
1256}
1257
Chris Lattner61511e12007-12-12 06:56:32 +00001258/// objc-type-name:
1259/// '(' objc-type-qualifiers[opt] type-name ')'
1260/// '(' objc-type-qualifiers[opt] ')'
1261///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001262ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001263 Declarator::TheContext context,
1264 ParsedAttributes *paramAttrs) {
1265 assert(context == Declarator::ObjCParameterContext ||
1266 context == Declarator::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +00001267 assert((paramAttrs != nullptr) ==
1268 (context == Declarator::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +00001269
Chris Lattner0ef13522007-10-09 17:51:17 +00001270 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +00001271
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001272 BalancedDelimiterTracker T(*this, tok::l_paren);
1273 T.consumeOpen();
1274
Chris Lattner2ebb1782008-08-23 01:48:03 +00001275 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001276 ObjCDeclContextSwitch ObjCDC(*this);
1277
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001278 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001279 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +00001280
John McCallba7bf592010-08-24 05:47:05 +00001281 ParsedType Ty;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001282 if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
John McCalla55902b2011-10-01 09:56:14 +00001283 // Parse an abstract declarator.
1284 DeclSpec declSpec(AttrFactory);
1285 declSpec.setObjCQualifiers(&DS);
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001286 DeclSpecContext dsContext = DSC_normal;
1287 if (context == Declarator::ObjCResultContext)
1288 dsContext = DSC_objc_method_result;
1289 ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
Fariborz Jahanianb6499eb2012-05-29 21:52:45 +00001290 declSpec.SetRangeEnd(Tok.getLocation());
John McCalla55902b2011-10-01 09:56:14 +00001291 Declarator declarator(declSpec, context);
1292 ParseDeclarator(declarator);
1293
1294 // If that's not invalid, extract a type.
1295 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001296 // Map a nullability specifier to a context-sensitive keyword attribute.
1297 bool addedToDeclSpec = false;
1298 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1299 addContextSensitiveTypeNullability(*this, declarator,
1300 DS.getNullability(),
1301 DS.getNullabilityLoc(),
1302 addedToDeclSpec);
1303
John McCalla55902b2011-10-01 09:56:14 +00001304 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1305 if (!type.isInvalid())
1306 Ty = type.get();
1307
1308 // If we're parsing a parameter, steal all the decl attributes
1309 // and add them to the decl spec.
1310 if (context == Declarator::ObjCParameterContext)
1311 takeDeclAttributes(*paramAttrs, declarator);
1312 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001313 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001314
Steve Naroff90255b42008-10-21 14:15:04 +00001315 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001316 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001317 else if (Tok.getLocation() == TypeStartLoc) {
1318 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001319 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001320 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001321 } else {
1322 // Otherwise, we found *something*, but didn't get a ')' in the right
1323 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001324 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001325 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001326 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001327}
1328
1329/// objc-method-decl:
1330/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001331/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001332/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001333/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001334///
1335/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001336/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001337/// objc-keyword-selector objc-keyword-decl
1338///
1339/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001340/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1341/// objc-selector ':' objc-keyword-attributes[opt] identifier
1342/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1343/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001344///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001345/// objc-parmlist:
1346/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001347///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001348/// objc-parms:
1349/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001350///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001351/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001352/// , ...
1353///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001354/// objc-keyword-attributes: [OBJC2]
1355/// __attribute__((unused))
1356///
John McCall48871652010-08-21 09:40:31 +00001357Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001358 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001359 tok::ObjCKeywordKind MethodImplKind,
1360 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001361 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001362
Douglas Gregor636a61e2010-04-07 00:21:17 +00001363 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001364 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001365 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001366 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001367 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001368 }
1369
Chris Lattner2ebb1782008-08-23 01:48:03 +00001370 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001371 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001372 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001373 if (Tok.is(tok::l_paren))
Craig Topper161e4db2014-05-21 06:02:52 +00001374 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext,
1375 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001376
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001377 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001378 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001379 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001380 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001381
Douglas Gregor636a61e2010-04-07 00:21:17 +00001382 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001383 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001384 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001385 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001386 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001387 }
1388
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001389 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001390 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001391 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001392
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001393 // An unnamed colon is valid.
1394 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001395 Diag(Tok, diag::err_expected_selector_for_method)
1396 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001397 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001398 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001399 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001400 }
Mike Stump11289f42009-09-09 15:08:12 +00001401
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001402 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001403 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001404 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001405 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001406 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001407
Chris Lattner5700fab2007-10-07 02:00:24 +00001408 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001409 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001410 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001411 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001412 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001413 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001414 methodAttrs.getList(), MethodImplKind,
1415 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001416 PD.complete(Result);
1417 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001418 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001419
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001420 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001421 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001422 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001423 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1424 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001425
1426 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001427 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001428 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001429 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001430
Chris Lattner5700fab2007-10-07 02:00:24 +00001431 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001432 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001433 break;
Mike Stump11289f42009-09-09 15:08:12 +00001434
John McCallba7bf592010-08-24 05:47:05 +00001435 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001436 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001437 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1438 Declarator::ObjCParameterContext,
1439 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001440
Chris Lattner5700fab2007-10-07 02:00:24 +00001441 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001442 // Regardless, collect all the attributes we've parsed so far.
Craig Topper161e4db2014-05-21 06:02:52 +00001443 ArgInfo.ArgAttrs = nullptr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001444 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001445 MaybeParseGNUAttributes(paramAttrs);
1446 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001447 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001448
Douglas Gregor45879692010-07-08 23:37:41 +00001449 // Code completion for the next piece of the selector.
1450 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001451 KeyIdents.push_back(SelIdent);
1452 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1453 mType == tok::minus,
1454 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001455 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001456 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001457 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001458 }
1459
Chris Lattner0ef13522007-10-09 17:51:17 +00001460 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001461 Diag(Tok, diag::err_expected)
1462 << tok::identifier; // missing argument name.
Chris Lattner5700fab2007-10-07 02:00:24 +00001463 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001464 }
Mike Stump11289f42009-09-09 15:08:12 +00001465
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001466 ArgInfo.Name = Tok.getIdentifierInfo();
1467 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001468 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001469
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001470 ArgInfos.push_back(ArgInfo);
1471 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001472 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001473
John McCall084e83d2011-03-24 11:26:52 +00001474 // Make sure the attributes persist.
1475 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1476
Douglas Gregor95887f92010-07-08 23:20:03 +00001477 // Code completion for the next piece of the selector.
1478 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001479 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1480 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001481 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001482 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001483 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001484 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001485 }
1486
Chris Lattner5700fab2007-10-07 02:00:24 +00001487 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001488 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001489 if (!SelIdent && Tok.isNot(tok::colon))
1490 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001491 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001492 SourceLocation ColonLoc = Tok.getLocation();
1493 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001494 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1495 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1496 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001497 }
1498 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001499 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001500 }
Mike Stump11289f42009-09-09 15:08:12 +00001501
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001502 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001503 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001504 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001505 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001506 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001507 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001508 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001509 ConsumeToken();
1510 break;
1511 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001512 if (!cStyleParamWarned) {
1513 Diag(Tok, diag::warn_cstyle_param);
1514 cStyleParamWarned = true;
1515 }
John McCall084e83d2011-03-24 11:26:52 +00001516 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001517 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001518 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001519 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1520 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001521 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001522 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001523 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1524 ParmDecl.getIdentifierLoc(),
1525 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001526 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001527 }
Mike Stump11289f42009-09-09 15:08:12 +00001528
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001529 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001530 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001531 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001532 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001533
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001534 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001535 return nullptr;
1536
Chris Lattner5700fab2007-10-07 02:00:24 +00001537 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1538 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001539 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001540 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001541 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001542 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001543 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001544 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001545 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001546
John McCall28a6aea2009-11-04 02:18:39 +00001547 PD.complete(Result);
1548 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001549}
1550
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001551/// objc-protocol-refs:
1552/// '<' identifier-list '>'
1553///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001554bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001555ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1556 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001557 bool WarnOnDeclarations, bool ForObjCContainer,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001558 SourceLocation &LAngleLoc, SourceLocation &EndLoc,
1559 bool consumeLastToken) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001560 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001561
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001562 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001563
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001564 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001565
Chris Lattner3bbae002008-07-26 04:03:38 +00001566 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001567 if (Tok.is(tok::code_completion)) {
Craig Topper883dd332015-12-24 23:58:11 +00001568 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001569 cutOffParsing();
1570 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001571 }
1572
Chris Lattner3bbae002008-07-26 04:03:38 +00001573 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001574 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001575 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001576 return true;
1577 }
1578 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1579 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001580 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001581 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001582
Alp Toker383d2c42014-01-01 03:08:43 +00001583 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001584 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001585 }
Mike Stump11289f42009-09-09 15:08:12 +00001586
Chris Lattner3bbae002008-07-26 04:03:38 +00001587 // Consume the '>'.
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001588 if (ParseGreaterThanInTemplateList(EndLoc, consumeLastToken,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001589 /*ObjCGenericList=*/false))
Chris Lattner3bbae002008-07-26 04:03:38 +00001590 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001591
Chris Lattner3bbae002008-07-26 04:03:38 +00001592 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001593 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Craig Toppera9247eb2015-10-22 04:59:56 +00001594 ProtocolIdents, Protocols);
Chris Lattner3bbae002008-07-26 04:03:38 +00001595 return false;
1596}
1597
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001598TypeResult Parser::parseObjCProtocolQualifierType(SourceLocation &rAngleLoc) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001599 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001600 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001601
1602 SourceLocation lAngleLoc;
1603 SmallVector<Decl *, 8> protocols;
1604 SmallVector<SourceLocation, 8> protocolLocs;
1605 (void)ParseObjCProtocolReferences(protocols, protocolLocs, false, false,
1606 lAngleLoc, rAngleLoc,
1607 /*consumeLastToken=*/true);
1608 TypeResult result = Actions.actOnObjCProtocolQualifierType(lAngleLoc,
1609 protocols,
1610 protocolLocs,
1611 rAngleLoc);
1612 if (result.isUsable()) {
1613 Diag(lAngleLoc, diag::warn_objc_protocol_qualifier_missing_id)
1614 << FixItHint::CreateInsertion(lAngleLoc, "id")
1615 << SourceRange(lAngleLoc, rAngleLoc);
1616 }
1617
1618 return result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001619}
1620
Douglas Gregore9d95f12015-07-07 03:57:35 +00001621/// Parse Objective-C type arguments or protocol qualifiers.
1622///
1623/// objc-type-arguments:
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001624/// '<' type-name '...'[opt] (',' type-name '...'[opt])* '>'
Douglas Gregore9d95f12015-07-07 03:57:35 +00001625///
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001626void Parser::parseObjCTypeArgsOrProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001627 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001628 SourceLocation &typeArgsLAngleLoc,
1629 SmallVectorImpl<ParsedType> &typeArgs,
1630 SourceLocation &typeArgsRAngleLoc,
1631 SourceLocation &protocolLAngleLoc,
1632 SmallVectorImpl<Decl *> &protocols,
1633 SmallVectorImpl<SourceLocation> &protocolLocs,
1634 SourceLocation &protocolRAngleLoc,
1635 bool consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001636 bool warnOnIncompleteProtocols) {
1637 assert(Tok.is(tok::less) && "Not at the start of type args or protocols");
1638 SourceLocation lAngleLoc = ConsumeToken();
1639
1640 // Whether all of the elements we've parsed thus far are single
1641 // identifiers, which might be types or might be protocols.
1642 bool allSingleIdentifiers = true;
1643 SmallVector<IdentifierInfo *, 4> identifiers;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001644 SmallVectorImpl<SourceLocation> &identifierLocs = protocolLocs;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001645
1646 // Parse a list of comma-separated identifiers, bailing out if we
1647 // see something different.
1648 do {
1649 // Parse a single identifier.
1650 if (Tok.is(tok::identifier) &&
1651 (NextToken().is(tok::comma) ||
1652 NextToken().is(tok::greater) ||
1653 NextToken().is(tok::greatergreater))) {
1654 identifiers.push_back(Tok.getIdentifierInfo());
1655 identifierLocs.push_back(ConsumeToken());
1656 continue;
1657 }
1658
1659 if (Tok.is(tok::code_completion)) {
1660 // FIXME: Also include types here.
1661 SmallVector<IdentifierLocPair, 4> identifierLocPairs;
1662 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1663 identifierLocPairs.push_back(IdentifierLocPair(identifiers[i],
1664 identifierLocs[i]));
1665 }
1666
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001667 QualType BaseT = Actions.GetTypeFromParser(baseType);
1668 if (!BaseT.isNull() && BaseT->acceptsObjCTypeParams()) {
1669 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
1670 } else {
Craig Topper883dd332015-12-24 23:58:11 +00001671 Actions.CodeCompleteObjCProtocolReferences(identifierLocPairs);
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001672 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001673 cutOffParsing();
1674 return;
1675 }
1676
1677 allSingleIdentifiers = false;
1678 break;
1679 } while (TryConsumeToken(tok::comma));
1680
1681 // If we parsed an identifier list, semantic analysis sorts out
1682 // whether it refers to protocols or to type arguments.
1683 if (allSingleIdentifiers) {
1684 // Parse the closing '>'.
1685 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001686 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001687 /*ObjCGenericList=*/true);
1688
1689 // Let Sema figure out what we parsed.
1690 Actions.actOnObjCTypeArgsOrProtocolQualifiers(getCurScope(),
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001691 baseType,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001692 lAngleLoc,
1693 identifiers,
1694 identifierLocs,
1695 rAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001696 typeArgsLAngleLoc,
1697 typeArgs,
1698 typeArgsRAngleLoc,
1699 protocolLAngleLoc,
1700 protocols,
1701 protocolRAngleLoc,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001702 warnOnIncompleteProtocols);
1703 return;
1704 }
1705
1706 // We syntactically matched a type argument, so commit to parsing
1707 // type arguments.
Douglas Gregore9d95f12015-07-07 03:57:35 +00001708
1709 // Convert the identifiers into type arguments.
1710 bool invalid = false;
1711 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1712 ParsedType typeArg
1713 = Actions.getTypeName(*identifiers[i], identifierLocs[i], getCurScope());
1714 if (typeArg) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001715 DeclSpec DS(AttrFactory);
1716 const char *prevSpec = nullptr;
1717 unsigned diagID;
1718 DS.SetTypeSpecType(TST_typename, identifierLocs[i], prevSpec, diagID,
1719 typeArg, Actions.getASTContext().getPrintingPolicy());
1720
1721 // Form a declarator to turn this into a type.
1722 Declarator D(DS, Declarator::TypeNameContext);
1723 TypeResult fullTypeArg = Actions.ActOnTypeName(getCurScope(), D);
1724 if (fullTypeArg.isUsable())
1725 typeArgs.push_back(fullTypeArg.get());
1726 else
1727 invalid = true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001728 } else {
1729 invalid = true;
1730 }
1731 }
1732
1733 // Continue parsing type-names.
1734 do {
1735 TypeResult typeArg = ParseTypeName();
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001736
1737 // Consume the '...' for a pack expansion.
1738 SourceLocation ellipsisLoc;
1739 TryConsumeToken(tok::ellipsis, ellipsisLoc);
1740 if (typeArg.isUsable() && ellipsisLoc.isValid()) {
1741 typeArg = Actions.ActOnPackExpansion(typeArg.get(), ellipsisLoc);
1742 }
1743
Douglas Gregore9d95f12015-07-07 03:57:35 +00001744 if (typeArg.isUsable()) {
1745 typeArgs.push_back(typeArg.get());
1746 } else {
1747 invalid = true;
1748 }
1749 } while (TryConsumeToken(tok::comma));
1750
1751 // Parse the closing '>'.
1752 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001753 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001754 /*ObjCGenericList=*/true);
1755
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001756 if (invalid) {
1757 typeArgs.clear();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001758 return;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001759 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001760
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001761 // Record left/right angle locations.
1762 typeArgsLAngleLoc = lAngleLoc;
1763 typeArgsRAngleLoc = rAngleLoc;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001764}
1765
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001766void Parser::parseObjCTypeArgsAndProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001767 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001768 SourceLocation &typeArgsLAngleLoc,
1769 SmallVectorImpl<ParsedType> &typeArgs,
1770 SourceLocation &typeArgsRAngleLoc,
1771 SourceLocation &protocolLAngleLoc,
1772 SmallVectorImpl<Decl *> &protocols,
1773 SmallVectorImpl<SourceLocation> &protocolLocs,
1774 SourceLocation &protocolRAngleLoc,
1775 bool consumeLastToken) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001776 assert(Tok.is(tok::less));
1777
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001778 // Parse the first angle-bracket-delimited clause.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001779 parseObjCTypeArgsOrProtocolQualifiers(baseType,
1780 typeArgsLAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001781 typeArgs,
1782 typeArgsRAngleLoc,
1783 protocolLAngleLoc,
1784 protocols,
1785 protocolLocs,
1786 protocolRAngleLoc,
1787 consumeLastToken,
Douglas Gregore83b9562015-07-07 03:57:53 +00001788 /*warnOnIncompleteProtocols=*/false);
1789
1790 // An Objective-C object pointer followed by type arguments
1791 // can then be followed again by a set of protocol references, e.g.,
1792 // \c NSArray<NSView><NSTextDelegate>
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001793 if ((consumeLastToken && Tok.is(tok::less)) ||
1794 (!consumeLastToken && NextToken().is(tok::less))) {
1795 // If we aren't consuming the last token, the prior '>' is still hanging
1796 // there. Consume it before we parse the protocol qualifiers.
1797 if (!consumeLastToken)
1798 ConsumeToken();
1799
1800 if (!protocols.empty()) {
1801 SkipUntilFlags skipFlags = SkipUntilFlags();
1802 if (!consumeLastToken)
1803 skipFlags = skipFlags | StopBeforeMatch;
Douglas Gregore83b9562015-07-07 03:57:53 +00001804 Diag(Tok, diag::err_objc_type_args_after_protocols)
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001805 << SourceRange(protocolLAngleLoc, protocolRAngleLoc);
1806 SkipUntil(tok::greater, tok::greatergreater, skipFlags);
Douglas Gregore83b9562015-07-07 03:57:53 +00001807 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001808 ParseObjCProtocolReferences(protocols, protocolLocs,
1809 /*WarnOnDeclarations=*/false,
1810 /*ForObjCContainer=*/false,
1811 protocolLAngleLoc, protocolRAngleLoc,
1812 consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001813 }
1814 }
1815}
1816
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001817TypeResult Parser::parseObjCTypeArgsAndProtocolQualifiers(
1818 SourceLocation loc,
1819 ParsedType type,
1820 bool consumeLastToken,
1821 SourceLocation &endLoc) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001822 assert(Tok.is(tok::less));
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001823 SourceLocation typeArgsLAngleLoc;
1824 SmallVector<ParsedType, 4> typeArgs;
1825 SourceLocation typeArgsRAngleLoc;
1826 SourceLocation protocolLAngleLoc;
1827 SmallVector<Decl *, 4> protocols;
1828 SmallVector<SourceLocation, 4> protocolLocs;
1829 SourceLocation protocolRAngleLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00001830
1831 // Parse type arguments and protocol qualifiers.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001832 parseObjCTypeArgsAndProtocolQualifiers(type, typeArgsLAngleLoc, typeArgs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001833 typeArgsRAngleLoc, protocolLAngleLoc,
1834 protocols, protocolLocs,
1835 protocolRAngleLoc, consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001836
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001837 // Compute the location of the last token.
1838 if (consumeLastToken)
1839 endLoc = PrevTokLocation;
1840 else
1841 endLoc = Tok.getLocation();
1842
1843 return Actions.actOnObjCTypeArgsAndProtocolQualifiers(
1844 getCurScope(),
1845 loc,
1846 type,
1847 typeArgsLAngleLoc,
1848 typeArgs,
1849 typeArgsRAngleLoc,
1850 protocolLAngleLoc,
1851 protocols,
1852 protocolLocs,
1853 protocolRAngleLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00001854}
1855
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001856void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1857 BalancedDelimiterTracker &T,
1858 SmallVectorImpl<Decl *> &AllIvarDecls,
1859 bool RBraceMissing) {
1860 if (!RBraceMissing)
1861 T.consumeClose();
1862
1863 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1864 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1865 Actions.ActOnObjCContainerFinishDefinition();
1866 // Call ActOnFields() even if we don't have any decls. This is useful
1867 // for code rewriting tools that need to be aware of the empty list.
1868 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1869 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001870 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001871}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001872
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001873/// objc-class-instance-variables:
1874/// '{' objc-instance-variable-decl-list[opt] '}'
1875///
1876/// objc-instance-variable-decl-list:
1877/// objc-visibility-spec
1878/// objc-instance-variable-decl ';'
1879/// ';'
1880/// objc-instance-variable-decl-list objc-visibility-spec
1881/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1882/// objc-instance-variable-decl-list ';'
1883///
1884/// objc-visibility-spec:
1885/// @private
1886/// @protected
1887/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001888/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001889///
1890/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001891/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001892///
John McCall48871652010-08-21 09:40:31 +00001893void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001894 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001895 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001896 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001897 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001898
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001899 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001900 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001901
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001902 BalancedDelimiterTracker T(*this, tok::l_brace);
1903 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001904 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001905 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001906 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001907
Steve Naroff00433d32007-08-21 21:17:12 +00001908 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001909 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001910 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001911 continue;
1912 }
Mike Stump11289f42009-09-09 15:08:12 +00001913
Steve Naroff00433d32007-08-21 21:17:12 +00001914 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001915 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001916 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001917 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001918 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001919 }
1920
Steve Naroff7c348172007-08-23 18:16:40 +00001921 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001922 case tok::objc_private:
1923 case tok::objc_public:
1924 case tok::objc_protected:
1925 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001926 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001927 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001928 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001929
1930 case tok::objc_end:
1931 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001932 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1933 Tok.setKind(tok::at);
1934 Tok.setLength(1);
1935 PP.EnterToken(Tok);
1936 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1937 T, AllIvarDecls, true);
1938 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001939
1940 default:
1941 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1942 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001943 }
1944 }
Mike Stump11289f42009-09-09 15:08:12 +00001945
Douglas Gregor48d46252010-01-13 21:54:15 +00001946 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001947 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001948 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001949 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001950 }
John McCallcfefb6d2009-11-03 02:38:08 +00001951
Benjamin Kramera39beb92014-09-03 11:06:10 +00001952 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1953 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1954 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001955 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001956 Decl *Field = Actions.ActOnIvar(
1957 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1958 FD.BitfieldSize, visibility);
1959 Actions.ActOnObjCContainerFinishDefinition();
1960 if (Field)
1961 AllIvarDecls.push_back(Field);
1962 FD.complete(Field);
1963 };
John McCallcfefb6d2009-11-03 02:38:08 +00001964
Chris Lattnera12405b2008-04-10 06:46:29 +00001965 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001966 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001967 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001968
Chris Lattner0ef13522007-10-09 17:51:17 +00001969 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001970 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001971 } else {
1972 Diag(Tok, diag::err_expected_semi_decl_list);
1973 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001974 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001975 }
1976 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001977 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1978 T, AllIvarDecls, false);
Steve Naroff00433d32007-08-21 21:17:12 +00001979 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001980}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001981
1982/// objc-protocol-declaration:
1983/// objc-protocol-definition
1984/// objc-protocol-forward-reference
1985///
1986/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00001987/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00001988/// objc-protocol-refs[opt]
1989/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00001990/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001991///
1992/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00001993/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001994///
James Dennett1355bd12012-06-11 06:19:40 +00001995/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001996/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001997/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001998Parser::DeclGroupPtrTy
1999Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
2000 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00002001 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002002 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
2003 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002004
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002005 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002006 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002007 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00002008 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002009 }
2010
Nico Weber69a79142013-04-04 00:15:10 +00002011 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00002012
Chris Lattner0ef13522007-10-09 17:51:17 +00002013 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002014 Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00002015 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002016 }
2017 // Save the protocol name, then consume it.
2018 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
2019 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002020
Alp Toker383d2c42014-01-01 03:08:43 +00002021 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00002022 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Craig Topper0f723bb2015-10-22 05:00:01 +00002023 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtoInfo,
John McCall53fa7142010-12-24 02:08:15 +00002024 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002025 }
Mike Stump11289f42009-09-09 15:08:12 +00002026
Erik Verbruggenf9887852011-12-08 09:58:43 +00002027 CheckNestedObjCContexts(AtLoc);
2028
Chris Lattner0ef13522007-10-09 17:51:17 +00002029 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002030 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002031 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
2032
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002033 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002034 while (1) {
2035 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00002036 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002037 Diag(Tok, diag::err_expected) << tok::identifier;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002038 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00002039 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002040 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00002041 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
2042 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002043 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00002044
Chris Lattner0ef13522007-10-09 17:51:17 +00002045 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002046 break;
2047 }
2048 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00002049 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00002050 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002051
Craig Topper0f723bb2015-10-22 05:00:01 +00002052 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtocolRefs,
John McCall53fa7142010-12-24 02:08:15 +00002053 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00002054 }
Mike Stump11289f42009-09-09 15:08:12 +00002055
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002056 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00002057 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002058
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002059 SmallVector<Decl *, 8> ProtocolRefs;
2060 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002061 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00002062 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002063 LAngleLoc, EndProtoLoc,
2064 /*consumeLastToken=*/true))
Douglas Gregorf6102672012-01-01 21:23:57 +00002065 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002066
John McCall48871652010-08-21 09:40:31 +00002067 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00002068 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002069 ProtocolRefs.data(),
2070 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00002071 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00002072 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002073
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00002074 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00002075 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002076}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002077
2078/// objc-implementation:
2079/// objc-class-implementation-prologue
2080/// objc-category-implementation-prologue
2081///
2082/// objc-class-implementation-prologue:
2083/// @implementation identifier objc-superclass[opt]
2084/// objc-class-instance-variables[opt]
2085///
2086/// objc-category-implementation-prologue:
2087/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002088Parser::DeclGroupPtrTy
2089Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002090 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
2091 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002092 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002093 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002094
Douglas Gregor49c22a72009-11-18 16:26:39 +00002095 // Code completion after '@implementation'.
2096 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002097 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002098 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002099 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00002100 }
2101
Nico Weber69a79142013-04-04 00:15:10 +00002102 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00002103
Chris Lattner0ef13522007-10-09 17:51:17 +00002104 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002105 Diag(Tok, diag::err_expected)
2106 << tok::identifier; // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002107 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002108 }
2109 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00002110 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002111 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00002112 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002113
Douglas Gregor85f3f952015-07-07 03:57:15 +00002114 // Neither a type parameter list nor a list of protocol references is
2115 // permitted here. Parse and diagnose them.
2116 if (Tok.is(tok::less)) {
2117 SourceLocation lAngleLoc, rAngleLoc;
2118 SmallVector<IdentifierLocPair, 8> protocolIdents;
2119 SourceLocation diagLoc = Tok.getLocation();
Richard Smith3df3f1d2015-11-03 01:19:56 +00002120 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
2121 if (parseObjCTypeParamListOrProtocolRefs(typeParamScope, lAngleLoc,
2122 protocolIdents, rAngleLoc)) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00002123 Diag(diagLoc, diag::err_objc_parameterized_implementation)
2124 << SourceRange(diagLoc, PrevTokLocation);
2125 } else if (lAngleLoc.isValid()) {
2126 Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
2127 << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
2128 }
2129 }
2130
Mike Stump11289f42009-09-09 15:08:12 +00002131 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002132 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002133 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002134 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002135 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002136
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002137 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002138 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002139 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002140 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002141 }
2142
Chris Lattner0ef13522007-10-09 17:51:17 +00002143 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002144 categoryId = Tok.getIdentifierInfo();
2145 categoryLoc = ConsumeToken();
2146 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002147 Diag(Tok, diag::err_expected)
2148 << tok::identifier; // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002149 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002150 }
Chris Lattner0ef13522007-10-09 17:51:17 +00002151 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002152 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002153 SkipUntil(tok::r_paren); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002154 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002155 }
2156 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00002157 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2158 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002159 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2160 SmallVector<Decl *, 4> protocols;
2161 SmallVector<SourceLocation, 4> protocolLocs;
2162 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2163 /*warnOnIncompleteProtocols=*/false,
2164 /*ForObjCContainer=*/false,
2165 protocolLAngleLoc, protocolRAngleLoc,
2166 /*consumeLastToken=*/true);
Fariborz Jahanian85888552013-05-17 17:58:11 +00002167 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002168 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002169 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00002170 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002171
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002172 } else {
2173 // We have a class implementation
2174 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002175 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00002176 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002177 // We have a super class
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002178 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002179 Diag(Tok, diag::err_expected)
2180 << tok::identifier; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002181 return DeclGroupPtrTy();
2182 }
2183 superClassId = Tok.getIdentifierInfo();
2184 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002185 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002186 ObjCImpDecl = Actions.ActOnStartClassImplementation(
2187 AtLoc, nameId, nameLoc,
2188 superClassId, superClassLoc);
2189
2190 if (Tok.is(tok::l_brace)) // we have ivars
2191 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002192 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2193 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002194
2195 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2196 SmallVector<Decl *, 4> protocols;
2197 SmallVector<SourceLocation, 4> protocolLocs;
2198 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2199 /*warnOnIncompleteProtocols=*/false,
2200 /*ForObjCContainer=*/false,
2201 protocolLAngleLoc, protocolRAngleLoc,
2202 /*consumeLastToken=*/true);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002203 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002204 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002205 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002206
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002207 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002208
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002209 {
2210 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00002211 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002212 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002213 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002214 MaybeParseMicrosoftAttributes(attrs);
2215 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
2216 DeclGroupRef DG = DGP.get();
2217 DeclsInGroup.append(DG.begin(), DG.end());
2218 }
2219 }
2220 }
2221
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00002222 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002223}
Steve Naroff33a1e802007-10-29 21:38:07 +00002224
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002225Parser::DeclGroupPtrTy
2226Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002227 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
2228 "ParseObjCAtEndDeclaration(): Expected @end");
2229 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002230 if (CurParsedObjCImpl)
2231 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002232 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00002233 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002234 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002235 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002236}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002237
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002238Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
2239 if (!Finished) {
2240 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00002241 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002242 P.Diag(P.Tok, diag::err_objc_missing_end)
2243 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
2244 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
2245 << Sema::OCK_Implementation;
2246 }
2247 }
Craig Topper161e4db2014-05-21 06:02:52 +00002248 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002249 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002250}
2251
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002252void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
2253 assert(!Finished);
2254 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
2255 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002256 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2257 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002258
2259 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
2260
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002261 if (HasCFunction)
2262 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
2263 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2264 false/*c-functions*/);
2265
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002266 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002267 for (LateParsedObjCMethodContainer::iterator
2268 I = LateParsedObjCMethods.begin(),
2269 E = LateParsedObjCMethods.end(); I != E; ++I)
2270 delete *I;
2271 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002272
2273 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002274}
2275
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002276/// compatibility-alias-decl:
2277/// @compatibility_alias alias-name class-name ';'
2278///
John McCall48871652010-08-21 09:40:31 +00002279Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002280 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
2281 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
2282 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00002283 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002284 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002285 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002286 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002287 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
2288 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00002289 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002290 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002291 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002292 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002293 IdentifierInfo *classId = Tok.getIdentifierInfo();
2294 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00002295 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00002296 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
2297 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002298}
2299
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002300/// property-synthesis:
2301/// @synthesize property-ivar-list ';'
2302///
2303/// property-ivar-list:
2304/// property-ivar
2305/// property-ivar-list ',' property-ivar
2306///
2307/// property-ivar:
2308/// identifier
2309/// identifier '=' identifier
2310///
John McCall48871652010-08-21 09:40:31 +00002311Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002312 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00002313 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002314 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00002315
Douglas Gregor88e72a02009-11-18 19:45:45 +00002316 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00002317 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002318 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002319 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002320 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002321 }
2322
Douglas Gregor88e72a02009-11-18 19:45:45 +00002323 if (Tok.isNot(tok::identifier)) {
2324 Diag(Tok, diag::err_synthesized_property_name);
2325 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002326 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00002327 }
Craig Topper161e4db2014-05-21 06:02:52 +00002328
2329 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002330 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2331 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002332 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00002333 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002334 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00002335 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002336 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002337 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002338 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002339 }
2340
Chris Lattner0ef13522007-10-09 17:51:17 +00002341 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002342 Diag(Tok, diag::err_expected) << tok::identifier;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002343 break;
2344 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002345 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002346 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002347 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002348 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002349 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002350 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002351 break;
2352 ConsumeToken(); // consume ','
2353 }
Alp Toker383d2c42014-01-01 03:08:43 +00002354 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00002355 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002356}
2357
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002358/// property-dynamic:
2359/// @dynamic property-list
2360///
2361/// property-list:
2362/// identifier
2363/// property-list ',' identifier
2364///
John McCall48871652010-08-21 09:40:31 +00002365Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002366 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2367 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002368 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002369 while (true) {
2370 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002371 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002372 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002373 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002374 }
2375
2376 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002377 Diag(Tok, diag::err_expected) << tok::identifier;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002378 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002379 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002380 }
2381
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002382 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2383 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002384 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Craig Topper161e4db2014-05-21 06:02:52 +00002385 propertyId, nullptr, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002386
Chris Lattner0ef13522007-10-09 17:51:17 +00002387 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002388 break;
2389 ConsumeToken(); // consume ','
2390 }
Alp Toker383d2c42014-01-01 03:08:43 +00002391 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00002392 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002393}
Mike Stump11289f42009-09-09 15:08:12 +00002394
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002395/// objc-throw-statement:
2396/// throw expression[opt];
2397///
John McCalldadc5752010-08-24 06:29:42 +00002398StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2399 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002400 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00002401 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00002402 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002403 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002404 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002405 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002406 }
2407 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00002408 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00002409 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002410 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002411}
2412
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002413/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002414/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002415///
John McCalldadc5752010-08-24 06:29:42 +00002416StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002417Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002418 ConsumeToken(); // consume synchronized
2419 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002420 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002421 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002422 }
John McCalld9bb7432011-07-27 21:50:02 +00002423
2424 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002425 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00002426 ExprResult operand(ParseExpression());
2427
2428 if (Tok.is(tok::r_paren)) {
2429 ConsumeParen(); // ')'
2430 } else {
2431 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002432 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00002433
2434 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002435 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002436 }
John McCalld9bb7432011-07-27 21:50:02 +00002437
2438 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002439 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00002440 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002441 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002442 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002443 }
Steve Naroffd9c26072008-06-04 20:36:13 +00002444
John McCalld9bb7432011-07-27 21:50:02 +00002445 // Check the @synchronized operand now.
2446 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002447 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002448
John McCalld9bb7432011-07-27 21:50:02 +00002449 // Parse the compound statement within a new scope.
2450 ParseScope bodyScope(this, Scope::DeclScope);
2451 StmtResult body(ParseCompoundStatementBody());
2452 bodyScope.Exit();
2453
2454 // If there was a semantic or parse error earlier with the
2455 // operand, fail now.
2456 if (operand.isInvalid())
2457 return StmtError();
2458
2459 if (body.isInvalid())
2460 body = Actions.ActOnNullStmt(Tok.getLocation());
2461
2462 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002463}
2464
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002465/// objc-try-catch-statement:
2466/// @try compound-statement objc-catch-list[opt]
2467/// @try compound-statement objc-catch-list[opt] @finally compound-statement
2468///
2469/// objc-catch-list:
2470/// @catch ( parameter-declaration ) compound-statement
2471/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2472/// catch-parameter-declaration:
2473/// parameter-declaration
2474/// '...' [OBJC2]
2475///
John McCalldadc5752010-08-24 06:29:42 +00002476StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002477 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002478
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002479 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00002480 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002481 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002482 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002483 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00002484 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00002485 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002486 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00002487 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002488 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002489 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002490 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002491
Chris Lattner0ef13522007-10-09 17:51:17 +00002492 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002493 // At this point, we need to lookahead to determine if this @ is the start
2494 // of an @catch or @finally. We don't want to consume the @ token if this
2495 // is an @try or @encode or something else.
2496 Token AfterAt = GetLookAheadToken(1);
2497 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2498 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2499 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002500
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002501 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002502 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002503 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002504 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002505 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002506 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00002507 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002508 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002509 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002510 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00002511 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002512 ParseDeclarator(ParmDecl);
2513
Douglas Gregore11ee112010-04-23 23:01:43 +00002514 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002515 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002516 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002517 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002518 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002519
Steve Naroff65a00892009-04-07 22:56:58 +00002520 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002521
Steve Naroff65a00892009-04-07 22:56:58 +00002522 if (Tok.is(tok::r_paren))
2523 RParenLoc = ConsumeParen();
2524 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002525 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002526
John McCalldadc5752010-08-24 06:29:42 +00002527 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002528 if (Tok.is(tok::l_brace))
2529 CatchBody = ParseCompoundStatementBody();
2530 else
Alp Tokerec543272013-12-24 09:48:30 +00002531 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002532 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002533 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00002534
John McCalldadc5752010-08-24 06:29:42 +00002535 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00002536 RParenLoc,
2537 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002538 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002539 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002540 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002541
Steve Naroffe6016792008-02-05 21:27:35 +00002542 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002543 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2544 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002545 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002546 }
2547 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002548 } else {
2549 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002550 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002551 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002552
John McCalldadc5752010-08-24 06:29:42 +00002553 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002554 if (Tok.is(tok::l_brace))
2555 FinallyBody = ParseCompoundStatementBody();
2556 else
Alp Tokerec543272013-12-24 09:48:30 +00002557 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002558 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002559 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002560 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002561 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002562 catch_or_finally_seen = true;
2563 break;
2564 }
2565 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002566 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002567 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002568 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002569 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002570
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002571 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002572 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002573 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002574}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002575
John McCall31168b02011-06-15 23:02:42 +00002576/// objc-autoreleasepool-statement:
2577/// @autoreleasepool compound-statement
2578///
2579StmtResult
2580Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2581 ConsumeToken(); // consume autoreleasepool
2582 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002583 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002584 return StmtError();
2585 }
2586 // Enter a scope to hold everything within the compound stmt. Compound
2587 // statements can always hold declarations.
2588 ParseScope BodyScope(this, Scope::DeclScope);
2589
2590 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2591
2592 BodyScope.Exit();
2593 if (AutoreleasePoolBody.isInvalid())
2594 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2595 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002596 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002597}
2598
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002599/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2600/// for later parsing.
2601void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
2602 LexedMethod* LM = new LexedMethod(this, MDecl);
2603 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2604 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002605 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002606 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002607 if (Tok.is(tok::kw_try)) {
2608 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002609 if (Tok.is(tok::colon)) {
2610 Toks.push_back(Tok);
2611 ConsumeToken();
2612 while (Tok.isNot(tok::l_brace)) {
2613 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2614 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2615 }
2616 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002617 Toks.push_back(Tok); // also store '{'
2618 }
2619 else if (Tok.is(tok::colon)) {
2620 ConsumeToken();
Richard Smithb9fa9962015-08-21 03:04:33 +00002621 // FIXME: This is wrong, due to C++11 braced initialization.
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002622 while (Tok.isNot(tok::l_brace)) {
2623 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2624 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2625 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002626 Toks.push_back(Tok); // also store '{'
2627 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002628 ConsumeBrace();
2629 // Consume everything up to (and including) the matching right brace.
2630 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002631 while (Tok.is(tok::kw_catch)) {
2632 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2633 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2634 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002635}
2636
Steve Naroff09bf8152007-09-06 21:24:23 +00002637/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002638///
John McCall48871652010-08-21 09:40:31 +00002639Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002640 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002641
John McCallfaf5fb42010-08-26 23:41:50 +00002642 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2643 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002644
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002645 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002646 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002647 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002648 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002649 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002650 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002651 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002652 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002653
Steve Naroffbb875722007-11-11 19:54:21 +00002654 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002655 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002656 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002657
Steve Naroffbb875722007-11-11 19:54:21 +00002658 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002659 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002660
Steve Naroffbb875722007-11-11 19:54:21 +00002661 // If we didn't find the '{', bail out.
2662 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002663 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002664 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002665
2666 if (!MDecl) {
2667 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002668 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002669 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002670 }
2671
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002672 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002673 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002674 assert (CurParsedObjCImpl
2675 && "ParseObjCMethodDefinition - Method out of @implementation");
2676 // Consume the tokens and store them for later parsing.
2677 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002678 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002679}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002680
John McCalldadc5752010-08-24 06:29:42 +00002681StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002682 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002683 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002684 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002685 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002686 }
2687
2688 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002689 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002690
2691 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002692 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002693
2694 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002695 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002696
2697 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2698 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002699
2700 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2701 getLangOpts().DebuggerSupport) {
2702 SkipUntil(tok::semi);
2703 return Actions.ActOnNullStmt(Tok.getLocation());
2704 }
2705
John McCalldadc5752010-08-24 06:29:42 +00002706 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002707 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002708 // If the expression is invalid, skip ahead to the next semicolon. Not
2709 // doing this opens us up to the possibility of infinite loops if
2710 // ParseExpression does not consume any tokens.
2711 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002712 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002713 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002714
Steve Naroffe6016792008-02-05 21:27:35 +00002715 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002716 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002717 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002718}
2719
John McCalldadc5752010-08-24 06:29:42 +00002720ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002721 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002722 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002723 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002724 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002725 return ExprError();
2726
Ted Kremeneke65b0862012-03-06 20:05:56 +00002727 case tok::minus:
2728 case tok::plus: {
2729 tok::TokenKind Kind = Tok.getKind();
2730 SourceLocation OpLoc = ConsumeToken();
2731
2732 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002733 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002734 switch (Kind) {
2735 case tok::minus: Symbol = "-"; break;
2736 case tok::plus: Symbol = "+"; break;
2737 default: llvm_unreachable("missing unary operator case");
2738 }
2739 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2740 << Symbol;
2741 return ExprError();
2742 }
2743
2744 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2745 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002746 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002747 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002748 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002749
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002750 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002751 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002752 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002753
2754 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002755 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002756 }
2757
Chris Lattnere002fbe2007-12-12 01:04:12 +00002758 case tok::string_literal: // primary-expression: string-literal
2759 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002760 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002761
2762 case tok::char_constant:
2763 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2764
2765 case tok::numeric_constant:
2766 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2767
2768 case tok::kw_true: // Objective-C++, etc.
2769 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2770 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2771 case tok::kw_false: // Objective-C++, etc.
2772 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2773 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2774
2775 case tok::l_square:
2776 // Objective-C array literal
2777 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2778
2779 case tok::l_brace:
2780 // Objective-C dictionary literal
2781 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2782
Patrick Beard0caa3942012-04-19 00:25:12 +00002783 case tok::l_paren:
2784 // Objective-C boxed expression
2785 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2786
Chris Lattnere002fbe2007-12-12 01:04:12 +00002787 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002788 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002789 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002790
Chris Lattner197a3012008-08-05 06:19:09 +00002791 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2792 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002793 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002794 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002795 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002796 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002797 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002798 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002799 const char *str = nullptr;
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002800 if (GetLookAheadToken(1).is(tok::l_brace)) {
2801 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2802 str =
2803 ch == 't' ? "try"
2804 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002805 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002806 }
2807 if (str) {
2808 SourceLocation kwLoc = Tok.getLocation();
2809 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2810 FixItHint::CreateReplacement(kwLoc, str));
2811 }
2812 else
2813 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2814 }
Chris Lattner197a3012008-08-05 06:19:09 +00002815 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002816 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002817}
2818
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002819/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002820///
2821/// This routine parses the receiver of a message send in
2822/// Objective-C++ either as a type or as an expression. Note that this
2823/// routine must not be called to parse a send to 'super', since it
2824/// has no way to return such a result.
2825///
2826/// \param IsExpr Whether the receiver was parsed as an expression.
2827///
2828/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2829/// IsExpr is true), the parsed expression. If the receiver was parsed
2830/// as a type (\c IsExpr is false), the parsed type.
2831///
2832/// \returns True if an error occurred during parsing or semantic
2833/// analysis, in which case the arguments do not have valid
2834/// values. Otherwise, returns false for a successful parse.
2835///
2836/// objc-receiver: [C++]
2837/// 'super' [not parsed here]
2838/// expression
2839/// simple-type-specifier
2840/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002841bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002842 InMessageExpressionRAIIObject InMessage(*this, true);
2843
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002844 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2845 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002846 TryAnnotateTypeOrScopeToken();
2847
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002848 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002849 // objc-receiver:
2850 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002851 // Make sure any typos in the receiver are corrected or diagnosed, so that
2852 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2853 // only the things that are valid ObjC receivers?
2854 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002855 if (Receiver.isInvalid())
2856 return true;
2857
2858 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002859 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002860 return false;
2861 }
2862
2863 // objc-receiver:
2864 // typename-specifier
2865 // simple-type-specifier
2866 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002867 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002868 ParseCXXSimpleTypeSpecifier(DS);
2869
2870 if (Tok.is(tok::l_paren)) {
2871 // If we see an opening parentheses at this point, we are
2872 // actually parsing an expression that starts with a
2873 // function-style cast, e.g.,
2874 //
2875 // postfix-expression:
2876 // simple-type-specifier ( expression-list [opt] )
2877 // typename-specifier ( expression-list [opt] )
2878 //
2879 // Parse the remainder of this case, then the (optional)
2880 // postfix-expression suffix, followed by the (optional)
2881 // right-hand side of the binary expression. We have an
2882 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002883 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002884 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002885 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002886 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002887 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002888 if (Receiver.isInvalid())
2889 return true;
2890
2891 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002892 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002893 return false;
2894 }
2895
2896 // We have a class message. Turn the simple-type-specifier or
2897 // typename-specifier we parsed into a type and parse the
2898 // remainder of the class message.
2899 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002900 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002901 if (Type.isInvalid())
2902 return true;
2903
2904 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002905 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002906 return false;
2907}
2908
Douglas Gregor990ccac2010-05-31 14:40:22 +00002909/// \brief Determine whether the parser is currently referring to a an
2910/// Objective-C message send, using a simplified heuristic to avoid overhead.
2911///
2912/// This routine will only return true for a subset of valid message-send
2913/// expressions.
2914bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002915 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002916 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002917 return GetLookAheadToken(1).is(tok::identifier) &&
2918 GetLookAheadToken(2).is(tok::identifier);
2919}
2920
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002921bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002922 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002923 InMessageExpression)
2924 return false;
2925
2926
2927 ParsedType Type;
2928
2929 if (Tok.is(tok::annot_typename))
2930 Type = getTypeAnnotation(Tok);
2931 else if (Tok.is(tok::identifier))
2932 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2933 getCurScope());
2934 else
2935 return false;
2936
2937 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2938 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002939 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002940 if (Tok.is(tok::identifier))
2941 TryAnnotateTypeOrScopeToken();
2942
2943 return Tok.is(tok::annot_typename);
2944 }
2945 }
2946
2947 return false;
2948}
2949
Mike Stump11289f42009-09-09 15:08:12 +00002950/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002951/// '[' objc-receiver objc-message-args ']'
2952///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002953/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002954/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002955/// expression
2956/// class-name
2957/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002958///
John McCalldadc5752010-08-24 06:29:42 +00002959ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002960 assert(Tok.is(tok::l_square) && "'[' expected");
2961 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2962
Douglas Gregora817a192010-05-27 23:06:34 +00002963 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002964 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002965 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002966 return ExprError();
2967 }
2968
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002969 InMessageExpressionRAIIObject InMessage(*this, true);
2970
David Blaikiebbafb8a2012-03-11 07:00:24 +00002971 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002972 // We completely separate the C and C++ cases because C++ requires
2973 // more complicated (read: slower) parsing.
2974
2975 // Handle send to super.
2976 // FIXME: This doesn't benefit from the same typo-correction we
2977 // get in Objective-C.
2978 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002979 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002980 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002981 ParsedType(), nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002982
2983 // Parse the receiver, which is either a type or an expression.
2984 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00002985 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002986 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002987 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002988 return ExprError();
2989 }
2990
2991 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002992 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2993 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002994 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002995
2996 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002997 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00002998 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00002999 }
3000
3001 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00003002 IdentifierInfo *Name = Tok.getIdentifierInfo();
3003 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00003004 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003005 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00003006 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00003007 NextToken().is(tok::period),
3008 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00003009 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00003010 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00003011 ParsedType(), nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003012
John McCallfaf5fb42010-08-26 23:41:50 +00003013 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00003014 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003015 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003016 return ExprError();
3017 }
3018
Douglas Gregore5798dc2010-04-21 20:38:13 +00003019 ConsumeToken(); // the type name
3020
Douglas Gregore83b9562015-07-07 03:57:53 +00003021 // Parse type arguments and protocol qualifiers.
3022 if (Tok.is(tok::less)) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003023 SourceLocation NewEndLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00003024 TypeResult NewReceiverType
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003025 = parseObjCTypeArgsAndProtocolQualifiers(NameLoc, ReceiverType,
3026 /*consumeLastToken=*/true,
3027 NewEndLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00003028 if (!NewReceiverType.isUsable()) {
3029 SkipUntil(tok::r_square, StopAtSemi);
3030 return ExprError();
3031 }
3032
3033 ReceiverType = NewReceiverType.get();
3034 }
3035
Douglas Gregore5798dc2010-04-21 20:38:13 +00003036 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00003037 ReceiverType, nullptr);
3038
John McCallfaf5fb42010-08-26 23:41:50 +00003039 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003040 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00003041 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00003042 }
Chris Lattner8f697062008-01-25 18:59:06 +00003043 }
Chris Lattnera36ec422010-04-11 08:28:14 +00003044
3045 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00003046 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003047 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003048 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003049 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00003050 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003051
John McCallba7bf592010-08-24 05:47:05 +00003052 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003053 ParsedType(), Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00003054}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003055
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003056/// \brief Parse the remainder of an Objective-C message following the
3057/// '[' objc-receiver.
3058///
3059/// This routine handles sends to super, class messages (sent to a
3060/// class name), and instance messages (sent to an object), and the
3061/// target is represented by \p SuperLoc, \p ReceiverType, or \p
3062/// ReceiverExpr, respectively. Only one of these parameters may have
3063/// a valid value.
3064///
3065/// \param LBracLoc The location of the opening '['.
3066///
3067/// \param SuperLoc If this is a send to 'super', the location of the
3068/// 'super' keyword that indicates a send to the superclass.
3069///
3070/// \param ReceiverType If this is a class message, the type of the
3071/// class we are sending a message to.
3072///
3073/// \param ReceiverExpr If this is an instance message, the expression
3074/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00003075///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003076/// objc-message-args:
3077/// objc-selector
3078/// objc-keywordarg-list
3079///
3080/// objc-keywordarg-list:
3081/// objc-keywordarg
3082/// objc-keywordarg-list objc-keywordarg
3083///
Mike Stump11289f42009-09-09 15:08:12 +00003084/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003085/// selector-name[opt] ':' objc-keywordexpr
3086///
3087/// objc-keywordexpr:
3088/// nonempty-expr-list
3089///
3090/// nonempty-expr-list:
3091/// assignment-expression
3092/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003093///
John McCalldadc5752010-08-24 06:29:42 +00003094ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00003095Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003096 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00003097 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00003098 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003099 InMessageExpressionRAIIObject InMessage(*this, true);
3100
Steve Naroffeae65032009-11-07 02:08:14 +00003101 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003102 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003103 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003104 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003105 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003106 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003107 false);
Steve Naroffeae65032009-11-07 02:08:14 +00003108 else
John McCallb268a282010-08-23 23:25:46 +00003109 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003110 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003111 cutOffParsing();
3112 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00003113 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00003114
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003115 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00003116 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003117 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003118
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003119 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003120 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003121 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00003122
Chris Lattner0ef13522007-10-09 17:51:17 +00003123 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003124 while (1) {
3125 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00003126 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003127 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00003128
Alp Toker383d2c42014-01-01 03:08:43 +00003129 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00003130 // We must manually skip to a ']', otherwise the expression skipper will
3131 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3132 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003133 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003134 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003135 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003136
Mike Stump11289f42009-09-09 15:08:12 +00003137 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003138
3139 if (Tok.is(tok::code_completion)) {
3140 if (SuperLoc.isValid())
3141 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003142 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003143 /*AtArgumentEpression=*/true);
3144 else if (ReceiverType)
3145 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003146 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003147 /*AtArgumentEpression=*/true);
3148 else
3149 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003150 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003151 /*AtArgumentEpression=*/true);
3152
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003153 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003154 return ExprError();
3155 }
3156
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00003157 ExprResult Expr;
3158 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3159 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3160 Expr = ParseBraceInitializer();
3161 } else
3162 Expr = ParseAssignmentExpression();
3163
3164 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003165 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00003166 // We must manually skip to a ']', otherwise the expression skipper will
3167 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3168 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003169 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003170 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00003171 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003172
Steve Naroff486760a2007-09-17 20:25:27 +00003173 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003174 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003175
Douglas Gregor1b605f72009-11-19 01:08:35 +00003176 // Code completion after each argument.
3177 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003178 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003179 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003180 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003181 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003182 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003183 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003184 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003185 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00003186 else
John McCallb268a282010-08-23 23:25:46 +00003187 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003188 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003189 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003190 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003191 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00003192 }
3193
Steve Naroff486760a2007-09-17 20:25:27 +00003194 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00003195 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00003196 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003197 break;
3198 // We have a selector or a colon, continue parsing.
3199 }
3200 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00003201 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003202 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00003203 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00003204 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00003205 if (Tok.is(tok::colon))
3206 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003207 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003208 if (Tok.is(tok::colon)) {
3209 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
3210 FixItHint::CreateRemoval(commaLoc);
3211 }
Chris Lattner197a3012008-08-05 06:19:09 +00003212 // We must manually skip to a ']', otherwise the expression skipper will
3213 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3214 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003215 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003216 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003217 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003218
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003219 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003220 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003221 }
3222 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00003223 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003224
Chris Lattner197a3012008-08-05 06:19:09 +00003225 // We must manually skip to a ']', otherwise the expression skipper will
3226 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3227 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003228 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003229 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003230 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00003231
Chris Lattner0ef13522007-10-09 17:51:17 +00003232 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00003233 Diag(Tok, diag::err_expected)
3234 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
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);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003239 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003240 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003241
Chris Lattner8f697062008-01-25 18:59:06 +00003242 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003243
Steve Naroffe61bfa82007-10-05 18:42:47 +00003244 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003245 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00003246 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003247 KeyLocs.push_back(Loc);
3248 }
Chris Lattner5700fab2007-10-07 02:00:24 +00003249 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003250
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003251 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003252 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003253 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003254 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003255 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003256 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00003257 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003258 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003259}
3260
John McCalldadc5752010-08-24 06:29:42 +00003261ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
3262 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003263 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003264
Chris Lattnere002fbe2007-12-12 01:04:12 +00003265 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
3266 // expressions. At this point, we know that the only valid thing that starts
3267 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003268 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003269 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00003270 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003271 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003272
Chris Lattnere002fbe2007-12-12 01:04:12 +00003273 while (Tok.is(tok::at)) {
3274 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00003275
Sebastian Redlc13f2682008-12-09 20:22:58 +00003276 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00003277 if (!isTokenStringLiteral())
3278 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00003279
John McCalldadc5752010-08-24 06:29:42 +00003280 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003281 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003282 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003283
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003284 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00003285 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003286
Craig Topper883dd332015-12-24 23:58:11 +00003287 return Actions.ParseObjCStringLiteral(AtLocs.data(), AtStrings);
Anders Carlsson76f4a902007-08-21 17:43:55 +00003288}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003289
Ted Kremeneke65b0862012-03-06 20:05:56 +00003290/// ParseObjCBooleanLiteral -
3291/// objc-scalar-literal : '@' boolean-keyword
3292/// ;
3293/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
3294/// ;
3295ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
3296 bool ArgValue) {
3297 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
3298 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
3299}
3300
3301/// ParseObjCCharacterLiteral -
3302/// objc-scalar-literal : '@' character-literal
3303/// ;
3304ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
3305 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
3306 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003307 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003308 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003309 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003310 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003311}
3312
3313/// ParseObjCNumericLiteral -
3314/// objc-scalar-literal : '@' scalar-literal
3315/// ;
3316/// scalar-literal : | numeric-constant /* any numeric constant. */
3317/// ;
3318ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3319 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3320 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003321 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003322 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003323 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003324 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003325}
3326
Patrick Beard0caa3942012-04-19 00:25:12 +00003327/// ParseObjCBoxedExpr -
3328/// objc-box-expression:
3329/// @( assignment-expression )
3330ExprResult
3331Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3332 if (Tok.isNot(tok::l_paren))
3333 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3334
3335 BalancedDelimiterTracker T(*this, tok::l_paren);
3336 T.consumeOpen();
3337 ExprResult ValueExpr(ParseAssignmentExpression());
3338 if (T.consumeClose())
3339 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00003340
3341 if (ValueExpr.isInvalid())
3342 return ExprError();
3343
Patrick Beard0caa3942012-04-19 00:25:12 +00003344 // Wrap the sub-expression in a parenthesized expression, to distinguish
3345 // a boxed expression from a literal.
3346 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003347 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00003348 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003349 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00003350}
3351
Ted Kremeneke65b0862012-03-06 20:05:56 +00003352ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00003353 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003354 ConsumeBracket(); // consume the l_square.
3355
3356 while (Tok.isNot(tok::r_square)) {
3357 // Parse list of array element expressions (all must be id types).
3358 ExprResult Res(ParseAssignmentExpression());
3359 if (Res.isInvalid()) {
3360 // We must manually skip to a ']', otherwise the expression skipper will
3361 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3362 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003363 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003364 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003365 }
3366
3367 // Parse the ellipsis that indicates a pack expansion.
3368 if (Tok.is(tok::ellipsis))
3369 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
3370 if (Res.isInvalid())
3371 return true;
3372
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003373 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003374
3375 if (Tok.is(tok::comma))
3376 ConsumeToken(); // Eat the ','.
3377 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00003378 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3379 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003380 }
3381 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramerf0623432012-08-23 22:51:59 +00003382 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00003383 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003384}
3385
3386ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3387 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3388 ConsumeBrace(); // consume the l_square.
3389 while (Tok.isNot(tok::r_brace)) {
3390 // Parse the comma separated key : value expressions.
3391 ExprResult KeyExpr;
3392 {
3393 ColonProtectionRAIIObject X(*this);
3394 KeyExpr = ParseAssignmentExpression();
3395 if (KeyExpr.isInvalid()) {
3396 // We must manually skip to a '}', otherwise the expression skipper will
3397 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3398 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003399 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003400 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003401 }
3402 }
3403
Alp Toker383d2c42014-01-01 03:08:43 +00003404 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003405 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00003406 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003407 }
3408
3409 ExprResult ValueExpr(ParseAssignmentExpression());
3410 if (ValueExpr.isInvalid()) {
3411 // We must manually skip to a '}', otherwise the expression skipper will
3412 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3413 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003414 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003415 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003416 }
3417
3418 // Parse the ellipsis that designates this as a pack expansion.
3419 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00003420 if (getLangOpts().CPlusPlus)
3421 TryConsumeToken(tok::ellipsis, EllipsisLoc);
3422
Ted Kremeneke65b0862012-03-06 20:05:56 +00003423 // We have a valid expression. Collect it in a vector so we can
3424 // build the argument list.
3425 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00003426 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00003427 };
3428 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00003429
3430 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00003431 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3432 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003433 }
3434 SourceLocation EndLoc = ConsumeBrace();
3435
3436 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00003437 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
3438 Elements.data(), Elements.size());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003439}
3440
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003441/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00003442/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00003443ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003444Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00003445 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003446
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003447 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003448
Chris Lattner197a3012008-08-05 06:19:09 +00003449 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003450 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3451
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003452 BalancedDelimiterTracker T(*this, tok::l_paren);
3453 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003454
Douglas Gregor220cac52009-02-18 17:45:20 +00003455 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003456
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003457 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003458
Douglas Gregor220cac52009-02-18 17:45:20 +00003459 if (Ty.isInvalid())
3460 return ExprError();
3461
Nico Webera7c7e602012-12-31 00:28:03 +00003462 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3463 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003464}
Anders Carlssone01493d2007-08-23 15:25:28 +00003465
3466/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00003467/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00003468ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003469Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00003470 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003471
Chris Lattner197a3012008-08-05 06:19:09 +00003472 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003473 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3474
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003475 BalancedDelimiterTracker T(*this, tok::l_paren);
3476 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003477
Chris Lattner197a3012008-08-05 06:19:09 +00003478 if (Tok.isNot(tok::identifier))
Alp Tokerec543272013-12-24 09:48:30 +00003479 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003480
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003481 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00003482 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003483
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003484 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00003485
Nico Webera7c7e602012-12-31 00:28:03 +00003486 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3487 T.getOpenLocation(), ProtoIdLoc,
3488 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00003489}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003490
3491/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003492/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003493ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003494 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003495
Chris Lattner197a3012008-08-05 06:19:09 +00003496 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003497 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3498
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003499 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003500 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003501
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003502 BalancedDelimiterTracker T(*this, tok::l_paren);
3503 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003504 bool HasOptionalParen = Tok.is(tok::l_paren);
3505 if (HasOptionalParen)
3506 ConsumeParen();
3507
Douglas Gregor67c692c2010-08-26 15:07:07 +00003508 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003509 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003510 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003511 return ExprError();
3512 }
3513
Chris Lattner4f472a32009-04-11 18:13:45 +00003514 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003515 if (!SelIdent && // missing selector name.
3516 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003517 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003518
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003519 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003520
Steve Naroff152dd812007-12-05 22:21:29 +00003521 unsigned nColons = 0;
3522 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003523 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003524 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003525 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003526 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003527 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3528 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003529 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003530
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003531 if (Tok.is(tok::r_paren))
3532 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003533
3534 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003535 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003536 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003537 return ExprError();
3538 }
3539
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003540 // Check for another keyword selector.
3541 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003542 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003543 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003544 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003545 break;
3546 }
Steve Naroff152dd812007-12-05 22:21:29 +00003547 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003548 if (HasOptionalParen && Tok.is(tok::r_paren))
3549 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003550 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003551 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003552 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3553 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003554 T.getCloseLocation(),
3555 !HasOptionalParen);
Gabor Greif24032f12007-10-19 15:38:32 +00003556 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003557
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003558void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3559 // MCDecl might be null due to error in method or c-function prototype, etc.
3560 Decl *MCDecl = LM.D;
3561 bool skip = MCDecl &&
3562 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3563 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3564 if (skip)
3565 return;
3566
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003567 // Save the current token position.
3568 SourceLocation OrigLoc = Tok.getLocation();
3569
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003570 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
3571 // Append the current token at the end of the new token stream so that it
3572 // doesn't get lost.
3573 LM.Toks.push_back(Tok);
3574 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
3575
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003576 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003577 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003578
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003579 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3580 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003581 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003582 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003583 parseMethod
3584 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
3585 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003586
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003587 // Tell the actions module that we have entered a method or c-function definition
3588 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003589 if (parseMethod)
3590 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3591 else
3592 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003593 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003594 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003595 else {
3596 if (Tok.is(tok::colon))
3597 ParseConstructorInitializer(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003598 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003599 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00003600
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003601 if (Tok.getLocation() != OrigLoc) {
3602 // Due to parsing error, we either went over the cached tokens or
3603 // there are still cached tokens left. If it's the latter case skip the
3604 // leftover tokens.
3605 // Since this is an uncommon situation that should be avoided, use the
3606 // expensive isBeforeInTranslationUnit call.
3607 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3608 OrigLoc))
3609 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3610 ConsumeAnyToken();
3611 }
3612
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003613 return;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003614}