blob: 3ef187af8d7e9d4b0af1db71dc2443a0857e7b41 [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:
182/// ':' identifier
183///
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;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000296
Chris Lattner0ef13522007-10-09 17:51:17 +0000297 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000298 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000299
300 // Code completion of superclass names.
301 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000302 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000303 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000304 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000305 }
306
Chris Lattner0ef13522007-10-09 17:51:17 +0000307 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000308 Diag(Tok, diag::err_expected)
309 << tok::identifier; // missing super class name.
Craig Topper161e4db2014-05-21 06:02:52 +0000310 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000311 }
312 superClassId = Tok.getIdentifierInfo();
313 superClassLoc = ConsumeToken();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000314 } else if (typeParameterList) {
315 // An objc-type-parameter-list is ambiguous with an objc-protocol-refs
316 // in an @interface without a specified superclass, so such classes
317 // are ill-formed. We have determined that we have an
318 // objc-type-parameter-list but no superclass, so complain and record
319 // as if we inherited from NSObject.
320 SourceLocation insertLoc = PP.getLocForEndOfToken(PrevTokLocation);
321 Diag(insertLoc, diag::err_objc_parameterized_class_without_base)
322 << nameId
323 << FixItHint::CreateInsertion(insertLoc, " : NSObject");
324 superClassId = PP.getIdentifierInfo("NSObject");
325 superClassLoc = Tok.getLocation();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000326 }
Douglas Gregor85f3f952015-07-07 03:57:15 +0000327
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000328 // Next, we need to check for any protocol references.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000329 SmallVector<Decl *, 8> ProtocolRefs;
330 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000331 if (LAngleLoc.isValid()) {
332 // We already parsed the protocols named when we thought we had a
333 // type parameter list. Translate them into actual protocol references.
334 for (const auto &pair : ProtocolIdents) {
335 ProtocolLocs.push_back(pair.second);
336 }
337 Actions.FindProtocolDeclaration(/*WarnOnDeclarations=*/true,
338 /*ForObjCContainer=*/true,
339 &ProtocolIdents[0], ProtocolIdents.size(),
340 ProtocolRefs);
341 } else if (Tok.is(tok::less) &&
342 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, true,
343 LAngleLoc, EndProtoLoc)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000344 return nullptr;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000345 }
Mike Stump11289f42009-09-09 15:08:12 +0000346
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000347 if (Tok.isNot(tok::less))
348 Actions.ActOnTypedefedProtocols(ProtocolRefs, superClassId, superClassLoc);
349
John McCall48871652010-08-21 09:40:31 +0000350 Decl *ClsType =
Douglas Gregor85f3f952015-07-07 03:57:15 +0000351 Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc, typeParameterList,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000352 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000353 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000354 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000355 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000356
Chris Lattner0ef13522007-10-09 17:51:17 +0000357 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000358 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000359
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000360 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000361
362 if (typeParameterList)
363 Actions.popObjCTypeParamList(getCurScope(), typeParameterList);
364
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000365 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000366}
367
Douglas Gregor813a0662015-06-19 18:14:38 +0000368/// Add an attribute for a context-sensitive type nullability to the given
369/// declarator.
370static void addContextSensitiveTypeNullability(Parser &P,
371 Declarator &D,
372 NullabilityKind nullability,
373 SourceLocation nullabilityLoc,
374 bool &addedToDeclSpec) {
375 // Create the attribute.
376 auto getNullabilityAttr = [&]() -> AttributeList * {
Douglas Gregorbec595a2015-06-19 18:27:45 +0000377 return D.getAttributePool().create(
378 P.getNullabilityKeyword(nullability),
379 SourceRange(nullabilityLoc),
380 nullptr, SourceLocation(),
381 nullptr, 0,
382 AttributeList::AS_ContextSensitiveKeyword);
Douglas Gregor813a0662015-06-19 18:14:38 +0000383 };
384
385 if (D.getNumTypeObjects() > 0) {
386 // Add the attribute to the declarator chunk nearest the declarator.
387 auto nullabilityAttr = getNullabilityAttr();
388 DeclaratorChunk &chunk = D.getTypeObject(0);
389 nullabilityAttr->setNext(chunk.getAttrListRef());
390 chunk.getAttrListRef() = nullabilityAttr;
391 } else if (!addedToDeclSpec) {
392 // Otherwise, just put it on the declaration specifiers (if one
393 // isn't there already).
394 D.getMutableDeclSpec().addAttributes(getNullabilityAttr());
395 addedToDeclSpec = true;
396 }
397}
398
Douglas Gregor85f3f952015-07-07 03:57:15 +0000399/// Parse an Objective-C type parameter list, if present, or capture
400/// the locations of the protocol identifiers for a list of protocol
401/// references.
402///
403/// objc-type-parameter-list:
404/// '<' objc-type-parameter (',' objc-type-parameter)* '>'
405///
406/// objc-type-parameter:
407/// identifier objc-type-parameter-bound[opt]
408///
409/// objc-type-parameter-bound:
410/// ':' type-name
411///
412/// \param lAngleLoc The location of the starting '<'.
413///
414/// \param protocolIdents Will capture the list of identifiers, if the
415/// angle brackets contain a list of protocol references rather than a
416/// type parameter list.
417///
418/// \param rAngleLoc The location of the ending '>'.
419ObjCTypeParamList *Parser::parseObjCTypeParamListOrProtocolRefs(
420 SourceLocation &lAngleLoc,
421 SmallVectorImpl<IdentifierLocPair> &protocolIdents,
422 SourceLocation &rAngleLoc,
423 bool mayBeProtocolList) {
424 assert(Tok.is(tok::less) && "Not at the beginning of a type parameter list");
425
426 // Within the type parameter list, don't treat '>' as an operator.
427 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
428
429 // Local function to "flush" the protocol identifiers, turning them into
430 // type parameters.
431 SmallVector<Decl *, 4> typeParams;
432 auto makeProtocolIdentsIntoTypeParameters = [&]() {
433 for (const auto &pair : protocolIdents) {
434 DeclResult typeParam = Actions.actOnObjCTypeParam(getCurScope(),
435 pair.first,
436 pair.second,
437 SourceLocation(),
438 ParsedType());
439 if (typeParam.isUsable())
440 typeParams.push_back(typeParam.get());
441 }
442
443 protocolIdents.clear();
444 mayBeProtocolList = false;
445 };
446
447 bool invalid = false;
448 lAngleLoc = ConsumeToken();
449 do {
450 // Parse the identifier.
451 if (!Tok.is(tok::identifier)) {
452 // Code completion.
453 if (Tok.is(tok::code_completion)) {
454 // FIXME: If these aren't protocol references, we'll need different
455 // completions.
456 Actions.CodeCompleteObjCProtocolReferences(protocolIdents.data(),
457 protocolIdents.size());
458 cutOffParsing();
459
460 // FIXME: Better recovery here?.
461 return nullptr;
462 }
463
464 Diag(Tok, diag::err_objc_expected_type_parameter);
465 invalid = true;
466 break;
467 }
468
469 IdentifierInfo *paramName = Tok.getIdentifierInfo();
470 SourceLocation paramLoc = ConsumeToken();
471
472 // If there is a bound, parse it.
473 SourceLocation colonLoc;
474 TypeResult boundType;
475 if (TryConsumeToken(tok::colon, colonLoc)) {
476 // Once we've seen a bound, we know this is not a list of protocol
477 // references.
478 if (mayBeProtocolList) {
479 // Up until now, we have been queuing up parameters because they
480 // might be protocol references. Turn them into parameters now.
481 makeProtocolIdentsIntoTypeParameters();
482 }
483
484 // type-name
485 boundType = ParseTypeName();
486 if (boundType.isInvalid())
487 invalid = true;
488 } else if (mayBeProtocolList) {
489 // If this could still be a protocol list, just capture the identifier.
490 // We don't want to turn it into a parameter.
491 protocolIdents.push_back(std::make_pair(paramName, paramLoc));
492 continue;
493 }
494
495 // Create the type parameter.
496 DeclResult typeParam = Actions.actOnObjCTypeParam(getCurScope(),
497 paramName,
498 paramLoc,
499 colonLoc,
500 boundType.isUsable()
501 ? boundType.get()
502 : ParsedType());
503 if (typeParam.isUsable())
504 typeParams.push_back(typeParam.get());
505 } while (TryConsumeToken(tok::comma));
506
507 // Parse the '>'.
508 if (invalid) {
509 SkipUntil(tok::greater, tok::at, StopBeforeMatch);
510 if (Tok.is(tok::greater))
511 ConsumeToken();
512 } else if (ParseGreaterThanInTemplateList(rAngleLoc,
513 /*ConsumeLastToken=*/true,
514 /*ObjCGenericList=*/true)) {
515 Diag(lAngleLoc, diag::note_matching) << "'<'";
516 SkipUntil({tok::greater, tok::greaterequal, tok::at, tok::minus,
517 tok::minus, tok::plus, tok::colon, tok::l_paren, tok::l_brace,
518 tok::comma, tok::semi },
519 StopBeforeMatch);
520 if (Tok.is(tok::greater))
521 ConsumeToken();
522 }
523
524 if (mayBeProtocolList) {
525 // A type parameter list must be followed by either a ':' (indicating the
526 // presence of a superclass) or a '(' (indicating that this is a category
527 // or extension). This disambiguates between an objc-type-parameter-list
528 // and a objc-protocol-refs.
529 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_paren)) {
530 // Returning null indicates that we don't have a type parameter list.
531 // The results the caller needs to handle the protocol references are
532 // captured in the reference parameters already.
533 return nullptr;
534 }
535
536 // We have a type parameter list that looks like a list of protocol
537 // references. Turn that parameter list into type parameters.
538 makeProtocolIdentsIntoTypeParameters();
539 }
540
541 // Form the type parameter list.
542 ObjCTypeParamList *list = Actions.actOnObjCTypeParamList(
543 getCurScope(),
544 lAngleLoc,
545 typeParams,
546 rAngleLoc);
547
548 // Clear out the angle locations; they're used by the caller to indicate
549 // whether there are any protocol references.
550 lAngleLoc = SourceLocation();
551 rAngleLoc = SourceLocation();
552 return list;
553}
554
555/// Parse an objc-type-parameter-list.
556ObjCTypeParamList *Parser::parseObjCTypeParamList() {
557 SourceLocation lAngleLoc;
558 SmallVector<IdentifierLocPair, 1> protocolIdents;
559 SourceLocation rAngleLoc;
560 return parseObjCTypeParamListOrProtocolRefs(lAngleLoc, protocolIdents,
561 rAngleLoc,
562 /*mayBeProtocolList=*/false);
563}
564
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000565/// objc-interface-decl-list:
566/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000567/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000568/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000569/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000570/// objc-interface-decl-list declaration
571/// objc-interface-decl-list ';'
572///
Steve Naroff99264b42007-08-22 16:35:03 +0000573/// objc-method-requirement: [OBJC2]
574/// @required
575/// @optional
576///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000577void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
578 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000579 SmallVector<Decl *, 32> allMethods;
580 SmallVector<Decl *, 16> allProperties;
581 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000582 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000583
Ted Kremenekc7c64312010-01-07 01:20:12 +0000584 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000585
Steve Naroff99264b42007-08-22 16:35:03 +0000586 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000587 // If this is a method prototype, parse it.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000588 if (Tok.isOneOf(tok::minus, tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000589 if (Decl *methodPrototype =
590 ParseObjCMethodPrototype(MethodImplKind, false))
591 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000592 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
593 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000594 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
595 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000596 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000597 if (Tok.is(tok::semi))
598 ConsumeToken();
599 }
Steve Naroff99264b42007-08-22 16:35:03 +0000600 continue;
601 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000602 if (Tok.is(tok::l_paren)) {
603 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000604 ParseObjCMethodDecl(Tok.getLocation(),
605 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000606 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000607 continue;
608 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000609 // Ignore excess semicolons.
610 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000611 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000612 continue;
613 }
Mike Stump11289f42009-09-09 15:08:12 +0000614
Chris Lattnerda9fb152008-10-20 06:10:06 +0000615 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000616 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000617 break;
Mike Stump11289f42009-09-09 15:08:12 +0000618
Douglas Gregorf1934162010-01-13 21:24:21 +0000619 // Code completion within an Objective-C interface.
620 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000621 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000622 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000623 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000624 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000625 }
626
Chris Lattner038a3e32008-10-20 05:46:22 +0000627 // If we don't have an @ directive, parse it as a function definition.
628 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000629 // The code below does not consume '}'s because it is afraid of eating the
630 // end of a namespace. Because of the way this code is structured, an
631 // erroneous r_brace would cause an infinite loop if not handled here.
632 if (Tok.is(tok::r_brace))
633 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000634 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000635 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000636 continue;
637 }
Mike Stump11289f42009-09-09 15:08:12 +0000638
Chris Lattner038a3e32008-10-20 05:46:22 +0000639 // Otherwise, we have an @ directive, eat the @.
640 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000641 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000642 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000643 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000644 }
645
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000646 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000647
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000648 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000649 AtEnd.setBegin(AtLoc);
650 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000651 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000652 } else if (DirectiveKind == tok::objc_not_keyword) {
653 Diag(Tok, diag::err_objc_unknown_at);
654 SkipUntil(tok::semi);
655 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000656 }
Mike Stump11289f42009-09-09 15:08:12 +0000657
Chris Lattnerda9fb152008-10-20 06:10:06 +0000658 // Eat the identifier.
659 ConsumeToken();
660
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000661 switch (DirectiveKind) {
662 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000663 // FIXME: If someone forgets an @end on a protocol, this loop will
664 // continue to eat up tons of stuff and spew lots of nonsense errors. It
665 // would probably be better to bail out if we saw an @class or @interface
666 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000667 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000668 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000669 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000670 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000671
672 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000673 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000674 Diag(AtLoc, diag::err_objc_missing_end)
675 << FixItHint::CreateInsertion(AtLoc, "@end\n");
676 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
677 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000678 ConsumeToken();
679 break;
680
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000681 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000682 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000683 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000684 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000685 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000686 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000687 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000688 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000689 break;
Mike Stump11289f42009-09-09 15:08:12 +0000690
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000691 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000692 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000693 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000694
Chris Lattner038a3e32008-10-20 05:46:22 +0000695 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000696 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000697 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000698 if (Tok.is(tok::l_paren)) {
699 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000700 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000701 }
Mike Stump11289f42009-09-09 15:08:12 +0000702
Douglas Gregor813a0662015-06-19 18:14:38 +0000703 bool addedToDeclSpec = false;
Benjamin Kramera39beb92014-09-03 11:06:10 +0000704 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
705 if (FD.D.getIdentifier() == nullptr) {
706 Diag(AtLoc, diag::err_objc_property_requires_field_name)
707 << FD.D.getSourceRange();
708 return;
709 }
710 if (FD.BitfieldSize) {
711 Diag(AtLoc, diag::err_objc_property_bitfield)
712 << FD.D.getSourceRange();
713 return;
714 }
715
Douglas Gregor813a0662015-06-19 18:14:38 +0000716 // Map a nullability property attribute to a context-sensitive keyword
717 // attribute.
718 if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
719 addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
720 OCDS.getNullabilityLoc(),
721 addedToDeclSpec);
722
Benjamin Kramera39beb92014-09-03 11:06:10 +0000723 // Install the property declarator into interfaceDecl.
724 IdentifierInfo *SelName =
725 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
726
727 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
728 IdentifierInfo *SetterName = OCDS.getSetterName();
729 Selector SetterSel;
730 if (SetterName)
731 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
732 else
733 SetterSel = SelectorTable::constructSetterSelector(
734 PP.getIdentifierTable(), PP.getSelectorTable(),
735 FD.D.getIdentifier());
736 bool isOverridingProperty = false;
737 Decl *Property = Actions.ActOnProperty(
738 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
739 &isOverridingProperty, MethodImplKind);
740 if (!isOverridingProperty)
741 allProperties.push_back(Property);
742
743 FD.complete(Property);
744 };
John McCallcfefb6d2009-11-03 02:38:08 +0000745
Chris Lattner038a3e32008-10-20 05:46:22 +0000746 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000747 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000748 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000749
John McCall405988b2011-03-26 01:53:26 +0000750 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000751 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000752 }
Steve Naroff99264b42007-08-22 16:35:03 +0000753 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000754
755 // We break out of the big loop in two cases: when we see @end or when we see
756 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000757 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000758 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000759 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000760 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000761 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000762 } else {
763 Diag(Tok, diag::err_objc_missing_end)
764 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
765 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
766 << (int) Actions.getObjCContainerKind();
767 AtEnd.setBegin(Tok.getLocation());
768 AtEnd.setEnd(Tok.getLocation());
769 }
Mike Stump11289f42009-09-09 15:08:12 +0000770
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000771 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000772 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000773 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000774}
775
Douglas Gregor813a0662015-06-19 18:14:38 +0000776/// Diagnose redundant or conflicting nullability information.
777static void diagnoseRedundantPropertyNullability(Parser &P,
778 ObjCDeclSpec &DS,
779 NullabilityKind nullability,
780 SourceLocation nullabilityLoc){
781 if (DS.getNullability() == nullability) {
782 P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000783 << DiagNullabilityKind(nullability, true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000784 << SourceRange(DS.getNullabilityLoc());
785 return;
786 }
787
788 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000789 << DiagNullabilityKind(nullability, true)
790 << DiagNullabilityKind(DS.getNullability(), true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000791 << SourceRange(DS.getNullabilityLoc());
792}
793
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000794/// Parse property attribute declarations.
795///
796/// property-attr-decl: '(' property-attrlist ')'
797/// property-attrlist:
798/// property-attribute
799/// property-attrlist ',' property-attribute
800/// property-attribute:
801/// getter '=' identifier
802/// setter '=' identifier ':'
803/// readonly
804/// readwrite
805/// assign
806/// retain
807/// copy
808/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000809/// atomic
810/// strong
811/// weak
812/// unsafe_unretained
Douglas Gregor813a0662015-06-19 18:14:38 +0000813/// nonnull
814/// nullable
815/// null_unspecified
Douglas Gregor849ebc22015-06-19 18:14:46 +0000816/// null_resettable
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000817///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000818void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000819 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000820 BalancedDelimiterTracker T(*this, tok::l_paren);
821 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000822
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000823 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000824 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000825 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000826 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000827 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000828 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000829
Chris Lattner76619232008-10-20 07:22:18 +0000830 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000831 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000832 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000833 return;
834 }
Mike Stump11289f42009-09-09 15:08:12 +0000835
Chris Lattner1db33542008-10-20 07:37:22 +0000836 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000837
Chris Lattner68e48682008-11-20 04:42:34 +0000838 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000839 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000840 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000841 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000842 else if (II->isStr("unsafe_unretained"))
843 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000844 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000845 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000846 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000847 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000848 else if (II->isStr("strong"))
849 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000850 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000851 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000852 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000853 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000854 else if (II->isStr("atomic"))
855 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000856 else if (II->isStr("weak"))
857 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000858 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000859 bool IsSetter = II->getNameStart()[0] == 's';
860
Chris Lattner825bca12008-10-20 07:39:53 +0000861 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000862 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
863 diag::err_objc_expected_equal_for_getter;
864
Alp Toker383d2c42014-01-01 03:08:43 +0000865 if (ExpectAndConsume(tok::equal, DiagID)) {
866 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000867 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000868 }
Mike Stump11289f42009-09-09 15:08:12 +0000869
Douglas Gregorc8537c52009-11-19 07:41:15 +0000870 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000871 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000872 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000873 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000874 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000875 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000876 }
877
Anders Carlssonfe15a782010-10-02 17:45:21 +0000878
879 SourceLocation SelLoc;
880 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
881
882 if (!SelIdent) {
883 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
884 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000885 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000886 return;
887 }
Mike Stump11289f42009-09-09 15:08:12 +0000888
Anders Carlssonfe15a782010-10-02 17:45:21 +0000889 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000890 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000891 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000892
Alp Toker383d2c42014-01-01 03:08:43 +0000893 if (ExpectAndConsume(tok::colon,
894 diag::err_expected_colon_after_setter_name)) {
895 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000896 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000897 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000898 } else {
899 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000900 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000901 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000902 } else if (II->isStr("nonnull")) {
903 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
904 diagnoseRedundantPropertyNullability(*this, DS,
905 NullabilityKind::NonNull,
906 Tok.getLocation());
907 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
908 DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
909 } else if (II->isStr("nullable")) {
910 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
911 diagnoseRedundantPropertyNullability(*this, DS,
912 NullabilityKind::Nullable,
913 Tok.getLocation());
914 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
915 DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
916 } else if (II->isStr("null_unspecified")) {
917 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
918 diagnoseRedundantPropertyNullability(*this, DS,
919 NullabilityKind::Unspecified,
920 Tok.getLocation());
921 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
922 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000923 } else if (II->isStr("null_resettable")) {
924 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
925 diagnoseRedundantPropertyNullability(*this, DS,
926 NullabilityKind::Unspecified,
927 Tok.getLocation());
928 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
929 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
930
931 // Also set the null_resettable bit.
932 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
Chris Lattner825bca12008-10-20 07:39:53 +0000933 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000934 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000935 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000936 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000937 }
Mike Stump11289f42009-09-09 15:08:12 +0000938
Chris Lattner1db33542008-10-20 07:37:22 +0000939 if (Tok.isNot(tok::comma))
940 break;
Mike Stump11289f42009-09-09 15:08:12 +0000941
Chris Lattner1db33542008-10-20 07:37:22 +0000942 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000943 }
Mike Stump11289f42009-09-09 15:08:12 +0000944
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000945 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000946}
947
Steve Naroff09bf8152007-09-06 21:24:23 +0000948/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000949/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000950/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000951///
952/// objc-instance-method: '-'
953/// objc-class-method: '+'
954///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000955/// objc-method-attributes: [OBJC2]
956/// __attribute__((deprecated))
957///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000958Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000959 bool MethodDefinition) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000960 assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000961
Mike Stump11289f42009-09-09 15:08:12 +0000962 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000963 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000964 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000965 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000966 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000967 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000968 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000969}
970
971/// objc-selector:
972/// identifier
973/// one of
974/// enum struct union if else while do for switch case default
975/// break continue return goto asm sizeof typeof __alignof
976/// unsigned long const short volatile signed restrict _Complex
977/// in out inout bycopy byref oneway int char float double void _Bool
978///
Chris Lattner4f472a32009-04-11 18:13:45 +0000979IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000980
Chris Lattner5700fab2007-10-07 02:00:24 +0000981 switch (Tok.getKind()) {
982 default:
Craig Topper161e4db2014-05-21 06:02:52 +0000983 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000984 case tok::ampamp:
985 case tok::ampequal:
986 case tok::amp:
987 case tok::pipe:
988 case tok::tilde:
989 case tok::exclaim:
990 case tok::exclaimequal:
991 case tok::pipepipe:
992 case tok::pipeequal:
993 case tok::caret:
994 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000995 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +0000996 if (isLetter(ThisTok[0])) {
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000997 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
998 Tok.setKind(tok::identifier);
999 SelectorLoc = ConsumeToken();
1000 return II;
1001 }
Craig Topper161e4db2014-05-21 06:02:52 +00001002 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001003 }
1004
Chris Lattner5700fab2007-10-07 02:00:24 +00001005 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001006 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +00001007 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001008 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001009 case tok::kw_break:
1010 case tok::kw_case:
1011 case tok::kw_catch:
1012 case tok::kw_char:
1013 case tok::kw_class:
1014 case tok::kw_const:
1015 case tok::kw_const_cast:
1016 case tok::kw_continue:
1017 case tok::kw_default:
1018 case tok::kw_delete:
1019 case tok::kw_do:
1020 case tok::kw_double:
1021 case tok::kw_dynamic_cast:
1022 case tok::kw_else:
1023 case tok::kw_enum:
1024 case tok::kw_explicit:
1025 case tok::kw_export:
1026 case tok::kw_extern:
1027 case tok::kw_false:
1028 case tok::kw_float:
1029 case tok::kw_for:
1030 case tok::kw_friend:
1031 case tok::kw_goto:
1032 case tok::kw_if:
1033 case tok::kw_inline:
1034 case tok::kw_int:
1035 case tok::kw_long:
1036 case tok::kw_mutable:
1037 case tok::kw_namespace:
1038 case tok::kw_new:
1039 case tok::kw_operator:
1040 case tok::kw_private:
1041 case tok::kw_protected:
1042 case tok::kw_public:
1043 case tok::kw_register:
1044 case tok::kw_reinterpret_cast:
1045 case tok::kw_restrict:
1046 case tok::kw_return:
1047 case tok::kw_short:
1048 case tok::kw_signed:
1049 case tok::kw_sizeof:
1050 case tok::kw_static:
1051 case tok::kw_static_cast:
1052 case tok::kw_struct:
1053 case tok::kw_switch:
1054 case tok::kw_template:
1055 case tok::kw_this:
1056 case tok::kw_throw:
1057 case tok::kw_true:
1058 case tok::kw_try:
1059 case tok::kw_typedef:
1060 case tok::kw_typeid:
1061 case tok::kw_typename:
1062 case tok::kw_typeof:
1063 case tok::kw_union:
1064 case tok::kw_unsigned:
1065 case tok::kw_using:
1066 case tok::kw_virtual:
1067 case tok::kw_void:
1068 case tok::kw_volatile:
1069 case tok::kw_wchar_t:
1070 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +00001071 case tok::kw__Bool:
1072 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001073 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +00001074 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001075 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +00001076 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +00001077 }
Steve Naroff99264b42007-08-22 16:35:03 +00001078}
1079
Fariborz Jahanian83615522008-01-02 22:54:34 +00001080/// objc-for-collection-in: 'in'
1081///
Fariborz Jahanian3622e592008-01-04 23:04:08 +00001082bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001083 // FIXME: May have to do additional look-ahead to only allow for
1084 // valid tokens following an 'in'; such as an identifier, unary operators,
1085 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001086 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +00001087 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +00001088}
1089
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001090/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +00001091/// qualifier list and builds their bitmask representation in the input
1092/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +00001093///
1094/// objc-type-qualifiers:
1095/// objc-type-qualifier
1096/// objc-type-qualifiers objc-type-qualifier
1097///
Douglas Gregor813a0662015-06-19 18:14:38 +00001098/// objc-type-qualifier:
1099/// 'in'
1100/// 'out'
1101/// 'inout'
1102/// 'oneway'
1103/// 'bycopy'
1104/// 'byref'
1105/// 'nonnull'
1106/// 'nullable'
1107/// 'null_unspecified'
1108///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001109void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001110 Declarator::TheContext Context) {
1111 assert(Context == Declarator::ObjCParameterContext ||
1112 Context == Declarator::ObjCResultContext);
1113
Chris Lattner61511e12007-12-12 06:56:32 +00001114 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +00001115 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +00001116 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +00001117 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001118 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +00001119 }
1120
Chris Lattner5e530bc2007-12-27 19:57:00 +00001121 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +00001122 return;
Mike Stump11289f42009-09-09 15:08:12 +00001123
Chris Lattner61511e12007-12-12 06:56:32 +00001124 const IdentifierInfo *II = Tok.getIdentifierInfo();
1125 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001126 if (II != ObjCTypeQuals[i] ||
1127 NextToken().is(tok::less) ||
1128 NextToken().is(tok::coloncolon))
Chris Lattner61511e12007-12-12 06:56:32 +00001129 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001130
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001131 ObjCDeclSpec::ObjCDeclQualifier Qual;
Douglas Gregor813a0662015-06-19 18:14:38 +00001132 NullabilityKind Nullability;
Chris Lattner61511e12007-12-12 06:56:32 +00001133 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +00001134 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001135 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
1136 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
1137 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
1138 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
1139 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
1140 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Douglas Gregor813a0662015-06-19 18:14:38 +00001141
1142 case objc_nonnull:
1143 Qual = ObjCDeclSpec::DQ_CSNullability;
1144 Nullability = NullabilityKind::NonNull;
1145 break;
1146
1147 case objc_nullable:
1148 Qual = ObjCDeclSpec::DQ_CSNullability;
1149 Nullability = NullabilityKind::Nullable;
1150 break;
1151
1152 case objc_null_unspecified:
1153 Qual = ObjCDeclSpec::DQ_CSNullability;
1154 Nullability = NullabilityKind::Unspecified;
1155 break;
Chris Lattner61511e12007-12-12 06:56:32 +00001156 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001157
1158 // FIXME: Diagnose redundant specifiers.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001159 DS.setObjCDeclQualifier(Qual);
Douglas Gregor813a0662015-06-19 18:14:38 +00001160 if (Qual == ObjCDeclSpec::DQ_CSNullability)
1161 DS.setNullability(Tok.getLocation(), Nullability);
1162
Chris Lattner61511e12007-12-12 06:56:32 +00001163 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001164 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +00001165 break;
1166 }
Mike Stump11289f42009-09-09 15:08:12 +00001167
Chris Lattner61511e12007-12-12 06:56:32 +00001168 // If this wasn't a recognized qualifier, bail out.
1169 if (II) return;
1170 }
1171}
1172
John McCalla55902b2011-10-01 09:56:14 +00001173/// Take all the decl attributes out of the given list and add
1174/// them to the given attribute set.
1175static void takeDeclAttributes(ParsedAttributes &attrs,
1176 AttributeList *list) {
1177 while (list) {
1178 AttributeList *cur = list;
1179 list = cur->getNext();
1180
1181 if (!cur->isUsedAsTypeAttr()) {
1182 // Clear out the next pointer. We're really completely
1183 // destroying the internal invariants of the declarator here,
1184 // but it doesn't matter because we're done with it.
Craig Topper161e4db2014-05-21 06:02:52 +00001185 cur->setNext(nullptr);
John McCalla55902b2011-10-01 09:56:14 +00001186 attrs.add(cur);
1187 }
1188 }
1189}
1190
1191/// takeDeclAttributes - Take all the decl attributes from the given
1192/// declarator and add them to the given list.
1193static void takeDeclAttributes(ParsedAttributes &attrs,
1194 Declarator &D) {
1195 // First, take ownership of all attributes.
1196 attrs.getPool().takeAllFrom(D.getAttributePool());
1197 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
1198
1199 // Now actually move the attributes over.
1200 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
1201 takeDeclAttributes(attrs, D.getAttributes());
1202 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
1203 takeDeclAttributes(attrs,
1204 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
1205}
1206
Chris Lattner61511e12007-12-12 06:56:32 +00001207/// objc-type-name:
1208/// '(' objc-type-qualifiers[opt] type-name ')'
1209/// '(' objc-type-qualifiers[opt] ')'
1210///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001211ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001212 Declarator::TheContext context,
1213 ParsedAttributes *paramAttrs) {
1214 assert(context == Declarator::ObjCParameterContext ||
1215 context == Declarator::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +00001216 assert((paramAttrs != nullptr) ==
1217 (context == Declarator::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +00001218
Chris Lattner0ef13522007-10-09 17:51:17 +00001219 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +00001220
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001221 BalancedDelimiterTracker T(*this, tok::l_paren);
1222 T.consumeOpen();
1223
Chris Lattner2ebb1782008-08-23 01:48:03 +00001224 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001225 ObjCDeclContextSwitch ObjCDC(*this);
1226
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001227 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001228 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +00001229
John McCallba7bf592010-08-24 05:47:05 +00001230 ParsedType Ty;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001231 if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
John McCalla55902b2011-10-01 09:56:14 +00001232 // Parse an abstract declarator.
1233 DeclSpec declSpec(AttrFactory);
1234 declSpec.setObjCQualifiers(&DS);
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001235 DeclSpecContext dsContext = DSC_normal;
1236 if (context == Declarator::ObjCResultContext)
1237 dsContext = DSC_objc_method_result;
1238 ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
Fariborz Jahanianb6499eb2012-05-29 21:52:45 +00001239 declSpec.SetRangeEnd(Tok.getLocation());
John McCalla55902b2011-10-01 09:56:14 +00001240 Declarator declarator(declSpec, context);
1241 ParseDeclarator(declarator);
1242
1243 // If that's not invalid, extract a type.
1244 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001245 // Map a nullability specifier to a context-sensitive keyword attribute.
1246 bool addedToDeclSpec = false;
1247 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1248 addContextSensitiveTypeNullability(*this, declarator,
1249 DS.getNullability(),
1250 DS.getNullabilityLoc(),
1251 addedToDeclSpec);
1252
John McCalla55902b2011-10-01 09:56:14 +00001253 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1254 if (!type.isInvalid())
1255 Ty = type.get();
1256
1257 // If we're parsing a parameter, steal all the decl attributes
1258 // and add them to the decl spec.
1259 if (context == Declarator::ObjCParameterContext)
1260 takeDeclAttributes(*paramAttrs, declarator);
1261 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001262 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001263
Steve Naroff90255b42008-10-21 14:15:04 +00001264 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001265 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001266 else if (Tok.getLocation() == TypeStartLoc) {
1267 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001268 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001269 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001270 } else {
1271 // Otherwise, we found *something*, but didn't get a ')' in the right
1272 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001273 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001274 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001275 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001276}
1277
1278/// objc-method-decl:
1279/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001280/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001281/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001282/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001283///
1284/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001285/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001286/// objc-keyword-selector objc-keyword-decl
1287///
1288/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001289/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1290/// objc-selector ':' objc-keyword-attributes[opt] identifier
1291/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1292/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001293///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001294/// objc-parmlist:
1295/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001296///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001297/// objc-parms:
1298/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001299///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001300/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001301/// , ...
1302///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001303/// objc-keyword-attributes: [OBJC2]
1304/// __attribute__((unused))
1305///
John McCall48871652010-08-21 09:40:31 +00001306Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001307 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001308 tok::ObjCKeywordKind MethodImplKind,
1309 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001310 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001311
Douglas Gregor636a61e2010-04-07 00:21:17 +00001312 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001313 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001314 /*ReturnType=*/ ParsedType());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001315 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001316 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001317 }
1318
Chris Lattner2ebb1782008-08-23 01:48:03 +00001319 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001320 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001321 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001322 if (Tok.is(tok::l_paren))
Craig Topper161e4db2014-05-21 06:02:52 +00001323 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext,
1324 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001325
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001326 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001327 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001328 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001329 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001330
Douglas Gregor636a61e2010-04-07 00:21:17 +00001331 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001332 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001333 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001334 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001335 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001336 }
1337
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001338 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001339 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001340 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001341
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001342 // An unnamed colon is valid.
1343 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001344 Diag(Tok, diag::err_expected_selector_for_method)
1345 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001346 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001347 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001348 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001349 }
Mike Stump11289f42009-09-09 15:08:12 +00001350
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001351 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001352 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001353 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001354 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001355 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001356
Chris Lattner5700fab2007-10-07 02:00:24 +00001357 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001358 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001359 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001360 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001361 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001362 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001363 methodAttrs.getList(), MethodImplKind,
1364 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001365 PD.complete(Result);
1366 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001367 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001368
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001369 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001370 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001371 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001372 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1373 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001374
1375 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001376 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001377 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001378 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001379
Chris Lattner5700fab2007-10-07 02:00:24 +00001380 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001381 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001382 break;
Mike Stump11289f42009-09-09 15:08:12 +00001383
John McCallba7bf592010-08-24 05:47:05 +00001384 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001385 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001386 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1387 Declarator::ObjCParameterContext,
1388 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001389
Chris Lattner5700fab2007-10-07 02:00:24 +00001390 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001391 // Regardless, collect all the attributes we've parsed so far.
Craig Topper161e4db2014-05-21 06:02:52 +00001392 ArgInfo.ArgAttrs = nullptr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001393 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001394 MaybeParseGNUAttributes(paramAttrs);
1395 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001396 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001397
Douglas Gregor45879692010-07-08 23:37:41 +00001398 // Code completion for the next piece of the selector.
1399 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001400 KeyIdents.push_back(SelIdent);
1401 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1402 mType == tok::minus,
1403 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001404 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001405 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001406 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001407 }
1408
Chris Lattner0ef13522007-10-09 17:51:17 +00001409 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001410 Diag(Tok, diag::err_expected)
1411 << tok::identifier; // missing argument name.
Chris Lattner5700fab2007-10-07 02:00:24 +00001412 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001413 }
Mike Stump11289f42009-09-09 15:08:12 +00001414
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001415 ArgInfo.Name = Tok.getIdentifierInfo();
1416 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001417 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001418
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001419 ArgInfos.push_back(ArgInfo);
1420 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001421 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001422
John McCall084e83d2011-03-24 11:26:52 +00001423 // Make sure the attributes persist.
1424 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1425
Douglas Gregor95887f92010-07-08 23:20:03 +00001426 // Code completion for the next piece of the selector.
1427 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001428 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1429 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001430 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001431 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001432 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001433 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001434 }
1435
Chris Lattner5700fab2007-10-07 02:00:24 +00001436 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001437 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001438 if (!SelIdent && Tok.isNot(tok::colon))
1439 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001440 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001441 SourceLocation ColonLoc = Tok.getLocation();
1442 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001443 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1444 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1445 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001446 }
1447 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001448 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001449 }
Mike Stump11289f42009-09-09 15:08:12 +00001450
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001451 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001452 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001453 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001454 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001455 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001456 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001457 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001458 ConsumeToken();
1459 break;
1460 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001461 if (!cStyleParamWarned) {
1462 Diag(Tok, diag::warn_cstyle_param);
1463 cStyleParamWarned = true;
1464 }
John McCall084e83d2011-03-24 11:26:52 +00001465 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001466 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001467 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001468 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1469 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001470 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001471 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001472 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1473 ParmDecl.getIdentifierLoc(),
1474 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001475 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001476 }
Mike Stump11289f42009-09-09 15:08:12 +00001477
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001478 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001479 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001480 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001481 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001482
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001483 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001484 return nullptr;
1485
Chris Lattner5700fab2007-10-07 02:00:24 +00001486 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1487 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001488 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001489 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001490 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001491 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001492 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001493 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001494 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001495
John McCall28a6aea2009-11-04 02:18:39 +00001496 PD.complete(Result);
1497 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001498}
1499
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001500/// objc-protocol-refs:
1501/// '<' identifier-list '>'
1502///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001503bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001504ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1505 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001506 bool WarnOnDeclarations, bool ForObjCContainer,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001507 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001508 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001509
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001510 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001511
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001512 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001513
Chris Lattner3bbae002008-07-26 04:03:38 +00001514 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001515 if (Tok.is(tok::code_completion)) {
1516 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1517 ProtocolIdents.size());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001518 cutOffParsing();
1519 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001520 }
1521
Chris Lattner3bbae002008-07-26 04:03:38 +00001522 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001523 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001524 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001525 return true;
1526 }
1527 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1528 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001529 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001530 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001531
Alp Toker383d2c42014-01-01 03:08:43 +00001532 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001533 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001534 }
Mike Stump11289f42009-09-09 15:08:12 +00001535
Chris Lattner3bbae002008-07-26 04:03:38 +00001536 // Consume the '>'.
Douglas Gregor85f3f952015-07-07 03:57:15 +00001537 if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true,
1538 /*ObjCGenericList=*/false))
Chris Lattner3bbae002008-07-26 04:03:38 +00001539 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001540
Chris Lattner3bbae002008-07-26 04:03:38 +00001541 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001542 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Chris Lattner3bbae002008-07-26 04:03:38 +00001543 &ProtocolIdents[0], ProtocolIdents.size(),
1544 Protocols);
1545 return false;
1546}
1547
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001548/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1549/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001550bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001551 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001552 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001553 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001554 SmallVector<Decl *, 8> ProtocolDecl;
1555 SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001556 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001557 false,
Douglas Gregor3a001f42010-11-19 17:10:50 +00001558 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001559 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1560 ProtocolLocs.data(), LAngleLoc);
1561 if (EndProtoLoc.isValid())
1562 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001563 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001564}
1565
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001566void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1567 BalancedDelimiterTracker &T,
1568 SmallVectorImpl<Decl *> &AllIvarDecls,
1569 bool RBraceMissing) {
1570 if (!RBraceMissing)
1571 T.consumeClose();
1572
1573 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1574 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1575 Actions.ActOnObjCContainerFinishDefinition();
1576 // Call ActOnFields() even if we don't have any decls. This is useful
1577 // for code rewriting tools that need to be aware of the empty list.
1578 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1579 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001580 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001581}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001582
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001583/// objc-class-instance-variables:
1584/// '{' objc-instance-variable-decl-list[opt] '}'
1585///
1586/// objc-instance-variable-decl-list:
1587/// objc-visibility-spec
1588/// objc-instance-variable-decl ';'
1589/// ';'
1590/// objc-instance-variable-decl-list objc-visibility-spec
1591/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1592/// objc-instance-variable-decl-list ';'
1593///
1594/// objc-visibility-spec:
1595/// @private
1596/// @protected
1597/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001598/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001599///
1600/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001601/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001602///
John McCall48871652010-08-21 09:40:31 +00001603void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001604 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001605 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001606 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001607 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001608
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001609 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001610 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001611
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001612 BalancedDelimiterTracker T(*this, tok::l_brace);
1613 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001614 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001615 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001616 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001617
Steve Naroff00433d32007-08-21 21:17:12 +00001618 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001619 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001620 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001621 continue;
1622 }
Mike Stump11289f42009-09-09 15:08:12 +00001623
Steve Naroff00433d32007-08-21 21:17:12 +00001624 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001625 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001626 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001627 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001628 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001629 }
1630
Steve Naroff7c348172007-08-23 18:16:40 +00001631 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001632 case tok::objc_private:
1633 case tok::objc_public:
1634 case tok::objc_protected:
1635 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001636 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001637 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001638 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001639
1640 case tok::objc_end:
1641 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001642 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1643 Tok.setKind(tok::at);
1644 Tok.setLength(1);
1645 PP.EnterToken(Tok);
1646 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1647 T, AllIvarDecls, true);
1648 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001649
1650 default:
1651 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1652 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001653 }
1654 }
Mike Stump11289f42009-09-09 15:08:12 +00001655
Douglas Gregor48d46252010-01-13 21:54:15 +00001656 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001657 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001658 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001659 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001660 }
John McCallcfefb6d2009-11-03 02:38:08 +00001661
Benjamin Kramera39beb92014-09-03 11:06:10 +00001662 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1663 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1664 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001665 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001666 Decl *Field = Actions.ActOnIvar(
1667 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1668 FD.BitfieldSize, visibility);
1669 Actions.ActOnObjCContainerFinishDefinition();
1670 if (Field)
1671 AllIvarDecls.push_back(Field);
1672 FD.complete(Field);
1673 };
John McCallcfefb6d2009-11-03 02:38:08 +00001674
Chris Lattnera12405b2008-04-10 06:46:29 +00001675 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001676 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001677 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001678
Chris Lattner0ef13522007-10-09 17:51:17 +00001679 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001680 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001681 } else {
1682 Diag(Tok, diag::err_expected_semi_decl_list);
1683 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001684 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001685 }
1686 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001687 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1688 T, AllIvarDecls, false);
Steve Naroff00433d32007-08-21 21:17:12 +00001689 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001690}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001691
1692/// objc-protocol-declaration:
1693/// objc-protocol-definition
1694/// objc-protocol-forward-reference
1695///
1696/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00001697/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00001698/// objc-protocol-refs[opt]
1699/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00001700/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001701///
1702/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00001703/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001704///
James Dennett1355bd12012-06-11 06:19:40 +00001705/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001706/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001707/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001708Parser::DeclGroupPtrTy
1709Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1710 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001711 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001712 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1713 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001714
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001715 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001716 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001717 cutOffParsing();
Douglas Gregorf6102672012-01-01 21:23:57 +00001718 return DeclGroupPtrTy();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001719 }
1720
Nico Weber69a79142013-04-04 00:15:10 +00001721 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00001722
Chris Lattner0ef13522007-10-09 17:51:17 +00001723 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001724 Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
Douglas Gregorf6102672012-01-01 21:23:57 +00001725 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001726 }
1727 // Save the protocol name, then consume it.
1728 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1729 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001730
Alp Toker383d2c42014-01-01 03:08:43 +00001731 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001732 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001733 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001734 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001735 }
Mike Stump11289f42009-09-09 15:08:12 +00001736
Erik Verbruggenf9887852011-12-08 09:58:43 +00001737 CheckNestedObjCContexts(AtLoc);
1738
Chris Lattner0ef13522007-10-09 17:51:17 +00001739 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001740 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001741 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1742
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001743 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001744 while (1) {
1745 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001746 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001747 Diag(Tok, diag::err_expected) << tok::identifier;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001748 SkipUntil(tok::semi);
Douglas Gregorf6102672012-01-01 21:23:57 +00001749 return DeclGroupPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001750 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001751 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1752 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001753 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001754
Chris Lattner0ef13522007-10-09 17:51:17 +00001755 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001756 break;
1757 }
1758 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00001759 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
Douglas Gregorf6102672012-01-01 21:23:57 +00001760 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001761
Steve Naroff93eb5f12007-10-10 17:32:04 +00001762 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001763 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001764 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001765 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001766 }
Mike Stump11289f42009-09-09 15:08:12 +00001767
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001768 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001769 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001770
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001771 SmallVector<Decl *, 8> ProtocolRefs;
1772 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001773 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001774 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001775 LAngleLoc, EndProtoLoc))
Douglas Gregorf6102672012-01-01 21:23:57 +00001776 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001777
John McCall48871652010-08-21 09:40:31 +00001778 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001779 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001780 ProtocolRefs.data(),
1781 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001782 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001783 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001784
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00001785 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00001786 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001787}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001788
1789/// objc-implementation:
1790/// objc-class-implementation-prologue
1791/// objc-category-implementation-prologue
1792///
1793/// objc-class-implementation-prologue:
1794/// @implementation identifier objc-superclass[opt]
1795/// objc-class-instance-variables[opt]
1796///
1797/// objc-category-implementation-prologue:
1798/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001799Parser::DeclGroupPtrTy
1800Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001801 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1802 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001803 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001804 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001805
Douglas Gregor49c22a72009-11-18 16:26:39 +00001806 // Code completion after '@implementation'.
1807 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001808 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001809 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001810 return DeclGroupPtrTy();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001811 }
1812
Nico Weber69a79142013-04-04 00:15:10 +00001813 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00001814
Chris Lattner0ef13522007-10-09 17:51:17 +00001815 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001816 Diag(Tok, diag::err_expected)
1817 << tok::identifier; // missing class or category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001818 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001819 }
1820 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001821 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001822 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00001823 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001824
Douglas Gregor85f3f952015-07-07 03:57:15 +00001825 // Neither a type parameter list nor a list of protocol references is
1826 // permitted here. Parse and diagnose them.
1827 if (Tok.is(tok::less)) {
1828 SourceLocation lAngleLoc, rAngleLoc;
1829 SmallVector<IdentifierLocPair, 8> protocolIdents;
1830 SourceLocation diagLoc = Tok.getLocation();
1831 if (parseObjCTypeParamListOrProtocolRefs(lAngleLoc, protocolIdents,
1832 rAngleLoc)) {
1833 Diag(diagLoc, diag::err_objc_parameterized_implementation)
1834 << SourceRange(diagLoc, PrevTokLocation);
1835 } else if (lAngleLoc.isValid()) {
1836 Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
1837 << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
1838 }
1839 }
1840
Mike Stump11289f42009-09-09 15:08:12 +00001841 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001842 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001843 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001844 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001845 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001846
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001847 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001848 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001849 cutOffParsing();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001850 return DeclGroupPtrTy();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001851 }
1852
Chris Lattner0ef13522007-10-09 17:51:17 +00001853 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001854 categoryId = Tok.getIdentifierInfo();
1855 categoryLoc = ConsumeToken();
1856 } else {
Alp Tokerec543272013-12-24 09:48:30 +00001857 Diag(Tok, diag::err_expected)
1858 << tok::identifier; // missing category name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001859 return DeclGroupPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001860 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001861 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001862 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001863 SkipUntil(tok::r_paren); // don't stop at ';'
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001864 return DeclGroupPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001865 }
1866 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00001867 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1868 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1869 AttributeFactory attr;
1870 DeclSpec DS(attr);
1871 (void)ParseObjCProtocolQualifiers(DS);
1872 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001873 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001874 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001875 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001876
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001877 } else {
1878 // We have a class implementation
1879 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001880 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00001881 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001882 // We have a super class
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001883 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001884 Diag(Tok, diag::err_expected)
1885 << tok::identifier; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001886 return DeclGroupPtrTy();
1887 }
1888 superClassId = Tok.getIdentifierInfo();
1889 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001890 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001891 ObjCImpDecl = Actions.ActOnStartClassImplementation(
1892 AtLoc, nameId, nameLoc,
1893 superClassId, superClassLoc);
1894
1895 if (Tok.is(tok::l_brace)) // we have ivars
1896 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00001897 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1898 Diag(Tok, diag::err_unexpected_protocol_qualifier);
1899 // try to recover.
1900 AttributeFactory attr;
1901 DeclSpec DS(attr);
1902 (void)ParseObjCProtocolQualifiers(DS);
1903 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001904 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001905 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001906
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001907 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001908
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001909 {
1910 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00001911 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001912 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001913 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001914 MaybeParseMicrosoftAttributes(attrs);
1915 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1916 DeclGroupRef DG = DGP.get();
1917 DeclsInGroup.append(DG.begin(), DG.end());
1918 }
1919 }
1920 }
1921
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001922 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001923}
Steve Naroff33a1e802007-10-29 21:38:07 +00001924
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00001925Parser::DeclGroupPtrTy
1926Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001927 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1928 "ParseObjCAtEndDeclaration(): Expected @end");
1929 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001930 if (CurParsedObjCImpl)
1931 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001932 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00001933 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00001934 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001935 return DeclGroupPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001936}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001937
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001938Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1939 if (!Finished) {
1940 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00001941 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001942 P.Diag(P.Tok, diag::err_objc_missing_end)
1943 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1944 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1945 << Sema::OCK_Implementation;
1946 }
1947 }
Craig Topper161e4db2014-05-21 06:02:52 +00001948 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001949 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001950}
1951
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001952void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1953 assert(!Finished);
1954 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1955 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001956 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1957 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001958
1959 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
1960
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001961 if (HasCFunction)
1962 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1963 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
1964 false/*c-functions*/);
1965
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001966 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001967 for (LateParsedObjCMethodContainer::iterator
1968 I = LateParsedObjCMethods.begin(),
1969 E = LateParsedObjCMethods.end(); I != E; ++I)
1970 delete *I;
1971 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001972
1973 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00001974}
1975
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001976/// compatibility-alias-decl:
1977/// @compatibility_alias alias-name class-name ';'
1978///
John McCall48871652010-08-21 09:40:31 +00001979Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001980 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1981 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1982 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001983 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001984 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00001985 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001986 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001987 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1988 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001989 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001990 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00001991 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001992 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001993 IdentifierInfo *classId = Tok.getIdentifierInfo();
1994 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00001995 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00001996 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1997 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001998}
1999
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002000/// property-synthesis:
2001/// @synthesize property-ivar-list ';'
2002///
2003/// property-ivar-list:
2004/// property-ivar
2005/// property-ivar-list ',' property-ivar
2006///
2007/// property-ivar:
2008/// identifier
2009/// identifier '=' identifier
2010///
John McCall48871652010-08-21 09:40:31 +00002011Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002012 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00002013 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002014 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00002015
Douglas Gregor88e72a02009-11-18 19:45:45 +00002016 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00002017 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002018 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002019 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002020 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002021 }
2022
Douglas Gregor88e72a02009-11-18 19:45:45 +00002023 if (Tok.isNot(tok::identifier)) {
2024 Diag(Tok, diag::err_synthesized_property_name);
2025 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002026 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00002027 }
Craig Topper161e4db2014-05-21 06:02:52 +00002028
2029 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002030 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2031 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002032 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00002033 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002034 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00002035 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002036 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002037 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002038 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002039 }
2040
Chris Lattner0ef13522007-10-09 17:51:17 +00002041 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002042 Diag(Tok, diag::err_expected) << tok::identifier;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002043 break;
2044 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002045 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002046 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002047 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002048 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002049 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002050 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002051 break;
2052 ConsumeToken(); // consume ','
2053 }
Alp Toker383d2c42014-01-01 03:08:43 +00002054 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00002055 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002056}
2057
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002058/// property-dynamic:
2059/// @dynamic property-list
2060///
2061/// property-list:
2062/// identifier
2063/// property-list ',' identifier
2064///
John McCall48871652010-08-21 09:40:31 +00002065Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002066 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2067 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002068 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002069 while (true) {
2070 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002071 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002072 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002073 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002074 }
2075
2076 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002077 Diag(Tok, diag::err_expected) << tok::identifier;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002078 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002079 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002080 }
2081
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002082 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2083 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002084 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
Craig Topper161e4db2014-05-21 06:02:52 +00002085 propertyId, nullptr, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002086
Chris Lattner0ef13522007-10-09 17:51:17 +00002087 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002088 break;
2089 ConsumeToken(); // consume ','
2090 }
Alp Toker383d2c42014-01-01 03:08:43 +00002091 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00002092 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002093}
Mike Stump11289f42009-09-09 15:08:12 +00002094
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002095/// objc-throw-statement:
2096/// throw expression[opt];
2097///
John McCalldadc5752010-08-24 06:29:42 +00002098StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2099 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002100 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00002101 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00002102 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002103 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002104 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002105 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002106 }
2107 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00002108 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00002109 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002110 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002111}
2112
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002113/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002114/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002115///
John McCalldadc5752010-08-24 06:29:42 +00002116StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002117Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002118 ConsumeToken(); // consume synchronized
2119 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002120 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002121 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002122 }
John McCalld9bb7432011-07-27 21:50:02 +00002123
2124 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002125 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00002126 ExprResult operand(ParseExpression());
2127
2128 if (Tok.is(tok::r_paren)) {
2129 ConsumeParen(); // ')'
2130 } else {
2131 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002132 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00002133
2134 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002135 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002136 }
John McCalld9bb7432011-07-27 21:50:02 +00002137
2138 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002139 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00002140 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002141 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002142 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002143 }
Steve Naroffd9c26072008-06-04 20:36:13 +00002144
John McCalld9bb7432011-07-27 21:50:02 +00002145 // Check the @synchronized operand now.
2146 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002147 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002148
John McCalld9bb7432011-07-27 21:50:02 +00002149 // Parse the compound statement within a new scope.
2150 ParseScope bodyScope(this, Scope::DeclScope);
2151 StmtResult body(ParseCompoundStatementBody());
2152 bodyScope.Exit();
2153
2154 // If there was a semantic or parse error earlier with the
2155 // operand, fail now.
2156 if (operand.isInvalid())
2157 return StmtError();
2158
2159 if (body.isInvalid())
2160 body = Actions.ActOnNullStmt(Tok.getLocation());
2161
2162 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002163}
2164
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002165/// objc-try-catch-statement:
2166/// @try compound-statement objc-catch-list[opt]
2167/// @try compound-statement objc-catch-list[opt] @finally compound-statement
2168///
2169/// objc-catch-list:
2170/// @catch ( parameter-declaration ) compound-statement
2171/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2172/// catch-parameter-declaration:
2173/// parameter-declaration
2174/// '...' [OBJC2]
2175///
John McCalldadc5752010-08-24 06:29:42 +00002176StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002177 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002178
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002179 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00002180 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002181 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002182 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002183 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00002184 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00002185 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002186 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00002187 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002188 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002189 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002190 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002191
Chris Lattner0ef13522007-10-09 17:51:17 +00002192 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002193 // At this point, we need to lookahead to determine if this @ is the start
2194 // of an @catch or @finally. We don't want to consume the @ token if this
2195 // is an @try or @encode or something else.
2196 Token AfterAt = GetLookAheadToken(1);
2197 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2198 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2199 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002200
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002201 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002202 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002203 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002204 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002205 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002206 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00002207 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002208 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002209 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002210 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00002211 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002212 ParseDeclarator(ParmDecl);
2213
Douglas Gregore11ee112010-04-23 23:01:43 +00002214 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002215 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002216 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002217 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002218 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002219
Steve Naroff65a00892009-04-07 22:56:58 +00002220 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002221
Steve Naroff65a00892009-04-07 22:56:58 +00002222 if (Tok.is(tok::r_paren))
2223 RParenLoc = ConsumeParen();
2224 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002225 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002226
John McCalldadc5752010-08-24 06:29:42 +00002227 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002228 if (Tok.is(tok::l_brace))
2229 CatchBody = ParseCompoundStatementBody();
2230 else
Alp Tokerec543272013-12-24 09:48:30 +00002231 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002232 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002233 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00002234
John McCalldadc5752010-08-24 06:29:42 +00002235 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00002236 RParenLoc,
2237 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002238 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002239 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002240 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002241
Steve Naroffe6016792008-02-05 21:27:35 +00002242 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002243 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2244 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002245 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002246 }
2247 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002248 } else {
2249 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002250 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002251 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002252
John McCalldadc5752010-08-24 06:29:42 +00002253 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002254 if (Tok.is(tok::l_brace))
2255 FinallyBody = ParseCompoundStatementBody();
2256 else
Alp Tokerec543272013-12-24 09:48:30 +00002257 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002258 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002259 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002260 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002261 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002262 catch_or_finally_seen = true;
2263 break;
2264 }
2265 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002266 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002267 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002268 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002269 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002270
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002271 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002272 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002273 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002274}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002275
John McCall31168b02011-06-15 23:02:42 +00002276/// objc-autoreleasepool-statement:
2277/// @autoreleasepool compound-statement
2278///
2279StmtResult
2280Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2281 ConsumeToken(); // consume autoreleasepool
2282 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002283 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002284 return StmtError();
2285 }
2286 // Enter a scope to hold everything within the compound stmt. Compound
2287 // statements can always hold declarations.
2288 ParseScope BodyScope(this, Scope::DeclScope);
2289
2290 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2291
2292 BodyScope.Exit();
2293 if (AutoreleasePoolBody.isInvalid())
2294 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2295 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002296 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002297}
2298
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002299/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2300/// for later parsing.
2301void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
2302 LexedMethod* LM = new LexedMethod(this, MDecl);
2303 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2304 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002305 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002306 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002307 if (Tok.is(tok::kw_try)) {
2308 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002309 if (Tok.is(tok::colon)) {
2310 Toks.push_back(Tok);
2311 ConsumeToken();
2312 while (Tok.isNot(tok::l_brace)) {
2313 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2314 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2315 }
2316 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002317 Toks.push_back(Tok); // also store '{'
2318 }
2319 else if (Tok.is(tok::colon)) {
2320 ConsumeToken();
2321 while (Tok.isNot(tok::l_brace)) {
2322 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2323 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2324 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002325 Toks.push_back(Tok); // also store '{'
2326 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002327 ConsumeBrace();
2328 // Consume everything up to (and including) the matching right brace.
2329 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002330 while (Tok.is(tok::kw_catch)) {
2331 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2332 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2333 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002334}
2335
Steve Naroff09bf8152007-09-06 21:24:23 +00002336/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002337///
John McCall48871652010-08-21 09:40:31 +00002338Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002339 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002340
John McCallfaf5fb42010-08-26 23:41:50 +00002341 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2342 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002343
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002344 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002345 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002346 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002347 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002348 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002349 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002350 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002351 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002352
Steve Naroffbb875722007-11-11 19:54:21 +00002353 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002354 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002355 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002356
Steve Naroffbb875722007-11-11 19:54:21 +00002357 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002358 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002359
Steve Naroffbb875722007-11-11 19:54:21 +00002360 // If we didn't find the '{', bail out.
2361 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002362 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002363 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002364
2365 if (!MDecl) {
2366 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002367 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002368 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002369 }
2370
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002371 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002372 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002373 assert (CurParsedObjCImpl
2374 && "ParseObjCMethodDefinition - Method out of @implementation");
2375 // Consume the tokens and store them for later parsing.
2376 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002377 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002378}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002379
John McCalldadc5752010-08-24 06:29:42 +00002380StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002381 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002382 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002383 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002384 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002385 }
2386
2387 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002388 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002389
2390 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002391 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002392
2393 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002394 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002395
2396 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2397 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002398
2399 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2400 getLangOpts().DebuggerSupport) {
2401 SkipUntil(tok::semi);
2402 return Actions.ActOnNullStmt(Tok.getLocation());
2403 }
2404
John McCalldadc5752010-08-24 06:29:42 +00002405 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002406 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002407 // If the expression is invalid, skip ahead to the next semicolon. Not
2408 // doing this opens us up to the possibility of infinite loops if
2409 // ParseExpression does not consume any tokens.
2410 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002411 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002412 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002413
Steve Naroffe6016792008-02-05 21:27:35 +00002414 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002415 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002416 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002417}
2418
John McCalldadc5752010-08-24 06:29:42 +00002419ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002420 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002421 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002422 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002423 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002424 return ExprError();
2425
Ted Kremeneke65b0862012-03-06 20:05:56 +00002426 case tok::minus:
2427 case tok::plus: {
2428 tok::TokenKind Kind = Tok.getKind();
2429 SourceLocation OpLoc = ConsumeToken();
2430
2431 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002432 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002433 switch (Kind) {
2434 case tok::minus: Symbol = "-"; break;
2435 case tok::plus: Symbol = "+"; break;
2436 default: llvm_unreachable("missing unary operator case");
2437 }
2438 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2439 << Symbol;
2440 return ExprError();
2441 }
2442
2443 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2444 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002445 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002446 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002447 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002448
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002449 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002450 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002451 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002452
2453 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002454 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002455 }
2456
Chris Lattnere002fbe2007-12-12 01:04:12 +00002457 case tok::string_literal: // primary-expression: string-literal
2458 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002459 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002460
2461 case tok::char_constant:
2462 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2463
2464 case tok::numeric_constant:
2465 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2466
2467 case tok::kw_true: // Objective-C++, etc.
2468 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2469 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2470 case tok::kw_false: // Objective-C++, etc.
2471 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2472 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2473
2474 case tok::l_square:
2475 // Objective-C array literal
2476 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2477
2478 case tok::l_brace:
2479 // Objective-C dictionary literal
2480 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2481
Patrick Beard0caa3942012-04-19 00:25:12 +00002482 case tok::l_paren:
2483 // Objective-C boxed expression
2484 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2485
Chris Lattnere002fbe2007-12-12 01:04:12 +00002486 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002487 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002488 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002489
Chris Lattner197a3012008-08-05 06:19:09 +00002490 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2491 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002492 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002493 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002494 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002495 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002496 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002497 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002498 const char *str = nullptr;
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002499 if (GetLookAheadToken(1).is(tok::l_brace)) {
2500 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2501 str =
2502 ch == 't' ? "try"
2503 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002504 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002505 }
2506 if (str) {
2507 SourceLocation kwLoc = Tok.getLocation();
2508 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2509 FixItHint::CreateReplacement(kwLoc, str));
2510 }
2511 else
2512 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2513 }
Chris Lattner197a3012008-08-05 06:19:09 +00002514 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002515 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002516}
2517
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002518/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002519///
2520/// This routine parses the receiver of a message send in
2521/// Objective-C++ either as a type or as an expression. Note that this
2522/// routine must not be called to parse a send to 'super', since it
2523/// has no way to return such a result.
2524///
2525/// \param IsExpr Whether the receiver was parsed as an expression.
2526///
2527/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2528/// IsExpr is true), the parsed expression. If the receiver was parsed
2529/// as a type (\c IsExpr is false), the parsed type.
2530///
2531/// \returns True if an error occurred during parsing or semantic
2532/// analysis, in which case the arguments do not have valid
2533/// values. Otherwise, returns false for a successful parse.
2534///
2535/// objc-receiver: [C++]
2536/// 'super' [not parsed here]
2537/// expression
2538/// simple-type-specifier
2539/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002540bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002541 InMessageExpressionRAIIObject InMessage(*this, true);
2542
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002543 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2544 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002545 TryAnnotateTypeOrScopeToken();
2546
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002547 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002548 // objc-receiver:
2549 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002550 // Make sure any typos in the receiver are corrected or diagnosed, so that
2551 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2552 // only the things that are valid ObjC receivers?
2553 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002554 if (Receiver.isInvalid())
2555 return true;
2556
2557 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002558 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002559 return false;
2560 }
2561
2562 // objc-receiver:
2563 // typename-specifier
2564 // simple-type-specifier
2565 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002566 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002567 ParseCXXSimpleTypeSpecifier(DS);
2568
2569 if (Tok.is(tok::l_paren)) {
2570 // If we see an opening parentheses at this point, we are
2571 // actually parsing an expression that starts with a
2572 // function-style cast, e.g.,
2573 //
2574 // postfix-expression:
2575 // simple-type-specifier ( expression-list [opt] )
2576 // typename-specifier ( expression-list [opt] )
2577 //
2578 // Parse the remainder of this case, then the (optional)
2579 // postfix-expression suffix, followed by the (optional)
2580 // right-hand side of the binary expression. We have an
2581 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002582 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002583 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002584 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002585 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002586 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002587 if (Receiver.isInvalid())
2588 return true;
2589
2590 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002591 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002592 return false;
2593 }
2594
2595 // We have a class message. Turn the simple-type-specifier or
2596 // typename-specifier we parsed into a type and parse the
2597 // remainder of the class message.
2598 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002599 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002600 if (Type.isInvalid())
2601 return true;
2602
2603 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002604 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002605 return false;
2606}
2607
Douglas Gregor990ccac2010-05-31 14:40:22 +00002608/// \brief Determine whether the parser is currently referring to a an
2609/// Objective-C message send, using a simplified heuristic to avoid overhead.
2610///
2611/// This routine will only return true for a subset of valid message-send
2612/// expressions.
2613bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002614 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002615 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002616 return GetLookAheadToken(1).is(tok::identifier) &&
2617 GetLookAheadToken(2).is(tok::identifier);
2618}
2619
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002620bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002621 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002622 InMessageExpression)
2623 return false;
2624
2625
2626 ParsedType Type;
2627
2628 if (Tok.is(tok::annot_typename))
2629 Type = getTypeAnnotation(Tok);
2630 else if (Tok.is(tok::identifier))
2631 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2632 getCurScope());
2633 else
2634 return false;
2635
2636 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2637 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002638 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002639 if (Tok.is(tok::identifier))
2640 TryAnnotateTypeOrScopeToken();
2641
2642 return Tok.is(tok::annot_typename);
2643 }
2644 }
2645
2646 return false;
2647}
2648
Mike Stump11289f42009-09-09 15:08:12 +00002649/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002650/// '[' objc-receiver objc-message-args ']'
2651///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002652/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002653/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002654/// expression
2655/// class-name
2656/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002657///
John McCalldadc5752010-08-24 06:29:42 +00002658ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002659 assert(Tok.is(tok::l_square) && "'[' expected");
2660 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2661
Douglas Gregora817a192010-05-27 23:06:34 +00002662 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002663 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002664 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002665 return ExprError();
2666 }
2667
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002668 InMessageExpressionRAIIObject InMessage(*this, true);
2669
David Blaikiebbafb8a2012-03-11 07:00:24 +00002670 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002671 // We completely separate the C and C++ cases because C++ requires
2672 // more complicated (read: slower) parsing.
2673
2674 // Handle send to super.
2675 // FIXME: This doesn't benefit from the same typo-correction we
2676 // get in Objective-C.
2677 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002678 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002679 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002680 ParsedType(), nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002681
2682 // Parse the receiver, which is either a type or an expression.
2683 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00002684 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002685 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002686 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002687 return ExprError();
2688 }
2689
2690 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002691 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2692 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002693 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002694
2695 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002696 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00002697 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00002698 }
2699
2700 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002701 IdentifierInfo *Name = Tok.getIdentifierInfo();
2702 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002703 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002704 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002705 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002706 NextToken().is(tok::period),
2707 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002708 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002709 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
Craig Topper161e4db2014-05-21 06:02:52 +00002710 ParsedType(), nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002711
John McCallfaf5fb42010-08-26 23:41:50 +00002712 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002713 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002714 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002715 return ExprError();
2716 }
2717
Douglas Gregore5798dc2010-04-21 20:38:13 +00002718 ConsumeToken(); // the type name
2719
2720 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00002721 ReceiverType, nullptr);
2722
John McCallfaf5fb42010-08-26 23:41:50 +00002723 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002724 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002725 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002726 }
Chris Lattner8f697062008-01-25 18:59:06 +00002727 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002728
2729 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00002730 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002731 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002732 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002733 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00002734 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002735
John McCallba7bf592010-08-24 05:47:05 +00002736 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002737 ParsedType(), Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00002738}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002739
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002740/// \brief Parse the remainder of an Objective-C message following the
2741/// '[' objc-receiver.
2742///
2743/// This routine handles sends to super, class messages (sent to a
2744/// class name), and instance messages (sent to an object), and the
2745/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2746/// ReceiverExpr, respectively. Only one of these parameters may have
2747/// a valid value.
2748///
2749/// \param LBracLoc The location of the opening '['.
2750///
2751/// \param SuperLoc If this is a send to 'super', the location of the
2752/// 'super' keyword that indicates a send to the superclass.
2753///
2754/// \param ReceiverType If this is a class message, the type of the
2755/// class we are sending a message to.
2756///
2757/// \param ReceiverExpr If this is an instance message, the expression
2758/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002759///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002760/// objc-message-args:
2761/// objc-selector
2762/// objc-keywordarg-list
2763///
2764/// objc-keywordarg-list:
2765/// objc-keywordarg
2766/// objc-keywordarg-list objc-keywordarg
2767///
Mike Stump11289f42009-09-09 15:08:12 +00002768/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002769/// selector-name[opt] ':' objc-keywordexpr
2770///
2771/// objc-keywordexpr:
2772/// nonempty-expr-list
2773///
2774/// nonempty-expr-list:
2775/// assignment-expression
2776/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002777///
John McCalldadc5752010-08-24 06:29:42 +00002778ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002779Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002780 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002781 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00002782 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002783 InMessageExpressionRAIIObject InMessage(*this, true);
2784
Steve Naroffeae65032009-11-07 02:08:14 +00002785 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002786 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002787 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002788 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002789 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002790 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002791 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002792 else
John McCallb268a282010-08-23 23:25:46 +00002793 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002794 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002795 cutOffParsing();
2796 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00002797 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002798
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002799 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002800 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002801 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002802
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002803 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002804 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002805 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00002806
Chris Lattner0ef13522007-10-09 17:51:17 +00002807 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002808 while (1) {
2809 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002810 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002811 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00002812
Alp Toker383d2c42014-01-01 03:08:43 +00002813 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00002814 // We must manually skip to a ']', otherwise the expression skipper will
2815 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2816 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002817 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002818 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002819 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002820
Mike Stump11289f42009-09-09 15:08:12 +00002821 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002822
2823 if (Tok.is(tok::code_completion)) {
2824 if (SuperLoc.isValid())
2825 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002826 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002827 /*AtArgumentEpression=*/true);
2828 else if (ReceiverType)
2829 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002830 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002831 /*AtArgumentEpression=*/true);
2832 else
2833 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002834 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002835 /*AtArgumentEpression=*/true);
2836
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002837 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002838 return ExprError();
2839 }
2840
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00002841 ExprResult Expr;
2842 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
2843 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2844 Expr = ParseBraceInitializer();
2845 } else
2846 Expr = ParseAssignmentExpression();
2847
2848 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002849 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002850 // We must manually skip to a ']', otherwise the expression skipper will
2851 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2852 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002853 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002854 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00002855 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002856
Steve Naroff486760a2007-09-17 20:25:27 +00002857 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002858 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002859
Douglas Gregor1b605f72009-11-19 01:08:35 +00002860 // Code completion after each argument.
2861 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002862 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002863 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002864 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002865 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002866 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002867 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002868 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002869 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002870 else
John McCallb268a282010-08-23 23:25:46 +00002871 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00002872 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002873 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002874 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002875 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002876 }
2877
Steve Naroff486760a2007-09-17 20:25:27 +00002878 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002879 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002880 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002881 break;
2882 // We have a selector or a colon, continue parsing.
2883 }
2884 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002885 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002886 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002887 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002888 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00002889 if (Tok.is(tok::colon))
2890 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002891 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00002892 if (Tok.is(tok::colon)) {
2893 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
2894 FixItHint::CreateRemoval(commaLoc);
2895 }
Chris Lattner197a3012008-08-05 06:19:09 +00002896 // We must manually skip to a ']', otherwise the expression skipper will
2897 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2898 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002899 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002900 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002901 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002902
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002903 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002904 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002905 }
2906 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00002907 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002908
Chris Lattner197a3012008-08-05 06:19:09 +00002909 // We must manually skip to a ']', otherwise the expression skipper will
2910 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2911 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002912 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002913 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002914 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002915
Chris Lattner0ef13522007-10-09 17:51:17 +00002916 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00002917 Diag(Tok, diag::err_expected)
2918 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00002919 // We must manually skip to a ']', otherwise the expression skipper will
2920 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2921 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002922 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002923 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002924 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00002925
Chris Lattner8f697062008-01-25 18:59:06 +00002926 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002927
Steve Naroffe61bfa82007-10-05 18:42:47 +00002928 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002929 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00002930 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002931 KeyLocs.push_back(Loc);
2932 }
Chris Lattner5700fab2007-10-07 02:00:24 +00002933 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002934
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002935 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002936 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002937 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002938 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002939 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002940 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00002941 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002942 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002943}
2944
John McCalldadc5752010-08-24 06:29:42 +00002945ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2946 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002947 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002948
Chris Lattnere002fbe2007-12-12 01:04:12 +00002949 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2950 // expressions. At this point, we know that the only valid thing that starts
2951 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002952 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00002953 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00002954 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002955 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002956
Chris Lattnere002fbe2007-12-12 01:04:12 +00002957 while (Tok.is(tok::at)) {
2958 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002959
Sebastian Redlc13f2682008-12-09 20:22:58 +00002960 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002961 if (!isTokenStringLiteral())
2962 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002963
John McCalldadc5752010-08-24 06:29:42 +00002964 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002965 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002966 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002967
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002968 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002969 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002970
Nico Webera7c7e602012-12-31 00:28:03 +00002971 return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
2972 AtStrings.size());
Anders Carlsson76f4a902007-08-21 17:43:55 +00002973}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002974
Ted Kremeneke65b0862012-03-06 20:05:56 +00002975/// ParseObjCBooleanLiteral -
2976/// objc-scalar-literal : '@' boolean-keyword
2977/// ;
2978/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2979/// ;
2980ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2981 bool ArgValue) {
2982 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
2983 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2984}
2985
2986/// ParseObjCCharacterLiteral -
2987/// objc-scalar-literal : '@' character-literal
2988/// ;
2989ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2990 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2991 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002992 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002993 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002994 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002995 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002996}
2997
2998/// ParseObjCNumericLiteral -
2999/// objc-scalar-literal : '@' scalar-literal
3000/// ;
3001/// scalar-literal : | numeric-constant /* any numeric constant. */
3002/// ;
3003ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3004 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3005 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003006 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003007 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003008 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003009 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003010}
3011
Patrick Beard0caa3942012-04-19 00:25:12 +00003012/// ParseObjCBoxedExpr -
3013/// objc-box-expression:
3014/// @( assignment-expression )
3015ExprResult
3016Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3017 if (Tok.isNot(tok::l_paren))
3018 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3019
3020 BalancedDelimiterTracker T(*this, tok::l_paren);
3021 T.consumeOpen();
3022 ExprResult ValueExpr(ParseAssignmentExpression());
3023 if (T.consumeClose())
3024 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00003025
3026 if (ValueExpr.isInvalid())
3027 return ExprError();
3028
Patrick Beard0caa3942012-04-19 00:25:12 +00003029 // Wrap the sub-expression in a parenthesized expression, to distinguish
3030 // a boxed expression from a literal.
3031 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003032 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00003033 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003034 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00003035}
3036
Ted Kremeneke65b0862012-03-06 20:05:56 +00003037ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00003038 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003039 ConsumeBracket(); // consume the l_square.
3040
3041 while (Tok.isNot(tok::r_square)) {
3042 // Parse list of array element expressions (all must be id types).
3043 ExprResult Res(ParseAssignmentExpression());
3044 if (Res.isInvalid()) {
3045 // We must manually skip to a ']', otherwise the expression skipper will
3046 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3047 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003048 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003049 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003050 }
3051
3052 // Parse the ellipsis that indicates a pack expansion.
3053 if (Tok.is(tok::ellipsis))
3054 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
3055 if (Res.isInvalid())
3056 return true;
3057
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003058 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003059
3060 if (Tok.is(tok::comma))
3061 ConsumeToken(); // Eat the ','.
3062 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00003063 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3064 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003065 }
3066 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramerf0623432012-08-23 22:51:59 +00003067 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00003068 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003069}
3070
3071ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3072 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3073 ConsumeBrace(); // consume the l_square.
3074 while (Tok.isNot(tok::r_brace)) {
3075 // Parse the comma separated key : value expressions.
3076 ExprResult KeyExpr;
3077 {
3078 ColonProtectionRAIIObject X(*this);
3079 KeyExpr = ParseAssignmentExpression();
3080 if (KeyExpr.isInvalid()) {
3081 // We must manually skip to a '}', otherwise the expression skipper will
3082 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3083 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003084 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003085 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003086 }
3087 }
3088
Alp Toker383d2c42014-01-01 03:08:43 +00003089 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003090 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00003091 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003092 }
3093
3094 ExprResult ValueExpr(ParseAssignmentExpression());
3095 if (ValueExpr.isInvalid()) {
3096 // We must manually skip to a '}', otherwise the expression skipper will
3097 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3098 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003099 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003100 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003101 }
3102
3103 // Parse the ellipsis that designates this as a pack expansion.
3104 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00003105 if (getLangOpts().CPlusPlus)
3106 TryConsumeToken(tok::ellipsis, EllipsisLoc);
3107
Ted Kremeneke65b0862012-03-06 20:05:56 +00003108 // We have a valid expression. Collect it in a vector so we can
3109 // build the argument list.
3110 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00003111 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00003112 };
3113 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00003114
3115 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00003116 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3117 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003118 }
3119 SourceLocation EndLoc = ConsumeBrace();
3120
3121 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00003122 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
3123 Elements.data(), Elements.size());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003124}
3125
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003126/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00003127/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00003128ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003129Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00003130 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003131
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003132 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003133
Chris Lattner197a3012008-08-05 06:19:09 +00003134 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003135 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3136
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003137 BalancedDelimiterTracker T(*this, tok::l_paren);
3138 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003139
Douglas Gregor220cac52009-02-18 17:45:20 +00003140 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003141
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003142 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003143
Douglas Gregor220cac52009-02-18 17:45:20 +00003144 if (Ty.isInvalid())
3145 return ExprError();
3146
Nico Webera7c7e602012-12-31 00:28:03 +00003147 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3148 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003149}
Anders Carlssone01493d2007-08-23 15:25:28 +00003150
3151/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00003152/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00003153ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003154Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00003155 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003156
Chris Lattner197a3012008-08-05 06:19:09 +00003157 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003158 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3159
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003160 BalancedDelimiterTracker T(*this, tok::l_paren);
3161 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003162
Chris Lattner197a3012008-08-05 06:19:09 +00003163 if (Tok.isNot(tok::identifier))
Alp Tokerec543272013-12-24 09:48:30 +00003164 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003165
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003166 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00003167 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003168
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003169 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00003170
Nico Webera7c7e602012-12-31 00:28:03 +00003171 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3172 T.getOpenLocation(), ProtoIdLoc,
3173 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00003174}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003175
3176/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003177/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003178ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003179 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003180
Chris Lattner197a3012008-08-05 06:19:09 +00003181 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003182 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3183
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003184 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003185 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003186
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003187 BalancedDelimiterTracker T(*this, tok::l_paren);
3188 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003189 bool HasOptionalParen = Tok.is(tok::l_paren);
3190 if (HasOptionalParen)
3191 ConsumeParen();
3192
Douglas Gregor67c692c2010-08-26 15:07:07 +00003193 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003194 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003195 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003196 return ExprError();
3197 }
3198
Chris Lattner4f472a32009-04-11 18:13:45 +00003199 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003200 if (!SelIdent && // missing selector name.
3201 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003202 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003203
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003204 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003205
Steve Naroff152dd812007-12-05 22:21:29 +00003206 unsigned nColons = 0;
3207 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003208 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003209 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003210 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003211 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003212 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3213 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003214 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003215
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003216 if (Tok.is(tok::r_paren))
3217 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003218
3219 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003220 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003221 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003222 return ExprError();
3223 }
3224
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003225 // Check for another keyword selector.
3226 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003227 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003228 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003229 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003230 break;
3231 }
Steve Naroff152dd812007-12-05 22:21:29 +00003232 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003233 if (HasOptionalParen && Tok.is(tok::r_paren))
3234 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003235 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003236 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003237 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3238 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003239 T.getCloseLocation(),
3240 !HasOptionalParen);
Gabor Greif24032f12007-10-19 15:38:32 +00003241 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003242
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003243void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3244 // MCDecl might be null due to error in method or c-function prototype, etc.
3245 Decl *MCDecl = LM.D;
3246 bool skip = MCDecl &&
3247 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3248 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3249 if (skip)
3250 return;
3251
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003252 // Save the current token position.
3253 SourceLocation OrigLoc = Tok.getLocation();
3254
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003255 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
3256 // Append the current token at the end of the new token stream so that it
3257 // doesn't get lost.
3258 LM.Toks.push_back(Tok);
3259 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
3260
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003261 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003262 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003263
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003264 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3265 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003266 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003267 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003268 parseMethod
3269 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
3270 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003271
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003272 // Tell the actions module that we have entered a method or c-function definition
3273 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003274 if (parseMethod)
3275 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3276 else
3277 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003278 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003279 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003280 else {
3281 if (Tok.is(tok::colon))
3282 ParseConstructorInitializer(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003283 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003284 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00003285
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003286 if (Tok.getLocation() != OrigLoc) {
3287 // Due to parsing error, we either went over the cached tokens or
3288 // there are still cached tokens left. If it's the latter case skip the
3289 // leftover tokens.
3290 // Since this is an uncommon situation that should be avoided, use the
3291 // expensive isBeforeInTranslationUnit call.
3292 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3293 OrigLoc))
3294 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3295 ConsumeAnyToken();
3296 }
3297
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003298 return;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003299}