blob: f7410b8a092a07a6fffb5ffa987f7e1ff3b40391 [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 Rosea7d03842013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Parse/ParseDiagnostic.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000018#include "clang/Parse/RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/Scope.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000022#include "llvm/ADT/SmallVector.h"
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +000023#include "llvm/ADT/StringExtras.h"
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'
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000048Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000049 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000050
Douglas Gregorf48706c2009-12-07 09:27:33 +000051 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000052 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000053 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +000054 return nullptr;
Douglas Gregorf48706c2009-12-07 09:27:33 +000055 }
Craig Topper161e4db2014-05-21 06:02:52 +000056
57 Decl *SingleDecl = nullptr;
Steve Naroff7c348172007-08-23 18:16:40 +000058 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000059 case tok::objc_class:
60 return ParseObjCAtClassDeclaration(AtLoc);
John McCall53fa7142010-12-24 02:08:15 +000061 case tok::objc_interface: {
John McCall084e83d2011-03-24 11:26:52 +000062 ParsedAttributes attrs(AttrFactory);
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000063 SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
64 break;
John McCall53fa7142010-12-24 02:08:15 +000065 }
66 case tok::objc_protocol: {
John McCall084e83d2011-03-24 11:26:52 +000067 ParsedAttributes attrs(AttrFactory);
Douglas Gregorf6102672012-01-01 21:23:57 +000068 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
John McCall53fa7142010-12-24 02:08:15 +000069 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000070 case tok::objc_implementation:
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +000071 return ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000072 case tok::objc_end:
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +000073 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattnerce90ef52008-08-23 02:02:23 +000074 case tok::objc_compatibility_alias:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000075 SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
76 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000077 case tok::objc_synthesize:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000078 SingleDecl = ParseObjCPropertySynthesize(AtLoc);
79 break;
Chris Lattnerce90ef52008-08-23 02:02:23 +000080 case tok::objc_dynamic:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000081 SingleDecl = ParseObjCPropertyDynamic(AtLoc);
82 break;
Douglas Gregorc50d4922012-12-11 22:11:52 +000083 case tok::objc_import:
Sean Callanan87596492014-12-09 23:47:56 +000084 if (getLangOpts().Modules || getLangOpts().DebuggerSupport)
Douglas Gregor0bf886d2012-01-03 18:24:14 +000085 return ParseModuleImport(AtLoc);
Manman Rendfcf1cb2017-01-20 20:03:00 +000086 Diag(AtLoc, diag::err_atimport);
Fariborz Jahaniana773d082014-03-26 22:02:43 +000087 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000088 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerce90ef52008-08-23 02:02:23 +000089 default:
90 Diag(AtLoc, diag::err_unexpected_at);
91 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000092 SingleDecl = nullptr;
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000093 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000094 }
Fariborz Jahanian3a039e32011-08-27 20:50:59 +000095 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000096}
97
Richard Smith3df3f1d2015-11-03 01:19:56 +000098/// Class to handle popping type parameters when leaving the scope.
99class Parser::ObjCTypeParamListScope {
100 Sema &Actions;
101 Scope *S;
102 ObjCTypeParamList *Params;
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000103
Richard Smith3df3f1d2015-11-03 01:19:56 +0000104public:
105 ObjCTypeParamListScope(Sema &Actions, Scope *S)
106 : Actions(Actions), S(S), Params(nullptr) {}
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000107
Richard Smith3df3f1d2015-11-03 01:19:56 +0000108 ~ObjCTypeParamListScope() {
109 leave();
110 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000111
Richard Smith3df3f1d2015-11-03 01:19:56 +0000112 void enter(ObjCTypeParamList *P) {
113 assert(!Params);
114 Params = P;
115 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000116
Richard Smith3df3f1d2015-11-03 01:19:56 +0000117 void leave() {
118 if (Params)
119 Actions.popObjCTypeParamList(S, Params);
120 Params = nullptr;
121 }
122};
123
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000124///
Mike Stump11289f42009-09-09 15:08:12 +0000125/// objc-class-declaration:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000126/// '@' 'class' objc-class-forward-decl (',' objc-class-forward-decl)* ';'
127///
128/// objc-class-forward-decl:
129/// identifier objc-type-parameter-list[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000130///
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000131Parser::DeclGroupPtrTy
132Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000133 ConsumeToken(); // the identifier "class"
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000134 SmallVector<IdentifierInfo *, 8> ClassNames;
135 SmallVector<SourceLocation, 8> ClassLocs;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000136 SmallVector<ObjCTypeParamList *, 8> ClassTypeParams;
Mike Stump11289f42009-09-09 15:08:12 +0000137
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000138 while (1) {
Nico Weber69a79142013-04-04 00:15:10 +0000139 MaybeSkipAttributes(tok::objc_class);
Alex Lorenzf1278212017-04-11 15:01:53 +0000140 if (expectIdentifier()) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000141 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000142 return Actions.ConvertDeclToDeclGroup(nullptr);
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000143 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000144 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +0000145 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000146 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000147
Douglas Gregor85f3f952015-07-07 03:57:15 +0000148 // Parse the optional objc-type-parameter-list.
149 ObjCTypeParamList *TypeParams = nullptr;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000150 if (Tok.is(tok::less))
Douglas Gregor85f3f952015-07-07 03:57:15 +0000151 TypeParams = parseObjCTypeParamList();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000152 ClassTypeParams.push_back(TypeParams);
Alp Toker383d2c42014-01-01 03:08:43 +0000153 if (!TryConsumeToken(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000154 break;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000155 }
Mike Stump11289f42009-09-09 15:08:12 +0000156
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000157 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +0000158 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@class"))
Craig Topper161e4db2014-05-21 06:02:52 +0000159 return Actions.ConvertDeclToDeclGroup(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000160
Ted Kremeneka26da852009-11-17 23:12:20 +0000161 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
162 ClassLocs.data(),
Douglas Gregor85f3f952015-07-07 03:57:15 +0000163 ClassTypeParams,
Ted Kremeneka26da852009-11-17 23:12:20 +0000164 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000165}
166
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000167void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
168{
169 Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
170 if (ock == Sema::OCK_None)
171 return;
172
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000173 Decl *Decl = Actions.getObjCDeclContext();
174 if (CurParsedObjCImpl) {
175 CurParsedObjCImpl->finish(AtLoc);
176 } else {
177 Actions.ActOnAtEnd(getCurScope(), AtLoc);
178 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000179 Diag(AtLoc, diag::err_objc_missing_end)
180 << FixItHint::CreateInsertion(AtLoc, "@end\n");
181 if (Decl)
182 Diag(Decl->getLocStart(), diag::note_objc_container_start)
183 << (int) ock;
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000184}
185
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000186///
187/// objc-interface:
188/// objc-class-interface-attributes[opt] objc-class-interface
189/// objc-category-interface
190///
191/// objc-class-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000192/// '@' 'interface' identifier objc-type-parameter-list[opt]
193/// objc-superclass[opt] objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000194/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000195/// objc-interface-decl-list
196/// @end
197///
198/// objc-category-interface:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000199/// '@' 'interface' identifier objc-type-parameter-list[opt]
200/// '(' identifier[opt] ')' objc-protocol-refs[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000201/// objc-interface-decl-list
202/// @end
203///
204/// objc-superclass:
Douglas Gregore9d95f12015-07-07 03:57:35 +0000205/// ':' identifier objc-type-arguments[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000206///
207/// objc-class-interface-attributes:
208/// __attribute__((visibility("default")))
209/// __attribute__((visibility("hidden")))
210/// __attribute__((deprecated))
211/// __attribute__((unavailable))
212/// __attribute__((objc_exception)) - used by NSException on 64-bit
Patrick Beardacfbe9e2012-04-06 18:12:22 +0000213/// __attribute__((objc_root_class))
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000214///
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000215Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +0000216 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000217 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000218 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000219 CheckNestedObjCContexts(AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000220 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000221
Douglas Gregor49c22a72009-11-18 16:26:39 +0000222 // Code completion after '@interface'.
223 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000224 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000225 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000226 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000227 }
228
Nico Weber69a79142013-04-04 00:15:10 +0000229 MaybeSkipAttributes(tok::objc_interface);
Nico Weber04e213b2013-04-03 17:36:11 +0000230
Alex Lorenzf1278212017-04-11 15:01:53 +0000231 if (expectIdentifier())
232 return nullptr; // missing class or category name.
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000233
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000234 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000235 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000236 SourceLocation nameLoc = ConsumeToken();
Douglas Gregor85f3f952015-07-07 03:57:15 +0000237
238 // Parse the objc-type-parameter-list or objc-protocol-refs. For the latter
239 // case, LAngleLoc will be valid and ProtocolIdents will capture the
240 // protocol references (that have not yet been resolved).
241 SourceLocation LAngleLoc, EndProtoLoc;
242 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
243 ObjCTypeParamList *typeParameterList = nullptr;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000244 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
245 if (Tok.is(tok::less))
246 typeParameterList = parseObjCTypeParamListOrProtocolRefs(
247 typeParamScope, LAngleLoc, ProtocolIdents, EndProtoLoc);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000248
249 if (Tok.is(tok::l_paren) &&
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000250 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000251
252 BalancedDelimiterTracker T(*this, tok::l_paren);
253 T.consumeOpen();
254
255 SourceLocation categoryLoc;
Craig Topper161e4db2014-05-21 06:02:52 +0000256 IdentifierInfo *categoryId = nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000257 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000258 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000259 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000260 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000261 }
262
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000263 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000264 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000265 categoryId = Tok.getIdentifierInfo();
266 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000267 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000268 else if (!getLangOpts().ObjC2) {
Alp Tokerec543272013-12-24 09:48:30 +0000269 Diag(Tok, diag::err_expected)
270 << tok::identifier; // missing category name.
Craig Topper161e4db2014-05-21 06:02:52 +0000271 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000272 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000273
274 T.consumeClose();
275 if (T.getCloseLocation().isInvalid())
Craig Topper161e4db2014-05-21 06:02:52 +0000276 return nullptr;
Douglas Gregor0c254a02011-09-23 19:19:41 +0000277
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000278 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000279 assert(LAngleLoc.isInvalid() && "Cannot have already parsed protocols");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000280 SmallVector<Decl *, 8> ProtocolRefs;
281 SmallVector<SourceLocation, 8> ProtocolLocs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000282 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000283 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000284 LAngleLoc, EndProtoLoc,
285 /*consumeLastToken=*/true))
Craig Topper161e4db2014-05-21 06:02:52 +0000286 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000287
Alex Lorenzf9371392017-03-23 11:44:25 +0000288 Decl *CategoryType = Actions.ActOnStartCategoryInterface(
289 AtLoc, nameId, nameLoc, typeParameterList, categoryId, categoryLoc,
290 ProtocolRefs.data(), ProtocolRefs.size(), ProtocolLocs.data(),
291 EndProtoLoc, attrs.getList());
292
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000293 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000294 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000295
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000296 ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000297
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000298 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000299 }
300 // Parse a class interface.
Craig Topper161e4db2014-05-21 06:02:52 +0000301 IdentifierInfo *superClassId = nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000302 SourceLocation superClassLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000303 SourceLocation typeArgsLAngleLoc;
304 SmallVector<ParsedType, 4> typeArgs;
305 SourceLocation typeArgsRAngleLoc;
306 SmallVector<Decl *, 4> protocols;
307 SmallVector<SourceLocation, 4> protocolLocs;
Chris Lattner0ef13522007-10-09 17:51:17 +0000308 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000309 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000310
311 // Code completion of superclass names.
312 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000313 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000314 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000315 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +0000316 }
317
Alex Lorenzf1278212017-04-11 15:01:53 +0000318 if (expectIdentifier())
319 return nullptr; // missing super class name.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000320 superClassId = Tok.getIdentifierInfo();
321 superClassLoc = ConsumeToken();
Douglas Gregore9d95f12015-07-07 03:57:35 +0000322
323 // Type arguments for the superclass or protocol conformances.
324 if (Tok.is(tok::less)) {
David Blaikieefdccaa2016-01-15 23:43:34 +0000325 parseObjCTypeArgsOrProtocolQualifiers(
326 nullptr, typeArgsLAngleLoc, typeArgs, typeArgsRAngleLoc, LAngleLoc,
327 protocols, protocolLocs, EndProtoLoc,
328 /*consumeLastToken=*/true,
329 /*warnOnIncompleteProtocols=*/true);
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +0000330 if (Tok.is(tok::eof))
331 return nullptr;
Douglas Gregore9d95f12015-07-07 03:57:35 +0000332 }
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000333 }
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +0000334
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000335 // Next, we need to check for any protocol references.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000336 if (LAngleLoc.isValid()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000337 if (!ProtocolIdents.empty()) {
338 // We already parsed the protocols named when we thought we had a
339 // type parameter list. Translate them into actual protocol references.
340 for (const auto &pair : ProtocolIdents) {
341 protocolLocs.push_back(pair.second);
342 }
343 Actions.FindProtocolDeclaration(/*WarnOnDeclarations=*/true,
344 /*ForObjCContainer=*/true,
Craig Toppera9247eb2015-10-22 04:59:56 +0000345 ProtocolIdents, protocols);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000346 }
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000347 } else if (protocols.empty() && Tok.is(tok::less) &&
348 ParseObjCProtocolReferences(protocols, protocolLocs, true, true,
349 LAngleLoc, EndProtoLoc,
350 /*consumeLastToken=*/true)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000351 return nullptr;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000352 }
Mike Stump11289f42009-09-09 15:08:12 +0000353
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000354 if (Tok.isNot(tok::less))
Argyrios Kyrtzidisf95a0002016-11-09 02:47:07 +0000355 Actions.ActOnTypedefedProtocols(protocols, protocolLocs,
356 superClassId, superClassLoc);
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000357
John McCall48871652010-08-21 09:40:31 +0000358 Decl *ClsType =
Douglas Gregore9d95f12015-07-07 03:57:35 +0000359 Actions.ActOnStartClassInterface(getCurScope(), AtLoc, nameId, nameLoc,
360 typeParameterList, superClassId,
361 superClassLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +0000362 typeArgs,
363 SourceRange(typeArgsLAngleLoc,
364 typeArgsRAngleLoc),
365 protocols.data(), protocols.size(),
366 protocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000367 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000368
Chris Lattner0ef13522007-10-09 17:51:17 +0000369 if (Tok.is(tok::l_brace))
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000370 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000371
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000372 ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000373
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000374 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000375}
376
Douglas Gregor813a0662015-06-19 18:14:38 +0000377/// Add an attribute for a context-sensitive type nullability to the given
378/// declarator.
379static void addContextSensitiveTypeNullability(Parser &P,
380 Declarator &D,
381 NullabilityKind nullability,
382 SourceLocation nullabilityLoc,
383 bool &addedToDeclSpec) {
384 // Create the attribute.
385 auto getNullabilityAttr = [&]() -> AttributeList * {
Douglas Gregorbec595a2015-06-19 18:27:45 +0000386 return D.getAttributePool().create(
387 P.getNullabilityKeyword(nullability),
388 SourceRange(nullabilityLoc),
389 nullptr, SourceLocation(),
390 nullptr, 0,
391 AttributeList::AS_ContextSensitiveKeyword);
Douglas Gregor813a0662015-06-19 18:14:38 +0000392 };
393
394 if (D.getNumTypeObjects() > 0) {
395 // Add the attribute to the declarator chunk nearest the declarator.
396 auto nullabilityAttr = getNullabilityAttr();
397 DeclaratorChunk &chunk = D.getTypeObject(0);
398 nullabilityAttr->setNext(chunk.getAttrListRef());
399 chunk.getAttrListRef() = nullabilityAttr;
400 } else if (!addedToDeclSpec) {
401 // Otherwise, just put it on the declaration specifiers (if one
402 // isn't there already).
403 D.getMutableDeclSpec().addAttributes(getNullabilityAttr());
404 addedToDeclSpec = true;
405 }
406}
407
Douglas Gregor85f3f952015-07-07 03:57:15 +0000408/// Parse an Objective-C type parameter list, if present, or capture
409/// the locations of the protocol identifiers for a list of protocol
410/// references.
411///
412/// objc-type-parameter-list:
413/// '<' objc-type-parameter (',' objc-type-parameter)* '>'
414///
415/// objc-type-parameter:
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000416/// objc-type-parameter-variance? identifier objc-type-parameter-bound[opt]
Douglas Gregor85f3f952015-07-07 03:57:15 +0000417///
418/// objc-type-parameter-bound:
419/// ':' type-name
420///
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000421/// objc-type-parameter-variance:
422/// '__covariant'
423/// '__contravariant'
424///
Douglas Gregor85f3f952015-07-07 03:57:15 +0000425/// \param lAngleLoc The location of the starting '<'.
426///
427/// \param protocolIdents Will capture the list of identifiers, if the
428/// angle brackets contain a list of protocol references rather than a
429/// type parameter list.
430///
431/// \param rAngleLoc The location of the ending '>'.
432ObjCTypeParamList *Parser::parseObjCTypeParamListOrProtocolRefs(
Richard Smith3df3f1d2015-11-03 01:19:56 +0000433 ObjCTypeParamListScope &Scope, SourceLocation &lAngleLoc,
434 SmallVectorImpl<IdentifierLocPair> &protocolIdents,
435 SourceLocation &rAngleLoc, bool mayBeProtocolList) {
Douglas Gregor85f3f952015-07-07 03:57:15 +0000436 assert(Tok.is(tok::less) && "Not at the beginning of a type parameter list");
437
438 // Within the type parameter list, don't treat '>' as an operator.
439 GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
440
441 // Local function to "flush" the protocol identifiers, turning them into
442 // type parameters.
443 SmallVector<Decl *, 4> typeParams;
444 auto makeProtocolIdentsIntoTypeParameters = [&]() {
Douglas Gregore83b9562015-07-07 03:57:53 +0000445 unsigned index = 0;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000446 for (const auto &pair : protocolIdents) {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000447 DeclResult typeParam = Actions.actOnObjCTypeParam(
David Blaikieefdccaa2016-01-15 23:43:34 +0000448 getCurScope(), ObjCTypeParamVariance::Invariant, SourceLocation(),
449 index++, pair.first, pair.second, SourceLocation(), nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000450 if (typeParam.isUsable())
451 typeParams.push_back(typeParam.get());
452 }
453
454 protocolIdents.clear();
455 mayBeProtocolList = false;
456 };
457
458 bool invalid = false;
459 lAngleLoc = ConsumeToken();
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000460
Douglas Gregor85f3f952015-07-07 03:57:15 +0000461 do {
Douglas Gregor1ac1b632015-07-07 03:58:54 +0000462 // Parse the variance, if any.
463 SourceLocation varianceLoc;
464 ObjCTypeParamVariance variance = ObjCTypeParamVariance::Invariant;
465 if (Tok.is(tok::kw___covariant) || Tok.is(tok::kw___contravariant)) {
466 variance = Tok.is(tok::kw___covariant)
467 ? ObjCTypeParamVariance::Covariant
468 : ObjCTypeParamVariance::Contravariant;
469 varianceLoc = ConsumeToken();
470
471 // Once we've seen a variance specific , we know this is not a
472 // list of protocol references.
473 if (mayBeProtocolList) {
474 // Up until now, we have been queuing up parameters because they
475 // might be protocol references. Turn them into parameters now.
476 makeProtocolIdentsIntoTypeParameters();
477 }
478 }
479
Douglas Gregor85f3f952015-07-07 03:57:15 +0000480 // Parse the identifier.
481 if (!Tok.is(tok::identifier)) {
482 // Code completion.
483 if (Tok.is(tok::code_completion)) {
484 // FIXME: If these aren't protocol references, we'll need different
485 // completions.
Craig Topper883dd332015-12-24 23:58:11 +0000486 Actions.CodeCompleteObjCProtocolReferences(protocolIdents);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000487 cutOffParsing();
488
489 // FIXME: Better recovery here?.
490 return nullptr;
491 }
492
493 Diag(Tok, diag::err_objc_expected_type_parameter);
494 invalid = true;
495 break;
496 }
497
498 IdentifierInfo *paramName = Tok.getIdentifierInfo();
499 SourceLocation paramLoc = ConsumeToken();
500
501 // If there is a bound, parse it.
502 SourceLocation colonLoc;
503 TypeResult boundType;
504 if (TryConsumeToken(tok::colon, colonLoc)) {
505 // Once we've seen a bound, we know this is not a list of protocol
506 // references.
507 if (mayBeProtocolList) {
508 // Up until now, we have been queuing up parameters because they
509 // might be protocol references. Turn them into parameters now.
510 makeProtocolIdentsIntoTypeParameters();
511 }
512
513 // type-name
514 boundType = ParseTypeName();
515 if (boundType.isInvalid())
516 invalid = true;
517 } else if (mayBeProtocolList) {
518 // If this could still be a protocol list, just capture the identifier.
519 // We don't want to turn it into a parameter.
520 protocolIdents.push_back(std::make_pair(paramName, paramLoc));
521 continue;
522 }
523
524 // Create the type parameter.
David Blaikieefdccaa2016-01-15 23:43:34 +0000525 DeclResult typeParam = Actions.actOnObjCTypeParam(
526 getCurScope(), variance, varianceLoc, typeParams.size(), paramName,
527 paramLoc, colonLoc, boundType.isUsable() ? boundType.get() : nullptr);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000528 if (typeParam.isUsable())
529 typeParams.push_back(typeParam.get());
530 } while (TryConsumeToken(tok::comma));
531
532 // Parse the '>'.
533 if (invalid) {
534 SkipUntil(tok::greater, tok::at, StopBeforeMatch);
535 if (Tok.is(tok::greater))
536 ConsumeToken();
537 } else if (ParseGreaterThanInTemplateList(rAngleLoc,
538 /*ConsumeLastToken=*/true,
539 /*ObjCGenericList=*/true)) {
540 Diag(lAngleLoc, diag::note_matching) << "'<'";
541 SkipUntil({tok::greater, tok::greaterequal, tok::at, tok::minus,
542 tok::minus, tok::plus, tok::colon, tok::l_paren, tok::l_brace,
543 tok::comma, tok::semi },
544 StopBeforeMatch);
545 if (Tok.is(tok::greater))
546 ConsumeToken();
547 }
548
549 if (mayBeProtocolList) {
550 // A type parameter list must be followed by either a ':' (indicating the
551 // presence of a superclass) or a '(' (indicating that this is a category
552 // or extension). This disambiguates between an objc-type-parameter-list
553 // and a objc-protocol-refs.
554 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_paren)) {
555 // Returning null indicates that we don't have a type parameter list.
556 // The results the caller needs to handle the protocol references are
557 // captured in the reference parameters already.
558 return nullptr;
559 }
560
561 // We have a type parameter list that looks like a list of protocol
562 // references. Turn that parameter list into type parameters.
563 makeProtocolIdentsIntoTypeParameters();
564 }
565
Richard Smith3df3f1d2015-11-03 01:19:56 +0000566 // Form the type parameter list and enter its scope.
Douglas Gregor85f3f952015-07-07 03:57:15 +0000567 ObjCTypeParamList *list = Actions.actOnObjCTypeParamList(
568 getCurScope(),
569 lAngleLoc,
570 typeParams,
571 rAngleLoc);
Richard Smith3df3f1d2015-11-03 01:19:56 +0000572 Scope.enter(list);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000573
574 // Clear out the angle locations; they're used by the caller to indicate
575 // whether there are any protocol references.
576 lAngleLoc = SourceLocation();
577 rAngleLoc = SourceLocation();
Akira Hatanaka8ebd5802015-12-16 06:25:38 +0000578 return invalid ? nullptr : list;
Douglas Gregor85f3f952015-07-07 03:57:15 +0000579}
580
581/// Parse an objc-type-parameter-list.
582ObjCTypeParamList *Parser::parseObjCTypeParamList() {
583 SourceLocation lAngleLoc;
584 SmallVector<IdentifierLocPair, 1> protocolIdents;
585 SourceLocation rAngleLoc;
Richard Smith3df3f1d2015-11-03 01:19:56 +0000586
587 ObjCTypeParamListScope Scope(Actions, getCurScope());
588 return parseObjCTypeParamListOrProtocolRefs(Scope, lAngleLoc, protocolIdents,
589 rAngleLoc,
Douglas Gregor85f3f952015-07-07 03:57:15 +0000590 /*mayBeProtocolList=*/false);
591}
592
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000593/// objc-interface-decl-list:
594/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000595/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000596/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000597/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000598/// objc-interface-decl-list declaration
599/// objc-interface-decl-list ';'
600///
Steve Naroff99264b42007-08-22 16:35:03 +0000601/// objc-method-requirement: [OBJC2]
602/// @required
603/// @optional
604///
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000605void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
606 Decl *CDecl) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000607 SmallVector<Decl *, 32> allMethods;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000608 SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000609 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000610
Ted Kremenekc7c64312010-01-07 01:20:12 +0000611 SourceRange AtEnd;
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +0000612
Steve Naroff99264b42007-08-22 16:35:03 +0000613 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000614 // If this is a method prototype, parse it.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000615 if (Tok.isOneOf(tok::minus, tok::plus)) {
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +0000616 if (Decl *methodPrototype =
617 ParseObjCMethodPrototype(MethodImplKind, false))
618 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000619 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
620 // method definitions.
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000621 if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
622 // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000623 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidise1ee6232011-12-17 04:13:22 +0000624 if (Tok.is(tok::semi))
625 ConsumeToken();
626 }
Steve Naroff99264b42007-08-22 16:35:03 +0000627 continue;
628 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000629 if (Tok.is(tok::l_paren)) {
630 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000631 ParseObjCMethodDecl(Tok.getLocation(),
632 tok::minus,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000633 MethodImplKind, false);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000634 continue;
635 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000636 // Ignore excess semicolons.
637 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000638 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000639 continue;
640 }
Mike Stump11289f42009-09-09 15:08:12 +0000641
Chris Lattnerda9fb152008-10-20 06:10:06 +0000642 // If we got to the end of the file, exit the loop.
Richard Smith34f30512013-11-23 04:06:09 +0000643 if (isEofOrEom())
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000644 break;
Mike Stump11289f42009-09-09 15:08:12 +0000645
Douglas Gregorf1934162010-01-13 21:24:21 +0000646 // Code completion within an Objective-C interface.
647 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000648 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000649 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000650 : Sema::PCC_ObjCInterface);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000651 return cutOffParsing();
Douglas Gregorf1934162010-01-13 21:24:21 +0000652 }
653
Chris Lattner038a3e32008-10-20 05:46:22 +0000654 // If we don't have an @ directive, parse it as a function definition.
655 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000656 // The code below does not consume '}'s because it is afraid of eating the
657 // end of a namespace. Because of the way this code is structured, an
658 // erroneous r_brace would cause an infinite loop if not handled here.
659 if (Tok.is(tok::r_brace))
660 break;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000661 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000662 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000663 continue;
664 }
Mike Stump11289f42009-09-09 15:08:12 +0000665
Chris Lattner038a3e32008-10-20 05:46:22 +0000666 // Otherwise, we have an @ directive, eat the @.
667 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000668 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000669 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000670 return cutOffParsing();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000671 }
672
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000673 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000675 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000676 AtEnd.setBegin(AtLoc);
677 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000678 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000679 } else if (DirectiveKind == tok::objc_not_keyword) {
680 Diag(Tok, diag::err_objc_unknown_at);
681 SkipUntil(tok::semi);
682 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000683 }
Mike Stump11289f42009-09-09 15:08:12 +0000684
Chris Lattnerda9fb152008-10-20 06:10:06 +0000685 // Eat the identifier.
686 ConsumeToken();
687
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000688 switch (DirectiveKind) {
689 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000690 // FIXME: If someone forgets an @end on a protocol, this loop will
691 // continue to eat up tons of stuff and spew lots of nonsense errors. It
692 // would probably be better to bail out if we saw an @class or @interface
693 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000694 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000695 // Skip until we see an '@' or '}' or ';'.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000696 SkipUntil(tok::r_brace, tok::at, StopAtSemi);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000697 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000698
699 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000700 case tok::objc_interface:
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000701 Diag(AtLoc, diag::err_objc_missing_end)
702 << FixItHint::CreateInsertion(AtLoc, "@end\n");
703 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
704 << (int) Actions.getObjCContainerKind();
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000705 ConsumeToken();
706 break;
707
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000708 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000709 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000710 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000711 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000712 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000713 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000714 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000715 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000716 break;
Mike Stump11289f42009-09-09 15:08:12 +0000717
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000718 case tok::objc_property:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000719 if (!getLangOpts().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000720 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000721
Chris Lattner038a3e32008-10-20 05:46:22 +0000722 ObjCDeclSpec OCDS;
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000723 SourceLocation LParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000724 // Parse property attribute list, if any.
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000725 if (Tok.is(tok::l_paren)) {
726 LParenLoc = Tok.getLocation();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000727 ParseObjCPropertyAttribute(OCDS);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000728 }
Mike Stump11289f42009-09-09 15:08:12 +0000729
Douglas Gregor813a0662015-06-19 18:14:38 +0000730 bool addedToDeclSpec = false;
Benjamin Kramera39beb92014-09-03 11:06:10 +0000731 auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
732 if (FD.D.getIdentifier() == nullptr) {
733 Diag(AtLoc, diag::err_objc_property_requires_field_name)
734 << FD.D.getSourceRange();
735 return;
736 }
737 if (FD.BitfieldSize) {
738 Diag(AtLoc, diag::err_objc_property_bitfield)
739 << FD.D.getSourceRange();
740 return;
741 }
742
Douglas Gregor813a0662015-06-19 18:14:38 +0000743 // Map a nullability property attribute to a context-sensitive keyword
744 // attribute.
745 if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
746 addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
747 OCDS.getNullabilityLoc(),
748 addedToDeclSpec);
749
Benjamin Kramera39beb92014-09-03 11:06:10 +0000750 // Install the property declarator into interfaceDecl.
751 IdentifierInfo *SelName =
752 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
753
754 Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
755 IdentifierInfo *SetterName = OCDS.getSetterName();
756 Selector SetterSel;
757 if (SetterName)
758 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
759 else
760 SetterSel = SelectorTable::constructSetterSelector(
761 PP.getIdentifierTable(), PP.getSelectorTable(),
762 FD.D.getIdentifier());
Benjamin Kramera39beb92014-09-03 11:06:10 +0000763 Decl *Property = Actions.ActOnProperty(
764 getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000765 MethodImplKind);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000766
767 FD.complete(Property);
768 };
John McCallcfefb6d2009-11-03 02:38:08 +0000769
Chris Lattner038a3e32008-10-20 05:46:22 +0000770 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +0000771 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +0000772 ParseStructDeclaration(DS, ObjCPropertyCallback);
Mike Stump11289f42009-09-09 15:08:12 +0000773
John McCall405988b2011-03-26 01:53:26 +0000774 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000775 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000776 }
Steve Naroff99264b42007-08-22 16:35:03 +0000777 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000778
779 // We break out of the big loop in two cases: when we see @end or when we see
780 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000781 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000782 Actions.CodeCompleteObjCAtDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000783 return cutOffParsing();
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000784 } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
Chris Lattnerda9fb152008-10-20 06:10:06 +0000785 ConsumeToken(); // the "end" identifier
Erik Verbruggenc6c8d932011-12-06 09:25:23 +0000786 } else {
787 Diag(Tok, diag::err_objc_missing_end)
788 << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
789 Diag(CDecl->getLocStart(), diag::note_objc_container_start)
790 << (int) Actions.getObjCContainerKind();
791 AtEnd.setBegin(Tok.getLocation());
792 AtEnd.setEnd(Tok.getLocation());
793 }
Mike Stump11289f42009-09-09 15:08:12 +0000794
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000795 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000796 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +0000797 Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
Steve Naroff99264b42007-08-22 16:35:03 +0000798}
799
Douglas Gregor813a0662015-06-19 18:14:38 +0000800/// Diagnose redundant or conflicting nullability information.
801static void diagnoseRedundantPropertyNullability(Parser &P,
802 ObjCDeclSpec &DS,
803 NullabilityKind nullability,
804 SourceLocation nullabilityLoc){
805 if (DS.getNullability() == nullability) {
806 P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000807 << DiagNullabilityKind(nullability, true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000808 << SourceRange(DS.getNullabilityLoc());
809 return;
810 }
811
812 P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000813 << DiagNullabilityKind(nullability, true)
814 << DiagNullabilityKind(DS.getNullability(), true)
Douglas Gregor813a0662015-06-19 18:14:38 +0000815 << SourceRange(DS.getNullabilityLoc());
816}
817
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000818/// Parse property attribute declarations.
819///
820/// property-attr-decl: '(' property-attrlist ')'
821/// property-attrlist:
822/// property-attribute
823/// property-attrlist ',' property-attribute
824/// property-attribute:
825/// getter '=' identifier
826/// setter '=' identifier ':'
827/// readonly
828/// readwrite
829/// assign
830/// retain
831/// copy
832/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000833/// atomic
834/// strong
835/// weak
836/// unsafe_unretained
Douglas Gregor813a0662015-06-19 18:14:38 +0000837/// nonnull
838/// nullable
839/// null_unspecified
Douglas Gregor849ebc22015-06-19 18:14:46 +0000840/// null_resettable
Manman Ren387ff7f2016-01-26 18:52:43 +0000841/// class
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000842///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000843void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000844 assert(Tok.getKind() == tok::l_paren);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000845 BalancedDelimiterTracker T(*this, tok::l_paren);
846 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +0000847
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000848 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000849 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000850 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000851 return cutOffParsing();
Steve Naroff936354c2009-10-08 21:55:05 +0000852 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000853 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000854
Chris Lattner76619232008-10-20 07:22:18 +0000855 // If this is not an identifier at all, bail out early.
Craig Topper161e4db2014-05-21 06:02:52 +0000856 if (!II) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000857 T.consumeClose();
Chris Lattner76619232008-10-20 07:22:18 +0000858 return;
859 }
Mike Stump11289f42009-09-09 15:08:12 +0000860
Chris Lattner1db33542008-10-20 07:37:22 +0000861 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000862
Chris Lattner68e48682008-11-20 04:42:34 +0000863 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000864 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000865 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000866 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000867 else if (II->isStr("unsafe_unretained"))
868 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000869 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000870 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000871 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000872 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000873 else if (II->isStr("strong"))
874 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000875 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000876 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000877 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000878 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000879 else if (II->isStr("atomic"))
880 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000881 else if (II->isStr("weak"))
882 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000883 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000884 bool IsSetter = II->getNameStart()[0] == 's';
885
Chris Lattner825bca12008-10-20 07:39:53 +0000886 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000887 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
Craig Topper3110a5c2015-11-14 18:16:02 +0000888 diag::err_objc_expected_equal_for_getter;
Anders Carlssonfe15a782010-10-02 17:45:21 +0000889
Alp Toker383d2c42014-01-01 03:08:43 +0000890 if (ExpectAndConsume(tok::equal, DiagID)) {
891 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner43c76c32008-10-20 07:00:43 +0000892 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000893 }
Mike Stump11289f42009-09-09 15:08:12 +0000894
Douglas Gregorc8537c52009-11-19 07:41:15 +0000895 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000896 if (IsSetter)
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000897 Actions.CodeCompleteObjCPropertySetter(getCurScope());
Douglas Gregorc8537c52009-11-19 07:41:15 +0000898 else
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000899 Actions.CodeCompleteObjCPropertyGetter(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000900 return cutOffParsing();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000901 }
902
Anders Carlssonfe15a782010-10-02 17:45:21 +0000903 SourceLocation SelLoc;
904 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
905
906 if (!SelIdent) {
907 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
908 << IsSetter;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000909 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000910 return;
911 }
Mike Stump11289f42009-09-09 15:08:12 +0000912
Anders Carlssonfe15a782010-10-02 17:45:21 +0000913 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000914 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000915 DS.setSetterName(SelIdent, SelLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000916
Alp Toker383d2c42014-01-01 03:08:43 +0000917 if (ExpectAndConsume(tok::colon,
918 diag::err_expected_colon_after_setter_name)) {
919 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000920 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000921 }
Chris Lattnerbeca7702008-10-20 07:24:39 +0000922 } else {
923 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000924 DS.setGetterName(SelIdent, SelLoc);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000925 }
Douglas Gregor813a0662015-06-19 18:14:38 +0000926 } else if (II->isStr("nonnull")) {
927 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
928 diagnoseRedundantPropertyNullability(*this, DS,
929 NullabilityKind::NonNull,
930 Tok.getLocation());
931 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
932 DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
933 } else if (II->isStr("nullable")) {
934 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
935 diagnoseRedundantPropertyNullability(*this, DS,
936 NullabilityKind::Nullable,
937 Tok.getLocation());
938 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
939 DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
940 } else if (II->isStr("null_unspecified")) {
941 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
942 diagnoseRedundantPropertyNullability(*this, DS,
943 NullabilityKind::Unspecified,
944 Tok.getLocation());
945 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
946 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000947 } else if (II->isStr("null_resettable")) {
948 if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
949 diagnoseRedundantPropertyNullability(*this, DS,
950 NullabilityKind::Unspecified,
951 Tok.getLocation());
952 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
953 DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
954
955 // Also set the null_resettable bit.
956 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
Manman Ren387ff7f2016-01-26 18:52:43 +0000957 } else if (II->isStr("class")) {
958 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_class);
Chris Lattner825bca12008-10-20 07:39:53 +0000959 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000960 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000961 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000962 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000963 }
Mike Stump11289f42009-09-09 15:08:12 +0000964
Chris Lattner1db33542008-10-20 07:37:22 +0000965 if (Tok.isNot(tok::comma))
966 break;
Mike Stump11289f42009-09-09 15:08:12 +0000967
Chris Lattner1db33542008-10-20 07:37:22 +0000968 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000969 }
Mike Stump11289f42009-09-09 15:08:12 +0000970
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000971 T.consumeClose();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000972}
973
Steve Naroff09bf8152007-09-06 21:24:23 +0000974/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000975/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000976/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000977///
978/// objc-instance-method: '-'
979/// objc-class-method: '+'
980///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000981/// objc-method-attributes: [OBJC2]
982/// __attribute__((deprecated))
983///
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000984Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000985 bool MethodDefinition) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000986 assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000987
Mike Stump11289f42009-09-09 15:08:12 +0000988 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000989 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000990 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000991 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000992 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000993 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000994 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000995}
996
997/// objc-selector:
998/// identifier
999/// one of
1000/// enum struct union if else while do for switch case default
1001/// break continue return goto asm sizeof typeof __alignof
1002/// unsigned long const short volatile signed restrict _Complex
1003/// in out inout bycopy byref oneway int char float double void _Bool
1004///
Chris Lattner4f472a32009-04-11 18:13:45 +00001005IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +00001006
Chris Lattner5700fab2007-10-07 02:00:24 +00001007 switch (Tok.getKind()) {
1008 default:
Craig Topper161e4db2014-05-21 06:02:52 +00001009 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001010 case tok::ampamp:
1011 case tok::ampequal:
1012 case tok::amp:
1013 case tok::pipe:
1014 case tok::tilde:
1015 case tok::exclaim:
1016 case tok::exclaimequal:
1017 case tok::pipepipe:
1018 case tok::pipeequal:
1019 case tok::caret:
1020 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +00001021 std::string ThisTok(PP.getSpelling(Tok));
Jordan Rosea7d03842013-02-08 22:30:41 +00001022 if (isLetter(ThisTok[0])) {
Malcolm Parsons731ca0e2016-11-03 12:25:51 +00001023 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok);
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001024 Tok.setKind(tok::identifier);
1025 SelectorLoc = ConsumeToken();
1026 return II;
1027 }
Craig Topper161e4db2014-05-21 06:02:52 +00001028 return nullptr;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +00001029 }
1030
Chris Lattner5700fab2007-10-07 02:00:24 +00001031 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001032 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +00001033 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001034 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001035 case tok::kw_break:
1036 case tok::kw_case:
1037 case tok::kw_catch:
1038 case tok::kw_char:
1039 case tok::kw_class:
1040 case tok::kw_const:
1041 case tok::kw_const_cast:
1042 case tok::kw_continue:
1043 case tok::kw_default:
1044 case tok::kw_delete:
1045 case tok::kw_do:
1046 case tok::kw_double:
1047 case tok::kw_dynamic_cast:
1048 case tok::kw_else:
1049 case tok::kw_enum:
1050 case tok::kw_explicit:
1051 case tok::kw_export:
1052 case tok::kw_extern:
1053 case tok::kw_false:
1054 case tok::kw_float:
1055 case tok::kw_for:
1056 case tok::kw_friend:
1057 case tok::kw_goto:
1058 case tok::kw_if:
1059 case tok::kw_inline:
1060 case tok::kw_int:
1061 case tok::kw_long:
1062 case tok::kw_mutable:
1063 case tok::kw_namespace:
1064 case tok::kw_new:
1065 case tok::kw_operator:
1066 case tok::kw_private:
1067 case tok::kw_protected:
1068 case tok::kw_public:
1069 case tok::kw_register:
1070 case tok::kw_reinterpret_cast:
1071 case tok::kw_restrict:
1072 case tok::kw_return:
1073 case tok::kw_short:
1074 case tok::kw_signed:
1075 case tok::kw_sizeof:
1076 case tok::kw_static:
1077 case tok::kw_static_cast:
1078 case tok::kw_struct:
1079 case tok::kw_switch:
1080 case tok::kw_template:
1081 case tok::kw_this:
1082 case tok::kw_throw:
1083 case tok::kw_true:
1084 case tok::kw_try:
1085 case tok::kw_typedef:
1086 case tok::kw_typeid:
1087 case tok::kw_typename:
1088 case tok::kw_typeof:
1089 case tok::kw_union:
1090 case tok::kw_unsigned:
1091 case tok::kw_using:
1092 case tok::kw_virtual:
1093 case tok::kw_void:
1094 case tok::kw_volatile:
1095 case tok::kw_wchar_t:
1096 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +00001097 case tok::kw__Bool:
1098 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +00001099 case tok::kw___alignof:
Richard Smithe301ba22015-11-11 02:02:15 +00001100 case tok::kw___auto_type:
Chris Lattner5700fab2007-10-07 02:00:24 +00001101 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001102 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +00001103 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +00001104 }
Steve Naroff99264b42007-08-22 16:35:03 +00001105}
1106
Fariborz Jahanian83615522008-01-02 22:54:34 +00001107/// objc-for-collection-in: 'in'
1108///
Fariborz Jahanian3622e592008-01-04 23:04:08 +00001109bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001110 // FIXME: May have to do additional look-ahead to only allow for
1111 // valid tokens following an 'in'; such as an identifier, unary operators,
1112 // '[' etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001113 return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +00001114 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +00001115}
1116
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001117/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +00001118/// qualifier list and builds their bitmask representation in the input
1119/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +00001120///
1121/// objc-type-qualifiers:
1122/// objc-type-qualifier
1123/// objc-type-qualifiers objc-type-qualifier
1124///
Douglas Gregor813a0662015-06-19 18:14:38 +00001125/// objc-type-qualifier:
1126/// 'in'
1127/// 'out'
1128/// 'inout'
1129/// 'oneway'
1130/// 'bycopy'
1131/// 'byref'
1132/// 'nonnull'
1133/// 'nullable'
1134/// 'null_unspecified'
1135///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001136void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001137 Declarator::TheContext Context) {
1138 assert(Context == Declarator::ObjCParameterContext ||
1139 Context == Declarator::ObjCResultContext);
1140
Chris Lattner61511e12007-12-12 06:56:32 +00001141 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +00001142 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +00001143 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
John McCalla55902b2011-10-01 09:56:14 +00001144 Context == Declarator::ObjCParameterContext);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001145 return cutOffParsing();
Douglas Gregor99fa2642010-08-24 01:06:58 +00001146 }
1147
Chris Lattner5e530bc2007-12-27 19:57:00 +00001148 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +00001149 return;
Mike Stump11289f42009-09-09 15:08:12 +00001150
Chris Lattner61511e12007-12-12 06:56:32 +00001151 const IdentifierInfo *II = Tok.getIdentifierInfo();
1152 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001153 if (II != ObjCTypeQuals[i] ||
1154 NextToken().is(tok::less) ||
1155 NextToken().is(tok::coloncolon))
Chris Lattner61511e12007-12-12 06:56:32 +00001156 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001157
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001158 ObjCDeclSpec::ObjCDeclQualifier Qual;
Douglas Gregor813a0662015-06-19 18:14:38 +00001159 NullabilityKind Nullability;
Chris Lattner61511e12007-12-12 06:56:32 +00001160 switch (i) {
David Blaikie83d382b2011-09-23 05:06:16 +00001161 default: llvm_unreachable("Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001162 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
1163 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
1164 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
1165 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
1166 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
1167 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Douglas Gregor813a0662015-06-19 18:14:38 +00001168
1169 case objc_nonnull:
1170 Qual = ObjCDeclSpec::DQ_CSNullability;
1171 Nullability = NullabilityKind::NonNull;
1172 break;
1173
1174 case objc_nullable:
1175 Qual = ObjCDeclSpec::DQ_CSNullability;
1176 Nullability = NullabilityKind::Nullable;
1177 break;
1178
1179 case objc_null_unspecified:
1180 Qual = ObjCDeclSpec::DQ_CSNullability;
1181 Nullability = NullabilityKind::Unspecified;
1182 break;
Chris Lattner61511e12007-12-12 06:56:32 +00001183 }
Douglas Gregor813a0662015-06-19 18:14:38 +00001184
1185 // FIXME: Diagnose redundant specifiers.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001186 DS.setObjCDeclQualifier(Qual);
Douglas Gregor813a0662015-06-19 18:14:38 +00001187 if (Qual == ObjCDeclSpec::DQ_CSNullability)
1188 DS.setNullability(Tok.getLocation(), Nullability);
1189
Chris Lattner61511e12007-12-12 06:56:32 +00001190 ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001191 II = nullptr;
Chris Lattner61511e12007-12-12 06:56:32 +00001192 break;
1193 }
Mike Stump11289f42009-09-09 15:08:12 +00001194
Chris Lattner61511e12007-12-12 06:56:32 +00001195 // If this wasn't a recognized qualifier, bail out.
1196 if (II) return;
1197 }
1198}
1199
John McCalla55902b2011-10-01 09:56:14 +00001200/// Take all the decl attributes out of the given list and add
1201/// them to the given attribute set.
1202static void takeDeclAttributes(ParsedAttributes &attrs,
1203 AttributeList *list) {
1204 while (list) {
1205 AttributeList *cur = list;
1206 list = cur->getNext();
1207
1208 if (!cur->isUsedAsTypeAttr()) {
1209 // Clear out the next pointer. We're really completely
1210 // destroying the internal invariants of the declarator here,
1211 // but it doesn't matter because we're done with it.
Craig Topper161e4db2014-05-21 06:02:52 +00001212 cur->setNext(nullptr);
John McCalla55902b2011-10-01 09:56:14 +00001213 attrs.add(cur);
1214 }
1215 }
1216}
1217
1218/// takeDeclAttributes - Take all the decl attributes from the given
1219/// declarator and add them to the given list.
1220static void takeDeclAttributes(ParsedAttributes &attrs,
1221 Declarator &D) {
1222 // First, take ownership of all attributes.
1223 attrs.getPool().takeAllFrom(D.getAttributePool());
1224 attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
1225
1226 // Now actually move the attributes over.
1227 takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
1228 takeDeclAttributes(attrs, D.getAttributes());
1229 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
1230 takeDeclAttributes(attrs,
1231 const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
1232}
1233
Chris Lattner61511e12007-12-12 06:56:32 +00001234/// objc-type-name:
1235/// '(' objc-type-qualifiers[opt] type-name ')'
1236/// '(' objc-type-qualifiers[opt] ')'
1237///
Douglas Gregor95d3e372011-03-08 19:17:54 +00001238ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
John McCalla55902b2011-10-01 09:56:14 +00001239 Declarator::TheContext context,
1240 ParsedAttributes *paramAttrs) {
1241 assert(context == Declarator::ObjCParameterContext ||
1242 context == Declarator::ObjCResultContext);
Craig Topper161e4db2014-05-21 06:02:52 +00001243 assert((paramAttrs != nullptr) ==
1244 (context == Declarator::ObjCParameterContext));
John McCalla55902b2011-10-01 09:56:14 +00001245
Chris Lattner0ef13522007-10-09 17:51:17 +00001246 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +00001247
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001248 BalancedDelimiterTracker T(*this, tok::l_paren);
1249 T.consumeOpen();
1250
Chris Lattner2ebb1782008-08-23 01:48:03 +00001251 SourceLocation TypeStartLoc = Tok.getLocation();
Fariborz Jahanian4bf82622011-08-22 17:59:19 +00001252 ObjCDeclContextSwitch ObjCDC(*this);
1253
Fariborz Jahaniand822d682007-10-31 21:59:43 +00001254 // Parse type qualifiers, in, inout, etc.
John McCalla55902b2011-10-01 09:56:14 +00001255 ParseObjCTypeQualifierList(DS, context);
Steve Naroff7e901fd2007-08-22 23:18:22 +00001256
John McCallba7bf592010-08-24 05:47:05 +00001257 ParsedType Ty;
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001258 if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
John McCalla55902b2011-10-01 09:56:14 +00001259 // Parse an abstract declarator.
1260 DeclSpec declSpec(AttrFactory);
1261 declSpec.setObjCQualifiers(&DS);
Douglas Gregor5c0870a2015-06-19 23:18:00 +00001262 DeclSpecContext dsContext = DSC_normal;
1263 if (context == Declarator::ObjCResultContext)
1264 dsContext = DSC_objc_method_result;
1265 ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
John McCalla55902b2011-10-01 09:56:14 +00001266 Declarator declarator(declSpec, context);
1267 ParseDeclarator(declarator);
1268
1269 // If that's not invalid, extract a type.
1270 if (!declarator.isInvalidType()) {
Douglas Gregor813a0662015-06-19 18:14:38 +00001271 // Map a nullability specifier to a context-sensitive keyword attribute.
1272 bool addedToDeclSpec = false;
1273 if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1274 addContextSensitiveTypeNullability(*this, declarator,
1275 DS.getNullability(),
1276 DS.getNullabilityLoc(),
1277 addedToDeclSpec);
1278
John McCalla55902b2011-10-01 09:56:14 +00001279 TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1280 if (!type.isInvalid())
1281 Ty = type.get();
1282
1283 // If we're parsing a parameter, steal all the decl attributes
1284 // and add them to the decl spec.
1285 if (context == Declarator::ObjCParameterContext)
1286 takeDeclAttributes(*paramAttrs, declarator);
1287 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001288 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00001289
Steve Naroff90255b42008-10-21 14:15:04 +00001290 if (Tok.is(tok::r_paren))
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001291 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001292 else if (Tok.getLocation() == TypeStartLoc) {
1293 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +00001294 Diag(Tok, diag::err_expected_type);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001295 SkipUntil(tok::r_paren, StopAtSemi);
Chris Lattnerb7954432008-10-22 03:52:06 +00001296 } else {
1297 // Otherwise, we found *something*, but didn't get a ')' in the right
1298 // place. Emit an error then return what we have as the type.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001299 T.consumeClose();
Chris Lattnerb7954432008-10-22 03:52:06 +00001300 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001301 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +00001302}
1303
1304/// objc-method-decl:
1305/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001306/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001307/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +00001308/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001309///
1310/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +00001311/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +00001312/// objc-keyword-selector objc-keyword-decl
1313///
1314/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001315/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1316/// objc-selector ':' objc-keyword-attributes[opt] identifier
1317/// ':' objc-type-name objc-keyword-attributes[opt] identifier
1318/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +00001319///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001320/// objc-parmlist:
1321/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +00001322///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001323/// objc-parms:
1324/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +00001325///
Steve Narofff1bc45b2007-08-22 18:35:33 +00001326/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +00001327/// , ...
1328///
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001329/// objc-keyword-attributes: [OBJC2]
1330/// __attribute__((unused))
1331///
John McCall48871652010-08-21 09:40:31 +00001332Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001333 tok::TokenKind mType,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001334 tok::ObjCKeywordKind MethodImplKind,
1335 bool MethodDefinition) {
John McCall2ec85372012-05-07 06:16:41 +00001336 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCall28a6aea2009-11-04 02:18:39 +00001337
Douglas Gregor636a61e2010-04-07 00:21:17 +00001338 if (Tok.is(tok::code_completion)) {
David Blaikieefdccaa2016-01-15 23:43:34 +00001339 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
1340 /*ReturnType=*/nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001341 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001342 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001343 }
1344
Chris Lattner2ebb1782008-08-23 01:48:03 +00001345 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +00001346 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001347 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +00001348 if (Tok.is(tok::l_paren))
Craig Topper161e4db2014-05-21 06:02:52 +00001349 ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext,
1350 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001351
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001352 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001353 ParsedAttributes methodAttrs(AttrFactory);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001354 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001355 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001356
Douglas Gregor636a61e2010-04-07 00:21:17 +00001357 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001358 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001359 ReturnType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001360 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001361 return nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00001362 }
1363
Ted Kremenek66f2d6b2010-02-18 23:05:16 +00001364 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +00001365 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001366 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +00001367
Steve Naroff7a54c0d2009-02-11 20:43:13 +00001368 // An unnamed colon is valid.
1369 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +00001370 Diag(Tok, diag::err_expected_selector_for_method)
1371 << SourceRange(mLoc, Tok.getLocation());
Fariborz Jahaniana5fc75f2012-07-26 17:32:28 +00001372 // Skip until we get a ; or @.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001373 SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001374 return nullptr;
Chris Lattner2ebb1782008-08-23 01:48:03 +00001375 }
Mike Stump11289f42009-09-09 15:08:12 +00001376
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001377 SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +00001378 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001379 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001380 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001381 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00001382
Chris Lattner5700fab2007-10-07 02:00:24 +00001383 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +00001384 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001385 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001386 mType, DSRet, ReturnType,
Craig Topper161e4db2014-05-21 06:02:52 +00001387 selLoc, Sel, nullptr,
Fariborz Jahanian60462092010-04-08 00:30:06 +00001388 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001389 methodAttrs.getList(), MethodImplKind,
1390 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +00001391 PD.complete(Result);
1392 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +00001393 }
Steve Naroffca85d1d2007-09-05 23:30:30 +00001394
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001395 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001396 SmallVector<SourceLocation, 12> KeyLocs;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001397 SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Richard Smithe233fbf2013-01-28 22:42:45 +00001398 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1399 Scope::FunctionDeclarationScope | Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +00001400
1401 AttributePool allParamAttrs(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001402 while (1) {
John McCall084e83d2011-03-24 11:26:52 +00001403 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +00001404 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001405
Chris Lattner5700fab2007-10-07 02:00:24 +00001406 // Each iteration parses a single keyword argument.
Alp Toker383d2c42014-01-01 03:08:43 +00001407 if (ExpectAndConsume(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +00001408 break;
Mike Stump11289f42009-09-09 15:08:12 +00001409
David Blaikieefdccaa2016-01-15 23:43:34 +00001410 ArgInfo.Type = nullptr;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001411 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
John McCalla55902b2011-10-01 09:56:14 +00001412 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1413 Declarator::ObjCParameterContext,
1414 &paramAttrs);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001415
Chris Lattner5700fab2007-10-07 02:00:24 +00001416 // If attributes exist before the argument name, parse them.
John McCalla55902b2011-10-01 09:56:14 +00001417 // Regardless, collect all the attributes we've parsed so far.
Craig Topper161e4db2014-05-21 06:02:52 +00001418 ArgInfo.ArgAttrs = nullptr;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001419 if (getLangOpts().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +00001420 MaybeParseGNUAttributes(paramAttrs);
1421 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +00001422 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001423
Douglas Gregor45879692010-07-08 23:37:41 +00001424 // Code completion for the next piece of the selector.
1425 if (Tok.is(tok::code_completion)) {
Douglas Gregor45879692010-07-08 23:37:41 +00001426 KeyIdents.push_back(SelIdent);
1427 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1428 mType == tok::minus,
1429 /*AtParameterName=*/true,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001430 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001431 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001432 return nullptr;
Douglas Gregor45879692010-07-08 23:37:41 +00001433 }
Alex Lorenzf1278212017-04-11 15:01:53 +00001434
1435 if (expectIdentifier())
1436 break; // missing argument name.
Mike Stump11289f42009-09-09 15:08:12 +00001437
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001438 ArgInfo.Name = Tok.getIdentifierInfo();
1439 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +00001440 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001441
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001442 ArgInfos.push_back(ArgInfo);
1443 KeyIdents.push_back(SelIdent);
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001444 KeyLocs.push_back(selLoc);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00001445
John McCall084e83d2011-03-24 11:26:52 +00001446 // Make sure the attributes persist.
1447 allParamAttrs.takeAllFrom(paramAttrs.getPool());
1448
Douglas Gregor95887f92010-07-08 23:20:03 +00001449 // Code completion for the next piece of the selector.
1450 if (Tok.is(tok::code_completion)) {
Douglas Gregor95887f92010-07-08 23:20:03 +00001451 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1452 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +00001453 /*AtParameterName=*/false,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00001454 ReturnType, KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001455 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001456 return nullptr;
Douglas Gregor95887f92010-07-08 23:20:03 +00001457 }
1458
Chris Lattner5700fab2007-10-07 02:00:24 +00001459 // Check for another keyword selector.
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001460 SelIdent = ParseObjCSelectorPiece(selLoc);
Ted Kremenek191ffd32012-09-12 16:50:35 +00001461 if (!SelIdent && Tok.isNot(tok::colon))
1462 break;
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001463 if (!SelIdent) {
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001464 SourceLocation ColonLoc = Tok.getLocation();
1465 if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
Fariborz Jahanian84f49842012-09-17 23:09:59 +00001466 Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1467 Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1468 Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
Fariborz Jahanianf4ffdf32012-09-17 19:15:26 +00001469 }
1470 }
Chris Lattner5700fab2007-10-07 02:00:24 +00001471 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +00001472 }
Mike Stump11289f42009-09-09 15:08:12 +00001473
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001474 bool isVariadic = false;
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001475 bool cStyleParamWarned = false;
Chris Lattner5700fab2007-10-07 02:00:24 +00001476 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +00001477 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +00001478 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001479 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +00001480 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +00001481 ConsumeToken();
1482 break;
1483 }
Fariborz Jahanian45337f52012-06-21 18:43:08 +00001484 if (!cStyleParamWarned) {
1485 Diag(Tok, diag::warn_cstyle_param);
1486 cStyleParamWarned = true;
1487 }
John McCall084e83d2011-03-24 11:26:52 +00001488 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +00001489 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001490 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +00001491 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1492 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001493 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +00001494 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +00001495 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1496 ParmDecl.getIdentifierLoc(),
1497 Param,
Craig Topper161e4db2014-05-21 06:02:52 +00001498 nullptr));
Chris Lattner5700fab2007-10-07 02:00:24 +00001499 }
Mike Stump11289f42009-09-09 15:08:12 +00001500
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001501 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001502 // If attributes exist after the method, parse them.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001503 if (getLangOpts().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001504 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001505
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001506 if (KeyIdents.size() == 0)
Craig Topper161e4db2014-05-21 06:02:52 +00001507 return nullptr;
1508
Chris Lattner5700fab2007-10-07 02:00:24 +00001509 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1510 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001511 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001512 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001513 mType, DSRet, ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00001514 KeyLocs, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001515 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001516 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001517 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001518
John McCall28a6aea2009-11-04 02:18:39 +00001519 PD.complete(Result);
1520 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001521}
1522
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001523/// objc-protocol-refs:
1524/// '<' identifier-list '>'
1525///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001526bool Parser::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001527ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1528 SmallVectorImpl<SourceLocation> &ProtocolLocs,
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001529 bool WarnOnDeclarations, bool ForObjCContainer,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001530 SourceLocation &LAngleLoc, SourceLocation &EndLoc,
1531 bool consumeLastToken) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001532 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001533
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001534 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001535
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001536 SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001537
Chris Lattner3bbae002008-07-26 04:03:38 +00001538 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001539 if (Tok.is(tok::code_completion)) {
Craig Topper883dd332015-12-24 23:58:11 +00001540 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001541 cutOffParsing();
1542 return true;
Douglas Gregorbaf69612009-11-18 04:19:12 +00001543 }
1544
Alex Lorenzf1278212017-04-11 15:01:53 +00001545 if (expectIdentifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001546 SkipUntil(tok::greater, StopAtSemi);
Chris Lattner3bbae002008-07-26 04:03:38 +00001547 return true;
1548 }
1549 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1550 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001551 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001552 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001553
Alp Toker383d2c42014-01-01 03:08:43 +00001554 if (!TryConsumeToken(tok::comma))
Chris Lattner3bbae002008-07-26 04:03:38 +00001555 break;
Chris Lattner3bbae002008-07-26 04:03:38 +00001556 }
Mike Stump11289f42009-09-09 15:08:12 +00001557
Chris Lattner3bbae002008-07-26 04:03:38 +00001558 // Consume the '>'.
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001559 if (ParseGreaterThanInTemplateList(EndLoc, consumeLastToken,
Douglas Gregor85f3f952015-07-07 03:57:15 +00001560 /*ObjCGenericList=*/false))
Chris Lattner3bbae002008-07-26 04:03:38 +00001561 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001562
Chris Lattner3bbae002008-07-26 04:03:38 +00001563 // Convert the list of protocols identifiers into a list of protocol decls.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00001564 Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
Craig Toppera9247eb2015-10-22 04:59:56 +00001565 ProtocolIdents, Protocols);
Chris Lattner3bbae002008-07-26 04:03:38 +00001566 return false;
1567}
1568
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001569TypeResult Parser::parseObjCProtocolQualifierType(SourceLocation &rAngleLoc) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001570 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
David Blaikiebbafb8a2012-03-11 07:00:24 +00001571 assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001572
1573 SourceLocation lAngleLoc;
1574 SmallVector<Decl *, 8> protocols;
1575 SmallVector<SourceLocation, 8> protocolLocs;
1576 (void)ParseObjCProtocolReferences(protocols, protocolLocs, false, false,
1577 lAngleLoc, rAngleLoc,
1578 /*consumeLastToken=*/true);
1579 TypeResult result = Actions.actOnObjCProtocolQualifierType(lAngleLoc,
1580 protocols,
1581 protocolLocs,
1582 rAngleLoc);
1583 if (result.isUsable()) {
1584 Diag(lAngleLoc, diag::warn_objc_protocol_qualifier_missing_id)
1585 << FixItHint::CreateInsertion(lAngleLoc, "id")
1586 << SourceRange(lAngleLoc, rAngleLoc);
1587 }
1588
1589 return result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001590}
1591
Douglas Gregore9d95f12015-07-07 03:57:35 +00001592/// Parse Objective-C type arguments or protocol qualifiers.
1593///
1594/// objc-type-arguments:
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001595/// '<' type-name '...'[opt] (',' type-name '...'[opt])* '>'
Douglas Gregore9d95f12015-07-07 03:57:35 +00001596///
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001597void Parser::parseObjCTypeArgsOrProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001598 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001599 SourceLocation &typeArgsLAngleLoc,
1600 SmallVectorImpl<ParsedType> &typeArgs,
1601 SourceLocation &typeArgsRAngleLoc,
1602 SourceLocation &protocolLAngleLoc,
1603 SmallVectorImpl<Decl *> &protocols,
1604 SmallVectorImpl<SourceLocation> &protocolLocs,
1605 SourceLocation &protocolRAngleLoc,
1606 bool consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001607 bool warnOnIncompleteProtocols) {
1608 assert(Tok.is(tok::less) && "Not at the start of type args or protocols");
1609 SourceLocation lAngleLoc = ConsumeToken();
1610
1611 // Whether all of the elements we've parsed thus far are single
1612 // identifiers, which might be types or might be protocols.
1613 bool allSingleIdentifiers = true;
1614 SmallVector<IdentifierInfo *, 4> identifiers;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001615 SmallVectorImpl<SourceLocation> &identifierLocs = protocolLocs;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001616
1617 // Parse a list of comma-separated identifiers, bailing out if we
1618 // see something different.
1619 do {
1620 // Parse a single identifier.
1621 if (Tok.is(tok::identifier) &&
1622 (NextToken().is(tok::comma) ||
1623 NextToken().is(tok::greater) ||
1624 NextToken().is(tok::greatergreater))) {
1625 identifiers.push_back(Tok.getIdentifierInfo());
1626 identifierLocs.push_back(ConsumeToken());
1627 continue;
1628 }
1629
1630 if (Tok.is(tok::code_completion)) {
1631 // FIXME: Also include types here.
1632 SmallVector<IdentifierLocPair, 4> identifierLocPairs;
1633 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1634 identifierLocPairs.push_back(IdentifierLocPair(identifiers[i],
1635 identifierLocs[i]));
1636 }
1637
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001638 QualType BaseT = Actions.GetTypeFromParser(baseType);
1639 if (!BaseT.isNull() && BaseT->acceptsObjCTypeParams()) {
1640 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
1641 } else {
Craig Topper883dd332015-12-24 23:58:11 +00001642 Actions.CodeCompleteObjCProtocolReferences(identifierLocPairs);
Douglas Gregorcedcd9f2015-07-07 06:20:36 +00001643 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001644 cutOffParsing();
1645 return;
1646 }
1647
1648 allSingleIdentifiers = false;
1649 break;
1650 } while (TryConsumeToken(tok::comma));
1651
1652 // If we parsed an identifier list, semantic analysis sorts out
1653 // whether it refers to protocols or to type arguments.
1654 if (allSingleIdentifiers) {
1655 // Parse the closing '>'.
1656 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001657 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001658 /*ObjCGenericList=*/true);
1659
1660 // Let Sema figure out what we parsed.
1661 Actions.actOnObjCTypeArgsOrProtocolQualifiers(getCurScope(),
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001662 baseType,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001663 lAngleLoc,
1664 identifiers,
1665 identifierLocs,
1666 rAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001667 typeArgsLAngleLoc,
1668 typeArgs,
1669 typeArgsRAngleLoc,
1670 protocolLAngleLoc,
1671 protocols,
1672 protocolRAngleLoc,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001673 warnOnIncompleteProtocols);
1674 return;
1675 }
1676
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001677 // We parsed an identifier list but stumbled into non single identifiers, this
1678 // means we might (a) check that what we already parsed is a legitimate type
1679 // (not a protocol or unknown type) and (b) parse the remaining ones, which
1680 // must all be type args.
Douglas Gregore9d95f12015-07-07 03:57:35 +00001681
1682 // Convert the identifiers into type arguments.
1683 bool invalid = false;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001684 IdentifierInfo *foundProtocolId = nullptr, *foundValidTypeId = nullptr;
1685 SourceLocation foundProtocolSrcLoc, foundValidTypeSrcLoc;
1686 SmallVector<IdentifierInfo *, 2> unknownTypeArgs;
1687 SmallVector<SourceLocation, 2> unknownTypeArgsLoc;
1688
Douglas Gregore9d95f12015-07-07 03:57:35 +00001689 for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1690 ParsedType typeArg
1691 = Actions.getTypeName(*identifiers[i], identifierLocs[i], getCurScope());
1692 if (typeArg) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001693 DeclSpec DS(AttrFactory);
1694 const char *prevSpec = nullptr;
1695 unsigned diagID;
1696 DS.SetTypeSpecType(TST_typename, identifierLocs[i], prevSpec, diagID,
1697 typeArg, Actions.getASTContext().getPrintingPolicy());
1698
1699 // Form a declarator to turn this into a type.
1700 Declarator D(DS, Declarator::TypeNameContext);
1701 TypeResult fullTypeArg = Actions.ActOnTypeName(getCurScope(), D);
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001702 if (fullTypeArg.isUsable()) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001703 typeArgs.push_back(fullTypeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001704 if (!foundValidTypeId) {
1705 foundValidTypeId = identifiers[i];
1706 foundValidTypeSrcLoc = identifierLocs[i];
1707 }
1708 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001709 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001710 unknownTypeArgs.push_back(identifiers[i]);
1711 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1712 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001713 } else {
1714 invalid = true;
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001715 if (!Actions.LookupProtocol(identifiers[i], identifierLocs[i])) {
1716 unknownTypeArgs.push_back(identifiers[i]);
1717 unknownTypeArgsLoc.push_back(identifierLocs[i]);
1718 } else if (!foundProtocolId) {
1719 foundProtocolId = identifiers[i];
1720 foundProtocolSrcLoc = identifierLocs[i];
1721 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001722 }
1723 }
1724
1725 // Continue parsing type-names.
1726 do {
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001727 Token CurTypeTok = Tok;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001728 TypeResult typeArg = ParseTypeName();
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001729
1730 // Consume the '...' for a pack expansion.
1731 SourceLocation ellipsisLoc;
1732 TryConsumeToken(tok::ellipsis, ellipsisLoc);
1733 if (typeArg.isUsable() && ellipsisLoc.isValid()) {
1734 typeArg = Actions.ActOnPackExpansion(typeArg.get(), ellipsisLoc);
1735 }
1736
Douglas Gregore9d95f12015-07-07 03:57:35 +00001737 if (typeArg.isUsable()) {
1738 typeArgs.push_back(typeArg.get());
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001739 if (!foundValidTypeId) {
1740 foundValidTypeId = CurTypeTok.getIdentifierInfo();
1741 foundValidTypeSrcLoc = CurTypeTok.getLocation();
1742 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001743 } else {
1744 invalid = true;
1745 }
1746 } while (TryConsumeToken(tok::comma));
1747
Bruno Cardoso Lopesc54768f2016-04-13 20:59:07 +00001748 // Diagnose the mix between type args and protocols.
1749 if (foundProtocolId && foundValidTypeId)
1750 Actions.DiagnoseTypeArgsAndProtocols(foundProtocolId, foundProtocolSrcLoc,
1751 foundValidTypeId,
1752 foundValidTypeSrcLoc);
1753
1754 // Diagnose unknown arg types.
1755 ParsedType T;
1756 if (unknownTypeArgs.size())
1757 for (unsigned i = 0, e = unknownTypeArgsLoc.size(); i < e; ++i)
1758 Actions.DiagnoseUnknownTypeName(unknownTypeArgs[i], unknownTypeArgsLoc[i],
1759 getCurScope(), nullptr, T);
1760
Douglas Gregore9d95f12015-07-07 03:57:35 +00001761 // Parse the closing '>'.
1762 SourceLocation rAngleLoc;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001763 (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
Douglas Gregore9d95f12015-07-07 03:57:35 +00001764 /*ObjCGenericList=*/true);
1765
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001766 if (invalid) {
1767 typeArgs.clear();
Douglas Gregore9d95f12015-07-07 03:57:35 +00001768 return;
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001769 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00001770
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001771 // Record left/right angle locations.
1772 typeArgsLAngleLoc = lAngleLoc;
1773 typeArgsRAngleLoc = rAngleLoc;
Douglas Gregore9d95f12015-07-07 03:57:35 +00001774}
1775
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001776void Parser::parseObjCTypeArgsAndProtocolQualifiers(
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001777 ParsedType baseType,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001778 SourceLocation &typeArgsLAngleLoc,
1779 SmallVectorImpl<ParsedType> &typeArgs,
1780 SourceLocation &typeArgsRAngleLoc,
1781 SourceLocation &protocolLAngleLoc,
1782 SmallVectorImpl<Decl *> &protocols,
1783 SmallVectorImpl<SourceLocation> &protocolLocs,
1784 SourceLocation &protocolRAngleLoc,
1785 bool consumeLastToken) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001786 assert(Tok.is(tok::less));
1787
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001788 // Parse the first angle-bracket-delimited clause.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001789 parseObjCTypeArgsOrProtocolQualifiers(baseType,
1790 typeArgsLAngleLoc,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001791 typeArgs,
1792 typeArgsRAngleLoc,
1793 protocolLAngleLoc,
1794 protocols,
1795 protocolLocs,
1796 protocolRAngleLoc,
1797 consumeLastToken,
Douglas Gregore83b9562015-07-07 03:57:53 +00001798 /*warnOnIncompleteProtocols=*/false);
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00001799 if (Tok.is(tok::eof)) // Nothing else to do here...
1800 return;
Douglas Gregore83b9562015-07-07 03:57:53 +00001801
1802 // An Objective-C object pointer followed by type arguments
1803 // can then be followed again by a set of protocol references, e.g.,
1804 // \c NSArray<NSView><NSTextDelegate>
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001805 if ((consumeLastToken && Tok.is(tok::less)) ||
1806 (!consumeLastToken && NextToken().is(tok::less))) {
1807 // If we aren't consuming the last token, the prior '>' is still hanging
1808 // there. Consume it before we parse the protocol qualifiers.
1809 if (!consumeLastToken)
1810 ConsumeToken();
1811
1812 if (!protocols.empty()) {
1813 SkipUntilFlags skipFlags = SkipUntilFlags();
1814 if (!consumeLastToken)
1815 skipFlags = skipFlags | StopBeforeMatch;
Douglas Gregore83b9562015-07-07 03:57:53 +00001816 Diag(Tok, diag::err_objc_type_args_after_protocols)
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001817 << SourceRange(protocolLAngleLoc, protocolRAngleLoc);
1818 SkipUntil(tok::greater, tok::greatergreater, skipFlags);
Douglas Gregore83b9562015-07-07 03:57:53 +00001819 } else {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001820 ParseObjCProtocolReferences(protocols, protocolLocs,
1821 /*WarnOnDeclarations=*/false,
1822 /*ForObjCContainer=*/false,
1823 protocolLAngleLoc, protocolRAngleLoc,
1824 consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001825 }
1826 }
1827}
1828
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001829TypeResult Parser::parseObjCTypeArgsAndProtocolQualifiers(
1830 SourceLocation loc,
1831 ParsedType type,
1832 bool consumeLastToken,
1833 SourceLocation &endLoc) {
Douglas Gregore83b9562015-07-07 03:57:53 +00001834 assert(Tok.is(tok::less));
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001835 SourceLocation typeArgsLAngleLoc;
1836 SmallVector<ParsedType, 4> typeArgs;
1837 SourceLocation typeArgsRAngleLoc;
1838 SourceLocation protocolLAngleLoc;
1839 SmallVector<Decl *, 4> protocols;
1840 SmallVector<SourceLocation, 4> protocolLocs;
1841 SourceLocation protocolRAngleLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00001842
1843 // Parse type arguments and protocol qualifiers.
Douglas Gregor10dc9d82015-07-07 03:58:28 +00001844 parseObjCTypeArgsAndProtocolQualifiers(type, typeArgsLAngleLoc, typeArgs,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001845 typeArgsRAngleLoc, protocolLAngleLoc,
1846 protocols, protocolLocs,
1847 protocolRAngleLoc, consumeLastToken);
Douglas Gregore83b9562015-07-07 03:57:53 +00001848
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00001849 if (Tok.is(tok::eof))
1850 return true; // Invalid type result.
1851
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00001852 // Compute the location of the last token.
1853 if (consumeLastToken)
1854 endLoc = PrevTokLocation;
1855 else
1856 endLoc = Tok.getLocation();
1857
1858 return Actions.actOnObjCTypeArgsAndProtocolQualifiers(
1859 getCurScope(),
1860 loc,
1861 type,
1862 typeArgsLAngleLoc,
1863 typeArgs,
1864 typeArgsRAngleLoc,
1865 protocolLAngleLoc,
1866 protocols,
1867 protocolLocs,
1868 protocolRAngleLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00001869}
1870
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001871void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1872 BalancedDelimiterTracker &T,
1873 SmallVectorImpl<Decl *> &AllIvarDecls,
1874 bool RBraceMissing) {
1875 if (!RBraceMissing)
1876 T.consumeClose();
1877
1878 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1879 Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1880 Actions.ActOnObjCContainerFinishDefinition();
1881 // Call ActOnFields() even if we don't have any decls. This is useful
1882 // for code rewriting tools that need to be aware of the empty list.
1883 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1884 AllIvarDecls,
Craig Topper161e4db2014-05-21 06:02:52 +00001885 T.getOpenLocation(), T.getCloseLocation(), nullptr);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001886}
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001887
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001888/// objc-class-instance-variables:
1889/// '{' objc-instance-variable-decl-list[opt] '}'
1890///
1891/// objc-instance-variable-decl-list:
1892/// objc-visibility-spec
1893/// objc-instance-variable-decl ';'
1894/// ';'
1895/// objc-instance-variable-decl-list objc-visibility-spec
1896/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1897/// objc-instance-variable-decl-list ';'
1898///
1899/// objc-visibility-spec:
1900/// @private
1901/// @protected
1902/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001903/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001904///
1905/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001906/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001907///
John McCall48871652010-08-21 09:40:31 +00001908void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001909 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001910 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001911 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001912 SmallVector<Decl *, 32> AllIvarDecls;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00001913
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001914 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001915 ObjCDeclContextSwitch ObjCDC(*this);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001916
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001917 BalancedDelimiterTracker T(*this, tok::l_brace);
1918 T.consumeOpen();
Steve Naroff00433d32007-08-21 21:17:12 +00001919 // While we still have something to read, read the instance variables.
Richard Smith34f30512013-11-23 04:06:09 +00001920 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001921 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001922
Steve Naroff00433d32007-08-21 21:17:12 +00001923 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001924 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00001925 ConsumeExtraSemi(InstanceVariableList);
Steve Naroff00433d32007-08-21 21:17:12 +00001926 continue;
1927 }
Mike Stump11289f42009-09-09 15:08:12 +00001928
Steve Naroff00433d32007-08-21 21:17:12 +00001929 // Set the default visibility to private.
Alp Toker383d2c42014-01-01 03:08:43 +00001930 if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
Douglas Gregor48d46252010-01-13 21:54:15 +00001931 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001932 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001933 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001934 }
1935
Steve Naroff7c348172007-08-23 18:16:40 +00001936 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001937 case tok::objc_private:
1938 case tok::objc_public:
1939 case tok::objc_protected:
1940 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001941 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001942 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001943 continue;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001944
1945 case tok::objc_end:
1946 Diag(Tok, diag::err_objc_unexpected_atend);
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001947 Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1948 Tok.setKind(tok::at);
1949 Tok.setLength(1);
1950 PP.EnterToken(Tok);
1951 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1952 T, AllIvarDecls, true);
1953 return;
Fariborz Jahanian0b171932013-03-20 18:45:49 +00001954
1955 default:
1956 Diag(Tok, diag::err_objc_illegal_visibility_spec);
1957 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001958 }
1959 }
Mike Stump11289f42009-09-09 15:08:12 +00001960
Douglas Gregor48d46252010-01-13 21:54:15 +00001961 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001962 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001963 Sema::PCC_ObjCInstanceVariableList);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001964 return cutOffParsing();
Douglas Gregor48d46252010-01-13 21:54:15 +00001965 }
John McCallcfefb6d2009-11-03 02:38:08 +00001966
Benjamin Kramera39beb92014-09-03 11:06:10 +00001967 auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1968 Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1969 // Install the declarator into the interface decl.
Douglas Gregor2a20bd12015-06-19 18:25:57 +00001970 FD.D.setObjCIvar(true);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001971 Decl *Field = Actions.ActOnIvar(
1972 getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1973 FD.BitfieldSize, visibility);
1974 Actions.ActOnObjCContainerFinishDefinition();
1975 if (Field)
1976 AllIvarDecls.push_back(Field);
1977 FD.complete(Field);
1978 };
John McCallcfefb6d2009-11-03 02:38:08 +00001979
Chris Lattnera12405b2008-04-10 06:46:29 +00001980 // Parse all the comma separated declarators.
Eli Friedman89b1f2c2012-08-08 23:04:35 +00001981 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00001982 ParseStructDeclaration(DS, ObjCIvarCallback);
Mike Stump11289f42009-09-09 15:08:12 +00001983
Chris Lattner0ef13522007-10-09 17:51:17 +00001984 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001985 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001986 } else {
1987 Diag(Tok, diag::err_expected_semi_decl_list);
1988 // Skip to end of block or statement
Alexey Bataevee6507d2013-11-18 08:17:37 +00001989 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Steve Naroff00433d32007-08-21 21:17:12 +00001990 }
1991 }
Fariborz Jahanian089f39e2013-03-20 18:09:33 +00001992 HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1993 T, AllIvarDecls, false);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001994}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001995
1996/// objc-protocol-declaration:
1997/// objc-protocol-definition
1998/// objc-protocol-forward-reference
1999///
2000/// objc-protocol-definition:
James Dennett1355bd12012-06-11 06:19:40 +00002001/// \@protocol identifier
Mike Stump11289f42009-09-09 15:08:12 +00002002/// objc-protocol-refs[opt]
2003/// objc-interface-decl-list
James Dennett1355bd12012-06-11 06:19:40 +00002004/// \@end
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002005///
2006/// objc-protocol-forward-reference:
James Dennett1355bd12012-06-11 06:19:40 +00002007/// \@protocol identifier-list ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002008///
James Dennett1355bd12012-06-11 06:19:40 +00002009/// "\@protocol identifier ;" should be resolved as "\@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00002010/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002011/// semicolon in the first alternative if objc-protocol-refs are omitted.
Douglas Gregorf6102672012-01-01 21:23:57 +00002012Parser::DeclGroupPtrTy
2013Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
2014 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00002015 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002016 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
2017 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002018
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002019 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002020 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002021 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002022 return nullptr;
Douglas Gregor5b4671c2009-11-18 04:49:41 +00002023 }
2024
Nico Weber69a79142013-04-04 00:15:10 +00002025 MaybeSkipAttributes(tok::objc_protocol);
Nico Weber04e213b2013-04-03 17:36:11 +00002026
Alex Lorenzf1278212017-04-11 15:01:53 +00002027 if (expectIdentifier())
2028 return nullptr; // missing protocol name.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002029 // Save the protocol name, then consume it.
2030 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
2031 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002032
Alp Toker383d2c42014-01-01 03:08:43 +00002033 if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00002034 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Craig Topper0f723bb2015-10-22 05:00:01 +00002035 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtoInfo,
John McCall53fa7142010-12-24 02:08:15 +00002036 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002037 }
Mike Stump11289f42009-09-09 15:08:12 +00002038
Erik Verbruggenf9887852011-12-08 09:58:43 +00002039 CheckNestedObjCContexts(AtLoc);
2040
Chris Lattner0ef13522007-10-09 17:51:17 +00002041 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002042 SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002043 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
2044
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002045 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002046 while (1) {
2047 ConsumeToken(); // the ','
Alex Lorenzf1278212017-04-11 15:01:53 +00002048 if (expectIdentifier()) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002049 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002050 return nullptr;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002051 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00002052 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
2053 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002054 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00002055
Chris Lattner0ef13522007-10-09 17:51:17 +00002056 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002057 break;
2058 }
2059 // Consume the ';'.
Alp Toker383d2c42014-01-01 03:08:43 +00002060 if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
David Blaikie0403cb12016-01-15 23:43:25 +00002061 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002062
Craig Topper0f723bb2015-10-22 05:00:01 +00002063 return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtocolRefs,
John McCall53fa7142010-12-24 02:08:15 +00002064 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00002065 }
Mike Stump11289f42009-09-09 15:08:12 +00002066
Steve Naroff0b6a01a2007-08-22 22:17:26 +00002067 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00002068 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002069
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002070 SmallVector<Decl *, 8> ProtocolRefs;
2071 SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00002072 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +00002073 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002074 LAngleLoc, EndProtoLoc,
2075 /*consumeLastToken=*/true))
David Blaikie0403cb12016-01-15 23:43:25 +00002076 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002077
John McCall48871652010-08-21 09:40:31 +00002078 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00002079 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002080 ProtocolRefs.data(),
2081 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00002082 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00002083 EndProtoLoc, attrs.getList());
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002084
Fariborz Jahanianb66de9f2011-08-22 21:44:58 +00002085 ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
Douglas Gregorf6102672012-01-01 21:23:57 +00002086 return Actions.ConvertDeclToDeclGroup(ProtoType);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002087}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002088
2089/// objc-implementation:
2090/// objc-class-implementation-prologue
2091/// objc-category-implementation-prologue
2092///
2093/// objc-class-implementation-prologue:
2094/// @implementation identifier objc-superclass[opt]
2095/// objc-class-instance-variables[opt]
2096///
2097/// objc-category-implementation-prologue:
2098/// @implementation identifier ( identifier )
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002099Parser::DeclGroupPtrTy
2100Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002101 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
2102 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002103 CheckNestedObjCContexts(AtLoc);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002104 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00002105
Douglas Gregor49c22a72009-11-18 16:26:39 +00002106 // Code completion after '@implementation'.
2107 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002108 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002109 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002110 return nullptr;
Douglas Gregor49c22a72009-11-18 16:26:39 +00002111 }
2112
Nico Weber69a79142013-04-04 00:15:10 +00002113 MaybeSkipAttributes(tok::objc_implementation);
Nico Weber04e213b2013-04-03 17:36:11 +00002114
Alex Lorenzf1278212017-04-11 15:01:53 +00002115 if (expectIdentifier())
2116 return nullptr; // missing class or category name.
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002117 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00002118 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002119 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Craig Topper161e4db2014-05-21 06:02:52 +00002120 Decl *ObjCImpDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002121
Douglas Gregor85f3f952015-07-07 03:57:15 +00002122 // Neither a type parameter list nor a list of protocol references is
2123 // permitted here. Parse and diagnose them.
2124 if (Tok.is(tok::less)) {
2125 SourceLocation lAngleLoc, rAngleLoc;
2126 SmallVector<IdentifierLocPair, 8> protocolIdents;
2127 SourceLocation diagLoc = Tok.getLocation();
Richard Smith3df3f1d2015-11-03 01:19:56 +00002128 ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
2129 if (parseObjCTypeParamListOrProtocolRefs(typeParamScope, lAngleLoc,
2130 protocolIdents, rAngleLoc)) {
Douglas Gregor85f3f952015-07-07 03:57:15 +00002131 Diag(diagLoc, diag::err_objc_parameterized_implementation)
2132 << SourceRange(diagLoc, PrevTokLocation);
2133 } else if (lAngleLoc.isValid()) {
2134 Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
2135 << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
2136 }
2137 }
2138
Mike Stump11289f42009-09-09 15:08:12 +00002139 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002140 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002141 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002142 SourceLocation categoryLoc, rparenLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002143 IdentifierInfo *categoryId = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002144
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002145 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002146 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002147 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +00002148 return nullptr;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00002149 }
2150
Chris Lattner0ef13522007-10-09 17:51:17 +00002151 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002152 categoryId = Tok.getIdentifierInfo();
2153 categoryLoc = ConsumeToken();
2154 } else {
Alp Tokerec543272013-12-24 09:48:30 +00002155 Diag(Tok, diag::err_expected)
2156 << tok::identifier; // missing category name.
David Blaikie0403cb12016-01-15 23:43:25 +00002157 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002158 }
Chris Lattner0ef13522007-10-09 17:51:17 +00002159 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002160 Diag(Tok, diag::err_expected) << tok::r_paren;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002161 SkipUntil(tok::r_paren); // don't stop at ';'
David Blaikie0403cb12016-01-15 23:43:25 +00002162 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002163 }
2164 rparenLoc = ConsumeParen();
Fariborz Jahanian85888552013-05-17 17:58:11 +00002165 if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2166 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002167 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2168 SmallVector<Decl *, 4> protocols;
2169 SmallVector<SourceLocation, 4> protocolLocs;
2170 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2171 /*warnOnIncompleteProtocols=*/false,
2172 /*ForObjCContainer=*/false,
2173 protocolLAngleLoc, protocolRAngleLoc,
2174 /*consumeLastToken=*/true);
Fariborz Jahanian85888552013-05-17 17:58:11 +00002175 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002176 ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002177 AtLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00002178 categoryLoc);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002179
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002180 } else {
2181 // We have a class implementation
2182 SourceLocation superClassLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00002183 IdentifierInfo *superClassId = nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00002184 if (TryConsumeToken(tok::colon)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002185 // We have a super class
Alex Lorenzf1278212017-04-11 15:01:53 +00002186 if (expectIdentifier())
2187 return nullptr; // missing super class name.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002188 superClassId = Tok.getIdentifierInfo();
2189 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002190 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002191 ObjCImpDecl = Actions.ActOnStartClassImplementation(
2192 AtLoc, nameId, nameLoc,
2193 superClassId, superClassLoc);
2194
2195 if (Tok.is(tok::l_brace)) // we have ivars
2196 ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002197 else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2198 Diag(Tok, diag::err_unexpected_protocol_qualifier);
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002199
2200 SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2201 SmallVector<Decl *, 4> protocols;
2202 SmallVector<SourceLocation, 4> protocolLocs;
2203 (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2204 /*warnOnIncompleteProtocols=*/false,
2205 /*ForObjCContainer=*/false,
2206 protocolLAngleLoc, protocolRAngleLoc,
2207 /*consumeLastToken=*/true);
Fariborz Jahanian46ed4d92013-04-24 23:23:47 +00002208 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002209 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002210 assert(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002211
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002212 SmallVector<Decl *, 8> DeclsInGroup;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002213
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002214 {
2215 ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
Richard Smith34f30512013-11-23 04:06:09 +00002216 while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002217 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002218 MaybeParseCXX11Attributes(attrs);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002219 if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
2220 DeclGroupRef DG = DGP.get();
2221 DeclsInGroup.append(DG.begin(), DG.end());
2222 }
2223 }
2224 }
2225
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00002226 return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002227}
Steve Naroff33a1e802007-10-29 21:38:07 +00002228
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002229Parser::DeclGroupPtrTy
2230Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002231 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
2232 "ParseObjCAtEndDeclaration(): Expected @end");
2233 ConsumeToken(); // the "end" identifier
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002234 if (CurParsedObjCImpl)
2235 CurParsedObjCImpl->finish(atEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002236 else
Ted Kremenekc7c64312010-01-07 01:20:12 +00002237 // missing @implementation
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002238 Diag(atEnd.getBegin(), diag::err_expected_objc_container);
David Blaikie0403cb12016-01-15 23:43:25 +00002239 return nullptr;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00002240}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002241
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002242Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
2243 if (!Finished) {
2244 finish(P.Tok.getLocation());
Richard Smith34f30512013-11-23 04:06:09 +00002245 if (P.isEofOrEom()) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002246 P.Diag(P.Tok, diag::err_objc_missing_end)
2247 << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
2248 P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
2249 << Sema::OCK_Implementation;
2250 }
2251 }
Craig Topper161e4db2014-05-21 06:02:52 +00002252 P.CurParsedObjCImpl = nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002253 assert(LateParsedObjCMethods.empty());
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002254}
2255
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002256void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
2257 assert(!Finished);
Alex Lorenz6c9af502017-07-03 10:12:24 +00002258 P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl, AtEnd.getBegin());
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002259 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002260 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2261 true/*Methods*/);
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002262
2263 P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
2264
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002265 if (HasCFunction)
2266 for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
2267 P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2268 false/*c-functions*/);
2269
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002270 /// \brief Clear and free the cached objc methods.
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002271 for (LateParsedObjCMethodContainer::iterator
2272 I = LateParsedObjCMethods.begin(),
2273 E = LateParsedObjCMethods.end(); I != E; ++I)
2274 delete *I;
2275 LateParsedObjCMethods.clear();
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002276
2277 Finished = true;
Argyrios Kyrtzidis004685b2011-11-29 08:14:54 +00002278}
2279
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002280/// compatibility-alias-decl:
2281/// @compatibility_alias alias-name class-name ';'
2282///
John McCall48871652010-08-21 09:40:31 +00002283Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00002284 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
2285 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
2286 ConsumeToken(); // consume compatibility_alias
Alex Lorenzf1278212017-04-11 15:01:53 +00002287 if (expectIdentifier())
Craig Topper161e4db2014-05-21 06:02:52 +00002288 return nullptr;
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002289 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
2290 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Alex Lorenzf1278212017-04-11 15:01:53 +00002291 if (expectIdentifier())
Craig Topper161e4db2014-05-21 06:02:52 +00002292 return nullptr;
Fariborz Jahanian49c64252007-10-11 23:42:27 +00002293 IdentifierInfo *classId = Tok.getIdentifierInfo();
2294 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Alp Toker383d2c42014-01-01 03:08:43 +00002295 ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
Richard Smithac4e36d2012-08-08 23:32:13 +00002296 return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
2297 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002298}
2299
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002300/// property-synthesis:
2301/// @synthesize property-ivar-list ';'
2302///
2303/// property-ivar-list:
2304/// property-ivar
2305/// property-ivar-list ',' property-ivar
2306///
2307/// property-ivar:
2308/// identifier
2309/// identifier '=' identifier
2310///
John McCall48871652010-08-21 09:40:31 +00002311Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002312 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
Fariborz Jahaniand56a2622013-04-29 15:35:35 +00002313 "ParseObjCPropertySynthesize(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002314 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00002315
Douglas Gregor88e72a02009-11-18 19:45:45 +00002316 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00002317 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002318 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002319 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002320 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002321 }
2322
Douglas Gregor88e72a02009-11-18 19:45:45 +00002323 if (Tok.isNot(tok::identifier)) {
2324 Diag(Tok, diag::err_synthesized_property_name);
2325 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002326 return nullptr;
Douglas Gregor88e72a02009-11-18 19:45:45 +00002327 }
Craig Topper161e4db2014-05-21 06:02:52 +00002328
2329 IdentifierInfo *propertyIvar = nullptr;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002330 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2331 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002332 SourceLocation propertyIvarLoc;
Alp Toker383d2c42014-01-01 03:08:43 +00002333 if (TryConsumeToken(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002334 // property '=' ivar-name
Douglas Gregor5d649882009-11-18 22:32:06 +00002335 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002336 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002337 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002338 return nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00002339 }
Alex Lorenzf1278212017-04-11 15:01:53 +00002340
2341 if (expectIdentifier())
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002342 break;
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00002343 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00002344 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002345 }
Manman Ren5b786402016-01-28 18:49:28 +00002346 Actions.ActOnPropertyImplDecl(
2347 getCurScope(), atLoc, propertyLoc, true,
2348 propertyId, propertyIvar, propertyIvarLoc,
2349 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Chris Lattner0ef13522007-10-09 17:51:17 +00002350 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002351 break;
2352 ConsumeToken(); // consume ','
2353 }
Alp Toker383d2c42014-01-01 03:08:43 +00002354 ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
Craig Topper161e4db2014-05-21 06:02:52 +00002355 return nullptr;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002356}
2357
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002358/// property-dynamic:
2359/// @dynamic property-list
2360///
2361/// property-list:
2362/// identifier
2363/// property-list ',' identifier
2364///
John McCall48871652010-08-21 09:40:31 +00002365Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002366 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2367 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00002368 ConsumeToken(); // consume dynamic
Manman Ren0fe61f82016-01-29 19:05:57 +00002369
2370 bool isClassProperty = false;
2371 if (Tok.is(tok::l_paren)) {
2372 ConsumeParen();
2373 const IdentifierInfo *II = Tok.getIdentifierInfo();
2374
2375 if (!II) {
2376 Diag(Tok, diag::err_objc_expected_property_attr) << II;
2377 SkipUntil(tok::r_paren, StopAtSemi);
2378 } else {
2379 SourceLocation AttrName = ConsumeToken(); // consume attribute name
2380 if (II->isStr("class")) {
2381 isClassProperty = true;
2382 if (Tok.isNot(tok::r_paren)) {
2383 Diag(Tok, diag::err_expected) << tok::r_paren;
2384 SkipUntil(tok::r_paren, StopAtSemi);
2385 } else
2386 ConsumeParen();
2387 } else {
2388 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
2389 SkipUntil(tok::r_paren, StopAtSemi);
2390 }
2391 }
2392 }
2393
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002394 while (true) {
2395 if (Tok.is(tok::code_completion)) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002396 Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002397 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002398 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002399 }
Alex Lorenzf1278212017-04-11 15:01:53 +00002400
2401 if (expectIdentifier()) {
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002402 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +00002403 return nullptr;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00002404 }
2405
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002406 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2407 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Manman Ren5b786402016-01-28 18:49:28 +00002408 Actions.ActOnPropertyImplDecl(
2409 getCurScope(), atLoc, propertyLoc, false,
2410 propertyId, nullptr, SourceLocation(),
Manman Ren0fe61f82016-01-29 19:05:57 +00002411 isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
Manman Ren5b786402016-01-28 18:49:28 +00002412 ObjCPropertyQueryKind::OBJC_PR_query_unknown);
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00002413
Chris Lattner0ef13522007-10-09 17:51:17 +00002414 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002415 break;
2416 ConsumeToken(); // consume ','
2417 }
Alp Toker383d2c42014-01-01 03:08:43 +00002418 ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
Craig Topper161e4db2014-05-21 06:02:52 +00002419 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002420}
Mike Stump11289f42009-09-09 15:08:12 +00002421
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002422/// objc-throw-statement:
2423/// throw expression[opt];
2424///
John McCalldadc5752010-08-24 06:29:42 +00002425StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2426 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002427 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00002428 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00002429 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002430 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002431 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002432 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002433 }
2434 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00002435 // consume ';'
Alp Toker383d2c42014-01-01 03:08:43 +00002436 ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002437 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002438}
2439
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002440/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002441/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002442///
John McCalldadc5752010-08-24 06:29:42 +00002443StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002444Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002445 ConsumeToken(); // consume synchronized
2446 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002447 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002448 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002449 }
John McCalld9bb7432011-07-27 21:50:02 +00002450
2451 // The operand is surrounded with parentheses.
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002452 ConsumeParen(); // '('
John McCalld9bb7432011-07-27 21:50:02 +00002453 ExprResult operand(ParseExpression());
2454
2455 if (Tok.is(tok::r_paren)) {
2456 ConsumeParen(); // ')'
2457 } else {
2458 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002459 Diag(Tok, diag::err_expected) << tok::r_paren;
John McCalld9bb7432011-07-27 21:50:02 +00002460
2461 // Skip forward until we see a left brace, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002462 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Fariborz Jahanian48085b82008-01-29 19:14:59 +00002463 }
John McCalld9bb7432011-07-27 21:50:02 +00002464
2465 // Require a compound statement.
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002466 if (Tok.isNot(tok::l_brace)) {
John McCalld9bb7432011-07-27 21:50:02 +00002467 if (!operand.isInvalid())
Alp Tokerec543272013-12-24 09:48:30 +00002468 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002469 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00002470 }
Steve Naroffd9c26072008-06-04 20:36:13 +00002471
John McCalld9bb7432011-07-27 21:50:02 +00002472 // Check the @synchronized operand now.
2473 if (!operand.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002474 operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002475
John McCalld9bb7432011-07-27 21:50:02 +00002476 // Parse the compound statement within a new scope.
2477 ParseScope bodyScope(this, Scope::DeclScope);
2478 StmtResult body(ParseCompoundStatementBody());
2479 bodyScope.Exit();
2480
2481 // If there was a semantic or parse error earlier with the
2482 // operand, fail now.
2483 if (operand.isInvalid())
2484 return StmtError();
2485
2486 if (body.isInvalid())
2487 body = Actions.ActOnNullStmt(Tok.getLocation());
2488
2489 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00002490}
2491
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002492/// objc-try-catch-statement:
2493/// @try compound-statement objc-catch-list[opt]
2494/// @try compound-statement objc-catch-list[opt] @finally compound-statement
2495///
2496/// objc-catch-list:
2497/// @catch ( parameter-declaration ) compound-statement
2498/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2499/// catch-parameter-declaration:
2500/// parameter-declaration
2501/// '...' [OBJC2]
2502///
John McCalldadc5752010-08-24 06:29:42 +00002503StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002504 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002505
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002506 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00002507 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002508 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002509 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002510 }
Benjamin Kramerf0623432012-08-23 22:51:59 +00002511 StmtVector CatchStmts;
John McCalldadc5752010-08-24 06:29:42 +00002512 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002513 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00002514 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002515 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002516 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002517 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00002518
Chris Lattner0ef13522007-10-09 17:51:17 +00002519 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00002520 // At this point, we need to lookahead to determine if this @ is the start
2521 // of an @catch or @finally. We don't want to consume the @ token if this
2522 // is an @try or @encode or something else.
2523 Token AfterAt = GetLookAheadToken(1);
2524 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2525 !AfterAt.isObjCAtKeyword(tok::objc_finally))
2526 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002527
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002528 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00002529 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002530 Decl *FirstPart = nullptr;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002531 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00002532 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002533 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00002534 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00002535 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002536 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002537 ParseDeclarationSpecifiers(DS);
Argyrios Kyrtzidis77450692011-07-01 22:22:40 +00002538 Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
Steve Naroff371b8fb2009-03-03 19:52:17 +00002539 ParseDeclarator(ParmDecl);
2540
Douglas Gregore11ee112010-04-23 23:01:43 +00002541 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00002542 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002543 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00002544 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002545 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00002546
Steve Naroff65a00892009-04-07 22:56:58 +00002547 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00002548
Steve Naroff65a00892009-04-07 22:56:58 +00002549 if (Tok.is(tok::r_paren))
2550 RParenLoc = ConsumeParen();
2551 else // Skip over garbage, until we get to ')'. Eat the ')'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002552 SkipUntil(tok::r_paren, StopAtSemi);
Steve Naroff65a00892009-04-07 22:56:58 +00002553
John McCalldadc5752010-08-24 06:29:42 +00002554 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002555 if (Tok.is(tok::l_brace))
2556 CatchBody = ParseCompoundStatementBody();
2557 else
Alp Tokerec543272013-12-24 09:48:30 +00002558 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002559 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00002560 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00002561
John McCalldadc5752010-08-24 06:29:42 +00002562 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00002563 RParenLoc,
2564 FirstPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002565 CatchBody.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002566 if (!Catch.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002567 CatchStmts.push_back(Catch.get());
Douglas Gregor96c79492010-04-23 22:50:49 +00002568
Steve Naroffe6016792008-02-05 21:27:35 +00002569 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00002570 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2571 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002572 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002573 }
2574 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00002575 } else {
2576 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00002577 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002578 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002579
John McCalldadc5752010-08-24 06:29:42 +00002580 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00002581 if (Tok.is(tok::l_brace))
2582 FinallyBody = ParseCompoundStatementBody();
2583 else
Alp Tokerec543272013-12-24 09:48:30 +00002584 Diag(Tok, diag::err_expected) << tok::l_brace;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002585 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00002586 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002587 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002588 FinallyBody.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002589 catch_or_finally_seen = true;
2590 break;
2591 }
2592 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002593 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002594 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002595 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00002596 }
Douglas Gregor96c79492010-04-23 22:50:49 +00002597
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002598 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002599 CatchStmts,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002600 FinallyStmt.get());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00002601}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002602
John McCall31168b02011-06-15 23:02:42 +00002603/// objc-autoreleasepool-statement:
2604/// @autoreleasepool compound-statement
2605///
2606StmtResult
2607Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2608 ConsumeToken(); // consume autoreleasepool
2609 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002610 Diag(Tok, diag::err_expected) << tok::l_brace;
John McCall31168b02011-06-15 23:02:42 +00002611 return StmtError();
2612 }
2613 // Enter a scope to hold everything within the compound stmt. Compound
2614 // statements can always hold declarations.
2615 ParseScope BodyScope(this, Scope::DeclScope);
2616
2617 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2618
2619 BodyScope.Exit();
2620 if (AutoreleasePoolBody.isInvalid())
2621 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2622 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002623 AutoreleasePoolBody.get());
John McCall31168b02011-06-15 23:02:42 +00002624}
2625
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002626/// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2627/// for later parsing.
2628void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002629 if (SkipFunctionBodies && (!MDecl || Actions.canSkipFunctionBody(MDecl)) &&
2630 trySkippingFunctionBody()) {
2631 Actions.ActOnSkippedFunctionBody(MDecl);
2632 return;
2633 }
2634
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002635 LexedMethod* LM = new LexedMethod(this, MDecl);
2636 CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2637 CachedTokens &Toks = LM->Toks;
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002638 // Begin by storing the '{' or 'try' or ':' token.
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002639 Toks.push_back(Tok);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002640 if (Tok.is(tok::kw_try)) {
2641 ConsumeToken();
Fariborz Jahanian053227f2012-08-10 20:34:17 +00002642 if (Tok.is(tok::colon)) {
2643 Toks.push_back(Tok);
2644 ConsumeToken();
2645 while (Tok.isNot(tok::l_brace)) {
2646 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2647 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2648 }
2649 }
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002650 Toks.push_back(Tok); // also store '{'
2651 }
2652 else if (Tok.is(tok::colon)) {
2653 ConsumeToken();
Richard Smithb9fa9962015-08-21 03:04:33 +00002654 // FIXME: This is wrong, due to C++11 braced initialization.
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00002655 while (Tok.isNot(tok::l_brace)) {
2656 ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2657 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2658 }
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002659 Toks.push_back(Tok); // also store '{'
2660 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002661 ConsumeBrace();
2662 // Consume everything up to (and including) the matching right brace.
2663 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00002664 while (Tok.is(tok::kw_catch)) {
2665 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2666 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2667 }
Fariborz Jahanian577574a2012-07-02 23:37:09 +00002668}
2669
Steve Naroff09bf8152007-09-06 21:24:23 +00002670/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002671///
John McCall48871652010-08-21 09:40:31 +00002672Decl *Parser::ParseObjCMethodDefinition() {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002673 Decl *MDecl = ParseObjCMethodPrototype();
Mike Stump11289f42009-09-09 15:08:12 +00002674
John McCallfaf5fb42010-08-26 23:41:50 +00002675 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2676 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00002677
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002678 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002679 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002680 if (CurParsedObjCImpl) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00002681 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00002682 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00002683 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002684 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00002685 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002686
Steve Naroffbb875722007-11-11 19:54:21 +00002687 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00002688 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00002689 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00002690
Steve Naroffbb875722007-11-11 19:54:21 +00002691 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002692 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Mike Stump11289f42009-09-09 15:08:12 +00002693
Steve Naroffbb875722007-11-11 19:54:21 +00002694 // If we didn't find the '{', bail out.
2695 if (Tok.isNot(tok::l_brace))
Craig Topper161e4db2014-05-21 06:02:52 +00002696 return nullptr;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00002697 }
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002698
2699 if (!MDecl) {
2700 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002701 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +00002702 return nullptr;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002703 }
2704
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00002705 // Allow the rest of sema to find private method decl implementations.
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002706 Actions.AddAnyMethodToGlobalPool(MDecl);
Fariborz Jahaniandb5743d2012-08-09 17:15:00 +00002707 assert (CurParsedObjCImpl
2708 && "ParseObjCMethodDefinition - Method out of @implementation");
2709 // Consume the tokens and store them for later parsing.
2710 StashAwayMethodOrFunctionBodyTokens(MDecl);
Steve Naroff7b8fa472007-11-13 23:01:27 +00002711 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002712}
Anders Carlsson76f4a902007-08-21 17:43:55 +00002713
John McCalldadc5752010-08-24 06:29:42 +00002714StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002715 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002716 Actions.CodeCompleteObjCAtStatement(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002717 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002718 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00002719 }
2720
2721 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00002722 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002723
2724 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00002725 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00002726
2727 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00002728 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00002729
2730 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2731 return ParseObjCAutoreleasePoolStmt(AtLoc);
Sean Callanan87596492014-12-09 23:47:56 +00002732
2733 if (Tok.isObjCAtKeyword(tok::objc_import) &&
2734 getLangOpts().DebuggerSupport) {
2735 SkipUntil(tok::semi);
2736 return Actions.ActOnNullStmt(Tok.getLocation());
2737 }
2738
Alex Lorenza589abc2016-12-01 12:14:38 +00002739 ExprStatementTokLoc = AtLoc;
John McCalldadc5752010-08-24 06:29:42 +00002740 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002741 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00002742 // If the expression is invalid, skip ahead to the next semicolon. Not
2743 // doing this opens us up to the possibility of infinite loops if
2744 // ParseExpression does not consume any tokens.
2745 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00002746 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00002747 }
Chris Lattner3ababf52009-12-07 16:33:19 +00002748
Steve Naroffe6016792008-02-05 21:27:35 +00002749 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00002750 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00002751 return Actions.ActOnExprStmt(Res);
Steve Naroffe6016792008-02-05 21:27:35 +00002752}
2753
John McCalldadc5752010-08-24 06:29:42 +00002754ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00002755 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002756 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002757 Actions.CodeCompleteObjCAtExpression(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002758 cutOffParsing();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002759 return ExprError();
2760
Ted Kremeneke65b0862012-03-06 20:05:56 +00002761 case tok::minus:
2762 case tok::plus: {
2763 tok::TokenKind Kind = Tok.getKind();
2764 SourceLocation OpLoc = ConsumeToken();
2765
2766 if (!Tok.is(tok::numeric_constant)) {
Craig Topper161e4db2014-05-21 06:02:52 +00002767 const char *Symbol = nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002768 switch (Kind) {
2769 case tok::minus: Symbol = "-"; break;
2770 case tok::plus: Symbol = "+"; break;
2771 default: llvm_unreachable("missing unary operator case");
2772 }
2773 Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2774 << Symbol;
2775 return ExprError();
2776 }
2777
2778 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2779 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002780 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002781 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00002782 ConsumeToken(); // Consume the literal token.
Ted Kremeneke65b0862012-03-06 20:05:56 +00002783
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002784 Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00002785 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002786 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002787
2788 return ParsePostfixExpressionSuffix(
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002789 Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002790 }
2791
Chris Lattnere002fbe2007-12-12 01:04:12 +00002792 case tok::string_literal: // primary-expression: string-literal
2793 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002794 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Ted Kremeneke65b0862012-03-06 20:05:56 +00002795
2796 case tok::char_constant:
2797 return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2798
2799 case tok::numeric_constant:
2800 return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2801
2802 case tok::kw_true: // Objective-C++, etc.
2803 case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2804 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2805 case tok::kw_false: // Objective-C++, etc.
2806 case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2807 return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2808
2809 case tok::l_square:
2810 // Objective-C array literal
2811 return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2812
2813 case tok::l_brace:
2814 // Objective-C dictionary literal
2815 return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2816
Patrick Beard0caa3942012-04-19 00:25:12 +00002817 case tok::l_paren:
2818 // Objective-C boxed expression
2819 return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2820
Chris Lattnere002fbe2007-12-12 01:04:12 +00002821 default:
Craig Topper161e4db2014-05-21 06:02:52 +00002822 if (Tok.getIdentifierInfo() == nullptr)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002823 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00002824
Chris Lattner197a3012008-08-05 06:19:09 +00002825 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2826 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002827 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002828 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002829 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00002830 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002831 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Erik Pilkington29099de2016-07-16 00:35:23 +00002832 case tok::objc_available:
2833 return ParseAvailabilityCheckExpr(AtLoc);
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002834 default: {
Craig Topper161e4db2014-05-21 06:02:52 +00002835 const char *str = nullptr;
Alex Lorenza589abc2016-12-01 12:14:38 +00002836 // Only provide the @try/@finally/@autoreleasepool fixit when we're sure
2837 // that this is a proper statement where such directives could actually
2838 // occur.
2839 if (GetLookAheadToken(1).is(tok::l_brace) &&
2840 ExprStatementTokLoc == AtLoc) {
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002841 char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2842 str =
2843 ch == 't' ? "try"
2844 : (ch == 'f' ? "finally"
Craig Topper161e4db2014-05-21 06:02:52 +00002845 : (ch == 'a' ? "autoreleasepool" : nullptr));
Fariborz Jahanian05d0d442012-07-09 20:00:35 +00002846 }
2847 if (str) {
2848 SourceLocation kwLoc = Tok.getLocation();
2849 return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2850 FixItHint::CreateReplacement(kwLoc, str));
2851 }
2852 else
2853 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2854 }
Chris Lattner197a3012008-08-05 06:19:09 +00002855 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002856 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00002857}
2858
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00002859/// \brief Parse the receiver of an Objective-C++ message send.
Douglas Gregor8d4de672010-04-21 22:36:40 +00002860///
2861/// This routine parses the receiver of a message send in
2862/// Objective-C++ either as a type or as an expression. Note that this
2863/// routine must not be called to parse a send to 'super', since it
2864/// has no way to return such a result.
2865///
2866/// \param IsExpr Whether the receiver was parsed as an expression.
2867///
2868/// \param TypeOrExpr If the receiver was parsed as an expression (\c
2869/// IsExpr is true), the parsed expression. If the receiver was parsed
2870/// as a type (\c IsExpr is false), the parsed type.
2871///
2872/// \returns True if an error occurred during parsing or semantic
2873/// analysis, in which case the arguments do not have valid
2874/// values. Otherwise, returns false for a successful parse.
2875///
2876/// objc-receiver: [C++]
2877/// 'super' [not parsed here]
2878/// expression
2879/// simple-type-specifier
2880/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00002881bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002882 InMessageExpressionRAIIObject InMessage(*this, true);
2883
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002884 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2885 tok::annot_cxxscope))
Douglas Gregor8d4de672010-04-21 22:36:40 +00002886 TryAnnotateTypeOrScopeToken();
2887
Kaelyn Uhrain237c7d32012-06-15 23:45:51 +00002888 if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00002889 // objc-receiver:
2890 // expression
Kaelyn Takatab16e6322014-11-20 22:06:40 +00002891 // Make sure any typos in the receiver are corrected or diagnosed, so that
2892 // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2893 // only the things that are valid ObjC receivers?
2894 ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002895 if (Receiver.isInvalid())
2896 return true;
2897
2898 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002899 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002900 return false;
2901 }
2902
2903 // objc-receiver:
2904 // typename-specifier
2905 // simple-type-specifier
2906 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00002907 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002908 ParseCXXSimpleTypeSpecifier(DS);
2909
2910 if (Tok.is(tok::l_paren)) {
2911 // If we see an opening parentheses at this point, we are
2912 // actually parsing an expression that starts with a
2913 // function-style cast, e.g.,
2914 //
2915 // postfix-expression:
2916 // simple-type-specifier ( expression-list [opt] )
2917 // typename-specifier ( expression-list [opt] )
2918 //
2919 // Parse the remainder of this case, then the (optional)
2920 // postfix-expression suffix, followed by the (optional)
2921 // right-hand side of the binary expression. We have an
2922 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00002923 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002924 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002925 Receiver = ParsePostfixExpressionSuffix(Receiver.get());
Douglas Gregor8d4de672010-04-21 22:36:40 +00002926 if (!Receiver.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002927 Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002928 if (Receiver.isInvalid())
2929 return true;
2930
2931 IsExpr = true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002932 TypeOrExpr = Receiver.get();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002933 return false;
2934 }
2935
2936 // We have a class message. Turn the simple-type-specifier or
2937 // typename-specifier we parsed into a type and parse the
2938 // remainder of the class message.
2939 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002940 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002941 if (Type.isInvalid())
2942 return true;
2943
2944 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00002945 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00002946 return false;
2947}
2948
Douglas Gregor990ccac2010-05-31 14:40:22 +00002949/// \brief Determine whether the parser is currently referring to a an
2950/// Objective-C message send, using a simplified heuristic to avoid overhead.
2951///
2952/// This routine will only return true for a subset of valid message-send
2953/// expressions.
2954bool Parser::isSimpleObjCMessageExpression() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002955 assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00002956 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00002957 return GetLookAheadToken(1).is(tok::identifier) &&
2958 GetLookAheadToken(2).is(tok::identifier);
2959}
2960
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002961bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002962 if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002963 InMessageExpression)
2964 return false;
2965
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002966 ParsedType Type;
2967
2968 if (Tok.is(tok::annot_typename))
2969 Type = getTypeAnnotation(Tok);
2970 else if (Tok.is(tok::identifier))
2971 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2972 getCurScope());
2973 else
2974 return false;
2975
2976 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
2977 const Token &AfterNext = GetLookAheadToken(2);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002978 if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002979 if (Tok.is(tok::identifier))
2980 TryAnnotateTypeOrScopeToken();
2981
2982 return Tok.is(tok::annot_typename);
2983 }
2984 }
2985
2986 return false;
2987}
2988
Mike Stump11289f42009-09-09 15:08:12 +00002989/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002990/// '[' objc-receiver objc-message-args ']'
2991///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002992/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00002993/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002994/// expression
2995/// class-name
2996/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00002997///
John McCalldadc5752010-08-24 06:29:42 +00002998ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00002999 assert(Tok.is(tok::l_square) && "'[' expected");
3000 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
3001
Douglas Gregora817a192010-05-27 23:06:34 +00003002 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003003 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003004 cutOffParsing();
Douglas Gregora817a192010-05-27 23:06:34 +00003005 return ExprError();
3006 }
3007
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003008 InMessageExpressionRAIIObject InMessage(*this, true);
3009
David Blaikiebbafb8a2012-03-11 07:00:24 +00003010 if (getLangOpts().CPlusPlus) {
Douglas Gregor8d4de672010-04-21 22:36:40 +00003011 // We completely separate the C and C++ cases because C++ requires
3012 // more complicated (read: slower) parsing.
3013
3014 // Handle send to super.
3015 // FIXME: This doesn't benefit from the same typo-correction we
3016 // get in Objective-C.
3017 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00003018 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
David Blaikieefdccaa2016-01-15 23:43:34 +00003019 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3020 nullptr);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003021
3022 // Parse the receiver, which is either a type or an expression.
3023 bool IsExpr;
Craig Topper161e4db2014-05-21 06:02:52 +00003024 void *TypeOrExpr = nullptr;
Douglas Gregor8d4de672010-04-21 22:36:40 +00003025 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003026 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor8d4de672010-04-21 22:36:40 +00003027 return ExprError();
3028 }
3029
3030 if (IsExpr)
David Blaikieefdccaa2016-01-15 23:43:34 +00003031 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3032 static_cast<Expr *>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00003033
3034 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00003035 ParsedType::getFromOpaquePtr(TypeOrExpr),
Craig Topper161e4db2014-05-21 06:02:52 +00003036 nullptr);
Chris Lattner47054fb2010-05-31 18:18:22 +00003037 }
3038
3039 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00003040 IdentifierInfo *Name = Tok.getIdentifierInfo();
3041 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00003042 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003043 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00003044 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00003045 NextToken().is(tok::period),
3046 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00003047 case Sema::ObjCSuperMessage:
David Blaikieefdccaa2016-01-15 23:43:34 +00003048 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3049 nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003050
John McCallfaf5fb42010-08-26 23:41:50 +00003051 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00003052 if (!ReceiverType) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003053 SkipUntil(tok::r_square, StopAtSemi);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003054 return ExprError();
3055 }
3056
Douglas Gregore5798dc2010-04-21 20:38:13 +00003057 ConsumeToken(); // the type name
3058
Douglas Gregore83b9562015-07-07 03:57:53 +00003059 // Parse type arguments and protocol qualifiers.
3060 if (Tok.is(tok::less)) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003061 SourceLocation NewEndLoc;
Douglas Gregore83b9562015-07-07 03:57:53 +00003062 TypeResult NewReceiverType
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003063 = parseObjCTypeArgsAndProtocolQualifiers(NameLoc, ReceiverType,
3064 /*consumeLastToken=*/true,
3065 NewEndLoc);
Douglas Gregore83b9562015-07-07 03:57:53 +00003066 if (!NewReceiverType.isUsable()) {
3067 SkipUntil(tok::r_square, StopAtSemi);
3068 return ExprError();
3069 }
3070
3071 ReceiverType = NewReceiverType.get();
3072 }
3073
Douglas Gregore5798dc2010-04-21 20:38:13 +00003074 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00003075 ReceiverType, nullptr);
3076
John McCallfaf5fb42010-08-26 23:41:50 +00003077 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003078 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00003079 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00003080 }
Chris Lattner8f697062008-01-25 18:59:06 +00003081 }
Chris Lattnera36ec422010-04-11 08:28:14 +00003082
3083 // Otherwise, an arbitrary expression can be the receiver of a send.
Kaelyn Takata15867822014-11-21 18:48:04 +00003084 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003085 if (Res.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003086 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003087 return Res;
Chris Lattner8f697062008-01-25 18:59:06 +00003088 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003089
David Blaikieefdccaa2016-01-15 23:43:34 +00003090 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3091 Res.get());
Chris Lattner8f697062008-01-25 18:59:06 +00003092}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003093
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003094/// \brief Parse the remainder of an Objective-C message following the
3095/// '[' objc-receiver.
3096///
3097/// This routine handles sends to super, class messages (sent to a
3098/// class name), and instance messages (sent to an object), and the
3099/// target is represented by \p SuperLoc, \p ReceiverType, or \p
3100/// ReceiverExpr, respectively. Only one of these parameters may have
3101/// a valid value.
3102///
3103/// \param LBracLoc The location of the opening '['.
3104///
3105/// \param SuperLoc If this is a send to 'super', the location of the
3106/// 'super' keyword that indicates a send to the superclass.
3107///
3108/// \param ReceiverType If this is a class message, the type of the
3109/// class we are sending a message to.
3110///
3111/// \param ReceiverExpr If this is an instance message, the expression
3112/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00003113///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003114/// objc-message-args:
3115/// objc-selector
3116/// objc-keywordarg-list
3117///
3118/// objc-keywordarg-list:
3119/// objc-keywordarg
3120/// objc-keywordarg-list objc-keywordarg
3121///
Mike Stump11289f42009-09-09 15:08:12 +00003122/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003123/// selector-name[opt] ':' objc-keywordexpr
3124///
3125/// objc-keywordexpr:
3126/// nonempty-expr-list
3127///
3128/// nonempty-expr-list:
3129/// assignment-expression
3130/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003131///
John McCalldadc5752010-08-24 06:29:42 +00003132ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00003133Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003134 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00003135 ParsedType ReceiverType,
Craig Toppera2c51532014-10-30 05:30:05 +00003136 Expr *ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00003137 InMessageExpressionRAIIObject InMessage(*this, true);
3138
Steve Naroffeae65032009-11-07 02:08:14 +00003139 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003140 if (SuperLoc.isValid())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003141 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003142 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003143 else if (ReceiverType)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003144 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003145 false);
Steve Naroffeae65032009-11-07 02:08:14 +00003146 else
John McCallb268a282010-08-23 23:25:46 +00003147 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003148 None, false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003149 cutOffParsing();
3150 return ExprError();
Steve Naroffeae65032009-11-07 02:08:14 +00003151 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00003152
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003153 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00003154 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003155 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003156
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003157 SmallVector<IdentifierInfo *, 12> KeyIdents;
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003158 SmallVector<SourceLocation, 12> KeyLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003159 ExprVector KeyExprs;
Steve Narofff73590d2007-09-27 14:38:14 +00003160
Chris Lattner0ef13522007-10-09 17:51:17 +00003161 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003162 while (1) {
3163 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00003164 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003165 KeyLocs.push_back(Loc);
Steve Naroff486760a2007-09-17 20:25:27 +00003166
Alp Toker383d2c42014-01-01 03:08:43 +00003167 if (ExpectAndConsume(tok::colon)) {
Chris Lattner197a3012008-08-05 06:19:09 +00003168 // We must manually skip to a ']', otherwise the expression skipper will
3169 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3170 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003171 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003172 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003173 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003174
Mike Stump11289f42009-09-09 15:08:12 +00003175 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003176
3177 if (Tok.is(tok::code_completion)) {
3178 if (SuperLoc.isValid())
3179 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003180 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003181 /*AtArgumentEpression=*/true);
3182 else if (ReceiverType)
3183 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003184 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003185 /*AtArgumentEpression=*/true);
3186 else
3187 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003188 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003189 /*AtArgumentEpression=*/true);
3190
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003191 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003192 return ExprError();
3193 }
3194
Fariborz Jahaniand5d6f3d2013-04-18 23:43:21 +00003195 ExprResult Expr;
3196 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3197 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3198 Expr = ParseBraceInitializer();
3199 } else
3200 Expr = ParseAssignmentExpression();
3201
3202 ExprResult Res(Expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003203 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00003204 // We must manually skip to a ']', otherwise the expression skipper will
3205 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3206 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003207 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003208 return Res;
Steve Naroff486760a2007-09-17 20:25:27 +00003209 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003210
Steve Naroff486760a2007-09-17 20:25:27 +00003211 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003212 KeyExprs.push_back(Res.get());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003213
Douglas Gregor1b605f72009-11-19 01:08:35 +00003214 // Code completion after each argument.
3215 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003216 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003217 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003218 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003219 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003220 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003221 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003222 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003223 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00003224 else
John McCallb268a282010-08-23 23:25:46 +00003225 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003226 KeyIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003227 /*AtArgumentEpression=*/false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003228 cutOffParsing();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003229 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00003230 }
3231
Steve Naroff486760a2007-09-17 20:25:27 +00003232 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00003233 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00003234 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003235 break;
3236 // We have a selector or a colon, continue parsing.
3237 }
3238 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00003239 while (Tok.is(tok::comma)) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003240 SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00003241 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00003242 ExprResult Res(ParseAssignmentExpression());
Kaelyn Takata15867822014-11-21 18:48:04 +00003243 if (Tok.is(tok::colon))
3244 Res = Actions.CorrectDelayedTyposInExpr(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003245 if (Res.isInvalid()) {
Fariborz Jahanian945b2f42012-05-21 22:43:44 +00003246 if (Tok.is(tok::colon)) {
3247 Diag(commaLoc, diag::note_extra_comma_message_arg) <<
3248 FixItHint::CreateRemoval(commaLoc);
3249 }
Chris Lattner197a3012008-08-05 06:19:09 +00003250 // We must manually skip to a ']', otherwise the expression skipper will
3251 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3252 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003253 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003254 return Res;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003255 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003256
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00003257 // We have a valid expression.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003258 KeyExprs.push_back(Res.get());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003259 }
3260 } else if (!selIdent) {
Alp Tokerec543272013-12-24 09:48:30 +00003261 Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003262
Chris Lattner197a3012008-08-05 06:19:09 +00003263 // We must manually skip to a ']', otherwise the expression skipper will
3264 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3265 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003266 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003267 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003268 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00003269
Chris Lattner0ef13522007-10-09 17:51:17 +00003270 if (Tok.isNot(tok::r_square)) {
Alp Toker35d87032013-12-30 23:29:50 +00003271 Diag(Tok, diag::err_expected)
3272 << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
Chris Lattner197a3012008-08-05 06:19:09 +00003273 // We must manually skip to a ']', otherwise the expression skipper will
3274 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3275 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003276 SkipUntil(tok::r_square, StopAtSemi);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003277 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00003278 }
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003279
Chris Lattner8f697062008-01-25 18:59:06 +00003280 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003281
Steve Naroffe61bfa82007-10-05 18:42:47 +00003282 unsigned nKeys = KeyIdents.size();
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003283 if (nKeys == 0) {
Chris Lattner5700fab2007-10-07 02:00:24 +00003284 KeyIdents.push_back(selIdent);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003285 KeyLocs.push_back(Loc);
3286 }
Chris Lattner5700fab2007-10-07 02:00:24 +00003287 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003288
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003289 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00003290 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003291 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00003292 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003293 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003294 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
John McCallb268a282010-08-23 23:25:46 +00003295 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Benjamin Kramerf0623432012-08-23 22:51:59 +00003296 LBracLoc, KeyLocs, RBracLoc, KeyExprs);
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00003297}
3298
John McCalldadc5752010-08-24 06:29:42 +00003299ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
3300 ExprResult Res(ParseStringLiteralExpression());
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003301 if (Res.isInvalid()) return Res;
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003302
Chris Lattnere002fbe2007-12-12 01:04:12 +00003303 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
3304 // expressions. At this point, we know that the only valid thing that starts
3305 // with '@' is an @"".
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003306 SmallVector<SourceLocation, 4> AtLocs;
Benjamin Kramerf0623432012-08-23 22:51:59 +00003307 ExprVector AtStrings;
Chris Lattnere002fbe2007-12-12 01:04:12 +00003308 AtLocs.push_back(AtLoc);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003309 AtStrings.push_back(Res.get());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003310
Chris Lattnere002fbe2007-12-12 01:04:12 +00003311 while (Tok.is(tok::at)) {
3312 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00003313
Sebastian Redlc13f2682008-12-09 20:22:58 +00003314 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00003315 if (!isTokenStringLiteral())
3316 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00003317
John McCalldadc5752010-08-24 06:29:42 +00003318 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003319 if (Lit.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003320 return Lit;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003321
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003322 AtStrings.push_back(Lit.get());
Chris Lattnere002fbe2007-12-12 01:04:12 +00003323 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003324
Craig Topper883dd332015-12-24 23:58:11 +00003325 return Actions.ParseObjCStringLiteral(AtLocs.data(), AtStrings);
Anders Carlsson76f4a902007-08-21 17:43:55 +00003326}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003327
Ted Kremeneke65b0862012-03-06 20:05:56 +00003328/// ParseObjCBooleanLiteral -
3329/// objc-scalar-literal : '@' boolean-keyword
3330/// ;
3331/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
3332/// ;
3333ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
3334 bool ArgValue) {
3335 SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
3336 return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
3337}
3338
3339/// ParseObjCCharacterLiteral -
3340/// objc-scalar-literal : '@' character-literal
3341/// ;
3342ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
3343 ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
3344 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003345 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003346 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003347 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003348 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003349}
3350
3351/// ParseObjCNumericLiteral -
3352/// objc-scalar-literal : '@' scalar-literal
3353/// ;
3354/// scalar-literal : | numeric-constant /* any numeric constant. */
3355/// ;
3356ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3357 ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3358 if (Lit.isInvalid()) {
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003359 return Lit;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003360 }
Benjamin Kramere6a4aff2012-03-07 00:14:40 +00003361 ConsumeToken(); // Consume the literal token.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003362 return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003363}
3364
Patrick Beard0caa3942012-04-19 00:25:12 +00003365/// ParseObjCBoxedExpr -
3366/// objc-box-expression:
3367/// @( assignment-expression )
3368ExprResult
3369Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3370 if (Tok.isNot(tok::l_paren))
3371 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3372
3373 BalancedDelimiterTracker T(*this, tok::l_paren);
3374 T.consumeOpen();
3375 ExprResult ValueExpr(ParseAssignmentExpression());
3376 if (T.consumeClose())
3377 return ExprError();
Argyrios Kyrtzidis9b4fe352012-05-10 20:02:36 +00003378
3379 if (ValueExpr.isInvalid())
3380 return ExprError();
3381
Patrick Beard0caa3942012-04-19 00:25:12 +00003382 // Wrap the sub-expression in a parenthesized expression, to distinguish
3383 // a boxed expression from a literal.
3384 SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003385 ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
Nico Webera7c7e602012-12-31 00:28:03 +00003386 return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003387 ValueExpr.get());
Patrick Beard0caa3942012-04-19 00:25:12 +00003388}
3389
Ted Kremeneke65b0862012-03-06 20:05:56 +00003390ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
Benjamin Kramerf0623432012-08-23 22:51:59 +00003391 ExprVector ElementExprs; // array elements.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003392 ConsumeBracket(); // consume the l_square.
3393
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003394 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003395 while (Tok.isNot(tok::r_square)) {
3396 // Parse list of array element expressions (all must be id types).
3397 ExprResult Res(ParseAssignmentExpression());
3398 if (Res.isInvalid()) {
3399 // We must manually skip to a ']', otherwise the expression skipper will
3400 // stop at the ']' when it skips to the ';'. We want it to skip beyond
3401 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003402 SkipUntil(tok::r_square, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003403 return Res;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003404 }
3405
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003406 Res = Actions.CorrectDelayedTyposInExpr(Res.get());
3407 if (Res.isInvalid())
3408 HasInvalidEltExpr = true;
3409
Ted Kremeneke65b0862012-03-06 20:05:56 +00003410 // Parse the ellipsis that indicates a pack expansion.
3411 if (Tok.is(tok::ellipsis))
3412 Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
3413 if (Res.isInvalid())
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003414 HasInvalidEltExpr = true;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003415
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003416 ElementExprs.push_back(Res.get());
Ted Kremeneke65b0862012-03-06 20:05:56 +00003417
3418 if (Tok.is(tok::comma))
3419 ConsumeToken(); // Eat the ','.
3420 else if (Tok.isNot(tok::r_square))
Alp Tokerec543272013-12-24 09:48:30 +00003421 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3422 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003423 }
3424 SourceLocation EndLoc = ConsumeBracket(); // location of ']'
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003425
3426 if (HasInvalidEltExpr)
3427 return ExprError();
3428
Benjamin Kramerf0623432012-08-23 22:51:59 +00003429 MultiExprArg Args(ElementExprs);
Nico Webera7c7e602012-12-31 00:28:03 +00003430 return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003431}
3432
3433ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3434 SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3435 ConsumeBrace(); // consume the l_square.
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003436 bool HasInvalidEltExpr = false;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003437 while (Tok.isNot(tok::r_brace)) {
3438 // Parse the comma separated key : value expressions.
3439 ExprResult KeyExpr;
3440 {
3441 ColonProtectionRAIIObject X(*this);
3442 KeyExpr = ParseAssignmentExpression();
3443 if (KeyExpr.isInvalid()) {
3444 // We must manually skip to a '}', otherwise the expression skipper will
3445 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3446 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003447 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003448 return KeyExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003449 }
3450 }
3451
Alp Toker383d2c42014-01-01 03:08:43 +00003452 if (ExpectAndConsume(tok::colon)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003453 SkipUntil(tok::r_brace, StopAtSemi);
Fariborz Jahanian507a5f82013-04-18 19:37:43 +00003454 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003455 }
3456
3457 ExprResult ValueExpr(ParseAssignmentExpression());
3458 if (ValueExpr.isInvalid()) {
3459 // We must manually skip to a '}', otherwise the expression skipper will
3460 // stop at the '}' when it skips to the ';'. We want it to skip beyond
3461 // the enclosing expression.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003462 SkipUntil(tok::r_brace, StopAtSemi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00003463 return ValueExpr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00003464 }
3465
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003466 // Check the key and value for possible typos
3467 KeyExpr = Actions.CorrectDelayedTyposInExpr(KeyExpr.get());
3468 ValueExpr = Actions.CorrectDelayedTyposInExpr(ValueExpr.get());
3469 if (KeyExpr.isInvalid() || ValueExpr.isInvalid())
3470 HasInvalidEltExpr = true;
3471
3472 // Parse the ellipsis that designates this as a pack expansion. Do not
3473 // ActOnPackExpansion here, leave it to template instantiation time where
3474 // we can get better diagnostics.
Ted Kremeneke65b0862012-03-06 20:05:56 +00003475 SourceLocation EllipsisLoc;
Alp Tokerec543272013-12-24 09:48:30 +00003476 if (getLangOpts().CPlusPlus)
3477 TryConsumeToken(tok::ellipsis, EllipsisLoc);
3478
Ted Kremeneke65b0862012-03-06 20:05:56 +00003479 // We have a valid expression. Collect it in a vector so we can
3480 // build the argument list.
3481 ObjCDictionaryElement Element = {
David Blaikie7a30dc52013-02-21 01:47:18 +00003482 KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
Ted Kremeneke65b0862012-03-06 20:05:56 +00003483 };
3484 Elements.push_back(Element);
Alp Toker383d2c42014-01-01 03:08:43 +00003485
3486 if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
Alp Tokerec543272013-12-24 09:48:30 +00003487 return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3488 << tok::comma);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003489 }
3490 SourceLocation EndLoc = ConsumeBrace();
Bruno Cardoso Lopes1383ddc2016-07-19 20:21:18 +00003491
3492 if (HasInvalidEltExpr)
3493 return ExprError();
Ted Kremeneke65b0862012-03-06 20:05:56 +00003494
3495 // Create the ObjCDictionaryLiteral.
Nico Webera7c7e602012-12-31 00:28:03 +00003496 return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
Craig Topperd4336e02015-12-24 23:58:15 +00003497 Elements);
Ted Kremeneke65b0862012-03-06 20:05:56 +00003498}
3499
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003500/// objc-encode-expression:
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +00003501/// \@encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00003502ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003503Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00003504 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003505
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003506 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003507
Chris Lattner197a3012008-08-05 06:19:09 +00003508 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003509 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3510
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003511 BalancedDelimiterTracker T(*this, tok::l_paren);
3512 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003513
Douglas Gregor220cac52009-02-18 17:45:20 +00003514 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003515
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003516 T.consumeClose();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003517
Douglas Gregor220cac52009-02-18 17:45:20 +00003518 if (Ty.isInvalid())
3519 return ExprError();
3520
Nico Webera7c7e602012-12-31 00:28:03 +00003521 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3522 Ty.get(), T.getCloseLocation());
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00003523}
Anders Carlssone01493d2007-08-23 15:25:28 +00003524
3525/// objc-protocol-expression
James Dennett1355bd12012-06-11 06:19:40 +00003526/// \@protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00003527ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003528Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00003529 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003530
Chris Lattner197a3012008-08-05 06:19:09 +00003531 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003532 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3533
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003534 BalancedDelimiterTracker T(*this, tok::l_paren);
3535 T.consumeOpen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003536
Alex Lorenzf1278212017-04-11 15:01:53 +00003537 if (expectIdentifier())
3538 return ExprError();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003539
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003540 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Argyrios Kyrtzidisb7e43672012-05-16 00:50:02 +00003541 SourceLocation ProtoIdLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003542
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003543 T.consumeClose();
Anders Carlssone01493d2007-08-23 15:25:28 +00003544
Nico Webera7c7e602012-12-31 00:28:03 +00003545 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3546 T.getOpenLocation(), ProtoIdLoc,
3547 T.getCloseLocation());
Anders Carlssone01493d2007-08-23 15:25:28 +00003548}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003549
3550/// objc-selector-expression
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003551/// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
Fariborz Jahanian02447d82013-01-22 18:35:43 +00003552ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003553 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003554
Chris Lattner197a3012008-08-05 06:19:09 +00003555 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003556 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3557
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003558 SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003559 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003560
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003561 BalancedDelimiterTracker T(*this, tok::l_paren);
3562 T.consumeOpen();
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003563 bool HasOptionalParen = Tok.is(tok::l_paren);
3564 if (HasOptionalParen)
3565 ConsumeParen();
3566
Douglas Gregor67c692c2010-08-26 15:07:07 +00003567 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003568 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003569 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003570 return ExprError();
3571 }
3572
Chris Lattner4f472a32009-04-11 18:13:45 +00003573 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00003574 if (!SelIdent && // missing selector name.
3575 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Alp Tokerec543272013-12-24 09:48:30 +00003576 return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00003577
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003578 KeyIdents.push_back(SelIdent);
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003579
Steve Naroff152dd812007-12-05 22:21:29 +00003580 unsigned nColons = 0;
3581 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003582 while (1) {
Alp Toker383d2c42014-01-01 03:08:43 +00003583 if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
Chris Lattner1ba64452010-08-27 22:32:41 +00003584 ++nColons;
Craig Topper161e4db2014-05-21 06:02:52 +00003585 KeyIdents.push_back(nullptr);
Alp Toker383d2c42014-01-01 03:08:43 +00003586 } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3587 return ExprError();
Chris Lattner1ba64452010-08-27 22:32:41 +00003588 ++nColons;
Alp Toker383d2c42014-01-01 03:08:43 +00003589
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003590 if (Tok.is(tok::r_paren))
3591 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00003592
3593 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003594 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003595 cutOffParsing();
Douglas Gregor67c692c2010-08-26 15:07:07 +00003596 return ExprError();
3597 }
3598
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003599 // Check for another keyword selector.
3600 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00003601 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003602 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00003603 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00003604 break;
3605 }
Steve Naroff152dd812007-12-05 22:21:29 +00003606 }
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003607 if (HasOptionalParen && Tok.is(tok::r_paren))
3608 ConsumeParen(); // ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003609 T.consumeClose();
Steve Naroff152dd812007-12-05 22:21:29 +00003610 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Nico Webera7c7e602012-12-31 00:28:03 +00003611 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3612 T.getOpenLocation(),
Fariborz Jahaniandacffc02014-06-24 17:02:19 +00003613 T.getCloseLocation(),
3614 !HasOptionalParen);
Eugene Zelenko1ced5092016-02-12 22:53:10 +00003615}
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003616
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003617void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3618 // MCDecl might be null due to error in method or c-function prototype, etc.
3619 Decl *MCDecl = LM.D;
3620 bool skip = MCDecl &&
3621 ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3622 (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3623 if (skip)
3624 return;
3625
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003626 // Save the current token position.
3627 SourceLocation OrigLoc = Tok.getLocation();
3628
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003629 assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
Alex Lorenz812012f2017-06-19 17:53:21 +00003630 // Store an artificial EOF token to ensure that we don't run off the end of
3631 // the method's body when we come to parse it.
3632 Token Eof;
3633 Eof.startToken();
3634 Eof.setKind(tok::eof);
3635 Eof.setEofData(MCDecl);
3636 Eof.setLocation(OrigLoc);
3637 LM.Toks.push_back(Eof);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003638 // Append the current token at the end of the new token stream so that it
3639 // doesn't get lost.
3640 LM.Toks.push_back(Tok);
David Blaikie2eabcc92016-02-09 18:52:09 +00003641 PP.EnterTokenStream(LM.Toks, true);
3642
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003643 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00003644 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003645
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003646 assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3647 "Inline objective-c method not starting with '{' or 'try' or ':'");
Alp Tokerf6a24ce2013-12-05 16:25:25 +00003648 // Enter a scope for the method or c-function body.
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003649 ParseScope BodyScope(this,
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003650 parseMethod
3651 ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
3652 : Scope::FnScope|Scope::DeclScope);
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003653
Fariborz Jahanian577574a2012-07-02 23:37:09 +00003654 // Tell the actions module that we have entered a method or c-function definition
3655 // with the specified Declarator for the method/function.
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +00003656 if (parseMethod)
3657 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3658 else
3659 Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
Fariborz Jahanian8cecfe92012-08-10 18:10:56 +00003660 if (Tok.is(tok::kw_try))
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003661 ParseFunctionTryBlock(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003662 else {
3663 if (Tok.is(tok::colon))
3664 ParseConstructorInitializer(MCDecl);
Akira Hatanakabd59b4892016-04-18 18:19:45 +00003665 else
3666 Actions.ActOnDefaultCtorInitializers(MCDecl);
Arnaud A. de Grandmaison6756a492014-03-23 20:28:07 +00003667 ParseFunctionStatementBody(MCDecl, BodyScope);
Fariborz Jahanianf64b4722012-08-10 21:15:06 +00003668 }
Alex Lorenz812012f2017-06-19 17:53:21 +00003669
Argyrios Kyrtzidis9a174fb2011-12-17 04:13:18 +00003670 if (Tok.getLocation() != OrigLoc) {
3671 // Due to parsing error, we either went over the cached tokens or
3672 // there are still cached tokens left. If it's the latter case skip the
3673 // leftover tokens.
3674 // Since this is an uncommon situation that should be avoided, use the
3675 // expensive isBeforeInTranslationUnit call.
3676 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3677 OrigLoc))
3678 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3679 ConsumeAnyToken();
3680 }
Alex Lorenz812012f2017-06-19 17:53:21 +00003681 // Clean up the remaining EOF token.
3682 ConsumeAnyToken();
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +00003683}