blob: 0ac418ad70c7d23d65074df5710e9f49a462530e [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 Gregor813a0662015-06-19 18:14:38 +000015#include "clang/AST/ASTContext.h"
Jordan Rose1e879d82018-03-23 00:07:18 +000016#include "clang/AST/PrettyDeclStackTrace.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"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000019#include "clang/Parse/RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/DeclSpec.h"
21#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"
Eugene Zelenko1ced5092016-02-12 22:53:10 +000024
Chris Lattnerda59c2f2006-11-05 02:08:13 +000025using namespace clang;
26
Nico Weber04e213b2013-04-03 17:36:11 +000027/// Skips attributes after an Objective-C @ directive. Emits a diagnostic.
Nico Weber69a79142013-04-04 00:15:10 +000028void Parser::MaybeSkipAttributes(tok::ObjCKeywordKind Kind) {
Nico Weber04e213b2013-04-03 17:36:11 +000029 ParsedAttributes attrs(AttrFactory);
30 if (Tok.is(tok::kw___attribute)) {
Nico Weber69a79142013-04-04 00:15:10 +000031 if (Kind == tok::objc_interface || Kind == tok::objc_protocol)
32 Diag(Tok, diag::err_objc_postfix_attribute_hint)
33 << (Kind == tok::objc_protocol);
34 else
35 Diag(Tok, diag::err_objc_postfix_attribute);
Nico Weber04e213b2013-04-03 17:36:11 +000036 ParseGNUAttributes(attrs);
37 }
38}
Chris Lattnerda59c2f2006-11-05 02:08:13 +000039
Chris Lattner3a907162008-12-08 21:53:24 +000040/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000041/// external-declaration: [C99 6.9]
42/// [OBJC] objc-class-definition
Steve Naroffe0933392007-10-29 21:39:29 +000043/// [OBJC] objc-class-declaration
44/// [OBJC] objc-alias-declaration
45/// [OBJC] objc-protocol-definition
46/// [OBJC] objc-method-definition
47/// [OBJC] '@' 'end'
Aaron Ballman1c606c22018-02-12 13:38:25 +000048Parser::DeclGroupPtrTy
49Parser::ParseObjCAtDirectives(ParsedAttributesWithRange &Attrs) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000050 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000051
Douglas Gregorf48706c2009-12-07 09:27:33 +000052 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000053 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000054 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +000055 return nullptr;
Douglas Gregorf48706c2009-12-07 09:27:33 +000056 }
Craig Topper161e4db2014-05-21 06:02:52 +000057
58 Decl *SingleDecl = nullptr;
Steve Naroff7c348172007-08-23 18:16:40 +000059 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000060 case tok::objc_class:
61 return ParseObjCAtClassDeclaration(AtLoc);
Aaron Ballman1c606c22018-02-12 13:38:25 +000062 case tok::objc_interface:
63 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, Attrs);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000064 break;
Aaron Ballman1c606c22018-02-12 13:38:25 +000065 case tok::objc_protocol:
66 return ParseObjCAtProtocolDeclaration(AtLoc, Attrs);
Chris Lattnerce90ef52008-08-23 02:02:23 +000067 case tok::objc_implementation:
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +000068 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000069 case tok::objc_end:
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +000070 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000071 case tok::objc_compatibility_alias:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000072 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
73 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000074 case tok::objc_synthesize:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000075 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
76 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000077 case tok::objc_dynamic:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000078 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
79 break;
Douglas Gregorc50d4922012-12-11 22:11:52 +000080 case tok::objc_import:
Hamza Sood81fe14e2017-11-21 09:42:42 +000081 if (getLangOpts().Modules || getLangOpts().DebuggerSupport) {
82 SingleDecl = ParseModuleImport(AtLoc);
83 break;
84 }
Manman Rendfcf1cb2017-01-20 20:03:00 +000085 Diag(AtLoc, diag::err_atimport);
Fariborz Jahaniana773d082014-03-26 22:02:43 +000086 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;
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000102
Richard Smith3df3f1d2015-11-03 01:19:56 +0000103public:
104 ObjCTypeParamListScope(Sema &Actions, Scope *S)
105 : Actions(Actions), S(S), Params(nullptr) {}
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000106
Richard Smith3df3f1d2015-11-03 01:19:56 +0000107 ~ObjCTypeParamListScope() {
108 leave();
109 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000110
Richard Smith3df3f1d2015-11-03 01:19:56 +0000111 void enter(ObjCTypeParamList *P) {
112 assert(!Params);
113 Params = P;
114 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000115
Richard Smith3df3f1d2015-11-03 01:19:56 +0000116 void leave() {
117 if (Params)
118 Actions.popObjCTypeParamList(S, Params);
119 Params = nullptr;
120 }
121};
122
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000123///
Mike Stump11289f42009-09-09 15:08:12 +0000124/// objc-class-declaration:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000125/// '@' 'class' objc-class-forward-decl (',' objc-class-forward-decl)* ';'
126///
127/// objc-class-forward-decl:
128/// identifier objc-type-parameter-list[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000129///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000130Parser::DeclGroupPtrTy
131Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000132 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000133 SmallVector<IdentifierInfo *, 8> ClassNames;
134 SmallVector<SourceLocation, 8> ClassLocs;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000135 SmallVector<ObjCTypeParamList *, 8> ClassTypeParams;
Mike Stump11289f42009-09-09 15:08:12 +0000136
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000137 while (1) {
Nico Weber69a79142013-04-04 00:15:10 +0000138 MaybeSkipAttributes(tok::objc_class);
Alex Lorenzf1278212017-04-11 15:01:53 +0000139 if (expectIdentifier()) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000140 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000141 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000142 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000143 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +0000144 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000145 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000146
Douglas Gregor85f3f952015-07-07 03:57:15 +0000147 // Parse the optional objc-type-parameter-list.
148 ObjCTypeParamList *TypeParams = nullptr;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000149 if (Tok.is(tok::less))
Douglas Gregor85f3f952015-07-07 03:57:15 +0000150 TypeParams = parseObjCTypeParamList();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000151 ClassTypeParams.push_back(TypeParams);
Alp Toker383d2c42014-01-01 03:08:43 +0000152 if (!TryConsumeToken(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000153 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000154 }
Mike Stump11289f42009-09-09 15:08:12 +0000155
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000156 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +0000157 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@class"))
Craig Topper161e4db2014-05-21 06:02:52 +0000158 return Actions.ConvertDeclToDeclGroup(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000159
Ted Kremeneka26da852009-11-17 23:12:20 +0000160 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
161 ClassLocs.data(),
Douglas Gregor85f3f952015-07-07 03:57:15 +0000162 ClassTypeParams,
Ted Kremeneka26da852009-11-17 23:12:20 +0000163 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000164}
165
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000166void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
167{
168 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
169 if (ock == Sema::OCK_None)
170 return;
171
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000172 Decl *Decl = Actions.getObjCDeclContext();
173 if (CurParsedObjCImpl) {
174 CurParsedObjCImpl->finish(AtLoc);
175 } else {
176 Actions.ActOnAtEnd(getCurScope(), AtLoc);
177 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000178 Diag(AtLoc, diag::err_objc_missing_end)
179 << FixItHint::CreateInsertion(AtLoc, "@end\n");
180 if (Decl)
181 Diag(Decl->getLocStart(), diag::note_objc_container_start)
182 << (int) ock;
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000183}
184
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000185///
186/// objc-interface:
187/// objc-class-interface-attributes[opt] objc-class-interface
188/// objc-category-interface
189///
190/// objc-class-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000191/// '@' 'interface' identifier objc-type-parameter-list[opt]
192/// objc-superclass[opt] objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000193/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000194/// objc-interface-decl-list
195/// @end
196///
197/// objc-category-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000198/// '@' 'interface' identifier objc-type-parameter-list[opt]
199/// '(' identifier[opt] ')' objc-protocol-refs[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000200/// objc-interface-decl-list
201/// @end
202///
203/// objc-superclass:
Douglas Gregore9d95f12015-07-07 03:57:35 +0000204/// ':' identifier objc-type-arguments[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000205///
206/// objc-class-interface-attributes:
207/// __attribute__((visibility("default")))
208/// __attribute__((visibility("hidden")))
209/// __attribute__((deprecated))
210/// __attribute__((unavailable))
211/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardacfbe9e2012-04-06 18:12:22 +0000212/// __attribute__((objc_root_class))
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000213///
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000214Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000215 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000216 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000217 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000218 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000219 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000220
Douglas Gregor49c22a72009-11-18 16:26:39 +0000221 // Code completion after '@interface'.
222 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000223 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000224 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000225 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000226 }
227
Nico Weber69a79142013-04-04 00:15:10 +0000228 MaybeSkipAttributes(tok::objc_interface);
Nico Weber04e213b2013-04-03 17:36:11 +0000229
Alex Lorenzf1278212017-04-11 15:01:53 +0000230 if (expectIdentifier())
231 return nullptr; // missing class or category name.
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;
Douglas Gregor0c254a02011-09-23 19:19:41 +0000276
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000277 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000278 assert(LAngleLoc.isInvalid() && "Cannot have already parsed protocols");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000279 SmallVector<Decl *, 8> ProtocolRefs;
280 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000281 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000282 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000283 LAngleLoc, EndProtoLoc,
284 /*consumeLastToken=*/true))
Craig Topper161e4db2014-05-21 06:02:52 +0000285 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000286
Alex Lorenzf9371392017-03-23 11:44:25 +0000287 Decl *CategoryType = Actions.ActOnStartCategoryInterface(
288 AtLoc, nameId, nameLoc, typeParameterList, categoryId, categoryLoc,
289 ProtocolRefs.data(), ProtocolRefs.size(), ProtocolLocs.data(),
290 EndProtoLoc, attrs.getList());
291
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000292 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000293 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000294
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000295 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000296
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000297 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000298 }
299 // Parse a class interface.
Craig Topper161e4db2014-05-21 06:02:52 +0000300 IdentifierInfo *superClassId = nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000301 SourceLocation superClassLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000302 SourceLocation typeArgsLAngleLoc;
303 SmallVector<ParsedType, 4> typeArgs;
304 SourceLocation typeArgsRAngleLoc;
305 SmallVector<Decl *, 4> protocols;
306 SmallVector<SourceLocation, 4> protocolLocs;
Chris Lattner0ef13522007-10-09 17:51:17 +0000307 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000308 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000309
310 // Code completion of superclass names.
311 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000312 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000313 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000314 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000315 }
316
Alex Lorenzf1278212017-04-11 15:01:53 +0000317 if (expectIdentifier())
318 return nullptr; // missing super class name.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000319 superClassId = Tok.getIdentifierInfo();
320 superClassLoc = ConsumeToken();
Douglas Gregore9d95f12015-07-07 03:57:35 +0000321
322 // Type arguments for the superclass or protocol conformances.
323 if (Tok.is(tok::less)) {
David Blaikieefdccaa2016-01-15 23:43:34 +0000324 parseObjCTypeArgsOrProtocolQualifiers(
325 nullptr, typeArgsLAngleLoc, typeArgs, typeArgsRAngleLoc, LAngleLoc,
326 protocols, protocolLocs, EndProtoLoc,
327 /*consumeLastToken=*/true,
328 /*warnOnIncompleteProtocols=*/true);
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +0000329 if (Tok.is(tok::eof))
330 return nullptr;
Douglas Gregore9d95f12015-07-07 03:57:35 +0000331 }
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000332 }
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +0000333
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000334 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000335 if (LAngleLoc.isValid()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000336 if (!ProtocolIdents.empty()) {
337 // We already parsed the protocols named when we thought we had a
338 // type parameter list. Translate them into actual protocol references.
339 for (const auto &pair : ProtocolIdents) {
340 protocolLocs.push_back(pair.second);
341 }
342 Actions.FindProtocolDeclaration(/*WarnOnDeclarations=*/true,
343 /*ForObjCContainer=*/true,
Craig Toppera9247eb2015-10-22 04:59:56 +0000344 ProtocolIdents, protocols);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000345 }
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000346 } else if (protocols.empty() && Tok.is(tok::less) &&
347 ParseObjCProtocolReferences(protocols, protocolLocs, true, true,
348 LAngleLoc, EndProtoLoc,
349 /*consumeLastToken=*/true)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000350 return nullptr;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000351 }
Mike Stump11289f42009-09-09 15:08:12 +0000352
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000353 if (Tok.isNot(tok::less))
Argyrios Kyrtzidisf95a0002016-11-09 02:47:07 +0000354 Actions.ActOnTypedefedProtocols(protocols, protocolLocs,
355 superClassId, superClassLoc);
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000356
John McCall48871652010-08-21 09:40:31 +0000357 Decl *ClsType =
Douglas Gregore9d95f12015-07-07 03:57:35 +0000358 Actions.ActOnStartClassInterface(getCurScope(), AtLoc, nameId, nameLoc,
359 typeParameterList, superClassId,
360 superClassLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000361 typeArgs,
362 SourceRange(typeArgsLAngleLoc,
363 typeArgsRAngleLoc),
364 protocols.data(), protocols.size(),
365 protocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000366 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000367
Chris Lattner0ef13522007-10-09 17:51:17 +0000368 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000369 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000370
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000371 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000372
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000373 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000374}
375
Douglas Gregor813a0662015-06-19 18:14:38 +0000376/// Add an attribute for a context-sensitive type nullability to the given
377/// declarator.
378static void addContextSensitiveTypeNullability(Parser &P,
379 Declarator &D,
380 NullabilityKind nullability,
381 SourceLocation nullabilityLoc,
382 bool &addedToDeclSpec) {
383 // Create the attribute.
384 auto getNullabilityAttr = [&]() -> AttributeList * {
Douglas Gregorbec595a2015-06-19 18:27:45 +0000385 return D.getAttributePool().create(
386 P.getNullabilityKeyword(nullability),
387 SourceRange(nullabilityLoc),
388 nullptr, SourceLocation(),
389 nullptr, 0,
390 AttributeList::AS_ContextSensitiveKeyword);
Douglas Gregor813a0662015-06-19 18:14:38 +0000391 };
392
393 if (D.getNumTypeObjects() > 0) {
394 // Add the attribute to the declarator chunk nearest the declarator.
395 auto nullabilityAttr = getNullabilityAttr();
396 DeclaratorChunk &chunk = D.getTypeObject(0);
397 nullabilityAttr->setNext(chunk.getAttrListRef());
398 chunk.getAttrListRef() = nullabilityAttr;
399 } else if (!addedToDeclSpec) {
400 // Otherwise, just put it on the declaration specifiers (if one
401 // isn't there already).
402 D.getMutableDeclSpec().addAttributes(getNullabilityAttr());
403 addedToDeclSpec = true;
404 }
405}
406
Douglas Gregor85f3f952015-07-07 03:57:15 +0000407/// Parse an Objective-C type parameter list, if present, or capture
408/// the locations of the protocol identifiers for a list of protocol
409/// references.
410///
411/// objc-type-parameter-list:
412/// '<' objc-type-parameter (',' objc-type-parameter)* '>'
413///
414/// objc-type-parameter:
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000415/// objc-type-parameter-variance? identifier objc-type-parameter-bound[opt]
Douglas Gregor85f3f952015-07-07 03:57:15 +0000416///
417/// objc-type-parameter-bound:
418/// ':' type-name
419///
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000420/// objc-type-parameter-variance:
421/// '__covariant'
422/// '__contravariant'
423///
Douglas Gregor85f3f952015-07-07 03:57:15 +0000424/// \param lAngleLoc The location of the starting '<'.
425///
426/// \param protocolIdents Will capture the list of identifiers, if the
427/// angle brackets contain a list of protocol references rather than a
428/// type parameter list.
429///
430/// \param rAngleLoc The location of the ending '>'.
431ObjCTypeParamList *Parser::parseObjCTypeParamListOrProtocolRefs(
Richard Smith3df3f1d2015-11-03 01:19:56 +0000432 ObjCTypeParamListScope &Scope, SourceLocation &lAngleLoc,
433 SmallVectorImpl<IdentifierLocPair> &protocolIdents,
434 SourceLocation &rAngleLoc, bool mayBeProtocolList) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000435 assert(Tok.is(tok::less) && "Not at the beginning of a type parameter list");
436
437 // Within the type parameter list, don't treat '>' as an operator.
438 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
439
440 // Local function to "flush" the protocol identifiers, turning them into
441 // type parameters.
442 SmallVector<Decl *, 4> typeParams;
443 auto makeProtocolIdentsIntoTypeParameters = [&]() {
Douglas Gregore83b9562015-07-07 03:57:53 +0000444 unsigned index = 0;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000445 for (const auto &pair : protocolIdents) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000446 DeclResult typeParam = Actions.actOnObjCTypeParam(
David Blaikieefdccaa2016-01-15 23:43:34 +0000447 getCurScope(), ObjCTypeParamVariance::Invariant, SourceLocation(),
448 index++, pair.first, pair.second, SourceLocation(), nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000449 if (typeParam.isUsable())
450 typeParams.push_back(typeParam.get());
451 }
452
453 protocolIdents.clear();
454 mayBeProtocolList = false;
455 };
456
457 bool invalid = false;
458 lAngleLoc = ConsumeToken();
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000459
Douglas Gregor85f3f952015-07-07 03:57:15 +0000460 do {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000461 // Parse the variance, if any.
462 SourceLocation varianceLoc;
463 ObjCTypeParamVariance variance = ObjCTypeParamVariance::Invariant;
464 if (Tok.is(tok::kw___covariant) || Tok.is(tok::kw___contravariant)) {
465 variance = Tok.is(tok::kw___covariant)
466 ? ObjCTypeParamVariance::Covariant
467 : ObjCTypeParamVariance::Contravariant;
468 varianceLoc = ConsumeToken();
469
470 // Once we've seen a variance specific , we know this is not a
471 // list of protocol references.
472 if (mayBeProtocolList) {
473 // Up until now, we have been queuing up parameters because they
474 // might be protocol references. Turn them into parameters now.
475 makeProtocolIdentsIntoTypeParameters();
476 }
477 }
478
Douglas Gregor85f3f952015-07-07 03:57:15 +0000479 // Parse the identifier.
480 if (!Tok.is(tok::identifier)) {
481 // Code completion.
482 if (Tok.is(tok::code_completion)) {
483 // FIXME: If these aren't protocol references, we'll need different
484 // completions.
Craig Topper883dd332015-12-24 23:58:11 +0000485 Actions.CodeCompleteObjCProtocolReferences(protocolIdents);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000486 cutOffParsing();
487
488 // FIXME: Better recovery here?.
489 return nullptr;
490 }
491
492 Diag(Tok, diag::err_objc_expected_type_parameter);
493 invalid = true;
494 break;
495 }
496
497 IdentifierInfo *paramName = Tok.getIdentifierInfo();
498 SourceLocation paramLoc = ConsumeToken();
499
500 // If there is a bound, parse it.
501 SourceLocation colonLoc;
502 TypeResult boundType;
503 if (TryConsumeToken(tok::colon, colonLoc)) {
504 // Once we've seen a bound, we know this is not a list of protocol
505 // references.
506 if (mayBeProtocolList) {
507 // Up until now, we have been queuing up parameters because they
508 // might be protocol references. Turn them into parameters now.
509 makeProtocolIdentsIntoTypeParameters();
510 }
511
512 // type-name
513 boundType = ParseTypeName();
514 if (boundType.isInvalid())
515 invalid = true;
516 } else if (mayBeProtocolList) {
517 // If this could still be a protocol list, just capture the identifier.
518 // We don't want to turn it into a parameter.
519 protocolIdents.push_back(std::make_pair(paramName, paramLoc));
520 continue;
521 }
522
523 // Create the type parameter.
David Blaikieefdccaa2016-01-15 23:43:34 +0000524 DeclResult typeParam = Actions.actOnObjCTypeParam(
525 getCurScope(), variance, varianceLoc, typeParams.size(), paramName,
526 paramLoc, colonLoc, boundType.isUsable() ? boundType.get() : nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000527 if (typeParam.isUsable())
528 typeParams.push_back(typeParam.get());
529 } while (TryConsumeToken(tok::comma));
530
531 // Parse the '>'.
532 if (invalid) {
533 SkipUntil(tok::greater, tok::at, StopBeforeMatch);
534 if (Tok.is(tok::greater))
535 ConsumeToken();
536 } else if (ParseGreaterThanInTemplateList(rAngleLoc,
537 /*ConsumeLastToken=*/true,
538 /*ObjCGenericList=*/true)) {
539 Diag(lAngleLoc, diag::note_matching) << "'<'";
540 SkipUntil({tok::greater, tok::greaterequal, tok::at, tok::minus,
541 tok::minus, tok::plus, tok::colon, tok::l_paren, tok::l_brace,
542 tok::comma, tok::semi },
543 StopBeforeMatch);
544 if (Tok.is(tok::greater))
545 ConsumeToken();
546 }
547
548 if (mayBeProtocolList) {
549 // A type parameter list must be followed by either a ':' (indicating the
550 // presence of a superclass) or a '(' (indicating that this is a category
551 // or extension). This disambiguates between an objc-type-parameter-list
552 // and a objc-protocol-refs.
553 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_paren)) {
554 // Returning null indicates that we don't have a type parameter list.
555 // The results the caller needs to handle the protocol references are
556 // captured in the reference parameters already.
557 return nullptr;
558 }
559
560 // We have a type parameter list that looks like a list of protocol
561 // references. Turn that parameter list into type parameters.
562 makeProtocolIdentsIntoTypeParameters();
563 }
564
Richard Smith3df3f1d2015-11-03 01:19:56 +0000565 // Form the type parameter list and enter its scope.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000566 ObjCTypeParamList *list = Actions.actOnObjCTypeParamList(
567 getCurScope(),
568 lAngleLoc,
569 typeParams,
570 rAngleLoc);
Richard Smith3df3f1d2015-11-03 01:19:56 +0000571 Scope.enter(list);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000572
573 // Clear out the angle locations; they're used by the caller to indicate
574 // whether there are any protocol references.
575 lAngleLoc = SourceLocation();
576 rAngleLoc = SourceLocation();
Akira Hatanaka8ebd5802015-12-16 06:25:38 +0000577 return invalid ? nullptr : list;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000578}
579
580/// Parse an objc-type-parameter-list.
581ObjCTypeParamList *Parser::parseObjCTypeParamList() {
582 SourceLocation lAngleLoc;
583 SmallVector<IdentifierLocPair, 1> protocolIdents;
584 SourceLocation rAngleLoc;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000585
586 ObjCTypeParamListScope Scope(Actions, getCurScope());
587 return parseObjCTypeParamListOrProtocolRefs(Scope, lAngleLoc, protocolIdents,
588 rAngleLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000589 /*mayBeProtocolList=*/false);
590}
591
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000592/// objc-interface-decl-list:
593/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000594/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000595/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000596/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000597/// objc-interface-decl-list declaration
598/// objc-interface-decl-list ';'
599///
Steve Naroff99264b42007-08-22 16:35:03 +0000600/// objc-method-requirement: [OBJC2]
601/// @required
602/// @optional
603///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000604void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
605 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000606 SmallVector<Decl *, 32> allMethods;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000607 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000608 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000609
Ted Kremenekc7c64312010-01-07 01:20:12 +0000610 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000611
Steve Naroff99264b42007-08-22 16:35:03 +0000612 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000613 // If this is a method prototype, parse it.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000614 if (Tok.isOneOf(tok::minus, tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000615 if (Decl *methodPrototype =
616 ParseObjCMethodPrototype(MethodImplKind, false))
617 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000618 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
619 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000620 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
621 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000622 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000623 if (Tok.is(tok::semi))
624 ConsumeToken();
625 }
Steve Naroff99264b42007-08-22 16:35:03 +0000626 continue;
627 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000628 if (Tok.is(tok::l_paren)) {
629 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000630 ParseObjCMethodDecl(Tok.getLocation(),
631 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000632 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000633 continue;
634 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000635 // Ignore excess semicolons.
636 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000637 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000638 continue;
639 }
Mike Stump11289f42009-09-09 15:08:12 +0000640
Chris Lattnerda9fb152008-10-20 06:10:06 +0000641 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000642 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000643 break;
Mike Stump11289f42009-09-09 15:08:12 +0000644
Douglas Gregorf1934162010-01-13 21:24:21 +0000645 // Code completion within an Objective-C interface.
646 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000647 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000648 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000649 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000650 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000651 }
652
Chris Lattner038a3e32008-10-20 05:46:22 +0000653 // If we don't have an @ directive, parse it as a function definition.
654 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000655 // The code below does not consume '}'s because it is afraid of eating the
656 // end of a namespace. Because of the way this code is structured, an
657 // erroneous r_brace would cause an infinite loop if not handled here.
658 if (Tok.is(tok::r_brace))
659 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000660 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000661 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000662 continue;
663 }
Mike Stump11289f42009-09-09 15:08:12 +0000664
Chris Lattner038a3e32008-10-20 05:46:22 +0000665 // Otherwise, we have an @ directive, eat the @.
666 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000667 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000668 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000669 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000670 }
671
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000672 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000673
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000674 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000675 AtEnd.setBegin(AtLoc);
676 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000677 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000678 } else if (DirectiveKind == tok::objc_not_keyword) {
679 Diag(Tok, diag::err_objc_unknown_at);
680 SkipUntil(tok::semi);
681 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000682 }
Mike Stump11289f42009-09-09 15:08:12 +0000683
Chris Lattnerda9fb152008-10-20 06:10:06 +0000684 // Eat the identifier.
685 ConsumeToken();
686
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000687 switch (DirectiveKind) {
688 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000689 // FIXME: If someone forgets an @end on a protocol, this loop will
690 // continue to eat up tons of stuff and spew lots of nonsense errors. It
691 // would probably be better to bail out if we saw an @class or @interface
692 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000693 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000694 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000695 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000696 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000697
698 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000699 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000700 Diag(AtLoc, diag::err_objc_missing_end)
701 << FixItHint::CreateInsertion(AtLoc, "@end\n");
702 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
703 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000704 ConsumeToken();
705 break;
706
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000707 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000708 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000709 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000710 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000711 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000712 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000713 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000714 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000715 break;
Mike Stump11289f42009-09-09 15:08:12 +0000716
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000717 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000718 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000719 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000720
Chris Lattner038a3e32008-10-20 05:46:22 +0000721 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000722 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000723 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000724 if (Tok.is(tok::l_paren)) {
725 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000726 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000727 }
Mike Stump11289f42009-09-09 15:08:12 +0000728
Douglas Gregor813a0662015-06-19 18:14:38 +0000729 bool addedToDeclSpec = false;
Benjamin Kramera39beb92014-09-03 11:06:10 +0000730 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
731 if (FD.D.getIdentifier() == nullptr) {
732 Diag(AtLoc, diag::err_objc_property_requires_field_name)
733 << FD.D.getSourceRange();
734 return;
735 }
736 if (FD.BitfieldSize) {
737 Diag(AtLoc, diag::err_objc_property_bitfield)
738 << FD.D.getSourceRange();
739 return;
740 }
741
Douglas Gregor813a0662015-06-19 18:14:38 +0000742 // Map a nullability property attribute to a context-sensitive keyword
743 // attribute.
744 if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
745 addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
746 OCDS.getNullabilityLoc(),
747 addedToDeclSpec);
748
Benjamin Kramera39beb92014-09-03 11:06:10 +0000749 // Install the property declarator into interfaceDecl.
750 IdentifierInfo *SelName =
751 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
752
753 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
754 IdentifierInfo *SetterName = OCDS.getSetterName();
755 Selector SetterSel;
756 if (SetterName)
757 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
758 else
759 SetterSel = SelectorTable::constructSetterSelector(
760 PP.getIdentifierTable(), PP.getSelectorTable(),
761 FD.D.getIdentifier());
Benjamin Kramera39beb92014-09-03 11:06:10 +0000762 Decl *Property = Actions.ActOnProperty(
763 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000764 MethodImplKind);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000765
766 FD.complete(Property);
767 };
John McCallcfefb6d2009-11-03 02:38:08 +0000768
Chris Lattner038a3e32008-10-20 05:46:22 +0000769 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000770 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000771 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000772
John McCall405988b2011-03-26 01:53:26 +0000773 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000774 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000775 }
Steve Naroff99264b42007-08-22 16:35:03 +0000776 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000777
778 // We break out of the big loop in two cases: when we see @end or when we see
779 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000780 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000781 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000782 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000783 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000784 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000785 } else {
786 Diag(Tok, diag::err_objc_missing_end)
787 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
788 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
789 << (int) Actions.getObjCContainerKind();
790 AtEnd.setBegin(Tok.getLocation());
791 AtEnd.setEnd(Tok.getLocation());
792 }
Mike Stump11289f42009-09-09 15:08:12 +0000793
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000794 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000795 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000796 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000797}
798
Douglas Gregor813a0662015-06-19 18:14:38 +0000799/// Diagnose redundant or conflicting nullability information.
800static void diagnoseRedundantPropertyNullability(Parser &P,
801 ObjCDeclSpec &DS,
802 NullabilityKind nullability,
803 SourceLocation nullabilityLoc){
804 if (DS.getNullability() == nullability) {
805 P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000806 << DiagNullabilityKind(nullability, true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000807 << SourceRange(DS.getNullabilityLoc());
808 return;
809 }
810
811 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000812 << DiagNullabilityKind(nullability, true)
813 << DiagNullabilityKind(DS.getNullability(), true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000814 << SourceRange(DS.getNullabilityLoc());
815}
816
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000817/// Parse property attribute declarations.
818///
819/// property-attr-decl: '(' property-attrlist ')'
820/// property-attrlist:
821/// property-attribute
822/// property-attrlist ',' property-attribute
823/// property-attribute:
824/// getter '=' identifier
825/// setter '=' identifier ':'
826/// readonly
827/// readwrite
828/// assign
829/// retain
830/// copy
831/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000832/// atomic
833/// strong
834/// weak
835/// unsafe_unretained
Douglas Gregor813a0662015-06-19 18:14:38 +0000836/// nonnull
837/// nullable
838/// null_unspecified
Douglas Gregor849ebc22015-06-19 18:14:46 +0000839/// null_resettable
Manman Ren387ff7f2016-01-26 18:52:43 +0000840/// class
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000841///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000842void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000843 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000844 BalancedDelimiterTracker T(*this, tok::l_paren);
845 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000846
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000847 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000848 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000849 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000850 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000851 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000852 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000853
Chris Lattner76619232008-10-20 07:22:18 +0000854 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000855 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000856 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000857 return;
858 }
Mike Stump11289f42009-09-09 15:08:12 +0000859
Chris Lattner1db33542008-10-20 07:37:22 +0000860 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000861
Chris Lattner68e48682008-11-20 04:42:34 +0000862 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000863 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000864 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000865 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000866 else if (II->isStr("unsafe_unretained"))
867 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000868 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000869 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000870 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000871 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000872 else if (II->isStr("strong"))
873 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000874 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000875 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000876 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000877 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000878 else if (II->isStr("atomic"))
879 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000880 else if (II->isStr("weak"))
881 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000882 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000883 bool IsSetter = II->getNameStart()[0] == 's';
884
Chris Lattner825bca12008-10-20 07:39:53 +0000885 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000886 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
Craig Topper3110a5c2015-11-14 18:16:02 +0000887 diag::err_objc_expected_equal_for_getter;
Anders Carlssonfe15a782010-10-02 17:45:21 +0000888
Alp Toker383d2c42014-01-01 03:08:43 +0000889 if (ExpectAndConsume(tok::equal, DiagID)) {
890 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000891 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000892 }
Mike Stump11289f42009-09-09 15:08:12 +0000893
Douglas Gregorc8537c52009-11-19 07:41:15 +0000894 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000895 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000896 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000897 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000898 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000899 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000900 }
901
Anders Carlssonfe15a782010-10-02 17:45:21 +0000902 SourceLocation SelLoc;
903 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
904
905 if (!SelIdent) {
906 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
907 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000908 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000909 return;
910 }
Mike Stump11289f42009-09-09 15:08:12 +0000911
Anders Carlssonfe15a782010-10-02 17:45:21 +0000912 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000913 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000914 DS.setSetterName(SelIdent, SelLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000915
Alp Toker383d2c42014-01-01 03:08:43 +0000916 if (ExpectAndConsume(tok::colon,
917 diag::err_expected_colon_after_setter_name)) {
918 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000919 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000920 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000921 } else {
922 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000923 DS.setGetterName(SelIdent, SelLoc);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000924 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000925 } else if (II->isStr("nonnull")) {
926 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
927 diagnoseRedundantPropertyNullability(*this, DS,
928 NullabilityKind::NonNull,
929 Tok.getLocation());
930 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
931 DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
932 } else if (II->isStr("nullable")) {
933 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
934 diagnoseRedundantPropertyNullability(*this, DS,
935 NullabilityKind::Nullable,
936 Tok.getLocation());
937 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
938 DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
939 } else if (II->isStr("null_unspecified")) {
940 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
941 diagnoseRedundantPropertyNullability(*this, DS,
942 NullabilityKind::Unspecified,
943 Tok.getLocation());
944 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
945 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000946 } else if (II->isStr("null_resettable")) {
947 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
948 diagnoseRedundantPropertyNullability(*this, DS,
949 NullabilityKind::Unspecified,
950 Tok.getLocation());
951 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
952 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
953
954 // Also set the null_resettable bit.
955 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
Manman Ren387ff7f2016-01-26 18:52:43 +0000956 } else if (II->isStr("class")) {
957 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_class);
Chris Lattner825bca12008-10-20 07:39:53 +0000958 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000959 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000960 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000961 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000962 }
Mike Stump11289f42009-09-09 15:08:12 +0000963
Chris Lattner1db33542008-10-20 07:37:22 +0000964 if (Tok.isNot(tok::comma))
965 break;
Mike Stump11289f42009-09-09 15:08:12 +0000966
Chris Lattner1db33542008-10-20 07:37:22 +0000967 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000968 }
Mike Stump11289f42009-09-09 15:08:12 +0000969
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000970 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000971}
972
Steve Naroff09bf8152007-09-06 21:24:23 +0000973/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000974/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000975/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000976///
977/// objc-instance-method: '-'
978/// objc-class-method: '+'
979///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000980/// objc-method-attributes: [OBJC2]
981/// __attribute__((deprecated))
982///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000983Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000984 bool MethodDefinition) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000985 assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000986
Mike Stump11289f42009-09-09 15:08:12 +0000987 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000988 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000989 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000990 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000991 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000992 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000993 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000994}
995
996/// objc-selector:
997/// identifier
998/// one of
999/// enum struct union if else while do for switch case default
1000/// break continue return goto asm sizeof typeof __alignof
1001/// unsigned long const short volatile signed restrict _Complex
1002/// in out inout bycopy byref oneway int char float double void _Bool
1003///
Chris Lattner4f472a32009-04-11 18:13:45 +00001004IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +00001005
Chris Lattner5700fab2007-10-07 02:00:24 +00001006 switch (Tok.getKind()) {
1007 default:
Craig Topper161e4db2014-05-21 06:02:52 +00001008 return nullptr;
Alex Lorenz9b9188d2017-07-13 10:50:21 +00001009 case tok::colon:
1010 // Empty selector piece uses the location of the ':'.
1011 SelectorLoc = Tok.getLocation();
1012 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001013 case tok::ampamp:
1014 case tok::ampequal:
1015 case tok::amp:
1016 case tok::pipe:
1017 case tok::tilde:
1018 case tok::exclaim:
1019 case tok::exclaimequal:
1020 case tok::pipepipe:
1021 case tok::pipeequal:
1022 case tok::caret:
1023 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +00001024 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +00001025 if (isLetter(ThisTok[0])) {
Malcolm Parsons731ca0e2016-11-03 12:25:51 +00001026 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok);
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001027 Tok.setKind(tok::identifier);
1028 SelectorLoc = ConsumeToken();
1029 return II;
1030 }
Craig Topper161e4db2014-05-21 06:02:52 +00001031 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001032 }
1033
Chris Lattner5700fab2007-10-07 02:00:24 +00001034 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001035 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +00001036 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001037 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001038 case tok::kw_break:
1039 case tok::kw_case:
1040 case tok::kw_catch:
1041 case tok::kw_char:
1042 case tok::kw_class:
1043 case tok::kw_const:
1044 case tok::kw_const_cast:
1045 case tok::kw_continue:
1046 case tok::kw_default:
1047 case tok::kw_delete:
1048 case tok::kw_do:
1049 case tok::kw_double:
1050 case tok::kw_dynamic_cast:
1051 case tok::kw_else:
1052 case tok::kw_enum:
1053 case tok::kw_explicit:
1054 case tok::kw_export:
1055 case tok::kw_extern:
1056 case tok::kw_false:
1057 case tok::kw_float:
1058 case tok::kw_for:
1059 case tok::kw_friend:
1060 case tok::kw_goto:
1061 case tok::kw_if:
1062 case tok::kw_inline:
1063 case tok::kw_int:
1064 case tok::kw_long:
1065 case tok::kw_mutable:
1066 case tok::kw_namespace:
1067 case tok::kw_new:
1068 case tok::kw_operator:
1069 case tok::kw_private:
1070 case tok::kw_protected:
1071 case tok::kw_public:
1072 case tok::kw_register:
1073 case tok::kw_reinterpret_cast:
1074 case tok::kw_restrict:
1075 case tok::kw_return:
1076 case tok::kw_short:
1077 case tok::kw_signed:
1078 case tok::kw_sizeof:
1079 case tok::kw_static:
1080 case tok::kw_static_cast:
1081 case tok::kw_struct:
1082 case tok::kw_switch:
1083 case tok::kw_template:
1084 case tok::kw_this:
1085 case tok::kw_throw:
1086 case tok::kw_true:
1087 case tok::kw_try:
1088 case tok::kw_typedef:
1089 case tok::kw_typeid:
1090 case tok::kw_typename:
1091 case tok::kw_typeof:
1092 case tok::kw_union:
1093 case tok::kw_unsigned:
1094 case tok::kw_using:
1095 case tok::kw_virtual:
1096 case tok::kw_void:
1097 case tok::kw_volatile:
1098 case tok::kw_wchar_t:
1099 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +00001100 case tok::kw__Bool:
1101 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001102 case tok::kw___alignof:
Richard Smithe301ba22015-11-11 02:02:15 +00001103 case tok::kw___auto_type:
Chris Lattner5700fab2007-10-07 02:00:24 +00001104 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001105 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +00001106 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +00001107 }
Steve Naroff99264b42007-08-22 16:35:03 +00001108}
1109
Fariborz Jahanian83615522008-01-02 22:54:34 +00001110/// objc-for-collection-in: 'in'
1111///
Fariborz Jahanian3622e592008-01-04 23:04:08 +00001112bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001113 // FIXME: May have to do additional look-ahead to only allow for
1114 // valid tokens following an 'in'; such as an identifier, unary operators,
1115 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001116 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +00001117 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +00001118}
1119
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001120/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +00001121/// qualifier list and builds their bitmask representation in the input
1122/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +00001123///
1124/// objc-type-qualifiers:
1125/// objc-type-qualifier
1126/// objc-type-qualifiers objc-type-qualifier
1127///
Douglas Gregor813a0662015-06-19 18:14:38 +00001128/// objc-type-qualifier:
1129/// 'in'
1130/// 'out'
1131/// 'inout'
1132/// 'oneway'
1133/// 'bycopy'
1134/// 'byref'
1135/// 'nonnull'
1136/// 'nullable'
1137/// 'null_unspecified'
1138///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001139void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
Faisal Vali421b2d12017-12-29 05:41:00 +00001140 DeclaratorContext Context) {
1141 assert(Context == DeclaratorContext::ObjCParameterContext ||
1142 Context == DeclaratorContext::ObjCResultContext);
John McCalla55902b2011-10-01 09:56:14 +00001143
Chris Lattner61511e12007-12-12 06:56:32 +00001144 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +00001145 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +00001146 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
Faisal Vali421b2d12017-12-29 05:41:00 +00001147 Context == DeclaratorContext::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001148 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +00001149 }
1150
Chris Lattner5e530bc2007-12-27 19:57:00 +00001151 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +00001152 return;
Mike Stump11289f42009-09-09 15:08:12 +00001153
Chris Lattner61511e12007-12-12 06:56:32 +00001154 const IdentifierInfo *II = Tok.getIdentifierInfo();
1155 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001156 if (II != ObjCTypeQuals[i] ||
1157 NextToken().is(tok::less) ||
1158 NextToken().is(tok::coloncolon))
Chris Lattner61511e12007-12-12 06:56:32 +00001159 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001160
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001161 ObjCDeclSpec::ObjCDeclQualifier Qual;
Douglas Gregor813a0662015-06-19 18:14:38 +00001162 NullabilityKind Nullability;
Chris Lattner61511e12007-12-12 06:56:32 +00001163 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +00001164 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001165 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
1166 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
1167 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
1168 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
1169 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
1170 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Douglas Gregor813a0662015-06-19 18:14:38 +00001171
1172 case objc_nonnull:
1173 Qual = ObjCDeclSpec::DQ_CSNullability;
1174 Nullability = NullabilityKind::NonNull;
1175 break;
1176
1177 case objc_nullable:
1178 Qual = ObjCDeclSpec::DQ_CSNullability;
1179 Nullability = NullabilityKind::Nullable;
1180 break;
1181
1182 case objc_null_unspecified:
1183 Qual = ObjCDeclSpec::DQ_CSNullability;
1184 Nullability = NullabilityKind::Unspecified;
1185 break;
Chris Lattner61511e12007-12-12 06:56:32 +00001186 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001187
1188 // FIXME: Diagnose redundant specifiers.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001189 DS.setObjCDeclQualifier(Qual);
Douglas Gregor813a0662015-06-19 18:14:38 +00001190 if (Qual == ObjCDeclSpec::DQ_CSNullability)
1191 DS.setNullability(Tok.getLocation(), Nullability);
1192
Chris Lattner61511e12007-12-12 06:56:32 +00001193 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001194 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +00001195 break;
1196 }
Mike Stump11289f42009-09-09 15:08:12 +00001197
Chris Lattner61511e12007-12-12 06:56:32 +00001198 // If this wasn't a recognized qualifier, bail out.
1199 if (II) return;
1200 }
1201}
1202
John McCalla55902b2011-10-01 09:56:14 +00001203/// Take all the decl attributes out of the given list and add
1204/// them to the given attribute set.
1205static void takeDeclAttributes(ParsedAttributes &attrs,
1206 AttributeList *list) {
1207 while (list) {
1208 AttributeList *cur = list;
1209 list = cur->getNext();
1210
1211 if (!cur->isUsedAsTypeAttr()) {
1212 // Clear out the next pointer. We're really completely
1213 // destroying the internal invariants of the declarator here,
1214 // but it doesn't matter because we're done with it.
Craig Topper161e4db2014-05-21 06:02:52 +00001215 cur->setNext(nullptr);
John McCalla55902b2011-10-01 09:56:14 +00001216 attrs.add(cur);
1217 }
1218 }
1219}
1220
1221/// takeDeclAttributes - Take all the decl attributes from the given
1222/// declarator and add them to the given list.
1223static void takeDeclAttributes(ParsedAttributes &attrs,
1224 Declarator &D) {
1225 // First, take ownership of all attributes.
1226 attrs.getPool().takeAllFrom(D.getAttributePool());
1227 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
1228
1229 // Now actually move the attributes over.
1230 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
1231 takeDeclAttributes(attrs, D.getAttributes());
1232 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
1233 takeDeclAttributes(attrs,
1234 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
1235}
1236
Chris Lattner61511e12007-12-12 06:56:32 +00001237/// objc-type-name:
1238/// '(' objc-type-qualifiers[opt] type-name ')'
1239/// '(' objc-type-qualifiers[opt] ')'
1240///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001241ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
Faisal Vali421b2d12017-12-29 05:41:00 +00001242 DeclaratorContext context,
John McCalla55902b2011-10-01 09:56:14 +00001243 ParsedAttributes *paramAttrs) {
Faisal Vali421b2d12017-12-29 05:41:00 +00001244 assert(context == DeclaratorContext::ObjCParameterContext ||
1245 context == DeclaratorContext::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +00001246 assert((paramAttrs != nullptr) ==
Faisal Vali421b2d12017-12-29 05:41:00 +00001247 (context == DeclaratorContext::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +00001248
Chris Lattner0ef13522007-10-09 17:51:17 +00001249 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +00001250
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001251 BalancedDelimiterTracker T(*this, tok::l_paren);
1252 T.consumeOpen();
1253
Chris Lattner2ebb1782008-08-23 01:48:03 +00001254 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001255 ObjCDeclContextSwitch ObjCDC(*this);
1256
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001257 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001258 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +00001259
John McCallba7bf592010-08-24 05:47:05 +00001260 ParsedType Ty;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001261 if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
John McCalla55902b2011-10-01 09:56:14 +00001262 // Parse an abstract declarator.
1263 DeclSpec declSpec(AttrFactory);
1264 declSpec.setObjCQualifiers(&DS);
Faisal Vali7db85c52017-12-31 00:06:40 +00001265 DeclSpecContext dsContext = DeclSpecContext::DSC_normal;
Faisal Vali421b2d12017-12-29 05:41:00 +00001266 if (context == DeclaratorContext::ObjCResultContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00001267 dsContext = DeclSpecContext::DSC_objc_method_result;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001268 ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
John McCalla55902b2011-10-01 09:56:14 +00001269 Declarator declarator(declSpec, context);
1270 ParseDeclarator(declarator);
1271
1272 // If that's not invalid, extract a type.
1273 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001274 // Map a nullability specifier to a context-sensitive keyword attribute.
1275 bool addedToDeclSpec = false;
1276 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1277 addContextSensitiveTypeNullability(*this, declarator,
1278 DS.getNullability(),
1279 DS.getNullabilityLoc(),
1280 addedToDeclSpec);
1281
John McCalla55902b2011-10-01 09:56:14 +00001282 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1283 if (!type.isInvalid())
1284 Ty = type.get();
1285
1286 // If we're parsing a parameter, steal all the decl attributes
1287 // and add them to the decl spec.
Faisal Vali421b2d12017-12-29 05:41:00 +00001288 if (context == DeclaratorContext::ObjCParameterContext)
John McCalla55902b2011-10-01 09:56:14 +00001289 takeDeclAttributes(*paramAttrs, declarator);
1290 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001291 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001292
Steve Naroff90255b42008-10-21 14:15:04 +00001293 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001294 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001295 else if (Tok.getLocation() == TypeStartLoc) {
1296 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001297 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001298 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001299 } else {
1300 // Otherwise, we found *something*, but didn't get a ')' in the right
1301 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001302 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001303 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001304 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001305}
1306
1307/// objc-method-decl:
1308/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001309/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001310/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001311/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001312///
1313/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001314/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001315/// objc-keyword-selector objc-keyword-decl
1316///
1317/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001318/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1319/// objc-selector ':' objc-keyword-attributes[opt] identifier
1320/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1321/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001322///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001323/// objc-parmlist:
1324/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001325///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001326/// objc-parms:
1327/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001328///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001329/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001330/// , ...
1331///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001332/// objc-keyword-attributes: [OBJC2]
1333/// __attribute__((unused))
1334///
John McCall48871652010-08-21 09:40:31 +00001335Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001336 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001337 tok::ObjCKeywordKind MethodImplKind,
1338 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001339 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001340
Douglas Gregor636a61e2010-04-07 00:21:17 +00001341 if (Tok.is(tok::code_completion)) {
David Blaikieefdccaa2016-01-15 23:43:34 +00001342 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
1343 /*ReturnType=*/nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001344 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001345 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001346 }
1347
Chris Lattner2ebb1782008-08-23 01:48:03 +00001348 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001349 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001350 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001351 if (Tok.is(tok::l_paren))
Faisal Vali421b2d12017-12-29 05:41:00 +00001352 ReturnType = ParseObjCTypeName(DSRet, DeclaratorContext::ObjCResultContext,
Craig Topper161e4db2014-05-21 06:02:52 +00001353 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001354
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001355 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001356 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001357 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001358 MaybeParseGNUAttributes(methodAttrs);
Aaron Ballman1c606c22018-02-12 13:38:25 +00001359 MaybeParseCXX11Attributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001360
Douglas Gregor636a61e2010-04-07 00:21:17 +00001361 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001362 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001363 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001364 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001365 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001366 }
1367
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001368 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001369 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001370 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001371
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001372 // An unnamed colon is valid.
1373 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001374 Diag(Tok, diag::err_expected_selector_for_method)
1375 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001376 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001377 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001378 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001379 }
Mike Stump11289f42009-09-09 15:08:12 +00001380
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001381 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001382 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001383 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001384 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001385 MaybeParseGNUAttributes(methodAttrs);
Aaron Ballman1c606c22018-02-12 13:38:25 +00001386 MaybeParseCXX11Attributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001387
Chris Lattner5700fab2007-10-07 02:00:24 +00001388 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001389 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001390 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001391 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001392 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001393 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001394 methodAttrs.getList(), MethodImplKind,
1395 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001396 PD.complete(Result);
1397 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001398 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001399
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001400 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001401 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001402 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001403 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1404 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001405
1406 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001407 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001408 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001409 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001410
Chris Lattner5700fab2007-10-07 02:00:24 +00001411 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001412 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001413 break;
Mike Stump11289f42009-09-09 15:08:12 +00001414
David Blaikieefdccaa2016-01-15 23:43:34 +00001415 ArgInfo.Type = nullptr;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001416 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001417 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
Faisal Vali421b2d12017-12-29 05:41:00 +00001418 DeclaratorContext::ObjCParameterContext,
John McCalla55902b2011-10-01 09:56:14 +00001419 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001420
Chris Lattner5700fab2007-10-07 02:00:24 +00001421 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001422 // Regardless, collect all the attributes we've parsed so far.
Aaron Ballman1c606c22018-02-12 13:38:25 +00001423 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001424 MaybeParseGNUAttributes(paramAttrs);
Aaron Ballman1c606c22018-02-12 13:38:25 +00001425 MaybeParseCXX11Attributes(paramAttrs);
1426 ArgInfo.ArgAttrs = paramAttrs.getList();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001427
Douglas Gregor45879692010-07-08 23:37:41 +00001428 // Code completion for the next piece of the selector.
1429 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001430 KeyIdents.push_back(SelIdent);
1431 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1432 mType == tok::minus,
1433 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001434 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001435 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001436 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001437 }
Alex Lorenzf1278212017-04-11 15:01:53 +00001438
1439 if (expectIdentifier())
1440 break; // missing argument name.
Mike Stump11289f42009-09-09 15:08:12 +00001441
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001442 ArgInfo.Name = Tok.getIdentifierInfo();
1443 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001444 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001445
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001446 ArgInfos.push_back(ArgInfo);
1447 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001448 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001449
John McCall084e83d2011-03-24 11:26:52 +00001450 // Make sure the attributes persist.
1451 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1452
Douglas Gregor95887f92010-07-08 23:20:03 +00001453 // Code completion for the next piece of the selector.
1454 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001455 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1456 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001457 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001458 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001459 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001460 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001461 }
1462
Chris Lattner5700fab2007-10-07 02:00:24 +00001463 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001464 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001465 if (!SelIdent && Tok.isNot(tok::colon))
1466 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001467 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001468 SourceLocation ColonLoc = Tok.getLocation();
1469 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001470 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1471 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1472 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001473 }
1474 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001475 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001476 }
Mike Stump11289f42009-09-09 15:08:12 +00001477
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001478 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001479 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001480 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001481 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001482 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001483 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001484 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001485 ConsumeToken();
1486 break;
1487 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001488 if (!cStyleParamWarned) {
1489 Diag(Tok, diag::warn_cstyle_param);
1490 cStyleParamWarned = true;
1491 }
John McCall084e83d2011-03-24 11:26:52 +00001492 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001493 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001494 // Parse the declarator.
Faisal Vali421b2d12017-12-29 05:41:00 +00001495 Declarator ParmDecl(DS, DeclaratorContext::PrototypeContext);
Chris Lattner5700fab2007-10-07 02:00:24 +00001496 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001497 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001498 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001499 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1500 ParmDecl.getIdentifierLoc(),
1501 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001502 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001503 }
Mike Stump11289f42009-09-09 15:08:12 +00001504
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001505 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001506 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001507 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001508 MaybeParseGNUAttributes(methodAttrs);
Aaron Ballman1c606c22018-02-12 13:38:25 +00001509 MaybeParseCXX11Attributes(methodAttrs);
1510
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001511 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001512 return nullptr;
1513
Chris Lattner5700fab2007-10-07 02:00:24 +00001514 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1515 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001516 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001517 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001518 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001519 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001520 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001521 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001522 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001523
John McCall28a6aea2009-11-04 02:18:39 +00001524 PD.complete(Result);
1525 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001526}
1527
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001528/// objc-protocol-refs:
1529/// '<' identifier-list '>'
1530///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001531bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001532ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1533 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001534 bool WarnOnDeclarations, bool ForObjCContainer,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001535 SourceLocation &LAngleLoc, SourceLocation &EndLoc,
1536 bool consumeLastToken) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001537 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001538
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001539 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001540
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001541 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001542
Chris Lattner3bbae002008-07-26 04:03:38 +00001543 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001544 if (Tok.is(tok::code_completion)) {
Craig Topper883dd332015-12-24 23:58:11 +00001545 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001546 cutOffParsing();
1547 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001548 }
1549
Alex Lorenzf1278212017-04-11 15:01:53 +00001550 if (expectIdentifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001551 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001552 return true;
1553 }
1554 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1555 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001556 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001557 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001558
Alp Toker383d2c42014-01-01 03:08:43 +00001559 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001560 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001561 }
Mike Stump11289f42009-09-09 15:08:12 +00001562
Chris Lattner3bbae002008-07-26 04:03:38 +00001563 // Consume the '>'.
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001564 if (ParseGreaterThanInTemplateList(EndLoc, consumeLastToken,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001565 /*ObjCGenericList=*/false))
Chris Lattner3bbae002008-07-26 04:03:38 +00001566 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001567
Chris Lattner3bbae002008-07-26 04:03:38 +00001568 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001569 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Craig Toppera9247eb2015-10-22 04:59:56 +00001570 ProtocolIdents, Protocols);
Chris Lattner3bbae002008-07-26 04:03:38 +00001571 return false;
1572}
1573
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001574TypeResult Parser::parseObjCProtocolQualifierType(SourceLocation &rAngleLoc) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001575 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001576 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001577
1578 SourceLocation lAngleLoc;
1579 SmallVector<Decl *, 8> protocols;
1580 SmallVector<SourceLocation, 8> protocolLocs;
1581 (void)ParseObjCProtocolReferences(protocols, protocolLocs, false, false,
1582 lAngleLoc, rAngleLoc,
1583 /*consumeLastToken=*/true);
1584 TypeResult result = Actions.actOnObjCProtocolQualifierType(lAngleLoc,
1585 protocols,
1586 protocolLocs,
1587 rAngleLoc);
1588 if (result.isUsable()) {
1589 Diag(lAngleLoc, diag::warn_objc_protocol_qualifier_missing_id)
1590 << FixItHint::CreateInsertion(lAngleLoc, "id")
1591 << SourceRange(lAngleLoc, rAngleLoc);
1592 }
1593
1594 return result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001595}
1596
Douglas Gregore9d95f12015-07-07 03:57:35 +00001597/// Parse Objective-C type arguments or protocol qualifiers.
1598///
1599/// objc-type-arguments:
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001600/// '<' type-name '...'[opt] (',' type-name '...'[opt])* '>'
Douglas Gregore9d95f12015-07-07 03:57:35 +00001601///
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001602void Parser::parseObjCTypeArgsOrProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001603 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001604 SourceLocation &typeArgsLAngleLoc,
1605 SmallVectorImpl<ParsedType> &typeArgs,
1606 SourceLocation &typeArgsRAngleLoc,
1607 SourceLocation &protocolLAngleLoc,
1608 SmallVectorImpl<Decl *> &protocols,
1609 SmallVectorImpl<SourceLocation> &protocolLocs,
1610 SourceLocation &protocolRAngleLoc,
1611 bool consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001612 bool warnOnIncompleteProtocols) {
1613 assert(Tok.is(tok::less) && "Not at the start of type args or protocols");
1614 SourceLocation lAngleLoc = ConsumeToken();
1615
1616 // Whether all of the elements we've parsed thus far are single
1617 // identifiers, which might be types or might be protocols.
1618 bool allSingleIdentifiers = true;
1619 SmallVector<IdentifierInfo *, 4> identifiers;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001620 SmallVectorImpl<SourceLocation> &identifierLocs = protocolLocs;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001621
1622 // Parse a list of comma-separated identifiers, bailing out if we
1623 // see something different.
1624 do {
1625 // Parse a single identifier.
1626 if (Tok.is(tok::identifier) &&
1627 (NextToken().is(tok::comma) ||
1628 NextToken().is(tok::greater) ||
1629 NextToken().is(tok::greatergreater))) {
1630 identifiers.push_back(Tok.getIdentifierInfo());
1631 identifierLocs.push_back(ConsumeToken());
1632 continue;
1633 }
1634
1635 if (Tok.is(tok::code_completion)) {
1636 // FIXME: Also include types here.
1637 SmallVector<IdentifierLocPair, 4> identifierLocPairs;
1638 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1639 identifierLocPairs.push_back(IdentifierLocPair(identifiers[i],
1640 identifierLocs[i]));
1641 }
1642
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001643 QualType BaseT = Actions.GetTypeFromParser(baseType);
1644 if (!BaseT.isNull() && BaseT->acceptsObjCTypeParams()) {
1645 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
1646 } else {
Craig Topper883dd332015-12-24 23:58:11 +00001647 Actions.CodeCompleteObjCProtocolReferences(identifierLocPairs);
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001648 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001649 cutOffParsing();
1650 return;
1651 }
1652
1653 allSingleIdentifiers = false;
1654 break;
1655 } while (TryConsumeToken(tok::comma));
1656
1657 // If we parsed an identifier list, semantic analysis sorts out
1658 // whether it refers to protocols or to type arguments.
1659 if (allSingleIdentifiers) {
1660 // Parse the closing '>'.
1661 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001662 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001663 /*ObjCGenericList=*/true);
1664
1665 // Let Sema figure out what we parsed.
1666 Actions.actOnObjCTypeArgsOrProtocolQualifiers(getCurScope(),
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001667 baseType,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001668 lAngleLoc,
1669 identifiers,
1670 identifierLocs,
1671 rAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001672 typeArgsLAngleLoc,
1673 typeArgs,
1674 typeArgsRAngleLoc,
1675 protocolLAngleLoc,
1676 protocols,
1677 protocolRAngleLoc,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001678 warnOnIncompleteProtocols);
1679 return;
1680 }
1681
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001682 // We parsed an identifier list but stumbled into non single identifiers, this
1683 // means we might (a) check that what we already parsed is a legitimate type
1684 // (not a protocol or unknown type) and (b) parse the remaining ones, which
1685 // must all be type args.
Douglas Gregore9d95f12015-07-07 03:57:35 +00001686
1687 // Convert the identifiers into type arguments.
1688 bool invalid = false;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001689 IdentifierInfo *foundProtocolId = nullptr, *foundValidTypeId = nullptr;
1690 SourceLocation foundProtocolSrcLoc, foundValidTypeSrcLoc;
1691 SmallVector<IdentifierInfo *, 2> unknownTypeArgs;
1692 SmallVector<SourceLocation, 2> unknownTypeArgsLoc;
1693
Douglas Gregore9d95f12015-07-07 03:57:35 +00001694 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1695 ParsedType typeArg
1696 = Actions.getTypeName(*identifiers[i], identifierLocs[i], getCurScope());
1697 if (typeArg) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001698 DeclSpec DS(AttrFactory);
1699 const char *prevSpec = nullptr;
1700 unsigned diagID;
Faisal Vali090da2d2018-01-01 18:23:28 +00001701 DS.SetTypeSpecType(TST_typename, identifierLocs[i], prevSpec, diagID,
1702 typeArg, Actions.getASTContext().getPrintingPolicy());
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001703
1704 // Form a declarator to turn this into a type.
Faisal Vali421b2d12017-12-29 05:41:00 +00001705 Declarator D(DS, DeclaratorContext::TypeNameContext);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001706 TypeResult fullTypeArg = Actions.ActOnTypeName(getCurScope(), D);
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001707 if (fullTypeArg.isUsable()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001708 typeArgs.push_back(fullTypeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001709 if (!foundValidTypeId) {
1710 foundValidTypeId = identifiers[i];
1711 foundValidTypeSrcLoc = identifierLocs[i];
1712 }
1713 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001714 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001715 unknownTypeArgs.push_back(identifiers[i]);
1716 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1717 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001718 } else {
1719 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001720 if (!Actions.LookupProtocol(identifiers[i], identifierLocs[i])) {
1721 unknownTypeArgs.push_back(identifiers[i]);
1722 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1723 } else if (!foundProtocolId) {
1724 foundProtocolId = identifiers[i];
1725 foundProtocolSrcLoc = identifierLocs[i];
1726 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001727 }
1728 }
1729
1730 // Continue parsing type-names.
1731 do {
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001732 Token CurTypeTok = Tok;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001733 TypeResult typeArg = ParseTypeName();
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001734
1735 // Consume the '...' for a pack expansion.
1736 SourceLocation ellipsisLoc;
1737 TryConsumeToken(tok::ellipsis, ellipsisLoc);
1738 if (typeArg.isUsable() && ellipsisLoc.isValid()) {
1739 typeArg = Actions.ActOnPackExpansion(typeArg.get(), ellipsisLoc);
1740 }
1741
Douglas Gregore9d95f12015-07-07 03:57:35 +00001742 if (typeArg.isUsable()) {
1743 typeArgs.push_back(typeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001744 if (!foundValidTypeId) {
1745 foundValidTypeId = CurTypeTok.getIdentifierInfo();
1746 foundValidTypeSrcLoc = CurTypeTok.getLocation();
1747 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001748 } else {
1749 invalid = true;
1750 }
1751 } while (TryConsumeToken(tok::comma));
1752
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001753 // Diagnose the mix between type args and protocols.
1754 if (foundProtocolId && foundValidTypeId)
1755 Actions.DiagnoseTypeArgsAndProtocols(foundProtocolId, foundProtocolSrcLoc,
1756 foundValidTypeId,
1757 foundValidTypeSrcLoc);
1758
1759 // Diagnose unknown arg types.
1760 ParsedType T;
1761 if (unknownTypeArgs.size())
1762 for (unsigned i = 0, e = unknownTypeArgsLoc.size(); i < e; ++i)
1763 Actions.DiagnoseUnknownTypeName(unknownTypeArgs[i], unknownTypeArgsLoc[i],
1764 getCurScope(), nullptr, T);
1765
Douglas Gregore9d95f12015-07-07 03:57:35 +00001766 // Parse the closing '>'.
1767 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001768 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001769 /*ObjCGenericList=*/true);
1770
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001771 if (invalid) {
1772 typeArgs.clear();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001773 return;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001774 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001775
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001776 // Record left/right angle locations.
1777 typeArgsLAngleLoc = lAngleLoc;
1778 typeArgsRAngleLoc = rAngleLoc;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001779}
1780
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001781void Parser::parseObjCTypeArgsAndProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001782 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001783 SourceLocation &typeArgsLAngleLoc,
1784 SmallVectorImpl<ParsedType> &typeArgs,
1785 SourceLocation &typeArgsRAngleLoc,
1786 SourceLocation &protocolLAngleLoc,
1787 SmallVectorImpl<Decl *> &protocols,
1788 SmallVectorImpl<SourceLocation> &protocolLocs,
1789 SourceLocation &protocolRAngleLoc,
1790 bool consumeLastToken) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001791 assert(Tok.is(tok::less));
1792
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001793 // Parse the first angle-bracket-delimited clause.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001794 parseObjCTypeArgsOrProtocolQualifiers(baseType,
1795 typeArgsLAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001796 typeArgs,
1797 typeArgsRAngleLoc,
1798 protocolLAngleLoc,
1799 protocols,
1800 protocolLocs,
1801 protocolRAngleLoc,
1802 consumeLastToken,
Douglas Gregore83b9562015-07-07 03:57:53 +00001803 /*warnOnIncompleteProtocols=*/false);
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00001804 if (Tok.is(tok::eof)) // Nothing else to do here...
1805 return;
Douglas Gregore83b9562015-07-07 03:57:53 +00001806
1807 // An Objective-C object pointer followed by type arguments
1808 // can then be followed again by a set of protocol references, e.g.,
1809 // \c NSArray<NSView><NSTextDelegate>
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001810 if ((consumeLastToken && Tok.is(tok::less)) ||
1811 (!consumeLastToken && NextToken().is(tok::less))) {
1812 // If we aren't consuming the last token, the prior '>' is still hanging
1813 // there. Consume it before we parse the protocol qualifiers.
1814 if (!consumeLastToken)
1815 ConsumeToken();
1816
1817 if (!protocols.empty()) {
1818 SkipUntilFlags skipFlags = SkipUntilFlags();
1819 if (!consumeLastToken)
1820 skipFlags = skipFlags | StopBeforeMatch;
Douglas Gregore83b9562015-07-07 03:57:53 +00001821 Diag(Tok, diag::err_objc_type_args_after_protocols)
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001822 << SourceRange(protocolLAngleLoc, protocolRAngleLoc);
1823 SkipUntil(tok::greater, tok::greatergreater, skipFlags);
Douglas Gregore83b9562015-07-07 03:57:53 +00001824 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001825 ParseObjCProtocolReferences(protocols, protocolLocs,
1826 /*WarnOnDeclarations=*/false,
1827 /*ForObjCContainer=*/false,
1828 protocolLAngleLoc, protocolRAngleLoc,
1829 consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001830 }
1831 }
1832}
1833
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001834TypeResult Parser::parseObjCTypeArgsAndProtocolQualifiers(
1835 SourceLocation loc,
1836 ParsedType type,
1837 bool consumeLastToken,
1838 SourceLocation &endLoc) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001839 assert(Tok.is(tok::less));
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001840 SourceLocation typeArgsLAngleLoc;
1841 SmallVector<ParsedType, 4> typeArgs;
1842 SourceLocation typeArgsRAngleLoc;
1843 SourceLocation protocolLAngleLoc;
1844 SmallVector<Decl *, 4> protocols;
1845 SmallVector<SourceLocation, 4> protocolLocs;
1846 SourceLocation protocolRAngleLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00001847
1848 // Parse type arguments and protocol qualifiers.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001849 parseObjCTypeArgsAndProtocolQualifiers(type, typeArgsLAngleLoc, typeArgs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001850 typeArgsRAngleLoc, protocolLAngleLoc,
1851 protocols, protocolLocs,
1852 protocolRAngleLoc, consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001853
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00001854 if (Tok.is(tok::eof))
1855 return true; // Invalid type result.
1856
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001857 // Compute the location of the last token.
1858 if (consumeLastToken)
1859 endLoc = PrevTokLocation;
1860 else
1861 endLoc = Tok.getLocation();
1862
1863 return Actions.actOnObjCTypeArgsAndProtocolQualifiers(
1864 getCurScope(),
1865 loc,
1866 type,
1867 typeArgsLAngleLoc,
1868 typeArgs,
1869 typeArgsRAngleLoc,
1870 protocolLAngleLoc,
1871 protocols,
1872 protocolLocs,
1873 protocolRAngleLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00001874}
1875
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001876void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1877 BalancedDelimiterTracker &T,
1878 SmallVectorImpl<Decl *> &AllIvarDecls,
1879 bool RBraceMissing) {
1880 if (!RBraceMissing)
1881 T.consumeClose();
1882
1883 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1884 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1885 Actions.ActOnObjCContainerFinishDefinition();
1886 // Call ActOnFields() even if we don't have any decls. This is useful
1887 // for code rewriting tools that need to be aware of the empty list.
1888 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1889 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001890 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001891}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001892
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001893/// objc-class-instance-variables:
1894/// '{' objc-instance-variable-decl-list[opt] '}'
1895///
1896/// objc-instance-variable-decl-list:
1897/// objc-visibility-spec
1898/// objc-instance-variable-decl ';'
1899/// ';'
1900/// objc-instance-variable-decl-list objc-visibility-spec
1901/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1902/// objc-instance-variable-decl-list ';'
1903///
1904/// objc-visibility-spec:
1905/// @private
1906/// @protected
1907/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001908/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001909///
1910/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001911/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001912///
John McCall48871652010-08-21 09:40:31 +00001913void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001914 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001915 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001916 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001917 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001918
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001919 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001920 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001921
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001922 BalancedDelimiterTracker T(*this, tok::l_brace);
1923 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001924 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001925 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001926 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001927
Steve Naroff00433d32007-08-21 21:17:12 +00001928 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001929 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001930 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001931 continue;
1932 }
Mike Stump11289f42009-09-09 15:08:12 +00001933
Steve Naroff00433d32007-08-21 21:17:12 +00001934 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001935 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001936 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001937 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001938 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001939 }
1940
Steve Naroff7c348172007-08-23 18:16:40 +00001941 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001942 case tok::objc_private:
1943 case tok::objc_public:
1944 case tok::objc_protected:
1945 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001946 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001947 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001948 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001949
1950 case tok::objc_end:
1951 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001952 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1953 Tok.setKind(tok::at);
1954 Tok.setLength(1);
1955 PP.EnterToken(Tok);
1956 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1957 T, AllIvarDecls, true);
1958 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001959
1960 default:
1961 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1962 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001963 }
1964 }
Mike Stump11289f42009-09-09 15:08:12 +00001965
Douglas Gregor48d46252010-01-13 21:54:15 +00001966 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001967 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001968 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001969 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001970 }
John McCallcfefb6d2009-11-03 02:38:08 +00001971
Benjamin Kramera39beb92014-09-03 11:06:10 +00001972 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1973 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1974 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001975 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001976 Decl *Field = Actions.ActOnIvar(
1977 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1978 FD.BitfieldSize, visibility);
1979 Actions.ActOnObjCContainerFinishDefinition();
1980 if (Field)
1981 AllIvarDecls.push_back(Field);
1982 FD.complete(Field);
1983 };
John McCallcfefb6d2009-11-03 02:38:08 +00001984
Chris Lattnera12405b2008-04-10 06:46:29 +00001985 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001986 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001987 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001988
Chris Lattner0ef13522007-10-09 17:51:17 +00001989 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001990 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001991 } else {
1992 Diag(Tok, diag::err_expected_semi_decl_list);
1993 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001994 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001995 }
1996 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001997 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1998 T, AllIvarDecls, false);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001999}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002000
2001/// objc-protocol-declaration:
2002/// objc-protocol-definition
2003/// objc-protocol-forward-reference
2004///
2005/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00002006/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00002007/// objc-protocol-refs[opt]
2008/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00002009/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002010///
2011/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00002012/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002013///
James Dennett1355bd12012-06-11 06:19:40 +00002014/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00002015/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002016/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00002017Parser::DeclGroupPtrTy
2018Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
2019 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00002020 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002021 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
2022 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002023
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002024 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002025 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002026 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002027 return nullptr;
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002028 }
2029
Nico Weber69a79142013-04-04 00:15:10 +00002030 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00002031
Alex Lorenzf1278212017-04-11 15:01:53 +00002032 if (expectIdentifier())
2033 return nullptr; // missing protocol name.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002034 // Save the protocol name, then consume it.
2035 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
2036 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002037
Alp Toker383d2c42014-01-01 03:08:43 +00002038 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00002039 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Craig Topper0f723bb2015-10-22 05:00:01 +00002040 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtoInfo,
John McCall53fa7142010-12-24 02:08:15 +00002041 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002042 }
Mike Stump11289f42009-09-09 15:08:12 +00002043
Erik Verbruggenf9887852011-12-08 09:58:43 +00002044 CheckNestedObjCContexts(AtLoc);
2045
Chris Lattner0ef13522007-10-09 17:51:17 +00002046 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002047 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002048 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
2049
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002050 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002051 while (1) {
2052 ConsumeToken(); // the ','
Alex Lorenzf1278212017-04-11 15:01:53 +00002053 if (expectIdentifier()) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002054 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002055 return nullptr;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002056 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00002057 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
2058 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002059 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00002060
Chris Lattner0ef13522007-10-09 17:51:17 +00002061 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002062 break;
2063 }
2064 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00002065 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
David Blaikie0403cb12016-01-15 23:43:25 +00002066 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002067
Craig Topper0f723bb2015-10-22 05:00:01 +00002068 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtocolRefs,
John McCall53fa7142010-12-24 02:08:15 +00002069 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00002070 }
Mike Stump11289f42009-09-09 15:08:12 +00002071
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002072 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00002073 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002074
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002075 SmallVector<Decl *, 8> ProtocolRefs;
2076 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002077 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00002078 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002079 LAngleLoc, EndProtoLoc,
2080 /*consumeLastToken=*/true))
David Blaikie0403cb12016-01-15 23:43:25 +00002081 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002082
John McCall48871652010-08-21 09:40:31 +00002083 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00002084 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002085 ProtocolRefs.data(),
2086 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00002087 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00002088 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002089
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00002090 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00002091 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002092}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002093
2094/// objc-implementation:
2095/// objc-class-implementation-prologue
2096/// objc-category-implementation-prologue
2097///
2098/// objc-class-implementation-prologue:
2099/// @implementation identifier objc-superclass[opt]
2100/// objc-class-instance-variables[opt]
2101///
2102/// objc-category-implementation-prologue:
2103/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002104Parser::DeclGroupPtrTy
2105Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002106 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
2107 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002108 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002109 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002110
Douglas Gregor49c22a72009-11-18 16:26:39 +00002111 // Code completion after '@implementation'.
2112 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002113 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002114 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002115 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +00002116 }
2117
Nico Weber69a79142013-04-04 00:15:10 +00002118 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00002119
Alex Lorenzf1278212017-04-11 15:01:53 +00002120 if (expectIdentifier())
2121 return nullptr; // missing class or category name.
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002122 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00002123 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002124 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00002125 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002126
Douglas Gregor85f3f952015-07-07 03:57:15 +00002127 // Neither a type parameter list nor a list of protocol references is
2128 // permitted here. Parse and diagnose them.
2129 if (Tok.is(tok::less)) {
2130 SourceLocation lAngleLoc, rAngleLoc;
2131 SmallVector<IdentifierLocPair, 8> protocolIdents;
2132 SourceLocation diagLoc = Tok.getLocation();
Richard Smith3df3f1d2015-11-03 01:19:56 +00002133 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
2134 if (parseObjCTypeParamListOrProtocolRefs(typeParamScope, lAngleLoc,
2135 protocolIdents, rAngleLoc)) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00002136 Diag(diagLoc, diag::err_objc_parameterized_implementation)
2137 << SourceRange(diagLoc, PrevTokLocation);
2138 } else if (lAngleLoc.isValid()) {
2139 Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
2140 << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
2141 }
2142 }
2143
Mike Stump11289f42009-09-09 15:08:12 +00002144 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002145 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002146 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002147 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002148 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002149
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002150 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002151 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002152 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002153 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002154 }
2155
Chris Lattner0ef13522007-10-09 17:51:17 +00002156 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002157 categoryId = Tok.getIdentifierInfo();
2158 categoryLoc = ConsumeToken();
2159 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002160 Diag(Tok, diag::err_expected)
2161 << tok::identifier; // missing category name.
David Blaikie0403cb12016-01-15 23:43:25 +00002162 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002163 }
Chris Lattner0ef13522007-10-09 17:51:17 +00002164 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002165 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002166 SkipUntil(tok::r_paren); // don't stop at ';'
David Blaikie0403cb12016-01-15 23:43:25 +00002167 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002168 }
2169 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00002170 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2171 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002172 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2173 SmallVector<Decl *, 4> protocols;
2174 SmallVector<SourceLocation, 4> protocolLocs;
2175 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2176 /*warnOnIncompleteProtocols=*/false,
2177 /*ForObjCContainer=*/false,
2178 protocolLAngleLoc, protocolRAngleLoc,
2179 /*consumeLastToken=*/true);
Fariborz Jahanian85888552013-05-17 17:58:11 +00002180 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002181 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002182 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00002183 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002184
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002185 } else {
2186 // We have a class implementation
2187 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002188 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00002189 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002190 // We have a super class
Alex Lorenzf1278212017-04-11 15:01:53 +00002191 if (expectIdentifier())
2192 return nullptr; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002193 superClassId = Tok.getIdentifierInfo();
2194 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002195 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002196 ObjCImpDecl = Actions.ActOnStartClassImplementation(
2197 AtLoc, nameId, nameLoc,
2198 superClassId, superClassLoc);
2199
2200 if (Tok.is(tok::l_brace)) // we have ivars
2201 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002202 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2203 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002204
2205 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2206 SmallVector<Decl *, 4> protocols;
2207 SmallVector<SourceLocation, 4> protocolLocs;
2208 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2209 /*warnOnIncompleteProtocols=*/false,
2210 /*ForObjCContainer=*/false,
2211 protocolLAngleLoc, protocolRAngleLoc,
2212 /*consumeLastToken=*/true);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002213 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002214 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002215 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002216
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002217 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002218
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002219 {
2220 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00002221 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002222 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002223 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002224 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
2225 DeclGroupRef DG = DGP.get();
2226 DeclsInGroup.append(DG.begin(), DG.end());
2227 }
2228 }
2229 }
2230
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00002231 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002232}
Steve Naroff33a1e802007-10-29 21:38:07 +00002233
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002234Parser::DeclGroupPtrTy
2235Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002236 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
2237 "ParseObjCAtEndDeclaration(): Expected @end");
2238 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002239 if (CurParsedObjCImpl)
2240 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002241 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00002242 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002243 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
David Blaikie0403cb12016-01-15 23:43:25 +00002244 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002245}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002246
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002247Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
2248 if (!Finished) {
2249 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00002250 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002251 P.Diag(P.Tok, diag::err_objc_missing_end)
2252 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
2253 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
2254 << Sema::OCK_Implementation;
2255 }
2256 }
Craig Topper161e4db2014-05-21 06:02:52 +00002257 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002258 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002259}
2260
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002261void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
2262 assert(!Finished);
Alex Lorenz6c9af502017-07-03 10:12:24 +00002263 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl, AtEnd.getBegin());
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002264 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002265 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2266 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002267
2268 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
2269
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002270 if (HasCFunction)
2271 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
2272 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2273 false/*c-functions*/);
2274
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002275 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002276 for (LateParsedObjCMethodContainer::iterator
2277 I = LateParsedObjCMethods.begin(),
2278 E = LateParsedObjCMethods.end(); I != E; ++I)
2279 delete *I;
2280 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002281
2282 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002283}
2284
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002285/// compatibility-alias-decl:
2286/// @compatibility_alias alias-name class-name ';'
2287///
John McCall48871652010-08-21 09:40:31 +00002288Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002289 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
2290 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
2291 ConsumeToken(); // consume compatibility_alias
Alex Lorenzf1278212017-04-11 15:01:53 +00002292 if (expectIdentifier())
Craig Topper161e4db2014-05-21 06:02:52 +00002293 return nullptr;
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002294 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
2295 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Alex Lorenzf1278212017-04-11 15:01:53 +00002296 if (expectIdentifier())
Craig Topper161e4db2014-05-21 06:02:52 +00002297 return nullptr;
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002298 IdentifierInfo *classId = Tok.getIdentifierInfo();
2299 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00002300 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00002301 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
2302 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002303}
2304
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002305/// property-synthesis:
2306/// @synthesize property-ivar-list ';'
2307///
2308/// property-ivar-list:
2309/// property-ivar
2310/// property-ivar-list ',' property-ivar
2311///
2312/// property-ivar:
2313/// identifier
2314/// identifier '=' identifier
2315///
John McCall48871652010-08-21 09:40:31 +00002316Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002317 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00002318 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002319 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00002320
Douglas Gregor88e72a02009-11-18 19:45:45 +00002321 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00002322 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002323 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002324 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002325 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002326 }
2327
Douglas Gregor88e72a02009-11-18 19:45:45 +00002328 if (Tok.isNot(tok::identifier)) {
2329 Diag(Tok, diag::err_synthesized_property_name);
2330 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002331 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00002332 }
Craig Topper161e4db2014-05-21 06:02:52 +00002333
2334 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002335 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2336 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002337 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00002338 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002339 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00002340 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002341 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002342 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002343 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002344 }
Alex Lorenzf1278212017-04-11 15:01:53 +00002345
2346 if (expectIdentifier())
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002347 break;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002348 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002349 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002350 }
Manman Ren5b786402016-01-28 18:49:28 +00002351 Actions.ActOnPropertyImplDecl(
2352 getCurScope(), atLoc, propertyLoc, true,
2353 propertyId, propertyIvar, propertyIvarLoc,
2354 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Chris Lattner0ef13522007-10-09 17:51:17 +00002355 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002356 break;
2357 ConsumeToken(); // consume ','
2358 }
Alp Toker383d2c42014-01-01 03:08:43 +00002359 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00002360 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002361}
2362
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002363/// property-dynamic:
2364/// @dynamic property-list
2365///
2366/// property-list:
2367/// identifier
2368/// property-list ',' identifier
2369///
John McCall48871652010-08-21 09:40:31 +00002370Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002371 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2372 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002373 ConsumeToken(); // consume dynamic
Manman Ren0fe61f82016-01-29 19:05:57 +00002374
2375 bool isClassProperty = false;
2376 if (Tok.is(tok::l_paren)) {
2377 ConsumeParen();
2378 const IdentifierInfo *II = Tok.getIdentifierInfo();
2379
2380 if (!II) {
2381 Diag(Tok, diag::err_objc_expected_property_attr) << II;
2382 SkipUntil(tok::r_paren, StopAtSemi);
2383 } else {
2384 SourceLocation AttrName = ConsumeToken(); // consume attribute name
2385 if (II->isStr("class")) {
2386 isClassProperty = true;
2387 if (Tok.isNot(tok::r_paren)) {
2388 Diag(Tok, diag::err_expected) << tok::r_paren;
2389 SkipUntil(tok::r_paren, StopAtSemi);
2390 } else
2391 ConsumeParen();
2392 } else {
2393 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
2394 SkipUntil(tok::r_paren, StopAtSemi);
2395 }
2396 }
2397 }
2398
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002399 while (true) {
2400 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002401 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002402 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002403 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002404 }
Alex Lorenzf1278212017-04-11 15:01:53 +00002405
2406 if (expectIdentifier()) {
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002407 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002408 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002409 }
2410
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002411 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2412 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Manman Ren5b786402016-01-28 18:49:28 +00002413 Actions.ActOnPropertyImplDecl(
2414 getCurScope(), atLoc, propertyLoc, false,
2415 propertyId, nullptr, SourceLocation(),
Manman Ren0fe61f82016-01-29 19:05:57 +00002416 isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
Manman Ren5b786402016-01-28 18:49:28 +00002417 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002418
Chris Lattner0ef13522007-10-09 17:51:17 +00002419 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002420 break;
2421 ConsumeToken(); // consume ','
2422 }
Alp Toker383d2c42014-01-01 03:08:43 +00002423 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00002424 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002425}
Mike Stump11289f42009-09-09 15:08:12 +00002426
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002427/// objc-throw-statement:
2428/// throw expression[opt];
2429///
John McCalldadc5752010-08-24 06:29:42 +00002430StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2431 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002432 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00002433 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00002434 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002435 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002436 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002437 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002438 }
2439 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00002440 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00002441 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002442 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002443}
2444
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002445/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002446/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002447///
John McCalldadc5752010-08-24 06:29:42 +00002448StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002449Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002450 ConsumeToken(); // consume synchronized
2451 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002452 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002453 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002454 }
John McCalld9bb7432011-07-27 21:50:02 +00002455
2456 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002457 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00002458 ExprResult operand(ParseExpression());
2459
2460 if (Tok.is(tok::r_paren)) {
2461 ConsumeParen(); // ')'
2462 } else {
2463 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002464 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00002465
2466 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002467 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002468 }
John McCalld9bb7432011-07-27 21:50:02 +00002469
2470 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002471 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00002472 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002473 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002474 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002475 }
Steve Naroffd9c26072008-06-04 20:36:13 +00002476
John McCalld9bb7432011-07-27 21:50:02 +00002477 // Check the @synchronized operand now.
2478 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002479 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002480
John McCalld9bb7432011-07-27 21:50:02 +00002481 // Parse the compound statement within a new scope.
Momchil Velikov57c681f2017-08-10 15:43:06 +00002482 ParseScope bodyScope(this, Scope::DeclScope | Scope::CompoundStmtScope);
John McCalld9bb7432011-07-27 21:50:02 +00002483 StmtResult body(ParseCompoundStatementBody());
2484 bodyScope.Exit();
2485
2486 // If there was a semantic or parse error earlier with the
2487 // operand, fail now.
2488 if (operand.isInvalid())
2489 return StmtError();
2490
2491 if (body.isInvalid())
2492 body = Actions.ActOnNullStmt(Tok.getLocation());
2493
2494 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002495}
2496
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002497/// objc-try-catch-statement:
2498/// @try compound-statement objc-catch-list[opt]
2499/// @try compound-statement objc-catch-list[opt] @finally compound-statement
2500///
2501/// objc-catch-list:
2502/// @catch ( parameter-declaration ) compound-statement
2503/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2504/// catch-parameter-declaration:
2505/// parameter-declaration
2506/// '...' [OBJC2]
2507///
John McCalldadc5752010-08-24 06:29:42 +00002508StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002509 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002510
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002511 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00002512 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002513 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002514 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002515 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00002516 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00002517 StmtResult FinallyStmt;
Momchil Velikov57c681f2017-08-10 15:43:06 +00002518 ParseScope TryScope(this, Scope::DeclScope | Scope::CompoundStmtScope);
John McCalldadc5752010-08-24 06:29:42 +00002519 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002520 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002521 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002522 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002523
Chris Lattner0ef13522007-10-09 17:51:17 +00002524 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002525 // At this point, we need to lookahead to determine if this @ is the start
2526 // of an @catch or @finally. We don't want to consume the @ token if this
2527 // is an @try or @encode or something else.
2528 Token AfterAt = GetLookAheadToken(1);
2529 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2530 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2531 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002532
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002533 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002534 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002535 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002536 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002537 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002538 ConsumeParen();
Momchil Velikov57c681f2017-08-10 15:43:06 +00002539 ParseScope CatchScope(this, Scope::DeclScope |
2540 Scope::CompoundStmtScope |
2541 Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002542 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002543 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002544 ParseDeclarationSpecifiers(DS);
Faisal Vali421b2d12017-12-29 05:41:00 +00002545 Declarator ParmDecl(DS, DeclaratorContext::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002546 ParseDeclarator(ParmDecl);
2547
Douglas Gregore11ee112010-04-23 23:01:43 +00002548 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002549 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002550 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002551 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002552 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002553
Steve Naroff65a00892009-04-07 22:56:58 +00002554 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002555
Steve Naroff65a00892009-04-07 22:56:58 +00002556 if (Tok.is(tok::r_paren))
2557 RParenLoc = ConsumeParen();
2558 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002559 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002560
John McCalldadc5752010-08-24 06:29:42 +00002561 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002562 if (Tok.is(tok::l_brace))
2563 CatchBody = ParseCompoundStatementBody();
2564 else
Alp Tokerec543272013-12-24 09:48:30 +00002565 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002566 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002567 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00002568
John McCalldadc5752010-08-24 06:29:42 +00002569 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00002570 RParenLoc,
2571 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002572 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002573 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002574 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002575
Steve Naroffe6016792008-02-05 21:27:35 +00002576 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002577 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2578 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002579 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002580 }
2581 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002582 } else {
2583 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002584 ConsumeToken(); // consume finally
Momchil Velikov57c681f2017-08-10 15:43:06 +00002585 ParseScope FinallyScope(this,
2586 Scope::DeclScope | Scope::CompoundStmtScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002587
John McCalldadc5752010-08-24 06:29:42 +00002588 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002589 if (Tok.is(tok::l_brace))
2590 FinallyBody = ParseCompoundStatementBody();
2591 else
Alp Tokerec543272013-12-24 09:48:30 +00002592 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002593 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002594 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002595 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002596 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002597 catch_or_finally_seen = true;
2598 break;
2599 }
2600 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002601 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002602 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002603 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002604 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002605
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002606 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002607 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002608 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002609}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002610
John McCall31168b02011-06-15 23:02:42 +00002611/// objc-autoreleasepool-statement:
2612/// @autoreleasepool compound-statement
2613///
2614StmtResult
2615Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2616 ConsumeToken(); // consume autoreleasepool
2617 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002618 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002619 return StmtError();
2620 }
2621 // Enter a scope to hold everything within the compound stmt. Compound
2622 // statements can always hold declarations.
Momchil Velikov57c681f2017-08-10 15:43:06 +00002623 ParseScope BodyScope(this, Scope::DeclScope | Scope::CompoundStmtScope);
John McCall31168b02011-06-15 23:02:42 +00002624
2625 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2626
2627 BodyScope.Exit();
2628 if (AutoreleasePoolBody.isInvalid())
2629 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2630 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002631 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002632}
2633
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002634/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2635/// for later parsing.
2636void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002637 if (SkipFunctionBodies && (!MDecl || Actions.canSkipFunctionBody(MDecl)) &&
2638 trySkippingFunctionBody()) {
2639 Actions.ActOnSkippedFunctionBody(MDecl);
2640 return;
2641 }
2642
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002643 LexedMethod* LM = new LexedMethod(this, MDecl);
2644 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2645 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002646 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002647 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002648 if (Tok.is(tok::kw_try)) {
2649 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002650 if (Tok.is(tok::colon)) {
2651 Toks.push_back(Tok);
2652 ConsumeToken();
2653 while (Tok.isNot(tok::l_brace)) {
2654 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2655 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2656 }
2657 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002658 Toks.push_back(Tok); // also store '{'
2659 }
2660 else if (Tok.is(tok::colon)) {
2661 ConsumeToken();
Richard Smithb9fa9962015-08-21 03:04:33 +00002662 // FIXME: This is wrong, due to C++11 braced initialization.
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002663 while (Tok.isNot(tok::l_brace)) {
2664 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2665 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2666 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002667 Toks.push_back(Tok); // also store '{'
2668 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002669 ConsumeBrace();
2670 // Consume everything up to (and including) the matching right brace.
2671 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002672 while (Tok.is(tok::kw_catch)) {
2673 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2674 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2675 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002676}
2677
Steve Naroff09bf8152007-09-06 21:24:23 +00002678/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002679///
John McCall48871652010-08-21 09:40:31 +00002680Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002681 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002682
Jordan Rose1e879d82018-03-23 00:07:18 +00002683 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, MDecl, Tok.getLocation(),
John McCallfaf5fb42010-08-26 23:41:50 +00002684 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002685
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002686 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002687 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002688 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002689 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002690 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002691 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002692 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002693 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002694
Steve Naroffbb875722007-11-11 19:54:21 +00002695 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002696 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002697 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002698
Steve Naroffbb875722007-11-11 19:54:21 +00002699 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002700 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002701
Steve Naroffbb875722007-11-11 19:54:21 +00002702 // If we didn't find the '{', bail out.
2703 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002704 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002705 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002706
2707 if (!MDecl) {
2708 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002709 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002710 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002711 }
2712
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002713 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002714 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002715 assert (CurParsedObjCImpl
2716 && "ParseObjCMethodDefinition - Method out of @implementation");
2717 // Consume the tokens and store them for later parsing.
2718 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002719 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002720}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002721
John McCalldadc5752010-08-24 06:29:42 +00002722StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002723 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002724 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002725 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002726 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002727 }
2728
2729 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002730 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002731
2732 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002733 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002734
2735 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002736 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002737
2738 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2739 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002740
2741 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2742 getLangOpts().DebuggerSupport) {
2743 SkipUntil(tok::semi);
2744 return Actions.ActOnNullStmt(Tok.getLocation());
2745 }
2746
Alex Lorenza589abc2016-12-01 12:14:38 +00002747 ExprStatementTokLoc = AtLoc;
John McCalldadc5752010-08-24 06:29:42 +00002748 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002749 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002750 // If the expression is invalid, skip ahead to the next semicolon. Not
2751 // doing this opens us up to the possibility of infinite loops if
2752 // ParseExpression does not consume any tokens.
2753 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002754 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002755 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002756
Steve Naroffe6016792008-02-05 21:27:35 +00002757 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002758 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002759 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002760}
2761
John McCalldadc5752010-08-24 06:29:42 +00002762ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002763 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002764 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002765 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002766 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002767 return ExprError();
2768
Ted Kremeneke65b0862012-03-06 20:05:56 +00002769 case tok::minus:
2770 case tok::plus: {
2771 tok::TokenKind Kind = Tok.getKind();
2772 SourceLocation OpLoc = ConsumeToken();
2773
2774 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002775 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002776 switch (Kind) {
2777 case tok::minus: Symbol = "-"; break;
2778 case tok::plus: Symbol = "+"; break;
2779 default: llvm_unreachable("missing unary operator case");
2780 }
2781 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2782 << Symbol;
2783 return ExprError();
2784 }
2785
2786 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2787 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002788 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002789 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002790 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002791
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002792 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002793 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002794 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002795
2796 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002797 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002798 }
2799
Chris Lattnere002fbe2007-12-12 01:04:12 +00002800 case tok::string_literal: // primary-expression: string-literal
2801 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002802 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002803
2804 case tok::char_constant:
2805 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2806
2807 case tok::numeric_constant:
2808 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2809
2810 case tok::kw_true: // Objective-C++, etc.
2811 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2812 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2813 case tok::kw_false: // Objective-C++, etc.
2814 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2815 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2816
2817 case tok::l_square:
2818 // Objective-C array literal
2819 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2820
2821 case tok::l_brace:
2822 // Objective-C dictionary literal
2823 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2824
Patrick Beard0caa3942012-04-19 00:25:12 +00002825 case tok::l_paren:
2826 // Objective-C boxed expression
2827 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2828
Chris Lattnere002fbe2007-12-12 01:04:12 +00002829 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002830 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002831 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002832
Chris Lattner197a3012008-08-05 06:19:09 +00002833 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2834 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002835 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002836 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002837 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002838 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002839 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Erik Pilkington29099de2016-07-16 00:35:23 +00002840 case tok::objc_available:
2841 return ParseAvailabilityCheckExpr(AtLoc);
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002842 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002843 const char *str = nullptr;
Alex Lorenza589abc2016-12-01 12:14:38 +00002844 // Only provide the @try/@finally/@autoreleasepool fixit when we're sure
2845 // that this is a proper statement where such directives could actually
2846 // occur.
2847 if (GetLookAheadToken(1).is(tok::l_brace) &&
2848 ExprStatementTokLoc == AtLoc) {
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002849 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2850 str =
2851 ch == 't' ? "try"
2852 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002853 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002854 }
2855 if (str) {
2856 SourceLocation kwLoc = Tok.getLocation();
2857 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2858 FixItHint::CreateReplacement(kwLoc, str));
2859 }
2860 else
2861 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2862 }
Chris Lattner197a3012008-08-05 06:19:09 +00002863 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002864 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002865}
2866
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002867/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002868///
2869/// This routine parses the receiver of a message send in
2870/// Objective-C++ either as a type or as an expression. Note that this
2871/// routine must not be called to parse a send to 'super', since it
2872/// has no way to return such a result.
2873///
2874/// \param IsExpr Whether the receiver was parsed as an expression.
2875///
2876/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2877/// IsExpr is true), the parsed expression. If the receiver was parsed
2878/// as a type (\c IsExpr is false), the parsed type.
2879///
2880/// \returns True if an error occurred during parsing or semantic
2881/// analysis, in which case the arguments do not have valid
2882/// values. Otherwise, returns false for a successful parse.
2883///
2884/// objc-receiver: [C++]
2885/// 'super' [not parsed here]
2886/// expression
2887/// simple-type-specifier
2888/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002889bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002890 InMessageExpressionRAIIObject InMessage(*this, true);
2891
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002892 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2893 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002894 TryAnnotateTypeOrScopeToken();
2895
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002896 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002897 // objc-receiver:
2898 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002899 // Make sure any typos in the receiver are corrected or diagnosed, so that
2900 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2901 // only the things that are valid ObjC receivers?
2902 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
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 // objc-receiver:
2912 // typename-specifier
2913 // simple-type-specifier
2914 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002915 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002916 ParseCXXSimpleTypeSpecifier(DS);
2917
2918 if (Tok.is(tok::l_paren)) {
2919 // If we see an opening parentheses at this point, we are
2920 // actually parsing an expression that starts with a
2921 // function-style cast, e.g.,
2922 //
2923 // postfix-expression:
2924 // simple-type-specifier ( expression-list [opt] )
2925 // typename-specifier ( expression-list [opt] )
2926 //
2927 // Parse the remainder of this case, then the (optional)
2928 // postfix-expression suffix, followed by the (optional)
2929 // right-hand side of the binary expression. We have an
2930 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002931 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002932 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002933 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002934 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002935 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002936 if (Receiver.isInvalid())
2937 return true;
2938
2939 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002940 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002941 return false;
2942 }
2943
2944 // We have a class message. Turn the simple-type-specifier or
2945 // typename-specifier we parsed into a type and parse the
2946 // remainder of the class message.
Faisal Vali421b2d12017-12-29 05:41:00 +00002947 Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002948 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002949 if (Type.isInvalid())
2950 return true;
2951
2952 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002953 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002954 return false;
2955}
2956
Douglas Gregor990ccac2010-05-31 14:40:22 +00002957/// \brief Determine whether the parser is currently referring to a an
2958/// Objective-C message send, using a simplified heuristic to avoid overhead.
2959///
2960/// This routine will only return true for a subset of valid message-send
2961/// expressions.
2962bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002963 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002964 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002965 return GetLookAheadToken(1).is(tok::identifier) &&
2966 GetLookAheadToken(2).is(tok::identifier);
2967}
2968
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002969bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002970 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002971 InMessageExpression)
2972 return false;
2973
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002974 ParsedType Type;
2975
2976 if (Tok.is(tok::annot_typename))
2977 Type = getTypeAnnotation(Tok);
2978 else if (Tok.is(tok::identifier))
2979 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2980 getCurScope());
2981 else
2982 return false;
2983
2984 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2985 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002986 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002987 if (Tok.is(tok::identifier))
2988 TryAnnotateTypeOrScopeToken();
2989
2990 return Tok.is(tok::annot_typename);
2991 }
2992 }
2993
2994 return false;
2995}
2996
Mike Stump11289f42009-09-09 15:08:12 +00002997/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002998/// '[' objc-receiver objc-message-args ']'
2999///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003000/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00003001/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003002/// expression
3003/// class-name
3004/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00003005///
John McCalldadc5752010-08-24 06:29:42 +00003006ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00003007 assert(Tok.is(tok::l_square) && "'[' expected");
3008 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
3009
Douglas Gregora817a192010-05-27 23:06:34 +00003010 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003011 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003012 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00003013 return ExprError();
3014 }
3015
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003016 InMessageExpressionRAIIObject InMessage(*this, true);
3017
David Blaikiebbafb8a2012-03-11 07:00:24 +00003018 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00003019 // We completely separate the C and C++ cases because C++ requires
3020 // more complicated (read: slower) parsing.
3021
3022 // Handle send to super.
3023 // FIXME: This doesn't benefit from the same typo-correction we
3024 // get in Objective-C.
3025 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00003026 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
David Blaikieefdccaa2016-01-15 23:43:34 +00003027 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3028 nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003029
3030 // Parse the receiver, which is either a type or an expression.
3031 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00003032 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00003033 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003034 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003035 return ExprError();
3036 }
3037
3038 if (IsExpr)
David Blaikieefdccaa2016-01-15 23:43:34 +00003039 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3040 static_cast<Expr *>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00003041
3042 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00003043 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00003044 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00003045 }
3046
3047 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00003048 IdentifierInfo *Name = Tok.getIdentifierInfo();
3049 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00003050 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003051 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00003052 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00003053 NextToken().is(tok::period),
3054 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00003055 case Sema::ObjCSuperMessage:
David Blaikieefdccaa2016-01-15 23:43:34 +00003056 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3057 nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003058
John McCallfaf5fb42010-08-26 23:41:50 +00003059 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00003060 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003061 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003062 return ExprError();
3063 }
3064
Douglas Gregore5798dc2010-04-21 20:38:13 +00003065 ConsumeToken(); // the type name
3066
Douglas Gregore83b9562015-07-07 03:57:53 +00003067 // Parse type arguments and protocol qualifiers.
3068 if (Tok.is(tok::less)) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003069 SourceLocation NewEndLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00003070 TypeResult NewReceiverType
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003071 = parseObjCTypeArgsAndProtocolQualifiers(NameLoc, ReceiverType,
3072 /*consumeLastToken=*/true,
3073 NewEndLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00003074 if (!NewReceiverType.isUsable()) {
3075 SkipUntil(tok::r_square, StopAtSemi);
3076 return ExprError();
3077 }
3078
3079 ReceiverType = NewReceiverType.get();
3080 }
3081
Douglas Gregore5798dc2010-04-21 20:38:13 +00003082 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00003083 ReceiverType, nullptr);
3084
John McCallfaf5fb42010-08-26 23:41:50 +00003085 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003086 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00003087 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00003088 }
Chris Lattner8f697062008-01-25 18:59:06 +00003089 }
Chris Lattnera36ec422010-04-11 08:28:14 +00003090
3091 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00003092 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003093 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003094 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003095 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00003096 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003097
David Blaikieefdccaa2016-01-15 23:43:34 +00003098 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3099 Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00003100}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003101
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003102/// \brief Parse the remainder of an Objective-C message following the
3103/// '[' objc-receiver.
3104///
3105/// This routine handles sends to super, class messages (sent to a
3106/// class name), and instance messages (sent to an object), and the
3107/// target is represented by \p SuperLoc, \p ReceiverType, or \p
3108/// ReceiverExpr, respectively. Only one of these parameters may have
3109/// a valid value.
3110///
3111/// \param LBracLoc The location of the opening '['.
3112///
3113/// \param SuperLoc If this is a send to 'super', the location of the
3114/// 'super' keyword that indicates a send to the superclass.
3115///
3116/// \param ReceiverType If this is a class message, the type of the
3117/// class we are sending a message to.
3118///
3119/// \param ReceiverExpr If this is an instance message, the expression
3120/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00003121///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003122/// objc-message-args:
3123/// objc-selector
3124/// objc-keywordarg-list
3125///
3126/// objc-keywordarg-list:
3127/// objc-keywordarg
3128/// objc-keywordarg-list objc-keywordarg
3129///
Mike Stump11289f42009-09-09 15:08:12 +00003130/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003131/// selector-name[opt] ':' objc-keywordexpr
3132///
3133/// objc-keywordexpr:
3134/// nonempty-expr-list
3135///
3136/// nonempty-expr-list:
3137/// assignment-expression
3138/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003139///
John McCalldadc5752010-08-24 06:29:42 +00003140ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00003141Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003142 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00003143 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00003144 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003145 InMessageExpressionRAIIObject InMessage(*this, true);
3146
Steve Naroffeae65032009-11-07 02:08:14 +00003147 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003148 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003149 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003150 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003151 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003152 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003153 false);
Steve Naroffeae65032009-11-07 02:08:14 +00003154 else
John McCallb268a282010-08-23 23:25:46 +00003155 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003156 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003157 cutOffParsing();
3158 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00003159 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00003160
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003161 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00003162 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003163 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003164
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003165 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003166 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003167 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00003168
Chris Lattner0ef13522007-10-09 17:51:17 +00003169 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003170 while (1) {
3171 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00003172 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003173 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00003174
Alp Toker383d2c42014-01-01 03:08:43 +00003175 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00003176 // We must manually skip to a ']', otherwise the expression skipper will
3177 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3178 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003179 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003180 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003181 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003182
Mike Stump11289f42009-09-09 15:08:12 +00003183 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003184
3185 if (Tok.is(tok::code_completion)) {
3186 if (SuperLoc.isValid())
3187 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003188 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003189 /*AtArgumentEpression=*/true);
3190 else if (ReceiverType)
3191 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003192 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003193 /*AtArgumentEpression=*/true);
3194 else
3195 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003196 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003197 /*AtArgumentEpression=*/true);
3198
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003199 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003200 return ExprError();
3201 }
3202
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00003203 ExprResult Expr;
3204 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3205 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3206 Expr = ParseBraceInitializer();
3207 } else
3208 Expr = ParseAssignmentExpression();
3209
3210 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003211 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00003212 // We must manually skip to a ']', otherwise the expression skipper will
3213 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3214 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003215 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003216 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00003217 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003218
Steve Naroff486760a2007-09-17 20:25:27 +00003219 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003220 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003221
Douglas Gregor1b605f72009-11-19 01:08:35 +00003222 // Code completion after each argument.
3223 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003224 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003225 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003226 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003227 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003228 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003229 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003230 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003231 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00003232 else
John McCallb268a282010-08-23 23:25:46 +00003233 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003234 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003235 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003236 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003237 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00003238 }
3239
Steve Naroff486760a2007-09-17 20:25:27 +00003240 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00003241 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00003242 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003243 break;
3244 // We have a selector or a colon, continue parsing.
3245 }
3246 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00003247 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003248 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00003249 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00003250 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00003251 if (Tok.is(tok::colon))
3252 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003253 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003254 if (Tok.is(tok::colon)) {
3255 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
3256 FixItHint::CreateRemoval(commaLoc);
3257 }
Chris Lattner197a3012008-08-05 06:19:09 +00003258 // We must manually skip to a ']', otherwise the expression skipper will
3259 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3260 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003261 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003262 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003263 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003264
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003265 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003266 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003267 }
3268 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00003269 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003270
Chris Lattner197a3012008-08-05 06:19:09 +00003271 // We must manually skip to a ']', otherwise the expression skipper will
3272 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3273 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003274 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003275 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003276 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00003277
Chris Lattner0ef13522007-10-09 17:51:17 +00003278 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00003279 Diag(Tok, diag::err_expected)
3280 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00003281 // We must manually skip to a ']', otherwise the expression skipper will
3282 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3283 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003284 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003285 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003286 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003287
Chris Lattner8f697062008-01-25 18:59:06 +00003288 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003289
Steve Naroffe61bfa82007-10-05 18:42:47 +00003290 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003291 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00003292 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003293 KeyLocs.push_back(Loc);
3294 }
Chris Lattner5700fab2007-10-07 02:00:24 +00003295 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003296
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003297 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003298 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003299 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003300 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003301 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003302 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00003303 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003304 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003305}
3306
John McCalldadc5752010-08-24 06:29:42 +00003307ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
3308 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003309 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003310
Chris Lattnere002fbe2007-12-12 01:04:12 +00003311 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
3312 // expressions. At this point, we know that the only valid thing that starts
3313 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003314 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003315 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00003316 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003317 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003318
Chris Lattnere002fbe2007-12-12 01:04:12 +00003319 while (Tok.is(tok::at)) {
3320 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00003321
Sebastian Redlc13f2682008-12-09 20:22:58 +00003322 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00003323 if (!isTokenStringLiteral())
3324 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00003325
John McCalldadc5752010-08-24 06:29:42 +00003326 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003327 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003328 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003329
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003330 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00003331 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003332
Craig Topper883dd332015-12-24 23:58:11 +00003333 return Actions.ParseObjCStringLiteral(AtLocs.data(), AtStrings);
Anders Carlsson76f4a902007-08-21 17:43:55 +00003334}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003335
Ted Kremeneke65b0862012-03-06 20:05:56 +00003336/// ParseObjCBooleanLiteral -
3337/// objc-scalar-literal : '@' boolean-keyword
3338/// ;
3339/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
3340/// ;
3341ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
3342 bool ArgValue) {
3343 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
3344 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
3345}
3346
3347/// ParseObjCCharacterLiteral -
3348/// objc-scalar-literal : '@' character-literal
3349/// ;
3350ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
3351 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
3352 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003353 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003354 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003355 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003356 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003357}
3358
3359/// ParseObjCNumericLiteral -
3360/// objc-scalar-literal : '@' scalar-literal
3361/// ;
3362/// scalar-literal : | numeric-constant /* any numeric constant. */
3363/// ;
3364ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3365 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3366 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003367 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003368 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003369 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003370 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003371}
3372
Patrick Beard0caa3942012-04-19 00:25:12 +00003373/// ParseObjCBoxedExpr -
3374/// objc-box-expression:
3375/// @( assignment-expression )
3376ExprResult
3377Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3378 if (Tok.isNot(tok::l_paren))
3379 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3380
3381 BalancedDelimiterTracker T(*this, tok::l_paren);
3382 T.consumeOpen();
3383 ExprResult ValueExpr(ParseAssignmentExpression());
3384 if (T.consumeClose())
3385 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00003386
3387 if (ValueExpr.isInvalid())
3388 return ExprError();
3389
Patrick Beard0caa3942012-04-19 00:25:12 +00003390 // Wrap the sub-expression in a parenthesized expression, to distinguish
3391 // a boxed expression from a literal.
3392 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003393 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00003394 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003395 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00003396}
3397
Ted Kremeneke65b0862012-03-06 20:05:56 +00003398ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00003399 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003400 ConsumeBracket(); // consume the l_square.
3401
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003402 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003403 while (Tok.isNot(tok::r_square)) {
3404 // Parse list of array element expressions (all must be id types).
3405 ExprResult Res(ParseAssignmentExpression());
3406 if (Res.isInvalid()) {
3407 // We must manually skip to a ']', otherwise the expression skipper will
3408 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3409 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003410 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003411 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003412 }
3413
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003414 Res = Actions.CorrectDelayedTyposInExpr(Res.get());
3415 if (Res.isInvalid())
3416 HasInvalidEltExpr = true;
3417
Ted Kremeneke65b0862012-03-06 20:05:56 +00003418 // Parse the ellipsis that indicates a pack expansion.
3419 if (Tok.is(tok::ellipsis))
3420 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
3421 if (Res.isInvalid())
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003422 HasInvalidEltExpr = true;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003423
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003424 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003425
3426 if (Tok.is(tok::comma))
3427 ConsumeToken(); // Eat the ','.
3428 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00003429 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3430 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003431 }
3432 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003433
3434 if (HasInvalidEltExpr)
3435 return ExprError();
3436
Benjamin Kramerf0623432012-08-23 22:51:59 +00003437 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00003438 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003439}
3440
3441ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3442 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3443 ConsumeBrace(); // consume the l_square.
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003444 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003445 while (Tok.isNot(tok::r_brace)) {
3446 // Parse the comma separated key : value expressions.
3447 ExprResult KeyExpr;
3448 {
3449 ColonProtectionRAIIObject X(*this);
3450 KeyExpr = ParseAssignmentExpression();
3451 if (KeyExpr.isInvalid()) {
3452 // We must manually skip to a '}', otherwise the expression skipper will
3453 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3454 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003455 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003456 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003457 }
3458 }
3459
Alp Toker383d2c42014-01-01 03:08:43 +00003460 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003461 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00003462 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003463 }
3464
3465 ExprResult ValueExpr(ParseAssignmentExpression());
3466 if (ValueExpr.isInvalid()) {
3467 // We must manually skip to a '}', otherwise the expression skipper will
3468 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3469 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003470 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003471 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003472 }
3473
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003474 // Check the key and value for possible typos
3475 KeyExpr = Actions.CorrectDelayedTyposInExpr(KeyExpr.get());
3476 ValueExpr = Actions.CorrectDelayedTyposInExpr(ValueExpr.get());
3477 if (KeyExpr.isInvalid() || ValueExpr.isInvalid())
3478 HasInvalidEltExpr = true;
3479
3480 // Parse the ellipsis that designates this as a pack expansion. Do not
3481 // ActOnPackExpansion here, leave it to template instantiation time where
3482 // we can get better diagnostics.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003483 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00003484 if (getLangOpts().CPlusPlus)
3485 TryConsumeToken(tok::ellipsis, EllipsisLoc);
3486
Ted Kremeneke65b0862012-03-06 20:05:56 +00003487 // We have a valid expression. Collect it in a vector so we can
3488 // build the argument list.
3489 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00003490 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00003491 };
3492 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00003493
3494 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00003495 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3496 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003497 }
3498 SourceLocation EndLoc = ConsumeBrace();
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003499
3500 if (HasInvalidEltExpr)
3501 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003502
3503 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00003504 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
Craig Topperd4336e02015-12-24 23:58:15 +00003505 Elements);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003506}
3507
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003508/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00003509/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00003510ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003511Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00003512 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003513
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003514 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003515
Chris Lattner197a3012008-08-05 06:19:09 +00003516 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003517 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3518
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003519 BalancedDelimiterTracker T(*this, tok::l_paren);
3520 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003521
Douglas Gregor220cac52009-02-18 17:45:20 +00003522 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003523
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003524 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003525
Douglas Gregor220cac52009-02-18 17:45:20 +00003526 if (Ty.isInvalid())
3527 return ExprError();
3528
Nico Webera7c7e602012-12-31 00:28:03 +00003529 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3530 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003531}
Anders Carlssone01493d2007-08-23 15:25:28 +00003532
3533/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00003534/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00003535ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003536Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00003537 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003538
Chris Lattner197a3012008-08-05 06:19:09 +00003539 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003540 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3541
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003542 BalancedDelimiterTracker T(*this, tok::l_paren);
3543 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003544
Alex Lorenzf1278212017-04-11 15:01:53 +00003545 if (expectIdentifier())
3546 return ExprError();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003547
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003548 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00003549 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003550
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003551 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00003552
Nico Webera7c7e602012-12-31 00:28:03 +00003553 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3554 T.getOpenLocation(), ProtoIdLoc,
3555 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00003556}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003557
3558/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003559/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003560ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003561 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003562
Chris Lattner197a3012008-08-05 06:19:09 +00003563 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003564 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3565
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003566 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003567 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003568
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003569 BalancedDelimiterTracker T(*this, tok::l_paren);
3570 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003571 bool HasOptionalParen = Tok.is(tok::l_paren);
3572 if (HasOptionalParen)
3573 ConsumeParen();
3574
Douglas Gregor67c692c2010-08-26 15:07:07 +00003575 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003576 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003577 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003578 return ExprError();
3579 }
3580
Chris Lattner4f472a32009-04-11 18:13:45 +00003581 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003582 if (!SelIdent && // missing selector name.
3583 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003584 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003585
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003586 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003587
Steve Naroff152dd812007-12-05 22:21:29 +00003588 unsigned nColons = 0;
3589 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003590 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003591 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003592 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003593 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003594 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3595 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003596 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003597
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003598 if (Tok.is(tok::r_paren))
3599 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003600
3601 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003602 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003603 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003604 return ExprError();
3605 }
3606
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003607 // Check for another keyword selector.
3608 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003609 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003610 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003611 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003612 break;
3613 }
Steve Naroff152dd812007-12-05 22:21:29 +00003614 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003615 if (HasOptionalParen && Tok.is(tok::r_paren))
3616 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003617 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003618 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003619 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3620 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003621 T.getCloseLocation(),
3622 !HasOptionalParen);
Eugene Zelenko1ced5092016-02-12 22:53:10 +00003623}
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003624
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003625void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3626 // MCDecl might be null due to error in method or c-function prototype, etc.
3627 Decl *MCDecl = LM.D;
3628 bool skip = MCDecl &&
3629 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3630 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3631 if (skip)
3632 return;
3633
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003634 // Save the current token position.
3635 SourceLocation OrigLoc = Tok.getLocation();
3636
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003637 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
Alex Lorenz812012f2017-06-19 17:53:21 +00003638 // Store an artificial EOF token to ensure that we don't run off the end of
3639 // the method's body when we come to parse it.
3640 Token Eof;
3641 Eof.startToken();
3642 Eof.setKind(tok::eof);
3643 Eof.setEofData(MCDecl);
3644 Eof.setLocation(OrigLoc);
3645 LM.Toks.push_back(Eof);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003646 // Append the current token at the end of the new token stream so that it
3647 // doesn't get lost.
3648 LM.Toks.push_back(Tok);
David Blaikie2eabcc92016-02-09 18:52:09 +00003649 PP.EnterTokenStream(LM.Toks, true);
3650
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003651 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003652 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003653
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003654 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3655 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003656 // Enter a scope for the method or c-function body.
Momchil Velikov57c681f2017-08-10 15:43:06 +00003657 ParseScope BodyScope(this, (parseMethod ? Scope::ObjCMethodScope : 0) |
3658 Scope::FnScope | Scope::DeclScope |
3659 Scope::CompoundStmtScope);
3660
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003661 // Tell the actions module that we have entered a method or c-function definition
3662 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003663 if (parseMethod)
3664 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3665 else
3666 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003667 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003668 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003669 else {
3670 if (Tok.is(tok::colon))
3671 ParseConstructorInitializer(MCDecl);
Akira Hatanakabd59b4892016-04-18 18:19:45 +00003672 else
3673 Actions.ActOnDefaultCtorInitializers(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003674 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003675 }
Alex Lorenz812012f2017-06-19 17:53:21 +00003676
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003677 if (Tok.getLocation() != OrigLoc) {
3678 // Due to parsing error, we either went over the cached tokens or
3679 // there are still cached tokens left. If it's the latter case skip the
3680 // leftover tokens.
3681 // Since this is an uncommon situation that should be avoided, use the
3682 // expensive isBeforeInTranslationUnit call.
3683 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3684 OrigLoc))
3685 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3686 ConsumeAnyToken();
3687 }
Alex Lorenz812012f2017-06-19 17:53:21 +00003688 // Clean up the remaining EOF token.
3689 ConsumeAnyToken();
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003690}