blob: 4f16d47dfa758fb02160a74fca8f55b9ba762dd0 [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
1661 Actions.CodeCompleteObjCProtocolReferences(identifierLocPairs.data(),
1662 identifierLocPairs.size());
1663 cutOffParsing();
1664 return;
1665 }
1666
1667 allSingleIdentifiers = false;
1668 break;
1669 } while (TryConsumeToken(tok::comma));
1670
1671 // If we parsed an identifier list, semantic analysis sorts out
1672 // whether it refers to protocols or to type arguments.
1673 if (allSingleIdentifiers) {
1674 // Parse the closing '>'.
1675 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001676 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001677 /*ObjCGenericList=*/true);
1678
1679 // Let Sema figure out what we parsed.
1680 Actions.actOnObjCTypeArgsOrProtocolQualifiers(getCurScope(),
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001681 baseType,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001682 lAngleLoc,
1683 identifiers,
1684 identifierLocs,
1685 rAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001686 typeArgsLAngleLoc,
1687 typeArgs,
1688 typeArgsRAngleLoc,
1689 protocolLAngleLoc,
1690 protocols,
1691 protocolRAngleLoc,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001692 warnOnIncompleteProtocols);
1693 return;
1694 }
1695
1696 // We syntactically matched a type argument, so commit to parsing
1697 // type arguments.
Douglas Gregore9d95f12015-07-07 03:57:35 +00001698
1699 // Convert the identifiers into type arguments.
1700 bool invalid = false;
1701 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1702 ParsedType typeArg
1703 = Actions.getTypeName(*identifiers[i], identifierLocs[i], getCurScope());
1704 if (typeArg) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001705 DeclSpec DS(AttrFactory);
1706 const char *prevSpec = nullptr;
1707 unsigned diagID;
1708 DS.SetTypeSpecType(TST_typename, identifierLocs[i], prevSpec, diagID,
1709 typeArg, Actions.getASTContext().getPrintingPolicy());
1710
1711 // Form a declarator to turn this into a type.
1712 Declarator D(DS, Declarator::TypeNameContext);
1713 TypeResult fullTypeArg = Actions.ActOnTypeName(getCurScope(), D);
1714 if (fullTypeArg.isUsable())
1715 typeArgs.push_back(fullTypeArg.get());
1716 else
1717 invalid = true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001718 } else {
1719 invalid = true;
1720 }
1721 }
1722
1723 // Continue parsing type-names.
1724 do {
1725 TypeResult typeArg = ParseTypeName();
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001726
1727 // Consume the '...' for a pack expansion.
1728 SourceLocation ellipsisLoc;
1729 TryConsumeToken(tok::ellipsis, ellipsisLoc);
1730 if (typeArg.isUsable() && ellipsisLoc.isValid()) {
1731 typeArg = Actions.ActOnPackExpansion(typeArg.get(), ellipsisLoc);
1732 }
1733
Douglas Gregore9d95f12015-07-07 03:57:35 +00001734 if (typeArg.isUsable()) {
1735 typeArgs.push_back(typeArg.get());
1736 } else {
1737 invalid = true;
1738 }
1739 } while (TryConsumeToken(tok::comma));
1740
1741 // Parse the closing '>'.
1742 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001743 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001744 /*ObjCGenericList=*/true);
1745
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001746 if (invalid) {
1747 typeArgs.clear();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001748 return;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001749 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001750
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001751 // Record left/right angle locations.
1752 typeArgsLAngleLoc = lAngleLoc;
1753 typeArgsRAngleLoc = rAngleLoc;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001754}
1755
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001756void Parser::parseObjCTypeArgsAndProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001757 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001758 SourceLocation &typeArgsLAngleLoc,
1759 SmallVectorImpl<ParsedType> &typeArgs,
1760 SourceLocation &typeArgsRAngleLoc,
1761 SourceLocation &protocolLAngleLoc,
1762 SmallVectorImpl<Decl *> &protocols,
1763 SmallVectorImpl<SourceLocation> &protocolLocs,
1764 SourceLocation &protocolRAngleLoc,
1765 bool consumeLastToken) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001766 assert(Tok.is(tok::less));
1767
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001768 // Parse the first angle-bracket-delimited clause.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001769 parseObjCTypeArgsOrProtocolQualifiers(baseType,
1770 typeArgsLAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001771 typeArgs,
1772 typeArgsRAngleLoc,
1773 protocolLAngleLoc,
1774 protocols,
1775 protocolLocs,
1776 protocolRAngleLoc,
1777 consumeLastToken,
Douglas Gregore83b9562015-07-07 03:57:53 +00001778 /*warnOnIncompleteProtocols=*/false);
1779
1780 // An Objective-C object pointer followed by type arguments
1781 // can then be followed again by a set of protocol references, e.g.,
1782 // \c NSArray<NSView><NSTextDelegate>
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001783 if ((consumeLastToken && Tok.is(tok::less)) ||
1784 (!consumeLastToken && NextToken().is(tok::less))) {
1785 // If we aren't consuming the last token, the prior '>' is still hanging
1786 // there. Consume it before we parse the protocol qualifiers.
1787 if (!consumeLastToken)
1788 ConsumeToken();
1789
1790 if (!protocols.empty()) {
1791 SkipUntilFlags skipFlags = SkipUntilFlags();
1792 if (!consumeLastToken)
1793 skipFlags = skipFlags | StopBeforeMatch;
Douglas Gregore83b9562015-07-07 03:57:53 +00001794 Diag(Tok, diag::err_objc_type_args_after_protocols)
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001795 << SourceRange(protocolLAngleLoc, protocolRAngleLoc);
1796 SkipUntil(tok::greater, tok::greatergreater, skipFlags);
Douglas Gregore83b9562015-07-07 03:57:53 +00001797 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001798 ParseObjCProtocolReferences(protocols, protocolLocs,
1799 /*WarnOnDeclarations=*/false,
1800 /*ForObjCContainer=*/false,
1801 protocolLAngleLoc, protocolRAngleLoc,
1802 consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001803 }
1804 }
1805}
1806
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001807TypeResult Parser::parseObjCTypeArgsAndProtocolQualifiers(
1808 SourceLocation loc,
1809 ParsedType type,
1810 bool consumeLastToken,
1811 SourceLocation &endLoc) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001812 assert(Tok.is(tok::less));
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001813 SourceLocation typeArgsLAngleLoc;
1814 SmallVector<ParsedType, 4> typeArgs;
1815 SourceLocation typeArgsRAngleLoc;
1816 SourceLocation protocolLAngleLoc;
1817 SmallVector<Decl *, 4> protocols;
1818 SmallVector<SourceLocation, 4> protocolLocs;
1819 SourceLocation protocolRAngleLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00001820
1821 // Parse type arguments and protocol qualifiers.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001822 parseObjCTypeArgsAndProtocolQualifiers(type, typeArgsLAngleLoc, typeArgs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001823 typeArgsRAngleLoc, protocolLAngleLoc,
1824 protocols, protocolLocs,
1825 protocolRAngleLoc, consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001826
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001827 // Compute the location of the last token.
1828 if (consumeLastToken)
1829 endLoc = PrevTokLocation;
1830 else
1831 endLoc = Tok.getLocation();
1832
1833 return Actions.actOnObjCTypeArgsAndProtocolQualifiers(
1834 getCurScope(),
1835 loc,
1836 type,
1837 typeArgsLAngleLoc,
1838 typeArgs,
1839 typeArgsRAngleLoc,
1840 protocolLAngleLoc,
1841 protocols,
1842 protocolLocs,
1843 protocolRAngleLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00001844}
1845
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001846void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1847 BalancedDelimiterTracker &T,
1848 SmallVectorImpl<Decl *> &AllIvarDecls,
1849 bool RBraceMissing) {
1850 if (!RBraceMissing)
1851 T.consumeClose();
1852
1853 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1854 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1855 Actions.ActOnObjCContainerFinishDefinition();
1856 // Call ActOnFields() even if we don't have any decls. This is useful
1857 // for code rewriting tools that need to be aware of the empty list.
1858 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1859 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001860 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001861}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001862
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001863/// objc-class-instance-variables:
1864/// '{' objc-instance-variable-decl-list[opt] '}'
1865///
1866/// objc-instance-variable-decl-list:
1867/// objc-visibility-spec
1868/// objc-instance-variable-decl ';'
1869/// ';'
1870/// objc-instance-variable-decl-list objc-visibility-spec
1871/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1872/// objc-instance-variable-decl-list ';'
1873///
1874/// objc-visibility-spec:
1875/// @private
1876/// @protected
1877/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001878/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001879///
1880/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001881/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001882///
John McCall48871652010-08-21 09:40:31 +00001883void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001884 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001885 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001886 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001887 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001888
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001889 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001890 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001891
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001892 BalancedDelimiterTracker T(*this, tok::l_brace);
1893 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001894 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001895 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001896 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001897
Steve Naroff00433d32007-08-21 21:17:12 +00001898 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001899 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001900 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001901 continue;
1902 }
Mike Stump11289f42009-09-09 15:08:12 +00001903
Steve Naroff00433d32007-08-21 21:17:12 +00001904 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001905 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001906 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001907 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001908 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001909 }
1910
Steve Naroff7c348172007-08-23 18:16:40 +00001911 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001912 case tok::objc_private:
1913 case tok::objc_public:
1914 case tok::objc_protected:
1915 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001916 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001917 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001918 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001919
1920 case tok::objc_end:
1921 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001922 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1923 Tok.setKind(tok::at);
1924 Tok.setLength(1);
1925 PP.EnterToken(Tok);
1926 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1927 T, AllIvarDecls, true);
1928 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001929
1930 default:
1931 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1932 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001933 }
1934 }
Mike Stump11289f42009-09-09 15:08:12 +00001935
Douglas Gregor48d46252010-01-13 21:54:15 +00001936 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001937 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001938 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001939 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001940 }
John McCallcfefb6d2009-11-03 02:38:08 +00001941
Benjamin Kramera39beb92014-09-03 11:06:10 +00001942 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1943 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1944 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001945 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001946 Decl *Field = Actions.ActOnIvar(
1947 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1948 FD.BitfieldSize, visibility);
1949 Actions.ActOnObjCContainerFinishDefinition();
1950 if (Field)
1951 AllIvarDecls.push_back(Field);
1952 FD.complete(Field);
1953 };
John McCallcfefb6d2009-11-03 02:38:08 +00001954
Chris Lattnera12405b2008-04-10 06:46:29 +00001955 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001956 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001957 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001958
Chris Lattner0ef13522007-10-09 17:51:17 +00001959 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001960 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001961 } else {
1962 Diag(Tok, diag::err_expected_semi_decl_list);
1963 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001964 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001965 }
1966 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001967 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1968 T, AllIvarDecls, false);
Steve Naroff00433d32007-08-21 21:17:12 +00001969 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001970}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001971
1972/// objc-protocol-declaration:
1973/// objc-protocol-definition
1974/// objc-protocol-forward-reference
1975///
1976/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00001977/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00001978/// objc-protocol-refs[opt]
1979/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00001980/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001981///
1982/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00001983/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001984///
James Dennett1355bd12012-06-11 06:19:40 +00001985/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001986/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001987/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001988Parser::DeclGroupPtrTy
1989Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1990 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001991 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001992 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1993 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001994
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001995 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001996 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001997 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00001998 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001999 }
2000
Nico Weber69a79142013-04-04 00:15:10 +00002001 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00002002
Chris Lattner0ef13522007-10-09 17:51:17 +00002003 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002004 Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00002005 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002006 }
2007 // Save the protocol name, then consume it.
2008 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
2009 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002010
Alp Toker383d2c42014-01-01 03:08:43 +00002011 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00002012 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002013 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00002014 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002015 }
Mike Stump11289f42009-09-09 15:08:12 +00002016
Erik Verbruggenf9887852011-12-08 09:58:43 +00002017 CheckNestedObjCContexts(AtLoc);
2018
Chris Lattner0ef13522007-10-09 17:51:17 +00002019 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002020 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002021 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
2022
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002023 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002024 while (1) {
2025 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00002026 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002027 Diag(Tok, diag::err_expected) << tok::identifier;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002028 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00002029 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002030 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00002031 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
2032 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002033 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00002034
Chris Lattner0ef13522007-10-09 17:51:17 +00002035 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002036 break;
2037 }
2038 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00002039 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00002040 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002041
Steve Naroff93eb5f12007-10-10 17:32:04 +00002042 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002043 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00002044 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00002045 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00002046 }
Mike Stump11289f42009-09-09 15:08:12 +00002047
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002048 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00002049 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002050
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002051 SmallVector<Decl *, 8> ProtocolRefs;
2052 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002053 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00002054 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002055 LAngleLoc, EndProtoLoc,
2056 /*consumeLastToken=*/true))
Douglas Gregorf6102672012-01-01 21:23:57 +00002057 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002058
John McCall48871652010-08-21 09:40:31 +00002059 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00002060 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002061 ProtocolRefs.data(),
2062 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00002063 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00002064 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002065
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00002066 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00002067 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002068}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002069
2070/// objc-implementation:
2071/// objc-class-implementation-prologue
2072/// objc-category-implementation-prologue
2073///
2074/// objc-class-implementation-prologue:
2075/// @implementation identifier objc-superclass[opt]
2076/// objc-class-instance-variables[opt]
2077///
2078/// objc-category-implementation-prologue:
2079/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002080Parser::DeclGroupPtrTy
2081Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002082 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
2083 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002084 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002085 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002086
Douglas Gregor49c22a72009-11-18 16:26:39 +00002087 // Code completion after '@implementation'.
2088 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002089 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002090 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002091 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00002092 }
2093
Nico Weber69a79142013-04-04 00:15:10 +00002094 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00002095
Chris Lattner0ef13522007-10-09 17:51:17 +00002096 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002097 Diag(Tok, diag::err_expected)
2098 << tok::identifier; // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002099 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002100 }
2101 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00002102 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002103 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00002104 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002105
Douglas Gregor85f3f952015-07-07 03:57:15 +00002106 // Neither a type parameter list nor a list of protocol references is
2107 // permitted here. Parse and diagnose them.
2108 if (Tok.is(tok::less)) {
2109 SourceLocation lAngleLoc, rAngleLoc;
2110 SmallVector<IdentifierLocPair, 8> protocolIdents;
2111 SourceLocation diagLoc = Tok.getLocation();
2112 if (parseObjCTypeParamListOrProtocolRefs(lAngleLoc, protocolIdents,
2113 rAngleLoc)) {
2114 Diag(diagLoc, diag::err_objc_parameterized_implementation)
2115 << SourceRange(diagLoc, PrevTokLocation);
2116 } else if (lAngleLoc.isValid()) {
2117 Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
2118 << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
2119 }
2120 }
2121
Mike Stump11289f42009-09-09 15:08:12 +00002122 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002123 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002124 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002125 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002126 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002127
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002128 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002129 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002130 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002131 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002132 }
2133
Chris Lattner0ef13522007-10-09 17:51:17 +00002134 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002135 categoryId = Tok.getIdentifierInfo();
2136 categoryLoc = ConsumeToken();
2137 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002138 Diag(Tok, diag::err_expected)
2139 << tok::identifier; // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002140 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00002141 }
Chris Lattner0ef13522007-10-09 17:51:17 +00002142 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002143 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002144 SkipUntil(tok::r_paren); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002145 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002146 }
2147 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00002148 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2149 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002150 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2151 SmallVector<Decl *, 4> protocols;
2152 SmallVector<SourceLocation, 4> protocolLocs;
2153 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2154 /*warnOnIncompleteProtocols=*/false,
2155 /*ForObjCContainer=*/false,
2156 protocolLAngleLoc, protocolRAngleLoc,
2157 /*consumeLastToken=*/true);
Fariborz Jahanian85888552013-05-17 17:58:11 +00002158 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002159 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002160 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00002161 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002162
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002163 } else {
2164 // We have a class implementation
2165 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002166 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00002167 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002168 // We have a super class
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002169 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002170 Diag(Tok, diag::err_expected)
2171 << tok::identifier; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002172 return DeclGroupPtrTy();
2173 }
2174 superClassId = Tok.getIdentifierInfo();
2175 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002176 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002177 ObjCImpDecl = Actions.ActOnStartClassImplementation(
2178 AtLoc, nameId, nameLoc,
2179 superClassId, superClassLoc);
2180
2181 if (Tok.is(tok::l_brace)) // we have ivars
2182 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002183 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2184 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002185
2186 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2187 SmallVector<Decl *, 4> protocols;
2188 SmallVector<SourceLocation, 4> protocolLocs;
2189 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2190 /*warnOnIncompleteProtocols=*/false,
2191 /*ForObjCContainer=*/false,
2192 protocolLAngleLoc, protocolRAngleLoc,
2193 /*consumeLastToken=*/true);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002194 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002195 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002196 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002197
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002198 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002199
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002200 {
2201 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00002202 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002203 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002204 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002205 MaybeParseMicrosoftAttributes(attrs);
2206 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
2207 DeclGroupRef DG = DGP.get();
2208 DeclsInGroup.append(DG.begin(), DG.end());
2209 }
2210 }
2211 }
2212
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00002213 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002214}
Steve Naroff33a1e802007-10-29 21:38:07 +00002215
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002216Parser::DeclGroupPtrTy
2217Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002218 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
2219 "ParseObjCAtEndDeclaration(): Expected @end");
2220 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002221 if (CurParsedObjCImpl)
2222 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002223 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00002224 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002225 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002226 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002227}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002228
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002229Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
2230 if (!Finished) {
2231 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00002232 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002233 P.Diag(P.Tok, diag::err_objc_missing_end)
2234 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
2235 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
2236 << Sema::OCK_Implementation;
2237 }
2238 }
Craig Topper161e4db2014-05-21 06:02:52 +00002239 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002240 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002241}
2242
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002243void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
2244 assert(!Finished);
2245 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
2246 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002247 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2248 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002249
2250 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
2251
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002252 if (HasCFunction)
2253 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
2254 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2255 false/*c-functions*/);
2256
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002257 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002258 for (LateParsedObjCMethodContainer::iterator
2259 I = LateParsedObjCMethods.begin(),
2260 E = LateParsedObjCMethods.end(); I != E; ++I)
2261 delete *I;
2262 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002263
2264 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002265}
2266
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002267/// compatibility-alias-decl:
2268/// @compatibility_alias alias-name class-name ';'
2269///
John McCall48871652010-08-21 09:40:31 +00002270Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002271 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
2272 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
2273 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00002274 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002275 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002276 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002277 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002278 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
2279 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00002280 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002281 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002282 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002283 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002284 IdentifierInfo *classId = Tok.getIdentifierInfo();
2285 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00002286 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00002287 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
2288 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002289}
2290
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002291/// property-synthesis:
2292/// @synthesize property-ivar-list ';'
2293///
2294/// property-ivar-list:
2295/// property-ivar
2296/// property-ivar-list ',' property-ivar
2297///
2298/// property-ivar:
2299/// identifier
2300/// identifier '=' identifier
2301///
John McCall48871652010-08-21 09:40:31 +00002302Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002303 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00002304 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002305 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00002306
Douglas Gregor88e72a02009-11-18 19:45:45 +00002307 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00002308 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002309 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002310 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002311 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002312 }
2313
Douglas Gregor88e72a02009-11-18 19:45:45 +00002314 if (Tok.isNot(tok::identifier)) {
2315 Diag(Tok, diag::err_synthesized_property_name);
2316 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002317 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00002318 }
Craig Topper161e4db2014-05-21 06:02:52 +00002319
2320 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002321 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2322 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002323 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00002324 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002325 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00002326 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002327 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002328 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002329 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002330 }
2331
Chris Lattner0ef13522007-10-09 17:51:17 +00002332 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002333 Diag(Tok, diag::err_expected) << tok::identifier;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002334 break;
2335 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002336 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002337 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002338 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002339 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002340 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002341 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002342 break;
2343 ConsumeToken(); // consume ','
2344 }
Alp Toker383d2c42014-01-01 03:08:43 +00002345 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00002346 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002347}
2348
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002349/// property-dynamic:
2350/// @dynamic property-list
2351///
2352/// property-list:
2353/// identifier
2354/// property-list ',' identifier
2355///
John McCall48871652010-08-21 09:40:31 +00002356Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002357 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2358 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002359 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002360 while (true) {
2361 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002362 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002363 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002364 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002365 }
2366
2367 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002368 Diag(Tok, diag::err_expected) << tok::identifier;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002369 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002370 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002371 }
2372
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002373 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2374 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002375 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Craig Topper161e4db2014-05-21 06:02:52 +00002376 propertyId, nullptr, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002377
Chris Lattner0ef13522007-10-09 17:51:17 +00002378 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002379 break;
2380 ConsumeToken(); // consume ','
2381 }
Alp Toker383d2c42014-01-01 03:08:43 +00002382 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00002383 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002384}
Mike Stump11289f42009-09-09 15:08:12 +00002385
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002386/// objc-throw-statement:
2387/// throw expression[opt];
2388///
John McCalldadc5752010-08-24 06:29:42 +00002389StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2390 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002391 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00002392 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00002393 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002394 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002395 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002396 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002397 }
2398 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00002399 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00002400 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002401 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002402}
2403
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002404/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002405/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002406///
John McCalldadc5752010-08-24 06:29:42 +00002407StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002408Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002409 ConsumeToken(); // consume synchronized
2410 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002411 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002412 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002413 }
John McCalld9bb7432011-07-27 21:50:02 +00002414
2415 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002416 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00002417 ExprResult operand(ParseExpression());
2418
2419 if (Tok.is(tok::r_paren)) {
2420 ConsumeParen(); // ')'
2421 } else {
2422 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002423 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00002424
2425 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002426 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002427 }
John McCalld9bb7432011-07-27 21:50:02 +00002428
2429 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002430 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00002431 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002432 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002433 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002434 }
Steve Naroffd9c26072008-06-04 20:36:13 +00002435
John McCalld9bb7432011-07-27 21:50:02 +00002436 // Check the @synchronized operand now.
2437 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002438 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002439
John McCalld9bb7432011-07-27 21:50:02 +00002440 // Parse the compound statement within a new scope.
2441 ParseScope bodyScope(this, Scope::DeclScope);
2442 StmtResult body(ParseCompoundStatementBody());
2443 bodyScope.Exit();
2444
2445 // If there was a semantic or parse error earlier with the
2446 // operand, fail now.
2447 if (operand.isInvalid())
2448 return StmtError();
2449
2450 if (body.isInvalid())
2451 body = Actions.ActOnNullStmt(Tok.getLocation());
2452
2453 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002454}
2455
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002456/// objc-try-catch-statement:
2457/// @try compound-statement objc-catch-list[opt]
2458/// @try compound-statement objc-catch-list[opt] @finally compound-statement
2459///
2460/// objc-catch-list:
2461/// @catch ( parameter-declaration ) compound-statement
2462/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2463/// catch-parameter-declaration:
2464/// parameter-declaration
2465/// '...' [OBJC2]
2466///
John McCalldadc5752010-08-24 06:29:42 +00002467StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002468 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002469
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002470 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00002471 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002472 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002473 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002474 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00002475 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00002476 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002477 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00002478 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002479 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002480 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002481 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002482
Chris Lattner0ef13522007-10-09 17:51:17 +00002483 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002484 // At this point, we need to lookahead to determine if this @ is the start
2485 // of an @catch or @finally. We don't want to consume the @ token if this
2486 // is an @try or @encode or something else.
2487 Token AfterAt = GetLookAheadToken(1);
2488 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2489 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2490 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002491
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002492 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002493 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002494 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002495 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002496 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002497 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00002498 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002499 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002500 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002501 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00002502 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002503 ParseDeclarator(ParmDecl);
2504
Douglas Gregore11ee112010-04-23 23:01:43 +00002505 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002506 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002507 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002508 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002509 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002510
Steve Naroff65a00892009-04-07 22:56:58 +00002511 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002512
Steve Naroff65a00892009-04-07 22:56:58 +00002513 if (Tok.is(tok::r_paren))
2514 RParenLoc = ConsumeParen();
2515 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002516 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002517
John McCalldadc5752010-08-24 06:29:42 +00002518 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002519 if (Tok.is(tok::l_brace))
2520 CatchBody = ParseCompoundStatementBody();
2521 else
Alp Tokerec543272013-12-24 09:48:30 +00002522 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002523 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002524 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00002525
John McCalldadc5752010-08-24 06:29:42 +00002526 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00002527 RParenLoc,
2528 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002529 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002530 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002531 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002532
Steve Naroffe6016792008-02-05 21:27:35 +00002533 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002534 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2535 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002536 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002537 }
2538 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002539 } else {
2540 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002541 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002542 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002543
John McCalldadc5752010-08-24 06:29:42 +00002544 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002545 if (Tok.is(tok::l_brace))
2546 FinallyBody = ParseCompoundStatementBody();
2547 else
Alp Tokerec543272013-12-24 09:48:30 +00002548 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002549 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002550 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002551 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002552 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002553 catch_or_finally_seen = true;
2554 break;
2555 }
2556 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002557 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002558 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002559 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002560 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002561
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002562 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002563 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002564 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002565}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002566
John McCall31168b02011-06-15 23:02:42 +00002567/// objc-autoreleasepool-statement:
2568/// @autoreleasepool compound-statement
2569///
2570StmtResult
2571Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2572 ConsumeToken(); // consume autoreleasepool
2573 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002574 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002575 return StmtError();
2576 }
2577 // Enter a scope to hold everything within the compound stmt. Compound
2578 // statements can always hold declarations.
2579 ParseScope BodyScope(this, Scope::DeclScope);
2580
2581 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2582
2583 BodyScope.Exit();
2584 if (AutoreleasePoolBody.isInvalid())
2585 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2586 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002587 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002588}
2589
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002590/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2591/// for later parsing.
2592void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
2593 LexedMethod* LM = new LexedMethod(this, MDecl);
2594 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2595 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002596 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002597 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002598 if (Tok.is(tok::kw_try)) {
2599 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002600 if (Tok.is(tok::colon)) {
2601 Toks.push_back(Tok);
2602 ConsumeToken();
2603 while (Tok.isNot(tok::l_brace)) {
2604 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2605 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2606 }
2607 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002608 Toks.push_back(Tok); // also store '{'
2609 }
2610 else if (Tok.is(tok::colon)) {
2611 ConsumeToken();
2612 while (Tok.isNot(tok::l_brace)) {
2613 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2614 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2615 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002616 Toks.push_back(Tok); // also store '{'
2617 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002618 ConsumeBrace();
2619 // Consume everything up to (and including) the matching right brace.
2620 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002621 while (Tok.is(tok::kw_catch)) {
2622 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2623 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2624 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002625}
2626
Steve Naroff09bf8152007-09-06 21:24:23 +00002627/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002628///
John McCall48871652010-08-21 09:40:31 +00002629Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002630 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002631
John McCallfaf5fb42010-08-26 23:41:50 +00002632 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2633 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002634
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002635 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002636 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002637 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002638 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002639 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002640 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002641 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002642 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002643
Steve Naroffbb875722007-11-11 19:54:21 +00002644 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002645 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002646 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002647
Steve Naroffbb875722007-11-11 19:54:21 +00002648 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002649 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002650
Steve Naroffbb875722007-11-11 19:54:21 +00002651 // If we didn't find the '{', bail out.
2652 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002653 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002654 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002655
2656 if (!MDecl) {
2657 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002658 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002659 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002660 }
2661
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002662 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002663 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002664 assert (CurParsedObjCImpl
2665 && "ParseObjCMethodDefinition - Method out of @implementation");
2666 // Consume the tokens and store them for later parsing.
2667 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002668 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002669}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002670
John McCalldadc5752010-08-24 06:29:42 +00002671StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002672 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002673 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002674 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002675 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002676 }
2677
2678 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002679 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002680
2681 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002682 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002683
2684 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002685 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002686
2687 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2688 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002689
2690 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2691 getLangOpts().DebuggerSupport) {
2692 SkipUntil(tok::semi);
2693 return Actions.ActOnNullStmt(Tok.getLocation());
2694 }
2695
John McCalldadc5752010-08-24 06:29:42 +00002696 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002697 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002698 // If the expression is invalid, skip ahead to the next semicolon. Not
2699 // doing this opens us up to the possibility of infinite loops if
2700 // ParseExpression does not consume any tokens.
2701 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002702 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002703 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002704
Steve Naroffe6016792008-02-05 21:27:35 +00002705 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002706 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002707 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002708}
2709
John McCalldadc5752010-08-24 06:29:42 +00002710ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002711 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002712 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002713 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002714 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002715 return ExprError();
2716
Ted Kremeneke65b0862012-03-06 20:05:56 +00002717 case tok::minus:
2718 case tok::plus: {
2719 tok::TokenKind Kind = Tok.getKind();
2720 SourceLocation OpLoc = ConsumeToken();
2721
2722 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002723 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002724 switch (Kind) {
2725 case tok::minus: Symbol = "-"; break;
2726 case tok::plus: Symbol = "+"; break;
2727 default: llvm_unreachable("missing unary operator case");
2728 }
2729 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2730 << Symbol;
2731 return ExprError();
2732 }
2733
2734 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2735 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002736 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002737 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002738 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002739
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002740 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002741 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002742 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002743
2744 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002745 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002746 }
2747
Chris Lattnere002fbe2007-12-12 01:04:12 +00002748 case tok::string_literal: // primary-expression: string-literal
2749 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002750 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002751
2752 case tok::char_constant:
2753 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2754
2755 case tok::numeric_constant:
2756 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2757
2758 case tok::kw_true: // Objective-C++, etc.
2759 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2760 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2761 case tok::kw_false: // Objective-C++, etc.
2762 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2763 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2764
2765 case tok::l_square:
2766 // Objective-C array literal
2767 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2768
2769 case tok::l_brace:
2770 // Objective-C dictionary literal
2771 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2772
Patrick Beard0caa3942012-04-19 00:25:12 +00002773 case tok::l_paren:
2774 // Objective-C boxed expression
2775 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2776
Chris Lattnere002fbe2007-12-12 01:04:12 +00002777 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002778 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002779 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002780
Chris Lattner197a3012008-08-05 06:19:09 +00002781 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2782 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002783 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002784 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002785 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002786 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002787 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002788 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002789 const char *str = nullptr;
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002790 if (GetLookAheadToken(1).is(tok::l_brace)) {
2791 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2792 str =
2793 ch == 't' ? "try"
2794 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002795 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002796 }
2797 if (str) {
2798 SourceLocation kwLoc = Tok.getLocation();
2799 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2800 FixItHint::CreateReplacement(kwLoc, str));
2801 }
2802 else
2803 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2804 }
Chris Lattner197a3012008-08-05 06:19:09 +00002805 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002806 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002807}
2808
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002809/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002810///
2811/// This routine parses the receiver of a message send in
2812/// Objective-C++ either as a type or as an expression. Note that this
2813/// routine must not be called to parse a send to 'super', since it
2814/// has no way to return such a result.
2815///
2816/// \param IsExpr Whether the receiver was parsed as an expression.
2817///
2818/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2819/// IsExpr is true), the parsed expression. If the receiver was parsed
2820/// as a type (\c IsExpr is false), the parsed type.
2821///
2822/// \returns True if an error occurred during parsing or semantic
2823/// analysis, in which case the arguments do not have valid
2824/// values. Otherwise, returns false for a successful parse.
2825///
2826/// objc-receiver: [C++]
2827/// 'super' [not parsed here]
2828/// expression
2829/// simple-type-specifier
2830/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002831bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002832 InMessageExpressionRAIIObject InMessage(*this, true);
2833
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002834 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2835 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002836 TryAnnotateTypeOrScopeToken();
2837
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002838 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002839 // objc-receiver:
2840 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002841 // Make sure any typos in the receiver are corrected or diagnosed, so that
2842 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2843 // only the things that are valid ObjC receivers?
2844 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002845 if (Receiver.isInvalid())
2846 return true;
2847
2848 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002849 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002850 return false;
2851 }
2852
2853 // objc-receiver:
2854 // typename-specifier
2855 // simple-type-specifier
2856 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002857 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002858 ParseCXXSimpleTypeSpecifier(DS);
2859
2860 if (Tok.is(tok::l_paren)) {
2861 // If we see an opening parentheses at this point, we are
2862 // actually parsing an expression that starts with a
2863 // function-style cast, e.g.,
2864 //
2865 // postfix-expression:
2866 // simple-type-specifier ( expression-list [opt] )
2867 // typename-specifier ( expression-list [opt] )
2868 //
2869 // Parse the remainder of this case, then the (optional)
2870 // postfix-expression suffix, followed by the (optional)
2871 // right-hand side of the binary expression. We have an
2872 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002873 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002874 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002875 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002876 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002877 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002878 if (Receiver.isInvalid())
2879 return true;
2880
2881 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002882 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002883 return false;
2884 }
2885
2886 // We have a class message. Turn the simple-type-specifier or
2887 // typename-specifier we parsed into a type and parse the
2888 // remainder of the class message.
2889 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002890 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002891 if (Type.isInvalid())
2892 return true;
2893
2894 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002895 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002896 return false;
2897}
2898
Douglas Gregor990ccac2010-05-31 14:40:22 +00002899/// \brief Determine whether the parser is currently referring to a an
2900/// Objective-C message send, using a simplified heuristic to avoid overhead.
2901///
2902/// This routine will only return true for a subset of valid message-send
2903/// expressions.
2904bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002905 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002906 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002907 return GetLookAheadToken(1).is(tok::identifier) &&
2908 GetLookAheadToken(2).is(tok::identifier);
2909}
2910
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002911bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002912 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002913 InMessageExpression)
2914 return false;
2915
2916
2917 ParsedType Type;
2918
2919 if (Tok.is(tok::annot_typename))
2920 Type = getTypeAnnotation(Tok);
2921 else if (Tok.is(tok::identifier))
2922 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2923 getCurScope());
2924 else
2925 return false;
2926
2927 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2928 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002929 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002930 if (Tok.is(tok::identifier))
2931 TryAnnotateTypeOrScopeToken();
2932
2933 return Tok.is(tok::annot_typename);
2934 }
2935 }
2936
2937 return false;
2938}
2939
Mike Stump11289f42009-09-09 15:08:12 +00002940/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002941/// '[' objc-receiver objc-message-args ']'
2942///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002943/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002944/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002945/// expression
2946/// class-name
2947/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002948///
John McCalldadc5752010-08-24 06:29:42 +00002949ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002950 assert(Tok.is(tok::l_square) && "'[' expected");
2951 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2952
Douglas Gregora817a192010-05-27 23:06:34 +00002953 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002954 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002955 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002956 return ExprError();
2957 }
2958
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002959 InMessageExpressionRAIIObject InMessage(*this, true);
2960
David Blaikiebbafb8a2012-03-11 07:00:24 +00002961 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002962 // We completely separate the C and C++ cases because C++ requires
2963 // more complicated (read: slower) parsing.
2964
2965 // Handle send to super.
2966 // FIXME: This doesn't benefit from the same typo-correction we
2967 // get in Objective-C.
2968 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002969 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002970 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002971 ParsedType(), nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002972
2973 // Parse the receiver, which is either a type or an expression.
2974 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00002975 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002976 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002977 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002978 return ExprError();
2979 }
2980
2981 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002982 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2983 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002984 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002985
2986 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002987 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00002988 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00002989 }
2990
2991 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002992 IdentifierInfo *Name = Tok.getIdentifierInfo();
2993 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002994 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002995 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002996 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002997 NextToken().is(tok::period),
2998 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002999 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00003000 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00003001 ParsedType(), nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003002
John McCallfaf5fb42010-08-26 23:41:50 +00003003 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00003004 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003005 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003006 return ExprError();
3007 }
3008
Douglas Gregore5798dc2010-04-21 20:38:13 +00003009 ConsumeToken(); // the type name
3010
Douglas Gregore83b9562015-07-07 03:57:53 +00003011 // Parse type arguments and protocol qualifiers.
3012 if (Tok.is(tok::less)) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003013 SourceLocation NewEndLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00003014 TypeResult NewReceiverType
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003015 = parseObjCTypeArgsAndProtocolQualifiers(NameLoc, ReceiverType,
3016 /*consumeLastToken=*/true,
3017 NewEndLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00003018 if (!NewReceiverType.isUsable()) {
3019 SkipUntil(tok::r_square, StopAtSemi);
3020 return ExprError();
3021 }
3022
3023 ReceiverType = NewReceiverType.get();
3024 }
3025
Douglas Gregore5798dc2010-04-21 20:38:13 +00003026 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00003027 ReceiverType, nullptr);
3028
John McCallfaf5fb42010-08-26 23:41:50 +00003029 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003030 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00003031 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00003032 }
Chris Lattner8f697062008-01-25 18:59:06 +00003033 }
Chris Lattnera36ec422010-04-11 08:28:14 +00003034
3035 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00003036 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003037 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003038 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003039 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00003040 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003041
John McCallba7bf592010-08-24 05:47:05 +00003042 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003043 ParsedType(), Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00003044}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003045
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003046/// \brief Parse the remainder of an Objective-C message following the
3047/// '[' objc-receiver.
3048///
3049/// This routine handles sends to super, class messages (sent to a
3050/// class name), and instance messages (sent to an object), and the
3051/// target is represented by \p SuperLoc, \p ReceiverType, or \p
3052/// ReceiverExpr, respectively. Only one of these parameters may have
3053/// a valid value.
3054///
3055/// \param LBracLoc The location of the opening '['.
3056///
3057/// \param SuperLoc If this is a send to 'super', the location of the
3058/// 'super' keyword that indicates a send to the superclass.
3059///
3060/// \param ReceiverType If this is a class message, the type of the
3061/// class we are sending a message to.
3062///
3063/// \param ReceiverExpr If this is an instance message, the expression
3064/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00003065///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003066/// objc-message-args:
3067/// objc-selector
3068/// objc-keywordarg-list
3069///
3070/// objc-keywordarg-list:
3071/// objc-keywordarg
3072/// objc-keywordarg-list objc-keywordarg
3073///
Mike Stump11289f42009-09-09 15:08:12 +00003074/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003075/// selector-name[opt] ':' objc-keywordexpr
3076///
3077/// objc-keywordexpr:
3078/// nonempty-expr-list
3079///
3080/// nonempty-expr-list:
3081/// assignment-expression
3082/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003083///
John McCalldadc5752010-08-24 06:29:42 +00003084ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00003085Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003086 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00003087 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00003088 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003089 InMessageExpressionRAIIObject InMessage(*this, true);
3090
Steve Naroffeae65032009-11-07 02:08:14 +00003091 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003092 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003093 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003094 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003095 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003096 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003097 false);
Steve Naroffeae65032009-11-07 02:08:14 +00003098 else
John McCallb268a282010-08-23 23:25:46 +00003099 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003100 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003101 cutOffParsing();
3102 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00003103 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00003104
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003105 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00003106 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003107 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003108
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003109 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003110 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003111 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00003112
Chris Lattner0ef13522007-10-09 17:51:17 +00003113 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003114 while (1) {
3115 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00003116 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003117 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00003118
Alp Toker383d2c42014-01-01 03:08:43 +00003119 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00003120 // We must manually skip to a ']', otherwise the expression skipper will
3121 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3122 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003123 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003124 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003125 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003126
Mike Stump11289f42009-09-09 15:08:12 +00003127 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003128
3129 if (Tok.is(tok::code_completion)) {
3130 if (SuperLoc.isValid())
3131 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003132 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003133 /*AtArgumentEpression=*/true);
3134 else if (ReceiverType)
3135 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003136 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003137 /*AtArgumentEpression=*/true);
3138 else
3139 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003140 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003141 /*AtArgumentEpression=*/true);
3142
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003143 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003144 return ExprError();
3145 }
3146
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00003147 ExprResult Expr;
3148 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3149 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3150 Expr = ParseBraceInitializer();
3151 } else
3152 Expr = ParseAssignmentExpression();
3153
3154 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003155 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00003156 // We must manually skip to a ']', otherwise the expression skipper will
3157 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3158 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003159 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003160 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00003161 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003162
Steve Naroff486760a2007-09-17 20:25:27 +00003163 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003164 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003165
Douglas Gregor1b605f72009-11-19 01:08:35 +00003166 // Code completion after each argument.
3167 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003168 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003169 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003170 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003171 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003172 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003173 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003174 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003175 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00003176 else
John McCallb268a282010-08-23 23:25:46 +00003177 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003178 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003179 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003180 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003181 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00003182 }
3183
Steve Naroff486760a2007-09-17 20:25:27 +00003184 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00003185 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00003186 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003187 break;
3188 // We have a selector or a colon, continue parsing.
3189 }
3190 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00003191 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003192 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00003193 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00003194 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00003195 if (Tok.is(tok::colon))
3196 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003197 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003198 if (Tok.is(tok::colon)) {
3199 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
3200 FixItHint::CreateRemoval(commaLoc);
3201 }
Chris Lattner197a3012008-08-05 06:19:09 +00003202 // We must manually skip to a ']', otherwise the expression skipper will
3203 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3204 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003205 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003206 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003207 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003208
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003209 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003210 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003211 }
3212 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00003213 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003214
Chris Lattner197a3012008-08-05 06:19:09 +00003215 // We must manually skip to a ']', otherwise the expression skipper will
3216 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3217 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003218 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003219 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003220 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00003221
Chris Lattner0ef13522007-10-09 17:51:17 +00003222 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00003223 Diag(Tok, diag::err_expected)
3224 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00003225 // We must manually skip to a ']', otherwise the expression skipper will
3226 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3227 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003228 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003229 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003230 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003231
Chris Lattner8f697062008-01-25 18:59:06 +00003232 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003233
Steve Naroffe61bfa82007-10-05 18:42:47 +00003234 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003235 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00003236 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003237 KeyLocs.push_back(Loc);
3238 }
Chris Lattner5700fab2007-10-07 02:00:24 +00003239 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003240
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003241 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003242 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003243 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003244 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003245 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003246 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00003247 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003248 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003249}
3250
John McCalldadc5752010-08-24 06:29:42 +00003251ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
3252 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003253 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003254
Chris Lattnere002fbe2007-12-12 01:04:12 +00003255 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
3256 // expressions. At this point, we know that the only valid thing that starts
3257 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003258 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003259 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00003260 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003261 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003262
Chris Lattnere002fbe2007-12-12 01:04:12 +00003263 while (Tok.is(tok::at)) {
3264 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00003265
Sebastian Redlc13f2682008-12-09 20:22:58 +00003266 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00003267 if (!isTokenStringLiteral())
3268 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00003269
John McCalldadc5752010-08-24 06:29:42 +00003270 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003271 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003272 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003273
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003274 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00003275 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003276
Nico Webera7c7e602012-12-31 00:28:03 +00003277 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
3278 AtStrings.size());
Anders Carlsson76f4a902007-08-21 17:43:55 +00003279}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003280
Ted Kremeneke65b0862012-03-06 20:05:56 +00003281/// ParseObjCBooleanLiteral -
3282/// objc-scalar-literal : '@' boolean-keyword
3283/// ;
3284/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
3285/// ;
3286ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
3287 bool ArgValue) {
3288 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
3289 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
3290}
3291
3292/// ParseObjCCharacterLiteral -
3293/// objc-scalar-literal : '@' character-literal
3294/// ;
3295ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
3296 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
3297 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003298 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003299 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003300 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003301 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003302}
3303
3304/// ParseObjCNumericLiteral -
3305/// objc-scalar-literal : '@' scalar-literal
3306/// ;
3307/// scalar-literal : | numeric-constant /* any numeric constant. */
3308/// ;
3309ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3310 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3311 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003312 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003313 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003314 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003315 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003316}
3317
Patrick Beard0caa3942012-04-19 00:25:12 +00003318/// ParseObjCBoxedExpr -
3319/// objc-box-expression:
3320/// @( assignment-expression )
3321ExprResult
3322Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3323 if (Tok.isNot(tok::l_paren))
3324 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3325
3326 BalancedDelimiterTracker T(*this, tok::l_paren);
3327 T.consumeOpen();
3328 ExprResult ValueExpr(ParseAssignmentExpression());
3329 if (T.consumeClose())
3330 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00003331
3332 if (ValueExpr.isInvalid())
3333 return ExprError();
3334
Patrick Beard0caa3942012-04-19 00:25:12 +00003335 // Wrap the sub-expression in a parenthesized expression, to distinguish
3336 // a boxed expression from a literal.
3337 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003338 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00003339 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003340 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00003341}
3342
Ted Kremeneke65b0862012-03-06 20:05:56 +00003343ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00003344 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003345 ConsumeBracket(); // consume the l_square.
3346
3347 while (Tok.isNot(tok::r_square)) {
3348 // Parse list of array element expressions (all must be id types).
3349 ExprResult Res(ParseAssignmentExpression());
3350 if (Res.isInvalid()) {
3351 // We must manually skip to a ']', otherwise the expression skipper will
3352 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3353 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003354 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003355 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003356 }
3357
3358 // Parse the ellipsis that indicates a pack expansion.
3359 if (Tok.is(tok::ellipsis))
3360 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
3361 if (Res.isInvalid())
3362 return true;
3363
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003364 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003365
3366 if (Tok.is(tok::comma))
3367 ConsumeToken(); // Eat the ','.
3368 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00003369 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3370 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003371 }
3372 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramerf0623432012-08-23 22:51:59 +00003373 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00003374 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003375}
3376
3377ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3378 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3379 ConsumeBrace(); // consume the l_square.
3380 while (Tok.isNot(tok::r_brace)) {
3381 // Parse the comma separated key : value expressions.
3382 ExprResult KeyExpr;
3383 {
3384 ColonProtectionRAIIObject X(*this);
3385 KeyExpr = ParseAssignmentExpression();
3386 if (KeyExpr.isInvalid()) {
3387 // We must manually skip to a '}', otherwise the expression skipper will
3388 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3389 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003390 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003391 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003392 }
3393 }
3394
Alp Toker383d2c42014-01-01 03:08:43 +00003395 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003396 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00003397 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003398 }
3399
3400 ExprResult ValueExpr(ParseAssignmentExpression());
3401 if (ValueExpr.isInvalid()) {
3402 // We must manually skip to a '}', otherwise the expression skipper will
3403 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3404 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003405 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003406 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003407 }
3408
3409 // Parse the ellipsis that designates this as a pack expansion.
3410 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00003411 if (getLangOpts().CPlusPlus)
3412 TryConsumeToken(tok::ellipsis, EllipsisLoc);
3413
Ted Kremeneke65b0862012-03-06 20:05:56 +00003414 // We have a valid expression. Collect it in a vector so we can
3415 // build the argument list.
3416 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00003417 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00003418 };
3419 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00003420
3421 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00003422 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3423 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003424 }
3425 SourceLocation EndLoc = ConsumeBrace();
3426
3427 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00003428 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
3429 Elements.data(), Elements.size());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003430}
3431
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003432/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00003433/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00003434ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003435Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00003436 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003437
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003438 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003439
Chris Lattner197a3012008-08-05 06:19:09 +00003440 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003441 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3442
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003443 BalancedDelimiterTracker T(*this, tok::l_paren);
3444 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003445
Douglas Gregor220cac52009-02-18 17:45:20 +00003446 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003447
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003448 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003449
Douglas Gregor220cac52009-02-18 17:45:20 +00003450 if (Ty.isInvalid())
3451 return ExprError();
3452
Nico Webera7c7e602012-12-31 00:28:03 +00003453 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3454 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003455}
Anders Carlssone01493d2007-08-23 15:25:28 +00003456
3457/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00003458/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00003459ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003460Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00003461 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003462
Chris Lattner197a3012008-08-05 06:19:09 +00003463 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003464 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3465
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003466 BalancedDelimiterTracker T(*this, tok::l_paren);
3467 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003468
Chris Lattner197a3012008-08-05 06:19:09 +00003469 if (Tok.isNot(tok::identifier))
Alp Tokerec543272013-12-24 09:48:30 +00003470 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003471
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003472 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00003473 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003474
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003475 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00003476
Nico Webera7c7e602012-12-31 00:28:03 +00003477 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3478 T.getOpenLocation(), ProtoIdLoc,
3479 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00003480}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003481
3482/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003483/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003484ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003485 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003486
Chris Lattner197a3012008-08-05 06:19:09 +00003487 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003488 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3489
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003490 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003491 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003492
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003493 BalancedDelimiterTracker T(*this, tok::l_paren);
3494 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003495 bool HasOptionalParen = Tok.is(tok::l_paren);
3496 if (HasOptionalParen)
3497 ConsumeParen();
3498
Douglas Gregor67c692c2010-08-26 15:07:07 +00003499 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003500 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003501 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003502 return ExprError();
3503 }
3504
Chris Lattner4f472a32009-04-11 18:13:45 +00003505 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003506 if (!SelIdent && // missing selector name.
3507 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003508 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003509
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003510 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003511
Steve Naroff152dd812007-12-05 22:21:29 +00003512 unsigned nColons = 0;
3513 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003514 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003515 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003516 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003517 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003518 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3519 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003520 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003521
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003522 if (Tok.is(tok::r_paren))
3523 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003524
3525 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003526 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003527 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003528 return ExprError();
3529 }
3530
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003531 // Check for another keyword selector.
3532 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003533 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003534 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003535 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003536 break;
3537 }
Steve Naroff152dd812007-12-05 22:21:29 +00003538 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003539 if (HasOptionalParen && Tok.is(tok::r_paren))
3540 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003541 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003542 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003543 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3544 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003545 T.getCloseLocation(),
3546 !HasOptionalParen);
Gabor Greif24032f12007-10-19 15:38:32 +00003547 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003548
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003549void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3550 // MCDecl might be null due to error in method or c-function prototype, etc.
3551 Decl *MCDecl = LM.D;
3552 bool skip = MCDecl &&
3553 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3554 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3555 if (skip)
3556 return;
3557
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003558 // Save the current token position.
3559 SourceLocation OrigLoc = Tok.getLocation();
3560
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003561 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
3562 // Append the current token at the end of the new token stream so that it
3563 // doesn't get lost.
3564 LM.Toks.push_back(Tok);
3565 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
3566
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003567 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003568 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003569
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003570 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3571 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003572 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003573 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003574 parseMethod
3575 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
3576 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003577
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003578 // Tell the actions module that we have entered a method or c-function definition
3579 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003580 if (parseMethod)
3581 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3582 else
3583 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003584 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003585 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003586 else {
3587 if (Tok.is(tok::colon))
3588 ParseConstructorInitializer(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003589 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003590 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00003591
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003592 if (Tok.getLocation() != OrigLoc) {
3593 // Due to parsing error, we either went over the cached tokens or
3594 // there are still cached tokens left. If it's the latter case skip the
3595 // leftover tokens.
3596 // Since this is an uncommon situation that should be avoided, use the
3597 // expensive isBeforeInTranslationUnit call.
3598 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3599 OrigLoc))
3600 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3601 ConsumeAnyToken();
3602 }
3603
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003604 return;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003605}