blob: ed6090453daa954791129cd4357e2386b3e2220e [file] [log] [blame]
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerda59c2f2006-11-05 02:08:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
John McCall8b0666c2010-08-20 18:27:03 +000014#include "clang/Parse/Parser.h"
Douglas Gregore9bba4f2010-09-15 14:51:05 +000015#include "RAIIObjectsForParser.h"
Douglas Gregor813a0662015-06-19 18:14:38 +000016#include "clang/AST/ASTContext.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000017#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/Scope.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000022#include "llvm/ADT/SmallVector.h"
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000024using namespace clang;
25
Nico Weber04e213b2013-04-03 17:36:11 +000026/// Skips attributes after an Objective-C @ directive. Emits a diagnostic.
Nico Weber69a79142013-04-04 00:15:10 +000027void Parser::MaybeSkipAttributes(tok::ObjCKeywordKind Kind) {
Nico Weber04e213b2013-04-03 17:36:11 +000028 ParsedAttributes attrs(AttrFactory);
29 if (Tok.is(tok::kw___attribute)) {
Nico Weber69a79142013-04-04 00:15:10 +000030 if (Kind == tok::objc_interface || Kind == tok::objc_protocol)
31 Diag(Tok, diag::err_objc_postfix_attribute_hint)
32 << (Kind == tok::objc_protocol);
33 else
34 Diag(Tok, diag::err_objc_postfix_attribute);
Nico Weber04e213b2013-04-03 17:36:11 +000035 ParseGNUAttributes(attrs);
36 }
37}
Chris Lattnerda59c2f2006-11-05 02:08:13 +000038
Chris Lattner3a907162008-12-08 21:53:24 +000039/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000040/// external-declaration: [C99 6.9]
41/// [OBJC] objc-class-definition
Steve Naroffe0933392007-10-29 21:39:29 +000042/// [OBJC] objc-class-declaration
43/// [OBJC] objc-alias-declaration
44/// [OBJC] objc-protocol-definition
45/// [OBJC] objc-method-definition
46/// [OBJC] '@' 'end'
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000047Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000048 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000049
Douglas Gregorf48706c2009-12-07 09:27:33 +000050 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000051 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000052 cutOffParsing();
53 return DeclGroupPtrTy();
Douglas Gregorf48706c2009-12-07 09:27:33 +000054 }
Craig Topper161e4db2014-05-21 06:02:52 +000055
56 Decl *SingleDecl = nullptr;
Steve Naroff7c348172007-08-23 18:16:40 +000057 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000058 case tok::objc_class:
59 return ParseObjCAtClassDeclaration(AtLoc);
John McCall53fa7142010-12-24 02:08:15 +000060 case tok::objc_interface: {
John McCall084e83d2011-03-24 11:26:52 +000061 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000062 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
63 break;
John McCall53fa7142010-12-24 02:08:15 +000064 }
65 case tok::objc_protocol: {
John McCall084e83d2011-03-24 11:26:52 +000066 ParsedAttributes attrs(AttrFactory);
Douglas Gregorf6102672012-01-01 21:23:57 +000067 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall53fa7142010-12-24 02:08:15 +000068 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000069 case tok::objc_implementation:
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +000070 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000071 case tok::objc_end:
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +000072 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000073 case tok::objc_compatibility_alias:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000074 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
75 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000076 case tok::objc_synthesize:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000077 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
78 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000079 case tok::objc_dynamic:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000080 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
81 break;
Douglas Gregorc50d4922012-12-11 22:11:52 +000082 case tok::objc_import:
Sean Callanan87596492014-12-09 23:47:56 +000083 if (getLangOpts().Modules || getLangOpts().DebuggerSupport)
Douglas Gregor0bf886d2012-01-03 18:24:14 +000084 return ParseModuleImport(AtLoc);
Fariborz Jahaniana773d082014-03-26 22:02:43 +000085 Diag(AtLoc, diag::err_atimport);
86 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000087 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerce90ef52008-08-23 02:02:23 +000088 default:
89 Diag(AtLoc, diag::err_unexpected_at);
90 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000091 SingleDecl = nullptr;
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000092 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000093 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000094 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000095}
96
97///
Mike Stump11289f42009-09-09 15:08:12 +000098/// objc-class-declaration:
Douglas Gregor85f3f952015-07-07 03:57:15 +000099/// '@' 'class' objc-class-forward-decl (',' objc-class-forward-decl)* ';'
100///
101/// objc-class-forward-decl:
102/// identifier objc-type-parameter-list[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000103///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000104Parser::DeclGroupPtrTy
105Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000106 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000107 SmallVector<IdentifierInfo *, 8> ClassNames;
108 SmallVector<SourceLocation, 8> ClassLocs;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000109 SmallVector<ObjCTypeParamList *, 8> ClassTypeParams;
Mike Stump11289f42009-09-09 15:08:12 +0000110
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000111 while (1) {
Nico Weber69a79142013-04-04 00:15:10 +0000112 MaybeSkipAttributes(tok::objc_class);
Chris Lattner0ef13522007-10-09 17:51:17 +0000113 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000114 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000115 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000116 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000117 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000118 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +0000119 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000120 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000121
Douglas Gregor85f3f952015-07-07 03:57:15 +0000122 // Parse the optional objc-type-parameter-list.
123 ObjCTypeParamList *TypeParams = nullptr;
124 if (Tok.is(tok::less)) {
125 TypeParams = parseObjCTypeParamList();
126 if (TypeParams)
127 Actions.popObjCTypeParamList(getCurScope(), TypeParams);
128 }
129 ClassTypeParams.push_back(TypeParams);
Alp Toker383d2c42014-01-01 03:08:43 +0000130 if (!TryConsumeToken(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000131 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000132 }
Mike Stump11289f42009-09-09 15:08:12 +0000133
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000134 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +0000135 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@class"))
Craig Topper161e4db2014-05-21 06:02:52 +0000136 return Actions.ConvertDeclToDeclGroup(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000137
Ted Kremeneka26da852009-11-17 23:12:20 +0000138 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
139 ClassLocs.data(),
Douglas Gregor85f3f952015-07-07 03:57:15 +0000140 ClassTypeParams,
Ted Kremeneka26da852009-11-17 23:12:20 +0000141 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000142}
143
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000144void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
145{
146 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
147 if (ock == Sema::OCK_None)
148 return;
149
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000150 Decl *Decl = Actions.getObjCDeclContext();
151 if (CurParsedObjCImpl) {
152 CurParsedObjCImpl->finish(AtLoc);
153 } else {
154 Actions.ActOnAtEnd(getCurScope(), AtLoc);
155 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000156 Diag(AtLoc, diag::err_objc_missing_end)
157 << FixItHint::CreateInsertion(AtLoc, "@end\n");
158 if (Decl)
159 Diag(Decl->getLocStart(), diag::note_objc_container_start)
160 << (int) ock;
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000161}
162
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000163///
164/// objc-interface:
165/// objc-class-interface-attributes[opt] objc-class-interface
166/// objc-category-interface
167///
168/// objc-class-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000169/// '@' 'interface' identifier objc-type-parameter-list[opt]
170/// objc-superclass[opt] objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000171/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000172/// objc-interface-decl-list
173/// @end
174///
175/// objc-category-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000176/// '@' 'interface' identifier objc-type-parameter-list[opt]
177/// '(' identifier[opt] ')' objc-protocol-refs[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000178/// objc-interface-decl-list
179/// @end
180///
181/// objc-superclass:
Douglas Gregore9d95f12015-07-07 03:57:35 +0000182/// ':' identifier objc-type-arguments[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000183///
184/// objc-class-interface-attributes:
185/// __attribute__((visibility("default")))
186/// __attribute__((visibility("hidden")))
187/// __attribute__((deprecated))
188/// __attribute__((unavailable))
189/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardacfbe9e2012-04-06 18:12:22 +0000190/// __attribute__((objc_root_class))
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000191///
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000192Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000193 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000194 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000195 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000196 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000197 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000198
Douglas Gregor49c22a72009-11-18 16:26:39 +0000199 // Code completion after '@interface'.
200 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000201 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000202 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000203 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000204 }
205
Nico Weber69a79142013-04-04 00:15:10 +0000206 MaybeSkipAttributes(tok::objc_interface);
Nico Weber04e213b2013-04-03 17:36:11 +0000207
Chris Lattner0ef13522007-10-09 17:51:17 +0000208 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000209 Diag(Tok, diag::err_expected)
210 << tok::identifier; // missing class or category name.
Craig Topper161e4db2014-05-21 06:02:52 +0000211 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000212 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000213
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000214 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000215 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000216 SourceLocation nameLoc = ConsumeToken();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000217
218 // Parse the objc-type-parameter-list or objc-protocol-refs. For the latter
219 // case, LAngleLoc will be valid and ProtocolIdents will capture the
220 // protocol references (that have not yet been resolved).
221 SourceLocation LAngleLoc, EndProtoLoc;
222 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
223 ObjCTypeParamList *typeParameterList = nullptr;
224 if (Tok.is(tok::less)) {
225 typeParameterList = parseObjCTypeParamListOrProtocolRefs(LAngleLoc,
226 ProtocolIdents,
227 EndProtoLoc);
228 }
229
230 if (Tok.is(tok::l_paren) &&
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000231 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000232
233 BalancedDelimiterTracker T(*this, tok::l_paren);
234 T.consumeOpen();
235
236 SourceLocation categoryLoc;
Craig Topper161e4db2014-05-21 06:02:52 +0000237 IdentifierInfo *categoryId = nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000238 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000239 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000240 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000241 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000242 }
243
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000244 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000245 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000246 categoryId = Tok.getIdentifierInfo();
247 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000248 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000249 else if (!getLangOpts().ObjC2) {
Alp Tokerec543272013-12-24 09:48:30 +0000250 Diag(Tok, diag::err_expected)
251 << tok::identifier; // missing category name.
Craig Topper161e4db2014-05-21 06:02:52 +0000252 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000253 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000254
255 T.consumeClose();
256 if (T.getCloseLocation().isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000257 return nullptr;
258
Douglas Gregor0c254a02011-09-23 19:19:41 +0000259 if (!attrs.empty()) { // categories don't support attributes.
260 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
261 attrs.clear();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000262 }
Douglas Gregor0c254a02011-09-23 19:19:41 +0000263
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000264 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000265 assert(LAngleLoc.isInvalid() && "Cannot have already parsed protocols");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000266 SmallVector<Decl *, 8> ProtocolRefs;
267 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000268 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000269 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000270 LAngleLoc, EndProtoLoc,
271 /*consumeLastToken=*/true))
Craig Topper161e4db2014-05-21 06:02:52 +0000272 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000273
John McCall48871652010-08-21 09:40:31 +0000274 Decl *CategoryType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000275 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000276 nameId, nameLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000277 typeParameterList,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000278 categoryId, categoryLoc,
279 ProtocolRefs.data(),
280 ProtocolRefs.size(),
281 ProtocolLocs.data(),
282 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000283
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000284 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000285 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000286
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000287 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000288
289 if (typeParameterList)
290 Actions.popObjCTypeParamList(getCurScope(), typeParameterList);
291
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000292 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000293 }
294 // Parse a class interface.
Craig Topper161e4db2014-05-21 06:02:52 +0000295 IdentifierInfo *superClassId = nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000296 SourceLocation superClassLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000297 SourceLocation typeArgsLAngleLoc;
298 SmallVector<ParsedType, 4> typeArgs;
299 SourceLocation typeArgsRAngleLoc;
300 SmallVector<Decl *, 4> protocols;
301 SmallVector<SourceLocation, 4> protocolLocs;
Chris Lattner0ef13522007-10-09 17:51:17 +0000302 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000303 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000304
305 // Code completion of superclass names.
306 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000307 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000308 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000309 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000310 }
311
Chris Lattner0ef13522007-10-09 17:51:17 +0000312 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000313 Diag(Tok, diag::err_expected)
314 << tok::identifier; // missing super class name.
Craig Topper161e4db2014-05-21 06:02:52 +0000315 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000316 }
317 superClassId = Tok.getIdentifierInfo();
318 superClassLoc = ConsumeToken();
Douglas Gregore9d95f12015-07-07 03:57:35 +0000319
320 // Type arguments for the superclass or protocol conformances.
321 if (Tok.is(tok::less)) {
Douglas Gregor10dc9d82015-07-07 03:58:28 +0000322 parseObjCTypeArgsOrProtocolQualifiers(ParsedType(),
323 typeArgsLAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000324 typeArgs,
325 typeArgsRAngleLoc,
326 LAngleLoc,
327 protocols,
328 protocolLocs,
329 EndProtoLoc,
330 /*consumeLastToken=*/true,
Douglas Gregore9d95f12015-07-07 03:57:35 +0000331 /*warnOnIncompleteProtocols=*/true);
332 }
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000333 }
Douglas Gregor85f3f952015-07-07 03:57:15 +0000334
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000335 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000336 if (LAngleLoc.isValid()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000337 if (!ProtocolIdents.empty()) {
338 // We already parsed the protocols named when we thought we had a
339 // type parameter list. Translate them into actual protocol references.
340 for (const auto &pair : ProtocolIdents) {
341 protocolLocs.push_back(pair.second);
342 }
343 Actions.FindProtocolDeclaration(/*WarnOnDeclarations=*/true,
344 /*ForObjCContainer=*/true,
345 &ProtocolIdents[0], ProtocolIdents.size(),
346 protocols);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000347 }
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000348 } else if (protocols.empty() && Tok.is(tok::less) &&
349 ParseObjCProtocolReferences(protocols, protocolLocs, true, true,
350 LAngleLoc, EndProtoLoc,
351 /*consumeLastToken=*/true)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000352 return nullptr;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000353 }
Mike Stump11289f42009-09-09 15:08:12 +0000354
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000355 if (Tok.isNot(tok::less))
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000356 Actions.ActOnTypedefedProtocols(protocols, superClassId, superClassLoc);
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000357
John McCall48871652010-08-21 09:40:31 +0000358 Decl *ClsType =
Douglas Gregore9d95f12015-07-07 03:57:35 +0000359 Actions.ActOnStartClassInterface(getCurScope(), AtLoc, nameId, nameLoc,
360 typeParameterList, superClassId,
361 superClassLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000362 typeArgs,
363 SourceRange(typeArgsLAngleLoc,
364 typeArgsRAngleLoc),
365 protocols.data(), protocols.size(),
366 protocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000367 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000368
Chris Lattner0ef13522007-10-09 17:51:17 +0000369 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000370 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000371
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000372 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000373
374 if (typeParameterList)
375 Actions.popObjCTypeParamList(getCurScope(), typeParameterList);
376
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000377 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000378}
379
Douglas Gregor813a0662015-06-19 18:14:38 +0000380/// Add an attribute for a context-sensitive type nullability to the given
381/// declarator.
382static void addContextSensitiveTypeNullability(Parser &P,
383 Declarator &D,
384 NullabilityKind nullability,
385 SourceLocation nullabilityLoc,
386 bool &addedToDeclSpec) {
387 // Create the attribute.
388 auto getNullabilityAttr = [&]() -> AttributeList * {
Douglas Gregorbec595a2015-06-19 18:27:45 +0000389 return D.getAttributePool().create(
390 P.getNullabilityKeyword(nullability),
391 SourceRange(nullabilityLoc),
392 nullptr, SourceLocation(),
393 nullptr, 0,
394 AttributeList::AS_ContextSensitiveKeyword);
Douglas Gregor813a0662015-06-19 18:14:38 +0000395 };
396
397 if (D.getNumTypeObjects() > 0) {
398 // Add the attribute to the declarator chunk nearest the declarator.
399 auto nullabilityAttr = getNullabilityAttr();
400 DeclaratorChunk &chunk = D.getTypeObject(0);
401 nullabilityAttr->setNext(chunk.getAttrListRef());
402 chunk.getAttrListRef() = nullabilityAttr;
403 } else if (!addedToDeclSpec) {
404 // Otherwise, just put it on the declaration specifiers (if one
405 // isn't there already).
406 D.getMutableDeclSpec().addAttributes(getNullabilityAttr());
407 addedToDeclSpec = true;
408 }
409}
410
Douglas Gregor85f3f952015-07-07 03:57:15 +0000411/// Parse an Objective-C type parameter list, if present, or capture
412/// the locations of the protocol identifiers for a list of protocol
413/// references.
414///
415/// objc-type-parameter-list:
416/// '<' objc-type-parameter (',' objc-type-parameter)* '>'
417///
418/// objc-type-parameter:
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000419/// objc-type-parameter-variance? identifier objc-type-parameter-bound[opt]
Douglas Gregor85f3f952015-07-07 03:57:15 +0000420///
421/// objc-type-parameter-bound:
422/// ':' type-name
423///
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000424/// objc-type-parameter-variance:
425/// '__covariant'
426/// '__contravariant'
427///
Douglas Gregor85f3f952015-07-07 03:57:15 +0000428/// \param lAngleLoc The location of the starting '<'.
429///
430/// \param protocolIdents Will capture the list of identifiers, if the
431/// angle brackets contain a list of protocol references rather than a
432/// type parameter list.
433///
434/// \param rAngleLoc The location of the ending '>'.
435ObjCTypeParamList *Parser::parseObjCTypeParamListOrProtocolRefs(
436 SourceLocation &lAngleLoc,
437 SmallVectorImpl<IdentifierLocPair> &protocolIdents,
438 SourceLocation &rAngleLoc,
439 bool mayBeProtocolList) {
440 assert(Tok.is(tok::less) && "Not at the beginning of a type parameter list");
441
442 // Within the type parameter list, don't treat '>' as an operator.
443 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
444
445 // Local function to "flush" the protocol identifiers, turning them into
446 // type parameters.
447 SmallVector<Decl *, 4> typeParams;
448 auto makeProtocolIdentsIntoTypeParameters = [&]() {
Douglas Gregore83b9562015-07-07 03:57:53 +0000449 unsigned index = 0;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000450 for (const auto &pair : protocolIdents) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000451 DeclResult typeParam = Actions.actOnObjCTypeParam(
452 getCurScope(),
453 ObjCTypeParamVariance::Invariant,
454 SourceLocation(),
455 index++,
456 pair.first,
457 pair.second,
458 SourceLocation(),
459 ParsedType());
Douglas Gregor85f3f952015-07-07 03:57:15 +0000460 if (typeParam.isUsable())
461 typeParams.push_back(typeParam.get());
462 }
463
464 protocolIdents.clear();
465 mayBeProtocolList = false;
466 };
467
468 bool invalid = false;
469 lAngleLoc = ConsumeToken();
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000470
Douglas Gregor85f3f952015-07-07 03:57:15 +0000471 do {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000472 // Parse the variance, if any.
473 SourceLocation varianceLoc;
474 ObjCTypeParamVariance variance = ObjCTypeParamVariance::Invariant;
475 if (Tok.is(tok::kw___covariant) || Tok.is(tok::kw___contravariant)) {
476 variance = Tok.is(tok::kw___covariant)
477 ? ObjCTypeParamVariance::Covariant
478 : ObjCTypeParamVariance::Contravariant;
479 varianceLoc = ConsumeToken();
480
481 // Once we've seen a variance specific , we know this is not a
482 // list of protocol references.
483 if (mayBeProtocolList) {
484 // Up until now, we have been queuing up parameters because they
485 // might be protocol references. Turn them into parameters now.
486 makeProtocolIdentsIntoTypeParameters();
487 }
488 }
489
Douglas Gregor85f3f952015-07-07 03:57:15 +0000490 // Parse the identifier.
491 if (!Tok.is(tok::identifier)) {
492 // Code completion.
493 if (Tok.is(tok::code_completion)) {
494 // FIXME: If these aren't protocol references, we'll need different
495 // completions.
496 Actions.CodeCompleteObjCProtocolReferences(protocolIdents.data(),
497 protocolIdents.size());
498 cutOffParsing();
499
500 // FIXME: Better recovery here?.
501 return nullptr;
502 }
503
504 Diag(Tok, diag::err_objc_expected_type_parameter);
505 invalid = true;
506 break;
507 }
508
509 IdentifierInfo *paramName = Tok.getIdentifierInfo();
510 SourceLocation paramLoc = ConsumeToken();
511
512 // If there is a bound, parse it.
513 SourceLocation colonLoc;
514 TypeResult boundType;
515 if (TryConsumeToken(tok::colon, colonLoc)) {
516 // Once we've seen a bound, we know this is not a list of protocol
517 // references.
518 if (mayBeProtocolList) {
519 // Up until now, we have been queuing up parameters because they
520 // might be protocol references. Turn them into parameters now.
521 makeProtocolIdentsIntoTypeParameters();
522 }
523
524 // type-name
525 boundType = ParseTypeName();
526 if (boundType.isInvalid())
527 invalid = true;
528 } else if (mayBeProtocolList) {
529 // If this could still be a protocol list, just capture the identifier.
530 // We don't want to turn it into a parameter.
531 protocolIdents.push_back(std::make_pair(paramName, paramLoc));
532 continue;
533 }
534
535 // Create the type parameter.
536 DeclResult typeParam = Actions.actOnObjCTypeParam(getCurScope(),
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000537 variance,
538 varianceLoc,
Douglas Gregore83b9562015-07-07 03:57:53 +0000539 typeParams.size(),
Douglas Gregor85f3f952015-07-07 03:57:15 +0000540 paramName,
541 paramLoc,
542 colonLoc,
543 boundType.isUsable()
544 ? boundType.get()
545 : ParsedType());
546 if (typeParam.isUsable())
547 typeParams.push_back(typeParam.get());
548 } while (TryConsumeToken(tok::comma));
549
550 // Parse the '>'.
551 if (invalid) {
552 SkipUntil(tok::greater, tok::at, StopBeforeMatch);
553 if (Tok.is(tok::greater))
554 ConsumeToken();
555 } else if (ParseGreaterThanInTemplateList(rAngleLoc,
556 /*ConsumeLastToken=*/true,
557 /*ObjCGenericList=*/true)) {
558 Diag(lAngleLoc, diag::note_matching) << "'<'";
559 SkipUntil({tok::greater, tok::greaterequal, tok::at, tok::minus,
560 tok::minus, tok::plus, tok::colon, tok::l_paren, tok::l_brace,
561 tok::comma, tok::semi },
562 StopBeforeMatch);
563 if (Tok.is(tok::greater))
564 ConsumeToken();
565 }
566
567 if (mayBeProtocolList) {
568 // A type parameter list must be followed by either a ':' (indicating the
569 // presence of a superclass) or a '(' (indicating that this is a category
570 // or extension). This disambiguates between an objc-type-parameter-list
571 // and a objc-protocol-refs.
572 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_paren)) {
573 // Returning null indicates that we don't have a type parameter list.
574 // The results the caller needs to handle the protocol references are
575 // captured in the reference parameters already.
576 return nullptr;
577 }
578
579 // We have a type parameter list that looks like a list of protocol
580 // references. Turn that parameter list into type parameters.
581 makeProtocolIdentsIntoTypeParameters();
582 }
583
584 // Form the type parameter list.
585 ObjCTypeParamList *list = Actions.actOnObjCTypeParamList(
586 getCurScope(),
587 lAngleLoc,
588 typeParams,
589 rAngleLoc);
590
591 // Clear out the angle locations; they're used by the caller to indicate
592 // whether there are any protocol references.
593 lAngleLoc = SourceLocation();
594 rAngleLoc = SourceLocation();
595 return list;
596}
597
598/// Parse an objc-type-parameter-list.
599ObjCTypeParamList *Parser::parseObjCTypeParamList() {
600 SourceLocation lAngleLoc;
601 SmallVector<IdentifierLocPair, 1> protocolIdents;
602 SourceLocation rAngleLoc;
603 return parseObjCTypeParamListOrProtocolRefs(lAngleLoc, protocolIdents,
604 rAngleLoc,
605 /*mayBeProtocolList=*/false);
606}
607
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000608/// objc-interface-decl-list:
609/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000610/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000611/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000612/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000613/// objc-interface-decl-list declaration
614/// objc-interface-decl-list ';'
615///
Steve Naroff99264b42007-08-22 16:35:03 +0000616/// objc-method-requirement: [OBJC2]
617/// @required
618/// @optional
619///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000620void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
621 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000622 SmallVector<Decl *, 32> allMethods;
623 SmallVector<Decl *, 16> allProperties;
624 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000625 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000626
Ted Kremenekc7c64312010-01-07 01:20:12 +0000627 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000628
Steve Naroff99264b42007-08-22 16:35:03 +0000629 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000630 // If this is a method prototype, parse it.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000631 if (Tok.isOneOf(tok::minus, tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000632 if (Decl *methodPrototype =
633 ParseObjCMethodPrototype(MethodImplKind, false))
634 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000635 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
636 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000637 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
638 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000639 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000640 if (Tok.is(tok::semi))
641 ConsumeToken();
642 }
Steve Naroff99264b42007-08-22 16:35:03 +0000643 continue;
644 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000645 if (Tok.is(tok::l_paren)) {
646 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000647 ParseObjCMethodDecl(Tok.getLocation(),
648 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000649 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000650 continue;
651 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000652 // Ignore excess semicolons.
653 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000654 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000655 continue;
656 }
Mike Stump11289f42009-09-09 15:08:12 +0000657
Chris Lattnerda9fb152008-10-20 06:10:06 +0000658 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000659 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000660 break;
Mike Stump11289f42009-09-09 15:08:12 +0000661
Douglas Gregorf1934162010-01-13 21:24:21 +0000662 // Code completion within an Objective-C interface.
663 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000664 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000665 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000666 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000667 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000668 }
669
Chris Lattner038a3e32008-10-20 05:46:22 +0000670 // If we don't have an @ directive, parse it as a function definition.
671 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000672 // The code below does not consume '}'s because it is afraid of eating the
673 // end of a namespace. Because of the way this code is structured, an
674 // erroneous r_brace would cause an infinite loop if not handled here.
675 if (Tok.is(tok::r_brace))
676 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000677 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000678 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000679 continue;
680 }
Mike Stump11289f42009-09-09 15:08:12 +0000681
Chris Lattner038a3e32008-10-20 05:46:22 +0000682 // Otherwise, we have an @ directive, eat the @.
683 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000684 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000685 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000686 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000687 }
688
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000689 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000690
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000691 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000692 AtEnd.setBegin(AtLoc);
693 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000694 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000695 } else if (DirectiveKind == tok::objc_not_keyword) {
696 Diag(Tok, diag::err_objc_unknown_at);
697 SkipUntil(tok::semi);
698 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000699 }
Mike Stump11289f42009-09-09 15:08:12 +0000700
Chris Lattnerda9fb152008-10-20 06:10:06 +0000701 // Eat the identifier.
702 ConsumeToken();
703
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000704 switch (DirectiveKind) {
705 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000706 // FIXME: If someone forgets an @end on a protocol, this loop will
707 // continue to eat up tons of stuff and spew lots of nonsense errors. It
708 // would probably be better to bail out if we saw an @class or @interface
709 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000710 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000711 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000712 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000713 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000714
715 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000716 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000717 Diag(AtLoc, diag::err_objc_missing_end)
718 << FixItHint::CreateInsertion(AtLoc, "@end\n");
719 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
720 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000721 ConsumeToken();
722 break;
723
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000724 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000725 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000726 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000727 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000728 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000729 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000730 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000731 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000732 break;
Mike Stump11289f42009-09-09 15:08:12 +0000733
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000734 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000735 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000736 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000737
Chris Lattner038a3e32008-10-20 05:46:22 +0000738 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000739 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000740 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000741 if (Tok.is(tok::l_paren)) {
742 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000743 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000744 }
Mike Stump11289f42009-09-09 15:08:12 +0000745
Douglas Gregor813a0662015-06-19 18:14:38 +0000746 bool addedToDeclSpec = false;
Benjamin Kramera39beb92014-09-03 11:06:10 +0000747 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
748 if (FD.D.getIdentifier() == nullptr) {
749 Diag(AtLoc, diag::err_objc_property_requires_field_name)
750 << FD.D.getSourceRange();
751 return;
752 }
753 if (FD.BitfieldSize) {
754 Diag(AtLoc, diag::err_objc_property_bitfield)
755 << FD.D.getSourceRange();
756 return;
757 }
758
Douglas Gregor813a0662015-06-19 18:14:38 +0000759 // Map a nullability property attribute to a context-sensitive keyword
760 // attribute.
761 if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
762 addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
763 OCDS.getNullabilityLoc(),
764 addedToDeclSpec);
765
Benjamin Kramera39beb92014-09-03 11:06:10 +0000766 // Install the property declarator into interfaceDecl.
767 IdentifierInfo *SelName =
768 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
769
770 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
771 IdentifierInfo *SetterName = OCDS.getSetterName();
772 Selector SetterSel;
773 if (SetterName)
774 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
775 else
776 SetterSel = SelectorTable::constructSetterSelector(
777 PP.getIdentifierTable(), PP.getSelectorTable(),
778 FD.D.getIdentifier());
779 bool isOverridingProperty = false;
780 Decl *Property = Actions.ActOnProperty(
781 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
782 &isOverridingProperty, MethodImplKind);
783 if (!isOverridingProperty)
784 allProperties.push_back(Property);
785
786 FD.complete(Property);
787 };
John McCallcfefb6d2009-11-03 02:38:08 +0000788
Chris Lattner038a3e32008-10-20 05:46:22 +0000789 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000790 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000791 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000792
John McCall405988b2011-03-26 01:53:26 +0000793 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000794 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000795 }
Steve Naroff99264b42007-08-22 16:35:03 +0000796 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000797
798 // We break out of the big loop in two cases: when we see @end or when we see
799 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000800 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000801 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000802 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000803 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000804 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000805 } else {
806 Diag(Tok, diag::err_objc_missing_end)
807 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
808 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
809 << (int) Actions.getObjCContainerKind();
810 AtEnd.setBegin(Tok.getLocation());
811 AtEnd.setEnd(Tok.getLocation());
812 }
Mike Stump11289f42009-09-09 15:08:12 +0000813
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000814 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000815 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000816 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000817}
818
Douglas Gregor813a0662015-06-19 18:14:38 +0000819/// Diagnose redundant or conflicting nullability information.
820static void diagnoseRedundantPropertyNullability(Parser &P,
821 ObjCDeclSpec &DS,
822 NullabilityKind nullability,
823 SourceLocation nullabilityLoc){
824 if (DS.getNullability() == nullability) {
825 P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000826 << DiagNullabilityKind(nullability, true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000827 << SourceRange(DS.getNullabilityLoc());
828 return;
829 }
830
831 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000832 << DiagNullabilityKind(nullability, true)
833 << DiagNullabilityKind(DS.getNullability(), true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000834 << SourceRange(DS.getNullabilityLoc());
835}
836
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000837/// Parse property attribute declarations.
838///
839/// property-attr-decl: '(' property-attrlist ')'
840/// property-attrlist:
841/// property-attribute
842/// property-attrlist ',' property-attribute
843/// property-attribute:
844/// getter '=' identifier
845/// setter '=' identifier ':'
846/// readonly
847/// readwrite
848/// assign
849/// retain
850/// copy
851/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000852/// atomic
853/// strong
854/// weak
855/// unsafe_unretained
Douglas Gregor813a0662015-06-19 18:14:38 +0000856/// nonnull
857/// nullable
858/// null_unspecified
Douglas Gregor849ebc22015-06-19 18:14:46 +0000859/// null_resettable
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000860///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000861void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000862 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000863 BalancedDelimiterTracker T(*this, tok::l_paren);
864 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000865
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000866 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000867 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000868 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000869 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000870 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000871 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000872
Chris Lattner76619232008-10-20 07:22:18 +0000873 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000874 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000875 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000876 return;
877 }
Mike Stump11289f42009-09-09 15:08:12 +0000878
Chris Lattner1db33542008-10-20 07:37:22 +0000879 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000880
Chris Lattner68e48682008-11-20 04:42:34 +0000881 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000882 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000883 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000884 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000885 else if (II->isStr("unsafe_unretained"))
886 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000887 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000888 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000889 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000890 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000891 else if (II->isStr("strong"))
892 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000893 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000894 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000895 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000896 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000897 else if (II->isStr("atomic"))
898 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000899 else if (II->isStr("weak"))
900 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000901 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000902 bool IsSetter = II->getNameStart()[0] == 's';
903
Chris Lattner825bca12008-10-20 07:39:53 +0000904 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000905 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
906 diag::err_objc_expected_equal_for_getter;
907
Alp Toker383d2c42014-01-01 03:08:43 +0000908 if (ExpectAndConsume(tok::equal, DiagID)) {
909 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000910 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000911 }
Mike Stump11289f42009-09-09 15:08:12 +0000912
Douglas Gregorc8537c52009-11-19 07:41:15 +0000913 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000914 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000915 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000916 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000917 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000918 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000919 }
920
Anders Carlssonfe15a782010-10-02 17:45:21 +0000921
922 SourceLocation SelLoc;
923 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
924
925 if (!SelIdent) {
926 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
927 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000928 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000929 return;
930 }
Mike Stump11289f42009-09-09 15:08:12 +0000931
Anders Carlssonfe15a782010-10-02 17:45:21 +0000932 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000933 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000934 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000935
Alp Toker383d2c42014-01-01 03:08:43 +0000936 if (ExpectAndConsume(tok::colon,
937 diag::err_expected_colon_after_setter_name)) {
938 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000939 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000940 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000941 } else {
942 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000943 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000944 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000945 } else if (II->isStr("nonnull")) {
946 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
947 diagnoseRedundantPropertyNullability(*this, DS,
948 NullabilityKind::NonNull,
949 Tok.getLocation());
950 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
951 DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
952 } else if (II->isStr("nullable")) {
953 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
954 diagnoseRedundantPropertyNullability(*this, DS,
955 NullabilityKind::Nullable,
956 Tok.getLocation());
957 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
958 DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
959 } else if (II->isStr("null_unspecified")) {
960 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
961 diagnoseRedundantPropertyNullability(*this, DS,
962 NullabilityKind::Unspecified,
963 Tok.getLocation());
964 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
965 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000966 } else if (II->isStr("null_resettable")) {
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);
973
974 // Also set the null_resettable bit.
975 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
Chris Lattner825bca12008-10-20 07:39:53 +0000976 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000977 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000978 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000979 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000980 }
Mike Stump11289f42009-09-09 15:08:12 +0000981
Chris Lattner1db33542008-10-20 07:37:22 +0000982 if (Tok.isNot(tok::comma))
983 break;
Mike Stump11289f42009-09-09 15:08:12 +0000984
Chris Lattner1db33542008-10-20 07:37:22 +0000985 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000986 }
Mike Stump11289f42009-09-09 15:08:12 +0000987
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000988 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000989}
990
Steve Naroff09bf8152007-09-06 21:24:23 +0000991/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000992/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000993/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000994///
995/// objc-instance-method: '-'
996/// objc-class-method: '+'
997///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000998/// objc-method-attributes: [OBJC2]
999/// __attribute__((deprecated))
1000///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001001Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001002 bool MethodDefinition) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001003 assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +00001004
Mike Stump11289f42009-09-09 15:08:12 +00001005 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +00001006 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001007 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001008 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +00001009 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +00001010 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +00001011 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +00001012}
1013
1014/// objc-selector:
1015/// identifier
1016/// one of
1017/// enum struct union if else while do for switch case default
1018/// break continue return goto asm sizeof typeof __alignof
1019/// unsigned long const short volatile signed restrict _Complex
1020/// in out inout bycopy byref oneway int char float double void _Bool
1021///
Chris Lattner4f472a32009-04-11 18:13:45 +00001022IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +00001023
Chris Lattner5700fab2007-10-07 02:00:24 +00001024 switch (Tok.getKind()) {
1025 default:
Craig Topper161e4db2014-05-21 06:02:52 +00001026 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001027 case tok::ampamp:
1028 case tok::ampequal:
1029 case tok::amp:
1030 case tok::pipe:
1031 case tok::tilde:
1032 case tok::exclaim:
1033 case tok::exclaimequal:
1034 case tok::pipepipe:
1035 case tok::pipeequal:
1036 case tok::caret:
1037 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +00001038 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +00001039 if (isLetter(ThisTok[0])) {
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001040 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
1041 Tok.setKind(tok::identifier);
1042 SelectorLoc = ConsumeToken();
1043 return II;
1044 }
Craig Topper161e4db2014-05-21 06:02:52 +00001045 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001046 }
1047
Chris Lattner5700fab2007-10-07 02:00:24 +00001048 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001049 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +00001050 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001051 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001052 case tok::kw_break:
1053 case tok::kw_case:
1054 case tok::kw_catch:
1055 case tok::kw_char:
1056 case tok::kw_class:
1057 case tok::kw_const:
1058 case tok::kw_const_cast:
1059 case tok::kw_continue:
1060 case tok::kw_default:
1061 case tok::kw_delete:
1062 case tok::kw_do:
1063 case tok::kw_double:
1064 case tok::kw_dynamic_cast:
1065 case tok::kw_else:
1066 case tok::kw_enum:
1067 case tok::kw_explicit:
1068 case tok::kw_export:
1069 case tok::kw_extern:
1070 case tok::kw_false:
1071 case tok::kw_float:
1072 case tok::kw_for:
1073 case tok::kw_friend:
1074 case tok::kw_goto:
1075 case tok::kw_if:
1076 case tok::kw_inline:
1077 case tok::kw_int:
1078 case tok::kw_long:
1079 case tok::kw_mutable:
1080 case tok::kw_namespace:
1081 case tok::kw_new:
1082 case tok::kw_operator:
1083 case tok::kw_private:
1084 case tok::kw_protected:
1085 case tok::kw_public:
1086 case tok::kw_register:
1087 case tok::kw_reinterpret_cast:
1088 case tok::kw_restrict:
1089 case tok::kw_return:
1090 case tok::kw_short:
1091 case tok::kw_signed:
1092 case tok::kw_sizeof:
1093 case tok::kw_static:
1094 case tok::kw_static_cast:
1095 case tok::kw_struct:
1096 case tok::kw_switch:
1097 case tok::kw_template:
1098 case tok::kw_this:
1099 case tok::kw_throw:
1100 case tok::kw_true:
1101 case tok::kw_try:
1102 case tok::kw_typedef:
1103 case tok::kw_typeid:
1104 case tok::kw_typename:
1105 case tok::kw_typeof:
1106 case tok::kw_union:
1107 case tok::kw_unsigned:
1108 case tok::kw_using:
1109 case tok::kw_virtual:
1110 case tok::kw_void:
1111 case tok::kw_volatile:
1112 case tok::kw_wchar_t:
1113 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +00001114 case tok::kw__Bool:
1115 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001116 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +00001117 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001118 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +00001119 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +00001120 }
Steve Naroff99264b42007-08-22 16:35:03 +00001121}
1122
Fariborz Jahanian83615522008-01-02 22:54:34 +00001123/// objc-for-collection-in: 'in'
1124///
Fariborz Jahanian3622e592008-01-04 23:04:08 +00001125bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001126 // FIXME: May have to do additional look-ahead to only allow for
1127 // valid tokens following an 'in'; such as an identifier, unary operators,
1128 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001129 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +00001130 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +00001131}
1132
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001133/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +00001134/// qualifier list and builds their bitmask representation in the input
1135/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +00001136///
1137/// objc-type-qualifiers:
1138/// objc-type-qualifier
1139/// objc-type-qualifiers objc-type-qualifier
1140///
Douglas Gregor813a0662015-06-19 18:14:38 +00001141/// objc-type-qualifier:
1142/// 'in'
1143/// 'out'
1144/// 'inout'
1145/// 'oneway'
1146/// 'bycopy'
1147/// 'byref'
1148/// 'nonnull'
1149/// 'nullable'
1150/// 'null_unspecified'
1151///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001152void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001153 Declarator::TheContext Context) {
1154 assert(Context == Declarator::ObjCParameterContext ||
1155 Context == Declarator::ObjCResultContext);
1156
Chris Lattner61511e12007-12-12 06:56:32 +00001157 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +00001158 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +00001159 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +00001160 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001161 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +00001162 }
1163
Chris Lattner5e530bc2007-12-27 19:57:00 +00001164 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +00001165 return;
Mike Stump11289f42009-09-09 15:08:12 +00001166
Chris Lattner61511e12007-12-12 06:56:32 +00001167 const IdentifierInfo *II = Tok.getIdentifierInfo();
1168 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001169 if (II != ObjCTypeQuals[i] ||
1170 NextToken().is(tok::less) ||
1171 NextToken().is(tok::coloncolon))
Chris Lattner61511e12007-12-12 06:56:32 +00001172 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001173
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001174 ObjCDeclSpec::ObjCDeclQualifier Qual;
Douglas Gregor813a0662015-06-19 18:14:38 +00001175 NullabilityKind Nullability;
Chris Lattner61511e12007-12-12 06:56:32 +00001176 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +00001177 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001178 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
1179 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
1180 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
1181 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
1182 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
1183 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Douglas Gregor813a0662015-06-19 18:14:38 +00001184
1185 case objc_nonnull:
1186 Qual = ObjCDeclSpec::DQ_CSNullability;
1187 Nullability = NullabilityKind::NonNull;
1188 break;
1189
1190 case objc_nullable:
1191 Qual = ObjCDeclSpec::DQ_CSNullability;
1192 Nullability = NullabilityKind::Nullable;
1193 break;
1194
1195 case objc_null_unspecified:
1196 Qual = ObjCDeclSpec::DQ_CSNullability;
1197 Nullability = NullabilityKind::Unspecified;
1198 break;
Chris Lattner61511e12007-12-12 06:56:32 +00001199 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001200
1201 // FIXME: Diagnose redundant specifiers.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001202 DS.setObjCDeclQualifier(Qual);
Douglas Gregor813a0662015-06-19 18:14:38 +00001203 if (Qual == ObjCDeclSpec::DQ_CSNullability)
1204 DS.setNullability(Tok.getLocation(), Nullability);
1205
Chris Lattner61511e12007-12-12 06:56:32 +00001206 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001207 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +00001208 break;
1209 }
Mike Stump11289f42009-09-09 15:08:12 +00001210
Chris Lattner61511e12007-12-12 06:56:32 +00001211 // If this wasn't a recognized qualifier, bail out.
1212 if (II) return;
1213 }
1214}
1215
John McCalla55902b2011-10-01 09:56:14 +00001216/// Take all the decl attributes out of the given list and add
1217/// them to the given attribute set.
1218static void takeDeclAttributes(ParsedAttributes &attrs,
1219 AttributeList *list) {
1220 while (list) {
1221 AttributeList *cur = list;
1222 list = cur->getNext();
1223
1224 if (!cur->isUsedAsTypeAttr()) {
1225 // Clear out the next pointer. We're really completely
1226 // destroying the internal invariants of the declarator here,
1227 // but it doesn't matter because we're done with it.
Craig Topper161e4db2014-05-21 06:02:52 +00001228 cur->setNext(nullptr);
John McCalla55902b2011-10-01 09:56:14 +00001229 attrs.add(cur);
1230 }
1231 }
1232}
1233
1234/// takeDeclAttributes - Take all the decl attributes from the given
1235/// declarator and add them to the given list.
1236static void takeDeclAttributes(ParsedAttributes &attrs,
1237 Declarator &D) {
1238 // First, take ownership of all attributes.
1239 attrs.getPool().takeAllFrom(D.getAttributePool());
1240 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
1241
1242 // Now actually move the attributes over.
1243 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
1244 takeDeclAttributes(attrs, D.getAttributes());
1245 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
1246 takeDeclAttributes(attrs,
1247 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
1248}
1249
Chris Lattner61511e12007-12-12 06:56:32 +00001250/// objc-type-name:
1251/// '(' objc-type-qualifiers[opt] type-name ')'
1252/// '(' objc-type-qualifiers[opt] ')'
1253///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001254ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001255 Declarator::TheContext context,
1256 ParsedAttributes *paramAttrs) {
1257 assert(context == Declarator::ObjCParameterContext ||
1258 context == Declarator::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +00001259 assert((paramAttrs != nullptr) ==
1260 (context == Declarator::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +00001261
Chris Lattner0ef13522007-10-09 17:51:17 +00001262 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +00001263
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001264 BalancedDelimiterTracker T(*this, tok::l_paren);
1265 T.consumeOpen();
1266
Chris Lattner2ebb1782008-08-23 01:48:03 +00001267 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001268 ObjCDeclContextSwitch ObjCDC(*this);
1269
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001270 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001271 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +00001272
John McCallba7bf592010-08-24 05:47:05 +00001273 ParsedType Ty;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001274 if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
John McCalla55902b2011-10-01 09:56:14 +00001275 // Parse an abstract declarator.
1276 DeclSpec declSpec(AttrFactory);
1277 declSpec.setObjCQualifiers(&DS);
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001278 DeclSpecContext dsContext = DSC_normal;
1279 if (context == Declarator::ObjCResultContext)
1280 dsContext = DSC_objc_method_result;
1281 ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
Fariborz Jahanianb6499eb2012-05-29 21:52:45 +00001282 declSpec.SetRangeEnd(Tok.getLocation());
John McCalla55902b2011-10-01 09:56:14 +00001283 Declarator declarator(declSpec, context);
1284 ParseDeclarator(declarator);
1285
1286 // If that's not invalid, extract a type.
1287 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001288 // Map a nullability specifier to a context-sensitive keyword attribute.
1289 bool addedToDeclSpec = false;
1290 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1291 addContextSensitiveTypeNullability(*this, declarator,
1292 DS.getNullability(),
1293 DS.getNullabilityLoc(),
1294 addedToDeclSpec);
1295
John McCalla55902b2011-10-01 09:56:14 +00001296 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1297 if (!type.isInvalid())
1298 Ty = type.get();
1299
1300 // If we're parsing a parameter, steal all the decl attributes
1301 // and add them to the decl spec.
1302 if (context == Declarator::ObjCParameterContext)
1303 takeDeclAttributes(*paramAttrs, declarator);
1304 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001305 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001306
Steve Naroff90255b42008-10-21 14:15:04 +00001307 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001308 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001309 else if (Tok.getLocation() == TypeStartLoc) {
1310 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001311 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001312 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001313 } else {
1314 // Otherwise, we found *something*, but didn't get a ')' in the right
1315 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001316 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001317 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001318 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001319}
1320
1321/// objc-method-decl:
1322/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001323/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001324/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001325/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001326///
1327/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001328/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001329/// objc-keyword-selector objc-keyword-decl
1330///
1331/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001332/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1333/// objc-selector ':' objc-keyword-attributes[opt] identifier
1334/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1335/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001336///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001337/// objc-parmlist:
1338/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001339///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001340/// objc-parms:
1341/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001342///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001343/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001344/// , ...
1345///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001346/// objc-keyword-attributes: [OBJC2]
1347/// __attribute__((unused))
1348///
John McCall48871652010-08-21 09:40:31 +00001349Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001350 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001351 tok::ObjCKeywordKind MethodImplKind,
1352 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001353 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001354
Douglas Gregor636a61e2010-04-07 00:21:17 +00001355 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001356 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001357 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001358 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001359 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001360 }
1361
Chris Lattner2ebb1782008-08-23 01:48:03 +00001362 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001363 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001364 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001365 if (Tok.is(tok::l_paren))
Craig Topper161e4db2014-05-21 06:02:52 +00001366 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext,
1367 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001368
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001369 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001370 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001371 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001372 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001373
Douglas Gregor636a61e2010-04-07 00:21:17 +00001374 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001375 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001376 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001377 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001378 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001379 }
1380
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001381 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001382 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001383 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001384
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001385 // An unnamed colon is valid.
1386 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001387 Diag(Tok, diag::err_expected_selector_for_method)
1388 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001389 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001390 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001391 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001392 }
Mike Stump11289f42009-09-09 15:08:12 +00001393
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001394 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001395 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001396 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001397 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001398 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001399
Chris Lattner5700fab2007-10-07 02:00:24 +00001400 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001401 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001402 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001403 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001404 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001405 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001406 methodAttrs.getList(), MethodImplKind,
1407 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001408 PD.complete(Result);
1409 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001410 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001411
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001412 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001413 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001414 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001415 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1416 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001417
1418 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001419 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001420 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001421 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001422
Chris Lattner5700fab2007-10-07 02:00:24 +00001423 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001424 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001425 break;
Mike Stump11289f42009-09-09 15:08:12 +00001426
John McCallba7bf592010-08-24 05:47:05 +00001427 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001428 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001429 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1430 Declarator::ObjCParameterContext,
1431 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001432
Chris Lattner5700fab2007-10-07 02:00:24 +00001433 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001434 // Regardless, collect all the attributes we've parsed so far.
Craig Topper161e4db2014-05-21 06:02:52 +00001435 ArgInfo.ArgAttrs = nullptr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001436 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001437 MaybeParseGNUAttributes(paramAttrs);
1438 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001439 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001440
Douglas Gregor45879692010-07-08 23:37:41 +00001441 // Code completion for the next piece of the selector.
1442 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001443 KeyIdents.push_back(SelIdent);
1444 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1445 mType == tok::minus,
1446 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001447 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001448 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001449 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001450 }
1451
Chris Lattner0ef13522007-10-09 17:51:17 +00001452 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001453 Diag(Tok, diag::err_expected)
1454 << tok::identifier; // missing argument name.
Chris Lattner5700fab2007-10-07 02:00:24 +00001455 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001456 }
Mike Stump11289f42009-09-09 15:08:12 +00001457
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001458 ArgInfo.Name = Tok.getIdentifierInfo();
1459 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001460 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001461
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001462 ArgInfos.push_back(ArgInfo);
1463 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001464 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001465
John McCall084e83d2011-03-24 11:26:52 +00001466 // Make sure the attributes persist.
1467 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1468
Douglas Gregor95887f92010-07-08 23:20:03 +00001469 // Code completion for the next piece of the selector.
1470 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001471 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1472 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001473 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001474 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001475 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001476 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001477 }
1478
Chris Lattner5700fab2007-10-07 02:00:24 +00001479 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001480 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001481 if (!SelIdent && Tok.isNot(tok::colon))
1482 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001483 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001484 SourceLocation ColonLoc = Tok.getLocation();
1485 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001486 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1487 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1488 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001489 }
1490 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001491 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001492 }
Mike Stump11289f42009-09-09 15:08:12 +00001493
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001494 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001495 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001496 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001497 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001498 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001499 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001500 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001501 ConsumeToken();
1502 break;
1503 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001504 if (!cStyleParamWarned) {
1505 Diag(Tok, diag::warn_cstyle_param);
1506 cStyleParamWarned = true;
1507 }
John McCall084e83d2011-03-24 11:26:52 +00001508 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001509 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001510 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001511 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1512 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001513 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001514 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001515 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1516 ParmDecl.getIdentifierLoc(),
1517 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001518 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001519 }
Mike Stump11289f42009-09-09 15:08:12 +00001520
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001521 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001522 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001523 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001524 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001525
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001526 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001527 return nullptr;
1528
Chris Lattner5700fab2007-10-07 02:00:24 +00001529 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1530 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001531 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001532 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001533 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001534 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001535 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001536 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001537 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001538
John McCall28a6aea2009-11-04 02:18:39 +00001539 PD.complete(Result);
1540 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001541}
1542
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001543/// objc-protocol-refs:
1544/// '<' identifier-list '>'
1545///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001546bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001547ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1548 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001549 bool WarnOnDeclarations, bool ForObjCContainer,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001550 SourceLocation &LAngleLoc, SourceLocation &EndLoc,
1551 bool consumeLastToken) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001552 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001553
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001554 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001555
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001556 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001557
Chris Lattner3bbae002008-07-26 04:03:38 +00001558 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001559 if (Tok.is(tok::code_completion)) {
1560 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1561 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001562 cutOffParsing();
1563 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001564 }
1565
Chris Lattner3bbae002008-07-26 04:03:38 +00001566 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001567 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001568 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001569 return true;
1570 }
1571 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1572 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001573 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001574 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001575
Alp Toker383d2c42014-01-01 03:08:43 +00001576 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001577 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001578 }
Mike Stump11289f42009-09-09 15:08:12 +00001579
Chris Lattner3bbae002008-07-26 04:03:38 +00001580 // Consume the '>'.
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001581 if (ParseGreaterThanInTemplateList(EndLoc, consumeLastToken,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001582 /*ObjCGenericList=*/false))
Chris Lattner3bbae002008-07-26 04:03:38 +00001583 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001584
Chris Lattner3bbae002008-07-26 04:03:38 +00001585 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001586 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Chris Lattner3bbae002008-07-26 04:03:38 +00001587 &ProtocolIdents[0], ProtocolIdents.size(),
1588 Protocols);
1589 return false;
1590}
1591
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001592TypeResult Parser::parseObjCProtocolQualifierType(SourceLocation &rAngleLoc) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001593 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001594 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001595
1596 SourceLocation lAngleLoc;
1597 SmallVector<Decl *, 8> protocols;
1598 SmallVector<SourceLocation, 8> protocolLocs;
1599 (void)ParseObjCProtocolReferences(protocols, protocolLocs, false, false,
1600 lAngleLoc, rAngleLoc,
1601 /*consumeLastToken=*/true);
1602 TypeResult result = Actions.actOnObjCProtocolQualifierType(lAngleLoc,
1603 protocols,
1604 protocolLocs,
1605 rAngleLoc);
1606 if (result.isUsable()) {
1607 Diag(lAngleLoc, diag::warn_objc_protocol_qualifier_missing_id)
1608 << FixItHint::CreateInsertion(lAngleLoc, "id")
1609 << SourceRange(lAngleLoc, rAngleLoc);
1610 }
1611
1612 return result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001613}
1614
Douglas Gregore9d95f12015-07-07 03:57:35 +00001615/// Parse Objective-C type arguments or protocol qualifiers.
1616///
1617/// objc-type-arguments:
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001618/// '<' type-name '...'[opt] (',' type-name '...'[opt])* '>'
Douglas Gregore9d95f12015-07-07 03:57:35 +00001619///
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001620void Parser::parseObjCTypeArgsOrProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001621 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001622 SourceLocation &typeArgsLAngleLoc,
1623 SmallVectorImpl<ParsedType> &typeArgs,
1624 SourceLocation &typeArgsRAngleLoc,
1625 SourceLocation &protocolLAngleLoc,
1626 SmallVectorImpl<Decl *> &protocols,
1627 SmallVectorImpl<SourceLocation> &protocolLocs,
1628 SourceLocation &protocolRAngleLoc,
1629 bool consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001630 bool warnOnIncompleteProtocols) {
1631 assert(Tok.is(tok::less) && "Not at the start of type args or protocols");
1632 SourceLocation lAngleLoc = ConsumeToken();
1633
1634 // Whether all of the elements we've parsed thus far are single
1635 // identifiers, which might be types or might be protocols.
1636 bool allSingleIdentifiers = true;
1637 SmallVector<IdentifierInfo *, 4> identifiers;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001638 SmallVectorImpl<SourceLocation> &identifierLocs = protocolLocs;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001639
1640 // Parse a list of comma-separated identifiers, bailing out if we
1641 // see something different.
1642 do {
1643 // Parse a single identifier.
1644 if (Tok.is(tok::identifier) &&
1645 (NextToken().is(tok::comma) ||
1646 NextToken().is(tok::greater) ||
1647 NextToken().is(tok::greatergreater))) {
1648 identifiers.push_back(Tok.getIdentifierInfo());
1649 identifierLocs.push_back(ConsumeToken());
1650 continue;
1651 }
1652
1653 if (Tok.is(tok::code_completion)) {
1654 // FIXME: Also include types here.
1655 SmallVector<IdentifierLocPair, 4> identifierLocPairs;
1656 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1657 identifierLocPairs.push_back(IdentifierLocPair(identifiers[i],
1658 identifierLocs[i]));
1659 }
1660
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001661 QualType BaseT = Actions.GetTypeFromParser(baseType);
1662 if (!BaseT.isNull() && BaseT->acceptsObjCTypeParams()) {
1663 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
1664 } else {
1665 Actions.CodeCompleteObjCProtocolReferences(identifierLocPairs.data(),
1666 identifierLocPairs.size());
1667 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001668 cutOffParsing();
1669 return;
1670 }
1671
1672 allSingleIdentifiers = false;
1673 break;
1674 } while (TryConsumeToken(tok::comma));
1675
1676 // If we parsed an identifier list, semantic analysis sorts out
1677 // whether it refers to protocols or to type arguments.
1678 if (allSingleIdentifiers) {
1679 // Parse the closing '>'.
1680 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001681 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001682 /*ObjCGenericList=*/true);
1683
1684 // Let Sema figure out what we parsed.
1685 Actions.actOnObjCTypeArgsOrProtocolQualifiers(getCurScope(),
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001686 baseType,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001687 lAngleLoc,
1688 identifiers,
1689 identifierLocs,
1690 rAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001691 typeArgsLAngleLoc,
1692 typeArgs,
1693 typeArgsRAngleLoc,
1694 protocolLAngleLoc,
1695 protocols,
1696 protocolRAngleLoc,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001697 warnOnIncompleteProtocols);
1698 return;
1699 }
1700
1701 // We syntactically matched a type argument, so commit to parsing
1702 // type arguments.
Douglas Gregore9d95f12015-07-07 03:57:35 +00001703
1704 // Convert the identifiers into type arguments.
1705 bool invalid = false;
1706 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1707 ParsedType typeArg
1708 = Actions.getTypeName(*identifiers[i], identifierLocs[i], getCurScope());
1709 if (typeArg) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001710 DeclSpec DS(AttrFactory);
1711 const char *prevSpec = nullptr;
1712 unsigned diagID;
1713 DS.SetTypeSpecType(TST_typename, identifierLocs[i], prevSpec, diagID,
1714 typeArg, Actions.getASTContext().getPrintingPolicy());
1715
1716 // Form a declarator to turn this into a type.
1717 Declarator D(DS, Declarator::TypeNameContext);
1718 TypeResult fullTypeArg = Actions.ActOnTypeName(getCurScope(), D);
1719 if (fullTypeArg.isUsable())
1720 typeArgs.push_back(fullTypeArg.get());
1721 else
1722 invalid = true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001723 } else {
1724 invalid = true;
1725 }
1726 }
1727
1728 // Continue parsing type-names.
1729 do {
1730 TypeResult typeArg = ParseTypeName();
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001731
1732 // Consume the '...' for a pack expansion.
1733 SourceLocation ellipsisLoc;
1734 TryConsumeToken(tok::ellipsis, ellipsisLoc);
1735 if (typeArg.isUsable() && ellipsisLoc.isValid()) {
1736 typeArg = Actions.ActOnPackExpansion(typeArg.get(), ellipsisLoc);
1737 }
1738
Douglas Gregore9d95f12015-07-07 03:57:35 +00001739 if (typeArg.isUsable()) {
1740 typeArgs.push_back(typeArg.get());
1741 } else {
1742 invalid = true;
1743 }
1744 } while (TryConsumeToken(tok::comma));
1745
1746 // Parse the closing '>'.
1747 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001748 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001749 /*ObjCGenericList=*/true);
1750
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001751 if (invalid) {
1752 typeArgs.clear();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001753 return;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001754 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001755
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001756 // Record left/right angle locations.
1757 typeArgsLAngleLoc = lAngleLoc;
1758 typeArgsRAngleLoc = rAngleLoc;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001759}
1760
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001761void Parser::parseObjCTypeArgsAndProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001762 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001763 SourceLocation &typeArgsLAngleLoc,
1764 SmallVectorImpl<ParsedType> &typeArgs,
1765 SourceLocation &typeArgsRAngleLoc,
1766 SourceLocation &protocolLAngleLoc,
1767 SmallVectorImpl<Decl *> &protocols,
1768 SmallVectorImpl<SourceLocation> &protocolLocs,
1769 SourceLocation &protocolRAngleLoc,
1770 bool consumeLastToken) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001771 assert(Tok.is(tok::less));
1772
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001773 // Parse the first angle-bracket-delimited clause.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001774 parseObjCTypeArgsOrProtocolQualifiers(baseType,
1775 typeArgsLAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001776 typeArgs,
1777 typeArgsRAngleLoc,
1778 protocolLAngleLoc,
1779 protocols,
1780 protocolLocs,
1781 protocolRAngleLoc,
1782 consumeLastToken,
Douglas Gregore83b9562015-07-07 03:57:53 +00001783 /*warnOnIncompleteProtocols=*/false);
1784
1785 // An Objective-C object pointer followed by type arguments
1786 // can then be followed again by a set of protocol references, e.g.,
1787 // \c NSArray<NSView><NSTextDelegate>
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001788 if ((consumeLastToken && Tok.is(tok::less)) ||
1789 (!consumeLastToken && NextToken().is(tok::less))) {
1790 // If we aren't consuming the last token, the prior '>' is still hanging
1791 // there. Consume it before we parse the protocol qualifiers.
1792 if (!consumeLastToken)
1793 ConsumeToken();
1794
1795 if (!protocols.empty()) {
1796 SkipUntilFlags skipFlags = SkipUntilFlags();
1797 if (!consumeLastToken)
1798 skipFlags = skipFlags | StopBeforeMatch;
Douglas Gregore83b9562015-07-07 03:57:53 +00001799 Diag(Tok, diag::err_objc_type_args_after_protocols)
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001800 << SourceRange(protocolLAngleLoc, protocolRAngleLoc);
1801 SkipUntil(tok::greater, tok::greatergreater, skipFlags);
Douglas Gregore83b9562015-07-07 03:57:53 +00001802 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001803 ParseObjCProtocolReferences(protocols, protocolLocs,
1804 /*WarnOnDeclarations=*/false,
1805 /*ForObjCContainer=*/false,
1806 protocolLAngleLoc, protocolRAngleLoc,
1807 consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001808 }
1809 }
1810}
1811
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001812TypeResult Parser::parseObjCTypeArgsAndProtocolQualifiers(
1813 SourceLocation loc,
1814 ParsedType type,
1815 bool consumeLastToken,
1816 SourceLocation &endLoc) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001817 assert(Tok.is(tok::less));
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001818 SourceLocation typeArgsLAngleLoc;
1819 SmallVector<ParsedType, 4> typeArgs;
1820 SourceLocation typeArgsRAngleLoc;
1821 SourceLocation protocolLAngleLoc;
1822 SmallVector<Decl *, 4> protocols;
1823 SmallVector<SourceLocation, 4> protocolLocs;
1824 SourceLocation protocolRAngleLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00001825
1826 // Parse type arguments and protocol qualifiers.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001827 parseObjCTypeArgsAndProtocolQualifiers(type, typeArgsLAngleLoc, typeArgs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001828 typeArgsRAngleLoc, protocolLAngleLoc,
1829 protocols, protocolLocs,
1830 protocolRAngleLoc, consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001831
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001832 // Compute the location of the last token.
1833 if (consumeLastToken)
1834 endLoc = PrevTokLocation;
1835 else
1836 endLoc = Tok.getLocation();
1837
1838 return Actions.actOnObjCTypeArgsAndProtocolQualifiers(
1839 getCurScope(),
1840 loc,
1841 type,
1842 typeArgsLAngleLoc,
1843 typeArgs,
1844 typeArgsRAngleLoc,
1845 protocolLAngleLoc,
1846 protocols,
1847 protocolLocs,
1848 protocolRAngleLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00001849}
1850
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001851void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1852 BalancedDelimiterTracker &T,
1853 SmallVectorImpl<Decl *> &AllIvarDecls,
1854 bool RBraceMissing) {
1855 if (!RBraceMissing)
1856 T.consumeClose();
1857
1858 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1859 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1860 Actions.ActOnObjCContainerFinishDefinition();
1861 // Call ActOnFields() even if we don't have any decls. This is useful
1862 // for code rewriting tools that need to be aware of the empty list.
1863 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1864 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001865 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001866}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001867
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001868/// objc-class-instance-variables:
1869/// '{' objc-instance-variable-decl-list[opt] '}'
1870///
1871/// objc-instance-variable-decl-list:
1872/// objc-visibility-spec
1873/// objc-instance-variable-decl ';'
1874/// ';'
1875/// objc-instance-variable-decl-list objc-visibility-spec
1876/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1877/// objc-instance-variable-decl-list ';'
1878///
1879/// objc-visibility-spec:
1880/// @private
1881/// @protected
1882/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001883/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001884///
1885/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001886/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001887///
John McCall48871652010-08-21 09:40:31 +00001888void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001889 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001890 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001891 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001892 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001893
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001894 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001895 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001896
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001897 BalancedDelimiterTracker T(*this, tok::l_brace);
1898 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001899 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001900 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001901 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001902
Steve Naroff00433d32007-08-21 21:17:12 +00001903 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001904 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001905 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001906 continue;
1907 }
Mike Stump11289f42009-09-09 15:08:12 +00001908
Steve Naroff00433d32007-08-21 21:17:12 +00001909 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001910 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001911 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001912 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001913 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001914 }
1915
Steve Naroff7c348172007-08-23 18:16:40 +00001916 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001917 case tok::objc_private:
1918 case tok::objc_public:
1919 case tok::objc_protected:
1920 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001921 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001922 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001923 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001924
1925 case tok::objc_end:
1926 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001927 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1928 Tok.setKind(tok::at);
1929 Tok.setLength(1);
1930 PP.EnterToken(Tok);
1931 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1932 T, AllIvarDecls, true);
1933 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001934
1935 default:
1936 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1937 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001938 }
1939 }
Mike Stump11289f42009-09-09 15:08:12 +00001940
Douglas Gregor48d46252010-01-13 21:54:15 +00001941 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001942 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001943 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001944 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001945 }
John McCallcfefb6d2009-11-03 02:38:08 +00001946
Benjamin Kramera39beb92014-09-03 11:06:10 +00001947 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1948 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1949 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001950 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001951 Decl *Field = Actions.ActOnIvar(
1952 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1953 FD.BitfieldSize, visibility);
1954 Actions.ActOnObjCContainerFinishDefinition();
1955 if (Field)
1956 AllIvarDecls.push_back(Field);
1957 FD.complete(Field);
1958 };
John McCallcfefb6d2009-11-03 02:38:08 +00001959
Chris Lattnera12405b2008-04-10 06:46:29 +00001960 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001961 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001962 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001963
Chris Lattner0ef13522007-10-09 17:51:17 +00001964 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001965 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001966 } else {
1967 Diag(Tok, diag::err_expected_semi_decl_list);
1968 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001969 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001970 }
1971 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001972 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1973 T, AllIvarDecls, false);
Steve Naroff00433d32007-08-21 21:17:12 +00001974 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001975}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001976
1977/// objc-protocol-declaration:
1978/// objc-protocol-definition
1979/// objc-protocol-forward-reference
1980///
1981/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00001982/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00001983/// objc-protocol-refs[opt]
1984/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00001985/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001986///
1987/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00001988/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001989///
James Dennett1355bd12012-06-11 06:19:40 +00001990/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001991/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001992/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001993Parser::DeclGroupPtrTy
1994Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1995 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001996 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001997 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1998 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001999
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002000 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002001 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002002 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00002003 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002004 }
2005
Nico Weber69a79142013-04-04 00:15:10 +00002006 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00002007
Chris Lattner0ef13522007-10-09 17:51:17 +00002008 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002009 Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00002010 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002011 }
2012 // Save the protocol name, then consume it.
2013 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
2014 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002015
Alp Toker383d2c42014-01-01 03:08:43 +00002016 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00002017 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002018 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00002019 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002020 }
Mike Stump11289f42009-09-09 15:08:12 +00002021
Erik Verbruggenf9887852011-12-08 09:58:43 +00002022 CheckNestedObjCContexts(AtLoc);
2023
Chris Lattner0ef13522007-10-09 17:51:17 +00002024 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002025 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002026 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
2027
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002028 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002029 while (1) {
2030 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00002031 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002032 Diag(Tok, diag::err_expected) << tok::identifier;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002033 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00002034 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002035 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00002036 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
2037 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002038 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00002039
Chris Lattner0ef13522007-10-09 17:51:17 +00002040 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002041 break;
2042 }
2043 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00002044 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00002045 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002046
Steve Naroff93eb5f12007-10-10 17:32:04 +00002047 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002048 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002049 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00002050 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00002051 }
Mike Stump11289f42009-09-09 15:08:12 +00002052
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002053 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00002054 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002055
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002056 SmallVector<Decl *, 8> ProtocolRefs;
2057 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002058 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00002059 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002060 LAngleLoc, EndProtoLoc,
2061 /*consumeLastToken=*/true))
Douglas Gregorf6102672012-01-01 21:23:57 +00002062 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002063
John McCall48871652010-08-21 09:40:31 +00002064 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00002065 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002066 ProtocolRefs.data(),
2067 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00002068 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00002069 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002070
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00002071 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00002072 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002073}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002074
2075/// objc-implementation:
2076/// objc-class-implementation-prologue
2077/// objc-category-implementation-prologue
2078///
2079/// objc-class-implementation-prologue:
2080/// @implementation identifier objc-superclass[opt]
2081/// objc-class-instance-variables[opt]
2082///
2083/// objc-category-implementation-prologue:
2084/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002085Parser::DeclGroupPtrTy
2086Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002087 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
2088 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002089 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002090 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002091
Douglas Gregor49c22a72009-11-18 16:26:39 +00002092 // Code completion after '@implementation'.
2093 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002094 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002095 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002096 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00002097 }
2098
Nico Weber69a79142013-04-04 00:15:10 +00002099 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00002100
Chris Lattner0ef13522007-10-09 17:51:17 +00002101 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002102 Diag(Tok, diag::err_expected)
2103 << tok::identifier; // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002104 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002105 }
2106 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00002107 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002108 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00002109 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002110
Douglas Gregor85f3f952015-07-07 03:57:15 +00002111 // Neither a type parameter list nor a list of protocol references is
2112 // permitted here. Parse and diagnose them.
2113 if (Tok.is(tok::less)) {
2114 SourceLocation lAngleLoc, rAngleLoc;
2115 SmallVector<IdentifierLocPair, 8> protocolIdents;
2116 SourceLocation diagLoc = Tok.getLocation();
2117 if (parseObjCTypeParamListOrProtocolRefs(lAngleLoc, protocolIdents,
2118 rAngleLoc)) {
2119 Diag(diagLoc, diag::err_objc_parameterized_implementation)
2120 << SourceRange(diagLoc, PrevTokLocation);
2121 } else if (lAngleLoc.isValid()) {
2122 Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
2123 << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
2124 }
2125 }
2126
Mike Stump11289f42009-09-09 15:08:12 +00002127 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002128 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002129 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002130 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002131 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002132
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002133 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002134 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002135 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002136 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002137 }
2138
Chris Lattner0ef13522007-10-09 17:51:17 +00002139 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002140 categoryId = Tok.getIdentifierInfo();
2141 categoryLoc = ConsumeToken();
2142 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002143 Diag(Tok, diag::err_expected)
2144 << tok::identifier; // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002145 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002146 }
Chris Lattner0ef13522007-10-09 17:51:17 +00002147 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002148 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002149 SkipUntil(tok::r_paren); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002150 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002151 }
2152 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00002153 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2154 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002155 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2156 SmallVector<Decl *, 4> protocols;
2157 SmallVector<SourceLocation, 4> protocolLocs;
2158 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2159 /*warnOnIncompleteProtocols=*/false,
2160 /*ForObjCContainer=*/false,
2161 protocolLAngleLoc, protocolRAngleLoc,
2162 /*consumeLastToken=*/true);
Fariborz Jahanian85888552013-05-17 17:58:11 +00002163 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002164 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002165 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00002166 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002167
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002168 } else {
2169 // We have a class implementation
2170 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002171 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00002172 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002173 // We have a super class
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002174 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002175 Diag(Tok, diag::err_expected)
2176 << tok::identifier; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002177 return DeclGroupPtrTy();
2178 }
2179 superClassId = Tok.getIdentifierInfo();
2180 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002181 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002182 ObjCImpDecl = Actions.ActOnStartClassImplementation(
2183 AtLoc, nameId, nameLoc,
2184 superClassId, superClassLoc);
2185
2186 if (Tok.is(tok::l_brace)) // we have ivars
2187 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002188 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2189 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002190
2191 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2192 SmallVector<Decl *, 4> protocols;
2193 SmallVector<SourceLocation, 4> protocolLocs;
2194 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2195 /*warnOnIncompleteProtocols=*/false,
2196 /*ForObjCContainer=*/false,
2197 protocolLAngleLoc, protocolRAngleLoc,
2198 /*consumeLastToken=*/true);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002199 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002200 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002201 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002202
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002203 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002204
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002205 {
2206 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00002207 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002208 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002209 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002210 MaybeParseMicrosoftAttributes(attrs);
2211 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
2212 DeclGroupRef DG = DGP.get();
2213 DeclsInGroup.append(DG.begin(), DG.end());
2214 }
2215 }
2216 }
2217
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00002218 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002219}
Steve Naroff33a1e802007-10-29 21:38:07 +00002220
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002221Parser::DeclGroupPtrTy
2222Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002223 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
2224 "ParseObjCAtEndDeclaration(): Expected @end");
2225 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002226 if (CurParsedObjCImpl)
2227 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002228 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00002229 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002230 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002231 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002232}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002233
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002234Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
2235 if (!Finished) {
2236 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00002237 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002238 P.Diag(P.Tok, diag::err_objc_missing_end)
2239 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
2240 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
2241 << Sema::OCK_Implementation;
2242 }
2243 }
Craig Topper161e4db2014-05-21 06:02:52 +00002244 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002245 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002246}
2247
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002248void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
2249 assert(!Finished);
2250 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
2251 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002252 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2253 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002254
2255 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
2256
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002257 if (HasCFunction)
2258 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
2259 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2260 false/*c-functions*/);
2261
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002262 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002263 for (LateParsedObjCMethodContainer::iterator
2264 I = LateParsedObjCMethods.begin(),
2265 E = LateParsedObjCMethods.end(); I != E; ++I)
2266 delete *I;
2267 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002268
2269 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002270}
2271
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002272/// compatibility-alias-decl:
2273/// @compatibility_alias alias-name class-name ';'
2274///
John McCall48871652010-08-21 09:40:31 +00002275Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002276 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
2277 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
2278 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00002279 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002280 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002281 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002282 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002283 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
2284 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00002285 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002286 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002287 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002288 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002289 IdentifierInfo *classId = Tok.getIdentifierInfo();
2290 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00002291 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00002292 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
2293 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002294}
2295
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002296/// property-synthesis:
2297/// @synthesize property-ivar-list ';'
2298///
2299/// property-ivar-list:
2300/// property-ivar
2301/// property-ivar-list ',' property-ivar
2302///
2303/// property-ivar:
2304/// identifier
2305/// identifier '=' identifier
2306///
John McCall48871652010-08-21 09:40:31 +00002307Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002308 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00002309 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002310 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00002311
Douglas Gregor88e72a02009-11-18 19:45:45 +00002312 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00002313 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002314 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002315 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002316 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002317 }
2318
Douglas Gregor88e72a02009-11-18 19:45:45 +00002319 if (Tok.isNot(tok::identifier)) {
2320 Diag(Tok, diag::err_synthesized_property_name);
2321 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002322 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00002323 }
Craig Topper161e4db2014-05-21 06:02:52 +00002324
2325 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002326 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2327 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002328 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00002329 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002330 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00002331 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002332 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002333 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002334 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002335 }
2336
Chris Lattner0ef13522007-10-09 17:51:17 +00002337 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002338 Diag(Tok, diag::err_expected) << tok::identifier;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002339 break;
2340 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002341 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002342 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002343 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002344 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002345 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002346 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002347 break;
2348 ConsumeToken(); // consume ','
2349 }
Alp Toker383d2c42014-01-01 03:08:43 +00002350 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00002351 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002352}
2353
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002354/// property-dynamic:
2355/// @dynamic property-list
2356///
2357/// property-list:
2358/// identifier
2359/// property-list ',' identifier
2360///
John McCall48871652010-08-21 09:40:31 +00002361Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002362 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2363 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002364 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002365 while (true) {
2366 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002367 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002368 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002369 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002370 }
2371
2372 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002373 Diag(Tok, diag::err_expected) << tok::identifier;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002374 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002375 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002376 }
2377
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002378 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2379 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002380 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Craig Topper161e4db2014-05-21 06:02:52 +00002381 propertyId, nullptr, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002382
Chris Lattner0ef13522007-10-09 17:51:17 +00002383 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002384 break;
2385 ConsumeToken(); // consume ','
2386 }
Alp Toker383d2c42014-01-01 03:08:43 +00002387 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00002388 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002389}
Mike Stump11289f42009-09-09 15:08:12 +00002390
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002391/// objc-throw-statement:
2392/// throw expression[opt];
2393///
John McCalldadc5752010-08-24 06:29:42 +00002394StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2395 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002396 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00002397 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00002398 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002399 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002400 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002401 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002402 }
2403 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00002404 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00002405 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002406 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002407}
2408
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002409/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002410/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002411///
John McCalldadc5752010-08-24 06:29:42 +00002412StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002413Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002414 ConsumeToken(); // consume synchronized
2415 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002416 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002417 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002418 }
John McCalld9bb7432011-07-27 21:50:02 +00002419
2420 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002421 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00002422 ExprResult operand(ParseExpression());
2423
2424 if (Tok.is(tok::r_paren)) {
2425 ConsumeParen(); // ')'
2426 } else {
2427 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002428 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00002429
2430 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002431 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002432 }
John McCalld9bb7432011-07-27 21:50:02 +00002433
2434 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002435 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00002436 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002437 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002438 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002439 }
Steve Naroffd9c26072008-06-04 20:36:13 +00002440
John McCalld9bb7432011-07-27 21:50:02 +00002441 // Check the @synchronized operand now.
2442 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002443 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002444
John McCalld9bb7432011-07-27 21:50:02 +00002445 // Parse the compound statement within a new scope.
2446 ParseScope bodyScope(this, Scope::DeclScope);
2447 StmtResult body(ParseCompoundStatementBody());
2448 bodyScope.Exit();
2449
2450 // If there was a semantic or parse error earlier with the
2451 // operand, fail now.
2452 if (operand.isInvalid())
2453 return StmtError();
2454
2455 if (body.isInvalid())
2456 body = Actions.ActOnNullStmt(Tok.getLocation());
2457
2458 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002459}
2460
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002461/// objc-try-catch-statement:
2462/// @try compound-statement objc-catch-list[opt]
2463/// @try compound-statement objc-catch-list[opt] @finally compound-statement
2464///
2465/// objc-catch-list:
2466/// @catch ( parameter-declaration ) compound-statement
2467/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2468/// catch-parameter-declaration:
2469/// parameter-declaration
2470/// '...' [OBJC2]
2471///
John McCalldadc5752010-08-24 06:29:42 +00002472StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002473 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002474
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002475 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00002476 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002477 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002478 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002479 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00002480 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00002481 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002482 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00002483 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002484 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002485 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002486 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002487
Chris Lattner0ef13522007-10-09 17:51:17 +00002488 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002489 // At this point, we need to lookahead to determine if this @ is the start
2490 // of an @catch or @finally. We don't want to consume the @ token if this
2491 // is an @try or @encode or something else.
2492 Token AfterAt = GetLookAheadToken(1);
2493 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2494 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2495 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002496
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002497 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002498 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002499 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002500 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002501 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002502 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00002503 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002504 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002505 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002506 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00002507 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002508 ParseDeclarator(ParmDecl);
2509
Douglas Gregore11ee112010-04-23 23:01:43 +00002510 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002511 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002512 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002513 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002514 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002515
Steve Naroff65a00892009-04-07 22:56:58 +00002516 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002517
Steve Naroff65a00892009-04-07 22:56:58 +00002518 if (Tok.is(tok::r_paren))
2519 RParenLoc = ConsumeParen();
2520 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002521 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002522
John McCalldadc5752010-08-24 06:29:42 +00002523 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002524 if (Tok.is(tok::l_brace))
2525 CatchBody = ParseCompoundStatementBody();
2526 else
Alp Tokerec543272013-12-24 09:48:30 +00002527 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002528 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002529 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00002530
John McCalldadc5752010-08-24 06:29:42 +00002531 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00002532 RParenLoc,
2533 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002534 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002535 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002536 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002537
Steve Naroffe6016792008-02-05 21:27:35 +00002538 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002539 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2540 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002541 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002542 }
2543 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002544 } else {
2545 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002546 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002547 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002548
John McCalldadc5752010-08-24 06:29:42 +00002549 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002550 if (Tok.is(tok::l_brace))
2551 FinallyBody = ParseCompoundStatementBody();
2552 else
Alp Tokerec543272013-12-24 09:48:30 +00002553 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002554 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002555 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002556 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002557 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002558 catch_or_finally_seen = true;
2559 break;
2560 }
2561 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002562 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002563 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002564 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002565 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002566
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002567 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002568 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002569 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002570}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002571
John McCall31168b02011-06-15 23:02:42 +00002572/// objc-autoreleasepool-statement:
2573/// @autoreleasepool compound-statement
2574///
2575StmtResult
2576Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2577 ConsumeToken(); // consume autoreleasepool
2578 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002579 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002580 return StmtError();
2581 }
2582 // Enter a scope to hold everything within the compound stmt. Compound
2583 // statements can always hold declarations.
2584 ParseScope BodyScope(this, Scope::DeclScope);
2585
2586 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2587
2588 BodyScope.Exit();
2589 if (AutoreleasePoolBody.isInvalid())
2590 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2591 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002592 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002593}
2594
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002595/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2596/// for later parsing.
2597void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
2598 LexedMethod* LM = new LexedMethod(this, MDecl);
2599 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2600 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002601 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002602 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002603 if (Tok.is(tok::kw_try)) {
2604 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002605 if (Tok.is(tok::colon)) {
2606 Toks.push_back(Tok);
2607 ConsumeToken();
2608 while (Tok.isNot(tok::l_brace)) {
2609 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2610 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2611 }
2612 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002613 Toks.push_back(Tok); // also store '{'
2614 }
2615 else if (Tok.is(tok::colon)) {
2616 ConsumeToken();
2617 while (Tok.isNot(tok::l_brace)) {
2618 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2619 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2620 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002621 Toks.push_back(Tok); // also store '{'
2622 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002623 ConsumeBrace();
2624 // Consume everything up to (and including) the matching right brace.
2625 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002626 while (Tok.is(tok::kw_catch)) {
2627 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2628 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2629 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002630}
2631
Steve Naroff09bf8152007-09-06 21:24:23 +00002632/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002633///
John McCall48871652010-08-21 09:40:31 +00002634Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002635 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002636
John McCallfaf5fb42010-08-26 23:41:50 +00002637 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2638 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002639
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002640 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002641 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002642 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002643 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002644 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002645 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002646 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002647 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002648
Steve Naroffbb875722007-11-11 19:54:21 +00002649 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002650 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002651 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002652
Steve Naroffbb875722007-11-11 19:54:21 +00002653 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002654 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002655
Steve Naroffbb875722007-11-11 19:54:21 +00002656 // If we didn't find the '{', bail out.
2657 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002658 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002659 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002660
2661 if (!MDecl) {
2662 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002663 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002664 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002665 }
2666
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002667 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002668 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002669 assert (CurParsedObjCImpl
2670 && "ParseObjCMethodDefinition - Method out of @implementation");
2671 // Consume the tokens and store them for later parsing.
2672 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002673 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002674}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002675
John McCalldadc5752010-08-24 06:29:42 +00002676StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002677 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002678 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002679 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002680 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002681 }
2682
2683 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002684 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002685
2686 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002687 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002688
2689 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002690 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002691
2692 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2693 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002694
2695 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2696 getLangOpts().DebuggerSupport) {
2697 SkipUntil(tok::semi);
2698 return Actions.ActOnNullStmt(Tok.getLocation());
2699 }
2700
John McCalldadc5752010-08-24 06:29:42 +00002701 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002702 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002703 // If the expression is invalid, skip ahead to the next semicolon. Not
2704 // doing this opens us up to the possibility of infinite loops if
2705 // ParseExpression does not consume any tokens.
2706 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002707 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002708 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002709
Steve Naroffe6016792008-02-05 21:27:35 +00002710 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002711 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002712 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002713}
2714
John McCalldadc5752010-08-24 06:29:42 +00002715ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002716 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002717 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002718 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002719 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002720 return ExprError();
2721
Ted Kremeneke65b0862012-03-06 20:05:56 +00002722 case tok::minus:
2723 case tok::plus: {
2724 tok::TokenKind Kind = Tok.getKind();
2725 SourceLocation OpLoc = ConsumeToken();
2726
2727 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002728 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002729 switch (Kind) {
2730 case tok::minus: Symbol = "-"; break;
2731 case tok::plus: Symbol = "+"; break;
2732 default: llvm_unreachable("missing unary operator case");
2733 }
2734 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2735 << Symbol;
2736 return ExprError();
2737 }
2738
2739 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2740 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002741 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002742 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002743 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002744
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002745 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002746 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002747 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002748
2749 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002750 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002751 }
2752
Chris Lattnere002fbe2007-12-12 01:04:12 +00002753 case tok::string_literal: // primary-expression: string-literal
2754 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002755 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002756
2757 case tok::char_constant:
2758 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2759
2760 case tok::numeric_constant:
2761 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2762
2763 case tok::kw_true: // Objective-C++, etc.
2764 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2765 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2766 case tok::kw_false: // Objective-C++, etc.
2767 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2768 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2769
2770 case tok::l_square:
2771 // Objective-C array literal
2772 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2773
2774 case tok::l_brace:
2775 // Objective-C dictionary literal
2776 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2777
Patrick Beard0caa3942012-04-19 00:25:12 +00002778 case tok::l_paren:
2779 // Objective-C boxed expression
2780 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2781
Chris Lattnere002fbe2007-12-12 01:04:12 +00002782 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002783 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002784 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002785
Chris Lattner197a3012008-08-05 06:19:09 +00002786 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2787 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002788 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002789 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002790 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002791 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002792 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002793 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002794 const char *str = nullptr;
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002795 if (GetLookAheadToken(1).is(tok::l_brace)) {
2796 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2797 str =
2798 ch == 't' ? "try"
2799 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002800 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002801 }
2802 if (str) {
2803 SourceLocation kwLoc = Tok.getLocation();
2804 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2805 FixItHint::CreateReplacement(kwLoc, str));
2806 }
2807 else
2808 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2809 }
Chris Lattner197a3012008-08-05 06:19:09 +00002810 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002811 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002812}
2813
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002814/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002815///
2816/// This routine parses the receiver of a message send in
2817/// Objective-C++ either as a type or as an expression. Note that this
2818/// routine must not be called to parse a send to 'super', since it
2819/// has no way to return such a result.
2820///
2821/// \param IsExpr Whether the receiver was parsed as an expression.
2822///
2823/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2824/// IsExpr is true), the parsed expression. If the receiver was parsed
2825/// as a type (\c IsExpr is false), the parsed type.
2826///
2827/// \returns True if an error occurred during parsing or semantic
2828/// analysis, in which case the arguments do not have valid
2829/// values. Otherwise, returns false for a successful parse.
2830///
2831/// objc-receiver: [C++]
2832/// 'super' [not parsed here]
2833/// expression
2834/// simple-type-specifier
2835/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002836bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002837 InMessageExpressionRAIIObject InMessage(*this, true);
2838
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002839 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2840 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002841 TryAnnotateTypeOrScopeToken();
2842
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002843 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002844 // objc-receiver:
2845 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002846 // Make sure any typos in the receiver are corrected or diagnosed, so that
2847 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2848 // only the things that are valid ObjC receivers?
2849 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002850 if (Receiver.isInvalid())
2851 return true;
2852
2853 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002854 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002855 return false;
2856 }
2857
2858 // objc-receiver:
2859 // typename-specifier
2860 // simple-type-specifier
2861 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002862 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002863 ParseCXXSimpleTypeSpecifier(DS);
2864
2865 if (Tok.is(tok::l_paren)) {
2866 // If we see an opening parentheses at this point, we are
2867 // actually parsing an expression that starts with a
2868 // function-style cast, e.g.,
2869 //
2870 // postfix-expression:
2871 // simple-type-specifier ( expression-list [opt] )
2872 // typename-specifier ( expression-list [opt] )
2873 //
2874 // Parse the remainder of this case, then the (optional)
2875 // postfix-expression suffix, followed by the (optional)
2876 // right-hand side of the binary expression. We have an
2877 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002878 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002879 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002880 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002881 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002882 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002883 if (Receiver.isInvalid())
2884 return true;
2885
2886 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002887 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002888 return false;
2889 }
2890
2891 // We have a class message. Turn the simple-type-specifier or
2892 // typename-specifier we parsed into a type and parse the
2893 // remainder of the class message.
2894 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002895 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002896 if (Type.isInvalid())
2897 return true;
2898
2899 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002900 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002901 return false;
2902}
2903
Douglas Gregor990ccac2010-05-31 14:40:22 +00002904/// \brief Determine whether the parser is currently referring to a an
2905/// Objective-C message send, using a simplified heuristic to avoid overhead.
2906///
2907/// This routine will only return true for a subset of valid message-send
2908/// expressions.
2909bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002910 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002911 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002912 return GetLookAheadToken(1).is(tok::identifier) &&
2913 GetLookAheadToken(2).is(tok::identifier);
2914}
2915
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002916bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002917 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002918 InMessageExpression)
2919 return false;
2920
2921
2922 ParsedType Type;
2923
2924 if (Tok.is(tok::annot_typename))
2925 Type = getTypeAnnotation(Tok);
2926 else if (Tok.is(tok::identifier))
2927 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2928 getCurScope());
2929 else
2930 return false;
2931
2932 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2933 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002934 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002935 if (Tok.is(tok::identifier))
2936 TryAnnotateTypeOrScopeToken();
2937
2938 return Tok.is(tok::annot_typename);
2939 }
2940 }
2941
2942 return false;
2943}
2944
Mike Stump11289f42009-09-09 15:08:12 +00002945/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002946/// '[' objc-receiver objc-message-args ']'
2947///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002948/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002949/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002950/// expression
2951/// class-name
2952/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002953///
John McCalldadc5752010-08-24 06:29:42 +00002954ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002955 assert(Tok.is(tok::l_square) && "'[' expected");
2956 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2957
Douglas Gregora817a192010-05-27 23:06:34 +00002958 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002959 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002960 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002961 return ExprError();
2962 }
2963
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002964 InMessageExpressionRAIIObject InMessage(*this, true);
2965
David Blaikiebbafb8a2012-03-11 07:00:24 +00002966 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002967 // We completely separate the C and C++ cases because C++ requires
2968 // more complicated (read: slower) parsing.
2969
2970 // Handle send to super.
2971 // FIXME: This doesn't benefit from the same typo-correction we
2972 // get in Objective-C.
2973 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002974 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002975 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002976 ParsedType(), nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002977
2978 // Parse the receiver, which is either a type or an expression.
2979 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00002980 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002981 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002982 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002983 return ExprError();
2984 }
2985
2986 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002987 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2988 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002989 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002990
2991 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002992 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00002993 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00002994 }
2995
2996 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002997 IdentifierInfo *Name = Tok.getIdentifierInfo();
2998 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002999 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003000 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00003001 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00003002 NextToken().is(tok::period),
3003 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00003004 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00003005 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00003006 ParsedType(), nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003007
John McCallfaf5fb42010-08-26 23:41:50 +00003008 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00003009 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003010 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003011 return ExprError();
3012 }
3013
Douglas Gregore5798dc2010-04-21 20:38:13 +00003014 ConsumeToken(); // the type name
3015
Douglas Gregore83b9562015-07-07 03:57:53 +00003016 // Parse type arguments and protocol qualifiers.
3017 if (Tok.is(tok::less)) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003018 SourceLocation NewEndLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00003019 TypeResult NewReceiverType
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003020 = parseObjCTypeArgsAndProtocolQualifiers(NameLoc, ReceiverType,
3021 /*consumeLastToken=*/true,
3022 NewEndLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00003023 if (!NewReceiverType.isUsable()) {
3024 SkipUntil(tok::r_square, StopAtSemi);
3025 return ExprError();
3026 }
3027
3028 ReceiverType = NewReceiverType.get();
3029 }
3030
Douglas Gregore5798dc2010-04-21 20:38:13 +00003031 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00003032 ReceiverType, nullptr);
3033
John McCallfaf5fb42010-08-26 23:41:50 +00003034 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003035 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00003036 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00003037 }
Chris Lattner8f697062008-01-25 18:59:06 +00003038 }
Chris Lattnera36ec422010-04-11 08:28:14 +00003039
3040 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00003041 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003042 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003043 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003044 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00003045 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003046
John McCallba7bf592010-08-24 05:47:05 +00003047 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003048 ParsedType(), Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00003049}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003050
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003051/// \brief Parse the remainder of an Objective-C message following the
3052/// '[' objc-receiver.
3053///
3054/// This routine handles sends to super, class messages (sent to a
3055/// class name), and instance messages (sent to an object), and the
3056/// target is represented by \p SuperLoc, \p ReceiverType, or \p
3057/// ReceiverExpr, respectively. Only one of these parameters may have
3058/// a valid value.
3059///
3060/// \param LBracLoc The location of the opening '['.
3061///
3062/// \param SuperLoc If this is a send to 'super', the location of the
3063/// 'super' keyword that indicates a send to the superclass.
3064///
3065/// \param ReceiverType If this is a class message, the type of the
3066/// class we are sending a message to.
3067///
3068/// \param ReceiverExpr If this is an instance message, the expression
3069/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00003070///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003071/// objc-message-args:
3072/// objc-selector
3073/// objc-keywordarg-list
3074///
3075/// objc-keywordarg-list:
3076/// objc-keywordarg
3077/// objc-keywordarg-list objc-keywordarg
3078///
Mike Stump11289f42009-09-09 15:08:12 +00003079/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003080/// selector-name[opt] ':' objc-keywordexpr
3081///
3082/// objc-keywordexpr:
3083/// nonempty-expr-list
3084///
3085/// nonempty-expr-list:
3086/// assignment-expression
3087/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003088///
John McCalldadc5752010-08-24 06:29:42 +00003089ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00003090Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003091 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00003092 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00003093 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003094 InMessageExpressionRAIIObject InMessage(*this, true);
3095
Steve Naroffeae65032009-11-07 02:08:14 +00003096 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003097 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003098 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003099 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003100 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003101 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003102 false);
Steve Naroffeae65032009-11-07 02:08:14 +00003103 else
John McCallb268a282010-08-23 23:25:46 +00003104 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003105 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003106 cutOffParsing();
3107 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00003108 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00003109
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003110 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00003111 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003112 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003113
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003114 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003115 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003116 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00003117
Chris Lattner0ef13522007-10-09 17:51:17 +00003118 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003119 while (1) {
3120 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00003121 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003122 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00003123
Alp Toker383d2c42014-01-01 03:08:43 +00003124 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00003125 // We must manually skip to a ']', otherwise the expression skipper will
3126 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3127 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003128 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003129 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003130 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003131
Mike Stump11289f42009-09-09 15:08:12 +00003132 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003133
3134 if (Tok.is(tok::code_completion)) {
3135 if (SuperLoc.isValid())
3136 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003137 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003138 /*AtArgumentEpression=*/true);
3139 else if (ReceiverType)
3140 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003141 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003142 /*AtArgumentEpression=*/true);
3143 else
3144 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003145 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003146 /*AtArgumentEpression=*/true);
3147
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003148 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003149 return ExprError();
3150 }
3151
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00003152 ExprResult Expr;
3153 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3154 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3155 Expr = ParseBraceInitializer();
3156 } else
3157 Expr = ParseAssignmentExpression();
3158
3159 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003160 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00003161 // We must manually skip to a ']', otherwise the expression skipper will
3162 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3163 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003164 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003165 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00003166 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003167
Steve Naroff486760a2007-09-17 20:25:27 +00003168 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003169 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003170
Douglas Gregor1b605f72009-11-19 01:08:35 +00003171 // Code completion after each argument.
3172 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003173 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003174 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003175 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003176 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003177 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003178 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003179 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003180 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00003181 else
John McCallb268a282010-08-23 23:25:46 +00003182 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003183 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003184 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003185 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003186 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00003187 }
3188
Steve Naroff486760a2007-09-17 20:25:27 +00003189 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00003190 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00003191 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003192 break;
3193 // We have a selector or a colon, continue parsing.
3194 }
3195 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00003196 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003197 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00003198 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00003199 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00003200 if (Tok.is(tok::colon))
3201 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003202 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003203 if (Tok.is(tok::colon)) {
3204 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
3205 FixItHint::CreateRemoval(commaLoc);
3206 }
Chris Lattner197a3012008-08-05 06:19:09 +00003207 // We must manually skip to a ']', otherwise the expression skipper will
3208 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3209 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003210 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003211 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003212 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003213
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003214 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003215 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003216 }
3217 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00003218 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003219
Chris Lattner197a3012008-08-05 06:19:09 +00003220 // We must manually skip to a ']', otherwise the expression skipper will
3221 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3222 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003223 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003224 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003225 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00003226
Chris Lattner0ef13522007-10-09 17:51:17 +00003227 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00003228 Diag(Tok, diag::err_expected)
3229 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00003230 // We must manually skip to a ']', otherwise the expression skipper will
3231 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3232 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003233 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003234 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003235 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003236
Chris Lattner8f697062008-01-25 18:59:06 +00003237 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003238
Steve Naroffe61bfa82007-10-05 18:42:47 +00003239 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003240 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00003241 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003242 KeyLocs.push_back(Loc);
3243 }
Chris Lattner5700fab2007-10-07 02:00:24 +00003244 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003245
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003246 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003247 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003248 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003249 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003250 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003251 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00003252 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003253 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003254}
3255
John McCalldadc5752010-08-24 06:29:42 +00003256ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
3257 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003258 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003259
Chris Lattnere002fbe2007-12-12 01:04:12 +00003260 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
3261 // expressions. At this point, we know that the only valid thing that starts
3262 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003263 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003264 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00003265 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003266 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003267
Chris Lattnere002fbe2007-12-12 01:04:12 +00003268 while (Tok.is(tok::at)) {
3269 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00003270
Sebastian Redlc13f2682008-12-09 20:22:58 +00003271 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00003272 if (!isTokenStringLiteral())
3273 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00003274
John McCalldadc5752010-08-24 06:29:42 +00003275 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003276 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003277 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003278
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003279 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00003280 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003281
Nico Webera7c7e602012-12-31 00:28:03 +00003282 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
3283 AtStrings.size());
Anders Carlsson76f4a902007-08-21 17:43:55 +00003284}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003285
Ted Kremeneke65b0862012-03-06 20:05:56 +00003286/// ParseObjCBooleanLiteral -
3287/// objc-scalar-literal : '@' boolean-keyword
3288/// ;
3289/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
3290/// ;
3291ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
3292 bool ArgValue) {
3293 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
3294 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
3295}
3296
3297/// ParseObjCCharacterLiteral -
3298/// objc-scalar-literal : '@' character-literal
3299/// ;
3300ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
3301 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
3302 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003303 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003304 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003305 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003306 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003307}
3308
3309/// ParseObjCNumericLiteral -
3310/// objc-scalar-literal : '@' scalar-literal
3311/// ;
3312/// scalar-literal : | numeric-constant /* any numeric constant. */
3313/// ;
3314ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3315 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3316 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003317 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003318 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003319 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003320 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003321}
3322
Patrick Beard0caa3942012-04-19 00:25:12 +00003323/// ParseObjCBoxedExpr -
3324/// objc-box-expression:
3325/// @( assignment-expression )
3326ExprResult
3327Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3328 if (Tok.isNot(tok::l_paren))
3329 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3330
3331 BalancedDelimiterTracker T(*this, tok::l_paren);
3332 T.consumeOpen();
3333 ExprResult ValueExpr(ParseAssignmentExpression());
3334 if (T.consumeClose())
3335 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00003336
3337 if (ValueExpr.isInvalid())
3338 return ExprError();
3339
Patrick Beard0caa3942012-04-19 00:25:12 +00003340 // Wrap the sub-expression in a parenthesized expression, to distinguish
3341 // a boxed expression from a literal.
3342 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003343 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00003344 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003345 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00003346}
3347
Ted Kremeneke65b0862012-03-06 20:05:56 +00003348ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00003349 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003350 ConsumeBracket(); // consume the l_square.
3351
3352 while (Tok.isNot(tok::r_square)) {
3353 // Parse list of array element expressions (all must be id types).
3354 ExprResult Res(ParseAssignmentExpression());
3355 if (Res.isInvalid()) {
3356 // We must manually skip to a ']', otherwise the expression skipper will
3357 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3358 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003359 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003360 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003361 }
3362
3363 // Parse the ellipsis that indicates a pack expansion.
3364 if (Tok.is(tok::ellipsis))
3365 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
3366 if (Res.isInvalid())
3367 return true;
3368
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003369 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003370
3371 if (Tok.is(tok::comma))
3372 ConsumeToken(); // Eat the ','.
3373 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00003374 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3375 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003376 }
3377 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramerf0623432012-08-23 22:51:59 +00003378 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00003379 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003380}
3381
3382ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3383 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3384 ConsumeBrace(); // consume the l_square.
3385 while (Tok.isNot(tok::r_brace)) {
3386 // Parse the comma separated key : value expressions.
3387 ExprResult KeyExpr;
3388 {
3389 ColonProtectionRAIIObject X(*this);
3390 KeyExpr = ParseAssignmentExpression();
3391 if (KeyExpr.isInvalid()) {
3392 // We must manually skip to a '}', otherwise the expression skipper will
3393 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3394 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003395 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003396 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003397 }
3398 }
3399
Alp Toker383d2c42014-01-01 03:08:43 +00003400 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003401 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00003402 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003403 }
3404
3405 ExprResult ValueExpr(ParseAssignmentExpression());
3406 if (ValueExpr.isInvalid()) {
3407 // We must manually skip to a '}', otherwise the expression skipper will
3408 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3409 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003410 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003411 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003412 }
3413
3414 // Parse the ellipsis that designates this as a pack expansion.
3415 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00003416 if (getLangOpts().CPlusPlus)
3417 TryConsumeToken(tok::ellipsis, EllipsisLoc);
3418
Ted Kremeneke65b0862012-03-06 20:05:56 +00003419 // We have a valid expression. Collect it in a vector so we can
3420 // build the argument list.
3421 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00003422 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00003423 };
3424 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00003425
3426 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00003427 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3428 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003429 }
3430 SourceLocation EndLoc = ConsumeBrace();
3431
3432 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00003433 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
3434 Elements.data(), Elements.size());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003435}
3436
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003437/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00003438/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00003439ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003440Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00003441 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003442
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003443 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003444
Chris Lattner197a3012008-08-05 06:19:09 +00003445 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003446 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3447
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003448 BalancedDelimiterTracker T(*this, tok::l_paren);
3449 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003450
Douglas Gregor220cac52009-02-18 17:45:20 +00003451 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003452
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003453 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003454
Douglas Gregor220cac52009-02-18 17:45:20 +00003455 if (Ty.isInvalid())
3456 return ExprError();
3457
Nico Webera7c7e602012-12-31 00:28:03 +00003458 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3459 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003460}
Anders Carlssone01493d2007-08-23 15:25:28 +00003461
3462/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00003463/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00003464ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003465Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00003466 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003467
Chris Lattner197a3012008-08-05 06:19:09 +00003468 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003469 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3470
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003471 BalancedDelimiterTracker T(*this, tok::l_paren);
3472 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003473
Chris Lattner197a3012008-08-05 06:19:09 +00003474 if (Tok.isNot(tok::identifier))
Alp Tokerec543272013-12-24 09:48:30 +00003475 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003476
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003477 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00003478 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003479
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003480 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00003481
Nico Webera7c7e602012-12-31 00:28:03 +00003482 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3483 T.getOpenLocation(), ProtoIdLoc,
3484 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00003485}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003486
3487/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003488/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003489ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003490 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003491
Chris Lattner197a3012008-08-05 06:19:09 +00003492 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003493 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3494
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003495 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003496 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003497
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003498 BalancedDelimiterTracker T(*this, tok::l_paren);
3499 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003500 bool HasOptionalParen = Tok.is(tok::l_paren);
3501 if (HasOptionalParen)
3502 ConsumeParen();
3503
Douglas Gregor67c692c2010-08-26 15:07:07 +00003504 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003505 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003506 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003507 return ExprError();
3508 }
3509
Chris Lattner4f472a32009-04-11 18:13:45 +00003510 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003511 if (!SelIdent && // missing selector name.
3512 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003513 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003514
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003515 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003516
Steve Naroff152dd812007-12-05 22:21:29 +00003517 unsigned nColons = 0;
3518 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003519 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003520 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003521 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003522 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003523 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3524 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003525 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003526
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003527 if (Tok.is(tok::r_paren))
3528 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003529
3530 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003531 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003532 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003533 return ExprError();
3534 }
3535
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003536 // Check for another keyword selector.
3537 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003538 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003539 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003540 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003541 break;
3542 }
Steve Naroff152dd812007-12-05 22:21:29 +00003543 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003544 if (HasOptionalParen && Tok.is(tok::r_paren))
3545 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003546 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003547 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003548 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3549 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003550 T.getCloseLocation(),
3551 !HasOptionalParen);
Gabor Greif24032f12007-10-19 15:38:32 +00003552 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003553
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003554void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3555 // MCDecl might be null due to error in method or c-function prototype, etc.
3556 Decl *MCDecl = LM.D;
3557 bool skip = MCDecl &&
3558 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3559 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3560 if (skip)
3561 return;
3562
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003563 // Save the current token position.
3564 SourceLocation OrigLoc = Tok.getLocation();
3565
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003566 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
3567 // Append the current token at the end of the new token stream so that it
3568 // doesn't get lost.
3569 LM.Toks.push_back(Tok);
3570 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
3571
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003572 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003573 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003574
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003575 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3576 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003577 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003578 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003579 parseMethod
3580 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
3581 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003582
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003583 // Tell the actions module that we have entered a method or c-function definition
3584 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003585 if (parseMethod)
3586 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3587 else
3588 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003589 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003590 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003591 else {
3592 if (Tok.is(tok::colon))
3593 ParseConstructorInitializer(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003594 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003595 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00003596
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003597 if (Tok.getLocation() != OrigLoc) {
3598 // Due to parsing error, we either went over the cached tokens or
3599 // there are still cached tokens left. If it's the latter case skip the
3600 // leftover tokens.
3601 // Since this is an uncommon situation that should be avoided, use the
3602 // expensive isBeforeInTranslationUnit call.
3603 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3604 OrigLoc))
3605 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3606 ConsumeAnyToken();
3607 }
3608
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003609 return;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003610}