blob: 0e4574056b0c18b3105ea2cb9de291b63eccab26 [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,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000270 LAngleLoc, EndProtoLoc))
Craig Topper161e4db2014-05-21 06:02:52 +0000271 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000272
John McCall48871652010-08-21 09:40:31 +0000273 Decl *CategoryType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000274 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000275 nameId, nameLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000276 typeParameterList,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000277 categoryId, categoryLoc,
278 ProtocolRefs.data(),
279 ProtocolRefs.size(),
280 ProtocolLocs.data(),
281 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000282
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000283 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000284 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000285
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000286 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000287
288 if (typeParameterList)
289 Actions.popObjCTypeParamList(getCurScope(), typeParameterList);
290
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000291 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000292 }
293 // Parse a class interface.
Craig Topper161e4db2014-05-21 06:02:52 +0000294 IdentifierInfo *superClassId = nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000295 SourceLocation superClassLoc;
Douglas Gregore9d95f12015-07-07 03:57:35 +0000296 DeclSpec superClassDS(AttrFactory);
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000297
Chris Lattner0ef13522007-10-09 17:51:17 +0000298 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000299 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000300
301 // Code completion of superclass names.
302 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000303 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000304 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000305 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000306 }
307
Chris Lattner0ef13522007-10-09 17:51:17 +0000308 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000309 Diag(Tok, diag::err_expected)
310 << tok::identifier; // missing super class name.
Craig Topper161e4db2014-05-21 06:02:52 +0000311 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000312 }
313 superClassId = Tok.getIdentifierInfo();
314 superClassLoc = ConsumeToken();
Douglas Gregore9d95f12015-07-07 03:57:35 +0000315
316 // Type arguments for the superclass or protocol conformances.
317 if (Tok.is(tok::less)) {
318 ParseObjCTypeArgsOrProtocolQualifiers(superClassDS,
319 /*warnOnIncompleteProtocols=*/true);
320 }
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000321 }
Douglas Gregor85f3f952015-07-07 03:57:15 +0000322
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000323 // Next, we need to check for any protocol references.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000324 SmallVector<Decl *, 8> ProtocolRefs;
325 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000326 if (LAngleLoc.isValid()) {
327 // We already parsed the protocols named when we thought we had a
328 // type parameter list. Translate them into actual protocol references.
329 for (const auto &pair : ProtocolIdents) {
330 ProtocolLocs.push_back(pair.second);
331 }
332 Actions.FindProtocolDeclaration(/*WarnOnDeclarations=*/true,
333 /*ForObjCContainer=*/true,
334 &ProtocolIdents[0], ProtocolIdents.size(),
335 ProtocolRefs);
Douglas Gregore9d95f12015-07-07 03:57:35 +0000336 } else if (auto protocols = superClassDS.getProtocolQualifiers()) {
337 // We already parsed the protocols named when we thought we had a
338 // type argument list (for a specialized superclass). Treat them
339 // as actual protocol references.
340 unsigned numProtocols = superClassDS.getNumProtocolQualifiers();
341 ProtocolRefs.append(protocols, protocols + numProtocols);
342 ProtocolLocs.append(superClassDS.getProtocolLocs(),
343 superClassDS.getProtocolLocs() + numProtocols);
344 LAngleLoc = superClassDS.getProtocolLAngleLoc();
345 EndProtoLoc = superClassDS.getLocEnd();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000346 } else if (Tok.is(tok::less) &&
347 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, true,
348 LAngleLoc, EndProtoLoc)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000349 return nullptr;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000350 }
Mike Stump11289f42009-09-09 15:08:12 +0000351
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000352 if (Tok.isNot(tok::less))
353 Actions.ActOnTypedefedProtocols(ProtocolRefs, superClassId, superClassLoc);
354
John McCall48871652010-08-21 09:40:31 +0000355 Decl *ClsType =
Douglas Gregore9d95f12015-07-07 03:57:35 +0000356 Actions.ActOnStartClassInterface(getCurScope(), AtLoc, nameId, nameLoc,
357 typeParameterList, superClassId,
358 superClassLoc,
359 superClassDS.getObjCTypeArgs(),
360 superClassDS.getObjCTypeArgsRange(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000361 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000362 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000363 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000364
Chris Lattner0ef13522007-10-09 17:51:17 +0000365 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000366 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000367
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000368 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000369
370 if (typeParameterList)
371 Actions.popObjCTypeParamList(getCurScope(), typeParameterList);
372
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000373 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000374}
375
Douglas Gregor813a0662015-06-19 18:14:38 +0000376/// Add an attribute for a context-sensitive type nullability to the given
377/// declarator.
378static void addContextSensitiveTypeNullability(Parser &P,
379 Declarator &D,
380 NullabilityKind nullability,
381 SourceLocation nullabilityLoc,
382 bool &addedToDeclSpec) {
383 // Create the attribute.
384 auto getNullabilityAttr = [&]() -> AttributeList * {
Douglas Gregorbec595a2015-06-19 18:27:45 +0000385 return D.getAttributePool().create(
386 P.getNullabilityKeyword(nullability),
387 SourceRange(nullabilityLoc),
388 nullptr, SourceLocation(),
389 nullptr, 0,
390 AttributeList::AS_ContextSensitiveKeyword);
Douglas Gregor813a0662015-06-19 18:14:38 +0000391 };
392
393 if (D.getNumTypeObjects() > 0) {
394 // Add the attribute to the declarator chunk nearest the declarator.
395 auto nullabilityAttr = getNullabilityAttr();
396 DeclaratorChunk &chunk = D.getTypeObject(0);
397 nullabilityAttr->setNext(chunk.getAttrListRef());
398 chunk.getAttrListRef() = nullabilityAttr;
399 } else if (!addedToDeclSpec) {
400 // Otherwise, just put it on the declaration specifiers (if one
401 // isn't there already).
402 D.getMutableDeclSpec().addAttributes(getNullabilityAttr());
403 addedToDeclSpec = true;
404 }
405}
406
Douglas Gregor85f3f952015-07-07 03:57:15 +0000407/// Parse an Objective-C type parameter list, if present, or capture
408/// the locations of the protocol identifiers for a list of protocol
409/// references.
410///
411/// objc-type-parameter-list:
412/// '<' objc-type-parameter (',' objc-type-parameter)* '>'
413///
414/// objc-type-parameter:
415/// identifier objc-type-parameter-bound[opt]
416///
417/// objc-type-parameter-bound:
418/// ':' type-name
419///
420/// \param lAngleLoc The location of the starting '<'.
421///
422/// \param protocolIdents Will capture the list of identifiers, if the
423/// angle brackets contain a list of protocol references rather than a
424/// type parameter list.
425///
426/// \param rAngleLoc The location of the ending '>'.
427ObjCTypeParamList *Parser::parseObjCTypeParamListOrProtocolRefs(
428 SourceLocation &lAngleLoc,
429 SmallVectorImpl<IdentifierLocPair> &protocolIdents,
430 SourceLocation &rAngleLoc,
431 bool mayBeProtocolList) {
432 assert(Tok.is(tok::less) && "Not at the beginning of a type parameter list");
433
434 // Within the type parameter list, don't treat '>' as an operator.
435 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
436
437 // Local function to "flush" the protocol identifiers, turning them into
438 // type parameters.
439 SmallVector<Decl *, 4> typeParams;
440 auto makeProtocolIdentsIntoTypeParameters = [&]() {
441 for (const auto &pair : protocolIdents) {
442 DeclResult typeParam = Actions.actOnObjCTypeParam(getCurScope(),
443 pair.first,
444 pair.second,
445 SourceLocation(),
446 ParsedType());
447 if (typeParam.isUsable())
448 typeParams.push_back(typeParam.get());
449 }
450
451 protocolIdents.clear();
452 mayBeProtocolList = false;
453 };
454
455 bool invalid = false;
456 lAngleLoc = ConsumeToken();
457 do {
458 // Parse the identifier.
459 if (!Tok.is(tok::identifier)) {
460 // Code completion.
461 if (Tok.is(tok::code_completion)) {
462 // FIXME: If these aren't protocol references, we'll need different
463 // completions.
464 Actions.CodeCompleteObjCProtocolReferences(protocolIdents.data(),
465 protocolIdents.size());
466 cutOffParsing();
467
468 // FIXME: Better recovery here?.
469 return nullptr;
470 }
471
472 Diag(Tok, diag::err_objc_expected_type_parameter);
473 invalid = true;
474 break;
475 }
476
477 IdentifierInfo *paramName = Tok.getIdentifierInfo();
478 SourceLocation paramLoc = ConsumeToken();
479
480 // If there is a bound, parse it.
481 SourceLocation colonLoc;
482 TypeResult boundType;
483 if (TryConsumeToken(tok::colon, colonLoc)) {
484 // Once we've seen a bound, we know this is not a list of protocol
485 // references.
486 if (mayBeProtocolList) {
487 // Up until now, we have been queuing up parameters because they
488 // might be protocol references. Turn them into parameters now.
489 makeProtocolIdentsIntoTypeParameters();
490 }
491
492 // type-name
493 boundType = ParseTypeName();
494 if (boundType.isInvalid())
495 invalid = true;
496 } else if (mayBeProtocolList) {
497 // If this could still be a protocol list, just capture the identifier.
498 // We don't want to turn it into a parameter.
499 protocolIdents.push_back(std::make_pair(paramName, paramLoc));
500 continue;
501 }
502
503 // Create the type parameter.
504 DeclResult typeParam = Actions.actOnObjCTypeParam(getCurScope(),
505 paramName,
506 paramLoc,
507 colonLoc,
508 boundType.isUsable()
509 ? boundType.get()
510 : ParsedType());
511 if (typeParam.isUsable())
512 typeParams.push_back(typeParam.get());
513 } while (TryConsumeToken(tok::comma));
514
515 // Parse the '>'.
516 if (invalid) {
517 SkipUntil(tok::greater, tok::at, StopBeforeMatch);
518 if (Tok.is(tok::greater))
519 ConsumeToken();
520 } else if (ParseGreaterThanInTemplateList(rAngleLoc,
521 /*ConsumeLastToken=*/true,
522 /*ObjCGenericList=*/true)) {
523 Diag(lAngleLoc, diag::note_matching) << "'<'";
524 SkipUntil({tok::greater, tok::greaterequal, tok::at, tok::minus,
525 tok::minus, tok::plus, tok::colon, tok::l_paren, tok::l_brace,
526 tok::comma, tok::semi },
527 StopBeforeMatch);
528 if (Tok.is(tok::greater))
529 ConsumeToken();
530 }
531
532 if (mayBeProtocolList) {
533 // A type parameter list must be followed by either a ':' (indicating the
534 // presence of a superclass) or a '(' (indicating that this is a category
535 // or extension). This disambiguates between an objc-type-parameter-list
536 // and a objc-protocol-refs.
537 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_paren)) {
538 // Returning null indicates that we don't have a type parameter list.
539 // The results the caller needs to handle the protocol references are
540 // captured in the reference parameters already.
541 return nullptr;
542 }
543
544 // We have a type parameter list that looks like a list of protocol
545 // references. Turn that parameter list into type parameters.
546 makeProtocolIdentsIntoTypeParameters();
547 }
548
549 // Form the type parameter list.
550 ObjCTypeParamList *list = Actions.actOnObjCTypeParamList(
551 getCurScope(),
552 lAngleLoc,
553 typeParams,
554 rAngleLoc);
555
556 // Clear out the angle locations; they're used by the caller to indicate
557 // whether there are any protocol references.
558 lAngleLoc = SourceLocation();
559 rAngleLoc = SourceLocation();
560 return list;
561}
562
563/// Parse an objc-type-parameter-list.
564ObjCTypeParamList *Parser::parseObjCTypeParamList() {
565 SourceLocation lAngleLoc;
566 SmallVector<IdentifierLocPair, 1> protocolIdents;
567 SourceLocation rAngleLoc;
568 return parseObjCTypeParamListOrProtocolRefs(lAngleLoc, protocolIdents,
569 rAngleLoc,
570 /*mayBeProtocolList=*/false);
571}
572
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000573/// objc-interface-decl-list:
574/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000575/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000576/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000577/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000578/// objc-interface-decl-list declaration
579/// objc-interface-decl-list ';'
580///
Steve Naroff99264b42007-08-22 16:35:03 +0000581/// objc-method-requirement: [OBJC2]
582/// @required
583/// @optional
584///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000585void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
586 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000587 SmallVector<Decl *, 32> allMethods;
588 SmallVector<Decl *, 16> allProperties;
589 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000590 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000591
Ted Kremenekc7c64312010-01-07 01:20:12 +0000592 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000593
Steve Naroff99264b42007-08-22 16:35:03 +0000594 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000595 // If this is a method prototype, parse it.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000596 if (Tok.isOneOf(tok::minus, tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000597 if (Decl *methodPrototype =
598 ParseObjCMethodPrototype(MethodImplKind, false))
599 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000600 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
601 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000602 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
603 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000604 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000605 if (Tok.is(tok::semi))
606 ConsumeToken();
607 }
Steve Naroff99264b42007-08-22 16:35:03 +0000608 continue;
609 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000610 if (Tok.is(tok::l_paren)) {
611 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000612 ParseObjCMethodDecl(Tok.getLocation(),
613 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000614 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000615 continue;
616 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000617 // Ignore excess semicolons.
618 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000619 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000620 continue;
621 }
Mike Stump11289f42009-09-09 15:08:12 +0000622
Chris Lattnerda9fb152008-10-20 06:10:06 +0000623 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000624 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000625 break;
Mike Stump11289f42009-09-09 15:08:12 +0000626
Douglas Gregorf1934162010-01-13 21:24:21 +0000627 // Code completion within an Objective-C interface.
628 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000629 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000630 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000631 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000632 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000633 }
634
Chris Lattner038a3e32008-10-20 05:46:22 +0000635 // If we don't have an @ directive, parse it as a function definition.
636 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000637 // The code below does not consume '}'s because it is afraid of eating the
638 // end of a namespace. Because of the way this code is structured, an
639 // erroneous r_brace would cause an infinite loop if not handled here.
640 if (Tok.is(tok::r_brace))
641 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000642 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000643 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000644 continue;
645 }
Mike Stump11289f42009-09-09 15:08:12 +0000646
Chris Lattner038a3e32008-10-20 05:46:22 +0000647 // Otherwise, we have an @ directive, eat the @.
648 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000649 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000650 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000651 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000652 }
653
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000654 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000655
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000656 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000657 AtEnd.setBegin(AtLoc);
658 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000659 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000660 } else if (DirectiveKind == tok::objc_not_keyword) {
661 Diag(Tok, diag::err_objc_unknown_at);
662 SkipUntil(tok::semi);
663 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000664 }
Mike Stump11289f42009-09-09 15:08:12 +0000665
Chris Lattnerda9fb152008-10-20 06:10:06 +0000666 // Eat the identifier.
667 ConsumeToken();
668
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000669 switch (DirectiveKind) {
670 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000671 // FIXME: If someone forgets an @end on a protocol, this loop will
672 // continue to eat up tons of stuff and spew lots of nonsense errors. It
673 // would probably be better to bail out if we saw an @class or @interface
674 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000675 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000676 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000677 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000678 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000679
680 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000681 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000682 Diag(AtLoc, diag::err_objc_missing_end)
683 << FixItHint::CreateInsertion(AtLoc, "@end\n");
684 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
685 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000686 ConsumeToken();
687 break;
688
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000689 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000690 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000691 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000692 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000693 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000694 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000695 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000696 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000697 break;
Mike Stump11289f42009-09-09 15:08:12 +0000698
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000699 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000700 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000701 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000702
Chris Lattner038a3e32008-10-20 05:46:22 +0000703 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000704 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000705 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000706 if (Tok.is(tok::l_paren)) {
707 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000708 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000709 }
Mike Stump11289f42009-09-09 15:08:12 +0000710
Douglas Gregor813a0662015-06-19 18:14:38 +0000711 bool addedToDeclSpec = false;
Benjamin Kramera39beb92014-09-03 11:06:10 +0000712 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
713 if (FD.D.getIdentifier() == nullptr) {
714 Diag(AtLoc, diag::err_objc_property_requires_field_name)
715 << FD.D.getSourceRange();
716 return;
717 }
718 if (FD.BitfieldSize) {
719 Diag(AtLoc, diag::err_objc_property_bitfield)
720 << FD.D.getSourceRange();
721 return;
722 }
723
Douglas Gregor813a0662015-06-19 18:14:38 +0000724 // Map a nullability property attribute to a context-sensitive keyword
725 // attribute.
726 if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
727 addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
728 OCDS.getNullabilityLoc(),
729 addedToDeclSpec);
730
Benjamin Kramera39beb92014-09-03 11:06:10 +0000731 // Install the property declarator into interfaceDecl.
732 IdentifierInfo *SelName =
733 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
734
735 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
736 IdentifierInfo *SetterName = OCDS.getSetterName();
737 Selector SetterSel;
738 if (SetterName)
739 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
740 else
741 SetterSel = SelectorTable::constructSetterSelector(
742 PP.getIdentifierTable(), PP.getSelectorTable(),
743 FD.D.getIdentifier());
744 bool isOverridingProperty = false;
745 Decl *Property = Actions.ActOnProperty(
746 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
747 &isOverridingProperty, MethodImplKind);
748 if (!isOverridingProperty)
749 allProperties.push_back(Property);
750
751 FD.complete(Property);
752 };
John McCallcfefb6d2009-11-03 02:38:08 +0000753
Chris Lattner038a3e32008-10-20 05:46:22 +0000754 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000755 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000756 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000757
John McCall405988b2011-03-26 01:53:26 +0000758 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000759 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000760 }
Steve Naroff99264b42007-08-22 16:35:03 +0000761 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000762
763 // We break out of the big loop in two cases: when we see @end or when we see
764 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000765 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000766 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000767 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000768 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000769 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000770 } else {
771 Diag(Tok, diag::err_objc_missing_end)
772 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
773 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
774 << (int) Actions.getObjCContainerKind();
775 AtEnd.setBegin(Tok.getLocation());
776 AtEnd.setEnd(Tok.getLocation());
777 }
Mike Stump11289f42009-09-09 15:08:12 +0000778
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000779 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000780 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000781 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000782}
783
Douglas Gregor813a0662015-06-19 18:14:38 +0000784/// Diagnose redundant or conflicting nullability information.
785static void diagnoseRedundantPropertyNullability(Parser &P,
786 ObjCDeclSpec &DS,
787 NullabilityKind nullability,
788 SourceLocation nullabilityLoc){
789 if (DS.getNullability() == nullability) {
790 P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000791 << DiagNullabilityKind(nullability, true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000792 << SourceRange(DS.getNullabilityLoc());
793 return;
794 }
795
796 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000797 << DiagNullabilityKind(nullability, true)
798 << DiagNullabilityKind(DS.getNullability(), true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000799 << SourceRange(DS.getNullabilityLoc());
800}
801
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000802/// Parse property attribute declarations.
803///
804/// property-attr-decl: '(' property-attrlist ')'
805/// property-attrlist:
806/// property-attribute
807/// property-attrlist ',' property-attribute
808/// property-attribute:
809/// getter '=' identifier
810/// setter '=' identifier ':'
811/// readonly
812/// readwrite
813/// assign
814/// retain
815/// copy
816/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000817/// atomic
818/// strong
819/// weak
820/// unsafe_unretained
Douglas Gregor813a0662015-06-19 18:14:38 +0000821/// nonnull
822/// nullable
823/// null_unspecified
Douglas Gregor849ebc22015-06-19 18:14:46 +0000824/// null_resettable
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000825///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000826void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000827 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000828 BalancedDelimiterTracker T(*this, tok::l_paren);
829 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000830
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000831 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000832 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000833 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000834 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000835 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000836 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000837
Chris Lattner76619232008-10-20 07:22:18 +0000838 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000839 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000840 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000841 return;
842 }
Mike Stump11289f42009-09-09 15:08:12 +0000843
Chris Lattner1db33542008-10-20 07:37:22 +0000844 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000845
Chris Lattner68e48682008-11-20 04:42:34 +0000846 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000847 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000848 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000849 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000850 else if (II->isStr("unsafe_unretained"))
851 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000852 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000853 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000854 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000855 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000856 else if (II->isStr("strong"))
857 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000858 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000859 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000860 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000861 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000862 else if (II->isStr("atomic"))
863 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000864 else if (II->isStr("weak"))
865 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000866 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000867 bool IsSetter = II->getNameStart()[0] == 's';
868
Chris Lattner825bca12008-10-20 07:39:53 +0000869 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000870 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
871 diag::err_objc_expected_equal_for_getter;
872
Alp Toker383d2c42014-01-01 03:08:43 +0000873 if (ExpectAndConsume(tok::equal, DiagID)) {
874 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000875 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000876 }
Mike Stump11289f42009-09-09 15:08:12 +0000877
Douglas Gregorc8537c52009-11-19 07:41:15 +0000878 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000879 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000880 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000881 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000882 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000883 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000884 }
885
Anders Carlssonfe15a782010-10-02 17:45:21 +0000886
887 SourceLocation SelLoc;
888 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
889
890 if (!SelIdent) {
891 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
892 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000893 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000894 return;
895 }
Mike Stump11289f42009-09-09 15:08:12 +0000896
Anders Carlssonfe15a782010-10-02 17:45:21 +0000897 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000898 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000899 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000900
Alp Toker383d2c42014-01-01 03:08:43 +0000901 if (ExpectAndConsume(tok::colon,
902 diag::err_expected_colon_after_setter_name)) {
903 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000904 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000905 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000906 } else {
907 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000908 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000909 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000910 } else if (II->isStr("nonnull")) {
911 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
912 diagnoseRedundantPropertyNullability(*this, DS,
913 NullabilityKind::NonNull,
914 Tok.getLocation());
915 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
916 DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
917 } else if (II->isStr("nullable")) {
918 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
919 diagnoseRedundantPropertyNullability(*this, DS,
920 NullabilityKind::Nullable,
921 Tok.getLocation());
922 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
923 DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
924 } else if (II->isStr("null_unspecified")) {
925 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
926 diagnoseRedundantPropertyNullability(*this, DS,
927 NullabilityKind::Unspecified,
928 Tok.getLocation());
929 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
930 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000931 } else if (II->isStr("null_resettable")) {
932 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
933 diagnoseRedundantPropertyNullability(*this, DS,
934 NullabilityKind::Unspecified,
935 Tok.getLocation());
936 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
937 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
938
939 // Also set the null_resettable bit.
940 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
Chris Lattner825bca12008-10-20 07:39:53 +0000941 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000942 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000943 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000944 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000945 }
Mike Stump11289f42009-09-09 15:08:12 +0000946
Chris Lattner1db33542008-10-20 07:37:22 +0000947 if (Tok.isNot(tok::comma))
948 break;
Mike Stump11289f42009-09-09 15:08:12 +0000949
Chris Lattner1db33542008-10-20 07:37:22 +0000950 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000951 }
Mike Stump11289f42009-09-09 15:08:12 +0000952
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000953 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000954}
955
Steve Naroff09bf8152007-09-06 21:24:23 +0000956/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000957/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000958/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000959///
960/// objc-instance-method: '-'
961/// objc-class-method: '+'
962///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000963/// objc-method-attributes: [OBJC2]
964/// __attribute__((deprecated))
965///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000966Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000967 bool MethodDefinition) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000968 assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000969
Mike Stump11289f42009-09-09 15:08:12 +0000970 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000971 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000972 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000973 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000974 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000975 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000976 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000977}
978
979/// objc-selector:
980/// identifier
981/// one of
982/// enum struct union if else while do for switch case default
983/// break continue return goto asm sizeof typeof __alignof
984/// unsigned long const short volatile signed restrict _Complex
985/// in out inout bycopy byref oneway int char float double void _Bool
986///
Chris Lattner4f472a32009-04-11 18:13:45 +0000987IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000988
Chris Lattner5700fab2007-10-07 02:00:24 +0000989 switch (Tok.getKind()) {
990 default:
Craig Topper161e4db2014-05-21 06:02:52 +0000991 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000992 case tok::ampamp:
993 case tok::ampequal:
994 case tok::amp:
995 case tok::pipe:
996 case tok::tilde:
997 case tok::exclaim:
998 case tok::exclaimequal:
999 case tok::pipepipe:
1000 case tok::pipeequal:
1001 case tok::caret:
1002 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +00001003 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +00001004 if (isLetter(ThisTok[0])) {
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001005 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
1006 Tok.setKind(tok::identifier);
1007 SelectorLoc = ConsumeToken();
1008 return II;
1009 }
Craig Topper161e4db2014-05-21 06:02:52 +00001010 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001011 }
1012
Chris Lattner5700fab2007-10-07 02:00:24 +00001013 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001014 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +00001015 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001016 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001017 case tok::kw_break:
1018 case tok::kw_case:
1019 case tok::kw_catch:
1020 case tok::kw_char:
1021 case tok::kw_class:
1022 case tok::kw_const:
1023 case tok::kw_const_cast:
1024 case tok::kw_continue:
1025 case tok::kw_default:
1026 case tok::kw_delete:
1027 case tok::kw_do:
1028 case tok::kw_double:
1029 case tok::kw_dynamic_cast:
1030 case tok::kw_else:
1031 case tok::kw_enum:
1032 case tok::kw_explicit:
1033 case tok::kw_export:
1034 case tok::kw_extern:
1035 case tok::kw_false:
1036 case tok::kw_float:
1037 case tok::kw_for:
1038 case tok::kw_friend:
1039 case tok::kw_goto:
1040 case tok::kw_if:
1041 case tok::kw_inline:
1042 case tok::kw_int:
1043 case tok::kw_long:
1044 case tok::kw_mutable:
1045 case tok::kw_namespace:
1046 case tok::kw_new:
1047 case tok::kw_operator:
1048 case tok::kw_private:
1049 case tok::kw_protected:
1050 case tok::kw_public:
1051 case tok::kw_register:
1052 case tok::kw_reinterpret_cast:
1053 case tok::kw_restrict:
1054 case tok::kw_return:
1055 case tok::kw_short:
1056 case tok::kw_signed:
1057 case tok::kw_sizeof:
1058 case tok::kw_static:
1059 case tok::kw_static_cast:
1060 case tok::kw_struct:
1061 case tok::kw_switch:
1062 case tok::kw_template:
1063 case tok::kw_this:
1064 case tok::kw_throw:
1065 case tok::kw_true:
1066 case tok::kw_try:
1067 case tok::kw_typedef:
1068 case tok::kw_typeid:
1069 case tok::kw_typename:
1070 case tok::kw_typeof:
1071 case tok::kw_union:
1072 case tok::kw_unsigned:
1073 case tok::kw_using:
1074 case tok::kw_virtual:
1075 case tok::kw_void:
1076 case tok::kw_volatile:
1077 case tok::kw_wchar_t:
1078 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +00001079 case tok::kw__Bool:
1080 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001081 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +00001082 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001083 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +00001084 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +00001085 }
Steve Naroff99264b42007-08-22 16:35:03 +00001086}
1087
Fariborz Jahanian83615522008-01-02 22:54:34 +00001088/// objc-for-collection-in: 'in'
1089///
Fariborz Jahanian3622e592008-01-04 23:04:08 +00001090bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001091 // FIXME: May have to do additional look-ahead to only allow for
1092 // valid tokens following an 'in'; such as an identifier, unary operators,
1093 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001094 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +00001095 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +00001096}
1097
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001098/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +00001099/// qualifier list and builds their bitmask representation in the input
1100/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +00001101///
1102/// objc-type-qualifiers:
1103/// objc-type-qualifier
1104/// objc-type-qualifiers objc-type-qualifier
1105///
Douglas Gregor813a0662015-06-19 18:14:38 +00001106/// objc-type-qualifier:
1107/// 'in'
1108/// 'out'
1109/// 'inout'
1110/// 'oneway'
1111/// 'bycopy'
1112/// 'byref'
1113/// 'nonnull'
1114/// 'nullable'
1115/// 'null_unspecified'
1116///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001117void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001118 Declarator::TheContext Context) {
1119 assert(Context == Declarator::ObjCParameterContext ||
1120 Context == Declarator::ObjCResultContext);
1121
Chris Lattner61511e12007-12-12 06:56:32 +00001122 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +00001123 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +00001124 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +00001125 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001126 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +00001127 }
1128
Chris Lattner5e530bc2007-12-27 19:57:00 +00001129 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +00001130 return;
Mike Stump11289f42009-09-09 15:08:12 +00001131
Chris Lattner61511e12007-12-12 06:56:32 +00001132 const IdentifierInfo *II = Tok.getIdentifierInfo();
1133 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001134 if (II != ObjCTypeQuals[i] ||
1135 NextToken().is(tok::less) ||
1136 NextToken().is(tok::coloncolon))
Chris Lattner61511e12007-12-12 06:56:32 +00001137 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001138
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001139 ObjCDeclSpec::ObjCDeclQualifier Qual;
Douglas Gregor813a0662015-06-19 18:14:38 +00001140 NullabilityKind Nullability;
Chris Lattner61511e12007-12-12 06:56:32 +00001141 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +00001142 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001143 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
1144 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
1145 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
1146 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
1147 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
1148 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Douglas Gregor813a0662015-06-19 18:14:38 +00001149
1150 case objc_nonnull:
1151 Qual = ObjCDeclSpec::DQ_CSNullability;
1152 Nullability = NullabilityKind::NonNull;
1153 break;
1154
1155 case objc_nullable:
1156 Qual = ObjCDeclSpec::DQ_CSNullability;
1157 Nullability = NullabilityKind::Nullable;
1158 break;
1159
1160 case objc_null_unspecified:
1161 Qual = ObjCDeclSpec::DQ_CSNullability;
1162 Nullability = NullabilityKind::Unspecified;
1163 break;
Chris Lattner61511e12007-12-12 06:56:32 +00001164 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001165
1166 // FIXME: Diagnose redundant specifiers.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001167 DS.setObjCDeclQualifier(Qual);
Douglas Gregor813a0662015-06-19 18:14:38 +00001168 if (Qual == ObjCDeclSpec::DQ_CSNullability)
1169 DS.setNullability(Tok.getLocation(), Nullability);
1170
Chris Lattner61511e12007-12-12 06:56:32 +00001171 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001172 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +00001173 break;
1174 }
Mike Stump11289f42009-09-09 15:08:12 +00001175
Chris Lattner61511e12007-12-12 06:56:32 +00001176 // If this wasn't a recognized qualifier, bail out.
1177 if (II) return;
1178 }
1179}
1180
John McCalla55902b2011-10-01 09:56:14 +00001181/// Take all the decl attributes out of the given list and add
1182/// them to the given attribute set.
1183static void takeDeclAttributes(ParsedAttributes &attrs,
1184 AttributeList *list) {
1185 while (list) {
1186 AttributeList *cur = list;
1187 list = cur->getNext();
1188
1189 if (!cur->isUsedAsTypeAttr()) {
1190 // Clear out the next pointer. We're really completely
1191 // destroying the internal invariants of the declarator here,
1192 // but it doesn't matter because we're done with it.
Craig Topper161e4db2014-05-21 06:02:52 +00001193 cur->setNext(nullptr);
John McCalla55902b2011-10-01 09:56:14 +00001194 attrs.add(cur);
1195 }
1196 }
1197}
1198
1199/// takeDeclAttributes - Take all the decl attributes from the given
1200/// declarator and add them to the given list.
1201static void takeDeclAttributes(ParsedAttributes &attrs,
1202 Declarator &D) {
1203 // First, take ownership of all attributes.
1204 attrs.getPool().takeAllFrom(D.getAttributePool());
1205 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
1206
1207 // Now actually move the attributes over.
1208 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
1209 takeDeclAttributes(attrs, D.getAttributes());
1210 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
1211 takeDeclAttributes(attrs,
1212 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
1213}
1214
Chris Lattner61511e12007-12-12 06:56:32 +00001215/// objc-type-name:
1216/// '(' objc-type-qualifiers[opt] type-name ')'
1217/// '(' objc-type-qualifiers[opt] ')'
1218///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001219ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001220 Declarator::TheContext context,
1221 ParsedAttributes *paramAttrs) {
1222 assert(context == Declarator::ObjCParameterContext ||
1223 context == Declarator::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +00001224 assert((paramAttrs != nullptr) ==
1225 (context == Declarator::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +00001226
Chris Lattner0ef13522007-10-09 17:51:17 +00001227 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +00001228
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001229 BalancedDelimiterTracker T(*this, tok::l_paren);
1230 T.consumeOpen();
1231
Chris Lattner2ebb1782008-08-23 01:48:03 +00001232 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001233 ObjCDeclContextSwitch ObjCDC(*this);
1234
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001235 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001236 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +00001237
John McCallba7bf592010-08-24 05:47:05 +00001238 ParsedType Ty;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001239 if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
John McCalla55902b2011-10-01 09:56:14 +00001240 // Parse an abstract declarator.
1241 DeclSpec declSpec(AttrFactory);
1242 declSpec.setObjCQualifiers(&DS);
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001243 DeclSpecContext dsContext = DSC_normal;
1244 if (context == Declarator::ObjCResultContext)
1245 dsContext = DSC_objc_method_result;
1246 ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
Fariborz Jahanianb6499eb2012-05-29 21:52:45 +00001247 declSpec.SetRangeEnd(Tok.getLocation());
John McCalla55902b2011-10-01 09:56:14 +00001248 Declarator declarator(declSpec, context);
1249 ParseDeclarator(declarator);
1250
1251 // If that's not invalid, extract a type.
1252 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001253 // Map a nullability specifier to a context-sensitive keyword attribute.
1254 bool addedToDeclSpec = false;
1255 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1256 addContextSensitiveTypeNullability(*this, declarator,
1257 DS.getNullability(),
1258 DS.getNullabilityLoc(),
1259 addedToDeclSpec);
1260
John McCalla55902b2011-10-01 09:56:14 +00001261 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1262 if (!type.isInvalid())
1263 Ty = type.get();
1264
1265 // If we're parsing a parameter, steal all the decl attributes
1266 // and add them to the decl spec.
1267 if (context == Declarator::ObjCParameterContext)
1268 takeDeclAttributes(*paramAttrs, declarator);
1269 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001270 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001271
Steve Naroff90255b42008-10-21 14:15:04 +00001272 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001273 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001274 else if (Tok.getLocation() == TypeStartLoc) {
1275 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001276 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001277 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001278 } else {
1279 // Otherwise, we found *something*, but didn't get a ')' in the right
1280 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001281 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001282 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001283 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001284}
1285
1286/// objc-method-decl:
1287/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001288/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001289/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001290/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001291///
1292/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001293/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001294/// objc-keyword-selector objc-keyword-decl
1295///
1296/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001297/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1298/// objc-selector ':' objc-keyword-attributes[opt] identifier
1299/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1300/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001301///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001302/// objc-parmlist:
1303/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001304///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001305/// objc-parms:
1306/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001307///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001308/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001309/// , ...
1310///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001311/// objc-keyword-attributes: [OBJC2]
1312/// __attribute__((unused))
1313///
John McCall48871652010-08-21 09:40:31 +00001314Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001315 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001316 tok::ObjCKeywordKind MethodImplKind,
1317 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001318 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001319
Douglas Gregor636a61e2010-04-07 00:21:17 +00001320 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001321 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001322 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001323 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001324 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001325 }
1326
Chris Lattner2ebb1782008-08-23 01:48:03 +00001327 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001328 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001329 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001330 if (Tok.is(tok::l_paren))
Craig Topper161e4db2014-05-21 06:02:52 +00001331 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext,
1332 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001333
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001334 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001335 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001336 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001337 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001338
Douglas Gregor636a61e2010-04-07 00:21:17 +00001339 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001340 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001341 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001342 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001343 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001344 }
1345
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001346 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001347 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001348 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001349
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001350 // An unnamed colon is valid.
1351 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001352 Diag(Tok, diag::err_expected_selector_for_method)
1353 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001354 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001355 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001356 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001357 }
Mike Stump11289f42009-09-09 15:08:12 +00001358
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001359 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001360 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001361 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001362 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001363 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001364
Chris Lattner5700fab2007-10-07 02:00:24 +00001365 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001366 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001367 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001368 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001369 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001370 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001371 methodAttrs.getList(), MethodImplKind,
1372 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001373 PD.complete(Result);
1374 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001375 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001376
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001377 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001378 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001379 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001380 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1381 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001382
1383 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001384 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001385 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001386 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001387
Chris Lattner5700fab2007-10-07 02:00:24 +00001388 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001389 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001390 break;
Mike Stump11289f42009-09-09 15:08:12 +00001391
John McCallba7bf592010-08-24 05:47:05 +00001392 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001393 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001394 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1395 Declarator::ObjCParameterContext,
1396 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001397
Chris Lattner5700fab2007-10-07 02:00:24 +00001398 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001399 // Regardless, collect all the attributes we've parsed so far.
Craig Topper161e4db2014-05-21 06:02:52 +00001400 ArgInfo.ArgAttrs = nullptr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001401 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001402 MaybeParseGNUAttributes(paramAttrs);
1403 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001404 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001405
Douglas Gregor45879692010-07-08 23:37:41 +00001406 // Code completion for the next piece of the selector.
1407 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001408 KeyIdents.push_back(SelIdent);
1409 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1410 mType == tok::minus,
1411 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001412 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001413 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001414 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001415 }
1416
Chris Lattner0ef13522007-10-09 17:51:17 +00001417 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001418 Diag(Tok, diag::err_expected)
1419 << tok::identifier; // missing argument name.
Chris Lattner5700fab2007-10-07 02:00:24 +00001420 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001421 }
Mike Stump11289f42009-09-09 15:08:12 +00001422
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001423 ArgInfo.Name = Tok.getIdentifierInfo();
1424 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001425 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001426
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001427 ArgInfos.push_back(ArgInfo);
1428 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001429 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001430
John McCall084e83d2011-03-24 11:26:52 +00001431 // Make sure the attributes persist.
1432 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1433
Douglas Gregor95887f92010-07-08 23:20:03 +00001434 // Code completion for the next piece of the selector.
1435 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001436 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1437 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001438 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001439 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001440 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001441 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001442 }
1443
Chris Lattner5700fab2007-10-07 02:00:24 +00001444 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001445 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001446 if (!SelIdent && Tok.isNot(tok::colon))
1447 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001448 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001449 SourceLocation ColonLoc = Tok.getLocation();
1450 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001451 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1452 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1453 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001454 }
1455 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001456 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001457 }
Mike Stump11289f42009-09-09 15:08:12 +00001458
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001459 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001460 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001461 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001462 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001463 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001464 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001465 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001466 ConsumeToken();
1467 break;
1468 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001469 if (!cStyleParamWarned) {
1470 Diag(Tok, diag::warn_cstyle_param);
1471 cStyleParamWarned = true;
1472 }
John McCall084e83d2011-03-24 11:26:52 +00001473 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001474 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001475 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001476 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1477 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001478 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001479 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001480 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1481 ParmDecl.getIdentifierLoc(),
1482 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001483 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001484 }
Mike Stump11289f42009-09-09 15:08:12 +00001485
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001486 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001487 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001488 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001489 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001490
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001491 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001492 return nullptr;
1493
Chris Lattner5700fab2007-10-07 02:00:24 +00001494 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1495 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001496 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001497 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001498 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001499 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001500 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001501 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001502 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001503
John McCall28a6aea2009-11-04 02:18:39 +00001504 PD.complete(Result);
1505 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001506}
1507
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001508/// objc-protocol-refs:
1509/// '<' identifier-list '>'
1510///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001511bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001512ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1513 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001514 bool WarnOnDeclarations, bool ForObjCContainer,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001515 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001516 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001517
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001518 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001519
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001520 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001521
Chris Lattner3bbae002008-07-26 04:03:38 +00001522 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001523 if (Tok.is(tok::code_completion)) {
1524 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1525 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001526 cutOffParsing();
1527 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001528 }
1529
Chris Lattner3bbae002008-07-26 04:03:38 +00001530 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001531 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001532 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001533 return true;
1534 }
1535 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1536 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001537 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001538 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001539
Alp Toker383d2c42014-01-01 03:08:43 +00001540 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001541 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001542 }
Mike Stump11289f42009-09-09 15:08:12 +00001543
Chris Lattner3bbae002008-07-26 04:03:38 +00001544 // Consume the '>'.
Douglas Gregor85f3f952015-07-07 03:57:15 +00001545 if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true,
1546 /*ObjCGenericList=*/false))
Chris Lattner3bbae002008-07-26 04:03:38 +00001547 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001548
Chris Lattner3bbae002008-07-26 04:03:38 +00001549 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001550 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Chris Lattner3bbae002008-07-26 04:03:38 +00001551 &ProtocolIdents[0], ProtocolIdents.size(),
1552 Protocols);
1553 return false;
1554}
1555
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001556/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1557/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001558bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001559 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001560 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001561 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001562 SmallVector<Decl *, 8> ProtocolDecl;
1563 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001564 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001565 false, LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001566 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1567 ProtocolLocs.data(), LAngleLoc);
1568 if (EndProtoLoc.isValid())
1569 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001570 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001571}
1572
Douglas Gregore9d95f12015-07-07 03:57:35 +00001573/// Parse Objective-C type arguments or protocol qualifiers.
1574///
1575/// objc-type-arguments:
1576/// '<' type-name (',' type-name)* '>'
1577///
1578void Parser::ParseObjCTypeArgsOrProtocolQualifiers(
1579 DeclSpec &DS,
1580 bool warnOnIncompleteProtocols) {
1581 assert(Tok.is(tok::less) && "Not at the start of type args or protocols");
1582 SourceLocation lAngleLoc = ConsumeToken();
1583
1584 // Whether all of the elements we've parsed thus far are single
1585 // identifiers, which might be types or might be protocols.
1586 bool allSingleIdentifiers = true;
1587 SmallVector<IdentifierInfo *, 4> identifiers;
1588 SmallVector<SourceLocation, 4> identifierLocs;
1589
1590 // Parse a list of comma-separated identifiers, bailing out if we
1591 // see something different.
1592 do {
1593 // Parse a single identifier.
1594 if (Tok.is(tok::identifier) &&
1595 (NextToken().is(tok::comma) ||
1596 NextToken().is(tok::greater) ||
1597 NextToken().is(tok::greatergreater))) {
1598 identifiers.push_back(Tok.getIdentifierInfo());
1599 identifierLocs.push_back(ConsumeToken());
1600 continue;
1601 }
1602
1603 if (Tok.is(tok::code_completion)) {
1604 // FIXME: Also include types here.
1605 SmallVector<IdentifierLocPair, 4> identifierLocPairs;
1606 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1607 identifierLocPairs.push_back(IdentifierLocPair(identifiers[i],
1608 identifierLocs[i]));
1609 }
1610
1611 Actions.CodeCompleteObjCProtocolReferences(identifierLocPairs.data(),
1612 identifierLocPairs.size());
1613 cutOffParsing();
1614 return;
1615 }
1616
1617 allSingleIdentifiers = false;
1618 break;
1619 } while (TryConsumeToken(tok::comma));
1620
1621 // If we parsed an identifier list, semantic analysis sorts out
1622 // whether it refers to protocols or to type arguments.
1623 if (allSingleIdentifiers) {
1624 // Parse the closing '>'.
1625 SourceLocation rAngleLoc;
1626 (void)ParseGreaterThanInTemplateList(rAngleLoc, /*ConsumeLastToken=*/true,
1627 /*ObjCGenericList=*/true);
1628
1629 // Let Sema figure out what we parsed.
1630 Actions.actOnObjCTypeArgsOrProtocolQualifiers(getCurScope(),
1631 DS,
1632 lAngleLoc,
1633 identifiers,
1634 identifierLocs,
1635 rAngleLoc,
1636 warnOnIncompleteProtocols);
1637 return;
1638 }
1639
1640 // We syntactically matched a type argument, so commit to parsing
1641 // type arguments.
1642 SmallVector<ParsedType, 4> typeArgs;
1643
1644 // Convert the identifiers into type arguments.
1645 bool invalid = false;
1646 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1647 ParsedType typeArg
1648 = Actions.getTypeName(*identifiers[i], identifierLocs[i], getCurScope());
1649 if (typeArg) {
1650 typeArgs.push_back(typeArg);
1651 } else {
1652 invalid = true;
1653 }
1654 }
1655
1656 // Continue parsing type-names.
1657 do {
1658 TypeResult typeArg = ParseTypeName();
1659 if (typeArg.isUsable()) {
1660 typeArgs.push_back(typeArg.get());
1661 } else {
1662 invalid = true;
1663 }
1664 } while (TryConsumeToken(tok::comma));
1665
1666 // Parse the closing '>'.
1667 SourceLocation rAngleLoc;
1668 (void)ParseGreaterThanInTemplateList(rAngleLoc, /*ConsumeLastToken=*/true,
1669 /*ObjCGenericList=*/true);
1670
1671 if (invalid)
1672 return;
1673
1674 // Update the DeclSpec appropriately.
1675 DS.setObjCTypeArgs(lAngleLoc, typeArgs, rAngleLoc);
1676}
1677
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001678void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1679 BalancedDelimiterTracker &T,
1680 SmallVectorImpl<Decl *> &AllIvarDecls,
1681 bool RBraceMissing) {
1682 if (!RBraceMissing)
1683 T.consumeClose();
1684
1685 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1686 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1687 Actions.ActOnObjCContainerFinishDefinition();
1688 // Call ActOnFields() even if we don't have any decls. This is useful
1689 // for code rewriting tools that need to be aware of the empty list.
1690 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1691 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001692 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001693}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001694
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001695/// objc-class-instance-variables:
1696/// '{' objc-instance-variable-decl-list[opt] '}'
1697///
1698/// objc-instance-variable-decl-list:
1699/// objc-visibility-spec
1700/// objc-instance-variable-decl ';'
1701/// ';'
1702/// objc-instance-variable-decl-list objc-visibility-spec
1703/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1704/// objc-instance-variable-decl-list ';'
1705///
1706/// objc-visibility-spec:
1707/// @private
1708/// @protected
1709/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001710/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001711///
1712/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001713/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001714///
John McCall48871652010-08-21 09:40:31 +00001715void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001716 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001717 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001718 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001719 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001720
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001721 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001722 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001723
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001724 BalancedDelimiterTracker T(*this, tok::l_brace);
1725 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001726 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001727 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001728 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001729
Steve Naroff00433d32007-08-21 21:17:12 +00001730 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001731 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001732 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001733 continue;
1734 }
Mike Stump11289f42009-09-09 15:08:12 +00001735
Steve Naroff00433d32007-08-21 21:17:12 +00001736 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001737 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001738 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001739 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001740 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001741 }
1742
Steve Naroff7c348172007-08-23 18:16:40 +00001743 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001744 case tok::objc_private:
1745 case tok::objc_public:
1746 case tok::objc_protected:
1747 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001748 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001749 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001750 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001751
1752 case tok::objc_end:
1753 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001754 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1755 Tok.setKind(tok::at);
1756 Tok.setLength(1);
1757 PP.EnterToken(Tok);
1758 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1759 T, AllIvarDecls, true);
1760 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001761
1762 default:
1763 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1764 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001765 }
1766 }
Mike Stump11289f42009-09-09 15:08:12 +00001767
Douglas Gregor48d46252010-01-13 21:54:15 +00001768 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001769 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001770 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001771 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001772 }
John McCallcfefb6d2009-11-03 02:38:08 +00001773
Benjamin Kramera39beb92014-09-03 11:06:10 +00001774 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1775 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1776 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001777 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001778 Decl *Field = Actions.ActOnIvar(
1779 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1780 FD.BitfieldSize, visibility);
1781 Actions.ActOnObjCContainerFinishDefinition();
1782 if (Field)
1783 AllIvarDecls.push_back(Field);
1784 FD.complete(Field);
1785 };
John McCallcfefb6d2009-11-03 02:38:08 +00001786
Chris Lattnera12405b2008-04-10 06:46:29 +00001787 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001788 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001789 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001790
Chris Lattner0ef13522007-10-09 17:51:17 +00001791 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001792 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001793 } else {
1794 Diag(Tok, diag::err_expected_semi_decl_list);
1795 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001796 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001797 }
1798 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001799 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1800 T, AllIvarDecls, false);
Steve Naroff00433d32007-08-21 21:17:12 +00001801 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001802}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001803
1804/// objc-protocol-declaration:
1805/// objc-protocol-definition
1806/// objc-protocol-forward-reference
1807///
1808/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00001809/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00001810/// objc-protocol-refs[opt]
1811/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00001812/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001813///
1814/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00001815/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001816///
James Dennett1355bd12012-06-11 06:19:40 +00001817/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001818/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001819/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001820Parser::DeclGroupPtrTy
1821Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1822 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001823 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001824 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1825 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001826
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001827 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001828 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001829 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00001830 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001831 }
1832
Nico Weber69a79142013-04-04 00:15:10 +00001833 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00001834
Chris Lattner0ef13522007-10-09 17:51:17 +00001835 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001836 Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00001837 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001838 }
1839 // Save the protocol name, then consume it.
1840 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1841 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001842
Alp Toker383d2c42014-01-01 03:08:43 +00001843 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001844 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001845 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001846 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001847 }
Mike Stump11289f42009-09-09 15:08:12 +00001848
Erik Verbruggenf9887852011-12-08 09:58:43 +00001849 CheckNestedObjCContexts(AtLoc);
1850
Chris Lattner0ef13522007-10-09 17:51:17 +00001851 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001852 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001853 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1854
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001855 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001856 while (1) {
1857 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001858 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001859 Diag(Tok, diag::err_expected) << tok::identifier;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001860 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00001861 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001862 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001863 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1864 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001865 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001866
Chris Lattner0ef13522007-10-09 17:51:17 +00001867 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001868 break;
1869 }
1870 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00001871 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00001872 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001873
Steve Naroff93eb5f12007-10-10 17:32:04 +00001874 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001875 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001876 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001877 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001878 }
Mike Stump11289f42009-09-09 15:08:12 +00001879
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001880 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001881 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001882
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001883 SmallVector<Decl *, 8> ProtocolRefs;
1884 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001885 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001886 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001887 LAngleLoc, EndProtoLoc))
Douglas Gregorf6102672012-01-01 21:23:57 +00001888 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001889
John McCall48871652010-08-21 09:40:31 +00001890 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001891 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001892 ProtocolRefs.data(),
1893 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001894 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001895 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001896
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001897 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00001898 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001899}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001900
1901/// objc-implementation:
1902/// objc-class-implementation-prologue
1903/// objc-category-implementation-prologue
1904///
1905/// objc-class-implementation-prologue:
1906/// @implementation identifier objc-superclass[opt]
1907/// objc-class-instance-variables[opt]
1908///
1909/// objc-category-implementation-prologue:
1910/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001911Parser::DeclGroupPtrTy
1912Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001913 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1914 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001915 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001916 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001917
Douglas Gregor49c22a72009-11-18 16:26:39 +00001918 // Code completion after '@implementation'.
1919 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001920 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001921 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001922 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001923 }
1924
Nico Weber69a79142013-04-04 00:15:10 +00001925 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00001926
Chris Lattner0ef13522007-10-09 17:51:17 +00001927 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001928 Diag(Tok, diag::err_expected)
1929 << tok::identifier; // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001930 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001931 }
1932 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001933 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001934 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00001935 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001936
Douglas Gregor85f3f952015-07-07 03:57:15 +00001937 // Neither a type parameter list nor a list of protocol references is
1938 // permitted here. Parse and diagnose them.
1939 if (Tok.is(tok::less)) {
1940 SourceLocation lAngleLoc, rAngleLoc;
1941 SmallVector<IdentifierLocPair, 8> protocolIdents;
1942 SourceLocation diagLoc = Tok.getLocation();
1943 if (parseObjCTypeParamListOrProtocolRefs(lAngleLoc, protocolIdents,
1944 rAngleLoc)) {
1945 Diag(diagLoc, diag::err_objc_parameterized_implementation)
1946 << SourceRange(diagLoc, PrevTokLocation);
1947 } else if (lAngleLoc.isValid()) {
1948 Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
1949 << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
1950 }
1951 }
1952
Mike Stump11289f42009-09-09 15:08:12 +00001953 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001954 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001955 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001956 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001957 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001958
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001959 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001960 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001961 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001962 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001963 }
1964
Chris Lattner0ef13522007-10-09 17:51:17 +00001965 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001966 categoryId = Tok.getIdentifierInfo();
1967 categoryLoc = ConsumeToken();
1968 } else {
Alp Tokerec543272013-12-24 09:48:30 +00001969 Diag(Tok, diag::err_expected)
1970 << tok::identifier; // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001971 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001972 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001973 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001974 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001975 SkipUntil(tok::r_paren); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001976 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001977 }
1978 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00001979 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1980 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1981 AttributeFactory attr;
1982 DeclSpec DS(attr);
1983 (void)ParseObjCProtocolQualifiers(DS);
1984 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001985 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001986 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001987 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001988
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001989 } else {
1990 // We have a class implementation
1991 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001992 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00001993 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001994 // We have a super class
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001995 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001996 Diag(Tok, diag::err_expected)
1997 << tok::identifier; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001998 return DeclGroupPtrTy();
1999 }
2000 superClassId = Tok.getIdentifierInfo();
2001 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002002 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002003 ObjCImpDecl = Actions.ActOnStartClassImplementation(
2004 AtLoc, nameId, nameLoc,
2005 superClassId, superClassLoc);
2006
2007 if (Tok.is(tok::l_brace)) // we have ivars
2008 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002009 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2010 Diag(Tok, diag::err_unexpected_protocol_qualifier);
2011 // try to recover.
2012 AttributeFactory attr;
2013 DeclSpec DS(attr);
2014 (void)ParseObjCProtocolQualifiers(DS);
2015 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002016 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002017 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002018
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002019 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002020
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002021 {
2022 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00002023 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002024 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002025 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002026 MaybeParseMicrosoftAttributes(attrs);
2027 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
2028 DeclGroupRef DG = DGP.get();
2029 DeclsInGroup.append(DG.begin(), DG.end());
2030 }
2031 }
2032 }
2033
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00002034 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002035}
Steve Naroff33a1e802007-10-29 21:38:07 +00002036
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002037Parser::DeclGroupPtrTy
2038Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002039 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
2040 "ParseObjCAtEndDeclaration(): Expected @end");
2041 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002042 if (CurParsedObjCImpl)
2043 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002044 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00002045 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002046 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002047 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002048}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002049
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002050Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
2051 if (!Finished) {
2052 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00002053 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002054 P.Diag(P.Tok, diag::err_objc_missing_end)
2055 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
2056 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
2057 << Sema::OCK_Implementation;
2058 }
2059 }
Craig Topper161e4db2014-05-21 06:02:52 +00002060 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002061 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002062}
2063
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002064void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
2065 assert(!Finished);
2066 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
2067 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002068 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2069 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002070
2071 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
2072
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002073 if (HasCFunction)
2074 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
2075 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2076 false/*c-functions*/);
2077
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002078 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002079 for (LateParsedObjCMethodContainer::iterator
2080 I = LateParsedObjCMethods.begin(),
2081 E = LateParsedObjCMethods.end(); I != E; ++I)
2082 delete *I;
2083 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002084
2085 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002086}
2087
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002088/// compatibility-alias-decl:
2089/// @compatibility_alias alias-name class-name ';'
2090///
John McCall48871652010-08-21 09:40:31 +00002091Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002092 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
2093 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
2094 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00002095 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002096 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002097 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002098 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002099 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
2100 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00002101 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002102 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002103 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002104 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002105 IdentifierInfo *classId = Tok.getIdentifierInfo();
2106 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00002107 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00002108 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
2109 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002110}
2111
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002112/// property-synthesis:
2113/// @synthesize property-ivar-list ';'
2114///
2115/// property-ivar-list:
2116/// property-ivar
2117/// property-ivar-list ',' property-ivar
2118///
2119/// property-ivar:
2120/// identifier
2121/// identifier '=' identifier
2122///
John McCall48871652010-08-21 09:40:31 +00002123Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002124 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00002125 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002126 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00002127
Douglas Gregor88e72a02009-11-18 19:45:45 +00002128 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00002129 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002130 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002131 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002132 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002133 }
2134
Douglas Gregor88e72a02009-11-18 19:45:45 +00002135 if (Tok.isNot(tok::identifier)) {
2136 Diag(Tok, diag::err_synthesized_property_name);
2137 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002138 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00002139 }
Craig Topper161e4db2014-05-21 06:02:52 +00002140
2141 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002142 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2143 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002144 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00002145 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002146 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00002147 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002148 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002149 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002150 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002151 }
2152
Chris Lattner0ef13522007-10-09 17:51:17 +00002153 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002154 Diag(Tok, diag::err_expected) << tok::identifier;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002155 break;
2156 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002157 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002158 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002159 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002160 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002161 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002162 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002163 break;
2164 ConsumeToken(); // consume ','
2165 }
Alp Toker383d2c42014-01-01 03:08:43 +00002166 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00002167 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002168}
2169
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002170/// property-dynamic:
2171/// @dynamic property-list
2172///
2173/// property-list:
2174/// identifier
2175/// property-list ',' identifier
2176///
John McCall48871652010-08-21 09:40:31 +00002177Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002178 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2179 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002180 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002181 while (true) {
2182 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002183 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002184 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002185 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002186 }
2187
2188 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002189 Diag(Tok, diag::err_expected) << tok::identifier;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002190 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002191 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002192 }
2193
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002194 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2195 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002196 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Craig Topper161e4db2014-05-21 06:02:52 +00002197 propertyId, nullptr, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002198
Chris Lattner0ef13522007-10-09 17:51:17 +00002199 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002200 break;
2201 ConsumeToken(); // consume ','
2202 }
Alp Toker383d2c42014-01-01 03:08:43 +00002203 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00002204 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002205}
Mike Stump11289f42009-09-09 15:08:12 +00002206
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002207/// objc-throw-statement:
2208/// throw expression[opt];
2209///
John McCalldadc5752010-08-24 06:29:42 +00002210StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2211 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002212 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00002213 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00002214 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002215 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002216 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002217 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002218 }
2219 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00002220 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00002221 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002222 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002223}
2224
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002225/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002226/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002227///
John McCalldadc5752010-08-24 06:29:42 +00002228StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002229Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002230 ConsumeToken(); // consume synchronized
2231 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002232 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002233 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002234 }
John McCalld9bb7432011-07-27 21:50:02 +00002235
2236 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002237 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00002238 ExprResult operand(ParseExpression());
2239
2240 if (Tok.is(tok::r_paren)) {
2241 ConsumeParen(); // ')'
2242 } else {
2243 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002244 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00002245
2246 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002247 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002248 }
John McCalld9bb7432011-07-27 21:50:02 +00002249
2250 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002251 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00002252 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002253 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002254 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002255 }
Steve Naroffd9c26072008-06-04 20:36:13 +00002256
John McCalld9bb7432011-07-27 21:50:02 +00002257 // Check the @synchronized operand now.
2258 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002259 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002260
John McCalld9bb7432011-07-27 21:50:02 +00002261 // Parse the compound statement within a new scope.
2262 ParseScope bodyScope(this, Scope::DeclScope);
2263 StmtResult body(ParseCompoundStatementBody());
2264 bodyScope.Exit();
2265
2266 // If there was a semantic or parse error earlier with the
2267 // operand, fail now.
2268 if (operand.isInvalid())
2269 return StmtError();
2270
2271 if (body.isInvalid())
2272 body = Actions.ActOnNullStmt(Tok.getLocation());
2273
2274 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002275}
2276
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002277/// objc-try-catch-statement:
2278/// @try compound-statement objc-catch-list[opt]
2279/// @try compound-statement objc-catch-list[opt] @finally compound-statement
2280///
2281/// objc-catch-list:
2282/// @catch ( parameter-declaration ) compound-statement
2283/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2284/// catch-parameter-declaration:
2285/// parameter-declaration
2286/// '...' [OBJC2]
2287///
John McCalldadc5752010-08-24 06:29:42 +00002288StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002289 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002290
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002291 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00002292 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002293 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002294 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002295 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00002296 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00002297 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002298 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00002299 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002300 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002301 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002302 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002303
Chris Lattner0ef13522007-10-09 17:51:17 +00002304 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002305 // At this point, we need to lookahead to determine if this @ is the start
2306 // of an @catch or @finally. We don't want to consume the @ token if this
2307 // is an @try or @encode or something else.
2308 Token AfterAt = GetLookAheadToken(1);
2309 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2310 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2311 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002312
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002313 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002314 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002315 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002316 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002317 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002318 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00002319 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002320 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002321 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002322 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00002323 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002324 ParseDeclarator(ParmDecl);
2325
Douglas Gregore11ee112010-04-23 23:01:43 +00002326 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002327 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002328 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002329 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002330 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002331
Steve Naroff65a00892009-04-07 22:56:58 +00002332 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002333
Steve Naroff65a00892009-04-07 22:56:58 +00002334 if (Tok.is(tok::r_paren))
2335 RParenLoc = ConsumeParen();
2336 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002337 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002338
John McCalldadc5752010-08-24 06:29:42 +00002339 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002340 if (Tok.is(tok::l_brace))
2341 CatchBody = ParseCompoundStatementBody();
2342 else
Alp Tokerec543272013-12-24 09:48:30 +00002343 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002344 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002345 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00002346
John McCalldadc5752010-08-24 06:29:42 +00002347 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00002348 RParenLoc,
2349 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002350 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002351 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002352 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002353
Steve Naroffe6016792008-02-05 21:27:35 +00002354 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002355 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2356 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002357 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002358 }
2359 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002360 } else {
2361 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002362 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002363 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002364
John McCalldadc5752010-08-24 06:29:42 +00002365 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002366 if (Tok.is(tok::l_brace))
2367 FinallyBody = ParseCompoundStatementBody();
2368 else
Alp Tokerec543272013-12-24 09:48:30 +00002369 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002370 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002371 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002372 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002373 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002374 catch_or_finally_seen = true;
2375 break;
2376 }
2377 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002378 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002379 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002380 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002381 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002382
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002383 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002384 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002385 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002386}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002387
John McCall31168b02011-06-15 23:02:42 +00002388/// objc-autoreleasepool-statement:
2389/// @autoreleasepool compound-statement
2390///
2391StmtResult
2392Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2393 ConsumeToken(); // consume autoreleasepool
2394 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002395 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002396 return StmtError();
2397 }
2398 // Enter a scope to hold everything within the compound stmt. Compound
2399 // statements can always hold declarations.
2400 ParseScope BodyScope(this, Scope::DeclScope);
2401
2402 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2403
2404 BodyScope.Exit();
2405 if (AutoreleasePoolBody.isInvalid())
2406 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2407 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002408 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002409}
2410
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002411/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2412/// for later parsing.
2413void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
2414 LexedMethod* LM = new LexedMethod(this, MDecl);
2415 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2416 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002417 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002418 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002419 if (Tok.is(tok::kw_try)) {
2420 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002421 if (Tok.is(tok::colon)) {
2422 Toks.push_back(Tok);
2423 ConsumeToken();
2424 while (Tok.isNot(tok::l_brace)) {
2425 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2426 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2427 }
2428 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002429 Toks.push_back(Tok); // also store '{'
2430 }
2431 else if (Tok.is(tok::colon)) {
2432 ConsumeToken();
2433 while (Tok.isNot(tok::l_brace)) {
2434 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2435 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2436 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002437 Toks.push_back(Tok); // also store '{'
2438 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002439 ConsumeBrace();
2440 // Consume everything up to (and including) the matching right brace.
2441 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002442 while (Tok.is(tok::kw_catch)) {
2443 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2444 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2445 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002446}
2447
Steve Naroff09bf8152007-09-06 21:24:23 +00002448/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002449///
John McCall48871652010-08-21 09:40:31 +00002450Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002451 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002452
John McCallfaf5fb42010-08-26 23:41:50 +00002453 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2454 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002455
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002456 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002457 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002458 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002459 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002460 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002461 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002462 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002463 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002464
Steve Naroffbb875722007-11-11 19:54:21 +00002465 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002466 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002467 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002468
Steve Naroffbb875722007-11-11 19:54:21 +00002469 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002470 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002471
Steve Naroffbb875722007-11-11 19:54:21 +00002472 // If we didn't find the '{', bail out.
2473 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002474 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002475 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002476
2477 if (!MDecl) {
2478 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002479 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002480 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002481 }
2482
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002483 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002484 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002485 assert (CurParsedObjCImpl
2486 && "ParseObjCMethodDefinition - Method out of @implementation");
2487 // Consume the tokens and store them for later parsing.
2488 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002489 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002490}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002491
John McCalldadc5752010-08-24 06:29:42 +00002492StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002493 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002494 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002495 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002496 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002497 }
2498
2499 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002500 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002501
2502 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002503 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002504
2505 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002506 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002507
2508 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2509 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002510
2511 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2512 getLangOpts().DebuggerSupport) {
2513 SkipUntil(tok::semi);
2514 return Actions.ActOnNullStmt(Tok.getLocation());
2515 }
2516
John McCalldadc5752010-08-24 06:29:42 +00002517 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002518 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002519 // If the expression is invalid, skip ahead to the next semicolon. Not
2520 // doing this opens us up to the possibility of infinite loops if
2521 // ParseExpression does not consume any tokens.
2522 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002523 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002524 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002525
Steve Naroffe6016792008-02-05 21:27:35 +00002526 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002527 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002528 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002529}
2530
John McCalldadc5752010-08-24 06:29:42 +00002531ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002532 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002533 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002534 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002535 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002536 return ExprError();
2537
Ted Kremeneke65b0862012-03-06 20:05:56 +00002538 case tok::minus:
2539 case tok::plus: {
2540 tok::TokenKind Kind = Tok.getKind();
2541 SourceLocation OpLoc = ConsumeToken();
2542
2543 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002544 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002545 switch (Kind) {
2546 case tok::minus: Symbol = "-"; break;
2547 case tok::plus: Symbol = "+"; break;
2548 default: llvm_unreachable("missing unary operator case");
2549 }
2550 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2551 << Symbol;
2552 return ExprError();
2553 }
2554
2555 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2556 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002557 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002558 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002559 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002560
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002561 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002562 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002563 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002564
2565 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002566 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002567 }
2568
Chris Lattnere002fbe2007-12-12 01:04:12 +00002569 case tok::string_literal: // primary-expression: string-literal
2570 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002571 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002572
2573 case tok::char_constant:
2574 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2575
2576 case tok::numeric_constant:
2577 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2578
2579 case tok::kw_true: // Objective-C++, etc.
2580 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2581 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2582 case tok::kw_false: // Objective-C++, etc.
2583 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2584 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2585
2586 case tok::l_square:
2587 // Objective-C array literal
2588 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2589
2590 case tok::l_brace:
2591 // Objective-C dictionary literal
2592 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2593
Patrick Beard0caa3942012-04-19 00:25:12 +00002594 case tok::l_paren:
2595 // Objective-C boxed expression
2596 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2597
Chris Lattnere002fbe2007-12-12 01:04:12 +00002598 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002599 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002600 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002601
Chris Lattner197a3012008-08-05 06:19:09 +00002602 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2603 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002604 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002605 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002606 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002607 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002608 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002609 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002610 const char *str = nullptr;
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002611 if (GetLookAheadToken(1).is(tok::l_brace)) {
2612 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2613 str =
2614 ch == 't' ? "try"
2615 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002616 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002617 }
2618 if (str) {
2619 SourceLocation kwLoc = Tok.getLocation();
2620 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2621 FixItHint::CreateReplacement(kwLoc, str));
2622 }
2623 else
2624 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2625 }
Chris Lattner197a3012008-08-05 06:19:09 +00002626 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002627 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002628}
2629
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002630/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002631///
2632/// This routine parses the receiver of a message send in
2633/// Objective-C++ either as a type or as an expression. Note that this
2634/// routine must not be called to parse a send to 'super', since it
2635/// has no way to return such a result.
2636///
2637/// \param IsExpr Whether the receiver was parsed as an expression.
2638///
2639/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2640/// IsExpr is true), the parsed expression. If the receiver was parsed
2641/// as a type (\c IsExpr is false), the parsed type.
2642///
2643/// \returns True if an error occurred during parsing or semantic
2644/// analysis, in which case the arguments do not have valid
2645/// values. Otherwise, returns false for a successful parse.
2646///
2647/// objc-receiver: [C++]
2648/// 'super' [not parsed here]
2649/// expression
2650/// simple-type-specifier
2651/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002652bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002653 InMessageExpressionRAIIObject InMessage(*this, true);
2654
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002655 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2656 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002657 TryAnnotateTypeOrScopeToken();
2658
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002659 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002660 // objc-receiver:
2661 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002662 // Make sure any typos in the receiver are corrected or diagnosed, so that
2663 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2664 // only the things that are valid ObjC receivers?
2665 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002666 if (Receiver.isInvalid())
2667 return true;
2668
2669 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002670 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002671 return false;
2672 }
2673
2674 // objc-receiver:
2675 // typename-specifier
2676 // simple-type-specifier
2677 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002678 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002679 ParseCXXSimpleTypeSpecifier(DS);
2680
2681 if (Tok.is(tok::l_paren)) {
2682 // If we see an opening parentheses at this point, we are
2683 // actually parsing an expression that starts with a
2684 // function-style cast, e.g.,
2685 //
2686 // postfix-expression:
2687 // simple-type-specifier ( expression-list [opt] )
2688 // typename-specifier ( expression-list [opt] )
2689 //
2690 // Parse the remainder of this case, then the (optional)
2691 // postfix-expression suffix, followed by the (optional)
2692 // right-hand side of the binary expression. We have an
2693 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002694 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002695 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002696 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002697 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002698 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002699 if (Receiver.isInvalid())
2700 return true;
2701
2702 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002703 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002704 return false;
2705 }
2706
2707 // We have a class message. Turn the simple-type-specifier or
2708 // typename-specifier we parsed into a type and parse the
2709 // remainder of the class message.
2710 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002711 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002712 if (Type.isInvalid())
2713 return true;
2714
2715 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002716 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002717 return false;
2718}
2719
Douglas Gregor990ccac2010-05-31 14:40:22 +00002720/// \brief Determine whether the parser is currently referring to a an
2721/// Objective-C message send, using a simplified heuristic to avoid overhead.
2722///
2723/// This routine will only return true for a subset of valid message-send
2724/// expressions.
2725bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002726 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002727 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002728 return GetLookAheadToken(1).is(tok::identifier) &&
2729 GetLookAheadToken(2).is(tok::identifier);
2730}
2731
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002732bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002733 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002734 InMessageExpression)
2735 return false;
2736
2737
2738 ParsedType Type;
2739
2740 if (Tok.is(tok::annot_typename))
2741 Type = getTypeAnnotation(Tok);
2742 else if (Tok.is(tok::identifier))
2743 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2744 getCurScope());
2745 else
2746 return false;
2747
2748 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2749 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002750 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002751 if (Tok.is(tok::identifier))
2752 TryAnnotateTypeOrScopeToken();
2753
2754 return Tok.is(tok::annot_typename);
2755 }
2756 }
2757
2758 return false;
2759}
2760
Mike Stump11289f42009-09-09 15:08:12 +00002761/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002762/// '[' objc-receiver objc-message-args ']'
2763///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002764/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002765/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002766/// expression
2767/// class-name
2768/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002769///
John McCalldadc5752010-08-24 06:29:42 +00002770ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002771 assert(Tok.is(tok::l_square) && "'[' expected");
2772 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2773
Douglas Gregora817a192010-05-27 23:06:34 +00002774 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002775 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002776 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002777 return ExprError();
2778 }
2779
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002780 InMessageExpressionRAIIObject InMessage(*this, true);
2781
David Blaikiebbafb8a2012-03-11 07:00:24 +00002782 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002783 // We completely separate the C and C++ cases because C++ requires
2784 // more complicated (read: slower) parsing.
2785
2786 // Handle send to super.
2787 // FIXME: This doesn't benefit from the same typo-correction we
2788 // get in Objective-C.
2789 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002790 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002791 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002792 ParsedType(), nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002793
2794 // Parse the receiver, which is either a type or an expression.
2795 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00002796 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002797 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002798 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002799 return ExprError();
2800 }
2801
2802 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002803 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2804 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002805 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002806
2807 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002808 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00002809 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00002810 }
2811
2812 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002813 IdentifierInfo *Name = Tok.getIdentifierInfo();
2814 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002815 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002816 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002817 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002818 NextToken().is(tok::period),
2819 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002820 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002821 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002822 ParsedType(), nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002823
John McCallfaf5fb42010-08-26 23:41:50 +00002824 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002825 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002826 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002827 return ExprError();
2828 }
2829
Douglas Gregore5798dc2010-04-21 20:38:13 +00002830 ConsumeToken(); // the type name
2831
2832 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00002833 ReceiverType, nullptr);
2834
John McCallfaf5fb42010-08-26 23:41:50 +00002835 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002836 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002837 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002838 }
Chris Lattner8f697062008-01-25 18:59:06 +00002839 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002840
2841 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00002842 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002843 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002844 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002845 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00002846 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002847
John McCallba7bf592010-08-24 05:47:05 +00002848 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002849 ParsedType(), Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00002850}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002851
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002852/// \brief Parse the remainder of an Objective-C message following the
2853/// '[' objc-receiver.
2854///
2855/// This routine handles sends to super, class messages (sent to a
2856/// class name), and instance messages (sent to an object), and the
2857/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2858/// ReceiverExpr, respectively. Only one of these parameters may have
2859/// a valid value.
2860///
2861/// \param LBracLoc The location of the opening '['.
2862///
2863/// \param SuperLoc If this is a send to 'super', the location of the
2864/// 'super' keyword that indicates a send to the superclass.
2865///
2866/// \param ReceiverType If this is a class message, the type of the
2867/// class we are sending a message to.
2868///
2869/// \param ReceiverExpr If this is an instance message, the expression
2870/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002871///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002872/// objc-message-args:
2873/// objc-selector
2874/// objc-keywordarg-list
2875///
2876/// objc-keywordarg-list:
2877/// objc-keywordarg
2878/// objc-keywordarg-list objc-keywordarg
2879///
Mike Stump11289f42009-09-09 15:08:12 +00002880/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002881/// selector-name[opt] ':' objc-keywordexpr
2882///
2883/// objc-keywordexpr:
2884/// nonempty-expr-list
2885///
2886/// nonempty-expr-list:
2887/// assignment-expression
2888/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002889///
John McCalldadc5752010-08-24 06:29:42 +00002890ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002891Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002892 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002893 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00002894 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002895 InMessageExpressionRAIIObject InMessage(*this, true);
2896
Steve Naroffeae65032009-11-07 02:08:14 +00002897 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002898 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002899 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002900 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002901 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002902 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002903 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002904 else
John McCallb268a282010-08-23 23:25:46 +00002905 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002906 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002907 cutOffParsing();
2908 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002909 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002910
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002911 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002912 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002913 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002914
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002915 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002916 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002917 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00002918
Chris Lattner0ef13522007-10-09 17:51:17 +00002919 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002920 while (1) {
2921 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002922 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002923 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00002924
Alp Toker383d2c42014-01-01 03:08:43 +00002925 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00002926 // We must manually skip to a ']', otherwise the expression skipper will
2927 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2928 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002929 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002930 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002931 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002932
Mike Stump11289f42009-09-09 15:08:12 +00002933 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002934
2935 if (Tok.is(tok::code_completion)) {
2936 if (SuperLoc.isValid())
2937 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002938 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002939 /*AtArgumentEpression=*/true);
2940 else if (ReceiverType)
2941 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002942 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002943 /*AtArgumentEpression=*/true);
2944 else
2945 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002946 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002947 /*AtArgumentEpression=*/true);
2948
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002949 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002950 return ExprError();
2951 }
2952
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00002953 ExprResult Expr;
2954 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
2955 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2956 Expr = ParseBraceInitializer();
2957 } else
2958 Expr = ParseAssignmentExpression();
2959
2960 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002961 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002962 // We must manually skip to a ']', otherwise the expression skipper will
2963 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2964 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002965 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002966 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00002967 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002968
Steve Naroff486760a2007-09-17 20:25:27 +00002969 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002970 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002971
Douglas Gregor1b605f72009-11-19 01:08:35 +00002972 // Code completion after each argument.
2973 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002974 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002975 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002976 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002977 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002978 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002979 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002980 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002981 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002982 else
John McCallb268a282010-08-23 23:25:46 +00002983 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002984 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002985 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002986 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002987 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002988 }
2989
Steve Naroff486760a2007-09-17 20:25:27 +00002990 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002991 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002992 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002993 break;
2994 // We have a selector or a colon, continue parsing.
2995 }
2996 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002997 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002998 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002999 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00003000 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00003001 if (Tok.is(tok::colon))
3002 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003003 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003004 if (Tok.is(tok::colon)) {
3005 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
3006 FixItHint::CreateRemoval(commaLoc);
3007 }
Chris Lattner197a3012008-08-05 06:19:09 +00003008 // We must manually skip to a ']', otherwise the expression skipper will
3009 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3010 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003011 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003012 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003013 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003014
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003015 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003016 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003017 }
3018 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00003019 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003020
Chris Lattner197a3012008-08-05 06:19:09 +00003021 // We must manually skip to a ']', otherwise the expression skipper will
3022 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3023 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003024 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003025 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003026 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00003027
Chris Lattner0ef13522007-10-09 17:51:17 +00003028 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00003029 Diag(Tok, diag::err_expected)
3030 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00003031 // We must manually skip to a ']', otherwise the expression skipper will
3032 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3033 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003034 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003035 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003036 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003037
Chris Lattner8f697062008-01-25 18:59:06 +00003038 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003039
Steve Naroffe61bfa82007-10-05 18:42:47 +00003040 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003041 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00003042 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003043 KeyLocs.push_back(Loc);
3044 }
Chris Lattner5700fab2007-10-07 02:00:24 +00003045 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003046
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003047 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003048 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003049 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003050 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003051 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003052 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00003053 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003054 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003055}
3056
John McCalldadc5752010-08-24 06:29:42 +00003057ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
3058 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003059 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003060
Chris Lattnere002fbe2007-12-12 01:04:12 +00003061 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
3062 // expressions. At this point, we know that the only valid thing that starts
3063 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003064 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003065 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00003066 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003067 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003068
Chris Lattnere002fbe2007-12-12 01:04:12 +00003069 while (Tok.is(tok::at)) {
3070 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00003071
Sebastian Redlc13f2682008-12-09 20:22:58 +00003072 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00003073 if (!isTokenStringLiteral())
3074 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00003075
John McCalldadc5752010-08-24 06:29:42 +00003076 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003077 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003078 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003079
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003080 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00003081 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003082
Nico Webera7c7e602012-12-31 00:28:03 +00003083 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
3084 AtStrings.size());
Anders Carlsson76f4a902007-08-21 17:43:55 +00003085}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003086
Ted Kremeneke65b0862012-03-06 20:05:56 +00003087/// ParseObjCBooleanLiteral -
3088/// objc-scalar-literal : '@' boolean-keyword
3089/// ;
3090/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
3091/// ;
3092ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
3093 bool ArgValue) {
3094 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
3095 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
3096}
3097
3098/// ParseObjCCharacterLiteral -
3099/// objc-scalar-literal : '@' character-literal
3100/// ;
3101ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
3102 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
3103 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003104 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003105 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003106 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003107 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003108}
3109
3110/// ParseObjCNumericLiteral -
3111/// objc-scalar-literal : '@' scalar-literal
3112/// ;
3113/// scalar-literal : | numeric-constant /* any numeric constant. */
3114/// ;
3115ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3116 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3117 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003118 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003119 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003120 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003121 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003122}
3123
Patrick Beard0caa3942012-04-19 00:25:12 +00003124/// ParseObjCBoxedExpr -
3125/// objc-box-expression:
3126/// @( assignment-expression )
3127ExprResult
3128Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3129 if (Tok.isNot(tok::l_paren))
3130 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3131
3132 BalancedDelimiterTracker T(*this, tok::l_paren);
3133 T.consumeOpen();
3134 ExprResult ValueExpr(ParseAssignmentExpression());
3135 if (T.consumeClose())
3136 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00003137
3138 if (ValueExpr.isInvalid())
3139 return ExprError();
3140
Patrick Beard0caa3942012-04-19 00:25:12 +00003141 // Wrap the sub-expression in a parenthesized expression, to distinguish
3142 // a boxed expression from a literal.
3143 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003144 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00003145 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003146 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00003147}
3148
Ted Kremeneke65b0862012-03-06 20:05:56 +00003149ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00003150 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003151 ConsumeBracket(); // consume the l_square.
3152
3153 while (Tok.isNot(tok::r_square)) {
3154 // Parse list of array element expressions (all must be id types).
3155 ExprResult Res(ParseAssignmentExpression());
3156 if (Res.isInvalid()) {
3157 // We must manually skip to a ']', otherwise the expression skipper will
3158 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3159 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003160 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003161 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003162 }
3163
3164 // Parse the ellipsis that indicates a pack expansion.
3165 if (Tok.is(tok::ellipsis))
3166 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
3167 if (Res.isInvalid())
3168 return true;
3169
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003170 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003171
3172 if (Tok.is(tok::comma))
3173 ConsumeToken(); // Eat the ','.
3174 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00003175 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3176 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003177 }
3178 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramerf0623432012-08-23 22:51:59 +00003179 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00003180 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003181}
3182
3183ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3184 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3185 ConsumeBrace(); // consume the l_square.
3186 while (Tok.isNot(tok::r_brace)) {
3187 // Parse the comma separated key : value expressions.
3188 ExprResult KeyExpr;
3189 {
3190 ColonProtectionRAIIObject X(*this);
3191 KeyExpr = ParseAssignmentExpression();
3192 if (KeyExpr.isInvalid()) {
3193 // We must manually skip to a '}', otherwise the expression skipper will
3194 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3195 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003196 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003197 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003198 }
3199 }
3200
Alp Toker383d2c42014-01-01 03:08:43 +00003201 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003202 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00003203 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003204 }
3205
3206 ExprResult ValueExpr(ParseAssignmentExpression());
3207 if (ValueExpr.isInvalid()) {
3208 // We must manually skip to a '}', otherwise the expression skipper will
3209 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3210 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003211 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003212 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003213 }
3214
3215 // Parse the ellipsis that designates this as a pack expansion.
3216 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00003217 if (getLangOpts().CPlusPlus)
3218 TryConsumeToken(tok::ellipsis, EllipsisLoc);
3219
Ted Kremeneke65b0862012-03-06 20:05:56 +00003220 // We have a valid expression. Collect it in a vector so we can
3221 // build the argument list.
3222 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00003223 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00003224 };
3225 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00003226
3227 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00003228 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3229 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003230 }
3231 SourceLocation EndLoc = ConsumeBrace();
3232
3233 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00003234 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
3235 Elements.data(), Elements.size());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003236}
3237
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003238/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00003239/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00003240ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003241Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00003242 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003243
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003244 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003245
Chris Lattner197a3012008-08-05 06:19:09 +00003246 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003247 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3248
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003249 BalancedDelimiterTracker T(*this, tok::l_paren);
3250 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003251
Douglas Gregor220cac52009-02-18 17:45:20 +00003252 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003253
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003254 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003255
Douglas Gregor220cac52009-02-18 17:45:20 +00003256 if (Ty.isInvalid())
3257 return ExprError();
3258
Nico Webera7c7e602012-12-31 00:28:03 +00003259 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3260 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003261}
Anders Carlssone01493d2007-08-23 15:25:28 +00003262
3263/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00003264/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00003265ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003266Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00003267 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003268
Chris Lattner197a3012008-08-05 06:19:09 +00003269 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003270 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3271
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003272 BalancedDelimiterTracker T(*this, tok::l_paren);
3273 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003274
Chris Lattner197a3012008-08-05 06:19:09 +00003275 if (Tok.isNot(tok::identifier))
Alp Tokerec543272013-12-24 09:48:30 +00003276 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003277
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003278 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00003279 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003280
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003281 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00003282
Nico Webera7c7e602012-12-31 00:28:03 +00003283 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3284 T.getOpenLocation(), ProtoIdLoc,
3285 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00003286}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003287
3288/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003289/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003290ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003291 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003292
Chris Lattner197a3012008-08-05 06:19:09 +00003293 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003294 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3295
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003296 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003297 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003298
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003299 BalancedDelimiterTracker T(*this, tok::l_paren);
3300 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003301 bool HasOptionalParen = Tok.is(tok::l_paren);
3302 if (HasOptionalParen)
3303 ConsumeParen();
3304
Douglas Gregor67c692c2010-08-26 15:07:07 +00003305 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003306 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003307 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003308 return ExprError();
3309 }
3310
Chris Lattner4f472a32009-04-11 18:13:45 +00003311 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003312 if (!SelIdent && // missing selector name.
3313 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003314 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003315
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003316 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003317
Steve Naroff152dd812007-12-05 22:21:29 +00003318 unsigned nColons = 0;
3319 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003320 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003321 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003322 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003323 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003324 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3325 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003326 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003327
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003328 if (Tok.is(tok::r_paren))
3329 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003330
3331 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003332 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003333 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003334 return ExprError();
3335 }
3336
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003337 // Check for another keyword selector.
3338 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003339 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003340 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003341 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003342 break;
3343 }
Steve Naroff152dd812007-12-05 22:21:29 +00003344 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003345 if (HasOptionalParen && Tok.is(tok::r_paren))
3346 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003347 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003348 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003349 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3350 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003351 T.getCloseLocation(),
3352 !HasOptionalParen);
Gabor Greif24032f12007-10-19 15:38:32 +00003353 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003354
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003355void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3356 // MCDecl might be null due to error in method or c-function prototype, etc.
3357 Decl *MCDecl = LM.D;
3358 bool skip = MCDecl &&
3359 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3360 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3361 if (skip)
3362 return;
3363
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003364 // Save the current token position.
3365 SourceLocation OrigLoc = Tok.getLocation();
3366
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003367 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
3368 // Append the current token at the end of the new token stream so that it
3369 // doesn't get lost.
3370 LM.Toks.push_back(Tok);
3371 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
3372
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003373 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003374 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003375
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003376 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3377 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003378 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003379 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003380 parseMethod
3381 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
3382 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003383
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003384 // Tell the actions module that we have entered a method or c-function definition
3385 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003386 if (parseMethod)
3387 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3388 else
3389 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003390 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003391 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003392 else {
3393 if (Tok.is(tok::colon))
3394 ParseConstructorInitializer(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003395 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003396 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00003397
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003398 if (Tok.getLocation() != OrigLoc) {
3399 // Due to parsing error, we either went over the cached tokens or
3400 // there are still cached tokens left. If it's the latter case skip the
3401 // leftover tokens.
3402 // Since this is an uncommon situation that should be avoided, use the
3403 // expensive isBeforeInTranslationUnit call.
3404 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3405 OrigLoc))
3406 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3407 ConsumeAnyToken();
3408 }
3409
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003410 return;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003411}