blob: 6a680907ccefe5211577b9750d6c53832371da93 [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();
David Blaikie0403cb12016-01-15 23:43:25 +000053 return nullptr;
Douglas Gregorf48706c2009-12-07 09:27:33 +000054 }
Craig Topper161e4db2014-05-21 06:02:52 +000055
56 Decl *SingleDecl = nullptr;
Steve Naroff7c348172007-08-23 18:16:40 +000057 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000058 case tok::objc_class:
59 return ParseObjCAtClassDeclaration(AtLoc);
John McCall53fa7142010-12-24 02:08:15 +000060 case tok::objc_interface: {
John McCall084e83d2011-03-24 11:26:52 +000061 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000062 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
63 break;
John McCall53fa7142010-12-24 02:08:15 +000064 }
65 case tok::objc_protocol: {
John McCall084e83d2011-03-24 11:26:52 +000066 ParsedAttributes attrs(AttrFactory);
Douglas Gregorf6102672012-01-01 21:23:57 +000067 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall53fa7142010-12-24 02:08:15 +000068 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000069 case tok::objc_implementation:
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +000070 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000071 case tok::objc_end:
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +000072 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000073 case tok::objc_compatibility_alias:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000074 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
75 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000076 case tok::objc_synthesize:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000077 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
78 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000079 case tok::objc_dynamic:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000080 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
81 break;
Douglas Gregorc50d4922012-12-11 22:11:52 +000082 case tok::objc_import:
Sean Callanan87596492014-12-09 23:47:56 +000083 if (getLangOpts().Modules || getLangOpts().DebuggerSupport)
Douglas Gregor0bf886d2012-01-03 18:24:14 +000084 return ParseModuleImport(AtLoc);
Fariborz Jahaniana773d082014-03-26 22:02:43 +000085 Diag(AtLoc, diag::err_atimport);
86 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000087 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerce90ef52008-08-23 02:02:23 +000088 default:
89 Diag(AtLoc, diag::err_unexpected_at);
90 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000091 SingleDecl = nullptr;
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000092 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000093 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000094 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000095}
96
Richard Smith3df3f1d2015-11-03 01:19:56 +000097/// Class to handle popping type parameters when leaving the scope.
98class Parser::ObjCTypeParamListScope {
99 Sema &Actions;
100 Scope *S;
101 ObjCTypeParamList *Params;
102public:
103 ObjCTypeParamListScope(Sema &Actions, Scope *S)
104 : Actions(Actions), S(S), Params(nullptr) {}
105 ~ObjCTypeParamListScope() {
106 leave();
107 }
108 void enter(ObjCTypeParamList *P) {
109 assert(!Params);
110 Params = P;
111 }
112 void leave() {
113 if (Params)
114 Actions.popObjCTypeParamList(S, Params);
115 Params = nullptr;
116 }
117};
118
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000119///
Mike Stump11289f42009-09-09 15:08:12 +0000120/// objc-class-declaration:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000121/// '@' 'class' objc-class-forward-decl (',' objc-class-forward-decl)* ';'
122///
123/// objc-class-forward-decl:
124/// identifier objc-type-parameter-list[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000125///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000126Parser::DeclGroupPtrTy
127Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000128 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000129 SmallVector<IdentifierInfo *, 8> ClassNames;
130 SmallVector<SourceLocation, 8> ClassLocs;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000131 SmallVector<ObjCTypeParamList *, 8> ClassTypeParams;
Mike Stump11289f42009-09-09 15:08:12 +0000132
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000133 while (1) {
Nico Weber69a79142013-04-04 00:15:10 +0000134 MaybeSkipAttributes(tok::objc_class);
Chris Lattner0ef13522007-10-09 17:51:17 +0000135 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000136 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000137 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000138 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000139 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000140 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +0000141 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000142 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000143
Douglas Gregor85f3f952015-07-07 03:57:15 +0000144 // Parse the optional objc-type-parameter-list.
145 ObjCTypeParamList *TypeParams = nullptr;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000146 if (Tok.is(tok::less))
Douglas Gregor85f3f952015-07-07 03:57:15 +0000147 TypeParams = parseObjCTypeParamList();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000148 ClassTypeParams.push_back(TypeParams);
Alp Toker383d2c42014-01-01 03:08:43 +0000149 if (!TryConsumeToken(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000150 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000151 }
Mike Stump11289f42009-09-09 15:08:12 +0000152
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000153 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +0000154 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@class"))
Craig Topper161e4db2014-05-21 06:02:52 +0000155 return Actions.ConvertDeclToDeclGroup(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000156
Ted Kremeneka26da852009-11-17 23:12:20 +0000157 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
158 ClassLocs.data(),
Douglas Gregor85f3f952015-07-07 03:57:15 +0000159 ClassTypeParams,
Ted Kremeneka26da852009-11-17 23:12:20 +0000160 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000161}
162
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000163void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
164{
165 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
166 if (ock == Sema::OCK_None)
167 return;
168
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000169 Decl *Decl = Actions.getObjCDeclContext();
170 if (CurParsedObjCImpl) {
171 CurParsedObjCImpl->finish(AtLoc);
172 } else {
173 Actions.ActOnAtEnd(getCurScope(), AtLoc);
174 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000175 Diag(AtLoc, diag::err_objc_missing_end)
176 << FixItHint::CreateInsertion(AtLoc, "@end\n");
177 if (Decl)
178 Diag(Decl->getLocStart(), diag::note_objc_container_start)
179 << (int) ock;
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000180}
181
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000182///
183/// objc-interface:
184/// objc-class-interface-attributes[opt] objc-class-interface
185/// objc-category-interface
186///
187/// objc-class-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000188/// '@' 'interface' identifier objc-type-parameter-list[opt]
189/// objc-superclass[opt] objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000190/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000191/// objc-interface-decl-list
192/// @end
193///
194/// objc-category-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000195/// '@' 'interface' identifier objc-type-parameter-list[opt]
196/// '(' identifier[opt] ')' objc-protocol-refs[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000197/// objc-interface-decl-list
198/// @end
199///
200/// objc-superclass:
Douglas Gregore9d95f12015-07-07 03:57:35 +0000201/// ':' identifier objc-type-arguments[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000202///
203/// objc-class-interface-attributes:
204/// __attribute__((visibility("default")))
205/// __attribute__((visibility("hidden")))
206/// __attribute__((deprecated))
207/// __attribute__((unavailable))
208/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardacfbe9e2012-04-06 18:12:22 +0000209/// __attribute__((objc_root_class))
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000210///
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000211Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000212 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000213 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000214 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000215 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000216 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000217
Douglas Gregor49c22a72009-11-18 16:26:39 +0000218 // Code completion after '@interface'.
219 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000220 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000221 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000222 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000223 }
224
Nico Weber69a79142013-04-04 00:15:10 +0000225 MaybeSkipAttributes(tok::objc_interface);
Nico Weber04e213b2013-04-03 17:36:11 +0000226
Chris Lattner0ef13522007-10-09 17:51:17 +0000227 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000228 Diag(Tok, diag::err_expected)
229 << tok::identifier; // missing class or category name.
Craig Topper161e4db2014-05-21 06:02:52 +0000230 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000231 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000232
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000233 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000234 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000235 SourceLocation nameLoc = ConsumeToken();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000236
237 // Parse the objc-type-parameter-list or objc-protocol-refs. For the latter
238 // case, LAngleLoc will be valid and ProtocolIdents will capture the
239 // protocol references (that have not yet been resolved).
240 SourceLocation LAngleLoc, EndProtoLoc;
241 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
242 ObjCTypeParamList *typeParameterList = nullptr;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000243 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
244 if (Tok.is(tok::less))
245 typeParameterList = parseObjCTypeParamListOrProtocolRefs(
246 typeParamScope, LAngleLoc, ProtocolIdents, EndProtoLoc);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000247
248 if (Tok.is(tok::l_paren) &&
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000249 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000250
251 BalancedDelimiterTracker T(*this, tok::l_paren);
252 T.consumeOpen();
253
254 SourceLocation categoryLoc;
Craig Topper161e4db2014-05-21 06:02:52 +0000255 IdentifierInfo *categoryId = nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000256 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000257 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000258 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000259 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000260 }
261
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000262 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000263 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000264 categoryId = Tok.getIdentifierInfo();
265 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000266 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000267 else if (!getLangOpts().ObjC2) {
Alp Tokerec543272013-12-24 09:48:30 +0000268 Diag(Tok, diag::err_expected)
269 << tok::identifier; // missing category name.
Craig Topper161e4db2014-05-21 06:02:52 +0000270 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000271 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000272
273 T.consumeClose();
274 if (T.getCloseLocation().isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000275 return nullptr;
276
Douglas Gregor0c254a02011-09-23 19:19:41 +0000277 if (!attrs.empty()) { // categories don't support attributes.
278 Diag(nameLoc, diag::err_objc_no_attributes_on_category);
279 attrs.clear();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000280 }
Douglas Gregor0c254a02011-09-23 19:19:41 +0000281
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000282 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000283 assert(LAngleLoc.isInvalid() && "Cannot have already parsed protocols");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000284 SmallVector<Decl *, 8> ProtocolRefs;
285 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000286 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000287 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000288 LAngleLoc, EndProtoLoc,
289 /*consumeLastToken=*/true))
Craig Topper161e4db2014-05-21 06:02:52 +0000290 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000291
John McCall48871652010-08-21 09:40:31 +0000292 Decl *CategoryType =
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000293 Actions.ActOnStartCategoryInterface(AtLoc,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000294 nameId, nameLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000295 typeParameterList,
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000296 categoryId, categoryLoc,
297 ProtocolRefs.data(),
298 ProtocolRefs.size(),
299 ProtocolLocs.data(),
300 EndProtoLoc);
Fariborz Jahanian9a3b2692011-08-19 18:02:47 +0000301
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000302 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000303 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000304
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000305 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000306
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000307 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000308 }
309 // Parse a class interface.
Craig Topper161e4db2014-05-21 06:02:52 +0000310 IdentifierInfo *superClassId = nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000311 SourceLocation superClassLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000312 SourceLocation typeArgsLAngleLoc;
313 SmallVector<ParsedType, 4> typeArgs;
314 SourceLocation typeArgsRAngleLoc;
315 SmallVector<Decl *, 4> protocols;
316 SmallVector<SourceLocation, 4> protocolLocs;
Chris Lattner0ef13522007-10-09 17:51:17 +0000317 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000318 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000319
320 // Code completion of superclass names.
321 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000322 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000323 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000324 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000325 }
326
Chris Lattner0ef13522007-10-09 17:51:17 +0000327 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000328 Diag(Tok, diag::err_expected)
329 << tok::identifier; // missing super class name.
Craig Topper161e4db2014-05-21 06:02:52 +0000330 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000331 }
332 superClassId = Tok.getIdentifierInfo();
333 superClassLoc = ConsumeToken();
Douglas Gregore9d95f12015-07-07 03:57:35 +0000334
335 // Type arguments for the superclass or protocol conformances.
336 if (Tok.is(tok::less)) {
David Blaikieefdccaa2016-01-15 23:43:34 +0000337 parseObjCTypeArgsOrProtocolQualifiers(
338 nullptr, typeArgsLAngleLoc, typeArgs, typeArgsRAngleLoc, LAngleLoc,
339 protocols, protocolLocs, EndProtoLoc,
340 /*consumeLastToken=*/true,
341 /*warnOnIncompleteProtocols=*/true);
Douglas Gregore9d95f12015-07-07 03:57:35 +0000342 }
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000343 }
Douglas Gregor85f3f952015-07-07 03:57:15 +0000344
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000345 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000346 if (LAngleLoc.isValid()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000347 if (!ProtocolIdents.empty()) {
348 // We already parsed the protocols named when we thought we had a
349 // type parameter list. Translate them into actual protocol references.
350 for (const auto &pair : ProtocolIdents) {
351 protocolLocs.push_back(pair.second);
352 }
353 Actions.FindProtocolDeclaration(/*WarnOnDeclarations=*/true,
354 /*ForObjCContainer=*/true,
Craig Toppera9247eb2015-10-22 04:59:56 +0000355 ProtocolIdents, protocols);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000356 }
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000357 } else if (protocols.empty() && Tok.is(tok::less) &&
358 ParseObjCProtocolReferences(protocols, protocolLocs, true, true,
359 LAngleLoc, EndProtoLoc,
360 /*consumeLastToken=*/true)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000361 return nullptr;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000362 }
Mike Stump11289f42009-09-09 15:08:12 +0000363
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000364 if (Tok.isNot(tok::less))
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000365 Actions.ActOnTypedefedProtocols(protocols, superClassId, superClassLoc);
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000366
John McCall48871652010-08-21 09:40:31 +0000367 Decl *ClsType =
Douglas Gregore9d95f12015-07-07 03:57:35 +0000368 Actions.ActOnStartClassInterface(getCurScope(), AtLoc, nameId, nameLoc,
369 typeParameterList, superClassId,
370 superClassLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000371 typeArgs,
372 SourceRange(typeArgsLAngleLoc,
373 typeArgsRAngleLoc),
374 protocols.data(), protocols.size(),
375 protocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000376 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000377
Chris Lattner0ef13522007-10-09 17:51:17 +0000378 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000379 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000380
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000381 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000382
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000383 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000384}
385
Douglas Gregor813a0662015-06-19 18:14:38 +0000386/// Add an attribute for a context-sensitive type nullability to the given
387/// declarator.
388static void addContextSensitiveTypeNullability(Parser &P,
389 Declarator &D,
390 NullabilityKind nullability,
391 SourceLocation nullabilityLoc,
392 bool &addedToDeclSpec) {
393 // Create the attribute.
394 auto getNullabilityAttr = [&]() -> AttributeList * {
Douglas Gregorbec595a2015-06-19 18:27:45 +0000395 return D.getAttributePool().create(
396 P.getNullabilityKeyword(nullability),
397 SourceRange(nullabilityLoc),
398 nullptr, SourceLocation(),
399 nullptr, 0,
400 AttributeList::AS_ContextSensitiveKeyword);
Douglas Gregor813a0662015-06-19 18:14:38 +0000401 };
402
403 if (D.getNumTypeObjects() > 0) {
404 // Add the attribute to the declarator chunk nearest the declarator.
405 auto nullabilityAttr = getNullabilityAttr();
406 DeclaratorChunk &chunk = D.getTypeObject(0);
407 nullabilityAttr->setNext(chunk.getAttrListRef());
408 chunk.getAttrListRef() = nullabilityAttr;
409 } else if (!addedToDeclSpec) {
410 // Otherwise, just put it on the declaration specifiers (if one
411 // isn't there already).
412 D.getMutableDeclSpec().addAttributes(getNullabilityAttr());
413 addedToDeclSpec = true;
414 }
415}
416
Douglas Gregor85f3f952015-07-07 03:57:15 +0000417/// Parse an Objective-C type parameter list, if present, or capture
418/// the locations of the protocol identifiers for a list of protocol
419/// references.
420///
421/// objc-type-parameter-list:
422/// '<' objc-type-parameter (',' objc-type-parameter)* '>'
423///
424/// objc-type-parameter:
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000425/// objc-type-parameter-variance? identifier objc-type-parameter-bound[opt]
Douglas Gregor85f3f952015-07-07 03:57:15 +0000426///
427/// objc-type-parameter-bound:
428/// ':' type-name
429///
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000430/// objc-type-parameter-variance:
431/// '__covariant'
432/// '__contravariant'
433///
Douglas Gregor85f3f952015-07-07 03:57:15 +0000434/// \param lAngleLoc The location of the starting '<'.
435///
436/// \param protocolIdents Will capture the list of identifiers, if the
437/// angle brackets contain a list of protocol references rather than a
438/// type parameter list.
439///
440/// \param rAngleLoc The location of the ending '>'.
441ObjCTypeParamList *Parser::parseObjCTypeParamListOrProtocolRefs(
Richard Smith3df3f1d2015-11-03 01:19:56 +0000442 ObjCTypeParamListScope &Scope, SourceLocation &lAngleLoc,
443 SmallVectorImpl<IdentifierLocPair> &protocolIdents,
444 SourceLocation &rAngleLoc, bool mayBeProtocolList) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000445 assert(Tok.is(tok::less) && "Not at the beginning of a type parameter list");
446
447 // Within the type parameter list, don't treat '>' as an operator.
448 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
449
450 // Local function to "flush" the protocol identifiers, turning them into
451 // type parameters.
452 SmallVector<Decl *, 4> typeParams;
453 auto makeProtocolIdentsIntoTypeParameters = [&]() {
Douglas Gregore83b9562015-07-07 03:57:53 +0000454 unsigned index = 0;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000455 for (const auto &pair : protocolIdents) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000456 DeclResult typeParam = Actions.actOnObjCTypeParam(
David Blaikieefdccaa2016-01-15 23:43:34 +0000457 getCurScope(), ObjCTypeParamVariance::Invariant, SourceLocation(),
458 index++, pair.first, pair.second, SourceLocation(), nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000459 if (typeParam.isUsable())
460 typeParams.push_back(typeParam.get());
461 }
462
463 protocolIdents.clear();
464 mayBeProtocolList = false;
465 };
466
467 bool invalid = false;
468 lAngleLoc = ConsumeToken();
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000469
Douglas Gregor85f3f952015-07-07 03:57:15 +0000470 do {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000471 // Parse the variance, if any.
472 SourceLocation varianceLoc;
473 ObjCTypeParamVariance variance = ObjCTypeParamVariance::Invariant;
474 if (Tok.is(tok::kw___covariant) || Tok.is(tok::kw___contravariant)) {
475 variance = Tok.is(tok::kw___covariant)
476 ? ObjCTypeParamVariance::Covariant
477 : ObjCTypeParamVariance::Contravariant;
478 varianceLoc = ConsumeToken();
479
480 // Once we've seen a variance specific , we know this is not a
481 // list of protocol references.
482 if (mayBeProtocolList) {
483 // Up until now, we have been queuing up parameters because they
484 // might be protocol references. Turn them into parameters now.
485 makeProtocolIdentsIntoTypeParameters();
486 }
487 }
488
Douglas Gregor85f3f952015-07-07 03:57:15 +0000489 // Parse the identifier.
490 if (!Tok.is(tok::identifier)) {
491 // Code completion.
492 if (Tok.is(tok::code_completion)) {
493 // FIXME: If these aren't protocol references, we'll need different
494 // completions.
Craig Topper883dd332015-12-24 23:58:11 +0000495 Actions.CodeCompleteObjCProtocolReferences(protocolIdents);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000496 cutOffParsing();
497
498 // FIXME: Better recovery here?.
499 return nullptr;
500 }
501
502 Diag(Tok, diag::err_objc_expected_type_parameter);
503 invalid = true;
504 break;
505 }
506
507 IdentifierInfo *paramName = Tok.getIdentifierInfo();
508 SourceLocation paramLoc = ConsumeToken();
509
510 // If there is a bound, parse it.
511 SourceLocation colonLoc;
512 TypeResult boundType;
513 if (TryConsumeToken(tok::colon, colonLoc)) {
514 // Once we've seen a bound, we know this is not a list of protocol
515 // references.
516 if (mayBeProtocolList) {
517 // Up until now, we have been queuing up parameters because they
518 // might be protocol references. Turn them into parameters now.
519 makeProtocolIdentsIntoTypeParameters();
520 }
521
522 // type-name
523 boundType = ParseTypeName();
524 if (boundType.isInvalid())
525 invalid = true;
526 } else if (mayBeProtocolList) {
527 // If this could still be a protocol list, just capture the identifier.
528 // We don't want to turn it into a parameter.
529 protocolIdents.push_back(std::make_pair(paramName, paramLoc));
530 continue;
531 }
532
533 // Create the type parameter.
David Blaikieefdccaa2016-01-15 23:43:34 +0000534 DeclResult typeParam = Actions.actOnObjCTypeParam(
535 getCurScope(), variance, varianceLoc, typeParams.size(), paramName,
536 paramLoc, colonLoc, boundType.isUsable() ? boundType.get() : nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000537 if (typeParam.isUsable())
538 typeParams.push_back(typeParam.get());
539 } while (TryConsumeToken(tok::comma));
540
541 // Parse the '>'.
542 if (invalid) {
543 SkipUntil(tok::greater, tok::at, StopBeforeMatch);
544 if (Tok.is(tok::greater))
545 ConsumeToken();
546 } else if (ParseGreaterThanInTemplateList(rAngleLoc,
547 /*ConsumeLastToken=*/true,
548 /*ObjCGenericList=*/true)) {
549 Diag(lAngleLoc, diag::note_matching) << "'<'";
550 SkipUntil({tok::greater, tok::greaterequal, tok::at, tok::minus,
551 tok::minus, tok::plus, tok::colon, tok::l_paren, tok::l_brace,
552 tok::comma, tok::semi },
553 StopBeforeMatch);
554 if (Tok.is(tok::greater))
555 ConsumeToken();
556 }
557
558 if (mayBeProtocolList) {
559 // A type parameter list must be followed by either a ':' (indicating the
560 // presence of a superclass) or a '(' (indicating that this is a category
561 // or extension). This disambiguates between an objc-type-parameter-list
562 // and a objc-protocol-refs.
563 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_paren)) {
564 // Returning null indicates that we don't have a type parameter list.
565 // The results the caller needs to handle the protocol references are
566 // captured in the reference parameters already.
567 return nullptr;
568 }
569
570 // We have a type parameter list that looks like a list of protocol
571 // references. Turn that parameter list into type parameters.
572 makeProtocolIdentsIntoTypeParameters();
573 }
574
Richard Smith3df3f1d2015-11-03 01:19:56 +0000575 // Form the type parameter list and enter its scope.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000576 ObjCTypeParamList *list = Actions.actOnObjCTypeParamList(
577 getCurScope(),
578 lAngleLoc,
579 typeParams,
580 rAngleLoc);
Richard Smith3df3f1d2015-11-03 01:19:56 +0000581 Scope.enter(list);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000582
583 // Clear out the angle locations; they're used by the caller to indicate
584 // whether there are any protocol references.
585 lAngleLoc = SourceLocation();
586 rAngleLoc = SourceLocation();
Akira Hatanaka8ebd5802015-12-16 06:25:38 +0000587 return invalid ? nullptr : list;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000588}
589
590/// Parse an objc-type-parameter-list.
591ObjCTypeParamList *Parser::parseObjCTypeParamList() {
592 SourceLocation lAngleLoc;
593 SmallVector<IdentifierLocPair, 1> protocolIdents;
594 SourceLocation rAngleLoc;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000595
596 ObjCTypeParamListScope Scope(Actions, getCurScope());
597 return parseObjCTypeParamListOrProtocolRefs(Scope, lAngleLoc, protocolIdents,
598 rAngleLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000599 /*mayBeProtocolList=*/false);
600}
601
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000602/// objc-interface-decl-list:
603/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000604/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000605/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000606/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000607/// objc-interface-decl-list declaration
608/// objc-interface-decl-list ';'
609///
Steve Naroff99264b42007-08-22 16:35:03 +0000610/// objc-method-requirement: [OBJC2]
611/// @required
612/// @optional
613///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000614void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
615 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000616 SmallVector<Decl *, 32> allMethods;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000617 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000618 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000619
Ted Kremenekc7c64312010-01-07 01:20:12 +0000620 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000621
Steve Naroff99264b42007-08-22 16:35:03 +0000622 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000623 // If this is a method prototype, parse it.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000624 if (Tok.isOneOf(tok::minus, tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000625 if (Decl *methodPrototype =
626 ParseObjCMethodPrototype(MethodImplKind, false))
627 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000628 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
629 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000630 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
631 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000632 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000633 if (Tok.is(tok::semi))
634 ConsumeToken();
635 }
Steve Naroff99264b42007-08-22 16:35:03 +0000636 continue;
637 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000638 if (Tok.is(tok::l_paren)) {
639 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000640 ParseObjCMethodDecl(Tok.getLocation(),
641 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000642 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000643 continue;
644 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000645 // Ignore excess semicolons.
646 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000647 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000648 continue;
649 }
Mike Stump11289f42009-09-09 15:08:12 +0000650
Chris Lattnerda9fb152008-10-20 06:10:06 +0000651 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000652 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000653 break;
Mike Stump11289f42009-09-09 15:08:12 +0000654
Douglas Gregorf1934162010-01-13 21:24:21 +0000655 // Code completion within an Objective-C interface.
656 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000657 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000658 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000659 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000660 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000661 }
662
Chris Lattner038a3e32008-10-20 05:46:22 +0000663 // If we don't have an @ directive, parse it as a function definition.
664 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000665 // The code below does not consume '}'s because it is afraid of eating the
666 // end of a namespace. Because of the way this code is structured, an
667 // erroneous r_brace would cause an infinite loop if not handled here.
668 if (Tok.is(tok::r_brace))
669 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000670 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000671 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000672 continue;
673 }
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattner038a3e32008-10-20 05:46:22 +0000675 // Otherwise, we have an @ directive, eat the @.
676 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000677 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000678 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000679 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000680 }
681
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000682 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000683
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000684 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000685 AtEnd.setBegin(AtLoc);
686 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000687 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000688 } else if (DirectiveKind == tok::objc_not_keyword) {
689 Diag(Tok, diag::err_objc_unknown_at);
690 SkipUntil(tok::semi);
691 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000692 }
Mike Stump11289f42009-09-09 15:08:12 +0000693
Chris Lattnerda9fb152008-10-20 06:10:06 +0000694 // Eat the identifier.
695 ConsumeToken();
696
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000697 switch (DirectiveKind) {
698 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000699 // FIXME: If someone forgets an @end on a protocol, this loop will
700 // continue to eat up tons of stuff and spew lots of nonsense errors. It
701 // would probably be better to bail out if we saw an @class or @interface
702 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000703 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000704 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000705 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000706 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000707
708 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000709 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000710 Diag(AtLoc, diag::err_objc_missing_end)
711 << FixItHint::CreateInsertion(AtLoc, "@end\n");
712 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
713 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000714 ConsumeToken();
715 break;
716
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000717 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000718 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000719 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000720 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000721 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000722 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000723 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000724 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000725 break;
Mike Stump11289f42009-09-09 15:08:12 +0000726
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000727 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000728 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000729 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000730
Chris Lattner038a3e32008-10-20 05:46:22 +0000731 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000732 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000733 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000734 if (Tok.is(tok::l_paren)) {
735 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000736 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000737 }
Mike Stump11289f42009-09-09 15:08:12 +0000738
Douglas Gregor813a0662015-06-19 18:14:38 +0000739 bool addedToDeclSpec = false;
Benjamin Kramera39beb92014-09-03 11:06:10 +0000740 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
741 if (FD.D.getIdentifier() == nullptr) {
742 Diag(AtLoc, diag::err_objc_property_requires_field_name)
743 << FD.D.getSourceRange();
744 return;
745 }
746 if (FD.BitfieldSize) {
747 Diag(AtLoc, diag::err_objc_property_bitfield)
748 << FD.D.getSourceRange();
749 return;
750 }
751
Douglas Gregor813a0662015-06-19 18:14:38 +0000752 // Map a nullability property attribute to a context-sensitive keyword
753 // attribute.
754 if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
755 addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
756 OCDS.getNullabilityLoc(),
757 addedToDeclSpec);
758
Benjamin Kramera39beb92014-09-03 11:06:10 +0000759 // Install the property declarator into interfaceDecl.
760 IdentifierInfo *SelName =
761 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
762
763 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
764 IdentifierInfo *SetterName = OCDS.getSetterName();
765 Selector SetterSel;
766 if (SetterName)
767 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
768 else
769 SetterSel = SelectorTable::constructSetterSelector(
770 PP.getIdentifierTable(), PP.getSelectorTable(),
771 FD.D.getIdentifier());
Benjamin Kramera39beb92014-09-03 11:06:10 +0000772 Decl *Property = Actions.ActOnProperty(
773 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000774 MethodImplKind);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000775
776 FD.complete(Property);
777 };
John McCallcfefb6d2009-11-03 02:38:08 +0000778
Chris Lattner038a3e32008-10-20 05:46:22 +0000779 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000780 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000781 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000782
John McCall405988b2011-03-26 01:53:26 +0000783 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000784 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000785 }
Steve Naroff99264b42007-08-22 16:35:03 +0000786 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000787
788 // We break out of the big loop in two cases: when we see @end or when we see
789 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000790 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000791 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000792 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000793 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000794 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000795 } else {
796 Diag(Tok, diag::err_objc_missing_end)
797 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
798 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
799 << (int) Actions.getObjCContainerKind();
800 AtEnd.setBegin(Tok.getLocation());
801 AtEnd.setEnd(Tok.getLocation());
802 }
Mike Stump11289f42009-09-09 15:08:12 +0000803
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000804 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000805 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000806 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000807}
808
Douglas Gregor813a0662015-06-19 18:14:38 +0000809/// Diagnose redundant or conflicting nullability information.
810static void diagnoseRedundantPropertyNullability(Parser &P,
811 ObjCDeclSpec &DS,
812 NullabilityKind nullability,
813 SourceLocation nullabilityLoc){
814 if (DS.getNullability() == nullability) {
815 P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000816 << DiagNullabilityKind(nullability, true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000817 << SourceRange(DS.getNullabilityLoc());
818 return;
819 }
820
821 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000822 << DiagNullabilityKind(nullability, true)
823 << DiagNullabilityKind(DS.getNullability(), true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000824 << SourceRange(DS.getNullabilityLoc());
825}
826
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000827/// Parse property attribute declarations.
828///
829/// property-attr-decl: '(' property-attrlist ')'
830/// property-attrlist:
831/// property-attribute
832/// property-attrlist ',' property-attribute
833/// property-attribute:
834/// getter '=' identifier
835/// setter '=' identifier ':'
836/// readonly
837/// readwrite
838/// assign
839/// retain
840/// copy
841/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000842/// atomic
843/// strong
844/// weak
845/// unsafe_unretained
Douglas Gregor813a0662015-06-19 18:14:38 +0000846/// nonnull
847/// nullable
848/// null_unspecified
Douglas Gregor849ebc22015-06-19 18:14:46 +0000849/// null_resettable
Manman Ren387ff7f2016-01-26 18:52:43 +0000850/// class
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000851///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000852void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000853 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000854 BalancedDelimiterTracker T(*this, tok::l_paren);
855 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000856
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000857 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000858 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000859 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000860 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000861 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000862 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000863
Chris Lattner76619232008-10-20 07:22:18 +0000864 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000865 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000866 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000867 return;
868 }
Mike Stump11289f42009-09-09 15:08:12 +0000869
Chris Lattner1db33542008-10-20 07:37:22 +0000870 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000871
Chris Lattner68e48682008-11-20 04:42:34 +0000872 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000873 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000874 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000875 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000876 else if (II->isStr("unsafe_unretained"))
877 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000878 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000879 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000880 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000881 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000882 else if (II->isStr("strong"))
883 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000884 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000885 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000886 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000887 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000888 else if (II->isStr("atomic"))
889 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000890 else if (II->isStr("weak"))
891 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000892 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000893 bool IsSetter = II->getNameStart()[0] == 's';
894
Chris Lattner825bca12008-10-20 07:39:53 +0000895 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000896 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
Craig Topper3110a5c2015-11-14 18:16:02 +0000897 diag::err_objc_expected_equal_for_getter;
Anders Carlssonfe15a782010-10-02 17:45:21 +0000898
Alp Toker383d2c42014-01-01 03:08:43 +0000899 if (ExpectAndConsume(tok::equal, DiagID)) {
900 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000901 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000902 }
Mike Stump11289f42009-09-09 15:08:12 +0000903
Douglas Gregorc8537c52009-11-19 07:41:15 +0000904 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000905 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000906 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000907 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000908 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000909 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000910 }
911
Anders Carlssonfe15a782010-10-02 17:45:21 +0000912 SourceLocation SelLoc;
913 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
914
915 if (!SelIdent) {
916 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
917 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000918 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000919 return;
920 }
Mike Stump11289f42009-09-09 15:08:12 +0000921
Anders Carlssonfe15a782010-10-02 17:45:21 +0000922 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000923 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000924 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000925
Alp Toker383d2c42014-01-01 03:08:43 +0000926 if (ExpectAndConsume(tok::colon,
927 diag::err_expected_colon_after_setter_name)) {
928 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000929 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000930 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000931 } else {
932 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000933 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000934 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000935 } else if (II->isStr("nonnull")) {
936 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
937 diagnoseRedundantPropertyNullability(*this, DS,
938 NullabilityKind::NonNull,
939 Tok.getLocation());
940 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
941 DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
942 } else if (II->isStr("nullable")) {
943 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
944 diagnoseRedundantPropertyNullability(*this, DS,
945 NullabilityKind::Nullable,
946 Tok.getLocation());
947 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
948 DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
949 } else if (II->isStr("null_unspecified")) {
950 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
951 diagnoseRedundantPropertyNullability(*this, DS,
952 NullabilityKind::Unspecified,
953 Tok.getLocation());
954 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
955 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000956 } else if (II->isStr("null_resettable")) {
957 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
958 diagnoseRedundantPropertyNullability(*this, DS,
959 NullabilityKind::Unspecified,
960 Tok.getLocation());
961 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
962 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
963
964 // Also set the null_resettable bit.
965 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
Manman Ren387ff7f2016-01-26 18:52:43 +0000966 } else if (II->isStr("class")) {
967 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_class);
Chris Lattner825bca12008-10-20 07:39:53 +0000968 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000969 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000970 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000971 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000972 }
Mike Stump11289f42009-09-09 15:08:12 +0000973
Chris Lattner1db33542008-10-20 07:37:22 +0000974 if (Tok.isNot(tok::comma))
975 break;
Mike Stump11289f42009-09-09 15:08:12 +0000976
Chris Lattner1db33542008-10-20 07:37:22 +0000977 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000978 }
Mike Stump11289f42009-09-09 15:08:12 +0000979
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000980 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000981}
982
Steve Naroff09bf8152007-09-06 21:24:23 +0000983/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000984/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000985/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000986///
987/// objc-instance-method: '-'
988/// objc-class-method: '+'
989///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000990/// objc-method-attributes: [OBJC2]
991/// __attribute__((deprecated))
992///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000993Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000994 bool MethodDefinition) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000995 assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000996
Mike Stump11289f42009-09-09 15:08:12 +0000997 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000998 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000999 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001000 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +00001001 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +00001002 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +00001003 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +00001004}
1005
1006/// objc-selector:
1007/// identifier
1008/// one of
1009/// enum struct union if else while do for switch case default
1010/// break continue return goto asm sizeof typeof __alignof
1011/// unsigned long const short volatile signed restrict _Complex
1012/// in out inout bycopy byref oneway int char float double void _Bool
1013///
Chris Lattner4f472a32009-04-11 18:13:45 +00001014IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +00001015
Chris Lattner5700fab2007-10-07 02:00:24 +00001016 switch (Tok.getKind()) {
1017 default:
Craig Topper161e4db2014-05-21 06:02:52 +00001018 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001019 case tok::ampamp:
1020 case tok::ampequal:
1021 case tok::amp:
1022 case tok::pipe:
1023 case tok::tilde:
1024 case tok::exclaim:
1025 case tok::exclaimequal:
1026 case tok::pipepipe:
1027 case tok::pipeequal:
1028 case tok::caret:
1029 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +00001030 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +00001031 if (isLetter(ThisTok[0])) {
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001032 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
1033 Tok.setKind(tok::identifier);
1034 SelectorLoc = ConsumeToken();
1035 return II;
1036 }
Craig Topper161e4db2014-05-21 06:02:52 +00001037 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001038 }
1039
Chris Lattner5700fab2007-10-07 02:00:24 +00001040 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001041 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +00001042 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001043 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001044 case tok::kw_break:
1045 case tok::kw_case:
1046 case tok::kw_catch:
1047 case tok::kw_char:
1048 case tok::kw_class:
1049 case tok::kw_const:
1050 case tok::kw_const_cast:
1051 case tok::kw_continue:
1052 case tok::kw_default:
1053 case tok::kw_delete:
1054 case tok::kw_do:
1055 case tok::kw_double:
1056 case tok::kw_dynamic_cast:
1057 case tok::kw_else:
1058 case tok::kw_enum:
1059 case tok::kw_explicit:
1060 case tok::kw_export:
1061 case tok::kw_extern:
1062 case tok::kw_false:
1063 case tok::kw_float:
1064 case tok::kw_for:
1065 case tok::kw_friend:
1066 case tok::kw_goto:
1067 case tok::kw_if:
1068 case tok::kw_inline:
1069 case tok::kw_int:
1070 case tok::kw_long:
1071 case tok::kw_mutable:
1072 case tok::kw_namespace:
1073 case tok::kw_new:
1074 case tok::kw_operator:
1075 case tok::kw_private:
1076 case tok::kw_protected:
1077 case tok::kw_public:
1078 case tok::kw_register:
1079 case tok::kw_reinterpret_cast:
1080 case tok::kw_restrict:
1081 case tok::kw_return:
1082 case tok::kw_short:
1083 case tok::kw_signed:
1084 case tok::kw_sizeof:
1085 case tok::kw_static:
1086 case tok::kw_static_cast:
1087 case tok::kw_struct:
1088 case tok::kw_switch:
1089 case tok::kw_template:
1090 case tok::kw_this:
1091 case tok::kw_throw:
1092 case tok::kw_true:
1093 case tok::kw_try:
1094 case tok::kw_typedef:
1095 case tok::kw_typeid:
1096 case tok::kw_typename:
1097 case tok::kw_typeof:
1098 case tok::kw_union:
1099 case tok::kw_unsigned:
1100 case tok::kw_using:
1101 case tok::kw_virtual:
1102 case tok::kw_void:
1103 case tok::kw_volatile:
1104 case tok::kw_wchar_t:
1105 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +00001106 case tok::kw__Bool:
1107 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001108 case tok::kw___alignof:
Richard Smithe301ba22015-11-11 02:02:15 +00001109 case tok::kw___auto_type:
Chris Lattner5700fab2007-10-07 02:00:24 +00001110 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001111 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +00001112 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +00001113 }
Steve Naroff99264b42007-08-22 16:35:03 +00001114}
1115
Fariborz Jahanian83615522008-01-02 22:54:34 +00001116/// objc-for-collection-in: 'in'
1117///
Fariborz Jahanian3622e592008-01-04 23:04:08 +00001118bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001119 // FIXME: May have to do additional look-ahead to only allow for
1120 // valid tokens following an 'in'; such as an identifier, unary operators,
1121 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001122 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +00001123 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +00001124}
1125
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001126/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +00001127/// qualifier list and builds their bitmask representation in the input
1128/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +00001129///
1130/// objc-type-qualifiers:
1131/// objc-type-qualifier
1132/// objc-type-qualifiers objc-type-qualifier
1133///
Douglas Gregor813a0662015-06-19 18:14:38 +00001134/// objc-type-qualifier:
1135/// 'in'
1136/// 'out'
1137/// 'inout'
1138/// 'oneway'
1139/// 'bycopy'
1140/// 'byref'
1141/// 'nonnull'
1142/// 'nullable'
1143/// 'null_unspecified'
1144///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001145void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001146 Declarator::TheContext Context) {
1147 assert(Context == Declarator::ObjCParameterContext ||
1148 Context == Declarator::ObjCResultContext);
1149
Chris Lattner61511e12007-12-12 06:56:32 +00001150 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +00001151 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +00001152 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +00001153 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001154 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +00001155 }
1156
Chris Lattner5e530bc2007-12-27 19:57:00 +00001157 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +00001158 return;
Mike Stump11289f42009-09-09 15:08:12 +00001159
Chris Lattner61511e12007-12-12 06:56:32 +00001160 const IdentifierInfo *II = Tok.getIdentifierInfo();
1161 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001162 if (II != ObjCTypeQuals[i] ||
1163 NextToken().is(tok::less) ||
1164 NextToken().is(tok::coloncolon))
Chris Lattner61511e12007-12-12 06:56:32 +00001165 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001166
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001167 ObjCDeclSpec::ObjCDeclQualifier Qual;
Douglas Gregor813a0662015-06-19 18:14:38 +00001168 NullabilityKind Nullability;
Chris Lattner61511e12007-12-12 06:56:32 +00001169 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +00001170 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001171 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
1172 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
1173 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
1174 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
1175 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
1176 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Douglas Gregor813a0662015-06-19 18:14:38 +00001177
1178 case objc_nonnull:
1179 Qual = ObjCDeclSpec::DQ_CSNullability;
1180 Nullability = NullabilityKind::NonNull;
1181 break;
1182
1183 case objc_nullable:
1184 Qual = ObjCDeclSpec::DQ_CSNullability;
1185 Nullability = NullabilityKind::Nullable;
1186 break;
1187
1188 case objc_null_unspecified:
1189 Qual = ObjCDeclSpec::DQ_CSNullability;
1190 Nullability = NullabilityKind::Unspecified;
1191 break;
Chris Lattner61511e12007-12-12 06:56:32 +00001192 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001193
1194 // FIXME: Diagnose redundant specifiers.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001195 DS.setObjCDeclQualifier(Qual);
Douglas Gregor813a0662015-06-19 18:14:38 +00001196 if (Qual == ObjCDeclSpec::DQ_CSNullability)
1197 DS.setNullability(Tok.getLocation(), Nullability);
1198
Chris Lattner61511e12007-12-12 06:56:32 +00001199 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001200 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +00001201 break;
1202 }
Mike Stump11289f42009-09-09 15:08:12 +00001203
Chris Lattner61511e12007-12-12 06:56:32 +00001204 // If this wasn't a recognized qualifier, bail out.
1205 if (II) return;
1206 }
1207}
1208
John McCalla55902b2011-10-01 09:56:14 +00001209/// Take all the decl attributes out of the given list and add
1210/// them to the given attribute set.
1211static void takeDeclAttributes(ParsedAttributes &attrs,
1212 AttributeList *list) {
1213 while (list) {
1214 AttributeList *cur = list;
1215 list = cur->getNext();
1216
1217 if (!cur->isUsedAsTypeAttr()) {
1218 // Clear out the next pointer. We're really completely
1219 // destroying the internal invariants of the declarator here,
1220 // but it doesn't matter because we're done with it.
Craig Topper161e4db2014-05-21 06:02:52 +00001221 cur->setNext(nullptr);
John McCalla55902b2011-10-01 09:56:14 +00001222 attrs.add(cur);
1223 }
1224 }
1225}
1226
1227/// takeDeclAttributes - Take all the decl attributes from the given
1228/// declarator and add them to the given list.
1229static void takeDeclAttributes(ParsedAttributes &attrs,
1230 Declarator &D) {
1231 // First, take ownership of all attributes.
1232 attrs.getPool().takeAllFrom(D.getAttributePool());
1233 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
1234
1235 // Now actually move the attributes over.
1236 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
1237 takeDeclAttributes(attrs, D.getAttributes());
1238 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
1239 takeDeclAttributes(attrs,
1240 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
1241}
1242
Chris Lattner61511e12007-12-12 06:56:32 +00001243/// objc-type-name:
1244/// '(' objc-type-qualifiers[opt] type-name ')'
1245/// '(' objc-type-qualifiers[opt] ')'
1246///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001247ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001248 Declarator::TheContext context,
1249 ParsedAttributes *paramAttrs) {
1250 assert(context == Declarator::ObjCParameterContext ||
1251 context == Declarator::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +00001252 assert((paramAttrs != nullptr) ==
1253 (context == Declarator::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +00001254
Chris Lattner0ef13522007-10-09 17:51:17 +00001255 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +00001256
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001257 BalancedDelimiterTracker T(*this, tok::l_paren);
1258 T.consumeOpen();
1259
Chris Lattner2ebb1782008-08-23 01:48:03 +00001260 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001261 ObjCDeclContextSwitch ObjCDC(*this);
1262
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001263 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001264 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +00001265
John McCallba7bf592010-08-24 05:47:05 +00001266 ParsedType Ty;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001267 if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
John McCalla55902b2011-10-01 09:56:14 +00001268 // Parse an abstract declarator.
1269 DeclSpec declSpec(AttrFactory);
1270 declSpec.setObjCQualifiers(&DS);
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001271 DeclSpecContext dsContext = DSC_normal;
1272 if (context == Declarator::ObjCResultContext)
1273 dsContext = DSC_objc_method_result;
1274 ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
Fariborz Jahanianb6499eb2012-05-29 21:52:45 +00001275 declSpec.SetRangeEnd(Tok.getLocation());
John McCalla55902b2011-10-01 09:56:14 +00001276 Declarator declarator(declSpec, context);
1277 ParseDeclarator(declarator);
1278
1279 // If that's not invalid, extract a type.
1280 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001281 // Map a nullability specifier to a context-sensitive keyword attribute.
1282 bool addedToDeclSpec = false;
1283 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1284 addContextSensitiveTypeNullability(*this, declarator,
1285 DS.getNullability(),
1286 DS.getNullabilityLoc(),
1287 addedToDeclSpec);
1288
John McCalla55902b2011-10-01 09:56:14 +00001289 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1290 if (!type.isInvalid())
1291 Ty = type.get();
1292
1293 // If we're parsing a parameter, steal all the decl attributes
1294 // and add them to the decl spec.
1295 if (context == Declarator::ObjCParameterContext)
1296 takeDeclAttributes(*paramAttrs, declarator);
1297 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001298 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001299
Steve Naroff90255b42008-10-21 14:15:04 +00001300 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001301 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001302 else if (Tok.getLocation() == TypeStartLoc) {
1303 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001304 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001305 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001306 } else {
1307 // Otherwise, we found *something*, but didn't get a ')' in the right
1308 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001309 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001310 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001311 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001312}
1313
1314/// objc-method-decl:
1315/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001316/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001317/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001318/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001319///
1320/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001321/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001322/// objc-keyword-selector objc-keyword-decl
1323///
1324/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001325/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1326/// objc-selector ':' objc-keyword-attributes[opt] identifier
1327/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1328/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001329///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001330/// objc-parmlist:
1331/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001332///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001333/// objc-parms:
1334/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001335///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001336/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001337/// , ...
1338///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001339/// objc-keyword-attributes: [OBJC2]
1340/// __attribute__((unused))
1341///
John McCall48871652010-08-21 09:40:31 +00001342Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001343 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001344 tok::ObjCKeywordKind MethodImplKind,
1345 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001346 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001347
Douglas Gregor636a61e2010-04-07 00:21:17 +00001348 if (Tok.is(tok::code_completion)) {
David Blaikieefdccaa2016-01-15 23:43:34 +00001349 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
1350 /*ReturnType=*/nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001351 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001352 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001353 }
1354
Chris Lattner2ebb1782008-08-23 01:48:03 +00001355 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001356 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001357 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001358 if (Tok.is(tok::l_paren))
Craig Topper161e4db2014-05-21 06:02:52 +00001359 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext,
1360 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001361
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001362 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001363 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001364 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001365 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001366
Douglas Gregor636a61e2010-04-07 00:21:17 +00001367 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001368 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001369 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001370 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001371 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001372 }
1373
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001374 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001375 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001376 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001377
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001378 // An unnamed colon is valid.
1379 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001380 Diag(Tok, diag::err_expected_selector_for_method)
1381 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001382 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001383 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001384 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001385 }
Mike Stump11289f42009-09-09 15:08:12 +00001386
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001387 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001388 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001389 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001390 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001391 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001392
Chris Lattner5700fab2007-10-07 02:00:24 +00001393 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001394 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001395 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001396 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001397 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001398 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001399 methodAttrs.getList(), MethodImplKind,
1400 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001401 PD.complete(Result);
1402 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001403 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001404
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001405 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001406 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001407 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001408 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1409 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001410
1411 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001412 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001413 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001414 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001415
Chris Lattner5700fab2007-10-07 02:00:24 +00001416 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001417 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001418 break;
Mike Stump11289f42009-09-09 15:08:12 +00001419
David Blaikieefdccaa2016-01-15 23:43:34 +00001420 ArgInfo.Type = nullptr;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001421 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001422 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1423 Declarator::ObjCParameterContext,
1424 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001425
Chris Lattner5700fab2007-10-07 02:00:24 +00001426 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001427 // Regardless, collect all the attributes we've parsed so far.
Craig Topper161e4db2014-05-21 06:02:52 +00001428 ArgInfo.ArgAttrs = nullptr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001429 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001430 MaybeParseGNUAttributes(paramAttrs);
1431 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001432 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001433
Douglas Gregor45879692010-07-08 23:37:41 +00001434 // Code completion for the next piece of the selector.
1435 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001436 KeyIdents.push_back(SelIdent);
1437 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1438 mType == tok::minus,
1439 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001440 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001441 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001442 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001443 }
1444
Chris Lattner0ef13522007-10-09 17:51:17 +00001445 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001446 Diag(Tok, diag::err_expected)
1447 << tok::identifier; // missing argument name.
Chris Lattner5700fab2007-10-07 02:00:24 +00001448 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +00001449 }
Mike Stump11289f42009-09-09 15:08:12 +00001450
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001451 ArgInfo.Name = Tok.getIdentifierInfo();
1452 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001453 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001454
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001455 ArgInfos.push_back(ArgInfo);
1456 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001457 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001458
John McCall084e83d2011-03-24 11:26:52 +00001459 // Make sure the attributes persist.
1460 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1461
Douglas Gregor95887f92010-07-08 23:20:03 +00001462 // Code completion for the next piece of the selector.
1463 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001464 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1465 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001466 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001467 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001468 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001469 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001470 }
1471
Chris Lattner5700fab2007-10-07 02:00:24 +00001472 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001473 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001474 if (!SelIdent && Tok.isNot(tok::colon))
1475 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001476 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001477 SourceLocation ColonLoc = Tok.getLocation();
1478 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001479 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1480 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1481 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001482 }
1483 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001484 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001485 }
Mike Stump11289f42009-09-09 15:08:12 +00001486
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001487 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001488 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001489 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001490 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001491 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001492 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001493 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001494 ConsumeToken();
1495 break;
1496 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001497 if (!cStyleParamWarned) {
1498 Diag(Tok, diag::warn_cstyle_param);
1499 cStyleParamWarned = true;
1500 }
John McCall084e83d2011-03-24 11:26:52 +00001501 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001502 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001503 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001504 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1505 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001506 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001507 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001508 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1509 ParmDecl.getIdentifierLoc(),
1510 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001511 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001512 }
Mike Stump11289f42009-09-09 15:08:12 +00001513
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001514 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001515 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001516 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001517 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001518
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001519 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001520 return nullptr;
1521
Chris Lattner5700fab2007-10-07 02:00:24 +00001522 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1523 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001524 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001525 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001526 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001527 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001528 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001529 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001530 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001531
John McCall28a6aea2009-11-04 02:18:39 +00001532 PD.complete(Result);
1533 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001534}
1535
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001536/// objc-protocol-refs:
1537/// '<' identifier-list '>'
1538///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001539bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001540ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1541 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001542 bool WarnOnDeclarations, bool ForObjCContainer,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001543 SourceLocation &LAngleLoc, SourceLocation &EndLoc,
1544 bool consumeLastToken) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001545 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001546
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001547 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001548
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001549 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001550
Chris Lattner3bbae002008-07-26 04:03:38 +00001551 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001552 if (Tok.is(tok::code_completion)) {
Craig Topper883dd332015-12-24 23:58:11 +00001553 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001554 cutOffParsing();
1555 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001556 }
1557
Chris Lattner3bbae002008-07-26 04:03:38 +00001558 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001559 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001560 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001561 return true;
1562 }
1563 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1564 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001565 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001566 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001567
Alp Toker383d2c42014-01-01 03:08:43 +00001568 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001569 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001570 }
Mike Stump11289f42009-09-09 15:08:12 +00001571
Chris Lattner3bbae002008-07-26 04:03:38 +00001572 // Consume the '>'.
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001573 if (ParseGreaterThanInTemplateList(EndLoc, consumeLastToken,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001574 /*ObjCGenericList=*/false))
Chris Lattner3bbae002008-07-26 04:03:38 +00001575 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001576
Chris Lattner3bbae002008-07-26 04:03:38 +00001577 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001578 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Craig Toppera9247eb2015-10-22 04:59:56 +00001579 ProtocolIdents, Protocols);
Chris Lattner3bbae002008-07-26 04:03:38 +00001580 return false;
1581}
1582
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001583TypeResult Parser::parseObjCProtocolQualifierType(SourceLocation &rAngleLoc) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001584 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001585 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001586
1587 SourceLocation lAngleLoc;
1588 SmallVector<Decl *, 8> protocols;
1589 SmallVector<SourceLocation, 8> protocolLocs;
1590 (void)ParseObjCProtocolReferences(protocols, protocolLocs, false, false,
1591 lAngleLoc, rAngleLoc,
1592 /*consumeLastToken=*/true);
1593 TypeResult result = Actions.actOnObjCProtocolQualifierType(lAngleLoc,
1594 protocols,
1595 protocolLocs,
1596 rAngleLoc);
1597 if (result.isUsable()) {
1598 Diag(lAngleLoc, diag::warn_objc_protocol_qualifier_missing_id)
1599 << FixItHint::CreateInsertion(lAngleLoc, "id")
1600 << SourceRange(lAngleLoc, rAngleLoc);
1601 }
1602
1603 return result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001604}
1605
Douglas Gregore9d95f12015-07-07 03:57:35 +00001606/// Parse Objective-C type arguments or protocol qualifiers.
1607///
1608/// objc-type-arguments:
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001609/// '<' type-name '...'[opt] (',' type-name '...'[opt])* '>'
Douglas Gregore9d95f12015-07-07 03:57:35 +00001610///
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001611void Parser::parseObjCTypeArgsOrProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001612 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001613 SourceLocation &typeArgsLAngleLoc,
1614 SmallVectorImpl<ParsedType> &typeArgs,
1615 SourceLocation &typeArgsRAngleLoc,
1616 SourceLocation &protocolLAngleLoc,
1617 SmallVectorImpl<Decl *> &protocols,
1618 SmallVectorImpl<SourceLocation> &protocolLocs,
1619 SourceLocation &protocolRAngleLoc,
1620 bool consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001621 bool warnOnIncompleteProtocols) {
1622 assert(Tok.is(tok::less) && "Not at the start of type args or protocols");
1623 SourceLocation lAngleLoc = ConsumeToken();
1624
1625 // Whether all of the elements we've parsed thus far are single
1626 // identifiers, which might be types or might be protocols.
1627 bool allSingleIdentifiers = true;
1628 SmallVector<IdentifierInfo *, 4> identifiers;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001629 SmallVectorImpl<SourceLocation> &identifierLocs = protocolLocs;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001630
1631 // Parse a list of comma-separated identifiers, bailing out if we
1632 // see something different.
1633 do {
1634 // Parse a single identifier.
1635 if (Tok.is(tok::identifier) &&
1636 (NextToken().is(tok::comma) ||
1637 NextToken().is(tok::greater) ||
1638 NextToken().is(tok::greatergreater))) {
1639 identifiers.push_back(Tok.getIdentifierInfo());
1640 identifierLocs.push_back(ConsumeToken());
1641 continue;
1642 }
1643
1644 if (Tok.is(tok::code_completion)) {
1645 // FIXME: Also include types here.
1646 SmallVector<IdentifierLocPair, 4> identifierLocPairs;
1647 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1648 identifierLocPairs.push_back(IdentifierLocPair(identifiers[i],
1649 identifierLocs[i]));
1650 }
1651
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001652 QualType BaseT = Actions.GetTypeFromParser(baseType);
1653 if (!BaseT.isNull() && BaseT->acceptsObjCTypeParams()) {
1654 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
1655 } else {
Craig Topper883dd332015-12-24 23:58:11 +00001656 Actions.CodeCompleteObjCProtocolReferences(identifierLocPairs);
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001657 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001658 cutOffParsing();
1659 return;
1660 }
1661
1662 allSingleIdentifiers = false;
1663 break;
1664 } while (TryConsumeToken(tok::comma));
1665
1666 // If we parsed an identifier list, semantic analysis sorts out
1667 // whether it refers to protocols or to type arguments.
1668 if (allSingleIdentifiers) {
1669 // Parse the closing '>'.
1670 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001671 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001672 /*ObjCGenericList=*/true);
1673
1674 // Let Sema figure out what we parsed.
1675 Actions.actOnObjCTypeArgsOrProtocolQualifiers(getCurScope(),
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001676 baseType,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001677 lAngleLoc,
1678 identifiers,
1679 identifierLocs,
1680 rAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001681 typeArgsLAngleLoc,
1682 typeArgs,
1683 typeArgsRAngleLoc,
1684 protocolLAngleLoc,
1685 protocols,
1686 protocolRAngleLoc,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001687 warnOnIncompleteProtocols);
1688 return;
1689 }
1690
1691 // We syntactically matched a type argument, so commit to parsing
1692 // type arguments.
Douglas Gregore9d95f12015-07-07 03:57:35 +00001693
1694 // Convert the identifiers into type arguments.
1695 bool invalid = false;
1696 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1697 ParsedType typeArg
1698 = Actions.getTypeName(*identifiers[i], identifierLocs[i], getCurScope());
1699 if (typeArg) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001700 DeclSpec DS(AttrFactory);
1701 const char *prevSpec = nullptr;
1702 unsigned diagID;
1703 DS.SetTypeSpecType(TST_typename, identifierLocs[i], prevSpec, diagID,
1704 typeArg, Actions.getASTContext().getPrintingPolicy());
1705
1706 // Form a declarator to turn this into a type.
1707 Declarator D(DS, Declarator::TypeNameContext);
1708 TypeResult fullTypeArg = Actions.ActOnTypeName(getCurScope(), D);
1709 if (fullTypeArg.isUsable())
1710 typeArgs.push_back(fullTypeArg.get());
1711 else
1712 invalid = true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001713 } else {
1714 invalid = true;
1715 }
1716 }
1717
1718 // Continue parsing type-names.
1719 do {
1720 TypeResult typeArg = ParseTypeName();
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001721
1722 // Consume the '...' for a pack expansion.
1723 SourceLocation ellipsisLoc;
1724 TryConsumeToken(tok::ellipsis, ellipsisLoc);
1725 if (typeArg.isUsable() && ellipsisLoc.isValid()) {
1726 typeArg = Actions.ActOnPackExpansion(typeArg.get(), ellipsisLoc);
1727 }
1728
Douglas Gregore9d95f12015-07-07 03:57:35 +00001729 if (typeArg.isUsable()) {
1730 typeArgs.push_back(typeArg.get());
1731 } else {
1732 invalid = true;
1733 }
1734 } while (TryConsumeToken(tok::comma));
1735
1736 // Parse the closing '>'.
1737 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001738 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001739 /*ObjCGenericList=*/true);
1740
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001741 if (invalid) {
1742 typeArgs.clear();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001743 return;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001744 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001745
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001746 // Record left/right angle locations.
1747 typeArgsLAngleLoc = lAngleLoc;
1748 typeArgsRAngleLoc = rAngleLoc;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001749}
1750
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001751void Parser::parseObjCTypeArgsAndProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001752 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001753 SourceLocation &typeArgsLAngleLoc,
1754 SmallVectorImpl<ParsedType> &typeArgs,
1755 SourceLocation &typeArgsRAngleLoc,
1756 SourceLocation &protocolLAngleLoc,
1757 SmallVectorImpl<Decl *> &protocols,
1758 SmallVectorImpl<SourceLocation> &protocolLocs,
1759 SourceLocation &protocolRAngleLoc,
1760 bool consumeLastToken) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001761 assert(Tok.is(tok::less));
1762
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001763 // Parse the first angle-bracket-delimited clause.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001764 parseObjCTypeArgsOrProtocolQualifiers(baseType,
1765 typeArgsLAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001766 typeArgs,
1767 typeArgsRAngleLoc,
1768 protocolLAngleLoc,
1769 protocols,
1770 protocolLocs,
1771 protocolRAngleLoc,
1772 consumeLastToken,
Douglas Gregore83b9562015-07-07 03:57:53 +00001773 /*warnOnIncompleteProtocols=*/false);
1774
1775 // An Objective-C object pointer followed by type arguments
1776 // can then be followed again by a set of protocol references, e.g.,
1777 // \c NSArray<NSView><NSTextDelegate>
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001778 if ((consumeLastToken && Tok.is(tok::less)) ||
1779 (!consumeLastToken && NextToken().is(tok::less))) {
1780 // If we aren't consuming the last token, the prior '>' is still hanging
1781 // there. Consume it before we parse the protocol qualifiers.
1782 if (!consumeLastToken)
1783 ConsumeToken();
1784
1785 if (!protocols.empty()) {
1786 SkipUntilFlags skipFlags = SkipUntilFlags();
1787 if (!consumeLastToken)
1788 skipFlags = skipFlags | StopBeforeMatch;
Douglas Gregore83b9562015-07-07 03:57:53 +00001789 Diag(Tok, diag::err_objc_type_args_after_protocols)
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001790 << SourceRange(protocolLAngleLoc, protocolRAngleLoc);
1791 SkipUntil(tok::greater, tok::greatergreater, skipFlags);
Douglas Gregore83b9562015-07-07 03:57:53 +00001792 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001793 ParseObjCProtocolReferences(protocols, protocolLocs,
1794 /*WarnOnDeclarations=*/false,
1795 /*ForObjCContainer=*/false,
1796 protocolLAngleLoc, protocolRAngleLoc,
1797 consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001798 }
1799 }
1800}
1801
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001802TypeResult Parser::parseObjCTypeArgsAndProtocolQualifiers(
1803 SourceLocation loc,
1804 ParsedType type,
1805 bool consumeLastToken,
1806 SourceLocation &endLoc) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001807 assert(Tok.is(tok::less));
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001808 SourceLocation typeArgsLAngleLoc;
1809 SmallVector<ParsedType, 4> typeArgs;
1810 SourceLocation typeArgsRAngleLoc;
1811 SourceLocation protocolLAngleLoc;
1812 SmallVector<Decl *, 4> protocols;
1813 SmallVector<SourceLocation, 4> protocolLocs;
1814 SourceLocation protocolRAngleLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00001815
1816 // Parse type arguments and protocol qualifiers.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001817 parseObjCTypeArgsAndProtocolQualifiers(type, typeArgsLAngleLoc, typeArgs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001818 typeArgsRAngleLoc, protocolLAngleLoc,
1819 protocols, protocolLocs,
1820 protocolRAngleLoc, consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001821
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001822 // Compute the location of the last token.
1823 if (consumeLastToken)
1824 endLoc = PrevTokLocation;
1825 else
1826 endLoc = Tok.getLocation();
1827
1828 return Actions.actOnObjCTypeArgsAndProtocolQualifiers(
1829 getCurScope(),
1830 loc,
1831 type,
1832 typeArgsLAngleLoc,
1833 typeArgs,
1834 typeArgsRAngleLoc,
1835 protocolLAngleLoc,
1836 protocols,
1837 protocolLocs,
1838 protocolRAngleLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00001839}
1840
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001841void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1842 BalancedDelimiterTracker &T,
1843 SmallVectorImpl<Decl *> &AllIvarDecls,
1844 bool RBraceMissing) {
1845 if (!RBraceMissing)
1846 T.consumeClose();
1847
1848 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1849 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1850 Actions.ActOnObjCContainerFinishDefinition();
1851 // Call ActOnFields() even if we don't have any decls. This is useful
1852 // for code rewriting tools that need to be aware of the empty list.
1853 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1854 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001855 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001856}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001857
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001858/// objc-class-instance-variables:
1859/// '{' objc-instance-variable-decl-list[opt] '}'
1860///
1861/// objc-instance-variable-decl-list:
1862/// objc-visibility-spec
1863/// objc-instance-variable-decl ';'
1864/// ';'
1865/// objc-instance-variable-decl-list objc-visibility-spec
1866/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1867/// objc-instance-variable-decl-list ';'
1868///
1869/// objc-visibility-spec:
1870/// @private
1871/// @protected
1872/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001873/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001874///
1875/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001876/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001877///
John McCall48871652010-08-21 09:40:31 +00001878void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001879 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001880 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001881 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001882 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001883
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001884 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001885 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001886
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001887 BalancedDelimiterTracker T(*this, tok::l_brace);
1888 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001889 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001890 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001891 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001892
Steve Naroff00433d32007-08-21 21:17:12 +00001893 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001894 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001895 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001896 continue;
1897 }
Mike Stump11289f42009-09-09 15:08:12 +00001898
Steve Naroff00433d32007-08-21 21:17:12 +00001899 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001900 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001901 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001902 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001903 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001904 }
1905
Steve Naroff7c348172007-08-23 18:16:40 +00001906 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001907 case tok::objc_private:
1908 case tok::objc_public:
1909 case tok::objc_protected:
1910 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001911 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001912 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001913 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001914
1915 case tok::objc_end:
1916 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001917 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1918 Tok.setKind(tok::at);
1919 Tok.setLength(1);
1920 PP.EnterToken(Tok);
1921 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1922 T, AllIvarDecls, true);
1923 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001924
1925 default:
1926 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1927 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001928 }
1929 }
Mike Stump11289f42009-09-09 15:08:12 +00001930
Douglas Gregor48d46252010-01-13 21:54:15 +00001931 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001932 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001933 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001934 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001935 }
John McCallcfefb6d2009-11-03 02:38:08 +00001936
Benjamin Kramera39beb92014-09-03 11:06:10 +00001937 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1938 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1939 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001940 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001941 Decl *Field = Actions.ActOnIvar(
1942 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1943 FD.BitfieldSize, visibility);
1944 Actions.ActOnObjCContainerFinishDefinition();
1945 if (Field)
1946 AllIvarDecls.push_back(Field);
1947 FD.complete(Field);
1948 };
John McCallcfefb6d2009-11-03 02:38:08 +00001949
Chris Lattnera12405b2008-04-10 06:46:29 +00001950 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001951 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001952 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001953
Chris Lattner0ef13522007-10-09 17:51:17 +00001954 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001955 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001956 } else {
1957 Diag(Tok, diag::err_expected_semi_decl_list);
1958 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001959 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001960 }
1961 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001962 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1963 T, AllIvarDecls, false);
Steve Naroff00433d32007-08-21 21:17:12 +00001964 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001965}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001966
1967/// objc-protocol-declaration:
1968/// objc-protocol-definition
1969/// objc-protocol-forward-reference
1970///
1971/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00001972/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00001973/// objc-protocol-refs[opt]
1974/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00001975/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001976///
1977/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00001978/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001979///
James Dennett1355bd12012-06-11 06:19:40 +00001980/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001981/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001982/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00001983Parser::DeclGroupPtrTy
1984Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1985 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001986 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001987 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1988 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001989
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001990 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001991 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001992 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00001993 return nullptr;
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001994 }
1995
Nico Weber69a79142013-04-04 00:15:10 +00001996 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00001997
Chris Lattner0ef13522007-10-09 17:51:17 +00001998 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001999 Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
David Blaikie0403cb12016-01-15 23:43:25 +00002000 return nullptr;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002001 }
2002 // Save the protocol name, then consume it.
2003 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
2004 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002005
Alp Toker383d2c42014-01-01 03:08:43 +00002006 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00002007 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Craig Topper0f723bb2015-10-22 05:00:01 +00002008 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtoInfo,
John McCall53fa7142010-12-24 02:08:15 +00002009 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002010 }
Mike Stump11289f42009-09-09 15:08:12 +00002011
Erik Verbruggenf9887852011-12-08 09:58:43 +00002012 CheckNestedObjCContexts(AtLoc);
2013
Chris Lattner0ef13522007-10-09 17:51:17 +00002014 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002015 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002016 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
2017
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002018 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002019 while (1) {
2020 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00002021 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002022 Diag(Tok, diag::err_expected) << tok::identifier;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002023 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002024 return nullptr;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002025 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00002026 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
2027 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002028 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00002029
Chris Lattner0ef13522007-10-09 17:51:17 +00002030 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002031 break;
2032 }
2033 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00002034 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
David Blaikie0403cb12016-01-15 23:43:25 +00002035 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002036
Craig Topper0f723bb2015-10-22 05:00:01 +00002037 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtocolRefs,
John McCall53fa7142010-12-24 02:08:15 +00002038 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00002039 }
Mike Stump11289f42009-09-09 15:08:12 +00002040
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002041 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00002042 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002043
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002044 SmallVector<Decl *, 8> ProtocolRefs;
2045 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002046 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00002047 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002048 LAngleLoc, EndProtoLoc,
2049 /*consumeLastToken=*/true))
David Blaikie0403cb12016-01-15 23:43:25 +00002050 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002051
John McCall48871652010-08-21 09:40:31 +00002052 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00002053 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002054 ProtocolRefs.data(),
2055 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00002056 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00002057 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002058
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00002059 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00002060 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002061}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002062
2063/// objc-implementation:
2064/// objc-class-implementation-prologue
2065/// objc-category-implementation-prologue
2066///
2067/// objc-class-implementation-prologue:
2068/// @implementation identifier objc-superclass[opt]
2069/// objc-class-instance-variables[opt]
2070///
2071/// objc-category-implementation-prologue:
2072/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002073Parser::DeclGroupPtrTy
2074Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002075 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
2076 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002077 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002078 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002079
Douglas Gregor49c22a72009-11-18 16:26:39 +00002080 // Code completion after '@implementation'.
2081 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002082 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002083 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002084 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +00002085 }
2086
Nico Weber69a79142013-04-04 00:15:10 +00002087 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00002088
Chris Lattner0ef13522007-10-09 17:51:17 +00002089 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002090 Diag(Tok, diag::err_expected)
2091 << tok::identifier; // missing class or category name.
David Blaikie0403cb12016-01-15 23:43:25 +00002092 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002093 }
2094 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00002095 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002096 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00002097 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002098
Douglas Gregor85f3f952015-07-07 03:57:15 +00002099 // Neither a type parameter list nor a list of protocol references is
2100 // permitted here. Parse and diagnose them.
2101 if (Tok.is(tok::less)) {
2102 SourceLocation lAngleLoc, rAngleLoc;
2103 SmallVector<IdentifierLocPair, 8> protocolIdents;
2104 SourceLocation diagLoc = Tok.getLocation();
Richard Smith3df3f1d2015-11-03 01:19:56 +00002105 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
2106 if (parseObjCTypeParamListOrProtocolRefs(typeParamScope, lAngleLoc,
2107 protocolIdents, rAngleLoc)) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00002108 Diag(diagLoc, diag::err_objc_parameterized_implementation)
2109 << SourceRange(diagLoc, PrevTokLocation);
2110 } else if (lAngleLoc.isValid()) {
2111 Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
2112 << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
2113 }
2114 }
2115
Mike Stump11289f42009-09-09 15:08:12 +00002116 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002117 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002118 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002119 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002120 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002121
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002122 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002123 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002124 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002125 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002126 }
2127
Chris Lattner0ef13522007-10-09 17:51:17 +00002128 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002129 categoryId = Tok.getIdentifierInfo();
2130 categoryLoc = ConsumeToken();
2131 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002132 Diag(Tok, diag::err_expected)
2133 << tok::identifier; // missing category name.
David Blaikie0403cb12016-01-15 23:43:25 +00002134 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002135 }
Chris Lattner0ef13522007-10-09 17:51:17 +00002136 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002137 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002138 SkipUntil(tok::r_paren); // don't stop at ';'
David Blaikie0403cb12016-01-15 23:43:25 +00002139 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002140 }
2141 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00002142 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2143 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002144 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2145 SmallVector<Decl *, 4> protocols;
2146 SmallVector<SourceLocation, 4> protocolLocs;
2147 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2148 /*warnOnIncompleteProtocols=*/false,
2149 /*ForObjCContainer=*/false,
2150 protocolLAngleLoc, protocolRAngleLoc,
2151 /*consumeLastToken=*/true);
Fariborz Jahanian85888552013-05-17 17:58:11 +00002152 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002153 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002154 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00002155 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002156
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002157 } else {
2158 // We have a class implementation
2159 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002160 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00002161 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002162 // We have a super class
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002163 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002164 Diag(Tok, diag::err_expected)
2165 << tok::identifier; // missing super class name.
David Blaikie0403cb12016-01-15 23:43:25 +00002166 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002167 }
2168 superClassId = Tok.getIdentifierInfo();
2169 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002170 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002171 ObjCImpDecl = Actions.ActOnStartClassImplementation(
2172 AtLoc, nameId, nameLoc,
2173 superClassId, superClassLoc);
2174
2175 if (Tok.is(tok::l_brace)) // we have ivars
2176 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002177 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2178 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002179
2180 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2181 SmallVector<Decl *, 4> protocols;
2182 SmallVector<SourceLocation, 4> protocolLocs;
2183 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2184 /*warnOnIncompleteProtocols=*/false,
2185 /*ForObjCContainer=*/false,
2186 protocolLAngleLoc, protocolRAngleLoc,
2187 /*consumeLastToken=*/true);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002188 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002189 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002190 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002191
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002192 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002193
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002194 {
2195 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00002196 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002197 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002198 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002199 MaybeParseMicrosoftAttributes(attrs);
2200 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
2201 DeclGroupRef DG = DGP.get();
2202 DeclsInGroup.append(DG.begin(), DG.end());
2203 }
2204 }
2205 }
2206
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00002207 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002208}
Steve Naroff33a1e802007-10-29 21:38:07 +00002209
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002210Parser::DeclGroupPtrTy
2211Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002212 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
2213 "ParseObjCAtEndDeclaration(): Expected @end");
2214 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002215 if (CurParsedObjCImpl)
2216 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002217 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00002218 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002219 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
David Blaikie0403cb12016-01-15 23:43:25 +00002220 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002221}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002222
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002223Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
2224 if (!Finished) {
2225 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00002226 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002227 P.Diag(P.Tok, diag::err_objc_missing_end)
2228 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
2229 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
2230 << Sema::OCK_Implementation;
2231 }
2232 }
Craig Topper161e4db2014-05-21 06:02:52 +00002233 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002234 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002235}
2236
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002237void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
2238 assert(!Finished);
2239 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
2240 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002241 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2242 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002243
2244 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
2245
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002246 if (HasCFunction)
2247 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
2248 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2249 false/*c-functions*/);
2250
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002251 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002252 for (LateParsedObjCMethodContainer::iterator
2253 I = LateParsedObjCMethods.begin(),
2254 E = LateParsedObjCMethods.end(); I != E; ++I)
2255 delete *I;
2256 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002257
2258 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002259}
2260
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002261/// compatibility-alias-decl:
2262/// @compatibility_alias alias-name class-name ';'
2263///
John McCall48871652010-08-21 09:40:31 +00002264Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002265 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
2266 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
2267 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00002268 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002269 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002270 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002271 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002272 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
2273 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00002274 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002275 Diag(Tok, diag::err_expected) << tok::identifier;
Craig Topper161e4db2014-05-21 06:02:52 +00002276 return nullptr;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002277 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002278 IdentifierInfo *classId = Tok.getIdentifierInfo();
2279 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00002280 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00002281 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
2282 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002283}
2284
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002285/// property-synthesis:
2286/// @synthesize property-ivar-list ';'
2287///
2288/// property-ivar-list:
2289/// property-ivar
2290/// property-ivar-list ',' property-ivar
2291///
2292/// property-ivar:
2293/// identifier
2294/// identifier '=' identifier
2295///
John McCall48871652010-08-21 09:40:31 +00002296Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002297 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00002298 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002299 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00002300
Douglas Gregor88e72a02009-11-18 19:45:45 +00002301 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00002302 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002303 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002304 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002305 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002306 }
2307
Douglas Gregor88e72a02009-11-18 19:45:45 +00002308 if (Tok.isNot(tok::identifier)) {
2309 Diag(Tok, diag::err_synthesized_property_name);
2310 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002311 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00002312 }
Craig Topper161e4db2014-05-21 06:02:52 +00002313
2314 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002315 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2316 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002317 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00002318 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002319 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00002320 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002321 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002322 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002323 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002324 }
2325
Chris Lattner0ef13522007-10-09 17:51:17 +00002326 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002327 Diag(Tok, diag::err_expected) << tok::identifier;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002328 break;
2329 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002330 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002331 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002332 }
Manman Ren5b786402016-01-28 18:49:28 +00002333 Actions.ActOnPropertyImplDecl(
2334 getCurScope(), atLoc, propertyLoc, true,
2335 propertyId, propertyIvar, propertyIvarLoc,
2336 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Chris Lattner0ef13522007-10-09 17:51:17 +00002337 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002338 break;
2339 ConsumeToken(); // consume ','
2340 }
Alp Toker383d2c42014-01-01 03:08:43 +00002341 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00002342 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002343}
2344
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002345/// property-dynamic:
2346/// @dynamic property-list
2347///
2348/// property-list:
2349/// identifier
2350/// property-list ',' identifier
2351///
John McCall48871652010-08-21 09:40:31 +00002352Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002353 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2354 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002355 ConsumeToken(); // consume dynamic
Manman Ren0fe61f82016-01-29 19:05:57 +00002356
2357 bool isClassProperty = false;
2358 if (Tok.is(tok::l_paren)) {
2359 ConsumeParen();
2360 const IdentifierInfo *II = Tok.getIdentifierInfo();
2361
2362 if (!II) {
2363 Diag(Tok, diag::err_objc_expected_property_attr) << II;
2364 SkipUntil(tok::r_paren, StopAtSemi);
2365 } else {
2366 SourceLocation AttrName = ConsumeToken(); // consume attribute name
2367 if (II->isStr("class")) {
2368 isClassProperty = true;
2369 if (Tok.isNot(tok::r_paren)) {
2370 Diag(Tok, diag::err_expected) << tok::r_paren;
2371 SkipUntil(tok::r_paren, StopAtSemi);
2372 } else
2373 ConsumeParen();
2374 } else {
2375 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
2376 SkipUntil(tok::r_paren, StopAtSemi);
2377 }
2378 }
2379 }
2380
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002381 while (true) {
2382 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002383 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002384 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002385 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002386 }
2387
2388 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00002389 Diag(Tok, diag::err_expected) << tok::identifier;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002390 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002391 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002392 }
2393
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002394 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2395 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Manman Ren5b786402016-01-28 18:49:28 +00002396 Actions.ActOnPropertyImplDecl(
2397 getCurScope(), atLoc, propertyLoc, false,
2398 propertyId, nullptr, SourceLocation(),
Manman Ren0fe61f82016-01-29 19:05:57 +00002399 isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
Manman Ren5b786402016-01-28 18:49:28 +00002400 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002401
Chris Lattner0ef13522007-10-09 17:51:17 +00002402 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002403 break;
2404 ConsumeToken(); // consume ','
2405 }
Alp Toker383d2c42014-01-01 03:08:43 +00002406 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00002407 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002408}
Mike Stump11289f42009-09-09 15:08:12 +00002409
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002410/// objc-throw-statement:
2411/// throw expression[opt];
2412///
John McCalldadc5752010-08-24 06:29:42 +00002413StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2414 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002415 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00002416 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00002417 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002418 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002419 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002420 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002421 }
2422 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00002423 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00002424 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002425 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002426}
2427
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002428/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002429/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002430///
John McCalldadc5752010-08-24 06:29:42 +00002431StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002432Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002433 ConsumeToken(); // consume synchronized
2434 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002435 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002436 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002437 }
John McCalld9bb7432011-07-27 21:50:02 +00002438
2439 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002440 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00002441 ExprResult operand(ParseExpression());
2442
2443 if (Tok.is(tok::r_paren)) {
2444 ConsumeParen(); // ')'
2445 } else {
2446 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002447 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00002448
2449 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002450 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002451 }
John McCalld9bb7432011-07-27 21:50:02 +00002452
2453 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002454 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00002455 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002456 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002457 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002458 }
Steve Naroffd9c26072008-06-04 20:36:13 +00002459
John McCalld9bb7432011-07-27 21:50:02 +00002460 // Check the @synchronized operand now.
2461 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002462 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002463
John McCalld9bb7432011-07-27 21:50:02 +00002464 // Parse the compound statement within a new scope.
2465 ParseScope bodyScope(this, Scope::DeclScope);
2466 StmtResult body(ParseCompoundStatementBody());
2467 bodyScope.Exit();
2468
2469 // If there was a semantic or parse error earlier with the
2470 // operand, fail now.
2471 if (operand.isInvalid())
2472 return StmtError();
2473
2474 if (body.isInvalid())
2475 body = Actions.ActOnNullStmt(Tok.getLocation());
2476
2477 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002478}
2479
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002480/// objc-try-catch-statement:
2481/// @try compound-statement objc-catch-list[opt]
2482/// @try compound-statement objc-catch-list[opt] @finally compound-statement
2483///
2484/// objc-catch-list:
2485/// @catch ( parameter-declaration ) compound-statement
2486/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2487/// catch-parameter-declaration:
2488/// parameter-declaration
2489/// '...' [OBJC2]
2490///
John McCalldadc5752010-08-24 06:29:42 +00002491StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002492 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002493
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002494 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00002495 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002496 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002497 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002498 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00002499 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00002500 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002501 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00002502 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002503 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002504 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002505 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002506
Chris Lattner0ef13522007-10-09 17:51:17 +00002507 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002508 // At this point, we need to lookahead to determine if this @ is the start
2509 // of an @catch or @finally. We don't want to consume the @ token if this
2510 // is an @try or @encode or something else.
2511 Token AfterAt = GetLookAheadToken(1);
2512 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2513 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2514 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002515
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002516 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002517 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002518 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002519 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002520 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002521 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00002522 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002523 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002524 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002525 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00002526 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002527 ParseDeclarator(ParmDecl);
2528
Douglas Gregore11ee112010-04-23 23:01:43 +00002529 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002530 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002531 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002532 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002533 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002534
Steve Naroff65a00892009-04-07 22:56:58 +00002535 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002536
Steve Naroff65a00892009-04-07 22:56:58 +00002537 if (Tok.is(tok::r_paren))
2538 RParenLoc = ConsumeParen();
2539 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002540 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002541
John McCalldadc5752010-08-24 06:29:42 +00002542 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002543 if (Tok.is(tok::l_brace))
2544 CatchBody = ParseCompoundStatementBody();
2545 else
Alp Tokerec543272013-12-24 09:48:30 +00002546 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002547 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002548 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00002549
John McCalldadc5752010-08-24 06:29:42 +00002550 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00002551 RParenLoc,
2552 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002553 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002554 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002555 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002556
Steve Naroffe6016792008-02-05 21:27:35 +00002557 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002558 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2559 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002560 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002561 }
2562 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002563 } else {
2564 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002565 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002566 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002567
John McCalldadc5752010-08-24 06:29:42 +00002568 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002569 if (Tok.is(tok::l_brace))
2570 FinallyBody = ParseCompoundStatementBody();
2571 else
Alp Tokerec543272013-12-24 09:48:30 +00002572 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002573 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002574 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002575 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002576 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002577 catch_or_finally_seen = true;
2578 break;
2579 }
2580 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002581 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002582 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002583 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002584 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002585
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002586 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002587 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002588 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002589}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002590
John McCall31168b02011-06-15 23:02:42 +00002591/// objc-autoreleasepool-statement:
2592/// @autoreleasepool compound-statement
2593///
2594StmtResult
2595Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2596 ConsumeToken(); // consume autoreleasepool
2597 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002598 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002599 return StmtError();
2600 }
2601 // Enter a scope to hold everything within the compound stmt. Compound
2602 // statements can always hold declarations.
2603 ParseScope BodyScope(this, Scope::DeclScope);
2604
2605 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2606
2607 BodyScope.Exit();
2608 if (AutoreleasePoolBody.isInvalid())
2609 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2610 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002611 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002612}
2613
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002614/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2615/// for later parsing.
2616void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
2617 LexedMethod* LM = new LexedMethod(this, MDecl);
2618 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2619 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002620 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002621 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002622 if (Tok.is(tok::kw_try)) {
2623 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002624 if (Tok.is(tok::colon)) {
2625 Toks.push_back(Tok);
2626 ConsumeToken();
2627 while (Tok.isNot(tok::l_brace)) {
2628 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2629 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2630 }
2631 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002632 Toks.push_back(Tok); // also store '{'
2633 }
2634 else if (Tok.is(tok::colon)) {
2635 ConsumeToken();
Richard Smithb9fa9962015-08-21 03:04:33 +00002636 // FIXME: This is wrong, due to C++11 braced initialization.
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002637 while (Tok.isNot(tok::l_brace)) {
2638 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2639 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2640 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002641 Toks.push_back(Tok); // also store '{'
2642 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002643 ConsumeBrace();
2644 // Consume everything up to (and including) the matching right brace.
2645 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002646 while (Tok.is(tok::kw_catch)) {
2647 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2648 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2649 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002650}
2651
Steve Naroff09bf8152007-09-06 21:24:23 +00002652/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002653///
John McCall48871652010-08-21 09:40:31 +00002654Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002655 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002656
John McCallfaf5fb42010-08-26 23:41:50 +00002657 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2658 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002659
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002660 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002661 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002662 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002663 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002664 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002665 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002666 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002667 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002668
Steve Naroffbb875722007-11-11 19:54:21 +00002669 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002670 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002671 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002672
Steve Naroffbb875722007-11-11 19:54:21 +00002673 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002674 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002675
Steve Naroffbb875722007-11-11 19:54:21 +00002676 // If we didn't find the '{', bail out.
2677 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002678 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002679 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002680
2681 if (!MDecl) {
2682 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002683 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002684 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002685 }
2686
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002687 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002688 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002689 assert (CurParsedObjCImpl
2690 && "ParseObjCMethodDefinition - Method out of @implementation");
2691 // Consume the tokens and store them for later parsing.
2692 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002693 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002694}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002695
John McCalldadc5752010-08-24 06:29:42 +00002696StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002697 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002698 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002699 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002700 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002701 }
2702
2703 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002704 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002705
2706 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002707 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002708
2709 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002710 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002711
2712 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2713 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002714
2715 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2716 getLangOpts().DebuggerSupport) {
2717 SkipUntil(tok::semi);
2718 return Actions.ActOnNullStmt(Tok.getLocation());
2719 }
2720
John McCalldadc5752010-08-24 06:29:42 +00002721 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002722 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002723 // If the expression is invalid, skip ahead to the next semicolon. Not
2724 // doing this opens us up to the possibility of infinite loops if
2725 // ParseExpression does not consume any tokens.
2726 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002727 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002728 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002729
Steve Naroffe6016792008-02-05 21:27:35 +00002730 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002731 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002732 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002733}
2734
John McCalldadc5752010-08-24 06:29:42 +00002735ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002736 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002737 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002738 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002739 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002740 return ExprError();
2741
Ted Kremeneke65b0862012-03-06 20:05:56 +00002742 case tok::minus:
2743 case tok::plus: {
2744 tok::TokenKind Kind = Tok.getKind();
2745 SourceLocation OpLoc = ConsumeToken();
2746
2747 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002748 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002749 switch (Kind) {
2750 case tok::minus: Symbol = "-"; break;
2751 case tok::plus: Symbol = "+"; break;
2752 default: llvm_unreachable("missing unary operator case");
2753 }
2754 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2755 << Symbol;
2756 return ExprError();
2757 }
2758
2759 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2760 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002761 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002762 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002763 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002764
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002765 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002766 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002767 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002768
2769 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002770 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002771 }
2772
Chris Lattnere002fbe2007-12-12 01:04:12 +00002773 case tok::string_literal: // primary-expression: string-literal
2774 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002775 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002776
2777 case tok::char_constant:
2778 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2779
2780 case tok::numeric_constant:
2781 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2782
2783 case tok::kw_true: // Objective-C++, etc.
2784 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2785 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2786 case tok::kw_false: // Objective-C++, etc.
2787 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2788 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2789
2790 case tok::l_square:
2791 // Objective-C array literal
2792 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2793
2794 case tok::l_brace:
2795 // Objective-C dictionary literal
2796 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2797
Patrick Beard0caa3942012-04-19 00:25:12 +00002798 case tok::l_paren:
2799 // Objective-C boxed expression
2800 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2801
Chris Lattnere002fbe2007-12-12 01:04:12 +00002802 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002803 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002804 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002805
Chris Lattner197a3012008-08-05 06:19:09 +00002806 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2807 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002808 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002809 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002810 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002811 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002812 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002813 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002814 const char *str = nullptr;
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002815 if (GetLookAheadToken(1).is(tok::l_brace)) {
2816 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2817 str =
2818 ch == 't' ? "try"
2819 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002820 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002821 }
2822 if (str) {
2823 SourceLocation kwLoc = Tok.getLocation();
2824 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2825 FixItHint::CreateReplacement(kwLoc, str));
2826 }
2827 else
2828 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2829 }
Chris Lattner197a3012008-08-05 06:19:09 +00002830 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002831 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002832}
2833
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002834/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002835///
2836/// This routine parses the receiver of a message send in
2837/// Objective-C++ either as a type or as an expression. Note that this
2838/// routine must not be called to parse a send to 'super', since it
2839/// has no way to return such a result.
2840///
2841/// \param IsExpr Whether the receiver was parsed as an expression.
2842///
2843/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2844/// IsExpr is true), the parsed expression. If the receiver was parsed
2845/// as a type (\c IsExpr is false), the parsed type.
2846///
2847/// \returns True if an error occurred during parsing or semantic
2848/// analysis, in which case the arguments do not have valid
2849/// values. Otherwise, returns false for a successful parse.
2850///
2851/// objc-receiver: [C++]
2852/// 'super' [not parsed here]
2853/// expression
2854/// simple-type-specifier
2855/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002856bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002857 InMessageExpressionRAIIObject InMessage(*this, true);
2858
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002859 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2860 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002861 TryAnnotateTypeOrScopeToken();
2862
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002863 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002864 // objc-receiver:
2865 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002866 // Make sure any typos in the receiver are corrected or diagnosed, so that
2867 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2868 // only the things that are valid ObjC receivers?
2869 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002870 if (Receiver.isInvalid())
2871 return true;
2872
2873 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002874 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002875 return false;
2876 }
2877
2878 // objc-receiver:
2879 // typename-specifier
2880 // simple-type-specifier
2881 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002882 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002883 ParseCXXSimpleTypeSpecifier(DS);
2884
2885 if (Tok.is(tok::l_paren)) {
2886 // If we see an opening parentheses at this point, we are
2887 // actually parsing an expression that starts with a
2888 // function-style cast, e.g.,
2889 //
2890 // postfix-expression:
2891 // simple-type-specifier ( expression-list [opt] )
2892 // typename-specifier ( expression-list [opt] )
2893 //
2894 // Parse the remainder of this case, then the (optional)
2895 // postfix-expression suffix, followed by the (optional)
2896 // right-hand side of the binary expression. We have an
2897 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002898 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002899 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002900 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002901 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002902 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002903 if (Receiver.isInvalid())
2904 return true;
2905
2906 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002907 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002908 return false;
2909 }
2910
2911 // We have a class message. Turn the simple-type-specifier or
2912 // typename-specifier we parsed into a type and parse the
2913 // remainder of the class message.
2914 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002915 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002916 if (Type.isInvalid())
2917 return true;
2918
2919 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002920 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002921 return false;
2922}
2923
Douglas Gregor990ccac2010-05-31 14:40:22 +00002924/// \brief Determine whether the parser is currently referring to a an
2925/// Objective-C message send, using a simplified heuristic to avoid overhead.
2926///
2927/// This routine will only return true for a subset of valid message-send
2928/// expressions.
2929bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002930 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002931 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002932 return GetLookAheadToken(1).is(tok::identifier) &&
2933 GetLookAheadToken(2).is(tok::identifier);
2934}
2935
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002936bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002937 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002938 InMessageExpression)
2939 return false;
2940
2941
2942 ParsedType Type;
2943
2944 if (Tok.is(tok::annot_typename))
2945 Type = getTypeAnnotation(Tok);
2946 else if (Tok.is(tok::identifier))
2947 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2948 getCurScope());
2949 else
2950 return false;
2951
2952 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2953 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002954 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002955 if (Tok.is(tok::identifier))
2956 TryAnnotateTypeOrScopeToken();
2957
2958 return Tok.is(tok::annot_typename);
2959 }
2960 }
2961
2962 return false;
2963}
2964
Mike Stump11289f42009-09-09 15:08:12 +00002965/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002966/// '[' objc-receiver objc-message-args ']'
2967///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002968/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002969/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002970/// expression
2971/// class-name
2972/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002973///
John McCalldadc5752010-08-24 06:29:42 +00002974ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002975 assert(Tok.is(tok::l_square) && "'[' expected");
2976 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2977
Douglas Gregora817a192010-05-27 23:06:34 +00002978 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002979 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002980 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00002981 return ExprError();
2982 }
2983
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002984 InMessageExpressionRAIIObject InMessage(*this, true);
2985
David Blaikiebbafb8a2012-03-11 07:00:24 +00002986 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002987 // We completely separate the C and C++ cases because C++ requires
2988 // more complicated (read: slower) parsing.
2989
2990 // Handle send to super.
2991 // FIXME: This doesn't benefit from the same typo-correction we
2992 // get in Objective-C.
2993 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002994 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
David Blaikieefdccaa2016-01-15 23:43:34 +00002995 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
2996 nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002997
2998 // Parse the receiver, which is either a type or an expression.
2999 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00003000 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00003001 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003002 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003003 return ExprError();
3004 }
3005
3006 if (IsExpr)
David Blaikieefdccaa2016-01-15 23:43:34 +00003007 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3008 static_cast<Expr *>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00003009
3010 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00003011 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00003012 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00003013 }
3014
3015 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00003016 IdentifierInfo *Name = Tok.getIdentifierInfo();
3017 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00003018 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003019 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00003020 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00003021 NextToken().is(tok::period),
3022 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00003023 case Sema::ObjCSuperMessage:
David Blaikieefdccaa2016-01-15 23:43:34 +00003024 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3025 nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003026
John McCallfaf5fb42010-08-26 23:41:50 +00003027 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00003028 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003029 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003030 return ExprError();
3031 }
3032
Douglas Gregore5798dc2010-04-21 20:38:13 +00003033 ConsumeToken(); // the type name
3034
Douglas Gregore83b9562015-07-07 03:57:53 +00003035 // Parse type arguments and protocol qualifiers.
3036 if (Tok.is(tok::less)) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003037 SourceLocation NewEndLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00003038 TypeResult NewReceiverType
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003039 = parseObjCTypeArgsAndProtocolQualifiers(NameLoc, ReceiverType,
3040 /*consumeLastToken=*/true,
3041 NewEndLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00003042 if (!NewReceiverType.isUsable()) {
3043 SkipUntil(tok::r_square, StopAtSemi);
3044 return ExprError();
3045 }
3046
3047 ReceiverType = NewReceiverType.get();
3048 }
3049
Douglas Gregore5798dc2010-04-21 20:38:13 +00003050 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00003051 ReceiverType, nullptr);
3052
John McCallfaf5fb42010-08-26 23:41:50 +00003053 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003054 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00003055 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00003056 }
Chris Lattner8f697062008-01-25 18:59:06 +00003057 }
Chris Lattnera36ec422010-04-11 08:28:14 +00003058
3059 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00003060 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003061 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003062 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003063 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00003064 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003065
David Blaikieefdccaa2016-01-15 23:43:34 +00003066 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3067 Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00003068}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003069
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003070/// \brief Parse the remainder of an Objective-C message following the
3071/// '[' objc-receiver.
3072///
3073/// This routine handles sends to super, class messages (sent to a
3074/// class name), and instance messages (sent to an object), and the
3075/// target is represented by \p SuperLoc, \p ReceiverType, or \p
3076/// ReceiverExpr, respectively. Only one of these parameters may have
3077/// a valid value.
3078///
3079/// \param LBracLoc The location of the opening '['.
3080///
3081/// \param SuperLoc If this is a send to 'super', the location of the
3082/// 'super' keyword that indicates a send to the superclass.
3083///
3084/// \param ReceiverType If this is a class message, the type of the
3085/// class we are sending a message to.
3086///
3087/// \param ReceiverExpr If this is an instance message, the expression
3088/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00003089///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003090/// objc-message-args:
3091/// objc-selector
3092/// objc-keywordarg-list
3093///
3094/// objc-keywordarg-list:
3095/// objc-keywordarg
3096/// objc-keywordarg-list objc-keywordarg
3097///
Mike Stump11289f42009-09-09 15:08:12 +00003098/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003099/// selector-name[opt] ':' objc-keywordexpr
3100///
3101/// objc-keywordexpr:
3102/// nonempty-expr-list
3103///
3104/// nonempty-expr-list:
3105/// assignment-expression
3106/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003107///
John McCalldadc5752010-08-24 06:29:42 +00003108ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00003109Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003110 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00003111 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00003112 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003113 InMessageExpressionRAIIObject InMessage(*this, true);
3114
Steve Naroffeae65032009-11-07 02:08:14 +00003115 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003116 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003117 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003118 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003119 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003120 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003121 false);
Steve Naroffeae65032009-11-07 02:08:14 +00003122 else
John McCallb268a282010-08-23 23:25:46 +00003123 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003124 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003125 cutOffParsing();
3126 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00003127 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00003128
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003129 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00003130 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003131 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003132
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003133 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003134 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003135 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00003136
Chris Lattner0ef13522007-10-09 17:51:17 +00003137 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003138 while (1) {
3139 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00003140 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003141 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00003142
Alp Toker383d2c42014-01-01 03:08:43 +00003143 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00003144 // We must manually skip to a ']', otherwise the expression skipper will
3145 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3146 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003147 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003148 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003149 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003150
Mike Stump11289f42009-09-09 15:08:12 +00003151 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003152
3153 if (Tok.is(tok::code_completion)) {
3154 if (SuperLoc.isValid())
3155 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003156 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003157 /*AtArgumentEpression=*/true);
3158 else if (ReceiverType)
3159 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003160 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003161 /*AtArgumentEpression=*/true);
3162 else
3163 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003164 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003165 /*AtArgumentEpression=*/true);
3166
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003167 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003168 return ExprError();
3169 }
3170
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00003171 ExprResult Expr;
3172 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3173 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3174 Expr = ParseBraceInitializer();
3175 } else
3176 Expr = ParseAssignmentExpression();
3177
3178 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003179 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00003180 // We must manually skip to a ']', otherwise the expression skipper will
3181 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3182 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003183 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003184 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00003185 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003186
Steve Naroff486760a2007-09-17 20:25:27 +00003187 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003188 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003189
Douglas Gregor1b605f72009-11-19 01:08:35 +00003190 // Code completion after each argument.
3191 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003192 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003193 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003194 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003195 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003196 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003197 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003198 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003199 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00003200 else
John McCallb268a282010-08-23 23:25:46 +00003201 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003202 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003203 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003204 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003205 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00003206 }
3207
Steve Naroff486760a2007-09-17 20:25:27 +00003208 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00003209 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00003210 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003211 break;
3212 // We have a selector or a colon, continue parsing.
3213 }
3214 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00003215 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003216 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00003217 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00003218 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00003219 if (Tok.is(tok::colon))
3220 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003221 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003222 if (Tok.is(tok::colon)) {
3223 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
3224 FixItHint::CreateRemoval(commaLoc);
3225 }
Chris Lattner197a3012008-08-05 06:19:09 +00003226 // We must manually skip to a ']', otherwise the expression skipper will
3227 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3228 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003229 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003230 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003231 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003232
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003233 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003234 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003235 }
3236 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00003237 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003238
Chris Lattner197a3012008-08-05 06:19:09 +00003239 // We must manually skip to a ']', otherwise the expression skipper will
3240 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3241 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003242 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003243 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003244 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00003245
Chris Lattner0ef13522007-10-09 17:51:17 +00003246 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00003247 Diag(Tok, diag::err_expected)
3248 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00003249 // We must manually skip to a ']', otherwise the expression skipper will
3250 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3251 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003252 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003253 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003254 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003255
Chris Lattner8f697062008-01-25 18:59:06 +00003256 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003257
Steve Naroffe61bfa82007-10-05 18:42:47 +00003258 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003259 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00003260 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003261 KeyLocs.push_back(Loc);
3262 }
Chris Lattner5700fab2007-10-07 02:00:24 +00003263 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003264
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003265 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003266 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003267 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003268 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003269 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003270 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00003271 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003272 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003273}
3274
John McCalldadc5752010-08-24 06:29:42 +00003275ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
3276 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003277 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003278
Chris Lattnere002fbe2007-12-12 01:04:12 +00003279 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
3280 // expressions. At this point, we know that the only valid thing that starts
3281 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003282 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003283 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00003284 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003285 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003286
Chris Lattnere002fbe2007-12-12 01:04:12 +00003287 while (Tok.is(tok::at)) {
3288 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00003289
Sebastian Redlc13f2682008-12-09 20:22:58 +00003290 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00003291 if (!isTokenStringLiteral())
3292 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00003293
John McCalldadc5752010-08-24 06:29:42 +00003294 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003295 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003296 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003297
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003298 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00003299 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003300
Craig Topper883dd332015-12-24 23:58:11 +00003301 return Actions.ParseObjCStringLiteral(AtLocs.data(), AtStrings);
Anders Carlsson76f4a902007-08-21 17:43:55 +00003302}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003303
Ted Kremeneke65b0862012-03-06 20:05:56 +00003304/// ParseObjCBooleanLiteral -
3305/// objc-scalar-literal : '@' boolean-keyword
3306/// ;
3307/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
3308/// ;
3309ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
3310 bool ArgValue) {
3311 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
3312 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
3313}
3314
3315/// ParseObjCCharacterLiteral -
3316/// objc-scalar-literal : '@' character-literal
3317/// ;
3318ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
3319 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
3320 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003321 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003322 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003323 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003324 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003325}
3326
3327/// ParseObjCNumericLiteral -
3328/// objc-scalar-literal : '@' scalar-literal
3329/// ;
3330/// scalar-literal : | numeric-constant /* any numeric constant. */
3331/// ;
3332ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3333 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3334 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003335 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003336 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003337 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003338 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003339}
3340
Patrick Beard0caa3942012-04-19 00:25:12 +00003341/// ParseObjCBoxedExpr -
3342/// objc-box-expression:
3343/// @( assignment-expression )
3344ExprResult
3345Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3346 if (Tok.isNot(tok::l_paren))
3347 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3348
3349 BalancedDelimiterTracker T(*this, tok::l_paren);
3350 T.consumeOpen();
3351 ExprResult ValueExpr(ParseAssignmentExpression());
3352 if (T.consumeClose())
3353 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00003354
3355 if (ValueExpr.isInvalid())
3356 return ExprError();
3357
Patrick Beard0caa3942012-04-19 00:25:12 +00003358 // Wrap the sub-expression in a parenthesized expression, to distinguish
3359 // a boxed expression from a literal.
3360 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003361 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00003362 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003363 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00003364}
3365
Ted Kremeneke65b0862012-03-06 20:05:56 +00003366ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00003367 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003368 ConsumeBracket(); // consume the l_square.
3369
3370 while (Tok.isNot(tok::r_square)) {
3371 // Parse list of array element expressions (all must be id types).
3372 ExprResult Res(ParseAssignmentExpression());
3373 if (Res.isInvalid()) {
3374 // We must manually skip to a ']', otherwise the expression skipper will
3375 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3376 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003377 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003378 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003379 }
3380
3381 // Parse the ellipsis that indicates a pack expansion.
3382 if (Tok.is(tok::ellipsis))
3383 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
3384 if (Res.isInvalid())
3385 return true;
3386
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003387 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003388
3389 if (Tok.is(tok::comma))
3390 ConsumeToken(); // Eat the ','.
3391 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00003392 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3393 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003394 }
3395 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Benjamin Kramerf0623432012-08-23 22:51:59 +00003396 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00003397 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003398}
3399
3400ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3401 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3402 ConsumeBrace(); // consume the l_square.
3403 while (Tok.isNot(tok::r_brace)) {
3404 // Parse the comma separated key : value expressions.
3405 ExprResult KeyExpr;
3406 {
3407 ColonProtectionRAIIObject X(*this);
3408 KeyExpr = ParseAssignmentExpression();
3409 if (KeyExpr.isInvalid()) {
3410 // We must manually skip to a '}', otherwise the expression skipper will
3411 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3412 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003413 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003414 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003415 }
3416 }
3417
Alp Toker383d2c42014-01-01 03:08:43 +00003418 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003419 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00003420 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003421 }
3422
3423 ExprResult ValueExpr(ParseAssignmentExpression());
3424 if (ValueExpr.isInvalid()) {
3425 // We must manually skip to a '}', otherwise the expression skipper will
3426 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3427 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003428 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003429 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003430 }
3431
3432 // Parse the ellipsis that designates this as a pack expansion.
3433 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00003434 if (getLangOpts().CPlusPlus)
3435 TryConsumeToken(tok::ellipsis, EllipsisLoc);
3436
Ted Kremeneke65b0862012-03-06 20:05:56 +00003437 // We have a valid expression. Collect it in a vector so we can
3438 // build the argument list.
3439 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00003440 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00003441 };
3442 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00003443
3444 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00003445 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3446 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003447 }
3448 SourceLocation EndLoc = ConsumeBrace();
3449
3450 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00003451 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
Craig Topperd4336e02015-12-24 23:58:15 +00003452 Elements);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003453}
3454
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003455/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00003456/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00003457ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003458Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00003459 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003460
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003461 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003462
Chris Lattner197a3012008-08-05 06:19:09 +00003463 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003464 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3465
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003466 BalancedDelimiterTracker T(*this, tok::l_paren);
3467 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003468
Douglas Gregor220cac52009-02-18 17:45:20 +00003469 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003470
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003471 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003472
Douglas Gregor220cac52009-02-18 17:45:20 +00003473 if (Ty.isInvalid())
3474 return ExprError();
3475
Nico Webera7c7e602012-12-31 00:28:03 +00003476 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3477 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003478}
Anders Carlssone01493d2007-08-23 15:25:28 +00003479
3480/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00003481/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00003482ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003483Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00003484 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003485
Chris Lattner197a3012008-08-05 06:19:09 +00003486 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003487 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3488
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003489 BalancedDelimiterTracker T(*this, tok::l_paren);
3490 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003491
Chris Lattner197a3012008-08-05 06:19:09 +00003492 if (Tok.isNot(tok::identifier))
Alp Tokerec543272013-12-24 09:48:30 +00003493 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003494
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003495 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00003496 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003497
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003498 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00003499
Nico Webera7c7e602012-12-31 00:28:03 +00003500 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3501 T.getOpenLocation(), ProtoIdLoc,
3502 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00003503}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003504
3505/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003506/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003507ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003508 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003509
Chris Lattner197a3012008-08-05 06:19:09 +00003510 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003511 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3512
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003513 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003514 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003515
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003516 BalancedDelimiterTracker T(*this, tok::l_paren);
3517 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003518 bool HasOptionalParen = Tok.is(tok::l_paren);
3519 if (HasOptionalParen)
3520 ConsumeParen();
3521
Douglas Gregor67c692c2010-08-26 15:07:07 +00003522 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003523 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003524 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003525 return ExprError();
3526 }
3527
Chris Lattner4f472a32009-04-11 18:13:45 +00003528 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003529 if (!SelIdent && // missing selector name.
3530 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003531 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003532
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003533 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003534
Steve Naroff152dd812007-12-05 22:21:29 +00003535 unsigned nColons = 0;
3536 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003537 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003538 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003539 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003540 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003541 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3542 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003543 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003544
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003545 if (Tok.is(tok::r_paren))
3546 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003547
3548 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003549 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003550 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003551 return ExprError();
3552 }
3553
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003554 // Check for another keyword selector.
3555 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003556 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003557 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003558 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003559 break;
3560 }
Steve Naroff152dd812007-12-05 22:21:29 +00003561 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003562 if (HasOptionalParen && Tok.is(tok::r_paren))
3563 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003564 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003565 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003566 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3567 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003568 T.getCloseLocation(),
3569 !HasOptionalParen);
Gabor Greif24032f12007-10-19 15:38:32 +00003570 }
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003571
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003572void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3573 // MCDecl might be null due to error in method or c-function prototype, etc.
3574 Decl *MCDecl = LM.D;
3575 bool skip = MCDecl &&
3576 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3577 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3578 if (skip)
3579 return;
3580
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003581 // Save the current token position.
3582 SourceLocation OrigLoc = Tok.getLocation();
3583
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003584 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
3585 // Append the current token at the end of the new token stream so that it
3586 // doesn't get lost.
3587 LM.Toks.push_back(Tok);
David Blaikie2eabcc92016-02-09 18:52:09 +00003588 PP.EnterTokenStream(LM.Toks, true);
3589
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003590 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003591 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003592
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003593 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3594 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003595 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003596 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003597 parseMethod
3598 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
3599 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003600
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003601 // Tell the actions module that we have entered a method or c-function definition
3602 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003603 if (parseMethod)
3604 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3605 else
3606 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003607 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003608 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003609 else {
3610 if (Tok.is(tok::colon))
3611 ParseConstructorInitializer(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003612 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003613 }
Fariborz Jahanian656b5a02012-08-09 21:12:39 +00003614
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003615 if (Tok.getLocation() != OrigLoc) {
3616 // Due to parsing error, we either went over the cached tokens or
3617 // there are still cached tokens left. If it's the latter case skip the
3618 // leftover tokens.
3619 // Since this is an uncommon situation that should be avoided, use the
3620 // expensive isBeforeInTranslationUnit call.
3621 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3622 OrigLoc))
3623 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3624 ConsumeAnyToken();
3625 }
3626
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003627 return;
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003628}