blob: 81b930a2284cdff98314f6cbc478c67f527bbf06 [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.
Erich Keane0aab7232018-07-03 20:30:34 +0000384 auto getNullabilityAttr = [&](AttributePool &Pool) -> AttributeList * {
385 return Pool.create(P.getNullabilityKeyword(nullability),
386 SourceRange(nullabilityLoc), nullptr, SourceLocation(),
387 nullptr, 0, AttributeList::AS_ContextSensitiveKeyword);
Douglas Gregor813a0662015-06-19 18:14:38 +0000388 };
389
390 if (D.getNumTypeObjects() > 0) {
391 // Add the attribute to the declarator chunk nearest the declarator.
Erich Keane0aab7232018-07-03 20:30:34 +0000392 auto nullabilityAttr = getNullabilityAttr(D.getAttributePool());
Douglas Gregor813a0662015-06-19 18:14:38 +0000393 DeclaratorChunk &chunk = D.getTypeObject(0);
394 nullabilityAttr->setNext(chunk.getAttrListRef());
395 chunk.getAttrListRef() = nullabilityAttr;
396 } else if (!addedToDeclSpec) {
397 // Otherwise, just put it on the declaration specifiers (if one
398 // isn't there already).
Erich Keane0aab7232018-07-03 20:30:34 +0000399 D.getMutableDeclSpec().addAttributes(
400 getNullabilityAttr(D.getDeclSpec().getAttributePool()));
Douglas Gregor813a0662015-06-19 18:14:38 +0000401 addedToDeclSpec = true;
402 }
403}
404
Douglas Gregor85f3f952015-07-07 03:57:15 +0000405/// Parse an Objective-C type parameter list, if present, or capture
406/// the locations of the protocol identifiers for a list of protocol
407/// references.
408///
409/// objc-type-parameter-list:
410/// '<' objc-type-parameter (',' objc-type-parameter)* '>'
411///
412/// objc-type-parameter:
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000413/// objc-type-parameter-variance? identifier objc-type-parameter-bound[opt]
Douglas Gregor85f3f952015-07-07 03:57:15 +0000414///
415/// objc-type-parameter-bound:
416/// ':' type-name
417///
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000418/// objc-type-parameter-variance:
419/// '__covariant'
420/// '__contravariant'
421///
Douglas Gregor85f3f952015-07-07 03:57:15 +0000422/// \param lAngleLoc The location of the starting '<'.
423///
424/// \param protocolIdents Will capture the list of identifiers, if the
425/// angle brackets contain a list of protocol references rather than a
426/// type parameter list.
427///
428/// \param rAngleLoc The location of the ending '>'.
429ObjCTypeParamList *Parser::parseObjCTypeParamListOrProtocolRefs(
Richard Smith3df3f1d2015-11-03 01:19:56 +0000430 ObjCTypeParamListScope &Scope, SourceLocation &lAngleLoc,
431 SmallVectorImpl<IdentifierLocPair> &protocolIdents,
432 SourceLocation &rAngleLoc, bool mayBeProtocolList) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000433 assert(Tok.is(tok::less) && "Not at the beginning of a type parameter list");
434
435 // Within the type parameter list, don't treat '>' as an operator.
436 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
437
438 // Local function to "flush" the protocol identifiers, turning them into
439 // type parameters.
440 SmallVector<Decl *, 4> typeParams;
441 auto makeProtocolIdentsIntoTypeParameters = [&]() {
Douglas Gregore83b9562015-07-07 03:57:53 +0000442 unsigned index = 0;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000443 for (const auto &pair : protocolIdents) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000444 DeclResult typeParam = Actions.actOnObjCTypeParam(
David Blaikieefdccaa2016-01-15 23:43:34 +0000445 getCurScope(), ObjCTypeParamVariance::Invariant, SourceLocation(),
446 index++, pair.first, pair.second, SourceLocation(), nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000447 if (typeParam.isUsable())
448 typeParams.push_back(typeParam.get());
449 }
450
451 protocolIdents.clear();
452 mayBeProtocolList = false;
453 };
454
455 bool invalid = false;
456 lAngleLoc = ConsumeToken();
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000457
Douglas Gregor85f3f952015-07-07 03:57:15 +0000458 do {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000459 // Parse the variance, if any.
460 SourceLocation varianceLoc;
461 ObjCTypeParamVariance variance = ObjCTypeParamVariance::Invariant;
462 if (Tok.is(tok::kw___covariant) || Tok.is(tok::kw___contravariant)) {
463 variance = Tok.is(tok::kw___covariant)
464 ? ObjCTypeParamVariance::Covariant
465 : ObjCTypeParamVariance::Contravariant;
466 varianceLoc = ConsumeToken();
467
468 // Once we've seen a variance specific , we know this is not a
469 // list of protocol references.
470 if (mayBeProtocolList) {
471 // Up until now, we have been queuing up parameters because they
472 // might be protocol references. Turn them into parameters now.
473 makeProtocolIdentsIntoTypeParameters();
474 }
475 }
476
Douglas Gregor85f3f952015-07-07 03:57:15 +0000477 // Parse the identifier.
478 if (!Tok.is(tok::identifier)) {
479 // Code completion.
480 if (Tok.is(tok::code_completion)) {
481 // FIXME: If these aren't protocol references, we'll need different
482 // completions.
Craig Topper883dd332015-12-24 23:58:11 +0000483 Actions.CodeCompleteObjCProtocolReferences(protocolIdents);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000484 cutOffParsing();
485
486 // FIXME: Better recovery here?.
487 return nullptr;
488 }
489
490 Diag(Tok, diag::err_objc_expected_type_parameter);
491 invalid = true;
492 break;
493 }
494
495 IdentifierInfo *paramName = Tok.getIdentifierInfo();
496 SourceLocation paramLoc = ConsumeToken();
497
498 // If there is a bound, parse it.
499 SourceLocation colonLoc;
500 TypeResult boundType;
501 if (TryConsumeToken(tok::colon, colonLoc)) {
502 // Once we've seen a bound, we know this is not a list of protocol
503 // references.
504 if (mayBeProtocolList) {
505 // Up until now, we have been queuing up parameters because they
506 // might be protocol references. Turn them into parameters now.
507 makeProtocolIdentsIntoTypeParameters();
508 }
509
510 // type-name
511 boundType = ParseTypeName();
512 if (boundType.isInvalid())
513 invalid = true;
514 } else if (mayBeProtocolList) {
515 // If this could still be a protocol list, just capture the identifier.
516 // We don't want to turn it into a parameter.
517 protocolIdents.push_back(std::make_pair(paramName, paramLoc));
518 continue;
519 }
520
521 // Create the type parameter.
David Blaikieefdccaa2016-01-15 23:43:34 +0000522 DeclResult typeParam = Actions.actOnObjCTypeParam(
523 getCurScope(), variance, varianceLoc, typeParams.size(), paramName,
524 paramLoc, colonLoc, boundType.isUsable() ? boundType.get() : nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000525 if (typeParam.isUsable())
526 typeParams.push_back(typeParam.get());
527 } while (TryConsumeToken(tok::comma));
528
529 // Parse the '>'.
530 if (invalid) {
531 SkipUntil(tok::greater, tok::at, StopBeforeMatch);
532 if (Tok.is(tok::greater))
533 ConsumeToken();
534 } else if (ParseGreaterThanInTemplateList(rAngleLoc,
535 /*ConsumeLastToken=*/true,
536 /*ObjCGenericList=*/true)) {
537 Diag(lAngleLoc, diag::note_matching) << "'<'";
538 SkipUntil({tok::greater, tok::greaterequal, tok::at, tok::minus,
539 tok::minus, tok::plus, tok::colon, tok::l_paren, tok::l_brace,
540 tok::comma, tok::semi },
541 StopBeforeMatch);
542 if (Tok.is(tok::greater))
543 ConsumeToken();
544 }
545
546 if (mayBeProtocolList) {
547 // A type parameter list must be followed by either a ':' (indicating the
548 // presence of a superclass) or a '(' (indicating that this is a category
549 // or extension). This disambiguates between an objc-type-parameter-list
550 // and a objc-protocol-refs.
551 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_paren)) {
552 // Returning null indicates that we don't have a type parameter list.
553 // The results the caller needs to handle the protocol references are
554 // captured in the reference parameters already.
555 return nullptr;
556 }
557
558 // We have a type parameter list that looks like a list of protocol
559 // references. Turn that parameter list into type parameters.
560 makeProtocolIdentsIntoTypeParameters();
561 }
562
Richard Smith3df3f1d2015-11-03 01:19:56 +0000563 // Form the type parameter list and enter its scope.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000564 ObjCTypeParamList *list = Actions.actOnObjCTypeParamList(
565 getCurScope(),
566 lAngleLoc,
567 typeParams,
568 rAngleLoc);
Richard Smith3df3f1d2015-11-03 01:19:56 +0000569 Scope.enter(list);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000570
571 // Clear out the angle locations; they're used by the caller to indicate
572 // whether there are any protocol references.
573 lAngleLoc = SourceLocation();
574 rAngleLoc = SourceLocation();
Akira Hatanaka8ebd5802015-12-16 06:25:38 +0000575 return invalid ? nullptr : list;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000576}
577
578/// Parse an objc-type-parameter-list.
579ObjCTypeParamList *Parser::parseObjCTypeParamList() {
580 SourceLocation lAngleLoc;
581 SmallVector<IdentifierLocPair, 1> protocolIdents;
582 SourceLocation rAngleLoc;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000583
584 ObjCTypeParamListScope Scope(Actions, getCurScope());
585 return parseObjCTypeParamListOrProtocolRefs(Scope, lAngleLoc, protocolIdents,
586 rAngleLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000587 /*mayBeProtocolList=*/false);
588}
589
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000590/// objc-interface-decl-list:
591/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000592/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000593/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000594/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000595/// objc-interface-decl-list declaration
596/// objc-interface-decl-list ';'
597///
Steve Naroff99264b42007-08-22 16:35:03 +0000598/// objc-method-requirement: [OBJC2]
599/// @required
600/// @optional
601///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000602void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
603 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000604 SmallVector<Decl *, 32> allMethods;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000605 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000606 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000607
Ted Kremenekc7c64312010-01-07 01:20:12 +0000608 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000609
Steve Naroff99264b42007-08-22 16:35:03 +0000610 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000611 // If this is a method prototype, parse it.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000612 if (Tok.isOneOf(tok::minus, tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000613 if (Decl *methodPrototype =
614 ParseObjCMethodPrototype(MethodImplKind, false))
615 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000616 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
617 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000618 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
619 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000620 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000621 if (Tok.is(tok::semi))
622 ConsumeToken();
623 }
Steve Naroff99264b42007-08-22 16:35:03 +0000624 continue;
625 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000626 if (Tok.is(tok::l_paren)) {
627 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000628 ParseObjCMethodDecl(Tok.getLocation(),
629 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000630 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000631 continue;
632 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000633 // Ignore excess semicolons.
634 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000635 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000636 continue;
637 }
Mike Stump11289f42009-09-09 15:08:12 +0000638
Chris Lattnerda9fb152008-10-20 06:10:06 +0000639 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000640 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000641 break;
Mike Stump11289f42009-09-09 15:08:12 +0000642
Douglas Gregorf1934162010-01-13 21:24:21 +0000643 // Code completion within an Objective-C interface.
644 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000645 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000646 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000647 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000648 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000649 }
650
Chris Lattner038a3e32008-10-20 05:46:22 +0000651 // If we don't have an @ directive, parse it as a function definition.
652 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000653 // The code below does not consume '}'s because it is afraid of eating the
654 // end of a namespace. Because of the way this code is structured, an
655 // erroneous r_brace would cause an infinite loop if not handled here.
656 if (Tok.is(tok::r_brace))
657 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000658 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000659 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000660 continue;
661 }
Mike Stump11289f42009-09-09 15:08:12 +0000662
Chris Lattner038a3e32008-10-20 05:46:22 +0000663 // Otherwise, we have an @ directive, eat the @.
664 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000665 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000666 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000667 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000668 }
669
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000670 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000671
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000672 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000673 AtEnd.setBegin(AtLoc);
674 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000675 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000676 } else if (DirectiveKind == tok::objc_not_keyword) {
677 Diag(Tok, diag::err_objc_unknown_at);
678 SkipUntil(tok::semi);
679 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000680 }
Mike Stump11289f42009-09-09 15:08:12 +0000681
Chris Lattnerda9fb152008-10-20 06:10:06 +0000682 // Eat the identifier.
683 ConsumeToken();
684
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000685 switch (DirectiveKind) {
686 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000687 // FIXME: If someone forgets an @end on a protocol, this loop will
688 // continue to eat up tons of stuff and spew lots of nonsense errors. It
689 // would probably be better to bail out if we saw an @class or @interface
690 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000691 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000692 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000693 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000694 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000695
696 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000697 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000698 Diag(AtLoc, diag::err_objc_missing_end)
699 << FixItHint::CreateInsertion(AtLoc, "@end\n");
700 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
701 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000702 ConsumeToken();
703 break;
704
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000705 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000706 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000707 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000708 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000709 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000710 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000711 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000712 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000713 break;
Mike Stump11289f42009-09-09 15:08:12 +0000714
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000715 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000716 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000717 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000718
Chris Lattner038a3e32008-10-20 05:46:22 +0000719 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000720 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000721 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000722 if (Tok.is(tok::l_paren)) {
723 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000724 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000725 }
Mike Stump11289f42009-09-09 15:08:12 +0000726
Douglas Gregor813a0662015-06-19 18:14:38 +0000727 bool addedToDeclSpec = false;
Benjamin Kramera39beb92014-09-03 11:06:10 +0000728 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
729 if (FD.D.getIdentifier() == nullptr) {
730 Diag(AtLoc, diag::err_objc_property_requires_field_name)
731 << FD.D.getSourceRange();
732 return;
733 }
734 if (FD.BitfieldSize) {
735 Diag(AtLoc, diag::err_objc_property_bitfield)
736 << FD.D.getSourceRange();
737 return;
738 }
739
Douglas Gregor813a0662015-06-19 18:14:38 +0000740 // Map a nullability property attribute to a context-sensitive keyword
741 // attribute.
742 if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
743 addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
744 OCDS.getNullabilityLoc(),
745 addedToDeclSpec);
746
Benjamin Kramera39beb92014-09-03 11:06:10 +0000747 // Install the property declarator into interfaceDecl.
748 IdentifierInfo *SelName =
749 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
750
751 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
752 IdentifierInfo *SetterName = OCDS.getSetterName();
753 Selector SetterSel;
754 if (SetterName)
755 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
756 else
757 SetterSel = SelectorTable::constructSetterSelector(
758 PP.getIdentifierTable(), PP.getSelectorTable(),
759 FD.D.getIdentifier());
Benjamin Kramera39beb92014-09-03 11:06:10 +0000760 Decl *Property = Actions.ActOnProperty(
761 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000762 MethodImplKind);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000763
764 FD.complete(Property);
765 };
John McCallcfefb6d2009-11-03 02:38:08 +0000766
Chris Lattner038a3e32008-10-20 05:46:22 +0000767 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000768 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000769 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000770
John McCall405988b2011-03-26 01:53:26 +0000771 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000772 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000773 }
Steve Naroff99264b42007-08-22 16:35:03 +0000774 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000775
776 // We break out of the big loop in two cases: when we see @end or when we see
777 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000778 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000779 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000780 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000781 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000782 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000783 } else {
784 Diag(Tok, diag::err_objc_missing_end)
785 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
786 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
787 << (int) Actions.getObjCContainerKind();
788 AtEnd.setBegin(Tok.getLocation());
789 AtEnd.setEnd(Tok.getLocation());
790 }
Mike Stump11289f42009-09-09 15:08:12 +0000791
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000792 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000793 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000794 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000795}
796
Douglas Gregor813a0662015-06-19 18:14:38 +0000797/// Diagnose redundant or conflicting nullability information.
798static void diagnoseRedundantPropertyNullability(Parser &P,
799 ObjCDeclSpec &DS,
800 NullabilityKind nullability,
801 SourceLocation nullabilityLoc){
802 if (DS.getNullability() == nullability) {
803 P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000804 << DiagNullabilityKind(nullability, true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000805 << SourceRange(DS.getNullabilityLoc());
806 return;
807 }
808
809 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000810 << DiagNullabilityKind(nullability, true)
811 << DiagNullabilityKind(DS.getNullability(), true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000812 << SourceRange(DS.getNullabilityLoc());
813}
814
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000815/// Parse property attribute declarations.
816///
817/// property-attr-decl: '(' property-attrlist ')'
818/// property-attrlist:
819/// property-attribute
820/// property-attrlist ',' property-attribute
821/// property-attribute:
822/// getter '=' identifier
823/// setter '=' identifier ':'
824/// readonly
825/// readwrite
826/// assign
827/// retain
828/// copy
829/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000830/// atomic
831/// strong
832/// weak
833/// unsafe_unretained
Douglas Gregor813a0662015-06-19 18:14:38 +0000834/// nonnull
835/// nullable
836/// null_unspecified
Douglas Gregor849ebc22015-06-19 18:14:46 +0000837/// null_resettable
Manman Ren387ff7f2016-01-26 18:52:43 +0000838/// class
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000839///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000840void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000841 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000842 BalancedDelimiterTracker T(*this, tok::l_paren);
843 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000844
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000845 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000846 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000847 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000848 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000849 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000850 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000851
Chris Lattner76619232008-10-20 07:22:18 +0000852 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000853 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000854 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000855 return;
856 }
Mike Stump11289f42009-09-09 15:08:12 +0000857
Chris Lattner1db33542008-10-20 07:37:22 +0000858 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000859
Chris Lattner68e48682008-11-20 04:42:34 +0000860 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000861 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000862 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000863 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000864 else if (II->isStr("unsafe_unretained"))
865 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000866 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000867 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000868 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000869 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000870 else if (II->isStr("strong"))
871 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000872 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000873 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000874 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000875 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000876 else if (II->isStr("atomic"))
877 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000878 else if (II->isStr("weak"))
879 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000880 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000881 bool IsSetter = II->getNameStart()[0] == 's';
882
Chris Lattner825bca12008-10-20 07:39:53 +0000883 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000884 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
Craig Topper3110a5c2015-11-14 18:16:02 +0000885 diag::err_objc_expected_equal_for_getter;
Anders Carlssonfe15a782010-10-02 17:45:21 +0000886
Alp Toker383d2c42014-01-01 03:08:43 +0000887 if (ExpectAndConsume(tok::equal, DiagID)) {
888 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000889 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000890 }
Mike Stump11289f42009-09-09 15:08:12 +0000891
Douglas Gregorc8537c52009-11-19 07:41:15 +0000892 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000893 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000894 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000895 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000896 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000897 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000898 }
899
Anders Carlssonfe15a782010-10-02 17:45:21 +0000900 SourceLocation SelLoc;
901 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
902
903 if (!SelIdent) {
904 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
905 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000906 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000907 return;
908 }
Mike Stump11289f42009-09-09 15:08:12 +0000909
Anders Carlssonfe15a782010-10-02 17:45:21 +0000910 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000911 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000912 DS.setSetterName(SelIdent, SelLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000913
Alp Toker383d2c42014-01-01 03:08:43 +0000914 if (ExpectAndConsume(tok::colon,
915 diag::err_expected_colon_after_setter_name)) {
916 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000917 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000918 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000919 } else {
920 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000921 DS.setGetterName(SelIdent, SelLoc);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000922 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000923 } else if (II->isStr("nonnull")) {
924 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
925 diagnoseRedundantPropertyNullability(*this, DS,
926 NullabilityKind::NonNull,
927 Tok.getLocation());
928 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
929 DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
930 } else if (II->isStr("nullable")) {
931 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
932 diagnoseRedundantPropertyNullability(*this, DS,
933 NullabilityKind::Nullable,
934 Tok.getLocation());
935 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
936 DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
937 } else if (II->isStr("null_unspecified")) {
938 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
939 diagnoseRedundantPropertyNullability(*this, DS,
940 NullabilityKind::Unspecified,
941 Tok.getLocation());
942 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
943 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000944 } else if (II->isStr("null_resettable")) {
945 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
946 diagnoseRedundantPropertyNullability(*this, DS,
947 NullabilityKind::Unspecified,
948 Tok.getLocation());
949 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
950 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
951
952 // Also set the null_resettable bit.
953 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
Manman Ren387ff7f2016-01-26 18:52:43 +0000954 } else if (II->isStr("class")) {
955 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_class);
Chris Lattner825bca12008-10-20 07:39:53 +0000956 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000957 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000958 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000959 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000960 }
Mike Stump11289f42009-09-09 15:08:12 +0000961
Chris Lattner1db33542008-10-20 07:37:22 +0000962 if (Tok.isNot(tok::comma))
963 break;
Mike Stump11289f42009-09-09 15:08:12 +0000964
Chris Lattner1db33542008-10-20 07:37:22 +0000965 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000966 }
Mike Stump11289f42009-09-09 15:08:12 +0000967
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000968 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000969}
970
Steve Naroff09bf8152007-09-06 21:24:23 +0000971/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000972/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000973/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000974///
975/// objc-instance-method: '-'
976/// objc-class-method: '+'
977///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000978/// objc-method-attributes: [OBJC2]
979/// __attribute__((deprecated))
980///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000981Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000982 bool MethodDefinition) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000983 assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000984
Mike Stump11289f42009-09-09 15:08:12 +0000985 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000986 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000987 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000988 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000989 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000990 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000991 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000992}
993
994/// objc-selector:
995/// identifier
996/// one of
997/// enum struct union if else while do for switch case default
998/// break continue return goto asm sizeof typeof __alignof
999/// unsigned long const short volatile signed restrict _Complex
1000/// in out inout bycopy byref oneway int char float double void _Bool
1001///
Chris Lattner4f472a32009-04-11 18:13:45 +00001002IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +00001003
Chris Lattner5700fab2007-10-07 02:00:24 +00001004 switch (Tok.getKind()) {
1005 default:
Craig Topper161e4db2014-05-21 06:02:52 +00001006 return nullptr;
Alex Lorenz9b9188d2017-07-13 10:50:21 +00001007 case tok::colon:
1008 // Empty selector piece uses the location of the ':'.
1009 SelectorLoc = Tok.getLocation();
1010 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001011 case tok::ampamp:
1012 case tok::ampequal:
1013 case tok::amp:
1014 case tok::pipe:
1015 case tok::tilde:
1016 case tok::exclaim:
1017 case tok::exclaimequal:
1018 case tok::pipepipe:
1019 case tok::pipeequal:
1020 case tok::caret:
1021 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +00001022 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +00001023 if (isLetter(ThisTok[0])) {
Malcolm Parsons731ca0e2016-11-03 12:25:51 +00001024 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok);
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001025 Tok.setKind(tok::identifier);
1026 SelectorLoc = ConsumeToken();
1027 return II;
1028 }
Craig Topper161e4db2014-05-21 06:02:52 +00001029 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001030 }
1031
Chris Lattner5700fab2007-10-07 02:00:24 +00001032 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001033 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +00001034 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001035 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001036 case tok::kw_break:
1037 case tok::kw_case:
1038 case tok::kw_catch:
1039 case tok::kw_char:
1040 case tok::kw_class:
1041 case tok::kw_const:
1042 case tok::kw_const_cast:
1043 case tok::kw_continue:
1044 case tok::kw_default:
1045 case tok::kw_delete:
1046 case tok::kw_do:
1047 case tok::kw_double:
1048 case tok::kw_dynamic_cast:
1049 case tok::kw_else:
1050 case tok::kw_enum:
1051 case tok::kw_explicit:
1052 case tok::kw_export:
1053 case tok::kw_extern:
1054 case tok::kw_false:
1055 case tok::kw_float:
1056 case tok::kw_for:
1057 case tok::kw_friend:
1058 case tok::kw_goto:
1059 case tok::kw_if:
1060 case tok::kw_inline:
1061 case tok::kw_int:
1062 case tok::kw_long:
1063 case tok::kw_mutable:
1064 case tok::kw_namespace:
1065 case tok::kw_new:
1066 case tok::kw_operator:
1067 case tok::kw_private:
1068 case tok::kw_protected:
1069 case tok::kw_public:
1070 case tok::kw_register:
1071 case tok::kw_reinterpret_cast:
1072 case tok::kw_restrict:
1073 case tok::kw_return:
1074 case tok::kw_short:
1075 case tok::kw_signed:
1076 case tok::kw_sizeof:
1077 case tok::kw_static:
1078 case tok::kw_static_cast:
1079 case tok::kw_struct:
1080 case tok::kw_switch:
1081 case tok::kw_template:
1082 case tok::kw_this:
1083 case tok::kw_throw:
1084 case tok::kw_true:
1085 case tok::kw_try:
1086 case tok::kw_typedef:
1087 case tok::kw_typeid:
1088 case tok::kw_typename:
1089 case tok::kw_typeof:
1090 case tok::kw_union:
1091 case tok::kw_unsigned:
1092 case tok::kw_using:
1093 case tok::kw_virtual:
1094 case tok::kw_void:
1095 case tok::kw_volatile:
1096 case tok::kw_wchar_t:
1097 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +00001098 case tok::kw__Bool:
1099 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001100 case tok::kw___alignof:
Richard Smithe301ba22015-11-11 02:02:15 +00001101 case tok::kw___auto_type:
Chris Lattner5700fab2007-10-07 02:00:24 +00001102 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001103 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +00001104 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +00001105 }
Steve Naroff99264b42007-08-22 16:35:03 +00001106}
1107
Fariborz Jahanian83615522008-01-02 22:54:34 +00001108/// objc-for-collection-in: 'in'
1109///
Fariborz Jahanian3622e592008-01-04 23:04:08 +00001110bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001111 // FIXME: May have to do additional look-ahead to only allow for
1112 // valid tokens following an 'in'; such as an identifier, unary operators,
1113 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001114 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +00001115 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +00001116}
1117
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001118/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +00001119/// qualifier list and builds their bitmask representation in the input
1120/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +00001121///
1122/// objc-type-qualifiers:
1123/// objc-type-qualifier
1124/// objc-type-qualifiers objc-type-qualifier
1125///
Douglas Gregor813a0662015-06-19 18:14:38 +00001126/// objc-type-qualifier:
1127/// 'in'
1128/// 'out'
1129/// 'inout'
1130/// 'oneway'
1131/// 'bycopy'
1132/// 'byref'
1133/// 'nonnull'
1134/// 'nullable'
1135/// 'null_unspecified'
1136///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001137void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
Faisal Vali421b2d12017-12-29 05:41:00 +00001138 DeclaratorContext Context) {
1139 assert(Context == DeclaratorContext::ObjCParameterContext ||
1140 Context == DeclaratorContext::ObjCResultContext);
John McCalla55902b2011-10-01 09:56:14 +00001141
Chris Lattner61511e12007-12-12 06:56:32 +00001142 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +00001143 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +00001144 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
Faisal Vali421b2d12017-12-29 05:41:00 +00001145 Context == DeclaratorContext::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001146 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +00001147 }
1148
Chris Lattner5e530bc2007-12-27 19:57:00 +00001149 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +00001150 return;
Mike Stump11289f42009-09-09 15:08:12 +00001151
Chris Lattner61511e12007-12-12 06:56:32 +00001152 const IdentifierInfo *II = Tok.getIdentifierInfo();
1153 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001154 if (II != ObjCTypeQuals[i] ||
1155 NextToken().is(tok::less) ||
1156 NextToken().is(tok::coloncolon))
Chris Lattner61511e12007-12-12 06:56:32 +00001157 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001158
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001159 ObjCDeclSpec::ObjCDeclQualifier Qual;
Douglas Gregor813a0662015-06-19 18:14:38 +00001160 NullabilityKind Nullability;
Chris Lattner61511e12007-12-12 06:56:32 +00001161 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +00001162 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001163 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
1164 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
1165 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
1166 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
1167 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
1168 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Douglas Gregor813a0662015-06-19 18:14:38 +00001169
1170 case objc_nonnull:
1171 Qual = ObjCDeclSpec::DQ_CSNullability;
1172 Nullability = NullabilityKind::NonNull;
1173 break;
1174
1175 case objc_nullable:
1176 Qual = ObjCDeclSpec::DQ_CSNullability;
1177 Nullability = NullabilityKind::Nullable;
1178 break;
1179
1180 case objc_null_unspecified:
1181 Qual = ObjCDeclSpec::DQ_CSNullability;
1182 Nullability = NullabilityKind::Unspecified;
1183 break;
Chris Lattner61511e12007-12-12 06:56:32 +00001184 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001185
1186 // FIXME: Diagnose redundant specifiers.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001187 DS.setObjCDeclQualifier(Qual);
Douglas Gregor813a0662015-06-19 18:14:38 +00001188 if (Qual == ObjCDeclSpec::DQ_CSNullability)
1189 DS.setNullability(Tok.getLocation(), Nullability);
1190
Chris Lattner61511e12007-12-12 06:56:32 +00001191 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001192 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +00001193 break;
1194 }
Mike Stump11289f42009-09-09 15:08:12 +00001195
Chris Lattner61511e12007-12-12 06:56:32 +00001196 // If this wasn't a recognized qualifier, bail out.
1197 if (II) return;
1198 }
1199}
1200
John McCalla55902b2011-10-01 09:56:14 +00001201/// Take all the decl attributes out of the given list and add
1202/// them to the given attribute set.
1203static void takeDeclAttributes(ParsedAttributes &attrs,
1204 AttributeList *list) {
1205 while (list) {
1206 AttributeList *cur = list;
1207 list = cur->getNext();
1208
1209 if (!cur->isUsedAsTypeAttr()) {
1210 // Clear out the next pointer. We're really completely
1211 // destroying the internal invariants of the declarator here,
1212 // but it doesn't matter because we're done with it.
Craig Topper161e4db2014-05-21 06:02:52 +00001213 cur->setNext(nullptr);
John McCalla55902b2011-10-01 09:56:14 +00001214 attrs.add(cur);
1215 }
1216 }
1217}
1218
1219/// takeDeclAttributes - Take all the decl attributes from the given
1220/// declarator and add them to the given list.
1221static void takeDeclAttributes(ParsedAttributes &attrs,
1222 Declarator &D) {
1223 // First, take ownership of all attributes.
1224 attrs.getPool().takeAllFrom(D.getAttributePool());
1225 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
1226
1227 // Now actually move the attributes over.
1228 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
1229 takeDeclAttributes(attrs, D.getAttributes());
1230 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
1231 takeDeclAttributes(attrs,
1232 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
1233}
1234
Chris Lattner61511e12007-12-12 06:56:32 +00001235/// objc-type-name:
1236/// '(' objc-type-qualifiers[opt] type-name ')'
1237/// '(' objc-type-qualifiers[opt] ')'
1238///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001239ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
Faisal Vali421b2d12017-12-29 05:41:00 +00001240 DeclaratorContext context,
John McCalla55902b2011-10-01 09:56:14 +00001241 ParsedAttributes *paramAttrs) {
Faisal Vali421b2d12017-12-29 05:41:00 +00001242 assert(context == DeclaratorContext::ObjCParameterContext ||
1243 context == DeclaratorContext::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +00001244 assert((paramAttrs != nullptr) ==
Faisal Vali421b2d12017-12-29 05:41:00 +00001245 (context == DeclaratorContext::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +00001246
Chris Lattner0ef13522007-10-09 17:51:17 +00001247 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +00001248
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001249 BalancedDelimiterTracker T(*this, tok::l_paren);
1250 T.consumeOpen();
1251
Chris Lattner2ebb1782008-08-23 01:48:03 +00001252 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001253 ObjCDeclContextSwitch ObjCDC(*this);
1254
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001255 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001256 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +00001257
John McCallba7bf592010-08-24 05:47:05 +00001258 ParsedType Ty;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001259 if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
John McCalla55902b2011-10-01 09:56:14 +00001260 // Parse an abstract declarator.
1261 DeclSpec declSpec(AttrFactory);
1262 declSpec.setObjCQualifiers(&DS);
Faisal Vali7db85c52017-12-31 00:06:40 +00001263 DeclSpecContext dsContext = DeclSpecContext::DSC_normal;
Faisal Vali421b2d12017-12-29 05:41:00 +00001264 if (context == DeclaratorContext::ObjCResultContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00001265 dsContext = DeclSpecContext::DSC_objc_method_result;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001266 ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
John McCalla55902b2011-10-01 09:56:14 +00001267 Declarator declarator(declSpec, context);
1268 ParseDeclarator(declarator);
1269
1270 // If that's not invalid, extract a type.
1271 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001272 // Map a nullability specifier to a context-sensitive keyword attribute.
1273 bool addedToDeclSpec = false;
1274 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1275 addContextSensitiveTypeNullability(*this, declarator,
1276 DS.getNullability(),
1277 DS.getNullabilityLoc(),
1278 addedToDeclSpec);
1279
John McCalla55902b2011-10-01 09:56:14 +00001280 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1281 if (!type.isInvalid())
1282 Ty = type.get();
1283
1284 // If we're parsing a parameter, steal all the decl attributes
1285 // and add them to the decl spec.
Faisal Vali421b2d12017-12-29 05:41:00 +00001286 if (context == DeclaratorContext::ObjCParameterContext)
John McCalla55902b2011-10-01 09:56:14 +00001287 takeDeclAttributes(*paramAttrs, declarator);
1288 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001289 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001290
Steve Naroff90255b42008-10-21 14:15:04 +00001291 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001292 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001293 else if (Tok.getLocation() == TypeStartLoc) {
1294 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001295 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001296 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001297 } else {
1298 // Otherwise, we found *something*, but didn't get a ')' in the right
1299 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001300 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001301 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001302 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001303}
1304
1305/// objc-method-decl:
1306/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001307/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001308/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001309/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001310///
1311/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001312/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001313/// objc-keyword-selector objc-keyword-decl
1314///
1315/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001316/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1317/// objc-selector ':' objc-keyword-attributes[opt] identifier
1318/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1319/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001320///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001321/// objc-parmlist:
1322/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001323///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001324/// objc-parms:
1325/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001326///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001327/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001328/// , ...
1329///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001330/// objc-keyword-attributes: [OBJC2]
1331/// __attribute__((unused))
1332///
John McCall48871652010-08-21 09:40:31 +00001333Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001334 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001335 tok::ObjCKeywordKind MethodImplKind,
1336 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001337 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001338
Douglas Gregor636a61e2010-04-07 00:21:17 +00001339 if (Tok.is(tok::code_completion)) {
David Blaikieefdccaa2016-01-15 23:43:34 +00001340 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
1341 /*ReturnType=*/nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001342 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001343 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001344 }
1345
Chris Lattner2ebb1782008-08-23 01:48:03 +00001346 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001347 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001348 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001349 if (Tok.is(tok::l_paren))
Faisal Vali421b2d12017-12-29 05:41:00 +00001350 ReturnType = ParseObjCTypeName(DSRet, DeclaratorContext::ObjCResultContext,
Craig Topper161e4db2014-05-21 06:02:52 +00001351 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001352
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001353 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001354 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001355 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001356 MaybeParseGNUAttributes(methodAttrs);
Aaron Ballman1c606c22018-02-12 13:38:25 +00001357 MaybeParseCXX11Attributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001358
Douglas Gregor636a61e2010-04-07 00:21:17 +00001359 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001360 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001361 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001362 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001363 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001364 }
1365
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001366 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001367 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001368 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001369
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001370 // An unnamed colon is valid.
1371 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001372 Diag(Tok, diag::err_expected_selector_for_method)
1373 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001374 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001375 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001376 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001377 }
Mike Stump11289f42009-09-09 15:08:12 +00001378
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001379 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001380 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001381 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001382 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001383 MaybeParseGNUAttributes(methodAttrs);
Aaron Ballman1c606c22018-02-12 13:38:25 +00001384 MaybeParseCXX11Attributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001385
Chris Lattner5700fab2007-10-07 02:00:24 +00001386 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001387 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001388 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001389 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001390 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001391 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001392 methodAttrs.getList(), MethodImplKind,
1393 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001394 PD.complete(Result);
1395 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001396 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001397
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001398 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001399 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001400 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001401 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1402 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001403
1404 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001405 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001406 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001407 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001408
Chris Lattner5700fab2007-10-07 02:00:24 +00001409 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001410 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001411 break;
Mike Stump11289f42009-09-09 15:08:12 +00001412
David Blaikieefdccaa2016-01-15 23:43:34 +00001413 ArgInfo.Type = nullptr;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001414 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001415 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
Faisal Vali421b2d12017-12-29 05:41:00 +00001416 DeclaratorContext::ObjCParameterContext,
John McCalla55902b2011-10-01 09:56:14 +00001417 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001418
Chris Lattner5700fab2007-10-07 02:00:24 +00001419 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001420 // Regardless, collect all the attributes we've parsed so far.
Aaron Ballman1c606c22018-02-12 13:38:25 +00001421 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001422 MaybeParseGNUAttributes(paramAttrs);
Aaron Ballman1c606c22018-02-12 13:38:25 +00001423 MaybeParseCXX11Attributes(paramAttrs);
1424 ArgInfo.ArgAttrs = paramAttrs.getList();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001425
Douglas Gregor45879692010-07-08 23:37:41 +00001426 // Code completion for the next piece of the selector.
1427 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001428 KeyIdents.push_back(SelIdent);
1429 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1430 mType == tok::minus,
1431 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001432 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001433 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001434 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001435 }
Alex Lorenzf1278212017-04-11 15:01:53 +00001436
1437 if (expectIdentifier())
1438 break; // missing argument name.
Mike Stump11289f42009-09-09 15:08:12 +00001439
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001440 ArgInfo.Name = Tok.getIdentifierInfo();
1441 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001442 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001443
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001444 ArgInfos.push_back(ArgInfo);
1445 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001446 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001447
John McCall084e83d2011-03-24 11:26:52 +00001448 // Make sure the attributes persist.
1449 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1450
Douglas Gregor95887f92010-07-08 23:20:03 +00001451 // Code completion for the next piece of the selector.
1452 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001453 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1454 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001455 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001456 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001457 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001458 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001459 }
1460
Chris Lattner5700fab2007-10-07 02:00:24 +00001461 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001462 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001463 if (!SelIdent && Tok.isNot(tok::colon))
1464 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001465 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001466 SourceLocation ColonLoc = Tok.getLocation();
1467 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001468 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1469 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1470 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001471 }
1472 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001473 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001474 }
Mike Stump11289f42009-09-09 15:08:12 +00001475
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001476 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001477 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001478 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001479 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001480 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001481 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001482 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001483 ConsumeToken();
1484 break;
1485 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001486 if (!cStyleParamWarned) {
1487 Diag(Tok, diag::warn_cstyle_param);
1488 cStyleParamWarned = true;
1489 }
John McCall084e83d2011-03-24 11:26:52 +00001490 DeclSpec DS(AttrFactory);
Faisal Valia534f072018-04-26 00:42:40 +00001491 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001492 // Parse the declarator.
Faisal Vali421b2d12017-12-29 05:41:00 +00001493 Declarator ParmDecl(DS, DeclaratorContext::PrototypeContext);
Chris Lattner5700fab2007-10-07 02:00:24 +00001494 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001495 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001496 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001497 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1498 ParmDecl.getIdentifierLoc(),
1499 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001500 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001501 }
Mike Stump11289f42009-09-09 15:08:12 +00001502
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001503 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001504 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001505 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001506 MaybeParseGNUAttributes(methodAttrs);
Aaron Ballman1c606c22018-02-12 13:38:25 +00001507 MaybeParseCXX11Attributes(methodAttrs);
1508
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001509 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001510 return nullptr;
1511
Chris Lattner5700fab2007-10-07 02:00:24 +00001512 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1513 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001514 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001515 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001516 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001517 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001518 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001519 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001520 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001521
John McCall28a6aea2009-11-04 02:18:39 +00001522 PD.complete(Result);
1523 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001524}
1525
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001526/// objc-protocol-refs:
1527/// '<' identifier-list '>'
1528///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001529bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001530ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1531 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001532 bool WarnOnDeclarations, bool ForObjCContainer,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001533 SourceLocation &LAngleLoc, SourceLocation &EndLoc,
1534 bool consumeLastToken) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001535 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001536
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001537 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001538
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001539 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001540
Chris Lattner3bbae002008-07-26 04:03:38 +00001541 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001542 if (Tok.is(tok::code_completion)) {
Craig Topper883dd332015-12-24 23:58:11 +00001543 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001544 cutOffParsing();
1545 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001546 }
1547
Alex Lorenzf1278212017-04-11 15:01:53 +00001548 if (expectIdentifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001549 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001550 return true;
1551 }
1552 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1553 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001554 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001555 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001556
Alp Toker383d2c42014-01-01 03:08:43 +00001557 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001558 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001559 }
Mike Stump11289f42009-09-09 15:08:12 +00001560
Chris Lattner3bbae002008-07-26 04:03:38 +00001561 // Consume the '>'.
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001562 if (ParseGreaterThanInTemplateList(EndLoc, consumeLastToken,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001563 /*ObjCGenericList=*/false))
Chris Lattner3bbae002008-07-26 04:03:38 +00001564 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001565
Chris Lattner3bbae002008-07-26 04:03:38 +00001566 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001567 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Craig Toppera9247eb2015-10-22 04:59:56 +00001568 ProtocolIdents, Protocols);
Chris Lattner3bbae002008-07-26 04:03:38 +00001569 return false;
1570}
1571
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001572TypeResult Parser::parseObjCProtocolQualifierType(SourceLocation &rAngleLoc) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001573 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001574 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001575
1576 SourceLocation lAngleLoc;
1577 SmallVector<Decl *, 8> protocols;
1578 SmallVector<SourceLocation, 8> protocolLocs;
1579 (void)ParseObjCProtocolReferences(protocols, protocolLocs, false, false,
1580 lAngleLoc, rAngleLoc,
1581 /*consumeLastToken=*/true);
1582 TypeResult result = Actions.actOnObjCProtocolQualifierType(lAngleLoc,
1583 protocols,
1584 protocolLocs,
1585 rAngleLoc);
1586 if (result.isUsable()) {
1587 Diag(lAngleLoc, diag::warn_objc_protocol_qualifier_missing_id)
1588 << FixItHint::CreateInsertion(lAngleLoc, "id")
1589 << SourceRange(lAngleLoc, rAngleLoc);
1590 }
1591
1592 return result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001593}
1594
Douglas Gregore9d95f12015-07-07 03:57:35 +00001595/// Parse Objective-C type arguments or protocol qualifiers.
1596///
1597/// objc-type-arguments:
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001598/// '<' type-name '...'[opt] (',' type-name '...'[opt])* '>'
Douglas Gregore9d95f12015-07-07 03:57:35 +00001599///
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001600void Parser::parseObjCTypeArgsOrProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001601 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001602 SourceLocation &typeArgsLAngleLoc,
1603 SmallVectorImpl<ParsedType> &typeArgs,
1604 SourceLocation &typeArgsRAngleLoc,
1605 SourceLocation &protocolLAngleLoc,
1606 SmallVectorImpl<Decl *> &protocols,
1607 SmallVectorImpl<SourceLocation> &protocolLocs,
1608 SourceLocation &protocolRAngleLoc,
1609 bool consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001610 bool warnOnIncompleteProtocols) {
1611 assert(Tok.is(tok::less) && "Not at the start of type args or protocols");
1612 SourceLocation lAngleLoc = ConsumeToken();
1613
1614 // Whether all of the elements we've parsed thus far are single
1615 // identifiers, which might be types or might be protocols.
1616 bool allSingleIdentifiers = true;
1617 SmallVector<IdentifierInfo *, 4> identifiers;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001618 SmallVectorImpl<SourceLocation> &identifierLocs = protocolLocs;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001619
1620 // Parse a list of comma-separated identifiers, bailing out if we
1621 // see something different.
1622 do {
1623 // Parse a single identifier.
1624 if (Tok.is(tok::identifier) &&
1625 (NextToken().is(tok::comma) ||
1626 NextToken().is(tok::greater) ||
1627 NextToken().is(tok::greatergreater))) {
1628 identifiers.push_back(Tok.getIdentifierInfo());
1629 identifierLocs.push_back(ConsumeToken());
1630 continue;
1631 }
1632
1633 if (Tok.is(tok::code_completion)) {
1634 // FIXME: Also include types here.
1635 SmallVector<IdentifierLocPair, 4> identifierLocPairs;
1636 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1637 identifierLocPairs.push_back(IdentifierLocPair(identifiers[i],
1638 identifierLocs[i]));
1639 }
1640
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001641 QualType BaseT = Actions.GetTypeFromParser(baseType);
1642 if (!BaseT.isNull() && BaseT->acceptsObjCTypeParams()) {
1643 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
1644 } else {
Craig Topper883dd332015-12-24 23:58:11 +00001645 Actions.CodeCompleteObjCProtocolReferences(identifierLocPairs);
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001646 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001647 cutOffParsing();
1648 return;
1649 }
1650
1651 allSingleIdentifiers = false;
1652 break;
1653 } while (TryConsumeToken(tok::comma));
1654
1655 // If we parsed an identifier list, semantic analysis sorts out
1656 // whether it refers to protocols or to type arguments.
1657 if (allSingleIdentifiers) {
1658 // Parse the closing '>'.
1659 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001660 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001661 /*ObjCGenericList=*/true);
1662
1663 // Let Sema figure out what we parsed.
1664 Actions.actOnObjCTypeArgsOrProtocolQualifiers(getCurScope(),
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001665 baseType,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001666 lAngleLoc,
1667 identifiers,
1668 identifierLocs,
1669 rAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001670 typeArgsLAngleLoc,
1671 typeArgs,
1672 typeArgsRAngleLoc,
1673 protocolLAngleLoc,
1674 protocols,
1675 protocolRAngleLoc,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001676 warnOnIncompleteProtocols);
1677 return;
1678 }
1679
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001680 // We parsed an identifier list but stumbled into non single identifiers, this
1681 // means we might (a) check that what we already parsed is a legitimate type
1682 // (not a protocol or unknown type) and (b) parse the remaining ones, which
1683 // must all be type args.
Douglas Gregore9d95f12015-07-07 03:57:35 +00001684
1685 // Convert the identifiers into type arguments.
1686 bool invalid = false;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001687 IdentifierInfo *foundProtocolId = nullptr, *foundValidTypeId = nullptr;
1688 SourceLocation foundProtocolSrcLoc, foundValidTypeSrcLoc;
1689 SmallVector<IdentifierInfo *, 2> unknownTypeArgs;
1690 SmallVector<SourceLocation, 2> unknownTypeArgsLoc;
1691
Douglas Gregore9d95f12015-07-07 03:57:35 +00001692 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1693 ParsedType typeArg
1694 = Actions.getTypeName(*identifiers[i], identifierLocs[i], getCurScope());
1695 if (typeArg) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001696 DeclSpec DS(AttrFactory);
1697 const char *prevSpec = nullptr;
1698 unsigned diagID;
Faisal Vali090da2d2018-01-01 18:23:28 +00001699 DS.SetTypeSpecType(TST_typename, identifierLocs[i], prevSpec, diagID,
1700 typeArg, Actions.getASTContext().getPrintingPolicy());
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001701
1702 // Form a declarator to turn this into a type.
Faisal Vali421b2d12017-12-29 05:41:00 +00001703 Declarator D(DS, DeclaratorContext::TypeNameContext);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001704 TypeResult fullTypeArg = Actions.ActOnTypeName(getCurScope(), D);
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001705 if (fullTypeArg.isUsable()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001706 typeArgs.push_back(fullTypeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001707 if (!foundValidTypeId) {
1708 foundValidTypeId = identifiers[i];
1709 foundValidTypeSrcLoc = identifierLocs[i];
1710 }
1711 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001712 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001713 unknownTypeArgs.push_back(identifiers[i]);
1714 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1715 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001716 } else {
1717 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001718 if (!Actions.LookupProtocol(identifiers[i], identifierLocs[i])) {
1719 unknownTypeArgs.push_back(identifiers[i]);
1720 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1721 } else if (!foundProtocolId) {
1722 foundProtocolId = identifiers[i];
1723 foundProtocolSrcLoc = identifierLocs[i];
1724 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001725 }
1726 }
1727
1728 // Continue parsing type-names.
1729 do {
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001730 Token CurTypeTok = Tok;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001731 TypeResult typeArg = ParseTypeName();
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001732
1733 // Consume the '...' for a pack expansion.
1734 SourceLocation ellipsisLoc;
1735 TryConsumeToken(tok::ellipsis, ellipsisLoc);
1736 if (typeArg.isUsable() && ellipsisLoc.isValid()) {
1737 typeArg = Actions.ActOnPackExpansion(typeArg.get(), ellipsisLoc);
1738 }
1739
Douglas Gregore9d95f12015-07-07 03:57:35 +00001740 if (typeArg.isUsable()) {
1741 typeArgs.push_back(typeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001742 if (!foundValidTypeId) {
1743 foundValidTypeId = CurTypeTok.getIdentifierInfo();
1744 foundValidTypeSrcLoc = CurTypeTok.getLocation();
1745 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001746 } else {
1747 invalid = true;
1748 }
1749 } while (TryConsumeToken(tok::comma));
1750
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001751 // Diagnose the mix between type args and protocols.
1752 if (foundProtocolId && foundValidTypeId)
1753 Actions.DiagnoseTypeArgsAndProtocols(foundProtocolId, foundProtocolSrcLoc,
1754 foundValidTypeId,
1755 foundValidTypeSrcLoc);
1756
1757 // Diagnose unknown arg types.
1758 ParsedType T;
1759 if (unknownTypeArgs.size())
1760 for (unsigned i = 0, e = unknownTypeArgsLoc.size(); i < e; ++i)
1761 Actions.DiagnoseUnknownTypeName(unknownTypeArgs[i], unknownTypeArgsLoc[i],
1762 getCurScope(), nullptr, T);
1763
Douglas Gregore9d95f12015-07-07 03:57:35 +00001764 // Parse the closing '>'.
1765 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001766 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001767 /*ObjCGenericList=*/true);
1768
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001769 if (invalid) {
1770 typeArgs.clear();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001771 return;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001772 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001773
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001774 // Record left/right angle locations.
1775 typeArgsLAngleLoc = lAngleLoc;
1776 typeArgsRAngleLoc = rAngleLoc;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001777}
1778
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001779void Parser::parseObjCTypeArgsAndProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001780 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001781 SourceLocation &typeArgsLAngleLoc,
1782 SmallVectorImpl<ParsedType> &typeArgs,
1783 SourceLocation &typeArgsRAngleLoc,
1784 SourceLocation &protocolLAngleLoc,
1785 SmallVectorImpl<Decl *> &protocols,
1786 SmallVectorImpl<SourceLocation> &protocolLocs,
1787 SourceLocation &protocolRAngleLoc,
1788 bool consumeLastToken) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001789 assert(Tok.is(tok::less));
1790
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001791 // Parse the first angle-bracket-delimited clause.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001792 parseObjCTypeArgsOrProtocolQualifiers(baseType,
1793 typeArgsLAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001794 typeArgs,
1795 typeArgsRAngleLoc,
1796 protocolLAngleLoc,
1797 protocols,
1798 protocolLocs,
1799 protocolRAngleLoc,
1800 consumeLastToken,
Douglas Gregore83b9562015-07-07 03:57:53 +00001801 /*warnOnIncompleteProtocols=*/false);
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00001802 if (Tok.is(tok::eof)) // Nothing else to do here...
1803 return;
Douglas Gregore83b9562015-07-07 03:57:53 +00001804
1805 // An Objective-C object pointer followed by type arguments
1806 // can then be followed again by a set of protocol references, e.g.,
1807 // \c NSArray<NSView><NSTextDelegate>
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001808 if ((consumeLastToken && Tok.is(tok::less)) ||
1809 (!consumeLastToken && NextToken().is(tok::less))) {
1810 // If we aren't consuming the last token, the prior '>' is still hanging
1811 // there. Consume it before we parse the protocol qualifiers.
1812 if (!consumeLastToken)
1813 ConsumeToken();
1814
1815 if (!protocols.empty()) {
1816 SkipUntilFlags skipFlags = SkipUntilFlags();
1817 if (!consumeLastToken)
1818 skipFlags = skipFlags | StopBeforeMatch;
Douglas Gregore83b9562015-07-07 03:57:53 +00001819 Diag(Tok, diag::err_objc_type_args_after_protocols)
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001820 << SourceRange(protocolLAngleLoc, protocolRAngleLoc);
1821 SkipUntil(tok::greater, tok::greatergreater, skipFlags);
Douglas Gregore83b9562015-07-07 03:57:53 +00001822 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001823 ParseObjCProtocolReferences(protocols, protocolLocs,
1824 /*WarnOnDeclarations=*/false,
1825 /*ForObjCContainer=*/false,
1826 protocolLAngleLoc, protocolRAngleLoc,
1827 consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001828 }
1829 }
1830}
1831
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001832TypeResult Parser::parseObjCTypeArgsAndProtocolQualifiers(
1833 SourceLocation loc,
1834 ParsedType type,
1835 bool consumeLastToken,
1836 SourceLocation &endLoc) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001837 assert(Tok.is(tok::less));
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001838 SourceLocation typeArgsLAngleLoc;
1839 SmallVector<ParsedType, 4> typeArgs;
1840 SourceLocation typeArgsRAngleLoc;
1841 SourceLocation protocolLAngleLoc;
1842 SmallVector<Decl *, 4> protocols;
1843 SmallVector<SourceLocation, 4> protocolLocs;
1844 SourceLocation protocolRAngleLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00001845
1846 // Parse type arguments and protocol qualifiers.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001847 parseObjCTypeArgsAndProtocolQualifiers(type, typeArgsLAngleLoc, typeArgs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001848 typeArgsRAngleLoc, protocolLAngleLoc,
1849 protocols, protocolLocs,
1850 protocolRAngleLoc, consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001851
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00001852 if (Tok.is(tok::eof))
1853 return true; // Invalid type result.
1854
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001855 // Compute the location of the last token.
1856 if (consumeLastToken)
1857 endLoc = PrevTokLocation;
1858 else
1859 endLoc = Tok.getLocation();
1860
1861 return Actions.actOnObjCTypeArgsAndProtocolQualifiers(
1862 getCurScope(),
1863 loc,
1864 type,
1865 typeArgsLAngleLoc,
1866 typeArgs,
1867 typeArgsRAngleLoc,
1868 protocolLAngleLoc,
1869 protocols,
1870 protocolLocs,
1871 protocolRAngleLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00001872}
1873
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001874void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1875 BalancedDelimiterTracker &T,
1876 SmallVectorImpl<Decl *> &AllIvarDecls,
1877 bool RBraceMissing) {
1878 if (!RBraceMissing)
1879 T.consumeClose();
1880
1881 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1882 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1883 Actions.ActOnObjCContainerFinishDefinition();
1884 // Call ActOnFields() even if we don't have any decls. This is useful
1885 // for code rewriting tools that need to be aware of the empty list.
1886 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1887 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001888 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001889}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001890
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001891/// objc-class-instance-variables:
1892/// '{' objc-instance-variable-decl-list[opt] '}'
1893///
1894/// objc-instance-variable-decl-list:
1895/// objc-visibility-spec
1896/// objc-instance-variable-decl ';'
1897/// ';'
1898/// objc-instance-variable-decl-list objc-visibility-spec
1899/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1900/// objc-instance-variable-decl-list ';'
1901///
1902/// objc-visibility-spec:
1903/// @private
1904/// @protected
1905/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001906/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001907///
1908/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001909/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001910///
John McCall48871652010-08-21 09:40:31 +00001911void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001912 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001913 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001914 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001915 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001916
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001917 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001918 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001919
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001920 BalancedDelimiterTracker T(*this, tok::l_brace);
1921 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001922 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001923 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001924 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001925
Steve Naroff00433d32007-08-21 21:17:12 +00001926 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001927 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001928 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001929 continue;
1930 }
Mike Stump11289f42009-09-09 15:08:12 +00001931
Steve Naroff00433d32007-08-21 21:17:12 +00001932 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001933 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001934 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001935 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001936 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001937 }
1938
Steve Naroff7c348172007-08-23 18:16:40 +00001939 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001940 case tok::objc_private:
1941 case tok::objc_public:
1942 case tok::objc_protected:
1943 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001944 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001945 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001946 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001947
1948 case tok::objc_end:
1949 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001950 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1951 Tok.setKind(tok::at);
1952 Tok.setLength(1);
1953 PP.EnterToken(Tok);
1954 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1955 T, AllIvarDecls, true);
1956 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001957
1958 default:
1959 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1960 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001961 }
1962 }
Mike Stump11289f42009-09-09 15:08:12 +00001963
Douglas Gregor48d46252010-01-13 21:54:15 +00001964 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001965 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001966 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001967 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001968 }
John McCallcfefb6d2009-11-03 02:38:08 +00001969
Benjamin Kramera39beb92014-09-03 11:06:10 +00001970 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1971 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1972 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001973 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001974 Decl *Field = Actions.ActOnIvar(
1975 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1976 FD.BitfieldSize, visibility);
1977 Actions.ActOnObjCContainerFinishDefinition();
1978 if (Field)
1979 AllIvarDecls.push_back(Field);
1980 FD.complete(Field);
1981 };
John McCallcfefb6d2009-11-03 02:38:08 +00001982
Chris Lattnera12405b2008-04-10 06:46:29 +00001983 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001984 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001985 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001986
Chris Lattner0ef13522007-10-09 17:51:17 +00001987 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001988 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001989 } else {
1990 Diag(Tok, diag::err_expected_semi_decl_list);
1991 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001992 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001993 }
1994 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001995 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1996 T, AllIvarDecls, false);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001997}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001998
1999/// objc-protocol-declaration:
2000/// objc-protocol-definition
2001/// objc-protocol-forward-reference
2002///
2003/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00002004/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00002005/// objc-protocol-refs[opt]
2006/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00002007/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002008///
2009/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00002010/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002011///
James Dennett1355bd12012-06-11 06:19:40 +00002012/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00002013/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002014/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00002015Parser::DeclGroupPtrTy
2016Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
2017 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00002018 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002019 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
2020 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002021
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002022 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002023 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002024 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002025 return nullptr;
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002026 }
2027
Nico Weber69a79142013-04-04 00:15:10 +00002028 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00002029
Alex Lorenzf1278212017-04-11 15:01:53 +00002030 if (expectIdentifier())
2031 return nullptr; // missing protocol name.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002032 // Save the protocol name, then consume it.
2033 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
2034 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002035
Alp Toker383d2c42014-01-01 03:08:43 +00002036 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00002037 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Craig Topper0f723bb2015-10-22 05:00:01 +00002038 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtoInfo,
John McCall53fa7142010-12-24 02:08:15 +00002039 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002040 }
Mike Stump11289f42009-09-09 15:08:12 +00002041
Erik Verbruggenf9887852011-12-08 09:58:43 +00002042 CheckNestedObjCContexts(AtLoc);
2043
Chris Lattner0ef13522007-10-09 17:51:17 +00002044 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002045 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002046 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
2047
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002048 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002049 while (1) {
2050 ConsumeToken(); // the ','
Alex Lorenzf1278212017-04-11 15:01:53 +00002051 if (expectIdentifier()) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002052 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002053 return nullptr;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002054 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00002055 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
2056 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002057 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00002058
Chris Lattner0ef13522007-10-09 17:51:17 +00002059 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002060 break;
2061 }
2062 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00002063 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
David Blaikie0403cb12016-01-15 23:43:25 +00002064 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002065
Craig Topper0f723bb2015-10-22 05:00:01 +00002066 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtocolRefs,
John McCall53fa7142010-12-24 02:08:15 +00002067 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00002068 }
Mike Stump11289f42009-09-09 15:08:12 +00002069
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002070 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00002071 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002072
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002073 SmallVector<Decl *, 8> ProtocolRefs;
2074 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002075 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00002076 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002077 LAngleLoc, EndProtoLoc,
2078 /*consumeLastToken=*/true))
David Blaikie0403cb12016-01-15 23:43:25 +00002079 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002080
John McCall48871652010-08-21 09:40:31 +00002081 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00002082 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002083 ProtocolRefs.data(),
2084 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00002085 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00002086 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002087
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00002088 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00002089 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002090}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002091
2092/// objc-implementation:
2093/// objc-class-implementation-prologue
2094/// objc-category-implementation-prologue
2095///
2096/// objc-class-implementation-prologue:
2097/// @implementation identifier objc-superclass[opt]
2098/// objc-class-instance-variables[opt]
2099///
2100/// objc-category-implementation-prologue:
2101/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002102Parser::DeclGroupPtrTy
2103Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002104 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
2105 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002106 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002107 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002108
Douglas Gregor49c22a72009-11-18 16:26:39 +00002109 // Code completion after '@implementation'.
2110 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002111 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002112 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002113 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +00002114 }
2115
Nico Weber69a79142013-04-04 00:15:10 +00002116 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00002117
Alex Lorenzf1278212017-04-11 15:01:53 +00002118 if (expectIdentifier())
2119 return nullptr; // missing class or category name.
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002120 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00002121 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002122 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00002123 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002124
Douglas Gregor85f3f952015-07-07 03:57:15 +00002125 // Neither a type parameter list nor a list of protocol references is
2126 // permitted here. Parse and diagnose them.
2127 if (Tok.is(tok::less)) {
2128 SourceLocation lAngleLoc, rAngleLoc;
2129 SmallVector<IdentifierLocPair, 8> protocolIdents;
2130 SourceLocation diagLoc = Tok.getLocation();
Richard Smith3df3f1d2015-11-03 01:19:56 +00002131 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
2132 if (parseObjCTypeParamListOrProtocolRefs(typeParamScope, lAngleLoc,
2133 protocolIdents, rAngleLoc)) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00002134 Diag(diagLoc, diag::err_objc_parameterized_implementation)
2135 << SourceRange(diagLoc, PrevTokLocation);
2136 } else if (lAngleLoc.isValid()) {
2137 Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
2138 << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
2139 }
2140 }
2141
Mike Stump11289f42009-09-09 15:08:12 +00002142 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002143 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002144 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002145 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002146 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002147
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002148 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002149 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002150 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002151 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002152 }
2153
Chris Lattner0ef13522007-10-09 17:51:17 +00002154 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002155 categoryId = Tok.getIdentifierInfo();
2156 categoryLoc = ConsumeToken();
2157 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002158 Diag(Tok, diag::err_expected)
2159 << tok::identifier; // missing category name.
David Blaikie0403cb12016-01-15 23:43:25 +00002160 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002161 }
Chris Lattner0ef13522007-10-09 17:51:17 +00002162 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002163 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002164 SkipUntil(tok::r_paren); // don't stop at ';'
David Blaikie0403cb12016-01-15 23:43:25 +00002165 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002166 }
2167 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00002168 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2169 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002170 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2171 SmallVector<Decl *, 4> protocols;
2172 SmallVector<SourceLocation, 4> protocolLocs;
2173 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2174 /*warnOnIncompleteProtocols=*/false,
2175 /*ForObjCContainer=*/false,
2176 protocolLAngleLoc, protocolRAngleLoc,
2177 /*consumeLastToken=*/true);
Fariborz Jahanian85888552013-05-17 17:58:11 +00002178 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002179 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002180 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00002181 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002182
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002183 } else {
2184 // We have a class implementation
2185 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002186 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00002187 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002188 // We have a super class
Alex Lorenzf1278212017-04-11 15:01:53 +00002189 if (expectIdentifier())
2190 return nullptr; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002191 superClassId = Tok.getIdentifierInfo();
2192 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002193 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002194 ObjCImpDecl = Actions.ActOnStartClassImplementation(
2195 AtLoc, nameId, nameLoc,
2196 superClassId, superClassLoc);
2197
2198 if (Tok.is(tok::l_brace)) // we have ivars
2199 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002200 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2201 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002202
2203 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2204 SmallVector<Decl *, 4> protocols;
2205 SmallVector<SourceLocation, 4> protocolLocs;
2206 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2207 /*warnOnIncompleteProtocols=*/false,
2208 /*ForObjCContainer=*/false,
2209 protocolLAngleLoc, protocolRAngleLoc,
2210 /*consumeLastToken=*/true);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002211 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002212 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002213 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002214
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002215 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002216
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002217 {
2218 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00002219 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002220 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002221 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002222 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
2223 DeclGroupRef DG = DGP.get();
2224 DeclsInGroup.append(DG.begin(), DG.end());
2225 }
2226 }
2227 }
2228
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00002229 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002230}
Steve Naroff33a1e802007-10-29 21:38:07 +00002231
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002232Parser::DeclGroupPtrTy
2233Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002234 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
2235 "ParseObjCAtEndDeclaration(): Expected @end");
2236 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002237 if (CurParsedObjCImpl)
2238 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002239 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00002240 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002241 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
David Blaikie0403cb12016-01-15 23:43:25 +00002242 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002243}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002244
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002245Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
2246 if (!Finished) {
2247 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00002248 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002249 P.Diag(P.Tok, diag::err_objc_missing_end)
2250 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
2251 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
2252 << Sema::OCK_Implementation;
2253 }
2254 }
Craig Topper161e4db2014-05-21 06:02:52 +00002255 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002256 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002257}
2258
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002259void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
2260 assert(!Finished);
Alex Lorenz6c9af502017-07-03 10:12:24 +00002261 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl, AtEnd.getBegin());
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002262 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002263 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2264 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002265
2266 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
2267
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002268 if (HasCFunction)
2269 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
2270 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2271 false/*c-functions*/);
2272
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002273 /// Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002274 for (LateParsedObjCMethodContainer::iterator
2275 I = LateParsedObjCMethods.begin(),
2276 E = LateParsedObjCMethods.end(); I != E; ++I)
2277 delete *I;
2278 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002279
2280 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002281}
2282
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002283/// compatibility-alias-decl:
2284/// @compatibility_alias alias-name class-name ';'
2285///
John McCall48871652010-08-21 09:40:31 +00002286Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002287 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
2288 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
2289 ConsumeToken(); // consume compatibility_alias
Alex Lorenzf1278212017-04-11 15:01:53 +00002290 if (expectIdentifier())
Craig Topper161e4db2014-05-21 06:02:52 +00002291 return nullptr;
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002292 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
2293 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Alex Lorenzf1278212017-04-11 15:01:53 +00002294 if (expectIdentifier())
Craig Topper161e4db2014-05-21 06:02:52 +00002295 return nullptr;
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002296 IdentifierInfo *classId = Tok.getIdentifierInfo();
2297 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00002298 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00002299 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
2300 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002301}
2302
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002303/// property-synthesis:
2304/// @synthesize property-ivar-list ';'
2305///
2306/// property-ivar-list:
2307/// property-ivar
2308/// property-ivar-list ',' property-ivar
2309///
2310/// property-ivar:
2311/// identifier
2312/// identifier '=' identifier
2313///
John McCall48871652010-08-21 09:40:31 +00002314Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002315 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00002316 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002317 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00002318
Douglas Gregor88e72a02009-11-18 19:45:45 +00002319 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00002320 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002321 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002322 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002323 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002324 }
2325
Douglas Gregor88e72a02009-11-18 19:45:45 +00002326 if (Tok.isNot(tok::identifier)) {
2327 Diag(Tok, diag::err_synthesized_property_name);
2328 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002329 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00002330 }
Craig Topper161e4db2014-05-21 06:02:52 +00002331
2332 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002333 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2334 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002335 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00002336 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002337 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00002338 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002339 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002340 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002341 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002342 }
Alex Lorenzf1278212017-04-11 15:01:53 +00002343
2344 if (expectIdentifier())
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002345 break;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002346 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002347 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002348 }
Manman Ren5b786402016-01-28 18:49:28 +00002349 Actions.ActOnPropertyImplDecl(
2350 getCurScope(), atLoc, propertyLoc, true,
2351 propertyId, propertyIvar, propertyIvarLoc,
2352 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Chris Lattner0ef13522007-10-09 17:51:17 +00002353 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002354 break;
2355 ConsumeToken(); // consume ','
2356 }
Alp Toker383d2c42014-01-01 03:08:43 +00002357 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00002358 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002359}
2360
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002361/// property-dynamic:
2362/// @dynamic property-list
2363///
2364/// property-list:
2365/// identifier
2366/// property-list ',' identifier
2367///
John McCall48871652010-08-21 09:40:31 +00002368Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002369 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2370 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002371 ConsumeToken(); // consume dynamic
Manman Ren0fe61f82016-01-29 19:05:57 +00002372
2373 bool isClassProperty = false;
2374 if (Tok.is(tok::l_paren)) {
2375 ConsumeParen();
2376 const IdentifierInfo *II = Tok.getIdentifierInfo();
2377
2378 if (!II) {
2379 Diag(Tok, diag::err_objc_expected_property_attr) << II;
2380 SkipUntil(tok::r_paren, StopAtSemi);
2381 } else {
2382 SourceLocation AttrName = ConsumeToken(); // consume attribute name
2383 if (II->isStr("class")) {
2384 isClassProperty = true;
2385 if (Tok.isNot(tok::r_paren)) {
2386 Diag(Tok, diag::err_expected) << tok::r_paren;
2387 SkipUntil(tok::r_paren, StopAtSemi);
2388 } else
2389 ConsumeParen();
2390 } else {
2391 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
2392 SkipUntil(tok::r_paren, StopAtSemi);
2393 }
2394 }
2395 }
2396
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002397 while (true) {
2398 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002399 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002400 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002401 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002402 }
Alex Lorenzf1278212017-04-11 15:01:53 +00002403
2404 if (expectIdentifier()) {
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002405 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002406 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002407 }
2408
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002409 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2410 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Manman Ren5b786402016-01-28 18:49:28 +00002411 Actions.ActOnPropertyImplDecl(
2412 getCurScope(), atLoc, propertyLoc, false,
2413 propertyId, nullptr, SourceLocation(),
Manman Ren0fe61f82016-01-29 19:05:57 +00002414 isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
Manman Ren5b786402016-01-28 18:49:28 +00002415 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002416
Chris Lattner0ef13522007-10-09 17:51:17 +00002417 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002418 break;
2419 ConsumeToken(); // consume ','
2420 }
Alp Toker383d2c42014-01-01 03:08:43 +00002421 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00002422 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002423}
Mike Stump11289f42009-09-09 15:08:12 +00002424
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002425/// objc-throw-statement:
2426/// throw expression[opt];
2427///
John McCalldadc5752010-08-24 06:29:42 +00002428StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2429 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002430 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00002431 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00002432 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002433 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002434 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002435 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002436 }
2437 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00002438 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00002439 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002440 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002441}
2442
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002443/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002444/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002445///
John McCalldadc5752010-08-24 06:29:42 +00002446StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002447Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002448 ConsumeToken(); // consume synchronized
2449 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002450 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002451 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002452 }
John McCalld9bb7432011-07-27 21:50:02 +00002453
2454 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002455 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00002456 ExprResult operand(ParseExpression());
2457
2458 if (Tok.is(tok::r_paren)) {
2459 ConsumeParen(); // ')'
2460 } else {
2461 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002462 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00002463
2464 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002465 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002466 }
John McCalld9bb7432011-07-27 21:50:02 +00002467
2468 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002469 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00002470 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002471 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002472 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002473 }
Steve Naroffd9c26072008-06-04 20:36:13 +00002474
John McCalld9bb7432011-07-27 21:50:02 +00002475 // Check the @synchronized operand now.
2476 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002477 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002478
John McCalld9bb7432011-07-27 21:50:02 +00002479 // Parse the compound statement within a new scope.
Momchil Velikov57c681f2017-08-10 15:43:06 +00002480 ParseScope bodyScope(this, Scope::DeclScope | Scope::CompoundStmtScope);
John McCalld9bb7432011-07-27 21:50:02 +00002481 StmtResult body(ParseCompoundStatementBody());
2482 bodyScope.Exit();
2483
2484 // If there was a semantic or parse error earlier with the
2485 // operand, fail now.
2486 if (operand.isInvalid())
2487 return StmtError();
2488
2489 if (body.isInvalid())
2490 body = Actions.ActOnNullStmt(Tok.getLocation());
2491
2492 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002493}
2494
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002495/// objc-try-catch-statement:
2496/// @try compound-statement objc-catch-list[opt]
2497/// @try compound-statement objc-catch-list[opt] @finally compound-statement
2498///
2499/// objc-catch-list:
2500/// @catch ( parameter-declaration ) compound-statement
2501/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2502/// catch-parameter-declaration:
2503/// parameter-declaration
2504/// '...' [OBJC2]
2505///
John McCalldadc5752010-08-24 06:29:42 +00002506StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002507 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002508
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002509 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00002510 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002511 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002512 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002513 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00002514 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00002515 StmtResult FinallyStmt;
Momchil Velikov57c681f2017-08-10 15:43:06 +00002516 ParseScope TryScope(this, Scope::DeclScope | Scope::CompoundStmtScope);
John McCalldadc5752010-08-24 06:29:42 +00002517 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002518 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002519 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002520 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002521
Chris Lattner0ef13522007-10-09 17:51:17 +00002522 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002523 // At this point, we need to lookahead to determine if this @ is the start
2524 // of an @catch or @finally. We don't want to consume the @ token if this
2525 // is an @try or @encode or something else.
2526 Token AfterAt = GetLookAheadToken(1);
2527 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2528 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2529 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002530
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002531 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002532 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002533 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002534 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002535 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002536 ConsumeParen();
Momchil Velikov57c681f2017-08-10 15:43:06 +00002537 ParseScope CatchScope(this, Scope::DeclScope |
2538 Scope::CompoundStmtScope |
2539 Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002540 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002541 DeclSpec DS(AttrFactory);
Faisal Valia534f072018-04-26 00:42:40 +00002542 ParseDeclarationSpecifiers(DS);
Faisal Vali421b2d12017-12-29 05:41:00 +00002543 Declarator ParmDecl(DS, DeclaratorContext::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002544 ParseDeclarator(ParmDecl);
2545
Douglas Gregore11ee112010-04-23 23:01:43 +00002546 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002547 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002548 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002549 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002550 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002551
Steve Naroff65a00892009-04-07 22:56:58 +00002552 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002553
Steve Naroff65a00892009-04-07 22:56:58 +00002554 if (Tok.is(tok::r_paren))
2555 RParenLoc = ConsumeParen();
2556 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002557 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002558
John McCalldadc5752010-08-24 06:29:42 +00002559 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002560 if (Tok.is(tok::l_brace))
2561 CatchBody = ParseCompoundStatementBody();
2562 else
Alp Tokerec543272013-12-24 09:48:30 +00002563 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002564 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002565 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00002566
John McCalldadc5752010-08-24 06:29:42 +00002567 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00002568 RParenLoc,
2569 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002570 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002571 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002572 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002573
Steve Naroffe6016792008-02-05 21:27:35 +00002574 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002575 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2576 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002577 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002578 }
2579 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002580 } else {
2581 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002582 ConsumeToken(); // consume finally
Momchil Velikov57c681f2017-08-10 15:43:06 +00002583 ParseScope FinallyScope(this,
2584 Scope::DeclScope | Scope::CompoundStmtScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002585
Shoaib Meenai5faf6d82018-06-08 00:30:00 +00002586 bool ShouldCapture =
2587 getTargetInfo().getTriple().isWindowsMSVCEnvironment();
2588 if (ShouldCapture)
2589 Actions.ActOnCapturedRegionStart(Tok.getLocation(), getCurScope(),
2590 CR_ObjCAtFinally, 1);
2591
John McCalldadc5752010-08-24 06:29:42 +00002592 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002593 if (Tok.is(tok::l_brace))
2594 FinallyBody = ParseCompoundStatementBody();
2595 else
Alp Tokerec543272013-12-24 09:48:30 +00002596 Diag(Tok, diag::err_expected) << tok::l_brace;
Shoaib Meenai5faf6d82018-06-08 00:30:00 +00002597
2598 if (FinallyBody.isInvalid()) {
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002599 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Shoaib Meenai5faf6d82018-06-08 00:30:00 +00002600 if (ShouldCapture)
2601 Actions.ActOnCapturedRegionError();
2602 } else if (ShouldCapture) {
2603 FinallyBody = Actions.ActOnCapturedRegionEnd(FinallyBody.get());
2604 }
2605
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002606 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002607 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002608 catch_or_finally_seen = true;
2609 break;
2610 }
2611 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002612 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002613 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002614 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002615 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002616
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002617 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002618 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002619 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002620}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002621
John McCall31168b02011-06-15 23:02:42 +00002622/// objc-autoreleasepool-statement:
2623/// @autoreleasepool compound-statement
2624///
2625StmtResult
2626Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2627 ConsumeToken(); // consume autoreleasepool
2628 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002629 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002630 return StmtError();
2631 }
2632 // Enter a scope to hold everything within the compound stmt. Compound
2633 // statements can always hold declarations.
Momchil Velikov57c681f2017-08-10 15:43:06 +00002634 ParseScope BodyScope(this, Scope::DeclScope | Scope::CompoundStmtScope);
John McCall31168b02011-06-15 23:02:42 +00002635
2636 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2637
2638 BodyScope.Exit();
2639 if (AutoreleasePoolBody.isInvalid())
2640 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2641 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002642 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002643}
2644
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002645/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2646/// for later parsing.
2647void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002648 if (SkipFunctionBodies && (!MDecl || Actions.canSkipFunctionBody(MDecl)) &&
2649 trySkippingFunctionBody()) {
2650 Actions.ActOnSkippedFunctionBody(MDecl);
2651 return;
2652 }
2653
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002654 LexedMethod* LM = new LexedMethod(this, MDecl);
2655 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2656 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002657 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002658 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002659 if (Tok.is(tok::kw_try)) {
2660 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002661 if (Tok.is(tok::colon)) {
2662 Toks.push_back(Tok);
2663 ConsumeToken();
2664 while (Tok.isNot(tok::l_brace)) {
2665 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2666 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2667 }
2668 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002669 Toks.push_back(Tok); // also store '{'
2670 }
2671 else if (Tok.is(tok::colon)) {
2672 ConsumeToken();
Richard Smithb9fa9962015-08-21 03:04:33 +00002673 // FIXME: This is wrong, due to C++11 braced initialization.
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002674 while (Tok.isNot(tok::l_brace)) {
2675 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2676 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2677 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002678 Toks.push_back(Tok); // also store '{'
2679 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002680 ConsumeBrace();
2681 // Consume everything up to (and including) the matching right brace.
2682 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002683 while (Tok.is(tok::kw_catch)) {
2684 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2685 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2686 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002687}
2688
Steve Naroff09bf8152007-09-06 21:24:23 +00002689/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002690///
John McCall48871652010-08-21 09:40:31 +00002691Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002692 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002693
Jordan Rose1e879d82018-03-23 00:07:18 +00002694 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, MDecl, Tok.getLocation(),
John McCallfaf5fb42010-08-26 23:41:50 +00002695 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002696
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002697 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002698 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002699 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002700 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002701 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002702 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002703 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002704 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002705
Steve Naroffbb875722007-11-11 19:54:21 +00002706 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002707 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002708 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002709
Steve Naroffbb875722007-11-11 19:54:21 +00002710 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002711 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002712
Steve Naroffbb875722007-11-11 19:54:21 +00002713 // If we didn't find the '{', bail out.
2714 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002715 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002716 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002717
2718 if (!MDecl) {
2719 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002720 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002721 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002722 }
2723
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002724 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002725 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002726 assert (CurParsedObjCImpl
2727 && "ParseObjCMethodDefinition - Method out of @implementation");
2728 // Consume the tokens and store them for later parsing.
2729 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002730 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002731}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002732
John McCalldadc5752010-08-24 06:29:42 +00002733StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002734 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002735 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002736 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002737 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002738 }
2739
2740 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002741 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002742
2743 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002744 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002745
2746 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002747 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002748
2749 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2750 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002751
2752 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2753 getLangOpts().DebuggerSupport) {
2754 SkipUntil(tok::semi);
2755 return Actions.ActOnNullStmt(Tok.getLocation());
2756 }
2757
Alex Lorenza589abc2016-12-01 12:14:38 +00002758 ExprStatementTokLoc = AtLoc;
John McCalldadc5752010-08-24 06:29:42 +00002759 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002760 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002761 // If the expression is invalid, skip ahead to the next semicolon. Not
2762 // doing this opens us up to the possibility of infinite loops if
2763 // ParseExpression does not consume any tokens.
2764 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002765 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002766 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002767
Steve Naroffe6016792008-02-05 21:27:35 +00002768 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002769 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002770 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002771}
2772
John McCalldadc5752010-08-24 06:29:42 +00002773ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002774 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002775 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002776 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002777 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002778 return ExprError();
2779
Ted Kremeneke65b0862012-03-06 20:05:56 +00002780 case tok::minus:
2781 case tok::plus: {
2782 tok::TokenKind Kind = Tok.getKind();
2783 SourceLocation OpLoc = ConsumeToken();
2784
2785 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002786 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002787 switch (Kind) {
2788 case tok::minus: Symbol = "-"; break;
2789 case tok::plus: Symbol = "+"; break;
2790 default: llvm_unreachable("missing unary operator case");
2791 }
2792 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2793 << Symbol;
2794 return ExprError();
2795 }
2796
2797 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2798 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002799 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002800 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002801 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002802
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002803 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002804 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002805 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002806
2807 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002808 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002809 }
2810
Chris Lattnere002fbe2007-12-12 01:04:12 +00002811 case tok::string_literal: // primary-expression: string-literal
2812 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002813 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002814
2815 case tok::char_constant:
2816 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2817
2818 case tok::numeric_constant:
2819 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2820
2821 case tok::kw_true: // Objective-C++, etc.
2822 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2823 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2824 case tok::kw_false: // Objective-C++, etc.
2825 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2826 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2827
2828 case tok::l_square:
2829 // Objective-C array literal
2830 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2831
2832 case tok::l_brace:
2833 // Objective-C dictionary literal
2834 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2835
Patrick Beard0caa3942012-04-19 00:25:12 +00002836 case tok::l_paren:
2837 // Objective-C boxed expression
2838 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2839
Chris Lattnere002fbe2007-12-12 01:04:12 +00002840 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002841 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002842 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002843
Chris Lattner197a3012008-08-05 06:19:09 +00002844 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2845 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002846 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002847 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002848 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002849 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002850 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Erik Pilkington29099de2016-07-16 00:35:23 +00002851 case tok::objc_available:
2852 return ParseAvailabilityCheckExpr(AtLoc);
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002853 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002854 const char *str = nullptr;
Alex Lorenza589abc2016-12-01 12:14:38 +00002855 // Only provide the @try/@finally/@autoreleasepool fixit when we're sure
2856 // that this is a proper statement where such directives could actually
2857 // occur.
2858 if (GetLookAheadToken(1).is(tok::l_brace) &&
2859 ExprStatementTokLoc == AtLoc) {
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002860 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2861 str =
2862 ch == 't' ? "try"
2863 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002864 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002865 }
2866 if (str) {
2867 SourceLocation kwLoc = Tok.getLocation();
2868 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2869 FixItHint::CreateReplacement(kwLoc, str));
2870 }
2871 else
2872 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2873 }
Chris Lattner197a3012008-08-05 06:19:09 +00002874 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002875 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002876}
2877
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002878/// Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002879///
2880/// This routine parses the receiver of a message send in
2881/// Objective-C++ either as a type or as an expression. Note that this
2882/// routine must not be called to parse a send to 'super', since it
2883/// has no way to return such a result.
2884///
2885/// \param IsExpr Whether the receiver was parsed as an expression.
2886///
2887/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2888/// IsExpr is true), the parsed expression. If the receiver was parsed
2889/// as a type (\c IsExpr is false), the parsed type.
2890///
2891/// \returns True if an error occurred during parsing or semantic
2892/// analysis, in which case the arguments do not have valid
2893/// values. Otherwise, returns false for a successful parse.
2894///
2895/// objc-receiver: [C++]
2896/// 'super' [not parsed here]
2897/// expression
2898/// simple-type-specifier
2899/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002900bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002901 InMessageExpressionRAIIObject InMessage(*this, true);
2902
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002903 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2904 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002905 TryAnnotateTypeOrScopeToken();
2906
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002907 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002908 // objc-receiver:
2909 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002910 // Make sure any typos in the receiver are corrected or diagnosed, so that
2911 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2912 // only the things that are valid ObjC receivers?
2913 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002914 if (Receiver.isInvalid())
2915 return true;
2916
2917 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002918 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002919 return false;
2920 }
2921
2922 // objc-receiver:
2923 // typename-specifier
2924 // simple-type-specifier
2925 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002926 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002927 ParseCXXSimpleTypeSpecifier(DS);
2928
2929 if (Tok.is(tok::l_paren)) {
2930 // If we see an opening parentheses at this point, we are
2931 // actually parsing an expression that starts with a
2932 // function-style cast, e.g.,
2933 //
2934 // postfix-expression:
2935 // simple-type-specifier ( expression-list [opt] )
2936 // typename-specifier ( expression-list [opt] )
2937 //
2938 // Parse the remainder of this case, then the (optional)
2939 // postfix-expression suffix, followed by the (optional)
2940 // right-hand side of the binary expression. We have an
2941 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002942 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002943 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002944 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002945 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002946 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002947 if (Receiver.isInvalid())
2948 return true;
2949
2950 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002951 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002952 return false;
2953 }
2954
2955 // We have a class message. Turn the simple-type-specifier or
2956 // typename-specifier we parsed into a type and parse the
2957 // remainder of the class message.
Faisal Vali421b2d12017-12-29 05:41:00 +00002958 Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002959 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002960 if (Type.isInvalid())
2961 return true;
2962
2963 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002964 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002965 return false;
2966}
2967
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002968/// Determine whether the parser is currently referring to a an
Douglas Gregor990ccac2010-05-31 14:40:22 +00002969/// Objective-C message send, using a simplified heuristic to avoid overhead.
2970///
2971/// This routine will only return true for a subset of valid message-send
2972/// expressions.
2973bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002974 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002975 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002976 return GetLookAheadToken(1).is(tok::identifier) &&
2977 GetLookAheadToken(2).is(tok::identifier);
2978}
2979
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002980bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002981 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002982 InMessageExpression)
2983 return false;
2984
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002985 ParsedType Type;
2986
2987 if (Tok.is(tok::annot_typename))
2988 Type = getTypeAnnotation(Tok);
2989 else if (Tok.is(tok::identifier))
2990 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2991 getCurScope());
2992 else
2993 return false;
2994
2995 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2996 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002997 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002998 if (Tok.is(tok::identifier))
2999 TryAnnotateTypeOrScopeToken();
3000
3001 return Tok.is(tok::annot_typename);
3002 }
3003 }
3004
3005 return false;
3006}
3007
Mike Stump11289f42009-09-09 15:08:12 +00003008/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003009/// '[' objc-receiver objc-message-args ']'
3010///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003011/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00003012/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003013/// expression
3014/// class-name
3015/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00003016///
John McCalldadc5752010-08-24 06:29:42 +00003017ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00003018 assert(Tok.is(tok::l_square) && "'[' expected");
3019 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
3020
Douglas Gregora817a192010-05-27 23:06:34 +00003021 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003022 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003023 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00003024 return ExprError();
3025 }
3026
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003027 InMessageExpressionRAIIObject InMessage(*this, true);
3028
David Blaikiebbafb8a2012-03-11 07:00:24 +00003029 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00003030 // We completely separate the C and C++ cases because C++ requires
3031 // more complicated (read: slower) parsing.
3032
3033 // Handle send to super.
3034 // FIXME: This doesn't benefit from the same typo-correction we
3035 // get in Objective-C.
3036 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00003037 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
David Blaikieefdccaa2016-01-15 23:43:34 +00003038 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3039 nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003040
3041 // Parse the receiver, which is either a type or an expression.
3042 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00003043 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00003044 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003045 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003046 return ExprError();
3047 }
3048
3049 if (IsExpr)
David Blaikieefdccaa2016-01-15 23:43:34 +00003050 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3051 static_cast<Expr *>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00003052
3053 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00003054 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00003055 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00003056 }
3057
3058 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00003059 IdentifierInfo *Name = Tok.getIdentifierInfo();
3060 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00003061 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003062 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00003063 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00003064 NextToken().is(tok::period),
3065 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00003066 case Sema::ObjCSuperMessage:
David Blaikieefdccaa2016-01-15 23:43:34 +00003067 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3068 nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003069
John McCallfaf5fb42010-08-26 23:41:50 +00003070 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00003071 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003072 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003073 return ExprError();
3074 }
3075
Douglas Gregore5798dc2010-04-21 20:38:13 +00003076 ConsumeToken(); // the type name
3077
Douglas Gregore83b9562015-07-07 03:57:53 +00003078 // Parse type arguments and protocol qualifiers.
3079 if (Tok.is(tok::less)) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003080 SourceLocation NewEndLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00003081 TypeResult NewReceiverType
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003082 = parseObjCTypeArgsAndProtocolQualifiers(NameLoc, ReceiverType,
3083 /*consumeLastToken=*/true,
3084 NewEndLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00003085 if (!NewReceiverType.isUsable()) {
3086 SkipUntil(tok::r_square, StopAtSemi);
3087 return ExprError();
3088 }
3089
3090 ReceiverType = NewReceiverType.get();
3091 }
3092
Douglas Gregore5798dc2010-04-21 20:38:13 +00003093 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00003094 ReceiverType, nullptr);
3095
John McCallfaf5fb42010-08-26 23:41:50 +00003096 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003097 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00003098 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00003099 }
Chris Lattner8f697062008-01-25 18:59:06 +00003100 }
Chris Lattnera36ec422010-04-11 08:28:14 +00003101
3102 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00003103 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003104 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003105 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003106 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00003107 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003108
David Blaikieefdccaa2016-01-15 23:43:34 +00003109 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3110 Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00003111}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003112
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003113/// Parse the remainder of an Objective-C message following the
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003114/// '[' objc-receiver.
3115///
3116/// This routine handles sends to super, class messages (sent to a
3117/// class name), and instance messages (sent to an object), and the
3118/// target is represented by \p SuperLoc, \p ReceiverType, or \p
3119/// ReceiverExpr, respectively. Only one of these parameters may have
3120/// a valid value.
3121///
3122/// \param LBracLoc The location of the opening '['.
3123///
3124/// \param SuperLoc If this is a send to 'super', the location of the
3125/// 'super' keyword that indicates a send to the superclass.
3126///
3127/// \param ReceiverType If this is a class message, the type of the
3128/// class we are sending a message to.
3129///
3130/// \param ReceiverExpr If this is an instance message, the expression
3131/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00003132///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003133/// objc-message-args:
3134/// objc-selector
3135/// objc-keywordarg-list
3136///
3137/// objc-keywordarg-list:
3138/// objc-keywordarg
3139/// objc-keywordarg-list objc-keywordarg
3140///
Mike Stump11289f42009-09-09 15:08:12 +00003141/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003142/// selector-name[opt] ':' objc-keywordexpr
3143///
3144/// objc-keywordexpr:
3145/// nonempty-expr-list
3146///
3147/// nonempty-expr-list:
3148/// assignment-expression
3149/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003150///
John McCalldadc5752010-08-24 06:29:42 +00003151ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00003152Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003153 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00003154 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00003155 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003156 InMessageExpressionRAIIObject InMessage(*this, true);
3157
Steve Naroffeae65032009-11-07 02:08:14 +00003158 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003159 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003160 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003161 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003162 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003163 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003164 false);
Steve Naroffeae65032009-11-07 02:08:14 +00003165 else
John McCallb268a282010-08-23 23:25:46 +00003166 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003167 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003168 cutOffParsing();
3169 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00003170 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00003171
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003172 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00003173 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003174 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003175
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003176 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003177 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003178 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00003179
Chris Lattner0ef13522007-10-09 17:51:17 +00003180 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003181 while (1) {
3182 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00003183 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003184 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00003185
Alp Toker383d2c42014-01-01 03:08:43 +00003186 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00003187 // We must manually skip to a ']', otherwise the expression skipper will
3188 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3189 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003190 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003191 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003192 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003193
Mike Stump11289f42009-09-09 15:08:12 +00003194 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003195
3196 if (Tok.is(tok::code_completion)) {
3197 if (SuperLoc.isValid())
3198 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003199 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003200 /*AtArgumentEpression=*/true);
3201 else if (ReceiverType)
3202 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003203 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003204 /*AtArgumentEpression=*/true);
3205 else
3206 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003207 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003208 /*AtArgumentEpression=*/true);
3209
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003210 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003211 return ExprError();
3212 }
3213
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00003214 ExprResult Expr;
3215 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3216 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3217 Expr = ParseBraceInitializer();
3218 } else
3219 Expr = ParseAssignmentExpression();
3220
3221 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003222 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00003223 // We must manually skip to a ']', otherwise the expression skipper will
3224 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3225 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003226 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003227 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00003228 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003229
Steve Naroff486760a2007-09-17 20:25:27 +00003230 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003231 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003232
Douglas Gregor1b605f72009-11-19 01:08:35 +00003233 // Code completion after each argument.
3234 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003235 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003236 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003237 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003238 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003239 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003240 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003241 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003242 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00003243 else
John McCallb268a282010-08-23 23:25:46 +00003244 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003245 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003246 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003247 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003248 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00003249 }
3250
Steve Naroff486760a2007-09-17 20:25:27 +00003251 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00003252 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00003253 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003254 break;
3255 // We have a selector or a colon, continue parsing.
3256 }
3257 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00003258 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003259 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00003260 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00003261 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00003262 if (Tok.is(tok::colon))
3263 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003264 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003265 if (Tok.is(tok::colon)) {
3266 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
3267 FixItHint::CreateRemoval(commaLoc);
3268 }
Chris Lattner197a3012008-08-05 06:19:09 +00003269 // We must manually skip to a ']', otherwise the expression skipper will
3270 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3271 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003272 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003273 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003274 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003275
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003276 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003277 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003278 }
3279 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00003280 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003281
Chris Lattner197a3012008-08-05 06:19:09 +00003282 // We must manually skip to a ']', otherwise the expression skipper will
3283 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3284 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003285 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003286 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003287 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00003288
Chris Lattner0ef13522007-10-09 17:51:17 +00003289 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00003290 Diag(Tok, diag::err_expected)
3291 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00003292 // We must manually skip to a ']', otherwise the expression skipper will
3293 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3294 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003295 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003296 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003297 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003298
Chris Lattner8f697062008-01-25 18:59:06 +00003299 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003300
Steve Naroffe61bfa82007-10-05 18:42:47 +00003301 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003302 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00003303 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003304 KeyLocs.push_back(Loc);
3305 }
Chris Lattner5700fab2007-10-07 02:00:24 +00003306 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003307
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003308 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003309 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003310 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003311 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003312 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003313 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00003314 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003315 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003316}
3317
John McCalldadc5752010-08-24 06:29:42 +00003318ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
3319 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003320 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003321
Chris Lattnere002fbe2007-12-12 01:04:12 +00003322 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
3323 // expressions. At this point, we know that the only valid thing that starts
3324 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003325 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003326 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00003327 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003328 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003329
Chris Lattnere002fbe2007-12-12 01:04:12 +00003330 while (Tok.is(tok::at)) {
3331 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00003332
Sebastian Redlc13f2682008-12-09 20:22:58 +00003333 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00003334 if (!isTokenStringLiteral())
3335 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00003336
John McCalldadc5752010-08-24 06:29:42 +00003337 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003338 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003339 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003340
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003341 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00003342 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003343
Craig Topper883dd332015-12-24 23:58:11 +00003344 return Actions.ParseObjCStringLiteral(AtLocs.data(), AtStrings);
Anders Carlsson76f4a902007-08-21 17:43:55 +00003345}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003346
Ted Kremeneke65b0862012-03-06 20:05:56 +00003347/// ParseObjCBooleanLiteral -
3348/// objc-scalar-literal : '@' boolean-keyword
3349/// ;
3350/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
3351/// ;
3352ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
3353 bool ArgValue) {
3354 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
3355 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
3356}
3357
3358/// ParseObjCCharacterLiteral -
3359/// objc-scalar-literal : '@' character-literal
3360/// ;
3361ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
3362 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
3363 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003364 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003365 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003366 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003367 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003368}
3369
3370/// ParseObjCNumericLiteral -
3371/// objc-scalar-literal : '@' scalar-literal
3372/// ;
3373/// scalar-literal : | numeric-constant /* any numeric constant. */
3374/// ;
3375ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3376 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3377 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003378 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003379 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003380 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003381 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003382}
3383
Patrick Beard0caa3942012-04-19 00:25:12 +00003384/// ParseObjCBoxedExpr -
3385/// objc-box-expression:
3386/// @( assignment-expression )
3387ExprResult
3388Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3389 if (Tok.isNot(tok::l_paren))
3390 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3391
3392 BalancedDelimiterTracker T(*this, tok::l_paren);
3393 T.consumeOpen();
3394 ExprResult ValueExpr(ParseAssignmentExpression());
3395 if (T.consumeClose())
3396 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00003397
3398 if (ValueExpr.isInvalid())
3399 return ExprError();
3400
Patrick Beard0caa3942012-04-19 00:25:12 +00003401 // Wrap the sub-expression in a parenthesized expression, to distinguish
3402 // a boxed expression from a literal.
3403 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003404 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00003405 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003406 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00003407}
3408
Ted Kremeneke65b0862012-03-06 20:05:56 +00003409ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00003410 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003411 ConsumeBracket(); // consume the l_square.
3412
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003413 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003414 while (Tok.isNot(tok::r_square)) {
3415 // Parse list of array element expressions (all must be id types).
3416 ExprResult Res(ParseAssignmentExpression());
3417 if (Res.isInvalid()) {
3418 // We must manually skip to a ']', otherwise the expression skipper will
3419 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3420 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003421 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003422 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003423 }
3424
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003425 Res = Actions.CorrectDelayedTyposInExpr(Res.get());
3426 if (Res.isInvalid())
3427 HasInvalidEltExpr = true;
3428
Ted Kremeneke65b0862012-03-06 20:05:56 +00003429 // Parse the ellipsis that indicates a pack expansion.
3430 if (Tok.is(tok::ellipsis))
3431 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
3432 if (Res.isInvalid())
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003433 HasInvalidEltExpr = true;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003434
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003435 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003436
3437 if (Tok.is(tok::comma))
3438 ConsumeToken(); // Eat the ','.
3439 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00003440 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3441 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003442 }
3443 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003444
3445 if (HasInvalidEltExpr)
3446 return ExprError();
3447
Benjamin Kramerf0623432012-08-23 22:51:59 +00003448 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00003449 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003450}
3451
3452ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3453 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3454 ConsumeBrace(); // consume the l_square.
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003455 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003456 while (Tok.isNot(tok::r_brace)) {
3457 // Parse the comma separated key : value expressions.
3458 ExprResult KeyExpr;
3459 {
3460 ColonProtectionRAIIObject X(*this);
3461 KeyExpr = ParseAssignmentExpression();
3462 if (KeyExpr.isInvalid()) {
3463 // We must manually skip to a '}', otherwise the expression skipper will
3464 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3465 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003466 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003467 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003468 }
3469 }
3470
Alp Toker383d2c42014-01-01 03:08:43 +00003471 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003472 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00003473 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003474 }
3475
3476 ExprResult ValueExpr(ParseAssignmentExpression());
3477 if (ValueExpr.isInvalid()) {
3478 // We must manually skip to a '}', otherwise the expression skipper will
3479 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3480 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003481 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003482 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003483 }
3484
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003485 // Check the key and value for possible typos
3486 KeyExpr = Actions.CorrectDelayedTyposInExpr(KeyExpr.get());
3487 ValueExpr = Actions.CorrectDelayedTyposInExpr(ValueExpr.get());
3488 if (KeyExpr.isInvalid() || ValueExpr.isInvalid())
3489 HasInvalidEltExpr = true;
3490
3491 // Parse the ellipsis that designates this as a pack expansion. Do not
3492 // ActOnPackExpansion here, leave it to template instantiation time where
3493 // we can get better diagnostics.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003494 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00003495 if (getLangOpts().CPlusPlus)
3496 TryConsumeToken(tok::ellipsis, EllipsisLoc);
3497
Ted Kremeneke65b0862012-03-06 20:05:56 +00003498 // We have a valid expression. Collect it in a vector so we can
3499 // build the argument list.
3500 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00003501 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00003502 };
3503 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00003504
3505 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00003506 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3507 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003508 }
3509 SourceLocation EndLoc = ConsumeBrace();
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003510
3511 if (HasInvalidEltExpr)
3512 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003513
3514 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00003515 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
Craig Topperd4336e02015-12-24 23:58:15 +00003516 Elements);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003517}
3518
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003519/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00003520/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00003521ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003522Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00003523 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003524
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003525 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003526
Chris Lattner197a3012008-08-05 06:19:09 +00003527 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003528 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3529
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003530 BalancedDelimiterTracker T(*this, tok::l_paren);
3531 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003532
Douglas Gregor220cac52009-02-18 17:45:20 +00003533 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003534
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003535 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003536
Douglas Gregor220cac52009-02-18 17:45:20 +00003537 if (Ty.isInvalid())
3538 return ExprError();
3539
Nico Webera7c7e602012-12-31 00:28:03 +00003540 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3541 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003542}
Anders Carlssone01493d2007-08-23 15:25:28 +00003543
3544/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00003545/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00003546ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003547Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00003548 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003549
Chris Lattner197a3012008-08-05 06:19:09 +00003550 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003551 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3552
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003553 BalancedDelimiterTracker T(*this, tok::l_paren);
3554 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003555
Alex Lorenzf1278212017-04-11 15:01:53 +00003556 if (expectIdentifier())
3557 return ExprError();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003558
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003559 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00003560 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003561
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003562 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00003563
Nico Webera7c7e602012-12-31 00:28:03 +00003564 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3565 T.getOpenLocation(), ProtoIdLoc,
3566 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00003567}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003568
3569/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003570/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003571ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003572 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003573
Chris Lattner197a3012008-08-05 06:19:09 +00003574 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003575 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3576
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003577 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003578 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003579
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003580 BalancedDelimiterTracker T(*this, tok::l_paren);
3581 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003582 bool HasOptionalParen = Tok.is(tok::l_paren);
3583 if (HasOptionalParen)
3584 ConsumeParen();
3585
Douglas Gregor67c692c2010-08-26 15:07:07 +00003586 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003587 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003588 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003589 return ExprError();
3590 }
3591
Chris Lattner4f472a32009-04-11 18:13:45 +00003592 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003593 if (!SelIdent && // missing selector name.
3594 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003595 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003596
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003597 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003598
Steve Naroff152dd812007-12-05 22:21:29 +00003599 unsigned nColons = 0;
3600 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003601 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003602 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003603 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003604 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003605 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3606 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003607 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003608
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003609 if (Tok.is(tok::r_paren))
3610 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003611
3612 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003613 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003614 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003615 return ExprError();
3616 }
3617
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003618 // Check for another keyword selector.
3619 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003620 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003621 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003622 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003623 break;
3624 }
Steve Naroff152dd812007-12-05 22:21:29 +00003625 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003626 if (HasOptionalParen && Tok.is(tok::r_paren))
3627 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003628 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003629 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003630 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3631 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003632 T.getCloseLocation(),
3633 !HasOptionalParen);
Eugene Zelenko1ced5092016-02-12 22:53:10 +00003634}
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003635
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003636void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3637 // MCDecl might be null due to error in method or c-function prototype, etc.
3638 Decl *MCDecl = LM.D;
3639 bool skip = MCDecl &&
3640 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3641 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3642 if (skip)
3643 return;
3644
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003645 // Save the current token position.
3646 SourceLocation OrigLoc = Tok.getLocation();
3647
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003648 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
Alex Lorenz812012f2017-06-19 17:53:21 +00003649 // Store an artificial EOF token to ensure that we don't run off the end of
3650 // the method's body when we come to parse it.
3651 Token Eof;
3652 Eof.startToken();
3653 Eof.setKind(tok::eof);
3654 Eof.setEofData(MCDecl);
3655 Eof.setLocation(OrigLoc);
3656 LM.Toks.push_back(Eof);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003657 // Append the current token at the end of the new token stream so that it
3658 // doesn't get lost.
3659 LM.Toks.push_back(Tok);
David Blaikie2eabcc92016-02-09 18:52:09 +00003660 PP.EnterTokenStream(LM.Toks, true);
3661
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003662 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003663 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003664
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003665 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3666 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003667 // Enter a scope for the method or c-function body.
Momchil Velikov57c681f2017-08-10 15:43:06 +00003668 ParseScope BodyScope(this, (parseMethod ? Scope::ObjCMethodScope : 0) |
3669 Scope::FnScope | Scope::DeclScope |
3670 Scope::CompoundStmtScope);
3671
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003672 // Tell the actions module that we have entered a method or c-function definition
3673 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003674 if (parseMethod)
3675 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3676 else
3677 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003678 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003679 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003680 else {
3681 if (Tok.is(tok::colon))
3682 ParseConstructorInitializer(MCDecl);
Akira Hatanakabd59b4892016-04-18 18:19:45 +00003683 else
3684 Actions.ActOnDefaultCtorInitializers(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003685 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003686 }
Alex Lorenz812012f2017-06-19 17:53:21 +00003687
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003688 if (Tok.getLocation() != OrigLoc) {
3689 // Due to parsing error, we either went over the cached tokens or
3690 // there are still cached tokens left. If it's the latter case skip the
3691 // leftover tokens.
3692 // Since this is an uncommon situation that should be avoided, use the
3693 // expensive isBeforeInTranslationUnit call.
3694 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3695 OrigLoc))
3696 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3697 ConsumeAnyToken();
3698 }
Alex Lorenz812012f2017-06-19 17:53:21 +00003699 // Clean up the remaining EOF token.
3700 ConsumeAnyToken();
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003701}